voyageai-cli 1.28.0 → 1.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -8
- package/package.json +2 -1
- package/src/commands/app.js +15 -0
- package/src/commands/benchmark.js +22 -8
- package/src/commands/chat.js +18 -0
- package/src/commands/chunk.js +10 -0
- package/src/commands/demo.js +4 -0
- package/src/commands/embed.js +13 -0
- package/src/commands/estimate.js +3 -0
- package/src/commands/eval.js +6 -0
- package/src/commands/explain.js +2 -0
- package/src/commands/generate.js +2 -0
- package/src/commands/ingest.js +4 -0
- package/src/commands/init.js +2 -0
- package/src/commands/mcp-server.js +2 -0
- package/src/commands/models.js +2 -0
- package/src/commands/ping.js +7 -0
- package/src/commands/pipeline.js +15 -0
- package/src/commands/playground.js +685 -8
- package/src/commands/query.js +16 -0
- package/src/commands/rerank.js +12 -0
- package/src/commands/scaffold.js +2 -0
- package/src/commands/search.js +11 -0
- package/src/commands/similarity.js +9 -0
- package/src/commands/store.js +4 -0
- package/src/commands/workflow.js +702 -13
- package/src/lib/capability-report.js +134 -0
- package/src/lib/chat.js +32 -1
- package/src/lib/config.js +2 -0
- package/src/lib/cost-display.js +107 -0
- package/src/lib/explanations.js +94 -0
- package/src/lib/llm.js +125 -18
- package/src/lib/npm-utils.js +265 -0
- package/src/lib/quality-audit.js +71 -0
- package/src/lib/security/blocked-domains.json +17 -0
- package/src/lib/security-audit.js +198 -0
- package/src/lib/telemetry.js +23 -1
- package/src/lib/workflow-registry.js +416 -0
- package/src/lib/workflow-scaffold.js +380 -0
- package/src/lib/workflow-test-runner.js +208 -0
- package/src/lib/workflow.js +559 -7
- package/src/playground/announcements.md +80 -0
- package/src/playground/assets/announcements/appstore.jpg +0 -0
- package/src/playground/assets/announcements/circuits.jpg +0 -0
- package/src/playground/assets/announcements/csvingest.jpg +0 -0
- package/src/playground/assets/announcements/green-wave.jpg +0 -0
- package/src/playground/help/workflow-nodes.js +472 -0
- package/src/playground/icons/V.png +0 -0
- package/src/playground/index.html +3634 -226
- package/src/workflows/consistency-check.json +4 -0
- package/src/workflows/cost-analysis.json +4 -0
- package/src/workflows/enrich-and-ingest.json +56 -0
- package/src/workflows/intelligent-ingest.json +66 -0
- package/src/workflows/kb-health-report.json +45 -0
- package/src/workflows/multi-collection-search.json +4 -0
- package/src/workflows/research-and-summarize.json +4 -0
- package/src/workflows/search-with-fallback.json +66 -0
- package/src/workflows/smart-ingest.json +4 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const { WORKFLOW_PREFIX, VAICLI_WORKFLOW_PREFIX } = require('./npm-utils');
|
|
6
|
+
const { validateWorkflow, ALL_TOOLS } = require('./workflow');
|
|
7
|
+
|
|
8
|
+
const CATEGORIES = ['retrieval', 'analysis', 'ingestion', 'domain-specific', 'utility', 'integration'];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Predefined Lucide icon names available for workflow branding.
|
|
12
|
+
* These map to SVG paths served by the playground/store.
|
|
13
|
+
*/
|
|
14
|
+
const BRANDING_ICONS = [
|
|
15
|
+
'trophy', 'search', 'dollar-sign', 'split', 'file-search', 'database',
|
|
16
|
+
'activity', 'globe', 'shield-alert', 'timer', 'refresh-cw', 'flask-conical',
|
|
17
|
+
'target', 'code', 'clipboard-list', 'layers', 'bar-chart-3', 'heart-pulse',
|
|
18
|
+
'brain', 'check-circle', 'zap', 'package', 'microscope', 'sparkle',
|
|
19
|
+
'scale', 'file-text', 'filter',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default branding colors per category.
|
|
24
|
+
*/
|
|
25
|
+
const CATEGORY_COLORS = {
|
|
26
|
+
retrieval: '#00D4AA',
|
|
27
|
+
analysis: '#8B5CF6',
|
|
28
|
+
ingestion: '#059669',
|
|
29
|
+
'domain-specific': '#1E40AF',
|
|
30
|
+
utility: '#0D9488',
|
|
31
|
+
integration: '#0EA5E9',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Suggest a branding icon based on category and tools.
|
|
36
|
+
* @param {string} category
|
|
37
|
+
* @param {string[]} tools
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
function suggestBrandingIcon(category, tools) {
|
|
41
|
+
const t = new Set(tools);
|
|
42
|
+
if (t.has('estimate') && t.has('similarity')) return 'trophy';
|
|
43
|
+
if (t.has('ingest')) return 'database';
|
|
44
|
+
if (t.has('generate') && t.has('query')) return 'sparkle';
|
|
45
|
+
if (t.has('rerank') && t.has('search')) return 'target';
|
|
46
|
+
if (t.has('search')) return 'search';
|
|
47
|
+
if (t.has('similarity')) return 'scale';
|
|
48
|
+
if (t.has('generate')) return 'brain';
|
|
49
|
+
if (t.has('http')) return 'globe';
|
|
50
|
+
const catMap = {
|
|
51
|
+
retrieval: 'search', analysis: 'bar-chart-3', ingestion: 'database',
|
|
52
|
+
'domain-specific': 'target', utility: 'zap', integration: 'package',
|
|
53
|
+
};
|
|
54
|
+
return catMap[category] || 'zap';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Guess a category based on the tools a workflow uses.
|
|
59
|
+
* @param {string[]} tools
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
function guessCategory(tools) {
|
|
63
|
+
const t = new Set(tools);
|
|
64
|
+
if (t.has('ingest') || t.has('chunk')) return 'ingestion';
|
|
65
|
+
if (t.has('generate') && (t.has('query') || t.has('search'))) return 'retrieval';
|
|
66
|
+
if (t.has('similarity') || t.has('aggregate')) return 'analysis';
|
|
67
|
+
if (t.has('http')) return 'integration';
|
|
68
|
+
if (t.has('query') || t.has('search') || t.has('rerank')) return 'retrieval';
|
|
69
|
+
return 'utility';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Extract tool names from a workflow definition's steps.
|
|
74
|
+
* @param {object} definition
|
|
75
|
+
* @returns {string[]}
|
|
76
|
+
*/
|
|
77
|
+
function extractTools(definition) {
|
|
78
|
+
const tools = new Set();
|
|
79
|
+
if (!definition.steps) return [];
|
|
80
|
+
for (const step of definition.steps) {
|
|
81
|
+
if (step.tool && ALL_TOOLS.has(step.tool)) {
|
|
82
|
+
tools.add(step.tool);
|
|
83
|
+
}
|
|
84
|
+
// Check loop inline steps
|
|
85
|
+
if (step.tool === 'loop' && step.inputs?.step?.tool) {
|
|
86
|
+
tools.add(step.inputs.step.tool);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return [...tools];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Generate a package name from a workflow name.
|
|
94
|
+
* @param {string} name
|
|
95
|
+
* @param {{ scope?: string }} [options]
|
|
96
|
+
* @returns {string}
|
|
97
|
+
*/
|
|
98
|
+
function toPackageName(name, options = {}) {
|
|
99
|
+
const slug = name
|
|
100
|
+
.toLowerCase()
|
|
101
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
102
|
+
.replace(/^-|-$/g, '');
|
|
103
|
+
const base = slug.startsWith(WORKFLOW_PREFIX.slice(0, -1))
|
|
104
|
+
? slug
|
|
105
|
+
: WORKFLOW_PREFIX + slug;
|
|
106
|
+
if (options.scope === 'vaicli') {
|
|
107
|
+
return `@vaicli/${base}`;
|
|
108
|
+
}
|
|
109
|
+
return base;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Generate a README.md from workflow definition and package metadata.
|
|
114
|
+
* @param {object} pkg - package.json content
|
|
115
|
+
* @param {object} definition - workflow definition
|
|
116
|
+
* @returns {string}
|
|
117
|
+
*/
|
|
118
|
+
function generateReadme(pkg, definition) {
|
|
119
|
+
const lines = [];
|
|
120
|
+
lines.push(`# ${pkg.name}`);
|
|
121
|
+
lines.push('');
|
|
122
|
+
lines.push(pkg.description || definition.description || 'A vai community workflow.');
|
|
123
|
+
lines.push('');
|
|
124
|
+
|
|
125
|
+
// Prerequisites
|
|
126
|
+
lines.push('## Prerequisites');
|
|
127
|
+
lines.push('');
|
|
128
|
+
lines.push('- [voyageai-cli](https://github.com/mrlynn/voyageai-cli) installed');
|
|
129
|
+
const tools = pkg.vai?.tools || [];
|
|
130
|
+
if (tools.includes('query') || tools.includes('search') || tools.includes('ingest')) {
|
|
131
|
+
lines.push('- A MongoDB collection with embedded documents');
|
|
132
|
+
}
|
|
133
|
+
if (tools.includes('generate')) {
|
|
134
|
+
lines.push('- An LLM provider configured (`vai config set llm-provider ...`)');
|
|
135
|
+
}
|
|
136
|
+
if (tools.includes('http')) {
|
|
137
|
+
lines.push('- Network access for external HTTP requests');
|
|
138
|
+
}
|
|
139
|
+
lines.push('');
|
|
140
|
+
|
|
141
|
+
// Inputs
|
|
142
|
+
const inputs = definition.inputs;
|
|
143
|
+
if (inputs && Object.keys(inputs).length > 0) {
|
|
144
|
+
lines.push('## Inputs');
|
|
145
|
+
lines.push('');
|
|
146
|
+
lines.push('| Input | Type | Required | Default | Description |');
|
|
147
|
+
lines.push('|-------|------|----------|---------|-------------|');
|
|
148
|
+
for (const [key, schema] of Object.entries(inputs)) {
|
|
149
|
+
const type = schema.type || 'string';
|
|
150
|
+
const req = schema.required ? 'Yes' : 'No';
|
|
151
|
+
const def = schema.default !== undefined ? String(schema.default) : '—';
|
|
152
|
+
const desc = schema.description || '';
|
|
153
|
+
lines.push(`| ${key} | ${type} | ${req} | ${def} | ${desc} |`);
|
|
154
|
+
}
|
|
155
|
+
lines.push('');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Usage
|
|
159
|
+
lines.push('## Usage');
|
|
160
|
+
lines.push('');
|
|
161
|
+
lines.push('```bash');
|
|
162
|
+
const inputFlags = inputs
|
|
163
|
+
? Object.entries(inputs)
|
|
164
|
+
.filter(([, s]) => s.required)
|
|
165
|
+
.map(([k]) => `--input ${k}="..."`)
|
|
166
|
+
.join(' \\\n ')
|
|
167
|
+
: '';
|
|
168
|
+
lines.push(`vai workflow run ${pkg.name}${inputFlags ? ' \\\n ' + inputFlags : ''}`);
|
|
169
|
+
lines.push('```');
|
|
170
|
+
lines.push('');
|
|
171
|
+
|
|
172
|
+
// Steps
|
|
173
|
+
if (definition.steps?.length) {
|
|
174
|
+
lines.push('## Steps');
|
|
175
|
+
lines.push('');
|
|
176
|
+
for (let i = 0; i < definition.steps.length; i++) {
|
|
177
|
+
const step = definition.steps[i];
|
|
178
|
+
lines.push(`${i + 1}. **${step.name || step.id}** — \`${step.tool}\``);
|
|
179
|
+
}
|
|
180
|
+
lines.push('');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
lines.push('## License');
|
|
184
|
+
lines.push('');
|
|
185
|
+
lines.push(pkg.license || 'MIT');
|
|
186
|
+
lines.push('');
|
|
187
|
+
|
|
188
|
+
return lines.join('\n');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Scaffold a publish-ready npm package from a workflow definition.
|
|
193
|
+
*
|
|
194
|
+
* @param {object} options
|
|
195
|
+
* @param {object} options.definition - Workflow definition (parsed JSON)
|
|
196
|
+
* @param {string} options.name - Package name (without vai-workflow- prefix)
|
|
197
|
+
* @param {string} [options.author] - Author name
|
|
198
|
+
* @param {string} [options.description] - Package description
|
|
199
|
+
* @param {string} [options.category] - Workflow category
|
|
200
|
+
* @param {string[]} [options.tags] - Tags
|
|
201
|
+
* @param {string} [options.scope] - Scope ('vaicli' for official @vaicli packages)
|
|
202
|
+
* @param {string} [options.outputDir] - Output directory (default: ./vai-workflow-<name>/)
|
|
203
|
+
* @returns {{ dir: string, files: string[] }}
|
|
204
|
+
*/
|
|
205
|
+
function scaffoldPackage(options) {
|
|
206
|
+
const { definition, name, author, description, category, tags, scope } = options;
|
|
207
|
+
|
|
208
|
+
const packageName = toPackageName(name, { scope });
|
|
209
|
+
// For scoped packages, use the unscoped part as the directory name
|
|
210
|
+
const dirName = packageName.startsWith('@') ? packageName.split('/')[1] : packageName;
|
|
211
|
+
const outputDir = options.outputDir || path.resolve(process.cwd(), dirName);
|
|
212
|
+
|
|
213
|
+
// Validate the workflow
|
|
214
|
+
const errors = validateWorkflow(definition);
|
|
215
|
+
if (errors.length > 0) {
|
|
216
|
+
throw new Error(`Workflow validation failed:\n ${errors.join('\n ')}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Extract metadata
|
|
220
|
+
const tools = extractTools(definition);
|
|
221
|
+
const guessedCategory = category || guessCategory(tools);
|
|
222
|
+
|
|
223
|
+
// Build package.json
|
|
224
|
+
const pkg = {
|
|
225
|
+
name: packageName,
|
|
226
|
+
version: '1.0.0',
|
|
227
|
+
description: description || definition.description || '',
|
|
228
|
+
main: 'workflow.json',
|
|
229
|
+
keywords: ['vai-workflow', 'voyageai-cli', ...tools, ...(tags || [])],
|
|
230
|
+
vai: {
|
|
231
|
+
workflowVersion: '1.0',
|
|
232
|
+
category: guessedCategory,
|
|
233
|
+
tags: tags || [],
|
|
234
|
+
tools,
|
|
235
|
+
branding: {
|
|
236
|
+
icon: suggestBrandingIcon(guessedCategory, tools),
|
|
237
|
+
color: CATEGORY_COLORS[guessedCategory] || '#0D9488',
|
|
238
|
+
},
|
|
239
|
+
inputs: {},
|
|
240
|
+
},
|
|
241
|
+
files: ['workflow.json', 'README.md'],
|
|
242
|
+
license: 'MIT',
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// Add publishConfig for scoped packages
|
|
246
|
+
if (scope === 'vaicli') {
|
|
247
|
+
pkg.publishConfig = { access: 'public' };
|
|
248
|
+
pkg.repository = {
|
|
249
|
+
type: 'git',
|
|
250
|
+
url: 'https://github.com/vaicli/workflows.git',
|
|
251
|
+
directory: `packages/${dirName}`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (author) {
|
|
256
|
+
pkg.author = author;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Copy input descriptions to vai.inputs
|
|
260
|
+
if (definition.inputs) {
|
|
261
|
+
for (const [key, schema] of Object.entries(definition.inputs)) {
|
|
262
|
+
pkg.vai.inputs[key] = {
|
|
263
|
+
type: schema.type || 'string',
|
|
264
|
+
required: !!schema.required,
|
|
265
|
+
...(schema.default !== undefined && { default: schema.default }),
|
|
266
|
+
description: schema.description || '',
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Generate files
|
|
272
|
+
const readme = generateReadme(pkg, definition);
|
|
273
|
+
const license = `MIT License\n\nCopyright (c) ${new Date().getFullYear()} ${author || 'Contributors'}\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n`;
|
|
274
|
+
|
|
275
|
+
// Write
|
|
276
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
277
|
+
const files = [];
|
|
278
|
+
|
|
279
|
+
fs.writeFileSync(path.join(outputDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n');
|
|
280
|
+
files.push('package.json');
|
|
281
|
+
|
|
282
|
+
fs.writeFileSync(path.join(outputDir, 'workflow.json'), JSON.stringify(definition, null, 2) + '\n');
|
|
283
|
+
files.push('workflow.json');
|
|
284
|
+
|
|
285
|
+
fs.writeFileSync(path.join(outputDir, 'README.md'), readme);
|
|
286
|
+
files.push('README.md');
|
|
287
|
+
|
|
288
|
+
fs.writeFileSync(path.join(outputDir, 'LICENSE'), license);
|
|
289
|
+
files.push('LICENSE');
|
|
290
|
+
|
|
291
|
+
// Create tests/ directory with a sample test case
|
|
292
|
+
const testsDir = path.join(outputDir, 'tests');
|
|
293
|
+
fs.mkdirSync(testsDir, { recursive: true });
|
|
294
|
+
|
|
295
|
+
// Build sample mocks based on the tools used
|
|
296
|
+
const sampleMocks = {};
|
|
297
|
+
for (const tool of tools) {
|
|
298
|
+
if (tool === 'query' || tool === 'search') {
|
|
299
|
+
sampleMocks[tool] = { results: [{ text: 'Sample result', score: 0.95 }], resultCount: 1 };
|
|
300
|
+
} else if (tool === 'embed') {
|
|
301
|
+
sampleMocks[tool] = { embedding: [0.1, 0.2, 0.3], model: 'voyage-3-large', dimensions: 3 };
|
|
302
|
+
} else if (tool === 'rerank') {
|
|
303
|
+
sampleMocks[tool] = { results: [{ text: 'Reranked result', score: 0.98 }], resultCount: 1 };
|
|
304
|
+
} else if (tool === 'generate') {
|
|
305
|
+
sampleMocks[tool] = { text: 'Generated text response', model: 'mock-llm', provider: 'mock' };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Build sample inputs from definition
|
|
310
|
+
const sampleInputs = {};
|
|
311
|
+
if (definition.inputs) {
|
|
312
|
+
for (const [key, schema] of Object.entries(definition.inputs)) {
|
|
313
|
+
if (schema.default !== undefined) {
|
|
314
|
+
sampleInputs[key] = schema.default;
|
|
315
|
+
} else if (schema.type === 'number') {
|
|
316
|
+
sampleInputs[key] = 10;
|
|
317
|
+
} else if (schema.type === 'boolean') {
|
|
318
|
+
sampleInputs[key] = true;
|
|
319
|
+
} else {
|
|
320
|
+
sampleInputs[key] = 'test value';
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Build expected steps
|
|
326
|
+
const expectedSteps = {};
|
|
327
|
+
if (definition.steps) {
|
|
328
|
+
for (const step of definition.steps) {
|
|
329
|
+
expectedSteps[step.id] = { status: 'completed' };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const sampleTestCase = {
|
|
334
|
+
name: 'basic workflow test',
|
|
335
|
+
inputs: sampleInputs,
|
|
336
|
+
mocks: sampleMocks,
|
|
337
|
+
expect: {
|
|
338
|
+
steps: expectedSteps,
|
|
339
|
+
noErrors: true,
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
fs.writeFileSync(path.join(testsDir, 'basic.test.json'), JSON.stringify(sampleTestCase, null, 2) + '\n');
|
|
344
|
+
files.push('tests/basic.test.json');
|
|
345
|
+
|
|
346
|
+
// Create fixtures directory with .gitkeep
|
|
347
|
+
const fixturesDir = path.join(testsDir, 'fixtures');
|
|
348
|
+
fs.mkdirSync(fixturesDir, { recursive: true });
|
|
349
|
+
fs.writeFileSync(path.join(fixturesDir, '.gitkeep'), '');
|
|
350
|
+
files.push('tests/fixtures/.gitkeep');
|
|
351
|
+
|
|
352
|
+
return { dir: outputDir, files };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Create an empty workflow template for interactive mode.
|
|
357
|
+
* @returns {object}
|
|
358
|
+
*/
|
|
359
|
+
function emptyWorkflowTemplate() {
|
|
360
|
+
return {
|
|
361
|
+
name: '',
|
|
362
|
+
description: '',
|
|
363
|
+
version: '1.0.0',
|
|
364
|
+
inputs: {},
|
|
365
|
+
steps: [],
|
|
366
|
+
output: {},
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
module.exports = {
|
|
371
|
+
scaffoldPackage,
|
|
372
|
+
generateReadme,
|
|
373
|
+
extractTools,
|
|
374
|
+
guessCategory,
|
|
375
|
+
suggestBrandingIcon,
|
|
376
|
+
toPackageName,
|
|
377
|
+
emptyWorkflowTemplate,
|
|
378
|
+
CATEGORIES,
|
|
379
|
+
BRANDING_ICONS,
|
|
380
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const { executeWorkflow } = require('./workflow');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Run a single workflow test case against a definition.
|
|
9
|
+
*
|
|
10
|
+
* @param {object} definition - Parsed workflow JSON
|
|
11
|
+
* @param {object} testCase - Test case object { name, inputs, mocks, expect }
|
|
12
|
+
* @returns {Promise<{ passed: boolean, assertions: Array<{pass: boolean, message: string}>, errors: string[] }>}
|
|
13
|
+
*/
|
|
14
|
+
async function runWorkflowTest(definition, testCase) {
|
|
15
|
+
const results = { passed: true, assertions: [], errors: [] };
|
|
16
|
+
|
|
17
|
+
// Build mock executors from test case mocks
|
|
18
|
+
const mockExecutors = {};
|
|
19
|
+
for (const [tool, mockOutput] of Object.entries(testCase.mocks || {})) {
|
|
20
|
+
mockExecutors[tool] = async () => mockOutput;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let result;
|
|
24
|
+
try {
|
|
25
|
+
result = await executeWorkflow(definition, {
|
|
26
|
+
inputs: testCase.inputs || {},
|
|
27
|
+
_mockExecutors: mockExecutors,
|
|
28
|
+
});
|
|
29
|
+
} catch (err) {
|
|
30
|
+
results.passed = false;
|
|
31
|
+
results.errors.push(err.message);
|
|
32
|
+
return results;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Check step statuses
|
|
36
|
+
if (testCase.expect && testCase.expect.steps) {
|
|
37
|
+
for (const [stepId, expected] of Object.entries(testCase.expect.steps)) {
|
|
38
|
+
const stepResult = result.steps.find(s => s.id === stepId);
|
|
39
|
+
if (!stepResult) {
|
|
40
|
+
results.assertions.push({ pass: false, message: `Step "${stepId}" not found in results` });
|
|
41
|
+
results.passed = false;
|
|
42
|
+
} else if (expected.status === 'completed') {
|
|
43
|
+
if (stepResult.skipped) {
|
|
44
|
+
results.assertions.push({ pass: false, message: `Step "${stepId}" was skipped, expected completed` });
|
|
45
|
+
results.passed = false;
|
|
46
|
+
} else if (stepResult.error) {
|
|
47
|
+
results.assertions.push({ pass: false, message: `Step "${stepId}" errored: ${stepResult.error}` });
|
|
48
|
+
results.passed = false;
|
|
49
|
+
} else {
|
|
50
|
+
results.assertions.push({ pass: true, message: `Step "${stepId}" completed` });
|
|
51
|
+
}
|
|
52
|
+
} else if (expected.status === 'skipped') {
|
|
53
|
+
if (stepResult.skipped) {
|
|
54
|
+
results.assertions.push({ pass: true, message: `Step "${stepId}" skipped` });
|
|
55
|
+
} else {
|
|
56
|
+
results.assertions.push({ pass: false, message: `Step "${stepId}" should have been skipped` });
|
|
57
|
+
results.passed = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Check output shape
|
|
64
|
+
if (testCase.expect && testCase.expect.output) {
|
|
65
|
+
for (const [key, constraint] of Object.entries(testCase.expect.output)) {
|
|
66
|
+
const value = result.output && result.output[key];
|
|
67
|
+
if (constraint.type === 'array') {
|
|
68
|
+
if (!Array.isArray(value)) {
|
|
69
|
+
results.assertions.push({ pass: false, message: `output.${key} should be array, got ${typeof value}` });
|
|
70
|
+
results.passed = false;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (constraint.type === 'string') {
|
|
75
|
+
if (typeof value !== 'string') {
|
|
76
|
+
results.assertions.push({ pass: false, message: `output.${key} should be string, got ${typeof value}` });
|
|
77
|
+
results.passed = false;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (constraint.type === 'number') {
|
|
82
|
+
if (typeof value !== 'number') {
|
|
83
|
+
results.assertions.push({ pass: false, message: `output.${key} should be number, got ${typeof value}` });
|
|
84
|
+
results.passed = false;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (constraint.type === 'object') {
|
|
89
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
90
|
+
results.assertions.push({ pass: false, message: `output.${key} should be object` });
|
|
91
|
+
results.passed = false;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (constraint.minLength != null) {
|
|
96
|
+
if (!value || value.length < constraint.minLength) {
|
|
97
|
+
results.assertions.push({ pass: false, message: `output.${key} length ${value ? value.length : 0} < ${constraint.minLength}` });
|
|
98
|
+
results.passed = false;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// If we got here with a type check, it passed
|
|
103
|
+
if (constraint.type || constraint.minLength != null) {
|
|
104
|
+
results.assertions.push({ pass: true, message: `output.${key} matches expected shape` });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Check noErrors flag
|
|
110
|
+
if (testCase.expect && testCase.expect.noErrors) {
|
|
111
|
+
const hasErrors = result.steps.some(s => s.error);
|
|
112
|
+
if (hasErrors) {
|
|
113
|
+
const errorSteps = result.steps.filter(s => s.error).map(s => `${s.id}: ${s.error}`);
|
|
114
|
+
results.assertions.push({ pass: false, message: `Expected no errors but found: ${errorSteps.join('; ')}` });
|
|
115
|
+
results.passed = false;
|
|
116
|
+
} else {
|
|
117
|
+
results.assertions.push({ pass: true, message: 'No step errors' });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return results;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Load test cases from a workflow package's tests/ directory.
|
|
126
|
+
*
|
|
127
|
+
* @param {string} packagePath - Path to the workflow package directory
|
|
128
|
+
* @returns {Array<object>} Array of test case objects
|
|
129
|
+
*/
|
|
130
|
+
function loadTestCases(packagePath) {
|
|
131
|
+
const testsDir = path.join(packagePath, 'tests');
|
|
132
|
+
if (!fs.existsSync(testsDir)) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const files = fs.readdirSync(testsDir).filter(f => f.endsWith('.test.json'));
|
|
137
|
+
const testCases = [];
|
|
138
|
+
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const filePath = path.join(testsDir, file);
|
|
141
|
+
try {
|
|
142
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
143
|
+
const testCase = JSON.parse(content);
|
|
144
|
+
testCase._file = file;
|
|
145
|
+
testCases.push(testCase);
|
|
146
|
+
} catch (err) {
|
|
147
|
+
testCases.push({
|
|
148
|
+
name: file,
|
|
149
|
+
_file: file,
|
|
150
|
+
_error: `Failed to load: ${err.message}`,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return testCases;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Run all test cases for a workflow package.
|
|
160
|
+
*
|
|
161
|
+
* @param {object} definition - Parsed workflow JSON
|
|
162
|
+
* @param {string} packagePath - Path to the workflow package directory
|
|
163
|
+
* @param {object} [options]
|
|
164
|
+
* @param {string} [options.testName] - Run only a specific test by name
|
|
165
|
+
* @returns {Promise<{ total: number, passed: number, failed: number, results: Array }>}
|
|
166
|
+
*/
|
|
167
|
+
async function runAllTests(definition, packagePath, options = {}) {
|
|
168
|
+
let testCases = loadTestCases(packagePath);
|
|
169
|
+
|
|
170
|
+
if (options.testName) {
|
|
171
|
+
testCases = testCases.filter(t => t.name === options.testName);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const results = [];
|
|
175
|
+
let passed = 0;
|
|
176
|
+
let failed = 0;
|
|
177
|
+
|
|
178
|
+
for (const testCase of testCases) {
|
|
179
|
+
if (testCase._error) {
|
|
180
|
+
results.push({ name: testCase.name, file: testCase._file, passed: false, error: testCase._error, assertions: [] });
|
|
181
|
+
failed++;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const result = await runWorkflowTest(definition, testCase);
|
|
186
|
+
results.push({
|
|
187
|
+
name: testCase.name || testCase._file,
|
|
188
|
+
file: testCase._file,
|
|
189
|
+
passed: result.passed,
|
|
190
|
+
assertions: result.assertions,
|
|
191
|
+
errors: result.errors,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
if (result.passed) {
|
|
195
|
+
passed++;
|
|
196
|
+
} else {
|
|
197
|
+
failed++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return { total: testCases.length, passed, failed, results };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = {
|
|
205
|
+
runWorkflowTest,
|
|
206
|
+
loadTestCases,
|
|
207
|
+
runAllTests,
|
|
208
|
+
};
|