u-foo 4.0.1 → 4.0.3
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 +12 -7
- package/README.zh-CN.md +5 -4
- package/bin/ucode.js +4 -0
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/dist/tui/darwin-x64/ufoo-tui +0 -0
- package/dist/tui/linux-arm64/ufoo-tui +0 -0
- package/dist/tui/linux-x64/ufoo-tui +0 -0
- package/package.json +1 -1
- package/src/agents/launch/launcher.js +30 -21
- package/src/agents/launch/launcherReadiness.js +35 -0
- package/src/agents/launch/ptyWrapper.js +5 -2
- package/src/agents/providers/nativeTransport.js +8 -1
- package/src/code/repl.js +2 -0
- package/src/runtime/daemon/mcpHttpServer.js +39 -10
- package/src/runtime/daemon/mcpStdioProxy.js +130 -10
- package/src/ui/rustUcodeHost.js +5 -0
package/README.md
CHANGED
|
@@ -132,16 +132,19 @@ loopback endpoint. Existing ufoo authentication fields and headers are retained.
|
|
|
132
132
|
Use `ufoo mcp configure codex --dry-run` to preview the change with credentials
|
|
133
133
|
redacted. Restart the Codex surface after configuring it.
|
|
134
134
|
|
|
135
|
-
For
|
|
136
|
-
stdio configuration:
|
|
135
|
+
For Cursor and any other host that has not been verified with direct HTTP,
|
|
136
|
+
keep the compatible stdio configuration:
|
|
137
137
|
|
|
138
138
|
```bash
|
|
139
139
|
ufoo mcp
|
|
140
140
|
```
|
|
141
141
|
|
|
142
142
|
This command is a stateless transport proxy into the same global server. It
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
accepts both newline-delimited JSON and standard `Content-Length` frames, and
|
|
144
|
+
answers in the framing that client used. Cursor can keep `command: ufoo` and
|
|
145
|
+
`args: ["mcp"]`. The proxy does not own Agent registrations or project state.
|
|
146
|
+
Inspect or restart the singleton listener with `ufoo mcp status` and
|
|
147
|
+
`ufoo mcp restart`.
|
|
145
148
|
|
|
146
149
|
## Runtime Model
|
|
147
150
|
|
|
@@ -332,9 +335,11 @@ select each layout directly. The one-child layout selects the most recently
|
|
|
332
335
|
activated live internal agent, or the first agent when none has been activated.
|
|
333
336
|
With no children, split modes remain selected while showing only the full-width
|
|
334
337
|
main pane; new children appear automatically when they become available.
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
+
When the input has focus and contains text, `Ctrl+C` first clears the selected
|
|
339
|
+
agent's draft without cancelling work or switching focus. `Esc` preserves the
|
|
340
|
+
draft. In either split layout, both keys first leave the footer if it has focus,
|
|
341
|
+
then return child focus to the main agent, then close the split. Running tasks
|
|
342
|
+
are preserved while moving through these layers. Both keys also
|
|
338
343
|
dismiss command completion and follow the main input's cancellation and project
|
|
339
344
|
return layers. At the main-only root, `Esc` keeps the application open and only
|
|
340
345
|
`Ctrl+C` exits. `Ctrl+Q` or `/window main` closes either split directly.
|
package/README.zh-CN.md
CHANGED
|
@@ -138,10 +138,11 @@ ufoo -g
|
|
|
138
138
|
ufoo mcp configure codex
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
重启 Codex surface
|
|
142
|
-
`ufoo mcp
|
|
143
|
-
|
|
144
|
-
`ufoo mcp
|
|
141
|
+
重启 Codex surface 后即可使用。Cursor 以及其他尚未验证直接 HTTP 的 host
|
|
142
|
+
继续配置 `ufoo mcp`。这个无状态 stdio 代理连接同一个 global server,同时接受
|
|
143
|
+
换行 JSON 和标准 `Content-Length` 帧,并按客户端使用的帧格式回写。Cursor 保持
|
|
144
|
+
`command: ufoo`、`args: ["mcp"]` 即可。它不拥有 Agent 注册或项目状态。用
|
|
145
|
+
`ufoo mcp status` 检查唯一 listener,用 `ufoo mcp restart` 只重启 listener。
|
|
145
146
|
|
|
146
147
|
## 运行模型
|
|
147
148
|
|
package/bin/ucode.js
CHANGED
|
@@ -16,6 +16,10 @@ const { resolveUcodeLaunch } = require("../src/code/launcher/ucode");
|
|
|
16
16
|
const { prepareUcodeBootstrap } = require("../src/code/launcher/ucodeBootstrap");
|
|
17
17
|
const { prepareUcodeRuntimeConfig } = require("../src/code/launcher/ucodeRuntimeConfig");
|
|
18
18
|
|
|
19
|
+
// CLI-wrapper startup text belongs to the parent agent. Native ucode loads
|
|
20
|
+
// its own system prompt; do not inject or pass the parent's text to its tools.
|
|
21
|
+
delete process.env.UFOO_STARTUP_BOOTSTRAP_TEXT;
|
|
22
|
+
|
|
19
23
|
function stripAppendSystemPromptArgs(args = [], targetFile = "") {
|
|
20
24
|
const normalizedTarget = String(targetFile || "").trim();
|
|
21
25
|
if (!Array.isArray(args) || args.length === 0) return { args: [], removed: false };
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -26,26 +26,13 @@ const {
|
|
|
26
26
|
} = require("../../runtime/terminal/adapters/hostAdapter");
|
|
27
27
|
const PtyWrapper = require("./ptyWrapper");
|
|
28
28
|
const ReadyDetector = require("./readyDetector");
|
|
29
|
+
const { setLauncherReady, isLauncherReady } = require("./launcherReadiness");
|
|
30
|
+
const STARTUP_WAIT_DIAGNOSTIC_MS = 10 * 60 * 1000;
|
|
29
31
|
const {
|
|
30
32
|
extractResumeConversationId,
|
|
31
33
|
persistConversationId,
|
|
32
34
|
} = require("./agyConversation");
|
|
33
35
|
|
|
34
|
-
function setLauncherReady(projectRoot, subscriber, ready) {
|
|
35
|
-
try {
|
|
36
|
-
const file = getUfooPaths(projectRoot).agentsFile;
|
|
37
|
-
if (!fs.existsSync(file)) return false;
|
|
38
|
-
return updateAgentsData(file, (data) => {
|
|
39
|
-
const meta = data.agents && data.agents[subscriber];
|
|
40
|
-
if (!meta || meta.status === "inactive") return false;
|
|
41
|
-
meta.launcher_ready = ready;
|
|
42
|
-
return true;
|
|
43
|
-
});
|
|
44
|
-
} catch {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
36
|
function connectSocket(sockPath) {
|
|
50
37
|
return new Promise((resolve, reject) => {
|
|
51
38
|
const client = net.createConnection(sockPath, () => resolve(client));
|
|
@@ -752,7 +739,7 @@ class AgentLauncher {
|
|
|
752
739
|
const sessionId = result.sessionId || (subscriberId.split(":")[1] || "");
|
|
753
740
|
const finalNickname = result.nickname || nickname || "";
|
|
754
741
|
|
|
755
|
-
if (["codex", "claude-code"].includes(this.agentType)) {
|
|
742
|
+
if (["codex", "claude-code", "ucode"].includes(this.agentType)) {
|
|
756
743
|
setLauncherReady(this.cwd, subscriberId, false);
|
|
757
744
|
}
|
|
758
745
|
|
|
@@ -804,7 +791,11 @@ class AgentLauncher {
|
|
|
804
791
|
}
|
|
805
792
|
|
|
806
793
|
// 7. 启动命令(PTY wrapper或直接spawn)
|
|
807
|
-
|
|
794
|
+
// Native ucode loads its bootstrap through the system-prompt file. A
|
|
795
|
+
// parent's wrapper bootstrap must never become user input in its TUI.
|
|
796
|
+
const startupBootstrapText = this.agentType === "ucode"
|
|
797
|
+
? ""
|
|
798
|
+
: String(process.env.UFOO_STARTUP_BOOTSTRAP_TEXT || "").trim();
|
|
808
799
|
|
|
809
800
|
// 7.1 PTY启用条件(显式开关 + 自动检测)
|
|
810
801
|
let shouldUsePty = false;
|
|
@@ -869,8 +860,17 @@ class AgentLauncher {
|
|
|
869
860
|
});
|
|
870
861
|
// Heal daemon inject-stamp desync (disk working, detector idle).
|
|
871
862
|
const agentsFilePath = getUfooPaths(this.cwd).agentsFile;
|
|
863
|
+
// Native ucode confirms readiness after the Rust client's authenticated
|
|
864
|
+
// handshake. Its bordered prompt is not a plain-text CLI ready marker.
|
|
865
|
+
const reconcileNativeReadiness = () => {
|
|
866
|
+
if (this.agentType === "ucode" && !readyDetector.ready
|
|
867
|
+
&& isLauncherReady(this.cwd, subscriberId)) {
|
|
868
|
+
readyDetector.confirmNativeReady();
|
|
869
|
+
}
|
|
870
|
+
};
|
|
872
871
|
const activityReconcileTimer = setInterval(() => {
|
|
873
872
|
try {
|
|
873
|
+
reconcileNativeReadiness();
|
|
874
874
|
reconcileDetectorOwnedActivity({
|
|
875
875
|
detector: launcherActivityDetector,
|
|
876
876
|
publisher: launcherPublisher,
|
|
@@ -948,15 +948,24 @@ class AgentLauncher {
|
|
|
948
948
|
|
|
949
949
|
// Elapsed time cannot prove that a host can accept input. Preserve
|
|
950
950
|
// queued work and report a slow startup instead of injecting blindly.
|
|
951
|
-
const
|
|
952
|
-
|
|
953
|
-
|
|
951
|
+
const startupWaitingTimer = setTimeout(() => {
|
|
952
|
+
reconcileNativeReadiness();
|
|
953
|
+
if (!readyDetector.ready) {
|
|
954
|
+
// The child TUI owns this terminal. Parent stderr would be written
|
|
955
|
+
// at its input cursor, so keep asynchronous diagnostics in IO logs.
|
|
956
|
+
wrapper.writeLogEntry({
|
|
957
|
+
ts: Date.now(), dir: "diagnostic", source: "launcher",
|
|
958
|
+
event: "startup.waiting", agent_type: this.agentType,
|
|
959
|
+
text: `Waiting for ${this.agentType} to become ready; queued work is retained.`,
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
}, STARTUP_WAIT_DIAGNOSTIC_MS);
|
|
954
963
|
|
|
955
964
|
// 设置退出回调(复用清理逻辑)
|
|
956
965
|
wrapper.onExit = async ({ exitCode, signal }) => {
|
|
957
966
|
await nativeMessages?.close();
|
|
958
967
|
// 清理 timers
|
|
959
|
-
clearTimeout(
|
|
968
|
+
clearTimeout(startupWaitingTimer);
|
|
960
969
|
clearInterval(activityReconcileTimer);
|
|
961
970
|
launcherActivityDetector.destroy();
|
|
962
971
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const { readJSON } = require("../../coordination/bus/utils");
|
|
5
|
+
const { getUfooPaths } = require("../../coordination/state/paths");
|
|
6
|
+
const { updateAgentsData } = require("../../coordination/state/agentsStore");
|
|
7
|
+
|
|
8
|
+
function setLauncherReady(projectRoot, subscriber, ready) {
|
|
9
|
+
if (!subscriber) return false;
|
|
10
|
+
try {
|
|
11
|
+
const file = getUfooPaths(projectRoot).agentsFile;
|
|
12
|
+
if (!fs.existsSync(file)) return false;
|
|
13
|
+
return updateAgentsData(file, (data) => {
|
|
14
|
+
const meta = data.agents && data.agents[subscriber];
|
|
15
|
+
if (!meta || meta.status === "inactive") return false;
|
|
16
|
+
meta.launcher_ready = ready;
|
|
17
|
+
return true;
|
|
18
|
+
});
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isLauncherReady(projectRoot, subscriber) {
|
|
25
|
+
if (!subscriber) return false;
|
|
26
|
+
try {
|
|
27
|
+
const data = readJSON(getUfooPaths(projectRoot).agentsFile, null);
|
|
28
|
+
const meta = data && data.agents && data.agents[subscriber];
|
|
29
|
+
return Boolean(meta && meta.status !== "inactive" && meta.launcher_ready === true);
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { setLauncherReady, isLauncherReady };
|
|
@@ -179,11 +179,14 @@ class PtyWrapper {
|
|
|
179
179
|
|
|
180
180
|
// 进程退出
|
|
181
181
|
this._ptyExitHandler = ({ exitCode, signal }) => {
|
|
182
|
+
// cleanup clears callbacks along with streams. Preserve the launcher's
|
|
183
|
+
// exit handler so natural child exits still clean up the wrapper host.
|
|
184
|
+
const onExit = this.onExit;
|
|
182
185
|
this.cleanup();
|
|
183
186
|
|
|
184
187
|
// 回调给launcher处理退出(不直接process.exit)
|
|
185
|
-
if (
|
|
186
|
-
|
|
188
|
+
if (onExit) {
|
|
189
|
+
onExit({ exitCode, signal });
|
|
187
190
|
}
|
|
188
191
|
};
|
|
189
192
|
this.pty.onExit(this._ptyExitHandler);
|
|
@@ -10,9 +10,16 @@ const { systemBlocksToAnthropicPayload } = require("./transports/systemBlocks");
|
|
|
10
10
|
const { buildResponsesPayload, isResponsesKeepalive, responseEventDelta, parseResponsesEvents, parseResponsesSsePayload } = require("./transports/responsesProtocol");
|
|
11
11
|
|
|
12
12
|
const DEFAULT_NATIVE_TIMEOUT_MS = 43200000;
|
|
13
|
+
const DEFAULT_NATIVE_CONNECT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
13
14
|
const DEFAULT_OPENAI_MAX_TOKENS = 32768;
|
|
14
15
|
const DEFAULT_ANTHROPIC_MAX_TOKENS = 32768;
|
|
15
|
-
|
|
16
|
+
// Undici otherwise imposes its own 10-second connection deadline, regardless
|
|
17
|
+
// of the model task budget. Explicit cancellation/task deadlines still apply.
|
|
18
|
+
const NATIVE_SSE_DISPATCHER_OPTIONS = Object.freeze({
|
|
19
|
+
connectTimeout: DEFAULT_NATIVE_CONNECT_TIMEOUT_MS,
|
|
20
|
+
headersTimeout: 0,
|
|
21
|
+
bodyTimeout: 0,
|
|
22
|
+
});
|
|
16
23
|
const ANTHROPIC_CACHE_CONTROL = Object.freeze({ type: "ephemeral" });
|
|
17
24
|
|
|
18
25
|
function nowMs() {
|
package/src/code/repl.js
CHANGED
|
@@ -26,6 +26,7 @@ const { summarizeSessionUsage, formatSessionUsageStatus } = require("./usageStor
|
|
|
26
26
|
const { listUcodeCommandsForHelp } = require("./commands");
|
|
27
27
|
const { applyUcodeModelCommand, suggestUcodeModels } = require("./modelCommand");
|
|
28
28
|
const { applyUcodePlanCommand } = require("./context/planMode");
|
|
29
|
+
const { setLauncherReady } = require("../agents/launch/launcherReadiness");
|
|
29
30
|
|
|
30
31
|
function printPrompt(stdout = process.stdout) {
|
|
31
32
|
stdout.write("> ");
|
|
@@ -358,6 +359,7 @@ async function runUcodeCoreAgent({
|
|
|
358
359
|
return runUcodeTui({
|
|
359
360
|
stdin,
|
|
360
361
|
stdout,
|
|
362
|
+
onReady: () => setLauncherReady(workspaceRoot, process.env.UFOO_SUBSCRIBER_ID || "", true),
|
|
361
363
|
runSingleCommand,
|
|
362
364
|
runNaturalLanguageTask,
|
|
363
365
|
runUbusCommand,
|
|
@@ -159,6 +159,8 @@ class GlobalMcpHttpServer {
|
|
|
159
159
|
this.activeRequests = new Map();
|
|
160
160
|
this.httpRequestCount = 0;
|
|
161
161
|
this.server = null;
|
|
162
|
+
this.startPromise = null;
|
|
163
|
+
this.stopPromise = null;
|
|
162
164
|
this.startedAt = "";
|
|
163
165
|
this.stopping = false;
|
|
164
166
|
}
|
|
@@ -358,33 +360,46 @@ class GlobalMcpHttpServer {
|
|
|
358
360
|
}
|
|
359
361
|
|
|
360
362
|
async start() {
|
|
363
|
+
if (this.stopPromise) await this.stopPromise;
|
|
361
364
|
if (this.server && this.server.listening) return this.getStatus();
|
|
365
|
+
if (this.startPromise) return this.startPromise;
|
|
366
|
+
const starting = this.startListener();
|
|
367
|
+
this.startPromise = starting;
|
|
368
|
+
try {
|
|
369
|
+
return await starting;
|
|
370
|
+
} finally {
|
|
371
|
+
this.startPromise = null;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async startListener() {
|
|
362
376
|
if (this.host !== "127.0.0.1" && this.host !== "::1") {
|
|
363
377
|
const err = new Error(`MCP HTTP host must be loopback, got: ${this.host}`);
|
|
364
378
|
err.code = "UFOO_MCP_NON_LOOPBACK_HOST";
|
|
365
379
|
throw err;
|
|
366
380
|
}
|
|
367
381
|
this.token = this.token || loadOrCreateMcpToken(this.tokenPath);
|
|
368
|
-
|
|
382
|
+
const server = http.createServer((req, res) => {
|
|
369
383
|
void this.handleHttpRequest(req, res);
|
|
370
384
|
});
|
|
371
|
-
this.server
|
|
385
|
+
this.server = server;
|
|
386
|
+
server.on("clientError", (_err, socket) => {
|
|
372
387
|
if (socket.writable) socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|
373
388
|
});
|
|
374
389
|
await new Promise((resolve, reject) => {
|
|
375
390
|
const onError = (err) => {
|
|
376
|
-
|
|
391
|
+
server.off("listening", onListening);
|
|
377
392
|
reject(err);
|
|
378
393
|
};
|
|
379
394
|
const onListening = () => {
|
|
380
|
-
|
|
395
|
+
server.off("error", onError);
|
|
381
396
|
resolve();
|
|
382
397
|
};
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
398
|
+
server.once("error", onError);
|
|
399
|
+
server.once("listening", onListening);
|
|
400
|
+
server.listen(this.port, this.host);
|
|
386
401
|
});
|
|
387
|
-
const address =
|
|
402
|
+
const address = server.address();
|
|
388
403
|
if (address && typeof address === "object") this.port = address.port;
|
|
389
404
|
this.startedAt = new Date().toISOString();
|
|
390
405
|
this.writeEndpointFile();
|
|
@@ -393,8 +408,23 @@ class GlobalMcpHttpServer {
|
|
|
393
408
|
}
|
|
394
409
|
|
|
395
410
|
async stop() {
|
|
396
|
-
if (this.
|
|
411
|
+
if (this.stopPromise) return this.stopPromise;
|
|
397
412
|
this.stopping = true;
|
|
413
|
+
const stopping = this.stopListener();
|
|
414
|
+
this.stopPromise = stopping;
|
|
415
|
+
try {
|
|
416
|
+
await stopping;
|
|
417
|
+
} finally {
|
|
418
|
+
this.stopPromise = null;
|
|
419
|
+
this.stopping = false;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async stopListener() {
|
|
424
|
+
// Shutdown must wait for the listener and endpoint publication to settle.
|
|
425
|
+
// Otherwise a delayed listening event can access a cleared server or leave
|
|
426
|
+
// a live listener and stale endpoint after shutdown has already completed.
|
|
427
|
+
if (this.startPromise) await this.startPromise.catch(() => {});
|
|
398
428
|
const sessions = Array.from(this.sessions.values());
|
|
399
429
|
this.sessions.clear();
|
|
400
430
|
await Promise.allSettled(sessions.map(async ({ transport, protocolServer }) => {
|
|
@@ -419,7 +449,6 @@ class GlobalMcpHttpServer {
|
|
|
419
449
|
} catch {
|
|
420
450
|
// Best effort; a PID mismatch is still visible in status diagnostics.
|
|
421
451
|
}
|
|
422
|
-
this.stopping = false;
|
|
423
452
|
}
|
|
424
453
|
}
|
|
425
454
|
|
|
@@ -22,11 +22,81 @@ const REPLAYABLE_TOOLS = new Set([
|
|
|
22
22
|
"list_agents", "ufoo_mcp_status", "poll_inbox", "wait_for_message",
|
|
23
23
|
]);
|
|
24
24
|
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "::1", "[::1]"]);
|
|
25
|
+
const CONTENT_LENGTH_HEADER = "content-length:";
|
|
26
|
+
const MAX_STDIO_HEADER_BYTES = 64 * 1024;
|
|
27
|
+
const MAX_STDIO_BODY_BYTES = 2 * 1024 * 1024;
|
|
25
28
|
|
|
26
29
|
function sleep(ms) {
|
|
27
30
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
function skipAsciiSpace(buffer) {
|
|
34
|
+
let index = 0;
|
|
35
|
+
while (index < buffer.length && (buffer[index] === 0x20 || buffer[index] === 0x09)) index += 1;
|
|
36
|
+
return index;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function classifyStdioChunk(buffer) {
|
|
40
|
+
const offset = skipAsciiSpace(buffer);
|
|
41
|
+
const sample = buffer.slice(offset, offset + CONTENT_LENGTH_HEADER.length).toString("latin1").toLowerCase();
|
|
42
|
+
if (!sample) return "empty";
|
|
43
|
+
if (sample.startsWith("{")) return "json";
|
|
44
|
+
if (sample.startsWith(CONTENT_LENGTH_HEADER)) return "header";
|
|
45
|
+
if (CONTENT_LENGTH_HEADER.startsWith(sample) && !sample.includes("\n")) return "prefix";
|
|
46
|
+
return "line";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function takeContentLengthFrame(buffer) {
|
|
50
|
+
let index = 0;
|
|
51
|
+
while (
|
|
52
|
+
index < buffer.length
|
|
53
|
+
&& (buffer[index] === 0x20 || buffer[index] === 0x09 || buffer[index] === 0x0d || buffer[index] === 0x0a)
|
|
54
|
+
) index += 1;
|
|
55
|
+
if (index === buffer.length) return { status: "empty" };
|
|
56
|
+
const slice = buffer.slice(index);
|
|
57
|
+
const crlf = slice.indexOf("\r\n\r\n");
|
|
58
|
+
const lf = slice.indexOf("\n\n");
|
|
59
|
+
let headerEnd = -1;
|
|
60
|
+
let separatorLength = 0;
|
|
61
|
+
if (crlf >= 0 && (lf < 0 || crlf <= lf)) {
|
|
62
|
+
headerEnd = crlf;
|
|
63
|
+
separatorLength = 4;
|
|
64
|
+
} else if (lf >= 0) {
|
|
65
|
+
headerEnd = lf;
|
|
66
|
+
separatorLength = 2;
|
|
67
|
+
}
|
|
68
|
+
if (headerEnd < 0) {
|
|
69
|
+
if (slice.length > MAX_STDIO_HEADER_BYTES) {
|
|
70
|
+
return { status: "invalid", rest: Buffer.alloc(0), message: "MCP header is too large" };
|
|
71
|
+
}
|
|
72
|
+
return { status: "need-more" };
|
|
73
|
+
}
|
|
74
|
+
const header = slice.slice(0, headerEnd).toString("utf8");
|
|
75
|
+
const match = header.match(/content-length:\s*(\d+)/i);
|
|
76
|
+
if (!match) {
|
|
77
|
+
return {
|
|
78
|
+
status: "invalid",
|
|
79
|
+
rest: slice.slice(headerEnd + separatorLength),
|
|
80
|
+
message: "MCP frame is missing Content-Length",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const length = Number(match[1]);
|
|
84
|
+
if (!Number.isSafeInteger(length) || length < 0 || length > MAX_STDIO_BODY_BYTES) {
|
|
85
|
+
return {
|
|
86
|
+
status: "invalid",
|
|
87
|
+
rest: slice.slice(headerEnd + separatorLength),
|
|
88
|
+
message: "MCP Content-Length is invalid",
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const bodyStart = headerEnd + separatorLength;
|
|
92
|
+
if (slice.length < bodyStart + length) return { status: "need-more" };
|
|
93
|
+
return {
|
|
94
|
+
status: "ok",
|
|
95
|
+
body: slice.slice(bodyStart, bodyStart + length).toString("utf8"),
|
|
96
|
+
rest: slice.slice(bodyStart + length),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
30
100
|
function validateEndpoint(raw = "") {
|
|
31
101
|
let parsed;
|
|
32
102
|
try {
|
|
@@ -85,12 +155,21 @@ class McpStdioProxy {
|
|
|
85
155
|
this.initializing = null;
|
|
86
156
|
this.activeRequests = new Map();
|
|
87
157
|
this.closed = false;
|
|
88
|
-
this.buffer =
|
|
158
|
+
this.buffer = Buffer.alloc(0);
|
|
159
|
+
this.inputFraming = "";
|
|
160
|
+
this.outputFraming = "ndjson";
|
|
89
161
|
}
|
|
90
162
|
|
|
91
163
|
writeMessage(message) {
|
|
92
164
|
if (!message || this.closed) return;
|
|
93
|
-
|
|
165
|
+
const json = JSON.stringify(message);
|
|
166
|
+
if (this.outputFraming === "content-length") {
|
|
167
|
+
const body = Buffer.from(json, "utf8");
|
|
168
|
+
this.output.write(`Content-Length: ${body.length}\r\n\r\n`);
|
|
169
|
+
this.output.write(body);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
this.output.write(`${json}\n`);
|
|
94
173
|
}
|
|
95
174
|
|
|
96
175
|
writeError(message) {
|
|
@@ -288,11 +367,10 @@ class McpStdioProxy {
|
|
|
288
367
|
}
|
|
289
368
|
}
|
|
290
369
|
|
|
291
|
-
|
|
292
|
-
if (!line.trim()) return;
|
|
370
|
+
handleJsonText(text) {
|
|
293
371
|
let request;
|
|
294
372
|
try {
|
|
295
|
-
request = JSON.parse(
|
|
373
|
+
request = JSON.parse(text);
|
|
296
374
|
} catch (err) {
|
|
297
375
|
this.writeMessage(createJsonRpcError(
|
|
298
376
|
null,
|
|
@@ -304,13 +382,55 @@ class McpStdioProxy {
|
|
|
304
382
|
void this.forward(request);
|
|
305
383
|
}
|
|
306
384
|
|
|
385
|
+
handleLine(line) {
|
|
386
|
+
if (!line.trim()) return;
|
|
387
|
+
this.handleJsonText(line);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
drainInput() {
|
|
391
|
+
while (this.buffer.length > 0) {
|
|
392
|
+
if (this.inputFraming === "content-length") {
|
|
393
|
+
const frame = takeContentLengthFrame(this.buffer);
|
|
394
|
+
if (frame.status === "need-more") return;
|
|
395
|
+
if (frame.status === "empty") {
|
|
396
|
+
this.buffer = Buffer.alloc(0);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
this.buffer = frame.rest;
|
|
400
|
+
this.outputFraming = "content-length";
|
|
401
|
+
if (frame.status === "invalid") {
|
|
402
|
+
this.writeMessage(createJsonRpcError(
|
|
403
|
+
null,
|
|
404
|
+
MCP_ERROR_CODES.PARSE_ERROR,
|
|
405
|
+
frame.message || "Parse error"
|
|
406
|
+
));
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
this.handleJsonText(frame.body);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const kind = classifyStdioChunk(this.buffer);
|
|
414
|
+
if (kind === "empty" || kind === "prefix") return;
|
|
415
|
+
if (kind === "header") {
|
|
416
|
+
this.inputFraming = "content-length";
|
|
417
|
+
this.outputFraming = "content-length";
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const newline = this.buffer.indexOf(0x0a);
|
|
422
|
+
if (newline < 0) return;
|
|
423
|
+
const line = this.buffer.slice(0, newline).toString("utf8").replace(/\r$/, "");
|
|
424
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
425
|
+
this.handleLine(line);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
307
429
|
start() {
|
|
308
|
-
this.input.setEncoding("utf8");
|
|
309
430
|
this.input.on("data", (chunk) => {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
this.
|
|
313
|
-
for (const line of lines) this.handleLine(line);
|
|
431
|
+
const next = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
432
|
+
this.buffer = this.buffer.length ? Buffer.concat([this.buffer, next]) : next;
|
|
433
|
+
this.drainInput();
|
|
314
434
|
});
|
|
315
435
|
this.input.on("end", () => {
|
|
316
436
|
void this.close();
|
package/src/ui/rustUcodeHost.js
CHANGED
|
@@ -208,6 +208,7 @@ async function runUcodeRust(props = {}) {
|
|
|
208
208
|
const backgroundTasks = new Map();
|
|
209
209
|
let backgroundSeq = 0;
|
|
210
210
|
let requestExit = false;
|
|
211
|
+
let clientReady = false;
|
|
211
212
|
const MARKDOWN_LOG_KINDS = new Set(["assistant", "error"]);
|
|
212
213
|
const surface = createAgentSurface({ maxEntries: 4000 });
|
|
213
214
|
const controller = createUcodeController({
|
|
@@ -761,6 +762,10 @@ async function runUcodeRust(props = {}) {
|
|
|
761
762
|
socket.write(encodeMessage(snap));
|
|
762
763
|
publishPendingInteraction();
|
|
763
764
|
publishPlan();
|
|
765
|
+
if (!clientReady) {
|
|
766
|
+
clientReady = true;
|
|
767
|
+
if (typeof props.onReady === "function") props.onReady();
|
|
768
|
+
}
|
|
764
769
|
},
|
|
765
770
|
async onCommand(cmd) {
|
|
766
771
|
const name = String(cmd.name || "");
|