@cartanova/qgrid-cli 2.15.2 → 2.15.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/dist/application/qgrid/qgrid.frame.js +154 -8
- package/bundle/dist/application/qgrid/qgrid.types.js +6 -3
- package/bundle/dist/i18n/en.js +2 -1
- package/bundle/dist/i18n/ko.js +2 -1
- package/bundle/dist/utils/providers/openai/openai-backend-protocol.js +6 -54
- package/bundle/dist/utils/providers/openai/openai-direct-client.js +2 -2
- package/bundle/dist/utils/providers/openai/openai-dispatcher.js +47 -19
- package/bundle/dist/utils/providers/openai/openai-image-generation.js +1 -2
- package/bundle/dist/utils/providers/openai/openai-realtime-protocol.js +389 -0
- package/bundle/dist/utils/providers/openai/openai-voice-client.js +456 -0
- package/bundle/src/application/qgrid/qgrid.frame.test.ts +6 -6
- package/bundle/src/application/qgrid/qgrid.frame.ts +213 -9
- package/bundle/src/application/qgrid/qgrid.types.ts +14 -2
- package/bundle/src/application/sonamu.generated.http +2 -0
- package/bundle/src/i18n/en.ts +1 -0
- package/bundle/src/i18n/ko.ts +1 -0
- package/bundle/src/utils/providers/openai/openai-backend-protocol.test.ts +8 -69
- package/bundle/src/utils/providers/openai/openai-backend-protocol.ts +7 -86
- package/bundle/src/utils/providers/openai/openai-direct-client.test.ts +0 -15
- package/bundle/src/utils/providers/openai/openai-direct-client.ts +3 -1
- package/bundle/src/utils/providers/openai/openai-dispatcher.test.ts +19 -93
- package/bundle/src/utils/providers/openai/openai-dispatcher.ts +98 -47
- package/bundle/src/utils/providers/openai/openai-image-generation.test.ts +6 -4
- package/bundle/src/utils/providers/openai/openai-image-generation.ts +0 -6
- package/bundle/src/utils/providers/openai/openai-realtime-protocol.ts +550 -0
- package/bundle/src/utils/providers/openai/openai-voice-client.test.ts +576 -0
- package/bundle/src/utils/providers/openai/openai-voice-client.ts +738 -0
- package/bundle/web-dist/client/assets/{index-VhUpqKIg.js → index-CRdZwZsw.js} +3 -3
- package/bundle/web-dist/client/assets/{logs-BwSzVf_A.js → logs-DyLC_2Rf.js} +1 -1
- package/bundle/web-dist/client/assets/{monit-B2CVdIml.js → monit-C0hO3Fy6.js} +1 -1
- package/bundle/web-dist/client/assets/{routes-BOOXpy3Q.js → routes-DTw4jsHI.js} +1 -1
- package/bundle/web-dist/client/assets/{settings-B_a3Nr0H.js → settings-Dg-1zSMq.js} +1 -1
- package/bundle/web-dist/client/assets/{show-D5Uxcb4a.js → show-BmqUEs1O.js} +1 -1
- package/bundle/web-dist/client/assets/{tokens-zlrqnP63.js → tokens-FQ82u55y.js} +1 -1
- package/bundle/web-dist/client/assets/{use-oauth-login-flow-qR-56G2w.js → use-oauth-login-flow-BXmbUSjc.js} +1 -1
- package/bundle/web-dist/client/assets/{use-update-token-mutation-B-vcLRx-.js → use-update-token-mutation-BWlMl3qg.js} +1 -1
- package/bundle/web-dist/client/index.html +1 -1
- package/bundle/web-dist/server/assets/{logs-6289J29J.js → logs-WZMx5ErO.js} +3 -3
- package/bundle/web-dist/server/assets/{monit-YVvwJuAF.js → monit-Bf6U1Xp5.js} +2 -2
- package/bundle/web-dist/server/assets/{plus-z5Hy__3C.js → plus-BwpfWi3W.js} +1 -1
- package/bundle/web-dist/server/assets/{refresh-cw-CNm0CERy.js → refresh-cw-Xk9nLu8E.js} +1 -1
- package/bundle/web-dist/server/assets/{request-model-DIuPZZwu.js → request-model-ByadPBS5.js} +1 -1
- package/bundle/web-dist/server/assets/{routes-CCdBJqP_.js → routes-WFysbWfc.js} +3 -3
- package/bundle/web-dist/server/assets/{services.generated-CqS8DCjT.js → services.generated-Bej_fJ74.js} +2 -0
- package/bundle/web-dist/server/assets/{settings-CXwnKiMG.js → settings-DnAbfheS.js} +3 -3
- package/bundle/web-dist/server/assets/{show-G1t8dOEh.js → show-DvpdOIGn.js} +2 -2
- package/bundle/web-dist/server/assets/{tokens-C4D67FBX.js → tokens-CAc4t_kZ.js} +4 -4
- package/bundle/web-dist/server/assets/{use-oauth-login-flow-CFzShQtp.js → use-oauth-login-flow-Cxm_7Qg6.js} +4 -4
- package/bundle/web-dist/server/assets/{use-update-token-mutation-oH0O0n-g.js → use-update-token-mutation-DnxqIThp.js} +1 -1
- package/bundle/web-dist/server/entry-server.generated.js +25 -25
- package/dist/cli.js +9 -38
- package/package.json +2 -2
- package/skills/qgrid/SKILL.md +5 -5
- package/skills/qgrid/references/ai-sdk-provider-contract.md +4 -0
- package/skills/qgrid/references/decision-rationale.md +13 -3
- package/skills/qgrid/references/openai-codex-runtime.md +17 -5
- package/skills/qgrid/references/request-log-run-lifecycle.md +2 -2
- package/skills/qgrid/references/sonamu-api-web-flow.md +1 -1
- package/skills/qgrid/references/verification-and-debugging.md +3 -3
|
@@ -127,17 +127,27 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
127
127
|
expect(run).toHaveBeenCalledOnce();
|
|
128
128
|
});
|
|
129
129
|
|
|
130
|
-
it.each([
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
it.each([
|
|
131
|
+
{ background: "transparent", size: "1024x1024" },
|
|
132
|
+
{ background: "opaque", size: "1152x2560" },
|
|
133
|
+
{ background: "auto", size: "auto" },
|
|
134
|
+
] as const)("passes explicit chat-model image controls to standalone Images: %j", async (controls) => {
|
|
135
|
+
const args = QueryInput.parse({
|
|
136
|
+
model: "openai/gpt-test", prompt: "an isolated red circle", imageGeneration: true,
|
|
137
|
+
imageGenerationOptions: { ...controls, quality: "high", outputFormat: "png" },
|
|
138
|
+
});
|
|
139
|
+
const generation = { route: "codex-images" as const, model: "gpt-image-2", size: "841x1870", quality: "low" };
|
|
140
|
+
let received: OpenAIResponsesOptions | undefined;
|
|
133
141
|
const d = dispatcher((options) => {
|
|
134
|
-
|
|
135
|
-
return events({ type: "image", base64: "
|
|
142
|
+
received = options;
|
|
143
|
+
return events({ type: "image", base64: "png", mimeType: "image/png", generation }, { type: "completed", responseId: "r", model: "gpt-image-2" });
|
|
136
144
|
});
|
|
137
145
|
await d.onTokenAdded(1, "one", credentials);
|
|
138
146
|
const result = await d.generate(request({ imageGeneration: args.imageGeneration, imageGenerationOptions: args.imageGenerationOptions }));
|
|
139
|
-
|
|
140
|
-
expect(
|
|
147
|
+
const direct = buildStandaloneImageRequest(received!);
|
|
148
|
+
expect(direct.url).toMatch(/\/images\/generations$/);
|
|
149
|
+
expect(direct.body).toMatchObject({ model: "gpt-image-2", ...controls, quality: "high" });
|
|
150
|
+
expect(result).toMatchObject({ model: "gpt-image-2", requestedModel: "gpt-test", images: [{ mediaType: "image/png", generation }] });
|
|
141
151
|
});
|
|
142
152
|
|
|
143
153
|
it("rejects unsupported output formats and non-PNG transparent requests", () => {
|
|
@@ -146,18 +156,6 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
146
156
|
}
|
|
147
157
|
});
|
|
148
158
|
|
|
149
|
-
it.each(["auto", "1152x2560"])("forwards %s size hints and preserves hosted observations", async (size) => {
|
|
150
|
-
const args = QueryInput.parse({ prompt: "draw in 9:20", imageGeneration: true, imageGenerationOptions: { size } });
|
|
151
|
-
const generation = { route: "codex-responses" as const, size: "841x1870", quality: "low" };
|
|
152
|
-
const d = dispatcher((options) => {
|
|
153
|
-
expect(buildOpenAIResponsesRequest(options).tools).toContainEqual({ type: "image_generation", size });
|
|
154
|
-
return events({ type: "image", base64: "data", mimeType: "image/png", generation }, { type: "completed", responseId: "r" });
|
|
155
|
-
});
|
|
156
|
-
await d.onTokenAdded(1, "one", credentials);
|
|
157
|
-
const result = await d.generate(request({ imageGeneration: args.imageGeneration, imageGenerationOptions: args.imageGenerationOptions }));
|
|
158
|
-
expect(result.images).toMatchObject([{ generation }]);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
159
|
beforeEach(() => { refreshMock.mockReset(); });
|
|
162
160
|
|
|
163
161
|
it("refreshes a quota 401 once, shares the recovery, and uses the new credentials", async () => {
|
|
@@ -253,34 +251,6 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
253
251
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
254
252
|
});
|
|
255
253
|
|
|
256
|
-
it.each(["transparent", "opaque", "auto"] as const)(
|
|
257
|
-
"preserves %s background through validation and Responses serialization",
|
|
258
|
-
async (background) => {
|
|
259
|
-
const args = QueryInput.parse({
|
|
260
|
-
model: "openai/gpt-test",
|
|
261
|
-
prompt: "an isolated red circle",
|
|
262
|
-
imageGeneration: true,
|
|
263
|
-
imageGenerationOptions: { background, quality: "high", size: "1024x1024" },
|
|
264
|
-
});
|
|
265
|
-
let body: ReturnType<typeof buildOpenAIResponsesRequest> | undefined;
|
|
266
|
-
const d = dispatcher((options) => {
|
|
267
|
-
body = buildOpenAIResponsesRequest(options);
|
|
268
|
-
return events(
|
|
269
|
-
{ type: "image", id: "i", base64: "png", mimeType: "image/png" },
|
|
270
|
-
{ type: "completed", responseId: "r" },
|
|
271
|
-
);
|
|
272
|
-
});
|
|
273
|
-
await d.onTokenAdded(1, "one", credentials);
|
|
274
|
-
await d.generate(request({
|
|
275
|
-
imageGeneration: args.imageGeneration,
|
|
276
|
-
imageGenerationOptions: args.imageGenerationOptions,
|
|
277
|
-
}));
|
|
278
|
-
expect(body?.tools).toEqual([{
|
|
279
|
-
type: "image_generation", background, quality: "high", size: "1024x1024",
|
|
280
|
-
}]);
|
|
281
|
-
},
|
|
282
|
-
);
|
|
283
|
-
|
|
284
254
|
it("rejects unsupported image backgrounds", () => {
|
|
285
255
|
expect(QueryInput.safeParse({
|
|
286
256
|
prompt: "a circle", imageGeneration: true,
|
|
@@ -418,7 +388,7 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
418
388
|
expect(onError).toHaveBeenCalledWith(failure);
|
|
419
389
|
});
|
|
420
390
|
|
|
421
|
-
it("returns multiple
|
|
391
|
+
it("returns multiple images from one image request", async () => {
|
|
422
392
|
const run = vi.fn(() => events(
|
|
423
393
|
{ type: "image", id: "first", base64: "first", mimeType: "image/png" },
|
|
424
394
|
{ type: "image", id: "second", base64: "second", mimeType: "image/webp" },
|
|
@@ -431,47 +401,6 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
431
401
|
expect(run).toHaveBeenCalledTimes(1);
|
|
432
402
|
});
|
|
433
403
|
|
|
434
|
-
it.each([true, false])("preserves aggregate hosted image usage separately from driver usage (driver usage: %s)", async (withDriverUsage) => {
|
|
435
|
-
const imageUsage = {
|
|
436
|
-
input_tokens: 2260,
|
|
437
|
-
input_tokens_details: { image_tokens: 2012, text_tokens: 248 },
|
|
438
|
-
output_tokens: 1415,
|
|
439
|
-
output_tokens_details: { image_tokens: 1415, text_tokens: 0 },
|
|
440
|
-
total_tokens: 3675,
|
|
441
|
-
};
|
|
442
|
-
const rawEvents = [
|
|
443
|
-
...["first", "second"].map((id) => ({ type: "response.output_item.done", item: {
|
|
444
|
-
id, type: "image_generation_call", result: "UklGRgAAAABXRUJQ",
|
|
445
|
-
size: "1037x1516", quality: "medium", background: "opaque", output_format: "webp",
|
|
446
|
-
} })),
|
|
447
|
-
{ type: "response.completed", response: {
|
|
448
|
-
id: "r", model: "gpt-6-luna",
|
|
449
|
-
...(withDriverUsage ? { usage: { input_tokens: 10, output_tokens: 3, total_tokens: 13 } } : {}),
|
|
450
|
-
tool_usage: { image_gen: imageUsage },
|
|
451
|
-
tools: [{ type: "image_generation", model: "gpt-image-2-codex", quality: "auto", size: "auto", background: "auto", output_format: "webp", n: 1 }],
|
|
452
|
-
} },
|
|
453
|
-
];
|
|
454
|
-
const d = dispatcher((options) => {
|
|
455
|
-
expect(buildOpenAIResponsesRequest(options).tools).toEqual([{
|
|
456
|
-
type: "image_generation", quality: "high", size: "1024x1536", output_format: "webp",
|
|
457
|
-
}]);
|
|
458
|
-
return events(...rawEvents.map(normalizeOpenAIEvent).filter((event): event is OpenAINormalizedEvent => event !== undefined));
|
|
459
|
-
});
|
|
460
|
-
await d.onTokenAdded(1, "one", credentials);
|
|
461
|
-
const result = await d.generate(request({ model: "gpt-6-luna", imageGeneration: true,
|
|
462
|
-
imageGenerationOptions: { quality: "high", size: "1024x1536", outputFormat: "webp" },
|
|
463
|
-
}));
|
|
464
|
-
expect(result.usage).toMatchObject({
|
|
465
|
-
inputTokens: withDriverUsage ? 10 : 0,
|
|
466
|
-
outputTokens: withDriverUsage ? 3 : 0,
|
|
467
|
-
totalTokens: withDriverUsage ? 13 : 0,
|
|
468
|
-
});
|
|
469
|
-
const generation = { route: "codex-responses", model: "gpt-image-2-codex", size: "1037x1516", quality: "medium", background: "opaque" };
|
|
470
|
-
expect(result.images?.map((image) => image.generation)).toEqual([
|
|
471
|
-
{ ...generation, usage: imageUsage }, generation,
|
|
472
|
-
]);
|
|
473
|
-
});
|
|
474
|
-
|
|
475
404
|
it("registers only the internal image function and allows text-only replies", async () => {
|
|
476
405
|
const run = vi.fn((options: OpenAIResponsesOptions) => {
|
|
477
406
|
const body = buildOpenAIResponsesRequest(options);
|
|
@@ -500,7 +429,7 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
500
429
|
expect(result.images).toBeUndefined();
|
|
501
430
|
});
|
|
502
431
|
|
|
503
|
-
it.each(["zero-usage", "model-config"])("
|
|
432
|
+
it.each(["zero-usage", "model-config"])("keeps automatic text-only replies when completed responses carry %s image metadata", async (metadata) => {
|
|
504
433
|
const completed = normalizeOpenAIEvent({
|
|
505
434
|
type: "response.completed",
|
|
506
435
|
response: {
|
|
@@ -517,7 +446,6 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
517
446
|
output: [{ type: "message" }],
|
|
518
447
|
},
|
|
519
448
|
});
|
|
520
|
-
expect(completed).toMatchObject({ type: "completed", imageGeneration: { route: "codex-responses" } });
|
|
521
449
|
const run = vi.fn(() => events({ type: "text-delta", text: "hello" }, completed!));
|
|
522
450
|
const d = dispatcher(run);
|
|
523
451
|
await d.onTokenAdded(1, "one", credentials);
|
|
@@ -613,8 +541,6 @@ describe("OpenAIDispatcher direct runtime", () => {
|
|
|
613
541
|
[{ ...done, item: { ...item, call_id: "" } }],
|
|
614
542
|
[{ ...done, item: { ...item, name: "unexpected" } }],
|
|
615
543
|
[done, { type: "image", base64: "png", mimeType: "image/png" }],
|
|
616
|
-
[{ type: "output-item", item: { type: "image_generation_call" }, completed: false }],
|
|
617
|
-
[{ type: "completed", responseId: "native", imageGeneration: { route: "codex-responses", usage: { input_tokens: 1, output_tokens: 0, total_tokens: 1 } } }],
|
|
618
544
|
];
|
|
619
545
|
for (const stream of badEvents) {
|
|
620
546
|
const run = vi.fn(() => events(...stream, { type: "completed", responseId: "driver" }));
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { getLogger } from "@logtape/logtape";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
type ImageGenerationMetadata,
|
|
6
|
-
QuotaThresholdExceededError,
|
|
7
|
-
} from "../../../application/qgrid/qgrid.types";
|
|
4
|
+
import { QuotaThresholdExceededError } from "../../../application/qgrid/qgrid.types";
|
|
8
5
|
import { TokenModel } from "../../../application/token/token.model";
|
|
9
6
|
import { type OpenAICredentials } from "../../../application/token/token.types";
|
|
10
7
|
import { isOpenAICredentials } from "../common/credentials";
|
|
@@ -26,10 +23,22 @@ import {
|
|
|
26
23
|
type OpenAIResponseItem,
|
|
27
24
|
type OpenAIResponsesOptions,
|
|
28
25
|
} from "./openai-backend-protocol";
|
|
29
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
OpenAIDirectClient,
|
|
28
|
+
type OpenAIDirectClientOptions,
|
|
29
|
+
type OpenAIDirectCredentials,
|
|
30
|
+
} from "./openai-direct-client";
|
|
30
31
|
import { readOpenAIQuotaUsage, type OpenAIRateLimitsWithMeta } from "./openai-quota";
|
|
31
32
|
import { handleChatgptAuthTokensRefresh } from "./openai-refresh";
|
|
32
33
|
import { type OpenAITransportKind, resolveOpenAITransportKind } from "./openai-transport-config";
|
|
34
|
+
import {
|
|
35
|
+
isUnauthorizedRealtimeError,
|
|
36
|
+
type OpenAISpeakRequest,
|
|
37
|
+
type OpenAISpeechResult,
|
|
38
|
+
type OpenAITranscribeRequest,
|
|
39
|
+
type OpenAITranscription,
|
|
40
|
+
OpenAIVoiceClient,
|
|
41
|
+
} from "./openai-voice-client";
|
|
33
42
|
|
|
34
43
|
const logger = getLogger(["qgrid", "openai-dispatcher"]);
|
|
35
44
|
|
|
@@ -115,6 +124,19 @@ type ClientEntry = {
|
|
|
115
124
|
export interface OpenAIDispatcherDependencies {
|
|
116
125
|
clientFactory?: OpenAIDirectClientFactory;
|
|
117
126
|
fetch?: typeof fetch;
|
|
127
|
+
voiceClient?: OpenAIVoiceClient;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Realtime voice call with the media-endpoint token selection contract. */
|
|
131
|
+
export type OpenAIVoiceSelection = {
|
|
132
|
+
preferredTokenId?: number;
|
|
133
|
+
requirePreferredToken?: boolean;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export interface OpenAIVoiceDispatchResult<T> {
|
|
137
|
+
result: T;
|
|
138
|
+
tokenId: number;
|
|
139
|
+
tokenName: string;
|
|
118
140
|
}
|
|
119
141
|
|
|
120
142
|
function abortError(signal: AbortSignal): Error {
|
|
@@ -293,6 +315,7 @@ export class OpenAIDispatcher implements ProviderDispatcher {
|
|
|
293
315
|
readonly selector = new SmoothWeightedRoundRobin();
|
|
294
316
|
readonly clientFactory: OpenAIDirectClientFactory;
|
|
295
317
|
readonly fetchImpl: typeof fetch;
|
|
318
|
+
readonly voiceClient: OpenAIVoiceClient;
|
|
296
319
|
readonly quotaBlocked = new Set<number>();
|
|
297
320
|
inFlight = 0;
|
|
298
321
|
|
|
@@ -304,6 +327,75 @@ export class OpenAIDispatcher implements ProviderDispatcher {
|
|
|
304
327
|
this.clientFactory =
|
|
305
328
|
dependencies.clientFactory ?? ((options) => new OpenAIDirectClient(options));
|
|
306
329
|
this.fetchImpl = dependencies.fetch ?? fetch;
|
|
330
|
+
this.voiceClient = dependencies.voiceClient ?? new OpenAIVoiceClient({ fetch: this.fetchImpl });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ---- Realtime voice (speech-to-text, text-to-speech) ----------------------
|
|
334
|
+
|
|
335
|
+
async transcribe(
|
|
336
|
+
request: OpenAITranscribeRequest & OpenAIVoiceSelection,
|
|
337
|
+
): Promise<OpenAIVoiceDispatchResult<OpenAITranscription>> {
|
|
338
|
+
const { preferredTokenId, requirePreferredToken, ...voiceRequest } = request;
|
|
339
|
+
|
|
340
|
+
return this.runVoice(
|
|
341
|
+
{ preferredTokenId, requirePreferredToken },
|
|
342
|
+
request.signal,
|
|
343
|
+
(credentials) => this.voiceClient.transcribe(credentials, voiceRequest),
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async speak(
|
|
348
|
+
request: OpenAISpeakRequest & OpenAIVoiceSelection,
|
|
349
|
+
): Promise<OpenAIVoiceDispatchResult<OpenAISpeechResult>> {
|
|
350
|
+
const { preferredTokenId, requirePreferredToken, ...voiceRequest } = request;
|
|
351
|
+
|
|
352
|
+
return this.runVoice(
|
|
353
|
+
{ preferredTokenId, requirePreferredToken },
|
|
354
|
+
request.signal,
|
|
355
|
+
(credentials) => this.voiceClient.speak(credentials, voiceRequest),
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Selects a token, runs the realtime call, and on one 401 refreshes that token's credentials and
|
|
361
|
+
* retries once (as the Responses transports do). Any other failure propagates: a realtime
|
|
362
|
+
* session that failed may already have consumed audio tokens.
|
|
363
|
+
*/
|
|
364
|
+
async runVoice<T>(
|
|
365
|
+
selection: OpenAIVoiceSelection,
|
|
366
|
+
signal: AbortSignal | undefined,
|
|
367
|
+
call: (credentials: OpenAIDirectCredentials) => Promise<T>,
|
|
368
|
+
): Promise<OpenAIVoiceDispatchResult<T>> {
|
|
369
|
+
const active = signal ?? new AbortController().signal;
|
|
370
|
+
|
|
371
|
+
const selected = await this.selectToken(
|
|
372
|
+
selection.preferredTokenId,
|
|
373
|
+
selection.requirePreferredToken ?? false,
|
|
374
|
+
active,
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
const { tokenId, metadata } = selected;
|
|
378
|
+
|
|
379
|
+
let credentials: OpenAIDirectCredentials = {
|
|
380
|
+
accessToken: metadata.credentials.accessToken,
|
|
381
|
+
accountId: metadata.credentials.accountId,
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
for (let attempt = 0; ; attempt++) {
|
|
385
|
+
try {
|
|
386
|
+
const result = await call(credentials);
|
|
387
|
+
this.invalidateRateLimitsCache(tokenId);
|
|
388
|
+
|
|
389
|
+
return { result, tokenId, tokenName: metadata.name };
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (attempt === 0 && error instanceof Error && isUnauthorizedRealtimeError(error)) {
|
|
392
|
+
credentials = await this.refreshCredentials(tokenId);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
307
399
|
}
|
|
308
400
|
|
|
309
401
|
async start(): Promise<void> {
|
|
@@ -578,14 +670,9 @@ export class OpenAIDispatcher implements ProviderDispatcher {
|
|
|
578
670
|
streamedMessageIds.add(itemId);
|
|
579
671
|
}
|
|
580
672
|
} else if (event.type === "image") {
|
|
581
|
-
if (auto)
|
|
582
|
-
throw new ImageGenerationError("incomplete", "Unexpected hosted image generation");
|
|
673
|
+
if (auto) throw new ImageGenerationError("incomplete", "Unexpected image output");
|
|
583
674
|
imageAttempted = true;
|
|
584
675
|
images.push(generatedImageFrom(event));
|
|
585
|
-
} else if (event.type === "output-item" && event.item.type === "image_generation_call") {
|
|
586
|
-
if (auto)
|
|
587
|
-
throw new ImageGenerationError("incomplete", "Unexpected hosted image generation");
|
|
588
|
-
imageAttempted = true;
|
|
589
676
|
} else if (auto && event.type === "output-item" && event.item.type === "function_call") {
|
|
590
677
|
if (event.item.name !== IMAGE_TOOL) {
|
|
591
678
|
throw new ImageGenerationError("incomplete", "Unexpected automatic image tool call");
|
|
@@ -632,17 +719,6 @@ export class OpenAIDispatcher implements ProviderDispatcher {
|
|
|
632
719
|
num_last_images_to_include: args.num_last_images_to_include,
|
|
633
720
|
});
|
|
634
721
|
} else if (event.type === "completed") {
|
|
635
|
-
const imageUsage = event.imageGeneration?.usage;
|
|
636
|
-
|
|
637
|
-
// The backend also reports all-zero image usage for text-only responses.
|
|
638
|
-
if (
|
|
639
|
-
auto &&
|
|
640
|
-
imageUsage &&
|
|
641
|
-
(imageUsage.input_tokens > 0 ||
|
|
642
|
-
imageUsage.output_tokens > 0 ||
|
|
643
|
-
imageUsage.total_tokens > 0)
|
|
644
|
-
)
|
|
645
|
-
throw new ImageGenerationError("incomplete", "Unexpected hosted image generation");
|
|
646
722
|
driverCompleted = true;
|
|
647
723
|
servingModel = event.model ?? servingModel;
|
|
648
724
|
|
|
@@ -655,31 +731,6 @@ export class OpenAIDispatcher implements ProviderDispatcher {
|
|
|
655
731
|
reasoningOutputTokens: event.usage.reasoningTokens,
|
|
656
732
|
};
|
|
657
733
|
}
|
|
658
|
-
|
|
659
|
-
if (event.imageGeneration) {
|
|
660
|
-
const hostedImages = images.filter(
|
|
661
|
-
(image) => image.generation?.route === "codex-responses",
|
|
662
|
-
);
|
|
663
|
-
|
|
664
|
-
for (const [index, image] of hostedImages.entries()) {
|
|
665
|
-
const generation: ImageGenerationMetadata = {
|
|
666
|
-
route: "codex-responses",
|
|
667
|
-
...image.generation,
|
|
668
|
-
};
|
|
669
|
-
|
|
670
|
-
// The image's own model wins; the response-level model only fills a gap.
|
|
671
|
-
if (generation.model === undefined && event.imageGeneration.model) {
|
|
672
|
-
generation.model = event.imageGeneration.model;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
// Tool usage covers the whole response, not each returned image.
|
|
676
|
-
if (index === 0 && event.imageGeneration.usage) {
|
|
677
|
-
generation.usage = event.imageGeneration.usage;
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
image.generation = generation;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
734
|
} else if (event.type === "error") {
|
|
684
735
|
if (req.imageGeneration)
|
|
685
736
|
throw new ImageGenerationError("incomplete", event.error.message);
|
|
@@ -152,9 +152,11 @@ describe("standalone transparent images", () => {
|
|
|
152
152
|
});
|
|
153
153
|
|
|
154
154
|
describe("standalone image model", () => {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
// Chat models use the same route: hosted Responses images reject driver reasoning effort.
|
|
156
|
+
it.each(["gpt-image-2", "gpt-6-luna"].flatMap((model) =>
|
|
157
|
+
[true, { background: "opaque" }, { background: "auto" }].map((imageGeneration) => [model, imageGeneration] as const)))(
|
|
158
|
+
"generates ordinary PNGs for %s directly without the Responses transport (%j)",
|
|
159
|
+
async (model, imageGeneration) => {
|
|
158
160
|
const stream = vi.fn();
|
|
159
161
|
const fetchMock = vi.fn<typeof fetch>(async () => Response.json({
|
|
160
162
|
data: [{ b64_json: png([255, 255]) }],
|
|
@@ -165,7 +167,7 @@ describe("standalone image model", () => {
|
|
|
165
167
|
transport: { stream },
|
|
166
168
|
});
|
|
167
169
|
const events = await Array.fromAsync(client.responses({
|
|
168
|
-
...options, model: "
|
|
170
|
+
...options, model, imageGeneration, reasoning: { effort: "high" },
|
|
169
171
|
}));
|
|
170
172
|
expect(stream).not.toHaveBeenCalled();
|
|
171
173
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
@@ -44,12 +44,6 @@ export function buildStandaloneImageRequest(options: OpenAIResponsesOptions) {
|
|
|
44
44
|
|
|
45
45
|
const controls = options.imageGeneration === true ? {} : options.imageGeneration;
|
|
46
46
|
|
|
47
|
-
if (options.model !== "gpt-image-2" && controls.background !== "transparent") {
|
|
48
|
-
throw new OpenAIProtocolError(
|
|
49
|
-
"Standalone images require an explicit transparent background request",
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
47
|
if (controls.output_format && controls.output_format !== "png") {
|
|
54
48
|
throw new OpenAIProtocolError("Standalone image generation only supports PNG output");
|
|
55
49
|
}
|