mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
Feat : console log
This commit is contained in:
@@ -80,7 +80,7 @@ export function saveUsageStats({ provider, model, tokens, connectionId, apiKey,
|
||||
|
||||
if (inTokens === 0 && outTokens === 0) return;
|
||||
|
||||
const time = new Date().toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit" });
|
||||
const time = new Date().toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit" });
|
||||
const accountSuffix = connectionId ? ` | account=${connectionId.slice(0, 8)}...` : "";
|
||||
console.log(`${COLORS.green}[${time}] 📊 [${label}] ${provider.toUpperCase()} | in=${inTokens} | out=${outTokens}${accountSuffix}${COLORS.reset}`);
|
||||
|
||||
|
||||
@@ -57,6 +57,33 @@ export function fixInvalidId(parsed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function cleanUsagePayload(payload) {
|
||||
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
let cleaned = payload;
|
||||
|
||||
if ("usage" in cleaned) {
|
||||
if (cleaned.usage === null) {
|
||||
const { usage, ...payloadWithoutUsage } = cleaned;
|
||||
cleaned = payloadWithoutUsage;
|
||||
} else if (typeof cleaned.usage === "object" && cleaned.usage.perf_metrics === null) {
|
||||
const { perf_metrics, ...usageWithoutPerf } = cleaned.usage;
|
||||
cleaned = { ...cleaned, usage: usageWithoutPerf };
|
||||
}
|
||||
}
|
||||
|
||||
if (cleaned.response && typeof cleaned.response === "object" && !Array.isArray(cleaned.response)) {
|
||||
const cleanedResponse = cleanUsagePayload(cleaned.response);
|
||||
if (cleanedResponse !== cleaned.response) {
|
||||
cleaned = { ...cleaned, response: cleanedResponse };
|
||||
}
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
// Format output as SSE
|
||||
export function formatSSE(data, sourceFormat) {
|
||||
if (data === null || data === undefined) return "data: null\n\n";
|
||||
@@ -64,23 +91,16 @@ export function formatSSE(data, sourceFormat) {
|
||||
|
||||
// OpenAI Responses API format
|
||||
if (data && data.event && data.data) {
|
||||
return `event: ${data.event}\ndata: ${JSON.stringify(data.data)}\n\n`;
|
||||
const cleanedEventData = cleanUsagePayload(data.data);
|
||||
return `event: ${data.event}\ndata: ${JSON.stringify(cleanedEventData)}\n\n`;
|
||||
}
|
||||
|
||||
data = cleanUsagePayload(data);
|
||||
|
||||
// Claude format
|
||||
if (sourceFormat === FORMATS.CLAUDE && data && data.type) {
|
||||
if (data.usage && typeof data.usage === 'object' && data.usage.perf_metrics === null) {
|
||||
const { perf_metrics, ...usageWithoutPerf } = data.usage;
|
||||
data = { ...data, usage: usageWithoutPerf };
|
||||
}
|
||||
return `event: ${data.type}\ndata: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
// Remove null perf_metrics
|
||||
if (data?.usage && typeof data.usage === 'object' && data.usage.perf_metrics === null) {
|
||||
const { perf_metrics, ...usageWithoutPerf } = data.usage;
|
||||
data = { ...data, usage: usageWithoutPerf };
|
||||
}
|
||||
|
||||
return `data: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user