spiracha 1.0.0 → 1.1.1

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 (92) hide show
  1. package/AGENTS.md +31 -1
  2. package/README.md +61 -7
  3. package/apps/ui/AGENTS.md +70 -0
  4. package/apps/ui/README.md +72 -0
  5. package/apps/ui/dist/client/assets/_threadId-CAIeH5mq.js +1 -0
  6. package/apps/ui/dist/client/assets/analytics-CqWZmyV6.js +1 -0
  7. package/apps/ui/dist/client/assets/checkbox-DXM4lkJq.js +1 -0
  8. package/apps/ui/dist/client/assets/data-table-DnPYMPCD.js +4 -0
  9. package/apps/ui/dist/client/assets/delete-confirm-dialog-CcZaRX33.js +11 -0
  10. package/apps/ui/dist/client/assets/download-DOwxk-cG.js +1 -0
  11. package/apps/ui/dist/client/assets/es2015-Bm0kEzx2.js +41 -0
  12. package/apps/ui/dist/client/assets/formatters-C12LmYaa.js +1 -0
  13. package/apps/ui/dist/client/assets/index-DdJ7ahIt.js +22 -0
  14. package/apps/ui/dist/client/assets/input-CEsI7EpI.js +1 -0
  15. package/apps/ui/dist/client/assets/metric-card-9jwBF7rG.js +1 -0
  16. package/apps/ui/dist/client/assets/page-header-Dr_h1CVv.js +1 -0
  17. package/apps/ui/dist/client/assets/projects._project-uyNGnpjH.js +1 -0
  18. package/apps/ui/dist/client/assets/projects._project-zoM8d2nH.js +1 -0
  19. package/apps/ui/dist/client/assets/projects.index-D1CWVN-O.js +1 -0
  20. package/apps/ui/dist/client/assets/projects.index-DukMuny6.js +1 -0
  21. package/apps/ui/dist/client/assets/routes-Gr2Wwh83.js +1 -0
  22. package/apps/ui/dist/client/assets/select-CFim44gT.js +1 -0
  23. package/apps/ui/dist/client/assets/settings-DqhyDxo2.js +1 -0
  24. package/apps/ui/dist/client/assets/styles-CMrP9Jb4.css +1 -0
  25. package/apps/ui/dist/client/assets/threads._threadId-DT75NiBa.js +1 -0
  26. package/apps/ui/dist/client/assets/threads._threadId-Df5VXIuZ.js +7 -0
  27. package/apps/ui/dist/client/favicon.ico +0 -0
  28. package/apps/ui/dist/client/logo192.png +0 -0
  29. package/apps/ui/dist/client/logo512.png +0 -0
  30. package/apps/ui/dist/client/manifest.json +25 -0
  31. package/apps/ui/dist/client/robots.txt +3 -0
  32. package/apps/ui/dist/server/assets/__23tanstack-start-plugin-adapters-BzCA6dXo.js +5 -0
  33. package/apps/ui/dist/server/assets/_tanstack-start-manifest_v-C0V305Nt.js +99 -0
  34. package/apps/ui/dist/server/assets/_threadId-B6SrBR9E.js +6 -0
  35. package/apps/ui/dist/server/assets/analytics-BMxW_bZL.js +139 -0
  36. package/apps/ui/dist/server/assets/button-CmTDnzOn.js +46 -0
  37. package/apps/ui/dist/server/assets/checkbox-C0hovF41.js +19 -0
  38. package/apps/ui/dist/server/assets/codex-queries-CAF6HYiG.js +109 -0
  39. package/apps/ui/dist/server/assets/codex-server-BFZq2Y2O.js +2062 -0
  40. package/apps/ui/dist/server/assets/data-table-Cdct823O.js +189 -0
  41. package/apps/ui/dist/server/assets/delete-confirm-dialog-CWqcTXTF.js +139 -0
  42. package/apps/ui/dist/server/assets/download-C5rkk_Bo.js +289 -0
  43. package/apps/ui/dist/server/assets/formatters-FJaGZgJk.js +91 -0
  44. package/apps/ui/dist/server/assets/input-B4tEzctc.js +46 -0
  45. package/apps/ui/dist/server/assets/loading-panel-DbLdvjtR.js +27 -0
  46. package/apps/ui/dist/server/assets/metric-card-ByEeLu0r.js +23 -0
  47. package/apps/ui/dist/server/assets/model-label-B1NWGc65.js +13 -0
  48. package/apps/ui/dist/server/assets/page-header-CxdZM86z.js +25 -0
  49. package/apps/ui/dist/server/assets/path-transforms-DL2IwtYd.js +31 -0
  50. package/apps/ui/dist/server/assets/projects._project-CJ7l0ynC.js +18 -0
  51. package/apps/ui/dist/server/assets/projects._project-CLSohrBp.js +26 -0
  52. package/apps/ui/dist/server/assets/projects._project-CcJLp_A8.js +337 -0
  53. package/apps/ui/dist/server/assets/projects.index-CaplpeMy.js +26 -0
  54. package/apps/ui/dist/server/assets/projects.index-srtogpuF.js +172 -0
  55. package/apps/ui/dist/server/assets/router-C_w-haH6.js +307 -0
  56. package/apps/ui/dist/server/assets/routes-BhbxvJE7.js +34 -0
  57. package/apps/ui/dist/server/assets/routes-CPe-ppmC.js +169 -0
  58. package/apps/ui/dist/server/assets/select-GW76p-ld.js +76 -0
  59. package/apps/ui/dist/server/assets/settings-MvWDgc1u.js +100 -0
  60. package/apps/ui/dist/server/assets/settings-store-DpEJEQ7M.js +52 -0
  61. package/apps/ui/dist/server/assets/sqlite-error-LZDrnxdd.js +13 -0
  62. package/apps/ui/dist/server/assets/start-HeKLHD9b.js +4 -0
  63. package/apps/ui/dist/server/assets/threads._threadId-BSSK4nkI.js +26 -0
  64. package/apps/ui/dist/server/assets/threads._threadId-Ba7vv6-K.js +18 -0
  65. package/apps/ui/dist/server/assets/threads._threadId-euyNckhj.js +1059 -0
  66. package/apps/ui/dist/server/assets/utils-C_uf36nf.js +8 -0
  67. package/apps/ui/dist/server/server.js +5678 -0
  68. package/package.json +53 -7
  69. package/src/export-chats.ts +4 -18
  70. package/src/lib/claude-exporter.ts +1 -1
  71. package/src/lib/codex-analytics.ts +100 -0
  72. package/src/lib/codex-browser-db.ts +605 -0
  73. package/src/lib/codex-browser-export.ts +429 -0
  74. package/src/lib/codex-browser-types.ts +224 -0
  75. package/src/lib/codex-exporter-cli.ts +6 -1
  76. package/src/lib/codex-exporter-db.ts +19 -20
  77. package/src/lib/codex-exporter-transcript.ts +158 -34
  78. package/src/lib/codex-exporter-types.ts +8 -0
  79. package/src/lib/codex-thread-cache.ts +58 -0
  80. package/src/lib/codex-thread-parser.ts +604 -0
  81. package/src/lib/interactive-cli.ts +10 -25
  82. package/src/lib/model-label.ts +24 -0
  83. package/src/lib/native-open.ts +54 -0
  84. package/src/lib/path-transforms.ts +46 -0
  85. package/src/lib/shared.ts +15 -1
  86. package/src/lib/sqlite-error.ts +14 -0
  87. package/src/lib/sqlite-retry.ts +53 -0
  88. package/src/lib/ui-cache.ts +96 -0
  89. package/src/lib/ui-export-files.ts +77 -0
  90. package/src/mcp-server.ts +1 -0
  91. package/src/spiracha.ts +16 -4
  92. package/src/ui-cli.ts +310 -0
@@ -0,0 +1,2062 @@
1
+ import { n as TSS_SERVER_FUNCTION, t as createServerFn } from "../server.js";
2
+ import { t as formatModelLabel$1 } from "./model-label-B1NWGc65.js";
3
+ import { t as isRetryableSqliteError } from "./sqlite-error-LZDrnxdd.js";
4
+ import { t as applyPathTransforms } from "./path-transforms-DL2IwtYd.js";
5
+ import { finished } from "node:stream/promises";
6
+ import { Database } from "bun:sqlite";
7
+ import { mkdir, mkdtemp, readdir, rename, rm, stat } from "node:fs/promises";
8
+ import os from "node:os";
9
+ import path from "node:path";
10
+ import { createReadStream, createWriteStream } from "node:fs";
11
+ import { createInterface } from "node:readline";
12
+ import { createHash, randomUUID } from "node:crypto";
13
+ import { z } from "zod";
14
+ //#region ../../node_modules/.bun/@tanstack+start-server-core@1.169.3/node_modules/@tanstack/start-server-core/dist/esm/createServerRpc.js
15
+ var createServerRpc = (serverFnMeta, splitImportFn) => {
16
+ const url = "/_serverFn/" + serverFnMeta.id;
17
+ return Object.assign(splitImportFn, {
18
+ url,
19
+ serverFnMeta,
20
+ [TSS_SERVER_FUNCTION]: true
21
+ });
22
+ };
23
+ //#endregion
24
+ //#region ../../src/lib/codex-exporter-types.ts
25
+ var DEFAULT_CODEX_DIR = path.join(os.homedir(), ".codex");
26
+ var DEFAULT_DB_PATH = path.join(DEFAULT_CODEX_DIR, "state_5.sqlite");
27
+ path.join(DEFAULT_CODEX_DIR, "sessions");
28
+ path.join(process.cwd(), "exports");
29
+ //#endregion
30
+ //#region ../../src/lib/shared.ts
31
+ var getPortablePathBasename = (value) => {
32
+ const trimmed = value.replace(/[\\/]+$/u, "");
33
+ if (!trimmed) return "";
34
+ return path.win32.basename(path.posix.basename(trimmed));
35
+ };
36
+ var cleanInlineTitle = (value) => {
37
+ const compact = (value.split("\n").map((line) => line.trim()).find((line) => line.length > 0) ?? "").replace(/\s+/g, " ").trim();
38
+ if (compact.length <= 160) return compact;
39
+ return `${compact.slice(0, 157).trimEnd()}...`;
40
+ };
41
+ var cleanExtractedText = (text) => {
42
+ return text.replace(/^\s*<\/?image>\s*$/gm, "").replace(/\n{3,}/g, "\n\n");
43
+ };
44
+ var formatModelLabel = formatModelLabel$1;
45
+ var asObject = (value) => {
46
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
47
+ return value;
48
+ };
49
+ var asString = (value) => {
50
+ return typeof value === "string" ? value : null;
51
+ };
52
+ var asNumber = (value) => {
53
+ return typeof value === "number" ? value : null;
54
+ };
55
+ var readJsonlObjects = (filePath) => {
56
+ const stream = createReadStream(filePath, { encoding: "utf8" });
57
+ const lines = createInterface({
58
+ crlfDelay: Infinity,
59
+ input: stream
60
+ });
61
+ const lineIterator = lines[Symbol.asyncIterator]();
62
+ let closed = false;
63
+ const close = () => {
64
+ if (closed) return;
65
+ closed = true;
66
+ lines.close();
67
+ stream.destroy();
68
+ };
69
+ const readNext = async () => {
70
+ while (true) {
71
+ const nextLine = await lineIterator.next();
72
+ if (nextLine.done) {
73
+ close();
74
+ return {
75
+ done: true,
76
+ value: void 0
77
+ };
78
+ }
79
+ const trimmed = nextLine.value.trim();
80
+ if (!trimmed) continue;
81
+ try {
82
+ return {
83
+ done: false,
84
+ value: JSON.parse(trimmed)
85
+ };
86
+ } catch {}
87
+ }
88
+ };
89
+ const iterator = {
90
+ [Symbol.asyncIterator]: () => iterator,
91
+ next: async () => readNext(),
92
+ return: async () => {
93
+ close();
94
+ return {
95
+ done: true,
96
+ value: void 0
97
+ };
98
+ },
99
+ throw: async (error) => {
100
+ close();
101
+ throw error;
102
+ }
103
+ };
104
+ return iterator;
105
+ };
106
+ var renderDocumentTitle = (title, format) => {
107
+ if (format === "md") return `# ${title}`;
108
+ return [title, "=".repeat(Math.max(title.length, 3))].join("\n");
109
+ };
110
+ var renderMetadataBlock = (entries, format) => {
111
+ const filteredEntries = entries.filter((entry) => entry.value !== null && entry.value !== void 0 && entry.value !== "");
112
+ if (filteredEntries.length === 0) return "";
113
+ if (format === "md") {
114
+ const lines = ["---"];
115
+ for (const entry of filteredEntries) lines.push(`${entry.key}: ${toMetadataValue(entry.value, "md")}`);
116
+ lines.push("---");
117
+ return `${lines.join("\n")}\n`;
118
+ }
119
+ const lines = ["Metadata", "--------"];
120
+ for (const entry of filteredEntries) lines.push(`${entry.key}: ${toMetadataValue(entry.value, "txt")}`);
121
+ return `${lines.join("\n")}\n`;
122
+ };
123
+ var renderSection = (title, body, format) => {
124
+ const trimmedBody = body.trimEnd();
125
+ if (!trimmedBody) return "";
126
+ if (format === "md") return `## ${title}\n\n${trimmedBody}\n`;
127
+ return `${title}\n${"-".repeat(Math.max(title.length, 3))}\n${trimmedBody}\n`;
128
+ };
129
+ var formatInlineLiteral = (value, format) => {
130
+ return format === "md" ? inlineCode(value) : value;
131
+ };
132
+ var inlineCode = (value) => {
133
+ const maxRunLength = (value.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
134
+ const fence = "`".repeat(maxRunLength + 1);
135
+ return `${fence}${value.startsWith("`") || value.endsWith("`") ? ` ${value} ` : value}${fence}`;
136
+ };
137
+ var createExportWriteStream = async (outputPath) => {
138
+ await mkdir(path.dirname(outputPath), { recursive: true });
139
+ return createWriteStream(outputPath, { encoding: "utf8" });
140
+ };
141
+ var finalizeExportWriteStream = async (stream) => {
142
+ stream.end();
143
+ await finished(stream);
144
+ };
145
+ var toMetadataValue = (value, format) => {
146
+ if (Array.isArray(value) || value && typeof value === "object") return JSON.stringify(value);
147
+ if (typeof value === "string") return format === "md" ? JSON.stringify(value) : value;
148
+ if (typeof value === "boolean" || typeof value === "number") return String(value);
149
+ return format === "md" ? JSON.stringify(String(value)) : String(value);
150
+ };
151
+ //#endregion
152
+ //#region ../../src/lib/codex-thread-parser.ts
153
+ var createEmptyStats = () => {
154
+ return {
155
+ assistantMessageCount: 0,
156
+ commentaryCount: 0,
157
+ execCommandCount: 0,
158
+ finalAnswerCount: 0,
159
+ messageCount: 0,
160
+ toolCallCount: 0,
161
+ toolOutputCount: 0,
162
+ userMessageCount: 0,
163
+ webSearchEventCount: 0
164
+ };
165
+ };
166
+ var createEmptySessionMeta = () => {
167
+ return {
168
+ baseInstructions: null,
169
+ cli_version: void 0,
170
+ cwd: void 0,
171
+ dynamicTools: [],
172
+ git: null,
173
+ id: void 0,
174
+ modelProvider: null,
175
+ originator: void 0,
176
+ source: void 0,
177
+ threadSource: null,
178
+ timestamp: void 0
179
+ };
180
+ };
181
+ var parseCodexTranscriptFile = async (sessionFile, options = {}) => {
182
+ const sessionMeta = createEmptySessionMeta();
183
+ const turnContexts = [];
184
+ const events = [];
185
+ const stats = createEmptyStats();
186
+ const includeRaw = options.includeRaw ?? true;
187
+ const maxEvents = options.maxEvents ?? Number.POSITIVE_INFINITY;
188
+ const maxTurnContexts = options.maxTurnContexts ?? Number.POSITIVE_INFINITY;
189
+ let sequence = 0;
190
+ for await (const parsed of readJsonlObjects(sessionFile)) {
191
+ captureSessionMeta$1(parsed, sessionMeta);
192
+ if (asString(parsed.type) === "turn_context") {
193
+ if (turnContexts.length < maxTurnContexts) captureTurnContext(parsed, turnContexts);
194
+ continue;
195
+ }
196
+ const event = toThreadEvent(parsed, sequence, includeRaw);
197
+ if (!event) continue;
198
+ events.push(event);
199
+ updateTranscriptStats(stats, event);
200
+ sequence += 1;
201
+ if (events.length >= maxEvents) break;
202
+ }
203
+ return {
204
+ events,
205
+ isPartial: Number.isFinite(maxEvents) || Number.isFinite(maxTurnContexts),
206
+ rawIncluded: includeRaw,
207
+ sessionMeta,
208
+ sourceFileSizeBytes: options.sourceFileSizeBytes ?? null,
209
+ stats,
210
+ statsArePartial: Number.isFinite(maxEvents),
211
+ turnContexts
212
+ };
213
+ };
214
+ var captureSessionMeta$1 = (parsed, sessionMeta) => {
215
+ if (parsed.type !== "session_meta") return;
216
+ const payload = asObject(parsed.payload);
217
+ if (!payload) return;
218
+ sessionMeta.baseInstructions = payload.base_instructions ?? sessionMeta.baseInstructions;
219
+ sessionMeta.cli_version = asString(payload.cli_version) ?? sessionMeta.cli_version;
220
+ sessionMeta.cwd = asString(payload.cwd) ?? sessionMeta.cwd;
221
+ sessionMeta.dynamicTools = parseDynamicTools(payload.dynamic_tools) ?? sessionMeta.dynamicTools;
222
+ sessionMeta.git = asObject(payload.git) ?? sessionMeta.git;
223
+ sessionMeta.id = asString(payload.id) ?? sessionMeta.id;
224
+ sessionMeta.modelProvider = asString(payload.model_provider) ?? sessionMeta.modelProvider;
225
+ sessionMeta.originator = asString(payload.originator) ?? sessionMeta.originator;
226
+ sessionMeta.source = asString(payload.source) ?? sessionMeta.source;
227
+ sessionMeta.threadSource = asString(payload.thread_source) ?? sessionMeta.threadSource;
228
+ sessionMeta.timestamp = asString(payload.timestamp) ?? sessionMeta.timestamp;
229
+ };
230
+ var parseDynamicTools = (value) => {
231
+ if (!Array.isArray(value)) return null;
232
+ return value.flatMap((entry) => {
233
+ const tool = asObject(entry);
234
+ if (!tool) return [];
235
+ return [{
236
+ deferLoading: tool.deferLoading === true || tool.defer_loading === true,
237
+ description: asString(tool.description) ?? "",
238
+ inputSchema: asObject(tool.inputSchema) ?? asObject(tool.input_schema) ?? null,
239
+ name: asString(tool.name) ?? "unknown",
240
+ namespace: asString(tool.namespace)
241
+ }];
242
+ });
243
+ };
244
+ var captureTurnContext = (parsed, turnContexts) => {
245
+ const payload = asObject(parsed.payload);
246
+ if (!payload) return;
247
+ turnContexts.push({
248
+ payload,
249
+ timestamp: asString(parsed.timestamp)
250
+ });
251
+ };
252
+ var toThreadEvent = (parsed, sequence, includeRaw) => {
253
+ const payload = asObject(parsed.payload);
254
+ if (!payload) return null;
255
+ const payloadType = asString(payload.type);
256
+ const timestamp = asString(parsed.timestamp);
257
+ if (parsed.type === "event_msg") return buildEventMessage(payload, payloadType, includeRaw ? parsed : {}, sequence, timestamp);
258
+ if (parsed.type !== "response_item") return null;
259
+ return buildResponseItemEvent(payload, payloadType, includeRaw ? parsed : {}, sequence, timestamp);
260
+ };
261
+ var buildEventMessage = (payload, payloadType, raw, sequence, timestamp) => {
262
+ if (payloadType === "task_started") return createTaskStartedEvent(payload, raw, sequence, timestamp);
263
+ if (payloadType === "task_complete") return createTaskCompleteEvent(payload, raw, sequence, timestamp);
264
+ return null;
265
+ };
266
+ var buildResponseItemEvent = (payload, payloadType, raw, sequence, timestamp) => {
267
+ if (payloadType === "message") return createMessageEvent(payload, raw, sequence, timestamp);
268
+ if (payloadType === "user_message") return createUserMessageEvent(payload, raw, sequence, timestamp);
269
+ if (payloadType === "agent_message") return createAgentMessageEvent(payload, raw, sequence, timestamp);
270
+ if (payloadType === "function_call") return createToolCallEvent(payload, raw, sequence, timestamp);
271
+ if (payloadType === "function_call_output") return createToolOutputEvent(payload, raw, sequence, timestamp);
272
+ if (payloadType === "reasoning") return createReasoningEvent(payload, raw, sequence, timestamp);
273
+ if (payloadType === "token_count") return createTokenCountEvent(payload, raw, sequence, timestamp);
274
+ if (payloadType === "web_search_call" || payloadType === "web_search_end") return createWebSearchEvent(payload, raw, sequence, timestamp);
275
+ if (payloadType === "task_started") return createTaskStartedEvent(payload, raw, sequence, timestamp);
276
+ if (payloadType === "task_complete") return createTaskCompleteEvent(payload, raw, sequence, timestamp);
277
+ return null;
278
+ };
279
+ var createMessageEvent = (payload, raw, sequence, timestamp) => {
280
+ const role = asString(payload.role);
281
+ const content = payload.content;
282
+ if (!role || content === void 0) return null;
283
+ return {
284
+ isHiddenByDefault: shouldHideTranscriptText(role, extractText$1(content)),
285
+ kind: "message",
286
+ memoryCitation: null,
287
+ model: asString(payload.model),
288
+ phase: asString(payload.phase),
289
+ raw,
290
+ role,
291
+ sequence,
292
+ text: extractText$1(content),
293
+ timestamp,
294
+ variant: "message"
295
+ };
296
+ };
297
+ var createUserMessageEvent = (payload, raw, sequence, timestamp) => {
298
+ return {
299
+ isHiddenByDefault: shouldHideTranscriptText("user", asString(payload.message)?.trim() ?? ""),
300
+ kind: "message",
301
+ memoryCitation: null,
302
+ model: null,
303
+ phase: null,
304
+ raw,
305
+ role: "user",
306
+ sequence,
307
+ text: asString(payload.message)?.trim() ?? "",
308
+ timestamp,
309
+ variant: "user_message"
310
+ };
311
+ };
312
+ var createAgentMessageEvent = (payload, raw, sequence, timestamp) => {
313
+ return {
314
+ isHiddenByDefault: false,
315
+ kind: "message",
316
+ memoryCitation: payload.memory_citation ?? null,
317
+ model: asString(payload.model),
318
+ phase: asString(payload.phase),
319
+ raw,
320
+ role: "assistant",
321
+ sequence,
322
+ text: asString(payload.message)?.trim() ?? "",
323
+ timestamp,
324
+ variant: "agent_message"
325
+ };
326
+ };
327
+ var createToolCallEvent = (payload, raw, sequence, timestamp) => {
328
+ const name = asString(payload.name) ?? "unknown";
329
+ const argumentsText = asString(payload.arguments);
330
+ const parsedArguments = parseExecCommandArguments$1(argumentsText);
331
+ return {
332
+ argumentsParseFailed: parsedArguments.argumentsParseFailed,
333
+ argumentsText,
334
+ callId: asString(payload.call_id),
335
+ command: parsedArguments.cmd,
336
+ kind: "tool_call",
337
+ name,
338
+ raw,
339
+ sequence,
340
+ timestamp,
341
+ workdir: parsedArguments.workdir
342
+ };
343
+ };
344
+ var createToolOutputEvent = (payload, raw, sequence, timestamp) => {
345
+ const outputText = asString(payload.output) ?? "";
346
+ return {
347
+ callId: asString(payload.call_id),
348
+ exitCode: parseExitCode(outputText),
349
+ kind: "tool_output",
350
+ outputText,
351
+ raw,
352
+ sequence,
353
+ summary: formatToolOutputSummary$1(outputText),
354
+ timestamp,
355
+ wallTime: parseWallTime(outputText)
356
+ };
357
+ };
358
+ var createReasoningEvent = (payload, raw, sequence, timestamp) => {
359
+ return {
360
+ content: payload.content ?? null,
361
+ hasEncryptedContent: Boolean(asString(payload.encrypted_content)),
362
+ kind: "reasoning",
363
+ raw,
364
+ sequence,
365
+ summary: toStringArray(payload.summary),
366
+ timestamp
367
+ };
368
+ };
369
+ var createTokenCountEvent = (payload, raw, sequence, timestamp) => {
370
+ return {
371
+ info: payload.info ?? null,
372
+ kind: "token_count",
373
+ rateLimits: payload.rate_limits ?? null,
374
+ raw,
375
+ sequence,
376
+ timestamp
377
+ };
378
+ };
379
+ var createTaskStartedEvent = (payload, raw, sequence, timestamp) => {
380
+ return {
381
+ collaborationModeKind: asString(payload.collaboration_mode_kind),
382
+ kind: "task_started",
383
+ modelContextWindow: asNumber(payload.model_context_window),
384
+ raw,
385
+ sequence,
386
+ startedAt: asNumber(payload.started_at),
387
+ timestamp,
388
+ turnId: asString(payload.turn_id)
389
+ };
390
+ };
391
+ var createTaskCompleteEvent = (payload, raw, sequence, timestamp) => {
392
+ return {
393
+ completedAt: asNumber(payload.completed_at),
394
+ durationMs: asNumber(payload.duration_ms),
395
+ kind: "task_complete",
396
+ lastAgentMessage: asString(payload.last_agent_message),
397
+ raw,
398
+ sequence,
399
+ timestamp,
400
+ timeToFirstTokenMs: asNumber(payload.time_to_first_token_ms),
401
+ turnId: asString(payload.turn_id)
402
+ };
403
+ };
404
+ var createWebSearchEvent = (payload, raw, sequence, timestamp) => {
405
+ const payloadType = asString(payload.type);
406
+ return {
407
+ action: payload.action ?? null,
408
+ callId: asString(payload.call_id),
409
+ kind: "web_search",
410
+ phase: payloadType === "web_search_end" ? "end" : "call",
411
+ query: asString(payload.query),
412
+ raw,
413
+ sequence,
414
+ status: asString(payload.status),
415
+ timestamp
416
+ };
417
+ };
418
+ var updateTranscriptStats = (stats, event) => {
419
+ if (event.kind === "message") {
420
+ stats.messageCount += 1;
421
+ if (event.role === "assistant") stats.assistantMessageCount += 1;
422
+ if (event.role === "user") stats.userMessageCount += 1;
423
+ if (event.phase === "commentary") stats.commentaryCount += 1;
424
+ if (event.phase === "final_answer") stats.finalAnswerCount += 1;
425
+ return;
426
+ }
427
+ if (event.kind === "tool_call") {
428
+ stats.toolCallCount += 1;
429
+ if (event.name === "exec_command") stats.execCommandCount += 1;
430
+ return;
431
+ }
432
+ if (event.kind === "tool_output") {
433
+ stats.toolOutputCount += 1;
434
+ return;
435
+ }
436
+ if (event.kind === "web_search") stats.webSearchEventCount += 1;
437
+ };
438
+ var toStringArray = (value) => {
439
+ if (!Array.isArray(value)) return [];
440
+ return value.map((entry) => asString(entry)).filter((entry) => Boolean(entry));
441
+ };
442
+ var parseExitCode = (outputText) => {
443
+ const match = /Process exited with code (\d+)/u.exec(outputText);
444
+ return match ? Number(match[1]) : null;
445
+ };
446
+ var parseWallTime = (outputText) => {
447
+ return /Wall time: ([^\n]+)/u.exec(outputText)?.[1] ?? null;
448
+ };
449
+ var formatToolOutputSummary$1 = (outputText) => {
450
+ return outputText.split("\n").map((line) => line.trim()).filter(Boolean).filter((line) => {
451
+ return line.startsWith("Command: ") || line.startsWith("Process exited with code ") || line.startsWith("Wall time: ");
452
+ }).join("\n");
453
+ };
454
+ var parseExecCommandArguments$1 = (argumentsText) => {
455
+ if (!argumentsText) return {
456
+ argumentsParseFailed: false,
457
+ cmd: null,
458
+ workdir: null
459
+ };
460
+ try {
461
+ const parsed = JSON.parse(argumentsText);
462
+ return {
463
+ argumentsParseFailed: false,
464
+ cmd: typeof parsed.cmd === "string" ? parsed.cmd : null,
465
+ workdir: typeof parsed.workdir === "string" ? parsed.workdir : null
466
+ };
467
+ } catch {
468
+ return {
469
+ argumentsParseFailed: true,
470
+ cmd: null,
471
+ workdir: null
472
+ };
473
+ }
474
+ };
475
+ var extractText$1 = (content) => {
476
+ if (typeof content === "string") return content.trim();
477
+ if (Array.isArray(content)) return content.map((entry) => extractTextPart(entry)).filter(Boolean).join("\n\n").trim();
478
+ if (content && typeof content === "object") return asString(content.text)?.trim() ?? "";
479
+ return "";
480
+ };
481
+ var extractTextPart = (entry) => {
482
+ const objectValue = asObject(entry);
483
+ if (!objectValue) return "";
484
+ const type = asString(objectValue.type);
485
+ const text = asString(objectValue.text);
486
+ if (type === "input_image") return "[Image attached]";
487
+ return text ?? "";
488
+ };
489
+ var shouldHideTranscriptText = (role, text) => {
490
+ if (!text) return true;
491
+ if (role === "developer") return true;
492
+ return text.startsWith("# AGENTS.md instructions for ") || text.startsWith("<permissions instructions>") || text.startsWith("<app-context>") || text.startsWith("<environment_context>") || text.startsWith("<collaboration_mode>") || text.startsWith("<skills_instructions>") || text.startsWith("<plugins_instructions>") || text.includes("Filesystem sandboxing defines which files can be read or written.");
493
+ };
494
+ //#endregion
495
+ //#region ../../src/lib/ui-cache.ts
496
+ var CACHE_DIR = path.join(os.tmpdir(), "spiracha-ui-cache");
497
+ var CACHE_ENVELOPE_VERSION = 1;
498
+ var ensureCacheDir = async () => {
499
+ await mkdir(CACHE_DIR, { recursive: true });
500
+ };
501
+ var toCachePath = (key) => {
502
+ const safeKey = key.replace(/[^a-zA-Z0-9._-]/gu, "_");
503
+ return path.join(CACHE_DIR, `${safeKey}-${hashCacheKeyParts(key)}.json`);
504
+ };
505
+ var hashCacheKeyParts = (...parts) => {
506
+ return createHash("sha1").update(parts.join("|")).digest("hex");
507
+ };
508
+ var getFileFingerprint = async (filePath) => {
509
+ const metadata = await stat(filePath);
510
+ return `${filePath}:${metadata.size}:${metadata.mtimeMs}`;
511
+ };
512
+ var getCachedJson = async (key) => {
513
+ await ensureCacheDir();
514
+ const filePath = toCachePath(key);
515
+ const file = Bun.file(filePath);
516
+ if (!await file.exists()) return null;
517
+ let parsed;
518
+ try {
519
+ parsed = await file.json();
520
+ } catch {
521
+ await rm(filePath, { force: true });
522
+ return null;
523
+ }
524
+ if (parsed && typeof parsed === "object" && "version" in parsed && parsed.version === CACHE_ENVELOPE_VERSION && "value" in parsed) return parsed.value;
525
+ return parsed;
526
+ };
527
+ var setCachedJson = async (key, value) => {
528
+ await ensureCacheDir();
529
+ const filePath = toCachePath(key);
530
+ const tempPath = `${filePath}.${randomUUID()}.tmp`;
531
+ const envelope = {
532
+ value,
533
+ version: CACHE_ENVELOPE_VERSION
534
+ };
535
+ await Bun.write(tempPath, JSON.stringify(envelope));
536
+ await rename(tempPath, filePath);
537
+ };
538
+ var withCachedJson = async (key, loader) => {
539
+ const filePath = toCachePath(key);
540
+ const existedBeforeRead = await Bun.file(filePath).exists();
541
+ const cached = await getCachedJson(key);
542
+ if (cached !== null || existedBeforeRead && await Bun.file(filePath).exists()) return cached;
543
+ const value = await loader();
544
+ await setCachedJson(key, value);
545
+ return value;
546
+ };
547
+ var invalidateCacheByPrefix = async (...prefixes) => {
548
+ await ensureCacheDir();
549
+ const entries = await readdir(CACHE_DIR);
550
+ await Promise.all(entries.filter((entry) => prefixes.some((prefix) => entry.startsWith(prefix))).map((entry) => rm(path.join(CACHE_DIR, entry), { force: true })));
551
+ };
552
+ //#endregion
553
+ //#region ../../src/lib/codex-thread-cache.ts
554
+ var LARGE_THREAD_SIZE_BYTES = 100 * 1024 * 1024;
555
+ var getCachedParsedCodexTranscript = async (sessionFile) => {
556
+ const fingerprint = await getFileFingerprint(sessionFile);
557
+ return withCachedJson(`thread-${hashCacheKeyParts(path.basename(sessionFile), fingerprint)}`, async () => parseCodexTranscriptFile(sessionFile));
558
+ };
559
+ var getThreadRolloutLoadState = async (sessionFile, largeTranscriptThresholdBytes = LARGE_THREAD_SIZE_BYTES) => {
560
+ const metadata = await stat(sessionFile);
561
+ return {
562
+ fileSizeBytes: metadata.size,
563
+ shouldDeferTranscriptLoad: metadata.size > largeTranscriptThresholdBytes
564
+ };
565
+ };
566
+ var getCachedThreadTranscriptPreview = async (sessionFile, options = {}) => {
567
+ const threshold = options.largeTranscriptThresholdBytes ?? 104857600;
568
+ const previewEventLimit = options.previewEventLimit ?? 200;
569
+ const fingerprint = await getFileFingerprint(sessionFile);
570
+ const { fileSizeBytes, shouldDeferTranscriptLoad } = await getThreadRolloutLoadState(sessionFile, threshold);
571
+ return withCachedJson(`thread-preview-${hashCacheKeyParts(path.basename(sessionFile), fingerprint, String(threshold), String(previewEventLimit))}`, async () => {
572
+ if (!shouldDeferTranscriptLoad) return parseCodexTranscriptFile(sessionFile, { sourceFileSizeBytes: fileSizeBytes });
573
+ return parseCodexTranscriptFile(sessionFile, {
574
+ includeRaw: false,
575
+ maxEvents: previewEventLimit,
576
+ maxTurnContexts: 0,
577
+ sourceFileSizeBytes: fileSizeBytes
578
+ });
579
+ });
580
+ };
581
+ //#endregion
582
+ //#region ../../src/lib/sqlite-retry.ts
583
+ var DEFAULT_RETRY_DELAYS_MS = [
584
+ 40,
585
+ 120,
586
+ 250
587
+ ];
588
+ var SLEEP_BUFFER = new Int32Array(new SharedArrayBuffer(4));
589
+ var sleepSync = (delayMs) => {
590
+ if (delayMs <= 0) return;
591
+ Atomics.wait(SLEEP_BUFFER, 0, 0, delayMs);
592
+ };
593
+ var toRetryExhaustedError = (attemptCount, error) => {
594
+ const message = error instanceof Error ? error.message : String(error);
595
+ return new Error(`SQLite operation failed after ${attemptCount} attempts: ${message}`, { cause: error });
596
+ };
597
+ var shouldRetrySqliteError = (error, attempt, delaysMs) => {
598
+ return isRetryableSqliteError(error) && attempt < delaysMs.length;
599
+ };
600
+ var runWithSqliteRetry = ({ action, delaysMs = DEFAULT_RETRY_DELAYS_MS, sleep = sleepSync }) => {
601
+ let attempt = 0;
602
+ while (true) try {
603
+ return action();
604
+ } catch (error) {
605
+ if (!shouldRetrySqliteError(error, attempt, delaysMs)) {
606
+ if (isRetryableSqliteError(error)) throw toRetryExhaustedError(attempt + 1, error);
607
+ throw error;
608
+ }
609
+ sleep(delaysMs[attempt] ?? 0);
610
+ attempt += 1;
611
+ }
612
+ };
613
+ //#endregion
614
+ //#region ../../src/lib/codex-browser-db.ts
615
+ var SQLITE_DELETE_BATCH_SIZE = 400;
616
+ var SESSION_FILE_DELETE_CONCURRENCY = 16;
617
+ var THREAD_LIST_IO_CONCURRENCY = 8;
618
+ var chunkValues = (values, chunkSize) => {
619
+ const chunks = [];
620
+ for (let index = 0; index < values.length; index += chunkSize) chunks.push(values.slice(index, index + chunkSize));
621
+ return chunks;
622
+ };
623
+ var isPromiseLike = (value) => {
624
+ if (typeof value !== "object" && typeof value !== "function" || value === null) return false;
625
+ return "then" in value && typeof value.then === "function";
626
+ };
627
+ var mapWithConcurrency = async (values, limit, mapper) => {
628
+ const results = new Array(values.length);
629
+ let nextIndex = 0;
630
+ const worker = async () => {
631
+ while (true) {
632
+ const currentIndex = nextIndex;
633
+ nextIndex += 1;
634
+ if (currentIndex >= values.length) return;
635
+ results[currentIndex] = await mapper(values[currentIndex], currentIndex);
636
+ }
637
+ };
638
+ await Promise.all(Array.from({ length: Math.min(limit, values.length) }, () => worker()));
639
+ return results;
640
+ };
641
+ var openReadonlyDb = (dbPath, busyTimeoutMs) => {
642
+ const db = new Database(dbPath, { readonly: true });
643
+ try {
644
+ db.exec(`PRAGMA busy_timeout = ${busyTimeoutMs}`);
645
+ return db;
646
+ } catch (error) {
647
+ db.close();
648
+ throw error;
649
+ }
650
+ };
651
+ var openWritableDb = (dbPath, busyTimeoutMs) => {
652
+ const db = new Database(dbPath);
653
+ try {
654
+ db.exec(`PRAGMA busy_timeout = ${busyTimeoutMs}`);
655
+ return db;
656
+ } catch (error) {
657
+ db.close();
658
+ throw error;
659
+ }
660
+ };
661
+ var toTimestampMs = (thread) => {
662
+ return thread.updated_at_ms ?? thread.updated_at * 1e3;
663
+ };
664
+ var parseDynamicToolRow = (row) => {
665
+ return {
666
+ deferLoading: Number(row.defer_loading ?? 0) === 1,
667
+ description: String(row.description ?? ""),
668
+ inputSchema: parseJsonSafely$1(typeof row.input_schema === "string" ? row.input_schema : null),
669
+ name: String(row.name ?? "unknown"),
670
+ namespace: typeof row.namespace === "string" ? row.namespace : null,
671
+ position: Number(row.position ?? 0),
672
+ threadId: String(row.thread_id)
673
+ };
674
+ };
675
+ var parseJsonSafely$1 = (value) => {
676
+ if (!value) return null;
677
+ try {
678
+ return JSON.parse(value);
679
+ } catch {
680
+ return null;
681
+ }
682
+ };
683
+ var withReadonlyDb = (dbPath, callback) => {
684
+ return runWithSqliteRetry({ action: () => {
685
+ const db = openReadonlyDb(dbPath, 5e3);
686
+ try {
687
+ const result = callback(db);
688
+ if (isPromiseLike(result)) throw new Error("Database callbacks must be synchronous");
689
+ return result;
690
+ } finally {
691
+ db.close();
692
+ }
693
+ } });
694
+ };
695
+ var withWritableDb = (dbPath, callback) => {
696
+ const db = runWithSqliteRetry({ action: () => {
697
+ return openWritableDb(dbPath, 5e3);
698
+ } });
699
+ try {
700
+ const result = callback(db);
701
+ if (isPromiseLike(result)) throw new Error("Database callbacks must be synchronous");
702
+ return result;
703
+ } finally {
704
+ db.close();
705
+ }
706
+ };
707
+ var resolveCodexThreadDbPath = () => {
708
+ const configuredDbPath = process.env.SPIRACHA_CODEX_DB?.trim();
709
+ if (configuredDbPath) return configuredDbPath;
710
+ const candidates = [
711
+ DEFAULT_DB_PATH,
712
+ path.join(DEFAULT_CODEX_DIR, "sqlite", "state_5.sqlite"),
713
+ path.join(os.homedir(), ".codex", "state_5.sqlite")
714
+ ];
715
+ for (const candidate of candidates) try {
716
+ runWithSqliteRetry({ action: () => {
717
+ return openReadonlyDb(candidate, 1500);
718
+ } }).close();
719
+ return candidate;
720
+ } catch {}
721
+ throw new Error(`Unable to open Codex thread database. Tried: ${candidates.join(", ")}`);
722
+ };
723
+ var readAllThreads = (dbPath) => {
724
+ return withReadonlyDb(dbPath, (db) => {
725
+ return db.query("SELECT * FROM threads ORDER BY COALESCE(updated_at_ms, updated_at * 1000) DESC, id DESC").all();
726
+ });
727
+ };
728
+ var filterThreadsByProject = (threads, projectName) => {
729
+ if (!projectName) return threads;
730
+ return threads.filter((thread) => getPortablePathBasename(thread.cwd) === projectName);
731
+ };
732
+ var buildProjectSummaryMap = (threads) => {
733
+ const projectMap = /* @__PURE__ */ new Map();
734
+ for (const thread of threads) {
735
+ const projectName = getPortablePathBasename(thread.cwd);
736
+ if (!projectName) continue;
737
+ const current = projectMap.get(projectName) ?? {
738
+ archivedThreadCount: 0,
739
+ cwdPaths: /* @__PURE__ */ new Set(),
740
+ lastUpdatedAtMs: null,
741
+ modelNames: /* @__PURE__ */ new Set(),
742
+ name: projectName,
743
+ threadCount: 0,
744
+ totalTokens: 0
745
+ };
746
+ current.archivedThreadCount += thread.archived ? 1 : 0;
747
+ current.cwdPaths.add(thread.cwd);
748
+ current.lastUpdatedAtMs = Math.max(current.lastUpdatedAtMs ?? 0, toTimestampMs(thread));
749
+ if (thread.model) current.modelNames.add(thread.model);
750
+ current.threadCount += 1;
751
+ current.totalTokens += thread.tokens_used;
752
+ projectMap.set(projectName, current);
753
+ }
754
+ return projectMap;
755
+ };
756
+ var mapProjectSummaries = (projectMap) => {
757
+ return [...projectMap.values()].map((project) => {
758
+ return {
759
+ archivedThreadCount: project.archivedThreadCount,
760
+ cwdPaths: [...project.cwdPaths].sort(),
761
+ lastUpdatedAtMs: project.lastUpdatedAtMs,
762
+ modelNames: [...project.modelNames].sort(),
763
+ name: project.name,
764
+ threadCount: project.threadCount,
765
+ totalTokens: project.totalTokens
766
+ };
767
+ }).sort((left, right) => {
768
+ if (left.totalTokens !== right.totalTokens) return right.totalTokens - left.totalTokens;
769
+ return left.name.localeCompare(right.name);
770
+ });
771
+ };
772
+ var getRelationsForThread = (db, threadId, existingTableNames) => {
773
+ if (!existingTableNames.has("thread_spawn_edges")) return {
774
+ childEdges: [],
775
+ parentThreadId: null
776
+ };
777
+ const parentRow = db.query("SELECT parent_thread_id, child_thread_id, status FROM thread_spawn_edges WHERE child_thread_id = ? LIMIT 1").get(threadId);
778
+ return {
779
+ childEdges: db.query("SELECT parent_thread_id, child_thread_id, status FROM thread_spawn_edges WHERE parent_thread_id = ? ORDER BY child_thread_id ASC").all(threadId),
780
+ parentThreadId: parentRow?.parent_thread_id ?? null
781
+ };
782
+ };
783
+ var getExistingTableNames = (db) => {
784
+ const rows = db.query("SELECT name FROM sqlite_master WHERE type = ?").all("table");
785
+ return new Set(rows.map((row) => row.name));
786
+ };
787
+ var getThreadDeleteTargets = (db, threadIds) => {
788
+ if (threadIds.length === 0) return [];
789
+ const targets = [];
790
+ for (const threadIdChunk of chunkValues(threadIds, SQLITE_DELETE_BATCH_SIZE)) {
791
+ const placeholders = threadIdChunk.map(() => "?").join(", ");
792
+ targets.push(...db.query(`SELECT id, rollout_path FROM threads WHERE id IN (${placeholders})`).all(...threadIdChunk));
793
+ }
794
+ return targets;
795
+ };
796
+ var deleteThreadIds = (db, threadIds) => {
797
+ if (threadIds.length === 0) return {
798
+ deletedSessionFiles: [],
799
+ deletedThreadIds: []
800
+ };
801
+ const existingTableNames = getExistingTableNames(db);
802
+ const threadTargets = getThreadDeleteTargets(db, threadIds);
803
+ const existingIds = threadTargets.map((target) => target.id);
804
+ if (existingIds.length === 0) return {
805
+ deletedSessionFiles: [],
806
+ deletedThreadIds: []
807
+ };
808
+ db.transaction((ids) => {
809
+ for (const threadIdChunk of chunkValues(ids, SQLITE_DELETE_BATCH_SIZE)) {
810
+ const placeholders = threadIdChunk.map(() => "?").join(", ");
811
+ if (existingTableNames.has("thread_dynamic_tools")) db.query(`DELETE FROM thread_dynamic_tools WHERE thread_id IN (${placeholders})`).run(...threadIdChunk);
812
+ if (existingTableNames.has("thread_goals")) db.query(`DELETE FROM thread_goals WHERE thread_id IN (${placeholders})`).run(...threadIdChunk);
813
+ if (existingTableNames.has("stage1_outputs")) db.query(`DELETE FROM stage1_outputs WHERE thread_id IN (${placeholders})`).run(...threadIdChunk);
814
+ if (existingTableNames.has("thread_spawn_edges")) db.query(`DELETE FROM thread_spawn_edges WHERE parent_thread_id IN (${placeholders}) OR child_thread_id IN (${placeholders})`).run(...threadIdChunk, ...threadIdChunk);
815
+ db.query(`DELETE FROM threads WHERE id IN (${placeholders})`).run(...threadIdChunk);
816
+ }
817
+ })(existingIds);
818
+ return {
819
+ deletedSessionFiles: threadTargets.map((target) => target.rollout_path),
820
+ deletedThreadIds: existingIds
821
+ };
822
+ };
823
+ var deleteThreadSessionFiles = async (sessionFiles) => {
824
+ const uniqueSessionFiles = [...new Set(sessionFiles)];
825
+ await mapWithConcurrency(uniqueSessionFiles, SESSION_FILE_DELETE_CONCURRENCY, async (sessionFile) => {
826
+ await rm(sessionFile, { force: true });
827
+ return sessionFile;
828
+ });
829
+ return uniqueSessionFiles;
830
+ };
831
+ var listCodexProjects = (dbPath) => {
832
+ return mapProjectSummaries(buildProjectSummaryMap(readAllThreads(dbPath)));
833
+ };
834
+ var compactThreadListRow = (thread) => {
835
+ return {
836
+ ...thread,
837
+ preview: cleanInlineTitle(thread.preview || thread.first_user_message || ""),
838
+ title: cleanInlineTitle(thread.title)
839
+ };
840
+ };
841
+ var listProjectThreads = async (dbPath, projectName, options = {}) => {
842
+ return (await mapWithConcurrency(filterThreadsByProject(readAllThreads(dbPath), projectName), THREAD_LIST_IO_CONCURRENCY, async (thread) => {
843
+ const rollout = await getThreadRolloutLoadState(thread.rollout_path, options.largeTranscriptThresholdBytes);
844
+ if (rollout.shouldDeferTranscriptLoad) return {
845
+ project: projectName,
846
+ rolloutSizeBytes: rollout.fileSizeBytes,
847
+ stats: {
848
+ deferred: true,
849
+ execCommandCount: 0,
850
+ toolCallCount: 0,
851
+ webSearchEventCount: 0
852
+ },
853
+ thread: compactThreadListRow(thread)
854
+ };
855
+ const transcript = await getCachedParsedCodexTranscript(thread.rollout_path);
856
+ return {
857
+ project: projectName,
858
+ rolloutSizeBytes: rollout.fileSizeBytes,
859
+ stats: {
860
+ deferred: false,
861
+ execCommandCount: transcript.stats.execCommandCount,
862
+ toolCallCount: transcript.stats.toolCallCount,
863
+ webSearchEventCount: transcript.stats.webSearchEventCount
864
+ },
865
+ thread: compactThreadListRow(thread)
866
+ };
867
+ })).sort((left, right) => toTimestampMs(right.thread) - toTimestampMs(left.thread));
868
+ };
869
+ var getThreadBrowseData = (dbPath, threadId) => {
870
+ return withReadonlyDb(dbPath, (db) => {
871
+ const existingTableNames = getExistingTableNames(db);
872
+ const thread = db.query("SELECT * FROM threads WHERE id = ? LIMIT 1").get(threadId);
873
+ if (!thread) throw new Error(`Thread not found: ${threadId}`);
874
+ return {
875
+ dynamicTools: (existingTableNames.has("thread_dynamic_tools") ? db.query("SELECT thread_id, position, name, description, input_schema, defer_loading, namespace FROM thread_dynamic_tools WHERE thread_id = ? ORDER BY position ASC").all(threadId) : []).map((row) => parseDynamicToolRow(row)),
876
+ project: getPortablePathBasename(thread.cwd),
877
+ relations: getRelationsForThread(db, threadId, existingTableNames),
878
+ thread
879
+ };
880
+ });
881
+ };
882
+ var getCodexDashboardSummary = (dbPath) => {
883
+ const threads = readAllThreads(dbPath);
884
+ const projects = mapProjectSummaries(buildProjectSummaryMap(threads));
885
+ const threadsWithRelations = withReadonlyDb(dbPath, (db) => {
886
+ if (!getExistingTableNames(db).has("thread_spawn_edges")) return 0;
887
+ const rows = db.query("SELECT parent_thread_id, child_thread_id FROM thread_spawn_edges").all();
888
+ return new Set(rows.flatMap((row) => [row.parent_thread_id, row.child_thread_id])).size;
889
+ });
890
+ return {
891
+ activeThreads: threads.filter((thread) => !thread.archived).length,
892
+ archivedThreads: threads.filter((thread) => Boolean(thread.archived)).length,
893
+ recentThreads: threads.slice(0, 5),
894
+ threadsWithRelations,
895
+ topProjectsByThreadCount: [...projects].sort((left, right) => {
896
+ if (left.threadCount !== right.threadCount) return right.threadCount - left.threadCount;
897
+ return left.name.localeCompare(right.name);
898
+ }).slice(0, 5),
899
+ topProjectsByTokens: projects.slice(0, 5),
900
+ totalProjects: projects.length,
901
+ totalThreads: threads.length,
902
+ totalTokens: threads.reduce((sum, thread) => sum + thread.tokens_used, 0)
903
+ };
904
+ };
905
+ var deleteCodexThread = async (dbPath, threadId, options = {}) => {
906
+ const result = withWritableDb(dbPath, (db) => {
907
+ return deleteThreadIds(db, [threadId]);
908
+ });
909
+ try {
910
+ if (options.deleteSessionFiles) return {
911
+ ...result,
912
+ deletedSessionFiles: await deleteThreadSessionFiles(result.deletedSessionFiles)
913
+ };
914
+ return {
915
+ ...result,
916
+ deletedSessionFiles: []
917
+ };
918
+ } finally {
919
+ await invalidateCodexUiCaches();
920
+ }
921
+ };
922
+ var deleteCodexThreads = async (dbPath, threadIds, options = {}) => {
923
+ const result = withWritableDb(dbPath, (db) => {
924
+ return deleteThreadIds(db, threadIds);
925
+ });
926
+ try {
927
+ if (options.deleteSessionFiles) return {
928
+ ...result,
929
+ deletedSessionFiles: await deleteThreadSessionFiles(result.deletedSessionFiles)
930
+ };
931
+ return {
932
+ ...result,
933
+ deletedSessionFiles: []
934
+ };
935
+ } finally {
936
+ await invalidateCodexUiCaches();
937
+ }
938
+ };
939
+ var deleteCodexProject = async (dbPath, projectName, options = {}) => {
940
+ const result = withWritableDb(dbPath, (db) => {
941
+ return {
942
+ ...deleteThreadIds(db, db.query("SELECT id, cwd FROM threads").all().filter((thread) => getPortablePathBasename(thread.cwd) === projectName).map((thread) => thread.id)),
943
+ projectName
944
+ };
945
+ });
946
+ try {
947
+ if (options.deleteSessionFiles) return {
948
+ ...result,
949
+ deletedSessionFiles: await deleteThreadSessionFiles(result.deletedSessionFiles)
950
+ };
951
+ return {
952
+ ...result,
953
+ deletedSessionFiles: []
954
+ };
955
+ } finally {
956
+ await invalidateCodexUiCaches();
957
+ }
958
+ };
959
+ var listScopedThreads = (dbPath, projectName) => {
960
+ return filterThreadsByProject(readAllThreads(dbPath), projectName);
961
+ };
962
+ var invalidateCodexUiCaches = async () => {
963
+ await invalidateCacheByPrefix("analytics-", "thread-");
964
+ };
965
+ //#endregion
966
+ //#region ../../src/lib/codex-analytics.ts
967
+ var roundToTwoDecimals = (value) => {
968
+ return Number(value.toFixed(2));
969
+ };
970
+ var incrementCount = (counts, key) => {
971
+ counts.set(key, (counts.get(key) ?? 0) + 1);
972
+ };
973
+ var toDistribution = (counts) => {
974
+ return [...counts.entries()].map(([label, count]) => ({
975
+ count,
976
+ label
977
+ })).sort((left, right) => {
978
+ if (left.count !== right.count) return right.count - left.count;
979
+ return left.label.localeCompare(right.label);
980
+ });
981
+ };
982
+ var buildModelsByTokens = (threads) => {
983
+ const models = /* @__PURE__ */ new Map();
984
+ for (const thread of threads) {
985
+ const model = thread.model ?? "unknown";
986
+ const current = models.get(model) ?? {
987
+ threadCount: 0,
988
+ totalTokens: 0
989
+ };
990
+ current.threadCount += 1;
991
+ current.totalTokens += thread.tokens_used;
992
+ models.set(model, current);
993
+ }
994
+ return [...models.entries()].map(([model, value]) => ({
995
+ model,
996
+ ...value
997
+ })).sort((left, right) => {
998
+ if (left.totalTokens !== right.totalTokens) return right.totalTokens - left.totalTokens;
999
+ return left.model.localeCompare(right.model);
1000
+ });
1001
+ };
1002
+ var buildAnalyticsCacheKey = async (dbPath, threads, project) => {
1003
+ const dbFingerprint = await getFileFingerprint(dbPath);
1004
+ const rolloutFingerprints = await Promise.all(threads.map((thread) => getFileFingerprint(thread.rollout_path)));
1005
+ return `analytics-${hashCacheKeyParts(dbFingerprint, project ?? "all", ...rolloutFingerprints)}`;
1006
+ };
1007
+ var computeCodexAnalytics = async (threads) => {
1008
+ const totalTokens = threads.reduce((sum, thread) => sum + thread.tokens_used, 0);
1009
+ const projectNames = new Set(threads.map((thread) => getPortablePathBasename(thread.cwd)).filter(Boolean));
1010
+ const toolUsage = /* @__PURE__ */ new Map();
1011
+ const transcripts = await Promise.all(threads.map((thread) => getCachedParsedCodexTranscript(thread.rollout_path)));
1012
+ let threadsWithWebSearch = 0;
1013
+ for (const transcript of transcripts) {
1014
+ if (transcript.stats.webSearchEventCount > 0) threadsWithWebSearch += 1;
1015
+ for (const event of transcript.events) if (event.kind === "tool_call") incrementCount(toolUsage, event.name);
1016
+ }
1017
+ return {
1018
+ modelsByTokens: buildModelsByTokens(threads),
1019
+ summary: {
1020
+ archivedThreads: threads.filter((thread) => Boolean(thread.archived)).length,
1021
+ averageTokensPerThread: threads.length === 0 ? 0 : roundToTwoDecimals(totalTokens / threads.length),
1022
+ distinctToolNames: toolUsage.size,
1023
+ threadsWithWebSearch,
1024
+ totalProjects: projectNames.size,
1025
+ totalThreads: threads.length,
1026
+ totalTokens
1027
+ },
1028
+ toolUsage: toDistribution(toolUsage).map((item) => ({
1029
+ count: item.count,
1030
+ name: item.label
1031
+ }))
1032
+ };
1033
+ };
1034
+ var getCodexAnalytics = async (input) => {
1035
+ const threads = listScopedThreads(input.dbPath, input.project);
1036
+ return withCachedJson(await buildAnalyticsCacheKey(input.dbPath, threads, input.project), async () => computeCodexAnalytics(threads));
1037
+ };
1038
+ //#endregion
1039
+ //#region ../../src/lib/codex-exporter-db.ts
1040
+ var matchesFilters = (value, options) => {
1041
+ return matchesCwdFilter(value, options.cwdFilter) && matchesProjectFilter(value, options.projectFilter);
1042
+ };
1043
+ var toCodexRelativePath = (targetPath) => {
1044
+ const codexRoot = path.resolve(DEFAULT_CODEX_DIR);
1045
+ const normalized = path.resolve(targetPath);
1046
+ if (normalized.startsWith(`${codexRoot}${path.sep}`)) return path.relative(codexRoot, normalized);
1047
+ return normalized;
1048
+ };
1049
+ var matchesCwdFilter = (value, cwdFilter) => {
1050
+ if (!cwdFilter) return true;
1051
+ return value === cwdFilter;
1052
+ };
1053
+ var matchesProjectFilter = (value, projectFilter) => {
1054
+ if (!projectFilter) return true;
1055
+ if (!value) return false;
1056
+ return getPortablePathBasename(value) === projectFilter;
1057
+ };
1058
+ //#endregion
1059
+ //#region ../../src/lib/codex-exporter-transcript.ts
1060
+ var convertSessionFile = async (target, options) => {
1061
+ let transcriptState;
1062
+ try {
1063
+ transcriptState = await collectCodexTranscript(target.sessionFile, options, target.thread?.model ?? null);
1064
+ } catch (error) {
1065
+ const message = error instanceof Error ? error.message : String(error);
1066
+ throw new Error(`Failed to read Codex transcript ${target.sessionFile}: ${message}`);
1067
+ }
1068
+ if (!matchesFilters(target.thread?.cwd ?? transcriptState.sessionMeta.cwd ?? null, options)) return null;
1069
+ if (transcriptState.sections.length === 0) return null;
1070
+ if (options.optimized) return transcriptState.sections.join("\n\n").trimEnd() + "\n";
1071
+ const title = getTitle(target, transcriptState.sessionMeta);
1072
+ const metadata = buildMetadataEntries(target, transcriptState.sessionMeta, options);
1073
+ return [
1074
+ renderDocumentTitle(title, options.outputFormat),
1075
+ "",
1076
+ renderMetadataBlock(metadata, options.outputFormat),
1077
+ ...transcriptState.sections
1078
+ ].filter(Boolean).join("\n").trimEnd() + "\n";
1079
+ };
1080
+ var writeSessionFileExport = async (target, options, outputPath, transform = (text) => text) => {
1081
+ const transcriptOutputPath = `${outputPath}.transcript.tmp`;
1082
+ let transcriptStream = null;
1083
+ const state = {
1084
+ assistantModel: target.thread?.model ?? null,
1085
+ sections: [],
1086
+ sessionMeta: {},
1087
+ startedTranscript: false
1088
+ };
1089
+ let wroteSection = false;
1090
+ try {
1091
+ transcriptStream = await createExportWriteStream(transcriptOutputPath);
1092
+ for await (const parsed of readJsonlObjects(target.sessionFile)) {
1093
+ captureSessionMeta(parsed, state.sessionMeta);
1094
+ const block = renderCodexTranscriptRecord(parsed, options, state);
1095
+ if (!block) continue;
1096
+ transcriptStream.write(transform(wroteSection ? `${getSectionSeparator(options)}${block}` : block));
1097
+ wroteSection = true;
1098
+ }
1099
+ await finalizeExportWriteStream(transcriptStream);
1100
+ transcriptStream = null;
1101
+ if (!matchesFilters(target.thread?.cwd ?? state.sessionMeta.cwd ?? null, options) || !wroteSection) return false;
1102
+ const outputStream = await createExportWriteStream(outputPath);
1103
+ try {
1104
+ const prefix = buildStreamExportPrefix(target, state.sessionMeta, options);
1105
+ if (prefix) outputStream.write(transform(prefix));
1106
+ const transcriptReadStream = createReadStream(transcriptOutputPath, { encoding: "utf8" });
1107
+ transcriptReadStream.pipe(outputStream, { end: false });
1108
+ await finished(transcriptReadStream);
1109
+ outputStream.write("\n");
1110
+ await finalizeExportWriteStream(outputStream);
1111
+ } catch (error) {
1112
+ outputStream.destroy();
1113
+ throw error;
1114
+ }
1115
+ return true;
1116
+ } catch (error) {
1117
+ if (transcriptStream) transcriptStream.destroy();
1118
+ throw error;
1119
+ } finally {
1120
+ await rm(transcriptOutputPath, { force: true });
1121
+ }
1122
+ };
1123
+ var collectCodexTranscript = async (sessionFile, options, assistantModel = null) => {
1124
+ const state = {
1125
+ assistantModel,
1126
+ sections: [],
1127
+ sessionMeta: {},
1128
+ startedTranscript: false
1129
+ };
1130
+ for await (const parsed of readJsonlObjects(sessionFile)) processCodexTranscriptRecord(parsed, options, state);
1131
+ return state;
1132
+ };
1133
+ var getSectionSeparator = (options) => {
1134
+ return options.optimized ? "\n\n" : "\n";
1135
+ };
1136
+ var processCodexTranscriptRecord = (parsed, options, state) => {
1137
+ captureSessionMeta(parsed, state.sessionMeta);
1138
+ const block = renderCodexTranscriptRecord(parsed, options, state);
1139
+ if (block) state.sections.push(block);
1140
+ };
1141
+ var renderCodexTranscriptRecord = (parsed, options, state) => {
1142
+ const message = extractMessageRecord(parsed);
1143
+ if (message) return processCodexMessageRecord(message, options, state);
1144
+ if (!options.includeTools) return "";
1145
+ const tool = extractToolRecord(parsed);
1146
+ if (!tool) return "";
1147
+ return options.optimized ? renderCompactToolBlock(tool, options.outputFormat) : renderToolBlock(tool, options.outputFormat);
1148
+ };
1149
+ var processCodexMessageRecord = (message, options, state) => {
1150
+ if (options.optimized) return processOptimizedCodexMessageRecord(message, options, state);
1151
+ return renderMessageBlock(message, options.outputFormat, state.assistantModel, options.includeCommentary);
1152
+ };
1153
+ var processOptimizedCodexMessageRecord = (message, options, state) => {
1154
+ if (message.role !== "user" && message.role !== "assistant") return "";
1155
+ if (message.role === "assistant" && message.phase === "commentary" && !options.includeCommentary) return "";
1156
+ const compact = compactMessageText(message, true);
1157
+ if (!compact) return "";
1158
+ if (!state.startedTranscript) {
1159
+ if (shouldSkipOptimizedPrelude(message.role, compact)) return "";
1160
+ state.startedTranscript = true;
1161
+ }
1162
+ return renderCompactBlock(message, compact, options.outputFormat, state.assistantModel);
1163
+ };
1164
+ var buildStreamExportPrefix = (target, sessionMeta, options) => {
1165
+ if (options.optimized) return "";
1166
+ const title = getTitle(target, sessionMeta);
1167
+ const metadata = buildMetadataEntries(target, sessionMeta, options);
1168
+ return `${[
1169
+ renderDocumentTitle(title, options.outputFormat),
1170
+ "",
1171
+ renderMetadataBlock(metadata, options.outputFormat)
1172
+ ].filter(Boolean).join("\n")}\n`;
1173
+ };
1174
+ var compactMessageText = (message, optimized) => {
1175
+ const cleaned = stripPreviewBlock(extractText(message.content));
1176
+ return optimized ? optimizePlainText(optimizeRenderedText(cleaned)) : cleaned.trim();
1177
+ };
1178
+ var formatToolOutputSummary = (outputText, outputFormat) => {
1179
+ if (!outputText) return "";
1180
+ const lines = outputText.split("\n").map((line) => line.trim()).filter(Boolean);
1181
+ if (lines.length === 0) return "";
1182
+ const summaryLines = [];
1183
+ const command = lines.find((line) => line.startsWith("Command: "));
1184
+ const exit = lines.find((line) => line.startsWith("Process exited with code "));
1185
+ const wall = lines.find((line) => line.startsWith("Wall time: "));
1186
+ if (command) summaryLines.push(command);
1187
+ if (exit) summaryLines.push(exit);
1188
+ if (wall) summaryLines.push(wall);
1189
+ if (outputFormat === "md") return summaryLines.map((line) => `*${line}*`).join("\n");
1190
+ return summaryLines.join("\n");
1191
+ };
1192
+ var parseExecCommandArguments = (argumentsText) => {
1193
+ if (!argumentsText) return {
1194
+ argumentsParseFailed: false,
1195
+ cmd: null,
1196
+ workdir: null
1197
+ };
1198
+ try {
1199
+ const parsed = JSON.parse(argumentsText);
1200
+ return {
1201
+ argumentsParseFailed: false,
1202
+ cmd: typeof parsed.cmd === "string" ? parsed.cmd : null,
1203
+ workdir: typeof parsed.workdir === "string" ? parsed.workdir : null
1204
+ };
1205
+ } catch {
1206
+ return {
1207
+ argumentsParseFailed: true,
1208
+ cmd: null,
1209
+ workdir: null
1210
+ };
1211
+ }
1212
+ };
1213
+ var getTitle = (target, sessionMeta) => {
1214
+ if (target.thread?.title) return cleanInlineTitle(target.thread.title);
1215
+ return sessionMeta.id ?? path.basename(target.sessionFile, ".jsonl");
1216
+ };
1217
+ var shouldSkipOptimizedPrelude = (role, text) => {
1218
+ if (role !== "user") return true;
1219
+ return text.startsWith("AGENTS.md instructions for ") || text.startsWith("# AGENTS.md instructions for ") || text.startsWith("<permissions instructions>") || text.startsWith("<environment_context>") || text.startsWith("<app-context>") || text.startsWith("<collaboration_mode>") || text.startsWith("<skills_instructions>") || text.startsWith("You are Codex, a coding agent based on GPT-5.") || text.startsWith("Read this before making changes.") || text.includes("Filesystem sandboxing defines which files can be read or written.") || text.includes("approval_policy") || text.includes("base_instructions");
1220
+ };
1221
+ var buildMetadataEntries = (target, sessionMeta, options) => {
1222
+ return [
1223
+ ...buildCodexExportIdentityMetadata(target, sessionMeta),
1224
+ ...buildCodexExportPathMetadata(target, options),
1225
+ ...buildCodexRelationMetadata(target),
1226
+ ...buildCodexThreadMetadata(target, sessionMeta),
1227
+ ...buildCodexAgentMetadata(target)
1228
+ ];
1229
+ };
1230
+ var buildCodexExportIdentityMetadata = (target, sessionMeta) => {
1231
+ const thread = target.thread;
1232
+ return [
1233
+ {
1234
+ key: "exported_from",
1235
+ value: thread ? "thread_db_and_session_jsonl" : "session_jsonl_fallback"
1236
+ },
1237
+ {
1238
+ key: "fallback_reason",
1239
+ value: target.fallbackReason
1240
+ },
1241
+ {
1242
+ key: "thread_id",
1243
+ value: thread?.id ?? sessionMeta.id ?? null
1244
+ },
1245
+ {
1246
+ key: "title",
1247
+ value: thread?.title || null
1248
+ }
1249
+ ];
1250
+ };
1251
+ var buildCodexExportPathMetadata = (target, options) => {
1252
+ const relativeOutputPath = target.outputRelativePath;
1253
+ return [
1254
+ {
1255
+ key: "source_output_relative_path",
1256
+ value: relativeOutputPath
1257
+ },
1258
+ {
1259
+ key: options.outputFormat === "md" ? "source_markdown_path" : "source_text_path",
1260
+ value: relativeOutputPath
1261
+ },
1262
+ {
1263
+ key: "rollout_path",
1264
+ value: target.sessionFile
1265
+ },
1266
+ {
1267
+ key: "rollout_path_relative_to_codex",
1268
+ value: toCodexRelativePath(target.sessionFile)
1269
+ }
1270
+ ];
1271
+ };
1272
+ var buildCodexRelationMetadata = (target) => {
1273
+ const childThreadIds = target.relations.childEdges.map((edge) => edge.child_thread_id);
1274
+ const childEdges = target.relations.childEdges.map((edge) => ({
1275
+ child_thread_id: edge.child_thread_id,
1276
+ status: edge.status
1277
+ }));
1278
+ return [
1279
+ {
1280
+ key: "parent_thread_id",
1281
+ value: target.relations.parentThreadId
1282
+ },
1283
+ {
1284
+ key: "child_thread_ids",
1285
+ value: childThreadIds
1286
+ },
1287
+ {
1288
+ key: "spawn_edges",
1289
+ value: childEdges
1290
+ }
1291
+ ];
1292
+ };
1293
+ var buildCodexThreadMetadata = (target, sessionMeta) => {
1294
+ return [...buildCodexThreadTimingMetadata(target, sessionMeta), ...buildCodexThreadIdentityMetadata(target, sessionMeta)];
1295
+ };
1296
+ var buildCodexThreadTimingMetadata = (target, sessionMeta) => {
1297
+ const thread = target.thread;
1298
+ return [
1299
+ {
1300
+ key: "created_at_unix",
1301
+ value: thread?.created_at ?? null
1302
+ },
1303
+ {
1304
+ key: "created_at_iso",
1305
+ value: formatUnixSeconds(thread?.created_at ?? null)
1306
+ },
1307
+ {
1308
+ key: "updated_at_unix",
1309
+ value: thread?.updated_at ?? null
1310
+ },
1311
+ {
1312
+ key: "updated_at_iso",
1313
+ value: formatUnixSeconds(thread?.updated_at ?? null)
1314
+ },
1315
+ {
1316
+ key: "archived_at_unix",
1317
+ value: thread?.archived_at ?? null
1318
+ },
1319
+ {
1320
+ key: "archived_at_iso",
1321
+ value: formatUnixSeconds(thread?.archived_at ?? null)
1322
+ },
1323
+ {
1324
+ key: "session_started_at_iso",
1325
+ value: sessionMeta.timestamp ?? null
1326
+ }
1327
+ ];
1328
+ };
1329
+ var buildCodexThreadIdentityMetadata = (target, sessionMeta) => {
1330
+ const thread = target.thread;
1331
+ return [
1332
+ {
1333
+ key: "archived",
1334
+ value: thread ? Boolean(thread.archived) : null
1335
+ },
1336
+ {
1337
+ key: "source",
1338
+ value: thread?.source ?? sessionMeta.source ?? null
1339
+ },
1340
+ {
1341
+ key: "originator",
1342
+ value: sessionMeta.originator ?? null
1343
+ },
1344
+ {
1345
+ key: "model_provider",
1346
+ value: thread?.model_provider ?? null
1347
+ },
1348
+ {
1349
+ key: "model",
1350
+ value: thread?.model ?? null
1351
+ },
1352
+ {
1353
+ key: "reasoning_effort",
1354
+ value: thread?.reasoning_effort ?? null
1355
+ },
1356
+ {
1357
+ key: "cli_version",
1358
+ value: thread?.cli_version || sessionMeta.cli_version || null
1359
+ },
1360
+ {
1361
+ key: "cwd",
1362
+ value: thread?.cwd || sessionMeta.cwd || null
1363
+ },
1364
+ {
1365
+ key: "approval_mode",
1366
+ value: thread?.approval_mode ?? null
1367
+ },
1368
+ {
1369
+ key: "sandbox_policy",
1370
+ value: parseJsonSafely(thread?.sandbox_policy ?? null)
1371
+ },
1372
+ {
1373
+ key: "memory_mode",
1374
+ value: thread?.memory_mode ?? null
1375
+ },
1376
+ {
1377
+ key: "tokens_used",
1378
+ value: thread?.tokens_used ?? null
1379
+ },
1380
+ {
1381
+ key: "has_user_event",
1382
+ value: thread ? Boolean(thread.has_user_event) : null
1383
+ }
1384
+ ];
1385
+ };
1386
+ var buildCodexAgentMetadata = (target) => {
1387
+ const thread = target.thread;
1388
+ return [
1389
+ {
1390
+ key: "git_sha",
1391
+ value: thread?.git_sha ?? null
1392
+ },
1393
+ {
1394
+ key: "git_branch",
1395
+ value: thread?.git_branch ?? null
1396
+ },
1397
+ {
1398
+ key: "git_origin_url",
1399
+ value: thread?.git_origin_url ?? null
1400
+ },
1401
+ {
1402
+ key: "agent_nickname",
1403
+ value: thread?.agent_nickname ?? null
1404
+ },
1405
+ {
1406
+ key: "agent_role",
1407
+ value: thread?.agent_role ?? null
1408
+ },
1409
+ {
1410
+ key: "agent_path",
1411
+ value: thread?.agent_path ?? null
1412
+ },
1413
+ {
1414
+ key: "first_user_message",
1415
+ value: thread?.first_user_message || null
1416
+ }
1417
+ ];
1418
+ };
1419
+ var parseJsonSafely = (value) => {
1420
+ if (!value) return null;
1421
+ try {
1422
+ return JSON.parse(value);
1423
+ } catch {
1424
+ return value;
1425
+ }
1426
+ };
1427
+ var formatUnixSeconds = (value) => {
1428
+ if (value === null || value === void 0) return null;
1429
+ return (/* @__PURE__ */ new Date(value * 1e3)).toISOString();
1430
+ };
1431
+ var captureSessionMeta = (parsed, meta) => {
1432
+ if (parsed.type !== "session_meta") return;
1433
+ const payload = asObject(parsed.payload);
1434
+ if (!payload) return;
1435
+ meta.id = asString(payload.id) ?? meta.id;
1436
+ meta.timestamp = asString(payload.timestamp) ?? meta.timestamp;
1437
+ meta.cwd = asString(payload.cwd) ?? meta.cwd;
1438
+ meta.source = asString(payload.source) ?? meta.source;
1439
+ meta.originator = asString(payload.originator) ?? meta.originator;
1440
+ meta.cli_version = asString(payload.cli_version) ?? meta.cli_version;
1441
+ };
1442
+ var extractMessageRecord = (parsed) => {
1443
+ if (parsed.type === "message") {
1444
+ const directMessage = normalizeMessage(parsed);
1445
+ if (directMessage) return directMessage;
1446
+ }
1447
+ if (parsed.type !== "response_item") return null;
1448
+ const payload = asObject(parsed.payload);
1449
+ if (!payload) return null;
1450
+ if (payload.type !== "message" && payload.type !== "agent_message" && payload.type !== "user_message") return null;
1451
+ return normalizeMessage(payload);
1452
+ };
1453
+ var normalizeMessage = (value) => {
1454
+ const type = asString(value.type);
1455
+ const role = asString(value.role) ?? (type === "agent_message" ? "assistant" : type === "user_message" ? "user" : null);
1456
+ const content = value.content ?? asString(value.message);
1457
+ const phase = asString(value.phase);
1458
+ if (!role || content === void 0) return null;
1459
+ return {
1460
+ content,
1461
+ model: asString(value.model),
1462
+ phase: phase ?? void 0,
1463
+ role
1464
+ };
1465
+ };
1466
+ var extractToolRecord = (parsed) => {
1467
+ if (parsed.type !== "response_item") return null;
1468
+ const payload = asObject(parsed.payload);
1469
+ if (!payload) return null;
1470
+ if (payload.type === "function_call") {
1471
+ const name = asString(payload.name);
1472
+ const argumentsText = asString(payload.arguments);
1473
+ const callId = asString(payload.call_id);
1474
+ if (name !== "exec_command") return null;
1475
+ return {
1476
+ argumentsText: argumentsText ?? void 0,
1477
+ callId,
1478
+ kind: "call",
1479
+ name
1480
+ };
1481
+ }
1482
+ if (payload.type === "function_call_output") {
1483
+ const callId = asString(payload.call_id);
1484
+ const outputText = asString(payload.output);
1485
+ if (!outputText?.includes("Command: ")) return null;
1486
+ return {
1487
+ callId,
1488
+ kind: "output",
1489
+ name: "function_call_output",
1490
+ outputText: outputText ?? void 0
1491
+ };
1492
+ }
1493
+ return null;
1494
+ };
1495
+ var renderMessageBlock = (message, outputFormat, assistantModel, includeCommentary) => {
1496
+ if (message.role !== "user" && message.role !== "assistant") return "";
1497
+ if (message.role === "assistant" && message.phase === "commentary" && !includeCommentary) return "";
1498
+ const text = cleanExtractedText(extractText(message.content)).trim();
1499
+ if (!text || shouldSkipMessage(message.role, text)) return "";
1500
+ return renderSection(message.role === "user" ? "User" : formatModelLabel(message.model ?? assistantModel), message.phase ? `Phase: ${message.phase}\n\n${text}` : text, outputFormat);
1501
+ };
1502
+ var renderToolBlock = (tool, outputFormat) => {
1503
+ if (tool.kind === "call") {
1504
+ const details = formatToolCallDetails(tool, outputFormat);
1505
+ return details ? renderSection("Tool", details, outputFormat) : "";
1506
+ }
1507
+ const summary = formatToolOutputSummary(tool.outputText ?? "", outputFormat);
1508
+ return summary ? renderSection("Tool Output", summary, outputFormat) : "";
1509
+ };
1510
+ var renderCompactBlock = (message, text, outputFormat, assistantModel) => {
1511
+ const prefix = message.role === "user" ? "U:" : `${formatModelLabel(message.model ?? assistantModel)}:`;
1512
+ const [firstLine, ...rest] = text.split("\n");
1513
+ if (rest.length === 0) return `${prefix} ${normalizeCompactLiteral(firstLine, outputFormat)}`;
1514
+ return [`${prefix} ${normalizeCompactLiteral(firstLine, outputFormat)}`, ...rest.map((line) => normalizeCompactLiteral(line, outputFormat))].join("\n");
1515
+ };
1516
+ var renderCompactToolBlock = (tool, outputFormat) => {
1517
+ if (tool.kind === "call") {
1518
+ const details = formatCompactToolCall(tool, outputFormat);
1519
+ return details ? `T: ${details}` : "";
1520
+ }
1521
+ const summary = formatCompactToolOutput(tool.outputText ?? "");
1522
+ return summary ? `R: ${summary}` : "";
1523
+ };
1524
+ var stripPreviewBlock = (text) => {
1525
+ const parts = text.split(/\n{2,}/).map((part) => part.trim()).filter(Boolean);
1526
+ if (parts.length < 2) return text.trim();
1527
+ const first = parts[0];
1528
+ const second = parts[1];
1529
+ const isTranscriptHeading = (value) => /^##\s+.+$/i.test(value);
1530
+ if (!(!/^([UA]):/i.test(first) && !isTranscriptHeading(first) && /^([UA]):/i.test(second) === false && isTranscriptHeading(second))) return text.trim();
1531
+ return parts.slice(1).join("\n\n");
1532
+ };
1533
+ var optimizeRenderedText = (text) => {
1534
+ return text.replace(/^\*Phase:\s+`[^`]+`\*\s*\n*/gm, "").replace(/^\s*<image\b[^>]*>\s*$/gim, "").replace(/^\s*<\/image>\s*$/gim, "").replace(/^\s*\[Image attached\]\s*$/gim, "").replace(/^#{1,6}\s+/gm, "").replace(/^```[^\n]*\n?/gm, "").replace(/\n```$/gm, "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^##\s+User\s*$/gm, "User:").replace(/^##\s+Assistant\s*$/gm, "Assistant:").replace(/`([^`]+)`/g, "$1").replace(/\*\*([^*]+)\*\*/g, "$1").replace(/\*([^*\n]+)\*/g, "$1").replace(/\n{3,}/g, "\n\n").trim();
1535
+ };
1536
+ var optimizePlainText = (text) => {
1537
+ return text.replace(/\r/g, "").replace(/^\s*<image\b[^>]*>\s*$/gim, "").replace(/^\s*<\/image>\s*$/gim, "").replace(/^\s*\[Image attached\]\s*$/gim, "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/`([^`]+)`/g, "$1").replace(/\*\*([^*]+)\*\*/g, "$1").replace(/\*([^*\n]+)\*/g, "$1").split("\n").map((line) => line.replace(/[ \t]+$/g, "")).join("\n").replace(/\n{3,}/g, "\n\n").trim();
1538
+ };
1539
+ var shouldSkipMessage = (role, text) => {
1540
+ if (text.startsWith("<environment_context>")) return true;
1541
+ if (text.startsWith("# AGENTS.md instructions for ")) return true;
1542
+ if (role === "user" && text.includes("<environment_context>")) return true;
1543
+ return false;
1544
+ };
1545
+ var formatToolCallDetails = (tool, outputFormat) => {
1546
+ if (tool.name !== "exec_command") return "";
1547
+ const details = parseExecCommandArguments(tool.argumentsText);
1548
+ return details.cmd ? `Command: ${formatInlineLiteral(details.cmd, outputFormat)}` : "";
1549
+ };
1550
+ var formatCompactToolCall = (tool, outputFormat) => {
1551
+ if (tool.name === "exec_command") {
1552
+ const details = parseExecCommandArguments(tool.argumentsText);
1553
+ if (!details.cmd) return "exec_command";
1554
+ const command = formatInlineLiteral(details.cmd, outputFormat);
1555
+ return details.workdir ? `exec_command ${command} @ ${details.workdir}` : `exec_command ${command}`;
1556
+ }
1557
+ return tool.callId ? `${tool.name} (${tool.callId})` : tool.name;
1558
+ };
1559
+ var formatCompactToolOutput = (outputText) => {
1560
+ if (!outputText) return "";
1561
+ const lines = outputText.split("\n").map((line) => line.trim()).filter(Boolean);
1562
+ const exit = lines.find((line) => line.startsWith("Process exited with code "));
1563
+ const wall = lines.find((line) => line.startsWith("Wall time: "));
1564
+ if (exit && wall) return `${exit.replace("Process ", "")}; ${wall.toLowerCase()}`;
1565
+ if (exit) return exit.replace("Process ", "");
1566
+ return "";
1567
+ };
1568
+ var extractText = (content) => {
1569
+ if (typeof content === "string") return content;
1570
+ if (Array.isArray(content)) return content.map((item) => extractContentPart(item)).filter((part) => part.length > 0).join("\n\n");
1571
+ if (content && typeof content === "object") {
1572
+ const text = asString(content.text);
1573
+ if (text) return text;
1574
+ }
1575
+ return "";
1576
+ };
1577
+ var extractContentPart = (value) => {
1578
+ if (!value || typeof value !== "object" || Array.isArray(value)) return "";
1579
+ const item = value;
1580
+ const type = asString(item.type);
1581
+ const text = asString(item.text);
1582
+ if ((type === "input_text" || type === "output_text" || type === "text") && text) return text;
1583
+ if (type === "input_image") return "[Image attached]";
1584
+ return text ?? "";
1585
+ };
1586
+ var normalizeCompactLiteral = (value, outputFormat) => {
1587
+ return outputFormat === "md" ? value : value.replace(/`([^`]+)`/g, "$1");
1588
+ };
1589
+ var UI_EXPORT_URL_PREFIX = "/__exports/";
1590
+ var DEFAULT_UI_EXPORT_DIR = path.join(os.tmpdir(), "spiracha-ui-exports");
1591
+ var DEFAULT_EXPORT_MAX_AGE_MS = 1440 * 60 * 1e3;
1592
+ var getUiExportDir = () => {
1593
+ return process.env["SPIRACHA_UI_EXPORT_DIR"]?.trim() || DEFAULT_UI_EXPORT_DIR;
1594
+ };
1595
+ var ensureUiExportDir = async () => {
1596
+ const exportDir = getUiExportDir();
1597
+ await mkdir(exportDir, { recursive: true });
1598
+ await purgeStaleUiExports(exportDir);
1599
+ return exportDir;
1600
+ };
1601
+ var buildUiExportDownloadUrl = (filePath) => {
1602
+ return `${UI_EXPORT_URL_PREFIX}${encodeURIComponent(path.basename(filePath))}`;
1603
+ };
1604
+ var purgeStaleUiExports = async (exportDir = getUiExportDir(), maxAgeMs = DEFAULT_EXPORT_MAX_AGE_MS) => {
1605
+ const entries = await readdir(exportDir, { withFileTypes: true });
1606
+ const cutoff = Date.now() - maxAgeMs;
1607
+ await Promise.all(entries.filter((entry) => entry.isFile()).map(async (entry) => {
1608
+ const filePath = path.join(exportDir, entry.name);
1609
+ if ((await stat(filePath)).mtimeMs >= cutoff) return;
1610
+ await rm(filePath, { force: true });
1611
+ }));
1612
+ };
1613
+ //#endregion
1614
+ //#region ../../src/lib/codex-browser-export.ts
1615
+ var LARGE_BROWSER_EXPORT_THRESHOLD_BYTES = 128 * 1024 * 1024;
1616
+ var toSanitizedFileName = (value) => {
1617
+ return value.replace(/[<>:"/\\|?*\u0000-\u001f]/gu, " ").replace(/\.\.+/gu, " ").replace(/\s+/gu, " ").trim();
1618
+ };
1619
+ var formatReadableExportDate = (value) => {
1620
+ const date = new Date(value);
1621
+ return `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, "0")}-${String(date.getUTCDate()).padStart(2, "0")}-${String(date.getUTCHours()).padStart(2, "0")}${String(date.getUTCMinutes()).padStart(2, "0")}`;
1622
+ };
1623
+ var buildExportBaseName = (thread) => {
1624
+ return `${toSanitizedFileName(getPortablePathBasename(thread.cwd) || "thread") || "thread"}-${formatReadableExportDate(thread.updated_at_ms ?? thread.updated_at * 1e3)}-${thread.id.slice(0, 8)}`;
1625
+ };
1626
+ var buildBatchExportBaseName = (threads) => {
1627
+ const firstThread = threads[0];
1628
+ if (!firstThread) throw new Error("No threads selected for export");
1629
+ return `${toSanitizedFileName(getPortablePathBasename(firstThread.cwd) || "threads") || "threads"}-${formatReadableExportDate(Math.max(...threads.map((thread) => thread.updated_at_ms ?? thread.updated_at * 1e3)))}-threads-${threads.length}`;
1630
+ };
1631
+ var buildUniqueArchivePath = (exportDir, exportBaseName) => {
1632
+ return path.join(exportDir, `${exportBaseName}-${randomUUID()}.zip`);
1633
+ };
1634
+ var toDownloadOptions = (input) => {
1635
+ return {
1636
+ cwdFilter: null,
1637
+ dbPath: input.dbPath,
1638
+ flat: false,
1639
+ includeCommentary: input.includeCommentary,
1640
+ includeTools: input.includeTools,
1641
+ inputDir: "",
1642
+ optimized: input.optimized,
1643
+ outputDir: "",
1644
+ outputFormat: input.outputFormat,
1645
+ projectFilter: null,
1646
+ threadIds: [input.threadId]
1647
+ };
1648
+ };
1649
+ var getMimeType = (outputFormat) => {
1650
+ return outputFormat === "md" ? "text/markdown; charset=utf-8" : "text/plain; charset=utf-8";
1651
+ };
1652
+ var resolvePublicExportDir = async (publicExportDir) => {
1653
+ if (publicExportDir) {
1654
+ await ensureDirectory(publicExportDir);
1655
+ return publicExportDir;
1656
+ }
1657
+ return ensureUiExportDir();
1658
+ };
1659
+ var ensureDirectory = async (directoryPath) => {
1660
+ await mkdir(directoryPath, { recursive: true });
1661
+ };
1662
+ var createExportWorkspace = async (exportDir, exportBaseName) => {
1663
+ return mkdtemp(path.join(exportDir, `${exportBaseName}-`));
1664
+ };
1665
+ var getRolloutSnapshot = async (rolloutPath) => {
1666
+ const metadata = await stat(rolloutPath);
1667
+ return {
1668
+ mtimeMs: metadata.mtimeMs,
1669
+ sizeBytes: metadata.size
1670
+ };
1671
+ };
1672
+ var logExportEvent = (level, event, details) => {
1673
+ console[level](`[spiracha:export] ${event}`, details);
1674
+ };
1675
+ var logRolloutChangeIfDetected = (threadId, rolloutPath, beforeSnapshot, afterSnapshot) => {
1676
+ if (beforeSnapshot.mtimeMs === afterSnapshot.mtimeMs && beforeSnapshot.sizeBytes === afterSnapshot.sizeBytes) return;
1677
+ logExportEvent("warn", "rollout_changed_during_export", {
1678
+ afterMtimeMs: afterSnapshot.mtimeMs,
1679
+ afterSizeBytes: afterSnapshot.sizeBytes,
1680
+ beforeMtimeMs: beforeSnapshot.mtimeMs,
1681
+ beforeSizeBytes: beforeSnapshot.sizeBytes,
1682
+ rolloutPath,
1683
+ threadId
1684
+ });
1685
+ };
1686
+ var cleanupExportWorkspace = async (workspacePath) => {
1687
+ try {
1688
+ await rm(workspacePath, {
1689
+ force: true,
1690
+ recursive: true
1691
+ });
1692
+ } catch (error) {
1693
+ logExportEvent("warn", "workspace_cleanup_failed", {
1694
+ error: error instanceof Error ? error.message : String(error),
1695
+ workspacePath
1696
+ });
1697
+ }
1698
+ };
1699
+ var zipExportFile = async (sourcePath, zipPath) => {
1700
+ const proc = Bun.spawn([
1701
+ "zip",
1702
+ "-9",
1703
+ "-j",
1704
+ zipPath,
1705
+ sourcePath
1706
+ ], {
1707
+ stderr: "pipe",
1708
+ stdout: "pipe"
1709
+ });
1710
+ const [stdoutText, stderrText, exitCode] = await Promise.all([
1711
+ new Response(proc.stdout).text(),
1712
+ new Response(proc.stderr).text(),
1713
+ proc.exited
1714
+ ]);
1715
+ if (exitCode !== 0) throw new Error(`zip failed (${exitCode}): ${(stderrText || stdoutText).trim()}`);
1716
+ };
1717
+ var zipExportDirectory = async (sourceDirectory, zipPath) => {
1718
+ const proc = Bun.spawn([
1719
+ "zip",
1720
+ "-9",
1721
+ "-r",
1722
+ zipPath,
1723
+ "."
1724
+ ], {
1725
+ cwd: sourceDirectory,
1726
+ stderr: "pipe",
1727
+ stdout: "pipe"
1728
+ });
1729
+ const [stdoutText, stderrText, exitCode] = await Promise.all([
1730
+ new Response(proc.stdout).text(),
1731
+ new Response(proc.stderr).text(),
1732
+ proc.exited
1733
+ ]);
1734
+ if (exitCode !== 0) throw new Error(`zip failed (${exitCode}): ${(stderrText || stdoutText).trim()}`);
1735
+ };
1736
+ var renderCodexThreadDownload = async (input) => {
1737
+ const startedAt = Date.now();
1738
+ const browseData = getThreadBrowseData(input.dbPath, input.threadId);
1739
+ const extension = input.outputFormat === "md" ? "md" : "txt";
1740
+ const fileBaseName = buildExportBaseName(browseData.thread);
1741
+ const fileName = `${fileBaseName}.${extension}`;
1742
+ const mimeType = getMimeType(input.outputFormat);
1743
+ const transform = (text) => input.pathDisplaySettings ? applyPathTransforms(text, {
1744
+ ...input.pathDisplaySettings,
1745
+ projectPath: browseData.thread.cwd
1746
+ }) : text;
1747
+ const rolloutSnapshotBefore = await getRolloutSnapshot(browseData.thread.rollout_path);
1748
+ logExportEvent("info", "single_start", {
1749
+ fileName,
1750
+ rolloutPath: browseData.thread.rollout_path,
1751
+ sizeBytes: rolloutSnapshotBefore.sizeBytes,
1752
+ threadId: input.threadId
1753
+ });
1754
+ try {
1755
+ if (rolloutSnapshotBefore.sizeBytes > (input.largeExportThresholdBytes ?? LARGE_BROWSER_EXPORT_THRESHOLD_BYTES)) {
1756
+ const exportBaseName = fileBaseName;
1757
+ const exportDir = await resolvePublicExportDir(input.publicExportDir);
1758
+ const workspaceDir = await createExportWorkspace(exportDir, exportBaseName);
1759
+ const savedPath = path.join(workspaceDir, `${exportBaseName}.${extension}`);
1760
+ const zipPath = buildUniqueArchivePath(exportDir, exportBaseName);
1761
+ try {
1762
+ if (!await writeSessionFileExport({
1763
+ fallbackReason: null,
1764
+ outputRelativePath: fileName,
1765
+ relations: browseData.relations,
1766
+ sessionFile: browseData.thread.rollout_path,
1767
+ thread: browseData.thread
1768
+ }, toDownloadOptions(input), savedPath, transform)) throw new Error(`Thread ${input.threadId} produced no exportable content`);
1769
+ await zipExportFile(savedPath, zipPath);
1770
+ } finally {
1771
+ await cleanupExportWorkspace(workspaceDir);
1772
+ }
1773
+ const rolloutSnapshotAfter = await getRolloutSnapshot(browseData.thread.rollout_path);
1774
+ logRolloutChangeIfDetected(input.threadId, browseData.thread.rollout_path, rolloutSnapshotBefore, rolloutSnapshotAfter);
1775
+ const zipStat = await Bun.file(zipPath).stat();
1776
+ logExportEvent("info", "single_zip_ready", {
1777
+ downloadUrl: buildUiExportDownloadUrl(zipPath),
1778
+ durationMs: Date.now() - startedAt,
1779
+ fileName: `${exportBaseName}.zip`,
1780
+ sizeBytes: zipStat.size,
1781
+ threadId: input.threadId,
1782
+ zipPath
1783
+ });
1784
+ return {
1785
+ downloadUrl: buildUiExportDownloadUrl(zipPath),
1786
+ fileName: `${exportBaseName}.zip`,
1787
+ mimeType: "application/zip",
1788
+ mode: "download_url"
1789
+ };
1790
+ }
1791
+ const content = await convertSessionFile({
1792
+ fallbackReason: null,
1793
+ outputRelativePath: fileName,
1794
+ relations: browseData.relations,
1795
+ sessionFile: browseData.thread.rollout_path,
1796
+ thread: browseData.thread
1797
+ }, toDownloadOptions(input));
1798
+ if (!content) throw new Error(`Thread ${input.threadId} produced no exportable content`);
1799
+ const rolloutSnapshotAfter = await getRolloutSnapshot(browseData.thread.rollout_path);
1800
+ logRolloutChangeIfDetected(input.threadId, browseData.thread.rollout_path, rolloutSnapshotBefore, rolloutSnapshotAfter);
1801
+ logExportEvent("info", "single_inline_ready", {
1802
+ durationMs: Date.now() - startedAt,
1803
+ fileName,
1804
+ sizeBytes: content.length,
1805
+ threadId: input.threadId
1806
+ });
1807
+ return {
1808
+ content: transform(content),
1809
+ fileName,
1810
+ mimeType,
1811
+ mode: "download"
1812
+ };
1813
+ } catch (error) {
1814
+ logExportEvent("error", "single_error", {
1815
+ error: error instanceof Error ? error.message : String(error),
1816
+ fileName,
1817
+ threadId: input.threadId
1818
+ });
1819
+ throw error;
1820
+ }
1821
+ };
1822
+ var renderCodexThreadsDownload = async (input) => {
1823
+ const startedAt = Date.now();
1824
+ const threadIds = [...new Set(input.threadIds)];
1825
+ if (threadIds.length === 0) throw new Error("No threads selected for export");
1826
+ const browseEntries = threadIds.map((threadId) => getThreadBrowseData(input.dbPath, threadId));
1827
+ const threads = browseEntries.map((entry) => entry.thread);
1828
+ const exportDir = await resolvePublicExportDir(input.publicExportDir);
1829
+ const exportBaseName = buildBatchExportBaseName(threads);
1830
+ const bundleDirectory = await createExportWorkspace(exportDir, exportBaseName);
1831
+ const zipPath = buildUniqueArchivePath(exportDir, exportBaseName);
1832
+ logExportEvent("info", "batch_start", {
1833
+ exportBaseName,
1834
+ selectedThreadCount: threadIds.length,
1835
+ selectedThreadIds: threadIds,
1836
+ zipPath
1837
+ });
1838
+ try {
1839
+ for (const entry of browseEntries) {
1840
+ const rolloutSnapshotBefore = await getRolloutSnapshot(entry.thread.rollout_path);
1841
+ const relativeFileName = `${buildExportBaseName(entry.thread)}.${input.outputFormat === "md" ? "md" : "txt"}`;
1842
+ const savedPath = path.join(bundleDirectory, relativeFileName);
1843
+ const transform = (text) => input.pathDisplaySettings ? applyPathTransforms(text, {
1844
+ ...input.pathDisplaySettings,
1845
+ projectPath: entry.thread.cwd
1846
+ }) : text;
1847
+ if (!await writeSessionFileExport({
1848
+ fallbackReason: null,
1849
+ outputRelativePath: relativeFileName,
1850
+ relations: entry.relations,
1851
+ sessionFile: entry.thread.rollout_path,
1852
+ thread: entry.thread
1853
+ }, {
1854
+ ...toDownloadOptions({
1855
+ ...input,
1856
+ threadId: entry.thread.id
1857
+ }),
1858
+ threadIds: [entry.thread.id]
1859
+ }, savedPath, transform)) throw new Error(`Thread ${entry.thread.id} produced no exportable content`);
1860
+ const rolloutSnapshotAfter = await getRolloutSnapshot(entry.thread.rollout_path);
1861
+ logRolloutChangeIfDetected(entry.thread.id, entry.thread.rollout_path, rolloutSnapshotBefore, rolloutSnapshotAfter);
1862
+ }
1863
+ await zipExportDirectory(bundleDirectory, zipPath);
1864
+ } catch (error) {
1865
+ logExportEvent("error", "batch_error", {
1866
+ error: error instanceof Error ? error.message : String(error),
1867
+ exportBaseName,
1868
+ selectedThreadCount: threadIds.length,
1869
+ selectedThreadIds: threadIds,
1870
+ zipPath
1871
+ });
1872
+ throw error;
1873
+ } finally {
1874
+ await cleanupExportWorkspace(bundleDirectory);
1875
+ }
1876
+ const zipStat = await Bun.file(zipPath).stat();
1877
+ logExportEvent("info", "batch_ready", {
1878
+ downloadUrl: buildUiExportDownloadUrl(zipPath),
1879
+ durationMs: Date.now() - startedAt,
1880
+ fileName: `${exportBaseName}.zip`,
1881
+ selectedThreadCount: threadIds.length,
1882
+ selectedThreadIds: threadIds,
1883
+ sizeBytes: zipStat.size,
1884
+ zipPath
1885
+ });
1886
+ return {
1887
+ downloadUrl: buildUiExportDownloadUrl(zipPath),
1888
+ fileName: `${exportBaseName}.zip`,
1889
+ mimeType: "application/zip",
1890
+ mode: "download_url"
1891
+ };
1892
+ };
1893
+ //#endregion
1894
+ //#region src/lib/codex-server.ts?tss-serverfn-split
1895
+ var projectSchema = z.object({ project: z.string().min(1) });
1896
+ var deleteProjectSchema = z.object({
1897
+ deleteSessionFiles: z.boolean().default(false),
1898
+ project: z.string().min(1)
1899
+ });
1900
+ var threadSchema = z.object({ threadId: z.string().min(1) });
1901
+ var deleteThreadSchema = z.object({
1902
+ deleteSessionFiles: z.boolean().default(false),
1903
+ threadId: z.string().min(1)
1904
+ });
1905
+ var deleteThreadsSchema = z.object({
1906
+ deleteSessionFiles: z.boolean().default(false),
1907
+ threadIds: z.array(z.string().min(1)).min(1)
1908
+ });
1909
+ var analyticsSchema = z.object({ project: z.string().min(1).nullable() });
1910
+ var exportSchema = z.object({
1911
+ convertToProjectRoot: z.boolean(),
1912
+ includeCommentary: z.boolean(),
1913
+ includeTools: z.boolean(),
1914
+ optimized: z.boolean(),
1915
+ outputFormat: z.enum(["md", "txt"]),
1916
+ redactUsername: z.boolean(),
1917
+ threadId: z.string().min(1)
1918
+ });
1919
+ var exportThreadsSchema = z.object({
1920
+ convertToProjectRoot: z.boolean(),
1921
+ includeCommentary: z.boolean(),
1922
+ includeTools: z.boolean(),
1923
+ optimized: z.boolean(),
1924
+ outputFormat: z.enum(["md", "txt"]),
1925
+ redactUsername: z.boolean(),
1926
+ threadIds: z.array(z.string().min(1)).min(1)
1927
+ });
1928
+ var getDbPath = () => process.env.SPIRACHA_CODEX_DB?.trim() || resolveCodexThreadDbPath();
1929
+ var isMissingFileError = (error) => {
1930
+ return error instanceof Error && /ENOENT|no such file/i.test(error.message);
1931
+ };
1932
+ var getDashboardSummaryFn_createServerFn_handler = createServerRpc({
1933
+ id: "792690638a3b10035a5b7368c3d98bdc70cbfe1e36a4aa5f45b1c49b8b1025b0",
1934
+ name: "getDashboardSummaryFn",
1935
+ filename: "src/lib/codex-server.ts"
1936
+ }, (opts) => getDashboardSummaryFn.__executeServer(opts));
1937
+ var getDashboardSummaryFn = createServerFn({ method: "GET" }).handler(getDashboardSummaryFn_createServerFn_handler, async () => {
1938
+ return getCodexDashboardSummary(getDbPath());
1939
+ });
1940
+ var listProjectsFn_createServerFn_handler = createServerRpc({
1941
+ id: "ccefccb816ba13508f23db4e31067b3403e750225257592d3ae11071ffc3fd6f",
1942
+ name: "listProjectsFn",
1943
+ filename: "src/lib/codex-server.ts"
1944
+ }, (opts) => listProjectsFn.__executeServer(opts));
1945
+ var listProjectsFn = createServerFn({ method: "GET" }).handler(listProjectsFn_createServerFn_handler, async () => {
1946
+ return listCodexProjects(getDbPath());
1947
+ });
1948
+ var listProjectThreadsFn_createServerFn_handler = createServerRpc({
1949
+ id: "59fb2cb4d60c8e7d47e0afcc914ee6f9d9f4bf076c8e66eab1693066753655b3",
1950
+ name: "listProjectThreadsFn",
1951
+ filename: "src/lib/codex-server.ts"
1952
+ }, (opts) => listProjectThreadsFn.__executeServer(opts));
1953
+ var listProjectThreadsFn = createServerFn({ method: "GET" }).inputValidator(projectSchema).handler(listProjectThreadsFn_createServerFn_handler, async ({ data }) => {
1954
+ return listProjectThreads(getDbPath(), data.project);
1955
+ });
1956
+ var getThreadSnapshotFn_createServerFn_handler = createServerRpc({
1957
+ id: "72991e2b6e0adbf8d63bb8b139dad88a00b77b7030ec28ceac36c3cce7846b4c",
1958
+ name: "getThreadSnapshotFn",
1959
+ filename: "src/lib/codex-server.ts"
1960
+ }, (opts) => getThreadSnapshotFn.__executeServer(opts));
1961
+ var getThreadSnapshotFn = createServerFn({ method: "GET" }).inputValidator(threadSchema).handler(getThreadSnapshotFn_createServerFn_handler, async ({ data }) => {
1962
+ const browseData = getThreadBrowseData(getDbPath(), data.threadId);
1963
+ const rollout = await getThreadRolloutLoadState(browseData.thread.rollout_path);
1964
+ let transcript = null;
1965
+ let transcriptState = rollout.shouldDeferTranscriptLoad ? "deferred" : "available";
1966
+ if (!rollout.shouldDeferTranscriptLoad) try {
1967
+ transcript = await getCachedParsedCodexTranscript(browseData.thread.rollout_path);
1968
+ } catch (error) {
1969
+ if (!isMissingFileError(error)) throw error;
1970
+ transcriptState = "missing";
1971
+ }
1972
+ return {
1973
+ ...browseData,
1974
+ availableTools: browseData.dynamicTools.length > 0 ? browseData.dynamicTools : transcript?.sessionMeta.dynamicTools ?? [],
1975
+ rollout,
1976
+ transcript,
1977
+ transcriptState
1978
+ };
1979
+ });
1980
+ var getThreadTranscriptFn_createServerFn_handler = createServerRpc({
1981
+ id: "5da27045f7e28ded6353bc16aace284af7ef1b4010ef04d0186a6feadb466497",
1982
+ name: "getThreadTranscriptFn",
1983
+ filename: "src/lib/codex-server.ts"
1984
+ }, (opts) => getThreadTranscriptFn.__executeServer(opts));
1985
+ var getThreadTranscriptFn = createServerFn({ method: "GET" }).inputValidator(threadSchema).handler(getThreadTranscriptFn_createServerFn_handler, async ({ data }) => {
1986
+ return getCachedThreadTranscriptPreview(getThreadBrowseData(getDbPath(), data.threadId).thread.rollout_path);
1987
+ });
1988
+ var getAnalyticsFn_createServerFn_handler = createServerRpc({
1989
+ id: "4712520da0f07bbd1f0907e5a162fe518516ff4caca3fd23876cc65539d87d7a",
1990
+ name: "getAnalyticsFn",
1991
+ filename: "src/lib/codex-server.ts"
1992
+ }, (opts) => getAnalyticsFn.__executeServer(opts));
1993
+ var getAnalyticsFn = createServerFn({ method: "GET" }).inputValidator(analyticsSchema).handler(getAnalyticsFn_createServerFn_handler, async ({ data }) => {
1994
+ return getCodexAnalytics({
1995
+ dbPath: getDbPath(),
1996
+ project: data.project
1997
+ });
1998
+ });
1999
+ var exportThreadFn_createServerFn_handler = createServerRpc({
2000
+ id: "0814663c3bdc58135f97d210d145ef0be5ca54ef9a5f1e3030be9b1bfc901e30",
2001
+ name: "exportThreadFn",
2002
+ filename: "src/lib/codex-server.ts"
2003
+ }, (opts) => exportThreadFn.__executeServer(opts));
2004
+ var exportThreadFn = createServerFn({ method: "POST" }).inputValidator(exportSchema).handler(exportThreadFn_createServerFn_handler, async ({ data }) => {
2005
+ return renderCodexThreadDownload({
2006
+ dbPath: getDbPath(),
2007
+ includeCommentary: data.includeCommentary,
2008
+ includeTools: data.includeTools,
2009
+ optimized: data.optimized,
2010
+ outputFormat: data.outputFormat,
2011
+ pathDisplaySettings: {
2012
+ convertToProjectRoot: data.convertToProjectRoot,
2013
+ redactUsername: data.redactUsername
2014
+ },
2015
+ threadId: data.threadId
2016
+ });
2017
+ });
2018
+ var exportThreadsFn_createServerFn_handler = createServerRpc({
2019
+ id: "b4e15c006e9a277470958bb008f89b5b0acc7256109581de44cf17d587d174a5",
2020
+ name: "exportThreadsFn",
2021
+ filename: "src/lib/codex-server.ts"
2022
+ }, (opts) => exportThreadsFn.__executeServer(opts));
2023
+ var exportThreadsFn = createServerFn({ method: "POST" }).inputValidator(exportThreadsSchema).handler(exportThreadsFn_createServerFn_handler, async ({ data }) => {
2024
+ return renderCodexThreadsDownload({
2025
+ dbPath: getDbPath(),
2026
+ includeCommentary: data.includeCommentary,
2027
+ includeTools: data.includeTools,
2028
+ optimized: data.optimized,
2029
+ outputFormat: data.outputFormat,
2030
+ pathDisplaySettings: {
2031
+ convertToProjectRoot: data.convertToProjectRoot,
2032
+ redactUsername: data.redactUsername
2033
+ },
2034
+ threadIds: data.threadIds
2035
+ });
2036
+ });
2037
+ var deleteThreadFn_createServerFn_handler = createServerRpc({
2038
+ id: "29727b7ad5b8fe42e83817376653e064d9fe8888799f056b2e59296b3396568b",
2039
+ name: "deleteThreadFn",
2040
+ filename: "src/lib/codex-server.ts"
2041
+ }, (opts) => deleteThreadFn.__executeServer(opts));
2042
+ var deleteThreadFn = createServerFn({ method: "POST" }).inputValidator(deleteThreadSchema).handler(deleteThreadFn_createServerFn_handler, async ({ data }) => {
2043
+ return deleteCodexThread(getDbPath(), data.threadId, { deleteSessionFiles: data.deleteSessionFiles });
2044
+ });
2045
+ var deleteThreadsFn_createServerFn_handler = createServerRpc({
2046
+ id: "96aa60bf7dd9b5bde415bcf3ad6f6955a975eecd9aa0516cf401cc39bebebe6c",
2047
+ name: "deleteThreadsFn",
2048
+ filename: "src/lib/codex-server.ts"
2049
+ }, (opts) => deleteThreadsFn.__executeServer(opts));
2050
+ var deleteThreadsFn = createServerFn({ method: "POST" }).inputValidator(deleteThreadsSchema).handler(deleteThreadsFn_createServerFn_handler, async ({ data }) => {
2051
+ return deleteCodexThreads(getDbPath(), data.threadIds, { deleteSessionFiles: data.deleteSessionFiles });
2052
+ });
2053
+ var deleteProjectFn_createServerFn_handler = createServerRpc({
2054
+ id: "164ee82cdd565ed96591a64312f0f7bd961040baf066a89d9f5636330d11360b",
2055
+ name: "deleteProjectFn",
2056
+ filename: "src/lib/codex-server.ts"
2057
+ }, (opts) => deleteProjectFn.__executeServer(opts));
2058
+ var deleteProjectFn = createServerFn({ method: "POST" }).inputValidator(deleteProjectSchema).handler(deleteProjectFn_createServerFn_handler, async ({ data }) => {
2059
+ return deleteCodexProject(getDbPath(), data.project, { deleteSessionFiles: data.deleteSessionFiles });
2060
+ });
2061
+ //#endregion
2062
+ export { deleteProjectFn_createServerFn_handler, deleteThreadFn_createServerFn_handler, deleteThreadsFn_createServerFn_handler, exportThreadFn_createServerFn_handler, exportThreadsFn_createServerFn_handler, getAnalyticsFn_createServerFn_handler, getDashboardSummaryFn_createServerFn_handler, getThreadSnapshotFn_createServerFn_handler, getThreadTranscriptFn_createServerFn_handler, listProjectThreadsFn_createServerFn_handler, listProjectsFn_createServerFn_handler };