Skip to content

Commit 1d04f1d

Browse files
author
Zoo (VP)
committed
test(opencode-go): cover completePrompt max_tokens fallback and trim diff churn
The codecov patch partials were not the streaming header fallbacks (those arms are all exercised by the metadata-absent createMessage tests): they were lines 720-722, pre-existing conditionals that became patch lines because the completePrompt Anthropic restructure re-indented the untouched params object. - Send the session header on the Anthropic client's defaultHeaders instead of a per-request option in completePrompt (per-request headers still override it with taskId when streaming), so the completePrompt params block stays byte-identical to main and out of the diff. The supportsTemperature false arm there is unreachable for Anthropic-format Go models by design. - Add a completePrompt test for the includeMaxTokens-on / modelMaxTokens-unset max_tokens fallback, mirroring the streaming test of the same name, and assert the defaultHeaders session header. Patch coverage of opencode-go.ts vs base is now 100% with no untaken branch arms on added lines.
1 parent f76b38a commit 1d04f1d

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

‎src/api/providers/__tests__/opencode-go.spec.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,35 @@ describe("OpencodeGoHandler", () => {
637637
expect(callArgs.max_tokens).toBe(2048)
638638
})
639639

640-
it("completePrompt sends the per-instance session ID to the Anthropic endpoint", async () => {
640+
it("completePrompt falls back to the model max_tokens when includeMaxTokens is on but modelMaxTokens is unset", async () => {
641641
mockAnthropicCreate.mockResolvedValue({
642642
content: [{ type: "text", text: "ok" }],
643643
})
644644

645-
const handler = new OpencodeGoHandler(anthropicOptions)
645+
const handler = new OpencodeGoHandler({ ...anthropicOptions, includeMaxTokens: true })
646646
await handler.completePrompt("ping")
647+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
648+
// qwen3.7-max maxTokens (65_536) clamped to 20% of 1M context => 65_536.
649+
expect(callArgs.max_tokens).toBe(65_536)
650+
})
647651

648-
expect(mockAnthropicCreate.mock.calls[0][1]?.headers).toMatchObject({
649-
"x-opencode-session": expect.any(String),
652+
it("completePrompt sends the per-instance session ID via the Anthropic client default headers", async () => {
653+
mockAnthropicCreate.mockResolvedValue({
654+
content: [{ type: "text", text: "ok" }],
650655
})
656+
657+
const handler = new OpencodeGoHandler(anthropicOptions)
658+
await handler.completePrompt("ping")
659+
660+
// completePrompt passes no per-request options; the session header
661+
// is a default header on the Anthropic client (streaming overrides
662+
// it with taskId when available).
663+
expect(mockAnthropicCreate.mock.calls[0][1]).toBeUndefined()
664+
expect(Anthropic).toHaveBeenCalledWith(
665+
expect.objectContaining({
666+
defaultHeaders: expect.objectContaining({ "x-opencode-session": expect.any(String) }),
667+
}),
668+
)
651669
})
652670

653671
it("completePrompt rethrows non-Error values unchanged from the Anthropic path", async () => {

‎src/api/providers/opencode-go.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
126126
defaultHeaders: {
127127
...DEFAULT_HEADERS,
128128
...(options.openAiHeaders || {}),
129+
// completePrompt has no taskId metadata, so the per-handler
130+
// session id provides operator-side session affinity here;
131+
// streaming requests override it with taskId when available.
132+
"x-opencode-session": this.sessionId,
129133
},
130134
})
131135
}
@@ -708,23 +712,20 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
708712

709713
if (format === "anthropic") {
710714
try {
711-
const message = await this.anthropicClient.messages.create(
712-
{
713-
model: modelId,
714-
// Honour the same includeMaxTokens/modelMaxTokens override
715-
// logic as the streaming path so non-streaming completions
716-
// respect the user's max-output slider instead of always
717-
// falling back to the model default.
718-
max_tokens:
719-
this.options.includeMaxTokens === true
720-
? this.options.modelMaxTokens || maxTokens || 16_384
721-
: (maxTokens ?? 16_384),
722-
temperature: this.supportsTemperature(modelId) ? (temperature ?? 1.0) : undefined,
723-
messages: [{ role: "user", content: prompt }],
724-
stream: false,
725-
},
726-
{ headers: { "x-opencode-session": this.sessionId } },
727-
)
715+
const message = await this.anthropicClient.messages.create({
716+
model: modelId,
717+
// Honour the same includeMaxTokens/modelMaxTokens override
718+
// logic as the streaming path so non-streaming completions
719+
// respect the user's max-output slider instead of always
720+
// falling back to the model default.
721+
max_tokens:
722+
this.options.includeMaxTokens === true
723+
? this.options.modelMaxTokens || maxTokens || 16_384
724+
: (maxTokens ?? 16_384),
725+
temperature: this.supportsTemperature(modelId) ? (temperature ?? 1.0) : undefined,
726+
messages: [{ role: "user", content: prompt }],
727+
stream: false,
728+
})
728729

729730
const content = message.content.find(({ type }) => type === "text")
730731
return content?.type === "text" ? content.text : ""

0 commit comments

Comments
 (0)