Skip to content

Commit 115818c

Browse files
committed
fix: filter Anthropic reserved keywords from system prompts
Filter out Anthropic-specific headers like x-anthropic-billing-header and x-anthropic-billing from system prompts before sending to Copilot API. These keywords are internal to Anthropic's billing system and should not be forwarded to the Copilot backend, as they may cause unexpected behavior or errors. Fixes #174 PR: #175
1 parent 086c48b commit 115818c

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/routes/messages/non-stream-translation.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,42 @@ function translateAnthropicMessagesToOpenAI(
7171
return [...systemMessages, ...otherMessages]
7272
}
7373

74+
const ANTHROPIC_RESERVED_KEYWORDS = [
75+
"x-anthropic-billing-header",
76+
"x-anthropic-billing",
77+
]
78+
79+
function filterAnthropicReservedContent(text: string): string {
80+
let filtered = text
81+
for (const keyword of ANTHROPIC_RESERVED_KEYWORDS) {
82+
filtered = filtered
83+
.split("\n")
84+
.filter((line) => !line.includes(keyword))
85+
.join("\n")
86+
}
87+
return filtered
88+
}
89+
7490
function handleSystemPrompt(
7591
system: string | Array<AnthropicTextBlock> | undefined,
7692
): Array<Message> {
7793
if (!system) {
7894
return []
7995
}
8096

81-
if (typeof system === "string") {
82-
return [{ role: "system", content: system }]
83-
} else {
84-
const systemText = system.map((block) => block.text).join("\n\n")
85-
return [{ role: "system", content: systemText }]
97+
let systemText: string
98+
systemText =
99+
typeof system === "string"
100+
? system
101+
: system.map((block) => block.text).join("\n\n")
102+
103+
systemText = filterAnthropicReservedContent(systemText)
104+
105+
if (systemText.trim().length === 0) {
106+
return []
86107
}
108+
109+
return [{ role: "system", content: systemText }]
87110
}
88111

89112
function handleUserMessage(message: AnthropicUserMessage): Array<Message> {

0 commit comments

Comments
 (0)