Skip to content

Commit 7a81ea0

Browse files
committed
feat(opencode): support custom body fields in provider options
Add a `body` option to the provider config that merges custom fields into every outgoing HTTP request body, analogous to the existing custom `headers` support. The field is extracted from provider options before the SDK constructor runs and injected in the shared fetch wrapper for all POST requests.
1 parent f09c859 commit 7a81ea0

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

‎packages/opencode/src/config/provider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export const Info = Schema.Struct({
109109
description:
110110
"Timeout in milliseconds between streamed SSE chunks for this provider. If no chunk arrives within this window, the request is aborted.",
111111
}),
112+
body: Schema.optional(Schema.Record(Schema.String, Schema.Any)).annotate({
113+
description: "Custom fields to merge into every outgoing HTTP request body for this provider",
114+
}),
112115
}),
113116
[Schema.Record(Schema.String, Schema.Any)],
114117
),

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,8 +1612,10 @@ export const layer = Layer.effect(
16121612
const customFetch = options["fetch"]
16131613
const chunkTimeout = options["chunkTimeout"]
16141614
const headerTimeout = options["headerTimeout"]
1615+
const customBody = options["body"] as Record<string, unknown> | undefined
16151616
delete options["chunkTimeout"]
16161617
delete options["headerTimeout"]
1618+
delete options["body"]
16171619

16181620
options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {
16191621
const fetchFn = customFetch ?? fetch
@@ -1650,6 +1652,12 @@ export const layer = Layer.effect(
16501652
}
16511653
}
16521654

1655+
if (customBody && opts.body && opts.method === "POST") {
1656+
const body = JSON.parse(opts.body as string)
1657+
Object.assign(body, customBody)
1658+
opts.body = JSON.stringify(body)
1659+
}
1660+
16531661
const res = await fetchFn(input, {
16541662
...opts,
16551663
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { afterEach, expect } from "bun:test"
2+
import { streamText } from "ai"
3+
import { Effect, Layer } from "effect"
4+
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
5+
import { disposeAllInstances, provideTmpdirServer } from "../fixture/fixture"
6+
import { testEffect } from "../lib/effect"
7+
import { reply, TestLLMServer } from "../lib/llm-server"
8+
import { testProviderConfig } from "../lib/test-provider"
9+
import { Env } from "@/env"
10+
import { Plugin } from "@/plugin"
11+
import { Provider } from "@/provider/provider"
12+
import { ModelID, ProviderID } from "@/provider/schema"
13+
14+
afterEach(async () => {
15+
await disposeAllInstances()
16+
})
17+
18+
const it = testEffect(
19+
Layer.mergeAll(
20+
Provider.defaultLayer,
21+
Env.defaultLayer,
22+
Plugin.defaultLayer,
23+
TestLLMServer.layer,
24+
CrossSpawnSpawner.defaultLayer,
25+
),
26+
)
27+
28+
it.live("custom body fields from provider options are merged into HTTP request body", () =>
29+
provideTmpdirServer(
30+
({ llm }) =>
31+
Effect.gen(function* () {
32+
yield* llm.push(reply().text("hello").stop())
33+
34+
const provider = yield* Provider.Service
35+
const model = yield* provider.getModel(ProviderID.make("test"), ModelID.make("test-model"))
36+
const result = streamText({
37+
model: yield* provider.getLanguage(model),
38+
messages: [{ role: "user", content: "hi" }],
39+
})
40+
41+
yield* Effect.promise(() => result.text)
42+
43+
const hits = yield* llm.hits
44+
expect(hits.length).toBeGreaterThan(0)
45+
const body = hits[0].body
46+
expect(body).toHaveProperty("custom_routing", "fast")
47+
expect(body).toHaveProperty("metadata")
48+
expect((body as any).metadata).toEqual({ source: "opencode" })
49+
}),
50+
{
51+
config: (url) => {
52+
const config = testProviderConfig(url)
53+
return {
54+
...config,
55+
provider: {
56+
test: {
57+
...config.provider.test,
58+
options: {
59+
...config.provider.test.options,
60+
body: {
61+
custom_routing: "fast",
62+
metadata: { source: "opencode" },
63+
},
64+
},
65+
},
66+
},
67+
}
68+
},
69+
},
70+
),
71+
)
72+
73+
it.live("custom body fields can override SDK fields like custom headers can", () =>
74+
provideTmpdirServer(
75+
({ llm }) =>
76+
Effect.gen(function* () {
77+
yield* llm.push(reply().text("works").stop())
78+
79+
const provider = yield* Provider.Service
80+
const model = yield* provider.getModel(ProviderID.make("test"), ModelID.make("test-model"))
81+
const result = streamText({
82+
model: yield* provider.getLanguage(model),
83+
messages: [{ role: "user", content: "hi" }],
84+
})
85+
86+
yield* Effect.promise(() => result.text)
87+
88+
const hits = yield* llm.hits
89+
expect(hits.length).toBeGreaterThan(0)
90+
const body = hits[0].body
91+
// custom body overrides SDK fields (same power as custom headers)
92+
expect(body).toHaveProperty("model", "custom-model-override")
93+
expect(body).toHaveProperty("extra_field", "present")
94+
}),
95+
{
96+
config: (url) => {
97+
const config = testProviderConfig(url)
98+
return {
99+
...config,
100+
provider: {
101+
test: {
102+
...config.provider.test,
103+
options: {
104+
...config.provider.test.options,
105+
body: {
106+
model: "custom-model-override",
107+
extra_field: "present",
108+
},
109+
},
110+
},
111+
},
112+
}
113+
},
114+
},
115+
),
116+
)
117+
118+
it.live("provider without custom body works normally", () =>
119+
provideTmpdirServer(
120+
({ llm }) =>
121+
Effect.gen(function* () {
122+
yield* llm.push(reply().text("no body").stop())
123+
124+
const provider = yield* Provider.Service
125+
const model = yield* provider.getModel(ProviderID.make("test"), ModelID.make("test-model"))
126+
const result = streamText({
127+
model: yield* provider.getLanguage(model),
128+
messages: [{ role: "user", content: "hi" }],
129+
})
130+
131+
expect(yield* Effect.promise(() => result.text)).toBe("no body")
132+
}),
133+
{
134+
config: (url) => testProviderConfig(url),
135+
},
136+
),
137+
)

0 commit comments

Comments
 (0)