@nandoblanco/opencode-local-search 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +31 -11
- package/cli.js +19 -1
- package/dist/gui-experimental.cjs +1 -0
- package/package.json +23 -3
- package/src/gui.css +662 -0
- package/src/gui.tsx +1251 -0
- package/src/index.ts +6 -6
- package/src/rpc.ts +113 -0
- package/src/search.ts +156 -63
- package/src/server.ts +112 -5
- package/src/sort.ts +5 -0
- package/src/tui.ts +64 -6
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import server from "./server.ts"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
export * from "./
|
|
7
|
-
export
|
|
3
|
+
export { server }
|
|
4
|
+
export default server
|
|
5
|
+
export * from "./search.ts"
|
|
6
|
+
export * from "./rpc.ts"
|
|
7
|
+
export { executeSearchQuery } from "./server.ts"
|
package/src/rpc.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { JsonSchema } from "@opencode/plugin/rpc"
|
|
2
|
+
import type {
|
|
3
|
+
DatePreset,
|
|
4
|
+
RoleFilter,
|
|
5
|
+
ScopeFilter,
|
|
6
|
+
SearchEntryMatch,
|
|
7
|
+
SearchMode,
|
|
8
|
+
SearchSort,
|
|
9
|
+
} from "./search.ts"
|
|
10
|
+
|
|
11
|
+
export interface RpcPreviewEntry {
|
|
12
|
+
id: string
|
|
13
|
+
messageId?: string
|
|
14
|
+
role: "user" | "assistant" | "shell"
|
|
15
|
+
kind: "message" | "tool" | "filepath"
|
|
16
|
+
text: string
|
|
17
|
+
toolName?: string
|
|
18
|
+
filePath?: string
|
|
19
|
+
timeCreated: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RpcSessionMatch {
|
|
23
|
+
id: string
|
|
24
|
+
title: string
|
|
25
|
+
directory: string
|
|
26
|
+
directoryNorm: string
|
|
27
|
+
timeCreated: number
|
|
28
|
+
timeUpdated: number
|
|
29
|
+
archived?: boolean
|
|
30
|
+
contextTokens?: number
|
|
31
|
+
totalTokens?: number
|
|
32
|
+
tools: string[]
|
|
33
|
+
files: string[]
|
|
34
|
+
entryCount: number
|
|
35
|
+
score: number
|
|
36
|
+
matchBadge: string
|
|
37
|
+
bestSnippet: string
|
|
38
|
+
bestRole: "user" | "assistant" | "shell" | "title"
|
|
39
|
+
bestMessageId?: string
|
|
40
|
+
totalMatches: number
|
|
41
|
+
matches: SearchEntryMatch[]
|
|
42
|
+
recentEntries: RpcPreviewEntry[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RpcParsedQueryInfo {
|
|
46
|
+
raw: string
|
|
47
|
+
terms: string[]
|
|
48
|
+
fuzzyTerms: string[]
|
|
49
|
+
regexPattern?: string
|
|
50
|
+
regexFlags?: string
|
|
51
|
+
regexError?: string
|
|
52
|
+
role: RoleFilter
|
|
53
|
+
scope?: ScopeFilter
|
|
54
|
+
fileFilter?: string
|
|
55
|
+
toolFilter?: string
|
|
56
|
+
inFilter?: "title" | "message" | "tool" | "file"
|
|
57
|
+
dateLabel?: string
|
|
58
|
+
effectiveMode: SearchMode
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface SearchQueryInput {
|
|
62
|
+
sort?: SearchSort
|
|
63
|
+
query: string
|
|
64
|
+
scope?: ScopeFilter
|
|
65
|
+
currentDirectory?: string
|
|
66
|
+
role?: RoleFilter
|
|
67
|
+
datePreset?: DatePreset
|
|
68
|
+
mode?: SearchMode
|
|
69
|
+
limit?: number
|
|
70
|
+
forceReload?: boolean
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface SearchQueryOutput {
|
|
74
|
+
totalSessions: number
|
|
75
|
+
loadedAt: number
|
|
76
|
+
parsed: RpcParsedQueryInfo
|
|
77
|
+
results: RpcSessionMatch[]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const searchInputSchema = {
|
|
81
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
82
|
+
type: "object",
|
|
83
|
+
properties: {
|
|
84
|
+
query: { type: "string" },
|
|
85
|
+
sort: { type: "string", enum: ["updated", "relevance", "newest", "oldest", "title", "context", "tokens"] },
|
|
86
|
+
scope: { type: "string", enum: ["all", "project"] },
|
|
87
|
+
currentDirectory: { type: "string" },
|
|
88
|
+
role: { type: "string", enum: ["all", "user", "assistant", "shell"] },
|
|
89
|
+
datePreset: { type: "string", enum: ["all", "today", "yesterday", "7d", "30d"] },
|
|
90
|
+
mode: { type: "string", enum: ["smart", "fuzzy", "regex"] },
|
|
91
|
+
limit: { type: "number" },
|
|
92
|
+
forceReload: { type: "boolean" },
|
|
93
|
+
},
|
|
94
|
+
required: ["query"],
|
|
95
|
+
additionalProperties: false,
|
|
96
|
+
} as unknown as JsonSchema.JsonSchema<SearchQueryInput>
|
|
97
|
+
|
|
98
|
+
const searchOutputSchema = {
|
|
99
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
100
|
+
type: "object",
|
|
101
|
+
additionalProperties: true,
|
|
102
|
+
} as unknown as JsonSchema.JsonSchema<SearchQueryOutput>
|
|
103
|
+
|
|
104
|
+
export const SearchRpc = {
|
|
105
|
+
id: "opencode-local-search",
|
|
106
|
+
methods: {
|
|
107
|
+
query: {
|
|
108
|
+
input: searchInputSchema,
|
|
109
|
+
output: searchOutputSchema,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
events: {},
|
|
113
|
+
} as const
|
package/src/search.ts
CHANGED
|
@@ -8,6 +8,8 @@ export type SearchMode = "smart" | "fuzzy" | "regex";
|
|
|
8
8
|
export type RoleFilter = "all" | "user" | "assistant" | "shell";
|
|
9
9
|
export type ScopeFilter = "all" | "project";
|
|
10
10
|
export type DatePreset = "all" | "today" | "yesterday" | "7d" | "30d";
|
|
11
|
+
import type { SearchSort } from "./sort.ts";
|
|
12
|
+
export { SEARCH_SORT_LABELS, type SearchSort } from "./sort.ts";
|
|
11
13
|
|
|
12
14
|
export interface SearchEntry {
|
|
13
15
|
id: string;
|
|
@@ -29,6 +31,9 @@ export interface IndexedSession {
|
|
|
29
31
|
directoryNorm: string;
|
|
30
32
|
timeCreated: number;
|
|
31
33
|
timeUpdated: number;
|
|
34
|
+
archived?: boolean;
|
|
35
|
+
contextTokens?: number;
|
|
36
|
+
totalTokens?: number;
|
|
32
37
|
tools: string[];
|
|
33
38
|
files: string[];
|
|
34
39
|
entries: SearchEntry[];
|
|
@@ -63,6 +68,7 @@ export interface SearchSessionMatch {
|
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
export interface SearchOptions {
|
|
71
|
+
sort?: SearchSort;
|
|
66
72
|
scope?: ScopeFilter;
|
|
67
73
|
currentDirectory?: string;
|
|
68
74
|
role?: RoleFilter;
|
|
@@ -92,9 +98,13 @@ interface SqliteRowsDriver {
|
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
let cachedIndex: SessionIndex | null = null;
|
|
101
|
+
let cachedSessionsMap: Map<string, IndexedSession> | null = null;
|
|
102
|
+
let cachedSessionFiles: Map<string, Set<string>> | null = null;
|
|
103
|
+
let cachedSessionTools: Map<string, Set<string>> | null = null;
|
|
104
|
+
let cachedMaxUpdated = 0;
|
|
95
105
|
let cachedIndexEvictTimer: ReturnType<typeof setTimeout> | null = null;
|
|
96
|
-
const INDEX_TTL_MS =
|
|
97
|
-
const INDEX_IDLE_EVICT_MS =
|
|
106
|
+
const INDEX_TTL_MS = 2000; // Fast in-memory hit window before incremental check
|
|
107
|
+
const INDEX_IDLE_EVICT_MS = 30 * 60_000; // Keep warm index in memory for 30m idle
|
|
98
108
|
const MAX_INDEXED_TEXT_CHARS = 4000;
|
|
99
109
|
|
|
100
110
|
function capIndexedText(text: string): string {
|
|
@@ -107,6 +117,10 @@ function scheduleIndexIdleEviction(): void {
|
|
|
107
117
|
}
|
|
108
118
|
cachedIndexEvictTimer = setTimeout(() => {
|
|
109
119
|
cachedIndex = null;
|
|
120
|
+
cachedSessionsMap = null;
|
|
121
|
+
cachedSessionFiles = null;
|
|
122
|
+
cachedSessionTools = null;
|
|
123
|
+
cachedMaxUpdated = 0;
|
|
110
124
|
cachedIndexEvictTimer = null;
|
|
111
125
|
}, INDEX_IDLE_EVICT_MS);
|
|
112
126
|
cachedIndexEvictTimer.unref?.();
|
|
@@ -240,16 +254,30 @@ function stringifyToolInput(input: unknown): string {
|
|
|
240
254
|
export async function loadSessionIndex(force = false, customDbPath?: string): Promise<SessionIndex> {
|
|
241
255
|
const dbPath = resolveDbPath(customDbPath);
|
|
242
256
|
if (!force && cachedIndex && cachedIndex.dbPath === dbPath && Date.now() - cachedIndex.loadedAt < INDEX_TTL_MS) {
|
|
257
|
+
scheduleIndexIdleEviction();
|
|
243
258
|
return cachedIndex;
|
|
244
259
|
}
|
|
245
260
|
if (!existsSync(dbPath)) {
|
|
246
261
|
return { dbPath, loadedAt: Date.now(), sessions: [] };
|
|
247
262
|
}
|
|
248
263
|
|
|
264
|
+
const canIncremental =
|
|
265
|
+
!force &&
|
|
266
|
+
cachedIndex !== null &&
|
|
267
|
+
cachedIndex.dbPath === dbPath &&
|
|
268
|
+
cachedSessionsMap !== null &&
|
|
269
|
+
cachedSessionFiles !== null &&
|
|
270
|
+
cachedSessionTools !== null;
|
|
271
|
+
|
|
272
|
+
if (!canIncremental) {
|
|
273
|
+
cachedMaxUpdated = 0;
|
|
274
|
+
}
|
|
275
|
+
|
|
249
276
|
const db = await openReadOnlyDb(dbPath);
|
|
250
|
-
const sessionsMap = new Map<string, IndexedSession>();
|
|
251
|
-
const sessionFiles = new Map<string, Set<string>>();
|
|
252
|
-
const sessionTools = new Map<string, Set<string>>();
|
|
277
|
+
const sessionsMap = canIncremental ? cachedSessionsMap! : new Map<string, IndexedSession>();
|
|
278
|
+
const sessionFiles = canIncremental ? cachedSessionFiles! : new Map<string, Set<string>>();
|
|
279
|
+
const sessionTools = canIncremental ? cachedSessionTools! : new Map<string, Set<string>>();
|
|
280
|
+
const changedIds: string[] = [];
|
|
253
281
|
|
|
254
282
|
try {
|
|
255
283
|
const tables = new Set(
|
|
@@ -268,21 +296,35 @@ export async function loadSessionIndex(force = false, customDbPath?: string): Pr
|
|
|
268
296
|
: v2Cols.has("workspace_directory")
|
|
269
297
|
? "COALESCE(workspace_directory, root_directory, '')"
|
|
270
298
|
: "''";
|
|
271
|
-
const
|
|
299
|
+
const archiveExpr = v2Cols.has("time_archived") ? "time_archived" : "NULL";
|
|
300
|
+
|
|
301
|
+
const v2Sessions = canIncremental
|
|
302
|
+
? db.all(
|
|
303
|
+
`SELECT id, title, ${dirExpr} AS directory, time_created, time_updated, ${archiveExpr} AS time_archived
|
|
304
|
+
FROM session_v2
|
|
305
|
+
WHERE time_updated > ?
|
|
306
|
+
ORDER BY time_updated DESC`,
|
|
307
|
+
cachedMaxUpdated
|
|
308
|
+
)
|
|
309
|
+
: db.all(`
|
|
310
|
+
SELECT id, title, ${dirExpr} AS directory, time_created, time_updated, ${archiveExpr} AS time_archived
|
|
311
|
+
FROM session_v2
|
|
312
|
+
ORDER BY time_updated DESC
|
|
313
|
+
`);
|
|
272
314
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
LIMIT 800
|
|
279
|
-
`);
|
|
315
|
+
if (canIncremental && v2Sessions.length === 0) {
|
|
316
|
+
cachedIndex!.loadedAt = Date.now();
|
|
317
|
+
scheduleIndexIdleEviction();
|
|
318
|
+
return cachedIndex!;
|
|
319
|
+
}
|
|
280
320
|
|
|
281
321
|
for (const row of v2Sessions) {
|
|
282
322
|
const id = String(row.id || "");
|
|
283
323
|
if (!id) continue;
|
|
284
324
|
const title = String(row.title || "(untitled session)");
|
|
285
325
|
const directory = String(row.directory || "");
|
|
326
|
+
const timeUpdated = Number(row.time_updated || 0);
|
|
327
|
+
if (timeUpdated > cachedMaxUpdated) cachedMaxUpdated = timeUpdated;
|
|
286
328
|
sessionsMap.set(id, {
|
|
287
329
|
id,
|
|
288
330
|
title,
|
|
@@ -290,43 +332,75 @@ export async function loadSessionIndex(force = false, customDbPath?: string): Pr
|
|
|
290
332
|
directory,
|
|
291
333
|
directoryNorm: normalizePath(directory),
|
|
292
334
|
timeCreated: Number(row.time_created || 0),
|
|
293
|
-
timeUpdated
|
|
335
|
+
timeUpdated,
|
|
336
|
+
archived: row.time_archived != null,
|
|
294
337
|
tools: [],
|
|
295
338
|
files: [],
|
|
296
339
|
entries: [],
|
|
297
340
|
});
|
|
298
341
|
sessionFiles.set(id, new Set());
|
|
299
342
|
sessionTools.set(id, new Set());
|
|
343
|
+
changedIds.push(id);
|
|
300
344
|
}
|
|
301
345
|
|
|
302
346
|
if (tables.has("session_message") && sessionsMap.size > 0) {
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
347
|
+
const targetIds = canIncremental ? changedIds : null;
|
|
348
|
+
const idFilterSql =
|
|
349
|
+
targetIds && targetIds.length > 0
|
|
350
|
+
? ` AND session_id IN (${targetIds.map(() => "?").join(",")})`
|
|
351
|
+
: "";
|
|
352
|
+
const idJoinFilterSql =
|
|
353
|
+
targetIds && targetIds.length > 0
|
|
354
|
+
? ` AND m.session_id IN (${targetIds.map(() => "?").join(",")})`
|
|
355
|
+
: "";
|
|
356
|
+
const idParams = targetIds && targetIds.length > 0 ? targetIds : [];
|
|
357
|
+
|
|
358
|
+
const msgWhere = `WHERE m.type IN ('user', 'assistant', 'shell')${idJoinFilterSql}`;
|
|
359
|
+
// Usage is read across assistant messages for loaded/updated sessions.
|
|
360
|
+
const usageRows = db.all(
|
|
361
|
+
`SELECT session_id, time_created, id, json_extract(data, '$.tokens') AS tokens
|
|
362
|
+
FROM session_message
|
|
363
|
+
WHERE type = 'assistant' AND json_valid(data)
|
|
364
|
+
AND json_type(data, '$.tokens') = 'object'${idFilterSql}
|
|
365
|
+
ORDER BY time_created ASC, id ASC`,
|
|
366
|
+
...idParams
|
|
367
|
+
);
|
|
368
|
+
for (const row of usageRows) {
|
|
369
|
+
const session = sessionsMap.get(String(row.session_id));
|
|
370
|
+
if (!session) continue;
|
|
371
|
+
const tokens = JSON.parse(String(row.tokens)) as {
|
|
372
|
+
input: number; output: number; reasoning: number; cache: { read: number; write: number };
|
|
373
|
+
};
|
|
374
|
+
const input = tokens.input + tokens.cache.read + tokens.cache.write;
|
|
375
|
+
session.contextTokens = input;
|
|
376
|
+
session.totalTokens = (session.totalTokens ?? 0) + input + tokens.output + tokens.reasoning;
|
|
377
|
+
}
|
|
306
378
|
let v2Messages: Record<string, unknown>[];
|
|
307
379
|
try {
|
|
308
|
-
v2Messages = db.all(
|
|
309
|
-
SELECT id, session_id, type, time_created, data
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
380
|
+
v2Messages = db.all(
|
|
381
|
+
`SELECT id, session_id, type, time_created, data
|
|
382
|
+
FROM (
|
|
383
|
+
SELECT m.id, m.session_id, m.type, m.time_created, m.data,
|
|
384
|
+
ROW_NUMBER() OVER (PARTITION BY m.session_id ORDER BY m.time_created DESC) AS rn
|
|
385
|
+
FROM session_message m
|
|
386
|
+
JOIN session_v2 s ON s.id = m.session_id
|
|
387
|
+
${msgWhere}
|
|
388
|
+
)
|
|
389
|
+
WHERE rn <= 250
|
|
390
|
+
ORDER BY time_created DESC
|
|
391
|
+
LIMIT 30000`,
|
|
392
|
+
...idParams
|
|
393
|
+
);
|
|
321
394
|
} catch {
|
|
322
|
-
v2Messages = db.all(
|
|
323
|
-
SELECT m.id, m.session_id, m.type, m.time_created, m.data
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
395
|
+
v2Messages = db.all(
|
|
396
|
+
`SELECT m.id, m.session_id, m.type, m.time_created, m.data
|
|
397
|
+
FROM session_message m
|
|
398
|
+
JOIN session_v2 s ON s.id = m.session_id
|
|
399
|
+
${msgWhere}
|
|
400
|
+
ORDER BY m.time_created DESC
|
|
401
|
+
LIMIT 18000`,
|
|
402
|
+
...idParams
|
|
403
|
+
);
|
|
330
404
|
}
|
|
331
405
|
|
|
332
406
|
for (const row of v2Messages) {
|
|
@@ -468,14 +542,16 @@ export async function loadSessionIndex(force = false, customDbPath?: string): Pr
|
|
|
468
542
|
}
|
|
469
543
|
|
|
470
544
|
// 2. OpenCode V1 Fallback Schema (session + message + part)
|
|
471
|
-
if (tables.has("session") && tables.has("message") && tables.has("part")) {
|
|
545
|
+
if (!canIncremental && tables.has("session") && tables.has("message") && tables.has("part")) {
|
|
546
|
+
const v1Cols = new Set(db.all("PRAGMA table_info(session)").map((col) => String(col.name || "")));
|
|
547
|
+
const v1ArchiveExpr = v1Cols.has("time_archived") ? "time_archived" : "NULL";
|
|
472
548
|
const v1Sessions = db.all(`
|
|
473
|
-
SELECT id, title, directory, time_created, time_updated
|
|
549
|
+
SELECT id, title, directory, time_created, time_updated, ${v1ArchiveExpr} AS time_archived
|
|
474
550
|
FROM session
|
|
475
551
|
ORDER BY time_updated DESC
|
|
476
|
-
LIMIT 500
|
|
477
552
|
`);
|
|
478
553
|
|
|
554
|
+
let v1AddedCount = 0;
|
|
479
555
|
for (const row of v1Sessions) {
|
|
480
556
|
const id = String(row.id || "");
|
|
481
557
|
if (!id || sessionsMap.has(id)) continue;
|
|
@@ -489,38 +565,42 @@ export async function loadSessionIndex(force = false, customDbPath?: string): Pr
|
|
|
489
565
|
directoryNorm: normalizePath(directory),
|
|
490
566
|
timeCreated: Number(row.time_created || 0),
|
|
491
567
|
timeUpdated: Number(row.time_updated || 0),
|
|
568
|
+
archived: row.time_archived != null,
|
|
492
569
|
tools: [],
|
|
493
570
|
files: [],
|
|
494
571
|
entries: [],
|
|
495
572
|
});
|
|
496
573
|
sessionFiles.set(id, new Set());
|
|
497
574
|
sessionTools.set(id, new Set());
|
|
575
|
+
v1AddedCount++;
|
|
498
576
|
}
|
|
499
577
|
|
|
500
|
-
let v1Parts: Record<string, unknown>[];
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
578
|
+
let v1Parts: Record<string, unknown>[] = [];
|
|
579
|
+
if (v1AddedCount > 0) {
|
|
580
|
+
try {
|
|
581
|
+
v1Parts = db.all(`
|
|
582
|
+
SELECT id, message_id, session_id, time_created, part_data, role
|
|
583
|
+
FROM (
|
|
584
|
+
SELECT p.id, p.message_id, p.session_id, p.time_created, p.data AS part_data,
|
|
585
|
+
json_extract(m.data, '$.role') AS role,
|
|
586
|
+
ROW_NUMBER() OVER (PARTITION BY p.session_id ORDER BY p.time_created DESC) AS rn
|
|
587
|
+
FROM part p
|
|
588
|
+
JOIN message m ON m.id = p.message_id
|
|
589
|
+
)
|
|
590
|
+
WHERE rn <= 250
|
|
591
|
+
ORDER BY time_created DESC
|
|
592
|
+
LIMIT 25000
|
|
593
|
+
`);
|
|
594
|
+
} catch {
|
|
595
|
+
v1Parts = db.all(`
|
|
505
596
|
SELECT p.id, p.message_id, p.session_id, p.time_created, p.data AS part_data,
|
|
506
|
-
json_extract(m.data, '$.role') AS role
|
|
507
|
-
ROW_NUMBER() OVER (PARTITION BY p.session_id ORDER BY p.time_created DESC) AS rn
|
|
597
|
+
json_extract(m.data, '$.role') AS role
|
|
508
598
|
FROM part p
|
|
509
599
|
JOIN message m ON m.id = p.message_id
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
`);
|
|
515
|
-
} catch {
|
|
516
|
-
v1Parts = db.all(`
|
|
517
|
-
SELECT p.id, p.message_id, p.session_id, p.time_created, p.data AS part_data,
|
|
518
|
-
json_extract(m.data, '$.role') AS role
|
|
519
|
-
FROM part p
|
|
520
|
-
JOIN message m ON m.id = p.message_id
|
|
521
|
-
ORDER BY p.time_created DESC
|
|
522
|
-
LIMIT 12000
|
|
523
|
-
`);
|
|
600
|
+
ORDER BY p.time_created DESC
|
|
601
|
+
LIMIT 12000
|
|
602
|
+
`);
|
|
603
|
+
}
|
|
524
604
|
}
|
|
525
605
|
|
|
526
606
|
for (const row of v1Parts) {
|
|
@@ -602,6 +682,9 @@ export async function loadSessionIndex(force = false, customDbPath?: string): Pr
|
|
|
602
682
|
}
|
|
603
683
|
|
|
604
684
|
const sessions = [...sessionsMap.values()].sort((a, b) => b.timeUpdated - a.timeUpdated);
|
|
685
|
+
cachedSessionsMap = sessionsMap;
|
|
686
|
+
cachedSessionFiles = sessionFiles;
|
|
687
|
+
cachedSessionTools = sessionTools;
|
|
605
688
|
cachedIndex = {
|
|
606
689
|
dbPath,
|
|
607
690
|
loadedAt: Date.now(),
|
|
@@ -1272,6 +1355,16 @@ export function searchSessionIndex(
|
|
|
1272
1355
|
});
|
|
1273
1356
|
}
|
|
1274
1357
|
|
|
1275
|
-
|
|
1358
|
+
const sort = options.sort ?? "updated";
|
|
1359
|
+
results.sort((a, b) => {
|
|
1360
|
+
let difference = 0;
|
|
1361
|
+
if (sort === "relevance") difference = b.score - a.score;
|
|
1362
|
+
if (sort === "newest") difference = b.session.timeCreated - a.session.timeCreated;
|
|
1363
|
+
if (sort === "oldest") difference = a.session.timeCreated - b.session.timeCreated;
|
|
1364
|
+
if (sort === "title") difference = a.session.title.localeCompare(b.session.title);
|
|
1365
|
+
if (sort === "context") difference = (b.session.contextTokens ?? -1) - (a.session.contextTokens ?? -1);
|
|
1366
|
+
if (sort === "tokens") difference = (b.session.totalTokens ?? -1) - (a.session.totalTokens ?? -1);
|
|
1367
|
+
return difference || b.session.timeUpdated - a.session.timeUpdated || a.session.id.localeCompare(b.session.id);
|
|
1368
|
+
});
|
|
1276
1369
|
return results.slice(0, limit);
|
|
1277
1370
|
}
|
package/src/server.ts
CHANGED
|
@@ -1,8 +1,115 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Plugin } from "@opencode/plugin"
|
|
2
|
+
import {
|
|
3
|
+
loadSessionIndex,
|
|
4
|
+
parseSearchQuery,
|
|
5
|
+
searchSessionIndex,
|
|
6
|
+
} from "./search.ts"
|
|
7
|
+
import {
|
|
8
|
+
SearchRpc,
|
|
9
|
+
type RpcSessionMatch,
|
|
10
|
+
type SearchQueryInput,
|
|
11
|
+
type SearchQueryOutput,
|
|
12
|
+
} from "./rpc.ts"
|
|
2
13
|
|
|
3
|
-
export
|
|
14
|
+
export async function executeSearchQuery(
|
|
15
|
+
input: SearchQueryInput,
|
|
16
|
+
customDbPath?: string,
|
|
17
|
+
): Promise<SearchQueryOutput> {
|
|
18
|
+
const query = input.query ?? ""
|
|
19
|
+
const scope = input.scope ?? "project"
|
|
20
|
+
const currentDirectory = input.currentDirectory ?? ""
|
|
21
|
+
const role = input.role ?? "all"
|
|
22
|
+
const datePreset = input.datePreset ?? "all"
|
|
23
|
+
const mode = input.mode ?? "smart"
|
|
24
|
+
const limit = input.limit ?? 60
|
|
25
|
+
|
|
26
|
+
const index = await loadSessionIndex(Boolean(input.forceReload), customDbPath)
|
|
27
|
+
const parsed = parseSearchQuery(query, {
|
|
28
|
+
mode,
|
|
29
|
+
role,
|
|
30
|
+
scope,
|
|
31
|
+
datePreset,
|
|
32
|
+
})
|
|
33
|
+
const matches = searchSessionIndex(index, query, {
|
|
34
|
+
sort: input.sort,
|
|
35
|
+
scope,
|
|
36
|
+
currentDirectory,
|
|
37
|
+
role,
|
|
38
|
+
datePreset,
|
|
39
|
+
mode,
|
|
40
|
+
limit,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const results: RpcSessionMatch[] = matches.map((m) => ({
|
|
44
|
+
id: m.session.id,
|
|
45
|
+
title: m.session.title,
|
|
46
|
+
directory: m.session.directory,
|
|
47
|
+
directoryNorm: m.session.directoryNorm,
|
|
48
|
+
timeCreated: m.session.timeCreated,
|
|
49
|
+
timeUpdated: m.session.timeUpdated,
|
|
50
|
+
archived: m.session.archived,
|
|
51
|
+
contextTokens: m.session.contextTokens,
|
|
52
|
+
totalTokens: m.session.totalTokens,
|
|
53
|
+
tools: m.session.tools.slice(0, 12),
|
|
54
|
+
files: m.session.files.slice(0, 16),
|
|
55
|
+
entryCount: m.session.entries.length,
|
|
56
|
+
score: m.score,
|
|
57
|
+
matchBadge: m.matchBadge,
|
|
58
|
+
bestSnippet: m.bestSnippet,
|
|
59
|
+
bestRole: m.bestRole,
|
|
60
|
+
bestMessageId: m.bestMatch?.messageId,
|
|
61
|
+
totalMatches: m.totalMatches,
|
|
62
|
+
matches: m.matches.slice(0, 24),
|
|
63
|
+
recentEntries: m.session.entries.slice(-10).map((e) => ({
|
|
64
|
+
id: e.id,
|
|
65
|
+
messageId: e.messageId,
|
|
66
|
+
role: e.role,
|
|
67
|
+
kind: e.kind,
|
|
68
|
+
text: e.text.length > 360 ? `${e.text.slice(0, 357)}...` : e.text,
|
|
69
|
+
toolName: e.toolName,
|
|
70
|
+
filePath: e.filePath,
|
|
71
|
+
timeCreated: e.timeCreated,
|
|
72
|
+
})),
|
|
73
|
+
}))
|
|
74
|
+
|
|
75
|
+
return JSON.parse(
|
|
76
|
+
JSON.stringify({
|
|
77
|
+
totalSessions: index.sessions.length,
|
|
78
|
+
loadedAt: index.loadedAt,
|
|
79
|
+
parsed: {
|
|
80
|
+
raw: parsed.raw,
|
|
81
|
+
terms: parsed.terms,
|
|
82
|
+
fuzzyTerms: parsed.fuzzyTerms,
|
|
83
|
+
regexPattern: parsed.regex?.source,
|
|
84
|
+
regexFlags: parsed.regex?.flags,
|
|
85
|
+
regexError: parsed.regexError,
|
|
86
|
+
role: parsed.role,
|
|
87
|
+
scope: parsed.scope,
|
|
88
|
+
fileFilter: parsed.fileFilter,
|
|
89
|
+
toolFilter: parsed.toolFilter,
|
|
90
|
+
inFilter: parsed.inFilter,
|
|
91
|
+
dateLabel: parsed.dateRange?.label,
|
|
92
|
+
effectiveMode: parsed.effectiveMode,
|
|
93
|
+
},
|
|
94
|
+
results,
|
|
95
|
+
}),
|
|
96
|
+
) as SearchQueryOutput
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const server: Plugin.Definition = {
|
|
4
100
|
id: "opencode-local-search",
|
|
5
|
-
async setup() {
|
|
6
|
-
|
|
101
|
+
async setup(ctx) {
|
|
102
|
+
if (ctx?.rpc?.register) {
|
|
103
|
+
await ctx.rpc.register(SearchRpc, {
|
|
104
|
+
async query(input) {
|
|
105
|
+
return executeSearchQuery(input)
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
void loadSessionIndex(false).catch(() => {})
|
|
111
|
+
}, 50).unref?.()
|
|
7
112
|
},
|
|
8
|
-
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default server
|
package/src/sort.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type SearchSort = "updated" | "relevance" | "newest" | "oldest" | "title" | "context" | "tokens";
|
|
2
|
+
export const SEARCH_SORT_LABELS: Record<SearchSort, string> = {
|
|
3
|
+
updated: "Recently updated", relevance: "Relevance", newest: "Newest created",
|
|
4
|
+
oldest: "Oldest created", title: "Title A–Z", context: "Latest context size", tokens: "Total tokens used",
|
|
5
|
+
};
|