opencode-copilot-delegate 0.10.1 → 0.12.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.
@@ -32,6 +32,11 @@ type ToastCall = {
32
32
  duration?: number
33
33
  }
34
34
 
35
+ type KeymapLayerCall = {
36
+ commands: ReadonlyArray<Record<string, unknown>>
37
+ bindings: ReadonlyArray<unknown>
38
+ }
39
+
35
40
  type TestApiControls = {
36
41
  api: TuiPluginApi
37
42
  commandFactories: Array<() => TuiCommand[]>
@@ -41,6 +46,21 @@ type TestApiControls = {
41
46
  disposeHandlers: Array<() => void | Promise<void>>
42
47
  toastCalls: ToastCall[]
43
48
  unregisterCalls: number
49
+ keymapLayerCalls: KeymapLayerCall[]
50
+ keymapUnregisterCalls: number
51
+ }
52
+
53
+ type CreateTestApiOptions = {
54
+ /**
55
+ * Which TUI registration surfaces the api stub exposes:
56
+ * - `'command'` (default): only `api.command.register` — OpenCode 1.14.41 baseline
57
+ * - `'keymap'`: only `api.keymap.registerLayer` — OpenCode 1.14.44+ canonical (and the 1.14.42–43 gap)
58
+ * - `'both'`: both surfaces — the 1.14.44+ shim era where `command.register` is restored as a deprecated shim
59
+ *
60
+ * Default `'command'` preserves the existing test suite's assertions about the
61
+ * legacy `command.register` registration path without modification.
62
+ */
63
+ surfaces?: 'command' | 'keymap' | 'both'
44
64
  }
45
65
 
46
66
  const originalFetch = globalThis.fetch
@@ -75,26 +95,54 @@ async function loadTuiPlugin(): Promise<TuiPlugin> {
75
95
  return plugin as TuiPlugin
76
96
  }
77
97
 
78
- function createTestApi(): TestApiControls {
98
+ function createTestApi(options: CreateTestApiOptions = {}): TestApiControls {
99
+ const surfaces = options.surfaces ?? 'command'
100
+ const exposeKeymap = surfaces === 'keymap' || surfaces === 'both'
101
+ const exposeCommand = surfaces === 'command' || surfaces === 'both'
102
+
79
103
  const commandFactories: Array<() => TuiCommand[]> = []
80
104
  const dialogReplacements: DialogReplacement[] = []
81
105
  const dialogSizes: Array<'medium' | 'large' | 'xlarge'> = []
82
106
  const disposeHandlers: Array<() => void | Promise<void>> = []
83
107
  const toastCalls: ToastCall[] = []
108
+ const keymapLayerCalls: KeymapLayerCall[] = []
84
109
  let clearCalls = 0
85
110
  let unregisterCalls = 0
111
+ let keymapUnregisterCalls = 0
112
+
113
+ const commandSurface = exposeCommand
114
+ ? {
115
+ register: (cb: () => TuiCommand[]) => {
116
+ commandFactories.push(cb)
117
+ return () => {
118
+ unregisterCalls += 1
119
+ }
120
+ },
121
+ trigger: () => {},
122
+ show: () => {},
123
+ }
124
+ : undefined
125
+
126
+ const keymapSurface = exposeKeymap
127
+ ? {
128
+ registerLayer: (layer: {
129
+ commands: ReadonlyArray<Record<string, unknown>>
130
+ bindings: ReadonlyArray<unknown>
131
+ }) => {
132
+ keymapLayerCalls.push({
133
+ commands: layer.commands,
134
+ bindings: layer.bindings,
135
+ })
136
+ return () => {
137
+ keymapUnregisterCalls += 1
138
+ }
139
+ },
140
+ }
141
+ : undefined
86
142
 
87
143
  const api = {
88
- command: {
89
- register: (cb: () => TuiCommand[]) => {
90
- commandFactories.push(cb)
91
- return () => {
92
- unregisterCalls += 1
93
- }
94
- },
95
- trigger: () => {},
96
- show: () => {},
97
- },
144
+ ...(commandSurface ? { command: commandSurface } : {}),
145
+ ...(keymapSurface ? { keymap: keymapSurface } : {}),
98
146
  ui: {
99
147
  Dialog: ((props: { children?: JSX.Element }) =>
100
148
  props.children ?? null) as TuiPluginApi['ui']['Dialog'],
@@ -148,6 +196,10 @@ function createTestApi(): TestApiControls {
148
196
  get unregisterCalls() {
149
197
  return unregisterCalls
150
198
  },
199
+ keymapLayerCalls,
200
+ get keymapUnregisterCalls() {
201
+ return keymapUnregisterCalls
202
+ },
151
203
  }
152
204
  }
153
205
 
@@ -353,6 +405,77 @@ describe('tui entrypoint', () => {
353
405
  expect(controls.toastCalls).toEqual([])
354
406
  })
355
407
 
408
+ it('prefers api.keymap.registerLayer when available (OpenCode 1.14.44+)', async () => {
409
+ const plugin = await loadTuiPlugin()
410
+ const controls = createTestApi({ surfaces: 'keymap' })
411
+
412
+ await plugin(controls.api, undefined, makePluginMeta())
413
+
414
+ expect(controls.keymapLayerCalls).toHaveLength(1)
415
+ expect(controls.commandFactories).toHaveLength(0)
416
+
417
+ const layer = controls.keymapLayerCalls[0]
418
+ expect(layer.bindings).toEqual([])
419
+ expect(layer.commands).toHaveLength(1)
420
+
421
+ const command = layer.commands[0]
422
+ expect(command).toMatchObject({
423
+ namespace: 'palette',
424
+ name: 'copilot-status',
425
+ title: 'Copilot Status',
426
+ category: 'Copilot',
427
+ })
428
+ expect(typeof command.run).toBe('function')
429
+
430
+ expect(controls.disposeHandlers).toHaveLength(1)
431
+ await controls.disposeHandlers[0]()
432
+ expect(controls.keymapUnregisterCalls).toBe(1)
433
+ expect(controls.unregisterCalls).toBe(0)
434
+ })
435
+
436
+ it('prefers api.keymap.registerLayer when both APIs exist (1.14.44+ shim era)', async () => {
437
+ const plugin = await loadTuiPlugin()
438
+ const controls = createTestApi({ surfaces: 'both' })
439
+
440
+ await plugin(controls.api, undefined, makePluginMeta())
441
+
442
+ expect(controls.keymapLayerCalls).toHaveLength(1)
443
+ expect(controls.commandFactories).toHaveLength(0)
444
+ })
445
+
446
+ it('falls back to api.command.register when keymap.registerLayer is absent (OpenCode 1.14.41)', async () => {
447
+ const plugin = await loadTuiPlugin()
448
+ const controls = createTestApi({ surfaces: 'command' })
449
+
450
+ await plugin(controls.api, undefined, makePluginMeta())
451
+
452
+ expect(controls.commandFactories).toHaveLength(1)
453
+ expect(controls.keymapLayerCalls).toHaveLength(0)
454
+ expect(controls.disposeHandlers).toHaveLength(1)
455
+
456
+ // Dispose unregisters via the command-register path, mirroring the
457
+ // keymap-path test's assertion that the correct teardown route runs.
458
+ await controls.disposeHandlers[0]()
459
+ expect(controls.unregisterCalls).toBe(1)
460
+ expect(controls.keymapUnregisterCalls).toBe(0)
461
+ })
462
+
463
+ it('opens the modal list when the keymap-path command is run', async () => {
464
+ const plugin = await loadTuiPlugin()
465
+ const controls = createTestApi({ surfaces: 'keymap' })
466
+
467
+ await plugin(controls.api, undefined, makePluginMeta())
468
+
469
+ const command = controls.keymapLayerCalls[0]?.commands[0] as
470
+ | (Record<string, unknown> & { run?: () => void })
471
+ | undefined
472
+ expect(command).toBeDefined()
473
+ command?.run?.()
474
+
475
+ expect(controls.dialogReplacements).toHaveLength(1)
476
+ expect(controls.dialogSizes).toEqual(['large'])
477
+ })
478
+
356
479
  it('keeps /copilot-status on a single read-only dialog replacement', async () => {
357
480
  const plugin = await loadTuiPlugin()
358
481
  const controls = createTestApi()
package/src/tui/index.tsx CHANGED
@@ -12,6 +12,98 @@ function errorMessage(error: unknown): string {
12
12
  return 'Unknown RPC error.'
13
13
  }
14
14
 
15
+ /**
16
+ * Narrow structural interface for `api.keymap.registerLayer`. Mirrors the
17
+ * shape exposed by `@opencode-ai/plugin/tui`'s `TuiKeymap` (which aliases
18
+ * `Keymap<Renderable, KeyEvent>` from `@opentui/keymap`). That upstream
19
+ * type is not exported through `@opencode-ai/plugin`'s public surface and
20
+ * `@opentui/keymap` is not a direct dependency here, so we declare the
21
+ * minimum contract needed for the dual-path registration to type-check
22
+ * without reaching into private OpenTUI types.
23
+ */
24
+ interface KeymapRegisterLayerHost {
25
+ registerLayer: (layer: {
26
+ commands: ReadonlyArray<{
27
+ namespace: string
28
+ name: string
29
+ title: string
30
+ category: string
31
+ run: () => void
32
+ }>
33
+ bindings: ReadonlyArray<unknown>
34
+ }) => () => void
35
+ }
36
+
37
+ function hasKeymapRegisterLayer(
38
+ api: Parameters<TuiPlugin>[0],
39
+ ): api is Parameters<TuiPlugin>[0] & { keymap: KeymapRegisterLayerHost } {
40
+ const candidate = (api as { keymap?: { registerLayer?: unknown } }).keymap
41
+ return candidate != null && typeof candidate.registerLayer === 'function'
42
+ }
43
+
44
+ /**
45
+ * Register the `/copilot-status` command using whichever TUI API surface
46
+ * the host OpenCode runtime exposes.
47
+ *
48
+ * OpenCode 1.14.42 removed `api.command.register` in favor of the new
49
+ * keymap engine (`api.keymap.registerLayer`). 1.14.44+ restored
50
+ * `api.command.register` as a deprecated shim that translates to
51
+ * `keymap.registerLayer` internally. The dual-path helper below prefers
52
+ * `keymap.registerLayer` when present (the canonical surface) and falls
53
+ * back to `command.register` for hosts pinned at 1.14.41 or earlier.
54
+ *
55
+ * If neither surface is available, registration is a no-op and a warning
56
+ * is logged. The TUI plugin still loads (dialog UI continues to work);
57
+ * only the slash command discovery affordance is missing.
58
+ *
59
+ * Reference: Magic Context commit `5fe1c4f` on cortexkit/magic-context
60
+ * established this dual-path pattern across the same upstream gap.
61
+ */
62
+ function registerCopilotStatusCommand(
63
+ api: Parameters<TuiPlugin>[0],
64
+ openList: () => void,
65
+ ): () => void {
66
+ if (hasKeymapRegisterLayer(api)) {
67
+ return api.keymap.registerLayer({
68
+ commands: [
69
+ {
70
+ namespace: 'palette',
71
+ name: 'copilot-status',
72
+ title: 'Copilot Status',
73
+ category: 'Copilot',
74
+ run() {
75
+ openList()
76
+ },
77
+ },
78
+ ],
79
+ bindings: [],
80
+ })
81
+ }
82
+
83
+ if (typeof api.command?.register === 'function') {
84
+ return api.command.register(() => [
85
+ {
86
+ title: 'Copilot Status',
87
+ value: '/copilot-status',
88
+ slash: { name: 'copilot-status' },
89
+ onSelect() {
90
+ openList()
91
+ },
92
+ },
93
+ ])
94
+ }
95
+
96
+ // Plugin-level logs normally go through `client.app.log`, but the TUI
97
+ // half has no equivalent structured channel — `console.warn` is the
98
+ // only practical option. Surfaces in dev/debug consoles; in production
99
+ // the message is purely defensive (it only fires when the host runtime
100
+ // exposes neither registration surface, which should never happen).
101
+ console.warn(
102
+ '[opencode-copilot-delegate] No supported TUI command-registration API found (neither api.keymap.registerLayer nor api.command.register). The /copilot-status slash command will not be available, but the plugin remains loaded.',
103
+ )
104
+ return () => {}
105
+ }
106
+
15
107
  const tui: TuiPlugin = async (api) => {
16
108
  const rpc = createRpcClient()
17
109
  const closeDialog = () => {
@@ -45,16 +137,9 @@ const tui: TuiPlugin = async (api) => {
45
137
  })
46
138
  }
47
139
 
48
- const unregister = api.command.register(() => [
49
- {
50
- title: 'Copilot Status',
51
- value: '/copilot-status',
52
- slash: { name: 'copilot-status' },
53
- onSelect: () => {
54
- openList()
55
- },
56
- },
57
- ])
140
+ const unregister = registerCopilotStatusCommand(api, () => {
141
+ openList()
142
+ })
58
143
 
59
144
  api.lifecycle.onDispose(() => {
60
145
  rpc.dispose()