carlos-flow 2.12.7 → 2.12.8
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/AGENTS.md +5 -5
- package/CHANGELOG.md +663 -660
- package/README.en.md +29 -0
- package/README.md +39 -0
- package/README.zh.md +39 -0
- package/TRIGGER_GUIDE.md +2 -2
- package/bridge/mcp-bridge.js +203 -203
- package/check-all.sh +0 -0
- package/ghost.json +1 -7
- package/package.json +114 -114
- package/plugins/carlos-flow-trigger.js +215 -215
- package/scripts/check-no-dead-handler.mjs +87 -87
- 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/release.mjs +6 -6
- package/scripts/sync-readme.mjs +102 -102
- package/scripts/verify-activation-e2e.sh +55 -55
- package/src/card.js +296 -296
- package/src/commander.js +9 -9
- package/src/dispatch.js +750 -557
- package/src/index.js +1808 -1713
- package/src/protocol.js +95 -95
- package/src/state.js +930 -930
- package/src/trigger.js +909 -909
package/README.en.md
CHANGED
|
@@ -224,6 +224,35 @@ One package, six platforms. The init command generates the right config file for
|
|
|
224
224
|
|
|
225
225
|
---
|
|
226
226
|
|
|
227
|
+
## 📦 Version Consistency Convention (Dev Environment)
|
|
228
|
+
|
|
229
|
+
When you configure Carlos Flow for your AI, there are three conventions worth knowing:
|
|
230
|
+
|
|
231
|
+
**1. The version you call = the current disk version**
|
|
232
|
+
|
|
233
|
+
The Bridge (`bridge/mcp-bridge.js`) restarts a new node process on *every call*, loading `D:\cindy\publish-output\carlos-flow\src\index.js`. There is no "old code resident" problem — every `flow_*` instruction you send, from phone or PC, always executes the latest version on disk.
|
|
234
|
+
|
|
235
|
+
**2. Version confirmation after key commits**
|
|
236
|
+
|
|
237
|
+
Once a new functional commit is pushed to `main`, I will actively run a consistency check in the **next session**:
|
|
238
|
+
```bash
|
|
239
|
+
# Compare working tree vs main branch code differences
|
|
240
|
+
git status --short
|
|
241
|
+
|
|
242
|
+
# Confirm the specified files are fully consistent
|
|
243
|
+
git diff HEAD -- src/index.js src/dispatch.js
|
|
244
|
+
|
|
245
|
+
# Confirm the remote push is complete
|
|
246
|
+
git log --oneline origin/main -1
|
|
247
|
+
```
|
|
248
|
+
When the result shows "working tree == HEAD (no differences)" and "origin/main == HEAD", the version you call is confirmed green.
|
|
249
|
+
|
|
250
|
+
**3. Maintain version consistency**
|
|
251
|
+
|
|
252
|
+
If you modify `src/index.js` / `src/dispatch.js` but haven't pushed yet, the next call may still run the old version. **Solution**: run `git push origin main` after each key change, and the next call will automatically pull the newest code. The bridge doesn't care about "what's pushed" — it only looks at the disk.
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
227
256
|
## 🚀 Quick Start
|
|
228
257
|
|
|
229
258
|
### 1. Install and initialize
|
package/README.md
CHANGED
|
@@ -52,6 +52,45 @@ A:完全没问题。它的设计目标就是让不懂复杂技术的人,也
|
|
|
52
52
|
|
|
53
53
|
---
|
|
54
54
|
|
|
55
|
+
## 📦 版本一致性约定(开发环境篇)
|
|
56
|
+
|
|
57
|
+
给你的 AI 配置 Carlos 工作流时,有三条约定需要知道:
|
|
58
|
+
|
|
59
|
+
**1. 你调用的版本 = 当前磁盘版本**
|
|
60
|
+
|
|
61
|
+
桥接器(`bridge/mcp-bridge.js`)每次调用都会重新启动一个 node 进程,加载 `D:\cindy\publish-output\carlos-flow\src\index.js`。不存在“旧代码驻留”的问题,你通过手机或电脑发出的每一条 `flow_*` 指令,执行的永远是磁盘上的最新版本。
|
|
62
|
+
|
|
63
|
+
**2. 关键 Commit 后的版本确认**
|
|
64
|
+
|
|
65
|
+
一旦有新的功能性 commit 推送到 `main`,我会主动执行一次确认循环(在下次会话中):
|
|
66
|
+
```bash
|
|
67
|
+
# 对比工作区与 main 分支的代码差异
|
|
68
|
+
git status --short
|
|
69
|
+
|
|
70
|
+
# 确认指定文件已全部一致
|
|
71
|
+
git diff HEAD -- src/index.js src/dispatch.js
|
|
72
|
+
|
|
73
|
+
# 确认远端推送完成
|
|
74
|
+
git log --oneline origin/main -1
|
|
75
|
+
```
|
|
76
|
+
确认结果显示「工作区与 HEAD 无差异」且「origin/main 与 HEAD 一致」,你调用的版本才算点绿。
|
|
77
|
+
|
|
78
|
+
**3. 保持版本一致**
|
|
79
|
+
|
|
80
|
+
如果你在进一步开发中修改了 `src/index.js` / `src/dispatch.js` 但尚未 push,那么下一轮调用可能仍执行旧版本。**解决方案**:每次关键修改后先 `git push origin main`,你的下一次调用就会自动拉取最新代码。桥接器不参与“已推送”的选择,它只看磁盘。
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 📚 关联文档
|
|
85
|
+
|
|
86
|
+
| 文档 | 说明 |
|
|
87
|
+
|------|------|
|
|
88
|
+
| [TRIGGER_GUIDE.md](./TRIGGER_GUIDE.md) | 智能触发引擎深度指南(配置、评分、激活、反馈学习) |
|
|
89
|
+
| [CHANGELOG.md](./CHANGELOG.md) | 版本历史(遵循 Keep a Changelog + SemVer) |
|
|
90
|
+
| [MIGRATION.md](./MIGRATION.md) | 从 `carlos-flow-cindy` / `carlos-flow-opencode` 迁移 |
|
|
91
|
+
| [QA 报告](./docs/QA_REPORT_v2.5.5.md) | 第三方只读审计(13/13 变异体检测通过) |
|
|
92
|
+
| [变异报告](./docs/MUTATION_REPORT_v2.5.5.md) | 变异注入详情与结果 |
|
|
93
|
+
|
|
55
94
|
## 🚀 准备好开始了吗?
|
|
56
95
|
只需几行命令,为你的 AI 加上这层保护罩。
|
|
57
96
|
```bash
|
package/README.zh.md
CHANGED
|
@@ -52,6 +52,45 @@ A:完全没问题。它的设计目标就是让不懂复杂技术的人,也
|
|
|
52
52
|
|
|
53
53
|
---
|
|
54
54
|
|
|
55
|
+
## 📦 版本一致性约定(开发环境篇)
|
|
56
|
+
|
|
57
|
+
给你的 AI 配置 Carlos 工作流时,有三条约定需要知道:
|
|
58
|
+
|
|
59
|
+
**1. 你调用的版本 = 当前磁盘版本**
|
|
60
|
+
|
|
61
|
+
桥接器(`bridge/mcp-bridge.js`)每次调用都会重新启动一个 node 进程,加载 `D:\cindy\publish-output\carlos-flow\src\index.js`。不存在“旧代码驻留”的问题,你通过手机或电脑发出的每一条 `flow_*` 指令,执行的永远是磁盘上的最新版本。
|
|
62
|
+
|
|
63
|
+
**2. 关键 Commit 后的版本确认**
|
|
64
|
+
|
|
65
|
+
一旦有新的功能性 commit 推送到 `main`,我会主动执行一次确认循环(在下次会话中):
|
|
66
|
+
```bash
|
|
67
|
+
# 对比工作区与 main 分支的代码差异
|
|
68
|
+
git status --short
|
|
69
|
+
|
|
70
|
+
# 确认指定文件已全部一致
|
|
71
|
+
git diff HEAD -- src/index.js src/dispatch.js
|
|
72
|
+
|
|
73
|
+
# 确认远端推送完成
|
|
74
|
+
git log --oneline origin/main -1
|
|
75
|
+
```
|
|
76
|
+
确认结果显示「工作区与 HEAD 无差异」且「origin/main 与 HEAD 一致」,你调用的版本才算点绿。
|
|
77
|
+
|
|
78
|
+
**3. 保持版本一致**
|
|
79
|
+
|
|
80
|
+
如果你在进一步开发中修改了 `src/index.js` / `src/dispatch.js` 但尚未 push,那么下一轮调用可能仍执行旧版本。**解决方案**:每次关键修改后先 `git push origin main`,你的下一次调用就会自动拉取最新代码。桥接器不参与“已推送”的选择,它只看磁盘。
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 📚 关联文档
|
|
85
|
+
|
|
86
|
+
| 文档 | 说明 |
|
|
87
|
+
|------|------|
|
|
88
|
+
| [TRIGGER_GUIDE.md](./TRIGGER_GUIDE.md) | 智能触发引擎深度指南(配置、评分、激活、反馈学习) |
|
|
89
|
+
| [CHANGELOG.md](./CHANGELOG.md) | 版本历史(遵循 Keep a Changelog + SemVer) |
|
|
90
|
+
| [MIGRATION.md](./MIGRATION.md) | 从 `carlos-flow-cindy` / `carlos-flow-opencode` 迁移 |
|
|
91
|
+
| [QA 报告](./docs/QA_REPORT_v2.5.5.md) | 第三方只读审计(13/13 变异体检测通过) |
|
|
92
|
+
| [变异报告](./docs/MUTATION_REPORT_v2.5.5.md) | 变异注入详情与结果 |
|
|
93
|
+
|
|
55
94
|
## 🚀 准备好开始了吗?
|
|
56
95
|
只需几行命令,为你的 AI 加上这层保护罩。
|
|
57
96
|
```bash
|
package/TRIGGER_GUIDE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Carlos Flow 智能触发引擎使用指南
|
|
2
2
|
|
|
3
|
-
> **版本**: v2.12.
|
|
3
|
+
> **版本**: v2.12.8
|
|
4
4
|
> **适用**:`carlos-flow` 单包架构(2.6.0+)
|
|
5
5
|
> **代码位置**:`src/trigger.js`(零外部依赖的纯 JavaScript 实现)
|
|
6
6
|
|
|
@@ -779,5 +779,5 @@ MIT License
|
|
|
779
779
|
|
|
780
780
|
---
|
|
781
781
|
|
|
782
|
-
**版本**: v2.12.
|
|
782
|
+
**版本**: v2.12.8
|
|
783
783
|
**更新日期**: 2026-11-12
|
package/bridge/mcp-bridge.js
CHANGED
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
// Carlos MCP Bridge — 安全 TCP 桥接层
|
|
2
|
-
// 监听 127.0.0.1:19099,仅本地访问
|
|
3
|
-
// 用法: node mcp-bridge.js [端口]
|
|
4
|
-
// 环境变量: CARLOS_BRIDGE_TOKEN(必填,用于认证)
|
|
5
|
-
|
|
6
|
-
import { spawn } from "node:child_process";
|
|
7
|
-
import { createServer } from "node:net";
|
|
8
|
-
import { createHash } from "node:crypto";
|
|
9
|
-
import { fileURLToPath } from "node:url";
|
|
10
|
-
import path from "node:path";
|
|
11
|
-
|
|
12
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
-
const PORT = parseInt(process.argv[2] || process.env.BRIDGE_PORT || "19099", 10);
|
|
14
|
-
const TOKEN = process.env.CARLOS_BRIDGE_TOKEN;
|
|
15
|
-
// 路径自探测:环境变量优先(向后兼容),否则按 bridge/ 的位置自动定位项目根目录。
|
|
16
|
-
// 这样在 Cursor / VS Code / Windsurf 里只需填 bridge 脚本路径,无需再手写 ROOT 绝对路径。
|
|
17
|
-
const PROJECT_ROOT = path.resolve(__dirname, "..");
|
|
18
|
-
const MCP_ROOT = process.env.CARLOS_FLOW_MCP_ROOT
|
|
19
|
-
? path.resolve(process.env.CARLOS_FLOW_MCP_ROOT)
|
|
20
|
-
: PROJECT_ROOT;
|
|
21
|
-
const MCP_SERVER = path.resolve(MCP_ROOT, "src/index.js");
|
|
22
|
-
const MAX_REQUEST_SIZE = 1024 * 1024; // 1MB
|
|
23
|
-
const RATE_LIMIT = 10; // 每秒最多请求数
|
|
24
|
-
// 请求超时:必须大于服务端的最长单次执行时间。
|
|
25
|
-
// 服务端 flow_run_verify 的内部超时是 120s(真实 spawn verify.cmd),
|
|
26
|
-
// 其它工具都是毫秒级。
|
|
27
|
-
// 旧实现固定 5s —— 结果是任何耗时 >5s 的验证命令(如 `npm test`)必然超时失败,
|
|
28
|
-
// 用户只看到「Timeout」,而服务端那边命令其实跑得好好的。
|
|
29
|
-
// 现在默认 180s(> 服务端 120s 上限),可用 CARLOS_BRIDGE_TIMEOUT_MS 覆盖。
|
|
30
|
-
const REQUEST_TIMEOUT = Number(process.env.CARLOS_BRIDGE_TIMEOUT_MS) || 180000; // 180s
|
|
31
|
-
|
|
32
|
-
if (!TOKEN) {
|
|
33
|
-
process.stderr.write("[bridge] ❌ CARLOS_BRIDGE_TOKEN 未设置(fail-closed)\n");
|
|
34
|
-
process.exit(1);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 启动 MCP server 子进程
|
|
38
|
-
// windowsHide: true —— Windows 下隐藏子进程控制台窗口,避免 Bridge 重启时弹黑框。
|
|
39
|
-
const child = spawn(process.execPath, [MCP_SERVER], {
|
|
40
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
41
|
-
env: { ...process.env },
|
|
42
|
-
windowsHide: true,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
let childStdout = "";
|
|
46
|
-
const pendingClients = new Map(); // reqId -> { socket, timer }
|
|
47
|
-
|
|
48
|
-
child.stdout.on("data", (data) => {
|
|
49
|
-
childStdout += data.toString("utf8");
|
|
50
|
-
const lines = childStdout.split("\n");
|
|
51
|
-
childStdout = lines.pop() || "";
|
|
52
|
-
for (const line of lines) {
|
|
53
|
-
const t = line.trim();
|
|
54
|
-
if (!t) continue;
|
|
55
|
-
try {
|
|
56
|
-
const resp = JSON.parse(t);
|
|
57
|
-
if (resp.jsonrpc === "2.0" && resp.id !== undefined) {
|
|
58
|
-
const client = pendingClients.get(resp.id);
|
|
59
|
-
if (client) {
|
|
60
|
-
clearTimeout(client.timer);
|
|
61
|
-
pendingClients.delete(resp.id);
|
|
62
|
-
try { client.socket.write(t + "\n"); } catch {}
|
|
63
|
-
try { client.socket.end(); } catch {}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
} catch {}
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
child.stderr.on("data", (data) => {
|
|
71
|
-
process.stderr.write("[mcp] " + data.toString("utf8"));
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
child.on("error", (e) => {
|
|
75
|
-
process.stderr.write("[Carlos-Diagnosis] ❌ MCP server 子进程启动失败: " + e.message + "\n");
|
|
76
|
-
process.stderr.write(
|
|
77
|
-
"[Carlos-Suggestion] 💡 请确认 Node.js 可用(node -v),且 " +
|
|
78
|
-
MCP_SERVER +
|
|
79
|
-
" 存在;若路径被移动过,可设置环境变量 CARLOS_FLOW_MCP_ROOT 指向项目根目录。\n",
|
|
80
|
-
);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
child.on("exit", (code) => {
|
|
84
|
-
process.stderr.write(`[bridge] MCP server 退出 (code=${code})\n`);
|
|
85
|
-
for (const [, c] of pendingClients) {
|
|
86
|
-
clearTimeout(c.timer);
|
|
87
|
-
try { c.socket.end(); } catch {}
|
|
88
|
-
}
|
|
89
|
-
pendingClients.clear();
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// 速率限制(per-connection sliding window)
|
|
93
|
-
const rateLimitMap = new Map(); // socket -> [timestamps]
|
|
94
|
-
|
|
95
|
-
function isRateLimited(socket) {
|
|
96
|
-
const now = Date.now();
|
|
97
|
-
const key = socket.remoteAddress + ":" + socket.remotePort;
|
|
98
|
-
const timestamps = rateLimitMap.get(key) || [];
|
|
99
|
-
const windowStart = now - 1000;
|
|
100
|
-
const recent = timestamps.filter((t) => t > windowStart);
|
|
101
|
-
recent.push(now);
|
|
102
|
-
rateLimitMap.set(key, recent);
|
|
103
|
-
return recent.length > RATE_LIMIT;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// TCP 服务器
|
|
107
|
-
const server = createServer((socket) => {
|
|
108
|
-
let buf = "";
|
|
109
|
-
let size = 0;
|
|
110
|
-
|
|
111
|
-
socket.on("data", (data) => {
|
|
112
|
-
size += data.length;
|
|
113
|
-
if (size > MAX_REQUEST_SIZE) {
|
|
114
|
-
socket.write(JSON.stringify({ error: "413 Payload Too Large" }) + "\n");
|
|
115
|
-
socket.end();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (isRateLimited(socket)) {
|
|
120
|
-
socket.write(JSON.stringify({ error: "429 Too Many Requests" }) + "\n");
|
|
121
|
-
socket.end();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
buf += data.toString("utf8");
|
|
126
|
-
const lines = buf.split("\n");
|
|
127
|
-
buf = lines.pop() || "";
|
|
128
|
-
for (const line of lines) {
|
|
129
|
-
const t = line.trim();
|
|
130
|
-
if (!t) continue;
|
|
131
|
-
|
|
132
|
-
// 健康检查
|
|
133
|
-
if (t === "GET /health") {
|
|
134
|
-
// 必须回传**身份**,否则客户端无法判断“这个 bridge 是不是我该用的那个”。
|
|
135
|
-
//
|
|
136
|
-
// 背景:早先 /health 只回 {status:"ok"},客户端只要看到 ok 就复用 ——
|
|
137
|
-
// 于是端口上若跑着**另一个 ROOT / 另一个 CARLOS_FLOW_HOME** 的 bridge,
|
|
138
|
-
// 客户端会静默连过去:它启动时的 HOME 生效、调用方传的 CARLOS_FLOW_HOME
|
|
139
|
-
// 完全失效(状态写到别人的目录),且协议可能不匹配导致挂死。
|
|
140
|
-
// 现在回传 root / flow_home / pid,客户端逐一比对,不符就当“未运行”。
|
|
141
|
-
const health = {
|
|
142
|
-
status: "ok",
|
|
143
|
-
root: MCP_ROOT,
|
|
144
|
-
flow_home: process.env.CARLOS_FLOW_HOME || "",
|
|
145
|
-
pid: process.pid,
|
|
146
|
-
port: PORT,
|
|
147
|
-
};
|
|
148
|
-
socket.write(JSON.stringify(health) + "\n");
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
const req = JSON.parse(t);
|
|
154
|
-
|
|
155
|
-
// Token 认证
|
|
156
|
-
const reqToken = req.params?._token || req._token;
|
|
157
|
-
if (reqToken !== TOKEN) {
|
|
158
|
-
socket.write(JSON.stringify({ jsonrpc: "2.0", id: req.id, error: { code: -1, message: "Unauthorized" } }) + "\n");
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (req.jsonrpc === "2.0") {
|
|
163
|
-
const reqId = req.id;
|
|
164
|
-
const timer = setTimeout(() => {
|
|
165
|
-
if (pendingClients.has(reqId)) {
|
|
166
|
-
pendingClients.delete(reqId);
|
|
167
|
-
try { socket.write(JSON.stringify({ jsonrpc: "2.0", id: reqId, error: { code: -1, message: "Timeout" } }) + "\n"); } catch {}
|
|
168
|
-
try { socket.end(); } catch {}
|
|
169
|
-
}
|
|
170
|
-
}, REQUEST_TIMEOUT);
|
|
171
|
-
|
|
172
|
-
pendingClients.set(reqId, { socket, timer });
|
|
173
|
-
child.stdin.write(t + "\n");
|
|
174
|
-
}
|
|
175
|
-
} catch {}
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
socket.on("error", () => {});
|
|
180
|
-
socket.on("close", () => {
|
|
181
|
-
for (const [reqId, c] of pendingClients) {
|
|
182
|
-
if (c.socket === socket) {
|
|
183
|
-
clearTimeout(c.timer);
|
|
184
|
-
pendingClients.delete(reqId);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
server.on("error", (e) => {
|
|
191
|
-
process.stderr.write("[bridge] TCP 错误: " + e.message + "\n");
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
server.listen(PORT, "127.0.0.1", () => {
|
|
195
|
-
process.stderr.write(`[bridge] Carlos MCP Bridge listening on 127.0.0.1:${PORT}\n`);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// 优雅退出
|
|
199
|
-
process.on("SIGTERM", () => {
|
|
200
|
-
child.stdin.end();
|
|
201
|
-
child.kill("SIGTERM");
|
|
202
|
-
server.close();
|
|
203
|
-
process.exit(0);
|
|
1
|
+
// Carlos MCP Bridge — 安全 TCP 桥接层
|
|
2
|
+
// 监听 127.0.0.1:19099,仅本地访问
|
|
3
|
+
// 用法: node mcp-bridge.js [端口]
|
|
4
|
+
// 环境变量: CARLOS_BRIDGE_TOKEN(必填,用于认证)
|
|
5
|
+
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { createServer } from "node:net";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
|
|
12
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const PORT = parseInt(process.argv[2] || process.env.BRIDGE_PORT || "19099", 10);
|
|
14
|
+
const TOKEN = process.env.CARLOS_BRIDGE_TOKEN;
|
|
15
|
+
// 路径自探测:环境变量优先(向后兼容),否则按 bridge/ 的位置自动定位项目根目录。
|
|
16
|
+
// 这样在 Cursor / VS Code / Windsurf 里只需填 bridge 脚本路径,无需再手写 ROOT 绝对路径。
|
|
17
|
+
const PROJECT_ROOT = path.resolve(__dirname, "..");
|
|
18
|
+
const MCP_ROOT = process.env.CARLOS_FLOW_MCP_ROOT
|
|
19
|
+
? path.resolve(process.env.CARLOS_FLOW_MCP_ROOT)
|
|
20
|
+
: PROJECT_ROOT;
|
|
21
|
+
const MCP_SERVER = path.resolve(MCP_ROOT, "src/index.js");
|
|
22
|
+
const MAX_REQUEST_SIZE = 1024 * 1024; // 1MB
|
|
23
|
+
const RATE_LIMIT = 10; // 每秒最多请求数
|
|
24
|
+
// 请求超时:必须大于服务端的最长单次执行时间。
|
|
25
|
+
// 服务端 flow_run_verify 的内部超时是 120s(真实 spawn verify.cmd),
|
|
26
|
+
// 其它工具都是毫秒级。
|
|
27
|
+
// 旧实现固定 5s —— 结果是任何耗时 >5s 的验证命令(如 `npm test`)必然超时失败,
|
|
28
|
+
// 用户只看到「Timeout」,而服务端那边命令其实跑得好好的。
|
|
29
|
+
// 现在默认 180s(> 服务端 120s 上限),可用 CARLOS_BRIDGE_TIMEOUT_MS 覆盖。
|
|
30
|
+
const REQUEST_TIMEOUT = Number(process.env.CARLOS_BRIDGE_TIMEOUT_MS) || 180000; // 180s
|
|
31
|
+
|
|
32
|
+
if (!TOKEN) {
|
|
33
|
+
process.stderr.write("[bridge] ❌ CARLOS_BRIDGE_TOKEN 未设置(fail-closed)\n");
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 启动 MCP server 子进程
|
|
38
|
+
// windowsHide: true —— Windows 下隐藏子进程控制台窗口,避免 Bridge 重启时弹黑框。
|
|
39
|
+
const child = spawn(process.execPath, [MCP_SERVER], {
|
|
40
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
41
|
+
env: { ...process.env },
|
|
42
|
+
windowsHide: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
let childStdout = "";
|
|
46
|
+
const pendingClients = new Map(); // reqId -> { socket, timer }
|
|
47
|
+
|
|
48
|
+
child.stdout.on("data", (data) => {
|
|
49
|
+
childStdout += data.toString("utf8");
|
|
50
|
+
const lines = childStdout.split("\n");
|
|
51
|
+
childStdout = lines.pop() || "";
|
|
52
|
+
for (const line of lines) {
|
|
53
|
+
const t = line.trim();
|
|
54
|
+
if (!t) continue;
|
|
55
|
+
try {
|
|
56
|
+
const resp = JSON.parse(t);
|
|
57
|
+
if (resp.jsonrpc === "2.0" && resp.id !== undefined) {
|
|
58
|
+
const client = pendingClients.get(resp.id);
|
|
59
|
+
if (client) {
|
|
60
|
+
clearTimeout(client.timer);
|
|
61
|
+
pendingClients.delete(resp.id);
|
|
62
|
+
try { client.socket.write(t + "\n"); } catch {}
|
|
63
|
+
try { client.socket.end(); } catch {}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch {}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
child.stderr.on("data", (data) => {
|
|
71
|
+
process.stderr.write("[mcp] " + data.toString("utf8"));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
child.on("error", (e) => {
|
|
75
|
+
process.stderr.write("[Carlos-Diagnosis] ❌ MCP server 子进程启动失败: " + e.message + "\n");
|
|
76
|
+
process.stderr.write(
|
|
77
|
+
"[Carlos-Suggestion] 💡 请确认 Node.js 可用(node -v),且 " +
|
|
78
|
+
MCP_SERVER +
|
|
79
|
+
" 存在;若路径被移动过,可设置环境变量 CARLOS_FLOW_MCP_ROOT 指向项目根目录。\n",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
child.on("exit", (code) => {
|
|
84
|
+
process.stderr.write(`[bridge] MCP server 退出 (code=${code})\n`);
|
|
85
|
+
for (const [, c] of pendingClients) {
|
|
86
|
+
clearTimeout(c.timer);
|
|
87
|
+
try { c.socket.end(); } catch {}
|
|
88
|
+
}
|
|
89
|
+
pendingClients.clear();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// 速率限制(per-connection sliding window)
|
|
93
|
+
const rateLimitMap = new Map(); // socket -> [timestamps]
|
|
94
|
+
|
|
95
|
+
function isRateLimited(socket) {
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
const key = socket.remoteAddress + ":" + socket.remotePort;
|
|
98
|
+
const timestamps = rateLimitMap.get(key) || [];
|
|
99
|
+
const windowStart = now - 1000;
|
|
100
|
+
const recent = timestamps.filter((t) => t > windowStart);
|
|
101
|
+
recent.push(now);
|
|
102
|
+
rateLimitMap.set(key, recent);
|
|
103
|
+
return recent.length > RATE_LIMIT;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// TCP 服务器
|
|
107
|
+
const server = createServer((socket) => {
|
|
108
|
+
let buf = "";
|
|
109
|
+
let size = 0;
|
|
110
|
+
|
|
111
|
+
socket.on("data", (data) => {
|
|
112
|
+
size += data.length;
|
|
113
|
+
if (size > MAX_REQUEST_SIZE) {
|
|
114
|
+
socket.write(JSON.stringify({ error: "413 Payload Too Large" }) + "\n");
|
|
115
|
+
socket.end();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (isRateLimited(socket)) {
|
|
120
|
+
socket.write(JSON.stringify({ error: "429 Too Many Requests" }) + "\n");
|
|
121
|
+
socket.end();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
buf += data.toString("utf8");
|
|
126
|
+
const lines = buf.split("\n");
|
|
127
|
+
buf = lines.pop() || "";
|
|
128
|
+
for (const line of lines) {
|
|
129
|
+
const t = line.trim();
|
|
130
|
+
if (!t) continue;
|
|
131
|
+
|
|
132
|
+
// 健康检查
|
|
133
|
+
if (t === "GET /health") {
|
|
134
|
+
// 必须回传**身份**,否则客户端无法判断“这个 bridge 是不是我该用的那个”。
|
|
135
|
+
//
|
|
136
|
+
// 背景:早先 /health 只回 {status:"ok"},客户端只要看到 ok 就复用 ——
|
|
137
|
+
// 于是端口上若跑着**另一个 ROOT / 另一个 CARLOS_FLOW_HOME** 的 bridge,
|
|
138
|
+
// 客户端会静默连过去:它启动时的 HOME 生效、调用方传的 CARLOS_FLOW_HOME
|
|
139
|
+
// 完全失效(状态写到别人的目录),且协议可能不匹配导致挂死。
|
|
140
|
+
// 现在回传 root / flow_home / pid,客户端逐一比对,不符就当“未运行”。
|
|
141
|
+
const health = {
|
|
142
|
+
status: "ok",
|
|
143
|
+
root: MCP_ROOT,
|
|
144
|
+
flow_home: process.env.CARLOS_FLOW_HOME || "",
|
|
145
|
+
pid: process.pid,
|
|
146
|
+
port: PORT,
|
|
147
|
+
};
|
|
148
|
+
socket.write(JSON.stringify(health) + "\n");
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const req = JSON.parse(t);
|
|
154
|
+
|
|
155
|
+
// Token 认证
|
|
156
|
+
const reqToken = req.params?._token || req._token;
|
|
157
|
+
if (reqToken !== TOKEN) {
|
|
158
|
+
socket.write(JSON.stringify({ jsonrpc: "2.0", id: req.id, error: { code: -1, message: "Unauthorized" } }) + "\n");
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (req.jsonrpc === "2.0") {
|
|
163
|
+
const reqId = req.id;
|
|
164
|
+
const timer = setTimeout(() => {
|
|
165
|
+
if (pendingClients.has(reqId)) {
|
|
166
|
+
pendingClients.delete(reqId);
|
|
167
|
+
try { socket.write(JSON.stringify({ jsonrpc: "2.0", id: reqId, error: { code: -1, message: "Timeout" } }) + "\n"); } catch {}
|
|
168
|
+
try { socket.end(); } catch {}
|
|
169
|
+
}
|
|
170
|
+
}, REQUEST_TIMEOUT);
|
|
171
|
+
|
|
172
|
+
pendingClients.set(reqId, { socket, timer });
|
|
173
|
+
child.stdin.write(t + "\n");
|
|
174
|
+
}
|
|
175
|
+
} catch {}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
socket.on("error", () => {});
|
|
180
|
+
socket.on("close", () => {
|
|
181
|
+
for (const [reqId, c] of pendingClients) {
|
|
182
|
+
if (c.socket === socket) {
|
|
183
|
+
clearTimeout(c.timer);
|
|
184
|
+
pendingClients.delete(reqId);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
server.on("error", (e) => {
|
|
191
|
+
process.stderr.write("[bridge] TCP 错误: " + e.message + "\n");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
server.listen(PORT, "127.0.0.1", () => {
|
|
195
|
+
process.stderr.write(`[bridge] Carlos MCP Bridge listening on 127.0.0.1:${PORT}\n`);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// 优雅退出
|
|
199
|
+
process.on("SIGTERM", () => {
|
|
200
|
+
child.stdin.end();
|
|
201
|
+
child.kill("SIGTERM");
|
|
202
|
+
server.close();
|
|
203
|
+
process.exit(0);
|
|
204
204
|
});
|
package/check-all.sh
CHANGED
|
File without changes
|
package/ghost.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"name": "Carlos 工作流",
|
|
6
6
|
"description": "机械门禁 MCP:留痕/派发/裁定/审计/快照/免检/还原。自动管理 Bridge 后台常驻,首次调用 ~3-5s,后续 ~1s。",
|
|
7
7
|
"whenToUse": "需要走 Carlos 工作流门禁时使用:留痕/派发/裁定/审计/快照/免检/还原",
|
|
8
|
-
"version": "2.12.
|
|
8
|
+
"version": "2.12.8",
|
|
9
9
|
"kind": "mcp",
|
|
10
10
|
"entry": "client/carlos.js",
|
|
11
11
|
"command": "门禁",
|
|
@@ -589,12 +589,6 @@
|
|
|
589
589
|
"message"
|
|
590
590
|
]
|
|
591
591
|
}
|
|
592
|
-
},
|
|
593
|
-
{
|
|
594
|
-
"description": "获取可用场景包清单"
|
|
595
|
-
},
|
|
596
|
-
{
|
|
597
|
-
"description": "执行场景包任务"
|
|
598
592
|
}
|
|
599
593
|
]
|
|
600
594
|
}
|