@javierkaiser/opencode-indexing 0.1.1 → 0.2.1

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/install.mjs CHANGED
@@ -1,17 +1,32 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * Cross-platform installer for opencode-indexing.
3
+ * Installer for opencode-indexing.
4
4
  *
5
5
  * Works on Windows, Linux and macOS (Node >= 22.6; Node 26 recommended).
6
6
  *
7
- * node install.mjs [--skip-deps] [--no-restart]
7
+ * node install.mjs [--local|--package] [spec] [--skip-deps] [--no-restart]
8
+ * npx @javierkaiser/opencode-indexing
8
9
  *
9
- * What it does:
10
- * 1. Copies the plugin into <config>/opencode/plugins/opencode-indexing
11
- * (config dir honors $XDG_CONFIG_HOME, defaulting to ~/.config).
12
- * 2. Installs runtime dependencies (ignore + optional lancedb/tree-sitter).
13
- * 3. Merges tool permissions into <config>/opencode/opencode.json.
14
- * 4. Restarts the OpenCode service so the plugin loads.
10
+ * There are two ways to install, because there are two audiences.
11
+ *
12
+ * --local Copies the plugin into <config>/opencode/plugins/opencode-indexing
13
+ * and installs its runtime dependencies there. This is the
14
+ * development path: edits to the source take effect on restart,
15
+ * with no publish in between.
16
+ *
17
+ * --package Hands the job to `opencode plugin add`, which installs the
18
+ * published package into OpenCode's own cache and records it in the
19
+ * global `plugins` array. This is the user-facing path: OpenCode
20
+ * owns the install, so `opencode plugin check` and `plugin update`
21
+ * work on it.
22
+ *
23
+ * The mode is picked from where this file runs. A checkout has `.git` or `test/`
24
+ * beside it and gets `--local`; anything else — the npx cache, a global install,
25
+ * an unpacked tarball — gets `--package`. Pass either flag to override, and the
26
+ * installer says which one it chose and why.
27
+ *
28
+ * Both modes write the tool permission rules and restart the service, so either
29
+ * one leaves a working install.
15
30
  */
16
31
  import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"
17
32
  import { homedir } from "node:os"
@@ -25,110 +40,44 @@ const skipDeps = has("--skip-deps")
25
40
  const noRestart = has("--no-restart")
26
41
 
27
42
  const source = dirname(fileURLToPath(import.meta.url))
43
+ const sourcePkg = JSON.parse(readFileSync(join(source, "package.json"), "utf8"))
28
44
  const configRoot = process.env.XDG_CONFIG_HOME?.trim() || join(homedir(), ".config")
29
45
  const configDir = join(configRoot, "opencode")
30
46
  const dest = join(configDir, "plugins", "opencode-indexing")
31
47
  const configFile = join(configDir, "opencode.json")
32
48
 
33
49
  const isWindows = process.platform === "win32"
50
+ const opencodeBin = isWindows ? "opencode.cmd" : "opencode"
34
51
  const log = (message) => console.log(message)
35
52
  const warn = (message) => console.warn(message)
36
53
 
37
- log("Installing opencode-indexing")
38
- log(` from: ${source}`)
39
- log(` to: ${dest}`)
40
-
41
- // ---------------------------------------------------------------------------
42
- // 1. Copy plugin files (keep a previously installed node_modules in place)
43
- // ---------------------------------------------------------------------------
44
54
  /**
45
- * Entries copied into the plugin directory.
55
+ * Whether this looks like the repository rather than an installed copy.
46
56
  *
47
- * An allowlist rather than a blocklist: a blocklist copied whatever local debris
48
- * happened to be in the working tree — a stray `test-run.log` was shipped into
49
- * every install. This mirrors the `files` field in package.json, minus the
50
- * installers, which are not needed inside an installed plugin.
57
+ * `.git` and `test/` are both absent from the published tarball, so their
58
+ * presence means someone is running the installer out of a working tree and
59
+ * almost certainly wants the source they are editing to be what gets loaded.
51
60
  */
52
- const COPY_ENTRIES = [
53
- "index.ts",
54
- "tui.ts",
55
- "tui",
56
- "src",
57
- "package.json",
58
- "tsconfig.json",
59
- "README.md",
60
- "LICENSE",
61
- "THIRD_PARTY_NOTICES.md",
62
- ]
63
-
64
- mkdirSync(dest, { recursive: true })
65
- for (const entry of readdirSync(dest)) {
66
- if (entry === "node_modules") continue
67
- rmSync(join(dest, entry), { recursive: true, force: true })
61
+ function looksLikeCheckout() {
62
+ return existsSync(join(source, ".git")) || existsSync(join(source, "test"))
68
63
  }
69
- for (const entry of COPY_ENTRIES) {
70
- const from = join(source, entry)
71
- if (!existsSync(from)) {
72
- warn(` skipping missing entry: ${entry}`)
73
- continue
74
- }
75
- cpSync(from, join(dest, entry), { recursive: true })
76
- }
77
- log(" files copied")
78
64
 
79
- // ---------------------------------------------------------------------------
80
- // 2. Minimal runtime package.json + dependency install
81
- // ---------------------------------------------------------------------------
82
- const sourcePkg = JSON.parse(readFileSync(join(source, "package.json"), "utf8"))
83
- const installedPkg = {
84
- name: sourcePkg.name,
85
- version: sourcePkg.version,
86
- type: "module",
87
- exports: {
88
- ".": "./index.ts",
89
- "./rpc": "./src/rpc.ts",
90
- "./tui": "./tui.ts",
91
- },
92
- dependencies: {
93
- ignore: ">=7.0.0",
94
- },
95
- optionalDependencies: {
96
- "@lancedb/lancedb": "0.26.2",
97
- "web-tree-sitter": "0.25.10",
98
- "tree-sitter-wasms": "0.1.13",
99
- },
100
- // The host (OpenCode TUI) provides the renderer stack; declaring optional
101
- // peers documents the contract without forcing a duplicate install.
102
- peerDependencies: {
103
- "@opencode/theme": ">=2.0.23",
104
- "@opentui/core": ">=0.5.14",
105
- "@opentui/solid": ">=0.5.14",
106
- "solid-js": ">=1.9.12",
107
- },
108
- peerDependenciesMeta: {
109
- "@opencode/theme": { optional: true },
110
- "@opentui/core": { optional: true },
111
- "@opentui/solid": { optional: true },
112
- "solid-js": { optional: true },
113
- },
114
- }
115
- writeFileSync(join(dest, "package.json"), JSON.stringify(installedPkg, null, 2) + "\n", "utf8")
65
+ const mode = has("--local") ? "local" : has("--package") ? "package" : looksLikeCheckout() ? "local" : "package"
66
+ const reason = has("--local") || has("--package") ? "requested" : looksLikeCheckout() ? "a checkout" : "an installed copy"
116
67
 
117
- if (!skipDeps) {
118
- log(" installing runtime deps (ignore + optional lancedb/tree-sitter)...")
119
- const npm = isWindows ? "npm.cmd" : "npm"
120
- const result = spawnSync(
121
- npm,
122
- ["install", "--omit=dev", "--omit=peer", "--no-audit", "--no-fund", "--loglevel=error"],
123
- { cwd: dest, stdio: "inherit", shell: isWindows },
124
- )
125
- if (result.error || result.status !== 0) {
126
- warn(` dependency install failed (${result.error?.message ?? `exit ${result.status}`}); the plugin still runs with reduced features`)
127
- }
128
- }
68
+ // A positional argument is the package spec for --package. `@latest` rather than
69
+ // the version this file was published as, because an exact version is skipped by
70
+ // `opencode plugin check` and `plugin update`: pinning would silently opt the
71
+ // install out of the update path OpenCode provides.
72
+ const positional = argv.filter((arg) => !arg.startsWith("-"))
73
+ const spec = positional[0] ?? `${sourcePkg.name}@latest`
74
+
75
+ log(`Installing ${sourcePkg.name} ${sourcePkg.version}`)
76
+ log(` from: ${source}`)
77
+ log(` mode: ${mode} (${reason})`)
129
78
 
130
79
  // ---------------------------------------------------------------------------
131
- // 3. Merge tool permissions into opencode.json (JSONC tolerant)
80
+ // Tool permissions (both modes)
132
81
  // ---------------------------------------------------------------------------
133
82
  function stripJsonc(input) {
134
83
  // Strip a UTF-8 BOM: editors on Windows may add one and JSON.parse rejects it.
@@ -189,51 +138,203 @@ const TOOLS = ["indexing_search", "indexing_status", "indexing_config", "indexin
189
138
  // Everything else only reads. This one writes the settings file, so the host
190
139
  // asks the user before it runs instead of taking it on trust.
191
140
  const ASK_BEFORE_RUN = new Set(["indexing_config_set"])
192
- try {
193
- let cfg = {}
194
- if (existsSync(configFile)) {
195
- cfg = JSON.parse(stripJsonc(readFileSync(configFile, "utf8")))
196
- if (typeof cfg !== "object" || cfg === null || Array.isArray(cfg)) cfg = {}
197
- }
198
141
 
199
- // Drop the V1 entries this installer used to write. Anything else the user
200
- // put there is left untouched.
201
- if (cfg.permission && typeof cfg.permission === "object" && !Array.isArray(cfg.permission)) {
202
- for (const tool of TOOLS) delete cfg.permission[tool]
203
- if (Object.keys(cfg.permission).length === 0) delete cfg.permission
204
- }
142
+ /**
143
+ * Add one permission rule per tool to the global config.
144
+ *
145
+ * `opencode plugin add` installs the plugin but knows nothing about its tools,
146
+ * so a package-mode install has to write these itself or the `ask` gate on
147
+ * `indexing_config_set` is lost. The read tools would be allowed by the base
148
+ * `{ "*": "allow" }` policy anyway; they are written out so the set is visible
149
+ * and can be tightened in one place.
150
+ */
151
+ function writePermissions() {
152
+ try {
153
+ let cfg = {}
154
+ if (existsSync(configFile)) {
155
+ cfg = JSON.parse(stripJsonc(readFileSync(configFile, "utf8")))
156
+ if (typeof cfg !== "object" || cfg === null || Array.isArray(cfg)) cfg = {}
157
+ }
205
158
 
206
- if (!Array.isArray(cfg.permissions)) cfg.permissions = []
207
- for (const tool of TOOLS) {
208
- const effect = ASK_BEFORE_RUN.has(tool) ? "ask" : "allow"
209
- const rule = cfg.permissions.find((entry) => entry && entry.action === tool)
210
- if (rule) {
211
- rule.resource = "*"
212
- rule.effect = effect
213
- } else {
214
- cfg.permissions.push({ action: tool, resource: "*", effect })
159
+ // Drop the V1 entries this installer used to write. Anything else the user
160
+ // put there is left untouched.
161
+ if (cfg.permission && typeof cfg.permission === "object" && !Array.isArray(cfg.permission)) {
162
+ for (const tool of TOOLS) delete cfg.permission[tool]
163
+ if (Object.keys(cfg.permission).length === 0) delete cfg.permission
215
164
  }
216
- }
217
165
 
218
- mkdirSync(configDir, { recursive: true })
219
- writeFileSync(configFile, JSON.stringify(cfg, null, 2) + "\n", "utf8")
220
- log(` permissions added to ${configFile}`)
221
- } catch (error) {
222
- warn(` could not update ${configFile}: ${error instanceof Error ? error.message : String(error)}`)
166
+ if (!Array.isArray(cfg.permissions)) cfg.permissions = []
167
+ for (const tool of TOOLS) {
168
+ const effect = ASK_BEFORE_RUN.has(tool) ? "ask" : "allow"
169
+ const rule = cfg.permissions.find((entry) => entry && entry.action === tool)
170
+ if (rule) {
171
+ rule.resource = "*"
172
+ rule.effect = effect
173
+ } else {
174
+ cfg.permissions.push({ action: tool, resource: "*", effect })
175
+ }
176
+ }
177
+
178
+ mkdirSync(configDir, { recursive: true })
179
+ writeFileSync(configFile, JSON.stringify(cfg, null, 2) + "\n", "utf8")
180
+ log(` permissions added to ${configFile}`)
181
+ } catch (error) {
182
+ warn(` could not update ${configFile}: ${error instanceof Error ? error.message : String(error)}`)
183
+ }
223
184
  }
224
185
 
225
186
  // ---------------------------------------------------------------------------
226
- // 4. Restart the OpenCode service so the plugin loads
187
+ // Service restart (both modes)
227
188
  // ---------------------------------------------------------------------------
228
- if (!noRestart) {
189
+ function restartService() {
190
+ if (noRestart) return
229
191
  log(" restarting OpenCode service...")
230
- const opencode = isWindows ? "opencode.cmd" : "opencode"
231
- const result = spawnSync(opencode, ["service", "restart"], { stdio: "inherit", shell: isWindows })
192
+ const result = spawnSync(opencodeBin, ["service", "restart"], { stdio: "inherit", shell: isWindows })
232
193
  if (result.error || result.status !== 0) {
233
194
  warn(" could not restart automatically. Run: opencode service restart")
234
195
  }
235
196
  }
236
197
 
198
+ // ---------------------------------------------------------------------------
199
+ // Local mode: copy the working tree into the config directory
200
+ // ---------------------------------------------------------------------------
201
+ /**
202
+ * Entries copied into the plugin directory.
203
+ *
204
+ * An allowlist rather than a blocklist: a blocklist copied whatever local debris
205
+ * happened to be in the working tree — a stray `test-run.log` was shipped into
206
+ * every install. This mirrors the `files` field in package.json, minus the
207
+ * installers, which are not needed inside an installed plugin.
208
+ */
209
+ const COPY_ENTRIES = [
210
+ "index.ts",
211
+ "tui.ts",
212
+ "tui",
213
+ "src",
214
+ "package.json",
215
+ "tsconfig.json",
216
+ "README.md",
217
+ "LICENSE",
218
+ "THIRD_PARTY_NOTICES.md",
219
+ ]
220
+
221
+ function installLocal() {
222
+ log(` to: ${dest}`)
223
+ mkdirSync(dest, { recursive: true })
224
+ for (const entry of readdirSync(dest)) {
225
+ if (entry === "node_modules") continue
226
+ rmSync(join(dest, entry), { recursive: true, force: true })
227
+ }
228
+ for (const entry of COPY_ENTRIES) {
229
+ const from = join(source, entry)
230
+ if (!existsSync(from)) {
231
+ warn(` skipping missing entry: ${entry}`)
232
+ continue
233
+ }
234
+ cpSync(from, join(dest, entry), { recursive: true })
235
+ }
236
+ log(" files copied")
237
+
238
+ // The installed package.json is derived, never hand-written: the list this
239
+ // used to keep in parallel is how `apache-arrow` came to be declared for the
240
+ // installer and missing from the published package, which broke every Kilo
241
+ // LanceDB import for one of the two install paths and not the other.
242
+ const installedPkg = {
243
+ name: sourcePkg.name,
244
+ version: sourcePkg.version,
245
+ type: "module",
246
+ exports: sourcePkg.exports,
247
+ dependencies: sourcePkg.dependencies,
248
+ optionalDependencies: sourcePkg.optionalDependencies,
249
+ peerDependencies: sourcePkg.peerDependencies,
250
+ peerDependenciesMeta: sourcePkg.peerDependenciesMeta,
251
+ }
252
+ writeFileSync(join(dest, "package.json"), JSON.stringify(installedPkg, null, 2) + "\n", "utf8")
253
+
254
+ if (skipDeps) {
255
+ log(" skipping dependency install (--skip-deps)")
256
+ return
257
+ }
258
+
259
+ log(" installing runtime deps (ignore + optional lancedb/tree-sitter)...")
260
+ const npm = isWindows ? "npm.cmd" : "npm"
261
+ // --omit=peer is why apache-arrow has to be a real dependency above: the
262
+ // native LanceDB binary loads without it and throws on first use.
263
+ const result = spawnSync(
264
+ npm,
265
+ ["install", "--omit=dev", "--omit=peer", "--no-audit", "--no-fund", "--loglevel=error"],
266
+ { cwd: dest, stdio: "inherit", shell: isWindows },
267
+ )
268
+ if (result.error || result.status !== 0) {
269
+ warn(` dependency install failed (${result.error?.message ?? `exit ${result.status}`}); the plugin still runs with reduced features`)
270
+ }
271
+ }
272
+
273
+ // ---------------------------------------------------------------------------
274
+ // Package mode: let OpenCode own the install
275
+ // ---------------------------------------------------------------------------
276
+ /** How many entries `opencode plugin list` reports for this plugin. */
277
+ function registeredCount() {
278
+ const result = spawnSync(opencodeBin, ["plugin", "list"], { encoding: "utf8", shell: isWindows })
279
+ if (result.error || result.status !== 0 || typeof result.stdout !== "string") return undefined
280
+ // `plugin list` prints a table: `ID VERSION SOURCE`. Compare the first
281
+ // column exactly rather than by prefix, so a longer id that happens to start
282
+ // the same way is not counted as this plugin.
283
+ return result.stdout
284
+ .split("\n")
285
+ .map((line) => line.trim().split(/\s+/)[0])
286
+ .filter((id) => id === "opencode.indexing").length
287
+ }
288
+
289
+ function installPackage() {
290
+ log(` spec: ${spec}`)
291
+
292
+ const added = spawnSync(opencodeBin, ["plugin", "add", spec], { stdio: "inherit", shell: isWindows })
293
+ if (added.error || added.status !== 0) {
294
+ // Nothing has been removed at this point, so a failure here leaves the
295
+ // previous install exactly as it was.
296
+ warn(` \`opencode plugin add\` failed (${added.error?.message ?? `exit ${added.status}`}); the previous install was left untouched`)
297
+ return false
298
+ }
299
+
300
+ // Remove the copied plugin *after* the package is in place. Both sources
301
+ // define `id: "opencode.indexing"`, and the config directory is scanned
302
+ // automatically, so leaving the copy behind would load the plugin twice.
303
+ if (existsSync(dest)) {
304
+ rmSync(dest, { recursive: true, force: true })
305
+ log(` removed the copied plugin at ${dest} (it would load twice)`)
306
+ }
307
+
308
+ return true
309
+ }
310
+
311
+ // ---------------------------------------------------------------------------
312
+ // Run
313
+ // ---------------------------------------------------------------------------
314
+ if (mode === "local") {
315
+ installLocal()
316
+ } else if (!installPackage()) {
317
+ warn(" not writing permissions or restarting: the install did not complete")
318
+ process.exit(1)
319
+ }
320
+
321
+ writePermissions()
322
+ // Before the count below, not after: `plugin list` asks the running service,
323
+ // which still holds the configuration from before this install.
324
+ restartService()
325
+
326
+ if (mode === "package") {
327
+ const count = registeredCount()
328
+ if (count !== undefined && count > 1) {
329
+ warn(` ${count} entries report id opencode.indexing; only one should. Check \`opencode plugin list\`.`)
330
+ }
331
+ }
332
+
237
333
  log("Done.")
238
- log("Verify with: opencode api get /api/info")
239
- log("Then run in a session: indexing_status (or /indexing-config in any client)")
334
+ if (mode === "package") {
335
+ log("Update with: opencode plugin update")
336
+ log("Remove with: opencode plugin remove " + spec)
337
+ } else {
338
+ log("Verify with: opencode api get /api/info")
339
+ log("Then run in a session: indexing_status (or /indexing-config in any client)")
340
+ }
package/install.ps1 CHANGED
@@ -3,8 +3,11 @@
3
3
  # and macOS all run the same code instead of drifting copies.
4
4
  #
5
5
  # Usage:
6
- # powershell -ExecutionPolicy Bypass -File install.ps1 [-SkipDeps] [-NoRestart]
6
+ # powershell -ExecutionPolicy Bypass -File install.ps1 [-Local|-Package] [-Spec <spec>] [-SkipDeps] [-NoRestart]
7
7
  param(
8
+ [switch]$Local,
9
+ [switch]$Package,
10
+ [string]$Spec,
8
11
  [switch]$SkipDeps,
9
12
  [switch]$NoRestart
10
13
  )
@@ -12,6 +15,9 @@ param(
12
15
  $ErrorActionPreference = "Stop"
13
16
  $script = Join-Path $PSScriptRoot "install.mjs"
14
17
  $forward = @()
18
+ if ($Local) { $forward += "--local" }
19
+ if ($Package) { $forward += "--package" }
20
+ if ($Spec) { $forward += $Spec }
15
21
  if ($SkipDeps) { $forward += "--skip-deps" }
16
22
  if ($NoRestart) { $forward += "--no-restart" }
17
23
 
package/install.sh CHANGED
@@ -3,7 +3,7 @@
3
3
  # Thin wrapper: the cross-platform logic lives in install.mjs.
4
4
  #
5
5
  # Usage:
6
- # sh install.sh [--skip-deps] [--no-restart]
6
+ # sh install.sh [--local|--package] [spec] [--skip-deps] [--no-restart]
7
7
  set -eu
8
8
 
9
9
  dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@javierkaiser/opencode-indexing",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "OpenCode plugin: semantic code search that reuses Kilo Code indexes (Qdrant/LanceDB) and maintains its own independent index",
5
5
  "license": "MIT",
6
6
  "author": "Carlos Javier Lust <javierkaiser@gmail.com>",
@@ -29,6 +29,9 @@
29
29
  "./rpc": "./src/rpc.ts",
30
30
  "./tui": "./tui.ts"
31
31
  },
32
+ "bin": {
33
+ "opencode-indexing": "./install.mjs"
34
+ },
32
35
  "files": [
33
36
  "index.ts",
34
37
  "tui.ts",
@@ -61,7 +64,8 @@
61
64
  "optionalDependencies": {
62
65
  "@lancedb/lancedb": "0.26.2",
63
66
  "tree-sitter-wasms": "0.1.13",
64
- "web-tree-sitter": "0.25.10"
67
+ "web-tree-sitter": "0.25.10",
68
+ "apache-arrow": "18.1.0"
65
69
  },
66
70
  "devDependencies": {
67
71
  "@opencode/plugin": "2.0.23",
package/src/config.ts CHANGED
@@ -63,7 +63,7 @@ interface KiloConfigIndexing {
63
63
  openrouter?: { apiKey?: string }
64
64
  }
65
65
 
66
- const VALID_PROVIDERS: readonly EmbedderProvider[] = [
66
+ export const VALID_PROVIDERS: readonly EmbedderProvider[] = [
67
67
  "mistral",
68
68
  "openai",
69
69
  "ollama",