Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/opencode/src/provider/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export namespace ModelsDev {
field: z.enum(["reasoning_content", "reasoning_details"]),
})
.strict(),
z
.object({
tagName: z.string(),
startWithReasoning: z.boolean().optional(),
})
.strict(),
])
.optional(),
cost: z
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ export namespace Provider {
z.object({
field: z.enum(["reasoning_content", "reasoning_details"]),
}),
z.object({
tagName: z.string(),
startWithReasoning: z.boolean().optional(),
}),
]),
}),
cost: z.object({
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/provider/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export namespace ProviderTransform {
if (
model.capabilities.interleaved &&
typeof model.capabilities.interleaved === "object" &&
"field" in model.capabilities.interleaved &&
model.capabilities.interleaved.field === "reasoning_content"
) {
return msgs.map((msg) => {
Expand Down
49 changes: 37 additions & 12 deletions packages/opencode/src/session/llm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Provider } from "@/provider/provider"
import { Log } from "@/util/log"
import { streamText, wrapLanguageModel, type ModelMessage, type StreamTextResult, type Tool, type ToolSet } from "ai"
import {
extractReasoningMiddleware,
streamText,
wrapLanguageModel,
type LanguageModelMiddleware,
type ModelMessage,
type StreamTextResult,
type Tool,
type ToolSet,
} from "ai"
import { clone, mergeDeep, pipe } from "remeda"
import { ProviderTransform } from "@/provider/transform"
import { Config } from "@/config/config"
Expand Down Expand Up @@ -171,22 +180,38 @@ export namespace LLM {
],
model: wrapLanguageModel({
model: language,
middleware: [
{
async transformParams(args) {
if (args.type === "stream") {
// @ts-expect-error
args.params.prompt = ProviderTransform.message(args.params.prompt, input.model)
}
return args.params
},
},
],
middleware: buildMiddleware(input.model),
}),
experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
})
}

function buildMiddleware(model: Provider.Model): LanguageModelMiddleware[] {
const middleware: LanguageModelMiddleware[] = [
{
async transformParams(args) {
if (args.type === "stream") {
// @ts-expect-error
args.params.prompt = ProviderTransform.message(args.params.prompt, model)
}
return args.params
},
},
]

const interleaved = model.capabilities.interleaved
if (typeof interleaved === "object" && "tagName" in interleaved) {
middleware.push(
extractReasoningMiddleware({
tagName: interleaved.tagName,
startWithReasoning: interleaved.startWithReasoning,
}),
)
}

return middleware
}

async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user">) {
const enabled = pipe(
input.agent.tools,
Expand Down
87 changes: 87 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1807,3 +1807,90 @@ test("custom model inherits api.url from models.dev provider", async () => {
},
})
})

test("model with interleaved tagName for think tag extraction", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
"think-provider": {
name: "Think Provider",
npm: "@ai-sdk/openai-compatible",
env: [],
models: {
"think-model": {
name: "Think Model",
tool_call: true,
reasoning: true,
limit: { context: 128000, output: 65536 },
interleaved: {
tagName: "think",
},
},
},
options: { apiKey: "test" },
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const providers = await Provider.list()
expect(providers["think-provider"]).toBeDefined()
const model = providers["think-provider"].models["think-model"]
expect(model.capabilities.interleaved).toEqual({ tagName: "think" })
expect(model.capabilities.reasoning).toBe(true)
},
})
})

test("model with interleaved tagName and startWithReasoning", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
"reasoning-provider": {
name: "Reasoning Provider",
npm: "@ai-sdk/openai-compatible",
env: [],
models: {
"reasoning-model": {
name: "Reasoning Model",
tool_call: true,
reasoning: true,
limit: { context: 128000, output: 65536 },
interleaved: {
tagName: "think",
startWithReasoning: true,
},
},
},
options: { apiKey: "test" },
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const providers = await Provider.list()
expect(providers["reasoning-provider"]).toBeDefined()
const model = providers["reasoning-provider"].models["reasoning-model"]
expect(model.capabilities.interleaved).toEqual({
tagName: "think",
startWithReasoning: true,
})
},
})
})
12 changes: 12 additions & 0 deletions packages/sdk/js/src/v2/gen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,10 @@ export type ProviderConfig = {
| {
field: "reasoning_content" | "reasoning_details"
}
| {
tagName: string
startWithReasoning?: boolean
}
cost?: {
input: number
output: number
Expand Down Expand Up @@ -1746,6 +1750,10 @@ export type Model = {
| {
field: "reasoning_content" | "reasoning_details"
}
| {
tagName: string
startWithReasoning?: boolean
}
}
cost: {
input: number
Expand Down Expand Up @@ -3450,6 +3458,10 @@ export type ProviderListResponses = {
| {
field: "reasoning_content" | "reasoning_details"
}
| {
tagName: string
startWithReasoning?: boolean
}
cost?: {
input: number
output: number
Expand Down
38 changes: 38 additions & 0 deletions packages/sdk/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3098,6 +3098,19 @@
},
"required": ["field"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"tagName": {
"type": "string"
},
"startWithReasoning": {
"type": "boolean"
}
},
"required": ["tagName"],
"additionalProperties": false
}
]
},
Expand Down Expand Up @@ -7925,6 +7938,19 @@
},
"required": ["field"],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"tagName": {
"type": "string"
},
"startWithReasoning": {
"type": "boolean"
}
},
"required": ["tagName"],
"additionalProperties": false
}
]
},
Expand Down Expand Up @@ -9015,6 +9041,18 @@
}
},
"required": ["field"]
},
{
"type": "object",
"properties": {
"tagName": {
"type": "string"
},
"startWithReasoning": {
"type": "boolean"
}
},
"required": ["tagName"]
}
]
}
Expand Down