2025-10-02 14:40:30 +00:00
|
|
|
import type { INodeTypeDescription } from 'n8n-workflow';
|
|
|
|
|
|
2025-11-24 14:25:12 +00:00
|
|
|
import type { ProgrammaticEvaluationInput, ProgrammaticViolation } from '@/validation/types';
|
2025-10-24 12:17:43 +00:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
evaluateConnections,
|
2025-12-10 10:35:54 +00:00
|
|
|
evaluateCredentials,
|
2025-11-26 16:34:34 +00:00
|
|
|
evaluateNodes,
|
2025-10-24 12:17:43 +00:00
|
|
|
evaluateTools,
|
|
|
|
|
evaluateAgentPrompt,
|
|
|
|
|
evaluateFromAi,
|
|
|
|
|
evaluateTrigger,
|
|
|
|
|
} from './evaluators';
|
2025-11-24 14:25:12 +00:00
|
|
|
import {
|
|
|
|
|
evaluateWorkflowSimilarity,
|
|
|
|
|
evaluateWorkflowSimilarityMultiple,
|
|
|
|
|
} from './evaluators/workflow-similarity';
|
2026-01-13 12:11:13 +00:00
|
|
|
import { calculateOverallScore } from './score';
|
2025-10-02 14:40:30 +00:00
|
|
|
|
2025-11-24 14:25:12 +00:00
|
|
|
export async function programmaticEvaluation(
|
2025-10-24 12:17:43 +00:00
|
|
|
input: ProgrammaticEvaluationInput,
|
2025-10-02 14:40:30 +00:00
|
|
|
nodeTypes: INodeTypeDescription[],
|
2025-10-24 12:17:43 +00:00
|
|
|
) {
|
2026-01-13 12:11:13 +00:00
|
|
|
const { generatedWorkflow, referenceWorkflows, preset = 'standard' } = input;
|
2025-10-02 14:40:30 +00:00
|
|
|
|
|
|
|
|
const connectionsEvaluationResult = evaluateConnections(generatedWorkflow, nodeTypes);
|
2025-11-26 16:34:34 +00:00
|
|
|
const nodesEvaluationResult = evaluateNodes(generatedWorkflow, nodeTypes);
|
2025-10-02 14:40:30 +00:00
|
|
|
const triggerEvaluationResult = evaluateTrigger(generatedWorkflow, nodeTypes);
|
|
|
|
|
const agentPromptEvaluationResult = evaluateAgentPrompt(generatedWorkflow);
|
|
|
|
|
const toolsEvaluationResult = evaluateTools(generatedWorkflow, nodeTypes);
|
|
|
|
|
const fromAiEvaluationResult = evaluateFromAi(generatedWorkflow, nodeTypes);
|
2025-12-10 10:35:54 +00:00
|
|
|
const credentialsEvaluationResult = evaluateCredentials(generatedWorkflow);
|
2025-10-02 14:40:30 +00:00
|
|
|
|
2026-01-13 12:11:13 +00:00
|
|
|
// Workflow similarity evaluation
|
2025-11-24 14:25:12 +00:00
|
|
|
let similarityEvaluationResult = null;
|
|
|
|
|
|
|
|
|
|
if (referenceWorkflows && referenceWorkflows.length > 0) {
|
|
|
|
|
try {
|
2026-01-13 12:11:13 +00:00
|
|
|
if (referenceWorkflows.length === 1) {
|
|
|
|
|
similarityEvaluationResult = await evaluateWorkflowSimilarity(
|
|
|
|
|
generatedWorkflow,
|
|
|
|
|
referenceWorkflows[0],
|
|
|
|
|
preset,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
similarityEvaluationResult = await evaluateWorkflowSimilarityMultiple(
|
|
|
|
|
generatedWorkflow,
|
|
|
|
|
referenceWorkflows,
|
|
|
|
|
preset,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-24 14:25:12 +00:00
|
|
|
} catch (error) {
|
2026-01-13 12:11:13 +00:00
|
|
|
// Fallback to neutral result if similarity check fails - error captured in violation
|
2025-11-24 14:25:12 +00:00
|
|
|
const violation: ProgrammaticViolation = {
|
|
|
|
|
name: 'workflow-similarity-evaluation-failed',
|
|
|
|
|
type: 'critical',
|
|
|
|
|
description: `Similarity evaluation failed: ${(error as Error).message}`,
|
|
|
|
|
pointsDeducted: 0,
|
|
|
|
|
};
|
|
|
|
|
similarityEvaluationResult = {
|
|
|
|
|
violations: [violation],
|
|
|
|
|
score: 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 14:40:30 +00:00
|
|
|
const overallScore = calculateOverallScore({
|
|
|
|
|
connections: connectionsEvaluationResult,
|
2025-11-26 16:34:34 +00:00
|
|
|
nodes: nodesEvaluationResult,
|
2025-10-02 14:40:30 +00:00
|
|
|
trigger: triggerEvaluationResult,
|
|
|
|
|
agentPrompt: agentPromptEvaluationResult,
|
|
|
|
|
tools: toolsEvaluationResult,
|
|
|
|
|
fromAi: fromAiEvaluationResult,
|
2025-12-10 10:35:54 +00:00
|
|
|
credentials: credentialsEvaluationResult,
|
2025-11-24 14:25:12 +00:00
|
|
|
similarity: similarityEvaluationResult,
|
2025-10-02 14:40:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
overallScore,
|
|
|
|
|
connections: connectionsEvaluationResult,
|
2025-11-26 16:34:34 +00:00
|
|
|
nodes: nodesEvaluationResult,
|
2025-10-02 14:40:30 +00:00
|
|
|
trigger: triggerEvaluationResult,
|
|
|
|
|
agentPrompt: agentPromptEvaluationResult,
|
|
|
|
|
tools: toolsEvaluationResult,
|
|
|
|
|
fromAi: fromAiEvaluationResult,
|
2025-12-10 10:35:54 +00:00
|
|
|
credentials: credentialsEvaluationResult,
|
2025-11-24 14:25:12 +00:00
|
|
|
similarity: similarityEvaluationResult,
|
2025-10-02 14:40:30 +00:00
|
|
|
};
|
|
|
|
|
}
|