Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(transcript-index): pure extract/search core

enes9de96f23c6a8
diff --git a/src/main/transcript-index-core.test.ts b/src/main/transcript-index-core.test.ts
new file mode 100644
index 000000000..aefa6a331
--- /dev/null
+++ b/src/main/transcript-index-core.test.ts
@@ -0,0 +1,67 @@
+import { describe, expect, it } from 'vitest'
+import {
+ extractEntryFields,
+ makeSnippet,
+ searchEntries,
+ INDEX_TEXT_CAP_BYTES,
+ type TranscriptIndexEntry
+} from './transcript-index-core'
+
+const raw = [
+ JSON.stringify({ type: 'user', cwd: '/Users/me/proj', message: { content: 'fix the tmux config please' } }),
+ JSON.stringify({ type: 'assistant', cwd: '/Users/me/proj', message: { content: [{ type: 'text', text: 'editing tmux.conf now' }] } })
+].join('\n')
+
+describe('extractEntryFields', () => {
+ it('pulls cwd, first-user-message title, and concatenated text', () => {
+ const f = extractEntryFields(raw)
+ expect(f.cwd).toBe('/Users/me/proj')
+ expect(f.title).toBe('fix the tmux config please')
+ expect(f.text).toContain('fix the tmux config')
+ expect(f.text).toContain('editing tmux.conf now')
+ })
+
+ it('trims a long title to ~80 chars and caps text at INDEX_TEXT_CAP_BYTES', () => {
+ const long = 'x'.repeat(500)
+ const big = Array.from({ length: 5000 }, (_, i) =>
+ JSON.stringify({ type: 'user', cwd: '/c', message: { content: long } })
+ ).join('\n')
+ const f = extractEntryFields(big)
+ expect(f.title.length).toBeLessThanOrEqual(80)
+ expect(f.text.length).toBeLessThanOrEqual(INDEX_TEXT_CAP_BYTES)
+ })
+
+ it('returns empty fields for unparseable input', () => {
+ expect(extractEntryFields('garbage\n{bad json')).toEqual({ cwd: '', title: '', text: '' })
+ })
+})
+
+describe('makeSnippet', () => {
+ it('centers on the match and trims', () => {
+ const s = makeSnippet('a'.repeat(300) + 'NEEDLE' + 'b'.repeat(300), 'needle')
+ expect(s.toLowerCase()).toContain('needle')
+ expect(s.length).toBeLessThanOrEqual(170)
+ })
+})
+
+describe('searchEntries', () => {
+ const entries: TranscriptIndexEntry[] = [
+ { sessionId: 's1', transcriptPath: '/p/s1.jsonl', cwd: '/Users/me/alpha', mtime: 100, title: 'old tmux work', text: 'tmux mouse mode' },
+ { sessionId: 's2', transcriptPath: '/p/s2.jsonl', cwd: '/Users/me/beta', mtime: 200, title: 'recent', text: 'something about TMUX clipboard' }
+ ]
+
+ it('matches title+text case-insensitively, newest first, capped', () => {
+ const hits = searchEntries(entries, 'tmux', 20)
+ expect(hits.map((h) => h.sessionId)).toEqual(['s2', 's1'])
+ expect(hits[0].projectLabel).toBe('beta')
+ expect(hits[0].snippet.toLowerCase()).toContain('tmux')
+ })
+
+ it('returns nothing for queries shorter than 2 chars', () => {
+ expect(searchEntries(entries, 't')).toEqual([])
+ })
+
+ it('respects the limit', () => {
+ expect(searchEntries(entries, 'tmux', 1)).toHaveLength(1)
+ })
+})
diff --git a/src/main/transcript-index-core.ts b/src/main/transcript-index-core.ts
new file mode 100644
index 000000000..329096721
--- /dev/null
+++ b/src/main/transcript-index-core.ts
@@ -0,0 +1,80 @@
+// Pure helpers for the transcript index: extract searchable fields from a transcript's raw
+// JSONL text, build snippets, and rank sessions by a query. No filesystem access — the
+// service layer (transcript-index.ts) reads files and calls these. Reuses the same JSONL
+// parser as the single-session reader so indexed text stays consistent with the find-bar.
+import { parseTranscriptLines } from './transcript-reader'
+
+export const INDEX_TEXT_CAP_BYTES = 200 * 1024
+
+export interface TranscriptIndexEntry {
+ sessionId: string
+ transcriptPath: string
+ cwd: string
+ mtime: number
+ title: string
+ text: string
+}
+
+export interface TranscriptHit {
+ sessionId: string
+ title: string
+ snippet: string
+ cwd: string
+ projectLabel: string
+ mtime: number
+}
+
+// Read `cwd` from the first JSONL line that carries it (every Claude line does); reliable,
+// unlike decoding the dashed directory name.
+function firstCwd(rawText: string): string {
+ for (const raw of rawText.split('\n')) {
+ if (!raw.trim()) continue
+ try {
+ const o = JSON.parse(raw) as { cwd?: unknown }
+ if (typeof o.cwd === 'string' && o.cwd) return o.cwd
+ } catch {
+ /* skip */
+ }
+ }
+ return ''
+}
+
+export function extractEntryFields(rawText: string): { cwd: string; title: string; text: string } {
+ const lines = parseTranscriptLines(rawText)
+ const firstUser = lines.find((l) => l.role === 'user')
+ const title = (firstUser?.text ?? '').replace(/\s+/g, ' ').trim().slice(0, 80)
+ let text = lines.map((l) => l.text).join('\n')
+ if (text.length > INDEX_TEXT_CAP_BYTES) text = text.slice(text.length - INDEX_TEXT_CAP_BYTES)
+ return { cwd: firstCwd(rawText), title, text }
+}
+
+export function makeSnippet(text: string, query: string): string {
+ const i = text.toLowerCase().indexOf(query.toLowerCase())
+ if (i < 0) return text.slice(0, 160).replace(/\s+/g, ' ').trim()
+ const start = Math.max(0, i - 60)
+ return (
+ (start > 0 ? '…' : '') +
+ text.slice(start, start + 160).replace(/\s+/g, ' ').trim()
+ )
+}
+
+export function searchEntries(
+ entries: TranscriptIndexEntry[],
+ query: string,
+ limit = 20
+): TranscriptHit[] {
+ const q = query.trim().toLowerCase()
+ if (q.length < 2) return []
+ return entries
+ .filter((e) => e.title.toLowerCase().includes(q) || e.text.toLowerCase().includes(q))
+ .sort((a, b) => b.mtime - a.mtime)
+ .slice(0, limit)
+ .map((e) => ({
+ sessionId: e.sessionId,
+ title: e.title || e.sessionId,
+ snippet: makeSnippet(`${e.title}\n${e.text}`, q),
+ cwd: e.cwd,
+ projectLabel: e.cwd ? e.cwd.split('/').filter(Boolean).pop() ?? e.cwd : '',
+ mtime: e.mtime
+ }))
+}