mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-22 08:15:53 +00:00
Squashed merge of: 1. fix/mcp-tsc-errors — 修复上游 MCP 重构后的 tsc 错误和测试失败 2. feat/pipe-mute-disconnect — Pipe IPC 逻辑断开、/lang 命令、mute 状态机 3. feat/stub-recovery-all — 实现全部 stub 恢复 (task 001-012) 4. feat/kairos-activation — KAIROS 激活解除阻塞 + 工具实现 5. codex/openclaw-autonomy-pr — 自治权限系统、运行记录、managed flows Additional: 6. daemon/job 命令层级化重构 (subcommand 架构) 7. 跨平台后台引擎抽象 (detached/tmux engines) 8. 修复 src/ 中 43 个预存在的 TypeScript 类型错误 9. 修复 langfuse isolated test mock 完整性 10. 修复 CodeRabbit 审查的 Critical/Major/Minor 问题 11. remote-control-server logger 抽象 (测试 stderr 静默化) 12. /simplify 审查修复 (代码复用、质量、效率)
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import { log, error as logError } from "../logger";
|
|
import type { Context } from "hono";
|
|
import type { SessionEvent } from "./event-bus";
|
|
import { getEventBus } from "./event-bus";
|
|
|
|
export interface SSEWriter {
|
|
send(event: SessionEvent): void;
|
|
close(): void;
|
|
}
|
|
|
|
export function createSSEWriter(c: Context): SSEWriter {
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
const encoder = new TextEncoder();
|
|
c.req.raw.signal.addEventListener("abort", () => {
|
|
controller.close();
|
|
});
|
|
|
|
// Store encoder and controller for later use
|
|
(c as any)._sseEncoder = encoder;
|
|
(c as any)._sseController = controller;
|
|
},
|
|
});
|
|
|
|
return {
|
|
send(event: SessionEvent) {
|
|
const encoder = (c as any)._sseEncoder as TextEncoder;
|
|
const controller = (c as any)._sseController as ReadableStreamDefaultController;
|
|
if (!encoder || !controller) return;
|
|
const data = JSON.stringify({
|
|
type: event.type,
|
|
payload: event.payload,
|
|
direction: event.direction,
|
|
seqNum: event.seqNum,
|
|
});
|
|
const msg = `id: ${event.seqNum}\nevent: message\ndata: ${data}\n\n`;
|
|
controller.enqueue(encoder.encode(msg));
|
|
},
|
|
close() {
|
|
const controller = (c as any)._sseController as ReadableStreamDefaultController;
|
|
controller?.close();
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Create SSE response stream for a session */
|
|
export function createSSEStream(c: Context, sessionId: string, fromSeqNum = 0) {
|
|
const bus = getEventBus(sessionId);
|
|
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
const encoder = new TextEncoder();
|
|
|
|
// Send historical events if reconnecting
|
|
if (fromSeqNum > 0) {
|
|
const missed = bus.getEventsSince(fromSeqNum);
|
|
for (const event of missed) {
|
|
const data = JSON.stringify({
|
|
type: event.type,
|
|
payload: event.payload,
|
|
direction: event.direction,
|
|
seqNum: event.seqNum,
|
|
});
|
|
controller.enqueue(encoder.encode(`id: ${event.seqNum}\nevent: message\ndata: ${data}\n\n`));
|
|
}
|
|
}
|
|
|
|
// Send initial keepalive
|
|
controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
|
|
// Subscribe to new events
|
|
const unsub = bus.subscribe((event) => {
|
|
const data = JSON.stringify({
|
|
type: event.type,
|
|
payload: event.payload,
|
|
direction: event.direction,
|
|
seqNum: event.seqNum,
|
|
});
|
|
try {
|
|
log(`[RC-DEBUG] SSE -> web: sessionId=${sessionId} type=${event.type} dir=${event.direction} seq=${event.seqNum}`);
|
|
controller.enqueue(encoder.encode(`id: ${event.seqNum}\nevent: message\ndata: ${data}\n\n`));
|
|
} catch {
|
|
unsub();
|
|
}
|
|
});
|
|
|
|
// Keepalive interval
|
|
const keepalive = setInterval(() => {
|
|
try {
|
|
controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
} catch {
|
|
clearInterval(keepalive);
|
|
unsub();
|
|
}
|
|
}, 15000);
|
|
|
|
// Cleanup on abort
|
|
c.req.raw.signal.addEventListener("abort", () => {
|
|
unsub();
|
|
clearInterval(keepalive);
|
|
try {
|
|
controller.close();
|
|
} catch {
|
|
// already closed
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache",
|
|
Connection: "keep-alive",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
});
|
|
}
|