pi-web-ui 0.110.0 → 0.112.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/CHANGELOG.md +55 -2
- package/bin/frp-manager.mjs +92 -7
- package/dist/server/agent-formatters.js +10 -6
- package/dist/server/agent-service-daemon.js +27 -9
- package/dist/server/agent-service.js +1442 -489
- package/dist/server/approval-audit.js +119 -0
- package/dist/server/approval-default-rules.js +4 -4
- package/dist/server/approval-rules.js +42 -17
- package/dist/server/attachment-store.js +8 -8
- package/dist/server/attachments.js +114 -31
- package/dist/server/canvas/canvas-agent-tool.js +593 -0
- package/dist/server/canvas/canvas-cards.js +144 -0
- package/dist/server/canvas/canvas-dispatch.js +155 -0
- package/dist/server/canvas/canvas-orchestrator.js +1094 -0
- package/dist/server/canvas/canvas-store.js +260 -0
- package/dist/server/client-id-utils.js +2 -2
- package/dist/server/client-state.js +52 -0
- package/dist/server/context-budget.js +6 -3
- package/dist/server/delegate-mode.js +6 -3
- package/dist/server/delegate-task.js +18 -3
- package/dist/server/dispatch-domain-handlers.js +3 -0
- package/dist/server/dsh/dsh-agent-service.js +357 -20
- package/dist/server/dsh/dsh-client.js +10 -1
- package/dist/server/dsh/dsh-serialize.js +1 -1
- package/dist/server/dsh/runtime/goal-rpc.mjs +128 -3
- package/dist/server/dsh/runtime/launcher.mjs +59 -5
- package/dist/server/dsh/runtime/runtime-root.mjs +353 -40
- package/dist/server/durable/durable-agent-service.js +3 -0
- package/dist/server/durable/durable-driver.js +75 -9
- package/dist/server/durable/durable-loader.js +1 -14
- package/dist/server/durable/durable-models.js +107 -0
- package/dist/server/durable/index.js +0 -4
- package/dist/server/engine-probe.js +76 -37
- package/dist/server/eval-tool.js +28 -3
- package/dist/server/file-transfer-routes.js +8 -2
- package/dist/server/files-service.js +10 -2
- package/dist/server/goal-service.js +7 -0
- package/dist/server/index.js +480 -59
- package/dist/server/json-utils.js +57 -0
- package/dist/server/marker-service.js +11 -1
- package/dist/server/markers/marker.js +45 -4
- package/dist/server/markers/registry.js +31 -0
- package/dist/server/message-window.js +44 -8
- package/dist/server/model-admin.js +68 -106
- package/dist/server/model-resolver.js +66 -44
- package/dist/server/office-parse.js +394 -7
- package/dist/server/plan-manager.js +3 -6
- package/dist/server/plugin-api-catalog.js +7 -0
- package/dist/server/plugin-assets.js +238 -0
- package/dist/server/plugin-facilities.js +22 -0
- package/dist/server/plugin-ui-slots.js +1 -0
- package/dist/server/plugins.js +296 -25
- package/dist/server/preset-fields.js +60 -2
- package/dist/server/preset-share.js +8 -2
- package/dist/server/prompt-assembler.js +9 -2
- package/dist/server/prompt-composer.js +84 -44
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/queue-utils.js +156 -0
- package/dist/server/remote-ssh-service.js +16 -4
- package/dist/server/sdk-builtin-extensions.js +85 -0
- package/dist/server/server-config.js +66 -0
- package/dist/server/session-parser.js +49 -13
- package/dist/server/session-search.js +4 -3
- package/dist/server/settings-service.js +56 -7
- package/dist/server/soft-cap.js +8 -6
- package/dist/server/subagent-helpers.js +22 -1
- package/dist/server/subagent-notify.js +93 -0
- package/dist/server/subagent-security-gate.js +282 -0
- package/dist/server/subagent-templates.js +8 -0
- package/dist/server/subagents.js +144 -17
- package/dist/server/terminals.js +196 -8
- package/dist/server/text-quote.js +61 -0
- package/dist/server/tool-approval.js +63 -4
- package/dist/server/tool-guards.js +208 -0
- package/dist/server/tool-host-factories.js +46 -10
- package/dist/server/tool-manager.js +30 -0
- package/dist/server/tool-overrides.js +21 -11
- package/dist/server/univer-service.js +448 -0
- package/dist/server/universal/driver-base.js +8 -0
- package/dist/server/universal/drivers/claude-code.js +39 -5
- package/dist/server/universal/drivers/dsh-driver.js +3 -0
- package/dist/server/universal/index.js +0 -1
- package/dist/server/universal/universal-attachments.js +5 -0
- package/dist/server/universal/universal-service.js +5 -0
- package/dist/server/universal/universal-session.js +114 -21
- package/dist/server/update-check.js +90 -5
- package/dist/server/workspace-snapshot.js +33 -2
- package/dist/server/worktree-service.js +189 -1
- package/docs/dsh-engine.md +493 -0
- package/package.json +5 -4
- package/plugin-sdk/client-utils.mjs +192 -5
- package/plugin-sdk/index.d.ts +80 -1
- package/plugin-sdk/index.mjs +99 -0
- package/plugin-sdk/package.json +5 -1
- package/plugins/catalog.json +33 -0
- package/themes/aetheris.css +114 -0
- package/themes/bases/obsidian-base.css +216 -0
- package/themes/md-preview.css +113 -0
- package/themes/wallpaper-flow.css +182 -0
- package/web/dist/assets/{TerminalPanel-BHjM6pzY.js → TerminalPanel-Dqr1dK28.js} +2 -2
- package/web/dist/assets/index-Dh8BX-lP.js +720 -0
- package/web/dist/assets/index-DqrC1I_a.css +1 -0
- package/web/dist/assets/{markdown-DXwnfD9T.js → markdown-BJZlfvdP.js} +24 -24
- package/web/dist/assets/markdown-DSNUiDzp.css +1 -0
- package/web/dist/index.html +13 -4
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/es-DIDZppIF.js +0 -2
- package/web/dist/assets/index-DwInT7-a.css +0 -1
- package/web/dist/assets/index-GfVFIgIL.js +0 -693
- package/web/dist/assets/markdown-CCeVCbci.css +0 -1
|
@@ -0,0 +1,1094 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* canvas-orchestrator.ts — 多引擎、多模型、多目录画布协作调度器。
|
|
3
|
+
*
|
|
4
|
+
* 核心功能:
|
|
5
|
+
* 1. 管理运行中的 CanvasGraph 拓扑生命周期;
|
|
6
|
+
* 2. 调度四大引擎(Pi / DSH / Claude Code / Durable)的并发会话;
|
|
7
|
+
* 3. 隔离各节点的工作目录(相对子目录,或自动启用 Git Worktree 物理隔离);
|
|
8
|
+
* 4. 沿连线执行契约化数据流转(Handoff);
|
|
9
|
+
* 5. 审查回路与防死循环熔断保护(Review Loop Breaker)。
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
12
|
+
import { isAbsolute, resolve, sep } from "node:path";
|
|
13
|
+
import { isRemoteWorkspaceUri } from "../remote-ssh-service.js";
|
|
14
|
+
import { existsSync as fsExistsSync, mkdirSync as fsMkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { join as pathJoin } from "node:path";
|
|
16
|
+
import { saveCanvasRuntimeState, loadCanvasGraph, saveCanvasGraph, isSafeGraphId } from "./canvas-store.js";
|
|
17
|
+
import { isInsideGitRepo, normalizeWirePath } from "../worktree-service.js";
|
|
18
|
+
import { execFile } from "node:child_process";
|
|
19
|
+
import { promisify } from "node:util";
|
|
20
|
+
const exec = promisify(execFile);
|
|
21
|
+
export class CanvasOrchestrator {
|
|
22
|
+
dataDir;
|
|
23
|
+
/** 每个节点的对话实录:key=`${graphId}:${nodeId}`,内存常驻 + 落盘持久化(canvas/logs/)。上限 300 条防爆内存。 */
|
|
24
|
+
nodeLogs = new Map();
|
|
25
|
+
/** 复用当前对话流:sink 里收到的标准 UiMessage 按节点缓存,前端抽屉直接复用 Message 组件渲染。 */
|
|
26
|
+
nodeMessages = new Map();
|
|
27
|
+
logSeq = 0;
|
|
28
|
+
baseCwd;
|
|
29
|
+
/** 运行期项目基准(graphId -> 目录):相对 cwd 相对它解析;缺省=服务启动目录。
|
|
30
|
+
* 谁先启动这次运行谁定:UI 按钮=点击者当前项目,AI 工具=所有者对话 cwd。 */
|
|
31
|
+
runBaseByGraph = new Map();
|
|
32
|
+
engineGetter;
|
|
33
|
+
broadcaster = null;
|
|
34
|
+
activeExecutions = new Map();
|
|
35
|
+
runningGraphs = new Map();
|
|
36
|
+
constructor(baseCwd, dataDir, engineGetter) {
|
|
37
|
+
this.baseCwd = baseCwd;
|
|
38
|
+
this.dataDir = dataDir;
|
|
39
|
+
this.engineGetter = engineGetter;
|
|
40
|
+
}
|
|
41
|
+
setBroadcaster(broadcaster) {
|
|
42
|
+
this.broadcaster = broadcaster;
|
|
43
|
+
}
|
|
44
|
+
broadcast(msg) {
|
|
45
|
+
try {
|
|
46
|
+
this.broadcaster?.(msg);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// 背压/断连导致广播失败不得污染调度主路径(故障注入 S3 命中)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
logKey(graphId, nodeId) {
|
|
53
|
+
return `${graphId}:${nodeId}`;
|
|
54
|
+
}
|
|
55
|
+
/** 读取节点对话实录(内存命中优先,否则尝试从落盘恢复) */
|
|
56
|
+
getNodeLog(graphId, nodeId) {
|
|
57
|
+
const key = this.logKey(graphId, nodeId);
|
|
58
|
+
const mem = this.nodeLogs.get(key);
|
|
59
|
+
if (mem)
|
|
60
|
+
return [...mem];
|
|
61
|
+
try {
|
|
62
|
+
const file = pathJoin(this.dataDir, "canvas", "logs", `${graphId}__${nodeId}.json`);
|
|
63
|
+
if (fsExistsSync(file)) {
|
|
64
|
+
const arr = JSON.parse(readFileSync(file, "utf8"));
|
|
65
|
+
if (Array.isArray(arr)) {
|
|
66
|
+
const capped = arr.slice(-300);
|
|
67
|
+
this.nodeLogs.set(key, capped);
|
|
68
|
+
return [...capped];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// 读盘失败回落空日志
|
|
74
|
+
}
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
/** 读取节点复用的对话流消息(与主对话同构的 UiMessage[],上限 200 条) */
|
|
78
|
+
getNodeMessages(graphId, nodeId) {
|
|
79
|
+
const mem = this.nodeMessages.get(this.logKey(graphId, nodeId));
|
|
80
|
+
return mem ? [...mem] : [];
|
|
81
|
+
}
|
|
82
|
+
/** 清空节点对话实录(内存 + 落盘) */
|
|
83
|
+
clearNodeLog(graphId, nodeId) {
|
|
84
|
+
const key = this.logKey(graphId, nodeId);
|
|
85
|
+
this.nodeLogs.delete(key);
|
|
86
|
+
this.nodeMessages.delete(key);
|
|
87
|
+
try {
|
|
88
|
+
const file = pathJoin(this.dataDir, "canvas", "logs", `${graphId}__${nodeId}.json`);
|
|
89
|
+
if (fsExistsSync(file))
|
|
90
|
+
unlinkSync(file);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// 忽略清理失败
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** 追加一条对话实录并广播 + 节流落盘 */
|
|
97
|
+
appendLog(graphId, nodeId, kind, text, toolName) {
|
|
98
|
+
if (!isSafeGraphId(graphId) || !isSafeGraphId(nodeId))
|
|
99
|
+
return;
|
|
100
|
+
const clean = (text ?? "").slice(0, 2000) || "(空)";
|
|
101
|
+
const entry = {
|
|
102
|
+
id: `log_${Date.now()}_${(this.logSeq += 1)}`,
|
|
103
|
+
ts: Date.now(),
|
|
104
|
+
kind,
|
|
105
|
+
text: clean,
|
|
106
|
+
...(toolName ? { toolName: toolName.slice(0, 80) } : {}),
|
|
107
|
+
};
|
|
108
|
+
const key = this.logKey(graphId, nodeId);
|
|
109
|
+
const list = this.nodeLogs.get(key) ?? [];
|
|
110
|
+
list.push(entry);
|
|
111
|
+
while (list.length > 300)
|
|
112
|
+
list.shift();
|
|
113
|
+
this.nodeLogs.set(key, list);
|
|
114
|
+
this.broadcast({ type: "canvas_node_log", graphId, nodeId, entry });
|
|
115
|
+
// 每 10 条落盘一次,避免高频 IO
|
|
116
|
+
if (list.length % 10 === 0 || kind === "error" || kind === "done") {
|
|
117
|
+
try {
|
|
118
|
+
const dir = pathJoin(this.dataDir, "canvas", "logs");
|
|
119
|
+
if (!fsExistsSync(dir))
|
|
120
|
+
fsMkdirSync(dir, { recursive: true });
|
|
121
|
+
writeFileSync(pathJoin(dir, `${graphId}__${nodeId}.json`), JSON.stringify(list.slice(-300)));
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// 落盘失败不影响主流程
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** 缓存并转播复用的对话流消息(直接透传主对话同构 UiMessage,前端复用 Message 组件渲染) */
|
|
129
|
+
pushNodeMessages(graphId, nodeId, incoming) {
|
|
130
|
+
if (!Array.isArray(incoming) || incoming.length === 0)
|
|
131
|
+
return;
|
|
132
|
+
const key = this.logKey(graphId, nodeId);
|
|
133
|
+
const list = this.nodeMessages.get(key) ?? [];
|
|
134
|
+
// 按 id 去重追加,保持 200 条上限
|
|
135
|
+
const seen = new Set(list.map((m) => m.id));
|
|
136
|
+
for (const m of incoming) {
|
|
137
|
+
if (!m || typeof m.id !== "string" || seen.has(m.id))
|
|
138
|
+
continue;
|
|
139
|
+
seen.add(m.id);
|
|
140
|
+
list.push(m);
|
|
141
|
+
}
|
|
142
|
+
while (list.length > 200)
|
|
143
|
+
list.shift();
|
|
144
|
+
this.nodeMessages.set(key, list);
|
|
145
|
+
this.broadcast({
|
|
146
|
+
type: "canvas_node_messages",
|
|
147
|
+
graphId,
|
|
148
|
+
nodeId,
|
|
149
|
+
appended: incoming.slice(-20),
|
|
150
|
+
streaming: true,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/** 确保该节点所需的真实工作目录(支持 Worktree 隔离) */
|
|
154
|
+
/** 有效运行基准:显式 runBase(本地绝对路径或远程 SSH URI)优先,否则服务启动目录 */
|
|
155
|
+
pickRunBase(runBase) {
|
|
156
|
+
const r = (runBase ?? "").trim();
|
|
157
|
+
if (r && (isRemoteWorkspaceUri(r) || isAbsolute(r)))
|
|
158
|
+
return isRemoteWorkspaceUri(r) ? r : resolve(r);
|
|
159
|
+
return resolve(this.baseCwd);
|
|
160
|
+
}
|
|
161
|
+
async resolveNodeCwd(node, runBase) {
|
|
162
|
+
// node.id 用于 worktree 目录名与 git 分支名;node.cwd 允许工作区外任意绝对路径。
|
|
163
|
+
if (!isSafeGraphId(node.id))
|
|
164
|
+
throw new Error(`Invalid canvas node id: ${JSON.stringify(node.id)}`);
|
|
165
|
+
const raw = (node.cwd ?? "").trim();
|
|
166
|
+
// 远程 SSH 工作区路径(如 ssh://user@host:22/path)原生透传给引擎,不走本地目录与 worktree
|
|
167
|
+
if (isRemoteWorkspaceUri(raw)) {
|
|
168
|
+
return raw;
|
|
169
|
+
}
|
|
170
|
+
const base = this.pickRunBase(runBase);
|
|
171
|
+
if (isRemoteWorkspaceUri(base)) {
|
|
172
|
+
return !raw || raw === "." ? base : `${base.replace(/\/+$/, "")}/${raw.replace(/^\/+/, "")}`;
|
|
173
|
+
}
|
|
174
|
+
// 空 = 运行基准;相对路径 = 相对运行基准(默认服务工作区,运行时可被当前项目覆盖);绝对路径 = 原样使用(不再拦截越出工作区)。
|
|
175
|
+
const targetDir = !raw ? base : isAbsolute(raw) ? resolve(raw) : resolve(base, raw);
|
|
176
|
+
if (!node.useWorktree) {
|
|
177
|
+
if (!existsSync(targetDir)) {
|
|
178
|
+
mkdirSync(targetDir, { recursive: true });
|
|
179
|
+
}
|
|
180
|
+
return targetDir;
|
|
181
|
+
}
|
|
182
|
+
// 启用 Git Worktree 隔离机制(仅工作区内目录才做 worktree;工作区外绝对路径直接使用目标目录)
|
|
183
|
+
const basePrefix = base.endsWith(sep) ? base : base + sep;
|
|
184
|
+
const insideBase = targetDir === base || targetDir.startsWith(basePrefix);
|
|
185
|
+
if (!insideBase) {
|
|
186
|
+
return targetDir;
|
|
187
|
+
}
|
|
188
|
+
const isGit = await isInsideGitRepo(base);
|
|
189
|
+
if (!isGit) {
|
|
190
|
+
// 如果不是 git 仓库,回落到普通子目录
|
|
191
|
+
return targetDir;
|
|
192
|
+
}
|
|
193
|
+
const worktreeDir = resolve(base, ".worktrees", `canvas-${node.id}`);
|
|
194
|
+
if (!existsSync(worktreeDir)) {
|
|
195
|
+
mkdirSync(resolve(base, ".worktrees"), { recursive: true });
|
|
196
|
+
try {
|
|
197
|
+
await exec("git", ["worktree", "add", "-B", `canvas/${node.id}`, normalizeWirePath(worktreeDir), "HEAD"], {
|
|
198
|
+
cwd: base,
|
|
199
|
+
windowsHide: true,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
// 若创建 worktree 失败(例如已存在锁),回退普通目录
|
|
204
|
+
return targetDir;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return worktreeDir;
|
|
208
|
+
}
|
|
209
|
+
/** 启动单个节点 */
|
|
210
|
+
async runNode(graph, nodeId, initialPrompt, runBase) {
|
|
211
|
+
const node = graph.nodes.find((n) => n.id === nodeId);
|
|
212
|
+
if (!node)
|
|
213
|
+
return;
|
|
214
|
+
this.runningGraphs.set(graph.id, graph);
|
|
215
|
+
const execKey = `${graph.id}:${nodeId}`;
|
|
216
|
+
if (this.activeExecutions.has(execKey)) {
|
|
217
|
+
// 已在运行中
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const controller = new AbortController();
|
|
221
|
+
this.activeExecutions.set(execKey, {
|
|
222
|
+
graphId: graph.id,
|
|
223
|
+
nodeId,
|
|
224
|
+
abortController: controller,
|
|
225
|
+
});
|
|
226
|
+
node.status = "running";
|
|
227
|
+
node.error = undefined;
|
|
228
|
+
node.currentAction = `[${node.engine.toUpperCase()}] 正在分配独立环境与工作目录...`;
|
|
229
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
230
|
+
this.appendLog(graph.id, nodeId, "system", `启动节点「${node.title}」(${node.engine}/${node.model}),工作目录: ${node.cwd || "."}(基准: ${this.pickRunBase(runBase ?? this.runBaseByGraph.get(graph.id))})`);
|
|
231
|
+
this.broadcast({
|
|
232
|
+
type: "canvas_node_event",
|
|
233
|
+
graphId: graph.id,
|
|
234
|
+
nodeId,
|
|
235
|
+
event: "started",
|
|
236
|
+
currentAction: node.currentAction,
|
|
237
|
+
});
|
|
238
|
+
try {
|
|
239
|
+
if ((runBase ?? "").trim())
|
|
240
|
+
this.runBaseByGraph.set(graph.id, runBase);
|
|
241
|
+
const resolvedCwd = await this.resolveNodeCwd(node, runBase ?? this.runBaseByGraph.get(graph.id));
|
|
242
|
+
if (controller.signal.aborted) {
|
|
243
|
+
this.activeExecutions.delete(execKey);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
node.currentAction = `[${node.engine.toUpperCase()}] 启动 ${node.model} 执行中 (${node.role})...`;
|
|
247
|
+
this.broadcast({
|
|
248
|
+
type: "canvas_node_event",
|
|
249
|
+
graphId: graph.id,
|
|
250
|
+
nodeId,
|
|
251
|
+
event: "action",
|
|
252
|
+
currentAction: node.currentAction,
|
|
253
|
+
});
|
|
254
|
+
// 组装前置契约提示词(若上游边有 handoff 产物)
|
|
255
|
+
const incomingEdges = graph.edges.filter((e) => e.targetNodeId === nodeId);
|
|
256
|
+
let contractContext = "";
|
|
257
|
+
for (const edge of incomingEdges) {
|
|
258
|
+
if (edge.lastHandoffSummary) {
|
|
259
|
+
const PORT_ZH = {
|
|
260
|
+
contract: "任务契约",
|
|
261
|
+
instruction: "AI指令",
|
|
262
|
+
system: "角色规约",
|
|
263
|
+
config: "控制参数",
|
|
264
|
+
};
|
|
265
|
+
const srcTitle = graph.nodes.find((n) => n.id === edge.sourceNodeId)?.title || edge.sourceNodeId;
|
|
266
|
+
const portSuffix = edge.targetPort && edge.targetPort !== "contract"
|
|
267
|
+
? ` → ${PORT_ZH[edge.targetPort] || edge.targetPort}`
|
|
268
|
+
: "";
|
|
269
|
+
contractContext += `\n【来自「${srcTitle}」的定向输入${portSuffix}】:\n${edge.lastHandoffSummary}\n`;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// 指令优先级:本次手动输入 > 节点持久化 instruction(AI 可输入) > systemPromptOverride > 直接使用输入 > 默认。
|
|
273
|
+
// instruction 支持 {{input}} / {{上游摘要}} / {{upstream}} / {{contract}} 变量引用上游契约产物;
|
|
274
|
+
// 留空或只写输入变量 = 直接使用输入(不再追加通用兜底,避免输入被复制两遍)。
|
|
275
|
+
const rawInstruction = initialPrompt?.trim() || node.instruction?.trim() || node.systemPromptOverride?.trim() || "";
|
|
276
|
+
let expandedInstruction = rawInstruction;
|
|
277
|
+
if (contractContext) {
|
|
278
|
+
expandedInstruction = expandedInstruction.replace(/\{\{\s*(上游摘要|upstream|contract|input)\s*\}\}/gi, contractContext);
|
|
279
|
+
}
|
|
280
|
+
const isInputOnly = contractContext.trim().length > 0 &&
|
|
281
|
+
(!expandedInstruction.trim() || expandedInstruction.trim() === contractContext.trim());
|
|
282
|
+
const cfgEntries = Object.entries(node.config ?? {}).filter((entry) => entry[1] !== undefined && entry[1] !== "");
|
|
283
|
+
let configContext = "";
|
|
284
|
+
if (!isCanvasPassthroughRole(node.role) && node.role !== "transform" && cfgEntries.length > 0) {
|
|
285
|
+
configContext = `\n【本节点专属参数】:\n${cfgEntries.map((entry) => `${entry[0]}=${String(entry[1])}`).join(";")}\n`;
|
|
286
|
+
}
|
|
287
|
+
let prompt = isInputOnly
|
|
288
|
+
? `${contractContext}${configContext ? `\n${configContext}` : ""}`
|
|
289
|
+
: `${expandedInstruction || "按照你的角色职责进行开发"}\n${configContext}${contractContext}`;
|
|
290
|
+
if (!contractContext.trim() &&
|
|
291
|
+
!initialPrompt?.trim() &&
|
|
292
|
+
!node.instruction?.trim() &&
|
|
293
|
+
!isCanvasPassthroughRole(node.role)) {
|
|
294
|
+
prompt += `\n\n【首节点启动指引】: 当前为流水线首节点。请结合当前工作区代码库现状,排查梳理项目架构规范与待实现的核心任务,输出明确的分工契约交由下游节点接力执行。`;
|
|
295
|
+
}
|
|
296
|
+
let outputSummary = "";
|
|
297
|
+
// AI 条件裁决(config.aiJudge=true):条件节点走一次轻量问答做语义判定,
|
|
298
|
+
// 只能回“结论:通过/不通过”,下游按它分流;失败回落关键词。未开启还是零 token 透传。
|
|
299
|
+
const wantAiJudge = (node.role === "ai_judge" || (node.role === "condition" && node.config?.aiJudge === true)) &&
|
|
300
|
+
graph.executionMode !== "simulate" &&
|
|
301
|
+
!!this.engineGetter;
|
|
302
|
+
let judgeModel = node.model;
|
|
303
|
+
if (wantAiJudge) {
|
|
304
|
+
const condRef = String(node.config?.condition ?? "").trim() || "上游产物符合预期";
|
|
305
|
+
const instructionAsk = (node.instruction ?? "").trim().slice(0, 500);
|
|
306
|
+
const judgeAsk = typeof node.config?.judge === "string" && node.config.judge.trim()
|
|
307
|
+
? String(node.config.judge).trim().slice(0, 500)
|
|
308
|
+
: instructionAsk || `判断上游产物是否满足【${condRef}】`;
|
|
309
|
+
const upstreamBlock = contractContext.trim()
|
|
310
|
+
? `上游产物摘要:\n${contractContext.slice(0, 4000)}\n问题:${judgeAsk}(以上游产物为依据)`
|
|
311
|
+
: `无上游产物,直接回答问题:${judgeAsk}`;
|
|
312
|
+
prompt = `你是流水线分支裁判。只做二选一语义判断,不干别的活,不调用任何工具。\n${upstreamBlock}\n要求:最后一行必须 exactly 输出“结论:通过”或“结论:不通过”,前面可附一行理由。`;
|
|
313
|
+
judgeModel =
|
|
314
|
+
typeof node.config?.judgeModel === "string" && node.config.judgeModel.trim()
|
|
315
|
+
? node.config.judgeModel.trim().slice(0, 120)
|
|
316
|
+
: node.model;
|
|
317
|
+
this.appendLog(graph.id, nodeId, "system", `AI 裁决启动(${node.engine}/${judgeModel}):${judgeAsk.slice(0, 200)}`);
|
|
318
|
+
}
|
|
319
|
+
else if (!isCanvasPassthroughRole(node.role) && node.role !== "transform") {
|
|
320
|
+
prompt += `\n\n【流水线自动化执行安全公约】:
|
|
321
|
+
1. 自治推进:这是无人值守的自动化协同流水线,严禁停留在向用户提问或等待人工决策阶段;遇分支情况请按最佳工程实践自主裁决并推进;
|
|
322
|
+
2. 避免前台阻塞:如需启动服务或验证测试,严禁启动阻塞终端的前台长进程;遇到端口占用(如 3000/3001 等)先检测并释放,验证完成后务必确保退出并清理所有后台子进程;
|
|
323
|
+
3. 交付明确:完成时请提供清晰的修改摘要、接口契约与当前状态,供下游节点或审查门禁接力。`;
|
|
324
|
+
}
|
|
325
|
+
this.appendLog(graph.id, nodeId, "prompt", prompt.slice(0, 2000));
|
|
326
|
+
// 纯透传控制节点(条件/循环/人工/汇聚/记忆/结束):不启动 LLM 会话,直接透传摘要。人工审批节点停留在 waiting_approval 等待抽屉批准,不自动向下游流转。
|
|
327
|
+
if (node.role === "transform") {
|
|
328
|
+
const transformed = executeTransformNode(graph, node);
|
|
329
|
+
node.status = "done";
|
|
330
|
+
node.currentAction = "格式转换完成(本地执行)";
|
|
331
|
+
node.lastOutputSummary = transformed;
|
|
332
|
+
this.appendLog(graph.id, nodeId, "done", transformed.slice(0, 2000));
|
|
333
|
+
this.activeExecutions.delete(execKey);
|
|
334
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
335
|
+
this.broadcast({
|
|
336
|
+
type: "canvas_node_event",
|
|
337
|
+
graphId: graph.id,
|
|
338
|
+
nodeId,
|
|
339
|
+
event: "done",
|
|
340
|
+
currentAction: node.currentAction,
|
|
341
|
+
summary: node.lastOutputSummary,
|
|
342
|
+
});
|
|
343
|
+
await this.handleDownstreamEdges(graph, node);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (!wantAiJudge && isCanvasPassthroughRole(node.role)) {
|
|
347
|
+
const passthroughSummary = buildPassthroughSummary(node, contractContext, initialPrompt);
|
|
348
|
+
if (node.role === "human") {
|
|
349
|
+
node.status = "waiting_approval";
|
|
350
|
+
node.currentAction =
|
|
351
|
+
(node.config?.approvalHint ? String(node.config.approvalHint) + " —— " : "") + "等待人工在抽屉中批准/驳回";
|
|
352
|
+
node.lastOutputSummary = passthroughSummary;
|
|
353
|
+
this.appendLog(graph.id, nodeId, "system", "人工审批节点已就绪,等待批准");
|
|
354
|
+
this.activeExecutions.delete(execKey);
|
|
355
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
356
|
+
this.broadcast({
|
|
357
|
+
type: "canvas_node_event",
|
|
358
|
+
graphId: graph.id,
|
|
359
|
+
nodeId,
|
|
360
|
+
event: "paused",
|
|
361
|
+
currentAction: node.currentAction,
|
|
362
|
+
summary: node.lastOutputSummary,
|
|
363
|
+
});
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
node.status = "done";
|
|
367
|
+
node.currentAction = `控制节点直通 (${node.role})`;
|
|
368
|
+
node.lastOutputSummary = passthroughSummary;
|
|
369
|
+
this.appendLog(graph.id, nodeId, "done", passthroughSummary.slice(0, 2000));
|
|
370
|
+
this.activeExecutions.delete(execKey);
|
|
371
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
372
|
+
this.broadcast({
|
|
373
|
+
type: "canvas_node_event",
|
|
374
|
+
graphId: graph.id,
|
|
375
|
+
nodeId,
|
|
376
|
+
event: "done",
|
|
377
|
+
currentAction: node.currentAction,
|
|
378
|
+
summary: node.lastOutputSummary,
|
|
379
|
+
});
|
|
380
|
+
await this.handleDownstreamEdges(graph, node);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
// 双模调度:真实引擎执行 (Real Engine) vs 拓扑演练演示 (Simulation)
|
|
384
|
+
if (graph.executionMode === "simulate" || !this.engineGetter) {
|
|
385
|
+
await this.simulateNodeExecution(node, resolvedCwd, prompt, controller.signal, (action) => {
|
|
386
|
+
node.currentAction = action;
|
|
387
|
+
this.broadcast({
|
|
388
|
+
type: "canvas_node_event",
|
|
389
|
+
graphId: graph.id,
|
|
390
|
+
nodeId,
|
|
391
|
+
event: "action",
|
|
392
|
+
currentAction: action,
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
outputSummary = `[${node.title}] 演练完成,工作目录:${node.cwd || "."}`;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
const savedModel = node.model;
|
|
399
|
+
if (wantAiJudge && judgeModel !== node.model)
|
|
400
|
+
node.model = judgeModel;
|
|
401
|
+
try {
|
|
402
|
+
outputSummary = await this.executeRealEngineNode(graph, node, resolvedCwd, prompt, controller.signal, (action) => {
|
|
403
|
+
node.currentAction = action;
|
|
404
|
+
this.broadcast({
|
|
405
|
+
type: "canvas_node_event",
|
|
406
|
+
graphId: graph.id,
|
|
407
|
+
nodeId,
|
|
408
|
+
event: "action",
|
|
409
|
+
currentAction: action,
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
finally {
|
|
414
|
+
node.model = savedModel;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (controller.signal.aborted) {
|
|
418
|
+
this.activeExecutions.delete(execKey);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
// 节点完成
|
|
422
|
+
node.status = "done";
|
|
423
|
+
node.currentAction = `已完成 (${node.engine.toUpperCase()})`;
|
|
424
|
+
node.lastOutputSummary = outputSummary;
|
|
425
|
+
if (wantAiJudge) {
|
|
426
|
+
const hits = [
|
|
427
|
+
...outputSummary.matchAll(/结论\s*[::]\s*(通过|不通过|成立|不成立|是|否|真|假|true|false|yes|no)/gi),
|
|
428
|
+
];
|
|
429
|
+
const last = hits.length > 0 ? String(hits[hits.length - 1][1]).toLowerCase() : "";
|
|
430
|
+
node.aiVerdict = last ? ["通过", "成立", "是", "真", "true", "yes"].includes(last) : undefined;
|
|
431
|
+
this.appendLog(graph.id, nodeId, "system", node.aiVerdict === undefined
|
|
432
|
+
? "AI 裁决未给出有效结论(默认走不成立边)。"
|
|
433
|
+
: `AI 裁决结论:${node.aiVerdict ? "通过" : "不通过"}(独立判定,未混入关键词)。`);
|
|
434
|
+
}
|
|
435
|
+
this.appendLog(graph.id, nodeId, "done", outputSummary.slice(0, 2000));
|
|
436
|
+
this.activeExecutions.delete(execKey);
|
|
437
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
438
|
+
this.broadcast({
|
|
439
|
+
type: "canvas_node_event",
|
|
440
|
+
graphId: graph.id,
|
|
441
|
+
nodeId,
|
|
442
|
+
event: "done",
|
|
443
|
+
currentAction: node.currentAction,
|
|
444
|
+
summary: node.lastOutputSummary,
|
|
445
|
+
});
|
|
446
|
+
// 触发下游交接或审查
|
|
447
|
+
await this.handleDownstreamEdges(graph, node);
|
|
448
|
+
}
|
|
449
|
+
catch (err) {
|
|
450
|
+
if (controller.signal.aborted) {
|
|
451
|
+
this.activeExecutions.delete(execKey);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const msg = err.message || String(err);
|
|
455
|
+
node.status = "error";
|
|
456
|
+
node.error = msg;
|
|
457
|
+
node.currentAction = `执行出错: ${msg}`;
|
|
458
|
+
this.appendLog(graph.id, node.id, "error", msg.slice(0, 2000));
|
|
459
|
+
this.activeExecutions.delete(execKey);
|
|
460
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
461
|
+
this.broadcast({
|
|
462
|
+
type: "canvas_node_event",
|
|
463
|
+
graphId: graph.id,
|
|
464
|
+
nodeId,
|
|
465
|
+
event: "error",
|
|
466
|
+
currentAction: node.currentAction,
|
|
467
|
+
error: msg,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
/** 运行全图(从所有入度为 0 的根节点,或者未完成节点并发拉起) */
|
|
472
|
+
async runAll(graph, initialPrompt, runBase) {
|
|
473
|
+
this.runningGraphs.set(graph.id, graph);
|
|
474
|
+
graph.status = "running";
|
|
475
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
476
|
+
// 找到没有入边或当前状态为 idle/error 的起点节点
|
|
477
|
+
const targetNodes = new Set(graph.nodes.map((n) => n.id));
|
|
478
|
+
for (const edge of graph.edges) {
|
|
479
|
+
targetNodes.delete(edge.targetNodeId);
|
|
480
|
+
}
|
|
481
|
+
let startNodes = graph.nodes.filter((n) => targetNodes.has(n.id));
|
|
482
|
+
if (startNodes.length === 0) {
|
|
483
|
+
startNodes = graph.nodes.filter((n) => n.status === "idle" || n.status === "error");
|
|
484
|
+
}
|
|
485
|
+
if (startNodes.length === 0 && graph.nodes.length > 0) {
|
|
486
|
+
startNodes = [graph.nodes[0]];
|
|
487
|
+
}
|
|
488
|
+
if ((runBase ?? "").trim())
|
|
489
|
+
this.runBaseByGraph.set(graph.id, runBase);
|
|
490
|
+
await Promise.all(startNodes.map((n) => this.runNode(graph, n.id, initialPrompt, runBase)));
|
|
491
|
+
}
|
|
492
|
+
/** 处理节点完成后的下游连线流转 */
|
|
493
|
+
async handleDownstreamEdges(graph, completedNode, via) {
|
|
494
|
+
let outEdges = graph.edges.filter((e) => e.sourceNodeId === completedNode.id);
|
|
495
|
+
if (via === "approve") {
|
|
496
|
+
// 人工批准只走批准/主线边,不触发驳回回路边(reject / loop_back / review)
|
|
497
|
+
outEdges = outEdges.filter((e) => e.type !== "reject" && e.type !== "loop_back" && e.type !== "review");
|
|
498
|
+
}
|
|
499
|
+
if (completedNode.role === "condition" || completedNode.role === "ai_judge") {
|
|
500
|
+
const aiMode = completedNode.role === "ai_judge" || completedNode.config?.aiJudge === true;
|
|
501
|
+
const condText = String(completedNode.config?.condition ?? "");
|
|
502
|
+
const upstreamText = graph.edges
|
|
503
|
+
.filter((e) => e.targetNodeId === completedNode.id)
|
|
504
|
+
.map((e) => e.lastHandoffSummary ?? "")
|
|
505
|
+
.join("\n") ||
|
|
506
|
+
completedNode.lastOutputSummary ||
|
|
507
|
+
"";
|
|
508
|
+
const summary = upstreamText;
|
|
509
|
+
const tokens = condText
|
|
510
|
+
.split(/[\s,\uFF0C\u3001/()\u2014\u2014]+/)
|
|
511
|
+
.map((t) => t.trim())
|
|
512
|
+
.filter((t) => t.length >= 2);
|
|
513
|
+
const hit = aiMode
|
|
514
|
+
? completedNode.aiVerdict === true
|
|
515
|
+
: tokens.length === 0
|
|
516
|
+
? true
|
|
517
|
+
: tokens.some((t) => summary.includes(t));
|
|
518
|
+
if (aiMode) {
|
|
519
|
+
this.appendLog(graph.id, completedNode.id, "system", `分支:${hit ? "成立边" : "不成立边"}(AI 独立裁决${completedNode.aiVerdict === undefined ? ",无结论默认不成立" : ""})。`);
|
|
520
|
+
}
|
|
521
|
+
outEdges = outEdges.filter((e) => {
|
|
522
|
+
if (e.type === "condition_true")
|
|
523
|
+
return hit;
|
|
524
|
+
if (e.type === "condition_false")
|
|
525
|
+
return !hit;
|
|
526
|
+
return true;
|
|
527
|
+
});
|
|
528
|
+
this.appendLog(graph.id, completedNode.id, "system", "条件分支判定:" + (hit ? "成立,走 true 边" : "不成立,走 false 边"));
|
|
529
|
+
}
|
|
530
|
+
for (const edge of outEdges) {
|
|
531
|
+
edge.lastHandoffSummary = completedNode.lastOutputSummary;
|
|
532
|
+
if (edge.type === "review" || edge.type === "loop_back" || edge.type === "reject") {
|
|
533
|
+
// 审查回路:检查是否超出最大驳回重试次数
|
|
534
|
+
const maxRetries = edge.maxLoopRetries ?? 3;
|
|
535
|
+
const current = edge.currentLoopCount ?? 0;
|
|
536
|
+
if (current >= maxRetries) {
|
|
537
|
+
// 达到熔断上限,暂停并等待人工介入
|
|
538
|
+
completedNode.status = "waiting_approval";
|
|
539
|
+
completedNode.currentAction = `已达审查重试上限 (${maxRetries} 次),暂停等待人工仲裁确认`;
|
|
540
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
541
|
+
this.broadcast({
|
|
542
|
+
type: "canvas_node_event",
|
|
543
|
+
graphId: graph.id,
|
|
544
|
+
nodeId: completedNode.id,
|
|
545
|
+
event: "paused",
|
|
546
|
+
currentAction: completedNode.currentAction,
|
|
547
|
+
});
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
edge.currentLoopCount = current + 1;
|
|
551
|
+
}
|
|
552
|
+
// 触发下游目标节点
|
|
553
|
+
const targetNode = graph.nodes.find((n) => n.id === edge.targetNodeId);
|
|
554
|
+
if (targetNode) {
|
|
555
|
+
this.appendLog(graph.id, targetNode.id, "handoff", `收到来自「${completedNode.title}」的交接${edge.label ? `(${edge.label})` : ""}:${(edge.lastHandoffSummary || "").slice(0, 1500)}`);
|
|
556
|
+
}
|
|
557
|
+
if (targetNode) {
|
|
558
|
+
const execKey = graph.id + ":" + targetNode.id;
|
|
559
|
+
if (targetNode.status === "running" || this.activeExecutions.has(execKey))
|
|
560
|
+
continue;
|
|
561
|
+
const upstreamIds = graph.edges.filter((e) => e.targetNodeId === targetNode.id).map((e) => e.sourceNodeId);
|
|
562
|
+
const allUpstreamDone = upstreamIds.every((uid) => graph.nodes.find((n) => n.id === uid)?.status === "done");
|
|
563
|
+
if (!via && !allUpstreamDone)
|
|
564
|
+
continue; // 人工批准直放(via=approve)不受合流门限制
|
|
565
|
+
targetNode.status = "running";
|
|
566
|
+
void this.runNode(graph, targetNode.id, undefined, this.runBaseByGraph.get(graph.id));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
// 检查图上所有节点是否都已经到达完成状态
|
|
570
|
+
const allDone = graph.nodes.every((n) => n.status === "done");
|
|
571
|
+
if (allDone) {
|
|
572
|
+
graph.status = "completed";
|
|
573
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/** 停止单个节点 */
|
|
577
|
+
stopNode(graphId, nodeId) {
|
|
578
|
+
const execKey = `${graphId}:${nodeId}`;
|
|
579
|
+
const execution = this.activeExecutions.get(execKey);
|
|
580
|
+
if (execution) {
|
|
581
|
+
execution.abortController?.abort();
|
|
582
|
+
this.activeExecutions.delete(execKey);
|
|
583
|
+
}
|
|
584
|
+
const graph = this.runningGraphs.get(graphId);
|
|
585
|
+
if (graph) {
|
|
586
|
+
const node = graph.nodes.find((n) => n.id === nodeId);
|
|
587
|
+
if (node) {
|
|
588
|
+
node.status = "idle";
|
|
589
|
+
node.currentAction = "已手动中止";
|
|
590
|
+
this.appendLog(graphId, nodeId, "system", "已手动中止当前任务");
|
|
591
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
592
|
+
this.broadcast({
|
|
593
|
+
type: "canvas_node_event",
|
|
594
|
+
graphId,
|
|
595
|
+
nodeId,
|
|
596
|
+
event: "paused",
|
|
597
|
+
currentAction: node.currentAction,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** 停止全图所有正在运行的节点 */
|
|
603
|
+
stopAll(graphId) {
|
|
604
|
+
this.runBaseByGraph.delete(graphId);
|
|
605
|
+
for (const [key, execution] of this.activeExecutions.entries()) {
|
|
606
|
+
if (execution.graphId === graphId) {
|
|
607
|
+
execution.abortController?.abort();
|
|
608
|
+
this.activeExecutions.delete(key);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const graph = this.runningGraphs.get(graphId);
|
|
612
|
+
if (graph) {
|
|
613
|
+
graph.status = "idle";
|
|
614
|
+
for (const node of graph.nodes) {
|
|
615
|
+
if (node.status === "running") {
|
|
616
|
+
node.status = "idle";
|
|
617
|
+
node.currentAction = "已手动中止";
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/** 审查门禁裁决(#707):批准/驳回落盘持久化并广播;执行中的节点同步收到审查意见 */
|
|
624
|
+
reviewNode(graphId, nodeId, decision, comment) {
|
|
625
|
+
if (!isSafeGraphId(graphId))
|
|
626
|
+
return null;
|
|
627
|
+
let graph = this.runningGraphs.get(graphId);
|
|
628
|
+
if (!graph) {
|
|
629
|
+
try {
|
|
630
|
+
graph = loadCanvasGraph(this.dataDir, graphId);
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
return null;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const node = graph.nodes.find((n) => n.id === nodeId);
|
|
637
|
+
if (!node)
|
|
638
|
+
return null;
|
|
639
|
+
const cleanComment = (comment ?? "").slice(0, 500);
|
|
640
|
+
if (decision === "approve") {
|
|
641
|
+
node.status = "done";
|
|
642
|
+
node.currentAction = cleanComment ? `人工审查批准合流:${cleanComment}` : "人工审查批准合流";
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
node.status = "waiting_approval";
|
|
646
|
+
node.currentAction = cleanComment ? `人工驳回:${cleanComment}` : "人工驳回:需补充完善测试与注释";
|
|
647
|
+
}
|
|
648
|
+
graph.updatedAt = Date.now();
|
|
649
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
650
|
+
this.runningGraphs.set(graphId, graph);
|
|
651
|
+
this.appendLog(graphId, nodeId, "review", `${decision === "approve" ? "人工审查批准合流" : "人工驳回"}${cleanComment ? `:${cleanComment}` : ""}`);
|
|
652
|
+
// 执行中的节点同步收到审查意见(复用节点事件通道,currentAction 即裁决正文)
|
|
653
|
+
if (this.activeExecutions.has(`${graphId}:${nodeId}`)) {
|
|
654
|
+
this.broadcast({
|
|
655
|
+
type: "canvas_node_event",
|
|
656
|
+
graphId,
|
|
657
|
+
nodeId,
|
|
658
|
+
event: decision === "approve" ? "done" : "paused",
|
|
659
|
+
currentAction: node.currentAction,
|
|
660
|
+
summary: node.lastOutputSummary,
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
if (decision === "approve") {
|
|
664
|
+
void this.handleDownstreamEdges(graph, node, "approve");
|
|
665
|
+
}
|
|
666
|
+
return graph;
|
|
667
|
+
}
|
|
668
|
+
/** 向节点插话干预 (Steer) */
|
|
669
|
+
async steerNode(graphId, nodeId, message) {
|
|
670
|
+
let graph = this.runningGraphs.get(graphId);
|
|
671
|
+
if (!graph) {
|
|
672
|
+
try {
|
|
673
|
+
graph = loadCanvasGraph(this.dataDir, graphId);
|
|
674
|
+
this.runningGraphs.set(graphId, graph);
|
|
675
|
+
}
|
|
676
|
+
catch {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
const node = graph.nodes.find((n) => n.id === nodeId);
|
|
681
|
+
if (!node)
|
|
682
|
+
return;
|
|
683
|
+
node.currentAction = `[收到人工插话指导]: ${message.slice(0, 40)}`;
|
|
684
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
685
|
+
this.appendLog(graphId, nodeId, "steer", message.slice(0, 2000));
|
|
686
|
+
this.broadcast({
|
|
687
|
+
type: "canvas_node_event",
|
|
688
|
+
graphId,
|
|
689
|
+
nodeId,
|
|
690
|
+
event: "action",
|
|
691
|
+
currentAction: node.currentAction,
|
|
692
|
+
});
|
|
693
|
+
// 真正投递给底层会话(若节点正在执行)
|
|
694
|
+
const execKey = `${graphId}:${nodeId}`;
|
|
695
|
+
const execution = this.activeExecutions.get(execKey);
|
|
696
|
+
if (execution?.session && typeof execution.session.prompt === "function") {
|
|
697
|
+
try {
|
|
698
|
+
await execution.session.prompt(message);
|
|
699
|
+
this.appendLog(graphId, nodeId, "system", "已将插话实时投递进正在运行的智能体会话。");
|
|
700
|
+
}
|
|
701
|
+
catch (err) {
|
|
702
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
703
|
+
this.appendLog(graphId, nodeId, "error", `插话投递底层会话失败: ${errMsg}`);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
else if (node.status !== "running") {
|
|
707
|
+
const cleanMsg = message.trim();
|
|
708
|
+
if (cleanMsg) {
|
|
709
|
+
node.instruction = node.instruction ? `${node.instruction}\n\n[人工指导补充]: ${cleanMsg}` : cleanMsg;
|
|
710
|
+
saveCanvasGraph(this.dataDir, graph);
|
|
711
|
+
this.appendLog(graphId, nodeId, "system", "节点未在运行,指导意见已暂存为该节点下一次执行指令。");
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
/** 重置全图节点状态为 idle */
|
|
716
|
+
resetStatus(graph) {
|
|
717
|
+
this.stopAll(graph.id);
|
|
718
|
+
graph.status = "idle";
|
|
719
|
+
for (const node of graph.nodes) {
|
|
720
|
+
node.status = "idle";
|
|
721
|
+
node.aiVerdict = undefined;
|
|
722
|
+
node.error = undefined;
|
|
723
|
+
node.currentAction = undefined;
|
|
724
|
+
node.lastOutputSummary = undefined;
|
|
725
|
+
}
|
|
726
|
+
for (const edge of graph.edges) {
|
|
727
|
+
edge.currentLoopCount = 0;
|
|
728
|
+
edge.lastHandoffSummary = undefined;
|
|
729
|
+
}
|
|
730
|
+
saveCanvasRuntimeState(this.dataDir, graph);
|
|
731
|
+
}
|
|
732
|
+
/** 节点执行过程模拟与调度心跳(具备取消信号响应) */
|
|
733
|
+
async simulateNodeExecution(node, _cwd, _prompt, signal, onAction) {
|
|
734
|
+
const steps = [
|
|
735
|
+
`[${node.engine.toUpperCase()}] 分析任务上下文与关联代码库...`,
|
|
736
|
+
`[${node.engine.toUpperCase()}] 规划模块变更方案与接口契约...`,
|
|
737
|
+
`[${node.engine.toUpperCase()}] 写入并应用变更...`,
|
|
738
|
+
`[${node.engine.toUpperCase()}] 验证输出结果与测试检查...`,
|
|
739
|
+
];
|
|
740
|
+
for (const step of steps) {
|
|
741
|
+
if (signal.aborted)
|
|
742
|
+
return;
|
|
743
|
+
onAction(step);
|
|
744
|
+
const stepDelay = process.env.NODE_ENV === "test" || process.env.VITEST ? 20 : 600;
|
|
745
|
+
await new Promise((resolve, reject) => {
|
|
746
|
+
const timer = setTimeout(resolve, stepDelay);
|
|
747
|
+
signal.addEventListener("abort", () => {
|
|
748
|
+
clearTimeout(timer);
|
|
749
|
+
reject(new Error("Aborted"));
|
|
750
|
+
}, { once: true });
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/** 真实智能体引擎执行通道(接入真实的会话、模型调用与代码修改) */
|
|
755
|
+
async executeRealEngineNode(graph, node, resolvedCwd, prompt, signal, onAction) {
|
|
756
|
+
if (!this.engineGetter) {
|
|
757
|
+
throw new Error("引擎服务提供者未注入");
|
|
758
|
+
}
|
|
759
|
+
let engineSvc = this.engineGetter(node.engine);
|
|
760
|
+
if (!engineSvc && node.engine !== "pi") {
|
|
761
|
+
engineSvc = this.engineGetter("pi");
|
|
762
|
+
this.appendLog(graph.id, node.id, "system", `引擎 ${node.engine} 不可用,已自动切换为 Pi 原生引擎。`);
|
|
763
|
+
}
|
|
764
|
+
if (!engineSvc) {
|
|
765
|
+
throw new Error(`找不到智能体引擎:${node.engine}`);
|
|
766
|
+
}
|
|
767
|
+
const clientId = `canvas:${graph.id}:${node.id}`;
|
|
768
|
+
let collectedText = "";
|
|
769
|
+
onAction(`[${node.engine.toUpperCase()}] 正在接入真实引擎运行时...`);
|
|
770
|
+
// sink 具名持有:finally 必须用同一引用 detach,否则旧闭包永久残留串台(#698)
|
|
771
|
+
// 复用当前对话流:snapshot/appended 的标准 UiMessage 直接缓存 + 转播,抽屉复用 Message 组件渲染;
|
|
772
|
+
// 工具调用与文本增量同时记一条轻量协同事件,保证调用/错误清晰可见。
|
|
773
|
+
const seenToolIds = new Set();
|
|
774
|
+
// 本轮运行期间从直播帧里看到的 errorMessage(与快照索引无关,压缩/翻页也不会丢)。
|
|
775
|
+
// 只收「本轮新增」的消息:attach / 首帧可能是带全量 history 的 snapshot,
|
|
776
|
+
// 否则该节点上一次留下的旧报错会被当成本轮失败(换模型后仍报旧错)。
|
|
777
|
+
let sinkError;
|
|
778
|
+
const baselineMsgIds = new Set();
|
|
779
|
+
let captureErrors = false;
|
|
780
|
+
const sink = (msg) => {
|
|
781
|
+
if (signal.aborted)
|
|
782
|
+
return;
|
|
783
|
+
if (msg.type === "message_delta") {
|
|
784
|
+
const delta = msg.delta || "";
|
|
785
|
+
collectedText += delta;
|
|
786
|
+
if (collectedText.length % 50 === 0) {
|
|
787
|
+
onAction(`[${node.engine.toUpperCase()}] 正在生成: ${collectedText.slice(-30).replace(/\n/g, " ")}...`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
else if (msg.type === "snapshot" || msg.type === "snapshot_delta") {
|
|
791
|
+
const messages = (msg.state?.messages || msg.appended || []);
|
|
792
|
+
if (messages.length > 0)
|
|
793
|
+
this.pushNodeMessages(graph.id, node.id, messages);
|
|
794
|
+
for (const m of messages) {
|
|
795
|
+
if (m.toolName) {
|
|
796
|
+
const tid = m.toolCallId || `${m.toolName}:${m.id}`;
|
|
797
|
+
if (!seenToolIds.has(tid)) {
|
|
798
|
+
seenToolIds.add(tid);
|
|
799
|
+
this.appendLog(graph.id, node.id, "tool", m.isError
|
|
800
|
+
? `工具 ${m.toolName} 报错${m.errorMessage ? `:${m.errorMessage.slice(0, 500)}` : ""}`
|
|
801
|
+
: `正在调用工具: ${m.toolName}`, m.toolName);
|
|
802
|
+
}
|
|
803
|
+
onAction(`[${node.engine.toUpperCase()}] 正在调用工具: ${m.toolName}`);
|
|
804
|
+
}
|
|
805
|
+
else if (m.errorMessage) {
|
|
806
|
+
if (captureErrors && !baselineMsgIds.has(m.id))
|
|
807
|
+
sinkError = m.errorMessage;
|
|
808
|
+
if (!baselineMsgIds.has(m.id)) {
|
|
809
|
+
this.appendLog(graph.id, node.id, "error", m.errorMessage.slice(0, 2000));
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
const cs = await engineSvc.attach(clientId, sink);
|
|
816
|
+
const execKey = `${graph.id}:${node.id}`;
|
|
817
|
+
const execution = this.activeExecutions.get(execKey);
|
|
818
|
+
if (execution) {
|
|
819
|
+
execution.session = cs;
|
|
820
|
+
}
|
|
821
|
+
try {
|
|
822
|
+
if (cs.cwd !== resolvedCwd) {
|
|
823
|
+
await cs.setCwd(resolvedCwd);
|
|
824
|
+
}
|
|
825
|
+
// 真实应用模型切换(若模型不存在或 Key 无效,真实报错)
|
|
826
|
+
// 归一化双前缀:历史存量可能是 opencode-go/opencode-go/xxx,压成 opencode-go/xxx 再切换。
|
|
827
|
+
if (node.model && typeof cs.switchModelOrThrow === "function") {
|
|
828
|
+
try {
|
|
829
|
+
await cs.switchModelOrThrow(normalizeCanvasModelId(node.model));
|
|
830
|
+
}
|
|
831
|
+
catch (err) {
|
|
832
|
+
// 尝试安全回退到当前引擎默认可用模型,避免特定模型未配置导致整张画布崩溃
|
|
833
|
+
let fallbackOk = false;
|
|
834
|
+
if (typeof cs.switchModel === "function") {
|
|
835
|
+
try {
|
|
836
|
+
await cs.switchModel("");
|
|
837
|
+
fallbackOk = true;
|
|
838
|
+
this.appendLog(graph.id, node.id, "system", `指定模型 [${node.model}] 不可用,已自动回落至引擎默认模型。`);
|
|
839
|
+
}
|
|
840
|
+
catch { }
|
|
841
|
+
}
|
|
842
|
+
if (!fallbackOk) {
|
|
843
|
+
throw new Error(`模型未配置或不可用 [${node.model}]: ${err.message}`);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
// 真实应用思考强度
|
|
848
|
+
if (node.thinkingLevel && typeof cs.setThinking === "function") {
|
|
849
|
+
try {
|
|
850
|
+
cs.setThinking(node.thinkingLevel);
|
|
851
|
+
}
|
|
852
|
+
catch { }
|
|
853
|
+
}
|
|
854
|
+
onAction(`[${node.engine.toUpperCase()}] 开始执行任务...`);
|
|
855
|
+
// 本轮起点快照:报错检测只看本次运行新增的消息,
|
|
856
|
+
// 避免把该节点历史运行留下的旧报错当成本轮失败。
|
|
857
|
+
const preRunMsgs = canvasSnapshotMessages(cs);
|
|
858
|
+
for (const m of preRunMsgs)
|
|
859
|
+
baselineMsgIds.add(m.id);
|
|
860
|
+
const baselineMsgCount = preRunMsgs.length;
|
|
861
|
+
captureErrors = true;
|
|
862
|
+
await cs.prompt(prompt);
|
|
863
|
+
// 等待执行完成(带超时和取消监听)
|
|
864
|
+
const startTime = Date.now();
|
|
865
|
+
const maxWaitMs = 15 * 60 * 1000;
|
|
866
|
+
while (!signal.aborted) {
|
|
867
|
+
await new Promise((r) => setTimeout(r, 1200));
|
|
868
|
+
if (signal.aborted) {
|
|
869
|
+
await cs.abort().catch(() => { });
|
|
870
|
+
throw new Error("任务已被手动中止");
|
|
871
|
+
}
|
|
872
|
+
let isStreaming = false;
|
|
873
|
+
let turnError;
|
|
874
|
+
if (typeof cs.readConversationForPlugins === "function") {
|
|
875
|
+
const snap = cs.readConversationForPlugins();
|
|
876
|
+
isStreaming = snap?.isStreaming === true;
|
|
877
|
+
// 注意:PluginConversationSnapshot 并无 `lastError` 字段(旧写法恒为 undefined),
|
|
878
|
+
// 于是 API 报错(如 403 RegionError)被当成执行成功、节点照样标 done。
|
|
879
|
+
// 与定时任务桥(agent-service-daemon)同口径:读尾部 assistant 消息的 errorMessage;
|
|
880
|
+
// 自动重试中的失败消息会被服务端隐藏,中止回合不算报错。
|
|
881
|
+
turnError =
|
|
882
|
+
tailAssistantError(canvasSnapshotMessages(cs).slice(baselineMsgCount)) ??
|
|
883
|
+
(sinkError && !CANVAS_ABORT_MESSAGES.test(sinkError.trim()) ? sinkError : undefined);
|
|
884
|
+
}
|
|
885
|
+
if (turnError) {
|
|
886
|
+
throw new Error(`引擎执行失败:${turnError}`);
|
|
887
|
+
}
|
|
888
|
+
if (!isStreaming && Date.now() - startTime > 3000) {
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
if (Date.now() - startTime > maxWaitMs) {
|
|
892
|
+
await cs.abort().catch(() => { });
|
|
893
|
+
throw new Error("任务执行超时 (15分钟)");
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
// 真实检测代码变更统计(仅本地项目支持本地 git diff)
|
|
897
|
+
let diffSummary = "";
|
|
898
|
+
try {
|
|
899
|
+
if (!isRemoteWorkspaceUri(resolvedCwd)) {
|
|
900
|
+
const { stdout: diffStat } = await exec("git", ["diff", "--stat"], {
|
|
901
|
+
cwd: resolvedCwd,
|
|
902
|
+
timeout: 5000,
|
|
903
|
+
windowsHide: true,
|
|
904
|
+
});
|
|
905
|
+
if (diffStat.trim()) {
|
|
906
|
+
diffSummary = `\n【代码变更统计】:\n${diffStat.trim()}`;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
catch { }
|
|
911
|
+
// 交付正文:优先用流式增量累积,其次取本轮新增的 assistant 正文。
|
|
912
|
+
// (部分引擎/重连路径只走 snapshot 增量、没有 message_delta,collectedText 会是空的,
|
|
913
|
+
// 旧逻辑于是把下游交接内容写成「交付完成(未产生文件修改)」的占位文案)
|
|
914
|
+
const replyText = (collectedText.trim() || extractRunReplyText(canvasSnapshotMessages(cs).slice(baselineMsgCount))).trim();
|
|
915
|
+
if (replyText)
|
|
916
|
+
return `${replyText.slice(-300)}${diffSummary}`;
|
|
917
|
+
return `[${node.title}] 交付完成${diffSummary || "(未产生文件修改)"}`;
|
|
918
|
+
}
|
|
919
|
+
finally {
|
|
920
|
+
if (execution) {
|
|
921
|
+
execution.session = undefined;
|
|
922
|
+
}
|
|
923
|
+
engineSvc.detach(clientId, sink);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
/** 读一次会话快照里的消息列表(快照失败/引擎不支持时返回空数组,不抛出)。 */
|
|
928
|
+
function canvasSnapshotMessages(cs) {
|
|
929
|
+
const reader = cs
|
|
930
|
+
?.readConversationForPlugins;
|
|
931
|
+
if (typeof reader !== "function")
|
|
932
|
+
return [];
|
|
933
|
+
try {
|
|
934
|
+
const snap = reader.call(cs);
|
|
935
|
+
return (snap?.messages ?? []);
|
|
936
|
+
}
|
|
937
|
+
catch {
|
|
938
|
+
return [];
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
/** SDK 主动中止的精确文案(与 web/src/aborted-turn.ts 同口径,中止不是真实报错) */
|
|
942
|
+
const CANVAS_ABORT_MESSAGES = /^(This operation was aborted|Request aborted|任务已被手动中止)$/;
|
|
943
|
+
/** 末尾 assistant 回合若是真实报错,返回其 errorMessage;正常收尾或中止返回 undefined。 */
|
|
944
|
+
function tailAssistantError(msgs) {
|
|
945
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
946
|
+
const m = msgs[i];
|
|
947
|
+
if (m.role !== "assistant")
|
|
948
|
+
continue;
|
|
949
|
+
if (m.stopReason === "aborted")
|
|
950
|
+
return undefined;
|
|
951
|
+
const err = (m.errorMessage ?? "").trim();
|
|
952
|
+
if (!err)
|
|
953
|
+
return undefined;
|
|
954
|
+
return CANVAS_ABORT_MESSAGES.test(err) ? undefined : err;
|
|
955
|
+
}
|
|
956
|
+
return undefined;
|
|
957
|
+
}
|
|
958
|
+
/** 从本轮新增消息里取最后一条 assistant 正文(text 块拼接),作为交付摘要兜底。 */
|
|
959
|
+
function extractRunReplyText(msgs) {
|
|
960
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
961
|
+
const m = msgs[i];
|
|
962
|
+
if (m.role !== "assistant" || m.stopReason === "aborted")
|
|
963
|
+
continue;
|
|
964
|
+
const text = (m.content ?? [])
|
|
965
|
+
.filter((b) => b.type === "text")
|
|
966
|
+
.map((b) => b.text)
|
|
967
|
+
.join("\n")
|
|
968
|
+
.trim();
|
|
969
|
+
if (text)
|
|
970
|
+
return text;
|
|
971
|
+
}
|
|
972
|
+
return "";
|
|
973
|
+
}
|
|
974
|
+
/** 纯透传控制节点集合(与前端 CanvasTypes.PASSTHROUGH_ROLES 保持一致) */
|
|
975
|
+
const CANVAS_PASSTHROUGH_ROLES = new Set([
|
|
976
|
+
"condition",
|
|
977
|
+
"loop",
|
|
978
|
+
"human",
|
|
979
|
+
"merge",
|
|
980
|
+
"memory",
|
|
981
|
+
"end",
|
|
982
|
+
"trigger",
|
|
983
|
+
"cron",
|
|
984
|
+
"webhook",
|
|
985
|
+
]);
|
|
986
|
+
function isCanvasPassthroughRole(role) {
|
|
987
|
+
return CANVAS_PASSTHROUGH_ROLES.has(role);
|
|
988
|
+
}
|
|
989
|
+
/** 格式转换节点:本地确定性执行,无需 LLM。text2json / json2text / template。 */
|
|
990
|
+
function executeTransformNode(graph, node) {
|
|
991
|
+
const cfg = node.config ?? {};
|
|
992
|
+
const mode = String(cfg["mode"] ?? "text2json");
|
|
993
|
+
const rawInput = graph.edges
|
|
994
|
+
.filter((e) => e.targetNodeId === node.id)
|
|
995
|
+
.map((e) => e.lastHandoffSummary ?? "")
|
|
996
|
+
.filter(Boolean)
|
|
997
|
+
.join("\n")
|
|
998
|
+
.trim();
|
|
999
|
+
const fallbackInput = rawInput || String(cfg["input"] ?? "");
|
|
1000
|
+
try {
|
|
1001
|
+
if (mode === "json2text") {
|
|
1002
|
+
if (!fallbackInput)
|
|
1003
|
+
return "【格式转换 " + node.title + "】输入为空,无输出";
|
|
1004
|
+
try {
|
|
1005
|
+
const v = JSON.parse(fallbackInput);
|
|
1006
|
+
if (Array.isArray(v))
|
|
1007
|
+
return v.map((x) => (typeof x === "string" ? x : JSON.stringify(x))).join("\n");
|
|
1008
|
+
if (v !== null && typeof v === "object") {
|
|
1009
|
+
const vals = Object.values(v);
|
|
1010
|
+
if (vals.every((x) => typeof x === "string" || typeof x === "number"))
|
|
1011
|
+
return vals.join("\n");
|
|
1012
|
+
return JSON.stringify(v, null, 2);
|
|
1013
|
+
}
|
|
1014
|
+
return String(v);
|
|
1015
|
+
}
|
|
1016
|
+
catch {
|
|
1017
|
+
return fallbackInput;
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
if (mode === "template") {
|
|
1021
|
+
const tpl = String(cfg["template"] ?? "{{input}}");
|
|
1022
|
+
return tpl.split("{{input}}").join(fallbackInput).split("{{上游摘要}}").join(fallbackInput);
|
|
1023
|
+
}
|
|
1024
|
+
if (!fallbackInput)
|
|
1025
|
+
return "【格式转换 " + node.title + "】输入为空,无输出";
|
|
1026
|
+
try {
|
|
1027
|
+
return JSON.stringify(JSON.parse(fallbackInput), null, 2);
|
|
1028
|
+
}
|
|
1029
|
+
catch {
|
|
1030
|
+
return JSON.stringify({ text: fallbackInput }, null, 2);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
catch (err) {
|
|
1034
|
+
return "【格式转换 " + node.title + "】转换失败:" + err.message;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
/** 归一化画布模型 ID:历史存量可能是 provider/provider/xxx,压成 provider/xxx */
|
|
1038
|
+
function normalizeCanvasModelId(raw) {
|
|
1039
|
+
const t = (raw ?? "").trim();
|
|
1040
|
+
const slash = t.indexOf("/");
|
|
1041
|
+
if (slash <= 0)
|
|
1042
|
+
return t;
|
|
1043
|
+
const provider = t.slice(0, slash);
|
|
1044
|
+
let rest = t.slice(slash + 1);
|
|
1045
|
+
const prefix = `${provider}/`;
|
|
1046
|
+
while (rest.startsWith(prefix))
|
|
1047
|
+
rest = rest.slice(prefix.length);
|
|
1048
|
+
return `${provider}/${rest}`;
|
|
1049
|
+
}
|
|
1050
|
+
function buildPassthroughSummary(node, contractContext, initialPrompt) {
|
|
1051
|
+
const cfg = node.config ?? {};
|
|
1052
|
+
const cfgText = Object.entries(cfg)
|
|
1053
|
+
.map(([k, v]) => `${k}=${String(v)}`)
|
|
1054
|
+
.slice(0, 8)
|
|
1055
|
+
.join(";");
|
|
1056
|
+
const contract = contractContext ? contractContext.slice(0, 1500) : "(无上游输入,直接透传)";
|
|
1057
|
+
switch (node.role) {
|
|
1058
|
+
case "condition":
|
|
1059
|
+
return `【条件分支 ${node.title}】判定配置:${cfgText || "未配置表达式,默认走 true 边"}\n透传上游产物:\n${contract}`;
|
|
1060
|
+
case "loop":
|
|
1061
|
+
return `【循环控制 ${node.title}】循环配置:${cfgText || "默认最多 3 次(熔断保护)"}\n透传上游产物:\n${contract}`;
|
|
1062
|
+
case "human":
|
|
1063
|
+
return `【人工审批 ${node.title}】${cfgText ? `审批提示:${cfgText}` : ""}\n待人工在抽屉中批准/驳回。上游产物:\n${contract}`;
|
|
1064
|
+
case "trigger": {
|
|
1065
|
+
const popup = (initialPrompt ?? "").trim();
|
|
1066
|
+
const payload = popup || String(cfg["payload"] ?? "").trim() || String(node.instruction ?? "").trim();
|
|
1067
|
+
const extra = contractContext ? contractContext.slice(0, 1000) : "";
|
|
1068
|
+
if (payload)
|
|
1069
|
+
return ("【手动触发 · 业务需求输入 " +
|
|
1070
|
+
node.title +
|
|
1071
|
+
"】任务内容:\n" +
|
|
1072
|
+
payload.slice(0, 1500) +
|
|
1073
|
+
(extra ? "\n上游输入:\n" + extra : ""));
|
|
1074
|
+
return ("【手动触发 · 业务需求输入 " + node.title + "】" + (extra ? "上游输入:\n" + extra : "(无载荷,直接启动)"));
|
|
1075
|
+
}
|
|
1076
|
+
case "cron":
|
|
1077
|
+
return ("【定时触发 " +
|
|
1078
|
+
node.title +
|
|
1079
|
+
"】计划:" +
|
|
1080
|
+
(cfgText || "未配置 Cron(默认手动拉起)") +
|
|
1081
|
+
"\n透传上游产物:\n" +
|
|
1082
|
+
contract);
|
|
1083
|
+
case "webhook":
|
|
1084
|
+
return ("【Webhook 触发 " + node.title + "】接入配置:" + (cfgText || "未配置 path") + "\n透传上游产物:\n" + contract);
|
|
1085
|
+
case "merge":
|
|
1086
|
+
return `【汇聚合并 ${node.title}】已汇总上游分支产物:\n${contract}`;
|
|
1087
|
+
case "memory":
|
|
1088
|
+
return `【记忆读写 ${node.title}】记忆键:${cfgText || "default"}\n快照:\n${contract}`;
|
|
1089
|
+
case "end":
|
|
1090
|
+
return `【流水线结束 ${node.title}】最终交付汇总:\n${contract}`;
|
|
1091
|
+
default:
|
|
1092
|
+
return `【控制节点 ${node.title}】透传:\n${contract}`;
|
|
1093
|
+
}
|
|
1094
|
+
}
|