fix some bugs
This commit is contained in:
31
src/index.ts
31
src/index.ts
@@ -16,6 +16,7 @@ import createWriteStream from "pino-rotating-file-stream";
|
|||||||
import { HOME_DIR } from "./constants";
|
import { HOME_DIR } from "./constants";
|
||||||
import { configureLogging } from "./utils/log";
|
import { configureLogging } from "./utils/log";
|
||||||
import { sessionUsageCache } from "./utils/cache";
|
import { sessionUsageCache } from "./utils/cache";
|
||||||
|
import Stream from "node:stream";
|
||||||
|
|
||||||
async function initializeClaudeConfig() {
|
async function initializeClaudeConfig() {
|
||||||
const homeDir = homedir();
|
const homeDir = homedir();
|
||||||
@@ -133,13 +134,14 @@ async function run(options: RunOptions = {}) {
|
|||||||
router(req, reply, config);
|
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 (req.sessionId && req.url.startsWith("/v1/messages")) {
|
||||||
if (payload instanceof ReadableStream) {
|
if (payload instanceof ReadableStream) {
|
||||||
const [originalStream, clonedStream] = payload.tee();
|
const [originalStream, clonedStream] = payload.tee();
|
||||||
const reader1 = clonedStream.getReader();
|
const read = async (stream: ReadableStream) => {
|
||||||
|
const reader = stream.getReader();
|
||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader1.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
// Process the value if needed
|
// Process the value if needed
|
||||||
const dataStr = new TextDecoder().decode(value);
|
const dataStr = new TextDecoder().decode(value);
|
||||||
@@ -152,13 +154,30 @@ async function run(options: RunOptions = {}) {
|
|||||||
sessionUsageCache.put(req.sessionId, message.usage);
|
sessionUsageCache.put(req.sessionId, message.usage);
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return originalStream;
|
read(clonedStream);
|
||||||
|
done(originalStream)
|
||||||
} else {
|
} else {
|
||||||
|
req.log.debug({payload}, 'onSend Hook')
|
||||||
sessionUsageCache.put(req.sessionId, payload.usage);
|
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();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,18 +16,15 @@ export async function executeCodeCommand(args: string[] = []) {
|
|||||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${config.PORT || 3456}`,
|
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
|
API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000), // Default to 10 minutes if not set
|
||||||
};
|
};
|
||||||
|
let settingsFlag: Record<string, any> | undefined;
|
||||||
const settingsFlag: Record<string, any> = {
|
|
||||||
env,
|
|
||||||
};
|
|
||||||
if (config?.StatusLine?.enabled) {
|
if (config?.StatusLine?.enabled) {
|
||||||
settingsFlag.statusLine = {
|
settingsFlag = {
|
||||||
|
statusLine: {
|
||||||
type: "command",
|
type: "command",
|
||||||
command: "ccr statusline",
|
command: "ccr statusline",
|
||||||
padding: 0,
|
padding: 0,
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if (Object.keys(settingsFlag).length > 0 && process.platform !== 'win32') {
|
}
|
||||||
args.push(`--settings=${JSON.stringify(settingsFlag)}`);
|
args.push(`--settings=${JSON.stringify(settingsFlag)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user