spiracha 1.0.0 → 1.1.0

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 (88) hide show
  1. package/AGENTS.md +28 -1
  2. package/README.md +47 -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-BjYaHqXk.js +1 -0
  7. package/apps/ui/dist/client/assets/checkbox-wPoGG3of.js +1 -0
  8. package/apps/ui/dist/client/assets/data-table-6yDgAdtf.js +4 -0
  9. package/apps/ui/dist/client/assets/delete-confirm-dialog-DJUAk7ha.js +11 -0
  10. package/apps/ui/dist/client/assets/download-BhWd-Pm5.js +1 -0
  11. package/apps/ui/dist/client/assets/es2015-BlyMI4CF.js +41 -0
  12. package/apps/ui/dist/client/assets/formatters-BxjZwWSE.js +1 -0
  13. package/apps/ui/dist/client/assets/index-T01rPkb4.js +22 -0
  14. package/apps/ui/dist/client/assets/input-B3YN8gzg.js +1 -0
  15. package/apps/ui/dist/client/assets/metric-card-BWW7TWER.js +1 -0
  16. package/apps/ui/dist/client/assets/page-header-BZ8Gnxgs.js +1 -0
  17. package/apps/ui/dist/client/assets/projects._project-B7XcpoLt.js +1 -0
  18. package/apps/ui/dist/client/assets/projects._project-EfBhCHPY.js +1 -0
  19. package/apps/ui/dist/client/assets/projects.index-4vfIwLjw.js +1 -0
  20. package/apps/ui/dist/client/assets/projects.index-DzEZ4pAJ.js +1 -0
  21. package/apps/ui/dist/client/assets/routes-CWCCZykE.js +1 -0
  22. package/apps/ui/dist/client/assets/select-DLXGsyZ4.js +1 -0
  23. package/apps/ui/dist/client/assets/settings-b0Xthfae.js +1 -0
  24. package/apps/ui/dist/client/assets/styles-8Wtc8YJw.css +1 -0
  25. package/apps/ui/dist/client/assets/threads._threadId-CgtoCqTb.js +1 -0
  26. package/apps/ui/dist/client/assets/threads._threadId-DBiDb38K.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-BjsXNYgm.js +99 -0
  34. package/apps/ui/dist/server/assets/_threadId-B6SrBR9E.js +6 -0
  35. package/apps/ui/dist/server/assets/analytics-Br_fZB6a.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-Cqh0hb93.js +1995 -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-CzHmFWGk.js +286 -0
  43. package/apps/ui/dist/server/assets/formatters-B6o5pTY9.js +72 -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/page-header-CxdZM86z.js +25 -0
  48. package/apps/ui/dist/server/assets/path-transforms-DD1e7rhY.js +31 -0
  49. package/apps/ui/dist/server/assets/projects._project-Bwf6iJC-.js +335 -0
  50. package/apps/ui/dist/server/assets/projects._project-CLSohrBp.js +26 -0
  51. package/apps/ui/dist/server/assets/projects._project-DdVSdfPe.js +18 -0
  52. package/apps/ui/dist/server/assets/projects.index-CaplpeMy.js +26 -0
  53. package/apps/ui/dist/server/assets/projects.index-DKeVeqUZ.js +171 -0
  54. package/apps/ui/dist/server/assets/router-ve2Hrl2Y.js +307 -0
  55. package/apps/ui/dist/server/assets/routes-BJyx5OmO.js +34 -0
  56. package/apps/ui/dist/server/assets/routes-pkOwjjYc.js +168 -0
  57. package/apps/ui/dist/server/assets/select-GW76p-ld.js +76 -0
  58. package/apps/ui/dist/server/assets/settings-MvWDgc1u.js +100 -0
  59. package/apps/ui/dist/server/assets/settings-store-DpEJEQ7M.js +52 -0
  60. package/apps/ui/dist/server/assets/sqlite-error-LZDrnxdd.js +13 -0
  61. package/apps/ui/dist/server/assets/start-BAvbjjfs.js +4 -0
  62. package/apps/ui/dist/server/assets/threads._threadId-BSSK4nkI.js +26 -0
  63. package/apps/ui/dist/server/assets/threads._threadId-D3PYZIwl.js +18 -0
  64. package/apps/ui/dist/server/assets/threads._threadId-D3xaWM86.js +1037 -0
  65. package/apps/ui/dist/server/assets/utils-C_uf36nf.js +8 -0
  66. package/apps/ui/dist/server/server.js +5678 -0
  67. package/package.json +47 -7
  68. package/src/export-chats.ts +1 -14
  69. package/src/lib/codex-analytics.ts +100 -0
  70. package/src/lib/codex-browser-db.ts +518 -0
  71. package/src/lib/codex-browser-export.ts +418 -0
  72. package/src/lib/codex-browser-types.ts +224 -0
  73. package/src/lib/codex-exporter-cli.ts +5 -0
  74. package/src/lib/codex-exporter-transcript.ts +143 -32
  75. package/src/lib/codex-exporter-types.ts +8 -0
  76. package/src/lib/codex-thread-cache.ts +58 -0
  77. package/src/lib/codex-thread-parser.ts +604 -0
  78. package/src/lib/interactive-cli.ts +5 -13
  79. package/src/lib/native-open.ts +54 -0
  80. package/src/lib/path-transforms.ts +45 -0
  81. package/src/lib/shared.ts +37 -1
  82. package/src/lib/sqlite-error.ts +14 -0
  83. package/src/lib/sqlite-retry.ts +39 -0
  84. package/src/lib/ui-cache.ts +96 -0
  85. package/src/lib/ui-export-files.ts +77 -0
  86. package/src/mcp-server.ts +1 -0
  87. package/src/spiracha.ts +14 -1
  88. package/src/ui-cli.ts +310 -0
@@ -0,0 +1,45 @@
1
+ export type PathDisplaySettings = {
2
+ convertToProjectRoot: boolean;
3
+ projectPath?: string | null;
4
+ redactUsername: boolean;
5
+ };
6
+
7
+ const escapeForRegex = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
8
+
9
+ const toUniquePathVariants = (projectPath: string) => {
10
+ const normalized = projectPath.trim();
11
+ const variants = [normalized, normalized.replaceAll('\\', '/'), normalized.replaceAll('/', '\\')].filter(Boolean);
12
+ return [...new Set(variants)].sort((left, right) => right.length - left.length);
13
+ };
14
+
15
+ const replaceExactProjectPath = (text: string, projectPath: string) => {
16
+ let result = text;
17
+
18
+ for (const variant of toUniquePathVariants(projectPath)) {
19
+ const escapedVariant = escapeForRegex(variant);
20
+ result = result.replace(new RegExp(`${escapedVariant}(?<separator>[\\\\/])`, 'gu'), '');
21
+ result = result.replace(new RegExp(`${escapedVariant}(?=$|[^A-Za-z0-9._-])`, 'gu'), '.');
22
+ }
23
+
24
+ return result;
25
+ };
26
+
27
+ const redactRemainingUsernames = (text: string) => {
28
+ return text
29
+ .replace(/\/Users\/[^/\\]+(?=\/|$)/gu, '~')
30
+ .replace(/[A-Za-z]:[\\/]+Users[\\/]+[^\\/]+(?=[\\/]|$)/gu, '~');
31
+ };
32
+
33
+ export const applyPathTransforms = (text: string, settings: PathDisplaySettings): string => {
34
+ let result = text;
35
+
36
+ if (settings.convertToProjectRoot && settings.projectPath) {
37
+ result = replaceExactProjectPath(result, settings.projectPath);
38
+ }
39
+
40
+ if (settings.redactUsername) {
41
+ result = redactRemainingUsernames(result);
42
+ }
43
+
44
+ return result;
45
+ };
package/src/lib/shared.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { createReadStream } from 'node:fs';
1
+ import { createReadStream, createWriteStream } from 'node:fs';
2
2
  import { mkdir } from 'node:fs/promises';
3
3
  import os from 'node:os';
4
4
  import path from 'node:path';
5
5
  import { createInterface } from 'node:readline';
6
+ import { finished } from 'node:stream/promises';
6
7
 
7
8
  export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
8
9
 
@@ -59,6 +60,31 @@ export const cleanExtractedText = (text: string): string => {
59
60
  return text.replace(/^\s*<\/?image>\s*$/gm, '').replace(/\n{3,}/g, '\n\n');
60
61
  };
61
62
 
63
+ export const formatModelLabel = (value: string | null | undefined): string => {
64
+ if (!value) {
65
+ return 'Assistant';
66
+ }
67
+
68
+ return value
69
+ .split(/[-_\s]+/u)
70
+ .filter(Boolean)
71
+ .map((part) => {
72
+ const lower = part.toLowerCase();
73
+ if (lower === 'gpt') {
74
+ return 'GPT';
75
+ }
76
+ if (/^[a-z]\d$/u.test(lower)) {
77
+ return lower.toUpperCase();
78
+ }
79
+ if (/^\d+(\.\d+)*$/u.test(part)) {
80
+ return part;
81
+ }
82
+
83
+ return `${part.slice(0, 1).toUpperCase()}${part.slice(1)}`;
84
+ })
85
+ .join(' ');
86
+ };
87
+
62
88
  export const asObject = (value: JsonValue): Record<string, JsonValue> | null => {
63
89
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
64
90
  return null;
@@ -207,6 +233,16 @@ export const writeExportFile = async (outputPath: string, content: string) => {
207
233
  await Bun.write(outputPath, content);
208
234
  };
209
235
 
236
+ export const createExportWriteStream = async (outputPath: string) => {
237
+ await mkdir(path.dirname(outputPath), { recursive: true });
238
+ return createWriteStream(outputPath, { encoding: 'utf8' });
239
+ };
240
+
241
+ export const finalizeExportWriteStream = async (stream: NodeJS.WritableStream) => {
242
+ stream.end();
243
+ await finished(stream);
244
+ };
245
+
210
246
  const toMetadataValue = (value: unknown, format: ExportFormat): string => {
211
247
  if (Array.isArray(value) || (value && typeof value === 'object')) {
212
248
  return JSON.stringify(value);
@@ -0,0 +1,14 @@
1
+ const SQLITE_RETRYABLE_PATTERNS = [
2
+ /unable to open database file/iu,
3
+ /database is locked/iu,
4
+ /SQLITE_BUSY/iu,
5
+ /SQLITE_CANTOPEN/iu,
6
+ ];
7
+
8
+ export const isRetryableSqliteError = (error: unknown) => {
9
+ if (!(error instanceof Error)) {
10
+ return false;
11
+ }
12
+
13
+ return SQLITE_RETRYABLE_PATTERNS.some((pattern) => pattern.test(error.message));
14
+ };
@@ -0,0 +1,39 @@
1
+ import { isRetryableSqliteError } from './sqlite-error';
2
+
3
+ const DEFAULT_RETRY_DELAYS_MS = [40, 120, 250] as const;
4
+ const SLEEP_BUFFER = new Int32Array(new SharedArrayBuffer(4));
5
+
6
+ type SyncRetryOptions<T> = {
7
+ action: () => T;
8
+ delaysMs?: readonly number[];
9
+ sleep?: (delayMs: number) => void;
10
+ };
11
+
12
+ const sleepSync = (delayMs: number) => {
13
+ if (delayMs <= 0) {
14
+ return;
15
+ }
16
+
17
+ Atomics.wait(SLEEP_BUFFER, 0, 0, delayMs);
18
+ };
19
+
20
+ export const runWithSqliteRetry = <T>({
21
+ action,
22
+ delaysMs = DEFAULT_RETRY_DELAYS_MS,
23
+ sleep = sleepSync,
24
+ }: SyncRetryOptions<T>): T => {
25
+ let attempt = 0;
26
+
27
+ while (true) {
28
+ try {
29
+ return action();
30
+ } catch (error) {
31
+ if (!isRetryableSqliteError(error) || attempt >= delaysMs.length) {
32
+ throw error;
33
+ }
34
+
35
+ sleep(delaysMs[attempt] ?? 0);
36
+ attempt += 1;
37
+ }
38
+ }
39
+ };
@@ -0,0 +1,96 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { mkdir, readdir, rename, rm, stat } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+
6
+ const CACHE_DIR = path.join(os.tmpdir(), 'spiracha-ui-cache');
7
+ const CACHE_ENVELOPE_VERSION = 1;
8
+
9
+ type CacheEnvelope<T> = {
10
+ value: T;
11
+ version: number;
12
+ };
13
+
14
+ const ensureCacheDir = async () => {
15
+ await mkdir(CACHE_DIR, { recursive: true });
16
+ };
17
+
18
+ const toCachePath = (key: string) => {
19
+ const safeKey = key.replace(/[^a-zA-Z0-9._-]/gu, '_');
20
+ return path.join(CACHE_DIR, `${safeKey}-${hashCacheKeyParts(key)}.json`);
21
+ };
22
+
23
+ export const hashCacheKeyParts = (...parts: string[]) => {
24
+ return createHash('sha1').update(parts.join('|')).digest('hex');
25
+ };
26
+
27
+ export const getFileFingerprint = async (filePath: string) => {
28
+ const metadata = await stat(filePath);
29
+ return `${filePath}:${metadata.size}:${metadata.mtimeMs}`;
30
+ };
31
+
32
+ export const getCachedJson = async <T>(key: string): Promise<T | null> => {
33
+ await ensureCacheDir();
34
+ const filePath = toCachePath(key);
35
+ const file = Bun.file(filePath);
36
+ if (!(await file.exists())) {
37
+ return null;
38
+ }
39
+
40
+ let parsed: CacheEnvelope<T> | T;
41
+ try {
42
+ parsed = (await file.json()) as CacheEnvelope<T> | T;
43
+ } catch {
44
+ await rm(filePath, { force: true });
45
+ return null;
46
+ }
47
+
48
+ if (
49
+ parsed &&
50
+ typeof parsed === 'object' &&
51
+ 'version' in parsed &&
52
+ (parsed as CacheEnvelope<T>).version === CACHE_ENVELOPE_VERSION &&
53
+ 'value' in parsed
54
+ ) {
55
+ return (parsed as CacheEnvelope<T>).value;
56
+ }
57
+
58
+ return parsed as T;
59
+ };
60
+
61
+ export const setCachedJson = async <T>(key: string, value: T) => {
62
+ await ensureCacheDir();
63
+ const filePath = toCachePath(key);
64
+ const tempPath = `${filePath}.${randomUUID()}.tmp`;
65
+ const envelope: CacheEnvelope<T> = {
66
+ value,
67
+ version: CACHE_ENVELOPE_VERSION,
68
+ };
69
+
70
+ await Bun.write(tempPath, JSON.stringify(envelope));
71
+ await rename(tempPath, filePath);
72
+ };
73
+
74
+ export const withCachedJson = async <T>(key: string, loader: () => Promise<T>): Promise<T> => {
75
+ const filePath = toCachePath(key);
76
+ const existedBeforeRead = await Bun.file(filePath).exists();
77
+ const cached = await getCachedJson<T>(key);
78
+ if (cached !== null || (existedBeforeRead && (await Bun.file(filePath).exists()))) {
79
+ return cached as T;
80
+ }
81
+
82
+ const value = await loader();
83
+ await setCachedJson(key, value);
84
+ return value;
85
+ };
86
+
87
+ export const invalidateCacheByPrefix = async (...prefixes: string[]) => {
88
+ await ensureCacheDir();
89
+ const entries = await readdir(CACHE_DIR);
90
+
91
+ await Promise.all(
92
+ entries
93
+ .filter((entry) => prefixes.some((prefix) => entry.startsWith(prefix)))
94
+ .map((entry) => rm(path.join(CACHE_DIR, entry), { force: true })),
95
+ );
96
+ };
@@ -0,0 +1,77 @@
1
+ import { mkdir, readdir, rm, stat } from 'node:fs/promises';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+
5
+ export const UI_EXPORT_DIR_ENV = 'SPIRACHA_UI_EXPORT_DIR';
6
+ export const UI_EXPORT_URL_PREFIX = '/__exports/';
7
+
8
+ const DEFAULT_UI_EXPORT_DIR = path.join(os.tmpdir(), 'spiracha-ui-exports');
9
+ const DEFAULT_EXPORT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
10
+
11
+ const decodeExportFileName = (value: string) => {
12
+ try {
13
+ return decodeURIComponent(value);
14
+ } catch {
15
+ return null;
16
+ }
17
+ };
18
+
19
+ const isSafeExportFileName = (value: string) => {
20
+ return value.length > 0 && value === path.basename(value) && !/[\\/]/u.test(value) && !value.includes('\0');
21
+ };
22
+
23
+ export const getUiExportDir = () => {
24
+ return process.env[UI_EXPORT_DIR_ENV]?.trim() || DEFAULT_UI_EXPORT_DIR;
25
+ };
26
+
27
+ export const ensureUiExportDir = async () => {
28
+ const exportDir = getUiExportDir();
29
+ await mkdir(exportDir, { recursive: true });
30
+ await purgeStaleUiExports(exportDir);
31
+ return exportDir;
32
+ };
33
+
34
+ export const buildUiExportDownloadUrl = (filePath: string) => {
35
+ return `${UI_EXPORT_URL_PREFIX}${encodeURIComponent(path.basename(filePath))}`;
36
+ };
37
+
38
+ export const buildUiExportContentDisposition = (filePath: string) => {
39
+ const fileName = path.basename(filePath);
40
+ return `attachment; filename*=UTF-8''${encodeURIComponent(fileName)}`;
41
+ };
42
+
43
+ export const purgeStaleUiExports = async (
44
+ exportDir: string = getUiExportDir(),
45
+ maxAgeMs: number = DEFAULT_EXPORT_MAX_AGE_MS,
46
+ ) => {
47
+ const entries = await readdir(exportDir, { withFileTypes: true });
48
+ const cutoff = Date.now() - maxAgeMs;
49
+
50
+ await Promise.all(
51
+ entries
52
+ .filter((entry) => entry.isFile())
53
+ .map(async (entry) => {
54
+ const filePath = path.join(exportDir, entry.name);
55
+ const metadata = await stat(filePath);
56
+ if (metadata.mtimeMs >= cutoff) {
57
+ return;
58
+ }
59
+
60
+ await rm(filePath, { force: true });
61
+ }),
62
+ );
63
+ };
64
+
65
+ export const resolveUiExportFilePathFromRequestPath = (pathname: string) => {
66
+ if (!pathname.startsWith(UI_EXPORT_URL_PREFIX)) {
67
+ return null;
68
+ }
69
+
70
+ const rawFileName = pathname.slice(UI_EXPORT_URL_PREFIX.length);
71
+ const fileName = decodeExportFileName(rawFileName);
72
+ if (!fileName || !isSafeExportFileName(fileName)) {
73
+ return null;
74
+ }
75
+
76
+ return path.join(getUiExportDir(), fileName);
77
+ };
package/src/mcp-server.ts CHANGED
@@ -59,6 +59,7 @@ server.registerTool(
59
59
  cwdFilter,
60
60
  dbPath: expandHome(args.dbPath ?? DEFAULT_DB_PATH),
61
61
  flat: args.flat ?? false,
62
+ includeCommentary: true,
62
63
  includeTools: args.includeTools ?? false,
63
64
  inputDir: expandHome(args.inputDir ?? DEFAULT_INPUT_DIR),
64
65
  optimized: args.optimized ?? false,
package/src/spiracha.ts CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  import { runExportChatsCli } from './export-chats';
4
4
  import { runExportClaudeCli } from './export-claude';
5
+ import { runUiCli } from './ui-cli';
5
6
 
6
- type SpirachaCommandKind = 'codex' | 'claude' | 'help';
7
+ type SpirachaCommandKind = 'codex' | 'claude' | 'help' | 'ui';
7
8
 
8
9
  type SpirachaInvocation = {
9
10
  kind: SpirachaCommandKind;
@@ -21,6 +22,10 @@ export const resolveSpirachaInvocation = (argv: string[]): SpirachaInvocation =>
21
22
  return { argv: rest, kind: 'codex' };
22
23
  }
23
24
 
25
+ if (firstArg === 'ui') {
26
+ return { argv: rest, kind: 'ui' };
27
+ }
28
+
24
29
  if (firstArg === '--help' || firstArg === '-h' || firstArg === 'help') {
25
30
  return { argv: [], kind: 'help' };
26
31
  }
@@ -36,10 +41,12 @@ export const getSpirachaHelpText = (): string => {
36
41
  ' spiracha',
37
42
  ' spiracha codex [Codex options]',
38
43
  ' spiracha claude [Claude options]',
44
+ ' spiracha ui [UI options]',
39
45
  '',
40
46
  'Commands:',
41
47
  ' codex Export Codex chats (default when no subcommand is provided)',
42
48
  ' claude Export a Claude transcript file or export directory',
49
+ ' ui Launch the local browser UI for browsing Codex history',
43
50
  '',
44
51
  'Aliases:',
45
52
  ' codex-chats',
@@ -48,6 +55,7 @@ export const getSpirachaHelpText = (): string => {
48
55
  'For command-specific help:',
49
56
  ' spiracha codex --help',
50
57
  ' spiracha claude --help',
58
+ ' spiracha ui --help',
51
59
  ].join('\n');
52
60
  };
53
61
 
@@ -64,6 +72,11 @@ export const runSpirachaCli = async (argv = process.argv.slice(2)): Promise<void
64
72
  return;
65
73
  }
66
74
 
75
+ if (invocation.kind === 'ui') {
76
+ await runUiCli(invocation.argv);
77
+ return;
78
+ }
79
+
67
80
  await runExportChatsCli(invocation.argv);
68
81
  };
69
82
 
package/src/ui-cli.ts ADDED
@@ -0,0 +1,310 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { access, constants } from 'node:fs/promises';
4
+ import path from 'node:path';
5
+ import { openUrlNatively } from './lib/native-open';
6
+ import { CliUsageError } from './lib/shared';
7
+ import {
8
+ buildUiExportContentDisposition,
9
+ ensureUiExportDir,
10
+ resolveUiExportFilePathFromRequestPath,
11
+ } from './lib/ui-export-files';
12
+
13
+ type UiCliOptions = {
14
+ dbPath: string | null;
15
+ host: string;
16
+ openBrowser: boolean;
17
+ port: number;
18
+ };
19
+
20
+ const DEFAULT_UI_HOST = '127.0.0.1';
21
+ const DEFAULT_UI_PORT = 3000;
22
+ const MAX_PORT_ATTEMPTS = 10;
23
+
24
+ const resolveUiDistPaths = () => {
25
+ const distRoot = path.resolve(import.meta.dir, '..', 'apps', 'ui', 'dist');
26
+ return {
27
+ clientDir: path.join(distRoot, 'client'),
28
+ serverEntryPath: path.join(distRoot, 'server', 'server.js'),
29
+ };
30
+ };
31
+
32
+ const ensureUiBuildExists = async () => {
33
+ const { clientDir, serverEntryPath } = resolveUiDistPaths();
34
+ const serverEntry = Bun.file(serverEntryPath);
35
+ const clientAssetExists = await Bun.file(path.join(clientDir, 'favicon.ico')).exists();
36
+ const serverEntryExists = await serverEntry.exists();
37
+
38
+ if (!clientAssetExists || !serverEntryExists) {
39
+ throw new Error('UI build artifacts are missing. Run `bun run build` before launching `spiracha ui`.');
40
+ }
41
+
42
+ await ensureUiExportDir();
43
+ };
44
+
45
+ const parsePort = (value: string) => {
46
+ const parsed = Number.parseInt(value, 10);
47
+ if (!Number.isInteger(parsed) || parsed <= 0 || parsed > 65535) {
48
+ throw new CliUsageError(`Invalid port: ${value}`);
49
+ }
50
+
51
+ return parsed;
52
+ };
53
+
54
+ const requireUiArgValue = (value: string | undefined, flag: string) => {
55
+ if (!value) {
56
+ throw new CliUsageError(`Missing value for ${flag}`);
57
+ }
58
+
59
+ return value;
60
+ };
61
+
62
+ const applyUiCliArg = (
63
+ argv: string[],
64
+ index: number,
65
+ state: UiCliOptions,
66
+ ): {
67
+ index: number;
68
+ state: UiCliOptions;
69
+ } => {
70
+ const arg = argv[index];
71
+
72
+ if (arg === '--no-open') {
73
+ return {
74
+ index,
75
+ state: {
76
+ ...state,
77
+ openBrowser: false,
78
+ },
79
+ };
80
+ }
81
+
82
+ if (arg === '--port') {
83
+ return {
84
+ index: index + 1,
85
+ state: {
86
+ ...state,
87
+ port: parsePort(requireUiArgValue(argv[index + 1], '--port')),
88
+ },
89
+ };
90
+ }
91
+
92
+ if (arg === '--host') {
93
+ return {
94
+ index: index + 1,
95
+ state: {
96
+ ...state,
97
+ host: requireUiArgValue(argv[index + 1], '--host'),
98
+ },
99
+ };
100
+ }
101
+
102
+ if (arg === '--db') {
103
+ return {
104
+ index: index + 1,
105
+ state: {
106
+ ...state,
107
+ dbPath: requireUiArgValue(argv[index + 1], '--db'),
108
+ },
109
+ };
110
+ }
111
+
112
+ throw new CliUsageError(`Unknown UI argument: ${arg}`);
113
+ };
114
+
115
+ export const parseUiCliArgs = (argv: string[]): UiCliOptions => {
116
+ let state: UiCliOptions = {
117
+ dbPath: null,
118
+ host: DEFAULT_UI_HOST,
119
+ openBrowser: true,
120
+ port: DEFAULT_UI_PORT,
121
+ };
122
+
123
+ for (let index = 0; index < argv.length; index += 1) {
124
+ const next = applyUiCliArg(argv, index, state);
125
+ state = next.state;
126
+ index = next.index;
127
+ }
128
+
129
+ return state;
130
+ };
131
+
132
+ export const getUiHelpText = (): string => {
133
+ return [
134
+ 'Launch the Spiracha browser UI.',
135
+ '',
136
+ 'Usage:',
137
+ ' spiracha ui [--port 3000] [--host 127.0.0.1] [--db FILE] [--no-open]',
138
+ '',
139
+ 'Options:',
140
+ ` --port HTTP port to bind (default: ${DEFAULT_UI_PORT})`,
141
+ ` --host Hostname to bind (default: ${DEFAULT_UI_HOST})`,
142
+ ' --db Override the Codex SQLite database path for the UI',
143
+ ' --no-open Do not open the browser automatically',
144
+ ' --help,-h Show this help text',
145
+ '',
146
+ 'Stop the UI with Ctrl+C.',
147
+ ].join('\n');
148
+ };
149
+
150
+ const toPublicFilePath = (clientDir: string, pathname: string) => {
151
+ const normalizedPath = pathname === '/' ? '' : pathname.replace(/^\/+/u, '');
152
+ const resolved = path.resolve(clientDir, normalizedPath);
153
+ const relative = path.relative(clientDir, resolved);
154
+
155
+ if (relative.startsWith('..') || path.isAbsolute(relative)) {
156
+ return null;
157
+ }
158
+
159
+ return resolved;
160
+ };
161
+
162
+ export const getUiStaticResponse = async (clientDir: string, pathname: string): Promise<Response | null> => {
163
+ if (pathname === '/') {
164
+ return null;
165
+ }
166
+
167
+ const exportFilePath = resolveUiExportFilePathFromRequestPath(pathname);
168
+ if (exportFilePath) {
169
+ try {
170
+ await access(exportFilePath, constants.R_OK);
171
+ } catch {
172
+ return new Response('Not Found', { status: 404 });
173
+ }
174
+
175
+ return new Response(Bun.file(exportFilePath), {
176
+ headers: {
177
+ 'cache-control': 'no-store',
178
+ 'content-disposition': buildUiExportContentDisposition(exportFilePath),
179
+ },
180
+ });
181
+ }
182
+
183
+ const targetPath = toPublicFilePath(clientDir, pathname);
184
+ if (!targetPath) {
185
+ return new Response('Forbidden', { status: 403 });
186
+ }
187
+
188
+ const file = Bun.file(targetPath);
189
+ if (!(await file.exists())) {
190
+ return null;
191
+ }
192
+
193
+ return new Response(file);
194
+ };
195
+
196
+ const openBrowserIfRequested = async (url: string, openBrowser: boolean) => {
197
+ if (!openBrowser) {
198
+ return;
199
+ }
200
+
201
+ try {
202
+ await openUrlNatively(url);
203
+ } catch (error) {
204
+ const message = error instanceof Error ? error.message : String(error);
205
+ console.warn(`Could not open the browser automatically: ${message}`);
206
+ }
207
+ };
208
+
209
+ const startUiServer = async (options: UiCliOptions) => {
210
+ await ensureUiBuildExists();
211
+ const { clientDir, serverEntryPath } = resolveUiDistPaths();
212
+ const serverModule = (await import(serverEntryPath)) as {
213
+ default: {
214
+ fetch: (request: Request) => Promise<Response> | Response;
215
+ };
216
+ };
217
+
218
+ if (options.dbPath) {
219
+ process.env.SPIRACHA_CODEX_DB = options.dbPath;
220
+ }
221
+
222
+ for (let attempt = 0; attempt < MAX_PORT_ATTEMPTS; attempt += 1) {
223
+ const port = options.port + attempt;
224
+
225
+ try {
226
+ const server = Bun.serve({
227
+ fetch: async (request) => {
228
+ const url = new URL(request.url);
229
+ const staticResponse = await getUiStaticResponse(clientDir, url.pathname);
230
+ if (staticResponse) {
231
+ return staticResponse;
232
+ }
233
+
234
+ return serverModule.default.fetch(request);
235
+ },
236
+ hostname: options.host,
237
+ idleTimeout: 30,
238
+ port,
239
+ });
240
+
241
+ return {
242
+ port,
243
+ server,
244
+ };
245
+ } catch (error) {
246
+ if (
247
+ attempt < MAX_PORT_ATTEMPTS - 1 &&
248
+ error instanceof Error &&
249
+ /address already in use|EADDRINUSE/i.test(error.message)
250
+ ) {
251
+ continue;
252
+ }
253
+
254
+ throw error;
255
+ }
256
+ }
257
+
258
+ throw new Error(`Unable to bind the UI server after ${MAX_PORT_ATTEMPTS} port attempts.`);
259
+ };
260
+
261
+ export const runUiCli = async (argv = process.argv.slice(2)) => {
262
+ try {
263
+ if (argv.includes('--help') || argv.includes('-h')) {
264
+ console.log(getUiHelpText());
265
+ return;
266
+ }
267
+ const options = parseUiCliArgs(argv);
268
+ const { port, server } = await startUiServer(options);
269
+ const url = `http://${options.host}:${port}`;
270
+
271
+ console.log(`Spiracha UI running at ${url}`);
272
+ console.log('Press Ctrl+C to stop.');
273
+
274
+ await openBrowserIfRequested(url, options.openBrowser);
275
+
276
+ let shuttingDown = false;
277
+ const shutdown = () => {
278
+ if (shuttingDown) {
279
+ return;
280
+ }
281
+ shuttingDown = true;
282
+ server.stop(true);
283
+ console.log('Spiracha UI stopped.');
284
+ process.exit(0);
285
+ };
286
+
287
+ process.once('SIGINT', shutdown);
288
+ process.once('SIGTERM', shutdown);
289
+
290
+ await new Promise<void>(() => {});
291
+ } catch (error) {
292
+ if (error instanceof CliUsageError) {
293
+ console.error(error.message);
294
+ console.error('');
295
+ console.error(getUiHelpText());
296
+ process.exit(1);
297
+ }
298
+
299
+ if (error instanceof Error) {
300
+ console.error(error.message);
301
+ process.exit(1);
302
+ }
303
+
304
+ throw error;
305
+ }
306
+ };
307
+
308
+ if (import.meta.main) {
309
+ await runUiCli();
310
+ }