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.
- package/README.md +44 -7
- package/bin/commands/video.js +14 -0
- package/bin/ima2.js +14 -4
- package/bin/lib/platform.js +34 -5
- package/docs/README.ko.md +43 -2
- package/lib/agentQueueWorker.js +6 -0
- package/lib/agentRuntime.js +3 -2
- package/lib/atomicWrite.js +14 -0
- package/lib/grokImageAdapter.js +6 -0
- package/lib/grokProxyLauncher.js +5 -3
- package/lib/grokVideoAdapter.js +1 -1
- package/lib/grokVideoPlannerPrompt.js +10 -0
- package/lib/inflight.js +1 -1
- package/lib/oauthLauncher.js +5 -0
- package/lib/videoFrameExtract.js +3 -3
- package/package.json +5 -7
- package/routes/capabilities.js +13 -0
- package/routes/edit.js +2 -1
- package/routes/generate.js +32 -6
- package/routes/health.js +4 -3
- package/routes/multimode.js +2 -1
- package/routes/video.js +35 -3
- package/server.js +29 -2
- package/skills/ima2/SKILL.md +48 -6
- package/ui/dist/.vite/manifest.json +12 -12
- package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-C21zqdTZ.js} +1 -1
- package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-BN-ga1lG.js} +1 -1
- package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BbMa4IhI.js} +1 -1
- package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DRwBJRDQ.js} +1 -1
- package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-Dp85kHCq.js} +2 -2
- package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BE8Q8MLD.js} +1 -1
- package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-PtH5x0sc.js} +1 -1
- package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-FnM9tHI9.js} +2 -2
- package/ui/dist/assets/SettingsWorkspace-MARPGyBL.js +1 -0
- package/ui/dist/assets/index-BAFI6htx.js +42 -0
- package/ui/dist/assets/{index-31uVIdt4.js → index-BSXxr_Bt.js} +1 -1
- package/ui/dist/assets/index-DS-ADE7U.css +1 -0
- package/ui/dist/index.html +2 -2
- package/bin/commands/annotate.ts +0 -119
- package/bin/commands/cancel.ts +0 -48
- package/bin/commands/canvas-versions.ts +0 -80
- package/bin/commands/capabilities.ts +0 -110
- package/bin/commands/cardnews.ts +0 -249
- package/bin/commands/comfy.ts +0 -54
- package/bin/commands/config.ts +0 -186
- package/bin/commands/defaults.ts +0 -192
- package/bin/commands/doctor.ts +0 -202
- package/bin/commands/edit.ts +0 -150
- package/bin/commands/gen.ts +0 -214
- package/bin/commands/grok.ts +0 -90
- package/bin/commands/history.ts +0 -146
- package/bin/commands/ls.ts +0 -64
- package/bin/commands/metadata.ts +0 -39
- package/bin/commands/multimode.ts +0 -196
- package/bin/commands/node.ts +0 -166
- package/bin/commands/observability.ts +0 -176
- package/bin/commands/ping.ts +0 -31
- package/bin/commands/prompt-sub/build.ts +0 -101
- package/bin/commands/prompt.ts +0 -492
- package/bin/commands/ps.ts +0 -81
- package/bin/commands/session.ts +0 -266
- package/bin/commands/show.ts +0 -72
- package/bin/commands/skill.ts +0 -70
- package/bin/commands/video.ts +0 -442
- package/bin/ima2.ts +0 -430
- package/bin/lib/args.ts +0 -92
- package/bin/lib/browser-id.ts +0 -16
- package/bin/lib/client.ts +0 -122
- package/bin/lib/config-store.ts +0 -120
- package/bin/lib/destructive-confirm.ts +0 -19
- package/bin/lib/doctor-checks.ts +0 -91
- package/bin/lib/error-hints.ts +0 -23
- package/bin/lib/files.ts +0 -39
- package/bin/lib/output.ts +0 -73
- package/bin/lib/platform.ts +0 -99
- package/bin/lib/recover-output.ts +0 -139
- package/bin/lib/sse.ts +0 -73
- package/bin/lib/star-prompt.ts +0 -97
- package/bin/lib/storage-doctor.ts +0 -39
- package/bin/lib/ui-build.ts +0 -85
- package/config.ts +0 -354
- package/lib/agentCommandParser.ts +0 -69
- package/lib/agentGenerationPlanner.ts +0 -273
- package/lib/agentQuestionResponder.ts +0 -266
- package/lib/agentQueueStore.ts +0 -270
- package/lib/agentQueueWorker.ts +0 -89
- package/lib/agentRuntime.ts +0 -604
- package/lib/agentSettings.ts +0 -72
- package/lib/agentStore.ts +0 -422
- package/lib/agentStoreRows.ts +0 -136
- package/lib/agentTypes.ts +0 -154
- package/lib/apiCachePolicy.ts +0 -11
- package/lib/assetLifecycle.ts +0 -146
- package/lib/canvasVersionStore.ts +0 -223
- package/lib/capabilities.ts +0 -126
- package/lib/cardNewsGenerator.ts +0 -271
- package/lib/cardNewsJobStore.ts +0 -142
- package/lib/cardNewsManifestStore.ts +0 -154
- package/lib/cardNewsPlanner.ts +0 -236
- package/lib/cardNewsPlannerClient.ts +0 -155
- package/lib/cardNewsPlannerPrompt.ts +0 -62
- package/lib/cardNewsPlannerSchema.ts +0 -321
- package/lib/cardNewsRoleTemplateStore.ts +0 -47
- package/lib/cardNewsTemplateStore.ts +0 -252
- package/lib/codexDetect.ts +0 -71
- package/lib/comfyBridge.ts +0 -235
- package/lib/composerSnapshot.ts +0 -33
- package/lib/configKeys.ts +0 -62
- package/lib/db.ts +0 -295
- package/lib/errInfo.ts +0 -43
- package/lib/errorClassify.ts +0 -100
- package/lib/generationCancel.ts +0 -28
- package/lib/generationErrors.ts +0 -238
- package/lib/grokImageAdapter.ts +0 -513
- package/lib/grokMultimodeAdapter.ts +0 -84
- package/lib/grokProxyLauncher.ts +0 -153
- package/lib/grokRuntime.ts +0 -23
- package/lib/grokSizeMapper.ts +0 -71
- package/lib/grokVideoAdapter.ts +0 -458
- package/lib/grokVideoCanvas.ts +0 -26
- package/lib/grokVideoDownload.ts +0 -59
- package/lib/grokVideoPlannerPrompt.ts +0 -67
- package/lib/historyIndex.ts +0 -51
- package/lib/historyList.ts +0 -181
- package/lib/imageMetadata.ts +0 -113
- package/lib/imageMetadataStore.ts +0 -67
- package/lib/imageModels.ts +0 -165
- package/lib/inflight.ts +0 -281
- package/lib/localImportStore.ts +0 -114
- package/lib/logger.ts +0 -161
- package/lib/nodeStore.ts +0 -91
- package/lib/oauthLauncher.ts +0 -94
- package/lib/oauthNormalize.ts +0 -30
- package/lib/oauthProxy/errors.ts +0 -128
- package/lib/oauthProxy/generators.ts +0 -494
- package/lib/oauthProxy/index.ts +0 -28
- package/lib/oauthProxy/prompts.ts +0 -123
- package/lib/oauthProxy/references.ts +0 -45
- package/lib/oauthProxy/runtime.ts +0 -115
- package/lib/oauthProxy/streams.ts +0 -232
- package/lib/oauthProxy/types.ts +0 -9
- package/lib/oauthProxy.ts +0 -3
- package/lib/openDirectory.ts +0 -47
- package/lib/pngInfo.ts +0 -26
- package/lib/promptBuilder/attachments.ts +0 -74
- package/lib/promptBuilder/client.ts +0 -130
- package/lib/promptBuilder/constants.ts +0 -9
- package/lib/promptBuilder/context.ts +0 -36
- package/lib/promptBuilder/errors.ts +0 -12
- package/lib/promptBuilder/requestSchema.ts +0 -56
- package/lib/promptBuilder/responseParser.ts +0 -219
- package/lib/promptBuilder/systemPrompt.ts +0 -135
- package/lib/promptBuilder/transport.ts +0 -94
- package/lib/promptBuilder/types.ts +0 -109
- package/lib/promptImport/curatedSources.ts +0 -141
- package/lib/promptImport/discoveryRegistry.ts +0 -329
- package/lib/promptImport/errors.ts +0 -18
- package/lib/promptImport/githubDiscovery.ts +0 -309
- package/lib/promptImport/githubFolder.ts +0 -397
- package/lib/promptImport/githubSource.ts +0 -257
- package/lib/promptImport/gptImageHints.ts +0 -70
- package/lib/promptImport/parsePromptCandidates.ts +0 -179
- package/lib/promptImport/promptIndex.ts +0 -326
- package/lib/promptImport/rankPromptCandidates.ts +0 -65
- package/lib/promptImport/types.ts +0 -103
- package/lib/promptSafetyPolicy.ts +0 -5
- package/lib/providerOptions.ts +0 -56
- package/lib/referenceImageCompress.ts +0 -84
- package/lib/refs.ts +0 -133
- package/lib/requestLogger.ts +0 -49
- package/lib/responsesDoctor.ts +0 -456
- package/lib/responsesErrors.ts +0 -83
- package/lib/responsesFallback.ts +0 -114
- package/lib/responsesImageAdapter.ts +0 -466
- package/lib/responsesParse.ts +0 -452
- package/lib/responsesTools.ts +0 -28
- package/lib/runtimeContext.ts +0 -146
- package/lib/runtimePorts.ts +0 -105
- package/lib/sessionStore.ts +0 -308
- package/lib/storageMigration.ts +0 -310
- package/lib/styleSheet.ts +0 -139
- package/lib/systemTrash.ts +0 -20
- package/lib/videoContinuity.ts +0 -180
- package/lib/videoFrameExtract.ts +0 -78
- package/lib/videoSeriesChain.ts +0 -29
- package/lib/visibleTextLanguagePolicy.ts +0 -7
- package/routes/agent.ts +0 -308
- package/routes/annotations.ts +0 -118
- package/routes/canvasVersions.ts +0 -69
- package/routes/capabilities.ts +0 -18
- package/routes/cardNews.ts +0 -211
- package/routes/comfy.ts +0 -43
- package/routes/edit.ts +0 -352
- package/routes/generate.ts +0 -492
- package/routes/grok.ts +0 -24
- package/routes/health.ts +0 -123
- package/routes/history.ts +0 -221
- package/routes/imageImport.ts +0 -37
- package/routes/index.ts +0 -52
- package/routes/metadata.ts +0 -77
- package/routes/multimode.ts +0 -499
- package/routes/nodes.ts +0 -578
- package/routes/promptBuilder.ts +0 -37
- package/routes/promptImport.ts +0 -379
- package/routes/prompts.ts +0 -428
- package/routes/quota.ts +0 -89
- package/routes/sessions.ts +0 -317
- package/routes/storage.ts +0 -47
- package/routes/video.ts +0 -300
- package/routes/videoExtended.ts +0 -284
- package/server.ts +0 -293
- package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
- package/ui/dist/assets/index-CjgnNtgt.css +0 -1
- package/ui/dist/assets/index-Da2s4_-5.js +0 -36
package/routes/multimode.ts
DELETED
|
@@ -1,499 +0,0 @@
|
|
|
1
|
-
import { mkdir, 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 { generateMultimodeViaResponses } from "../lib/responsesImageAdapter.js";
|
|
10
|
-
import { generateMultimodeViaGrok } from "../lib/grokMultimodeAdapter.js";
|
|
11
|
-
import { startJob, finishJob, registerJobAbortController, isJobCanceled } from "../lib/inflight.js";
|
|
12
|
-
import {
|
|
13
|
-
isGenerationCanceledError,
|
|
14
|
-
makeGenerationCanceledError,
|
|
15
|
-
throwIfJobCanceled,
|
|
16
|
-
} from "../lib/generationCancel.js";
|
|
17
|
-
import { logEvent, logError } from "../lib/logger.js";
|
|
18
|
-
import { embedImageMetadataBestEffort } from "../lib/imageMetadataStore.js";
|
|
19
|
-
import { invalidateHistoryIndex } from "../lib/historyIndex.js";
|
|
20
|
-
import {
|
|
21
|
-
normalizeComposerInsertedPrompts,
|
|
22
|
-
normalizeComposerPrompt,
|
|
23
|
-
} from "../lib/composerSnapshot.js";
|
|
24
|
-
|
|
25
|
-
import { errInfo } from "../lib/errInfo.js";
|
|
26
|
-
import { requireRuntimeContext, type RouteRuntimeContext, type RuntimeContext } from "../lib/runtimeContext.js";
|
|
27
|
-
function sendSse(res: Response, event: string, data: unknown) {
|
|
28
|
-
res.write(`event: ${event}\n`);
|
|
29
|
-
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function validateModeration(ctx: RuntimeContext, moderation: unknown) {
|
|
33
|
-
if (typeof moderation !== "string" || !ctx.config.oauth.validModeration.has(moderation)) {
|
|
34
|
-
return { error: "moderation must be one of: auto, low" };
|
|
35
|
-
}
|
|
36
|
-
return { moderation };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function normalizeMaxImages(value: unknown): number {
|
|
40
|
-
return Math.min(8, Math.max(1, Math.trunc(Number(value) || 1)));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function sequenceStatus(returned: number, requested: number): "empty" | "partial" | "complete" {
|
|
44
|
-
if (returned <= 0) return "empty";
|
|
45
|
-
if (returned < requested) return "partial";
|
|
46
|
-
return "complete";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
interface MultimodeImage {
|
|
50
|
-
b64: string;
|
|
51
|
-
revisedPrompt?: string | null;
|
|
52
|
-
mime?: string | null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function imageFormatFromMime(mime: string | null | undefined): "png" | "jpeg" | "webp" {
|
|
56
|
-
if (mime === "image/jpeg") return "jpeg";
|
|
57
|
-
if (mime === "image/webp") return "webp";
|
|
58
|
-
return "png";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
type MultimodeRouteItem = {
|
|
62
|
-
image: string;
|
|
63
|
-
filename: string;
|
|
64
|
-
revisedPrompt: string | null;
|
|
65
|
-
sequenceId: string;
|
|
66
|
-
sequenceIndex: number;
|
|
67
|
-
sequenceTotalRequested: number;
|
|
68
|
-
sequenceTotalReturned: number;
|
|
69
|
-
sequenceStatus: ReturnType<typeof sequenceStatus>;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export function registerMultimodeRoutes(app: Express, ctxRaw: RouteRuntimeContext) {
|
|
73
|
-
const ctx = requireRuntimeContext(ctxRaw);
|
|
74
|
-
app.post("/api/generate/multimode", async (req: Request, res: Response) => {
|
|
75
|
-
const requestId = typeof req.body?.requestId === "string" ? req.body.requestId : req.id;
|
|
76
|
-
let finishStatus = "completed";
|
|
77
|
-
let finishHttpStatus = 200;
|
|
78
|
-
let finishErrorCode;
|
|
79
|
-
let finishMeta = {};
|
|
80
|
-
let finishCanceled = false;
|
|
81
|
-
const cancelController = new AbortController();
|
|
82
|
-
const images: MultimodeRouteItem[] = [];
|
|
83
|
-
const persistedIndexes = new Set<number>();
|
|
84
|
-
let routeMaxImages = 0;
|
|
85
|
-
let routeSequenceId = "";
|
|
86
|
-
let routeStartTime = Date.now();
|
|
87
|
-
let routeActiveProvider = "auto";
|
|
88
|
-
let routeQuality = "medium";
|
|
89
|
-
let routeEffectiveSize = "1024x1024";
|
|
90
|
-
let routeModeration = "low";
|
|
91
|
-
let routeImageModel: string | null = null;
|
|
92
|
-
let routeWebSearchEnabled = true;
|
|
93
|
-
let routePromptMode: "auto" | "direct" = "auto";
|
|
94
|
-
let routeQualityWarnings: unknown[] = [];
|
|
95
|
-
let routeComposerPrompt: string | null = null;
|
|
96
|
-
let routeComposerInsertedPrompts: ReturnType<typeof normalizeComposerInsertedPrompts> = [];
|
|
97
|
-
let latestUsage: Record<string, number> | null = null;
|
|
98
|
-
let latestWebSearchCalls = 0;
|
|
99
|
-
let latestExtraIgnored = 0;
|
|
100
|
-
|
|
101
|
-
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
|
|
102
|
-
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
103
|
-
res.setHeader("Connection", "keep-alive");
|
|
104
|
-
res.flushHeaders?.();
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const {
|
|
108
|
-
prompt,
|
|
109
|
-
quality: rawQuality = "medium",
|
|
110
|
-
size = "1024x1024",
|
|
111
|
-
format = "png",
|
|
112
|
-
moderation = "low",
|
|
113
|
-
provider = "auto",
|
|
114
|
-
references = [],
|
|
115
|
-
mode: promptMode = "auto",
|
|
116
|
-
model: rawModel,
|
|
117
|
-
reasoningEffort: rawReasoningEffort,
|
|
118
|
-
webSearchEnabled: rawWebSearchEnabled = true,
|
|
119
|
-
} = req.body;
|
|
120
|
-
const composerPrompt = normalizeComposerPrompt(req.body?.composerPrompt);
|
|
121
|
-
const composerInsertedPrompts = normalizeComposerInsertedPrompts(
|
|
122
|
-
req.body?.composerInsertedPrompts,
|
|
123
|
-
);
|
|
124
|
-
const maxImages = normalizeMaxImages(req.body?.maxImages);
|
|
125
|
-
const normalizedPromptMode = promptMode === "direct" ? "direct" : "auto";
|
|
126
|
-
const { quality, warnings: qualityWarnings } = normalizeOAuthParams({ provider, quality: rawQuality });
|
|
127
|
-
const providerOptions = resolveProviderOptions(ctx, {
|
|
128
|
-
provider,
|
|
129
|
-
rawModel,
|
|
130
|
-
rawReasoningEffort,
|
|
131
|
-
rawSize: size,
|
|
132
|
-
rawWebSearchEnabled,
|
|
133
|
-
});
|
|
134
|
-
if (providerOptions.error) {
|
|
135
|
-
finishStatus = "error";
|
|
136
|
-
finishHttpStatus = providerOptions.status;
|
|
137
|
-
finishErrorCode = providerOptions.code;
|
|
138
|
-
sendSse(res, "error", { error: providerOptions.error, code: providerOptions.code, status: providerOptions.status, requestId });
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const imageModel = providerOptions.model;
|
|
142
|
-
const reasoningEffort = providerOptions.reasoningEffort;
|
|
143
|
-
const effectiveSize = providerOptions.size;
|
|
144
|
-
const webSearchEnabled = providerOptions.webSearchEnabled;
|
|
145
|
-
const activeProvider = providerOptions.provider;
|
|
146
|
-
if (!prompt) {
|
|
147
|
-
finishStatus = "error";
|
|
148
|
-
finishHttpStatus = 400;
|
|
149
|
-
finishErrorCode = "PROMPT_REQUIRED";
|
|
150
|
-
sendSse(res, "error", { error: "Prompt is required", code: finishErrorCode, status: 400, requestId });
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
const moderationCheck = validateModeration(ctx, moderation);
|
|
154
|
-
if (moderationCheck.error) {
|
|
155
|
-
finishStatus = "error";
|
|
156
|
-
finishHttpStatus = 400;
|
|
157
|
-
finishErrorCode = "INVALID_MODERATION";
|
|
158
|
-
sendSse(res, "error", { error: moderationCheck.error, code: finishErrorCode, status: 400, requestId });
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const refCheckResult = validateAndNormalizeRefs(references);
|
|
162
|
-
if (refCheckResult.error) {
|
|
163
|
-
finishStatus = "error";
|
|
164
|
-
finishHttpStatus = 400;
|
|
165
|
-
finishErrorCode = refCheckResult.code;
|
|
166
|
-
sendSse(res, "error", { error: refCheckResult.error, code: refCheckResult.code, status: 400, requestId });
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
const refCheck = refCheckResult as Extract<typeof refCheckResult, { refs: string[] }>;
|
|
170
|
-
const referencePayload = summarizeReferencePayload(references);
|
|
171
|
-
|
|
172
|
-
startJob({
|
|
173
|
-
requestId,
|
|
174
|
-
kind: "multimode",
|
|
175
|
-
prompt,
|
|
176
|
-
meta: {
|
|
177
|
-
kind: "multimode",
|
|
178
|
-
quality,
|
|
179
|
-
model: imageModel,
|
|
180
|
-
size: effectiveSize,
|
|
181
|
-
maxImages,
|
|
182
|
-
refsCount: referencePayload.refsCount,
|
|
183
|
-
referenceBytes: referencePayload.referenceBytes,
|
|
184
|
-
referenceB64Chars: referencePayload.referenceB64Chars,
|
|
185
|
-
composerPrompt,
|
|
186
|
-
composerInsertedPrompts,
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
registerJobAbortController(requestId, cancelController);
|
|
190
|
-
|
|
191
|
-
logEvent("multimode", "request", {
|
|
192
|
-
requestId,
|
|
193
|
-
quality,
|
|
194
|
-
model: imageModel,
|
|
195
|
-
size: effectiveSize,
|
|
196
|
-
moderation,
|
|
197
|
-
maxImages,
|
|
198
|
-
refs: refCheck.refs.length,
|
|
199
|
-
referenceBytes: referencePayload.referenceBytes,
|
|
200
|
-
promptChars: typeof prompt === "string" ? prompt.length : 0,
|
|
201
|
-
webSearchEnabled,
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
const startTime = Date.now();
|
|
205
|
-
const mimeMap: Record<string, string> = { png: "image/png", jpeg: "image/jpeg", webp: "image/webp" };
|
|
206
|
-
const mmFormat = activeProvider === "grok" ? "jpeg" : String(format);
|
|
207
|
-
const mime = mimeMap[mmFormat] || "image/png";
|
|
208
|
-
const sequenceId = `seq_${Date.now().toString(36)}_${randomBytes(4).toString("hex")}`;
|
|
209
|
-
routeMaxImages = maxImages;
|
|
210
|
-
routeSequenceId = sequenceId;
|
|
211
|
-
routeStartTime = startTime;
|
|
212
|
-
routeActiveProvider = activeProvider ?? "auto";
|
|
213
|
-
routeQuality = quality;
|
|
214
|
-
routeEffectiveSize = effectiveSize;
|
|
215
|
-
routeModeration = moderation;
|
|
216
|
-
routeImageModel = imageModel ?? null;
|
|
217
|
-
routeWebSearchEnabled = webSearchEnabled ?? false;
|
|
218
|
-
routePromptMode = normalizedPromptMode;
|
|
219
|
-
routeQualityWarnings = qualityWarnings;
|
|
220
|
-
routeComposerPrompt = composerPrompt;
|
|
221
|
-
routeComposerInsertedPrompts = composerInsertedPrompts;
|
|
222
|
-
await mkdir(ctx.config.storage.generatedDir, { recursive: true });
|
|
223
|
-
|
|
224
|
-
const persistAndSendImage = async (
|
|
225
|
-
image: MultimodeImage,
|
|
226
|
-
index: number,
|
|
227
|
-
totalReturned: number,
|
|
228
|
-
status: ReturnType<typeof sequenceStatus>,
|
|
229
|
-
) => {
|
|
230
|
-
if (persistedIndexes.has(index)) return;
|
|
231
|
-
throwIfJobCanceled(requestId);
|
|
232
|
-
const resultMime = activeProvider === "grok"
|
|
233
|
-
? (image.mime || detectImageMimeFromB64(image.b64) || mime)
|
|
234
|
-
: mime;
|
|
235
|
-
const resultFormat = activeProvider === "grok" ? imageFormatFromMime(resultMime) : mmFormat;
|
|
236
|
-
const rand = randomBytes(ctx.config.ids.generatedHexBytes).toString("hex");
|
|
237
|
-
const filename = `${Date.now()}_${rand}_multimode_${index}.${resultFormat}`;
|
|
238
|
-
const meta = {
|
|
239
|
-
kind: "multimode-image",
|
|
240
|
-
generationStrategy: "one-call-text-sequence",
|
|
241
|
-
sequenceId,
|
|
242
|
-
sequenceIndex: index + 1,
|
|
243
|
-
sequenceTotalRequested: maxImages,
|
|
244
|
-
sequenceTotalReturned: totalReturned,
|
|
245
|
-
sequenceStatus: status,
|
|
246
|
-
stageLabel: String.fromCharCode(65 + index),
|
|
247
|
-
requestId,
|
|
248
|
-
prompt,
|
|
249
|
-
userPrompt: prompt,
|
|
250
|
-
revisedPrompt: image.revisedPrompt || null,
|
|
251
|
-
promptMode: normalizedPromptMode,
|
|
252
|
-
composerPrompt,
|
|
253
|
-
composerInsertedPrompts,
|
|
254
|
-
quality,
|
|
255
|
-
size: effectiveSize,
|
|
256
|
-
format: resultFormat,
|
|
257
|
-
moderation,
|
|
258
|
-
model: activeProvider === "grok" ? (quality === "high" ? "grok-imagine-image-quality" : imageModel) : imageModel,
|
|
259
|
-
provider: activeProvider,
|
|
260
|
-
createdAt: Date.now(),
|
|
261
|
-
usage: latestUsage,
|
|
262
|
-
webSearchCalls: latestWebSearchCalls,
|
|
263
|
-
webSearchEnabled,
|
|
264
|
-
refsCount: refCheck.refs.length,
|
|
265
|
-
};
|
|
266
|
-
const rawBuffer = Buffer.from(image.b64, "base64");
|
|
267
|
-
const embedded = await embedImageMetadataBestEffort(rawBuffer, resultFormat, meta, {
|
|
268
|
-
version: ctx.packageVersion,
|
|
269
|
-
});
|
|
270
|
-
await writeFile(join(ctx.config.storage.generatedDir, filename), embedded.buffer);
|
|
271
|
-
await writeFile(join(ctx.config.storage.generatedDir, filename + ".json"), JSON.stringify(meta)).catch(() => {});
|
|
272
|
-
invalidateHistoryIndex();
|
|
273
|
-
const item = {
|
|
274
|
-
image: `data:${resultMime};base64,${image.b64}`,
|
|
275
|
-
filename,
|
|
276
|
-
revisedPrompt: image.revisedPrompt || null,
|
|
277
|
-
sequenceId,
|
|
278
|
-
sequenceIndex: index + 1,
|
|
279
|
-
sequenceTotalRequested: maxImages,
|
|
280
|
-
sequenceTotalReturned: totalReturned,
|
|
281
|
-
sequenceStatus: status,
|
|
282
|
-
};
|
|
283
|
-
persistedIndexes.add(index);
|
|
284
|
-
images.push(item);
|
|
285
|
-
sendSse(res, "image", item);
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
sendSse(res, "phase", { phase: "streaming", requestId, sequenceId, maxImages });
|
|
289
|
-
|
|
290
|
-
let generated: { images: Array<{ b64: string; revisedPrompt?: string | null }>; usage: Record<string, number> | null; webSearchCalls?: number; extraIgnored?: number };
|
|
291
|
-
|
|
292
|
-
if (activeProvider === "grok") {
|
|
293
|
-
const grokModel = quality === "high" ? "grok-imagine-image-quality" : imageModel;
|
|
294
|
-
generated = await generateMultimodeViaGrok(prompt, ctx, {
|
|
295
|
-
model: grokModel,
|
|
296
|
-
maxImages,
|
|
297
|
-
size: effectiveSize,
|
|
298
|
-
signal: cancelController.signal,
|
|
299
|
-
requestId,
|
|
300
|
-
references: refCheck.refDetails,
|
|
301
|
-
onFinalImage: async (image, index) => {
|
|
302
|
-
const totalReturned = Math.max(index + 1, images.length + 1);
|
|
303
|
-
await persistAndSendImage(image, index, totalReturned, sequenceStatus(totalReturned, maxImages));
|
|
304
|
-
},
|
|
305
|
-
});
|
|
306
|
-
} else {
|
|
307
|
-
generated = await generateMultimodeViaResponses(
|
|
308
|
-
activeProvider,
|
|
309
|
-
prompt,
|
|
310
|
-
quality,
|
|
311
|
-
effectiveSize,
|
|
312
|
-
moderation,
|
|
313
|
-
refCheck.refDetails || refCheck.refs,
|
|
314
|
-
requestId,
|
|
315
|
-
normalizedPromptMode,
|
|
316
|
-
ctx,
|
|
317
|
-
{
|
|
318
|
-
model: imageModel,
|
|
319
|
-
maxImages,
|
|
320
|
-
reasoningEffort,
|
|
321
|
-
webSearchEnabled,
|
|
322
|
-
onPartialImage: (partial) =>
|
|
323
|
-
isJobCanceled(requestId)
|
|
324
|
-
? undefined
|
|
325
|
-
: sendSse(res, "partial", {
|
|
326
|
-
image: `data:${mime};base64,${partial.b64}`,
|
|
327
|
-
requestId,
|
|
328
|
-
sequenceId,
|
|
329
|
-
index: partial.index,
|
|
330
|
-
}),
|
|
331
|
-
onFinalImage: async (image, index) => {
|
|
332
|
-
const totalReturned = Math.max(index + 1, images.length + 1);
|
|
333
|
-
await persistAndSendImage(image, index, totalReturned, sequenceStatus(totalReturned, maxImages));
|
|
334
|
-
},
|
|
335
|
-
signal: cancelController.signal,
|
|
336
|
-
},
|
|
337
|
-
);
|
|
338
|
-
}
|
|
339
|
-
throwIfJobCanceled(requestId);
|
|
340
|
-
|
|
341
|
-
latestUsage = generated.usage || null;
|
|
342
|
-
latestWebSearchCalls = generated.webSearchCalls || 0;
|
|
343
|
-
latestExtraIgnored = generated.extraIgnored || 0;
|
|
344
|
-
for (const [index, image] of generated.images.entries() as IterableIterator<[number, MultimodeImage]>) {
|
|
345
|
-
await persistAndSendImage(
|
|
346
|
-
image,
|
|
347
|
-
index,
|
|
348
|
-
generated.images.length,
|
|
349
|
-
sequenceStatus(generated.images.length, maxImages),
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const returned = images.length;
|
|
354
|
-
const status = sequenceStatus(returned, maxImages);
|
|
355
|
-
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
356
|
-
if (returned === 0) {
|
|
357
|
-
finishStatus = "error";
|
|
358
|
-
finishHttpStatus = 422;
|
|
359
|
-
finishErrorCode = "EMPTY_RESPONSE";
|
|
360
|
-
finishMeta = { sequenceId, filenames: [], imageCount: 0, maxImages, status, composerPrompt: routeComposerPrompt, composerInsertedPrompts: routeComposerInsertedPrompts };
|
|
361
|
-
sendSse(res, "error", {
|
|
362
|
-
error: "No image data returned from the multimode stream",
|
|
363
|
-
code: finishErrorCode,
|
|
364
|
-
status: finishHttpStatus,
|
|
365
|
-
requestId, sequenceId, requested: maxImages, returned,
|
|
366
|
-
});
|
|
367
|
-
logEvent("multimode", "empty_response", { requestId, sequenceId, maxImages, elapsedMs: Date.now() - startTime });
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
finishMeta = {
|
|
371
|
-
sequenceId,
|
|
372
|
-
filenames: images.map((image) => image.filename),
|
|
373
|
-
imageCount: returned,
|
|
374
|
-
maxImages,
|
|
375
|
-
status,
|
|
376
|
-
composerPrompt: routeComposerPrompt,
|
|
377
|
-
composerInsertedPrompts: routeComposerInsertedPrompts,
|
|
378
|
-
};
|
|
379
|
-
finishHttpStatus = 200;
|
|
380
|
-
sendSse(res, "done", {
|
|
381
|
-
ok: true,
|
|
382
|
-
requestId,
|
|
383
|
-
sequenceId,
|
|
384
|
-
requested: maxImages,
|
|
385
|
-
returned,
|
|
386
|
-
status,
|
|
387
|
-
elapsed,
|
|
388
|
-
images,
|
|
389
|
-
provider: activeProvider,
|
|
390
|
-
quality,
|
|
391
|
-
size: effectiveSize,
|
|
392
|
-
moderation,
|
|
393
|
-
model: imageModel,
|
|
394
|
-
usage: latestUsage,
|
|
395
|
-
webSearchCalls: latestWebSearchCalls,
|
|
396
|
-
webSearchEnabled,
|
|
397
|
-
warnings: qualityWarnings,
|
|
398
|
-
extraIgnored: latestExtraIgnored,
|
|
399
|
-
promptMode: normalizedPromptMode,
|
|
400
|
-
});
|
|
401
|
-
logEvent("multimode", "saved", {
|
|
402
|
-
requestId,
|
|
403
|
-
sequenceId,
|
|
404
|
-
imageCount: returned,
|
|
405
|
-
maxImages,
|
|
406
|
-
status,
|
|
407
|
-
elapsedMs: Date.now() - startTime,
|
|
408
|
-
});
|
|
409
|
-
} catch (e) {
|
|
410
|
-
const err = errInfo(e);
|
|
411
|
-
const ext = (err.raw && typeof err.raw === "object" ? err.raw as Record<string, unknown> : {});
|
|
412
|
-
const fallbackCode = err.code || classifyUpstreamError(err.message);
|
|
413
|
-
if (isGenerationCanceledError(err.raw) || isJobCanceled(requestId)) {
|
|
414
|
-
const canceled = makeGenerationCanceledError();
|
|
415
|
-
finishCanceled = true;
|
|
416
|
-
finishHttpStatus = canceled.status;
|
|
417
|
-
finishErrorCode = canceled.code;
|
|
418
|
-
sendSse(res, "error", {
|
|
419
|
-
error: canceled.message,
|
|
420
|
-
code: canceled.code,
|
|
421
|
-
status: canceled.status,
|
|
422
|
-
requestId,
|
|
423
|
-
});
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
if ((fallbackCode === "RESPONSES_IMAGE_TIMEOUT" || err.status === 504) && images.length > 0) {
|
|
427
|
-
const status = sequenceStatus(images.length, routeMaxImages);
|
|
428
|
-
const elapsed = ((Date.now() - routeStartTime) / 1000).toFixed(1);
|
|
429
|
-
finishStatus = "completed";
|
|
430
|
-
finishHttpStatus = 206;
|
|
431
|
-
finishMeta = {
|
|
432
|
-
sequenceId: routeSequenceId,
|
|
433
|
-
filenames: images.map((image) => image.filename),
|
|
434
|
-
imageCount: images.length,
|
|
435
|
-
maxImages: routeMaxImages,
|
|
436
|
-
status,
|
|
437
|
-
partialErrorCode: "RESPONSES_IMAGE_TIMEOUT",
|
|
438
|
-
composerPrompt: routeComposerPrompt,
|
|
439
|
-
composerInsertedPrompts: routeComposerInsertedPrompts,
|
|
440
|
-
};
|
|
441
|
-
sendSse(res, "done", {
|
|
442
|
-
ok: true,
|
|
443
|
-
partial: true,
|
|
444
|
-
requestId,
|
|
445
|
-
sequenceId: routeSequenceId,
|
|
446
|
-
requested: routeMaxImages,
|
|
447
|
-
returned: images.length,
|
|
448
|
-
status,
|
|
449
|
-
elapsed,
|
|
450
|
-
images,
|
|
451
|
-
provider: routeActiveProvider,
|
|
452
|
-
quality: routeQuality,
|
|
453
|
-
size: routeEffectiveSize,
|
|
454
|
-
moderation: routeModeration,
|
|
455
|
-
model: routeImageModel,
|
|
456
|
-
usage: latestUsage,
|
|
457
|
-
webSearchCalls: latestWebSearchCalls,
|
|
458
|
-
webSearchEnabled: routeWebSearchEnabled,
|
|
459
|
-
warnings: routeQualityWarnings,
|
|
460
|
-
extraIgnored: latestExtraIgnored,
|
|
461
|
-
promptMode: routePromptMode,
|
|
462
|
-
warning: {
|
|
463
|
-
code: "RESPONSES_IMAGE_TIMEOUT",
|
|
464
|
-
message: "The provider timed out after returning partial multimode results.",
|
|
465
|
-
},
|
|
466
|
-
});
|
|
467
|
-
logEvent("multimode", "partial_timeout", {
|
|
468
|
-
requestId,
|
|
469
|
-
sequenceId: routeSequenceId,
|
|
470
|
-
imageCount: images.length,
|
|
471
|
-
maxImages: routeMaxImages,
|
|
472
|
-
});
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
finishStatus = "error";
|
|
476
|
-
finishHttpStatus = err.status || 500;
|
|
477
|
-
finishErrorCode = fallbackCode || "MULTIMODE_GENERATE_FAILED";
|
|
478
|
-
logError("multimode", "error", err.raw, { requestId, code: finishErrorCode });
|
|
479
|
-
sendSse(res, "error", {
|
|
480
|
-
error: err.message,
|
|
481
|
-
code: finishErrorCode,
|
|
482
|
-
status: finishHttpStatus,
|
|
483
|
-
requestId,
|
|
484
|
-
upstreamCode: ext.upstreamCode || null,
|
|
485
|
-
upstreamType: ext.upstreamType || null,
|
|
486
|
-
upstreamParam: ext.upstreamParam || null,
|
|
487
|
-
});
|
|
488
|
-
} finally {
|
|
489
|
-
finishJob(requestId, {
|
|
490
|
-
canceled: finishCanceled,
|
|
491
|
-
status: finishStatus,
|
|
492
|
-
httpStatus: finishHttpStatus,
|
|
493
|
-
errorCode: finishErrorCode,
|
|
494
|
-
meta: finishMeta,
|
|
495
|
-
});
|
|
496
|
-
res.end();
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
}
|