Skip to content

Commit b1234ce

Browse files
committed
feat: add query parameter support for OpenAI-compatible providers
Adds a queryParams option to OpenaiCompatibleProviderSettings that appends query parameters to all API requests. This enables support for providers that require query string parameters for API versioning, tenant identification, or feature flags. Fixes anomalyco#7198
1 parent f3e7923 commit b1234ce

2 files changed

Lines changed: 149 additions & 2 deletions

File tree

‎packages/opencode/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface OpenaiCompatibleProviderSettings {
2929
*/
3030
headers?: Record<string, string>
3131

32+
/**
33+
* Custom query parameter to append to the requests
34+
*/
35+
queryParams?: Record<string, string>
36+
3237
/**
3338
* Custom fetch implementation.
3439
*/
@@ -65,11 +70,21 @@ export function createOpenaiCompatible(options: OpenaiCompatibleProviderSettings
6570

6671
const getHeaders = () => withUserAgentSuffix(headers, `ai-sdk/openai-compatible/${VERSION}`)
6772

73+
const getUrl = ({ path }: { path: string }) => {
74+
const url = new URL(`${baseURL}${path}`)
75+
if (options.queryParams) {
76+
for (const [key, value] of Object.entries(options.queryParams)) {
77+
url.searchParams.set(key, value)
78+
}
79+
}
80+
return url.toString()
81+
}
82+
6883
const createChatModel = (modelId: OpenaiCompatibleModelId) => {
6984
return new OpenAICompatibleChatLanguageModel(modelId, {
7085
provider: `${options.name ?? "openai-compatible"}.chat`,
7186
headers: getHeaders,
72-
url: ({ path }) => `${baseURL}${path}`,
87+
url: getUrl,
7388
fetch: options.fetch,
7489
})
7590
}
@@ -78,7 +93,7 @@ export function createOpenaiCompatible(options: OpenaiCompatibleProviderSettings
7893
return new OpenAIResponsesLanguageModel(modelId, {
7994
provider: `${options.name ?? "openai-compatible"}.responses`,
8095
headers: getHeaders,
81-
url: ({ path }) => `${baseURL}${path}`,
96+
url: getUrl,
8297
fetch: options.fetch,
8398
})
8499
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { test, expect } from "bun:test"
2+
import path from "path"
3+
import { tmpdir } from "../fixture/fixture"
4+
import { Instance } from "../../src/project/instance"
5+
import { Provider } from "../../src/provider/provider"
6+
7+
test("provider with queryParams in config", async () => {
8+
await using tmp = await tmpdir({
9+
init: async (dir) => {
10+
await Bun.write(
11+
path.join(dir, "opencode.json"),
12+
JSON.stringify({
13+
$schema: "https://opencode.ai/config.json",
14+
provider: {
15+
"custom-openai": {
16+
name: "Custom OpenAI with Query Params",
17+
npm: "@ai-sdk/openai-compatible",
18+
env: [],
19+
models: {
20+
"gpt-4": {
21+
name: "GPT-4",
22+
tool_call: true,
23+
limit: { context: 128000, output: 4096 },
24+
},
25+
},
26+
options: {
27+
apiKey: "test-key",
28+
baseURL: "https://api.example.com/v1",
29+
queryParams: {
30+
"api-version": "2024-01-01",
31+
tenant: "my-org",
32+
},
33+
},
34+
},
35+
},
36+
}),
37+
)
38+
},
39+
})
40+
await Instance.provide({
41+
directory: tmp.path,
42+
fn: async () => {
43+
const providers = await Provider.list()
44+
expect(providers["custom-openai"]).toBeDefined()
45+
expect(providers["custom-openai"].name).toBe("Custom OpenAI with Query Params")
46+
expect(providers["custom-openai"].options.queryParams).toEqual({
47+
"api-version": "2024-01-01",
48+
tenant: "my-org",
49+
})
50+
},
51+
})
52+
})
53+
54+
test("provider without queryParams still works", async () => {
55+
await using tmp = await tmpdir({
56+
init: async (dir) => {
57+
await Bun.write(
58+
path.join(dir, "opencode.json"),
59+
JSON.stringify({
60+
$schema: "https://opencode.ai/config.json",
61+
provider: {
62+
"custom-openai": {
63+
name: "Custom OpenAI",
64+
npm: "@ai-sdk/openai-compatible",
65+
env: [],
66+
models: {
67+
"gpt-4": {
68+
name: "GPT-4",
69+
tool_call: true,
70+
limit: { context: 128000, output: 4096 },
71+
},
72+
},
73+
options: {
74+
apiKey: "test-key",
75+
baseURL: "https://api.example.com/v1",
76+
},
77+
},
78+
},
79+
}),
80+
)
81+
},
82+
})
83+
await Instance.provide({
84+
directory: tmp.path,
85+
fn: async () => {
86+
const providers = await Provider.list()
87+
expect(providers["custom-openai"]).toBeDefined()
88+
expect(providers["custom-openai"].name).toBe("Custom OpenAI")
89+
expect(providers["custom-openai"].options.queryParams).toBeUndefined()
90+
},
91+
})
92+
})
93+
94+
test("provider with empty queryParams", async () => {
95+
await using tmp = await tmpdir({
96+
init: async (dir) => {
97+
await Bun.write(
98+
path.join(dir, "opencode.json"),
99+
JSON.stringify({
100+
$schema: "https://opencode.ai/config.json",
101+
provider: {
102+
"custom-openai": {
103+
name: "Custom OpenAI Empty Params",
104+
npm: "@ai-sdk/openai-compatible",
105+
env: [],
106+
models: {
107+
"gpt-4": {
108+
name: "GPT-4",
109+
tool_call: true,
110+
limit: { context: 128000, output: 4096 },
111+
},
112+
},
113+
options: {
114+
apiKey: "test-key",
115+
baseURL: "https://api.example.com/v1",
116+
queryParams: {},
117+
},
118+
},
119+
},
120+
}),
121+
)
122+
},
123+
})
124+
await Instance.provide({
125+
directory: tmp.path,
126+
fn: async () => {
127+
const providers = await Provider.list()
128+
expect(providers["custom-openai"]).toBeDefined()
129+
expect(providers["custom-openai"].options.queryParams).toEqual({})
130+
},
131+
})
132+
})

0 commit comments

Comments
 (0)