ima2-gen 1.1.21 → 1.1.23

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 (214) hide show
  1. package/README.md +44 -7
  2. package/bin/commands/video.js +14 -0
  3. package/bin/ima2.js +14 -4
  4. package/bin/lib/platform.js +34 -5
  5. package/docs/README.ko.md +43 -2
  6. package/lib/agentQueueWorker.js +6 -0
  7. package/lib/agentRuntime.js +3 -2
  8. package/lib/atomicWrite.js +14 -0
  9. package/lib/grokImageAdapter.js +6 -0
  10. package/lib/grokProxyLauncher.js +5 -3
  11. package/lib/grokVideoAdapter.js +1 -1
  12. package/lib/grokVideoPlannerPrompt.js +10 -0
  13. package/lib/inflight.js +1 -1
  14. package/lib/oauthLauncher.js +5 -0
  15. package/lib/videoFrameExtract.js +3 -3
  16. package/package.json +5 -7
  17. package/routes/capabilities.js +13 -0
  18. package/routes/edit.js +2 -1
  19. package/routes/generate.js +32 -6
  20. package/routes/health.js +4 -3
  21. package/routes/multimode.js +2 -1
  22. package/routes/video.js +35 -3
  23. package/server.js +29 -2
  24. package/skills/ima2/SKILL.md +48 -6
  25. package/ui/dist/.vite/manifest.json +12 -12
  26. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-C21zqdTZ.js} +1 -1
  27. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-BN-ga1lG.js} +1 -1
  28. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BbMa4IhI.js} +1 -1
  29. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DRwBJRDQ.js} +1 -1
  30. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-Dp85kHCq.js} +2 -2
  31. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BE8Q8MLD.js} +1 -1
  32. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-PtH5x0sc.js} +1 -1
  33. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-FnM9tHI9.js} +2 -2
  34. package/ui/dist/assets/SettingsWorkspace-MARPGyBL.js +1 -0
  35. package/ui/dist/assets/index-BAFI6htx.js +42 -0
  36. package/ui/dist/assets/{index-31uVIdt4.js → index-BSXxr_Bt.js} +1 -1
  37. package/ui/dist/assets/index-DS-ADE7U.css +1 -0
  38. package/ui/dist/index.html +2 -2
  39. package/bin/commands/annotate.ts +0 -119
  40. package/bin/commands/cancel.ts +0 -48
  41. package/bin/commands/canvas-versions.ts +0 -80
  42. package/bin/commands/capabilities.ts +0 -110
  43. package/bin/commands/cardnews.ts +0 -249
  44. package/bin/commands/comfy.ts +0 -54
  45. package/bin/commands/config.ts +0 -186
  46. package/bin/commands/defaults.ts +0 -192
  47. package/bin/commands/doctor.ts +0 -202
  48. package/bin/commands/edit.ts +0 -150
  49. package/bin/commands/gen.ts +0 -214
  50. package/bin/commands/grok.ts +0 -90
  51. package/bin/commands/history.ts +0 -146
  52. package/bin/commands/ls.ts +0 -64
  53. package/bin/commands/metadata.ts +0 -39
  54. package/bin/commands/multimode.ts +0 -196
  55. package/bin/commands/node.ts +0 -166
  56. package/bin/commands/observability.ts +0 -176
  57. package/bin/commands/ping.ts +0 -31
  58. package/bin/commands/prompt-sub/build.ts +0 -101
  59. package/bin/commands/prompt.ts +0 -492
  60. package/bin/commands/ps.ts +0 -81
  61. package/bin/commands/session.ts +0 -266
  62. package/bin/commands/show.ts +0 -72
  63. package/bin/commands/skill.ts +0 -70
  64. package/bin/commands/video.ts +0 -442
  65. package/bin/ima2.ts +0 -430
  66. package/bin/lib/args.ts +0 -92
  67. package/bin/lib/browser-id.ts +0 -16
  68. package/bin/lib/client.ts +0 -122
  69. package/bin/lib/config-store.ts +0 -120
  70. package/bin/lib/destructive-confirm.ts +0 -19
  71. package/bin/lib/doctor-checks.ts +0 -91
  72. package/bin/lib/error-hints.ts +0 -23
  73. package/bin/lib/files.ts +0 -39
  74. package/bin/lib/output.ts +0 -73
  75. package/bin/lib/platform.ts +0 -99
  76. package/bin/lib/recover-output.ts +0 -139
  77. package/bin/lib/sse.ts +0 -73
  78. package/bin/lib/star-prompt.ts +0 -97
  79. package/bin/lib/storage-doctor.ts +0 -39
  80. package/bin/lib/ui-build.ts +0 -85
  81. package/config.ts +0 -354
  82. package/lib/agentCommandParser.ts +0 -69
  83. package/lib/agentGenerationPlanner.ts +0 -273
  84. package/lib/agentQuestionResponder.ts +0 -266
  85. package/lib/agentQueueStore.ts +0 -270
  86. package/lib/agentQueueWorker.ts +0 -89
  87. package/lib/agentRuntime.ts +0 -604
  88. package/lib/agentSettings.ts +0 -72
  89. package/lib/agentStore.ts +0 -422
  90. package/lib/agentStoreRows.ts +0 -136
  91. package/lib/agentTypes.ts +0 -154
  92. package/lib/apiCachePolicy.ts +0 -11
  93. package/lib/assetLifecycle.ts +0 -146
  94. package/lib/canvasVersionStore.ts +0 -223
  95. package/lib/capabilities.ts +0 -126
  96. package/lib/cardNewsGenerator.ts +0 -271
  97. package/lib/cardNewsJobStore.ts +0 -142
  98. package/lib/cardNewsManifestStore.ts +0 -154
  99. package/lib/cardNewsPlanner.ts +0 -236
  100. package/lib/cardNewsPlannerClient.ts +0 -155
  101. package/lib/cardNewsPlannerPrompt.ts +0 -62
  102. package/lib/cardNewsPlannerSchema.ts +0 -321
  103. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  104. package/lib/cardNewsTemplateStore.ts +0 -252
  105. package/lib/codexDetect.ts +0 -71
  106. package/lib/comfyBridge.ts +0 -235
  107. package/lib/composerSnapshot.ts +0 -33
  108. package/lib/configKeys.ts +0 -62
  109. package/lib/db.ts +0 -295
  110. package/lib/errInfo.ts +0 -43
  111. package/lib/errorClassify.ts +0 -100
  112. package/lib/generationCancel.ts +0 -28
  113. package/lib/generationErrors.ts +0 -238
  114. package/lib/grokImageAdapter.ts +0 -513
  115. package/lib/grokMultimodeAdapter.ts +0 -84
  116. package/lib/grokProxyLauncher.ts +0 -153
  117. package/lib/grokRuntime.ts +0 -23
  118. package/lib/grokSizeMapper.ts +0 -71
  119. package/lib/grokVideoAdapter.ts +0 -458
  120. package/lib/grokVideoCanvas.ts +0 -26
  121. package/lib/grokVideoDownload.ts +0 -59
  122. package/lib/grokVideoPlannerPrompt.ts +0 -67
  123. package/lib/historyIndex.ts +0 -51
  124. package/lib/historyList.ts +0 -181
  125. package/lib/imageMetadata.ts +0 -113
  126. package/lib/imageMetadataStore.ts +0 -67
  127. package/lib/imageModels.ts +0 -165
  128. package/lib/inflight.ts +0 -281
  129. package/lib/localImportStore.ts +0 -114
  130. package/lib/logger.ts +0 -161
  131. package/lib/nodeStore.ts +0 -91
  132. package/lib/oauthLauncher.ts +0 -94
  133. package/lib/oauthNormalize.ts +0 -30
  134. package/lib/oauthProxy/errors.ts +0 -128
  135. package/lib/oauthProxy/generators.ts +0 -494
  136. package/lib/oauthProxy/index.ts +0 -28
  137. package/lib/oauthProxy/prompts.ts +0 -123
  138. package/lib/oauthProxy/references.ts +0 -45
  139. package/lib/oauthProxy/runtime.ts +0 -115
  140. package/lib/oauthProxy/streams.ts +0 -232
  141. package/lib/oauthProxy/types.ts +0 -9
  142. package/lib/oauthProxy.ts +0 -3
  143. package/lib/openDirectory.ts +0 -47
  144. package/lib/pngInfo.ts +0 -26
  145. package/lib/promptBuilder/attachments.ts +0 -74
  146. package/lib/promptBuilder/client.ts +0 -130
  147. package/lib/promptBuilder/constants.ts +0 -9
  148. package/lib/promptBuilder/context.ts +0 -36
  149. package/lib/promptBuilder/errors.ts +0 -12
  150. package/lib/promptBuilder/requestSchema.ts +0 -56
  151. package/lib/promptBuilder/responseParser.ts +0 -219
  152. package/lib/promptBuilder/systemPrompt.ts +0 -135
  153. package/lib/promptBuilder/transport.ts +0 -94
  154. package/lib/promptBuilder/types.ts +0 -109
  155. package/lib/promptImport/curatedSources.ts +0 -141
  156. package/lib/promptImport/discoveryRegistry.ts +0 -329
  157. package/lib/promptImport/errors.ts +0 -18
  158. package/lib/promptImport/githubDiscovery.ts +0 -309
  159. package/lib/promptImport/githubFolder.ts +0 -397
  160. package/lib/promptImport/githubSource.ts +0 -257
  161. package/lib/promptImport/gptImageHints.ts +0 -70
  162. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  163. package/lib/promptImport/promptIndex.ts +0 -326
  164. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  165. package/lib/promptImport/types.ts +0 -103
  166. package/lib/promptSafetyPolicy.ts +0 -5
  167. package/lib/providerOptions.ts +0 -56
  168. package/lib/referenceImageCompress.ts +0 -84
  169. package/lib/refs.ts +0 -133
  170. package/lib/requestLogger.ts +0 -49
  171. package/lib/responsesDoctor.ts +0 -456
  172. package/lib/responsesErrors.ts +0 -83
  173. package/lib/responsesFallback.ts +0 -114
  174. package/lib/responsesImageAdapter.ts +0 -466
  175. package/lib/responsesParse.ts +0 -452
  176. package/lib/responsesTools.ts +0 -28
  177. package/lib/runtimeContext.ts +0 -146
  178. package/lib/runtimePorts.ts +0 -105
  179. package/lib/sessionStore.ts +0 -308
  180. package/lib/storageMigration.ts +0 -310
  181. package/lib/styleSheet.ts +0 -139
  182. package/lib/systemTrash.ts +0 -20
  183. package/lib/videoContinuity.ts +0 -180
  184. package/lib/videoFrameExtract.ts +0 -78
  185. package/lib/videoSeriesChain.ts +0 -29
  186. package/lib/visibleTextLanguagePolicy.ts +0 -7
  187. package/routes/agent.ts +0 -308
  188. package/routes/annotations.ts +0 -118
  189. package/routes/canvasVersions.ts +0 -69
  190. package/routes/capabilities.ts +0 -18
  191. package/routes/cardNews.ts +0 -211
  192. package/routes/comfy.ts +0 -43
  193. package/routes/edit.ts +0 -352
  194. package/routes/generate.ts +0 -492
  195. package/routes/grok.ts +0 -24
  196. package/routes/health.ts +0 -123
  197. package/routes/history.ts +0 -221
  198. package/routes/imageImport.ts +0 -37
  199. package/routes/index.ts +0 -52
  200. package/routes/metadata.ts +0 -77
  201. package/routes/multimode.ts +0 -499
  202. package/routes/nodes.ts +0 -578
  203. package/routes/promptBuilder.ts +0 -37
  204. package/routes/promptImport.ts +0 -379
  205. package/routes/prompts.ts +0 -428
  206. package/routes/quota.ts +0 -89
  207. package/routes/sessions.ts +0 -317
  208. package/routes/storage.ts +0 -47
  209. package/routes/video.ts +0 -300
  210. package/routes/videoExtended.ts +0 -284
  211. package/server.ts +0 -293
  212. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  213. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  214. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,492 +0,0 @@
1
- import { mkdir, readFile, writeFile } from "fs/promises";
2
- import { join } from "path";
3
- import { randomBytes } from "crypto";
4
- import type { Express, Request, Response } from "express";
5
- import { detectImageMimeFromB64, summarizeReferencePayload, validateAndNormalizeRefs } from "../lib/refs.js";
6
- import { classifyUpstreamError } from "../lib/errorClassify.js";
7
- import { normalizeOAuthParams } from "../lib/oauthNormalize.js";
8
- import { resolveProviderOptions } from "../lib/providerOptions.js";
9
- import { generateViaResponses } from "../lib/responsesImageAdapter.js";
10
- import { generateViaGrok, planGrokImage } from "../lib/grokImageAdapter.js";
11
- import { isNonRetryableGenerationError, normalizeGenerationFailure, type UpstreamErr } from "../lib/generationErrors.js";
12
- import { startJob, finishJob, registerJobAbortController, isJobCanceled } from "../lib/inflight.js";
13
- import {
14
- isGenerationCanceledError,
15
- makeGenerationCanceledError,
16
- throwIfJobCanceled,
17
- } from "../lib/generationCancel.js";
18
- import { logEvent, logError } from "../lib/logger.js";
19
- import { embedImageMetadataBestEffort } from "../lib/imageMetadataStore.js";
20
- import { invalidateHistoryIndex } from "../lib/historyIndex.js";
21
- import {
22
- normalizeComposerInsertedPrompts,
23
- normalizeComposerPrompt,
24
- } from "../lib/composerSnapshot.js";
25
-
26
- import { errInfo } from "../lib/errInfo.js";
27
- import { requireRuntimeContext, type RouteRuntimeContext, type RuntimeContext } from "../lib/runtimeContext.js";
28
- function validateModeration(ctx: RuntimeContext, moderation: unknown) {
29
- if (typeof moderation !== "string" || !ctx.config.oauth.validModeration.has(moderation)) {
30
- return { error: "moderation must be one of: auto, low" };
31
- }
32
- return { moderation };
33
- }
34
-
35
- function imageFormatFromMime(mime: string | null | undefined): "png" | "jpeg" | "webp" {
36
- if (mime === "image/jpeg") return "jpeg";
37
- if (mime === "image/webp") return "webp";
38
- return "png";
39
- }
40
-
41
- export function registerGenerateRoutes(app: Express, ctxRaw: RouteRuntimeContext) {
42
- const ctx = requireRuntimeContext(ctxRaw);
43
- app.post("/api/generate", async (req: Request, res: Response) => {
44
- const requestId = typeof req.body?.requestId === "string" ? req.body.requestId : req.id;
45
- let finishStatus = "completed";
46
- let finishHttpStatus;
47
- let finishErrorCode;
48
- let finishMeta = {};
49
- let finishCanceled = false;
50
- const cancelController = new AbortController();
51
- try {
52
- const sessionId = typeof req.body?.sessionId === "string" ? req.body.sessionId : null;
53
- const clientNodeId = typeof req.body?.clientNodeId === "string" ? req.body.clientNodeId : null;
54
- const {
55
- prompt,
56
- quality: rawQuality = "medium",
57
- size = "1024x1024",
58
- format = "png",
59
- moderation = "low",
60
- provider = "auto",
61
- n = 1,
62
- references = [],
63
- mode: promptMode = "auto",
64
- model: rawModel,
65
- reasoningEffort: rawReasoningEffort,
66
- webSearchEnabled: rawWebSearchEnabled = true,
67
- } = req.body;
68
- const composerPrompt = normalizeComposerPrompt(req.body?.composerPrompt);
69
- const composerInsertedPrompts = normalizeComposerInsertedPrompts(
70
- req.body?.composerInsertedPrompts,
71
- );
72
- const { quality, warnings: qualityWarnings } = normalizeOAuthParams({ provider, quality: rawQuality });
73
- const providerOptions = resolveProviderOptions(ctx, {
74
- provider,
75
- rawModel,
76
- rawReasoningEffort,
77
- rawSize: size,
78
- rawWebSearchEnabled,
79
- });
80
- if (providerOptions.error) {
81
- finishStatus = "error";
82
- finishHttpStatus = providerOptions.status;
83
- finishErrorCode = providerOptions.code;
84
- return res.status(providerOptions.status).json({ error: providerOptions.error, code: providerOptions.code });
85
- }
86
- const imageModel = providerOptions.model;
87
- const reasoningEffort = providerOptions.reasoningEffort;
88
- const effectiveSize = providerOptions.size;
89
- const webSearchEnabled = providerOptions.webSearchEnabled;
90
- const activeProvider = providerOptions.provider;
91
- const normalizedPromptMode = promptMode === "direct" ? "direct" : "auto";
92
-
93
- if (!prompt) return res.status(400).json({ error: "Prompt is required" });
94
- const moderationCheck = validateModeration(ctx, moderation);
95
- if (moderationCheck.error) return res.status(400).json({ error: moderationCheck.error });
96
- const count = Math.min(Math.max(parseInt(n) || 1, 1), 8);
97
- const referencePayload = summarizeReferencePayload(references);
98
-
99
- startJob({
100
- requestId,
101
- kind: "classic",
102
- prompt,
103
- meta: {
104
- kind: "classic",
105
- sessionId,
106
- parentNodeId: null,
107
- clientNodeId,
108
- quality,
109
- model: imageModel,
110
- size: effectiveSize,
111
- n: count,
112
- refsCount: referencePayload.refsCount,
113
- referenceBytes: referencePayload.referenceBytes,
114
- referenceB64Chars: referencePayload.referenceB64Chars,
115
- composerPrompt,
116
- composerInsertedPrompts,
117
- },
118
- });
119
- registerJobAbortController(requestId, cancelController);
120
-
121
- const refCheckResult = validateAndNormalizeRefs(references);
122
- if (refCheckResult.error) {
123
- finishStatus = "error";
124
- finishHttpStatus = 400;
125
- finishErrorCode = refCheckResult.code;
126
- return res.status(400).json({ error: refCheckResult.error, code: refCheckResult.code });
127
- }
128
- const refCheck = refCheckResult as Extract<typeof refCheckResult, { refs: string[] }>;
129
- if (activeProvider === "grok" && refCheck.refs.length > 3) {
130
- finishStatus = "error";
131
- finishHttpStatus = 400;
132
- finishErrorCode = "GROK_REF_TOO_MANY";
133
- return res.status(400).json({
134
- error: "Grok image editing supports up to 3 reference images",
135
- code: "GROK_REF_TOO_MANY",
136
- requestId,
137
- });
138
- }
139
-
140
- const client = req.get("x-ima2-client") || "ui";
141
- const referenceDiagnostics = refCheck.referenceDiagnostics || [];
142
- const referenceMismatchCount = referenceDiagnostics.filter((ref) => ref.warnings?.includes("mime_mismatch")).length;
143
- logEvent("generate", "request", {
144
- requestId,
145
- client,
146
- provider: activeProvider,
147
- quality,
148
- model: imageModel,
149
- size: effectiveSize,
150
- moderation,
151
- n: count,
152
- refs: refCheck.refs.length,
153
- referenceBytes: referencePayload.referenceBytes,
154
- referenceMismatchCount,
155
- refDetectedMimes: [...new Set(referenceDiagnostics.map((ref) => ref.detectedMime).filter(Boolean))].join(","),
156
- refDeclaredMimes: [...new Set(referenceDiagnostics.map((ref) => ref.declaredMime).filter(Boolean))].join(","),
157
- sessionId,
158
- clientNodeId,
159
- promptChars: typeof prompt === "string" ? prompt.length : 0,
160
- promptMode: normalizedPromptMode,
161
- webSearchEnabled,
162
- });
163
- const startTime = Date.now();
164
-
165
- const mimeMap: Record<string, string> = { png: "image/png", jpeg: "image/jpeg", webp: "image/webp" };
166
- const effectiveFormat = activeProvider === "grok" ? "jpeg" : String(format);
167
- const mime = mimeMap[effectiveFormat] || "image/png";
168
- await mkdir(ctx.config.storage.generatedDir, { recursive: true });
169
-
170
- const sharedGrokPlan = activeProvider === "grok"
171
- ? await planGrokImage(prompt, ctx, {
172
- model: quality === "high" ? "grok-imagine-image-quality" : imageModel,
173
- size: effectiveSize,
174
- signal: cancelController.signal,
175
- requestId,
176
- referenceCount: refCheck.refs.length,
177
- references: refCheck.refDetails,
178
- })
179
- : null;
180
-
181
- const generateOne = async () => {
182
- if (activeProvider === "grok") {
183
- const grokModel = quality === "high" ? "grok-imagine-image-quality" : imageModel;
184
- const r = await generateViaGrok(prompt, ctx, {
185
- model: grokModel,
186
- size: effectiveSize,
187
- signal: cancelController.signal,
188
- requestId,
189
- plannedPrompt: sharedGrokPlan?.prompt,
190
- webSearchCalls: sharedGrokPlan?.webSearchCalls,
191
- references: refCheck.refDetails,
192
- });
193
- throwIfJobCanceled(requestId);
194
- return r;
195
- }
196
-
197
- const MAX_RETRIES = 1;
198
- let lastErr: unknown;
199
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
200
- try {
201
- const r = await generateViaResponses(
202
- activeProvider,
203
- prompt,
204
- quality,
205
- effectiveSize,
206
- moderation,
207
- refCheck.refDetails || refCheck.refs,
208
- requestId,
209
- normalizedPromptMode,
210
- ctx,
211
- {
212
- model: imageModel,
213
- reasoningEffort,
214
- webSearchEnabled,
215
- signal: cancelController.signal,
216
- allowPromptOnlyOAuthFallback: activeProvider !== "api",
217
- },
218
- );
219
- throwIfJobCanceled(requestId);
220
- if (r.b64) return r;
221
- lastErr = new Error("Empty response (safety refusal)");
222
- } catch (e) {
223
- lastErr = e;
224
- if (isNonRetryableGenerationError(e as UpstreamErr | null | undefined)) break;
225
- }
226
- if (attempt < MAX_RETRIES) {
227
- const errCode = (lastErr && typeof lastErr === "object" && "code" in lastErr)
228
- ? (lastErr as { code?: unknown }).code
229
- : undefined;
230
- logEvent("generate", "retry", { requestId, attempt: attempt + 1, errorCode: errCode });
231
- }
232
- }
233
- throw normalizeGenerationFailure(lastErr as UpstreamErr | null | undefined, {
234
- safetyMessage: "Content generation refused after retries",
235
- });
236
- };
237
-
238
- const results = await Promise.allSettled(Array.from({ length: count }, generateOne));
239
- throwIfJobCanceled(requestId);
240
- const images: Array<{ image: string; filename: string; revisedPrompt: any }> = [];
241
- let totalUsage: Record<string, number> | null = null;
242
- let totalWebSearchCalls = 0;
243
- let firstRetryMeta: Record<string, unknown> | null = null;
244
- for (const r of results) {
245
- if (r.status === "fulfilled" && r.value.b64) {
246
- throwIfJobCanceled(requestId);
247
- const valueWithMime = r.value as typeof r.value & { mime?: string };
248
- const resultMime = activeProvider === "grok"
249
- ? (valueWithMime.mime || detectImageMimeFromB64(r.value.b64) || mime)
250
- : mime;
251
- const resultFormat = activeProvider === "grok" ? imageFormatFromMime(resultMime) : effectiveFormat;
252
- const retryValue = r.value as typeof r.value & {
253
- retryKind?: string;
254
- initialEventCount?: number;
255
- initialEventTypes?: unknown;
256
- referencesDroppedOnRetry?: boolean;
257
- developerPromptDroppedOnRetry?: boolean;
258
- webSearchDroppedOnRetry?: boolean;
259
- };
260
- if (!firstRetryMeta && retryValue.retryKind) {
261
- firstRetryMeta = {
262
- retryKind: retryValue.retryKind,
263
- initialEventCount: retryValue.initialEventCount ?? null,
264
- initialEventTypes: retryValue.initialEventTypes || null,
265
- referencesDroppedOnRetry: retryValue.referencesDroppedOnRetry ?? null,
266
- developerPromptDroppedOnRetry: retryValue.developerPromptDroppedOnRetry ?? null,
267
- webSearchDroppedOnRetry: retryValue.webSearchDroppedOnRetry ?? null,
268
- };
269
- }
270
- const rand = randomBytes(ctx.config.ids.generatedHexBytes).toString("hex");
271
- const filename = `${Date.now()}_${rand}_${images.length}.${resultFormat}`;
272
- const meta = {
273
- kind: "classic",
274
- requestId,
275
- sessionId,
276
- clientNodeId,
277
- prompt,
278
- userPrompt: prompt,
279
- revisedPrompt: r.value.revisedPrompt || null,
280
- promptMode: normalizedPromptMode,
281
- composerPrompt,
282
- composerInsertedPrompts,
283
- quality,
284
- size: effectiveSize,
285
- format: resultFormat,
286
- moderation,
287
- model: activeProvider === "grok" ? (quality === "high" ? "grok-imagine-image-quality" : imageModel) : imageModel,
288
- reasoningEffort,
289
- provider: activeProvider,
290
- createdAt: Date.now(),
291
- usage: r.value.usage || null,
292
- webSearchCalls: r.value.webSearchCalls || 0,
293
- webSearchEnabled,
294
- refsCount: refCheck.refs.length,
295
- };
296
- const rawBuffer = Buffer.from(r.value.b64, "base64");
297
- const embedded: any = await embedImageMetadataBestEffort(rawBuffer, resultFormat, meta, {
298
- version: ctx.packageVersion,
299
- });
300
- if (!embedded.embedded) {
301
- logEvent("generate", "metadata_embed_skipped", {
302
- requestId,
303
- filename,
304
- code: embedded.code,
305
- warning: embedded.warning,
306
- });
307
- }
308
- await writeFile(join(ctx.config.storage.generatedDir, filename), embedded.buffer);
309
- await writeFile(join(ctx.config.storage.generatedDir, filename + ".json"), JSON.stringify(meta)).catch(() => {});
310
- invalidateHistoryIndex();
311
- images.push({
312
- image: `data:${resultMime};base64,${r.value.b64}`,
313
- filename,
314
- revisedPrompt: r.value.revisedPrompt || null,
315
- });
316
- if (r.value.usage) {
317
- const usageValue = r.value.usage;
318
- if (!totalUsage) totalUsage = { ...usageValue };
319
- else {
320
- const tu = totalUsage;
321
- Object.keys(usageValue).forEach((k) => {
322
- if (typeof usageValue[k] === "number") tu[k] = (tu[k] || 0) + usageValue[k];
323
- });
324
- }
325
- }
326
- if (typeof r.value.webSearchCalls === "number") {
327
- totalWebSearchCalls = activeProvider === "grok"
328
- ? Math.max(totalWebSearchCalls, r.value.webSearchCalls)
329
- : totalWebSearchCalls + r.value.webSearchCalls;
330
- }
331
- } else if (r.status === "rejected") {
332
- logError("generate", "parallel_failed", r.reason, { requestId });
333
- }
334
- }
335
-
336
- if (images.length === 0) {
337
- const firstErr = results.find((r) => r.status === "rejected")?.reason;
338
- if (firstErr?.code) {
339
- const status = firstErr.status || 500;
340
- if (isGenerationCanceledError(firstErr)) {
341
- finishCanceled = true;
342
- finishHttpStatus = firstErr.status;
343
- finishErrorCode = firstErr.code;
344
- return res.status(firstErr.status).json({
345
- error: firstErr.message,
346
- code: firstErr.code,
347
- requestId,
348
- });
349
- }
350
- finishStatus = "error";
351
- finishHttpStatus = status;
352
- finishErrorCode = firstErr.code;
353
- return res.status(status).json({
354
- error: firstErr.message,
355
- code: firstErr.code,
356
- upstreamCode: firstErr.upstreamCode || null,
357
- upstreamType: firstErr.upstreamType || null,
358
- upstreamParam: firstErr.upstreamParam || null,
359
- diagnosticReason: firstErr.diagnosticReason || null,
360
- retryKind: firstErr.retryKind || null,
361
- initialEventCount: firstErr.initialEventCount ?? null,
362
- initialEventTypes: firstErr.initialEventTypes || null,
363
- referencesDroppedOnRetry: firstErr.referencesDroppedOnRetry ?? null,
364
- developerPromptDroppedOnRetry: firstErr.developerPromptDroppedOnRetry ?? null,
365
- webSearchDroppedOnRetry: firstErr.webSearchDroppedOnRetry ?? null,
366
- fallbackEventCount: firstErr.fallbackEventCount ?? null,
367
- fallbackEventTypes: firstErr.fallbackEventTypes || null,
368
- fallbackImageCallSeen: firstErr.fallbackImageCallSeen ?? null,
369
- fallbackImageResultCount: firstErr.fallbackImageResultCount ?? null,
370
- errorEventCount: firstErr.eventCount ?? null,
371
- eventTypes: firstErr.eventTypes || null,
372
- webSearchCalls: firstErr.webSearchCalls ?? null,
373
- responseDiagnostics: firstErr.responseDiagnostics || null,
374
- toolTypes: firstErr.toolTypes || null,
375
- toolChoiceKind: firstErr.toolChoiceKind || null,
376
- requestId,
377
- });
378
- }
379
- finishStatus = "error";
380
- finishHttpStatus = 500;
381
- finishErrorCode = "GENERATE_ALL_FAILED";
382
- return res.status(500).json({ error: "All generation attempts failed" });
383
- }
384
-
385
- const elapsed = +((Date.now() - startTime) / 1000).toFixed(1);
386
- // Persist elapsed (computed after the generation loop) into each image's sidecar.
387
- // forward-fix: only newly generated items get elapsed. The embedded XMP is written
388
- // earlier in the loop (before elapsed exists), so history reload relies on this sidecar patch.
389
- await Promise.all(
390
- images.map(async ({ filename }) => {
391
- try {
392
- const sidecarPath = join(ctx.config.storage.generatedDir, filename + ".json");
393
- const sidecarMeta = JSON.parse(await readFile(sidecarPath, "utf-8"));
394
- sidecarMeta.elapsed = elapsed;
395
- await writeFile(sidecarPath, JSON.stringify(sidecarMeta));
396
- } catch {
397
- /* best-effort, matches the sidecar write .catch(() => {}) above */
398
- }
399
- }),
400
- );
401
- const firstRevised = images[0]?.revisedPrompt || null;
402
- const extra = {
403
- usage: totalUsage,
404
- provider: activeProvider,
405
- reasoningEffort,
406
- webSearchCalls: totalWebSearchCalls,
407
- quality,
408
- size: effectiveSize,
409
- moderation,
410
- model: imageModel,
411
- warnings: qualityWarnings,
412
- revisedPrompt: firstRevised,
413
- promptMode: normalizedPromptMode,
414
- webSearchEnabled,
415
- ...(firstRetryMeta || {}),
416
- };
417
-
418
- if (count === 1) {
419
- finishHttpStatus = 200;
420
- finishMeta = { filenames: [images[0].filename], imageCount: 1 };
421
- logEvent("generate", "saved", {
422
- requestId,
423
- imageCount: 1,
424
- elapsedMs: Date.now() - startTime,
425
- filename: images[0].filename,
426
- });
427
- res.json({ image: images[0].image, elapsed, filename: images[0].filename, requestId, ...extra });
428
- } else {
429
- finishHttpStatus = 200;
430
- finishMeta = { filenames: images.map((image) => image.filename), imageCount: images.length };
431
- logEvent("generate", "saved", {
432
- requestId,
433
- imageCount: images.length,
434
- elapsedMs: Date.now() - startTime,
435
- });
436
- res.json({ images, elapsed, count: images.length, requestId, ...extra });
437
- }
438
- } catch (e) {
439
- const err = errInfo(e);
440
- const ext = (err.raw && typeof err.raw === "object" ? err.raw as Record<string, unknown> : {});
441
- const fallbackCode = err.code || classifyUpstreamError(err.message);
442
- if (isGenerationCanceledError(err.raw) || isJobCanceled(requestId)) {
443
- const canceled = makeGenerationCanceledError();
444
- finishCanceled = true;
445
- finishHttpStatus = canceled.status;
446
- finishErrorCode = canceled.code;
447
- return res.status(canceled.status).json({
448
- error: canceled.message,
449
- code: canceled.code,
450
- requestId,
451
- });
452
- }
453
- finishStatus = "error";
454
- finishHttpStatus = err.status || 500;
455
- finishErrorCode = fallbackCode || "GENERATE_FAILED";
456
- logError("generate", "error", err.raw, { requestId, code: finishErrorCode });
457
- res.status(err.status || 500).json({
458
- error: err.message,
459
- code: fallbackCode,
460
- upstreamCode: ext.upstreamCode || null,
461
- upstreamType: ext.upstreamType || null,
462
- upstreamParam: ext.upstreamParam || null,
463
- diagnosticReason: ext.diagnosticReason || null,
464
- retryKind: ext.retryKind || null,
465
- initialEventCount: ext.initialEventCount ?? null,
466
- initialEventTypes: ext.initialEventTypes || null,
467
- referencesDroppedOnRetry: ext.referencesDroppedOnRetry ?? null,
468
- developerPromptDroppedOnRetry: ext.developerPromptDroppedOnRetry ?? null,
469
- webSearchDroppedOnRetry: ext.webSearchDroppedOnRetry ?? null,
470
- fallbackEventCount: ext.fallbackEventCount ?? null,
471
- fallbackEventTypes: ext.fallbackEventTypes || null,
472
- fallbackImageCallSeen: ext.fallbackImageCallSeen ?? null,
473
- fallbackImageResultCount: ext.fallbackImageResultCount ?? null,
474
- errorEventCount: ext.eventCount ?? null,
475
- eventTypes: ext.eventTypes || null,
476
- webSearchCalls: ext.webSearchCalls ?? null,
477
- responseDiagnostics: ext.responseDiagnostics || null,
478
- toolTypes: ext.toolTypes || null,
479
- toolChoiceKind: ext.toolChoiceKind || null,
480
- requestId,
481
- });
482
- } finally {
483
- finishJob(requestId, {
484
- canceled: finishCanceled,
485
- status: finishStatus,
486
- httpStatus: finishHttpStatus,
487
- errorCode: finishErrorCode,
488
- meta: finishMeta,
489
- });
490
- }
491
- });
492
- }
package/routes/grok.ts DELETED
@@ -1,24 +0,0 @@
1
- import type { Express } from "express";
2
- import type { RouteRuntimeContext } from "../lib/runtimeContext.js";
3
- import { getGrokProxyUrl } from "../lib/grokRuntime.js";
4
-
5
- export function registerGrokRoutes(app: Express, ctx: RouteRuntimeContext) {
6
- app.get("/api/grok/status", async (_req, res) => {
7
- const grokCfg = (ctx.config as any).grokProvider || {};
8
- const timeoutMs = grokCfg.statusTimeoutMs || 3000;
9
- try {
10
- const r = await fetch(getGrokProxyUrl(ctx, "/v1/models"), {
11
- signal: AbortSignal.timeout(timeoutMs),
12
- });
13
- if (r.ok) {
14
- const data: any = await r.json();
15
- const models: string[] = data?.data?.map((m: any) => m.id).filter(Boolean) || [];
16
- const hasImageModel = models.some((m: string) => m.startsWith("grok-imagine"));
17
- return res.json({ status: hasImageModel ? "ready" : "no_image_model", models });
18
- }
19
- return res.json({ status: "error", reason: `HTTP ${r.status}` });
20
- } catch {
21
- return res.json({ status: "offline" });
22
- }
23
- });
24
- }
package/routes/health.ts DELETED
@@ -1,123 +0,0 @@
1
- import type { Express, Request, Response } from "express";
2
- import { abortJob, listJobs, listTerminalJobs } from "../lib/inflight.js";
3
-
4
- import { errInfo } from "../lib/errInfo.js";
5
- import { requireRuntimeContext, type RouteRuntimeContext } from "../lib/runtimeContext.js";
6
- export function registerHealthRoutes(app: Express, ctxRaw: RouteRuntimeContext) {
7
- const ctx = requireRuntimeContext(ctxRaw);
8
- const runtimePorts = () => ({
9
- backend: {
10
- configuredPort: Number(ctx.serverConfiguredPort || ctx.config.server.port),
11
- actualPort: Number(ctx.serverActualPort || ctx.config.server.port),
12
- url: ctx.serverUrl || `http://localhost:${ctx.serverActualPort || ctx.config.server.port}`,
13
- },
14
- oauth: {
15
- configuredPort: Number(ctx.oauthPort),
16
- actualPort: Number(ctx.oauthActualPort || ctx.oauthPort),
17
- url: ctx.oauthUrl,
18
- status: ctx.oauthReadyState,
19
- },
20
- grok: {
21
- configuredPort: Number(ctx.grokPort),
22
- actualPort: Number(ctx.grokActualPort || ctx.grokPort),
23
- url: ctx.grokUrl,
24
- },
25
- });
26
-
27
- app.get("/api/providers", (_req: Request, res: Response) => {
28
- res.json({
29
- apiKey: Boolean(ctx.hasApiKey),
30
- oauth: true,
31
- oauthPort: ctx.oauthPort,
32
- oauthActualPort: ctx.oauthActualPort || ctx.oauthPort,
33
- oauthUrl: ctx.oauthUrl,
34
- apiKeyDisabled: false,
35
- apiKeySource: ctx.apiKeySource ?? "none",
36
- runtime: runtimePorts(),
37
- });
38
- });
39
-
40
- app.get("/api/health", (_req: Request, res: Response) => {
41
- res.json({
42
- ok: true,
43
- version: ctx.packageVersion,
44
- provider: "oauth",
45
- uptimeSec: Math.round(process.uptime()),
46
- activeJobs: listJobs().length,
47
- pid: process.pid,
48
- startedAt: ctx.startedAt,
49
- runtime: runtimePorts(),
50
- });
51
- });
52
-
53
- app.get("/api/oauth/status", async (_req: Request, res: Response) => {
54
- if (ctx.oauthReadyState === "starting") {
55
- return res.json({ status: "starting", runtime: runtimePorts() });
56
- }
57
- if (ctx.oauthReadyState === "failed") {
58
- return res.json({ status: "offline", runtime: runtimePorts() });
59
- }
60
- try {
61
- const r = await fetch(`${ctx.oauthUrl}/v1/models`, {
62
- signal: AbortSignal.timeout(ctx.config.oauth.statusTimeoutMs),
63
- });
64
- if (r.ok) {
65
- const data = (await r.json()) as { data?: Array<{ id: string }> };
66
- res.json({ status: "ready", models: data.data?.map((m) => m.id) || [], runtime: runtimePorts() });
67
- } else {
68
- res.json({ status: "auth_required", runtime: runtimePorts() });
69
- }
70
- } catch {
71
- res.json({ status: "offline", runtime: runtimePorts() });
72
- }
73
- });
74
-
75
- app.get("/api/inflight", (req: Request, res: Response) => {
76
- const kind =
77
- typeof req.query.kind === "string" && req.query.kind.length > 0
78
- ? req.query.kind
79
- : undefined;
80
- const sessionId =
81
- typeof req.query.sessionId === "string" && req.query.sessionId.length > 0
82
- ? req.query.sessionId
83
- : undefined;
84
- const includeTerminal =
85
- req.query.includeTerminal === "1" || req.query.includeTerminal === "true";
86
- const jobs = listJobs({ kind, sessionId });
87
- if (!includeTerminal) return res.json({ jobs });
88
- return res.json({
89
- jobs,
90
- terminalJobs: listTerminalJobs({ kind, sessionId }),
91
- });
92
- });
93
-
94
- app.delete("/api/inflight/:requestId", (req: Request<{ requestId: string }>, res: Response) => {
95
- res.json(abortJob(req.params.requestId));
96
- });
97
-
98
- app.get("/api/billing", async (_req: Request, res: Response) => {
99
- if (!ctx.hasApiKey) {
100
- return res.json({ oauth: true, apiKeyValid: false, apiKeySource: "none" });
101
- }
102
-
103
- try {
104
- const headers = { Authorization: `Bearer ${ctx.apiKey}`, "Content-Type": "application/json" };
105
- const start = Math.floor(new Date(new Date().getFullYear(), new Date().getMonth(), 1).getTime() / 1000);
106
- const end = Math.floor(Date.now() / 1000);
107
- const [subRes, usageRes, modelsRes] = await Promise.allSettled([
108
- fetch(`https://api.openai.com/v1/organization/costs?start_time=${start}&end_time=${end}&bucket_width=1d&limit=31`, { headers }),
109
- fetch("https://api.openai.com/dashboard/billing/credit_grants", { headers }),
110
- fetch("https://api.openai.com/v1/models", { headers }),
111
- ]);
112
-
113
- const billing: Record<string, unknown> = { apiKeySource: ctx.apiKeySource ?? "env" };
114
- if (subRes.status === "fulfilled" && subRes.value.ok) billing.costs = await subRes.value.json();
115
- if (usageRes.status === "fulfilled" && usageRes.value.ok) billing.credits = await usageRes.value.json();
116
- billing.apiKeyValid = modelsRes.status === "fulfilled" && modelsRes.value.ok === true;
117
- res.json(billing);
118
- } catch (e) {
119
- const err = errInfo(e);
120
- res.status(500).json({ error: err.message, apiKeyValid: false });
121
- }
122
- });
123
- }