Add fine grained tool streaming for OpenRouter Anthropic (#9629)

This commit is contained in:
Matt Rubens 2025-11-26 18:02:16 -05:00 committed by GitHub
parent fb9c57e1d9
commit 339a869ea9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 11 deletions

View file

@ -194,6 +194,7 @@ describe("OpenRouterHandler", () => {
top_p: undefined,
transforms: ["middle-out"],
}),
{ headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } },
)
})
@ -218,7 +219,9 @@ describe("OpenRouterHandler", () => {
await handler.createMessage("test", []).next()
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ transforms: ["middle-out"] }))
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ transforms: ["middle-out"] }), {
headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" },
})
})
it("adds cache control for supported models", async () => {
@ -260,6 +263,7 @@ describe("OpenRouterHandler", () => {
}),
]),
}),
{ headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } },
)
})
@ -295,14 +299,16 @@ describe("OpenRouterHandler", () => {
expect(result).toBe("test completion")
expect(mockCreate).toHaveBeenCalledWith({
model: mockOptions.openRouterModelId,
max_tokens: 8192,
thinking: undefined,
temperature: 0,
messages: [{ role: "user", content: "test prompt" }],
stream: false,
})
expect(mockCreate).toHaveBeenCalledWith(
{
model: mockOptions.openRouterModelId,
max_tokens: 8192,
temperature: 0,
messages: [{ role: "user", content: "test prompt" }],
stream: false,
},
{ headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } },
)
})
it("handles API errors", async () => {

View file

@ -213,9 +213,14 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
...(metadata?.tool_choice && { tool_choice: metadata.tool_choice }),
}
// Add Anthropic beta header for fine-grained tool streaming when using Anthropic models
const requestOptions = modelId.startsWith("anthropic/")
? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }
: undefined
let stream
try {
stream = await this.client.chat.completions.create(completionParams)
stream = await this.client.chat.completions.create(completionParams, requestOptions)
} catch (error) {
throw handleOpenAIError(error, this.providerName)
}
@ -417,9 +422,14 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
...(reasoning && { reasoning }),
}
// Add Anthropic beta header for fine-grained tool streaming when using Anthropic models
const requestOptions = modelId.startsWith("anthropic/")
? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }
: undefined
let response
try {
response = await this.client.chat.completions.create(completionParams)
response = await this.client.chat.completions.create(completionParams, requestOptions)
} catch (error) {
throw handleOpenAIError(error, this.providerName)
}