@zzusp/ccsm 1.0.1 → 1.0.2

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 (70) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +236 -236
  3. package/bin/cli.mjs +52 -52
  4. package/dist/assets/{DiskUsage-CKhggLs5.js → DiskUsage-BY6XwffG.js} +2 -2
  5. package/dist/assets/DiskUsage-BY6XwffG.js.map +1 -0
  6. package/dist/assets/{ImportPage-wge4VhZ-.js → ImportPage-Cwq5bx7G.js} +2 -2
  7. package/dist/assets/ImportPage-Cwq5bx7G.js.map +1 -0
  8. package/dist/assets/MarkdownContent-BFu7Nkk_.js +2 -0
  9. package/dist/assets/MarkdownContent-BFu7Nkk_.js.map +1 -0
  10. package/dist/assets/{ProjectMemory-Q4XX40j_.js → ProjectMemory-CcE3KbUK.js} +2 -2
  11. package/dist/assets/ProjectMemory-CcE3KbUK.js.map +1 -0
  12. package/dist/assets/index-CrWxV6sb.css +1 -0
  13. package/dist/assets/index-DTbWl1jb.js +11 -0
  14. package/dist/assets/index-DTbWl1jb.js.map +1 -0
  15. package/dist/assets/markdown-Bag5rX3T.js +30 -0
  16. package/dist/assets/markdown-Bag5rX3T.js.map +1 -0
  17. package/dist/index.html +26 -26
  18. package/package.json +85 -83
  19. package/server/index.ts +130 -130
  20. package/server/lib/active-sessions.test.ts +119 -119
  21. package/server/lib/active-sessions.ts +95 -95
  22. package/server/lib/bundle.test.ts +182 -182
  23. package/server/lib/bundle.ts +86 -86
  24. package/server/lib/claude-paths.test.ts +126 -126
  25. package/server/lib/claude-paths.ts +43 -43
  26. package/server/lib/cleanup-suggestions.ts +131 -131
  27. package/server/lib/constants.ts +8 -8
  28. package/server/lib/delete-project.ts +100 -100
  29. package/server/lib/delete.test.ts +244 -244
  30. package/server/lib/delete.ts +192 -192
  31. package/server/lib/disk-usage.ts +81 -81
  32. package/server/lib/encode-cwd.ts +24 -24
  33. package/server/lib/export-bundle.ts +236 -236
  34. package/server/lib/export-import-bundle.test.ts +337 -337
  35. package/server/lib/fs-size.ts +38 -38
  36. package/server/lib/import-bundle.ts +488 -488
  37. package/server/lib/load-memory.ts +120 -120
  38. package/server/lib/load-session.ts +209 -209
  39. package/server/lib/modified-files.test.ts +280 -280
  40. package/server/lib/modified-files.ts +228 -228
  41. package/server/lib/open-folder.ts +47 -47
  42. package/server/lib/parse-jsonl.ts +160 -139
  43. package/server/lib/port.ts +23 -23
  44. package/server/lib/safe-id.test.ts +41 -41
  45. package/server/lib/safe-id.ts +6 -6
  46. package/server/lib/safe-remove.test.ts +73 -73
  47. package/server/lib/safe-remove.ts +25 -25
  48. package/server/lib/scan.ts +289 -286
  49. package/server/lib/search-all.ts +130 -130
  50. package/server/lib/search-session.ts +203 -203
  51. package/server/lib/system-tags.ts +20 -20
  52. package/server/lib/update.ts +67 -67
  53. package/server/lib/version.test.ts +39 -39
  54. package/server/lib/version.ts +117 -117
  55. package/server/routes/disk-cleanup.ts +54 -54
  56. package/server/routes/disk.ts +9 -9
  57. package/server/routes/import.ts +87 -87
  58. package/server/routes/projects.ts +104 -104
  59. package/server/routes/search.ts +79 -79
  60. package/server/routes/sessions.ts +130 -130
  61. package/server/routes/version.ts +34 -34
  62. package/server/types.ts +1 -1
  63. package/shared/constants.ts +7 -7
  64. package/shared/types.ts +513 -511
  65. package/dist/assets/DiskUsage-CKhggLs5.js.map +0 -1
  66. package/dist/assets/ImportPage-wge4VhZ-.js.map +0 -1
  67. package/dist/assets/ProjectMemory-Q4XX40j_.js.map +0 -1
  68. package/dist/assets/index-7aMrnHJG.js +0 -7
  69. package/dist/assets/index-7aMrnHJG.js.map +0 -1
  70. package/dist/assets/index-BOeI_J4B.css +0 -1
@@ -1,87 +1,87 @@
1
- import { Hono } from 'hono';
2
- import { commitImport, ImportError, previewImport } from '../lib/import-bundle.ts';
3
- import type { ImportCollisionPolicy } from '../types.ts';
4
-
5
- export const importRoute = new Hono();
6
-
7
- const POLICIES: ReadonlySet<ImportCollisionPolicy> = new Set([
8
- 'skip',
9
- 'overwrite-if-newer',
10
- 'keep-both',
11
- ]);
12
-
13
- importRoute.post('/preview', async (c) => {
14
- if (!isAcceptableOrigin(c.req.header('origin'))) {
15
- return c.json({ error: 'origin not allowed' }, 403);
16
- }
17
- let body: { bundleDir?: unknown; targetCwd?: unknown; collisionPolicy?: unknown };
18
- try {
19
- body = await c.req.json();
20
- } catch {
21
- return c.json({ error: 'invalid JSON body' }, 400);
22
- }
23
- if (typeof body.bundleDir !== 'string' || body.bundleDir.trim() === '') {
24
- return c.json({ error: 'bundleDir is required' }, 400);
25
- }
26
- const targetCwd =
27
- typeof body.targetCwd === 'string' && body.targetCwd.trim() !== '' ? body.targetCwd : undefined;
28
-
29
- try {
30
- const result = await previewImport({
31
- bundleDir: body.bundleDir,
32
- targetCwd,
33
- collisionPolicy: normalizePolicy(body.collisionPolicy),
34
- });
35
- return c.json(result);
36
- } catch (err) {
37
- if (err instanceof ImportError) return c.json({ error: err.message }, 400);
38
- throw err;
39
- }
40
- });
41
-
42
- importRoute.post('/', async (c) => {
43
- if (!isAcceptableOrigin(c.req.header('origin'))) {
44
- return c.json({ error: 'origin not allowed' }, 403);
45
- }
46
- let body: { bundleDir?: unknown; targetCwd?: unknown; collisionPolicy?: unknown };
47
- try {
48
- body = await c.req.json();
49
- } catch {
50
- return c.json({ error: 'invalid JSON body' }, 400);
51
- }
52
- if (typeof body.bundleDir !== 'string' || body.bundleDir.trim() === '') {
53
- return c.json({ error: 'bundleDir is required' }, 400);
54
- }
55
- if (typeof body.targetCwd !== 'string' || body.targetCwd.trim() === '') {
56
- return c.json({ error: 'targetCwd is required' }, 400);
57
- }
58
-
59
- try {
60
- const result = await commitImport({
61
- bundleDir: body.bundleDir,
62
- targetCwd: body.targetCwd,
63
- collisionPolicy: normalizePolicy(body.collisionPolicy),
64
- });
65
- return c.json(result);
66
- } catch (err) {
67
- if (err instanceof ImportError) return c.json({ error: err.message }, 400);
68
- throw err;
69
- }
70
- });
71
-
72
- function normalizePolicy(raw: unknown): ImportCollisionPolicy {
73
- return typeof raw === 'string' && POLICIES.has(raw as ImportCollisionPolicy)
74
- ? (raw as ImportCollisionPolicy)
75
- : 'skip';
76
- }
77
-
78
- function isAcceptableOrigin(origin: string | undefined): boolean {
79
- if (!origin) return false;
80
- try {
81
- const url = new URL(origin);
82
- if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
83
- return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
84
- } catch {
85
- return false;
86
- }
87
- }
1
+ import { Hono } from 'hono';
2
+ import { commitImport, ImportError, previewImport } from '../lib/import-bundle.ts';
3
+ import type { ImportCollisionPolicy } from '../types.ts';
4
+
5
+ export const importRoute = new Hono();
6
+
7
+ const POLICIES: ReadonlySet<ImportCollisionPolicy> = new Set([
8
+ 'skip',
9
+ 'overwrite-if-newer',
10
+ 'keep-both',
11
+ ]);
12
+
13
+ importRoute.post('/preview', async (c) => {
14
+ if (!isAcceptableOrigin(c.req.header('origin'))) {
15
+ return c.json({ error: 'origin not allowed' }, 403);
16
+ }
17
+ let body: { bundleDir?: unknown; targetCwd?: unknown; collisionPolicy?: unknown };
18
+ try {
19
+ body = await c.req.json();
20
+ } catch {
21
+ return c.json({ error: 'invalid JSON body' }, 400);
22
+ }
23
+ if (typeof body.bundleDir !== 'string' || body.bundleDir.trim() === '') {
24
+ return c.json({ error: 'bundleDir is required' }, 400);
25
+ }
26
+ const targetCwd =
27
+ typeof body.targetCwd === 'string' && body.targetCwd.trim() !== '' ? body.targetCwd : undefined;
28
+
29
+ try {
30
+ const result = await previewImport({
31
+ bundleDir: body.bundleDir,
32
+ targetCwd,
33
+ collisionPolicy: normalizePolicy(body.collisionPolicy),
34
+ });
35
+ return c.json(result);
36
+ } catch (err) {
37
+ if (err instanceof ImportError) return c.json({ error: err.message }, 400);
38
+ throw err;
39
+ }
40
+ });
41
+
42
+ importRoute.post('/', async (c) => {
43
+ if (!isAcceptableOrigin(c.req.header('origin'))) {
44
+ return c.json({ error: 'origin not allowed' }, 403);
45
+ }
46
+ let body: { bundleDir?: unknown; targetCwd?: unknown; collisionPolicy?: unknown };
47
+ try {
48
+ body = await c.req.json();
49
+ } catch {
50
+ return c.json({ error: 'invalid JSON body' }, 400);
51
+ }
52
+ if (typeof body.bundleDir !== 'string' || body.bundleDir.trim() === '') {
53
+ return c.json({ error: 'bundleDir is required' }, 400);
54
+ }
55
+ if (typeof body.targetCwd !== 'string' || body.targetCwd.trim() === '') {
56
+ return c.json({ error: 'targetCwd is required' }, 400);
57
+ }
58
+
59
+ try {
60
+ const result = await commitImport({
61
+ bundleDir: body.bundleDir,
62
+ targetCwd: body.targetCwd,
63
+ collisionPolicy: normalizePolicy(body.collisionPolicy),
64
+ });
65
+ return c.json(result);
66
+ } catch (err) {
67
+ if (err instanceof ImportError) return c.json({ error: err.message }, 400);
68
+ throw err;
69
+ }
70
+ });
71
+
72
+ function normalizePolicy(raw: unknown): ImportCollisionPolicy {
73
+ return typeof raw === 'string' && POLICIES.has(raw as ImportCollisionPolicy)
74
+ ? (raw as ImportCollisionPolicy)
75
+ : 'skip';
76
+ }
77
+
78
+ function isAcceptableOrigin(origin: string | undefined): boolean {
79
+ if (!origin) return false;
80
+ try {
81
+ const url = new URL(origin);
82
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
83
+ return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
@@ -1,104 +1,104 @@
1
- import { Hono } from 'hono';
2
- import { deleteProject } from '../lib/delete-project.ts';
3
- import { ExportError, exportBundle } from '../lib/export-bundle.ts';
4
- import { loadProjectMemory } from '../lib/load-memory.ts';
5
- import { openFolder } from '../lib/open-folder.ts';
6
- import { isSafeId } from '../lib/safe-id.ts';
7
- import { listProjects, listSessionsForProject, resolveProjectCwd } from '../lib/scan.ts';
8
-
9
- export const projectsRoute = new Hono();
10
-
11
- projectsRoute.get('/', async (c) => {
12
- const projects = await listProjects();
13
- return c.json(projects);
14
- });
15
-
16
- projectsRoute.get('/:id/sessions', async (c) => {
17
- const id = c.req.param('id');
18
- if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
19
- const sessions = await listSessionsForProject(id);
20
- return c.json(sessions);
21
- });
22
-
23
- projectsRoute.get('/:id/memory', async (c) => {
24
- const id = c.req.param('id');
25
- if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
26
- const memory = await loadProjectMemory(id);
27
- return c.json(memory);
28
- });
29
-
30
- projectsRoute.post('/:id/reveal', async (c) => {
31
- if (!isAcceptableOrigin(c.req.header('origin'))) {
32
- return c.json({ error: 'origin not allowed' }, 403);
33
- }
34
- const id = c.req.param('id');
35
- if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
36
-
37
- const cwd = await resolveProjectCwd(id);
38
- if (!cwd) return c.json({ error: 'project not found' }, 404);
39
- if (!cwd.resolved) return c.json({ error: 'directory missing on disk' }, 404);
40
-
41
- const result = openFolder(cwd.decoded);
42
- if (!result.ok) return c.json({ error: result.error ?? 'failed to open folder' }, 500);
43
-
44
- return c.json({ ok: true, path: cwd.decoded });
45
- });
46
-
47
- projectsRoute.post('/:id/export', async (c) => {
48
- if (!isAcceptableOrigin(c.req.header('origin'))) {
49
- return c.json({ error: 'origin not allowed' }, 403);
50
- }
51
- const id = c.req.param('id');
52
- if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
53
-
54
- let body: { sessionIds?: unknown; destDir?: unknown };
55
- try {
56
- body = await c.req.json();
57
- } catch {
58
- return c.json({ error: 'invalid JSON body' }, 400);
59
- }
60
- if (typeof body.destDir !== 'string' || body.destDir.trim() === '') {
61
- return c.json({ error: 'destDir is required' }, 400);
62
- }
63
-
64
- let sessionIds: string[] | 'all';
65
- if (body.sessionIds === undefined || body.sessionIds === 'all') {
66
- sessionIds = 'all';
67
- } else if (
68
- Array.isArray(body.sessionIds) &&
69
- body.sessionIds.every((s) => typeof s === 'string')
70
- ) {
71
- sessionIds = body.sessionIds as string[];
72
- } else {
73
- return c.json({ error: 'sessionIds must be an array of strings or "all"' }, 400);
74
- }
75
-
76
- try {
77
- const result = await exportBundle(id, sessionIds, body.destDir);
78
- return c.json(result);
79
- } catch (err) {
80
- if (err instanceof ExportError) return c.json({ error: err.message }, 400);
81
- throw err;
82
- }
83
- });
84
-
85
- projectsRoute.delete('/:id', async (c) => {
86
- if (!isAcceptableOrigin(c.req.header('origin'))) {
87
- return c.json({ error: 'origin not allowed' }, 403);
88
- }
89
- const id = c.req.param('id');
90
- if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
91
- const result = await deleteProject(id);
92
- return c.json(result);
93
- });
94
-
95
- function isAcceptableOrigin(origin: string | undefined): boolean {
96
- if (!origin) return false;
97
- try {
98
- const url = new URL(origin);
99
- if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
100
- return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
101
- } catch {
102
- return false;
103
- }
104
- }
1
+ import { Hono } from 'hono';
2
+ import { deleteProject } from '../lib/delete-project.ts';
3
+ import { ExportError, exportBundle } from '../lib/export-bundle.ts';
4
+ import { loadProjectMemory } from '../lib/load-memory.ts';
5
+ import { openFolder } from '../lib/open-folder.ts';
6
+ import { isSafeId } from '../lib/safe-id.ts';
7
+ import { listProjects, listSessionsForProject, resolveProjectCwd } from '../lib/scan.ts';
8
+
9
+ export const projectsRoute = new Hono();
10
+
11
+ projectsRoute.get('/', async (c) => {
12
+ const projects = await listProjects();
13
+ return c.json(projects);
14
+ });
15
+
16
+ projectsRoute.get('/:id/sessions', async (c) => {
17
+ const id = c.req.param('id');
18
+ if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
19
+ const sessions = await listSessionsForProject(id);
20
+ return c.json(sessions);
21
+ });
22
+
23
+ projectsRoute.get('/:id/memory', async (c) => {
24
+ const id = c.req.param('id');
25
+ if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
26
+ const memory = await loadProjectMemory(id);
27
+ return c.json(memory);
28
+ });
29
+
30
+ projectsRoute.post('/:id/reveal', async (c) => {
31
+ if (!isAcceptableOrigin(c.req.header('origin'))) {
32
+ return c.json({ error: 'origin not allowed' }, 403);
33
+ }
34
+ const id = c.req.param('id');
35
+ if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
36
+
37
+ const cwd = await resolveProjectCwd(id);
38
+ if (!cwd) return c.json({ error: 'project not found' }, 404);
39
+ if (!cwd.resolved) return c.json({ error: 'directory missing on disk' }, 404);
40
+
41
+ const result = openFolder(cwd.decoded);
42
+ if (!result.ok) return c.json({ error: result.error ?? 'failed to open folder' }, 500);
43
+
44
+ return c.json({ ok: true, path: cwd.decoded });
45
+ });
46
+
47
+ projectsRoute.post('/:id/export', async (c) => {
48
+ if (!isAcceptableOrigin(c.req.header('origin'))) {
49
+ return c.json({ error: 'origin not allowed' }, 403);
50
+ }
51
+ const id = c.req.param('id');
52
+ if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
53
+
54
+ let body: { sessionIds?: unknown; destDir?: unknown };
55
+ try {
56
+ body = await c.req.json();
57
+ } catch {
58
+ return c.json({ error: 'invalid JSON body' }, 400);
59
+ }
60
+ if (typeof body.destDir !== 'string' || body.destDir.trim() === '') {
61
+ return c.json({ error: 'destDir is required' }, 400);
62
+ }
63
+
64
+ let sessionIds: string[] | 'all';
65
+ if (body.sessionIds === undefined || body.sessionIds === 'all') {
66
+ sessionIds = 'all';
67
+ } else if (
68
+ Array.isArray(body.sessionIds) &&
69
+ body.sessionIds.every((s) => typeof s === 'string')
70
+ ) {
71
+ sessionIds = body.sessionIds as string[];
72
+ } else {
73
+ return c.json({ error: 'sessionIds must be an array of strings or "all"' }, 400);
74
+ }
75
+
76
+ try {
77
+ const result = await exportBundle(id, sessionIds, body.destDir);
78
+ return c.json(result);
79
+ } catch (err) {
80
+ if (err instanceof ExportError) return c.json({ error: err.message }, 400);
81
+ throw err;
82
+ }
83
+ });
84
+
85
+ projectsRoute.delete('/:id', async (c) => {
86
+ if (!isAcceptableOrigin(c.req.header('origin'))) {
87
+ return c.json({ error: 'origin not allowed' }, 403);
88
+ }
89
+ const id = c.req.param('id');
90
+ if (!isSafeId(id)) return c.json({ error: 'invalid project id' }, 400);
91
+ const result = await deleteProject(id);
92
+ return c.json(result);
93
+ });
94
+
95
+ function isAcceptableOrigin(origin: string | undefined): boolean {
96
+ if (!origin) return false;
97
+ try {
98
+ const url = new URL(origin);
99
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
100
+ return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
101
+ } catch {
102
+ return false;
103
+ }
104
+ }
@@ -1,79 +1,79 @@
1
- import { Hono } from 'hono';
2
- import { stream } from 'hono/streaming';
3
- import { searchAll, type SearchAllOpts } from '../lib/search-all.ts';
4
- import type { SearchBlockKind } from '../types.ts';
5
-
6
- export const searchRoute = new Hono();
7
-
8
- const ALL_KINDS: ReadonlyArray<SearchBlockKind> = [
9
- 'text',
10
- 'tool_use',
11
- 'tool_result',
12
- 'thinking',
13
- ];
14
- const DEFAULT_INCLUDE: ReadonlySet<SearchBlockKind> = new Set([
15
- 'text',
16
- 'tool_use',
17
- 'thinking',
18
- ]);
19
-
20
- const Q_MIN = 2;
21
- const Q_MAX = 200;
22
- const PER_SESSION_MIN = 1;
23
- const PER_SESSION_MAX = 20;
24
- const PER_SESSION_DEFAULT = 5;
25
- const MAX_SESSIONS_MIN = 1;
26
- const MAX_SESSIONS_MAX = 200;
27
- const MAX_SESSIONS_DEFAULT = 50;
28
-
29
- searchRoute.get('/', async (c) => {
30
- const q = c.req.query('q') ?? '';
31
- if (q.length < Q_MIN) {
32
- return c.json({ error: `q must be at least ${Q_MIN} characters` }, 400);
33
- }
34
- if (q.length > Q_MAX) {
35
- return c.json({ error: `q exceeds max length ${Q_MAX}` }, 400);
36
- }
37
-
38
- const perSession = clampInt(c.req.query('perSession'), PER_SESSION_MIN, PER_SESSION_MAX, PER_SESSION_DEFAULT);
39
- const maxSessions = clampInt(c.req.query('maxSessions'), MAX_SESSIONS_MIN, MAX_SESSIONS_MAX, MAX_SESSIONS_DEFAULT);
40
- const include = parseInclude(c.req.query('include'));
41
-
42
- const opts: SearchAllOpts = { query: q, include, perSession, maxSessions };
43
-
44
- c.header('Content-Type', 'application/x-ndjson; charset=utf-8');
45
- c.header('Cache-Control', 'no-store');
46
- c.header('X-Accel-Buffering', 'no');
47
-
48
- return stream(c, async (s) => {
49
- let aborted = false;
50
- s.onAbort(() => {
51
- aborted = true;
52
- });
53
- for await (const event of searchAll(opts)) {
54
- if (aborted) return;
55
- await s.write(JSON.stringify(event) + '\n');
56
- }
57
- });
58
- });
59
-
60
- function clampInt(raw: string | undefined, min: number, max: number, fallback: number): number {
61
- if (raw === undefined) return fallback;
62
- const n = Number(raw);
63
- if (!Number.isFinite(n) || !Number.isInteger(n)) return fallback;
64
- if (n < min) return min;
65
- if (n > max) return max;
66
- return n;
67
- }
68
-
69
- function parseInclude(raw: string | undefined): ReadonlySet<SearchBlockKind> {
70
- if (!raw) return DEFAULT_INCLUDE;
71
- const parts = raw.split(',').map((s) => s.trim()).filter(Boolean);
72
- const set = new Set<SearchBlockKind>();
73
- for (const p of parts) {
74
- if ((ALL_KINDS as readonly string[]).includes(p)) {
75
- set.add(p as SearchBlockKind);
76
- }
77
- }
78
- return set.size > 0 ? set : DEFAULT_INCLUDE;
79
- }
1
+ import { Hono } from 'hono';
2
+ import { stream } from 'hono/streaming';
3
+ import { searchAll, type SearchAllOpts } from '../lib/search-all.ts';
4
+ import type { SearchBlockKind } from '../types.ts';
5
+
6
+ export const searchRoute = new Hono();
7
+
8
+ const ALL_KINDS: ReadonlyArray<SearchBlockKind> = [
9
+ 'text',
10
+ 'tool_use',
11
+ 'tool_result',
12
+ 'thinking',
13
+ ];
14
+ const DEFAULT_INCLUDE: ReadonlySet<SearchBlockKind> = new Set([
15
+ 'text',
16
+ 'tool_use',
17
+ 'thinking',
18
+ ]);
19
+
20
+ const Q_MIN = 2;
21
+ const Q_MAX = 200;
22
+ const PER_SESSION_MIN = 1;
23
+ const PER_SESSION_MAX = 20;
24
+ const PER_SESSION_DEFAULT = 5;
25
+ const MAX_SESSIONS_MIN = 1;
26
+ const MAX_SESSIONS_MAX = 200;
27
+ const MAX_SESSIONS_DEFAULT = 50;
28
+
29
+ searchRoute.get('/', async (c) => {
30
+ const q = c.req.query('q') ?? '';
31
+ if (q.length < Q_MIN) {
32
+ return c.json({ error: `q must be at least ${Q_MIN} characters` }, 400);
33
+ }
34
+ if (q.length > Q_MAX) {
35
+ return c.json({ error: `q exceeds max length ${Q_MAX}` }, 400);
36
+ }
37
+
38
+ const perSession = clampInt(c.req.query('perSession'), PER_SESSION_MIN, PER_SESSION_MAX, PER_SESSION_DEFAULT);
39
+ const maxSessions = clampInt(c.req.query('maxSessions'), MAX_SESSIONS_MIN, MAX_SESSIONS_MAX, MAX_SESSIONS_DEFAULT);
40
+ const include = parseInclude(c.req.query('include'));
41
+
42
+ const opts: SearchAllOpts = { query: q, include, perSession, maxSessions };
43
+
44
+ c.header('Content-Type', 'application/x-ndjson; charset=utf-8');
45
+ c.header('Cache-Control', 'no-store');
46
+ c.header('X-Accel-Buffering', 'no');
47
+
48
+ return stream(c, async (s) => {
49
+ let aborted = false;
50
+ s.onAbort(() => {
51
+ aborted = true;
52
+ });
53
+ for await (const event of searchAll(opts)) {
54
+ if (aborted) return;
55
+ await s.write(JSON.stringify(event) + '\n');
56
+ }
57
+ });
58
+ });
59
+
60
+ function clampInt(raw: string | undefined, min: number, max: number, fallback: number): number {
61
+ if (raw === undefined) return fallback;
62
+ const n = Number(raw);
63
+ if (!Number.isFinite(n) || !Number.isInteger(n)) return fallback;
64
+ if (n < min) return min;
65
+ if (n > max) return max;
66
+ return n;
67
+ }
68
+
69
+ function parseInclude(raw: string | undefined): ReadonlySet<SearchBlockKind> {
70
+ if (!raw) return DEFAULT_INCLUDE;
71
+ const parts = raw.split(',').map((s) => s.trim()).filter(Boolean);
72
+ const set = new Set<SearchBlockKind>();
73
+ for (const p of parts) {
74
+ if ((ALL_KINDS as readonly string[]).includes(p)) {
75
+ set.add(p as SearchBlockKind);
76
+ }
77
+ }
78
+ return set.size > 0 ? set : DEFAULT_INCLUDE;
79
+ }