ima2-gen 3.13.1 → 3.14.0

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 (157) hide show
  1. package/.env.example +10 -0
  2. package/CHANGELOG.md +14 -0
  3. package/README.md +15 -2
  4. package/bin/commands/capabilities.js +5 -3
  5. package/bin/commands/config.js +15 -4
  6. package/bin/commands/defaults.js +7 -5
  7. package/bin/commands/doctor.js +88 -39
  8. package/bin/commands/gen.js +21 -4
  9. package/bin/commands/models.js +2 -2
  10. package/bin/commands/ping.js +1 -1
  11. package/bin/commands/prompt.js +5 -3
  12. package/bin/commands/service.js +32 -4
  13. package/bin/commands/tools.js +19 -3
  14. package/bin/commands/upscale.js +18 -4
  15. package/bin/commands/video.js +37 -50
  16. package/bin/ima2.js +28 -9
  17. package/bin/lib/characterResolve.js +2 -1
  18. package/bin/lib/client.js +155 -20
  19. package/bin/lib/config-store.js +40 -7
  20. package/bin/lib/doctor-bundle.js +6 -2
  21. package/bin/lib/doctor-checks.js +13 -29
  22. package/bin/lib/doctor-media.js +3 -3
  23. package/bin/lib/doctor-providers.js +48 -35
  24. package/bin/lib/doctor-report.js +78 -0
  25. package/bin/lib/doctor-runtime.js +173 -0
  26. package/bin/lib/mcpJob.js +19 -7
  27. package/bin/lib/output.js +3 -0
  28. package/bin/lib/platform.js +12 -9
  29. package/bin/lib/recover-output.js +9 -2
  30. package/bin/lib/sse.js +9 -1
  31. package/bin/lib/videoMcp.js +16 -8
  32. package/config.js +41 -5
  33. package/docs/API.md +154 -16
  34. package/docs/CLI.md +65 -1
  35. package/docs/DOCKER.md +19 -4
  36. package/docs/DOCKER.zh-CN.md +11 -4
  37. package/docs/DOCKER.zh-TW.md +11 -4
  38. package/docs/FAQ.md +13 -0
  39. package/docs/NPX_QUICKSTART.md +6 -0
  40. package/docs/README.ja.md +15 -2
  41. package/docs/README.ko.md +15 -2
  42. package/docs/README.zh-CN.md +15 -2
  43. package/docs/README.zh-TW.md +15 -2
  44. package/docs/migration/runtime-test-inventory.md +79 -8
  45. package/lib/agyArtifact.js +82 -0
  46. package/lib/agyArtifactRead.js +249 -0
  47. package/lib/agyImageAdapter.js +2 -329
  48. package/lib/agyProcess.js +122 -0
  49. package/lib/apiRequestBudget.js +48 -0
  50. package/lib/assetLifecycle.js +42 -9
  51. package/lib/canvasVersionStore.js +46 -14
  52. package/lib/capabilities.js +7 -1
  53. package/lib/configKeys.js +2 -0
  54. package/lib/errors/providerMap.js +26 -0
  55. package/lib/eventBus.js +10 -6
  56. package/lib/eventsPolicy.js +9 -0
  57. package/lib/geminiApiImageAdapter.js +1 -214
  58. package/lib/generatePipeline.js +39 -165
  59. package/lib/generatedMediaAccess.js +74 -0
  60. package/lib/generationErrors.js +3 -22
  61. package/lib/grokImageAdapter.js +2 -353
  62. package/lib/grokImageCore.js +1 -57
  63. package/lib/grokImageDownload.js +159 -0
  64. package/lib/grokImageDownloadPolicy.js +124 -0
  65. package/lib/grokImagePlanner.js +301 -0
  66. package/lib/grokMultimodeAdapter.js +1 -75
  67. package/lib/grokVideoDownload.js +137 -34
  68. package/lib/inflight.js +112 -112
  69. package/lib/jobs/terminalStore.js +45 -0
  70. package/lib/lanSessionStore.js +131 -0
  71. package/lib/localAccessPolicy.js +180 -0
  72. package/lib/localLanAccess.js +281 -0
  73. package/lib/logger.js +22 -13
  74. package/lib/mcp/downloadMediaResult.js +72 -78
  75. package/lib/multimodePipeline.js +39 -126
  76. package/lib/nodeGeneration.js +44 -101
  77. package/lib/nodeStore.js +25 -11
  78. package/lib/nodeValidation.js +5 -1
  79. package/lib/pinnedHttpGet.js +174 -0
  80. package/lib/promptImport/githubFolder.js +32 -18
  81. package/lib/promptImport/githubSource.js +14 -10
  82. package/lib/promptImport/parsePromptCandidates.js +32 -2
  83. package/lib/providers/adapters/agyOperations.js +179 -0
  84. package/lib/providers/adapters/comfy.js +1 -0
  85. package/lib/providers/adapters/geminiOperations.js +214 -0
  86. package/lib/providers/adapters/googleExecution.js +97 -0
  87. package/lib/providers/adapters/grokExecution.js +137 -0
  88. package/lib/providers/adapters/grokMultimodeOperations.js +83 -0
  89. package/lib/providers/adapters/grokOperations.js +63 -0
  90. package/lib/providers/adapters/openaiExecution.js +118 -0
  91. package/lib/providers/adapters/openaiOperations.js +201 -0
  92. package/lib/providers/adapters/openaiTypes.js +1 -0
  93. package/lib/providers/derive.js +4 -0
  94. package/lib/providers/deriveCore.js +2 -7
  95. package/lib/providers/execution/admission.js +25 -0
  96. package/lib/providers/execution/index.js +34 -0
  97. package/lib/providers/execution/legacy.js +17 -0
  98. package/lib/providers/execution/legacyClassic.js +66 -0
  99. package/lib/providers/execution/legacyEdit.js +30 -0
  100. package/lib/providers/execution/legacyMultimode.js +34 -0
  101. package/lib/providers/execution/legacyNode.js +41 -0
  102. package/lib/providers/execution/types.js +1 -0
  103. package/lib/providers/registry.js +19 -14
  104. package/lib/providers/surfaceSupport.js +22 -0
  105. package/lib/responsesImageAdapter.js +1 -389
  106. package/lib/responsesTransport.js +190 -0
  107. package/lib/spriteJobEvents.js +20 -2
  108. package/lib/ssePublish.js +5 -4
  109. package/lib/videoExtendI2vOperation.js +76 -0
  110. package/package.json +5 -2
  111. package/routes/edit.js +31 -123
  112. package/routes/events.js +140 -63
  113. package/routes/mcpMedia.js +10 -2
  114. package/routes/models.js +28 -25
  115. package/routes/videoExtended.js +9 -68
  116. package/server.js +24 -46
  117. package/ui/dist/.vite/manifest.json +90 -63
  118. package/ui/dist/assets/{AgentWorkspace-PbBytTeV.js → AgentWorkspace-CL3MLv3l.js} +2 -2
  119. package/ui/dist/assets/App-B9cEM6dJ.js +9 -0
  120. package/ui/dist/assets/AssetGenWorkspace-swWPvXrx.js +2 -0
  121. package/ui/dist/assets/AssetsWorkspace-CRVeYU2F.js +1 -0
  122. package/ui/dist/assets/{CardNewsWorkspace-BS3jKOja.js → CardNewsWorkspace-BAOxWLKf.js} +2 -2
  123. package/ui/dist/assets/GenerationRequestLogPanel-DZtYZm-D.js +1 -0
  124. package/ui/dist/assets/HomeWorkspace-BeZ1s0Jm.js +1 -0
  125. package/ui/dist/assets/KeyingPanel-ZyBSXioh.js +1 -0
  126. package/ui/dist/assets/NodeCanvas-BvvcCErf.js +7 -0
  127. package/ui/dist/assets/{PromptBuilderPanel-D97A4pwB.js → PromptBuilderPanel-DBycmShA.js} +2 -2
  128. package/ui/dist/assets/PromptImportDialog-ChvMv7ek.js +2 -0
  129. package/ui/dist/assets/PromptImportDiscoverySection-60py7whH.js +1 -0
  130. package/ui/dist/assets/PromptImportFolderSection-CCRh_-_7.js +1 -0
  131. package/ui/dist/assets/PromptLibraryPanel-BFWQO8OT.js +2 -0
  132. package/ui/dist/assets/SettingsWorkspace-B5Ghsgdj.js +1 -0
  133. package/ui/dist/assets/SpriteRecipeWorkspace-DRWdkmwU.js +1 -0
  134. package/ui/dist/assets/index-7ngEOVxt.js +5 -0
  135. package/ui/dist/assets/index-B5jy7ePg.css +1 -0
  136. package/ui/dist/assets/index-gxh_02DQ.js +22 -0
  137. package/ui/dist/assets/{pptxgen.es-D4pedbwM.js → pptxgen.es-OUNhQLEV.js} +1 -1
  138. package/ui/dist/assets/promptBuilderStore-BTUUzbBT.js +1 -0
  139. package/ui/dist/assets/useAgentDialogFocus-DSc2qY8p.js +1 -0
  140. package/ui/dist/index.html +3 -2
  141. package/ui/dist/assets/AssetGenWorkspace-B-TGlvVb.js +0 -2
  142. package/ui/dist/assets/AssetsWorkspace-LnGaW151.js +0 -1
  143. package/ui/dist/assets/GenerationRequestLogPanel-DuDWD_ic.js +0 -1
  144. package/ui/dist/assets/HomeWorkspace-DVP7UG6O.js +0 -1
  145. package/ui/dist/assets/KeyingPanel-5bRUFQaq.js +0 -1
  146. package/ui/dist/assets/NodeCanvas-BBEpERTb.js +0 -7
  147. package/ui/dist/assets/PromptImportDialog-C89MN5JY.js +0 -2
  148. package/ui/dist/assets/PromptImportDiscoverySection-BgRfDOs2.js +0 -1
  149. package/ui/dist/assets/PromptImportFolderSection-zTMyriT1.js +0 -1
  150. package/ui/dist/assets/PromptLibraryPanel-BD96qZu_.js +0 -2
  151. package/ui/dist/assets/SettingsWorkspace-BAOk3X_8.js +0 -1
  152. package/ui/dist/assets/SpriteRecipeWorkspace-ClIM5QRo.js +0 -1
  153. package/ui/dist/assets/index-0fpfY8vu.css +0 -1
  154. package/ui/dist/assets/index-BFciPGTn.js +0 -5
  155. package/ui/dist/assets/index-BbykeXR9.js +0 -30
  156. package/ui/dist/assets/promptBuilderStore-Do7M8g7J.js +0 -1
  157. package/ui/dist/assets/useAgentDialogFocus-B6-QgEVI.js +0 -1
@@ -0,0 +1,30 @@
1
+ import { detectImageMimeFromB64 } from "../../refs.js";
2
+ import { generateViaAtlasCloud } from "../../atlasCloudImageAdapter.js";
3
+ import { generateViaMinimax } from "../../minimaxImageAdapter.js";
4
+ import { generateViaComfy } from "../../comfyImageAdapter.js";
5
+ function executeImageToImage(ctx, request) {
6
+ const { provider, prompt, sourceImage, signal, requestId, options } = request;
7
+ const references = [{ b64: sourceImage, declaredMime: null, detectedMime: detectImageMimeFromB64(sourceImage) || null }];
8
+ const editPrompt = `Edit this image: ${prompt}`;
9
+ const params = { model: options.model, size: options.size, signal, ...(requestId !== undefined ? { requestId } : {}), references };
10
+ switch (provider) {
11
+ case "atlascloud": return generateViaAtlasCloud(editPrompt, ctx, { ...params, quality: options.quality });
12
+ case "minimax": return generateViaMinimax(editPrompt, ctx, params);
13
+ // LoadImage binding owns i2i; missing workflow binding still refuses there.
14
+ case "comfy": return generateViaComfy(editPrompt, ctx, params);
15
+ default: throw new Error(`Unsupported legacy edit provider: ${provider}`);
16
+ }
17
+ }
18
+ async function executeEdit(ctx, request) {
19
+ return executeImageToImage(ctx, request);
20
+ }
21
+ export async function prepareLegacyEdit(ctx, request, _progress) {
22
+ return { execute: async () => {
23
+ try {
24
+ return { kind: "single", value: await executeEdit(ctx, request) };
25
+ }
26
+ catch (error) {
27
+ throw error;
28
+ } // Route owns failure normalization and job cleanup.
29
+ } };
30
+ }
@@ -0,0 +1,34 @@
1
+ import { generateViaAtlasCloud } from "../../atlasCloudImageAdapter.js";
2
+ import { generateViaMinimax } from "../../minimaxImageAdapter.js";
3
+ import { generateViaNai } from "../../naiImageAdapter.js";
4
+ function executeSingleLane(ctx, request) {
5
+ const { provider, rawPrompt, references, signal, requestId, options } = request;
6
+ const params = { model: options.model, size: options.size, signal, ...(requestId !== undefined ? { requestId } : {}), references };
7
+ switch (provider) {
8
+ case "atlascloud": return generateViaAtlasCloud(rawPrompt, ctx, { ...params, quality: options.quality });
9
+ case "minimax": return generateViaMinimax(rawPrompt, ctx, params);
10
+ // Text-to-image only: no references; admission rejects them before startJob.
11
+ case "nai": return generateViaNai(rawPrompt, ctx, {
12
+ model: options.model, size: options.size, signal, requestId, ...request.nai,
13
+ });
14
+ default: throw new Error(`Unsupported legacy multimode provider: ${provider}`);
15
+ }
16
+ }
17
+ async function executeSequence(ctx, request, _progress) {
18
+ const result = await executeSingleLane(ctx, request);
19
+ // Preserve the old per-lane projection: downstream detects bytes, not MIME here.
20
+ return {
21
+ images: [{ b64: result.b64, ...(result.revisedPrompt !== undefined ? { revisedPrompt: result.revisedPrompt } : {}) }],
22
+ usage: result.usage, webSearchCalls: result.webSearchCalls,
23
+ };
24
+ }
25
+ export async function prepareLegacyMultimode(ctx, request, progress = {}) {
26
+ return { execute: async () => {
27
+ try {
28
+ return { kind: "sequence", value: await executeSequence(ctx, request, progress) };
29
+ }
30
+ catch (error) {
31
+ throw error;
32
+ } // Preserve native errors; caller owns timeout recovery.
33
+ } };
34
+ }
@@ -0,0 +1,41 @@
1
+ import { generateViaAtlasCloud } from "../../atlasCloudImageAdapter.js";
2
+ import { generateViaMinimax } from "../../minimaxImageAdapter.js";
3
+ import { generateViaNai } from "../../naiImageAdapter.js";
4
+ import { requireRuntimeContext } from "../../runtimeContext.js";
5
+ export async function prepareLegacyNode(ctx, request, _progress) {
6
+ return { execute: async () => {
7
+ try {
8
+ return { kind: "single", value: await executeNodeAttempt(ctx, request) };
9
+ }
10
+ catch (error) {
11
+ throw error;
12
+ } // The caller owns retries and normalization.
13
+ } };
14
+ }
15
+ // Keep the legacy one-attempt branches together; retry/lifecycle stays at the caller.
16
+ async function executeNodeAttempt(ctx, request) {
17
+ const { provider, sourceImage: parentB64, rawPrompt: prompt, references, requestId, signal, options } = request;
18
+ const { model, size, quality } = options;
19
+ return provider === "atlascloud"
20
+ ? await generateViaAtlasCloud(parentB64 ? `Edit this image: ${prompt}` : prompt, requireRuntimeContext(ctx), {
21
+ model, size, quality, signal, requestId,
22
+ references: parentB64
23
+ ? [{ b64: parentB64, declaredMime: null, detectedMime: null }, ...references]
24
+ : references,
25
+ })
26
+ : provider === "minimax"
27
+ ? await generateViaMinimax(parentB64 ? `Edit this image: ${prompt}` : prompt, requireRuntimeContext(ctx), {
28
+ model, size, signal, requestId,
29
+ references: parentB64
30
+ ? [{ b64: parentB64, declaredMime: null, detectedMime: null }, ...references]
31
+ : references,
32
+ })
33
+ : provider === "nai"
34
+ ? await generateViaNai(prompt, requireRuntimeContext(ctx), {
35
+ model, size, signal, requestId, ...request.nai,
36
+ })
37
+ : unsupportedNodeProvider(provider);
38
+ }
39
+ function unsupportedNodeProvider(provider) {
40
+ throw new Error(`Unsupported node execution provider: ${provider}`);
41
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,15 +1,17 @@
1
- const EDIT = { edit: true, mask: false, streaming: false };
1
+ const EDIT = { generate: true, edit: true, mask: false, streaming: false };
2
2
  // OAuth/API image models edit, mask, and stream. Masks are real on the active
3
3
  // path: routes/edit.ts excludes only grok/agy/grok-api/gemini-api/atlascloud/
4
4
  // minimax from masking and then calls editViaResponses, whose options accept
5
5
  // `mask` (lib/responsesImageAdapter.ts). The legacy editViaOAuth path in
6
6
  // lib/oauthProxy/multimodeGenerators.ts rejects masks unconditionally, but no
7
7
  // route reaches it, so it does not describe current behavior.
8
- const RESPONSES = { edit: true, mask: true, streaming: true };
9
- const UNSUPPORTED = { edit: false, mask: false, streaming: false };
8
+ const RESPONSES = { generate: true, edit: true, mask: true, streaming: true };
9
+ const UNSUPPORTED = { generate: false, edit: false, mask: false, streaming: false };
10
+ const GENERATE_ONLY = { generate: true, edit: false, mask: false, streaming: false };
10
11
  export const REGISTRY = [
11
12
  {
12
13
  id: "oauth",
14
+ surfaces: ["generate", "edit", "multimode", "node"],
13
15
  vendor: "openai",
14
16
  credentials: [{
15
17
  kind: "oauth-proxy",
@@ -33,6 +35,7 @@ export const REGISTRY = [
33
35
  },
34
36
  {
35
37
  id: "api",
38
+ surfaces: ["generate", "edit", "multimode", "node"],
36
39
  vendor: "openai",
37
40
  credentials: [{
38
41
  kind: "api-key",
@@ -57,6 +60,7 @@ export const REGISTRY = [
57
60
  },
58
61
  {
59
62
  id: "grok",
63
+ surfaces: ["generate", "edit", "multimode", "node", "video"],
60
64
  vendor: "xai",
61
65
  credentials: [{ kind: "oauth-proxy", envVars: ["IMA2_GROK_PROXY_HOST", "IMA2_GROK_PROXY_PORT"], configKey: "grokProvider" }],
62
66
  models: [
@@ -73,6 +77,7 @@ export const REGISTRY = [
73
77
  },
74
78
  {
75
79
  id: "grok-api",
80
+ surfaces: ["generate", "edit", "multimode", "node", "video"],
76
81
  vendor: "xai",
77
82
  credentials: [{
78
83
  kind: "api-key",
@@ -96,6 +101,7 @@ export const REGISTRY = [
96
101
  },
97
102
  {
98
103
  id: "agy",
104
+ surfaces: ["generate", "edit", "multimode", "node"],
99
105
  vendor: "google",
100
106
  credentials: [{ kind: "local-cli", envVars: ["IMA2_AGY_BIN"], optionalApiKeyEnv: "GEMINI_API_KEY" }],
101
107
  models: [
@@ -109,6 +115,7 @@ export const REGISTRY = [
109
115
  },
110
116
  {
111
117
  id: "gemini-api",
118
+ surfaces: ["generate", "edit", "multimode", "node"],
112
119
  vendor: "google",
113
120
  credentials: [
114
121
  {
@@ -132,6 +139,7 @@ export const REGISTRY = [
132
139
  },
133
140
  {
134
141
  id: "atlascloud",
142
+ surfaces: ["generate", "edit", "multimode", "node"],
135
143
  vendor: "atlascloud",
136
144
  credentials: [{
137
145
  kind: "api-key",
@@ -152,6 +160,7 @@ export const REGISTRY = [
152
160
  },
153
161
  {
154
162
  id: "minimax",
163
+ surfaces: ["generate", "edit", "multimode", "node"],
155
164
  vendor: "minimax",
156
165
  credentials: [{
157
166
  kind: "api-key",
@@ -173,6 +182,7 @@ export const REGISTRY = [
173
182
  },
174
183
  {
175
184
  id: "nai",
185
+ surfaces: ["generate", "multimode", "node"],
176
186
  vendor: "novelai",
177
187
  credentials: [{
178
188
  kind: "api-key",
@@ -190,17 +200,11 @@ export const REGISTRY = [
190
200
  configKey: "naiApiKey",
191
201
  }],
192
202
  models: [
193
- // EDIT rather than UNSUPPORTED, deliberately. `supports` has no "generate"
194
- // bit, so an all-false triple means "listed but cannot produce images at
195
- // all" (that is what gpt-5.3-codex-spark is) and would push these models
196
- // into UNSUPPORTED_IMAGE_MODEL_IDS — wrong, since V5 generates fine.
197
- // Reference input is refused at the ROUTES (NAI_REF_UNSUPPORTED /
198
- // NAI_EDIT_UNSUPPORTED) and `referenceLimits` below declares no capacity,
199
- // so nothing here advertises an img2img path the adapter lacks.
200
- { id: "nai-diffusion-5-full", aliases: ["nai-v5-full"], kind: "image", supports: EDIT },
201
- { id: "nai-diffusion-5-curated", aliases: ["nai-v5-curated"], kind: "image", supports: EDIT },
202
- { id: "nai-diffusion-4-5-full", aliases: ["nai-v45-full"], kind: "image", supports: EDIT },
203
- { id: "nai-diffusion-4-5-curated", aliases: ["nai-v45-curated"], kind: "image", supports: EDIT },
203
+ // Generation support does not imply reference editing or masks.
204
+ { id: "nai-diffusion-5-full", aliases: ["nai-v5-full"], kind: "image", supports: GENERATE_ONLY },
205
+ { id: "nai-diffusion-5-curated", aliases: ["nai-v5-curated"], kind: "image", supports: GENERATE_ONLY },
206
+ { id: "nai-diffusion-4-5-full", aliases: ["nai-v45-full"], kind: "image", supports: GENERATE_ONLY },
207
+ { id: "nai-diffusion-4-5-curated", aliases: ["nai-v45-curated"], kind: "image", supports: GENERATE_ONLY },
204
208
  ],
205
209
  // Empty because the lane accepts no reference input at all; the routes
206
210
  // refuse it with NAI_REF_UNSUPPORTED rather than dropping it silently.
@@ -214,6 +218,7 @@ export const REGISTRY = [
214
218
  },
215
219
  {
216
220
  id: "comfy",
221
+ surfaces: ["generate", "edit", "video"],
217
222
  vendor: "comfy",
218
223
  credentials: [{
219
224
  kind: "local-http",
@@ -0,0 +1,22 @@
1
+ export const PROVIDER_SURFACES = [
2
+ "generate", "edit", "multimode", "node", "video",
3
+ ];
4
+ /** Project supported operations; a runtime catalog is not an empty static one. */
5
+ export function deriveProviderSurfaceSupportFrom(registry, providerId, surface) {
6
+ const provider = registry.find((entry) => entry.id === providerId);
7
+ if (!provider)
8
+ return null;
9
+ const catalogAccess = provider.catalogAccess ?? "static";
10
+ const runtime = catalogAccess === "runtime";
11
+ const models = provider.models.filter((model) => model.kind === (surface === "video" ? "video" : "image"));
12
+ const runnable = runtime || models.some((model) => surface === "edit" ? model.supports.edit : model.supports.generate);
13
+ const supported = provider.surfaces.includes(surface) && runnable;
14
+ return {
15
+ supported,
16
+ references: supported && (runtime || models.some((model) => model.supports.edit)),
17
+ mask: supported && surface === "edit" && models.some((model) => model.supports.mask),
18
+ streaming: supported && (surface === "multimode" || surface === "node")
19
+ && models.some((model) => model.supports.streaming),
20
+ catalogAccess,
21
+ };
22
+ }
@@ -1,389 +1 @@
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 { requireRuntimeContext } from "./runtimeContext.js";
8
- import { parseJson, parseStream, safeDiagnosticLabel, } from "./responsesParse.js";
9
- import { imageToolChoice, imageToolChoiceKind, tools, toolTypes, } from "./responsesTools.js";
10
- import { emptyResponseError } from "./responsesErrors.js";
11
- import { retryPromptOnlyJsonImage } from "./responsesFallback.js";
12
- import { AUTO_PROMPT_FIDELITY_SUFFIX, DIRECT_PROMPT_FIDELITY_SUFFIX, EDIT_DEVELOPER_PROMPT, EDIT_NO_SEARCH_DEVELOPER_PROMPT, GENERATE_DEVELOPER_PROMPT, GENERATE_NO_SEARCH_DEVELOPER_PROMPT, MULTIMODE_DEVELOPER_PROMPT, MULTIMODE_NO_SEARCH_DEVELOPER_PROMPT, buildEditTextPrompt, buildMultimodeSequencePrompt, buildUserTextPrompt, waitForOAuthReady, } from "./oauthProxy.js";
13
- const RESPONSES_ERROR_MARKER = "ima2ResponsesError";
14
- function makeError(message, { status = 500, code = "RESPONSES_IMAGE_ERROR", cause, ...rest } = {}) {
15
- const err = new Error(message);
16
- err.status = status;
17
- err.code = code;
18
- if (cause)
19
- err.cause = cause;
20
- Object.assign(err, rest);
21
- Object.defineProperty(err, RESPONSES_ERROR_MARKER, { value: true });
22
- return err;
23
- }
24
- function parseOpenAIErrorBody(text) {
25
- try {
26
- const parsed = JSON.parse(text);
27
- const error = parsed?.error || {};
28
- return {
29
- message: typeof error.message === "string" && error.message ? error.message : "OpenAI request failed",
30
- code: safeDiagnosticLabel(error.code),
31
- type: safeDiagnosticLabel(error.type),
32
- param: safeDiagnosticLabel(error.param),
33
- };
34
- }
35
- catch {
36
- return null;
37
- }
38
- }
39
- function normalizedCode(upstream) {
40
- const byCode = classifyUpstreamErrorCode(upstream?.code);
41
- if (byCode !== "UNKNOWN")
42
- return byCode;
43
- const byType = classifyUpstreamErrorCode(upstream?.type);
44
- if (byType !== "UNKNOWN")
45
- return byType;
46
- const byMessage = classifyUpstreamError(upstream?.message);
47
- return byMessage !== "UNKNOWN" ? byMessage : "RESPONSES_IMAGE_ERROR";
48
- }
49
- function safeUpstreamClientMessage(upstream, status) {
50
- const code = normalizedCode(upstream);
51
- if (code === "AUTH_API_KEY_INVALID")
52
- return "API key is invalid or unavailable.";
53
- if (code === "MODERATION_REFUSED")
54
- return "OpenAI refused the image request for safety reasons.";
55
- if (code === "INVALID_REQUEST") {
56
- return upstream?.param
57
- ? "OpenAI rejected the image request parameters."
58
- : "OpenAI rejected the image request.";
59
- }
60
- if (status === 401 || status === 403)
61
- return "OpenAI authentication failed.";
62
- if (status === 429)
63
- return "OpenAI rate limited the image request.";
64
- return "OpenAI rejected the image request.";
65
- }
66
- function safeBaseUrl(value) {
67
- try {
68
- const parsed = new URL(value);
69
- parsed.username = "";
70
- parsed.password = "";
71
- return parsed.toString().replace(/\/$/, "");
72
- }
73
- catch {
74
- return value.replace(/\/$/, "");
75
- }
76
- }
77
- function apiAuthorizationHeader(apiKey) {
78
- const key = typeof apiKey === "string" ? apiKey.trim() : "";
79
- if (!key) {
80
- throw makeError("API key is required for API provider image generation", {
81
- status: 401,
82
- code: "API_KEY_REQUIRED",
83
- });
84
- }
85
- if (/[\u0000-\u001f\u007f]/.test(key)) {
86
- throw makeError("API key contains invalid characters.", {
87
- status: 401,
88
- code: "AUTH_API_KEY_INVALID",
89
- });
90
- }
91
- return `Bearer ${key}`;
92
- }
93
- function isKnownResponsesError(value) {
94
- return Boolean(value &&
95
- typeof value === "object" &&
96
- value.ima2ResponsesError === true);
97
- }
98
- async function getEndpoint(ctx, provider, _scope) {
99
- if (provider === "api") {
100
- return {
101
- url: "https://api.openai.com/v1/responses",
102
- headers: {
103
- "Content-Type": "application/json",
104
- Accept: "text/event-stream",
105
- Authorization: apiAuthorizationHeader(ctx.apiKey),
106
- },
107
- };
108
- }
109
- await waitForOAuthReady(ctx);
110
- const port = ctx?.config?.oauth?.proxyPort || 10531;
111
- return {
112
- url: `${safeBaseUrl(ctx?.oauthUrl || `http://127.0.0.1:${port}`)}/v1/responses`,
113
- headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
114
- };
115
- }
116
- function normalizeRef(ref) {
117
- const b64 = typeof ref === "string" ? ref : ref?.b64;
118
- const detectedMime = typeof ref === "object" && ref?.detectedMime
119
- ? ref.detectedMime
120
- : detectImageMimeFromB64(b64);
121
- const declaredMime = typeof ref === "object" ? ref?.declaredMime : null;
122
- const mime = ["image/png", "image/jpeg", "image/webp"].includes(detectedMime)
123
- ? detectedMime
124
- : ["image/png", "image/jpeg", "image/webp"].includes(declaredMime)
125
- ? declaredMime
126
- : "image/png";
127
- return { type: "input_image", image_url: `data:${mime};base64,${b64}` };
128
- }
129
- function combineAbortSignals(signals) {
130
- const only = signals[0];
131
- if (signals.length === 1 && only)
132
- return only;
133
- const controller = new AbortController();
134
- for (const signal of signals) {
135
- if (signal.aborted) {
136
- controller.abort();
137
- break;
138
- }
139
- signal.addEventListener("abort", () => controller.abort(), { once: true });
140
- }
141
- return controller.signal;
142
- }
143
- async function postResponses({ ctx, provider, scope, payload, requestId, maxImages = 1, signal = null, onPartialImage = null, onFinalImage = null, }) {
144
- const { url, headers } = await getEndpoint(ctx, provider, scope);
145
- const timeoutMs = ctx?.config?.oauth?.generationTimeoutMs || 400 * 1000;
146
- const controller = new AbortController();
147
- const timer = setTimeout(() => controller.abort(), timeoutMs);
148
- const fetchSignal = signal
149
- ? combineAbortSignals([controller.signal, signal])
150
- : controller.signal;
151
- try {
152
- const res = await fetch(url, {
153
- method: "POST",
154
- headers: headers,
155
- signal: fetchSignal,
156
- body: JSON.stringify(payload),
157
- });
158
- logEvent(scope, "response", { requestId, provider, status: res.status, contentType: res.headers.get("content-type") });
159
- if (!res.ok) {
160
- const text = await res.text();
161
- const upstream = parseOpenAIErrorBody(text);
162
- if (res.status >= 400 && res.status < 500 && upstream?.message) {
163
- throw makeError(safeUpstreamClientMessage(upstream, res.status), {
164
- status: res.status,
165
- code: normalizedCode(upstream),
166
- upstreamBodyChars: text.length,
167
- upstreamCode: upstream.code,
168
- upstreamType: upstream.type,
169
- upstreamParam: upstream.param,
170
- upstreamMessageRedacted: true,
171
- });
172
- }
173
- throw makeError(`${provider === "api" ? "OpenAI API" : "OAuth proxy"} returned ${res.status}`, {
174
- status: res.status,
175
- upstreamBodyChars: text.length,
176
- });
177
- }
178
- if (requestId)
179
- setJobPhase(requestId, "streaming");
180
- const contentType = res.headers.get("content-type") || "";
181
- return contentType.includes("text/event-stream")
182
- ? await parseStream(res, { requestId, scope, maxImages, onPartialImage, onFinalImage })
183
- : await parseJson(res, maxImages);
184
- }
185
- catch (e) {
186
- const err = errInfo(e);
187
- if (err.name === "AbortError") {
188
- if (signal?.aborted) {
189
- throw makeError("Generation canceled", {
190
- status: 499,
191
- code: "GENERATION_CANCELED",
192
- cause: err.raw,
193
- });
194
- }
195
- throw makeError("Responses image generation timed out", { status: 504, code: "RESPONSES_IMAGE_TIMEOUT", cause: err.raw });
196
- }
197
- if (isKnownResponsesError(err.raw))
198
- throw err.raw;
199
- throw makeError("Responses request failed before receiving a response", {
200
- status: 502,
201
- code: "NETWORK_FAILED",
202
- errorName: err.name,
203
- upstreamMessageRedacted: true,
204
- });
205
- }
206
- finally {
207
- clearTimeout(timer);
208
- }
209
- }
210
- export async function generateViaResponses(provider, prompt, quality, size, moderation = "low", references = [], requestId = null, mode = "auto", ctxRaw = {}, options = {}) {
211
- const ctx = requireRuntimeContext(ctxRaw);
212
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.6-luna";
213
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
214
- const requestTools = tools(webSearchEnabled, {
215
- quality,
216
- size,
217
- moderation,
218
- ...(options.partialImages ? { partial_images: options.partialImages } : {}),
219
- ...(options.background ? { background: options.background } : {}),
220
- ...(options.outputFormat ? { output_format: options.outputFormat } : {}),
221
- });
222
- const toolChoice = imageToolChoice(options.forceImageToolChoice ?? ctx.config?.oauth?.forceImageToolChoice !== false);
223
- const toolChoiceKind = imageToolChoiceKind(toolChoice);
224
- const referenceInputs = references.map(normalizeRef);
225
- const userContent = referenceInputs.length
226
- ? [...referenceInputs, { type: "input_text", text: buildUserTextPrompt(prompt, mode, { webSearchEnabled, size }) }]
227
- : buildUserTextPrompt(prompt, mode, { webSearchEnabled, size });
228
- const result = await postResponses({
229
- ctx,
230
- provider,
231
- scope: provider === "api" ? "api-generate" : "oauth",
232
- requestId,
233
- maxImages: 1,
234
- ...(options.signal !== undefined ? { signal: options.signal } : {}),
235
- ...(options.onPartialImage !== undefined ? { onPartialImage: options.onPartialImage } : {}),
236
- ...(options.onFinalImage !== undefined ? { onFinalImage: options.onFinalImage } : {}),
237
- payload: {
238
- model,
239
- input: [
240
- { role: "developer", content: webSearchEnabled ? GENERATE_DEVELOPER_PROMPT : GENERATE_NO_SEARCH_DEVELOPER_PROMPT },
241
- { role: "user", content: userContent },
242
- ],
243
- tools: requestTools,
244
- tool_choice: toolChoice,
245
- reasoning: { effort: options.reasoningEffort || "low" },
246
- stream: true,
247
- },
248
- });
249
- const image = result.images[0];
250
- if (!image?.b64) {
251
- if (options.allowPromptOnlyOAuthFallback === true) {
252
- const fallback = await retryPromptOnlyJsonImage({
253
- postResponses,
254
- ctx,
255
- provider,
256
- prompt,
257
- mode,
258
- model,
259
- quality,
260
- size,
261
- moderation,
262
- requestId,
263
- ...(options.signal !== undefined ? { signal: options.signal } : {}),
264
- initial: result,
265
- referenceInputs,
266
- webSearchDroppedOnRetry: webSearchEnabled,
267
- ...(options.reasoningEffort !== undefined ? { reasoningEffort: options.reasoningEffort } : {}),
268
- ...(options.background !== undefined ? { background: options.background } : {}),
269
- ...(options.outputFormat !== undefined ? { outputFormat: options.outputFormat } : {}),
270
- });
271
- if (fallback)
272
- return fallback;
273
- }
274
- throw emptyResponseError("No image data received from Responses API", result, {
275
- provider,
276
- model,
277
- quality,
278
- size,
279
- moderation,
280
- webSearchEnabled,
281
- refsCount: referenceInputs.length,
282
- inputImageCount: referenceInputs.length,
283
- promptChars: typeof prompt === "string" ? prompt.length : 0,
284
- toolTypes: toolTypes(requestTools),
285
- toolChoiceKind,
286
- });
287
- }
288
- return { b64: image.b64, usage: result.usage, webSearchCalls: result.webSearchCalls, revisedPrompt: image.revisedPrompt, text: result.text };
289
- }
290
- export async function generateMultimodeViaResponses(provider, prompt, quality, size, moderation = "low", references = [], requestId = null, mode = "auto", ctxRaw = {}, options = {}) {
291
- const ctx = requireRuntimeContext(ctxRaw);
292
- const maxGeneratedImages = Math.max(1, Math.trunc(Number(ctx.config.limits.maxGeneratedImages) || 24));
293
- const maxImages = Math.min(maxGeneratedImages, Math.max(1, Math.trunc(Number(options.maxImages) || 1)));
294
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.6-luna";
295
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
296
- const requestTools = tools(webSearchEnabled, { quality, size, moderation, ...(options.partialImages ? { partial_images: options.partialImages } : {}) });
297
- const userText = buildMultimodeSequencePrompt(mode === "direct"
298
- ? `${prompt}${DIRECT_PROMPT_FIDELITY_SUFFIX}`
299
- : `${prompt}${webSearchEnabled ? "" : ""}${AUTO_PROMPT_FIDELITY_SUFFIX}`, maxImages, { webSearchEnabled, size });
300
- const referenceInputs = references.map(normalizeRef);
301
- const userContent = referenceInputs.length
302
- ? [...referenceInputs, { type: "input_text", text: userText }]
303
- : userText;
304
- return await postResponses({
305
- ctx,
306
- provider,
307
- scope: provider === "api" ? "api-multimode" : "oauth-multimode",
308
- requestId,
309
- maxImages,
310
- ...(options.signal !== undefined ? { signal: options.signal } : {}),
311
- ...(options.onPartialImage !== undefined ? { onPartialImage: options.onPartialImage } : {}),
312
- ...(options.onFinalImage !== undefined ? { onFinalImage: options.onFinalImage } : {}),
313
- payload: {
314
- model,
315
- input: [
316
- { role: "developer", content: webSearchEnabled ? MULTIMODE_DEVELOPER_PROMPT : MULTIMODE_NO_SEARCH_DEVELOPER_PROMPT },
317
- { role: "user", content: userContent },
318
- ],
319
- tools: requestTools,
320
- tool_choice: "required",
321
- reasoning: { effort: options.reasoningEffort || "low" },
322
- stream: true,
323
- },
324
- });
325
- }
326
- export async function editViaResponses(provider, prompt, imageB64, quality, size, moderation = "low", mode = "auto", ctxRaw = {}, requestId = null, options = {}) {
327
- const ctx = requireRuntimeContext(ctxRaw);
328
- const model = options.model || ctx.config?.imageModels?.default || "gpt-5.6-luna";
329
- const webSearchEnabled = options.webSearchEnabled !== false && options.searchMode !== "off";
330
- const requestTools = tools(webSearchEnabled, { quality, size, moderation });
331
- const toolChoice = imageToolChoice(options.forceImageToolChoice ?? ctx.config?.oauth?.forceImageToolChoice !== false);
332
- const toolChoiceKind = imageToolChoiceKind(toolChoice);
333
- const imageForRequest = await compressReferenceB64ForOAuth(imageB64, {
334
- maxB64Bytes: ctx.config?.limits?.maxRefB64Bytes,
335
- force: true,
336
- });
337
- const referenceImages = await Promise.all((Array.isArray(options.references) ? options.references : []).map((ref) => compressReferenceB64ForOAuth(typeof ref === "string" ? ref : ref?.b64, {
338
- maxB64Bytes: ctx.config?.limits?.maxRefB64Bytes,
339
- force: true,
340
- })));
341
- const maskContent = typeof options.mask === "string" && options.mask.length > 0
342
- ? [
343
- { type: "input_image", image_url: `data:image/png;base64,${options.mask}` },
344
- { 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." },
345
- ]
346
- : [];
347
- const userContent = [
348
- { type: "input_image", image_url: `data:image/jpeg;base64,${imageForRequest.b64}` },
349
- ...referenceImages.map(({ b64 }) => ({ type: "input_image", image_url: `data:image/jpeg;base64,${b64}` })),
350
- ...maskContent,
351
- { type: "input_text", text: buildEditTextPrompt(prompt, mode, { webSearchEnabled, size }) },
352
- ];
353
- const result = await postResponses({
354
- ctx,
355
- provider,
356
- scope: provider === "api" ? "api-edit" : "oauth-edit",
357
- requestId,
358
- maxImages: 1,
359
- signal: options.signal,
360
- payload: {
361
- model,
362
- input: [
363
- { role: "developer", content: webSearchEnabled ? EDIT_DEVELOPER_PROMPT : EDIT_NO_SEARCH_DEVELOPER_PROMPT },
364
- { role: "user", content: userContent },
365
- ],
366
- tools: requestTools,
367
- tool_choice: toolChoice,
368
- reasoning: { effort: options.reasoningEffort || "low" },
369
- stream: true,
370
- },
371
- });
372
- const image = result.images[0];
373
- if (!image?.b64) {
374
- throw emptyResponseError("No image data received from Responses edit", result, {
375
- provider,
376
- model,
377
- quality,
378
- size,
379
- moderation,
380
- webSearchEnabled,
381
- refsCount: referenceImages.length,
382
- inputImageCount: 1 + referenceImages.length + (maskContent.length ? 1 : 0),
383
- promptChars: typeof prompt === "string" ? prompt.length : 0,
384
- toolTypes: toolTypes(requestTools),
385
- toolChoiceKind,
386
- });
387
- }
388
- return { b64: image.b64, usage: result.usage, revisedPrompt: image.revisedPrompt, webSearchCalls: result.webSearchCalls };
389
- }
1
+ export { generateViaResponses, generateMultimodeViaResponses, editViaResponses, } from "./providers/adapters/openaiOperations.js";