Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit 89d948a

Browse files
committed
fix(tui): use keymap.registerLayer with command.register fallback
OpenCode 1.14.42 (anomalyco/opencode#26053, "introduce opentui keymap as sole key/cmd engine") removed api.command.register from the TUI plugin API. The replacement is api.keymap.registerLayer({ commands, bindings }). A later patch (1.14.44+) reinstated api.command.register as a deprecated shim that translates internally to keymap.registerLayer, but versions 1.14.42–1.14.43 are still broken. Magic Context's TUI was calling api.command.register unconditionally at src/tui/index.tsx:468, throwing TypeError on init for any host >=1.14.42 where the shim hadn't landed yet. Because the call ran *before* api.slots.register, the sidebar slot never registered either — so the entire Magic Context TUI surface vanished, not just the command palette entries. Fix: Extracted the single api.command.register call into a new helper registerCommandPaletteEntries(api) that: 1. Prefers api.keymap.registerLayer when present (canonical 1.14.42+ API). Calling it directly skips the deprecation warning and works without depending on the now-deprecated api.command namespace existing at all. Translates each entry to the keymap shape: namespace="palette" + name + title + category + run(). 2. Falls back to api.command.register on hosts predating keymap (1.14.0–1.14.41). 3. Logs no error if neither surface is present — the TUI host can still load with the sidebar visible. /ctx-status and /ctx-recomp remain reachable via server-side slash commands which bridge to the TUI dialogs through RPC. Reference: cortexkit#61, mirroring the same fix shipped to cortexkit/aft#33 with the same pattern. No rebuild needed: package.json exports point ./tui at the raw source (./src/tui/index.tsx), so the fix is live as soon as the file is on disk.
1 parent a751d55 commit 89d948a

1 file changed

Lines changed: 97 additions & 18 deletions

File tree

‎packages/plugin/src/tui/index.tsx‎

Lines changed: 97 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,94 @@ function showStatusDialog(api: TuiPluginApi) {
453453
})
454454
}
455455

456+
/**
457+
* Register Magic Context command palette entries, preferring the v1.14.42+
458+
* `keymap.registerLayer` API and falling back to the legacy
459+
* `api.command.register` for older hosts.
460+
*
461+
* The `keymap.registerLayer` shape uses `name`/`title`/`run`/`namespace`
462+
* (see `@opencode-ai/plugin/tui` types) and is what the host's own legacy
463+
* command-shim translates into. Calling it directly skips the deprecation
464+
* warning and works without depending on the (now-deprecated) `api.command`
465+
* namespace existing at all.
466+
*
467+
* Version coverage:
468+
* 1.14.0–1.14.41 — `api.command.register` only
469+
* 1.14.42–1.14.43 — both surfaces broken (api.command removed, keymap landed
470+
* but with bugs); plugins crash on init either way
471+
* 1.14.44+ — `api.keymap.registerLayer` canonical, `api.command` shim
472+
*/
473+
function registerCommandPaletteEntries(api: TuiPluginApi): void {
474+
type ApiAny = {
475+
keymap?: {
476+
registerLayer?: (layer: {
477+
commands: Array<Record<string, unknown>>
478+
bindings: Array<Record<string, unknown>>
479+
}) => unknown
480+
}
481+
command?: {
482+
register?: (cb: () => Array<Record<string, unknown>>) => unknown
483+
}
484+
}
485+
const apiAny = api as unknown as ApiAny
486+
487+
if (typeof apiAny.keymap?.registerLayer === "function") {
488+
apiAny.keymap.registerLayer({
489+
commands: [
490+
{
491+
namespace: "palette",
492+
name: "magic-context.status",
493+
title: "Magic Context: Status",
494+
category: "Magic Context",
495+
run() {
496+
showStatusDialog(api)
497+
},
498+
},
499+
{
500+
namespace: "palette",
501+
name: "magic-context.recomp",
502+
title: "Magic Context: Recomp",
503+
category: "Magic Context",
504+
run() {
505+
showRecompDialog(api)
506+
},
507+
},
508+
],
509+
bindings: [],
510+
})
511+
return
512+
}
513+
514+
if (typeof apiAny.command?.register === "function") {
515+
apiAny.command.register(() => [
516+
{
517+
title: "Magic Context: Status",
518+
value: "magic-context.status",
519+
category: "Magic Context",
520+
onSelect() {
521+
showStatusDialog(api)
522+
},
523+
},
524+
{
525+
title: "Magic Context: Recomp",
526+
value: "magic-context.recomp",
527+
category: "Magic Context",
528+
onSelect() {
529+
showRecompDialog(api)
530+
},
531+
},
532+
])
533+
return
534+
}
535+
536+
// Neither API surface is present. The TUI host can still load — we only
537+
// lose the command palette entry points. The sidebar (registered above
538+
// via api.slots.register) remains visible. Status/Recomp are still
539+
// reachable through the server-side `/ctx-status` and `/ctx-recomp`
540+
// slash commands, which the server handler bridges to the TUI dialogs
541+
// via RPC.
542+
}
543+
456544
const tui: TuiPlugin = async (api, _options, meta) => {
457545
// Initialize RPC client for server communication
458546
const directory = api.state.path.directory ?? ""
@@ -465,24 +553,15 @@ const tui: TuiPlugin = async (api, _options, meta) => {
465553
// are registered server-side so there's only one /ctx-* registration).
466554
// The server detects TUI mode and sends dialog requests via RPC instead
467555
// of sendIgnoredMessage.
468-
api.command.register(() => [
469-
{
470-
title: "Magic Context: Status",
471-
value: "magic-context.status",
472-
category: "Magic Context",
473-
onSelect() {
474-
showStatusDialog(api)
475-
},
476-
},
477-
{
478-
title: "Magic Context: Recomp",
479-
value: "magic-context.recomp",
480-
category: "Magic Context",
481-
onSelect() {
482-
showRecompDialog(api)
483-
},
484-
},
485-
])
556+
//
557+
// OpenCode 1.14.42 removed `api.command.register` entirely
558+
// (anomalyco/opencode#26053). A later patch (1.14.44+) reinstated it as
559+
// a deprecated shim that translates to `api.keymap.registerLayer`. To
560+
// work across all hosts (1.14.0–1.14.41 with command-only, the broken
561+
// 1.14.42–1.14.43, and 1.14.44+ where both exist), we prefer
562+
// `api.keymap.registerLayer` and fall back to `api.command.register`
563+
// only when keymap is missing.
564+
registerCommandPaletteEntries(api)
486565

487566
// Poll for server→TUI messages: toasts and dialog requests.
488567
// Single poller because consumeTuiMessages() is destructive (deletes consumed rows).

0 commit comments

Comments
 (0)