carlos-flow 2.12.0 → 2.12.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/TRIGGER_GUIDE.md +783 -783
- package/bridge/mcp-bridge.js +203 -203
- package/check-all.sh +0 -0
- package/ghost.json +26 -1
- package/package.json +113 -113
- package/plugins/carlos-flow-trigger.js +215 -215
- package/scripts/check-no-dead-handler.mjs +2 -2
- package/scripts/check-templates.mjs +126 -126
- package/scripts/clean-readme-noise.mjs +53 -53
- package/scripts/install-hooks.mjs +166 -166
- package/scripts/lang-audit.mjs +172 -172
- package/scripts/mutation-check.mjs +483 -483
- package/scripts/sync-readme.mjs +102 -102
- package/scripts/verify-activation-e2e.sh +55 -55
- package/smoke-test.js +2 -2
- package/src/card.js +296 -288
- package/src/dispatch.js +557 -548
- package/src/index.js +1713 -1686
- package/src/profiles.js +17 -0
- package/src/protocol.js +95 -85
- package/src/state.js +930 -898
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
// carlos-flow-trigger — OpenCode 插件:把智能触发引擎真正接到消息事件上
|
|
2
|
-
//
|
|
3
|
-
// 为什么需要这个插件:
|
|
4
|
-
// Carlos 工作流的「智能触发引擎」(src/trigger.js)原本只接在**命令行客户端**
|
|
5
|
-
// (client/carlos.js)上 —— MCP server 根本不接触发引擎。
|
|
6
|
-
// 而在 OpenCode 里,Agent 走的是 MCP 工具路径(直接调 flow_* 工具),
|
|
7
|
-
// 于是 README 里那张「工作流会自己认出该上场的时候」的流程图在**实际使用路径上是不成立的**。
|
|
8
|
-
//
|
|
9
|
-
// 本插件做的事:
|
|
10
|
-
// 监听 OpenCode 的 `chat.message` 钩子(用户每发一条消息都会触发),
|
|
11
|
-
// 把消息原文交给 MCP 的 flow_trigger_check 工具 —— 由它做评分与决策,
|
|
12
|
-
// 达标时**实际完成激活**(写入用户原话 + 开占位卡 → 会话进入门禁态)。
|
|
13
|
-
//
|
|
14
|
-
// 评分与决策逻辑与命令行路径**共用同一份实现**(src/trigger.js),
|
|
15
|
-
// 所以两条路径的行为完全一致,不会出现「命令行能触发、MCP 不能」。
|
|
16
|
-
//
|
|
17
|
-
// 安装:
|
|
18
|
-
// 方式 A(本地文件):把本文件放到 OpenCode 的插件目录
|
|
19
|
-
// ~/.config/opencode/plugins/carlos-flow-trigger.js
|
|
20
|
-
// Windows 下则是 %APPDATA%\opencode\plugins\carlos-flow-trigger.js
|
|
21
|
-
// 项目级则放 .opencode/plugins/carlos-flow-trigger.js
|
|
22
|
-
// 方式 B(npm 包):在 opencode.json 里加
|
|
23
|
-
// { "plugin": ["carlos-flow/plugins/carlos-flow-trigger.js"] }
|
|
24
|
-
// 然后重启 OpenCode。
|
|
25
|
-
//
|
|
26
|
-
// 可选环境变量:
|
|
27
|
-
// CARLOS_FLOW_TRIGGER=off 关闭插件(临时停用自动触发)
|
|
28
|
-
// CARLOS_FLOW_TRIGGER_DEBUG=1 打印触发决策日志
|
|
29
|
-
//
|
|
30
|
-
// 说明:
|
|
31
|
-
// · 插件只做「判断 + 激活」,**不代替 Agent 决定改哪些文件**;
|
|
32
|
-
// 激活后仍需补齐 scope / acceptance / verify.cmd 才能派发(闸门不放松);
|
|
33
|
-
// · 任何异常都只记日志、绝不阻断用户消息(插件出错不应该让 OpenCode 挂掉)。
|
|
34
|
-
|
|
35
|
-
import { spawn } from "node:child_process";
|
|
36
|
-
import { existsSync } from "node:fs";
|
|
37
|
-
import { homedir } from "node:os";
|
|
38
|
-
import { join, resolve } from "node:path";
|
|
39
|
-
|
|
40
|
-
const DEBUG = process.env.CARLOS_FLOW_TRIGGER_DEBUG === "1";
|
|
41
|
-
const ENABLED = process.env.CARLOS_FLOW_TRIGGER !== "off";
|
|
42
|
-
|
|
43
|
-
// 找到 MCP server 的 src/index.js。
|
|
44
|
-
// 插件可能位于:
|
|
45
|
-
// 1) <pkg>/plugins/carlos-flow-trigger.js(npm 包内,方式 B)
|
|
46
|
-
// 2) ~/.config/opencode/plugins/carlos-flow-trigger.js(本地文件,方式 A)
|
|
47
|
-
// 3) <repo>/plugins/carlos-flow-trigger.js(源码 checkout)
|
|
48
|
-
//
|
|
49
|
-
// ⚠️ 包名是 **carlos-flow**(v2.6.0 起统一单包)。
|
|
50
|
-
// 旧版这里硬编码的是 carlos-flow-opencode —— 单包化之后那个目录根本不存在,
|
|
51
|
-
// 于是除「方式 B」(插件就在包内)外的所有安装方式都**找不到 MCP server**,
|
|
52
|
-
// 自动触发**静默失效**。
|
|
53
|
-
// 现在同时接受新包名与两个已弃用的旧包名(兼容老用户尚未迁移的 node_modules)。
|
|
54
|
-
const PKG_NAMES = ["carlos-flow", "carlos-flow-opencode", "carlos-flow-cindy"];
|
|
55
|
-
function findMcpServer() {
|
|
56
|
-
const here = new URL(".", import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1");
|
|
57
|
-
const dir = resolve(here);
|
|
58
|
-
const candidates = [
|
|
59
|
-
join(dir, "..", "src", "index.js"), // <pkg>/plugins/ → <pkg>/src/
|
|
60
|
-
];
|
|
61
|
-
for (const name of PKG_NAMES) {
|
|
62
|
-
candidates.push(
|
|
63
|
-
join(dir, "..", "..", "node_modules", name, "src", "index.js"),
|
|
64
|
-
join(dir, "..", "..", "..", "node_modules", name, "src", "index.js"),
|
|
65
|
-
join(homedir(), ".config", "opencode", "node_modules", name, "src", "index.js"),
|
|
66
|
-
join(homedir(), "AppData", "Roaming", "opencode", "node_modules", name, "src", "index.js"),
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
for (const p of candidates) {
|
|
70
|
-
try {
|
|
71
|
-
if (existsSync(p)) return p;
|
|
72
|
-
} catch {}
|
|
73
|
-
}
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 以 MCP 的 stdio 协议调一个工具(一次性拉起,用完即退)
|
|
78
|
-
async function callMcpTool(serverPath, toolName, args, timeoutMs = 15000) {
|
|
79
|
-
return new Promise((resolvePromise) => {
|
|
80
|
-
const child = spawn(process.execPath, [serverPath], {
|
|
81
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
82
|
-
windowsHide: true,
|
|
83
|
-
});
|
|
84
|
-
let buf = "";
|
|
85
|
-
let stderr = "";
|
|
86
|
-
const pending = new Map();
|
|
87
|
-
let id = 1;
|
|
88
|
-
|
|
89
|
-
const timer = setTimeout(() => {
|
|
90
|
-
try { child.kill(); } catch {}
|
|
91
|
-
resolvePromise({ ok: false, error: "timeout" });
|
|
92
|
-
}, timeoutMs);
|
|
93
|
-
|
|
94
|
-
child.stdout.on("data", (d) => {
|
|
95
|
-
buf += d.toString();
|
|
96
|
-
let i;
|
|
97
|
-
while ((i = buf.indexOf("\n")) >= 0) {
|
|
98
|
-
const line = buf.slice(0, i);
|
|
99
|
-
buf = buf.slice(i + 1);
|
|
100
|
-
if (!line.trim()) continue;
|
|
101
|
-
try {
|
|
102
|
-
const msg = JSON.parse(line);
|
|
103
|
-
if (msg.id && pending.has(msg.id)) {
|
|
104
|
-
pending.get(msg.id)(msg);
|
|
105
|
-
pending.delete(msg.id);
|
|
106
|
-
}
|
|
107
|
-
} catch {}
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
child.stderr.on("data", (d) => { stderr += d.toString(); });
|
|
111
|
-
|
|
112
|
-
const req = (method, params) => new Promise((r) => {
|
|
113
|
-
const rid = id++;
|
|
114
|
-
pending.set(rid, r);
|
|
115
|
-
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id: rid, method, params }) + "\n");
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
child.on("error", (e) => {
|
|
119
|
-
clearTimeout(timer);
|
|
120
|
-
try { child.kill(); } catch {}
|
|
121
|
-
resolvePromise({ ok: false, error: String(e && e.message ? e.message : e) });
|
|
122
|
-
});
|
|
123
|
-
child.on("exit", () => {
|
|
124
|
-
clearTimeout(timer);
|
|
125
|
-
resolvePromise({ ok: false, error: stderr.slice(-300) || "server exited" });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
(async () => {
|
|
129
|
-
try {
|
|
130
|
-
await req("initialize", {
|
|
131
|
-
protocolVersion: "2024-11-05",
|
|
132
|
-
capabilities: {},
|
|
133
|
-
clientInfo: { name: "carlos-flow-trigger", version: "1.0.0" },
|
|
134
|
-
});
|
|
135
|
-
const res = await req("tools/call", { name: toolName, arguments: args });
|
|
136
|
-
const text = res?.result?.content?.[0]?.text || "";
|
|
137
|
-
clearTimeout(timer);
|
|
138
|
-
try { child.kill(); } catch {}
|
|
139
|
-
if (res?.result?.isError) {
|
|
140
|
-
resolvePromise({ ok: false, error: String(text).slice(0, 300) });
|
|
141
|
-
} else {
|
|
142
|
-
try {
|
|
143
|
-
resolvePromise({ ok: true, data: JSON.parse(text) });
|
|
144
|
-
} catch {
|
|
145
|
-
resolvePromise({ ok: false, error: "bad json: " + String(text).slice(0, 200) });
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
} catch (e) {
|
|
149
|
-
clearTimeout(timer);
|
|
150
|
-
try { child.kill(); } catch {}
|
|
151
|
-
resolvePromise({ ok: false, error: String(e && e.message ? e.message : e) });
|
|
152
|
-
}
|
|
153
|
-
})();
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// 从 parts 里取出用户消息文本(只看 type==="text" 的部分)
|
|
158
|
-
function extractText(parts) {
|
|
159
|
-
if (!Array.isArray(parts)) return "";
|
|
160
|
-
return parts
|
|
161
|
-
.filter((p) => p && p.type === "text" && typeof p.text === "string")
|
|
162
|
-
.map((p) => p.text)
|
|
163
|
-
.join("\n")
|
|
164
|
-
.trim();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export const CarlosFlowTriggerPlugin = async ({ client, $, worktree, directory }) => {
|
|
168
|
-
const serverPath = findMcpServer();
|
|
169
|
-
if (!serverPath) {
|
|
170
|
-
console.error("[carlos-flow-trigger] 未找到 MCP server(src/index.js),自动触发未启用");
|
|
171
|
-
console.error("[carlos-flow-trigger] 请确认已全局安装:npm install -g carlos-flow");
|
|
172
|
-
}
|
|
173
|
-
if (DEBUG) {
|
|
174
|
-
console.error("[carlos-flow-trigger] server:", serverPath, "cwd:", worktree || directory);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return {
|
|
178
|
-
"chat.message": async (input, output) => {
|
|
179
|
-
if (!ENABLED) return;
|
|
180
|
-
if (!serverPath) return;
|
|
181
|
-
|
|
182
|
-
const message = extractText(output && output.parts);
|
|
183
|
-
if (!message) return;
|
|
184
|
-
|
|
185
|
-
const sessionId = input.sessionID || "default";
|
|
186
|
-
try {
|
|
187
|
-
const res = await callMcpTool(serverPath, "flow_trigger_check", {
|
|
188
|
-
sessionId,
|
|
189
|
-
message,
|
|
190
|
-
cwd: worktree || directory || process.cwd(),
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
if (!res.ok) {
|
|
194
|
-
if (DEBUG) console.error("[carlos-flow-trigger] 调用失败:", res.error);
|
|
195
|
-
return; // 插件出错绝不阻断用户消息
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const d = res.data || {};
|
|
199
|
-
if (DEBUG) {
|
|
200
|
-
console.error(
|
|
201
|
-
`[carlos-flow-trigger] decision=${d.decision} score=${d.score} activated=${d.activated}`
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
if (d.activated) {
|
|
205
|
-
console.error(
|
|
206
|
-
`[carlos-flow-trigger] 已自动激活 Carlos 工作流(评分 ${d.score})—— ` +
|
|
207
|
-
`请补齐 ${(d.pending_fields || []).join(" / ")} 后再派发`
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
} catch (e) {
|
|
211
|
-
if (DEBUG) console.error("[carlos-flow-trigger] 异常:", e && e.message ? e.message : e);
|
|
212
|
-
}
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
};
|
|
1
|
+
// carlos-flow-trigger — OpenCode 插件:把智能触发引擎真正接到消息事件上
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要这个插件:
|
|
4
|
+
// Carlos 工作流的「智能触发引擎」(src/trigger.js)原本只接在**命令行客户端**
|
|
5
|
+
// (client/carlos.js)上 —— MCP server 根本不接触发引擎。
|
|
6
|
+
// 而在 OpenCode 里,Agent 走的是 MCP 工具路径(直接调 flow_* 工具),
|
|
7
|
+
// 于是 README 里那张「工作流会自己认出该上场的时候」的流程图在**实际使用路径上是不成立的**。
|
|
8
|
+
//
|
|
9
|
+
// 本插件做的事:
|
|
10
|
+
// 监听 OpenCode 的 `chat.message` 钩子(用户每发一条消息都会触发),
|
|
11
|
+
// 把消息原文交给 MCP 的 flow_trigger_check 工具 —— 由它做评分与决策,
|
|
12
|
+
// 达标时**实际完成激活**(写入用户原话 + 开占位卡 → 会话进入门禁态)。
|
|
13
|
+
//
|
|
14
|
+
// 评分与决策逻辑与命令行路径**共用同一份实现**(src/trigger.js),
|
|
15
|
+
// 所以两条路径的行为完全一致,不会出现「命令行能触发、MCP 不能」。
|
|
16
|
+
//
|
|
17
|
+
// 安装:
|
|
18
|
+
// 方式 A(本地文件):把本文件放到 OpenCode 的插件目录
|
|
19
|
+
// ~/.config/opencode/plugins/carlos-flow-trigger.js
|
|
20
|
+
// Windows 下则是 %APPDATA%\opencode\plugins\carlos-flow-trigger.js
|
|
21
|
+
// 项目级则放 .opencode/plugins/carlos-flow-trigger.js
|
|
22
|
+
// 方式 B(npm 包):在 opencode.json 里加
|
|
23
|
+
// { "plugin": ["carlos-flow/plugins/carlos-flow-trigger.js"] }
|
|
24
|
+
// 然后重启 OpenCode。
|
|
25
|
+
//
|
|
26
|
+
// 可选环境变量:
|
|
27
|
+
// CARLOS_FLOW_TRIGGER=off 关闭插件(临时停用自动触发)
|
|
28
|
+
// CARLOS_FLOW_TRIGGER_DEBUG=1 打印触发决策日志
|
|
29
|
+
//
|
|
30
|
+
// 说明:
|
|
31
|
+
// · 插件只做「判断 + 激活」,**不代替 Agent 决定改哪些文件**;
|
|
32
|
+
// 激活后仍需补齐 scope / acceptance / verify.cmd 才能派发(闸门不放松);
|
|
33
|
+
// · 任何异常都只记日志、绝不阻断用户消息(插件出错不应该让 OpenCode 挂掉)。
|
|
34
|
+
|
|
35
|
+
import { spawn } from "node:child_process";
|
|
36
|
+
import { existsSync } from "node:fs";
|
|
37
|
+
import { homedir } from "node:os";
|
|
38
|
+
import { join, resolve } from "node:path";
|
|
39
|
+
|
|
40
|
+
const DEBUG = process.env.CARLOS_FLOW_TRIGGER_DEBUG === "1";
|
|
41
|
+
const ENABLED = process.env.CARLOS_FLOW_TRIGGER !== "off";
|
|
42
|
+
|
|
43
|
+
// 找到 MCP server 的 src/index.js。
|
|
44
|
+
// 插件可能位于:
|
|
45
|
+
// 1) <pkg>/plugins/carlos-flow-trigger.js(npm 包内,方式 B)
|
|
46
|
+
// 2) ~/.config/opencode/plugins/carlos-flow-trigger.js(本地文件,方式 A)
|
|
47
|
+
// 3) <repo>/plugins/carlos-flow-trigger.js(源码 checkout)
|
|
48
|
+
//
|
|
49
|
+
// ⚠️ 包名是 **carlos-flow**(v2.6.0 起统一单包)。
|
|
50
|
+
// 旧版这里硬编码的是 carlos-flow-opencode —— 单包化之后那个目录根本不存在,
|
|
51
|
+
// 于是除「方式 B」(插件就在包内)外的所有安装方式都**找不到 MCP server**,
|
|
52
|
+
// 自动触发**静默失效**。
|
|
53
|
+
// 现在同时接受新包名与两个已弃用的旧包名(兼容老用户尚未迁移的 node_modules)。
|
|
54
|
+
const PKG_NAMES = ["carlos-flow", "carlos-flow-opencode", "carlos-flow-cindy"];
|
|
55
|
+
function findMcpServer() {
|
|
56
|
+
const here = new URL(".", import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1");
|
|
57
|
+
const dir = resolve(here);
|
|
58
|
+
const candidates = [
|
|
59
|
+
join(dir, "..", "src", "index.js"), // <pkg>/plugins/ → <pkg>/src/
|
|
60
|
+
];
|
|
61
|
+
for (const name of PKG_NAMES) {
|
|
62
|
+
candidates.push(
|
|
63
|
+
join(dir, "..", "..", "node_modules", name, "src", "index.js"),
|
|
64
|
+
join(dir, "..", "..", "..", "node_modules", name, "src", "index.js"),
|
|
65
|
+
join(homedir(), ".config", "opencode", "node_modules", name, "src", "index.js"),
|
|
66
|
+
join(homedir(), "AppData", "Roaming", "opencode", "node_modules", name, "src", "index.js"),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
for (const p of candidates) {
|
|
70
|
+
try {
|
|
71
|
+
if (existsSync(p)) return p;
|
|
72
|
+
} catch {}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 以 MCP 的 stdio 协议调一个工具(一次性拉起,用完即退)
|
|
78
|
+
async function callMcpTool(serverPath, toolName, args, timeoutMs = 15000) {
|
|
79
|
+
return new Promise((resolvePromise) => {
|
|
80
|
+
const child = spawn(process.execPath, [serverPath], {
|
|
81
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
82
|
+
windowsHide: true,
|
|
83
|
+
});
|
|
84
|
+
let buf = "";
|
|
85
|
+
let stderr = "";
|
|
86
|
+
const pending = new Map();
|
|
87
|
+
let id = 1;
|
|
88
|
+
|
|
89
|
+
const timer = setTimeout(() => {
|
|
90
|
+
try { child.kill(); } catch {}
|
|
91
|
+
resolvePromise({ ok: false, error: "timeout" });
|
|
92
|
+
}, timeoutMs);
|
|
93
|
+
|
|
94
|
+
child.stdout.on("data", (d) => {
|
|
95
|
+
buf += d.toString();
|
|
96
|
+
let i;
|
|
97
|
+
while ((i = buf.indexOf("\n")) >= 0) {
|
|
98
|
+
const line = buf.slice(0, i);
|
|
99
|
+
buf = buf.slice(i + 1);
|
|
100
|
+
if (!line.trim()) continue;
|
|
101
|
+
try {
|
|
102
|
+
const msg = JSON.parse(line);
|
|
103
|
+
if (msg.id && pending.has(msg.id)) {
|
|
104
|
+
pending.get(msg.id)(msg);
|
|
105
|
+
pending.delete(msg.id);
|
|
106
|
+
}
|
|
107
|
+
} catch {}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
child.stderr.on("data", (d) => { stderr += d.toString(); });
|
|
111
|
+
|
|
112
|
+
const req = (method, params) => new Promise((r) => {
|
|
113
|
+
const rid = id++;
|
|
114
|
+
pending.set(rid, r);
|
|
115
|
+
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id: rid, method, params }) + "\n");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
child.on("error", (e) => {
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
try { child.kill(); } catch {}
|
|
121
|
+
resolvePromise({ ok: false, error: String(e && e.message ? e.message : e) });
|
|
122
|
+
});
|
|
123
|
+
child.on("exit", () => {
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
resolvePromise({ ok: false, error: stderr.slice(-300) || "server exited" });
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
(async () => {
|
|
129
|
+
try {
|
|
130
|
+
await req("initialize", {
|
|
131
|
+
protocolVersion: "2024-11-05",
|
|
132
|
+
capabilities: {},
|
|
133
|
+
clientInfo: { name: "carlos-flow-trigger", version: "1.0.0" },
|
|
134
|
+
});
|
|
135
|
+
const res = await req("tools/call", { name: toolName, arguments: args });
|
|
136
|
+
const text = res?.result?.content?.[0]?.text || "";
|
|
137
|
+
clearTimeout(timer);
|
|
138
|
+
try { child.kill(); } catch {}
|
|
139
|
+
if (res?.result?.isError) {
|
|
140
|
+
resolvePromise({ ok: false, error: String(text).slice(0, 300) });
|
|
141
|
+
} else {
|
|
142
|
+
try {
|
|
143
|
+
resolvePromise({ ok: true, data: JSON.parse(text) });
|
|
144
|
+
} catch {
|
|
145
|
+
resolvePromise({ ok: false, error: "bad json: " + String(text).slice(0, 200) });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} catch (e) {
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
try { child.kill(); } catch {}
|
|
151
|
+
resolvePromise({ ok: false, error: String(e && e.message ? e.message : e) });
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// 从 parts 里取出用户消息文本(只看 type==="text" 的部分)
|
|
158
|
+
function extractText(parts) {
|
|
159
|
+
if (!Array.isArray(parts)) return "";
|
|
160
|
+
return parts
|
|
161
|
+
.filter((p) => p && p.type === "text" && typeof p.text === "string")
|
|
162
|
+
.map((p) => p.text)
|
|
163
|
+
.join("\n")
|
|
164
|
+
.trim();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export const CarlosFlowTriggerPlugin = async ({ client, $, worktree, directory }) => {
|
|
168
|
+
const serverPath = findMcpServer();
|
|
169
|
+
if (!serverPath) {
|
|
170
|
+
console.error("[carlos-flow-trigger] 未找到 MCP server(src/index.js),自动触发未启用");
|
|
171
|
+
console.error("[carlos-flow-trigger] 请确认已全局安装:npm install -g carlos-flow");
|
|
172
|
+
}
|
|
173
|
+
if (DEBUG) {
|
|
174
|
+
console.error("[carlos-flow-trigger] server:", serverPath, "cwd:", worktree || directory);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
"chat.message": async (input, output) => {
|
|
179
|
+
if (!ENABLED) return;
|
|
180
|
+
if (!serverPath) return;
|
|
181
|
+
|
|
182
|
+
const message = extractText(output && output.parts);
|
|
183
|
+
if (!message) return;
|
|
184
|
+
|
|
185
|
+
const sessionId = input.sessionID || "default";
|
|
186
|
+
try {
|
|
187
|
+
const res = await callMcpTool(serverPath, "flow_trigger_check", {
|
|
188
|
+
sessionId,
|
|
189
|
+
message,
|
|
190
|
+
cwd: worktree || directory || process.cwd(),
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (!res.ok) {
|
|
194
|
+
if (DEBUG) console.error("[carlos-flow-trigger] 调用失败:", res.error);
|
|
195
|
+
return; // 插件出错绝不阻断用户消息
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const d = res.data || {};
|
|
199
|
+
if (DEBUG) {
|
|
200
|
+
console.error(
|
|
201
|
+
`[carlos-flow-trigger] decision=${d.decision} score=${d.score} activated=${d.activated}`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (d.activated) {
|
|
205
|
+
console.error(
|
|
206
|
+
`[carlos-flow-trigger] 已自动激活 Carlos 工作流(评分 ${d.score})—— ` +
|
|
207
|
+
`请补齐 ${(d.pending_fields || []).join(" / ")} 后再派发`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
} catch (e) {
|
|
211
|
+
if (DEBUG) console.error("[carlos-flow-trigger] 异常:", e && e.message ? e.message : e);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
};
|
|
@@ -71,8 +71,8 @@ try {
|
|
|
71
71
|
const hasValidation = /validateToolArgs\(/.test(src);
|
|
72
72
|
if (hasValidation) pass("CallTool handler 调用了 validateToolArgs(参数校验未丢失)");
|
|
73
73
|
else fail("CallTool handler 没有调用 validateToolArgs —— 参数校验可能已丢失");
|
|
74
|
-
if (declared.length ===
|
|
75
|
-
else fail(`TOOLS 声明了 ${declared.length} 个工具(期望
|
|
74
|
+
if (declared.length === 20) pass(`TOOLS 声明 ${declared.length} 个工具`);
|
|
75
|
+
else fail(`TOOLS 声明了 ${declared.length} 个工具(期望 20)`);
|
|
76
76
|
} catch (e) {
|
|
77
77
|
fail(`读取 src/index.js 失败:${e.message}`);
|
|
78
78
|
}
|