ima2-gen 1.1.21 → 1.1.22

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.
Files changed (208) hide show
  1. package/README.md +30 -4
  2. package/bin/ima2.js +14 -4
  3. package/bin/lib/platform.js +34 -5
  4. package/docs/README.ko.md +31 -0
  5. package/lib/agentQueueWorker.js +6 -0
  6. package/lib/agentRuntime.js +3 -2
  7. package/lib/atomicWrite.js +14 -0
  8. package/lib/grokProxyLauncher.js +5 -3
  9. package/lib/inflight.js +1 -1
  10. package/lib/oauthLauncher.js +5 -0
  11. package/lib/videoFrameExtract.js +3 -3
  12. package/package.json +5 -7
  13. package/routes/edit.js +2 -1
  14. package/routes/generate.js +4 -3
  15. package/routes/health.js +4 -3
  16. package/routes/multimode.js +2 -1
  17. package/routes/video.js +4 -2
  18. package/server.js +29 -2
  19. package/ui/dist/.vite/manifest.json +12 -12
  20. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-COxQ5TjU.js} +1 -1
  21. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-B0OkcuVz.js} +1 -1
  22. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BSsclEBh.js} +1 -1
  23. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DpC9A5Rz.js} +1 -1
  24. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-CVwT0rLd.js} +2 -2
  25. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BDCkRCRs.js} +1 -1
  26. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-QoKbZD83.js} +1 -1
  27. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-BhFgeKnY.js} +2 -2
  28. package/ui/dist/assets/SettingsWorkspace-CfjrlH5R.js +1 -0
  29. package/ui/dist/assets/index-C-mur7pa.css +1 -0
  30. package/ui/dist/assets/index-CCP5nUOj.js +42 -0
  31. package/ui/dist/assets/{index-31uVIdt4.js → index-Cxhzi3bs.js} +1 -1
  32. package/ui/dist/index.html +2 -2
  33. package/bin/commands/annotate.ts +0 -119
  34. package/bin/commands/cancel.ts +0 -48
  35. package/bin/commands/canvas-versions.ts +0 -80
  36. package/bin/commands/capabilities.ts +0 -110
  37. package/bin/commands/cardnews.ts +0 -249
  38. package/bin/commands/comfy.ts +0 -54
  39. package/bin/commands/config.ts +0 -186
  40. package/bin/commands/defaults.ts +0 -192
  41. package/bin/commands/doctor.ts +0 -202
  42. package/bin/commands/edit.ts +0 -150
  43. package/bin/commands/gen.ts +0 -214
  44. package/bin/commands/grok.ts +0 -90
  45. package/bin/commands/history.ts +0 -146
  46. package/bin/commands/ls.ts +0 -64
  47. package/bin/commands/metadata.ts +0 -39
  48. package/bin/commands/multimode.ts +0 -196
  49. package/bin/commands/node.ts +0 -166
  50. package/bin/commands/observability.ts +0 -176
  51. package/bin/commands/ping.ts +0 -31
  52. package/bin/commands/prompt-sub/build.ts +0 -101
  53. package/bin/commands/prompt.ts +0 -492
  54. package/bin/commands/ps.ts +0 -81
  55. package/bin/commands/session.ts +0 -266
  56. package/bin/commands/show.ts +0 -72
  57. package/bin/commands/skill.ts +0 -70
  58. package/bin/commands/video.ts +0 -442
  59. package/bin/ima2.ts +0 -430
  60. package/bin/lib/args.ts +0 -92
  61. package/bin/lib/browser-id.ts +0 -16
  62. package/bin/lib/client.ts +0 -122
  63. package/bin/lib/config-store.ts +0 -120
  64. package/bin/lib/destructive-confirm.ts +0 -19
  65. package/bin/lib/doctor-checks.ts +0 -91
  66. package/bin/lib/error-hints.ts +0 -23
  67. package/bin/lib/files.ts +0 -39
  68. package/bin/lib/output.ts +0 -73
  69. package/bin/lib/platform.ts +0 -99
  70. package/bin/lib/recover-output.ts +0 -139
  71. package/bin/lib/sse.ts +0 -73
  72. package/bin/lib/star-prompt.ts +0 -97
  73. package/bin/lib/storage-doctor.ts +0 -39
  74. package/bin/lib/ui-build.ts +0 -85
  75. package/config.ts +0 -354
  76. package/lib/agentCommandParser.ts +0 -69
  77. package/lib/agentGenerationPlanner.ts +0 -273
  78. package/lib/agentQuestionResponder.ts +0 -266
  79. package/lib/agentQueueStore.ts +0 -270
  80. package/lib/agentQueueWorker.ts +0 -89
  81. package/lib/agentRuntime.ts +0 -604
  82. package/lib/agentSettings.ts +0 -72
  83. package/lib/agentStore.ts +0 -422
  84. package/lib/agentStoreRows.ts +0 -136
  85. package/lib/agentTypes.ts +0 -154
  86. package/lib/apiCachePolicy.ts +0 -11
  87. package/lib/assetLifecycle.ts +0 -146
  88. package/lib/canvasVersionStore.ts +0 -223
  89. package/lib/capabilities.ts +0 -126
  90. package/lib/cardNewsGenerator.ts +0 -271
  91. package/lib/cardNewsJobStore.ts +0 -142
  92. package/lib/cardNewsManifestStore.ts +0 -154
  93. package/lib/cardNewsPlanner.ts +0 -236
  94. package/lib/cardNewsPlannerClient.ts +0 -155
  95. package/lib/cardNewsPlannerPrompt.ts +0 -62
  96. package/lib/cardNewsPlannerSchema.ts +0 -321
  97. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  98. package/lib/cardNewsTemplateStore.ts +0 -252
  99. package/lib/codexDetect.ts +0 -71
  100. package/lib/comfyBridge.ts +0 -235
  101. package/lib/composerSnapshot.ts +0 -33
  102. package/lib/configKeys.ts +0 -62
  103. package/lib/db.ts +0 -295
  104. package/lib/errInfo.ts +0 -43
  105. package/lib/errorClassify.ts +0 -100
  106. package/lib/generationCancel.ts +0 -28
  107. package/lib/generationErrors.ts +0 -238
  108. package/lib/grokImageAdapter.ts +0 -513
  109. package/lib/grokMultimodeAdapter.ts +0 -84
  110. package/lib/grokProxyLauncher.ts +0 -153
  111. package/lib/grokRuntime.ts +0 -23
  112. package/lib/grokSizeMapper.ts +0 -71
  113. package/lib/grokVideoAdapter.ts +0 -458
  114. package/lib/grokVideoCanvas.ts +0 -26
  115. package/lib/grokVideoDownload.ts +0 -59
  116. package/lib/grokVideoPlannerPrompt.ts +0 -67
  117. package/lib/historyIndex.ts +0 -51
  118. package/lib/historyList.ts +0 -181
  119. package/lib/imageMetadata.ts +0 -113
  120. package/lib/imageMetadataStore.ts +0 -67
  121. package/lib/imageModels.ts +0 -165
  122. package/lib/inflight.ts +0 -281
  123. package/lib/localImportStore.ts +0 -114
  124. package/lib/logger.ts +0 -161
  125. package/lib/nodeStore.ts +0 -91
  126. package/lib/oauthLauncher.ts +0 -94
  127. package/lib/oauthNormalize.ts +0 -30
  128. package/lib/oauthProxy/errors.ts +0 -128
  129. package/lib/oauthProxy/generators.ts +0 -494
  130. package/lib/oauthProxy/index.ts +0 -28
  131. package/lib/oauthProxy/prompts.ts +0 -123
  132. package/lib/oauthProxy/references.ts +0 -45
  133. package/lib/oauthProxy/runtime.ts +0 -115
  134. package/lib/oauthProxy/streams.ts +0 -232
  135. package/lib/oauthProxy/types.ts +0 -9
  136. package/lib/oauthProxy.ts +0 -3
  137. package/lib/openDirectory.ts +0 -47
  138. package/lib/pngInfo.ts +0 -26
  139. package/lib/promptBuilder/attachments.ts +0 -74
  140. package/lib/promptBuilder/client.ts +0 -130
  141. package/lib/promptBuilder/constants.ts +0 -9
  142. package/lib/promptBuilder/context.ts +0 -36
  143. package/lib/promptBuilder/errors.ts +0 -12
  144. package/lib/promptBuilder/requestSchema.ts +0 -56
  145. package/lib/promptBuilder/responseParser.ts +0 -219
  146. package/lib/promptBuilder/systemPrompt.ts +0 -135
  147. package/lib/promptBuilder/transport.ts +0 -94
  148. package/lib/promptBuilder/types.ts +0 -109
  149. package/lib/promptImport/curatedSources.ts +0 -141
  150. package/lib/promptImport/discoveryRegistry.ts +0 -329
  151. package/lib/promptImport/errors.ts +0 -18
  152. package/lib/promptImport/githubDiscovery.ts +0 -309
  153. package/lib/promptImport/githubFolder.ts +0 -397
  154. package/lib/promptImport/githubSource.ts +0 -257
  155. package/lib/promptImport/gptImageHints.ts +0 -70
  156. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  157. package/lib/promptImport/promptIndex.ts +0 -326
  158. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  159. package/lib/promptImport/types.ts +0 -103
  160. package/lib/promptSafetyPolicy.ts +0 -5
  161. package/lib/providerOptions.ts +0 -56
  162. package/lib/referenceImageCompress.ts +0 -84
  163. package/lib/refs.ts +0 -133
  164. package/lib/requestLogger.ts +0 -49
  165. package/lib/responsesDoctor.ts +0 -456
  166. package/lib/responsesErrors.ts +0 -83
  167. package/lib/responsesFallback.ts +0 -114
  168. package/lib/responsesImageAdapter.ts +0 -466
  169. package/lib/responsesParse.ts +0 -452
  170. package/lib/responsesTools.ts +0 -28
  171. package/lib/runtimeContext.ts +0 -146
  172. package/lib/runtimePorts.ts +0 -105
  173. package/lib/sessionStore.ts +0 -308
  174. package/lib/storageMigration.ts +0 -310
  175. package/lib/styleSheet.ts +0 -139
  176. package/lib/systemTrash.ts +0 -20
  177. package/lib/videoContinuity.ts +0 -180
  178. package/lib/videoFrameExtract.ts +0 -78
  179. package/lib/videoSeriesChain.ts +0 -29
  180. package/lib/visibleTextLanguagePolicy.ts +0 -7
  181. package/routes/agent.ts +0 -308
  182. package/routes/annotations.ts +0 -118
  183. package/routes/canvasVersions.ts +0 -69
  184. package/routes/capabilities.ts +0 -18
  185. package/routes/cardNews.ts +0 -211
  186. package/routes/comfy.ts +0 -43
  187. package/routes/edit.ts +0 -352
  188. package/routes/generate.ts +0 -492
  189. package/routes/grok.ts +0 -24
  190. package/routes/health.ts +0 -123
  191. package/routes/history.ts +0 -221
  192. package/routes/imageImport.ts +0 -37
  193. package/routes/index.ts +0 -52
  194. package/routes/metadata.ts +0 -77
  195. package/routes/multimode.ts +0 -499
  196. package/routes/nodes.ts +0 -578
  197. package/routes/promptBuilder.ts +0 -37
  198. package/routes/promptImport.ts +0 -379
  199. package/routes/prompts.ts +0 -428
  200. package/routes/quota.ts +0 -89
  201. package/routes/sessions.ts +0 -317
  202. package/routes/storage.ts +0 -47
  203. package/routes/video.ts +0 -300
  204. package/routes/videoExtended.ts +0 -284
  205. package/server.ts +0 -293
  206. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  207. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  208. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,83 +0,0 @@
1
- import type { ParsedResponsesResult } from "./responsesParse.js";
2
-
3
- const RESPONSES_ERROR_MARKER = "ima2ResponsesError";
4
-
5
- export const RESPONSE_DIAGNOSTIC_CODES = new Set([
6
- "STREAM_PARSE_FAILED",
7
- "IMAGE_TOOL_NOT_CALLED",
8
- "WEB_SEARCH_ONLY_RESPONSE",
9
- "IMAGE_TOOL_FAILED",
10
- "IMAGE_TOOL_COMPLETED_WITHOUT_RESULT",
11
- "OAUTH_IMAGE_CAPABILITY_UNAVAILABLE",
12
- "RESPONSES_STREAM_ERROR",
13
- ]);
14
-
15
- export interface EmptyResponseMeta {
16
- provider?: string;
17
- model?: string;
18
- toolTypes?: string[];
19
- toolChoiceKind?: string;
20
- quality?: string;
21
- size?: string;
22
- moderation?: string;
23
- webSearchEnabled?: boolean;
24
- refsCount?: number;
25
- inputImageCount?: number;
26
- promptChars?: number;
27
- retryKind?: string;
28
- initialEventCount?: number;
29
- initialEventTypes?: Record<string, number>;
30
- referencesDroppedOnRetry?: boolean;
31
- developerPromptDroppedOnRetry?: boolean;
32
- webSearchDroppedOnRetry?: boolean;
33
- fallbackEventCount?: number;
34
- fallbackEventTypes?: Record<string, number>;
35
- fallbackImageCallSeen?: boolean;
36
- fallbackImageResultCount?: number;
37
- }
38
-
39
- interface ResponsesError extends Error {
40
- status: number;
41
- code: string;
42
- [key: string]: unknown;
43
- }
44
-
45
- function diagnosticReason(code: string): string | null {
46
- return code === "EMPTY_RESPONSE" ? null : code.toLowerCase();
47
- }
48
-
49
- function messageForCode(code: string, fallback: string) {
50
- if (code === "STREAM_PARSE_FAILED") return "Responses image stream could not be parsed.";
51
- if (code === "WEB_SEARCH_ONLY_RESPONSE") return "Responses called web search but not the image tool.";
52
- if (code === "IMAGE_TOOL_NOT_CALLED") return "Responses completed without calling the image tool.";
53
- if (code === "IMAGE_TOOL_FAILED") return "Responses image tool call failed.";
54
- if (code === "IMAGE_TOOL_COMPLETED_WITHOUT_RESULT") return "Responses image tool completed without image data.";
55
- return fallback;
56
- }
57
-
58
- export function classifyNoImageResponse(result: ParsedResponsesResult): string {
59
- const diagnostics = result.diagnostics;
60
- const bytesRead = Number(diagnostics.streamStats.bytesRead);
61
- if (Number.isFinite(bytesRead) && bytesRead > 0 && result.eventCount === 0) return "STREAM_PARSE_FAILED";
62
- if (diagnostics.imageCallFailed) return "IMAGE_TOOL_FAILED";
63
- if (diagnostics.imageCallCompleted && diagnostics.imageResultCount === 0) return "IMAGE_TOOL_COMPLETED_WITHOUT_RESULT";
64
- if (!diagnostics.imageCallSeen && (result.webSearchCalls > 0 || diagnostics.webSearchCallSeen)) return "WEB_SEARCH_ONLY_RESPONSE";
65
- if (!diagnostics.imageCallSeen && diagnostics.messageOutputSeen) return "IMAGE_TOOL_NOT_CALLED";
66
- return "EMPTY_RESPONSE";
67
- }
68
-
69
- export function emptyResponseError(message: string, result: ParsedResponsesResult, meta: EmptyResponseMeta): ResponsesError {
70
- const code = classifyNoImageResponse(result);
71
- const err = new Error(messageForCode(code, message)) as ResponsesError;
72
- err.status = 422;
73
- err.code = code;
74
- err.eventCount = result.eventCount;
75
- err.eventTypes = result.eventTypes;
76
- err.webSearchCalls = result.webSearchCalls;
77
- err.responseDiagnostics = result.diagnostics;
78
- Object.assign(err, meta);
79
- const reason = diagnosticReason(code);
80
- if (reason) err.diagnosticReason = reason;
81
- Object.defineProperty(err, RESPONSES_ERROR_MARKER, { value: true });
82
- return err;
83
- }
@@ -1,114 +0,0 @@
1
- import { logEvent } from "./logger.js";
2
- import type { ParsedResponsesResult } from "./responsesParse.js";
3
- import type { RouteRuntimeContext } from "./runtimeContext.js";
4
- import { imageToolChoice, tools } from "./responsesTools.js";
5
- import { emptyResponseError } from "./responsesErrors.js";
6
- import { buildUserTextPrompt } from "./oauthProxy.js";
7
-
8
- type PostResponses = (args: {
9
- ctx: RouteRuntimeContext;
10
- provider: string | undefined;
11
- scope: string;
12
- payload: unknown;
13
- requestId?: string | null;
14
- maxImages?: number;
15
- signal?: AbortSignal | null;
16
- }) => Promise<ParsedResponsesResult>;
17
-
18
- export async function retryPromptOnlyJsonImage({
19
- postResponses,
20
- ctx,
21
- provider,
22
- prompt,
23
- mode,
24
- model,
25
- quality,
26
- size,
27
- moderation,
28
- requestId,
29
- signal,
30
- initial,
31
- referencesDroppedOnRetry,
32
- webSearchDroppedOnRetry,
33
- reasoningEffort,
34
- }: {
35
- postResponses: PostResponses;
36
- ctx: RouteRuntimeContext;
37
- provider: string | undefined;
38
- prompt: string | undefined;
39
- mode: string;
40
- model: string;
41
- quality?: string;
42
- size?: string;
43
- moderation?: string;
44
- requestId: string | null;
45
- signal?: AbortSignal | null;
46
- initial: ParsedResponsesResult;
47
- referencesDroppedOnRetry: boolean;
48
- webSearchDroppedOnRetry: boolean;
49
- reasoningEffort?: string;
50
- }) {
51
- if (provider === "api") return null;
52
- const retryKind = "prompt_only_json_image_tool";
53
- const retryMeta = {
54
- retryKind,
55
- initialEventCount: initial.eventCount,
56
- initialEventTypes: initial.eventTypes,
57
- referencesDroppedOnRetry,
58
- developerPromptDroppedOnRetry: true,
59
- webSearchDroppedOnRetry,
60
- };
61
- logEvent("oauth", "retry_json", { requestId, ...retryMeta });
62
- let retry: ParsedResponsesResult;
63
- try {
64
- retry = await postResponses({
65
- ctx,
66
- provider,
67
- scope: "oauth-fallback",
68
- requestId,
69
- maxImages: 1,
70
- signal,
71
- payload: {
72
- model,
73
- input: [{ role: "user", content: buildUserTextPrompt(prompt, mode, { webSearchEnabled: false }) }],
74
- tools: tools(false, { quality, size, moderation }),
75
- tool_choice: imageToolChoice(true),
76
- reasoning: { effort: reasoningEffort || "low" },
77
- stream: false,
78
- },
79
- });
80
- } catch (e) {
81
- if (e && typeof e === "object") Object.assign(e, retryMeta);
82
- throw e;
83
- }
84
- const image = retry.images[0];
85
- if (image?.b64) {
86
- logEvent("oauth", "retry_image", { requestId, retryKind, imageChars: image.b64.length });
87
- return { b64: image.b64, usage: retry.usage, webSearchCalls: initial.webSearchCalls, revisedPrompt: image.revisedPrompt, text: retry.text, ...retryMeta };
88
- }
89
- logEvent("oauth", "retry_no_image", {
90
- requestId,
91
- retryKind,
92
- fallbackEventCount: retry.eventCount,
93
- fallbackImageCallSeen: retry.diagnostics.imageCallSeen,
94
- fallbackImageResultCount: retry.diagnostics.imageResultCount,
95
- });
96
- throw emptyResponseError("No image data received from Responses API fallback", retry, {
97
- provider,
98
- model,
99
- quality,
100
- size,
101
- moderation,
102
- webSearchEnabled: false,
103
- refsCount: 0,
104
- inputImageCount: 0,
105
- promptChars: typeof prompt === "string" ? prompt.length : 0,
106
- toolTypes: ["image_generation"],
107
- toolChoiceKind: "image_generation",
108
- ...retryMeta,
109
- fallbackEventCount: retry.eventCount,
110
- fallbackEventTypes: retry.eventTypes,
111
- fallbackImageCallSeen: retry.diagnostics.imageCallSeen,
112
- fallbackImageResultCount: retry.diagnostics.imageResultCount,
113
- });
114
- }
@@ -1,466 +0,0 @@
1
- import { logEvent } from "./logger.js";
2
- import { classifyUpstreamError, classifyUpstreamErrorCode } from "./errorClassify.js";
3
- import { compressReferenceB64ForOAuth } from "./referenceImageCompress.js";
4
- import { detectImageMimeFromB64 } from "./refs.js";
5
- import { errInfo } from "./errInfo.js";
6
- import { setJobPhase } from "./inflight.js";
7
- import { type RouteRuntimeContext, requireRuntimeContext } from "./runtimeContext.js";
8
- import {
9
- parseJson,
10
- parseStream,
11
- safeDiagnosticLabel,
12
- type FinalImageHandler,
13
- } from "./responsesParse.js";
14
- import {
15
- imageToolChoice,
16
- imageToolChoiceKind,
17
- tools,
18
- toolTypes,
19
- } from "./responsesTools.js";
20
- import { emptyResponseError } from "./responsesErrors.js";
21
- import { retryPromptOnlyJsonImage } from "./responsesFallback.js";
22
- import {
23
- AUTO_PROMPT_FIDELITY_SUFFIX,
24
- DIRECT_PROMPT_FIDELITY_SUFFIX,
25
- EDIT_DEVELOPER_PROMPT,
26
- EDIT_NO_SEARCH_DEVELOPER_PROMPT,
27
- GENERATE_DEVELOPER_PROMPT,
28
- GENERATE_NO_SEARCH_DEVELOPER_PROMPT,
29
- MULTIMODE_DEVELOPER_PROMPT,
30
- MULTIMODE_NO_SEARCH_DEVELOPER_PROMPT,
31
- buildEditTextPrompt,
32
- buildMultimodeSequencePrompt,
33
- buildUserTextPrompt,
34
- waitForOAuthReady,
35
- } from "./oauthProxy.js";
36
-
37
- interface MakeErrorOptions {
38
- status?: number;
39
- code?: string;
40
- cause?: unknown;
41
- [key: string]: unknown;
42
- }
43
-
44
- interface ResponsesError extends Error {
45
- status: number;
46
- code: string;
47
- cause?: unknown;
48
- [key: string]: unknown;
49
- }
50
-
51
- const RESPONSES_ERROR_MARKER = "ima2ResponsesError";
52
-
53
- function makeError(message: string, { status = 500, code = "RESPONSES_IMAGE_ERROR", cause, ...rest }: MakeErrorOptions = {}): ResponsesError {
54
- const err = new Error(message) as ResponsesError;
55
- err.status = status;
56
- err.code = code;
57
- if (cause) err.cause = cause;
58
- Object.assign(err, rest);
59
- Object.defineProperty(err, RESPONSES_ERROR_MARKER, { value: true });
60
- return err;
61
- }
62
-
63
- interface UpstreamError {
64
- message: string;
65
- code: string | null;
66
- type: string | null;
67
- param: string | null;
68
- }
69
-
70
- function parseOpenAIErrorBody(text: string): UpstreamError | null {
71
- try {
72
- const parsed = JSON.parse(text);
73
- const error = parsed?.error || {};
74
- return {
75
- message: typeof error.message === "string" && error.message ? error.message : "OpenAI request failed",
76
- code: safeDiagnosticLabel(error.code),
77
- type: safeDiagnosticLabel(error.type),
78
- param: safeDiagnosticLabel(error.param),
79
- };
80
- } catch {
81
- return null;
82
- }
83
- }
84
-
85
- function normalizedCode(upstream: UpstreamError | null | undefined) {
86
- const byCode = classifyUpstreamErrorCode(upstream?.code);
87
- if (byCode !== "UNKNOWN") return byCode;
88
- const byType = classifyUpstreamErrorCode(upstream?.type);
89
- if (byType !== "UNKNOWN") return byType;
90
- const byMessage = classifyUpstreamError(upstream?.message);
91
- return byMessage !== "UNKNOWN" ? byMessage : "RESPONSES_IMAGE_ERROR";
92
- }
93
-
94
- function safeUpstreamClientMessage(upstream: UpstreamError | null | undefined, status: number) {
95
- const code = normalizedCode(upstream);
96
- if (code === "AUTH_API_KEY_INVALID") return "API key is invalid or unavailable.";
97
- if (code === "MODERATION_REFUSED") return "OpenAI refused the image request for safety reasons.";
98
- if (code === "INVALID_REQUEST") return "OpenAI rejected the image request parameters.";
99
- if (status === 401 || status === 403) return "OpenAI authentication failed.";
100
- if (status === 429) return "OpenAI rate limited the image request.";
101
- return "OpenAI rejected the image request.";
102
- }
103
-
104
- function safeBaseUrl(value: string) {
105
- try {
106
- const parsed = new URL(value);
107
- parsed.username = "";
108
- parsed.password = "";
109
- return parsed.toString().replace(/\/$/, "");
110
- } catch {
111
- return value.replace(/\/$/, "");
112
- }
113
- }
114
-
115
- function apiAuthorizationHeader(apiKey: string | undefined) {
116
- const key = typeof apiKey === "string" ? apiKey.trim() : "";
117
- if (!key) {
118
- throw makeError("API key is required for API provider image generation", {
119
- status: 401,
120
- code: "API_KEY_REQUIRED",
121
- });
122
- }
123
- if (/[\u0000-\u001f\u007f]/.test(key)) {
124
- throw makeError("API key contains invalid characters.", {
125
- status: 401,
126
- code: "AUTH_API_KEY_INVALID",
127
- });
128
- }
129
- return `Bearer ${key}`;
130
- }
131
-
132
- function isKnownResponsesError(value: unknown) {
133
- return Boolean(
134
- value &&
135
- typeof value === "object" &&
136
- (value as { ima2ResponsesError?: unknown }).ima2ResponsesError === true,
137
- );
138
- }
139
-
140
- async function getEndpoint(ctx: RouteRuntimeContext, provider: string | undefined, _scope: string) {
141
- if (provider === "api") {
142
- return {
143
- url: "https://api.openai.com/v1/responses",
144
- headers: {
145
- "Content-Type": "application/json",
146
- Accept: "text/event-stream",
147
- Authorization: apiAuthorizationHeader(ctx.apiKey),
148
- },
149
- };
150
- }
151
- await waitForOAuthReady(ctx);
152
- const port = ctx?.config?.oauth?.proxyPort || 10531;
153
- return {
154
- url: `${safeBaseUrl(ctx?.oauthUrl || `http://127.0.0.1:${port}`)}/v1/responses`,
155
- headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
156
- };
157
- }
158
-
159
- type ReferenceRef = string | { b64?: string; detectedMime?: string | null; declaredMime?: string | null };
160
-
161
- function normalizeRef(ref: ReferenceRef) {
162
- const b64 = typeof ref === "string" ? ref : ref?.b64;
163
- const detectedMime = typeof ref === "object" && ref?.detectedMime
164
- ? ref.detectedMime
165
- : detectImageMimeFromB64(b64);
166
- const declaredMime = typeof ref === "object" ? ref?.declaredMime : null;
167
- const mime = ["image/png", "image/jpeg", "image/webp"].includes(detectedMime as string)
168
- ? detectedMime
169
- : ["image/png", "image/jpeg", "image/webp"].includes(declaredMime as string)
170
- ? declaredMime
171
- : "image/png";
172
- return { type: "input_image", image_url: `data:${mime};base64,${b64}` };
173
- }
174
-
175
- interface PostResponsesArgs {
176
- ctx: RouteRuntimeContext;
177
- provider: string | undefined;
178
- scope: string;
179
- payload: unknown;
180
- requestId?: string | null;
181
- maxImages?: number;
182
- signal?: AbortSignal | null;
183
- onPartialImage?: ((partial: { b64: string; index: number | null | undefined }) => void) | null;
184
- onFinalImage?: FinalImageHandler | null;
185
- }
186
-
187
- function combineAbortSignals(signals: AbortSignal[]): AbortSignal {
188
- if (signals.length === 1) return signals[0];
189
- const controller = new AbortController();
190
- for (const signal of signals) {
191
- if (signal.aborted) {
192
- controller.abort();
193
- break;
194
- }
195
- signal.addEventListener("abort", () => controller.abort(), { once: true });
196
- }
197
- return controller.signal;
198
- }
199
-
200
- async function postResponses({
201
- ctx,
202
- provider,
203
- scope,
204
- payload,
205
- requestId,
206
- maxImages = 1,
207
- signal = null,
208
- onPartialImage = null,
209
- onFinalImage = null,
210
- }: PostResponsesArgs) {
211
- const { url, headers } = await getEndpoint(ctx, provider, scope);
212
- const timeoutMs = ctx?.config?.oauth?.generationTimeoutMs || 400 * 1000;
213
- const controller = new AbortController();
214
- const timer = setTimeout(() => controller.abort(), timeoutMs);
215
- const fetchSignal = signal
216
- ? combineAbortSignals([controller.signal, signal])
217
- : controller.signal;
218
- try {
219
- const res = await fetch(url, {
220
- method: "POST",
221
- headers: headers as Record<string, string>,
222
- signal: fetchSignal,
223
- body: JSON.stringify(payload),
224
- });
225
- logEvent(scope, "response", { requestId, provider, status: res.status, contentType: res.headers.get("content-type") });
226
- if (!res.ok) {
227
- const text = await res.text();
228
- const upstream = parseOpenAIErrorBody(text);
229
- if (res.status >= 400 && res.status < 500 && upstream?.message) {
230
- throw makeError(safeUpstreamClientMessage(upstream, res.status), {
231
- status: res.status,
232
- code: normalizedCode(upstream),
233
- upstreamBodyChars: text.length,
234
- upstreamCode: upstream.code,
235
- upstreamType: upstream.type,
236
- upstreamParam: upstream.param,
237
- upstreamMessageRedacted: true,
238
- });
239
- }
240
- throw makeError(`${provider === "api" ? "OpenAI API" : "OAuth proxy"} returned ${res.status}`, {
241
- status: res.status,
242
- upstreamBodyChars: text.length,
243
- });
244
- }
245
- if (requestId) setJobPhase(requestId, "streaming");
246
- const contentType = res.headers.get("content-type") || "";
247
- return contentType.includes("text/event-stream")
248
- ? await parseStream(res, { requestId, scope, maxImages, onPartialImage, onFinalImage })
249
- : await parseJson(res, maxImages);
250
- } catch (e) {
251
- const err = errInfo(e);
252
- if (err.name === "AbortError") {
253
- if (signal?.aborted) {
254
- throw makeError("Generation canceled", {
255
- status: 499,
256
- code: "GENERATION_CANCELED",
257
- cause: err.raw,
258
- });
259
- }
260
- throw makeError("Responses image generation timed out", { status: 504, code: "RESPONSES_IMAGE_TIMEOUT", cause: err.raw });
261
- }
262
- if (isKnownResponsesError(err.raw)) throw err.raw;
263
- throw makeError("Responses request failed before receiving a response", {
264
- status: 502,
265
- code: "NETWORK_FAILED",
266
- errorName: err.name,
267
- upstreamMessageRedacted: true,
268
- });
269
- } finally {
270
- clearTimeout(timer);
271
- }
272
- }
273
-
274
- interface GenerateOptions {
275
- webSearchEnabled?: boolean;
276
- searchMode?: string;
277
- onPartialImage?: ((partial: { b64: string; index: number | null | undefined }) => void) | null;
278
- onFinalImage?: FinalImageHandler | null;
279
- model?: string;
280
- partialImages?: number;
281
- reasoningEffort?: string;
282
- maxImages?: number;
283
- references?: ReferenceRef[];
284
- mask?: string;
285
- signal?: AbortSignal | null;
286
- forceImageToolChoice?: boolean;
287
- allowPromptOnlyOAuthFallback?: boolean;
288
- }
289
-
290
- export async function generateViaResponses(provider: string | undefined, prompt: string | undefined, quality: string | undefined, size: string | undefined, moderation: string = "low", references: ReferenceRef[] = [], requestId: string | null = null, mode: string = "auto", ctxRaw: RouteRuntimeContext = {}, options: GenerateOptions = {}) {
291
- const ctx = requireRuntimeContext(ctxRaw);
292
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.4-mini";
293
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
294
- const requestTools = tools(webSearchEnabled, { quality, size, moderation, ...(options.partialImages ? { partial_images: options.partialImages } : {}) });
295
- const toolChoice = imageToolChoice(options.forceImageToolChoice ?? ctx.config?.oauth?.forceImageToolChoice !== false);
296
- const toolChoiceKind = imageToolChoiceKind(toolChoice);
297
- const referenceInputs = references.map(normalizeRef);
298
- const userContent = referenceInputs.length
299
- ? [...referenceInputs, { type: "input_text", text: buildUserTextPrompt(prompt, mode, { webSearchEnabled }) }]
300
- : buildUserTextPrompt(prompt, mode, { webSearchEnabled });
301
- const result = await postResponses({
302
- ctx,
303
- provider,
304
- scope: provider === "api" ? "api-generate" : "oauth",
305
- requestId,
306
- maxImages: 1,
307
- signal: options.signal,
308
- onPartialImage: options.onPartialImage,
309
- onFinalImage: options.onFinalImage,
310
- payload: {
311
- model,
312
- input: [
313
- { role: "developer", content: webSearchEnabled ? GENERATE_DEVELOPER_PROMPT : GENERATE_NO_SEARCH_DEVELOPER_PROMPT },
314
- { role: "user", content: userContent },
315
- ],
316
- tools: requestTools,
317
- tool_choice: toolChoice,
318
- reasoning: { effort: options.reasoningEffort || "low" },
319
- stream: true,
320
- },
321
- });
322
- const image = result.images[0];
323
- if (!image?.b64) {
324
- if (options.allowPromptOnlyOAuthFallback === true) {
325
- const fallback = await retryPromptOnlyJsonImage({
326
- postResponses,
327
- ctx,
328
- provider,
329
- prompt,
330
- mode,
331
- model,
332
- quality,
333
- size,
334
- moderation,
335
- requestId,
336
- signal: options.signal,
337
- initial: result,
338
- referencesDroppedOnRetry: referenceInputs.length > 0,
339
- webSearchDroppedOnRetry: webSearchEnabled,
340
- reasoningEffort: options.reasoningEffort,
341
- });
342
- if (fallback) return fallback;
343
- }
344
- throw emptyResponseError("No image data received from Responses API", result, {
345
- provider,
346
- model,
347
- quality,
348
- size,
349
- moderation,
350
- webSearchEnabled,
351
- refsCount: referenceInputs.length,
352
- inputImageCount: referenceInputs.length,
353
- promptChars: typeof prompt === "string" ? prompt.length : 0,
354
- toolTypes: toolTypes(requestTools),
355
- toolChoiceKind,
356
- });
357
- }
358
- return { b64: image.b64, usage: result.usage, webSearchCalls: result.webSearchCalls, revisedPrompt: image.revisedPrompt, text: result.text };
359
- }
360
-
361
- export async function generateMultimodeViaResponses(provider: string | undefined, prompt: string | undefined, quality: string | undefined, size: string | undefined, moderation: string = "low", references: ReferenceRef[] = [], requestId: string | null = null, mode: string = "auto", ctxRaw: RouteRuntimeContext = {}, options: GenerateOptions = {}) {
362
- const ctx = requireRuntimeContext(ctxRaw);
363
- const maxImages = Math.min(8, Math.max(1, Math.trunc(Number(options.maxImages) || 1)));
364
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.4-mini";
365
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
366
- const requestTools = tools(webSearchEnabled, { quality, size, moderation, ...(options.partialImages ? { partial_images: options.partialImages } : {}) });
367
- const userText = buildMultimodeSequencePrompt(
368
- mode === "direct"
369
- ? `${prompt}${DIRECT_PROMPT_FIDELITY_SUFFIX}`
370
- : `${prompt}${webSearchEnabled ? "" : ""}${AUTO_PROMPT_FIDELITY_SUFFIX}`,
371
- maxImages,
372
- { webSearchEnabled },
373
- );
374
- const referenceInputs = references.map(normalizeRef);
375
- const userContent = referenceInputs.length
376
- ? [...referenceInputs, { type: "input_text", text: userText }]
377
- : userText;
378
- return await postResponses({
379
- ctx,
380
- provider,
381
- scope: provider === "api" ? "api-multimode" : "oauth-multimode",
382
- requestId,
383
- maxImages,
384
- signal: options.signal,
385
- onPartialImage: options.onPartialImage,
386
- onFinalImage: options.onFinalImage,
387
- payload: {
388
- model,
389
- input: [
390
- { role: "developer", content: webSearchEnabled ? MULTIMODE_DEVELOPER_PROMPT : MULTIMODE_NO_SEARCH_DEVELOPER_PROMPT },
391
- { role: "user", content: userContent },
392
- ],
393
- tools: requestTools,
394
- tool_choice: "required",
395
- reasoning: { effort: options.reasoningEffort || "low" },
396
- stream: true,
397
- },
398
- });
399
- }
400
-
401
- export async function editViaResponses(provider: string | undefined, prompt: string | undefined, imageB64: string | undefined, quality: string | undefined, size: string | undefined, moderation: string = "low", mode: string = "auto", ctxRaw: RouteRuntimeContext = {}, requestId: string | null = null, options: GenerateOptions = {}) {
402
- const ctx = requireRuntimeContext(ctxRaw);
403
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.4-mini";
404
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
405
- const requestTools = tools(webSearchEnabled, { quality, size, moderation });
406
- const toolChoice = imageToolChoice(options.forceImageToolChoice ?? ctx.config?.oauth?.forceImageToolChoice !== false);
407
- const toolChoiceKind = imageToolChoiceKind(toolChoice);
408
- const imageForRequest = await compressReferenceB64ForOAuth(imageB64, {
409
- maxB64Bytes: ctx.config?.limits?.maxRefB64Bytes,
410
- force: true,
411
- });
412
- const referenceImages = await Promise.all((Array.isArray(options.references) ? options.references : []).map((ref: ReferenceRef) =>
413
- compressReferenceB64ForOAuth(typeof ref === "string" ? ref : ref?.b64, {
414
- maxB64Bytes: ctx.config?.limits?.maxRefB64Bytes,
415
- force: true,
416
- }),
417
- ));
418
- const maskContent = typeof options.mask === "string" && options.mask.length > 0
419
- ? [
420
- { type: "input_image", image_url: `data:image/png;base64,${options.mask}` },
421
- { type: "input_text", text: "The previous image is an edit mask guide. Use it as prompt guidance for where the edit should apply; it is not a visible final image element." },
422
- ]
423
- : [];
424
- const userContent = [
425
- { type: "input_image", image_url: `data:image/jpeg;base64,${imageForRequest.b64}` },
426
- ...referenceImages.map(({ b64 }) => ({ type: "input_image", image_url: `data:image/jpeg;base64,${b64}` })),
427
- ...maskContent,
428
- { type: "input_text", text: buildEditTextPrompt(prompt, mode, { webSearchEnabled }) },
429
- ];
430
- const result = await postResponses({
431
- ctx,
432
- provider,
433
- scope: provider === "api" ? "api-edit" : "oauth-edit",
434
- requestId,
435
- maxImages: 1,
436
- signal: options.signal,
437
- payload: {
438
- model,
439
- input: [
440
- { role: "developer", content: webSearchEnabled ? EDIT_DEVELOPER_PROMPT : EDIT_NO_SEARCH_DEVELOPER_PROMPT },
441
- { role: "user", content: userContent },
442
- ],
443
- tools: requestTools,
444
- tool_choice: toolChoice,
445
- reasoning: { effort: options.reasoningEffort || "low" },
446
- stream: true,
447
- },
448
- });
449
- const image = result.images[0];
450
- if (!image?.b64) {
451
- throw emptyResponseError("No image data received from Responses edit", result, {
452
- provider,
453
- model,
454
- quality,
455
- size,
456
- moderation,
457
- webSearchEnabled,
458
- refsCount: referenceImages.length,
459
- inputImageCount: 1 + referenceImages.length + (maskContent.length ? 1 : 0),
460
- promptChars: typeof prompt === "string" ? prompt.length : 0,
461
- toolTypes: toolTypes(requestTools),
462
- toolChoiceKind,
463
- });
464
- }
465
- return { b64: image.b64, usage: result.usage, revisedPrompt: image.revisedPrompt, webSearchCalls: result.webSearchCalls };
466
- }