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
package/lib/agentStore.ts DELETED
@@ -1,422 +0,0 @@
1
- import { ulid } from "ulid";
2
- import { getDb } from "./db.js";
3
- import { getAgentQueueProjection } from "./agentQueueStore.js";
4
- import {
5
- DEFAULT_AGENT_GENERATION_SETTINGS,
6
- mergeAgentGenerationSettings,
7
- } from "./agentSettings.js";
8
- import {
9
- cleanString,
10
- cleanStringArray,
11
- imageFromRow,
12
- jsonStringArray,
13
- parseStringArray,
14
- sessionFromRow,
15
- turnFromRow,
16
- type AgentImageRow,
17
- type AgentSessionRow,
18
- type AgentTurnRow,
19
- } from "./agentStoreRows.js";
20
- import {
21
- AGENT_ALLOWED_TOOLS,
22
- type AgentGenerationSettings,
23
- type AgentImageHandle,
24
- type AgentImageInput,
25
- type AgentTurn,
26
- type AgentTurnRole,
27
- type AgentTurnStatus,
28
- type AgentWorkspacePayload,
29
- } from "./agentTypes.js";
30
-
31
- type FindingRow = {
32
- id: string;
33
- query: string;
34
- url: string | null;
35
- title: string | null;
36
- snippet: string | null;
37
- };
38
- type LockRow = { styleLocks: string; subjectLocks: string };
39
- type ReferenceRow = {
40
- id: string;
41
- role: string;
42
- imageId: string | null;
43
- filename: string | null;
44
- url: string | null;
45
- prompt: string | null;
46
- };
47
-
48
- function now() {
49
- return Date.now();
50
- }
51
-
52
- export function listAgentSessions() {
53
- const db = getDb();
54
- const rows = db.prepare(`
55
- SELECT
56
- s.id,
57
- s.title,
58
- s.codex_thread_id AS codexThreadId,
59
- s.last_turn_id AS lastTurnId,
60
- s.current_image_id AS currentImageId,
61
- s.compacted,
62
- s.web_search_enabled AS webSearchEnabled,
63
- s.generation_settings AS generationSettings,
64
- s.updated_at AS updatedAt,
65
- COUNT(i.id) AS imageCount
66
- FROM agent_sessions s
67
- LEFT JOIN agent_images i ON i.session_id = s.id
68
- GROUP BY s.id
69
- ORDER BY s.updated_at DESC
70
- `).all() as AgentSessionRow[];
71
- return rows.map(sessionFromRow);
72
- }
73
-
74
- export function getAgentSession(id: string) {
75
- return listAgentSessions().find((session) => session.id === id) ?? null;
76
- }
77
-
78
- export function createAgentSession(input: {
79
- title?: unknown;
80
- currentImage?: AgentImageInput | null;
81
- webSearchEnabled?: boolean;
82
- } = {}) {
83
- const db = getDb();
84
- const id = `as_${ulid()}`;
85
- const t = now();
86
- const generationSettings = {
87
- ...DEFAULT_AGENT_GENERATION_SETTINGS,
88
- webSearchEnabled: input.webSearchEnabled !== false,
89
- };
90
- db.prepare(`
91
- INSERT INTO agent_sessions
92
- (id, title, codex_thread_id, web_search_enabled, generation_settings, created_at, updated_at)
93
- VALUES (?, ?, ?, ?, ?, ?, ?)
94
- `).run(
95
- id,
96
- cleanString(input.title, "New Agent"),
97
- `codex_${ulid()}`,
98
- input.webSearchEnabled === false ? 0 : 1,
99
- JSON.stringify(generationSettings),
100
- t,
101
- t,
102
- );
103
- if (input.currentImage) importAgentImage(id, input.currentImage);
104
- return getAgentSession(id)!;
105
- }
106
-
107
- export function renameAgentSession(id: string, title: unknown) {
108
- const cleanTitle = cleanString(title, "New Agent");
109
- const res = getDb()
110
- .prepare("UPDATE agent_sessions SET title = ?, updated_at = ? WHERE id = ?")
111
- .run(cleanTitle, now(), id);
112
- return res.changes > 0;
113
- }
114
-
115
- export function setAgentWebSearch(id: string, enabled: boolean) {
116
- const current = getAgentSession(id)?.generationSettings ?? DEFAULT_AGENT_GENERATION_SETTINGS;
117
- const next = { ...current, webSearchEnabled: enabled };
118
- const res = getDb()
119
- .prepare("UPDATE agent_sessions SET web_search_enabled = ?, generation_settings = ?, updated_at = ? WHERE id = ?")
120
- .run(enabled ? 1 : 0, JSON.stringify(next), now(), id);
121
- return res.changes > 0;
122
- }
123
-
124
- export function setAgentGenerationSettings(id: string, patch: unknown) {
125
- const current = getAgentSession(id)?.generationSettings ?? DEFAULT_AGENT_GENERATION_SETTINGS;
126
- const next = mergeAgentGenerationSettings(current, patch);
127
- const res = getDb()
128
- .prepare("UPDATE agent_sessions SET generation_settings = ?, web_search_enabled = ?, updated_at = ? WHERE id = ?")
129
- .run(JSON.stringify(next), next.webSearchEnabled ? 1 : 0, now(), id);
130
- return res.changes > 0;
131
- }
132
-
133
- export function getAgentGenerationSettings(id: string): AgentGenerationSettings {
134
- return getAgentSession(id)?.generationSettings ?? DEFAULT_AGENT_GENERATION_SETTINGS;
135
- }
136
-
137
- export function setAgentLocks(id: string, locks: { styleLocks?: unknown; subjectLocks?: unknown }) {
138
- const res = getDb().prepare(`
139
- UPDATE agent_sessions
140
- SET style_locks = COALESCE(?, style_locks),
141
- subject_locks = COALESCE(?, subject_locks),
142
- updated_at = ?
143
- WHERE id = ?
144
- `).run(
145
- Array.isArray(locks.styleLocks) ? JSON.stringify(cleanStringArray(locks.styleLocks)) : null,
146
- Array.isArray(locks.subjectLocks) ? JSON.stringify(cleanStringArray(locks.subjectLocks)) : null,
147
- now(),
148
- id,
149
- );
150
- return res.changes > 0;
151
- }
152
-
153
- export function setAgentCurrentImage(sessionId: string, imageIdValue: unknown) {
154
- const imageId = cleanString(imageIdValue);
155
- if (!imageId) return false;
156
- const image = getDb()
157
- .prepare("SELECT id FROM agent_images WHERE session_id = ? AND id = ?")
158
- .get(sessionId, imageId) as { id: string } | undefined;
159
- if (!image) return false;
160
- const res = getDb()
161
- .prepare("UPDATE agent_sessions SET current_image_id = ?, updated_at = ? WHERE id = ?")
162
- .run(imageId, now(), sessionId);
163
- return res.changes > 0;
164
- }
165
-
166
- export function deleteAgentSession(id: string) {
167
- const res = getDb().prepare("DELETE FROM agent_sessions WHERE id = ?").run(id);
168
- return res.changes > 0;
169
- }
170
-
171
- export function appendAgentTurn(input: {
172
- sessionId: string;
173
- role: AgentTurnRole;
174
- text?: string;
175
- status?: AgentTurnStatus;
176
- imageIds?: string[];
177
- webFindingIds?: string[];
178
- raw?: unknown;
179
- }) {
180
- const id = `at_${ulid()}`;
181
- const t = now();
182
- getDb().prepare(`
183
- INSERT INTO agent_turns
184
- (id, session_id, role, text, status, image_ids, web_finding_ids, raw, created_at)
185
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
186
- `).run(
187
- id,
188
- input.sessionId,
189
- input.role,
190
- cleanString(input.text, ""),
191
- input.status ?? "complete",
192
- jsonStringArray(input.imageIds),
193
- jsonStringArray(input.webFindingIds),
194
- JSON.stringify(input.raw ?? {}),
195
- t,
196
- );
197
- touchAgentSession(input.sessionId, { lastTurnId: id });
198
- return getAgentTurns(input.sessionId).find((turn) => turn.id === id)!;
199
- }
200
-
201
- export function getAgentTurns(sessionId: string) {
202
- const rows = getDb().prepare(`
203
- SELECT
204
- id,
205
- role,
206
- text,
207
- status,
208
- image_ids AS imageIds,
209
- web_finding_ids AS webFindingIds,
210
- raw,
211
- created_at AS createdAt
212
- FROM agent_turns
213
- WHERE session_id = ?
214
- ORDER BY created_at ASC
215
- `).all(sessionId) as AgentTurnRow[];
216
- return rows.map(turnFromRow);
217
- }
218
-
219
- export function importAgentImage(sessionId: string, input: AgentImageInput) {
220
- const id = cleanString(input.id, `ai_${ulid()}`);
221
- const filename = cleanString(input.filename, `${id}.png`);
222
- const url = cleanString(input.url, `/generated/${filename}`);
223
- const t = typeof input.createdAt === "number" ? input.createdAt : now();
224
- getDb().prepare(`
225
- INSERT OR REPLACE INTO agent_images
226
- (id, session_id, filename, url, thumb_url, prompt, revised_prompt, width, height, created_at)
227
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
228
- `).run(
229
- id,
230
- sessionId,
231
- filename,
232
- url,
233
- input.thumbUrl ?? null,
234
- input.prompt ?? null,
235
- input.revisedPrompt ?? null,
236
- input.width ?? null,
237
- input.height ?? null,
238
- t,
239
- );
240
- touchAgentSession(sessionId, { currentImageId: id });
241
- return getAgentImages(sessionId).find((image) => image.id === id)!;
242
- }
243
-
244
- export function getAgentImages(sessionId: string) {
245
- const rows = getDb().prepare(`
246
- SELECT
247
- id,
248
- filename,
249
- url,
250
- thumb_url AS thumbUrl,
251
- prompt,
252
- revised_prompt AS revisedPrompt,
253
- width,
254
- height,
255
- created_at AS createdAt
256
- FROM agent_images
257
- WHERE session_id = ?
258
- ORDER BY created_at ASC
259
- `).all(sessionId) as AgentImageRow[];
260
- return rows.map(imageFromRow);
261
- }
262
-
263
- export function recordAgentWebFinding(input: {
264
- sessionId: string;
265
- query: string;
266
- url?: string | null;
267
- title?: string | null;
268
- snippet?: string | null;
269
- }) {
270
- const id = `aw_${ulid()}`;
271
- getDb().prepare(`
272
- INSERT INTO agent_web_findings
273
- (id, session_id, query, url, title, snippet, created_at)
274
- VALUES (?, ?, ?, ?, ?, ?, ?)
275
- `).run(id, input.sessionId, input.query, input.url ?? null, input.title ?? null, input.snippet ?? null, now());
276
- return id;
277
- }
278
-
279
- export function getAgentWorkspacePayload(selectedSessionId?: string | null): AgentWorkspacePayload {
280
- const sessions = listAgentSessions();
281
- const selected = selectedSessionId && sessions.some((item) => item.id === selectedSessionId)
282
- ? selectedSessionId
283
- : sessions[0]?.id ?? null;
284
- const turnsBySession: Record<string, AgentTurn[]> = {};
285
- const imagesById: Record<string, AgentImageHandle> = {};
286
- const imageIdsBySession: Record<string, string[]> = {};
287
- for (const session of sessions) {
288
- turnsBySession[session.id] = getAgentTurns(session.id);
289
- const images = getAgentImages(session.id);
290
- imageIdsBySession[session.id] = images.map((image) => image.id);
291
- for (const image of images) imagesById[image.id] = image;
292
- }
293
- const currentImageId = selected ? getCurrentImageId(selected) : null;
294
- const queueProjection = getAgentQueueProjection(sessions.map((session) => session.id));
295
- return {
296
- sessions,
297
- turnsBySession,
298
- imagesById,
299
- imageIdsBySession,
300
- selectedSessionId: selected,
301
- currentImageId,
302
- allowedTools: AGENT_ALLOWED_TOOLS,
303
- manifest: selected ? buildImageContextManifest(selected) : null,
304
- queueBySession: queueProjection.queueBySession,
305
- runSummaryBySession: queueProjection.runSummaryBySession,
306
- };
307
- }
308
-
309
- export function buildImageContextManifest(sessionId: string) {
310
- const session = getAgentSession(sessionId);
311
- if (!session) return "";
312
- const image = session.lastImageId
313
- ? getAgentImages(sessionId).find((item) => item.id === session.lastImageId)
314
- : null;
315
- const findings = getAgentWebFindings(sessionId);
316
- const references = getAgentReferences(sessionId);
317
- const locks = getAgentLocks(sessionId);
318
- return [
319
- "<ima2-image-context>",
320
- `sessionId: ${session.id}`,
321
- `codexThreadId: ${session.codexThreadId ?? ""}`,
322
- `compactStatus: ${session.compacted ? "compacted" : "live"}`,
323
- "currentImage:",
324
- ` id: ${image?.id ?? ""}`,
325
- ` path: ${image?.filename ?? ""}`,
326
- ` prompt: ${image?.prompt ?? ""}`,
327
- ` revisedPrompt: ${image?.revisedPrompt ?? ""}`,
328
- "styleLocks:",
329
- ...locks.styleLocks.map((lock) => ` - ${lock}`),
330
- "subjectLocks:",
331
- ...locks.subjectLocks.map((lock) => ` - ${lock}`),
332
- "references:",
333
- ...references.map((ref) => ` - id: ${ref.id}\n role: ${ref.role}\n imageId: ${ref.imageId ?? ""}\n url: ${ref.url ?? ""}`),
334
- "webFindings:",
335
- ...findings.map((finding) => ` - query: ${finding.query}\n url: ${finding.url ?? ""}\n snippet: ${finding.snippet ?? ""}`),
336
- "constraints:",
337
- " - final user-visible output must be an image",
338
- ` - allowed tools: ${AGENT_ALLOWED_TOOLS.join(", ")}`,
339
- "</ima2-image-context>",
340
- ].join("\n");
341
- }
342
-
343
- export function compactAgentSession(sessionId: string) {
344
- getDb()
345
- .prepare("UPDATE agent_sessions SET compacted = 1, updated_at = ? WHERE id = ?")
346
- .run(now(), sessionId);
347
- appendAgentTurn({
348
- sessionId,
349
- role: "assistant",
350
- text: "Image context compacted and manifest retained for resume.",
351
- status: "complete",
352
- });
353
- }
354
-
355
- export function restartAgentRuntimeSession(sessionId: string, reason: string) {
356
- const nextThreadId = `codex_${ulid()}`;
357
- getDb().prepare(`
358
- UPDATE agent_sessions
359
- SET codex_thread_id = ?,
360
- compacted = 0,
361
- updated_at = ?
362
- WHERE id = ?
363
- `).run(nextThreadId, now(), sessionId);
364
- appendAgentTurn({
365
- sessionId,
366
- role: "tool",
367
- text: `Codex runtime restarted: ${reason}`,
368
- status: "error",
369
- });
370
- return nextThreadId;
371
- }
372
-
373
- export function touchAgentSession(sessionId: string, fields: { lastTurnId?: string; currentImageId?: string } = {}) {
374
- getDb().prepare(`
375
- UPDATE agent_sessions
376
- SET last_turn_id = COALESCE(?, last_turn_id),
377
- current_image_id = COALESCE(?, current_image_id),
378
- updated_at = ?
379
- WHERE id = ?
380
- `).run(fields.lastTurnId ?? null, fields.currentImageId ?? null, now(), sessionId);
381
- }
382
-
383
- function getCurrentImageId(sessionId: string) {
384
- const row = getDb()
385
- .prepare("SELECT current_image_id AS currentImageId FROM agent_sessions WHERE id = ?")
386
- .get(sessionId) as { currentImageId?: string | null } | undefined;
387
- return row?.currentImageId ?? null;
388
- }
389
-
390
- function getAgentWebFindings(sessionId: string) {
391
- return getDb().prepare(`
392
- SELECT id, query, url, title, snippet
393
- FROM agent_web_findings
394
- WHERE session_id = ?
395
- ORDER BY created_at ASC
396
- `).all(sessionId) as FindingRow[];
397
- }
398
-
399
- function getAgentReferences(sessionId: string) {
400
- return getDb().prepare(`
401
- SELECT
402
- id,
403
- role,
404
- image_id AS imageId,
405
- filename,
406
- url,
407
- prompt
408
- FROM agent_references
409
- WHERE session_id = ?
410
- ORDER BY created_at ASC
411
- `).all(sessionId) as ReferenceRow[];
412
- }
413
-
414
- function getAgentLocks(sessionId: string) {
415
- const row = getDb()
416
- .prepare("SELECT style_locks AS styleLocks, subject_locks AS subjectLocks FROM agent_sessions WHERE id = ?")
417
- .get(sessionId) as LockRow | undefined;
418
- return {
419
- styleLocks: parseStringArray(row?.styleLocks ?? "[]"),
420
- subjectLocks: parseStringArray(row?.subjectLocks ?? "[]"),
421
- };
422
- }
@@ -1,136 +0,0 @@
1
- import {
2
- DEFAULT_AGENT_GENERATION_SETTINGS,
3
- normalizeAgentGenerationSettings,
4
- } from "./agentSettings.js";
5
- import type {
6
- AgentImageHandle,
7
- AgentSessionSummary,
8
- AgentToolCallSummary,
9
- AgentTurn,
10
- AgentTurnRole,
11
- AgentTurnStatus,
12
- } from "./agentTypes.js";
13
-
14
- export type AgentSessionRow = {
15
- id: string;
16
- title: string;
17
- codexThreadId: string | null;
18
- lastTurnId: string | null;
19
- currentImageId: string | null;
20
- compacted: number;
21
- webSearchEnabled: number;
22
- generationSettings: string;
23
- updatedAt: number;
24
- imageCount: number;
25
- };
26
-
27
- export type AgentTurnRow = {
28
- id: string;
29
- role: AgentTurnRole;
30
- text: string;
31
- status: AgentTurnStatus;
32
- imageIds: string;
33
- webFindingIds: string;
34
- raw: string;
35
- createdAt: number;
36
- };
37
-
38
- export type AgentImageRow = {
39
- id: string;
40
- filename: string;
41
- url: string;
42
- thumbUrl: string | null;
43
- prompt: string | null;
44
- revisedPrompt: string | null;
45
- width: number | null;
46
- height: number | null;
47
- createdAt: number;
48
- };
49
-
50
- export function cleanString(value: unknown, fallback = "") {
51
- if (typeof value !== "string") return fallback;
52
- return value.trim().slice(0, 10_000) || fallback;
53
- }
54
-
55
- export function parseStringArray(value: string) {
56
- try {
57
- const parsed = JSON.parse(value);
58
- return Array.isArray(parsed) ? parsed.filter((item) => typeof item === "string") : [];
59
- } catch {
60
- return [];
61
- }
62
- }
63
-
64
- export function parseJsonObject(value: string): Record<string, unknown> {
65
- try {
66
- const parsed = JSON.parse(value);
67
- return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed as Record<string, unknown> : {};
68
- } catch {
69
- return {};
70
- }
71
- }
72
-
73
- export function parseToolCalls(raw: string): AgentToolCallSummary[] | undefined {
74
- const toolCalls = parseJsonObject(raw).toolCalls;
75
- if (!Array.isArray(toolCalls)) return undefined;
76
- return toolCalls.filter((item): item is AgentToolCallSummary => (
77
- item &&
78
- typeof item === "object" &&
79
- typeof (item as { id?: unknown }).id === "string" &&
80
- typeof (item as { name?: unknown }).name === "string"
81
- ));
82
- }
83
-
84
- export function jsonStringArray(values: readonly string[] | undefined) {
85
- return JSON.stringify(Array.isArray(values) ? values.filter(Boolean) : []);
86
- }
87
-
88
- export function cleanStringArray(values: unknown) {
89
- if (!Array.isArray(values)) return [];
90
- return values.filter((value) => typeof value === "string" && value.trim()).map((value) => value.trim());
91
- }
92
-
93
- export function sessionFromRow(row: AgentSessionRow): AgentSessionSummary {
94
- return {
95
- id: row.id,
96
- title: row.title,
97
- codexThreadId: row.codexThreadId,
98
- lastTurnId: row.lastTurnId,
99
- lastImageId: row.currentImageId,
100
- imageCount: row.imageCount,
101
- compacted: row.compacted === 1,
102
- webSearchEnabled: row.webSearchEnabled === 1,
103
- generationSettings: normalizeAgentGenerationSettings(
104
- parseJsonObject(row.generationSettings),
105
- { ...DEFAULT_AGENT_GENERATION_SETTINGS, webSearchEnabled: row.webSearchEnabled === 1 },
106
- ),
107
- updatedAt: row.updatedAt,
108
- };
109
- }
110
-
111
- export function turnFromRow(row: AgentTurnRow): AgentTurn {
112
- return {
113
- id: row.id,
114
- role: row.role,
115
- text: row.text,
116
- status: row.status,
117
- imageIds: parseStringArray(row.imageIds),
118
- webFindingIds: parseStringArray(row.webFindingIds),
119
- toolCalls: parseToolCalls(row.raw),
120
- createdAt: row.createdAt,
121
- };
122
- }
123
-
124
- export function imageFromRow(row: AgentImageRow): AgentImageHandle {
125
- return {
126
- id: row.id,
127
- filename: row.filename,
128
- url: row.url,
129
- thumbUrl: row.thumbUrl,
130
- prompt: row.prompt,
131
- revisedPrompt: row.revisedPrompt,
132
- width: row.width,
133
- height: row.height,
134
- createdAt: row.createdAt,
135
- };
136
- }
package/lib/agentTypes.ts DELETED
@@ -1,154 +0,0 @@
1
- export const AGENT_ALLOWED_TOOLS = [
2
- "ima2.get_image_context",
3
- "ima2.web_search",
4
- "ima2.generate_image",
5
- "ima2.generate_video",
6
- ] as const;
7
-
8
- export type AgentToolName = typeof AGENT_ALLOWED_TOOLS[number];
9
- export type AgentTurnRole = "user" | "assistant" | "tool";
10
- export type AgentTurnStatus = "streaming" | "complete" | "error";
11
- export type AgentToolCallStatus = "queued" | "running" | "complete" | "error";
12
- export type AgentQueueStatus = "queued" | "running" | "succeeded" | "failed" | "canceled";
13
- export type AgentSessionRunStatus = "idle" | "queued" | "running" | "error";
14
- export type AgentGenerationStrategy = "auto" | "manual";
15
- export type AgentGenerationPlanMode = "single" | "fanout" | "question" | "video";
16
- export type AgentGenerationPlanSource = "auto-default" | "auto-request" | "manual-settings" | "slash-command" | "question-command";
17
- export type AgentSlashCommandName = "question" | "help" | "variants" | "generate" | "parallelism";
18
-
19
- export interface AgentGenerationSettings {
20
- provider: "oauth" | "api" | "grok";
21
- model: string;
22
- quality: "low" | "medium" | "high";
23
- size: string;
24
- format: "png" | "jpeg" | "webp";
25
- moderation: "auto" | "low";
26
- reasoningEffort: "low" | "medium" | "high" | "xhigh";
27
- webSearchEnabled: boolean;
28
- generationStrategy: AgentGenerationStrategy;
29
- variants: number;
30
- maxAutoVariants: number;
31
- parallelism: number;
32
- }
33
-
34
- export interface AgentSlashCommand {
35
- name: AgentSlashCommandName;
36
- rawName: string;
37
- raw: string;
38
- prompt: string;
39
- value?: number;
40
- }
41
-
42
- export interface AgentToolCallSummary {
43
- id: string;
44
- name: AgentToolName;
45
- status: AgentToolCallStatus;
46
- startedAt?: number | null;
47
- finishedAt?: number | null;
48
- durationMs?: number | null;
49
- requestId?: string | null;
50
- inputSummary?: string | null;
51
- outputSummary?: string | null;
52
- imageIds?: string[];
53
- webFindingIds?: string[];
54
- errorCode?: string | null;
55
- errorMessage?: string | null;
56
- }
57
-
58
- export interface AgentQueueItem {
59
- id: string;
60
- sessionId: string;
61
- requestId: string;
62
- prompt: string;
63
- status: AgentQueueStatus;
64
- position: number;
65
- resultImageIds: string[];
66
- errorCode?: string | null;
67
- errorMessage?: string | null;
68
- createdAt: number;
69
- startedAt?: number | null;
70
- finishedAt?: number | null;
71
- options: AgentGenerationSettings;
72
- plan: AgentGenerationPlan;
73
- }
74
-
75
- export interface AgentGenerationPlan {
76
- mode: AgentGenerationPlanMode;
77
- prompts: string[];
78
- requestedVariants: number;
79
- plannedVariants: number;
80
- plannedParallelism: number;
81
- source: AgentGenerationPlanSource;
82
- reason: string;
83
- command?: AgentSlashCommandName | null;
84
- assistantText?: string | null;
85
- }
86
-
87
- export interface AgentSessionRunSummary {
88
- status: AgentSessionRunStatus;
89
- queuedCount: number;
90
- runningCount: number;
91
- lastQueueItemId?: string | null;
92
- lastError?: string | null;
93
- }
94
-
95
- export interface AgentImageInput {
96
- id?: string | null;
97
- filename?: string | null;
98
- url?: string | null;
99
- thumbUrl?: string | null;
100
- prompt?: string | null;
101
- revisedPrompt?: string | null;
102
- createdAt?: number | null;
103
- width?: number | null;
104
- height?: number | null;
105
- }
106
-
107
- export interface AgentImageHandle {
108
- id: string;
109
- filename: string;
110
- url: string;
111
- thumbUrl?: string | null;
112
- prompt?: string | null;
113
- revisedPrompt?: string | null;
114
- createdAt: number;
115
- width?: number | null;
116
- height?: number | null;
117
- }
118
-
119
- export interface AgentSessionSummary {
120
- id: string;
121
- title: string;
122
- codexThreadId: string | null;
123
- lastTurnId: string | null;
124
- lastImageId: string | null;
125
- imageCount: number;
126
- compacted: boolean;
127
- webSearchEnabled: boolean;
128
- generationSettings: AgentGenerationSettings;
129
- updatedAt: number;
130
- }
131
-
132
- export interface AgentTurn {
133
- id: string;
134
- role: AgentTurnRole;
135
- text: string;
136
- imageIds: string[];
137
- webFindingIds: string[];
138
- status: AgentTurnStatus;
139
- toolCalls?: AgentToolCallSummary[];
140
- createdAt: number;
141
- }
142
-
143
- export interface AgentWorkspacePayload {
144
- sessions: AgentSessionSummary[];
145
- turnsBySession: Record<string, AgentTurn[]>;
146
- imagesById: Record<string, AgentImageHandle>;
147
- imageIdsBySession: Record<string, string[]>;
148
- selectedSessionId: string | null;
149
- currentImageId: string | null;
150
- allowedTools: readonly AgentToolName[];
151
- manifest: string | null;
152
- queueBySession: Record<string, AgentQueueItem[]>;
153
- runSummaryBySession: Record<string, AgentSessionRunSummary>;
154
- }