copilot-api-plus 1.0.45 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +25 -13
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -2875,7 +2875,7 @@ const truncateMessages = async (payload, model) => {
|
|
|
2875
2875
|
const tokenCount = await getTokenCount(payload, model);
|
|
2876
2876
|
const safeLimit = Math.floor(maxPromptTokens * .95);
|
|
2877
2877
|
if (tokenCount.input <= safeLimit) return payload;
|
|
2878
|
-
|
|
2878
|
+
console.log(`WARN Prompt tokens (${tokenCount.input}) exceed safe limit (${safeLimit}/${maxPromptTokens}). Auto-truncating context...`);
|
|
2879
2879
|
const groups = groupMessages(payload.messages);
|
|
2880
2880
|
const systemGroups = groups.filter((g) => g.isSystem);
|
|
2881
2881
|
const conversationGroups = groups.filter((g) => !g.isSystem);
|
|
@@ -2902,7 +2902,7 @@ const truncateMessages = async (payload, model) => {
|
|
|
2902
2902
|
if (newTokenCount.input <= safeLimit) {
|
|
2903
2903
|
if (dropCount > 0) {
|
|
2904
2904
|
const droppedMessages = conversationGroups.slice(0, dropCount).reduce((sum, g) => sum + g.messages.length, 0);
|
|
2905
|
-
|
|
2905
|
+
console.log(`Truncated ${droppedMessages} messages (${dropCount} groups). Tokens: ${tokenCount.input} -> ${newTokenCount.input} (limit: ${maxPromptTokens})`);
|
|
2906
2906
|
}
|
|
2907
2907
|
return truncatedPayload;
|
|
2908
2908
|
}
|
|
@@ -2954,10 +2954,14 @@ const createChatCompletions = async (payload) => {
|
|
|
2954
2954
|
endpoint: `${copilotBaseUrl(state)}/chat/completions`
|
|
2955
2955
|
});
|
|
2956
2956
|
const url = `${copilotBaseUrl(state)}/chat/completions`;
|
|
2957
|
+
const body = payload.stream ? {
|
|
2958
|
+
...payload,
|
|
2959
|
+
stream_options: { include_usage: true }
|
|
2960
|
+
} : payload;
|
|
2957
2961
|
const fetchOptions = {
|
|
2958
2962
|
method: "POST",
|
|
2959
2963
|
headers,
|
|
2960
|
-
body: JSON.stringify(
|
|
2964
|
+
body: JSON.stringify(body)
|
|
2961
2965
|
};
|
|
2962
2966
|
const maxRetries = 2;
|
|
2963
2967
|
let lastError;
|
|
@@ -3043,11 +3047,15 @@ async function handleCompletion$1(c) {
|
|
|
3043
3047
|
const sseChunk = chunk;
|
|
3044
3048
|
if (sseChunk.data && sseChunk.data !== "[DONE]") {
|
|
3045
3049
|
const parsed = JSON.parse(sseChunk.data);
|
|
3046
|
-
if (parsed.usage)
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3050
|
+
if (parsed.usage) {
|
|
3051
|
+
const usage = {
|
|
3052
|
+
inputTokens: parsed.usage.prompt_tokens ?? 0,
|
|
3053
|
+
outputTokens: parsed.usage.completion_tokens ?? 0,
|
|
3054
|
+
cacheReadTokens: parsed.usage.prompt_tokens_details?.cached_tokens
|
|
3055
|
+
};
|
|
3056
|
+
setTokenUsage(usage);
|
|
3057
|
+
console.log(`[${formatTokenUsage(usage)}]`);
|
|
3058
|
+
}
|
|
3051
3059
|
}
|
|
3052
3060
|
} catch {}
|
|
3053
3061
|
await stream.writeSSE(chunk);
|
|
@@ -3516,11 +3524,15 @@ async function handleCompletion(c) {
|
|
|
3516
3524
|
if (!rawEvent.data) continue;
|
|
3517
3525
|
const chunk = JSON.parse(rawEvent.data);
|
|
3518
3526
|
const events$1 = translateChunkToAnthropicEvents(chunk, streamState);
|
|
3519
|
-
if (chunk.usage)
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3527
|
+
if (chunk.usage) {
|
|
3528
|
+
const usage = {
|
|
3529
|
+
inputTokens: chunk.usage.prompt_tokens - (chunk.usage.prompt_tokens_details?.cached_tokens ?? 0),
|
|
3530
|
+
outputTokens: chunk.usage.completion_tokens,
|
|
3531
|
+
cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens
|
|
3532
|
+
};
|
|
3533
|
+
setTokenUsage(usage);
|
|
3534
|
+
console.log(`[${formatTokenUsage(usage)}]`);
|
|
3535
|
+
}
|
|
3524
3536
|
for (const event of events$1) await stream.writeSSE({
|
|
3525
3537
|
event: event.type,
|
|
3526
3538
|
data: JSON.stringify(event)
|