mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-01-30 06:12:06 +00:00
fix token-speed plugin
This commit is contained in:
@@ -8,6 +8,7 @@ import { pathAliasPlugin } from "./esbuild-plugin-path-alias";
|
|||||||
const watch = process.argv.includes("--watch");
|
const watch = process.argv.includes("--watch");
|
||||||
|
|
||||||
// Get the absolute path to the src directory (ES module compatible)
|
// Get the absolute path to the src directory (ES module compatible)
|
||||||
|
// @ts-ignore
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
const baseUrl = path.resolve(__dirname, "..");
|
const baseUrl = path.resolve(__dirname, "..");
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export class TempFileOutputHandler implements OutputHandler {
|
|||||||
this.config = {
|
this.config = {
|
||||||
subdirectory: 'claude-code-router',
|
subdirectory: 'claude-code-router',
|
||||||
extension: 'json',
|
extension: 'json',
|
||||||
includeTimestamp: true,
|
includeTimestamp: false,
|
||||||
prefix: 'session',
|
prefix: 'session',
|
||||||
...config
|
...config
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -160,13 +160,16 @@ export const tokenSpeedPlugin: CCRPlugin = {
|
|||||||
|
|
||||||
// Add onRequest hook to capture actual request start time (before processing)
|
// Add onRequest hook to capture actual request start time (before processing)
|
||||||
fastify.addHook('onRequest', async (request) => {
|
fastify.addHook('onRequest', async (request) => {
|
||||||
|
const url = new URL(`http://127.0.0.1${request.url}`);
|
||||||
|
if (!url.pathname.endsWith("/v1/messages")) return;
|
||||||
(request as any).requestStartTime = performance.now();
|
(request as any).requestStartTime = performance.now();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add onSend hook to intercept both streaming and non-streaming responses
|
// Add onSend hook to intercept both streaming and non-streaming responses
|
||||||
fastify.addHook('onSend', async (request, _reply, payload) => {
|
fastify.addHook('onSend', async (request, _reply, payload) => {
|
||||||
|
const startTime = (request as any).requestStartTime;
|
||||||
|
if (!startTime) return;
|
||||||
const requestId = (request as any).id || Date.now().toString();
|
const requestId = (request as any).id || Date.now().toString();
|
||||||
const startTime = (request as any).requestStartTime || performance.now();
|
|
||||||
|
|
||||||
// Extract session ID from request body metadata
|
// Extract session ID from request body metadata
|
||||||
let sessionId: string | undefined;
|
let sessionId: string | undefined;
|
||||||
@@ -177,8 +180,8 @@ export const tokenSpeedPlugin: CCRPlugin = {
|
|||||||
sessionId = match ? match[1] : undefined;
|
sessionId = match ? match[1] : undefined;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Ignore errors extracting session ID
|
|
||||||
}
|
}
|
||||||
|
if (!sessionId) return;
|
||||||
|
|
||||||
// Get tokenizer for this specific request
|
// Get tokenizer for this specific request
|
||||||
const tokenizer = await getTokenizerForRequest(request);
|
const tokenizer = await getTokenizerForRequest(request);
|
||||||
|
|||||||
Reference in New Issue
Block a user