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,273 +0,0 @@
1
- import type {
2
- AgentGenerationPlan,
3
- AgentGenerationPlanSource,
4
- AgentGenerationSettings,
5
- AgentSlashCommand,
6
- } from "./agentTypes.js";
7
-
8
- const HARD_MAX_VARIANTS = 8;
9
- const AMBIGUOUS_MULTI_VARIANTS = 3;
10
- const KOREAN_COUNT_WORDS: Array<[RegExp, number]> = [
11
- [/(?:한|하나|1)\s*(?:장|개|가지|컷|시안|버전)/u, 1],
12
- [/(?:두|둘|2)\s*(?:장|개|가지|컷|시안|버전)/u, 2],
13
- [/(?:세|셋|3)\s*(?:장|개|가지|컷|시안|버전)/u, 3],
14
- [/(?:네|넷|4)\s*(?:장|개|가지|컷|시안|버전)/u, 4],
15
- [/(?:다섯|5)\s*(?:장|개|가지|컷|시안|버전)/u, 5],
16
- [/(?:여섯|6)\s*(?:장|개|가지|컷|시안|버전)/u, 6],
17
- [/(?:일곱|7)\s*(?:장|개|가지|컷|시안|버전)/u, 7],
18
- [/(?:여덟|8)\s*(?:장|개|가지|컷|시안|버전)/u, 8],
19
- ];
20
- const ENGLISH_COUNT_WORDS: Array<[RegExp, number]> = [
21
- [/\b(?:one|1)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 1],
22
- [/\b(?:two|2)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 2],
23
- [/\b(?:three|3)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 3],
24
- [/\b(?:four|4)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 4],
25
- [/\b(?:five|5)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 5],
26
- [/\b(?:six|6)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 6],
27
- [/\b(?:seven|7)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 7],
28
- [/\b(?:eight|8)\s*(?:image|variant|version|option|candidate|shot|render)?s?\b/iu, 8],
29
- ];
30
-
31
- type PlanningInput = {
32
- prompt: string;
33
- settings: AgentGenerationSettings;
34
- command?: AgentSlashCommand | null;
35
- };
36
-
37
- type VariantDecision = {
38
- count: number;
39
- requested: number;
40
- source: AgentGenerationPlanSource;
41
- reason: string;
42
- };
43
-
44
- export function deriveAgentGenerationPlan({ prompt, settings, command = null }: PlanningInput): AgentGenerationPlan {
45
- if (command?.name === "question" || command?.name === "help") {
46
- return {
47
- mode: "question",
48
- prompts: [],
49
- requestedVariants: 0,
50
- plannedVariants: 0,
51
- plannedParallelism: 0,
52
- source: "question-command",
53
- reason: command.name === "help" ? "Slash help answered without image generation." : "Question command answered without image generation.",
54
- command: command.name,
55
- assistantText: null,
56
- };
57
- }
58
-
59
- if (isVideoIntent(prompt)) {
60
- return {
61
- mode: "video",
62
- prompts: [prompt],
63
- requestedVariants: 1,
64
- plannedVariants: 1,
65
- plannedParallelism: 1,
66
- source: "auto-request",
67
- reason: "Video generation detected from prompt keywords.",
68
- command: command?.name ?? null,
69
- assistantText: null,
70
- };
71
- }
72
-
73
- const variantDecision = decideVariantCount(prompt, settings, command);
74
- const plannedParallelism = resolvePlannedParallelism(settings, variantDecision.count, command);
75
- const prompts = buildGenerationPrompts(prompt, variantDecision.count);
76
- return {
77
- mode: prompts.length > 1 ? "fanout" : "single",
78
- prompts,
79
- requestedVariants: variantDecision.requested,
80
- plannedVariants: prompts.length,
81
- plannedParallelism,
82
- source: variantDecision.source,
83
- reason: variantDecision.reason,
84
- command: command?.name ?? null,
85
- assistantText: null,
86
- };
87
- }
88
-
89
- export function normalizeAgentGenerationPlan(
90
- prompt: string,
91
- value: unknown,
92
- settings: AgentGenerationSettings,
93
- ): AgentGenerationPlan {
94
- const input = value && typeof value === "object" ? value as Record<string, unknown> : {};
95
- const prompts = cleanPromptArray(input.prompts);
96
- if (prompts.length === 0) return deriveAgentGenerationPlan({ prompt, settings, command: cleanCommand(input.command) });
97
-
98
- const plannedVariants = cleanCount(input.plannedVariants, prompts.length, 0, HARD_MAX_VARIANTS);
99
- const requestedParallelism = cleanCount(input.plannedParallelism, settings.parallelism, 1, HARD_MAX_VARIANTS);
100
- const plannedParallelism = resolvePlannedParallelism({ ...settings, parallelism: requestedParallelism }, plannedVariants, null);
101
- return {
102
- mode: input.mode === "question" ? "question" : input.mode === "video" ? "video" : prompts.length > 1 ? "fanout" : "single",
103
- prompts,
104
- requestedVariants: cleanCount(input.requestedVariants, plannedVariants, 0, HARD_MAX_VARIANTS),
105
- plannedVariants,
106
- plannedParallelism,
107
- source: cleanPlanSource(input.source),
108
- reason: cleanReason(input.reason, prompts.length > 1 ? "Stored fanout plan." : "Stored single-image plan."),
109
- command: cleanCommandName(input.command),
110
- assistantText: typeof input.assistantText === "string" ? input.assistantText : null,
111
- };
112
- }
113
-
114
- function decideVariantCount(
115
- prompt: string,
116
- settings: AgentGenerationSettings,
117
- command: AgentSlashCommand | null,
118
- ): VariantDecision {
119
- if (command?.name === "variants" || command?.name === "generate") {
120
- const count = clampCount(command.value ?? AMBIGUOUS_MULTI_VARIANTS, HARD_MAX_VARIANTS);
121
- return {
122
- count,
123
- requested: count,
124
- source: "slash-command",
125
- reason: `Slash command requested ${count} variant${count === 1 ? "" : "s"}.`,
126
- };
127
- }
128
-
129
- if (settings.generationStrategy === "manual") {
130
- const count = clampCount(settings.variants, HARD_MAX_VARIANTS);
131
- return {
132
- count,
133
- requested: count,
134
- source: "manual-settings",
135
- reason: `Manual settings requested ${count} variant${count === 1 ? "" : "s"}.`,
136
- };
137
- }
138
-
139
- const inferred = inferRequestedVariantCount(prompt);
140
- const capped = Math.min(inferred.count, clampCount(settings.maxAutoVariants, HARD_MAX_VARIANTS));
141
- return {
142
- count: capped,
143
- requested: inferred.count,
144
- source: inferred.count > 1 ? "auto-request" : "auto-default",
145
- reason: capped < inferred.count
146
- ? `User request implied ${inferred.count} variants; capped at ${capped}.`
147
- : inferred.reason,
148
- };
149
- }
150
-
151
- function inferRequestedVariantCount(prompt: string): { count: number; reason: string } {
152
- const text = prompt.trim();
153
- for (const [pattern, count] of [...KOREAN_COUNT_WORDS, ...ENGLISH_COUNT_WORDS]) {
154
- if (pattern.test(text)) {
155
- return { count, reason: `User request explicitly implies ${count} variant${count === 1 ? "" : "s"}.` };
156
- }
157
- }
158
- if (/(?:여러|몇\s*가지|시안|후보|버전|다양하게|several|multiple|options|variants|versions|candidates|alternatives)/iu.test(text)) {
159
- return { count: AMBIGUOUS_MULTI_VARIANTS, reason: `User request asks for multiple options; planning ${AMBIGUOUS_MULTI_VARIANTS} variants.` };
160
- }
161
- if (/(?:비교|대안|a\/b|compare|comparison)/iu.test(text)) {
162
- return { count: 2, reason: "User request implies comparison; planning 2 variants." };
163
- }
164
- return { count: 1, reason: "Defaulting to one image because the request did not ask for multiple variants." };
165
- }
166
-
167
- function resolvePlannedParallelism(
168
- settings: AgentGenerationSettings,
169
- plannedVariants: number,
170
- command: AgentSlashCommand | null,
171
- ): number {
172
- if (plannedVariants <= 0) return 0;
173
- const requested = command?.name === "parallelism" && command.value ? command.value : settings.parallelism;
174
- const providerCap = settings.provider === "oauth" ? 2 : HARD_MAX_VARIANTS;
175
- const qualityCap = settings.quality === "high" ? 2 : HARD_MAX_VARIANTS;
176
- return Math.max(1, Math.min(plannedVariants, clampCount(requested, HARD_MAX_VARIANTS), providerCap, qualityCap));
177
- }
178
-
179
- function buildGenerationPrompts(prompt: string, count: number): string[] {
180
- if (count <= 1) return [prompt];
181
- return Array.from({ length: count }, (_, index) => [
182
- prompt,
183
- "",
184
- `Variant ${index + 1}/${count}: explore a distinct composition while preserving the request.`,
185
- ].join("\n"));
186
- }
187
-
188
- function cleanPromptArray(value: unknown): string[] {
189
- if (!Array.isArray(value)) return [];
190
- return value.filter((item): item is string => typeof item === "string" && item.trim().length > 0)
191
- .slice(0, HARD_MAX_VARIANTS)
192
- .map((item) => item.trim());
193
- }
194
-
195
- function cleanPlanSource(value: unknown): AgentGenerationPlanSource {
196
- if (
197
- value === "auto-default" ||
198
- value === "auto-request" ||
199
- value === "manual-settings" ||
200
- value === "slash-command" ||
201
- value === "question-command"
202
- ) return value;
203
- return "auto-default";
204
- }
205
-
206
- function cleanCommandName(value: unknown): AgentGenerationPlan["command"] {
207
- if (
208
- value === "question" ||
209
- value === "help" ||
210
- value === "variants" ||
211
- value === "generate" ||
212
- value === "parallelism"
213
- ) return value;
214
- return null;
215
- }
216
-
217
- function cleanCommand(value: unknown): AgentSlashCommand | null {
218
- if (!value || typeof value !== "object") return null;
219
- const input = value as Record<string, unknown>;
220
- const name = cleanCommandName(input.name);
221
- if (!name) return null;
222
- return {
223
- name,
224
- rawName: typeof input.rawName === "string" ? input.rawName : name,
225
- raw: typeof input.raw === "string" ? input.raw : "",
226
- prompt: typeof input.prompt === "string" ? input.prompt : "",
227
- ...(typeof input.value === "number" ? { value: input.value } : {}),
228
- };
229
- }
230
-
231
- function cleanReason(value: unknown, fallback: string): string {
232
- return typeof value === "string" && value.trim() ? value.trim().slice(0, 240) : fallback;
233
- }
234
-
235
- function cleanCount(value: unknown, fallback: number, min: number, max: number): number {
236
- const numeric = typeof value === "number" ? value : Number(value);
237
- if (!Number.isFinite(numeric)) return fallback;
238
- return Math.max(min, Math.min(max, Math.round(numeric)));
239
- }
240
-
241
- function clampCount(value: number, max: number): number {
242
- return Math.max(1, Math.min(max, Math.round(value)));
243
- }
244
-
245
- const VIDEO_INTENT_PATTERN = /(?:^|\s|[^\p{L}])(?:video|animate|animation)(?:\s|[^\p{L}]|$)|(?:동영상|비디오|영상|애니메이트|움직이|클립)/iu;
246
-
247
- function isVideoIntent(prompt: string): boolean {
248
- return VIDEO_INTENT_PATTERN.test(prompt);
249
- }
250
-
251
- export interface VideoParamsFromPrompt {
252
- duration?: number;
253
- resolution?: "480p" | "720p";
254
- aspectRatio?: string;
255
- }
256
-
257
- const DURATION_PATTERN = /(\d{1,2})\s*(?:s|sec|seconds?|초)/i;
258
- const RESOLUTION_PATTERN = /(720p|480p)/i;
259
- const ASPECT_PATTERN = /(16:9|9:16|4:3|3:4|3:2|2:3|1:1)/;
260
-
261
- export function parseVideoParams(prompt: string): VideoParamsFromPrompt {
262
- const params: VideoParamsFromPrompt = {};
263
- const durMatch = DURATION_PATTERN.exec(prompt);
264
- if (durMatch) {
265
- const d = parseInt(durMatch[1]);
266
- if (d >= 1 && d <= 15) params.duration = d;
267
- }
268
- const resMatch = RESOLUTION_PATTERN.exec(prompt);
269
- if (resMatch) params.resolution = resMatch[1].toLowerCase() as "480p" | "720p";
270
- const aspMatch = ASPECT_PATTERN.exec(prompt);
271
- if (aspMatch) params.aspectRatio = aspMatch[1];
272
- return params;
273
- }
@@ -1,266 +0,0 @@
1
- import { errInfo } from "./errInfo.js";
2
- import { logEvent } from "./logger.js";
3
- import { waitForOAuthReady } from "./oauthProxy/runtime.js";
4
- import { requireRuntimeContext, type RouteRuntimeContext } from "./runtimeContext.js";
5
-
6
- const AGENT_QUESTION_DEVELOPER_PROMPT = [
7
- "You are the text-only question mode for ima2-gen Agent.",
8
- "Answer the user's question directly and naturally in the user's language.",
9
- "Do not generate images, do not call image tools, and do not turn the question into an image prompt.",
10
- "Do not simply repeat the user's question unless the user explicitly asks you to repeat text.",
11
- "Keep the answer concise and useful for an image-generation workflow when relevant.",
12
- ].join(" ");
13
-
14
- type AgentQuestionOptions = {
15
- provider?: string;
16
- model?: string;
17
- reasoningEffort?: string;
18
- requestId?: string;
19
- signal?: AbortSignal | null;
20
- };
21
-
22
- type AgentQuestionResult = {
23
- text: string;
24
- usage: Record<string, unknown> | null;
25
- };
26
-
27
- type QuestionEndpoint = {
28
- url: string;
29
- headers: Record<string, string>;
30
- };
31
-
32
- type TextContentPart = {
33
- type?: string;
34
- text?: string | { value?: string };
35
- value?: string;
36
- refusal?: string;
37
- };
38
-
39
- type TextResponseBody = {
40
- output_text?: string;
41
- output?: Array<{
42
- type?: string;
43
- content?: TextContentPart[];
44
- }>;
45
- usage?: Record<string, unknown>;
46
- };
47
-
48
- type SseEvent = {
49
- type?: string;
50
- delta?: string;
51
- text?: string;
52
- item?: {
53
- type?: string;
54
- content?: TextContentPart[];
55
- };
56
- response?: { usage?: Record<string, unknown> };
57
- error?: { code?: string; message?: string };
58
- };
59
-
60
- export async function requestAgentQuestionAnswer(
61
- ctxRaw: RouteRuntimeContext,
62
- question: string,
63
- options: AgentQuestionOptions = {},
64
- ): Promise<AgentQuestionResult> {
65
- try {
66
- const ctx = requireRuntimeContext(ctxRaw);
67
- const endpoint = await resolveQuestionEndpoint(ctx, options.provider);
68
- const timeoutMs = ctx.config.oauth?.generationTimeoutMs ?? 120_000;
69
- const controller = new AbortController();
70
- const timer = setTimeout(() => controller.abort(), timeoutMs);
71
- const signal = options.signal ? combineSignals([controller.signal, options.signal]) : controller.signal;
72
- try {
73
- const res = await fetch(endpoint.url, {
74
- method: "POST",
75
- headers: endpoint.headers,
76
- signal,
77
- body: JSON.stringify({
78
- model: options.model || ctx.config.imageModels?.default || "gpt-5.4-mini",
79
- input: [
80
- { role: "developer", content: AGENT_QUESTION_DEVELOPER_PROMPT },
81
- { role: "user", content: question },
82
- ],
83
- reasoning: { effort: options.reasoningEffort || "low" },
84
- stream: true,
85
- }),
86
- });
87
- logEvent("agent_question", "response", {
88
- requestId: options.requestId,
89
- provider: options.provider,
90
- status: res.status,
91
- contentType: res.headers.get("content-type"),
92
- });
93
- if (!res.ok) throw await questionHttpError(res, options.provider);
94
- const parsed = res.headers.get("content-type")?.includes("text/event-stream")
95
- ? await readTextStream(res)
96
- : readTextBody(await res.json() as TextResponseBody);
97
- if (!parsed.text.trim()) throw questionError("Agent question returned an empty response", "AGENT_QUESTION_EMPTY", 502);
98
- return { text: parsed.text.trim().slice(0, 4_000), usage: parsed.usage };
99
- } finally {
100
- clearTimeout(timer);
101
- }
102
- } catch (error) {
103
- const err = errInfo(error);
104
- if (err.name === "AbortError") {
105
- throw questionError("Agent question timed out", "AGENT_QUESTION_TIMEOUT", 504, err.raw);
106
- }
107
- throw err.raw;
108
- }
109
- }
110
-
111
- async function resolveQuestionEndpoint(
112
- ctx: ReturnType<typeof requireRuntimeContext>,
113
- provider: string | undefined,
114
- ): Promise<QuestionEndpoint> {
115
- try {
116
- if (provider === "api") {
117
- if (!ctx.apiKey) throw questionError("API key is required for Agent question mode", "API_KEY_REQUIRED", 401);
118
- return {
119
- url: "https://api.openai.com/v1/responses",
120
- headers: {
121
- "Content-Type": "application/json",
122
- Accept: "text/event-stream",
123
- Authorization: `Bearer ${ctx.apiKey}`,
124
- },
125
- };
126
- }
127
- await waitForOAuthReady(ctx);
128
- return {
129
- url: `${ctx.oauthUrl}/v1/responses`,
130
- headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
131
- };
132
- } catch (error) {
133
- throw errInfo(error).raw;
134
- }
135
- }
136
-
137
- async function questionHttpError(res: Response, provider: string | undefined): Promise<Error> {
138
- try {
139
- const text = await res.text();
140
- const err = questionError(
141
- `${provider === "api" ? "OpenAI API" : "OAuth proxy"} rejected Agent question mode`,
142
- "AGENT_QUESTION_UPSTREAM_FAILED",
143
- res.status >= 400 && res.status < 600 ? res.status : 502,
144
- );
145
- err.upstreamBodyChars = text.length;
146
- return err;
147
- } catch (error) {
148
- throw errInfo(error).raw;
149
- }
150
- }
151
-
152
- async function readTextStream(res: Response): Promise<AgentQuestionResult> {
153
- try {
154
- const reader = res.body?.getReader();
155
- if (!reader) return { text: "", usage: null };
156
- const decoder = new TextDecoder();
157
- const parts: string[] = [];
158
- let usage: Record<string, unknown> | null = null;
159
- let buffer = "";
160
- while (true) {
161
- const { done, value } = await reader.read();
162
- if (done) break;
163
- buffer += decoder.decode(value, { stream: true });
164
- let boundary = buffer.indexOf("\n\n");
165
- while (boundary !== -1) {
166
- const block = buffer.slice(0, boundary);
167
- buffer = buffer.slice(boundary + 2);
168
- const event = parseSseEvent(block);
169
- if (event) collectTextEvent(event, parts, (nextUsage) => {
170
- usage = nextUsage;
171
- });
172
- boundary = buffer.indexOf("\n\n");
173
- }
174
- }
175
- const finalEvent = parseSseEvent(buffer);
176
- if (finalEvent) collectTextEvent(finalEvent, parts, (nextUsage) => {
177
- usage = nextUsage;
178
- });
179
- return { text: parts.join("").trim(), usage };
180
- } catch (error) {
181
- throw errInfo(error).raw;
182
- }
183
- }
184
-
185
- function readTextBody(body: TextResponseBody): AgentQuestionResult {
186
- if (typeof body.output_text === "string" && body.output_text.trim()) {
187
- return { text: body.output_text, usage: body.usage ?? null };
188
- }
189
- const parts: string[] = [];
190
- for (const item of body.output ?? []) {
191
- appendContentText(item.content, parts);
192
- }
193
- return { text: parts.join("\n\n").trim(), usage: body.usage ?? null };
194
- }
195
-
196
- function parseSseEvent(block: string): SseEvent | null {
197
- const data = block
198
- .split("\n")
199
- .filter((line) => line.startsWith("data:"))
200
- .map((line) => line.slice(5).trimStart())
201
- .join("\n");
202
- if (!data || data === "[DONE]") return null;
203
- try {
204
- const parsed = JSON.parse(data) as unknown;
205
- return parsed && typeof parsed === "object" ? parsed as SseEvent : null;
206
- } catch {
207
- return null;
208
- }
209
- }
210
-
211
- function collectTextEvent(
212
- event: SseEvent,
213
- parts: string[],
214
- setUsage: (usage: Record<string, unknown>) => void,
215
- ) {
216
- if (event.type === "response.output_text.delta" && typeof event.delta === "string") parts.push(event.delta);
217
- if (event.type === "response.output_text.done" && typeof event.text === "string" && parts.length === 0) parts.push(event.text);
218
- if (event.type === "response.output_item.done" && event.item?.type === "message" && parts.length === 0) {
219
- appendContentText(event.item.content, parts);
220
- }
221
- if ((event.type === "response.completed" || event.type === "response.incomplete") && event.response?.usage) {
222
- setUsage(event.response.usage);
223
- }
224
- if (event.type === "error") {
225
- throw questionError(
226
- event.error?.message || "Agent question stream failed",
227
- event.error?.code || "AGENT_QUESTION_STREAM_ERROR",
228
- 502,
229
- );
230
- }
231
- }
232
-
233
- function appendContentText(content: TextContentPart[] | undefined, parts: string[]) {
234
- for (const part of content ?? []) {
235
- if (typeof part.text === "string" && part.text.trim()) parts.push(part.text);
236
- else if (part.text && typeof part.text === "object" && typeof part.text.value === "string") parts.push(part.text.value);
237
- else if (typeof part.value === "string" && part.value.trim()) parts.push(part.value);
238
- else if (typeof part.refusal === "string" && part.refusal.trim()) parts.push(part.refusal);
239
- }
240
- }
241
-
242
- function combineSignals(signals: AbortSignal[]): AbortSignal {
243
- if (signals.length === 1) return signals[0];
244
- const controller = new AbortController();
245
- for (const signal of signals) {
246
- if (signal.aborted) {
247
- controller.abort();
248
- break;
249
- }
250
- signal.addEventListener("abort", () => controller.abort(), { once: true });
251
- }
252
- return controller.signal;
253
- }
254
-
255
- function questionError(message: string, code: string, status: number, cause?: unknown) {
256
- const err = new Error(message) as Error & {
257
- code?: string;
258
- status?: number;
259
- cause?: unknown;
260
- upstreamBodyChars?: number;
261
- };
262
- err.code = code;
263
- err.status = status;
264
- if (cause) err.cause = cause;
265
- return err;
266
- }