dsh-workflow-canvas 0.1.2 → 0.2.1
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 +162 -130
- package/README.zh.md +126 -101
- package/client.js +1326 -252
- package/docs/DETAIL.md +44 -28
- package/docs/MECHANISM.md +135 -0
- package/docs/MECHANISM.zh.md +132 -0
- 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/docs/DETAIL.md
CHANGED
|
@@ -1,53 +1,69 @@
|
|
|
1
1
|
# Workflow canvas
|
|
2
2
|
|
|
3
|
-
A
|
|
4
|
-
|
|
5
|
-
spawned for real, Lead nodes pause the run and
|
|
6
|
-
itself.
|
|
3
|
+
A node-graph canvas for orchestrating DSH subagents, docked in the right
|
|
4
|
+
sidebar. Draw a DAG (or load a starter template), enable it, and ask the agent
|
|
5
|
+
to run it — subagent nodes are spawned for real, Lead nodes pause the run and
|
|
6
|
+
are performed by the conversation itself.
|
|
7
7
|
|
|
8
8
|
## Canvas
|
|
9
9
|
|
|
10
10
|
- **Subagent node** (green top bar): spawns a child agent with its own
|
|
11
|
-
`provider` / `model` / `reasoning effort` / capability scope
|
|
12
|
-
reuses the node's session; optional
|
|
11
|
+
`provider` / `model` / `reasoning effort` / capability scope (`standard` or
|
|
12
|
+
`minimal`). Optional **Memory** reuses the node's child session; optional
|
|
13
|
+
**Fallback** retries once on another model.
|
|
13
14
|
- **Lead node** (blue top bar): performed by the conversation, never spawned. The
|
|
14
|
-
entry node carries the task; added Lead nodes carry a role.
|
|
15
|
-
- **Sockets are typed** (`prompt` / `structured` / `session`); a link
|
|
16
|
-
|
|
15
|
+
entry node carries the overall task; added Lead nodes carry a role.
|
|
16
|
+
- **Sockets are typed** (`prompt` / `structured` / `session`); a link only joins
|
|
17
|
+
the same type (either direction), and cycles are blocked immediately. Drag a
|
|
18
|
+
connected input away to detach.
|
|
17
19
|
- Nodes the entry cannot reach are drawn dashed and skipped at run time.
|
|
18
|
-
- Wheel zooms around the cursor;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
- Wheel zooms around the cursor; middle-drag (or `Space` + drag) pans; `⊡ Fit`
|
|
21
|
+
fits all nodes into the visible viewport; the percentage button resets to 100%
|
|
22
|
+
on the Lead.
|
|
23
|
+
- Marquee selection: drag on empty canvas to select multiple nodes; dragging any
|
|
24
|
+
selected node moves the whole selection.
|
|
25
|
+
- Right-drag across links cuts them on release; the swept path and links about to
|
|
26
|
+
be cut highlight in red (toggle under `⋯`).
|
|
27
|
+
- Shortcuts: `Shift+A` opens the Add Node menu at the cursor,
|
|
28
|
+
`Ctrl+D` duplicates selected nodes, `Delete` removes selected nodes, `Ctrl+Z`
|
|
29
|
+
undoes, and `Esc` closes popups. `⋯` also provides `⊞ Auto layout`,
|
|
30
|
+
`⇧ Copy JSON`, and `Visual effects` (`Minimal` / `Standard` / `Vivid`).
|
|
31
|
+
- Click any subagent node's status pill (e.g. `Done ↗`) to jump straight to its
|
|
32
|
+
live subagent chat pane.
|
|
24
33
|
|
|
25
34
|
## Editing from chat
|
|
26
35
|
|
|
27
36
|
| Tool | Effect |
|
|
28
37
|
|---|---|
|
|
38
|
+
| `canvas_inspect` | reads the current session canvas (enabled state, task, nodes, links) |
|
|
29
39
|
| `canvas_generate` | replaces the canvas with a node/edge sketch |
|
|
30
40
|
| `canvas_patch` | edits in place: update nodes by id, add/remove nodes and links |
|
|
31
41
|
|
|
32
|
-
|
|
33
|
-
1.5s. A patch document is applied in merge mode
|
|
42
|
+
`canvas_generate` and `canvas_patch` publish `<workspace>/.dsh/canvas/<session>.json`,
|
|
43
|
+
which the panel polls every 1.5s. A patch document is applied in merge mode so
|
|
44
|
+
node positions survive.
|
|
34
45
|
|
|
35
46
|
## Running
|
|
36
47
|
|
|
37
|
-
Subagent nodes run in parallel waves (up to 4)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
Subagent nodes run in parallel waves (up to 4) via `canvas_workflow`, which also
|
|
49
|
+
supports `task` (override the entry task for template reuse) and `reset: true`
|
|
50
|
+
(discard a paused run). The run stops at every Lead node with
|
|
51
|
+
`status: "need-lead"` and returns the collected results, any unrecovered
|
|
52
|
+
`failed` nodes, plus — for `session` links into that Lead — the upstream child
|
|
53
|
+
session ids so the conversation can keep talking to those agents.
|
|
41
54
|
|
|
42
55
|
## Memory
|
|
43
56
|
|
|
44
|
-
|
|
45
|
-
memory on resumes that child (cold resume after a restart);
|
|
46
|
-
the record is dropped and the node starts fresh.
|
|
57
|
+
`node id → child session id` is persisted on disk, keyed per session. A node
|
|
58
|
+
with memory on resumes that child (including cold resume after a Host restart);
|
|
59
|
+
if the Host refuses, the record is dropped and the node starts fresh.
|
|
47
60
|
|
|
48
61
|
## Limits
|
|
49
62
|
|
|
50
|
-
-
|
|
51
|
-
`dsh web`,
|
|
52
|
-
|
|
53
|
-
-
|
|
63
|
+
- Saved presets live in browser storage and do not auto-sync between the Desktop
|
|
64
|
+
app and `dsh web`, nor across machines (use `⋯ → ⇧ Copy JSON` and `⇩ Import`
|
|
65
|
+
to share or migrate them; starter templates are built in under `☰ Presets`).
|
|
66
|
+
- Lead steps have no tracked state; they show **Runs in chat** (or **Ready** once all upstream subagents finish).
|
|
67
|
+
- Up to 4 nodes run at a time.
|
|
68
|
+
|
|
69
|
+
For architecture diagrams and runtime internals, see [MECHANISM.md](MECHANISM.md) ([中文](MECHANISM.zh.md)).
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Mechanism & Architecture
|
|
2
|
+
|
|
3
|
+
English | [中文](MECHANISM.zh.md)
|
|
4
|
+
|
|
5
|
+
This document explains how **DSH Workflow Canvas** works under the hood: how the
|
|
6
|
+
sidebar canvas (Client) and the DSH Host stay in sync, how the DAG executor runs
|
|
7
|
+
parallel waves and pauses at Lead gates, what the three link types actually do
|
|
8
|
+
at run time, and how memory cold-resume, fallbacks, and live status tracking work.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 1. Architecture: Bidirectional Client ↔ Host Bridge
|
|
13
|
+
|
|
14
|
+
The plugin splits cleanly into two halves:
|
|
15
|
+
- **Client (`client.js`)**: Mounted as a right-sidebar `Workflow` tab (`keepMounted: true`, preserving state while collapsed). Owns visual editing, presets, shortcuts, visual effects, and live status rendering.
|
|
16
|
+
- **Host (`index.js`)**: Registers the bundled `workflow-canvas` skill, the `/canvas-sync` command, and four tools (`canvas_inspect`, `canvas_generate`, `canvas_patch`, `canvas_workflow`). Owns real subagent execution and durable state persistence.
|
|
17
|
+
|
|
18
|
+
```mermaid
|
|
19
|
+
flowchart LR
|
|
20
|
+
subgraph Client["Sidebar Canvas (client.js)"]
|
|
21
|
+
UI["Graph Editor / Presets / Status Pills"]
|
|
22
|
+
LS["localStorage\n(drafts / presets / prefs)"]
|
|
23
|
+
Poll["workspaceFiles Poll\n(every 1.5s)"]
|
|
24
|
+
Cat["subagentCatalog Observer\n(every 700ms while running)"]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
subgraph Host["Host Plugin (index.js)"]
|
|
28
|
+
Sync["/canvas-sync command"]
|
|
29
|
+
Store["Atomic Disk Store\n.workflow-canvas-state.json"]
|
|
30
|
+
Tools["canvas_inspect / generate / patch"]
|
|
31
|
+
Exec["canvas_workflow\nDAG Wave Executor"]
|
|
32
|
+
Sub["ctx.subagents\nReal Child Sessions"]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
UI <--> LS
|
|
36
|
+
UI -- "enable / sync / turn edge\n/canvas-sync" --> Sync
|
|
37
|
+
Sync --> Store
|
|
38
|
+
Tools -- "writes .dsh/canvas/<sid>.json" --> Poll
|
|
39
|
+
Poll -- "timestamp guard + merge layout" --> UI
|
|
40
|
+
Exec <--> Store
|
|
41
|
+
Exec -- "startActivation\nlabel#nodeId" --> Sub
|
|
42
|
+
Sub -- "live status & childId" --> Cat
|
|
43
|
+
Cat --> UI
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Sync & Conflict-Free Editing
|
|
47
|
+
1. **Canvas → Host (`pushPlan` → `/canvas-sync`)**:
|
|
48
|
+
- Triggered when pressing **● ON**, clicking `↥ Sync`, or starting a chat turn while `enabled && dirty`.
|
|
49
|
+
- The Host stores the plan per session in `canvasBySession` and persists it **atomically** (write `.tmp` → `renameSync`) to `<DSH_PROFILE_DIR>/.workflow-canvas-state.json`, so plans and node memory survive Host restarts.
|
|
50
|
+
2. **Host → Canvas (Workspace Bridge + 1.5s Polling)**:
|
|
51
|
+
- `canvas_generate` and `canvas_patch` update `canvasBySession` immediately and publish `<workspace>/.dsh/canvas/<sessionId>.json` stamped with `at`.
|
|
52
|
+
- The client polls via `remote.workspaceFiles.read` every 1.5s.
|
|
53
|
+
- **Local-wins timestamp guard**: every local edit updates `localStorage.at`; a stale bridge file with `doc.at < local.at` is ignored so manual edits are never clobbered.
|
|
54
|
+
- **In-place `merge` mode**: `canvas_patch` publishes `mode: "merge"` together with `removeNodes` and `removeEdges`, so existing node coordinates and the current viewport remain untouched while newly added nodes are placed neatly relative to their upstream parent.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 2. DAG Wave Execution & Lead Handover
|
|
59
|
+
|
|
60
|
+
When `canvas_workflow` runs, `executePlan` executes the following pipeline:
|
|
61
|
+
|
|
62
|
+
1. **Entry stripping & task override**: The entry `lead` node is stripped (`withoutEntry`) and never spawned. If `args.task` is provided, it overrides `plan.task` for this run (allowing blank-task templates to be reused straight from chat).
|
|
63
|
+
2. **Cycle validation (Kahn topological sort)**: `orderNodes` validates the DAG is acyclic (the client also blocks cycles interactively via `wouldCycle`).
|
|
64
|
+
3. **Reachability pruning**: BFS from `'lead'` filters out disconnected nodes. Unreachable nodes are drawn dashed, skipped at run time, and listed in `skipped` without burning tokens.
|
|
65
|
+
4. **Parallel waves & resumable pauses**:
|
|
66
|
+
- Progress per session is tracked in `runBySession` (`{ sig, done, results, labels, failed }`).
|
|
67
|
+
- Each iteration finds every remaining node whose dependencies are all in `done`:
|
|
68
|
+
- Ready **subagent nodes** run in parallel via `Promise.all`, up to **4 per wave** (`MAX_PARALLEL = 4`).
|
|
69
|
+
- When only **Lead nodes** are ready and no `leadResult` was passed, execution pauses with `status: "need-lead"`, returning collected results, any failures, and `session` child IDs.
|
|
70
|
+
- The conversation performs that Lead step and calls `canvas_workflow({ leadResult: "..." })` to resume seamlessly.
|
|
71
|
+
- Changing the graph (different plan `sig`) or passing `reset: true` discards paused progress and restarts cleanly.
|
|
72
|
+
|
|
73
|
+
```mermaid
|
|
74
|
+
sequenceDiagram
|
|
75
|
+
participant Chat as Main Chat (Lead)
|
|
76
|
+
participant Exec as Host Executor (executePlan)
|
|
77
|
+
participant Sub1 as Subagents Wave 1 (up to 4)
|
|
78
|
+
participant Sub2 as Subagents Wave 2 (up to 4)
|
|
79
|
+
|
|
80
|
+
Chat->>Exec: canvas_workflow()
|
|
81
|
+
Exec->>Sub1: Parallel startActivation (Nodes A, B)
|
|
82
|
+
Sub1-->>Exec: Results (text or structured JSON)
|
|
83
|
+
Note over Exec: Reaches Lead node — pauses with state kept
|
|
84
|
+
Exec-->>Chat: status="need-lead" + results + sessions (purple links)
|
|
85
|
+
Note over Chat: Lead decides (can follow up with purple sessions)
|
|
86
|
+
Chat->>Exec: canvas_workflow(leadResult="approved plan")
|
|
87
|
+
Exec->>Sub2: Parallel startActivation (Nodes C, D)
|
|
88
|
+
Sub2-->>Exec: Results
|
|
89
|
+
Exec-->>Chat: status="done" + results + failed + skipped
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## 3. Runtime Semantics of the Three Link Types
|
|
95
|
+
|
|
96
|
+
The three link types are distinct runtime channels, not just visual colour coding:
|
|
97
|
+
|
|
98
|
+
| Link Type | Colour | Runtime Behaviour |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| `prompt` | grey | Appends upstream text as a labelled section inside the downstream node's `briefFor` prompt. |
|
|
101
|
+
| `structured` | blue | `spawnOnce` detects any outgoing `structured` edge and **automatically** injects a JSON `outputSchema` (`type: "object", additionalProperties: true`) into `startActivation`. `textOf` extracts `result.structured` as JSON and wraps it in a `` ```json `` block for downstream consumers. |
|
|
102
|
+
| `session` (`session-ref`) | purple | **Into a subagent**: resolves the upstream `childId` (preferring in-process `liveChild`, falling back to disk-persisted `childByNode`) and passes it to `startActivation({ ...spec, childId })`, running the downstream step **inside the upstream child's live conversation**.<br>**Into a Lead**: reports `sessions: [{ label, childId }]` in `need-lead` so the main chat can message that subagent directly for follow-ups or fixes. |
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 4. Memory Cold-Resume, Fallback Retry & Failure Isolation
|
|
107
|
+
|
|
108
|
+
### Memory (`keepSession`) & Cold Resume
|
|
109
|
+
- The Host keeps two maps keyed by `<sessionId>::<nodeId>`: `liveChild` (current process) and `childByNode` (persisted in `.workflow-canvas-state.json`).
|
|
110
|
+
- A node with `keepSession: true` resumes `liveChild || childByNode`.
|
|
111
|
+
- After a Host restart, `childByNode` enables **cold resume**; if the Host refuses activation (`activation-failed`), the stale ID is dropped and the node starts a fresh child session automatically.
|
|
112
|
+
|
|
113
|
+
### Fallback Model & Failure Reporting
|
|
114
|
+
- If a subagent fails to start (`activation-failed`), throws (`run-failed`), or stops with `stopReason !== 'completed'`:
|
|
115
|
+
1. When `fallbackModel` / `fallbackProvider` is configured, it retries once on the fallback route.
|
|
116
|
+
2. Unrecovered failures do **not** abort unrelated branches — they are recorded in `failed: [{ id, label, reason }]`.
|
|
117
|
+
3. Downstream prompts built by `briefFor` receive an explicit upstream-failure warning so downstream agents do not hallucinate on error text, and `canvas_workflow` highlights failed nodes prominently in its rendered summary.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## 5. Live Status Pills & Click-to-Open Subagent Chats
|
|
122
|
+
|
|
123
|
+
1. **Node tagging**: `spawnOnce` tags every spawned child with `label: "<node.label>#<node.id>"`.
|
|
124
|
+
2. **Catalog projection tracking**: While a turn runs (every 700ms, plus on mount and turn completion), the client reads `subagentCatalog` and `subagentTiming` from `sessions.list`, matching `/#([^\s#]+)$/` to map each child back to its canvas node (`running` / `done` / `failed`) and latest `childId`.
|
|
125
|
+
3. **Lead `⏸ Ready` detection**: For non-entry Lead nodes, the client highlights **`⏸ Ready`** as soon as all upstream subagents are `done` and no downstream node has started yet.
|
|
126
|
+
4. **Jump to live chat**: Clicking any subagent's status pill (e.g. `Running ↗`, `Done ↗`) opens `dsh-resource://subagentchat/session/<childId>?parent=<sessionId>&mode=<mode>` via `sidebarRight.openResource(..., { kind: 'subagentchat', preferNewPane: true })`.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 6. Skill & Tool Discovery
|
|
131
|
+
|
|
132
|
+
Because DSH loads skills on demand rather than auto-injecting every plugin guide, the plugin combines three layers for reliable agent adoption:
|
|
133
|
+
1. **Bundled skill provider**: `index.js` registers `workflow-canvas` via `ctx.skills.registerProvider` with bilingual trigger keywords (`canvas / workflow / pipeline / 画布 / 工作流 / 编排`) and all four `canvas_*` tool names, serving [skills/workflow-canvas/SKILL.md](../skills/workflow-canvas/SKILL.md).
|
|
134
|
+
2. **Tool-level pointers**: Every `canvas_*` tool description reminds the model to load the `workflow-canvas` skill first, and tool schemas are always in context.
|
|
135
|
+
3. **Optional `AGENTS.md` rule**: Adding a single line in a project's `AGENTS.md` guarantees deterministic skill loading in every session.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# 工作机制与架构原理
|
|
2
|
+
|
|
3
|
+
中文 | [English](MECHANISM.md)
|
|
4
|
+
|
|
5
|
+
本文说明 **DSH Workflow Canvas** 的底层实现机制:侧边栏画布(Client)如何与 DSH Host 双向同步、DAG 执行器如何按波次调度子代理与暂停交接、三种连线在运行时的真实差异,以及记忆冷恢复、失败降级和实时状态追踪的工作方式。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. 整体架构:Client 与 Host 的双向通道
|
|
10
|
+
|
|
11
|
+
插件由两部分组成:
|
|
12
|
+
- **客户端(`client.js`)**:挂载在 DSH 右侧边栏的 `Workflow` 标签页(`keepMounted: true`,收起侧边栏时保留画布状态),负责图形编辑、交互、预设存储与实时状态渲染。
|
|
13
|
+
- **Host 侧(`index.js`)**:注册技能 `workflow-canvas`、同步命令 `/canvas-sync`,以及 4 个工具(`canvas_inspect`、`canvas_generate`、`canvas_patch`、`canvas_workflow`),负责真实调度子代理和持久化执行状态。
|
|
14
|
+
|
|
15
|
+
```mermaid
|
|
16
|
+
flowchart LR
|
|
17
|
+
subgraph Client["客户端画布 (client.js)"]
|
|
18
|
+
UI["画布编辑 / 预设 / 状态胶囊"]
|
|
19
|
+
LS["localStorage\n(画布草稿 / 预设 / 偏好)"]
|
|
20
|
+
Poll["workspaceFiles 轮询\n(每 1.5s)"]
|
|
21
|
+
Cat["subagentCatalog 投影监听\n(运行中每 700ms)"]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
subgraph Host["Host 插件 (index.js)"]
|
|
25
|
+
Sync["/canvas-sync 命令"]
|
|
26
|
+
Store["原子持久化\n.workflow-canvas-state.json"]
|
|
27
|
+
Tools["canvas_inspect / generate / patch"]
|
|
28
|
+
Exec["canvas_workflow\nDAG 波次执行器"]
|
|
29
|
+
Sub["ctx.subagents\n真实子代理会话"]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
UI <--> LS
|
|
33
|
+
UI -- "启用 / 同步 / 轮次边界\n/canvas-sync" --> Sync
|
|
34
|
+
Sync --> Store
|
|
35
|
+
Tools -- "写入 .dsh/canvas/<sid>.json" --> Poll
|
|
36
|
+
Poll -- "时间戳比对 + merge 保留排版" --> UI
|
|
37
|
+
Exec <--> Store
|
|
38
|
+
Exec -- "startActivation\nlabel#nodeId" --> Sub
|
|
39
|
+
Sub -- "状态与 childId" --> Cat
|
|
40
|
+
Cat --> UI
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 双向同步与防覆盖机制
|
|
44
|
+
1. **画布 → Host(`pushPlan` → `/canvas-sync`)**:
|
|
45
|
+
- 用户点击 **● 已启用**、手动点击 `↥ 同步`,或在已启用且有未同步改动(`dirty`)时发送新消息触发轮次切换,客户端会把当前有效执行计划编码后通过 `/canvas-sync` 推送给 Host;
|
|
46
|
+
- Host 存入按会话隔离的 `canvasBySession`,并通过写 `.tmp` 再 `renameSync` 的方式**原子落盘**到 `<DSH_PROFILE_DIR>/.workflow-canvas-state.json`,因此重启 Host 后画布计划与节点记忆都不会丢失。
|
|
47
|
+
2. **Host → 画布(工作区桥接文件 + 1.5s 轮询)**:
|
|
48
|
+
- 当对话调用 `canvas_generate` 或 `canvas_patch` 时,Host 一方面直接更新 `canvasBySession`,另一方面把文档写入 `<workspace>/.dsh/canvas/<sessionId>.json`(携带时间戳 `at`);
|
|
49
|
+
- 客户端面板每 1.5 秒通过 `remote.workspaceFiles.read` 读取该文件;
|
|
50
|
+
- **本地优先防覆盖**:每次手动编辑都会给 `localStorage` 记上时间戳 `at`,若本地时间晚于远端文件,旧文件不会覆盖用户的最新改动;
|
|
51
|
+
- **就地合并(`merge` 模式)**:`canvas_patch` 下发 `mode: "merge"` 与 `removeNodes` / `removeEdges`,客户端按节点 ID 就地合并字段,**完整保留用户手摆坐标与当前视角**,仅给新增节点按上游位置智能排布。
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 2. DAG 波次执行与 Lead 暂停交接
|
|
56
|
+
|
|
57
|
+
调用 `canvas_workflow` 时,Host 的 `executePlan` 按以下流水线运行:
|
|
58
|
+
|
|
59
|
+
1. **入口剥离与任务解析**:剥离作为总任务容器的入口节点 `lead`(永不作为子代理执行)。若调用时传入 `task` 参数,则覆盖入口 Lead 的总任务(方便直接复用空任务预设)。
|
|
60
|
+
2. **成环校验(Kahn 拓扑排序)**:用 `orderNodes` 校验 DAG 无环(客户端手动画线时也会用 `wouldCycle` 即时拦截)。
|
|
61
|
+
3. **可达性剪枝**:从入口 `lead` 做广度优先遍历,不可达的孤立节点在画布上显示为虚线,运行时被自动跳过并列入 `skipped`,不会消耗 Token。
|
|
62
|
+
4. **波次并行与暂停续跑**:
|
|
63
|
+
- 每个会话的运行进度保存在 `runBySession`(包含计划签名 `sig`、`done`、`results`、`labels`、`failed`);
|
|
64
|
+
- 每轮循环找出所有依赖均已完成的就绪节点:
|
|
65
|
+
- 若存在就绪的**子代理节点**,每次最多取 **4 个**(`MAX_PARALLEL = 4`)通过 `Promise.all` 并行执行;
|
|
66
|
+
- 若就绪节点只剩 **Lead 节点**且尚未收到 `leadResult`,执行器立刻暂停并返回 `status: "need-lead"`,把已收集的上游结果、失败汇总以及紫线关联的子会话 `childId` 全部交回主对话;
|
|
67
|
+
- 主对话完成该 Lead 步骤后,再次调用 `canvas_workflow({ leadResult: "..." })`,执行器从断点无缝继续下一波次;
|
|
68
|
+
- 若中途修改了画布(计划签名 `sig` 变化)或显式传入 `reset: true`,则丢弃旧进度从头重跑。
|
|
69
|
+
|
|
70
|
+
```mermaid
|
|
71
|
+
sequenceDiagram
|
|
72
|
+
participant Chat as 主对话 (Lead)
|
|
73
|
+
participant Exec as Host 执行器 (executePlan)
|
|
74
|
+
participant Sub1 as 子代理 Wave 1 (并行 ≤4)
|
|
75
|
+
participant Sub2 as 子代理 Wave 2 (并行 ≤4)
|
|
76
|
+
|
|
77
|
+
Chat->>Exec: canvas_workflow()
|
|
78
|
+
Exec->>Sub1: 并行 startActivation (节点 A, B)
|
|
79
|
+
Sub1-->>Exec: 返回结果 (或结构化 JSON)
|
|
80
|
+
Note over Exec: 遇到中间 Lead 节点,保留进度暂停
|
|
81
|
+
Exec-->>Chat: status="need-lead" + results + sessions(紫线)
|
|
82
|
+
Note over Chat: Lead 拍板(必要时可直接追问紫线子代理)
|
|
83
|
+
Chat->>Exec: canvas_workflow(leadResult="拍板结论")
|
|
84
|
+
Exec->>Sub2: 并行 startActivation (节点 C, D)
|
|
85
|
+
Sub2-->>Exec: 返回结果
|
|
86
|
+
Exec-->>Chat: status="done" + results + failed + skipped
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## 3. 三种连线的底层语义差异
|
|
92
|
+
|
|
93
|
+
三种连线不只是视觉颜色区分,在 Host 执行器里对应完全不同的数据与会话通道:
|
|
94
|
+
|
|
95
|
+
| 连线类型 | 颜色 | 运行时底层行为 |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `prompt` | 灰 | 将上游输出文本作为独立 Markdown 章节拼入下游 `briefFor` prompt。 |
|
|
98
|
+
| `structured` | 蓝 | `spawnOnce` 检测到节点有 `structured` 出边时,**自动**在 `startActivation` 中注入 JSON `outputSchema`(`type: "object", additionalProperties: true`);执行完成后通过 `textOf` 优先提取解析后的 `result.structured` 序列化为 JSON,再以 `` ```json `` 代码块交给下游。 |
|
|
99
|
+
| `session` (`session-ref`) | 紫 | **指向子代理时**:查找上游节点的 `childId`(优先进程内 `liveChild`,重启后兜底读落盘 `childByNode`),直接作为 `childId` 传入 `startActivation`,下游**在上游的同一个子会话里继续跑**(完整保留上游上下文与工具历史,无需重新读代码库);<br>**指向 Lead 时**:在 `need-lead` 返回的 `sessions: [{ label, childId }]` 里上报该子会话 ID,主对话可直接给对应子代理发消息追问或下达补修。 |
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## 4. 记忆冷恢复、备用模型与失败隔离
|
|
104
|
+
|
|
105
|
+
### 记忆(`keepSession`)与冷恢复
|
|
106
|
+
- Host 维护两层映射:`liveChild`(当前进程内活跃会话)和 `childByNode`(随 `.workflow-canvas-state.json` 落盘,按 `sessionId::nodeId` 隔离)。
|
|
107
|
+
- 节点开启记忆后,再次运行时按 `liveChild → childByNode` 顺序尝试复用旧 `childId`。
|
|
108
|
+
- **Host 重启后的冷恢复**:重启后 `liveChild` 为空,执行器会用落盘的 `childByNode` 尝试冷恢复;如果 Host 拒绝激活(`activation-failed`),插件会自动清除过期记录并无缝改开新会话,不会卡死工作流。
|
|
109
|
+
|
|
110
|
+
### 备用模型降级与失败透传
|
|
111
|
+
- 任意子代理出现启动失败(`activation-failed`)、运行异常(`run-failed`)或 `stopReason !== 'completed'` 时:
|
|
112
|
+
1. 若配置了 `fallbackModel` / `fallbackProvider`,自动用备用模型重试一次;
|
|
113
|
+
2. 若未配置备用模型或重试仍失败,**不中断整张图的其他分支**,而是把该节点记入 `failed: [{ id, label, reason }]`;
|
|
114
|
+
3. 同时在下游节点的 `briefFor` 上游段落头部插入显式警告,提示下游不要把报错文本当作正常结论继续编造,并在最终返回及 Lead 暂停输出中高亮列出所有失败节点。
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## 5. 实时状态胶囊与点击直达子会话
|
|
119
|
+
|
|
120
|
+
1. **节点反查标记**:`spawnOnce` 启动子代理时,固定把 `spec.label` 设为 `<节点名>#<nodeId>`。
|
|
121
|
+
2. **状态实时追踪**:客户端监听当前会话的运行态,在对话运行期间每 700ms(以及挂载与每轮结束时)读取 `sessions.list` 投影中的 `subagentCatalog` 与 `subagentTiming`,用末尾正则 `/#([^\s#]+)$/` 精准反查节点状态(`running` / `done` / `failed`)以及对应的 `childId`。
|
|
122
|
+
3. **Lead「待拍板」自动高亮**:对于非入口 Lead 节点,客户端检测到其所有上游子代理均为 `done` 且下游尚未启动时,自动亮起蓝色 **`⏸ 待拍板`** 提示。
|
|
123
|
+
4. **一键直达真实子对话**:点击任意子代理节点右下角状态胶囊(如 `进行中 ↗` / `已完成 ↗`),客户端调用 `sidebarRight.openResource('dsh-resource://subagentchat/session/<childId>?parent=<sessionId>&mode=<mode>', { kind: 'subagentchat', preferNewPane: true })`,直接在新侧边窗格打开该子代理的真实对话记录。
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 6. Skill 与工具引导机制
|
|
128
|
+
|
|
129
|
+
DSH 不会强制把所有插件文档塞进每次系统提示词,因此插件采用三层协同引导,让模型能稳定、准确地调用画布工具:
|
|
130
|
+
1. **Skill 目录注册**:`index.js` 通过 `ctx.skills.registerProvider` 注册内置技能 `workflow-canvas`,摘要中包含中英触发词(`canvas / workflow / pipeline / 画布 / 工作流 / 编排`)与 4 个 `canvas_*` 工具名;正文读取自 [skills/workflow-canvas/SKILL.md](../skills/workflow-canvas/SKILL.md)。
|
|
131
|
+
2. **工具说明互指**:`canvas_inspect`、`canvas_generate`、`canvas_patch`、`canvas_workflow` 的工具描述均提示模型按需先加载 `workflow-canvas` 技能,而工具定义始终在上下文中。
|
|
132
|
+
3. **项目级锁定(可选)**:在项目 `AGENTS.md` 中加一行显式指令,可在任意对话中实现最高触发稳定性。
|
|
Binary file
|
|
Binary file
|