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