@nandoblanco/opencode-local-search 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +243 -2
- package/assets/screenshot.png +0 -0
- package/cli.js +269 -0
- package/package.json +67 -4
- package/src/index.ts +7 -0
- package/src/search.ts +1277 -0
- package/src/server.ts +8 -0
- package/src/tui.ts +1570 -0
package/src/tui.ts
ADDED
|
@@ -0,0 +1,1570 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import type { TuiPluginContext } from "@opencode-ai/plugin/tui";
|
|
4
|
+
import { TextAttributes } from "@opentui/core";
|
|
5
|
+
import {
|
|
6
|
+
createComponent,
|
|
7
|
+
createElement,
|
|
8
|
+
createTextNode,
|
|
9
|
+
effect,
|
|
10
|
+
insert,
|
|
11
|
+
insertNode,
|
|
12
|
+
setProp,
|
|
13
|
+
use,
|
|
14
|
+
} from "@opentui/solid";
|
|
15
|
+
import {
|
|
16
|
+
createEffect,
|
|
17
|
+
createMemo,
|
|
18
|
+
createSignal,
|
|
19
|
+
For,
|
|
20
|
+
onCleanup,
|
|
21
|
+
onMount,
|
|
22
|
+
Show,
|
|
23
|
+
} from "solid-js";
|
|
24
|
+
import {
|
|
25
|
+
type DatePreset,
|
|
26
|
+
formatRelativeTime,
|
|
27
|
+
loadSessionIndex,
|
|
28
|
+
parseSearchQuery,
|
|
29
|
+
type RoleFilter,
|
|
30
|
+
type ScopeFilter,
|
|
31
|
+
type SearchMode,
|
|
32
|
+
type SearchSessionMatch,
|
|
33
|
+
searchSessionIndex,
|
|
34
|
+
type SessionIndex,
|
|
35
|
+
} from "./search.ts";
|
|
36
|
+
|
|
37
|
+
function splitHighlightedText(
|
|
38
|
+
text: string,
|
|
39
|
+
term: string
|
|
40
|
+
): Array<{ text: string; match: boolean }> {
|
|
41
|
+
if (!term || !term.trim() || !text) {
|
|
42
|
+
return [{ text, match: false }];
|
|
43
|
+
}
|
|
44
|
+
const cleanTerm = term.trim().toLowerCase();
|
|
45
|
+
const words = cleanTerm.split(/\s+/).filter(Boolean);
|
|
46
|
+
const candidates = words.length > 1 ? [cleanTerm, ...words] : [cleanTerm];
|
|
47
|
+
const parts: Array<{ text: string; match: boolean }> = [];
|
|
48
|
+
const lower = text.toLowerCase();
|
|
49
|
+
let cursor = 0;
|
|
50
|
+
|
|
51
|
+
while (cursor < text.length) {
|
|
52
|
+
let bestIdx = -1;
|
|
53
|
+
let bestLen = 0;
|
|
54
|
+
for (const w of candidates) {
|
|
55
|
+
const idx = lower.indexOf(w, cursor);
|
|
56
|
+
if (idx !== -1 && (bestIdx === -1 || idx < bestIdx || (idx === bestIdx && w.length > bestLen))) {
|
|
57
|
+
bestIdx = idx;
|
|
58
|
+
bestLen = w.length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (bestIdx === -1) {
|
|
62
|
+
parts.push({ text: text.slice(cursor), match: false });
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
if (bestIdx > cursor) {
|
|
66
|
+
parts.push({ text: text.slice(cursor, bestIdx), match: false });
|
|
67
|
+
}
|
|
68
|
+
parts.push({ text: text.slice(bestIdx, bestIdx + bestLen), match: true });
|
|
69
|
+
cursor = bestIdx + bestLen;
|
|
70
|
+
}
|
|
71
|
+
return parts.length ? parts : [{ text, match: false }];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface SearchNavigationTarget {
|
|
75
|
+
messageId: string;
|
|
76
|
+
snippet: string;
|
|
77
|
+
matchedTerm: string;
|
|
78
|
+
kind: string;
|
|
79
|
+
timeCreated: number;
|
|
80
|
+
messageRatio?: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface DialogProps {
|
|
84
|
+
context: TuiPluginContext;
|
|
85
|
+
initialQuery: string;
|
|
86
|
+
currentDirectory: string;
|
|
87
|
+
close: () => void;
|
|
88
|
+
navigateSession: (sessionId: string, target?: SearchNavigationTarget) => void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const ROLE_CYCLE: RoleFilter[] = ["all", "user", "assistant", "shell"];
|
|
92
|
+
const DATE_CYCLE: DatePreset[] = ["all", "today", "yesterday", "7d", "30d"];
|
|
93
|
+
const MODE_CYCLE: SearchMode[] = ["smart", "fuzzy", "regex"];
|
|
94
|
+
|
|
95
|
+
function createTuiRenderer() {
|
|
96
|
+
|
|
97
|
+
function el(
|
|
98
|
+
tag: string,
|
|
99
|
+
props?: Record<string, unknown> | null,
|
|
100
|
+
...children: unknown[]
|
|
101
|
+
) {
|
|
102
|
+
const node = createElement(tag);
|
|
103
|
+
if (props) {
|
|
104
|
+
for (const [key, val] of Object.entries(props)) {
|
|
105
|
+
if (val === undefined) continue;
|
|
106
|
+
if (key === "ref" && typeof val === "function") {
|
|
107
|
+
use(val as (e: unknown) => void, node, undefined);
|
|
108
|
+
} else if (
|
|
109
|
+
typeof val === "function" &&
|
|
110
|
+
!key.startsWith("on") &&
|
|
111
|
+
key !== "renderBefore"
|
|
112
|
+
) {
|
|
113
|
+
effect((prev: unknown) => setProp(node, key, (val as () => unknown)(), prev));
|
|
114
|
+
} else {
|
|
115
|
+
setProp(node, key, val);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const child of children) {
|
|
120
|
+
if (child === undefined || child === null || child === false) continue;
|
|
121
|
+
if (typeof child === "string") {
|
|
122
|
+
insertNode(node, createTextNode(child));
|
|
123
|
+
} else {
|
|
124
|
+
insert(node, child);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return node;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function SessionSearchDialog(props: DialogProps) {
|
|
131
|
+
const [query, setQuery] = createSignal(props.initialQuery);
|
|
132
|
+
const [scope, setScope] = createSignal<ScopeFilter>("all");
|
|
133
|
+
const [role, setRole] = createSignal<RoleFilter>("all");
|
|
134
|
+
const [datePreset, setDatePreset] = createSignal<DatePreset>("all");
|
|
135
|
+
const [mode, setMode] = createSignal<SearchMode>("smart");
|
|
136
|
+
const [focusPane, setFocusPane] = createSignal<"search" | "list" | "preview">("search");
|
|
137
|
+
const [selectedIndex, setSelectedIndex] = createSignal(0);
|
|
138
|
+
const [selectedSnippetIndex, setSelectedSnippetIndex] = createSignal(0);
|
|
139
|
+
const [indexData, setIndexData] = createSignal<SessionIndex>({
|
|
140
|
+
dbPath: "",
|
|
141
|
+
loadedAt: 0,
|
|
142
|
+
sessions: [],
|
|
143
|
+
});
|
|
144
|
+
const [loading, setLoading] = createSignal(true);
|
|
145
|
+
const [errorMsg, setErrorMsg] = createSignal("");
|
|
146
|
+
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
148
|
+
let rootRef: any;
|
|
149
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
150
|
+
let inputRef: any;
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
152
|
+
let listScrollRef: any;
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
+
let previewScrollRef: any;
|
|
155
|
+
|
|
156
|
+
const resolveDialogWidth = (): string | number => {
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
|
+
const opts = (props.context as any)?.options;
|
|
159
|
+
const raw =
|
|
160
|
+
opts?.dialogWidth ??
|
|
161
|
+
opts?.width ??
|
|
162
|
+
process.env.OPENCODE_SESSION_SEARCH_DIALOG_WIDTH ??
|
|
163
|
+
process.env.OPENCODE_DIALOG_WIDTH ??
|
|
164
|
+
"80%";
|
|
165
|
+
if (typeof raw === "number" && Number.isFinite(raw) && raw > 0) {
|
|
166
|
+
return raw <= 100 ? `${Math.round(raw)}%` : Math.round(raw);
|
|
167
|
+
}
|
|
168
|
+
if (typeof raw === "string") {
|
|
169
|
+
const trimmed = raw.trim();
|
|
170
|
+
if (/^\d+(\.\d+)?%$/.test(trimmed)) return trimmed;
|
|
171
|
+
const num = Number(trimmed);
|
|
172
|
+
if (Number.isFinite(num) && num > 0) {
|
|
173
|
+
return num <= 100 ? `${Math.round(num)}%` : Math.round(num);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return "80%";
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const applyDialogWidth = () => {
|
|
180
|
+
try {
|
|
181
|
+
props.context.ui.dialog.set({
|
|
182
|
+
size: "xlarge",
|
|
183
|
+
centered: true,
|
|
184
|
+
});
|
|
185
|
+
} catch {
|
|
186
|
+
// ignore if dialog state is not ready yet
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (!rootRef) return;
|
|
190
|
+
try {
|
|
191
|
+
// Walk up to the dialog container box (direct child of the full-screen absolute overlay)
|
|
192
|
+
// and set its width/maxWidth to the configured percentage of the terminal viewport (default 80%).
|
|
193
|
+
const targetWidth = resolveDialogWidth();
|
|
194
|
+
let cursor = rootRef;
|
|
195
|
+
let dialogBox = rootRef;
|
|
196
|
+
while (cursor.parent && cursor.parent._positionType !== "absolute") {
|
|
197
|
+
cursor = cursor.parent;
|
|
198
|
+
dialogBox = cursor;
|
|
199
|
+
}
|
|
200
|
+
dialogBox.width = targetWidth;
|
|
201
|
+
dialogBox.maxWidth = targetWidth;
|
|
202
|
+
dialogBox.minWidth = 72;
|
|
203
|
+
|
|
204
|
+
let inner = rootRef;
|
|
205
|
+
while (inner && inner !== dialogBox) {
|
|
206
|
+
inner.width = "100%";
|
|
207
|
+
inner.maxWidth = "100%";
|
|
208
|
+
inner = inner.parent;
|
|
209
|
+
}
|
|
210
|
+
rootRef.requestRender?.();
|
|
211
|
+
} catch {
|
|
212
|
+
// ignore layout mutation errors
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const theme = () => props.context.theme.surface("dialog");
|
|
217
|
+
|
|
218
|
+
const reloadIndex = async (force = false) => {
|
|
219
|
+
setLoading(true);
|
|
220
|
+
setErrorMsg("");
|
|
221
|
+
try {
|
|
222
|
+
const loaded = await loadSessionIndex(force);
|
|
223
|
+
setIndexData(loaded);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
setErrorMsg(err instanceof Error ? err.message : String(err));
|
|
226
|
+
} finally {
|
|
227
|
+
setLoading(false);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const parsedQuery = createMemo(() =>
|
|
232
|
+
parseSearchQuery(query(), {
|
|
233
|
+
scope: scope(),
|
|
234
|
+
currentDirectory: props.currentDirectory,
|
|
235
|
+
role: role(),
|
|
236
|
+
datePreset: datePreset(),
|
|
237
|
+
mode: mode(),
|
|
238
|
+
})
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
const results = createMemo<SearchSessionMatch[]>(() => {
|
|
242
|
+
const idx = indexData();
|
|
243
|
+
if (!idx.sessions.length) return [];
|
|
244
|
+
return searchSessionIndex(idx, query(), {
|
|
245
|
+
scope: scope(),
|
|
246
|
+
currentDirectory: props.currentDirectory,
|
|
247
|
+
role: role(),
|
|
248
|
+
datePreset: datePreset(),
|
|
249
|
+
mode: mode(),
|
|
250
|
+
limit: 60,
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
createEffect(() => {
|
|
255
|
+
query();
|
|
256
|
+
scope();
|
|
257
|
+
role();
|
|
258
|
+
datePreset();
|
|
259
|
+
mode();
|
|
260
|
+
setSelectedIndex(0);
|
|
261
|
+
setSelectedSnippetIndex(0);
|
|
262
|
+
queueMicrotask(() => listScrollRef?.scrollTo?.(0));
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const selectedMatch = createMemo<SearchSessionMatch | undefined>(() => {
|
|
266
|
+
const list = results();
|
|
267
|
+
if (!list.length) return undefined;
|
|
268
|
+
const idx = Math.max(0, Math.min(list.length - 1, selectedIndex()));
|
|
269
|
+
return list[idx];
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const moveSelection = (delta: number) => {
|
|
273
|
+
if (focusPane() === "preview") {
|
|
274
|
+
const snippets = selectedMatch()?.matches ?? [];
|
|
275
|
+
if (!snippets.length) return;
|
|
276
|
+
const nextSnippet = Math.max(
|
|
277
|
+
0,
|
|
278
|
+
Math.min(snippets.length - 1, selectedSnippetIndex() + delta)
|
|
279
|
+
);
|
|
280
|
+
setSelectedSnippetIndex(nextSnippet);
|
|
281
|
+
queueMicrotask(() => {
|
|
282
|
+
previewScrollRef?.scrollChildIntoView?.(`snippet-row-${nextSnippet}`);
|
|
283
|
+
});
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const list = results();
|
|
287
|
+
if (!list.length) return;
|
|
288
|
+
const next = Math.max(0, Math.min(list.length - 1, selectedIndex() + delta));
|
|
289
|
+
setSelectedIndex(next);
|
|
290
|
+
setSelectedSnippetIndex(0);
|
|
291
|
+
queueMicrotask(() => {
|
|
292
|
+
listScrollRef?.scrollChildIntoView?.(`session-row-${next}`);
|
|
293
|
+
previewScrollRef?.scrollTo?.(0);
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const toggleScope = () => {
|
|
298
|
+
setScope((prev: ScopeFilter) => (prev === "all" ? "project" : "all"));
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const cycleRole = () => {
|
|
302
|
+
setRole((prev: RoleFilter) => ROLE_CYCLE[(ROLE_CYCLE.indexOf(prev) + 1) % ROLE_CYCLE.length]);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const cycleDate = () => {
|
|
306
|
+
setDatePreset((prev: DatePreset) => DATE_CYCLE[(DATE_CYCLE.indexOf(prev) + 1) % DATE_CYCLE.length]);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const cycleMode = () => {
|
|
310
|
+
setMode((prev: SearchMode) => MODE_CYCLE[(MODE_CYCLE.indexOf(prev) + 1) % MODE_CYCLE.length]);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const openSelected = (explicitHit?: SearchSessionMatch["matches"][number]) => {
|
|
314
|
+
const current = selectedMatch();
|
|
315
|
+
if (!current) return;
|
|
316
|
+
|
|
317
|
+
const snippets = current.matches ?? [];
|
|
318
|
+
const chosen =
|
|
319
|
+
explicitHit ??
|
|
320
|
+
snippets[Math.max(0, Math.min(snippets.length - 1, selectedSnippetIndex()))] ??
|
|
321
|
+
current.bestMatch;
|
|
322
|
+
|
|
323
|
+
// If the active match is a session title match, prefer the first message match in the session if available
|
|
324
|
+
const messageHit =
|
|
325
|
+
chosen && chosen.messageId
|
|
326
|
+
? chosen
|
|
327
|
+
: snippets.find((m) => Boolean(m.messageId)) ?? chosen;
|
|
328
|
+
|
|
329
|
+
let messageRatio: number | undefined;
|
|
330
|
+
if (messageHit?.messageId && current.session.entries.length > 0) {
|
|
331
|
+
const uniqueMsgIds: string[] = [];
|
|
332
|
+
const sortedEntries = [...current.session.entries].sort(
|
|
333
|
+
(a, b) => a.timeCreated - b.timeCreated
|
|
334
|
+
);
|
|
335
|
+
for (const entry of sortedEntries) {
|
|
336
|
+
const entryMsgId = entry.messageId || entry.id.split(":")[0];
|
|
337
|
+
if (entryMsgId && !uniqueMsgIds.includes(entryMsgId)) {
|
|
338
|
+
uniqueMsgIds.push(entryMsgId);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const msgIdx = uniqueMsgIds.indexOf(messageHit.messageId);
|
|
342
|
+
if (msgIdx !== -1 && uniqueMsgIds.length > 1) {
|
|
343
|
+
messageRatio = msgIdx / (uniqueMsgIds.length - 1);
|
|
344
|
+
} else if (msgIdx === 0) {
|
|
345
|
+
messageRatio = 0;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
props.navigateSession(
|
|
350
|
+
current.session.id,
|
|
351
|
+
messageHit
|
|
352
|
+
? {
|
|
353
|
+
messageId: messageHit.messageId,
|
|
354
|
+
snippet: messageHit.snippet,
|
|
355
|
+
matchedTerm: messageHit.matchedTerm,
|
|
356
|
+
kind: messageHit.kind,
|
|
357
|
+
timeCreated: messageHit.timeCreated,
|
|
358
|
+
messageRatio,
|
|
359
|
+
}
|
|
360
|
+
: undefined
|
|
361
|
+
);
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
365
|
+
const handleKeyPress = (event: any) => {
|
|
366
|
+
const name = String(event.name || "").toLowerCase();
|
|
367
|
+
const enter = ["return", "enter", "linefeed"].includes(name);
|
|
368
|
+
|
|
369
|
+
if (name === "escape") {
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
event.stopPropagation();
|
|
372
|
+
props.close();
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (enter) {
|
|
377
|
+
event.preventDefault();
|
|
378
|
+
event.stopPropagation();
|
|
379
|
+
openSelected();
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (name === "tab") {
|
|
384
|
+
event.preventDefault();
|
|
385
|
+
event.stopPropagation();
|
|
386
|
+
const order: Array<"search" | "list" | "preview"> = ["search", "list", "preview"];
|
|
387
|
+
const idx = order.indexOf(focusPane());
|
|
388
|
+
const next = order[(idx + (event.shift ? -1 : 1) + order.length) % order.length];
|
|
389
|
+
setFocusPane(next);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (event.ctrl && name === "p") {
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
event.stopPropagation();
|
|
396
|
+
toggleScope();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (event.ctrl && name === "g") {
|
|
400
|
+
event.preventDefault();
|
|
401
|
+
event.stopPropagation();
|
|
402
|
+
cycleRole();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (event.ctrl && name === "t") {
|
|
406
|
+
event.preventDefault();
|
|
407
|
+
event.stopPropagation();
|
|
408
|
+
cycleDate();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (event.ctrl && name === "r") {
|
|
412
|
+
event.preventDefault();
|
|
413
|
+
event.stopPropagation();
|
|
414
|
+
cycleMode();
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (event.ctrl && name === "l") {
|
|
418
|
+
event.preventDefault();
|
|
419
|
+
event.stopPropagation();
|
|
420
|
+
reloadIndex(true);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (
|
|
425
|
+
name === "down" ||
|
|
426
|
+
(event.ctrl && name === "n") ||
|
|
427
|
+
(focusPane() === "list" && !event.ctrl && name === "j")
|
|
428
|
+
) {
|
|
429
|
+
event.preventDefault();
|
|
430
|
+
event.stopPropagation();
|
|
431
|
+
moveSelection(1);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (
|
|
435
|
+
name === "up" ||
|
|
436
|
+
(event.ctrl && name === "k") ||
|
|
437
|
+
(focusPane() === "list" && !event.ctrl && name === "k")
|
|
438
|
+
) {
|
|
439
|
+
event.preventDefault();
|
|
440
|
+
event.stopPropagation();
|
|
441
|
+
moveSelection(-1);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (name === "pagedown" || (event.ctrl && name === "d")) {
|
|
445
|
+
event.preventDefault();
|
|
446
|
+
event.stopPropagation();
|
|
447
|
+
if (focusPane() === "preview") {
|
|
448
|
+
previewScrollRef?.scrollBy?.(8);
|
|
449
|
+
} else {
|
|
450
|
+
moveSelection(6);
|
|
451
|
+
}
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (name === "pageup" || (event.ctrl && name === "u")) {
|
|
455
|
+
event.preventDefault();
|
|
456
|
+
event.stopPropagation();
|
|
457
|
+
if (focusPane() === "preview") {
|
|
458
|
+
previewScrollRef?.scrollBy?.(-8);
|
|
459
|
+
} else {
|
|
460
|
+
moveSelection(-6);
|
|
461
|
+
}
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (focusPane() === "preview" && !event.ctrl && (name === "j" || name === "k")) {
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
event.stopPropagation();
|
|
467
|
+
previewScrollRef?.scrollBy?.(name === "j" ? 2 : -2);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (focusPane() !== "search" && !event.ctrl && name === "/") {
|
|
471
|
+
event.preventDefault();
|
|
472
|
+
event.stopPropagation();
|
|
473
|
+
setFocusPane("search");
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
onMount(() => {
|
|
479
|
+
props.context.renderer.keyInput.prependListener("keypress", handleKeyPress);
|
|
480
|
+
applyDialogWidth();
|
|
481
|
+
queueMicrotask(() => {
|
|
482
|
+
applyDialogWidth();
|
|
483
|
+
if (props.initialQuery && inputRef && inputRef.value !== props.initialQuery) {
|
|
484
|
+
inputRef.value = props.initialQuery;
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
reloadIndex(false);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
onCleanup(() => {
|
|
491
|
+
props.context.renderer.keyInput.removeListener("keypress", handleKeyPress);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
const Pill = (pillProps: {
|
|
495
|
+
label: () => string;
|
|
496
|
+
shortcut?: string;
|
|
497
|
+
active: () => boolean;
|
|
498
|
+
onClick: () => void;
|
|
499
|
+
}) =>
|
|
500
|
+
el(
|
|
501
|
+
"box",
|
|
502
|
+
{
|
|
503
|
+
flexDirection: "row",
|
|
504
|
+
flexShrink: 0,
|
|
505
|
+
paddingX: 1,
|
|
506
|
+
backgroundColor: () =>
|
|
507
|
+
pillProps.active()
|
|
508
|
+
? theme().background.action.primary.focused
|
|
509
|
+
: theme().background.raised.base,
|
|
510
|
+
onMouseUp: pillProps.onClick,
|
|
511
|
+
},
|
|
512
|
+
el(
|
|
513
|
+
"text",
|
|
514
|
+
{
|
|
515
|
+
fg: () =>
|
|
516
|
+
pillProps.active()
|
|
517
|
+
? theme().text.action.primary.focused
|
|
518
|
+
: theme().text.base,
|
|
519
|
+
attributes: () => (pillProps.active() ? TextAttributes.BOLD : undefined),
|
|
520
|
+
},
|
|
521
|
+
pillProps.label
|
|
522
|
+
),
|
|
523
|
+
pillProps.shortcut
|
|
524
|
+
? el(
|
|
525
|
+
"text",
|
|
526
|
+
{
|
|
527
|
+
fg: () =>
|
|
528
|
+
pillProps.active()
|
|
529
|
+
? theme().text.action.primary.focused
|
|
530
|
+
: theme().text.muted,
|
|
531
|
+
},
|
|
532
|
+
` ${pillProps.shortcut}`
|
|
533
|
+
)
|
|
534
|
+
: null
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
return el(
|
|
538
|
+
"box",
|
|
539
|
+
{
|
|
540
|
+
ref: (r: unknown) => {
|
|
541
|
+
rootRef = r;
|
|
542
|
+
queueMicrotask(applyDialogWidth);
|
|
543
|
+
},
|
|
544
|
+
width: "100%",
|
|
545
|
+
height: 34,
|
|
546
|
+
flexDirection: "column",
|
|
547
|
+
backgroundColor: () => theme().background.base,
|
|
548
|
+
paddingLeft: 2,
|
|
549
|
+
paddingRight: 2,
|
|
550
|
+
paddingTop: 1,
|
|
551
|
+
paddingBottom: 1,
|
|
552
|
+
gap: 1,
|
|
553
|
+
},
|
|
554
|
+
// 1. Header Bar (single-line, no wrapping)
|
|
555
|
+
el(
|
|
556
|
+
"box",
|
|
557
|
+
{
|
|
558
|
+
flexDirection: "row",
|
|
559
|
+
justifyContent: "space-between",
|
|
560
|
+
alignItems: "center",
|
|
561
|
+
border: ["bottom"],
|
|
562
|
+
borderColor: () => theme().border.base,
|
|
563
|
+
paddingBottom: 0,
|
|
564
|
+
flexShrink: 0,
|
|
565
|
+
width: "100%",
|
|
566
|
+
},
|
|
567
|
+
el(
|
|
568
|
+
"box",
|
|
569
|
+
{ flexDirection: "row", gap: 2, alignItems: "center", flexShrink: 0 },
|
|
570
|
+
el(
|
|
571
|
+
"text",
|
|
572
|
+
{
|
|
573
|
+
wrapMode: "none",
|
|
574
|
+
flexShrink: 0,
|
|
575
|
+
fg: () => theme().text.action.primary.base,
|
|
576
|
+
attributes: TextAttributes.BOLD,
|
|
577
|
+
},
|
|
578
|
+
"opencode / search"
|
|
579
|
+
),
|
|
580
|
+
el(
|
|
581
|
+
"text",
|
|
582
|
+
{
|
|
583
|
+
wrapMode: "none",
|
|
584
|
+
flexShrink: 0,
|
|
585
|
+
fg: () => theme().text.muted,
|
|
586
|
+
},
|
|
587
|
+
() => props.context.ui.format.path(props.currentDirectory)
|
|
588
|
+
)
|
|
589
|
+
),
|
|
590
|
+
el(
|
|
591
|
+
"text",
|
|
592
|
+
{
|
|
593
|
+
wrapMode: "none",
|
|
594
|
+
flexShrink: 0,
|
|
595
|
+
fg: () => theme().text.muted,
|
|
596
|
+
},
|
|
597
|
+
"[↑↓] select [Enter] open [Tab] pane [Esc] close"
|
|
598
|
+
)
|
|
599
|
+
),
|
|
600
|
+
|
|
601
|
+
// 2. Boxed Filter + Pills Card
|
|
602
|
+
el(
|
|
603
|
+
"box",
|
|
604
|
+
{
|
|
605
|
+
flexDirection: "column",
|
|
606
|
+
backgroundColor: () =>
|
|
607
|
+
theme().background.raised?.base ?? theme().background.base,
|
|
608
|
+
border: true,
|
|
609
|
+
borderColor: () =>
|
|
610
|
+
focusPane() === "search"
|
|
611
|
+
? theme().text.action.primary.base
|
|
612
|
+
: theme().border.base,
|
|
613
|
+
paddingLeft: 2,
|
|
614
|
+
paddingRight: 2,
|
|
615
|
+
paddingTop: 0,
|
|
616
|
+
paddingBottom: 0,
|
|
617
|
+
gap: 0,
|
|
618
|
+
flexShrink: 0,
|
|
619
|
+
width: "100%",
|
|
620
|
+
},
|
|
621
|
+
el(
|
|
622
|
+
"box",
|
|
623
|
+
{
|
|
624
|
+
flexDirection: "row",
|
|
625
|
+
justifyContent: "space-between",
|
|
626
|
+
alignItems: "center",
|
|
627
|
+
gap: 1,
|
|
628
|
+
width: "100%",
|
|
629
|
+
},
|
|
630
|
+
el(
|
|
631
|
+
"text",
|
|
632
|
+
{
|
|
633
|
+
width: 7,
|
|
634
|
+
wrapMode: "none",
|
|
635
|
+
flexShrink: 0,
|
|
636
|
+
fg: () => theme().text.action.primary.base,
|
|
637
|
+
attributes: TextAttributes.BOLD,
|
|
638
|
+
},
|
|
639
|
+
"Filter:"
|
|
640
|
+
),
|
|
641
|
+
el(
|
|
642
|
+
"box",
|
|
643
|
+
{
|
|
644
|
+
flexGrow: 1,
|
|
645
|
+
flexShrink: 1,
|
|
646
|
+
minWidth: 0,
|
|
647
|
+
},
|
|
648
|
+
el("input", {
|
|
649
|
+
id: "session-search-input",
|
|
650
|
+
width: "100%",
|
|
651
|
+
ref: (r: unknown) => {
|
|
652
|
+
inputRef = r;
|
|
653
|
+
if (props.initialQuery && r && (r as { value?: string }).value !== props.initialQuery) {
|
|
654
|
+
(r as { value: string }).value = props.initialQuery;
|
|
655
|
+
}
|
|
656
|
+
},
|
|
657
|
+
value: () => query(),
|
|
658
|
+
focused: () => focusPane() === "search",
|
|
659
|
+
maxLength: 2000,
|
|
660
|
+
placeholder:
|
|
661
|
+
"Search keywords, \"exact phrase\", /regex/i, ~fuzzy, role:user, date:7d, file:path, tool:name...",
|
|
662
|
+
placeholderColor: () => theme().text.muted,
|
|
663
|
+
textColor: () => theme().text.base,
|
|
664
|
+
focusedTextColor: () => theme().text.formfield.focused,
|
|
665
|
+
cursorColor: () => theme().text.formfield.focused,
|
|
666
|
+
backgroundColor: () =>
|
|
667
|
+
theme().background.raised?.base ?? theme().background.base,
|
|
668
|
+
focusedBackgroundColor: () =>
|
|
669
|
+
theme().background.raised?.base ?? theme().background.base,
|
|
670
|
+
onMouseDown: () => setFocusPane("search"),
|
|
671
|
+
onInput: (val: string) => setQuery(val),
|
|
672
|
+
})
|
|
673
|
+
),
|
|
674
|
+
el(
|
|
675
|
+
"text",
|
|
676
|
+
{
|
|
677
|
+
wrapMode: "none",
|
|
678
|
+
flexShrink: 0,
|
|
679
|
+
fg: () =>
|
|
680
|
+
theme().text.feedback?.success?.base ??
|
|
681
|
+
theme().text.action.primary.base,
|
|
682
|
+
},
|
|
683
|
+
() =>
|
|
684
|
+
loading()
|
|
685
|
+
? "indexing SQLite..."
|
|
686
|
+
: `${results().length} of ${indexData().sessions.length} sessions · 0 tokens`
|
|
687
|
+
)
|
|
688
|
+
),
|
|
689
|
+
// Filter pills bar
|
|
690
|
+
el(
|
|
691
|
+
"box",
|
|
692
|
+
{
|
|
693
|
+
flexDirection: "row",
|
|
694
|
+
flexWrap: "wrap",
|
|
695
|
+
gap: 1,
|
|
696
|
+
alignItems: "center",
|
|
697
|
+
},
|
|
698
|
+
Pill({
|
|
699
|
+
label: () =>
|
|
700
|
+
`scope:${(parsedQuery().scope ?? scope()) === "project" ? "project" : "all"}`,
|
|
701
|
+
shortcut: "^p",
|
|
702
|
+
active: () => (parsedQuery().scope ?? scope()) === "project",
|
|
703
|
+
onClick: toggleScope,
|
|
704
|
+
}),
|
|
705
|
+
Pill({
|
|
706
|
+
label: () => `role:${parsedQuery().role}`,
|
|
707
|
+
shortcut: "^g",
|
|
708
|
+
active: () => parsedQuery().role !== "all",
|
|
709
|
+
onClick: cycleRole,
|
|
710
|
+
}),
|
|
711
|
+
Pill({
|
|
712
|
+
label: () =>
|
|
713
|
+
`date:${parsedQuery().dateRange?.label ?? datePreset()}`,
|
|
714
|
+
shortcut: "^t",
|
|
715
|
+
active: () => Boolean(parsedQuery().dateRange),
|
|
716
|
+
onClick: cycleDate,
|
|
717
|
+
}),
|
|
718
|
+
Pill({
|
|
719
|
+
label: () => `mode:${parsedQuery().effectiveMode}`,
|
|
720
|
+
shortcut: "^r",
|
|
721
|
+
active: () => parsedQuery().effectiveMode !== "smart",
|
|
722
|
+
onClick: cycleMode,
|
|
723
|
+
}),
|
|
724
|
+
createComponent(Show, {
|
|
725
|
+
get when() {
|
|
726
|
+
return Boolean(parsedQuery().fileFilter);
|
|
727
|
+
},
|
|
728
|
+
get children() {
|
|
729
|
+
return el(
|
|
730
|
+
"text",
|
|
731
|
+
{ fg: () => theme().text.action.primary.base },
|
|
732
|
+
() => ` [file:${parsedQuery().fileFilter}]`
|
|
733
|
+
);
|
|
734
|
+
},
|
|
735
|
+
}),
|
|
736
|
+
createComponent(Show, {
|
|
737
|
+
get when() {
|
|
738
|
+
return Boolean(parsedQuery().toolFilter);
|
|
739
|
+
},
|
|
740
|
+
get children() {
|
|
741
|
+
return el(
|
|
742
|
+
"text",
|
|
743
|
+
{ fg: () => theme().text.action.primary.base },
|
|
744
|
+
() => ` [tool:${parsedQuery().toolFilter}]`
|
|
745
|
+
);
|
|
746
|
+
},
|
|
747
|
+
}),
|
|
748
|
+
createComponent(Show, {
|
|
749
|
+
get when() {
|
|
750
|
+
return Boolean(parsedQuery().regexError);
|
|
751
|
+
},
|
|
752
|
+
get children() {
|
|
753
|
+
return el(
|
|
754
|
+
"text",
|
|
755
|
+
{ fg: () => theme().text.feedback.error.base },
|
|
756
|
+
() => ` Regex: ${parsedQuery().regexError}`
|
|
757
|
+
);
|
|
758
|
+
},
|
|
759
|
+
})
|
|
760
|
+
)
|
|
761
|
+
),
|
|
762
|
+
|
|
763
|
+
// 3. Split-Pane Main Body: Two Side-by-Side Bordered Cards
|
|
764
|
+
el(
|
|
765
|
+
"box",
|
|
766
|
+
{
|
|
767
|
+
flexDirection: "row",
|
|
768
|
+
gap: 2,
|
|
769
|
+
flexGrow: 1,
|
|
770
|
+
minHeight: 0,
|
|
771
|
+
width: "100%",
|
|
772
|
+
},
|
|
773
|
+
// Left Card: Results List (44% width)
|
|
774
|
+
el(
|
|
775
|
+
"box",
|
|
776
|
+
{
|
|
777
|
+
width: "44%",
|
|
778
|
+
height: "100%",
|
|
779
|
+
flexDirection: "column",
|
|
780
|
+
flexShrink: 0,
|
|
781
|
+
backgroundColor: () =>
|
|
782
|
+
theme().background.raised?.base ?? theme().background.base,
|
|
783
|
+
border: true,
|
|
784
|
+
borderColor: () =>
|
|
785
|
+
focusPane() === "list"
|
|
786
|
+
? theme().text.action.primary.base
|
|
787
|
+
: theme().border.base,
|
|
788
|
+
paddingLeft: 1,
|
|
789
|
+
paddingRight: 1,
|
|
790
|
+
paddingTop: 0,
|
|
791
|
+
paddingBottom: 0,
|
|
792
|
+
onMouseDown: () => setFocusPane("list"),
|
|
793
|
+
},
|
|
794
|
+
el(
|
|
795
|
+
"box",
|
|
796
|
+
{
|
|
797
|
+
flexDirection: "row",
|
|
798
|
+
justifyContent: "space-between",
|
|
799
|
+
border: ["bottom"],
|
|
800
|
+
borderColor: () => theme().border.base,
|
|
801
|
+
paddingX: 1,
|
|
802
|
+
flexShrink: 0,
|
|
803
|
+
},
|
|
804
|
+
el(
|
|
805
|
+
"text",
|
|
806
|
+
{
|
|
807
|
+
fg: () => theme().text.action.primary.base,
|
|
808
|
+
attributes: TextAttributes.BOLD,
|
|
809
|
+
},
|
|
810
|
+
"◈ MATCHING SESSIONS"
|
|
811
|
+
),
|
|
812
|
+
el(
|
|
813
|
+
"text",
|
|
814
|
+
{ fg: () => theme().text.muted },
|
|
815
|
+
() =>
|
|
816
|
+
`${(parsedQuery().scope ?? scope()) === "project" ? "project" : "all"} · ${parsedQuery().effectiveMode}`
|
|
817
|
+
)
|
|
818
|
+
),
|
|
819
|
+
el(
|
|
820
|
+
"scrollbox",
|
|
821
|
+
{
|
|
822
|
+
id: "session-search-list",
|
|
823
|
+
ref: (r: unknown) => {
|
|
824
|
+
listScrollRef = r;
|
|
825
|
+
},
|
|
826
|
+
width: "100%",
|
|
827
|
+
flexGrow: 1,
|
|
828
|
+
minHeight: 0,
|
|
829
|
+
scrollbarOptions: { visible: false },
|
|
830
|
+
},
|
|
831
|
+
createComponent(Show, {
|
|
832
|
+
get when() {
|
|
833
|
+
return !loading() && results().length > 0;
|
|
834
|
+
},
|
|
835
|
+
get fallback() {
|
|
836
|
+
return el(
|
|
837
|
+
"box",
|
|
838
|
+
{ paddingX: 1, paddingY: 1 },
|
|
839
|
+
el(
|
|
840
|
+
"text",
|
|
841
|
+
{ fg: () => theme().text.muted },
|
|
842
|
+
() =>
|
|
843
|
+
loading()
|
|
844
|
+
? "Loading sessions..."
|
|
845
|
+
: errorMsg()
|
|
846
|
+
? `Error: ${errorMsg()}`
|
|
847
|
+
: "No matching sessions found. Try ~fuzzy, scope:all, or fewer terms."
|
|
848
|
+
)
|
|
849
|
+
);
|
|
850
|
+
},
|
|
851
|
+
get children() {
|
|
852
|
+
return createComponent(For, {
|
|
853
|
+
get each() {
|
|
854
|
+
return results();
|
|
855
|
+
},
|
|
856
|
+
children: (item: SearchSessionMatch, idx: () => number) => {
|
|
857
|
+
const isSelected = () => selectedIndex() === idx();
|
|
858
|
+
return el(
|
|
859
|
+
"box",
|
|
860
|
+
{
|
|
861
|
+
id: () => `session-row-${idx()}`,
|
|
862
|
+
flexDirection: "column",
|
|
863
|
+
paddingX: 1,
|
|
864
|
+
paddingY: 0,
|
|
865
|
+
marginBottom: 1,
|
|
866
|
+
flexShrink: 0,
|
|
867
|
+
backgroundColor: () =>
|
|
868
|
+
isSelected()
|
|
869
|
+
? theme().background.action.primary.focused
|
|
870
|
+
: undefined,
|
|
871
|
+
onMouseUp: () => {
|
|
872
|
+
setSelectedIndex(idx());
|
|
873
|
+
openSelected();
|
|
874
|
+
},
|
|
875
|
+
onMouseOver: () => {
|
|
876
|
+
setSelectedIndex(idx());
|
|
877
|
+
},
|
|
878
|
+
},
|
|
879
|
+
el(
|
|
880
|
+
"box",
|
|
881
|
+
{
|
|
882
|
+
flexDirection: "row",
|
|
883
|
+
justifyContent: "space-between",
|
|
884
|
+
width: "100%",
|
|
885
|
+
},
|
|
886
|
+
el(
|
|
887
|
+
"text",
|
|
888
|
+
{
|
|
889
|
+
wrapMode: "none",
|
|
890
|
+
truncate: true,
|
|
891
|
+
flexShrink: 1,
|
|
892
|
+
minWidth: 0,
|
|
893
|
+
fg: () =>
|
|
894
|
+
isSelected()
|
|
895
|
+
? theme().text.action.primary.focused
|
|
896
|
+
: theme().text.base,
|
|
897
|
+
attributes: () =>
|
|
898
|
+
isSelected() ? TextAttributes.BOLD : undefined,
|
|
899
|
+
},
|
|
900
|
+
() =>
|
|
901
|
+
`${isSelected() ? "▸ " : " "}${item.session.title}`
|
|
902
|
+
),
|
|
903
|
+
el(
|
|
904
|
+
"text",
|
|
905
|
+
{
|
|
906
|
+
wrapMode: "none",
|
|
907
|
+
flexShrink: 0,
|
|
908
|
+
fg: () =>
|
|
909
|
+
isSelected()
|
|
910
|
+
? theme().text.action.primary.focused
|
|
911
|
+
: theme().text.action.primary.base,
|
|
912
|
+
},
|
|
913
|
+
() =>
|
|
914
|
+
` [${item.matchBadge}] ${formatRelativeTime(item.session.timeUpdated)}`
|
|
915
|
+
)
|
|
916
|
+
),
|
|
917
|
+
el(
|
|
918
|
+
"text",
|
|
919
|
+
{
|
|
920
|
+
wrapMode: "none",
|
|
921
|
+
truncate: true,
|
|
922
|
+
fg: () =>
|
|
923
|
+
isSelected()
|
|
924
|
+
? theme().text.action.primary.focused
|
|
925
|
+
: theme().text.muted,
|
|
926
|
+
},
|
|
927
|
+
() => ` ${item.bestSnippet}`
|
|
928
|
+
)
|
|
929
|
+
);
|
|
930
|
+
},
|
|
931
|
+
});
|
|
932
|
+
},
|
|
933
|
+
})
|
|
934
|
+
)
|
|
935
|
+
),
|
|
936
|
+
|
|
937
|
+
// Right Card: Rich Match & Session Context Preview (56% width)
|
|
938
|
+
el(
|
|
939
|
+
"box",
|
|
940
|
+
{
|
|
941
|
+
flexGrow: 1,
|
|
942
|
+
height: "100%",
|
|
943
|
+
minWidth: 0,
|
|
944
|
+
minHeight: 0,
|
|
945
|
+
flexDirection: "column",
|
|
946
|
+
backgroundColor: () =>
|
|
947
|
+
theme().background.raised?.base ?? theme().background.base,
|
|
948
|
+
border: true,
|
|
949
|
+
borderColor: () =>
|
|
950
|
+
focusPane() === "preview"
|
|
951
|
+
? theme().text.action.primary.base
|
|
952
|
+
: theme().border.base,
|
|
953
|
+
paddingLeft: 2,
|
|
954
|
+
paddingRight: 2,
|
|
955
|
+
paddingTop: 0,
|
|
956
|
+
paddingBottom: 1,
|
|
957
|
+
onMouseDown: () => setFocusPane("preview"),
|
|
958
|
+
},
|
|
959
|
+
el(
|
|
960
|
+
"box",
|
|
961
|
+
{
|
|
962
|
+
flexDirection: "row",
|
|
963
|
+
justifyContent: "space-between",
|
|
964
|
+
border: ["bottom"],
|
|
965
|
+
borderColor: () => theme().border.base,
|
|
966
|
+
marginBottom: 1,
|
|
967
|
+
flexShrink: 0,
|
|
968
|
+
},
|
|
969
|
+
el(
|
|
970
|
+
"text",
|
|
971
|
+
{
|
|
972
|
+
fg: () => theme().text.action.primary.base,
|
|
973
|
+
attributes: TextAttributes.BOLD,
|
|
974
|
+
},
|
|
975
|
+
"◈ CONVERSATION & MATCH PREVIEW"
|
|
976
|
+
),
|
|
977
|
+
el(
|
|
978
|
+
"text",
|
|
979
|
+
{ fg: () => theme().text.muted },
|
|
980
|
+
() =>
|
|
981
|
+
selectedMatch()
|
|
982
|
+
? `${selectedMatch()?.totalMatches ?? 0} match${selectedMatch()?.totalMatches === 1 ? "" : "es"} · updated ${formatRelativeTime(selectedMatch()?.session.timeUpdated ?? 0)}`
|
|
983
|
+
: "no selection"
|
|
984
|
+
)
|
|
985
|
+
),
|
|
986
|
+
el(
|
|
987
|
+
"scrollbox",
|
|
988
|
+
{
|
|
989
|
+
id: "session-search-preview",
|
|
990
|
+
ref: (r: unknown) => {
|
|
991
|
+
previewScrollRef = r;
|
|
992
|
+
},
|
|
993
|
+
flexGrow: 1,
|
|
994
|
+
minWidth: 0,
|
|
995
|
+
minHeight: 0,
|
|
996
|
+
focused: () => focusPane() === "preview",
|
|
997
|
+
},
|
|
998
|
+
createComponent(Show, {
|
|
999
|
+
get when() {
|
|
1000
|
+
return selectedMatch();
|
|
1001
|
+
},
|
|
1002
|
+
get fallback() {
|
|
1003
|
+
return el(
|
|
1004
|
+
"text",
|
|
1005
|
+
{ fg: () => theme().text.muted },
|
|
1006
|
+
"Select a session on the left to inspect matching messages, tools, and files."
|
|
1007
|
+
);
|
|
1008
|
+
},
|
|
1009
|
+
get children() {
|
|
1010
|
+
return el(
|
|
1011
|
+
"box",
|
|
1012
|
+
{ flexDirection: "column", gap: 1 },
|
|
1013
|
+
el(
|
|
1014
|
+
"box",
|
|
1015
|
+
{
|
|
1016
|
+
flexDirection: "column",
|
|
1017
|
+
paddingLeft: 1,
|
|
1018
|
+
border: ["left"],
|
|
1019
|
+
borderColor: () => theme().text.action.primary.base,
|
|
1020
|
+
},
|
|
1021
|
+
el(
|
|
1022
|
+
"text",
|
|
1023
|
+
{
|
|
1024
|
+
fg: () => theme().text.base,
|
|
1025
|
+
attributes: TextAttributes.BOLD,
|
|
1026
|
+
},
|
|
1027
|
+
() => selectedMatch()?.session.title ?? ""
|
|
1028
|
+
),
|
|
1029
|
+
el(
|
|
1030
|
+
"text",
|
|
1031
|
+
{ fg: () => theme().text.muted, wrapMode: "word" },
|
|
1032
|
+
() => {
|
|
1033
|
+
const m = selectedMatch();
|
|
1034
|
+
if (!m) return "";
|
|
1035
|
+
const dir = m.session.directory
|
|
1036
|
+
? props.context.ui.format.path(m.session.directory)
|
|
1037
|
+
: "(no directory)";
|
|
1038
|
+
return `${dir} · updated ${formatRelativeTime(m.session.timeUpdated)} · ${m.totalMatches} match${m.totalMatches === 1 ? "" : "es"}`;
|
|
1039
|
+
}
|
|
1040
|
+
),
|
|
1041
|
+
createComponent(Show, {
|
|
1042
|
+
get when() {
|
|
1043
|
+
return (selectedMatch()?.session.tools.length ?? 0) > 0;
|
|
1044
|
+
},
|
|
1045
|
+
get children() {
|
|
1046
|
+
return el(
|
|
1047
|
+
"text",
|
|
1048
|
+
{ fg: () => theme().text.muted, wrapMode: "word" },
|
|
1049
|
+
() =>
|
|
1050
|
+
`Tools: ${selectedMatch()?.session.tools.slice(0, 8).join(", ")}`
|
|
1051
|
+
);
|
|
1052
|
+
},
|
|
1053
|
+
}),
|
|
1054
|
+
createComponent(Show, {
|
|
1055
|
+
get when() {
|
|
1056
|
+
return (selectedMatch()?.session.files.length ?? 0) > 0;
|
|
1057
|
+
},
|
|
1058
|
+
get children() {
|
|
1059
|
+
return el(
|
|
1060
|
+
"text",
|
|
1061
|
+
{ fg: () => theme().text.muted, wrapMode: "word" },
|
|
1062
|
+
() =>
|
|
1063
|
+
`Files: ${selectedMatch()
|
|
1064
|
+
?.session.files.slice(0, 4)
|
|
1065
|
+
.map((f: string) => props.context.ui.format.path(f))
|
|
1066
|
+
.join(", ")}${(selectedMatch()?.session.files.length ?? 0) > 4 ? ` (+${(selectedMatch()?.session.files.length ?? 0) - 4} more)` : ""}`
|
|
1067
|
+
);
|
|
1068
|
+
},
|
|
1069
|
+
})
|
|
1070
|
+
),
|
|
1071
|
+
el(
|
|
1072
|
+
"text",
|
|
1073
|
+
{
|
|
1074
|
+
fg: () => theme().text.action.primary.base,
|
|
1075
|
+
attributes: TextAttributes.BOLD,
|
|
1076
|
+
marginTop: 1,
|
|
1077
|
+
},
|
|
1078
|
+
() =>
|
|
1079
|
+
query().trim()
|
|
1080
|
+
? `◈ MATCHING SNIPPETS (${selectedMatch()?.matches.length ?? 0} SHOWN)`
|
|
1081
|
+
: "◈ RECENT CONVERSATION PREVIEW"
|
|
1082
|
+
),
|
|
1083
|
+
createComponent(For, {
|
|
1084
|
+
get each() {
|
|
1085
|
+
return selectedMatch()?.matches ?? [];
|
|
1086
|
+
},
|
|
1087
|
+
children: (hit: SearchSessionMatch["matches"][number], idx: () => number) => {
|
|
1088
|
+
const isActiveSnippet = () => idx() === selectedSnippetIndex();
|
|
1089
|
+
return el(
|
|
1090
|
+
"box",
|
|
1091
|
+
{
|
|
1092
|
+
id: () => `snippet-row-${idx()}`,
|
|
1093
|
+
flexDirection: "column",
|
|
1094
|
+
paddingLeft: 1,
|
|
1095
|
+
marginBottom: 1,
|
|
1096
|
+
border: ["left"],
|
|
1097
|
+
borderColor: () =>
|
|
1098
|
+
isActiveSnippet()
|
|
1099
|
+
? theme().text.action.primary.base
|
|
1100
|
+
: theme().border.base,
|
|
1101
|
+
onMouseUp: () => {
|
|
1102
|
+
setSelectedSnippetIndex(idx());
|
|
1103
|
+
openSelected(hit);
|
|
1104
|
+
},
|
|
1105
|
+
},
|
|
1106
|
+
el(
|
|
1107
|
+
"text",
|
|
1108
|
+
{
|
|
1109
|
+
fg: () =>
|
|
1110
|
+
isActiveSnippet()
|
|
1111
|
+
? theme().text.action.primary.base
|
|
1112
|
+
: theme().text.muted,
|
|
1113
|
+
attributes: TextAttributes.BOLD,
|
|
1114
|
+
},
|
|
1115
|
+
() => {
|
|
1116
|
+
const roleTag =
|
|
1117
|
+
hit.kind === "title"
|
|
1118
|
+
? "[TITLE]"
|
|
1119
|
+
: hit.kind === "fuzzy"
|
|
1120
|
+
? `[FUZZY · ${hit.role.toUpperCase()}]`
|
|
1121
|
+
: hit.kind === "filepath"
|
|
1122
|
+
? "[FILE]"
|
|
1123
|
+
: hit.kind === "tool"
|
|
1124
|
+
? `[TOOL:${hit.toolName || "call"}]`
|
|
1125
|
+
: hit.role === "user"
|
|
1126
|
+
? "[USER]"
|
|
1127
|
+
: "[ASSISTANT]";
|
|
1128
|
+
const prefix = isActiveSnippet() ? "▸ " : "";
|
|
1129
|
+
return `${prefix}${roleTag} · ${formatRelativeTime(hit.timeCreated)}`;
|
|
1130
|
+
}
|
|
1131
|
+
),
|
|
1132
|
+
el(
|
|
1133
|
+
"text",
|
|
1134
|
+
{
|
|
1135
|
+
wrapMode: "word",
|
|
1136
|
+
fg: () => theme().text.base,
|
|
1137
|
+
},
|
|
1138
|
+
createComponent(For, {
|
|
1139
|
+
get each() {
|
|
1140
|
+
return splitHighlightedText(hit.snippet, hit.matchedTerm);
|
|
1141
|
+
},
|
|
1142
|
+
children: (seg: { text: string; match: boolean }) =>
|
|
1143
|
+
el(
|
|
1144
|
+
"span",
|
|
1145
|
+
{
|
|
1146
|
+
fg: () =>
|
|
1147
|
+
seg.match
|
|
1148
|
+
? theme().text.action.primary.base
|
|
1149
|
+
: theme().text.base,
|
|
1150
|
+
attributes: () =>
|
|
1151
|
+
seg.match ? TextAttributes.BOLD : undefined,
|
|
1152
|
+
},
|
|
1153
|
+
seg.text
|
|
1154
|
+
),
|
|
1155
|
+
})
|
|
1156
|
+
)
|
|
1157
|
+
);
|
|
1158
|
+
},
|
|
1159
|
+
})
|
|
1160
|
+
);
|
|
1161
|
+
},
|
|
1162
|
+
})
|
|
1163
|
+
)
|
|
1164
|
+
)
|
|
1165
|
+
)
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
return (props: DialogProps) => createComponent(SessionSearchDialog, props);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1173
|
+
function extractRenderableText(node: any): string {
|
|
1174
|
+
if (!node) return "";
|
|
1175
|
+
if (typeof node.plainText === "string" && node.plainText.length > 0) {
|
|
1176
|
+
return node.plainText;
|
|
1177
|
+
}
|
|
1178
|
+
if (typeof node._content === "string" && node._content.length > 0) {
|
|
1179
|
+
return node._content;
|
|
1180
|
+
}
|
|
1181
|
+
if (typeof node.content === "string" && node.content.length > 0) {
|
|
1182
|
+
return node.content;
|
|
1183
|
+
}
|
|
1184
|
+
if (typeof node.textBuffer?.getPlainText === "function") {
|
|
1185
|
+
try {
|
|
1186
|
+
const text = node.textBuffer.getPlainText();
|
|
1187
|
+
if (typeof text === "string" && text.length > 0) return text;
|
|
1188
|
+
} catch {
|
|
1189
|
+
// ignore buffer read errors
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
return "";
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function normalizeSearchNeedle(raw: string): string {
|
|
1196
|
+
return raw
|
|
1197
|
+
.replace(/^\.{2,}\s*/, "")
|
|
1198
|
+
.replace(/\s*\.{2,}$/, "")
|
|
1199
|
+
.replace(/^\[[^\]]+\]\s*/, "")
|
|
1200
|
+
.replace(/\s+/g, " ")
|
|
1201
|
+
.trim()
|
|
1202
|
+
.toLowerCase();
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
let activeScrollInterval: ReturnType<typeof setInterval> | null = null;
|
|
1206
|
+
let activeScrollTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
1207
|
+
|
|
1208
|
+
function clearActiveScrollTimers() {
|
|
1209
|
+
if (activeScrollInterval) {
|
|
1210
|
+
clearInterval(activeScrollInterval);
|
|
1211
|
+
activeScrollInterval = null;
|
|
1212
|
+
}
|
|
1213
|
+
if (activeScrollTimeout) {
|
|
1214
|
+
clearTimeout(activeScrollTimeout);
|
|
1215
|
+
activeScrollTimeout = null;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function scrollToMatchedMessage(
|
|
1220
|
+
context: TuiPluginContext,
|
|
1221
|
+
sessionId: string,
|
|
1222
|
+
target?: SearchNavigationTarget
|
|
1223
|
+
) {
|
|
1224
|
+
clearActiveScrollTimers();
|
|
1225
|
+
if (!target) return;
|
|
1226
|
+
if (target.kind === "title" && !target.messageId) return;
|
|
1227
|
+
|
|
1228
|
+
try {
|
|
1229
|
+
void Promise.resolve(context.data?.session?.message?.sync?.(sessionId)).catch(
|
|
1230
|
+
() => {}
|
|
1231
|
+
);
|
|
1232
|
+
} catch {
|
|
1233
|
+
// ignore sync errors
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
const rawNeedle = normalizeSearchNeedle(target.snippet || "");
|
|
1237
|
+
const matchedTermLower = (target.matchedTerm || "").trim().toLowerCase();
|
|
1238
|
+
|
|
1239
|
+
// Build candidate phrase needles from most specific to broadest
|
|
1240
|
+
const needles: string[] = [];
|
|
1241
|
+
if (rawNeedle.length >= 8) {
|
|
1242
|
+
needles.push(rawNeedle.slice(0, 48));
|
|
1243
|
+
if (matchedTermLower && rawNeedle.includes(matchedTermLower)) {
|
|
1244
|
+
const idx = rawNeedle.indexOf(matchedTermLower);
|
|
1245
|
+
const start = Math.max(0, idx - 12);
|
|
1246
|
+
const windowSlice = rawNeedle.slice(start, start + 36).trim();
|
|
1247
|
+
if (windowSlice.length >= 6 && !needles.includes(windowSlice)) {
|
|
1248
|
+
needles.push(windowSlice);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
if (matchedTermLower.length >= 2 && !needles.includes(matchedTermLower)) {
|
|
1253
|
+
needles.push(matchedTermLower);
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
let attempts = 0;
|
|
1257
|
+
const maxAttempts = 25;
|
|
1258
|
+
|
|
1259
|
+
const tryScroll = (): boolean => {
|
|
1260
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1261
|
+
const root = (context.renderer as any)?.root;
|
|
1262
|
+
if (!root || typeof root.getChildren !== "function") return false;
|
|
1263
|
+
|
|
1264
|
+
// 1. Find the largest visible ScrollBoxRenderable in the active session view
|
|
1265
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1266
|
+
const scrollBoxes: any[] = [];
|
|
1267
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1268
|
+
const findScrollBoxes = (node: any) => {
|
|
1269
|
+
if (!node || node.isDestroyed || node.visible === false) return;
|
|
1270
|
+
if (
|
|
1271
|
+
typeof node.scrollTo === "function" &&
|
|
1272
|
+
typeof node.scrollBy === "function" &&
|
|
1273
|
+
node.content &&
|
|
1274
|
+
node.viewport
|
|
1275
|
+
) {
|
|
1276
|
+
scrollBoxes.push(node);
|
|
1277
|
+
}
|
|
1278
|
+
const children =
|
|
1279
|
+
typeof node.getChildren === "function" ? node.getChildren() : [];
|
|
1280
|
+
for (const child of children) {
|
|
1281
|
+
findScrollBoxes(child);
|
|
1282
|
+
}
|
|
1283
|
+
};
|
|
1284
|
+
findScrollBoxes(root);
|
|
1285
|
+
if (!scrollBoxes.length) return false;
|
|
1286
|
+
|
|
1287
|
+
scrollBoxes.sort(
|
|
1288
|
+
(a, b) =>
|
|
1289
|
+
(b.viewport?.width ?? 0) * (b.viewport?.height ?? 0) -
|
|
1290
|
+
(a.viewport?.width ?? 0) * (a.viewport?.height ?? 0)
|
|
1291
|
+
);
|
|
1292
|
+
const scrollBox = scrollBoxes[0];
|
|
1293
|
+
const topChildren =
|
|
1294
|
+
typeof scrollBox.content?.getChildren === "function"
|
|
1295
|
+
? scrollBox.content.getChildren()
|
|
1296
|
+
: [];
|
|
1297
|
+
if (!topChildren.length || (scrollBox.scrollHeight ?? 0) <= 0) {
|
|
1298
|
+
return false;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// 2. Determine chronological message ratio (0 = oldest at top, 1 = newest at bottom)
|
|
1302
|
+
let ratio = target.messageRatio;
|
|
1303
|
+
try {
|
|
1304
|
+
const liveMessages = context.data?.session?.message?.list?.(sessionId);
|
|
1305
|
+
if (Array.isArray(liveMessages) && liveMessages.length > 0 && target.messageId) {
|
|
1306
|
+
const msgIdx = liveMessages.findIndex((m) => m?.id === target.messageId);
|
|
1307
|
+
if (msgIdx !== -1) {
|
|
1308
|
+
ratio =
|
|
1309
|
+
liveMessages.length > 1 ? msgIdx / (liveMessages.length - 1) : 0;
|
|
1310
|
+
const liveMsg = liveMessages[msgIdx] as unknown as Record<string, unknown>;
|
|
1311
|
+
if (typeof liveMsg?.text === "string") {
|
|
1312
|
+
const liveNeedle = normalizeSearchNeedle(liveMsg.text).slice(0, 48);
|
|
1313
|
+
if (liveNeedle.length >= 6 && !needles.includes(liveNeedle)) {
|
|
1314
|
+
needles.unshift(liveNeedle);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
} catch {
|
|
1320
|
+
// ignore data store lookup errors
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
// 3. Walk descendants of scrollBox.content to locate the matching message node
|
|
1324
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1325
|
+
let idMatchNode: any = null;
|
|
1326
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1327
|
+
const textMatchesByTier: Array<Array<any>> = needles.map(() => []);
|
|
1328
|
+
|
|
1329
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1330
|
+
const walkContent = (node: any) => {
|
|
1331
|
+
if (!node || node.isDestroyed || node.visible === false) return;
|
|
1332
|
+
if (
|
|
1333
|
+
!idMatchNode &&
|
|
1334
|
+
target.messageId &&
|
|
1335
|
+
typeof node.id === "string" &&
|
|
1336
|
+
(node.id === target.messageId || node.id.includes(target.messageId))
|
|
1337
|
+
) {
|
|
1338
|
+
idMatchNode = node;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
const rawText = extractRenderableText(node);
|
|
1342
|
+
if (rawText) {
|
|
1343
|
+
const normalized = rawText.replace(/\s+/g, " ").toLowerCase();
|
|
1344
|
+
for (let i = 0; i < needles.length; i++) {
|
|
1345
|
+
if (normalized.includes(needles[i])) {
|
|
1346
|
+
textMatchesByTier[i].push(node);
|
|
1347
|
+
break;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
const children =
|
|
1353
|
+
typeof node.getChildren === "function" ? node.getChildren() : [];
|
|
1354
|
+
for (const child of children) {
|
|
1355
|
+
walkContent(child);
|
|
1356
|
+
}
|
|
1357
|
+
};
|
|
1358
|
+
walkContent(scrollBox.content);
|
|
1359
|
+
|
|
1360
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1361
|
+
let targetNode: any = idMatchNode;
|
|
1362
|
+
if (!targetNode) {
|
|
1363
|
+
const candidates = textMatchesByTier.find((tier) => tier.length > 0);
|
|
1364
|
+
if (candidates && candidates.length > 0) {
|
|
1365
|
+
if (candidates.length === 1 || ratio === undefined) {
|
|
1366
|
+
targetNode = candidates[0];
|
|
1367
|
+
} else {
|
|
1368
|
+
const totalHeight = Math.max(1, scrollBox.scrollHeight || 1);
|
|
1369
|
+
const contentTop = scrollBox.content?.y ?? 0;
|
|
1370
|
+
let bestDist = Infinity;
|
|
1371
|
+
for (const candidate of candidates) {
|
|
1372
|
+
const relY = Math.max(0, (candidate.y ?? 0) - contentTop) / totalHeight;
|
|
1373
|
+
const dist = Math.abs(relY - ratio);
|
|
1374
|
+
if (dist < bestDist) {
|
|
1375
|
+
bestDist = dist;
|
|
1376
|
+
targetNode = candidate;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
const applyScrollToNode = (
|
|
1384
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1385
|
+
node: any
|
|
1386
|
+
) => {
|
|
1387
|
+
const viewportTop = scrollBox.viewport?.y ?? 0;
|
|
1388
|
+
const viewportHeight = scrollBox.viewport?.height ?? 20;
|
|
1389
|
+
const desiredScreenY = viewportTop + Math.floor(viewportHeight * 0.22);
|
|
1390
|
+
const dy = (node.y ?? 0) - desiredScreenY;
|
|
1391
|
+
scrollBox._hasManualScroll = true;
|
|
1392
|
+
if (dy !== 0) {
|
|
1393
|
+
scrollBox.scrollBy({ x: 0, y: dy });
|
|
1394
|
+
}
|
|
1395
|
+
scrollBox._hasManualScroll = true;
|
|
1396
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1397
|
+
(context.renderer as any)?.requestRender?.();
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
if (targetNode) {
|
|
1401
|
+
applyScrollToNode(targetNode);
|
|
1402
|
+
if (activeScrollTimeout) clearTimeout(activeScrollTimeout);
|
|
1403
|
+
activeScrollTimeout = setTimeout(() => {
|
|
1404
|
+
activeScrollTimeout = null;
|
|
1405
|
+
try {
|
|
1406
|
+
applyScrollToNode(targetNode);
|
|
1407
|
+
} catch {
|
|
1408
|
+
// ignore
|
|
1409
|
+
}
|
|
1410
|
+
}, 120);
|
|
1411
|
+
return true;
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
// Fallback when text is inside a collapsed tool output and layout has stabilized
|
|
1415
|
+
if (attempts >= 6 && ratio !== undefined) {
|
|
1416
|
+
const childIdx = Math.max(
|
|
1417
|
+
0,
|
|
1418
|
+
Math.min(topChildren.length - 1, Math.round(ratio * (topChildren.length - 1)))
|
|
1419
|
+
);
|
|
1420
|
+
const approxNode = topChildren[childIdx];
|
|
1421
|
+
if (approxNode) {
|
|
1422
|
+
applyScrollToNode(approxNode);
|
|
1423
|
+
return true;
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
return false;
|
|
1428
|
+
};
|
|
1429
|
+
|
|
1430
|
+
activeScrollInterval = setInterval(() => {
|
|
1431
|
+
attempts++;
|
|
1432
|
+
if (tryScroll() || attempts >= maxAttempts) {
|
|
1433
|
+
if (activeScrollInterval) {
|
|
1434
|
+
clearInterval(activeScrollInterval);
|
|
1435
|
+
activeScrollInterval = null;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
}, 60);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
async function openFallbackSelectDialog(
|
|
1442
|
+
context: TuiPluginContext,
|
|
1443
|
+
initialQuery: string,
|
|
1444
|
+
currentDirectory: string
|
|
1445
|
+
) {
|
|
1446
|
+
const index = await loadSessionIndex(false);
|
|
1447
|
+
const matches = searchSessionIndex(index, initialQuery, {
|
|
1448
|
+
currentDirectory,
|
|
1449
|
+
limit: 50,
|
|
1450
|
+
});
|
|
1451
|
+
|
|
1452
|
+
if (!matches.length) {
|
|
1453
|
+
context.ui.toast.show({
|
|
1454
|
+
title: "Session Search",
|
|
1455
|
+
message: initialQuery
|
|
1456
|
+
? `No sessions matched "${initialQuery}".`
|
|
1457
|
+
: "No sessions found.",
|
|
1458
|
+
variant: "warning",
|
|
1459
|
+
});
|
|
1460
|
+
return;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
const options = matches.map((m) => ({
|
|
1464
|
+
title: m.session.title,
|
|
1465
|
+
value: m.session.id,
|
|
1466
|
+
description: `[${m.matchBadge}] ${m.bestSnippet}`,
|
|
1467
|
+
footer: `${formatRelativeTime(m.session.timeUpdated)} · ${context.ui.format.path(m.session.directory || "")}`,
|
|
1468
|
+
}));
|
|
1469
|
+
|
|
1470
|
+
const selectedId = await context.ui.dialog.select<string>({
|
|
1471
|
+
title: initialQuery ? `Session Search: "${initialQuery}"` : "Session Search",
|
|
1472
|
+
placeholder: "Filter matching sessions...",
|
|
1473
|
+
options,
|
|
1474
|
+
});
|
|
1475
|
+
|
|
1476
|
+
if (selectedId) {
|
|
1477
|
+
context.ui.router.navigate({
|
|
1478
|
+
type: "session",
|
|
1479
|
+
sessionID: selectedId,
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
export default {
|
|
1485
|
+
id: "opencode-local-search",
|
|
1486
|
+
async setup(context: TuiPluginContext) {
|
|
1487
|
+
if (!context?.ui) {
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
const currentDirectory = context.location?.directory ?? process.cwd();
|
|
1491
|
+
let renderDialog: ReturnType<typeof createTuiRenderer> | null = null;
|
|
1492
|
+
try {
|
|
1493
|
+
renderDialog = createTuiRenderer();
|
|
1494
|
+
} catch {
|
|
1495
|
+
renderDialog = null;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
const openSearchPopup = async (rawArgs?: unknown) => {
|
|
1499
|
+
const initialQuery =
|
|
1500
|
+
typeof rawArgs === "string"
|
|
1501
|
+
? rawArgs.trim()
|
|
1502
|
+
: typeof (rawArgs as { arguments?: unknown } | undefined)?.arguments === "string"
|
|
1503
|
+
? ((rawArgs as { arguments: string }).arguments).trim()
|
|
1504
|
+
: "";
|
|
1505
|
+
if (!renderDialog) {
|
|
1506
|
+
await openFallbackSelectDialog(context, initialQuery, currentDirectory);
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
try {
|
|
1511
|
+
context.ui.dialog.show(() =>
|
|
1512
|
+
renderDialog({
|
|
1513
|
+
context,
|
|
1514
|
+
initialQuery,
|
|
1515
|
+
currentDirectory,
|
|
1516
|
+
close: () => context.ui.dialog.clear(),
|
|
1517
|
+
navigateSession: (
|
|
1518
|
+
sessionId: string,
|
|
1519
|
+
target?: SearchNavigationTarget
|
|
1520
|
+
) => {
|
|
1521
|
+
context.ui.dialog.clear();
|
|
1522
|
+
context.ui.router.navigate({
|
|
1523
|
+
type: "session",
|
|
1524
|
+
sessionID: sessionId,
|
|
1525
|
+
});
|
|
1526
|
+
scrollToMatchedMessage(context, sessionId, target);
|
|
1527
|
+
},
|
|
1528
|
+
})
|
|
1529
|
+
);
|
|
1530
|
+
context.ui.dialog.set({
|
|
1531
|
+
size: "xlarge",
|
|
1532
|
+
centered: true,
|
|
1533
|
+
});
|
|
1534
|
+
} catch {
|
|
1535
|
+
await openFallbackSelectDialog(context, initialQuery, currentDirectory);
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
|
|
1539
|
+
let layerRegistered = false;
|
|
1540
|
+
context.ui.slot({
|
|
1541
|
+
append: "app",
|
|
1542
|
+
render() {
|
|
1543
|
+
if (!layerRegistered) {
|
|
1544
|
+
layerRegistered = true;
|
|
1545
|
+
context.keymap.layer(() => ({
|
|
1546
|
+
mode: "global",
|
|
1547
|
+
commands: [
|
|
1548
|
+
{
|
|
1549
|
+
id: "session.history.search",
|
|
1550
|
+
title: "Search session history...",
|
|
1551
|
+
description:
|
|
1552
|
+
"Interactive zero-token popup to search past sessions (multi-term, regex, fuzzy, role/date/file/tool filters)",
|
|
1553
|
+
category: "Session",
|
|
1554
|
+
palette: true,
|
|
1555
|
+
slash: {
|
|
1556
|
+
name: "search",
|
|
1557
|
+
arguments: true,
|
|
1558
|
+
},
|
|
1559
|
+
run: (input) => {
|
|
1560
|
+
void openSearchPopup(input);
|
|
1561
|
+
},
|
|
1562
|
+
},
|
|
1563
|
+
],
|
|
1564
|
+
}));
|
|
1565
|
+
}
|
|
1566
|
+
return null;
|
|
1567
|
+
},
|
|
1568
|
+
});
|
|
1569
|
+
},
|
|
1570
|
+
};
|