opencode-ghost 0.1.1 → 0.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 CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2026-09-24
11
+
12
+ ### Added
13
+
14
+ - Slash-command autocomplete while typing: the remaining command name is
15
+ ghosted from configured commands, skills, and builtin TUI commands, with
16
+ known argument options shown as `[a | b]` hints and completed with `Tab`
17
+ (staged: command name, then arguments). Purely local — no model calls.
18
+ - Prompt-history ghost while typing: if the line being typed is a prefix of a
19
+ message already sent in the same session, the rest is ghosted and `Tab`
20
+ pulls the full line back.
21
+ - New option `argHints` (per-command argument option lists for the completion
22
+ hints).
10
23
  ## [0.1.1] - 2026-09-24
11
24
 
12
25
  ### Added
@@ -43,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
43
56
  - Options for `model`, `acceptKeys`, `maxChars`, `idleDelayMs`,
44
57
  `recentMessages` and `system`.
45
58
 
46
- [Unreleased]: https://github.com/ozandogrultan/opencode-ghost/compare/v0.1.1...HEAD
59
+ [Unreleased]: https://github.com/ozandogrultan/opencode-ghost/compare/v0.2.0...HEAD
60
+ [0.2.0]: https://github.com/ozandogrultan/opencode-ghost/compare/v0.1.1...v0.2.0
47
61
  [0.1.1]: https://github.com/ozandogrultan/opencode-ghost/compare/v0.1.0...v0.1.1
48
62
  [0.1.0]: https://github.com/ozandogrultan/opencode-ghost/releases/tag/v0.1.0
package/README.md CHANGED
@@ -17,6 +17,14 @@ dimmed ghost text you accept with <kbd>Tab</kbd> (or <kbd>→</kbd>).
17
17
  default) for one line that sounds like you.
18
18
  - **Ghost text, `Tab` to accept.** The suggestion renders under the prompt;
19
19
  `Tab` or `→` drops it into the input so you can edit and send.
20
+ - **Slash-command autocomplete while typing.** When the input starts with `/`,
21
+ the remaining command name is ghosted from configured commands, skills, and
22
+ the builtin TUI commands; known argument options render as `[a | b]` hints.
23
+ Tab completes in stages (command name, then each argument). Purely local — no
24
+ model calls.
25
+ - **History ghost while typing.** If the line you are typing is a prefix of a
26
+ message you already sent in the same session, the rest of it is ghosted as
27
+ well, Tab pulls the full line back. Also purely local.
20
28
  - **`/suggest` toggles it.** State is stored in the plugin KV.
21
29
 
22
30
  ## Requirements
@@ -89,6 +97,7 @@ Pass an options object as the second element of the plugin tuple:
89
97
  | `system` | `string` | built-in | Override the system prompt sent to the suggestion model. |
90
98
  | `backOnEmptyLeft` | `boolean` | `false` | Return to the home screen with Left when the prompt is empty. |
91
99
  | `internalSessionMarkerDir` | `string` | unset | Optional extra crash-recovery tracking. A configuration-free sweep already reaps leftover hidden sessions, so this is only an additional safety net. |
100
+ | `argHints` | `Record<string, string[]>` | `{}` | Argument option lists per slash command, shown as `[a \| b \| c]` after the command name and completed with Tab, e.g. `"/keepwarm": ["6h", "always", "off", "status"]`. |
92
101
 
93
102
  ## Commands
94
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-ghost",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Claude Code-style next-prompt suggestions in the opencode TUI, shown as dimmed ghost text and accepted with Tab. An opencode plugin.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Builtin TUI slash commands. The plugin API does not expose the TUI's own
3
+ * command palette, so the completable subset we observed in the shipped binary
4
+ * is curated here. Names are fixed; descriptions are ours and only used for the
5
+ * completion display.
6
+ */
7
+ export type BuiltinCommand = {
8
+ name: string
9
+ description: string
10
+ }
11
+
12
+ export const BUILTIN_COMMANDS: BuiltinCommand[] = [
13
+ { name: "new", description: "New session" },
14
+ { name: "agent", description: "Cycle agent" },
15
+ { name: "model", description: "Choose model" },
16
+ { name: "mcp", description: "Toggle MCP servers" },
17
+ { name: "open", description: "Command palette" },
18
+ { name: "share", description: "Share session" },
19
+ { name: "unshare", description: "Unshare session" },
20
+ { name: "compact", description: "Compact session" },
21
+ { name: "export", description: "Export session" },
22
+ { name: "fork", description: "Fork session" },
23
+ { name: "undo", description: "Undo messages" },
24
+ { name: "redo", description: "Redo messages" },
25
+ { name: "terminal", description: "Toggle terminal" },
26
+ { name: "workspace", description: "Workspace controls" },
27
+ { name: "suggest", description: "Toggle prompt suggestions" },
28
+ ]
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Pure slash-command completion: given the current prompt input, the pool of
3
+ * known command names (configured commands, skills, and builtins) and optional
4
+ * per-command argument hints, decide what ghost text to render and what Tab
5
+ * should insert.
6
+ */
7
+ export type CommandPool = {
8
+ readonly name: string
9
+ readonly description?: string
10
+ }
11
+
12
+ export type Completion = {
13
+ /** Dimmed text rendered after the input, e.g. `warm` for `/keep`+`warm`. */
14
+ ghost?: string
15
+ /** What Tab places into the input when accepting. */
16
+ insert?: string
17
+ /** Argument options shown as `[a | b]`, filtered by the word in progress. */
18
+ args?: string[]
19
+ /** Non-insertable hint text shown when nothing can be completed. */
20
+ hint?: string
21
+ }
22
+
23
+ /** Parse leading `/name args...` from the input; `args` is "" until a space exists. */
24
+ function parseSlash(input: string): { name: string; args: string; nameComplete: boolean } | undefined {
25
+ if (!input.startsWith("/")) return undefined
26
+ const line = input.split("\n")[0]
27
+ const space = line.indexOf(" ")
28
+ if (space === -1) return { name: line.slice(1), args: "", nameComplete: false }
29
+ return { name: line.slice(1, space), args: line.slice(space + 1), nameComplete: true }
30
+ }
31
+
32
+ /** Options that still match the word being typed, excluding an exact hit. */
33
+ function matchOptions(typed: string, options: readonly string[]): string[] | undefined {
34
+ const lower = typed.toLowerCase()
35
+ const matches = options.filter(
36
+ (option) => option.toLowerCase() !== lower && option.toLowerCase().startsWith(lower),
37
+ )
38
+ return matches.length > 0 ? matches : undefined
39
+ }
40
+
41
+ export function completeCommand(
42
+ input: string,
43
+ pool: readonly CommandPool[],
44
+ argHints: Readonly<Record<string, readonly string[]>> = {},
45
+ ): Completion | undefined {
46
+ const parsed = parseSlash(input)
47
+ if (!parsed) return undefined
48
+ const lower = parsed.name.toLowerCase()
49
+ const exact = pool.find((command) => command.name.toLowerCase() === lower)
50
+
51
+ // Still typing the command name (no space yet).
52
+ if (!parsed.nameComplete) {
53
+ if (exact) {
54
+ const hints = argHints[lower]
55
+ return {
56
+ args: hints ? [...hints] : undefined,
57
+ hint: hints ? undefined : exact.description,
58
+ insert: `/${exact.name} `,
59
+ }
60
+ }
61
+ const matches = pool.filter((command) => command.name.toLowerCase().startsWith(lower))
62
+ if (matches.length === 0) return undefined
63
+ const next = matches[0]
64
+ return { ghost: next.name.slice(parsed.name.length), insert: `/${next.name} ` }
65
+ }
66
+
67
+ // `/name args...`: complete the in-progress argument word against hints.
68
+ const hintList = argHints[lower]
69
+ if (hintList) {
70
+ const matches = matchOptions(parsed.args, hintList)
71
+ if (matches) {
72
+ const completed = parsed.args + matches[0].slice(parsed.args.length)
73
+ return {
74
+ args: matches,
75
+ ghost: matches[0].slice(parsed.args.length),
76
+ insert: `/${parsed.name} ${completed} `,
77
+ }
78
+ }
79
+ return undefined
80
+ }
81
+ if (exact && parsed.args === "") return { hint: exact.description }
82
+ return undefined
83
+ }
package/src/options.ts CHANGED
@@ -8,6 +8,7 @@ export type PromptSuggestOptions = {
8
8
  idleDelayMs?: number
9
9
  recentMessages?: number
10
10
  system?: string
11
+ argHints?: Record<string, string[]>
11
12
  }
12
13
 
13
14
  export type ResolvedOptions = {
@@ -20,6 +21,7 @@ export type ResolvedOptions = {
20
21
  idleDelayMs: number
21
22
  recentMessages: number
22
23
  system: string
24
+ argHints: Record<string, readonly string[]>
23
25
  }
24
26
 
25
27
  export const DEFAULT_SYSTEM = [
@@ -77,5 +79,13 @@ export function resolveOptions(options: PromptSuggestOptions | undefined): Resol
77
79
  ? recentMessages
78
80
  : DEFAULTS.recentMessages,
79
81
  system: typeof input.system === "string" && input.system.trim() ? input.system : DEFAULT_SYSTEM,
82
+ argHints:
83
+ input.argHints && typeof input.argHints === "object" && !Array.isArray(input.argHints)
84
+ ? Object.fromEntries(
85
+ Object.entries(input.argHints)
86
+ .filter(([, value]) => Array.isArray(value) && value.length > 0)
87
+ .map(([key, value]) => [key.toLowerCase(), [...value]]),
88
+ )
89
+ : {},
80
90
  }
81
91
  }
package/src/tui.tsx CHANGED
@@ -5,6 +5,8 @@ import * as fs from "node:fs"
5
5
  import * as path from "node:path"
6
6
  import { resolveOptions, type PromptSuggestOptions } from "./options"
7
7
  import { clip, isEcho, normalize, parseModel } from "./text"
8
+ import { BUILTIN_COMMANDS } from "./builtins"
9
+ import { completeCommand, type CommandPool } from "./completion"
8
10
 
9
11
  const HIDDEN_TITLE = "ghost-hidden"
10
12
  // Survives the server's automatic title generation, which renames the hidden
@@ -259,6 +261,9 @@ const tui: TuiPlugin = async (api, rawOptions) => {
259
261
  sweepTimer.unref?.()
260
262
 
261
263
  const [ghost, setGhost] = createSignal<{ sessionID: string; text: string } | undefined>()
264
+ const [completion, setCompletion] = createSignal<
265
+ { sessionID: string; ghost?: string; insert?: string; args?: string[]; hint?: string } | undefined
266
+ >()
262
267
  let promptRef: TuiPromptRef | undefined
263
268
  let timer: ReturnType<typeof setTimeout> | undefined
264
269
  let generating = false
@@ -273,6 +278,96 @@ const tui: TuiPlugin = async (api, rawOptions) => {
273
278
 
274
279
  const currentInput = () => promptRef?.current.input ?? ""
275
280
 
281
+ // Completable commands: configured commands/skills from the server layered
282
+ // under the builtin TUI slash commands. One config read, refreshed lazily; no
283
+ // model calls are involved anywhere in completion.
284
+ let commandPool: CommandPool[] = BUILTIN_COMMANDS
285
+ let poolFetching = false
286
+ let poolFetchedAt = 0
287
+ const refreshPool = async () => {
288
+ if (poolFetching) return
289
+ poolFetching = true
290
+ try {
291
+ const result = await api.client.command.list()
292
+ const rows = (result as { data?: unknown }).data
293
+ if (Array.isArray(rows)) {
294
+ const configured: CommandPool[] = rows
295
+ .map((row) => row as { name?: unknown; description?: unknown })
296
+ .filter((row) => typeof row.name === "string" && row.name.length > 0)
297
+ .map((row) => ({
298
+ name: row.name as string,
299
+ description: typeof row.description === "string" ? row.description : undefined,
300
+ }))
301
+ const seen = new Set(configured.map((command) => `${command.name.toLowerCase()}`))
302
+ const merged = [
303
+ ...configured,
304
+ ...BUILTIN_COMMANDS.filter((command) => !seen.has(command.name.toLowerCase())),
305
+ ]
306
+ commandPool = merged.length > 0 ? merged : BUILTIN_COMMANDS
307
+ poolFetchedAt = Date.now()
308
+ }
309
+ } catch {
310
+ // Keep the current pool; the poll loop retries on the next TTL window.
311
+ } finally {
312
+ poolFetching = false
313
+ }
314
+ }
315
+ void refreshPool()
316
+
317
+ // History ghost while typing: if the input is a prefix of a message the user
318
+ // already sent in this session, offer the rest of it. No model involved.
319
+ const historyCandidate = (sessionID: string, input: string) => {
320
+ const wants = input.replace(/\s+/g, " ").trimEnd()
321
+ if (wants.length < 2 || /\n/.test(input)) return undefined
322
+ const collapsed = wants.toLowerCase()
323
+ const messages = api.state.session.messages(sessionID).filter((m) => m.role === "user")
324
+ for (let i = messages.length - 1; i >= 0; i--) {
325
+ const text = api.state
326
+ .part(messages[i].id)
327
+ .filter((part) => part.type === "text")
328
+ .map((part) => (part.type === "text" ? part.text : ""))
329
+ .join(" ")
330
+ .replace(/\s+/g, " ")
331
+ .trim()
332
+ if (text.length <= collapsed.length) continue
333
+ if (!text.toLowerCase().startsWith(collapsed)) continue
334
+ return { ghost: text.slice(collapsed.length), insert: text }
335
+ }
336
+ return undefined
337
+ }
338
+
339
+ const POLL_INTERVAL_MS = 100
340
+ const POOL_TTL_MS = 60_000
341
+ let completionKey = ""
342
+ const pollTimer = setInterval(() => {
343
+ const sessionID = currentSessionID()
344
+ if (!sessionID) {
345
+ if (completion()) setCompletion(undefined)
346
+ return
347
+ }
348
+ const input = promptRef?.current.input ?? ""
349
+ if (poolFetchedAt && Date.now() - poolFetchedAt > POOL_TTL_MS) void refreshPool()
350
+ const key = `${sessionID}\u0000${input}`
351
+ const found = input.startsWith("/")
352
+ ? completeCommand(input, commandPool, opts.argHints)
353
+ : historyCandidate(sessionID, input)
354
+ if (completionKey === `${key}\u0000${found?.insert ?? ""}`) return
355
+ completionKey = `${key}\u0000${found?.insert ?? ""}`
356
+ setCompletion(
357
+ found
358
+ ? {
359
+ sessionID,
360
+ ghost: found.ghost,
361
+ insert: found.insert,
362
+ args: "args" in found ? found.args : undefined,
363
+ hint: "hint" in found ? found.hint : undefined,
364
+ }
365
+ : undefined,
366
+ )
367
+ setAcceptVisible(Boolean(found?.insert) || Boolean(ghost()))
368
+ }, POLL_INTERVAL_MS)
369
+ pollTimer.unref?.()
370
+
276
371
  const isEnabled = () => {
277
372
  try {
278
373
  return api.kv.get<boolean>(ENABLED_KEY, opts.enabled) !== false
@@ -400,11 +495,11 @@ const tui: TuiPlugin = async (api, rawOptions) => {
400
495
  priority: 100,
401
496
  bindings: opts.acceptKeys.map((key) => ({
402
497
  key,
403
- desc: "Accept suggested prompt",
498
+ desc: "Accept suggested prompt or completion",
404
499
  preventDefault: true,
405
500
  cmd: () => {
406
- accept()
407
- return true
501
+ if (completionAccept()) return true
502
+ return accept()
408
503
  },
409
504
  })),
410
505
  })
@@ -415,12 +510,30 @@ const tui: TuiPlugin = async (api, rawOptions) => {
415
510
  acceptLayer = undefined
416
511
  }
417
512
 
418
- const showGhost = (value: { sessionID: string; text: string } | undefined) => {
419
- setGhost(value)
513
+ let acceptVisible = false
514
+ const setAcceptVisible = (value: boolean) => {
515
+ if (acceptVisible === value) return
516
+ acceptVisible = value
420
517
  if (value) enableAccept()
421
518
  else disableAccept()
422
519
  }
423
520
 
521
+ const showGhost = (value: { sessionID: string; text: string } | undefined) => {
522
+ setGhost(value)
523
+ setAcceptVisible(Boolean(value) || Boolean(completion()?.insert))
524
+ }
525
+
526
+ const completionAccept = () => {
527
+ const current = completion()
528
+ if (!current || currentSessionID() !== current.sessionID) return false
529
+ if (!promptRef || !current.insert) return false
530
+ completionKey = ""
531
+ promptRef.set({ input: current.insert, parts: [] })
532
+ promptRef.focus()
533
+ setCompletion(undefined)
534
+ return true
535
+ }
536
+
424
537
  const run = async (sessionID: string) => {
425
538
  if (generating) return
426
539
  if (!isEnabled()) return
@@ -504,6 +617,13 @@ const tui: TuiPlugin = async (api, rawOptions) => {
504
617
  const current = ghost()
505
618
  return current && current.sessionID === props.session_id ? current.text : ""
506
619
  }
620
+ const completionText = () => {
621
+ const current = completion()
622
+ if (!current || current.sessionID !== props.session_id) return ""
623
+ if (current.args && current.args.length > 0) return `[${current.args.join(" | ")}]`
624
+ if (current.hint) return current.hint
625
+ return current.ghost ?? ""
626
+ }
507
627
  return (
508
628
  <api.ui.Prompt
509
629
  sessionID={props.session_id}
@@ -511,9 +631,11 @@ const tui: TuiPlugin = async (api, rawOptions) => {
511
631
  disabled={props.disabled}
512
632
  showPlaceholder={false}
513
633
  hint={
514
- suggestion() ? (
634
+ suggestion() || completionText() ? (
515
635
  <box marginLeft={1}>
516
- <text fg={api.theme.current.textMuted}>{suggestion()}</text>
636
+ <text fg={api.theme.current.textMuted}>
637
+ {completionText() || suggestion()}
638
+ </text>
517
639
  </box>
518
640
  ) : undefined
519
641
  }
@@ -563,6 +685,7 @@ const tui: TuiPlugin = async (api, rawOptions) => {
563
685
  api.lifecycle.onDispose(() => {
564
686
  if (timer) clearTimeout(timer)
565
687
  clearInterval(sweepTimer)
688
+ clearInterval(pollTimer)
566
689
  offIdle()
567
690
  offStatus()
568
691
  backLayer()