Skip to content

Commit d033a14

Browse files
zoomote[bot]roomoteedelauna
authored
[Feat] Add custom request fields for OpenAI-compatible providers (#1350)
* feat(openai): support custom extra request body * test(webview): add Extra Body visual baseline * fix(i18n): translate Extra Body settings * test(webview): cover Extra Body themes * test(webview): add Extra Body theme baselines * test(webview): remove Extra Body snapshot clipping * test(webview): refresh unclipped Extra Body baselines * fix(openai): harden custom request field validation * test(webview): apply visual themes after gallery mount * test(webview): refresh gallery-mounted provider baselines * test(ci): run mutation discovery from package binaries * test(webview): type mutation fixtures precisely --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent b2f63d3 commit d033a14

38 files changed

Lines changed: 745 additions & 24 deletions

‎packages/types/src/__tests__/provider-settings.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ANTHROPIC_API_PROTOCOL, OPENAI_API_PROTOCOL, providerIdentifiers, provi
22
import {
33
getApiProtocol,
44
OPEN_AI_CODEX_SERVICE_TIER_KEY,
5+
parseOpenAiExtraBody,
56
PROVIDER_SETTINGS_KEYS,
67
providerSettingsSchema,
78
providerSettingsSchemaDiscriminated,
@@ -22,6 +23,84 @@ describe("provider settings discriminated union", () => {
2223
})
2324
})
2425

26+
describe("OpenAI-compatible extra body settings", () => {
27+
it("accepts a JSON object with provider-specific nested fields", () => {
28+
const settings = {
29+
apiProvider: providerIdentifiers.openai,
30+
openAiExtraBody: JSON.stringify({ metadata: { completion_window: "balanced" }, store: false }),
31+
}
32+
33+
expect(providerSettingsSchemaDiscriminated.parse(settings)).toEqual(settings)
34+
expect(PROVIDER_SETTINGS_KEYS).toContain("openAiExtraBody")
35+
})
36+
37+
it.each(["not json", "[]", "null", '"value"'])("rejects non-object JSON: %s", (openAiExtraBody) => {
38+
expect(
39+
providerSettingsSchemaDiscriminated.safeParse({
40+
apiProvider: providerIdentifiers.openai,
41+
openAiExtraBody,
42+
}).success,
43+
).toBe(false)
44+
})
45+
46+
it.each([
47+
"__proto__",
48+
"constructor",
49+
"prototype",
50+
"max_completion_tokens",
51+
"max_tokens",
52+
"messages",
53+
"model",
54+
"parallel_tool_calls",
55+
"reasoning",
56+
"reasoning_effort",
57+
"response_format",
58+
"stream",
59+
"stream_options",
60+
"temperature",
61+
"tool_choice",
62+
"tools",
63+
])("rejects the reserved extra-body key %s", (reservedKey) => {
64+
expect(
65+
providerSettingsSchemaDiscriminated.safeParse({
66+
apiProvider: providerIdentifiers.openai,
67+
openAiExtraBody: JSON.stringify({ [reservedKey]: "override" }),
68+
}).success,
69+
).toBe(false)
70+
})
71+
72+
it("reports and filters reserved keys while preserving allowed nested fields", () => {
73+
const result = parseOpenAiExtraBody(
74+
JSON.stringify({
75+
metadata: { completion_window: "balanced" },
76+
model: "overridden-model",
77+
response_format: { type: "json_object" },
78+
stream: false,
79+
}),
80+
)
81+
82+
expect(result).toEqual({
83+
success: false,
84+
reason: "reservedKeys",
85+
reservedKeys: ["model", "response_format", "stream"],
86+
data: { metadata: { completion_window: "balanced" } },
87+
})
88+
})
89+
90+
it("accepts stop and n as provider-specific request fields", () => {
91+
const settings = {
92+
apiProvider: providerIdentifiers.openai,
93+
openAiExtraBody: JSON.stringify({ stop: ["DONE"], n: 2 }),
94+
}
95+
96+
expect(providerSettingsSchemaDiscriminated.parse(settings)).toEqual(settings)
97+
expect(parseOpenAiExtraBody(settings.openAiExtraBody)).toEqual({
98+
success: true,
99+
data: { stop: ["DONE"], n: 2 },
100+
})
101+
})
102+
})
103+
25104
describe("OpenAI Codex provider settings", () => {
26105
it("preserves the Fast preference in general and provider-specific schemas", () => {
27106
const settings = {

‎packages/types/src/provider-settings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { providerDefinitionList, type ProviderDefinition } from "./provider-sett
44
import { API_PROVIDER_FIELD, SETTINGS_SHAPE_FIELD } from "./provider-settings/common.js"
55
export {
66
OPEN_AI_CODEX_SERVICE_TIER_KEY,
7+
parseOpenAiExtraBody,
78
kimiCodeAuthMethodSchema,
89
type KimiCodeAuthMethod,
910
nanoGptDefaultRoutingPreference,

‎packages/types/src/provider-settings/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { basetenProviderDefinition } from "./baseten.js"
3737
import type { ProviderDefinition } from "./common.js"
3838

3939
export { OPEN_AI_CODEX_SERVICE_TIER_KEY } from "./openai-codex.js"
40+
export { parseOpenAiExtraBody } from "./openai.js"
4041
export { kimiCodeAuthMethodSchema, type KimiCodeAuthMethod } from "./kimi-code.js"
4142
export { zaiApiLineSchema, type ZaiApiLine } from "./zai.js"
4243
export {

‎packages/types/src/provider-settings/openai.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,74 @@ import { baseProviderSettingsShape, createModelIdAccessor, createProviderDefinit
66

77
export const OPEN_AI_MODEL_ID_FIELD = "openAiModelId"
88

9+
const OPENAI_EXTRA_BODY_RESERVED_KEYS = [
10+
// Prototype-pollution defenses; remaining keys are request-owned, including tool-call protocol controls.
11+
"__proto__",
12+
"constructor",
13+
"prototype",
14+
"max_completion_tokens",
15+
"max_tokens",
16+
"messages",
17+
"model",
18+
"parallel_tool_calls",
19+
"reasoning",
20+
"reasoning_effort",
21+
"response_format",
22+
"stream",
23+
"stream_options",
24+
"temperature",
25+
"tool_choice",
26+
"tools",
27+
] as const
28+
29+
type OpenAiExtraBodyParseResult =
30+
| { success: true; data: Record<string, unknown> }
31+
| {
32+
success: false
33+
reason: "invalidJson" | "objectRequired" | "reservedKeys"
34+
data: Record<string, unknown>
35+
reservedKeys?: string[]
36+
}
37+
38+
export function parseOpenAiExtraBody(value: string | undefined): OpenAiExtraBodyParseResult {
39+
if (!value?.trim()) {
40+
return { success: true, data: {} }
41+
}
42+
43+
let parsed: unknown
44+
try {
45+
parsed = JSON.parse(value)
46+
} catch {
47+
return { success: false, reason: "invalidJson", data: {} }
48+
}
49+
50+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
51+
return { success: false, reason: "objectRequired", data: {} }
52+
}
53+
54+
const entries = Object.entries(parsed)
55+
const reservedKeys = entries
56+
.map(([key]) => key)
57+
.filter((key) => (OPENAI_EXTRA_BODY_RESERVED_KEYS as readonly string[]).includes(key))
58+
const data = Object.fromEntries(entries.filter(([key]) => !reservedKeys.includes(key)))
59+
60+
if (reservedKeys.length > 0) {
61+
return { success: false, reason: "reservedKeys", reservedKeys, data }
62+
}
63+
64+
return { success: true, data }
65+
}
66+
67+
const openAiExtraBodySchema = z
68+
.string()
69+
.superRefine((value, ctx) => {
70+
const result = parseOpenAiExtraBody(value)
71+
if (!result.success) {
72+
ctx.addIssue({ code: "custom", message: result.reason })
73+
}
74+
})
75+
.optional()
76+
977
export const openAiProviderDefinition = createProviderDefinition({
1078
apiProvider: providerIdentifiers.openai,
1179
modelIdKey: OPEN_AI_MODEL_ID_FIELD,
@@ -22,5 +90,6 @@ export const openAiProviderDefinition = createProviderDefinition({
2290
openAiStreamingEnabled: z.boolean().optional(),
2391
openAiHostHeader: z.string().optional(), // Keep temporarily for backward compatibility during migration.
2492
openAiHeaders: z.record(z.string(), z.string()).optional(),
93+
openAiExtraBody: openAiExtraBodySchema,
2594
},
2695
})

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ describe("OpenAiHandler", () => {
171171
})
172172
})
173173

174+
describe("withExtraBody", () => {
175+
it("gives request-owned options precedence when an allowed Extra Body field collides", () => {
176+
const extraBodyHandler = new OpenAiHandler({
177+
...mockOptions,
178+
openAiExtraBody: JSON.stringify({ service_tier: "flex" }),
179+
})
180+
181+
expect(extraBodyHandler["withExtraBody"]({})).toEqual({ service_tier: "flex" })
182+
expect(extraBodyHandler["withExtraBody"]({ service_tier: "default" })).toEqual({
183+
service_tier: "default",
184+
})
185+
})
186+
})
187+
174188
describe("createMessage", () => {
175189
const systemPrompt = "You are a helpful assistant."
176190
const messages: Anthropic.Messages.MessageParam[] = [
@@ -261,6 +275,45 @@ describe("OpenAiHandler", () => {
261275
expect(textChunks[0].text).toBe("Test response")
262276
})
263277

278+
it("adds Extra Body fields to streaming requests without allowing reserved field overrides", async () => {
279+
const extraBodyHandler = new OpenAiHandler({
280+
...mockOptions,
281+
openAiExtraBody: JSON.stringify({
282+
metadata: { completion_window: "balanced" },
283+
model: "overridden-model",
284+
messages: [],
285+
stream: false,
286+
}),
287+
})
288+
289+
await collectStream(extraBodyHandler.createMessage(systemPrompt, messages))
290+
291+
expect(mockCreate).toHaveBeenCalledWith(
292+
expect.objectContaining({
293+
metadata: { completion_window: "balanced" },
294+
model: mockOptions.openAiModelId,
295+
stream: true,
296+
messages: expect.arrayContaining([expect.objectContaining({ role: "user" })]),
297+
}),
298+
{},
299+
)
300+
})
301+
302+
it("adds Extra Body fields to non-streaming requests", async () => {
303+
const extraBodyHandler = new OpenAiHandler({
304+
...mockOptions,
305+
openAiStreamingEnabled: false,
306+
openAiExtraBody: JSON.stringify({ metadata: { completion_window: "balanced" } }),
307+
})
308+
309+
await collectStream(extraBodyHandler.createMessage(systemPrompt, messages))
310+
311+
expect(mockCreate).toHaveBeenCalledWith(
312+
expect.objectContaining({ metadata: { completion_window: "balanced" } }),
313+
{},
314+
)
315+
})
316+
264317
it("streams reasoning chunks from delta.reasoning_content", async () => {
265318
mockCreate.mockImplementationOnce(async () =>
266319
asyncStreamFrom([
@@ -863,6 +916,20 @@ describe("OpenAiHandler", () => {
863916
)
864917
})
865918

919+
it("adds Extra Body fields to single-completion requests", async () => {
920+
const extraBodyHandler = new OpenAiHandler({
921+
...mockOptions,
922+
openAiExtraBody: JSON.stringify({ metadata: { completion_window: "balanced" } }),
923+
})
924+
925+
await extraBodyHandler.completePrompt("Test prompt")
926+
927+
expect(mockCreate).toHaveBeenCalledWith(
928+
expect.objectContaining({ metadata: { completion_window: "balanced" } }),
929+
{},
930+
)
931+
})
932+
866933
it("should handle API errors", async () => {
867934
mockCreate.mockRejectedValueOnce(new Error("API Error"))
868935
await expect(handler.completePrompt("Test prompt")).rejects.toThrow("OpenAI completion error: API Error")
@@ -1123,6 +1190,21 @@ describe("OpenAiHandler", () => {
11231190
)
11241191
})
11251192

1193+
it.each([true, false])("adds Extra Body fields to O3 requests when streaming is %s", async (streaming) => {
1194+
const o3Handler = new OpenAiHandler({
1195+
...o3Options,
1196+
openAiStreamingEnabled: streaming,
1197+
openAiExtraBody: JSON.stringify({ metadata: { completion_window: "balanced" } }),
1198+
})
1199+
1200+
await collectStream(o3Handler.createMessage("system", []))
1201+
1202+
expect(mockCreate).toHaveBeenCalledWith(
1203+
expect.objectContaining({ metadata: { completion_window: "balanced" } }),
1204+
{},
1205+
)
1206+
})
1207+
11261208
it("should handle tool calls with O3 model in streaming mode", async () => {
11271209
const o3Handler = new OpenAiHandler(o3Options)
11281210

0 commit comments

Comments
 (0)