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,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
- }