From 915495553a5ce6906b393c6e0a6969a4cfface8e Mon Sep 17 00:00:00 2001 From: musistudio Date: Tue, 19 Aug 2025 22:33:59 +0800 Subject: [PATCH] fix some bugs --- src/index.ts | 53 +++++++++++++++++++++++++++------------- src/utils/codeCommand.ts | 19 ++++++-------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/index.ts b/src/index.ts index 16fc1b9..80c4b21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import createWriteStream from "pino-rotating-file-stream"; import { HOME_DIR } from "./constants"; import { configureLogging } from "./utils/log"; import { sessionUsageCache } from "./utils/cache"; +import Stream from "node:stream"; async function initializeClaudeConfig() { const homeDir = homedir(); @@ -133,32 +134,50 @@ async function run(options: RunOptions = {}) { router(req, reply, config); } }); - server.addHook("onSend", async (req, reply, payload) => { + server.addHook("onSend", (req, reply, payload, done) => { if (req.sessionId && req.url.startsWith("/v1/messages")) { if (payload instanceof ReadableStream) { const [originalStream, clonedStream] = payload.tee(); - const reader1 = clonedStream.getReader(); - while (true) { - const { done, value } = await reader1.read(); - if (done) break; - // Process the value if needed - const dataStr = new TextDecoder().decode(value); - if (!dataStr.startsWith("event: message_delta")) { - continue; + const read = async (stream: ReadableStream) => { + const reader = stream.getReader(); + while (true) { + const { done, value } = await reader.read(); + if (done) break; + // Process the value if needed + const dataStr = new TextDecoder().decode(value); + if (!dataStr.startsWith("event: message_delta")) { + continue; + } + const str = dataStr.slice(27); + try { + const message = JSON.parse(str); + sessionUsageCache.put(req.sessionId, message.usage); + } catch {} } - const str = dataStr.slice(27); - try { - const message = JSON.parse(str); - sessionUsageCache.put(req.sessionId, message.usage); - } catch {} } - - return originalStream; + read(clonedStream); + done(originalStream) } else { + req.log.debug({payload}, 'onSend Hook') sessionUsageCache.put(req.sessionId, payload.usage); + if (payload instanceof Buffer || payload instanceof Response) { + done(null, payload); + } else if(typeof payload === "object") { + done(null, JSON.stringify(payload)); + } else { + done(null, payload); + } + } + } else { + if(payload instanceof Buffer || payload instanceof Response || payload === null || payload instanceof ReadableStream || payload instanceof Stream) { + done(null, payload); + } else if(typeof payload === "object") { + req.log.debug({payload}, 'onSend Hook') + done(null, JSON.stringify(payload)); + } else { + done(null, payload); } } - return payload; }); server.start(); } diff --git a/src/utils/codeCommand.ts b/src/utils/codeCommand.ts index d0562b8..9b2940c 100644 --- a/src/utils/codeCommand.ts +++ b/src/utils/codeCommand.ts @@ -16,18 +16,15 @@ export async function executeCodeCommand(args: string[] = []) { ANTHROPIC_BASE_URL: `http://127.0.0.1:${config.PORT || 3456}`, API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000), // Default to 10 minutes if not set }; - - const settingsFlag: Record = { - env, - }; + let settingsFlag: Record | undefined; if (config?.StatusLine?.enabled) { - settingsFlag.statusLine = { - type: "command", - command: "ccr statusline", - padding: 0, - }; - } - if (Object.keys(settingsFlag).length > 0 && process.platform !== 'win32') { + settingsFlag = { + statusLine: { + type: "command", + command: "ccr statusline", + padding: 0, + } + } args.push(`--settings=${JSON.stringify(settingsFlag)}`); }