ima2-gen 1.1.21 → 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +44 -7
  2. package/bin/commands/video.js +14 -0
  3. package/bin/ima2.js +14 -4
  4. package/bin/lib/platform.js +34 -5
  5. package/docs/README.ko.md +43 -2
  6. package/lib/agentQueueWorker.js +6 -0
  7. package/lib/agentRuntime.js +3 -2
  8. package/lib/atomicWrite.js +14 -0
  9. package/lib/grokImageAdapter.js +6 -0
  10. package/lib/grokProxyLauncher.js +5 -3
  11. package/lib/grokVideoAdapter.js +1 -1
  12. package/lib/grokVideoPlannerPrompt.js +10 -0
  13. package/lib/inflight.js +1 -1
  14. package/lib/oauthLauncher.js +5 -0
  15. package/lib/videoFrameExtract.js +3 -3
  16. package/package.json +5 -7
  17. package/routes/capabilities.js +13 -0
  18. package/routes/edit.js +2 -1
  19. package/routes/generate.js +32 -6
  20. package/routes/health.js +4 -3
  21. package/routes/multimode.js +2 -1
  22. package/routes/video.js +35 -3
  23. package/server.js +29 -2
  24. package/skills/ima2/SKILL.md +48 -6
  25. package/ui/dist/.vite/manifest.json +12 -12
  26. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-C21zqdTZ.js} +1 -1
  27. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-BN-ga1lG.js} +1 -1
  28. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BbMa4IhI.js} +1 -1
  29. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DRwBJRDQ.js} +1 -1
  30. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-Dp85kHCq.js} +2 -2
  31. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BE8Q8MLD.js} +1 -1
  32. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-PtH5x0sc.js} +1 -1
  33. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-FnM9tHI9.js} +2 -2
  34. package/ui/dist/assets/SettingsWorkspace-MARPGyBL.js +1 -0
  35. package/ui/dist/assets/index-BAFI6htx.js +42 -0
  36. package/ui/dist/assets/{index-31uVIdt4.js → index-BSXxr_Bt.js} +1 -1
  37. package/ui/dist/assets/index-DS-ADE7U.css +1 -0
  38. package/ui/dist/index.html +2 -2
  39. package/bin/commands/annotate.ts +0 -119
  40. package/bin/commands/cancel.ts +0 -48
  41. package/bin/commands/canvas-versions.ts +0 -80
  42. package/bin/commands/capabilities.ts +0 -110
  43. package/bin/commands/cardnews.ts +0 -249
  44. package/bin/commands/comfy.ts +0 -54
  45. package/bin/commands/config.ts +0 -186
  46. package/bin/commands/defaults.ts +0 -192
  47. package/bin/commands/doctor.ts +0 -202
  48. package/bin/commands/edit.ts +0 -150
  49. package/bin/commands/gen.ts +0 -214
  50. package/bin/commands/grok.ts +0 -90
  51. package/bin/commands/history.ts +0 -146
  52. package/bin/commands/ls.ts +0 -64
  53. package/bin/commands/metadata.ts +0 -39
  54. package/bin/commands/multimode.ts +0 -196
  55. package/bin/commands/node.ts +0 -166
  56. package/bin/commands/observability.ts +0 -176
  57. package/bin/commands/ping.ts +0 -31
  58. package/bin/commands/prompt-sub/build.ts +0 -101
  59. package/bin/commands/prompt.ts +0 -492
  60. package/bin/commands/ps.ts +0 -81
  61. package/bin/commands/session.ts +0 -266
  62. package/bin/commands/show.ts +0 -72
  63. package/bin/commands/skill.ts +0 -70
  64. package/bin/commands/video.ts +0 -442
  65. package/bin/ima2.ts +0 -430
  66. package/bin/lib/args.ts +0 -92
  67. package/bin/lib/browser-id.ts +0 -16
  68. package/bin/lib/client.ts +0 -122
  69. package/bin/lib/config-store.ts +0 -120
  70. package/bin/lib/destructive-confirm.ts +0 -19
  71. package/bin/lib/doctor-checks.ts +0 -91
  72. package/bin/lib/error-hints.ts +0 -23
  73. package/bin/lib/files.ts +0 -39
  74. package/bin/lib/output.ts +0 -73
  75. package/bin/lib/platform.ts +0 -99
  76. package/bin/lib/recover-output.ts +0 -139
  77. package/bin/lib/sse.ts +0 -73
  78. package/bin/lib/star-prompt.ts +0 -97
  79. package/bin/lib/storage-doctor.ts +0 -39
  80. package/bin/lib/ui-build.ts +0 -85
  81. package/config.ts +0 -354
  82. package/lib/agentCommandParser.ts +0 -69
  83. package/lib/agentGenerationPlanner.ts +0 -273
  84. package/lib/agentQuestionResponder.ts +0 -266
  85. package/lib/agentQueueStore.ts +0 -270
  86. package/lib/agentQueueWorker.ts +0 -89
  87. package/lib/agentRuntime.ts +0 -604
  88. package/lib/agentSettings.ts +0 -72
  89. package/lib/agentStore.ts +0 -422
  90. package/lib/agentStoreRows.ts +0 -136
  91. package/lib/agentTypes.ts +0 -154
  92. package/lib/apiCachePolicy.ts +0 -11
  93. package/lib/assetLifecycle.ts +0 -146
  94. package/lib/canvasVersionStore.ts +0 -223
  95. package/lib/capabilities.ts +0 -126
  96. package/lib/cardNewsGenerator.ts +0 -271
  97. package/lib/cardNewsJobStore.ts +0 -142
  98. package/lib/cardNewsManifestStore.ts +0 -154
  99. package/lib/cardNewsPlanner.ts +0 -236
  100. package/lib/cardNewsPlannerClient.ts +0 -155
  101. package/lib/cardNewsPlannerPrompt.ts +0 -62
  102. package/lib/cardNewsPlannerSchema.ts +0 -321
  103. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  104. package/lib/cardNewsTemplateStore.ts +0 -252
  105. package/lib/codexDetect.ts +0 -71
  106. package/lib/comfyBridge.ts +0 -235
  107. package/lib/composerSnapshot.ts +0 -33
  108. package/lib/configKeys.ts +0 -62
  109. package/lib/db.ts +0 -295
  110. package/lib/errInfo.ts +0 -43
  111. package/lib/errorClassify.ts +0 -100
  112. package/lib/generationCancel.ts +0 -28
  113. package/lib/generationErrors.ts +0 -238
  114. package/lib/grokImageAdapter.ts +0 -513
  115. package/lib/grokMultimodeAdapter.ts +0 -84
  116. package/lib/grokProxyLauncher.ts +0 -153
  117. package/lib/grokRuntime.ts +0 -23
  118. package/lib/grokSizeMapper.ts +0 -71
  119. package/lib/grokVideoAdapter.ts +0 -458
  120. package/lib/grokVideoCanvas.ts +0 -26
  121. package/lib/grokVideoDownload.ts +0 -59
  122. package/lib/grokVideoPlannerPrompt.ts +0 -67
  123. package/lib/historyIndex.ts +0 -51
  124. package/lib/historyList.ts +0 -181
  125. package/lib/imageMetadata.ts +0 -113
  126. package/lib/imageMetadataStore.ts +0 -67
  127. package/lib/imageModels.ts +0 -165
  128. package/lib/inflight.ts +0 -281
  129. package/lib/localImportStore.ts +0 -114
  130. package/lib/logger.ts +0 -161
  131. package/lib/nodeStore.ts +0 -91
  132. package/lib/oauthLauncher.ts +0 -94
  133. package/lib/oauthNormalize.ts +0 -30
  134. package/lib/oauthProxy/errors.ts +0 -128
  135. package/lib/oauthProxy/generators.ts +0 -494
  136. package/lib/oauthProxy/index.ts +0 -28
  137. package/lib/oauthProxy/prompts.ts +0 -123
  138. package/lib/oauthProxy/references.ts +0 -45
  139. package/lib/oauthProxy/runtime.ts +0 -115
  140. package/lib/oauthProxy/streams.ts +0 -232
  141. package/lib/oauthProxy/types.ts +0 -9
  142. package/lib/oauthProxy.ts +0 -3
  143. package/lib/openDirectory.ts +0 -47
  144. package/lib/pngInfo.ts +0 -26
  145. package/lib/promptBuilder/attachments.ts +0 -74
  146. package/lib/promptBuilder/client.ts +0 -130
  147. package/lib/promptBuilder/constants.ts +0 -9
  148. package/lib/promptBuilder/context.ts +0 -36
  149. package/lib/promptBuilder/errors.ts +0 -12
  150. package/lib/promptBuilder/requestSchema.ts +0 -56
  151. package/lib/promptBuilder/responseParser.ts +0 -219
  152. package/lib/promptBuilder/systemPrompt.ts +0 -135
  153. package/lib/promptBuilder/transport.ts +0 -94
  154. package/lib/promptBuilder/types.ts +0 -109
  155. package/lib/promptImport/curatedSources.ts +0 -141
  156. package/lib/promptImport/discoveryRegistry.ts +0 -329
  157. package/lib/promptImport/errors.ts +0 -18
  158. package/lib/promptImport/githubDiscovery.ts +0 -309
  159. package/lib/promptImport/githubFolder.ts +0 -397
  160. package/lib/promptImport/githubSource.ts +0 -257
  161. package/lib/promptImport/gptImageHints.ts +0 -70
  162. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  163. package/lib/promptImport/promptIndex.ts +0 -326
  164. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  165. package/lib/promptImport/types.ts +0 -103
  166. package/lib/promptSafetyPolicy.ts +0 -5
  167. package/lib/providerOptions.ts +0 -56
  168. package/lib/referenceImageCompress.ts +0 -84
  169. package/lib/refs.ts +0 -133
  170. package/lib/requestLogger.ts +0 -49
  171. package/lib/responsesDoctor.ts +0 -456
  172. package/lib/responsesErrors.ts +0 -83
  173. package/lib/responsesFallback.ts +0 -114
  174. package/lib/responsesImageAdapter.ts +0 -466
  175. package/lib/responsesParse.ts +0 -452
  176. package/lib/responsesTools.ts +0 -28
  177. package/lib/runtimeContext.ts +0 -146
  178. package/lib/runtimePorts.ts +0 -105
  179. package/lib/sessionStore.ts +0 -308
  180. package/lib/storageMigration.ts +0 -310
  181. package/lib/styleSheet.ts +0 -139
  182. package/lib/systemTrash.ts +0 -20
  183. package/lib/videoContinuity.ts +0 -180
  184. package/lib/videoFrameExtract.ts +0 -78
  185. package/lib/videoSeriesChain.ts +0 -29
  186. package/lib/visibleTextLanguagePolicy.ts +0 -7
  187. package/routes/agent.ts +0 -308
  188. package/routes/annotations.ts +0 -118
  189. package/routes/canvasVersions.ts +0 -69
  190. package/routes/capabilities.ts +0 -18
  191. package/routes/cardNews.ts +0 -211
  192. package/routes/comfy.ts +0 -43
  193. package/routes/edit.ts +0 -352
  194. package/routes/generate.ts +0 -492
  195. package/routes/grok.ts +0 -24
  196. package/routes/health.ts +0 -123
  197. package/routes/history.ts +0 -221
  198. package/routes/imageImport.ts +0 -37
  199. package/routes/index.ts +0 -52
  200. package/routes/metadata.ts +0 -77
  201. package/routes/multimode.ts +0 -499
  202. package/routes/nodes.ts +0 -578
  203. package/routes/promptBuilder.ts +0 -37
  204. package/routes/promptImport.ts +0 -379
  205. package/routes/prompts.ts +0 -428
  206. package/routes/quota.ts +0 -89
  207. package/routes/sessions.ts +0 -317
  208. package/routes/storage.ts +0 -47
  209. package/routes/video.ts +0 -300
  210. package/routes/videoExtended.ts +0 -284
  211. package/server.ts +0 -293
  212. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  213. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  214. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,270 +0,0 @@
1
- import { ulid } from "ulid";
2
- import { getDb } from "./db.js";
3
- import {
4
- type AgentGenerationSettings,
5
- type AgentQueueItem,
6
- type AgentQueueStatus,
7
- type AgentSlashCommand,
8
- type AgentSessionRunSummary,
9
- } from "./agentTypes.js";
10
- import { DEFAULT_AGENT_GENERATION_SETTINGS, normalizeAgentGenerationSettings } from "./agentSettings.js";
11
- import { normalizeAgentGenerationPlan } from "./agentGenerationPlanner.js";
12
-
13
- type AgentQueueRow = {
14
- id: string;
15
- sessionId: string;
16
- requestId: string;
17
- prompt: string;
18
- options: string;
19
- toolPlan: string;
20
- status: AgentQueueStatus;
21
- position: number;
22
- resultImageIds: string;
23
- errorCode: string | null;
24
- errorMessage: string | null;
25
- createdAt: number;
26
- startedAt: number | null;
27
- finishedAt: number | null;
28
- };
29
-
30
- export type AgentQueueProjection = {
31
- queueBySession: Record<string, AgentQueueItem[]>;
32
- runSummaryBySession: Record<string, AgentSessionRunSummary>;
33
- };
34
-
35
- export type AgentQueueLimits = {
36
- maxGlobalRunning: number;
37
- maxSessionRunning: number;
38
- };
39
-
40
- export function createAgentQueueItem(input: {
41
- sessionId: string;
42
- prompt: string;
43
- options?: unknown;
44
- plan?: unknown;
45
- command?: AgentSlashCommand | null;
46
- }) {
47
- const t = Date.now();
48
- const id = `aq_${ulid()}`;
49
- const requestId = `agent_queue_${ulid()}`;
50
- const options = normalizeAgentGenerationSettings(input.options);
51
- const plan = input.plan
52
- ? normalizeAgentGenerationPlan(input.prompt, input.plan, options)
53
- : normalizeAgentGenerationPlan(input.prompt, { command: input.command }, options);
54
- const position = getQueuedCount(input.sessionId) + 1;
55
- getDb().prepare(`
56
- INSERT INTO agent_queue_items
57
- (id, session_id, request_id, prompt, options, tool_plan, status, position, result_image_ids, created_at)
58
- VALUES (?, ?, ?, ?, ?, ?, 'queued', ?, '[]', ?)
59
- `).run(id, input.sessionId, requestId, input.prompt, JSON.stringify(options), JSON.stringify(plan), position, t);
60
- return getAgentQueueItem(id)!;
61
- }
62
-
63
- export function getAgentQueueItem(id: string) {
64
- const row = getDb().prepare(`
65
- SELECT
66
- id,
67
- session_id AS sessionId,
68
- request_id AS requestId,
69
- prompt,
70
- options,
71
- tool_plan AS toolPlan,
72
- status,
73
- position,
74
- result_image_ids AS resultImageIds,
75
- error_code AS errorCode,
76
- error_message AS errorMessage,
77
- created_at AS createdAt,
78
- started_at AS startedAt,
79
- finished_at AS finishedAt
80
- FROM agent_queue_items
81
- WHERE id = ?
82
- `).get(id) as AgentQueueRow | undefined;
83
- return row ? queueItemFromRow(row) : null;
84
- }
85
-
86
- export function listAgentQueueItems(sessionId?: string | null) {
87
- const where = sessionId ? "WHERE session_id = ?" : "";
88
- const rows = getDb().prepare(`
89
- SELECT
90
- id,
91
- session_id AS sessionId,
92
- request_id AS requestId,
93
- prompt,
94
- options,
95
- tool_plan AS toolPlan,
96
- status,
97
- position,
98
- result_image_ids AS resultImageIds,
99
- error_code AS errorCode,
100
- error_message AS errorMessage,
101
- created_at AS createdAt,
102
- started_at AS startedAt,
103
- finished_at AS finishedAt
104
- FROM agent_queue_items
105
- ${where}
106
- ORDER BY
107
- CASE status WHEN 'running' THEN 0 WHEN 'queued' THEN 1 WHEN 'failed' THEN 2 ELSE 3 END,
108
- created_at DESC
109
- LIMIT 80
110
- `).all(...(sessionId ? [sessionId] : [])) as AgentQueueRow[];
111
- return rows.map(queueItemFromRow);
112
- }
113
-
114
- export function getAgentQueueProjection(sessionIds: readonly string[]): AgentQueueProjection {
115
- const queueBySession: Record<string, AgentQueueItem[]> = {};
116
- const runSummaryBySession: Record<string, AgentSessionRunSummary> = {};
117
- for (const sessionId of sessionIds) {
118
- const items = listAgentQueueItems(sessionId);
119
- queueBySession[sessionId] = items;
120
- runSummaryBySession[sessionId] = summarizeQueue(items);
121
- }
122
- return { queueBySession, runSummaryBySession };
123
- }
124
-
125
- export function claimNextAgentQueueItem(limits: AgentQueueLimits) {
126
- if (countRunningItems() >= limits.maxGlobalRunning) return null;
127
- const rows = getDb().prepare(`
128
- SELECT
129
- id,
130
- session_id AS sessionId,
131
- request_id AS requestId,
132
- prompt,
133
- options,
134
- tool_plan AS toolPlan,
135
- status,
136
- position,
137
- result_image_ids AS resultImageIds,
138
- error_code AS errorCode,
139
- error_message AS errorMessage,
140
- created_at AS createdAt,
141
- started_at AS startedAt,
142
- finished_at AS finishedAt
143
- FROM agent_queue_items
144
- WHERE status = 'queued'
145
- ORDER BY created_at ASC
146
- LIMIT 20
147
- `).all() as AgentQueueRow[];
148
-
149
- for (const row of rows) {
150
- if (countRunningItems(row.sessionId) >= limits.maxSessionRunning) continue;
151
- const res = getDb().prepare(`
152
- UPDATE agent_queue_items
153
- SET status = 'running', started_at = ?, error_code = NULL, error_message = NULL
154
- WHERE id = ? AND status = 'queued'
155
- `).run(Date.now(), row.id);
156
- if (res.changes > 0) return getAgentQueueItem(row.id);
157
- }
158
- return null;
159
- }
160
-
161
- export function completeAgentQueueItem(id: string, imageIds: readonly string[]) {
162
- getDb().prepare(`
163
- UPDATE agent_queue_items
164
- SET status = 'succeeded',
165
- result_image_ids = ?,
166
- finished_at = ?,
167
- error_code = NULL,
168
- error_message = NULL
169
- WHERE id = ?
170
- `).run(JSON.stringify([...imageIds]), Date.now(), id);
171
- }
172
-
173
- export function failAgentQueueItem(id: string, error: { code?: string | null; message: string }) {
174
- getDb().prepare(`
175
- UPDATE agent_queue_items
176
- SET status = 'failed',
177
- error_code = ?,
178
- error_message = ?,
179
- finished_at = ?
180
- WHERE id = ?
181
- `).run(error.code ?? "AGENT_QUEUE_FAILED", error.message, Date.now(), id);
182
- }
183
-
184
- export function cancelAgentQueueItem(id: string) {
185
- const res = getDb().prepare(`
186
- UPDATE agent_queue_items
187
- SET status = 'canceled', finished_at = ?
188
- WHERE id = ? AND status = 'queued'
189
- `).run(Date.now(), id);
190
- return res.changes > 0;
191
- }
192
-
193
- export function retryAgentQueueItem(id: string) {
194
- const res = getDb().prepare(`
195
- UPDATE agent_queue_items
196
- SET status = 'queued',
197
- position = ?,
198
- result_image_ids = '[]',
199
- error_code = NULL,
200
- error_message = NULL,
201
- started_at = NULL,
202
- finished_at = NULL
203
- WHERE id = ? AND status IN ('failed', 'canceled')
204
- `).run(getQueuedCount(null) + 1, id);
205
- return res.changes > 0;
206
- }
207
-
208
- function queueItemFromRow(row: AgentQueueRow): AgentQueueItem {
209
- const options = normalizeAgentGenerationSettings(parseJson(row.options, {}));
210
- return {
211
- id: row.id,
212
- sessionId: row.sessionId,
213
- requestId: row.requestId,
214
- prompt: row.prompt,
215
- status: row.status,
216
- position: row.position,
217
- resultImageIds: parseStringArray(row.resultImageIds),
218
- errorCode: row.errorCode,
219
- errorMessage: row.errorMessage,
220
- createdAt: row.createdAt,
221
- startedAt: row.startedAt,
222
- finishedAt: row.finishedAt,
223
- options,
224
- plan: normalizeAgentGenerationPlan(row.prompt, parseJson(row.toolPlan, {}), options),
225
- };
226
- }
227
-
228
- function summarizeQueue(items: readonly AgentQueueItem[]): AgentSessionRunSummary {
229
- const queuedCount = items.filter((item) => item.status === "queued").length;
230
- const runningCount = items.filter((item) => item.status === "running").length;
231
- const failed = items.find((item) => item.status === "failed");
232
- return {
233
- status: runningCount > 0 ? "running" : queuedCount > 0 ? "queued" : failed ? "error" : "idle",
234
- queuedCount,
235
- runningCount,
236
- lastQueueItemId: items[0]?.id ?? null,
237
- lastError: failed?.errorMessage ?? null,
238
- };
239
- }
240
-
241
- function getQueuedCount(sessionId: string | null) {
242
- const row = sessionId
243
- ? getDb().prepare("SELECT COUNT(*) AS count FROM agent_queue_items WHERE session_id = ? AND status = 'queued'").get(sessionId)
244
- : getDb().prepare("SELECT COUNT(*) AS count FROM agent_queue_items WHERE status = 'queued'").get();
245
- return Number((row as { count?: number } | undefined)?.count ?? 0);
246
- }
247
-
248
- function countRunningItems(sessionId?: string) {
249
- const row = sessionId
250
- ? getDb().prepare("SELECT COUNT(*) AS count FROM agent_queue_items WHERE session_id = ? AND status = 'running'").get(sessionId)
251
- : getDb().prepare("SELECT COUNT(*) AS count FROM agent_queue_items WHERE status = 'running'").get();
252
- return Number((row as { count?: number } | undefined)?.count ?? 0);
253
- }
254
-
255
- function parseJson(value: string, fallback: unknown) {
256
- try {
257
- return JSON.parse(value);
258
- } catch {
259
- return fallback;
260
- }
261
- }
262
-
263
- function parseStringArray(value: string) {
264
- const parsed = parseJson(value, []);
265
- return Array.isArray(parsed) ? parsed.filter((item): item is string => typeof item === "string") : [];
266
- }
267
-
268
- export function getDefaultAgentQueueOptions(): AgentGenerationSettings {
269
- return DEFAULT_AGENT_GENERATION_SETTINGS;
270
- }
@@ -1,89 +0,0 @@
1
- import {
2
- claimNextAgentQueueItem,
3
- completeAgentQueueItem,
4
- failAgentQueueItem,
5
- getAgentQueueItem,
6
- type AgentQueueLimits,
7
- } from "./agentQueueStore.js";
8
- import { runAgentGenerationPlan } from "./agentRuntime.js";
9
- import { errInfo } from "./errInfo.js";
10
- import { finishJob, setJobPhase, startJob } from "./inflight.js";
11
- import { logEvent } from "./logger.js";
12
- import type { RuntimeContext } from "./runtimeContext.js";
13
-
14
- const DEFAULT_LIMITS: AgentQueueLimits = {
15
- maxGlobalRunning: 2,
16
- maxSessionRunning: 1,
17
- };
18
-
19
- let workerTimer: NodeJS.Timeout | null = null;
20
- let ticking = false;
21
-
22
- export function ensureAgentQueueWorker(ctx: RuntimeContext) {
23
- if (workerTimer) return;
24
- workerTimer = setInterval(() => {
25
- void tickAgentQueueWorker(ctx);
26
- }, 1_500);
27
- workerTimer.unref?.();
28
- void tickAgentQueueWorker(ctx);
29
- }
30
-
31
- export async function tickAgentQueueWorker(ctx: RuntimeContext) {
32
- if (ticking) return;
33
- ticking = true;
34
- try {
35
- while (true) {
36
- const item = claimNextAgentQueueItem(DEFAULT_LIMITS);
37
- if (!item) return;
38
- void runClaimedQueueItem(ctx, item.id).finally(() => {
39
- void tickAgentQueueWorker(ctx);
40
- });
41
- }
42
- } finally {
43
- ticking = false;
44
- }
45
- }
46
-
47
- async function runClaimedQueueItem(ctx: RuntimeContext, itemId: string) {
48
- const item = getAgentQueueItem(itemId);
49
- if (!item) return;
50
- startJob({
51
- requestId: item.requestId,
52
- kind: "agent_queue",
53
- prompt: item.prompt,
54
- meta: {
55
- sessionId: item.sessionId,
56
- queueItemId: item.id,
57
- variants: item.plan.plannedVariants,
58
- parallelism: item.plan.plannedParallelism,
59
- requestedVariants: item.plan.requestedVariants,
60
- },
61
- });
62
- try {
63
- logEvent("agent_queue", "start", { itemId: item.id, sessionId: item.sessionId });
64
- setJobPhase(item.requestId, "streaming");
65
- const result = await runAgentGenerationPlan(ctx, item.sessionId, item.prompt, item.plan, {
66
- ...item.options,
67
- requestId: item.requestId,
68
- webSearchEnabled: item.options.webSearchEnabled,
69
- parallelism: item.plan.plannedParallelism,
70
- }, {
71
- appendUserTurn: false,
72
- });
73
- completeAgentQueueItem(item.id, result.imageIds);
74
- finishJob(item.requestId, {
75
- status: "completed",
76
- meta: { imageIds: result.imageIds },
77
- });
78
- logEvent("agent_queue", "finish", { itemId: item.id, imageCount: result.imageIds.length });
79
- } catch (error) {
80
- const err = errInfo(error);
81
- failAgentQueueItem(item.id, { code: err.code, message: err.message });
82
- finishJob(item.requestId, {
83
- status: "failed",
84
- errorCode: err.code,
85
- meta: { queueItemId: item.id },
86
- });
87
- logEvent("agent_queue", "error", { itemId: item.id, code: err.code, message: err.message });
88
- }
89
- }