copilot-api-plus 1.2.46 → 1.2.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 +59 -2
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -2760,6 +2760,63 @@ function getAnthropicToolUseBlocks(toolCalls) {
|
|
|
2760
2760
|
}));
|
|
2761
2761
|
}
|
|
2762
2762
|
//#endregion
|
|
2763
|
+
//#region src/routes/messages/strip-reminders.ts
|
|
2764
|
+
const REMINDER_RE = /<system-reminder>[\s\S]*?<\/system-reminder>/g;
|
|
2765
|
+
function stripFromText(text) {
|
|
2766
|
+
if (!text.includes("<system-reminder>")) return text;
|
|
2767
|
+
return text.replaceAll(REMINDER_RE, "").replaceAll(/\n{3,}/g, "\n\n").trim();
|
|
2768
|
+
}
|
|
2769
|
+
function stripFromBlock(block) {
|
|
2770
|
+
if (block.type !== "text") return block;
|
|
2771
|
+
const text = block.text;
|
|
2772
|
+
const stripped = stripFromText(text);
|
|
2773
|
+
if (stripped === text) return block;
|
|
2774
|
+
return {
|
|
2775
|
+
...block,
|
|
2776
|
+
text: stripped
|
|
2777
|
+
};
|
|
2778
|
+
}
|
|
2779
|
+
function stripFromMessage(message) {
|
|
2780
|
+
if (typeof message.content === "string") {
|
|
2781
|
+
const stripped = stripFromText(message.content);
|
|
2782
|
+
if (stripped === message.content) return message;
|
|
2783
|
+
return {
|
|
2784
|
+
...message,
|
|
2785
|
+
content: stripped
|
|
2786
|
+
};
|
|
2787
|
+
}
|
|
2788
|
+
let changed = false;
|
|
2789
|
+
const newBlocks = message.content.map((block) => {
|
|
2790
|
+
const next = stripFromBlock(block);
|
|
2791
|
+
if (next !== block) changed = true;
|
|
2792
|
+
return next;
|
|
2793
|
+
});
|
|
2794
|
+
if (!changed) return message;
|
|
2795
|
+
return {
|
|
2796
|
+
...message,
|
|
2797
|
+
content: newBlocks
|
|
2798
|
+
};
|
|
2799
|
+
}
|
|
2800
|
+
/**
|
|
2801
|
+
* Return a shallow-cloned payload with `<system-reminder>` blocks removed
|
|
2802
|
+
* from every message's text content. The input payload is NOT mutated.
|
|
2803
|
+
* If no reminders are present anywhere, the original payload is returned
|
|
2804
|
+
* unchanged (no allocation).
|
|
2805
|
+
*/
|
|
2806
|
+
function stripSystemReminders(payload) {
|
|
2807
|
+
let changed = false;
|
|
2808
|
+
const newMessages = payload.messages.map((m) => {
|
|
2809
|
+
const next = stripFromMessage(m);
|
|
2810
|
+
if (next !== m) changed = true;
|
|
2811
|
+
return next;
|
|
2812
|
+
});
|
|
2813
|
+
if (!changed) return payload;
|
|
2814
|
+
return {
|
|
2815
|
+
...payload,
|
|
2816
|
+
messages: newMessages
|
|
2817
|
+
};
|
|
2818
|
+
}
|
|
2819
|
+
//#endregion
|
|
2763
2820
|
//#region src/routes/messages/count-tokens-handler.ts
|
|
2764
2821
|
/**
|
|
2765
2822
|
* Handles token counting for Anthropic messages.
|
|
@@ -2772,7 +2829,7 @@ async function handleCountTokens(c) {
|
|
|
2772
2829
|
try {
|
|
2773
2830
|
const anthropicBeta = c.req.header("anthropic-beta");
|
|
2774
2831
|
const anthropicPayload = await c.req.json();
|
|
2775
|
-
const openAIPayload = translateToOpenAI(anthropicPayload);
|
|
2832
|
+
const openAIPayload = translateToOpenAI(stripSystemReminders(anthropicPayload));
|
|
2776
2833
|
const translatedModelName = translateModelName(anthropicPayload.model);
|
|
2777
2834
|
const selectedModel = findModel(translatedModelName) ?? findModel(anthropicPayload.model);
|
|
2778
2835
|
if (!selectedModel) {
|
|
@@ -3086,7 +3143,7 @@ async function handleCompletion(c) {
|
|
|
3086
3143
|
messages_count: anthropicPayload.messages.length,
|
|
3087
3144
|
max_tokens: anthropicPayload.max_tokens
|
|
3088
3145
|
});
|
|
3089
|
-
const openAIPayload = translateToOpenAI(anthropicPayload);
|
|
3146
|
+
const openAIPayload = translateToOpenAI(stripSystemReminders(anthropicPayload));
|
|
3090
3147
|
if (state.manualApprove) await awaitApproval();
|
|
3091
3148
|
const response = await createChatCompletions(openAIPayload);
|
|
3092
3149
|
if (isNonStreaming(response)) return c.json(translateToAnthropic(response));
|