Skip to content
This repository was archived by the owner on Nov 7, 2026. It is now read-only.

Commit b2a3926

Browse files
authored
fix(core): accept interleaved Copilot reasoning_opaque values (anomalyco#54207)
1 parent 3884062 commit b2a3926

4 files changed

Lines changed: 96 additions & 14 deletions

File tree

‎packages/core/src/github-copilot/chat/convert-to-openai-compatible-chat-messages.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ export function convertToOpenAICompatibleChatMessages(prompt: LanguageModelV3Pro
8282

8383
for (const part of content) {
8484
const partMetadata = getOpenAIMetadata(part)
85-
// Check for reasoningOpaque on any part (may be attached to text/tool-call)
85+
// reasoningOpaque may be attached to any part. Interleaved thinking yields one per
86+
// reasoning segment; like the Copilot VS Code client, replay the latest opaque value
87+
// with the concatenated reasoning text.
8688
const partOpaque = (part.providerOptions as { copilot?: { reasoningOpaque?: string } })?.copilot
8789
?.reasoningOpaque
88-
if (partOpaque && !reasoningOpaque) {
90+
if (partOpaque) {
8991
reasoningOpaque = partOpaque
9092
}
9193

@@ -95,7 +97,7 @@ export function convertToOpenAICompatibleChatMessages(prompt: LanguageModelV3Pro
9597
break
9698
}
9799
case "reasoning": {
98-
if (part.text) reasoningText = part.text
100+
if (part.text) reasoningText = (reasoningText ?? "") + part.text
99101
break
100102
}
101103
case "tool-call": {

‎packages/core/src/github-copilot/chat/openai-compatible-chat-language-model.ts‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,9 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
465465

466466
const delta = choice.delta
467467

468-
// Capture reasoning_opaque for Copilot multi-turn reasoning
469-
if (delta.reasoning_opaque) {
470-
if (reasoningOpaque != null) {
471-
throw new InvalidResponseDataError({
472-
data: delta,
473-
message:
474-
"Multiple reasoning_opaque values received in a single response. Only one thinking part per response is supported.",
475-
})
476-
}
477-
reasoningOpaque = delta.reasoning_opaque
478-
}
468+
// Interleaved thinking (Claude) sends a new reasoning_opaque before each tool call.
469+
// Keep the latest, matching the Copilot VS Code client, which replays only that one.
470+
if (delta.reasoning_opaque) reasoningOpaque = delta.reasoning_opaque
479471

480472
// enqueue reasoning before text deltas (Copilot uses reasoning_text):
481473
const reasoningContent = delta.reasoning_text

‎packages/core/test/github-copilot/convert-to-copilot-messages.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,48 @@ describe("reasoning (copilot-specific)", () => {
475475
},
476476
])
477477
})
478+
479+
test("should replay interleaved reasoning as concatenated text with the latest reasoning_opaque", () => {
480+
const result = convertToCopilotMessages([
481+
{
482+
role: "assistant",
483+
content: [
484+
{
485+
type: "reasoning",
486+
text: "Read the readme first.",
487+
providerOptions: { copilot: { reasoningOpaque: "opaque-first" } },
488+
},
489+
{
490+
type: "tool-call",
491+
toolCallId: "call_first",
492+
toolName: "read_file",
493+
input: { filePath: "/README.md" },
494+
providerOptions: { copilot: { reasoningOpaque: "opaque-first" } },
495+
},
496+
{
497+
type: "reasoning",
498+
text: "Then the manifest.",
499+
providerOptions: { copilot: { reasoningOpaque: "opaque-second" } },
500+
},
501+
{
502+
type: "tool-call",
503+
toolCallId: "call_second",
504+
toolName: "read_file",
505+
input: { filePath: "/package.json" },
506+
providerOptions: { copilot: { reasoningOpaque: "opaque-second" } },
507+
},
508+
],
509+
},
510+
])
511+
512+
expect(result).toMatchObject([
513+
{
514+
role: "assistant",
515+
reasoning_text: "Read the readme first.Then the manifest.",
516+
reasoning_opaque: "opaque-second",
517+
},
518+
])
519+
})
478520
})
479521

480522
describe("full conversation", () => {

‎packages/core/test/github-copilot/copilot-chat-model.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ const FIXTURES = {
7171
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{}","name":"read_file"},"id":"call_reasoning_only_2","index":1,"type":"function"}]}}],"created":1769917420,"id":"opaque-only","usage":{"completion_tokens":12,"prompt_tokens":123,"prompt_tokens_details":{"cached_tokens":0},"total_tokens":135,"reasoning_tokens":0},"model":"gemini-3-flash-preview"}`,
7272
`data: [DONE]`,
7373
],
74+
75+
// Interleaved thinking (Claude) sends a new reasoning_opaque before each tool call
76+
interleavedReasoningOpaque: [
77+
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"Read the readme first."}}],"created":1791590400,"id":"interleaved","model":"claude-opus-5.5"}`,
78+
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{\\"filePath\\":\\"/README.md\\"}","name":"read_file"},"id":"call_first","index":0,"type":"function"}],"reasoning_opaque":"opaque-first"}}],"created":1791590400,"id":"interleaved","model":"claude-opus-5.5"}`,
79+
`data: {"choices":[{"index":0,"delta":{"content":null,"role":"assistant","reasoning_text":"Then the manifest."}}],"created":1791590401,"id":"interleaved","model":"claude-opus-5.5"}`,
80+
`data: {"choices":[{"finish_reason":"tool_calls","index":0,"delta":{"content":null,"role":"assistant","tool_calls":[{"function":{"arguments":"{\\"filePath\\":\\"/package.json\\"}","name":"read_file"},"id":"call_second","index":1,"type":"function"}],"reasoning_opaque":"opaque-second"}}],"created":1791590401,"id":"interleaved","usage":{"completion_tokens":42,"prompt_tokens":1200,"total_tokens":1242},"model":"claude-opus-5.5"}`,
81+
`data: [DONE]`,
82+
],
7483
}
7584

7685
function createMockFetch(chunks: string[]) {
@@ -482,6 +491,43 @@ describe("doStream", () => {
482491
})
483492
})
484493

494+
test("should pair each interleaved reasoning segment with its own reasoning_opaque", async () => {
495+
const mockFetch = createMockFetch(FIXTURES.interleavedReasoningOpaque)
496+
const model = createModel(mockFetch)
497+
498+
const { stream } = await model.doStream({
499+
prompt: TEST_PROMPT,
500+
includeRawChunks: false,
501+
})
502+
503+
const parts = await convertReadableStreamToArray(stream)
504+
505+
expect(parts.filter((p) => p.type === "error")).toEqual([])
506+
expect(
507+
parts.filter((p) => p.type === "reasoning-delta" || p.type === "reasoning-end" || p.type === "tool-call"),
508+
).toMatchObject([
509+
{ type: "reasoning-delta", delta: "Read the readme first." },
510+
{ type: "reasoning-end", providerMetadata: { copilot: { reasoningOpaque: "opaque-first" } } },
511+
{
512+
type: "tool-call",
513+
toolCallId: "call_first",
514+
providerMetadata: { copilot: { reasoningOpaque: "opaque-first" } },
515+
},
516+
{ type: "reasoning-delta", delta: "Then the manifest." },
517+
{ type: "reasoning-end", providerMetadata: { copilot: { reasoningOpaque: "opaque-second" } } },
518+
{
519+
type: "tool-call",
520+
toolCallId: "call_second",
521+
providerMetadata: { copilot: { reasoningOpaque: "opaque-second" } },
522+
},
523+
])
524+
expect(parts.at(-1)).toMatchObject({
525+
type: "finish",
526+
finishReason: { unified: "tool-calls" },
527+
providerMetadata: { copilot: { reasoningOpaque: "opaque-second" } },
528+
})
529+
})
530+
485531
test("should include response metadata from first chunk", async () => {
486532
const mockFetch = createMockFetch(FIXTURES.basicText)
487533
const model = createModel(mockFetch)

0 commit comments

Comments
 (0)