|
1 | 1 | import type { WorkflowTemplate } from './types.js'; |
2 | 2 |
|
3 | | -export function isWorkflowTemplate(value: unknown): value is WorkflowTemplate { |
4 | | - if (!value || typeof value !== 'object') return false; |
5 | | - const obj = value as { name?: unknown; steps?: unknown }; |
6 | | - if (typeof obj.name !== 'string' || obj.name.trim().length === 0) return false; |
7 | | - if (!Array.isArray(obj.steps)) return false; |
8 | | - return obj.steps.every((step) => { |
9 | | - if (!step || typeof step !== 'object') return false; |
10 | | - const candidate = step as { |
11 | | - type?: unknown; |
12 | | - agentId?: unknown; |
13 | | - agentName?: unknown; |
14 | | - promptPath?: unknown; |
15 | | - model?: unknown; |
16 | | - modelReasoningEffort?: unknown; |
17 | | - module?: unknown; |
18 | | - executeOnce?: unknown; |
19 | | - }; |
20 | | - if ( |
21 | | - candidate.type !== 'module' || |
22 | | - typeof candidate.agentId !== 'string' || |
23 | | - typeof candidate.agentName !== 'string' || |
24 | | - typeof candidate.promptPath !== 'string' |
25 | | - ) { |
26 | | - return false; |
27 | | - } |
28 | | - |
29 | | - if (candidate.model !== undefined && typeof candidate.model !== 'string') { |
30 | | - return false; |
31 | | - } |
32 | | - |
33 | | - if ( |
34 | | - candidate.modelReasoningEffort !== undefined && |
35 | | - candidate.modelReasoningEffort !== 'low' && |
36 | | - candidate.modelReasoningEffort !== 'medium' && |
37 | | - candidate.modelReasoningEffort !== 'high' |
38 | | - ) { |
39 | | - return false; |
40 | | - } |
41 | | - |
42 | | - if (candidate.executeOnce !== undefined && typeof candidate.executeOnce !== 'boolean') { |
43 | | - return false; |
44 | | - } |
45 | | - |
46 | | - if (candidate.module === undefined) { |
47 | | - return true; |
48 | | - } |
49 | | - |
50 | | - if (!candidate.module || typeof candidate.module !== 'object') { |
51 | | - return false; |
52 | | - } |
53 | | - |
54 | | - const moduleMeta = candidate.module as { |
55 | | - id?: unknown; |
56 | | - behavior?: unknown; |
57 | | - }; |
58 | | - |
59 | | - if (typeof moduleMeta.id !== 'string') { |
60 | | - return false; |
61 | | - } |
62 | | - |
63 | | - if (moduleMeta.behavior === undefined) { |
64 | | - return true; |
65 | | - } |
66 | | - |
67 | | - if (!moduleMeta.behavior || typeof moduleMeta.behavior !== 'object') { |
68 | | - return false; |
69 | | - } |
70 | | - |
71 | | - const behavior = moduleMeta.behavior as { |
72 | | - type?: unknown; |
73 | | - action?: unknown; |
74 | | - steps?: unknown; |
75 | | - trigger?: unknown; |
76 | | - maxIterations?: unknown; |
77 | | - }; |
78 | | - |
79 | | - if (behavior.type !== 'loop' || behavior.action !== 'stepBack') { |
80 | | - return false; |
81 | | - } |
82 | | - |
83 | | - if (typeof behavior.steps !== 'number' || behavior.steps <= 0) { |
84 | | - return false; |
85 | | - } |
| 3 | +export interface ValidationResult { |
| 4 | + valid: boolean; |
| 5 | + errors: string[]; |
| 6 | +} |
86 | 7 |
|
87 | | - if (typeof behavior.trigger !== 'string') { |
88 | | - return false; |
89 | | - } |
| 8 | +export function validateWorkflowTemplate(value: unknown): ValidationResult { |
| 9 | + const errors: string[] = []; |
| 10 | + if (!value || typeof value !== 'object') { |
| 11 | + return { valid: false, errors: ['Template is not an object'] }; |
| 12 | + } |
90 | 13 |
|
91 | | - if (behavior.maxIterations !== undefined && typeof behavior.maxIterations !== 'number') { |
92 | | - return false; |
93 | | - } |
| 14 | + const obj = value as { name?: unknown; steps?: unknown }; |
| 15 | + if (typeof obj.name !== 'string' || obj.name.trim().length === 0) { |
| 16 | + errors.push('Template.name must be a non-empty string'); |
| 17 | + } |
| 18 | + if (!Array.isArray(obj.steps)) { |
| 19 | + errors.push('Template.steps must be an array'); |
| 20 | + } else { |
| 21 | + obj.steps.forEach((step, index) => { |
| 22 | + if (!step || typeof step !== 'object') { |
| 23 | + errors.push(`Step[${index}] must be an object`); |
| 24 | + return; |
| 25 | + } |
| 26 | + const candidate = step as { |
| 27 | + type?: unknown; |
| 28 | + agentId?: unknown; |
| 29 | + agentName?: unknown; |
| 30 | + promptPath?: unknown; |
| 31 | + model?: unknown; |
| 32 | + modelReasoningEffort?: unknown; |
| 33 | + module?: unknown; |
| 34 | + executeOnce?: unknown; |
| 35 | + }; |
| 36 | + |
| 37 | + if (candidate.type !== 'module') { |
| 38 | + errors.push(`Step[${index}].type must be 'module'`); |
| 39 | + } |
| 40 | + if (typeof candidate.agentId !== 'string') { |
| 41 | + errors.push(`Step[${index}].agentId must be a string`); |
| 42 | + } |
| 43 | + if (typeof candidate.agentName !== 'string') { |
| 44 | + errors.push(`Step[${index}].agentName must be a string`); |
| 45 | + } |
| 46 | + if (typeof candidate.promptPath !== 'string') { |
| 47 | + errors.push(`Step[${index}].promptPath must be a string`); |
| 48 | + } |
| 49 | + |
| 50 | + if (candidate.model !== undefined && typeof candidate.model !== 'string') { |
| 51 | + errors.push(`Step[${index}].model must be a string`); |
| 52 | + } |
| 53 | + |
| 54 | + if (candidate.modelReasoningEffort !== undefined) { |
| 55 | + const mre = candidate.modelReasoningEffort; |
| 56 | + if (mre !== 'low' && mre !== 'medium' && mre !== 'high') { |
| 57 | + errors.push( |
| 58 | + `Step[${index}].modelReasoningEffort must be one of 'low'|'medium'|'high' (got '${String(mre)}')`, |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (candidate.executeOnce !== undefined && typeof candidate.executeOnce !== 'boolean') { |
| 64 | + errors.push(`Step[${index}].executeOnce must be a boolean`); |
| 65 | + } |
| 66 | + |
| 67 | + if (candidate.module !== undefined) { |
| 68 | + if (!candidate.module || typeof candidate.module !== 'object') { |
| 69 | + errors.push(`Step[${index}].module must be an object`); |
| 70 | + } else { |
| 71 | + const moduleMeta = candidate.module as { id?: unknown; behavior?: unknown }; |
| 72 | + if (typeof moduleMeta.id !== 'string') { |
| 73 | + errors.push(`Step[${index}].module.id must be a string`); |
| 74 | + } |
| 75 | + if (moduleMeta.behavior !== undefined) { |
| 76 | + if (!moduleMeta.behavior || typeof moduleMeta.behavior !== 'object') { |
| 77 | + errors.push(`Step[${index}].module.behavior must be an object`); |
| 78 | + } else { |
| 79 | + const behavior = moduleMeta.behavior as { |
| 80 | + type?: unknown; |
| 81 | + action?: unknown; |
| 82 | + steps?: unknown; |
| 83 | + trigger?: unknown; |
| 84 | + maxIterations?: unknown; |
| 85 | + }; |
| 86 | + if (behavior.type !== 'loop' || behavior.action !== 'stepBack') { |
| 87 | + errors.push(`Step[${index}].module.behavior must be { type: 'loop', action: 'stepBack', ... }`); |
| 88 | + } |
| 89 | + if (typeof behavior.steps !== 'number' || behavior.steps <= 0) { |
| 90 | + errors.push(`Step[${index}].module.behavior.steps must be a positive number`); |
| 91 | + } |
| 92 | + if (typeof behavior.trigger !== 'string') { |
| 93 | + errors.push(`Step[${index}].module.behavior.trigger must be a string`); |
| 94 | + } |
| 95 | + if (behavior.maxIterations !== undefined && typeof behavior.maxIterations !== 'number') { |
| 96 | + errors.push(`Step[${index}].module.behavior.maxIterations must be a number`); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + return { valid: errors.length === 0, errors }; |
| 106 | +} |
94 | 107 |
|
95 | | - return true; |
96 | | - }); |
| 108 | +export function isWorkflowTemplate(value: unknown): value is WorkflowTemplate { |
| 109 | + return validateWorkflowTemplate(value).valid; |
97 | 110 | } |
0 commit comments