Skip to content

Commit b6644b4

Browse files
committed
fix(openai): make onboarding models account-aware
1 parent 570eab5 commit b6644b4

5 files changed

Lines changed: 372 additions & 9 deletions

File tree

‎extensions/openai/openai-provider.live.test.ts‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { describe, expect, it } from "vitest";
55
import { buildOpenAIProvider } from "./openai-provider.js";
66

77
const OPENAI_API_KEY = process.env.OPENAI_API_KEY ?? "";
8-
const DEFAULT_LIVE_MODEL_IDS = ["chat-latest", "gpt-5.5", "gpt-5.4-mini", "gpt-5.4-nano"] as const;
8+
const DEFAULT_LIVE_MODEL_IDS = [
9+
"gpt-5.6",
10+
"chat-latest",
11+
"gpt-5.5",
12+
"gpt-5.4-mini",
13+
"gpt-5.4-nano",
14+
] as const;
915
const liveEnabled = OPENAI_API_KEY.trim().length > 0 && process.env.OPENCLAW_LIVE_TEST === "1";
1016
const describeLive = liveEnabled ? describe : describe.skip;
1117

@@ -22,6 +28,25 @@ type LiveModelCase = {
2228

2329
function resolveLiveModelCase(modelId: string): LiveModelCase {
2430
switch (modelId) {
31+
case "gpt-5.6":
32+
case "gpt-5.6-sol":
33+
case "gpt-5.6-terra":
34+
case "gpt-5.6-luna":
35+
return {
36+
modelId,
37+
templateId: "gpt-5.5",
38+
templateName: "GPT-5.5",
39+
cost:
40+
modelId === "gpt-5.6-terra"
41+
? { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 3.125 }
42+
: modelId === "gpt-5.6-luna"
43+
? { input: 1, output: 6, cacheRead: 0.1, cacheWrite: 1.25 }
44+
: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
45+
contextWindow: 1_050_000,
46+
maxTokens: 128_000,
47+
reasoning: true,
48+
textVerbosity: "low",
49+
};
2550
case "chat-latest":
2651
return {
2752
modelId,
@@ -39,7 +64,7 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
3964
templateId: "gpt-5.5",
4065
templateName: "GPT-5.5",
4166
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
42-
contextWindow: 1_000_000,
67+
contextWindow: 1_050_000,
4368
maxTokens: 128_000,
4469
reasoning: true,
4570
textVerbosity: "low",
@@ -50,7 +75,7 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
5075
templateId: "gpt-5.4-pro",
5176
templateName: "GPT-5.4 Pro",
5277
cost: { input: 30, output: 180, cacheRead: 0, cacheWrite: 0 },
53-
contextWindow: 1_000_000,
78+
contextWindow: 1_050_000,
5479
maxTokens: 128_000,
5580
reasoning: true,
5681
textVerbosity: "low",
@@ -60,8 +85,8 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
6085
modelId,
6186
templateId: "gpt-5.2",
6287
templateName: "GPT-5.2",
63-
cost: { input: 1.75, output: 14, cacheRead: 0.175, cacheWrite: 0 },
64-
contextWindow: 400_000,
88+
cost: { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 0 },
89+
contextWindow: 1_050_000,
6590
maxTokens: 128_000,
6691
reasoning: true,
6792
textVerbosity: "low",
@@ -71,8 +96,8 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
7196
modelId,
7297
templateId: "gpt-5.2-pro",
7398
templateName: "GPT-5.2 Pro",
74-
cost: { input: 21, output: 168, cacheRead: 0, cacheWrite: 0 },
75-
contextWindow: 400_000,
99+
cost: { input: 30, output: 180, cacheRead: 0, cacheWrite: 0 },
100+
contextWindow: 1_050_000,
76101
maxTokens: 128_000,
77102
reasoning: true,
78103
textVerbosity: "low",
@@ -82,7 +107,7 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
82107
modelId,
83108
templateId: "gpt-5-mini",
84109
templateName: "GPT-5 mini",
85-
cost: { input: 0.25, output: 2, cacheRead: 0.025, cacheWrite: 0 },
110+
cost: { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
86111
contextWindow: 400_000,
87112
maxTokens: 128_000,
88113
reasoning: true,
@@ -93,7 +118,7 @@ function resolveLiveModelCase(modelId: string): LiveModelCase {
93118
modelId,
94119
templateId: "gpt-5-nano",
95120
templateName: "GPT-5 nano",
96-
cost: { input: 0.05, output: 0.4, cacheRead: 0.005, cacheWrite: 0 },
121+
cost: { input: 0.2, output: 1.25, cacheRead: 0.02, cacheWrite: 0 },
97122
contextWindow: 400_000,
98123
maxTokens: 128_000,
99124
reasoning: true,

‎extensions/openai/openai-provider.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ describe("buildOpenAIProvider", () => {
493493
data: [
494494
{ id: "gpt-5.6", object: "model" },
495495
{ id: "gpt-5.5", object: "model" },
496+
{ id: "chat-latest", object: "model" },
497+
{ id: "gpt-5.4", object: "model" },
498+
{ id: "gpt-5.4-pro", object: "model" },
499+
{ id: "gpt-5.4-mini", object: "model" },
500+
{ id: "gpt-5.4-nano", object: "model" },
501+
{ id: "gpt-5.3-codex-spark", object: "model" },
496502
{ id: "not-in-manifest", object: "model" },
497503
],
498504
}),
@@ -508,6 +514,47 @@ describe("buildOpenAIProvider", () => {
508514
expect(provider.apiKey).toBe("sk-openai");
509515
expect(provider.models.map((model) => model.id)).toContain("gpt-5.6");
510516
expect(provider.models.map((model) => model.id)).toContain("gpt-5.5");
517+
expect(provider.models.map((model) => model.id)).toEqual(
518+
expect.arrayContaining([
519+
"chat-latest",
520+
"gpt-5.4",
521+
"gpt-5.4-pro",
522+
"gpt-5.4-mini",
523+
"gpt-5.4-nano",
524+
]),
525+
);
526+
expect(provider.models.find((model) => model.id === "chat-latest")).toMatchObject({
527+
api: "openai-responses",
528+
baseUrl: "https://api.openai.com/v1",
529+
contextWindow: 400_000,
530+
maxTokens: 128_000,
531+
reasoning: false,
532+
cost: { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
533+
});
534+
expect(provider.models.find((model) => model.id === "gpt-5.4-pro")).toMatchObject({
535+
api: "openai-responses",
536+
baseUrl: "https://api.openai.com/v1",
537+
contextWindow: 1_050_000,
538+
maxTokens: 128_000,
539+
reasoning: true,
540+
input: ["text"],
541+
cost: { input: 30, output: 180, cacheRead: 0, cacheWrite: 0 },
542+
});
543+
expect(provider.models.find((model) => model.id === "gpt-5.4-mini")).toMatchObject({
544+
api: "openai-responses",
545+
baseUrl: "https://api.openai.com/v1",
546+
contextWindow: 400_000,
547+
maxTokens: 128_000,
548+
cost: { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
549+
});
550+
expect(provider.models.find((model) => model.id === "gpt-5.4-nano")).toMatchObject({
551+
api: "openai-responses",
552+
baseUrl: "https://api.openai.com/v1",
553+
contextWindow: 400_000,
554+
maxTokens: 128_000,
555+
cost: { input: 0.2, output: 1.25, cacheRead: 0.02, cacheWrite: 0 },
556+
});
557+
expect(provider.models.map((model) => model.id)).not.toContain("gpt-5.3-codex-spark");
511558
expect(provider.models.map((model) => model.id)).not.toContain("not-in-manifest");
512559
const fetchParams = vi.mocked(fetchGuard).mock.calls[0]?.[0];
513560
expect(fetchParams?.url).toBe("https://api.openai.com/v1/models");
@@ -521,6 +568,38 @@ describe("buildOpenAIProvider", () => {
521568
expect(release).toHaveBeenCalledOnce();
522569
});
523570

571+
it("does not surface platform models omitted by the account's live catalog", async () => {
572+
const fetchGuard: LiveModelCatalogFetchGuard = vi.fn(async () => ({
573+
response: Response.json({ data: [{ id: "gpt-5.5", object: "model" }] }),
574+
finalUrl: "https://api.openai.com/v1/models",
575+
release: async () => undefined,
576+
}));
577+
578+
const provider = await buildOpenAILiveProviderConfig({
579+
apiKey: "sk-openai",
580+
fetchGuard,
581+
});
582+
583+
expect(provider.models.map((model) => model.id)).toEqual(["gpt-5.5"]);
584+
});
585+
586+
it("keeps only manifest fallback models when OpenAI discovery is unavailable", async () => {
587+
const fetchGuard: LiveModelCatalogFetchGuard = vi.fn(async () => ({
588+
response: new Response("temporarily unavailable", { status: 503 }),
589+
finalUrl: "https://api.openai.com/v1/models",
590+
release: async () => undefined,
591+
}));
592+
593+
const provider = await buildOpenAILiveProviderConfig({
594+
apiKey: "sk-openai",
595+
fetchGuard,
596+
});
597+
598+
expect(provider.models.map((model) => model.id)).toEqual(
599+
manifest.modelCatalog.providers.openai.models.map((model) => model.id),
600+
);
601+
});
602+
524603
it("skips OpenAI live discovery for custom OpenAI-compatible base URLs", async () => {
525604
const customBaseUrl = "https://example-proxy.invalid/v1";
526605
const fetchGuard: LiveModelCatalogFetchGuard = vi.fn(async () => {

‎extensions/openai/openai-provider.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,60 @@ function buildOpenAIManifestModelsForBaseUrl(baseUrl: string): ModelDefinitionCo
185185
);
186186
}
187187

188+
function buildOpenAIDiscoverablePlatformModels(baseUrl: string): ModelDefinitionConfig[] {
189+
const models = [
190+
{
191+
id: OPENAI_CHAT_LATEST_MODEL_ID,
192+
name: "Chat Latest",
193+
reasoning: false,
194+
cost: OPENAI_CHAT_LATEST_COST,
195+
contextWindow: 400_000,
196+
},
197+
{
198+
id: OPENAI_GPT_54_MODEL_ID,
199+
name: "GPT-5.4",
200+
reasoning: true,
201+
cost: OPENAI_GPT_54_COST,
202+
contextWindow: OPENAI_GPT_54_CONTEXT_TOKENS,
203+
},
204+
{
205+
id: OPENAI_GPT_54_PRO_MODEL_ID,
206+
name: "GPT-5.4 Pro",
207+
reasoning: true,
208+
cost: OPENAI_GPT_54_PRO_COST,
209+
contextWindow: OPENAI_GPT_54_PRO_CONTEXT_TOKENS,
210+
},
211+
{
212+
id: OPENAI_GPT_54_MINI_MODEL_ID,
213+
name: "GPT-5.4 Mini",
214+
reasoning: true,
215+
cost: OPENAI_GPT_54_MINI_COST,
216+
contextWindow: OPENAI_GPT_54_MINI_CONTEXT_TOKENS,
217+
},
218+
{
219+
id: OPENAI_GPT_54_NANO_MODEL_ID,
220+
name: "GPT-5.4 Nano",
221+
reasoning: true,
222+
cost: OPENAI_GPT_54_NANO_COST,
223+
contextWindow: OPENAI_GPT_54_NANO_CONTEXT_TOKENS,
224+
},
225+
] as const;
226+
227+
// First-party discovery must retain provider-owned costs and capabilities;
228+
// generic projection would otherwise surface valid models as zero-cost.
229+
return models.map(({ id, name, reasoning, cost, contextWindow }) => ({
230+
id,
231+
name,
232+
reasoning,
233+
cost,
234+
contextWindow,
235+
api: "openai-responses",
236+
baseUrl,
237+
input: id === OPENAI_GPT_54_PRO_MODEL_ID ? ["text"] : ["text", "image"],
238+
maxTokens: OPENAI_GPT_54_MAX_TOKENS,
239+
}));
240+
}
241+
188242
async function buildOpenAILiveProviderConfig(
189243
params: BuildOpenAILiveProviderConfigParams,
190244
): Promise<ModelProviderConfig> {
@@ -207,6 +261,33 @@ async function buildOpenAILiveProviderConfig(
207261
api: "openai-responses",
208262
},
209263
models,
264+
projectRows: (rows, fallback) => {
265+
const discoveredIds = new Set(
266+
rows.flatMap((row) => {
267+
if (!row || typeof row !== "object" || Array.isArray(row)) {
268+
return [];
269+
}
270+
const candidate = row as { id?: unknown; object?: unknown };
271+
if (candidate.object !== undefined && candidate.object !== "model") {
272+
return [];
273+
}
274+
const modelId = typeof candidate.id === "string" ? candidate.id.trim() : "";
275+
return modelId ? [modelId] : [];
276+
}),
277+
);
278+
const selectedIds = new Set<string>();
279+
// Discovery alone confirms account access; leave the manifest as the
280+
// advisory fallback when OpenAI cannot return an authenticated catalog.
281+
return [...fallback.models, ...buildOpenAIDiscoverablePlatformModels(baseUrl)].filter(
282+
(model) => {
283+
if (!discoveredIds.has(model.id) || selectedIds.has(model.id)) {
284+
return false;
285+
}
286+
selectedIds.add(model.id);
287+
return true;
288+
},
289+
);
290+
},
210291
apiKey: params.apiKey,
211292
discoveryApiKey: params.discoveryApiKey,
212293
fetchGuard: params.fetchGuard,

0 commit comments

Comments
 (0)