GeezerVison is showing github.com — better interleaved thinking support (#5298) · avillagran/opencode@df64612
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .opencode/opencode.jsonc +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | "instructions": ["STYLE_GUIDE.md"], | 8 | 8 | "provider": { | 9 | 9 | "opencode": { | 10 | | - "options": { | 11 | | - // "baseURL": "http://localhost:8080", | 12 | | - }, | | 10 | + "options": {}, | 13 | 11 | }, | 14 | 12 | }, | 15 | 13 | "mcp": { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/agent.ts -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,7 +25,6 @@ import { Provider } from "../provider/provider" | 25 | 25 | import { Installation } from "@/installation" | 26 | 26 | import { MessageV2 } from "@/session/message-v2" | 27 | 27 | import { Config } from "@/config/config" | 28 | | -import { MCP } from "@/mcp" | 29 | 28 | import { Todo } from "@/session/todo" | 30 | 29 | import { z } from "zod" | 31 | 30 | import { LoadAPIKeyError } from "ai" | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/lsp/server.ts +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -211,7 +211,15 @@ export namespace LSPServer { | 211 | 211 |
| 212 | 212 | export const Biome: Info = { | 213 | 213 | id: "biome", | 214 | | - root: NearestRoot(["biome.json", "biome.jsonc", "package-lock.json", "bun.lockb", "bun.lock", "pnpm-lock.yaml", "yarn.lock"]), | | 214 | + root: NearestRoot([ | | 215 | + "biome.json", | | 216 | + "biome.jsonc", | | 217 | + "package-lock.json", | | 218 | + "bun.lockb", | | 219 | + "bun.lock", | | 220 | + "pnpm-lock.yaml", | | 221 | + "yarn.lock", | | 222 | + ]), | 215 | 223 | extensions: [ | 216 | 224 | ".ts", | 217 | 225 | ".tsx", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/models.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,16 @@ export namespace ModelsDev { | 17 | 17 | reasoning: z.boolean(), | 18 | 18 | temperature: z.boolean(), | 19 | 19 | tool_call: z.boolean(), | | 20 | + interleaved: z | | 21 | + .union([ | | 22 | + z.literal(true), | | 23 | + z | | 24 | + .object({ | | 25 | + field: z.enum(["reasoning_content", "reasoning_details"]), | | 26 | + }) | | 27 | + .strict(), | | 28 | + ]) | | 29 | + .optional(), | 20 | 30 | cost: z | 21 | 31 | .object({ | 22 | 32 | input: z.number(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/provider.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -349,6 +349,12 @@ export namespace Provider { | 349 | 349 | video: z.boolean(), | 350 | 350 | pdf: z.boolean(), | 351 | 351 | }), | | 352 | + interleaved: z.union([ | | 353 | + z.boolean(), | | 354 | + z.object({ | | 355 | + field: z.enum(["reasoning_content", "reasoning_details"]), | | 356 | + }), | | 357 | + ]), | 352 | 358 | }), | 353 | 359 | cost: z.object({ | 354 | 360 | input: z.number(), | @@ -450,6 +456,7 @@ export namespace Provider { | 450 | 456 | video: model.modalities?.output?.includes("video") ?? false, | 451 | 457 | pdf: model.modalities?.output?.includes("pdf") ?? false, | 452 | 458 | }, | | 459 | + interleaved: model.interleaved ?? false, | 453 | 460 | }, | 454 | 461 | } | 455 | 462 | } | @@ -567,6 +574,7 @@ export namespace Provider { | 567 | 574 | video: model.modalities?.output?.includes("video") ?? existingModel?.capabilities.output.video ?? false, | 568 | 575 | pdf: model.modalities?.output?.includes("pdf") ?? existingModel?.capabilities.output.pdf ?? false, | 569 | 576 | }, | | 577 | + interleaved: model.interleaved ?? false, | 570 | 578 | }, | 571 | 579 | cost: { | 572 | 580 | input: model?.cost?.input ?? existingModel?.cost?.input ?? 0, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/transform.ts +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -273,7 +273,23 @@ export namespace ProviderTransform { | 273 | 273 | return options | 274 | 274 | } | 275 | 275 |
| 276 | | - export function providerOptions(model: Provider.Model, options: { [x: string]: any }) { | | 276 | + export function providerOptions(model: Provider.Model, options: { [x: string]: any }, messages: ModelMessage[]) { | | 277 | + if (model.capabilities.interleaved && typeof model.capabilities.interleaved === "object") { | | 278 | + const cot = [] | | 279 | + const assistantMessages = messages.filter((msg) => msg.role === "assistant") | | 280 | + for (const msg of assistantMessages) { | | 281 | + for (const part of msg.content) { | | 282 | + if (typeof part === "string") { | | 283 | + continue | | 284 | + } | | 285 | + if (part.type === "reasoning") { | | 286 | + cot.push(part) | | 287 | + } | | 288 | + } | | 289 | + } | | 290 | + options[model.capabilities.interleaved.field] = cot | | 291 | + } | | 292 | + | 277 | 293 | switch (model.api.npm) { | 278 | 294 | case "@ai-sdk/openai": | 279 | 295 | case "@ai-sdk/azure": | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/compaction.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -143,6 +143,7 @@ export namespace SessionCompaction { | 143 | 143 | providerOptions: ProviderTransform.providerOptions( | 144 | 144 | model, | 145 | 145 | pipe({}, mergeDeep(ProviderTransform.options(model, input.sessionID)), mergeDeep(model.options)), | | 146 | + [], | 146 | 147 | ), | 147 | 148 | headers: model.headers, | 148 | 149 | abortSignal: input.abort, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +34-33Lines changed: 34 additions & 33 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -515,6 +515,37 @@ export namespace SessionPrompt { | 515 | 515 | }) | 516 | 516 | } | 517 | 517 |
| | 518 | + const messages = [ | | 519 | + ...system.map( | | 520 | + (x): ModelMessage => ({ | | 521 | + role: "system", | | 522 | + content: x, | | 523 | + }), | | 524 | + ), | | 525 | + ...MessageV2.toModelMessage( | | 526 | + msgs.filter((m) => { | | 527 | + if (m.info.role !== "assistant" || m.info.error === undefined) { | | 528 | + return true | | 529 | + } | | 530 | + if ( | | 531 | + MessageV2.AbortedError.isInstance(m.info.error) && | | 532 | + m.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning") | | 533 | + ) { | | 534 | + return true | | 535 | + } | | 536 | + | | 537 | + return false | | 538 | + }), | | 539 | + ), | | 540 | + ...(isLastStep | | 541 | + ? [ | | 542 | + { | | 543 | + role: "assistant" as const, | | 544 | + content: MAX_STEPS, | | 545 | + }, | | 546 | + ] | | 547 | + : []), | | 548 | + ] | 518 | 549 | const result = await processor.process({ | 519 | 550 | onError(error) { | 520 | 551 | log.error("stream error", { | @@ -562,42 +593,12 @@ export namespace SessionPrompt { | 562 | 593 | OUTPUT_TOKEN_MAX, | 563 | 594 | ), | 564 | 595 | abortSignal: abort, | 565 | | - providerOptions: ProviderTransform.providerOptions(model, params.options), | | 596 | + providerOptions: ProviderTransform.providerOptions(model, params.options, messages), | 566 | 597 | stopWhen: stepCountIs(1), | 567 | 598 | temperature: params.temperature, | 568 | 599 | topP: params.topP, | 569 | 600 | toolChoice: isLastStep ? "none" : undefined, | 570 | | - messages: [ | 571 | | - ...system.map( | 572 | | - (x): ModelMessage => ({ | 573 | | - role: "system", | 574 | | - content: x, | 575 | | - }), | 576 | | - ), | 577 | | - ...MessageV2.toModelMessage( | 578 | | - msgs.filter((m) => { | 579 | | - if (m.info.role !== "assistant" || m.info.error === undefined) { | 580 | | - return true | 581 | | - } | 582 | | - if ( | 583 | | - MessageV2.AbortedError.isInstance(m.info.error) && | 584 | | - m.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning") | 585 | | - ) { | 586 | | - return true | 587 | | - } | 588 | | - | 589 | | - return false | 590 | | - }), | 591 | | - ), | 592 | | - ...(isLastStep | 593 | | - ? [ | 594 | | - { | 595 | | - role: "assistant" as const, | 596 | | - content: MAX_STEPS, | 597 | | - }, | 598 | | - ] | 599 | | - : []), | 600 | | - ], | | 601 | + messages, | 601 | 602 | tools: model.capabilities.toolcall === false ? undefined : tools, | 602 | 603 | model: wrapLanguageModel({ | 603 | 604 | model: language, | @@ -1464,7 +1465,7 @@ export namespace SessionPrompt { | 1464 | 1465 | await generateText({ | 1465 | 1466 | // use higher # for reasoning models since reasoning tokens eat up a lot of the budget | 1466 | 1467 | maxOutputTokens: small.capabilities.reasoning ? 3000 : 20, | 1467 | | - providerOptions: ProviderTransform.providerOptions(small, options), | | 1468 | + providerOptions: ProviderTransform.providerOptions(small, options, []), | 1468 | 1469 | messages: [ | 1469 | 1470 | ...SystemPrompt.title(small.providerID).map( | 1470 | 1471 | (x): ModelMessage => ({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/summary.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,7 +91,7 @@ export namespace SessionSummary { | 91 | 91 | if (textPart && !userMsg.summary?.title) { | 92 | 92 | const result = await generateText({ | 93 | 93 | maxOutputTokens: small.capabilities.reasoning ? 1500 : 20, | 94 | | - providerOptions: ProviderTransform.providerOptions(small, options), | | 94 | + providerOptions: ProviderTransform.providerOptions(small, options, []), | 95 | 95 | messages: [ | 96 | 96 | ...SystemPrompt.title(small.providerID).map( | 97 | 97 | (x): ModelMessage => ({ | @@ -144,7 +144,7 @@ export namespace SessionSummary { | 144 | 144 | const result = await generateText({ | 145 | 145 | model: language, | 146 | 146 | maxOutputTokens: 100, | 147 | | - providerOptions: ProviderTransform.providerOptions(small, options), | | 147 | + providerOptions: ProviderTransform.providerOptions(small, options, []), | 148 | 148 | messages: [ | 149 | 149 | ...SystemPrompt.summarize(small.providerID).map( | 150 | 150 | (x): ModelMessage => ({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/provider/transform.test.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,6 +130,7 @@ describe("ProviderTransform.message - DeepSeek reasoning content", () => { | 130 | 130 | toolcall: true, | 131 | 131 | input: { text: true, audio: false, image: false, video: false, pdf: false }, | 132 | 132 | output: { text: true, audio: false, image: false, video: false, pdf: false }, | | 133 | + interleaved: false, | 133 | 134 | }, | 134 | 135 | cost: { | 135 | 136 | input: 0.001, | @@ -184,6 +185,7 @@ describe("ProviderTransform.message - DeepSeek reasoning content", () => { | 184 | 185 | toolcall: true, | 185 | 186 | input: { text: true, audio: false, image: false, video: false, pdf: false }, | 186 | 187 | output: { text: true, audio: false, image: false, video: false, pdf: false }, | | 188 | + interleaved: false, | 187 | 189 | }, | 188 | 190 | cost: { | 189 | 191 | input: 0.001, | @@ -236,6 +238,7 @@ describe("ProviderTransform.message - DeepSeek reasoning content", () => { | 236 | 238 | toolcall: true, | 237 | 239 | input: { text: true, audio: false, image: false, video: false, pdf: false }, | 238 | 240 | output: { text: true, audio: false, image: false, video: false, pdf: false }, | | 241 | + interleaved: false, | 239 | 242 | }, | 240 | 243 | cost: { | 241 | 244 | input: 0.001, | @@ -281,6 +284,7 @@ describe("ProviderTransform.message - DeepSeek reasoning content", () => { | 281 | 284 | toolcall: true, | 282 | 285 | input: { text: true, audio: false, image: true, video: false, pdf: false }, | 283 | 286 | output: { text: true, audio: false, image: false, video: false, pdf: false }, | | 287 | + interleaved: false, | 284 | 288 | }, | 285 | 289 | cost: { | 286 | 290 | input: 0.03, | |
|