@fieldwangai/agentflow 0.1.153 → 0.1.154
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 +16 -34
- package/README.zh-CN.md +14 -34
- package/agents/agentflow-node-executor-code.md +13 -3
- package/agents/agentflow-node-executor-planning.md +13 -3
- package/agents/agentflow-node-executor-requirement.md +13 -3
- package/agents/agentflow-node-executor-test.md +13 -3
- package/agents/agentflow-node-executor-ui.md +13 -3
- package/agents/agentflow-node-executor.md +13 -3
- package/agents/en/agentflow-node-executor.md +13 -3
- package/agents/zh/agentflow-node-executor.md +13 -3
- package/bin/agentflow.mjs +3 -3
- package/bin/lib/admin-builtin-pipelines.mjs +3 -2
- package/bin/lib/agent-runners.mjs +0 -929
- package/bin/lib/auth.mjs +2 -1
- package/bin/lib/catalog-agents.mjs +2 -1
- package/bin/lib/catalog-flows.mjs +165 -90
- package/bin/lib/composer-agent.mjs +3 -793
- package/bin/lib/composer-skill-router.mjs +3 -323
- package/bin/lib/exec-buffered.mjs +32 -0
- package/bin/lib/flow-dsl/cli.mjs +276 -0
- package/bin/lib/flow-dsl/codegen.mjs +358 -0
- package/bin/lib/flow-dsl/defs.mjs +103 -0
- package/bin/lib/flow-dsl/index.mjs +77 -0
- package/bin/lib/flow-dsl/ir.mjs +305 -0
- package/bin/lib/flow-dsl/layout.mjs +115 -0
- package/bin/lib/flow-dsl/legacy-yaml.mjs +268 -0
- package/bin/lib/flow-dsl/lint.mjs +241 -0
- package/bin/lib/flow-dsl/packages.mjs +251 -0
- package/bin/lib/flow-dsl/parser.mjs +444 -0
- package/bin/lib/flow-import.mjs +96 -26
- package/bin/lib/flow-write.mjs +48 -12
- package/bin/lib/help.mjs +8 -28
- package/bin/lib/html-escape.mjs +19 -0
- package/bin/lib/http-util.mjs +49 -0
- package/bin/lib/legacy-flow-execution.mjs +37 -0
- package/bin/lib/locales/en.json +1 -1
- package/bin/lib/locales/zh.json +1 -1
- package/bin/lib/main.mjs +152 -286
- package/bin/lib/marketplace.mjs +206 -61
- package/bin/lib/model-config.mjs +10 -0
- package/bin/lib/node-package-archive.mjs +240 -0
- package/bin/lib/node-package-bootstrap.mjs +72 -0
- package/bin/lib/node-package-manifest.mjs +174 -0
- package/bin/lib/paths.mjs +42 -19
- package/bin/lib/prd-workflow-routes.mjs +2949 -0
- package/bin/lib/prd-workflow-server.mjs +4884 -0
- package/bin/lib/run-events.mjs +0 -66
- package/bin/lib/schedule-config.mjs +4 -4
- package/bin/lib/skill-runtime.mjs +35 -0
- package/bin/lib/startup-storage-migrations.mjs +274 -0
- package/bin/lib/ui-server.mjs +1087 -18122
- package/bin/lib/user-env.mjs +16 -0
- package/bin/lib/workspace-auto-layout.mjs +234 -0
- package/bin/lib/workspace-flow-store.mjs +478 -0
- package/bin/lib/workspace-graph-merge.mjs +41 -34
- package/bin/lib/workspace-routes.mjs +3088 -0
- package/bin/lib/workspace-server.mjs +6199 -0
- package/bin/lib/workspace-state.mjs +331 -0
- package/bin/lib/workspace.mjs +2 -1
- package/builtin/nodes/agent_subAgent.md +1 -0
- package/builtin/nodes/control_agent_toBool.md +2 -0
- package/builtin/nodes/control_anyOne.md +2 -0
- package/builtin/nodes/control_cancelled.md +2 -0
- package/builtin/nodes/control_cd_workspace.md +5 -11
- package/builtin/nodes/control_delay.md +2 -0
- package/builtin/nodes/control_end.md +2 -0
- package/builtin/nodes/control_if.md +1 -0
- package/builtin/nodes/control_interval_loop.md +2 -0
- package/builtin/nodes/control_load_mcp.md +24 -0
- package/builtin/nodes/control_load_skills.md +8 -23
- package/builtin/nodes/control_start.md +2 -0
- package/builtin/nodes/control_toBool.md +2 -0
- package/builtin/nodes/control_user_workspace.md +2 -0
- package/builtin/nodes/control_wait_until.md +2 -0
- package/builtin/nodes/display_ascii.md +1 -0
- package/builtin/nodes/display_chart.md +1 -0
- package/builtin/nodes/display_html.md +1 -0
- package/builtin/nodes/display_image.md +1 -0
- package/builtin/nodes/display_markdown.md +1 -0
- package/builtin/nodes/display_mermaid.md +1 -0
- package/builtin/nodes/display_react_app.md +1 -0
- package/builtin/nodes/display_table.md +1 -0
- package/builtin/nodes/provide_bool.md +1 -0
- package/builtin/nodes/provide_file.md +1 -0
- package/builtin/nodes/provide_password.md +1 -0
- package/builtin/nodes/provide_str.md +1 -0
- package/builtin/nodes/tool_display_share_link.md +1 -0
- package/builtin/nodes/tool_get_env.md +2 -0
- package/builtin/nodes/tool_git_checkout.md +1 -0
- package/builtin/nodes/tool_git_worktree_load.md +1 -0
- package/builtin/nodes/tool_git_worktree_unload.md +1 -0
- package/builtin/nodes/tool_gitlab_create_mr.md +1 -0
- package/builtin/nodes/tool_jenkins_build.md +2 -0
- package/builtin/nodes/tool_load_key.md +2 -0
- package/builtin/nodes/tool_nodejs.md +17 -19
- package/builtin/nodes/tool_print.md +2 -0
- package/builtin/nodes/tool_save_key.md +2 -0
- package/builtin/nodes/tool_set_run_env.md +1 -0
- package/builtin/nodes/tool_user_ask.md +2 -0
- package/builtin/nodes/tool_user_check.md +2 -0
- package/builtin/nodes/tool_wecom_send_app_markdown.md +1 -0
- package/builtin/nodes/tool_wecom_send_group_markdown.md +1 -0
- package/builtin/nodes/workspace_one_click_task.md +44 -0
- package/builtin/nodes/workspace_run.md +16 -0
- package/builtin/nodes/workspace_scheduled_run.md +16 -0
- package/builtin/pipelines/module-migrate/scripts/gate.mjs +37 -0
- package/builtin/pipelines/module-migrate/scripts/static-check.mjs +82 -0
- package/builtin/pipelines/module-migrate/workspace.flow.js +172 -0
- package/builtin/pipelines/module-migrate/workspace.layout.json +134 -0
- package/builtin/pipelines/new/scripts/lint-flow.mjs +38 -0
- package/builtin/pipelines/new/workspace.flow.js +91 -0
- package/builtin/pipelines/new/workspace.layout.json +70 -0
- package/builtin/web-ui/dist/assets/{WorkflowAssistantThread-B9xslwI0.js → WorkflowAssistantThread-CKClwj96.js} +1 -1
- package/builtin/web-ui/dist/assets/index-BZ5KqLur.js +870 -0
- package/builtin/web-ui/dist/assets/index-CEXmmwM2.css +1 -0
- package/builtin/web-ui/dist/index.html +2 -2
- package/package.json +2 -1
- package/reference/flow-control-capabilities.md +77 -158
- package/reference/flow-layout.md +1 -1
- package/reference/flow-prompt-handler-check.md +2 -2
- package/skills/agentflow-author-flow/SKILL.md +1 -1
- package/skills/agentflow-cli/SKILL.md +139 -24
- package/skills/agentflow-cli/agents/openai.yaml +2 -2
- package/skills/agentflow-cli/scripts/agentflow-cli.mjs +654 -20
- package/skills/agentflow-cli/scripts/agentflow-runtime.mjs +97 -0
- package/skills/agentflow-flow-add-instances/SKILL.md +1 -1
- package/skills/agentflow-flow-dsl/SKILL.md +206 -0
- package/skills/agentflow-flow-dsl/agents/openai.yaml +4 -0
- package/skills/agentflow-flow-dsl/references/node-calls.md +39 -0
- package/skills/agentflow-flow-edit-node-fields/SKILL.md +1 -1
- package/skills/agentflow-flow-recipes/SKILL.md +7 -4
- package/skills/agentflow-flow-recipes/references/recipes.md +97 -43
- package/skills/agentflow-flow-sync-ui/SKILL.md +1 -1
- package/skills/agentflow-node-dsl/SKILL.md +212 -0
- package/skills/agentflow-node-dsl/agents/openai.yaml +4 -0
- package/skills/agentflow-node-reference/SKILL.md +2 -2
- package/skills/agentflow-node-reference/references/builtin-nodes.md +166 -115
- package/skills/agentflow-runtime-reference/references/runtime.md +1 -1
- package/skills/agentflow-workspace-ascii/SKILL.md +9 -16
- package/skills/agentflow-workspace-graph/SKILL.md +62 -48
- package/skills/agentflow-workspace-html/SKILL.md +7 -2
- package/skills/agentflow-workspace-image/SKILL.md +6 -2
- package/skills/agentflow-workspace-markdown/SKILL.md +14 -21
- package/skills/agentflow-workspace-mermaid/SKILL.md +8 -16
- package/bin/lib/api-runner.mjs +0 -387
- package/bin/lib/apply.mjs +0 -903
- package/bin/lib/composer-flow-instances.mjs +0 -68
- package/bin/lib/composer-flow-skeleton.mjs +0 -334
- package/bin/lib/composer-flow-validate.mjs +0 -47
- package/bin/lib/composer-model-router.mjs +0 -185
- package/bin/lib/composer-node-schema.mjs +0 -303
- package/bin/lib/composer-planner.mjs +0 -751
- package/bin/lib/composer-script-ops.mjs +0 -233
- package/bin/lib/flow-static-preview.mjs +0 -104
- package/bin/lib/hub-login.mjs +0 -54
- package/bin/lib/hub-publish.mjs +0 -159
- package/bin/lib/hub-remote.mjs +0 -189
- package/bin/lib/hub.mjs +0 -299
- package/bin/lib/jenkins.mjs +0 -380
- package/bin/lib/node-execute.mjs +0 -539
- package/bin/lib/normalize-node-tool-command.mjs +0 -97
- package/bin/lib/runtime-context.mjs +0 -243
- package/bin/lib/scheduler.mjs +0 -601
- package/bin/lib/ui-print.mjs +0 -94
- package/bin/pipeline/build-node-prompt.mjs +0 -271
- package/bin/pipeline/check-cache.mjs +0 -191
- package/bin/pipeline/check-flow.mjs +0 -543
- package/bin/pipeline/collect-nodes.mjs +0 -212
- package/bin/pipeline/compute-cache-md5.mjs +0 -177
- package/bin/pipeline/ensure-run-dir.mjs +0 -71
- package/bin/pipeline/gc.mjs +0 -129
- package/bin/pipeline/get-env.mjs +0 -59
- package/bin/pipeline/get-resolved-values.mjs +0 -344
- package/bin/pipeline/load-key.mjs +0 -62
- package/bin/pipeline/parse-flow.mjs +0 -708
- package/bin/pipeline/post-process-control-if.mjs +0 -23
- package/bin/pipeline/post-process-node.mjs +0 -490
- package/bin/pipeline/pre-process-node.mjs +0 -1430
- package/bin/pipeline/resolve-inputs.mjs +0 -201
- package/bin/pipeline/run-tool-nodejs.mjs +0 -167
- package/bin/pipeline/save-key.mjs +0 -93
- package/bin/pipeline/snapshot-prior-round.mjs +0 -70
- package/bin/pipeline/validate-for-ui.mjs +0 -234
- package/bin/pipeline/validate-script-output.mjs +0 -130
- package/bin/pipeline/write-result.mjs +0 -182
- package/builtin/pipelines/module-migrate/flow.yaml +0 -819
- package/builtin/pipelines/new/flow.yaml +0 -545
- package/builtin/pipelines/new/scripts/check-flow.mjs +0 -9
- package/builtin/pipelines/new/scripts/collect-nodes.mjs +0 -211
- package/builtin/web-ui/dist/assets/index-DZ328oSo.css +0 -1
- package/builtin/web-ui/dist/assets/index-DmUV7ZCL.js +0 -888
- package/skills/agentflow-node-authoring/SKILL.md +0 -57
package/bin/lib/node-execute.mjs
DELETED
|
@@ -1,539 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from "child_process";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { outputNodeBasename, outputDirForNode } from "../pipeline/get-exec-id.mjs";
|
|
5
|
-
import { writeResult } from "../pipeline/write-result.mjs";
|
|
6
|
-
import {
|
|
7
|
-
runCodexAgentForNode,
|
|
8
|
-
runCodexAgentWithPrompt,
|
|
9
|
-
runClaudeCodeAgentForNode,
|
|
10
|
-
runClaudeCodeAgentWithPrompt,
|
|
11
|
-
runCursorAgentForNode,
|
|
12
|
-
runCursorAgentWithPrompt,
|
|
13
|
-
runOpenCodeAgentForNode,
|
|
14
|
-
runOpenCodeAgentWithPrompt,
|
|
15
|
-
} from "./agent-runners.mjs";
|
|
16
|
-
import { runApiAgentForNode } from "./api-runner.mjs";
|
|
17
|
-
import { log } from "./log.mjs";
|
|
18
|
-
import { LOCAL_ONLY_DEFINITION_IDS, LOCAL_ONLY_TERMINAL_SUCCESS_IDS } from "./paths.mjs";
|
|
19
|
-
import { resolveCliAndModel } from "./model-config.mjs";
|
|
20
|
-
import { parseJsonStdout, runNodeScript } from "./pipeline-scripts.mjs";
|
|
21
|
-
import { emitEvent } from "./run-events.mjs";
|
|
22
|
-
import { getRunDir } from "./workspace.mjs";
|
|
23
|
-
import { nodeToolCommandToArgv } from "./normalize-node-tool-command.mjs";
|
|
24
|
-
import { buildPipelineScriptPathHint } from "./flow-normalize.mjs";
|
|
25
|
-
import { validateAndParse } from "../pipeline/validate-script-output.mjs";
|
|
26
|
-
|
|
27
|
-
const TOOL_NODEJS_MAX_RETRIES = 3;
|
|
28
|
-
const TOOL_NODEJS_RETRY_DELAY_MS = 1000;
|
|
29
|
-
const AI_HEAL_ENABLED = !(
|
|
30
|
-
process.env.AGENTFLOW_TOOL_NODEJS_AI_HEAL === "0" ||
|
|
31
|
-
process.env.AGENTFLOW_TOOL_NODEJS_AI_HEAL === "false"
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
// ─── AI 自愈:失败后调用 Cursor/OpenCode CLI 修复脚本再重试 ──────────────────
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @returns {{ path: string | null, parsed: string | null, reason: "none" | "unparsable" | "not-found" }}
|
|
38
|
-
*/
|
|
39
|
-
function extractScriptPath(resolvedScript) {
|
|
40
|
-
const { argv } = nodeToolCommandToArgv(resolvedScript);
|
|
41
|
-
if (argv.length === 0) return { path: null, parsed: null, reason: "unparsable" };
|
|
42
|
-
const candidate = argv[0];
|
|
43
|
-
if (!candidate) return { path: null, parsed: null, reason: "unparsable" };
|
|
44
|
-
const abs = path.isAbsolute(candidate) ? candidate : path.resolve(candidate);
|
|
45
|
-
if (fs.existsSync(abs)) return { path: abs, parsed: abs, reason: "none" };
|
|
46
|
-
return { path: null, parsed: abs, reason: "not-found" };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
function buildHealPrompt(scriptPath, command, errorInfo, scriptContent) {
|
|
51
|
-
const stderrSlice = (errorInfo.stderr || "").trim().slice(0, 4000) || "(无)";
|
|
52
|
-
const stdoutSlice = (errorInfo.stdout || "").trim().slice(0, 2000) || "(无)";
|
|
53
|
-
return [
|
|
54
|
-
"你是脚本调试助手。以下 Node.js 脚本执行失败,请直接修复脚本文件中的错误。",
|
|
55
|
-
"",
|
|
56
|
-
`## 脚本路径\n${scriptPath}`,
|
|
57
|
-
`## 执行命令\n${command}`,
|
|
58
|
-
`## 退出码\n${errorInfo.exitCode}`,
|
|
59
|
-
`## 标准错误 (stderr)\n${stderrSlice}`,
|
|
60
|
-
`## 标准输出 (stdout)\n${stdoutSlice}`,
|
|
61
|
-
"## 当前脚本内容",
|
|
62
|
-
"```javascript",
|
|
63
|
-
scriptContent.slice(0, 30000),
|
|
64
|
-
"```",
|
|
65
|
-
"",
|
|
66
|
-
"## 修复要求",
|
|
67
|
-
`1. 直接编辑脚本文件 \`${scriptPath}\` 修复错误`,
|
|
68
|
-
"2. 保持脚本的输入输出格式不变(stdout:JSON `{\"err_code\":0,\"message\":{...}}` 中 message 的键对应各输出槽位,或纯文本→写入 result)",
|
|
69
|
-
"3. 只修复导致失败的问题,不做无关改动",
|
|
70
|
-
"4. 不要创建新文件、不要修改其他文件",
|
|
71
|
-
].join("\n");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 调用 Cursor/OpenCode CLI 让 AI 分析 stderr 并修复脚本文件,然后由调用方重新执行。
|
|
76
|
-
* @returns {Promise<boolean>} 是否成功完成修复(AI 调用无报错即视为成功,实际修复效果由重试验证)
|
|
77
|
-
*/
|
|
78
|
-
async function healToolNodejsWithAI(workspaceRoot, flowName, uuid, instanceId, resolvedScript, errorInfo, cli, model) {
|
|
79
|
-
const ext = extractScriptPath(resolvedScript);
|
|
80
|
-
if (!ext.path) {
|
|
81
|
-
if (ext.reason === "not-found") {
|
|
82
|
-
log.warn(
|
|
83
|
-
`[tool_nodejs AI 自愈] 脚本文件不存在,跳过(这是 flow.yaml 模板路径错,非脚本内容错):` +
|
|
84
|
-
`${ext.parsed}。建议把 flow.yaml 里 tool_nodejs.script 的 ` +
|
|
85
|
-
`\`\${workspaceRoot}/.workspace/agentflow/pipelines/\${flowName}/scripts/...\` ` +
|
|
86
|
-
`改为 \`\${flowDir}/scripts/xxx.mjs\`(兼容 user/workspace/builtin 安装)`,
|
|
87
|
-
);
|
|
88
|
-
} else {
|
|
89
|
-
log.warn(`[tool_nodejs AI 自愈] 无法从命令提取脚本路径,跳过: ${resolvedScript.slice(0, 120)}`);
|
|
90
|
-
}
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
const scriptPath = ext.path;
|
|
94
|
-
|
|
95
|
-
let scriptContent;
|
|
96
|
-
try {
|
|
97
|
-
scriptContent = fs.readFileSync(scriptPath, "utf-8");
|
|
98
|
-
} catch {
|
|
99
|
-
log.warn(`[tool_nodejs AI 自愈] 无法读取脚本文件,跳过: ${scriptPath}`);
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const prompt = buildHealPrompt(scriptPath, resolvedScript, errorInfo, scriptContent);
|
|
104
|
-
const healCli =
|
|
105
|
-
cli === "opencode" ? "opencode" : cli === "codex" ? "codex" : cli === "claude-code" ? "claude-code" : "cursor";
|
|
106
|
-
|
|
107
|
-
log.info(`[tool_nodejs AI 自愈] ${instanceId} 调用 ${healCli}${model ? ` (${model})` : ""} 修复 ${path.basename(scriptPath)}`);
|
|
108
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
109
|
-
event: "tool-nodejs-ai-heal-start",
|
|
110
|
-
instanceId,
|
|
111
|
-
scriptPath,
|
|
112
|
-
cli: healCli,
|
|
113
|
-
model: model ?? null,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
if (healCli === "opencode") {
|
|
118
|
-
const { finished } = runOpenCodeAgentWithPrompt(workspaceRoot, prompt, { model: model || undefined, force: true });
|
|
119
|
-
await finished;
|
|
120
|
-
} else if (healCli === "codex") {
|
|
121
|
-
const { finished } = runCodexAgentWithPrompt(workspaceRoot, prompt, { model: model || undefined, force: true });
|
|
122
|
-
await finished;
|
|
123
|
-
} else if (healCli === "claude-code") {
|
|
124
|
-
const { finished } = runClaudeCodeAgentWithPrompt(workspaceRoot, prompt, { model: model || undefined });
|
|
125
|
-
await finished;
|
|
126
|
-
} else {
|
|
127
|
-
const { finished } = runCursorAgentWithPrompt(workspaceRoot, prompt, { model: model || undefined });
|
|
128
|
-
await finished;
|
|
129
|
-
}
|
|
130
|
-
log.info(`[tool_nodejs AI 自愈] ${instanceId} AI 修复完成,即将重试脚本`);
|
|
131
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
132
|
-
event: "tool-nodejs-ai-heal-done",
|
|
133
|
-
instanceId,
|
|
134
|
-
scriptPath,
|
|
135
|
-
});
|
|
136
|
-
return true;
|
|
137
|
-
} catch (healErr) {
|
|
138
|
-
log.warn(`[tool_nodejs AI 自愈] ${instanceId} AI 修复失败: ${healErr.message?.slice(0, 200) || healErr}`);
|
|
139
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
140
|
-
event: "tool-nodejs-ai-heal-failed",
|
|
141
|
-
instanceId,
|
|
142
|
-
scriptPath,
|
|
143
|
-
error: healErr.message?.slice(0, 300) || String(healErr),
|
|
144
|
-
});
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// ─── 内联执行 + 重试 ────────────────────────────────────────────────────────
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* tool_nodejs + script 内联执行:直接跑脚本、写 result,无中间进程。
|
|
153
|
-
* 失败时可通过 AI(Cursor/OpenCode CLI)自动分析 stderr 修复脚本再重试,
|
|
154
|
-
* 最多重试 TOOL_NODEJS_MAX_RETRIES 次。
|
|
155
|
-
* 设置 AGENTFLOW_TOOL_NODEJS_AI_HEAL=0 可关闭 AI 自愈回退为简单重试。
|
|
156
|
-
*
|
|
157
|
-
* 协议:与 run-tool-nodejs.mjs 一致——validate-script-output 解析 stdout;
|
|
158
|
-
* JSON 时 message 的每个键写入同名 output 槽位(含 result);纯文本则等价于 message.result。
|
|
159
|
-
*/
|
|
160
|
-
async function executeToolNodejsInline(workspaceRoot, flowName, uuid, instanceId, resolvedScript, execId, healOptions, execWorkspaceRoot = workspaceRoot) {
|
|
161
|
-
let lastError;
|
|
162
|
-
for (let attempt = 1; attempt <= TOOL_NODEJS_MAX_RETRIES + 1; attempt++) {
|
|
163
|
-
try {
|
|
164
|
-
executeToolNodejsOnce(workspaceRoot, flowName, uuid, instanceId, resolvedScript, execId, execWorkspaceRoot);
|
|
165
|
-
return;
|
|
166
|
-
} catch (err) {
|
|
167
|
-
lastError = err;
|
|
168
|
-
if (attempt <= TOOL_NODEJS_MAX_RETRIES) {
|
|
169
|
-
const tag = `[tool_nodejs 自愈] ${instanceId} 第 ${attempt}/${TOOL_NODEJS_MAX_RETRIES} 次重试`;
|
|
170
|
-
log.warn(`${tag}:${err.message?.slice(0, 200) || err}`);
|
|
171
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
172
|
-
event: "tool-nodejs-retry",
|
|
173
|
-
instanceId,
|
|
174
|
-
attempt,
|
|
175
|
-
maxRetries: TOOL_NODEJS_MAX_RETRIES,
|
|
176
|
-
error: err.message?.slice(0, 300) || String(err),
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
if (AI_HEAL_ENABLED && healOptions) {
|
|
180
|
-
const errorInfo = {
|
|
181
|
-
stdout: err.scriptStdout || "",
|
|
182
|
-
stderr: err.scriptStderr || "",
|
|
183
|
-
exitCode: err.scriptExitCode ?? 1,
|
|
184
|
-
};
|
|
185
|
-
await healToolNodejsWithAI(
|
|
186
|
-
workspaceRoot, flowName, uuid, instanceId, resolvedScript,
|
|
187
|
-
errorInfo, healOptions.cli, healOptions.model,
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (TOOL_NODEJS_RETRY_DELAY_MS > 0) {
|
|
192
|
-
spawnSync("sleep", [String(TOOL_NODEJS_RETRY_DELAY_MS / 1000)], { stdio: "ignore" });
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
throw lastError;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function persistToolNodejsStderr(outputDir, instanceId, execId, stderr) {
|
|
201
|
-
if (!stderr) return;
|
|
202
|
-
try {
|
|
203
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
204
|
-
fs.writeFileSync(path.join(outputDir, outputNodeBasename(instanceId, execId, "stderr")), stderr, "utf-8");
|
|
205
|
-
} catch (_) {}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function executeToolNodejsOnce(workspaceRoot, flowName, uuid, instanceId, resolvedScript, execId, execWorkspaceRoot = workspaceRoot) {
|
|
209
|
-
const runDir = getRunDir(workspaceRoot, flowName, uuid);
|
|
210
|
-
const outputDir = path.join(runDir, outputDirForNode(instanceId));
|
|
211
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
212
|
-
|
|
213
|
-
const { argv, commandLine: normalized } = nodeToolCommandToArgv(resolvedScript);
|
|
214
|
-
let child;
|
|
215
|
-
if (/^node\s/i.test(String(normalized).trim()) && argv.length >= 1) {
|
|
216
|
-
child = spawnSync(process.execPath, argv, {
|
|
217
|
-
cwd: execWorkspaceRoot,
|
|
218
|
-
shell: false,
|
|
219
|
-
stdio: ["inherit", "pipe", "pipe"],
|
|
220
|
-
});
|
|
221
|
-
} else {
|
|
222
|
-
child = spawnSync(normalized, [], {
|
|
223
|
-
cwd: execWorkspaceRoot,
|
|
224
|
-
shell: true,
|
|
225
|
-
stdio: ["inherit", "pipe", "pipe"],
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const stdout = child.stdout?.toString("utf-8") ?? "";
|
|
230
|
-
const stderr = child.stderr?.toString("utf-8") ?? "";
|
|
231
|
-
const exitCode = child.status ?? 1;
|
|
232
|
-
|
|
233
|
-
// 直接写文件模式:stdout 为空 + exit 0 → 脚本已自行写入 output 文件,无需解析
|
|
234
|
-
if (!stdout.trim() && exitCode === 0) {
|
|
235
|
-
persistToolNodejsStderr(outputDir, instanceId, execId, stderr);
|
|
236
|
-
writeResult(workspaceRoot, flowName, uuid, instanceId,
|
|
237
|
-
{ status: "success", message: "执行完成" },
|
|
238
|
-
{ preserveBody: true, execId });
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// 非零退出码 + 无 stdout → 直接失败
|
|
243
|
-
if (!stdout.trim() && exitCode !== 0) {
|
|
244
|
-
const baseDetail = `脚本退出码 ${exitCode}` + (stderr.trim() ? `:${stderr.trim().slice(0, 200)}` : "");
|
|
245
|
-
const detail = baseDetail + buildPipelineScriptPathHint(stderr);
|
|
246
|
-
persistToolNodejsStderr(outputDir, instanceId, execId, stderr);
|
|
247
|
-
writeResult(workspaceRoot, flowName, uuid, instanceId,
|
|
248
|
-
{ status: "failed", message: detail },
|
|
249
|
-
{ preserveBody: true, execId });
|
|
250
|
-
const err = new Error(`Script failed: ${detail}`);
|
|
251
|
-
err.scriptStdout = stdout;
|
|
252
|
-
err.scriptStderr = stderr;
|
|
253
|
-
err.scriptExitCode = exitCode;
|
|
254
|
-
throw err;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// JSON stdout 模式(兼容旧脚本)
|
|
258
|
-
const { ok, errors, payload } = validateAndParse(stdout);
|
|
259
|
-
|
|
260
|
-
if (!ok) {
|
|
261
|
-
const baseDetail =
|
|
262
|
-
exitCode !== 0
|
|
263
|
-
? `脚本退出码 ${exitCode}` + (stderr.trim() ? `:${stderr.trim().slice(0, 200)}` : "")
|
|
264
|
-
: errors.length
|
|
265
|
-
? errors.join("; ")
|
|
266
|
-
: "脚本无输出";
|
|
267
|
-
const detail = baseDetail + buildPipelineScriptPathHint(stderr);
|
|
268
|
-
persistToolNodejsStderr(outputDir, instanceId, execId, stderr);
|
|
269
|
-
writeResult(workspaceRoot, flowName, uuid, instanceId,
|
|
270
|
-
{ status: "failed", message: detail },
|
|
271
|
-
{ preserveBody: true, execId });
|
|
272
|
-
const err = new Error(`Script failed: ${detail}`);
|
|
273
|
-
err.scriptStdout = stdout;
|
|
274
|
-
err.scriptStderr = stderr;
|
|
275
|
-
err.scriptExitCode = exitCode;
|
|
276
|
-
throw err;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
const isSynthetic = Boolean(payload._synthetic);
|
|
280
|
-
const success = isSynthetic ? exitCode === 0 : payload.err_code === 0;
|
|
281
|
-
const message = payload.message;
|
|
282
|
-
// 备份由 snapshotPriorRoundIfNeeded 在 pre-process 入口统一处理,此处只写新 output。
|
|
283
|
-
for (const slot of Object.keys(message)) {
|
|
284
|
-
if (slot === "_synthetic") continue;
|
|
285
|
-
const content = message[slot];
|
|
286
|
-
if (content == null) continue;
|
|
287
|
-
fs.writeFileSync(
|
|
288
|
-
path.join(outputDir, outputNodeBasename(instanceId, execId, slot)),
|
|
289
|
-
String(content),
|
|
290
|
-
"utf-8",
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
persistToolNodejsStderr(outputDir, instanceId, execId, stderr);
|
|
294
|
-
|
|
295
|
-
writeResult(workspaceRoot, flowName, uuid, instanceId,
|
|
296
|
-
{ status: success ? "success" : "failed",
|
|
297
|
-
message: success ? "执行完成" : "执行未通过" },
|
|
298
|
-
{ preserveBody: true, execId });
|
|
299
|
-
|
|
300
|
-
if (!success) {
|
|
301
|
-
const hint = stderr.trim() ? ` ${stderr.trim().slice(0, 280)}` : "";
|
|
302
|
-
const err = new Error(`Script failed (exit ${exitCode}): ${String(normalized).slice(0, 120)}${hint}`);
|
|
303
|
-
err.scriptStdout = stdout;
|
|
304
|
-
err.scriptStderr = stderr;
|
|
305
|
-
err.scriptExitCode = exitCode;
|
|
306
|
-
throw err;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Execute one node: 按 definitionId / resolvedScript / directCommand 决定执行方式。
|
|
312
|
-
*/
|
|
313
|
-
export async function executeNode(workspaceRoot, flowName, uuid, instanceId, preOutput, options = {}) {
|
|
314
|
-
const { definitionId, directCommand, resolvedScript, promptPath, nodeContext, taskBody, resultPath, subagent } = preOutput;
|
|
315
|
-
const runDir = getRunDir(workspaceRoot, flowName, uuid);
|
|
316
|
-
const intermediatePath = runDir;
|
|
317
|
-
const execWorkspaceRoot = path.resolve(preOutput.workspaceContext?.workspaceRoot || preOutput.workspaceContext?.cwd || workspaceRoot);
|
|
318
|
-
|
|
319
|
-
if (definitionId && LOCAL_ONLY_DEFINITION_IDS.has(definitionId)) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (resolvedScript) {
|
|
324
|
-
const execId = preOutput.execId ?? 1;
|
|
325
|
-
let healOptions = null;
|
|
326
|
-
if (AI_HEAL_ENABLED) {
|
|
327
|
-
const { cli, model } = resolveCliAndModel(workspaceRoot, preOutput.model ?? null, options.model ?? null);
|
|
328
|
-
healOptions = {
|
|
329
|
-
cli: cli === "api" ? "cursor" : cli,
|
|
330
|
-
model,
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
334
|
-
event: "direct-command-start",
|
|
335
|
-
instanceId,
|
|
336
|
-
directCommand: resolvedScript,
|
|
337
|
-
});
|
|
338
|
-
try {
|
|
339
|
-
await executeToolNodejsInline(workspaceRoot, flowName, uuid, instanceId, resolvedScript, execId, healOptions, execWorkspaceRoot);
|
|
340
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
341
|
-
event: "direct-command-done",
|
|
342
|
-
instanceId,
|
|
343
|
-
directCommand: resolvedScript,
|
|
344
|
-
});
|
|
345
|
-
} catch (err) {
|
|
346
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
347
|
-
event: "direct-command-failed",
|
|
348
|
-
instanceId,
|
|
349
|
-
directCommand: resolvedScript,
|
|
350
|
-
error: err.message,
|
|
351
|
-
});
|
|
352
|
-
throw err;
|
|
353
|
-
}
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (directCommand) {
|
|
358
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
359
|
-
event: "direct-command-start",
|
|
360
|
-
instanceId,
|
|
361
|
-
directCommand,
|
|
362
|
-
});
|
|
363
|
-
try {
|
|
364
|
-
const result = spawnSync(directCommand, [], { cwd: execWorkspaceRoot, shell: true, stdio: "inherit" });
|
|
365
|
-
if (result.status !== 0) {
|
|
366
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
367
|
-
event: "direct-command-failed",
|
|
368
|
-
instanceId,
|
|
369
|
-
directCommand,
|
|
370
|
-
exitCode: result.status,
|
|
371
|
-
});
|
|
372
|
-
throw new Error(`Direct command failed: ${directCommand}`);
|
|
373
|
-
}
|
|
374
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
375
|
-
event: "direct-command-done",
|
|
376
|
-
instanceId,
|
|
377
|
-
directCommand,
|
|
378
|
-
});
|
|
379
|
-
} catch (err) {
|
|
380
|
-
if (err.message && !err.message.includes("Direct command failed")) {
|
|
381
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
382
|
-
event: "direct-command-failed",
|
|
383
|
-
instanceId,
|
|
384
|
-
directCommand,
|
|
385
|
-
error: err.message,
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
throw err;
|
|
389
|
-
}
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
const { cli, model } = resolveCliAndModel(workspaceRoot, preOutput.model ?? null, options.model ?? null);
|
|
394
|
-
|
|
395
|
-
const execId = preOutput.execId ?? 1;
|
|
396
|
-
// 备份由 snapshotPriorRoundIfNeeded 在 pre-process 入口统一处理,此处直接进入 agent 执行。
|
|
397
|
-
|
|
398
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
399
|
-
event: "agent-invoke-start",
|
|
400
|
-
instanceId,
|
|
401
|
-
subagent: subagent ?? null,
|
|
402
|
-
promptPath: promptPath ?? null,
|
|
403
|
-
resultPathRel: resultPath ?? null,
|
|
404
|
-
modelCli: cli,
|
|
405
|
-
model: model ?? null,
|
|
406
|
-
execWorkspaceRoot,
|
|
407
|
-
});
|
|
408
|
-
try {
|
|
409
|
-
if (cli === "api") {
|
|
410
|
-
await runApiAgentForNode(
|
|
411
|
-
workspaceRoot,
|
|
412
|
-
{ promptPath, nodeContext: nodeContext ?? "", taskBody: taskBody ?? "", intermediatePath, resultPathRel: resultPath, subagent, instanceId },
|
|
413
|
-
{
|
|
414
|
-
model,
|
|
415
|
-
onToolCall: options.onToolCall,
|
|
416
|
-
flowName,
|
|
417
|
-
uuid,
|
|
418
|
-
execWorkspaceRoot,
|
|
419
|
-
},
|
|
420
|
-
);
|
|
421
|
-
} else if (cli === "opencode") {
|
|
422
|
-
await runOpenCodeAgentForNode(
|
|
423
|
-
workspaceRoot,
|
|
424
|
-
{ promptPath, nodeContext: nodeContext ?? "", taskBody: taskBody ?? "", intermediatePath, resultPathRel: resultPath, subagent, instanceId },
|
|
425
|
-
{
|
|
426
|
-
model,
|
|
427
|
-
stderrBuffer: options.stderrBuffer,
|
|
428
|
-
force: options.force,
|
|
429
|
-
outputPrefix: options.outputPrefix,
|
|
430
|
-
prefixColor: options.prefixColor,
|
|
431
|
-
onToolCall: options.onToolCall,
|
|
432
|
-
flowName,
|
|
433
|
-
uuid,
|
|
434
|
-
execWorkspaceRoot,
|
|
435
|
-
},
|
|
436
|
-
);
|
|
437
|
-
} else if (cli === "codex") {
|
|
438
|
-
await runCodexAgentForNode(
|
|
439
|
-
workspaceRoot,
|
|
440
|
-
{ promptPath, nodeContext: nodeContext ?? "", taskBody: taskBody ?? "", intermediatePath, resultPathRel: resultPath, subagent, instanceId },
|
|
441
|
-
{
|
|
442
|
-
model,
|
|
443
|
-
stderrBuffer: options.stderrBuffer,
|
|
444
|
-
force: options.force,
|
|
445
|
-
outputPrefix: options.outputPrefix,
|
|
446
|
-
prefixColor: options.prefixColor,
|
|
447
|
-
onToolCall: options.onToolCall,
|
|
448
|
-
flowName,
|
|
449
|
-
uuid,
|
|
450
|
-
execWorkspaceRoot,
|
|
451
|
-
},
|
|
452
|
-
);
|
|
453
|
-
} else if (cli === "claude-code") {
|
|
454
|
-
await runClaudeCodeAgentForNode(
|
|
455
|
-
workspaceRoot,
|
|
456
|
-
{ promptPath, nodeContext: nodeContext ?? "", taskBody: taskBody ?? "", intermediatePath, resultPathRel: resultPath, subagent, instanceId },
|
|
457
|
-
{
|
|
458
|
-
model,
|
|
459
|
-
stderrBuffer: options.stderrBuffer,
|
|
460
|
-
force: options.force,
|
|
461
|
-
outputPrefix: options.outputPrefix,
|
|
462
|
-
prefixColor: options.prefixColor,
|
|
463
|
-
onToolCall: options.onToolCall,
|
|
464
|
-
flowName,
|
|
465
|
-
uuid,
|
|
466
|
-
execWorkspaceRoot,
|
|
467
|
-
},
|
|
468
|
-
);
|
|
469
|
-
} else {
|
|
470
|
-
await runCursorAgentForNode(
|
|
471
|
-
workspaceRoot,
|
|
472
|
-
{ promptPath, nodeContext: nodeContext ?? "", taskBody: taskBody ?? "", intermediatePath, resultPathRel: resultPath, subagent, instanceId },
|
|
473
|
-
{
|
|
474
|
-
model,
|
|
475
|
-
stderrBuffer: options.stderrBuffer,
|
|
476
|
-
force: options.force,
|
|
477
|
-
outputPrefix: options.outputPrefix,
|
|
478
|
-
prefixColor: options.prefixColor,
|
|
479
|
-
onToolCall: options.onToolCall,
|
|
480
|
-
flowName,
|
|
481
|
-
uuid,
|
|
482
|
-
execWorkspaceRoot,
|
|
483
|
-
},
|
|
484
|
-
);
|
|
485
|
-
}
|
|
486
|
-
emitEvent(workspaceRoot, flowName, uuid, {
|
|
487
|
-
event: "agent-invoke-done",
|
|
488
|
-
instanceId,
|
|
489
|
-
subagent: subagent ?? null,
|
|
490
|
-
modelCli: cli,
|
|
491
|
-
model: model ?? null,
|
|
492
|
-
});
|
|
493
|
-
} catch (err) {
|
|
494
|
-
const payload = {
|
|
495
|
-
event: "agent-invoke-failed",
|
|
496
|
-
instanceId,
|
|
497
|
-
subagent: subagent ?? null,
|
|
498
|
-
modelCli: cli,
|
|
499
|
-
model: model ?? null,
|
|
500
|
-
error: err && err.message ? String(err.message) : String(err),
|
|
501
|
-
};
|
|
502
|
-
if (err && err.cursorStderrTail) payload.cursorStderrTail = err.cursorStderrTail;
|
|
503
|
-
emitEvent(workspaceRoot, flowName, uuid, payload);
|
|
504
|
-
throw err;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* @param {number | undefined} [opts.elapsedMs] 本节点执行耗时(毫秒),写入 result.elapsedMs 供 UI 展示
|
|
510
|
-
*/
|
|
511
|
-
export function runPostProcess(workspaceRoot, flowName, uuid, instanceId, execId, opts = {}) {
|
|
512
|
-
const args = [workspaceRoot, flowName, uuid, instanceId, String(execId)];
|
|
513
|
-
const { elapsedMs } = opts;
|
|
514
|
-
if (elapsedMs != null && Number.isFinite(elapsedMs) && elapsedMs >= 0) {
|
|
515
|
-
args.push(String(Math.round(elapsedMs)));
|
|
516
|
-
}
|
|
517
|
-
const result = runNodeScript(workspaceRoot, "post-process-node.mjs", args, {
|
|
518
|
-
captureStdout: true,
|
|
519
|
-
});
|
|
520
|
-
parseJsonStdout(result);
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
export function ensureLocalNodeTerminalSuccess(workspaceRoot, flowName, uuid, instanceId, preOutput) {
|
|
524
|
-
if (!preOutput.definitionId || !LOCAL_ONLY_TERMINAL_SUCCESS_IDS.has(preOutput.definitionId)) return;
|
|
525
|
-
const payload = {
|
|
526
|
-
status: "success",
|
|
527
|
-
message: "已通过",
|
|
528
|
-
execId: preOutput.execId ?? 1,
|
|
529
|
-
};
|
|
530
|
-
const result = runNodeScript(
|
|
531
|
-
workspaceRoot,
|
|
532
|
-
"write-result.mjs",
|
|
533
|
-
[workspaceRoot, flowName, uuid, instanceId, "--json", JSON.stringify(payload)],
|
|
534
|
-
{ captureStdout: true },
|
|
535
|
-
);
|
|
536
|
-
if (result.status !== 0) {
|
|
537
|
-
log.warn(`[agentflow] ensureLocalNodeTerminalSuccess write-result failed for ${instanceId}: ${result.stdout || result.stderr || result.status}`);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 规范化 tool_nodejs 的 script 命令行,修复误写 `node "${workspaceRoot}/..."` 等导致的错误路径。
|
|
3
|
-
* 供 node-execute(内联执行)与 run-tool-nodejs(子进程)共用。
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 合并 bash 风格「'片段'/后续路径」拼接,得到真实文件系统路径。
|
|
8
|
-
* 同时处理中间嵌入的 `/'T'/` 形态(脚本模板里对变量加单引号,
|
|
9
|
-
* 但这些单引号对 spawn/非 shell 调用是字面量,需剥掉)。
|
|
10
|
-
*/
|
|
11
|
-
export function normalizeConcatenatedSingleQuotedPath(s) {
|
|
12
|
-
let out = String(s).trim();
|
|
13
|
-
for (let n = 0; n < 64; n++) {
|
|
14
|
-
// 开头:'x'/y 或 'x'.y → xy
|
|
15
|
-
let next = out.replace(/^'([^']*)'(\/|\.)/, "$1$2");
|
|
16
|
-
// 中间:/'x'/ 或 /'x'. → /x/ 或 /x. (保留前后分隔符,避免误吃尾随空格后的独立 '…' 参数)
|
|
17
|
-
next = next.replace(/(\/)'([^'\s]*)'(\/|\.)/g, "$1$2$3");
|
|
18
|
-
if (next === out) break;
|
|
19
|
-
out = next;
|
|
20
|
-
}
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 去掉首段外层双引号(含 `node "'/a'/.b" --flags`:首段结束后还有参数)。
|
|
26
|
-
*/
|
|
27
|
-
export function stripOuterDoubleQuotes(rest) {
|
|
28
|
-
const t = rest.trim();
|
|
29
|
-
if (!t.startsWith('"')) return t;
|
|
30
|
-
for (let i = 1; i < t.length; i++) {
|
|
31
|
-
if (t[i] === '"' && (i + 1 >= t.length || /\s/.test(t[i + 1]))) {
|
|
32
|
-
return (t.slice(1, i) + t.slice(i + 1)).trim();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (t.endsWith('"')) return t.slice(1, -1).trim();
|
|
36
|
-
return t.slice(1).trim();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 极简 shell 式分词:未加引号片段、单引号块、双引号块。
|
|
41
|
-
*/
|
|
42
|
-
export function parseShellLikeArgs(input) {
|
|
43
|
-
const args = [];
|
|
44
|
-
const str = String(input);
|
|
45
|
-
let i = 0;
|
|
46
|
-
const len = str.length;
|
|
47
|
-
while (i < len) {
|
|
48
|
-
while (i < len && /\s/.test(str[i])) i++;
|
|
49
|
-
if (i >= len) break;
|
|
50
|
-
let arg = "";
|
|
51
|
-
if (str[i] === "'") {
|
|
52
|
-
i++;
|
|
53
|
-
while (i < len && str[i] !== "'") arg += str[i++];
|
|
54
|
-
if (str[i] === "'") i++;
|
|
55
|
-
} else if (str[i] === '"') {
|
|
56
|
-
i++;
|
|
57
|
-
while (i < len && str[i] !== '"') {
|
|
58
|
-
if (str[i] === "\\" && i + 1 < len) arg += str[++i];
|
|
59
|
-
else arg += str[i];
|
|
60
|
-
i++;
|
|
61
|
-
}
|
|
62
|
-
if (str[i] === '"') i++;
|
|
63
|
-
} else {
|
|
64
|
-
while (i < len && !/\s/.test(str[i])) arg += str[i++];
|
|
65
|
-
}
|
|
66
|
-
args.push(arg);
|
|
67
|
-
}
|
|
68
|
-
return args;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @param {string} resolvedScript - 如 node "'/root'/.workspace/.../x.mjs" 或 node "${workspaceRoot}/..." 解析后的错误形态
|
|
73
|
-
* @returns {string}
|
|
74
|
-
*/
|
|
75
|
-
export function normalizeNodeToolCommandLine(resolvedScript) {
|
|
76
|
-
const t = String(resolvedScript).trim();
|
|
77
|
-
const m = t.match(/^node\s+/i);
|
|
78
|
-
if (!m) return resolvedScript;
|
|
79
|
-
let rest = t.slice(m[0].length).trim();
|
|
80
|
-
rest = stripOuterDoubleQuotes(rest);
|
|
81
|
-
rest = normalizeConcatenatedSingleQuotedPath(rest);
|
|
82
|
-
return `node ${rest}`;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @returns {{ argv: string[], commandLine: string }}
|
|
87
|
-
*/
|
|
88
|
-
export function nodeToolCommandToArgv(commandLine) {
|
|
89
|
-
const normalized = normalizeNodeToolCommandLine(commandLine);
|
|
90
|
-
const nodeLead = normalized.match(/^node\s+/i);
|
|
91
|
-
if (!nodeLead) {
|
|
92
|
-
return { argv: [], commandLine: normalized };
|
|
93
|
-
}
|
|
94
|
-
const rest = normalized.slice(nodeLead[0].length).trim();
|
|
95
|
-
const argv = parseShellLikeArgs(rest);
|
|
96
|
-
return { argv, commandLine: normalized };
|
|
97
|
-
}
|