-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-db-search.ts
More file actions
executable file
·1503 lines (1351 loc) · 41.8 KB
/
opencode-db-search.ts
File metadata and controls
executable file
·1503 lines (1351 loc) · 41.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bun
import { Database } from "bun:sqlite";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
// ─── Helpers ──────────────────────────────────────────────────────────────────
const BIN = "opencode-db-search";
function die(msg: string): never {
console.error(`error: ${msg}`);
process.exit(1);
}
const tty = process.stdout.isTTY ?? false;
function ts(ms: number | null | undefined): string {
if (ms == null) return "—";
return new Date(ms).toISOString().replace("T", " ").slice(0, 19);
}
function ago(ms: number | null | undefined): string {
if (ms == null) return "—";
const diff = Date.now() - ms;
if (diff < 60_000) return "just now";
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)}m ago`;
if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)}h ago`;
return `${Math.floor(diff / 86_400_000)}d ago`;
}
const ANSI_RE = /\x1b\[[0-9;]*m/g;
/** Strip ANSI escape sequences, returning the plain string. */
function stripAnsi(s: string): string {
return s.replace(ANSI_RE, "");
}
/** Visible character count (excludes ANSI escapes). */
function visibleLen(s: string): number {
return stripAnsi(s).length;
}
/**
* Truncate a string to n visible characters, preserving ANSI sequences.
* Appends "…" if truncated.
*/
function trunc(s: string, n: number): string {
if (n <= 0) return "";
// Fast path: no ANSI codes
if (!s.includes("\x1b")) {
if (s.length <= n) return s;
return s.slice(0, n - 1) + "…";
}
// Walk through the string, counting only visible characters
if (visibleLen(s) <= n) return s;
let visible = 0;
let i = 0;
let result = "";
// Use a sticky-like approach: set lastIndex and test at each position
const re = new RegExp(ANSI_RE.source, "g");
while (i < s.length && visible < n - 1) {
re.lastIndex = i;
const m = re.exec(s);
if (m && m.index === i) {
result += m[0];
i += m[0].length;
} else {
result += s[i];
visible++;
i++;
}
}
// Reset any open styles before the ellipsis
return `${result}\x1b[0m…`;
}
function pad(s: string, n: number): string {
const len = visibleLen(s);
return len >= n ? s : s + " ".repeat(n - len);
}
// ─── Table Rendering ──────────────────────────────────────────────────────────
const BOX = {
tl: "┌",
tr: "┐",
bl: "└",
br: "┘",
h: "─",
v: "│",
lm: "├",
rm: "┤",
tm: "┬",
bm: "┴",
x: "┼",
} as const;
interface Column {
key: string;
label: string;
align?: "left" | "right";
/** Maximum width this column can grow to */
max?: number;
/** Minimum width */
min?: number;
/** If true, this column absorbs remaining terminal width */
flex?: boolean;
}
function termWidth(): number {
return process.stdout.columns || 120;
}
/**
* Render a unicode box-drawing table with dynamic column widths.
*
* @param cols Column definitions
* @param rows Array of row objects (values are strings)
* @param opts Optional: { footer?: string }
*/
function table(
cols: Column[],
rows: Array<Record<string, string>>,
opts?: { footer?: string; emptyMsg?: string },
): void {
if (rows.length === 0) {
if (opts?.emptyMsg) console.log(opts.emptyMsg);
return;
}
const tw = termWidth();
// Measure natural width per column (header label + all data values)
const natural: number[] = cols.map((c) => {
let w = c.label.length;
for (const r of rows) {
const len = visibleLen(r[c.key] || "");
if (len > w) w = len;
}
// Clamp to min/max
if (c.min && w < c.min) w = c.min;
if (c.max && w > c.max) w = c.max;
return w;
});
// Total: content + padding (1 space each side) + borders (cols+1 border chars)
const fixedOverhead = cols.length + 1 + cols.length * 2; // borders + padding
const contentBudget = Math.max(cols.length, tw - fixedOverhead); // floor at 1 char/col
// First pass: assign non-flex columns their natural width
const widths: number[] = new Array(cols.length).fill(0);
let usedByFixed = 0;
const flexIndices: number[] = [];
for (let i = 0; i < cols.length; i++) {
if (cols[i].flex) {
flexIndices.push(i);
} else {
widths[i] = Math.max(1, Math.min(natural[i], contentBudget));
usedByFixed += widths[i];
}
}
// Distribute remaining space to flex columns
const remaining = Math.max(0, contentBudget - usedByFixed);
if (flexIndices.length > 0) {
const totalNatural = flexIndices.reduce((s, i) => s + natural[i], 0);
let distributed = 0;
for (let fi = 0; fi < flexIndices.length; fi++) {
const i = flexIndices[fi];
const minW = cols[i].min || 4;
const maxW = cols[i].max;
if (fi === flexIndices.length - 1) {
widths[i] = Math.max(remaining - distributed, minW);
} else {
const share =
totalNatural > 0
? Math.floor((natural[i] / totalNatural) * remaining)
: Math.floor(remaining / flexIndices.length);
widths[i] = Math.max(share, minW);
}
if (maxW && widths[i] > maxW) widths[i] = maxW;
distributed += widths[i];
}
}
// Final shrink pass: if total exceeds budget, shrink widest columns first.
// Two rounds: first respects min, then allows going below min (floor at 1).
let totalWidth = widths.reduce((a, b) => a + b, 0);
if (totalWidth > contentBudget) {
let excess = totalWidth - contentBudget;
const order = widths.map((w, i) => ({ w, i })).sort((a, b) => b.w - a.w);
// Round 1: shrink to min
for (const { i } of order) {
if (excess <= 0) break;
const floor = cols[i].min || 1;
const shrink = Math.min(excess, widths[i] - floor);
if (shrink > 0) {
widths[i] -= shrink;
excess -= shrink;
}
}
// Round 2: if still over budget, shrink below min (floor at 1)
if (excess > 0) {
order.sort((a, b) => widths[b.i] - widths[a.i]);
for (const { i } of order) {
if (excess <= 0) break;
const shrink = Math.min(excess, widths[i] - 1);
if (shrink > 0) {
widths[i] -= shrink;
excess -= shrink;
}
}
}
}
// Cell formatter: truncate + pad
function fmtCell(
value: string,
width: number,
align: "left" | "right",
): string {
const visible = visibleLen(value);
const display = visible > width ? trunc(value, width) : value;
const displayLen = visibleLen(display);
const gap = Math.max(0, width - displayLen);
return align === "right"
? " ".repeat(gap) + display
: display + " ".repeat(gap);
}
// Build lines
function hline(left: string, mid: string, right: string): string {
return left + widths.map((w) => BOX.h.repeat(w + 2)).join(mid) + right;
}
// Render
const dim = (s: string) => ansi("2", s);
console.log(dim(hline(BOX.tl, BOX.tm, BOX.tr)));
console.log(
dim(BOX.v) +
cols
.map(
(c, i) =>
` ${ansi("1", fmtCell(c.label, widths[i], c.align || "left"))} `,
)
.join(dim(BOX.v)) +
dim(BOX.v),
);
console.log(dim(hline(BOX.lm, BOX.x, BOX.rm)));
for (const r of rows) {
const cells = cols.map((c) => r[c.key] || "");
console.log(
dim(BOX.v) +
cells
.map((c, i) => ` ${fmtCell(c, widths[i], cols[i].align || "left")} `)
.join(dim(BOX.v)) +
dim(BOX.v),
);
}
console.log(dim(hline(BOX.bl, BOX.bm, BOX.br)));
if (opts?.footer) {
console.log(dim(opts.footer));
}
}
/** Print a bold section heading. */
function heading(text: string): void {
console.log(ansi("1", text));
}
/** Render key-value pairs in a clean aligned format. */
function kvBlock(pairs: Array<[string, string]>): void {
if (pairs.length === 0) return;
const maxKey = Math.max(...pairs.map(([k]) => k.length));
for (const [k, v] of pairs) {
console.log(`${ansi("1", pad(k, maxKey))} ${v}`);
}
}
/**
* Word-wrap text to fit within a given width.
* Breaks at spaces; falls back to hard-break for words exceeding width.
*/
function wordWrap(text: string, width: number): string[] {
if (width <= 0) return [text];
const words = text.split(/\s+/);
const lines: string[] = [];
let line = "";
for (const word of words) {
if (word === "") continue;
if (line.length === 0) {
// Hard-break words longer than width
if (visibleLen(word) > width) {
let remaining = word;
while (visibleLen(remaining) > width) {
lines.push(remaining.slice(0, width));
remaining = remaining.slice(width);
}
line = remaining;
} else {
line = word;
}
} else if (visibleLen(line) + 1 + visibleLen(word) <= width) {
line += " " + word;
} else {
lines.push(line);
if (visibleLen(word) > width) {
let remaining = word;
while (visibleLen(remaining) > width) {
lines.push(remaining.slice(0, width));
remaining = remaining.slice(width);
}
line = remaining;
} else {
line = word;
}
}
}
if (line) lines.push(line);
return lines.length > 0 ? lines : [""];
}
/**
* Render a search result card with box-drawing borders.
*/
function card(opts: {
id: string;
badge: string;
title: string;
subtitle?: string;
body: string[];
width?: number;
maxBodyLines?: number;
}): void {
const w = opts.width || 80;
const inner = w - 4; // "│ " + content + " │"
const maxLines = opts.maxBodyLines || 6;
const dim = (s: string) => ansi("2", s);
// Top border: ┌─ id ──...── badge ─┐
const idStr = ` ${opts.id} `;
const badgeStr = ` ${opts.badge} `;
const fill = w - 2 - idStr.length - badgeStr.length; // -2 for ┌ and ┐
const topLine =
dim(BOX.tl + BOX.h) +
ansi("1;36", idStr) +
dim(BOX.h.repeat(Math.max(1, fill))) +
dim(badgeStr) +
dim(BOX.h + BOX.tr);
console.log(topLine);
// Body line renderer
const printLine = (text: string) => {
const padded = pad(text, inner);
// Truncate if pad somehow exceeds inner width
const display = visibleLen(padded) > inner ? trunc(padded, inner) : padded;
console.log(`${dim(BOX.v)} ${display} ${dim(BOX.v)}`);
};
// Title (bold)
printLine(ansi("1", trunc(opts.title, inner)));
// Subtitle (dimmed)
if (opts.subtitle) {
printLine(ansi("2", trunc(opts.subtitle, inner)));
}
// Blank separator before body
if (opts.body.length > 0) {
printLine("");
let printed = 0;
for (const line of opts.body) {
if (printed >= maxLines) {
printLine(ansi("2", "…"));
break;
}
printLine(line);
printed++;
}
}
// Bottom border
console.log(dim(BOX.bl + BOX.h.repeat(w - 2) + BOX.br));
}
function size(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function ansi(code: string, text: string): string {
return tty ? `\x1b[${code}m${text}\x1b[0m` : text;
}
function highlight(text: string, query: string): string {
if (!query || !tty) return text;
const lower = text.toLowerCase();
const q = query.toLowerCase();
let result = "";
let i = 0;
while (i < text.length) {
const idx = lower.indexOf(q, i);
if (idx === -1) {
result += text.slice(i);
break;
}
result += text.slice(i, idx);
result += ansi("1;33", text.slice(idx, idx + query.length));
i = idx + query.length;
}
return result;
}
function escapeLike(s: string): string {
return s.replace(/%/g, "\\%").replace(/_/g, "\\_");
}
function partPreview(raw: string): string {
try {
const d = JSON.parse(raw);
if (d.type === "text" && (d.content || d.text))
return (d.content || d.text).replace(/\n/g, " ");
if (d.type === "tool") {
const title = d.state?.title || d.tool || "tool";
const out = d.state?.output
? `: ${d.state.output}`
: d.state?.input
? `: ${d.state.input}`
: "";
return `[tool:${title}]${out}`.replace(/\n/g, " ");
}
if (d.type === "reasoning" && (d.content || d.text))
return `[reasoning] ${(d.content || d.text).replace(/\n/g, " ")}`;
if (d.type === "snapshot") return "[snapshot]";
if (d.type === "subtask") return `[subtask] ${d.title || ""}`.trim();
if (d.type === "file") return `[file] ${d.path || ""}`.trim();
return `[${d.type || "unknown"}]`;
} catch {
return raw.slice(0, 80);
}
}
/** Extract a top-level field from a JSON string, returning fallback on failure. */
function jsonField(raw: string, field: string, fallback = "?"): string {
try {
const v = JSON.parse(raw)[field];
return v != null && v !== "" ? String(v) : fallback;
} catch {
return fallback;
}
}
function msgPreview(parts: Array<{ data: string }>): string {
for (const p of parts) {
const pv = partPreview(p.data);
if (pv && !pv.startsWith("[")) return pv;
}
for (const p of parts) {
const pv = partPreview(p.data);
if (pv) return pv;
}
return "";
}
/** Print data as formatted JSON. Used with `return void jsonOut(...)` for early return. */
function jsonOut(data: unknown): void {
console.log(JSON.stringify(data, null, 2));
}
/**
* Fetch parts for a message, ordered by id.
*/
function fetchParts(
db: Database,
messageId: string,
): Array<{ id: string; data: string; time_created: number }> {
return db
.query(
`SELECT id, data, time_created FROM part WHERE message_id = ? ORDER BY id ASC`,
)
.all(messageId) as any[];
}
/**
* Look up a session by exact ID. If not found, prints suggestions for
* prefix matches and returns null, or dies if no matches at all.
*/
function findSession(db: Database, id: string): any {
const row = db
.query(
`SELECT s.*, p.worktree, p.vcs, p.name as project_name
FROM session s
LEFT JOIN project p ON s.project_id = p.id
WHERE s.id = ?`,
)
.get(id);
if (row) return row;
// Suggest prefix matches
const similar = db
.query(`SELECT id, title FROM session WHERE id LIKE ? ESCAPE '\\' LIMIT 5`)
.all(`${escapeLike(id)}%`) as any[];
if (similar.length > 0) {
console.log(`Session "${id}" not found. Similar IDs:`);
for (const s of similar) console.log(` ${s.id} ${s.title}`);
return null;
}
die(`Session not found: ${id}`);
}
// ─── Query Building ───────────────────────────────────────────────────────────
class WhereBuilder {
private conditions: string[] = [];
private values: any[] = [];
add(condition: string, ...params: any[]): this {
this.conditions.push(condition);
this.values.push(...params);
return this;
}
toSql(): string {
return this.conditions.length > 0
? `WHERE ${this.conditions.join(" AND ")}`
: "";
}
get params(): any[] {
return this.values;
}
}
const VALID_SQL_ALIAS = /^[a-z_][a-z0-9_]*$/i;
function sortOrder(
sort: string,
opts?: { alias?: string; extraClauses?: Record<string, string> },
): string {
const alias = opts?.alias || "";
if (alias && !VALID_SQL_ALIAS.test(alias)) die(`Invalid SQL alias: ${alias}`);
const a = alias ? `${alias}.` : "";
const extra = opts?.extraClauses || {};
// Only allow known sort keys — unknown values fall through to default
if (sort in extra) return extra[sort];
if (sort === "created") return `${a}time_created DESC`;
return `${a}time_updated DESC`; // default for "updated" or any unknown value
}
// ─── Row Types ────────────────────────────────────────────────────────────────
/** Minimal session row returned by list/search queries. */
interface SessionRow {
id: string;
title: string;
directory: string;
project_id: string;
parent_id: string | null;
time_created: number;
time_updated: number;
time_archived: number | null;
version: string;
[key: string]: unknown; // allow extra columns from JOINs
}
/** Row from COUNT/GROUP BY aggregations. */
interface CountRow {
count: number;
[key: string]: unknown;
}
// ─── DB Resolution ────────────────────────────────────────────────────────────
function dataDir(): string {
return path.join(
process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"),
"opencode",
);
}
function listDbs(): Array<{
name: string;
path: string;
size: number;
mtime: number;
}> {
const dir = dataDir();
if (!fs.existsSync(dir)) return [];
return fs
.readdirSync(dir)
.filter((f) => f.startsWith("opencode") && f.endsWith(".db"))
.map((f) => {
const full = path.join(dir, f);
const stat = fs.statSync(full);
return { name: f, path: full, size: stat.size, mtime: stat.mtimeMs };
})
.sort((a, b) => b.mtime - a.mtime);
}
function channelFromName(name: string): string {
if (name === "opencode.db") return "latest/beta/prod";
const m = name.match(/^opencode-(.+)\.db$/);
return m ? m[1] : "unknown";
}
function resolveDb(dbFlag?: string, channelFlag?: string): string {
if (dbFlag) {
if (!fs.existsSync(dbFlag)) die(`DB file not found: ${dbFlag}`);
return dbFlag;
}
const explicit = process.env.OPENCODE_DB;
if (explicit) {
if (explicit === ":memory:") return explicit;
const resolved = path.isAbsolute(explicit)
? explicit
: path.join(dataDir(), explicit);
if (!fs.existsSync(resolved)) die(`OPENCODE_DB not found: ${resolved}`);
return resolved;
}
if (channelFlag) {
const safe = channelFlag.replace(/[^a-zA-Z0-9._-]/g, "-");
const name = ["latest", "beta", "prod"].includes(channelFlag)
? "opencode.db"
: `opencode-${safe}.db`;
const full = path.join(dataDir(), name);
if (!fs.existsSync(full)) die(`Channel DB not found: ${full}`);
return full;
}
const dbs = listDbs(); // already sorted by mtime desc
if (dbs.length === 0) die(`No opencode DB found in ${dataDir()}`);
return dbs[0].path;
}
/** Open the DB using --db / --channel flags or auto-detect. */
function openDb(): Database {
const p = resolveDb(flag("db"), flag("channel"));
console.error(`DB: ${p}`);
return new Database(p, { readonly: true });
}
// ─── Project ID Inference ─────────────────────────────────────────────────────
function inferProject(): string | null {
try {
// Check cached project ID first (matches opencode's project.ts:159-227)
const gitDir = Bun.spawnSync(["git", "rev-parse", "--git-dir"]);
if (gitDir.exitCode === 0) {
const dir = gitDir.stdout.toString().trim();
// Handle worktrees: resolve common dir
const common = Bun.spawnSync(["git", "rev-parse", "--git-common-dir"]);
const root =
common.exitCode === 0 ? common.stdout.toString().trim() : dir;
const cached = path.join(root, "opencode");
if (fs.existsSync(cached)) {
const id = fs.readFileSync(cached, "utf-8").trim();
if (id) return id;
}
}
// Fall back to root commit hash
const result = Bun.spawnSync([
"git",
"rev-list",
"--max-parents=0",
"HEAD",
]);
if (result.exitCode !== 0) return null;
const commits = result.stdout.toString().trim().split("\n").sort();
return commits[0] || null;
} catch {
return null;
}
}
// ─── Arg Parsing ──────────────────────────────────────────────────────────────
/** Flags that never consume a following positional as their value. */
const BOOLEAN_FLAGS = new Set([
"all",
"archived",
"forks",
"parts",
"json",
"fork",
"dry-run",
"help",
]);
function parseArgs(argv: string[]): {
cmd: string;
pos: string[];
flags: Record<string, string | true>;
} {
const cmd = argv[0] || "help";
const pos: string[] = [];
const flags: Record<string, string | true> = {};
for (let i = 1; i < argv.length; i++) {
const a = argv[i];
if (a.startsWith("--")) {
const eq = a.indexOf("=");
if (eq !== -1) {
flags[a.slice(2, eq)] = a.slice(eq + 1);
} else {
const key = a.slice(2);
const next = argv[i + 1];
if (BOOLEAN_FLAGS.has(key) || !next || next.startsWith("--")) {
flags[key] = true;
} else {
flags[key] = next;
i++;
}
}
} else {
pos.push(a);
}
}
return { cmd, pos, flags };
}
// ─── Subcommands ──────────────────────────────────────────────────────────────
function cmdDbs() {
const dir = dataDir();
const dbs = listDbs();
if (dbs.length === 0) {
console.log(`No opencode DBs found in ${dir}`);
return;
}
if (bool("json"))
return void jsonOut(
dbs.map((d) => ({ ...d, channel: channelFromName(d.name) })),
);
console.log(`Data dir: ${dir}\n`);
table(
[
{ key: "file", label: "File" },
{ key: "size", label: "Size", align: "right" },
{ key: "modified", label: "Modified" },
{ key: "channel", label: "Channel" },
],
dbs.map((d) => ({
file: d.name,
size: size(d.size),
modified: ts(d.mtime),
channel: channelFromName(d.name),
})),
);
}
function cmdProjects(db: Database) {
const rows = db
.query(
`SELECT p.*,
(SELECT COUNT(*) FROM session s WHERE s.project_id = p.id) as session_count,
(SELECT MAX(s.time_updated) FROM session s WHERE s.project_id = p.id) as last_session
FROM project p
ORDER BY last_session DESC NULLS LAST`,
)
.all() as any[];
if (bool("json")) return void jsonOut(rows);
table(
[
{ key: "id", label: "ID", max: 42 },
{ key: "worktree", label: "Worktree", flex: true, min: 12 },
{ key: "vcs", label: "VCS", max: 5 },
{ key: "sessions", label: "Sessions", align: "right", max: 10 },
{ key: "updated", label: "Updated", max: 12 },
],
rows.map((r: any) => ({
id: r.id,
worktree: r.worktree || "(none)",
vcs: r.vcs || "—",
sessions: String(r.session_count),
updated: ago(r.last_session),
})),
{ footer: `${rows.length} project(s)`, emptyMsg: "No projects found." },
);
}
function cmdSessions(db: Database) {
const all = bool("all");
const archived = bool("archived");
const forks = bool("forks");
const limit = num("limit", 100);
const sort = flag("sort") || "updated";
const dir = flag("directory");
const proj = flag("project");
const since = flag("since");
const before = flag("before");
const w = new WhereBuilder();
if (!all) {
w.add("s.project_id = ?", proj || inferProject() || "global");
} else if (proj) {
w.add("s.project_id = ?", proj);
}
if (!archived) w.add("s.time_archived IS NULL");
if (!forks) w.add("s.parent_id IS NULL");
if (dir) w.add("s.directory LIKE ? ESCAPE '\\'", `${escapeLike(dir)}%`);
if (since) {
const ms = new Date(since).getTime();
if (isNaN(ms)) die(`Invalid date for --since: ${since}`);
w.add("s.time_updated >= ?", ms);
}
if (before) {
const ms = new Date(before).getTime();
if (isNaN(ms)) die(`Invalid date for --before: ${before}`);
w.add("s.time_updated <= ?", ms);
}
const order = sortOrder(sort, {
alias: "s",
extraClauses: { title: "s.title ASC" },
});
const rows = db
.query(
`SELECT s.id, s.title, s.directory, s.project_id, s.parent_id,
s.time_created, s.time_updated, s.time_archived, s.version,
(SELECT COUNT(*) FROM message m WHERE m.session_id = s.id) as msg_count,
p.worktree
FROM session s
LEFT JOIN project p ON s.project_id = p.id
${w.toSql()}
ORDER BY ${order}
LIMIT ?`,
)
.all(...w.params, limit) as any[];
if (bool("json")) return void jsonOut(rows);
table(
[
{ key: "id", label: "ID", max: 32 },
{ key: "title", label: "Title", flex: true, min: 10 },
{ key: "directory", label: "Directory", flex: true, min: 10 },
{ key: "updated", label: "Updated", max: 12 },
{ key: "msgs", label: "Msgs", align: "right", max: 6 },
],
rows.map((r: any) => ({
id: r.id,
title: r.title || "(untitled)",
directory: r.directory || "",
updated: ago(r.time_updated),
msgs: String(r.msg_count),
})),
{ footer: `${rows.length} session(s)`, emptyMsg: "No sessions found." },
);
}
function cmdMessages(db: Database) {
const sid = parsed.pos[0];
if (!sid) die(`Usage: ${BIN} messages <sessionID> [options]`);
const limit = num("limit", 20);
const offset = num("offset", 0);
const role = flag("role");
const showParts = bool("parts");
const w = new WhereBuilder();
w.add("m.session_id = ?", sid);
if (role) w.add("json_extract(m.data, '$.role') = ?", role);
const rows = db
.query(
`SELECT m.id, m.data, m.time_created
FROM message m
${w.toSql()}
ORDER BY m.time_created ASC, m.id ASC
LIMIT ? OFFSET ?`,
)
.all(...w.params, limit, offset) as any[];
if (bool("json")) {
const data = showParts
? rows.map((r: any) => ({ ...r, parts: fetchParts(db, r.id) }))
: rows;
return void jsonOut(data);
}
// Prepare rows with parts for display
const prepared = rows.map((r: any) => {
const parts = fetchParts(db, r.id);
return {
...r,
parts,
role: jsonField(r.data, "role"),
preview: msgPreview(parts),
};
});
table(
[
{ key: "id", label: "ID", max: 32 },
{ key: "role", label: "Role", max: 10 },
{ key: "time", label: "Time", max: 20 },
{ key: "preview", label: "Preview", flex: true, min: 16 },
],
prepared.map((r: any) => ({
id: r.id,
role: r.role,
time: ts(r.time_created),
preview: r.preview,
})),
{ footer: `${rows.length} message(s)`, emptyMsg: "No messages found." },
);
if (showParts) {
console.log();
for (const r of prepared) {
if (r.parts.length === 0) continue;
console.log(ansi("1", `${r.id}`) + ansi("2", ` (${r.role})`));
for (const p of r.parts) {
console.log(
` ${ansi("36", pad(`[${jsonField(p.data, "type")}]`, 14))} ${trunc(partPreview(p.data), termWidth() - 18)}`,
);
}
console.log();
}
}
}
function cmdParts(db: Database) {
const sid = parsed.pos[0];
const mid = flag("message");
if (!sid && !mid)
die(`Usage: ${BIN} parts <sessionID> [--message <messageID>] [options]`);
const limit = num("limit", 50);
const typeFilter = flag("type");
const w = new WhereBuilder();
if (sid) w.add("p.session_id = ?", sid);
if (mid) w.add("p.message_id = ?", mid);
if (typeFilter) w.add("json_extract(p.data, '$.type') = ?", typeFilter);
const rows = db
.query(
`SELECT p.id, p.message_id, p.session_id, p.data, p.time_created
FROM part p
${w.toSql()}
ORDER BY p.time_created ASC, p.id ASC
LIMIT ?`,
)
.all(...w.params, limit) as any[];
if (bool("json")) return void jsonOut(rows);
table(
[
{ key: "id", label: "ID", max: 32 },
{ key: "type", label: "Type", max: 14 },
{ key: "message", label: "Message", max: 32 },
{ key: "preview", label: "Preview", flex: true, min: 16 },
],
rows.map((r: any) => ({
id: r.id,
type: jsonField(r.data, "type"),
message: r.message_id,
preview: partPreview(r.data),
})),
{ footer: `${rows.length} part(s)`, emptyMsg: "No parts found." },
);
}
function cmdSearch(db: Database) {
const query = parsed.pos[0];
if (!query) die(`Usage: ${BIN} search <query> [options]`);
const scope = flag("scope") || "all";
const proj = flag("project");
const sid = flag("session");
const limit = num("limit", 20);
const sort = flag("sort") || "updated";
const width = num("width", 80);
const order = sortOrder(sort, { alias: "s" });
const pattern = `%${escapeLike(query)}%`;
interface SearchResult {
session: string;
title: string;
directory: string;
match: string;
preview: string;
updated: number;
created: number;
}
const results: SearchResult[] = [];
/** Apply common project/session filters to a WhereBuilder. */
function addScopeFilters(w: WhereBuilder): void {
if (proj) w.add("s.project_id = ?", proj);