dsh-workflow-canvas 0.1.2 → 0.2.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 +163 -130
- package/README.zh.md +125 -101
- package/client.js +1326 -252
- package/docs/DETAIL.md +42 -28
- package/docs/images/canvas-demo-zh.png +0 -0
- package/docs/images/canvas-demo.png +0 -0
- package/index.js +273 -84
- package/package.json +1 -1
- package/skills/workflow-canvas/SKILL.md +9 -7
- package/docs/images/node-types.svg +0 -86
package/index.js
CHANGED
|
@@ -125,7 +125,7 @@ function downstreamLabels(plan, node) {
|
|
|
125
125
|
.filter(Boolean);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
function briefFor(plan, node, results) {
|
|
128
|
+
function briefFor(plan, node, results, failedMap) {
|
|
129
129
|
const ups = upstreamOf(plan, node);
|
|
130
130
|
const downs = downstreamLabels(plan, node);
|
|
131
131
|
const head = [];
|
|
@@ -140,14 +140,18 @@ function briefFor(plan, node, results) {
|
|
|
140
140
|
const value = results[u.from.id];
|
|
141
141
|
const type = u.edge.type || 'prompt';
|
|
142
142
|
const body = typeof value === 'string' ? value : '(没有结果)';
|
|
143
|
+
const failInfo = failedMap && failedMap[u.from.id];
|
|
144
|
+
const warn = failInfo
|
|
145
|
+
? '【警告:该上游节点执行失败(' + (failInfo.reason || 'unknown') + '),请勿将其报错当作正常结论,必要时说明受阻情况】\n'
|
|
146
|
+
: '';
|
|
143
147
|
if (type === 'structured') {
|
|
144
|
-
text += '\n## 来自【' + u.from.label + '】的数据(JSON,可直接解析)\n```json\n'
|
|
148
|
+
text += '\n## 来自【' + u.from.label + '】的数据(JSON,可直接解析)\n' + warn + '```json\n'
|
|
145
149
|
+ body + '\n```\n';
|
|
146
150
|
} else if (type === 'session-ref') {
|
|
147
|
-
text += '\n## 来自【' + u.from.label + '】的会话引用(当前按文本传递)\n'
|
|
151
|
+
text += '\n## 来自【' + u.from.label + '】的会话引用(当前按文本传递)\n' + warn
|
|
148
152
|
+ body + '\n';
|
|
149
153
|
} else {
|
|
150
|
-
text += '\n## 来自【' + u.from.label + '】\n' + body + '\n';
|
|
154
|
+
text += '\n## 来自【' + u.from.label + '】\n' + warn + body + '\n';
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
157
|
return text;
|
|
@@ -172,12 +176,12 @@ function failed(result) {
|
|
|
172
176
|
return reason !== undefined && reason !== 'completed';
|
|
173
177
|
}
|
|
174
178
|
|
|
175
|
-
async function spawnOnce(scope, plan, node, results, parent, signal, route, forcedChild, sid) {
|
|
179
|
+
async function spawnOnce(scope, plan, node, results, parent, signal, route, forcedChild, sid, failedMap) {
|
|
176
180
|
const childKey = (id) => String(sid || '') + '::' + id;
|
|
177
181
|
const spec = {
|
|
178
182
|
provider: 'spawn',
|
|
179
183
|
label: node.label + '#' + node.id,
|
|
180
|
-
prompt: [{ type: 'text', text: briefFor(plan, node, results) }],
|
|
184
|
+
prompt: [{ type: 'text', text: briefFor(plan, node, results, failedMap) }],
|
|
181
185
|
parent,
|
|
182
186
|
signal,
|
|
183
187
|
delivery: 'caller',
|
|
@@ -190,7 +194,9 @@ async function spawnOnce(scope, plan, node, results, parent, signal, route, forc
|
|
|
190
194
|
if (node.reasoning) agentOptions.reasoningEffort = node.reasoning;
|
|
191
195
|
if (Object.keys(agentOptions).length) spec.agentOptions = agentOptions;
|
|
192
196
|
if (node.preset) spec.agentPreset = node.preset;
|
|
193
|
-
|
|
197
|
+
const wantsStructured = Boolean(node.structured)
|
|
198
|
+
|| (plan.edges || []).some((e) => e.from === node.id && e.type === 'structured');
|
|
199
|
+
if (wantsStructured) {
|
|
194
200
|
spec.outputSchema = {
|
|
195
201
|
type: 'object',
|
|
196
202
|
additionalProperties: true,
|
|
@@ -227,14 +233,16 @@ async function spawnOnce(scope, plan, node, results, parent, signal, route, forc
|
|
|
227
233
|
|
|
228
234
|
// A `session` edge means "continue the upstream node's conversation": run this
|
|
229
235
|
// step inside that child session instead of starting a fresh one.
|
|
230
|
-
if (forcedChild
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
236
|
+
if (forcedChild) {
|
|
237
|
+
const continued = await run(forcedChild);
|
|
238
|
+
if (continued.ok) {
|
|
239
|
+
liveChild.set(childKey(node.id), forcedChild);
|
|
234
240
|
return continued;
|
|
235
|
-
} catch (_e) {
|
|
236
|
-
/* fall through to a fresh session */
|
|
237
241
|
}
|
|
242
|
+
if (continued.reason !== 'activation-failed') {
|
|
243
|
+
return continued;
|
|
244
|
+
}
|
|
245
|
+
/* activation refused (e.g. cold session gone): fall through to a fresh session */
|
|
238
246
|
}
|
|
239
247
|
|
|
240
248
|
// Memory: prefer the session created in this process, then the one persisted
|
|
@@ -263,28 +271,31 @@ async function spawnOnce(scope, plan, node, results, parent, signal, route, forc
|
|
|
263
271
|
return started;
|
|
264
272
|
}
|
|
265
273
|
|
|
266
|
-
async function spawnNode(scope, plan, node, results, parent, signal, sid) {
|
|
274
|
+
async function spawnNode(scope, plan, node, results, parent, signal, sid, failedMap) {
|
|
267
275
|
const continueFrom = (plan.edges || [])
|
|
268
276
|
.filter((e) => e.to === node.id && e.type === 'session-ref')
|
|
269
|
-
.map((e) => liveChild.get(String(sid || '') + '::' + e.from)
|
|
277
|
+
.map((e) => liveChild.get(String(sid || '') + '::' + e.from)
|
|
278
|
+
|| childByNode.get(String(sid || '') + '::' + e.from))
|
|
270
279
|
.find((id) => typeof id === 'string');
|
|
271
280
|
|
|
272
281
|
if (continueFrom) {
|
|
273
|
-
const continued = await spawnOnce(scope, plan, node, results, parent, signal, undefined, continueFrom, sid);
|
|
274
|
-
if (continued.ok) return continued.text + '\n(已在上游会话中继续)';
|
|
282
|
+
const continued = await spawnOnce(scope, plan, node, results, parent, signal, undefined, continueFrom, sid, failedMap);
|
|
283
|
+
if (continued.ok) return { ok: true, text: continued.text + '\n(已在上游会话中继续)' };
|
|
275
284
|
}
|
|
276
285
|
|
|
277
|
-
const first = await spawnOnce(scope, plan, node, results, parent, signal, undefined, undefined, sid);
|
|
278
|
-
if (first.ok) return first.text;
|
|
286
|
+
const first = await spawnOnce(scope, plan, node, results, parent, signal, undefined, undefined, sid, failedMap);
|
|
287
|
+
if (first.ok) return { ok: true, text: first.text };
|
|
279
288
|
if (!node.fallbackModel && !node.fallbackProvider) {
|
|
280
|
-
|
|
289
|
+
const reason = first.reason || 'unknown';
|
|
290
|
+
return { ok: false, reason, text: first.text + '\n(失败原因:' + reason + ',未配置备用模型)' };
|
|
281
291
|
}
|
|
282
292
|
const retry = await spawnOnce(scope, plan, node, results, parent, signal, {
|
|
283
293
|
provider: node.fallbackProvider,
|
|
284
294
|
model: node.fallbackModel,
|
|
285
|
-
}, undefined, sid);
|
|
286
|
-
if (retry.ok) return retry.text + '\n(已用备用模型重试成功)';
|
|
287
|
-
|
|
295
|
+
}, undefined, sid, failedMap);
|
|
296
|
+
if (retry.ok) return { ok: true, text: retry.text + '\n(已用备用模型重试成功)' };
|
|
297
|
+
const reason = retry.reason || 'unknown';
|
|
298
|
+
return { ok: false, reason, text: retry.text + '\n(重试仍失败:' + reason + ')' };
|
|
288
299
|
}
|
|
289
300
|
|
|
290
301
|
/** Ids reachable from the entry Lead; every other node is skipped at run time. */
|
|
@@ -365,6 +376,10 @@ function normalizeCanvas(args) {
|
|
|
365
376
|
const type = ['prompt', 'structured', 'session-ref'].includes(raw.type) ? raw.type : 'prompt';
|
|
366
377
|
edges.push({ id: 'e' + edges.length, from, to, type });
|
|
367
378
|
}
|
|
379
|
+
const structuredSources = new Set(edges.filter((e) => e.type === 'structured').map((e) => e.from));
|
|
380
|
+
for (const node of nodes) {
|
|
381
|
+
if (structuredSources.has(node.id)) node.structured = true;
|
|
382
|
+
}
|
|
368
383
|
|
|
369
384
|
// Layout: depth of the longest path from the entry, then stack per column.
|
|
370
385
|
const depth = new Map(nodes.map((n) => [n.id, n.id === 'lead' ? 0 : 1]));
|
|
@@ -389,6 +404,78 @@ function normalizeCanvas(args) {
|
|
|
389
404
|
return { nodes, edges, task: String(args.task || '') };
|
|
390
405
|
}
|
|
391
406
|
|
|
407
|
+
/**
|
|
408
|
+
* Apply an in-place patch to an existing plan: update/add/remove nodes and
|
|
409
|
+
* edges without losing untouched nodes. Preserves `'lead'` as a valid edge
|
|
410
|
+
* source so newly added branches can wire directly from the entry Lead.
|
|
411
|
+
*/
|
|
412
|
+
function applyPatch(plan, args) {
|
|
413
|
+
const nodes = withoutEntry(plan.nodes).map((n) => ({ ...n }));
|
|
414
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
415
|
+
const ALLOWED = ['label', 'role', 'provider', 'model', 'reasoning', 'preset', 'keepSession', 'structured', 'fallbackModel', 'fallbackProvider'];
|
|
416
|
+
const touched = [];
|
|
417
|
+
for (const patch of args && Array.isArray(args.nodes) ? args.nodes : []) {
|
|
418
|
+
// 'lead' is the reserved entry node and is never edited here.
|
|
419
|
+
if (!patch || typeof patch.id !== 'string' || patch.id === 'lead') continue;
|
|
420
|
+
const existing = byId.get(patch.id);
|
|
421
|
+
if (existing) {
|
|
422
|
+
for (const key of ALLOWED) {
|
|
423
|
+
if (patch[key] !== undefined) existing[key] = patch[key];
|
|
424
|
+
}
|
|
425
|
+
touched.push(patch.id);
|
|
426
|
+
} else {
|
|
427
|
+
const created = {
|
|
428
|
+
id: patch.id,
|
|
429
|
+
kind: patch.kind === 'lead' ? 'lead' : 'agent',
|
|
430
|
+
label: patch.label || patch.id,
|
|
431
|
+
role: '', provider: '', model: '', reasoning: '', preset: 'standard',
|
|
432
|
+
structured: false, keepSession: false, fallbackModel: '', fallbackProvider: '',
|
|
433
|
+
};
|
|
434
|
+
for (const key of ALLOWED) {
|
|
435
|
+
if (patch[key] !== undefined) created[key] = patch[key];
|
|
436
|
+
}
|
|
437
|
+
created.preset = created.preset === 'minimal' ? 'minimal' : 'standard';
|
|
438
|
+
nodes.push(created);
|
|
439
|
+
byId.set(created.id, created);
|
|
440
|
+
touched.push(created.id);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const removed = new Set(Array.isArray(args && args.removeNodes) ? args.removeNodes : []);
|
|
444
|
+
removed.delete('lead');
|
|
445
|
+
const keptNodes = nodes.filter((n) => !removed.has(n.id));
|
|
446
|
+
const keptIds = new Set(keptNodes.map((n) => n.id));
|
|
447
|
+
const rawRemoveEdges = Array.isArray(args && args.removeEdges) ? args.removeEdges : [];
|
|
448
|
+
const matchesRemovedEdge = (edge) => rawRemoveEdges.some((r) => r
|
|
449
|
+
&& String(r.from) === edge.from
|
|
450
|
+
&& String(r.to) === edge.to
|
|
451
|
+
&& (!r.type || r.type === edge.type));
|
|
452
|
+
let edges = (Array.isArray(plan.edges) ? plan.edges : [])
|
|
453
|
+
.filter((e) => !matchesRemovedEdge(e) && !removed.has(e.from) && !removed.has(e.to))
|
|
454
|
+
.map((e) => ({ ...e }));
|
|
455
|
+
for (const edge of Array.isArray(args && args.edges) ? args.edges : []) {
|
|
456
|
+
if (!edge || typeof edge.from !== 'string' || typeof edge.to !== 'string') continue;
|
|
457
|
+
if (edge.from === edge.to || edge.to === 'lead') continue;
|
|
458
|
+
const validFrom = edge.from === 'lead' || keptIds.has(edge.from);
|
|
459
|
+
const validTo = keptIds.has(edge.to);
|
|
460
|
+
if (!validFrom || !validTo) continue;
|
|
461
|
+
const type = ['prompt', 'structured', 'session-ref'].includes(edge.type) ? edge.type : 'prompt';
|
|
462
|
+
if (edges.some((e) => e.from === edge.from && e.to === edge.to && e.type === type)) continue;
|
|
463
|
+
edges.push({ from: edge.from, to: edge.to, type });
|
|
464
|
+
}
|
|
465
|
+
const structuredSources = new Set(edges.filter((e) => e.type === 'structured').map((e) => e.from));
|
|
466
|
+
for (const node of keptNodes) {
|
|
467
|
+
if (structuredSources.has(node.id)) node.structured = true;
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
plan: { ...plan, nodes: keptNodes, edges },
|
|
471
|
+
touched,
|
|
472
|
+
removed: Array.from(removed),
|
|
473
|
+
removeEdges: rawRemoveEdges
|
|
474
|
+
.filter((r) => r && typeof r.from === 'string' && typeof r.to === 'string')
|
|
475
|
+
.map((r) => (r.type ? { from: r.from, to: r.to, type: r.type } : { from: r.from, to: r.to })),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
392
479
|
/**
|
|
393
480
|
* Drop a generated canvas where the browser can pick it up. `workspaceFiles`
|
|
394
481
|
* resolves a session's root from its header cwd, so the client reads the same
|
|
@@ -418,17 +505,67 @@ async function runPlan(scope, args, exec) {
|
|
|
418
505
|
return executePlan(scope, payload.plan, args, exec);
|
|
419
506
|
}
|
|
420
507
|
|
|
508
|
+
function inspectPlan(sid) {
|
|
509
|
+
const payload = sid ? canvasBySession.get(sid) : undefined;
|
|
510
|
+
if (!payload) {
|
|
511
|
+
return {
|
|
512
|
+
hasCanvas: false,
|
|
513
|
+
enabled: false,
|
|
514
|
+
task: '',
|
|
515
|
+
nodes: [],
|
|
516
|
+
edges: [],
|
|
517
|
+
paused: false,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
const plan = payload.plan || {};
|
|
521
|
+
const nodes = withoutEntry(plan.nodes).map((n) => ({
|
|
522
|
+
id: n.id,
|
|
523
|
+
kind: n.kind === 'lead' ? 'lead' : 'agent',
|
|
524
|
+
label: n.label || n.id,
|
|
525
|
+
role: n.role || '',
|
|
526
|
+
provider: n.provider || '',
|
|
527
|
+
model: n.model || '',
|
|
528
|
+
reasoning: n.reasoning || '',
|
|
529
|
+
preset: n.preset === 'minimal' ? 'minimal' : 'standard',
|
|
530
|
+
keepSession: Boolean(n.keepSession),
|
|
531
|
+
structured: Boolean(n.structured),
|
|
532
|
+
fallbackModel: n.fallbackModel || '',
|
|
533
|
+
fallbackProvider: n.fallbackProvider || '',
|
|
534
|
+
}));
|
|
535
|
+
const edges = Array.isArray(plan.edges) ? plan.edges.map((e) => ({
|
|
536
|
+
from: e.from,
|
|
537
|
+
to: e.to,
|
|
538
|
+
type: e.type || 'prompt',
|
|
539
|
+
})) : [];
|
|
540
|
+
return {
|
|
541
|
+
hasCanvas: true,
|
|
542
|
+
enabled: Boolean(payload.enabled),
|
|
543
|
+
task: String(plan.task || ''),
|
|
544
|
+
nodes,
|
|
545
|
+
edges,
|
|
546
|
+
paused: Boolean(sid && runBySession.has(sid)),
|
|
547
|
+
error: payload.error ? String(payload.error) : undefined,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
421
551
|
/** Takes a plan directly so tests can feed any plan shape (sync, generate, patch). */
|
|
422
552
|
async function executePlan(scope, plan, args, exec) {
|
|
553
|
+
const opts = args || {};
|
|
423
554
|
const agent = exec && exec.agent;
|
|
424
555
|
const sid = agent && agent.id !== undefined ? String(agent.id) : '';
|
|
425
556
|
if (!plan || !Array.isArray(plan.nodes) || !plan.nodes.length) {
|
|
426
557
|
return { error: '画布没有可执行的节点。' };
|
|
427
558
|
}
|
|
428
559
|
|
|
560
|
+
const overrideTask = typeof opts.task === 'string' ? opts.task.trim() : '';
|
|
561
|
+
const effectiveTask = overrideTask || String(plan.task || '').trim();
|
|
562
|
+
if (!effectiveTask) {
|
|
563
|
+
return { error: '请先在入口 Lead 节点填写「本次任务」,或在调用 canvas_workflow 时传入 task。' };
|
|
564
|
+
}
|
|
565
|
+
|
|
429
566
|
// Backstop: no matter how a plan arrived (sync, generate, patch, a stale store),
|
|
430
567
|
// the entry Lead is never executed.
|
|
431
|
-
plan = { ...plan, nodes: withoutEntry(plan.nodes) };
|
|
568
|
+
plan = { ...plan, task: effectiveTask, nodes: withoutEntry(plan.nodes) };
|
|
432
569
|
|
|
433
570
|
const order = orderNodes(plan);
|
|
434
571
|
if (!order) return { error: '存在环,无法执行有向无环图。' };
|
|
@@ -446,10 +583,14 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
446
583
|
const skipped = order.filter((n) => !reachable.has(n.id)).map((n) => n.label || n.id);
|
|
447
584
|
if (!runnable.length) return { error: '没有从入口 Lead 连通的节点,无法执行。' };
|
|
448
585
|
|
|
586
|
+
if (opts.reset === true && sid) {
|
|
587
|
+
runBySession.delete(sid);
|
|
588
|
+
}
|
|
589
|
+
|
|
449
590
|
const sig = JSON.stringify(plan);
|
|
450
591
|
let state = runBySession.get(sid);
|
|
451
592
|
if (!state || state.sig !== sig) {
|
|
452
|
-
state = { sig, done: new Set(), results: {}, labels: {} };
|
|
593
|
+
state = { sig, done: new Set(), results: {}, labels: {}, failed: {} };
|
|
453
594
|
runBySession.set(sid, state);
|
|
454
595
|
}
|
|
455
596
|
|
|
@@ -460,7 +601,7 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
460
601
|
.map((e) => e.from)
|
|
461
602
|
.filter((id) => byId.has(id));
|
|
462
603
|
|
|
463
|
-
let leadResult = typeof
|
|
604
|
+
let leadResult = typeof opts.leadResult === 'string' ? opts.leadResult : undefined;
|
|
464
605
|
const MAX_PARALLEL = 4;
|
|
465
606
|
|
|
466
607
|
for (;;) {
|
|
@@ -468,7 +609,13 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
468
609
|
if (!remaining.length) {
|
|
469
610
|
runBySession.delete(sid);
|
|
470
611
|
saveStore();
|
|
471
|
-
return {
|
|
612
|
+
return {
|
|
613
|
+
status: 'done',
|
|
614
|
+
results: state.results,
|
|
615
|
+
labels: state.labels,
|
|
616
|
+
failed: Object.values(state.failed),
|
|
617
|
+
skipped,
|
|
618
|
+
};
|
|
472
619
|
}
|
|
473
620
|
|
|
474
621
|
const ready = remaining.filter((n) => depsOf(n).every((id) => state.done.has(id)));
|
|
@@ -477,12 +624,16 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
477
624
|
const agentReady = ready.filter((n) => n.kind !== 'lead');
|
|
478
625
|
if (agentReady.length) {
|
|
479
626
|
const batch = agentReady.slice(0, MAX_PARALLEL);
|
|
480
|
-
const
|
|
481
|
-
batch.map((node) => spawnNode(scope, plan, node, state.results, agent, exec.signal, sid)),
|
|
627
|
+
const outcomes = await Promise.all(
|
|
628
|
+
batch.map((node) => spawnNode(scope, plan, node, state.results, agent, exec.signal, sid, state.failed)),
|
|
482
629
|
);
|
|
483
630
|
batch.forEach((node, i) => {
|
|
484
|
-
|
|
631
|
+
const outcome = outcomes[i];
|
|
632
|
+
state.results[node.id] = outcome.text;
|
|
485
633
|
state.labels[node.id] = node.label;
|
|
634
|
+
if (!outcome.ok) {
|
|
635
|
+
state.failed[node.id] = { id: node.id, label: node.label, reason: outcome.reason || 'unknown' };
|
|
636
|
+
}
|
|
486
637
|
state.done.add(node.id);
|
|
487
638
|
});
|
|
488
639
|
continue;
|
|
@@ -500,6 +651,7 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
500
651
|
},
|
|
501
652
|
results: state.results,
|
|
502
653
|
labels: state.labels,
|
|
654
|
+
failed: Object.values(state.failed),
|
|
503
655
|
sessions: depsOf(leadNode)
|
|
504
656
|
.filter((id) => (plan.edges || []).some((e) => e.to === leadNode.id && e.from === id && e.type === 'session-ref'))
|
|
505
657
|
.map((id) => ({
|
|
@@ -508,7 +660,7 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
508
660
|
|| childByNode.get(String(sid || '') + '::' + id)
|
|
509
661
|
|| null,
|
|
510
662
|
})),
|
|
511
|
-
hint: '这一步由你(Lead 本体)完成。完成后再次调用本工具,把结果放进 leadResult
|
|
663
|
+
hint: '这一步由你(Lead 本体)完成。完成后再次调用本工具,把结果放进 leadResult 继续;若需从头重跑可传 reset: true。',
|
|
512
664
|
};
|
|
513
665
|
}
|
|
514
666
|
state.results[leadNode.id] = leadResult;
|
|
@@ -520,7 +672,7 @@ async function executePlan(scope, plan, args, exec) {
|
|
|
520
672
|
|
|
521
673
|
/** Agent-facing guide, shipped as a skill so any agent can load it on demand. */
|
|
522
674
|
const SKILL_NAME = 'workflow-canvas';
|
|
523
|
-
const SKILL_DESCRIPTION = 'Workflow canvas (画布 / 工作流 / 编排): draw, edit or run a node graph of DSH subagents — add or re-role nodes, wire typed links, run in parallel waves, handle Lead steps, presets and memory. Load before using canvas_generate, canvas_patch or canvas_workflow.';
|
|
675
|
+
const SKILL_DESCRIPTION = 'Workflow canvas (画布 / 工作流 / 编排): inspect, draw, edit or run a node graph of DSH subagents — add or re-role nodes, wire typed links, run in parallel waves, handle Lead steps, presets and memory. Load before using canvas_inspect, canvas_generate, canvas_patch or canvas_workflow.';
|
|
524
676
|
function skillPath() {
|
|
525
677
|
try {
|
|
526
678
|
return fileURLToPath(new URL('./skills/workflow-canvas/SKILL.md', import.meta.url));
|
|
@@ -628,6 +780,7 @@ export function apply(ctx) {
|
|
|
628
780
|
structured: { type: 'boolean' },
|
|
629
781
|
keepSession: { type: 'boolean' },
|
|
630
782
|
fallbackModel: { type: 'string' },
|
|
783
|
+
fallbackProvider: { type: 'string' },
|
|
631
784
|
},
|
|
632
785
|
},
|
|
633
786
|
},
|
|
@@ -668,8 +821,8 @@ export function apply(ctx) {
|
|
|
668
821
|
text: '已生成画布:' + nodes.length + ' 个节点、' + edges.length + ' 条连线\n'
|
|
669
822
|
+ lines.join('\n')
|
|
670
823
|
+ (v.delivered
|
|
671
|
-
? '\n\n
|
|
672
|
-
: '\n\n
|
|
824
|
+
? '\n\n画布已经自动同步到右侧画布,直接看即可(若没出现,点画布上的「⊡ 全览」)。'
|
|
825
|
+
: '\n\n**下一步**:点画布右上角「⋯ → ⇩ 导入」,它会把这段画布数据装进当前画布。')
|
|
673
826
|
+ '\n```json canvas\n'
|
|
674
827
|
+ JSON.stringify(canvas) + '\n```',
|
|
675
828
|
}];
|
|
@@ -742,57 +895,19 @@ export function apply(ctx) {
|
|
|
742
895
|
const sid = agent && agent.id !== undefined ? String(agent.id) : '';
|
|
743
896
|
const payload = sid ? canvasBySession.get(sid) : undefined;
|
|
744
897
|
if (!payload || !payload.plan) return { error: '当前会话还没有画布数据,先用「启用」同步一次。' };
|
|
745
|
-
const
|
|
746
|
-
const
|
|
747
|
-
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
748
|
-
const ALLOWED = ['label', 'role', 'provider', 'model', 'reasoning', 'preset', 'keepSession', 'structured', 'fallbackModel', 'fallbackProvider'];
|
|
749
|
-
const touched = [];
|
|
750
|
-
for (const patch of args && Array.isArray(args.nodes) ? args.nodes : []) {
|
|
751
|
-
// 'lead' is the reserved entry node and is never edited here.
|
|
752
|
-
if (!patch || typeof patch.id !== 'string' || patch.id === 'lead') continue;
|
|
753
|
-
const existing = byId.get(patch.id);
|
|
754
|
-
if (existing) {
|
|
755
|
-
for (const key of ALLOWED) {
|
|
756
|
-
if (patch[key] !== undefined) existing[key] = patch[key];
|
|
757
|
-
}
|
|
758
|
-
touched.push(patch.id);
|
|
759
|
-
} else {
|
|
760
|
-
const created = {
|
|
761
|
-
id: patch.id,
|
|
762
|
-
kind: patch.kind === 'lead' ? 'lead' : 'agent',
|
|
763
|
-
label: patch.label || patch.id,
|
|
764
|
-
role: '', provider: '', model: '', reasoning: '', preset: 'standard',
|
|
765
|
-
structured: false, keepSession: false, fallbackModel: '', fallbackProvider: '',
|
|
766
|
-
};
|
|
767
|
-
for (const key of ALLOWED) {
|
|
768
|
-
if (patch[key] !== undefined) created[key] = patch[key];
|
|
769
|
-
}
|
|
770
|
-
created.preset = created.preset === 'minimal' ? 'minimal' : 'standard';
|
|
771
|
-
nodes.push(created);
|
|
772
|
-
byId.set(created.id, created);
|
|
773
|
-
touched.push(created.id);
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
const removed = new Set(Array.isArray(args && args.removeNodes) ? args.removeNodes : []);
|
|
777
|
-
const keptNodes = nodes.filter((n) => !removed.has(n.id));
|
|
778
|
-
let edges = Array.isArray(plan.edges) ? plan.edges.slice() : [];
|
|
779
|
-
const dropEdges = new Set((Array.isArray(args && args.removeEdges) ? args.removeEdges : [])
|
|
780
|
-
.map((e) => String(e.from) + '>' + String(e.to)));
|
|
781
|
-
edges = edges.filter((e) => !dropEdges.has(e.from + '>' + e.to)
|
|
782
|
-
&& !removed.has(e.from) && !removed.has(e.to));
|
|
783
|
-
for (const edge of Array.isArray(args && args.edges) ? args.edges : []) {
|
|
784
|
-
if (!edge || !byId.has(edge.from) || !byId.has(edge.to)) continue;
|
|
785
|
-
if (removed.has(edge.from) || removed.has(edge.to)) continue;
|
|
786
|
-
const type = ['prompt', 'structured', 'session-ref'].includes(edge.type) ? edge.type : 'prompt';
|
|
787
|
-
if (edges.some((e) => e.from === edge.from && e.to === edge.to && e.type === type)) continue;
|
|
788
|
-
edges.push({ from: edge.from, to: edge.to, type });
|
|
789
|
-
}
|
|
790
|
-
const nextPlan = { ...plan, nodes: keptNodes, edges };
|
|
898
|
+
const patched = applyPatch(payload.plan, args);
|
|
899
|
+
const nextPlan = patched.plan;
|
|
791
900
|
canvasBySession.set(sid, { enabled: payload.enabled, plan: nextPlan });
|
|
792
901
|
runBySession.delete(sid);
|
|
793
902
|
saveStore();
|
|
794
|
-
const delivered = publishCanvas(scope, sid, {
|
|
795
|
-
|
|
903
|
+
const delivered = publishCanvas(scope, sid, {
|
|
904
|
+
mode: 'merge',
|
|
905
|
+
nodes: nextPlan.nodes,
|
|
906
|
+
edges: nextPlan.edges,
|
|
907
|
+
removeNodes: patched.removed,
|
|
908
|
+
removeEdges: patched.removeEdges,
|
|
909
|
+
});
|
|
910
|
+
const summary = '已更新 ' + patched.touched.length + ' 个节点、删除 ' + patched.removed.length + ' 个、连线 ' + nextPlan.edges.length + ' 条';
|
|
796
911
|
return { delivered, summary };
|
|
797
912
|
} catch (error) {
|
|
798
913
|
return { error: String((error && error.message) || error) };
|
|
@@ -804,15 +919,82 @@ export function apply(ctx) {
|
|
|
804
919
|
}
|
|
805
920
|
});
|
|
806
921
|
|
|
922
|
+
ctx.inject(['tools'], (scope) => {
|
|
923
|
+
try {
|
|
924
|
+
scope.effect(() => scope.tools.register({
|
|
925
|
+
name: 'canvas_inspect',
|
|
926
|
+
description: 'Read the current session workflow canvas (enabled status, overall task, nodes with ids/models/roles, links, and whether a run is paused at a Lead step). Call this before canvas_patch when the user edited the canvas manually or loaded a preset.',
|
|
927
|
+
parameters: {
|
|
928
|
+
type: 'object',
|
|
929
|
+
properties: {},
|
|
930
|
+
additionalProperties: false,
|
|
931
|
+
},
|
|
932
|
+
output: {
|
|
933
|
+
schema: {
|
|
934
|
+
type: 'object',
|
|
935
|
+
properties: {
|
|
936
|
+
hasCanvas: { type: 'boolean' },
|
|
937
|
+
enabled: { type: 'boolean' },
|
|
938
|
+
task: { type: 'string' },
|
|
939
|
+
nodes: { type: 'array' },
|
|
940
|
+
edges: { type: 'array' },
|
|
941
|
+
paused: { type: 'boolean' },
|
|
942
|
+
error: { type: 'string' },
|
|
943
|
+
},
|
|
944
|
+
},
|
|
945
|
+
render: (_args, value) => {
|
|
946
|
+
const v = value || {};
|
|
947
|
+
if (!v.hasCanvas) {
|
|
948
|
+
return [{ type: 'text', text: '当前会话尚未同步画布。请让用户在右侧 Workflow 画布点「启用」或「同步」,或直接用 canvas_generate 生成新画布。' }];
|
|
949
|
+
}
|
|
950
|
+
const head = [
|
|
951
|
+
'画布状态:' + (v.enabled ? '● 已启用' : '○ 已禁用') + (v.paused ? '(当前暂停在 Lead 步骤)' : ''),
|
|
952
|
+
'总任务:' + (v.task || '(未填写,运行时可通过 task 参数指定)'),
|
|
953
|
+
];
|
|
954
|
+
if (v.error) head.push('校验错误:' + v.error);
|
|
955
|
+
const nodeLines = (v.nodes || []).map((n) => {
|
|
956
|
+
const flags = [];
|
|
957
|
+
if (n.kind === 'lead') flags.push('Lead');
|
|
958
|
+
if (n.model) flags.push(n.model);
|
|
959
|
+
if (n.reasoning) flags.push('reasoning=' + n.reasoning);
|
|
960
|
+
if (n.preset === 'minimal') flags.push('minimal');
|
|
961
|
+
if (n.keepSession) flags.push('记忆');
|
|
962
|
+
if (n.fallbackModel) flags.push('备用=' + n.fallbackModel);
|
|
963
|
+
return '- [' + n.id + '] ' + n.label
|
|
964
|
+
+ (flags.length ? ' (' + flags.join(', ') + ')' : '')
|
|
965
|
+
+ (n.role ? ':' + n.role : '');
|
|
966
|
+
});
|
|
967
|
+
const edgeLines = (v.edges || []).map((e) => '- ' + e.from + ' --(' + e.type + ')--> ' + e.to);
|
|
968
|
+
return [{
|
|
969
|
+
type: 'text',
|
|
970
|
+
text: head.join('\n')
|
|
971
|
+
+ '\n\n节点(' + nodeLines.length + '):\n' + (nodeLines.join('\n') || '(无)')
|
|
972
|
+
+ '\n\n连线(' + edgeLines.length + '):\n' + (edgeLines.join('\n') || '(无)'),
|
|
973
|
+
}];
|
|
974
|
+
},
|
|
975
|
+
},
|
|
976
|
+
execute: (_args, exec) => {
|
|
977
|
+
const agent = exec && exec.agent;
|
|
978
|
+
const sid = agent && agent.id !== undefined ? String(agent.id) : '';
|
|
979
|
+
return inspectPlan(sid);
|
|
980
|
+
},
|
|
981
|
+
}), 'workflow-canvas: inspect tool');
|
|
982
|
+
} catch (_e) {
|
|
983
|
+
/* tool registry unavailable */
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
|
|
807
987
|
ctx.inject(['tools', 'subagents'], (scope) => {
|
|
808
988
|
try {
|
|
809
989
|
scope.effect(() => scope.tools.register({
|
|
810
990
|
name: 'canvas_workflow',
|
|
811
|
-
description: 'Run the workflow canvas of the current session. Executes subagent nodes in parallel waves; when a Lead node is reached it returns status "need-lead" and expects a follow-up call carrying leadResult. Load the `workflow-canvas` skill first when
|
|
991
|
+
description: 'Run the workflow canvas of the current session. Executes subagent nodes in parallel waves; when a Lead node is reached it returns status "need-lead" and expects a follow-up call carrying leadResult. Supports optional `task` to override the entry Lead task and `reset: true` to restart from scratch. Load the `workflow-canvas` skill first when unsure.',
|
|
812
992
|
parameters: {
|
|
813
993
|
type: 'object',
|
|
814
994
|
properties: {
|
|
815
995
|
leadResult: { type: 'string', description: 'Your own output for the Lead step that was just requested.' },
|
|
996
|
+
task: { type: 'string', description: 'Optional overall task overriding the entry Lead task for this run (useful for reusing a preset).' },
|
|
997
|
+
reset: { type: 'boolean', description: 'Discard any paused run progress and start from the beginning.' },
|
|
816
998
|
},
|
|
817
999
|
additionalProperties: false,
|
|
818
1000
|
},
|
|
@@ -824,6 +1006,7 @@ export function apply(ctx) {
|
|
|
824
1006
|
node: { type: 'object' },
|
|
825
1007
|
results: { type: 'object' },
|
|
826
1008
|
labels: { type: 'object' },
|
|
1009
|
+
failed: { type: 'array' },
|
|
827
1010
|
sessions: { type: 'array' },
|
|
828
1011
|
skipped: { type: 'array' },
|
|
829
1012
|
hint: { type: 'string' },
|
|
@@ -840,6 +1023,9 @@ export function apply(ctx) {
|
|
|
840
1023
|
return '## ' + (labels[id] || id) + '\n' + text;
|
|
841
1024
|
});
|
|
842
1025
|
const body = blocks.length ? blocks.join('\n\n') : '(暂无上游结果)';
|
|
1026
|
+
const failedList = Array.isArray(v.failed) && v.failed.length
|
|
1027
|
+
? '\n\n⚠ 失败节点:' + v.failed.map((f) => (f.label || f.id) + '(' + (f.reason || 'unknown') + ')').join('、')
|
|
1028
|
+
: '';
|
|
843
1029
|
if (v.status === 'need-lead') {
|
|
844
1030
|
const n = v.node || {};
|
|
845
1031
|
const handed = Array.isArray(v.sessions) ? v.sessions.filter((s) => s && s.childId) : [];
|
|
@@ -851,6 +1037,7 @@ export function apply(ctx) {
|
|
|
851
1037
|
type: 'text',
|
|
852
1038
|
text: '需要你(Lead)完成「' + n.label + '」:' + n.role
|
|
853
1039
|
+ '\n需要用到:' + ((n.needs && n.needs.length) ? n.needs.join('、') : '(无上游)')
|
|
1040
|
+
+ failedList
|
|
854
1041
|
+ '\n\n已完成节点的结果:\n' + body
|
|
855
1042
|
+ handover
|
|
856
1043
|
+ '\n\n' + (v.hint || ''),
|
|
@@ -860,7 +1047,7 @@ export function apply(ctx) {
|
|
|
860
1047
|
const skip = Array.isArray(v.skipped) && v.skipped.length
|
|
861
1048
|
? '\n\n已跳过(未连线):' + v.skipped.join('、')
|
|
862
1049
|
: '';
|
|
863
|
-
return [{ type: 'text', text: '
|
|
1050
|
+
return [{ type: 'text', text: '画布执行完成。' + failedList + '\n\n' + body + skip }];
|
|
864
1051
|
}
|
|
865
1052
|
return [{ type: 'text', text: JSON.stringify(v) }];
|
|
866
1053
|
},
|
|
@@ -883,6 +1070,8 @@ export {
|
|
|
883
1070
|
briefFor,
|
|
884
1071
|
withoutEntry,
|
|
885
1072
|
normalizeCanvas,
|
|
1073
|
+
applyPatch,
|
|
1074
|
+
inspectPlan,
|
|
886
1075
|
runPlan,
|
|
887
1076
|
executePlan,
|
|
888
1077
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: workflow-canvas
|
|
3
|
-
description:
|
|
3
|
+
description: Workflow canvas (画布 / 工作流 / 编排): inspect, draw, edit or run a node graph of DSH subagents — add or re-role nodes, wire typed links, run in parallel waves, handle Lead steps, presets and memory. Load before using canvas_inspect, canvas_generate, canvas_patch or canvas_workflow.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Workflow canvas
|
|
@@ -16,11 +16,12 @@ A canvas is a DAG of agents the user can see and edit.
|
|
|
16
16
|
|
|
17
17
|
| The user asks for | Call |
|
|
18
18
|
|---|---|
|
|
19
|
+
| inspecting or editing a manually drawn / preset canvas | `canvas_inspect` first to read node ids, models and links |
|
|
19
20
|
| a visual workflow, pipeline, or graph of agents | `canvas_generate` (replaces the canvas) |
|
|
20
21
|
| a change to the existing canvas | `canvas_patch` (edits in place, keeps the layout) |
|
|
21
|
-
| running the canvas | `canvas_workflow` |
|
|
22
|
+
| running the canvas | `canvas_workflow` (supports `leadResult`, `task` override, `reset: true`) |
|
|
22
23
|
|
|
23
|
-
Prefer `canvas_patch` for edits: it merges by node id, so renamed, re-roled or re-modelled nodes keep their position and untouched nodes stay as they are. Regenerating throws the layout away.
|
|
24
|
+
Prefer `canvas_patch` for edits: it merges by node id, so renamed, re-roled or re-modelled nodes keep their position and untouched nodes stay as they are. Call `canvas_inspect` first if you do not already know the current node ids. Regenerating throws the layout away.
|
|
24
25
|
|
|
25
26
|
## Node fields
|
|
26
27
|
|
|
@@ -48,13 +49,14 @@ Links only join matching types. Nodes the entry Lead cannot reach are skipped at
|
|
|
48
49
|
## Running
|
|
49
50
|
|
|
50
51
|
1. The canvas must be enabled (the pill over the canvas); `canvas_workflow` refuses while it is disabled.
|
|
51
|
-
2.
|
|
52
|
-
3.
|
|
53
|
-
4.
|
|
52
|
+
2. You may pass `task` to override the entry Lead's task (handy when reusing a preset), or `reset: true` to discard a paused run and start from scratch.
|
|
53
|
+
3. Subagent nodes run in parallel waves of up to four; the run stops at every Lead node and returns `status: "need-lead"`.
|
|
54
|
+
4. Do that Lead step yourself, then call `canvas_workflow` again with `leadResult` — do not spawn an agent for it.
|
|
55
|
+
5. When a `session-ref` link points into that Lead node the result lists `sessions: [{ label, childId }]`; you may message those agents directly to follow up.
|
|
54
56
|
|
|
55
57
|
## Failures
|
|
56
58
|
|
|
57
|
-
A failing node (unknown model, provider error, a stop reason other than completed) does not abort the run. Its result carries the reason,
|
|
59
|
+
A failing node (unknown model, provider error, a stop reason other than completed) does not abort the run. Its result carries the reason, a node with `fallbackModel` is retried once on that model, and unrecovered failures are listed in `failed: [{ id, label, reason }]` while downstream nodes are warned not to treat the error as valid output. Failed nodes show red in the canvas.
|
|
58
60
|
|
|
59
61
|
## Memory
|
|
60
62
|
|