@nandoblanco/opencode-local-search 1.0.1 → 1.1.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/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- "use strict";
1
+ import server from "./server.ts"
2
2
 
3
- import server from "./server.ts";
4
-
5
- export { server };
6
- export * from "./search.ts";
7
- export default server;
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,107 @@
1
+ import type { JsonSchema } from "@opencode/plugin/rpc"
2
+ import type {
3
+ DatePreset,
4
+ RoleFilter,
5
+ ScopeFilter,
6
+ SearchEntryMatch,
7
+ SearchMode,
8
+ } from "./search.ts"
9
+
10
+ export interface RpcPreviewEntry {
11
+ id: string
12
+ messageId?: string
13
+ role: "user" | "assistant" | "shell"
14
+ kind: "message" | "tool" | "filepath"
15
+ text: string
16
+ toolName?: string
17
+ filePath?: string
18
+ timeCreated: number
19
+ }
20
+
21
+ export interface RpcSessionMatch {
22
+ id: string
23
+ title: string
24
+ directory: string
25
+ directoryNorm: string
26
+ timeCreated: number
27
+ timeUpdated: number
28
+ tools: string[]
29
+ files: string[]
30
+ entryCount: number
31
+ score: number
32
+ matchBadge: string
33
+ bestSnippet: string
34
+ bestRole: "user" | "assistant" | "shell" | "title"
35
+ bestMessageId?: string
36
+ totalMatches: number
37
+ matches: SearchEntryMatch[]
38
+ recentEntries: RpcPreviewEntry[]
39
+ }
40
+
41
+ export interface RpcParsedQueryInfo {
42
+ raw: string
43
+ terms: string[]
44
+ fuzzyTerms: string[]
45
+ regexPattern?: string
46
+ regexFlags?: string
47
+ regexError?: string
48
+ role: RoleFilter
49
+ scope?: ScopeFilter
50
+ fileFilter?: string
51
+ toolFilter?: string
52
+ inFilter?: "title" | "message" | "tool" | "file"
53
+ dateLabel?: string
54
+ effectiveMode: SearchMode
55
+ }
56
+
57
+ export interface SearchQueryInput {
58
+ query: string
59
+ scope?: ScopeFilter
60
+ currentDirectory?: string
61
+ role?: RoleFilter
62
+ datePreset?: DatePreset
63
+ mode?: SearchMode
64
+ limit?: number
65
+ forceReload?: boolean
66
+ }
67
+
68
+ export interface SearchQueryOutput {
69
+ totalSessions: number
70
+ loadedAt: number
71
+ parsed: RpcParsedQueryInfo
72
+ results: RpcSessionMatch[]
73
+ }
74
+
75
+ const searchInputSchema = {
76
+ $schema: "https://json-schema.org/draft/2020-12/schema",
77
+ type: "object",
78
+ properties: {
79
+ query: { type: "string" },
80
+ scope: { type: "string", enum: ["all", "project"] },
81
+ currentDirectory: { type: "string" },
82
+ role: { type: "string", enum: ["all", "user", "assistant", "shell"] },
83
+ datePreset: { type: "string", enum: ["all", "today", "yesterday", "7d", "30d"] },
84
+ mode: { type: "string", enum: ["smart", "fuzzy", "regex"] },
85
+ limit: { type: "number" },
86
+ forceReload: { type: "boolean" },
87
+ },
88
+ required: ["query"],
89
+ additionalProperties: false,
90
+ } as unknown as JsonSchema.JsonSchema<SearchQueryInput>
91
+
92
+ const searchOutputSchema = {
93
+ $schema: "https://json-schema.org/draft/2020-12/schema",
94
+ type: "object",
95
+ additionalProperties: true,
96
+ } as unknown as JsonSchema.JsonSchema<SearchQueryOutput>
97
+
98
+ export const SearchRpc = {
99
+ id: "opencode-local-search",
100
+ methods: {
101
+ query: {
102
+ input: searchInputSchema,
103
+ output: searchOutputSchema,
104
+ },
105
+ },
106
+ events: {},
107
+ } as const
package/src/server.ts CHANGED
@@ -1,8 +1,108 @@
1
- "use strict";
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 default {
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
+ scope,
35
+ currentDirectory,
36
+ role,
37
+ datePreset,
38
+ mode,
39
+ limit,
40
+ })
41
+
42
+ const results: RpcSessionMatch[] = matches.map((m) => ({
43
+ id: m.session.id,
44
+ title: m.session.title,
45
+ directory: m.session.directory,
46
+ directoryNorm: m.session.directoryNorm,
47
+ timeCreated: m.session.timeCreated,
48
+ timeUpdated: m.session.timeUpdated,
49
+ tools: m.session.tools.slice(0, 12),
50
+ files: m.session.files.slice(0, 16),
51
+ entryCount: m.session.entries.length,
52
+ score: m.score,
53
+ matchBadge: m.matchBadge,
54
+ bestSnippet: m.bestSnippet,
55
+ bestRole: m.bestRole,
56
+ bestMessageId: m.bestMatch?.messageId,
57
+ totalMatches: m.totalMatches,
58
+ matches: m.matches.slice(0, 24),
59
+ recentEntries: m.session.entries.slice(-10).map((e) => ({
60
+ id: e.id,
61
+ messageId: e.messageId,
62
+ role: e.role,
63
+ kind: e.kind,
64
+ text: e.text.length > 360 ? `${e.text.slice(0, 357)}...` : e.text,
65
+ toolName: e.toolName,
66
+ filePath: e.filePath,
67
+ timeCreated: e.timeCreated,
68
+ })),
69
+ }))
70
+
71
+ return JSON.parse(
72
+ JSON.stringify({
73
+ totalSessions: index.sessions.length,
74
+ loadedAt: index.loadedAt,
75
+ parsed: {
76
+ raw: parsed.raw,
77
+ terms: parsed.terms,
78
+ fuzzyTerms: parsed.fuzzyTerms,
79
+ regexPattern: parsed.regex?.source,
80
+ regexFlags: parsed.regex?.flags,
81
+ regexError: parsed.regexError,
82
+ role: parsed.role,
83
+ scope: parsed.scope,
84
+ fileFilter: parsed.fileFilter,
85
+ toolFilter: parsed.toolFilter,
86
+ inFilter: parsed.inFilter,
87
+ dateLabel: parsed.dateRange?.label,
88
+ effectiveMode: parsed.effectiveMode,
89
+ },
90
+ results,
91
+ }),
92
+ ) as SearchQueryOutput
93
+ }
94
+
95
+ const server: Plugin.Definition = {
4
96
  id: "opencode-local-search",
5
- async setup() {
6
- // Strictly a zero-token TUI popup plugin. No LLM agent tools are registered.
97
+ async setup(ctx) {
98
+ if (ctx?.rpc?.register) {
99
+ await ctx.rpc.register(SearchRpc, {
100
+ async query(input) {
101
+ return executeSearchQuery(input)
102
+ },
103
+ })
104
+ }
7
105
  },
8
- };
106
+ }
107
+
108
+ export default server