kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

Feature request: an in-process plugin API for the prompt editor (edit events, caret, decorations, virtual text)

Open wkasekende opened this issue 1 month ago • 1 comments

An extension point that lets a plugin run code inside the TUI process and work with the prompt editor while the person types: read the draft and the caret, see each edit before it is applied, paint over the draft, and write back. Today a plugin cannot do this, and nothing else in the plugin system can stand in for it.

The use case. I am building OpenCues, an assistant that lives in the prompt box. When you pause, it puts a short note under the words it is about (4 + 4 gets ↳ 4 + 4 = 8 (underscore to apply)), and a _ takes the note. It already runs inside Claude Code (its Mods API) and OpenCode (its TUI plugin API). Kimi Code is the next host I would like to support, and the same API would serve any plugin that works on the draft: inline suggestions, spell and grammar marks, expansions, linting of the prompt.

Why today's plugins and hooks cannot do it (from docs/en/customization/plugins.md and hooks.md)

  • A plugin contributes skills, agents, system-prompt text, MCP servers and slash commands. None of it runs in the TUI process or reaches the editor.
  • Hooks are shell commands that get JSON on stdin. The events fire at submit, tool use, stop and so on. None fires while typing (the nearest, UserPromptSubmit, fires at Enter), and a process per keystroke would be too slow anyway.

What already exists inside the app. The seams are there, they are just not exposed:

  • packages/pi-tui/src/editor-component.ts: the EditorComponent interface is documented as the thing that "allows extensions to provide their own editor implementation".
  • apps/kimi-code/src/tui/components/editor/custom-editor.ts: CustomEditor extends Editor, overriding render(width) and handleInput(data) and reading getCursor().

Prior art. Pi, the project pi-tui is forked from, lets extensions replace the main editor (CustomEditor), place persistent widgets next to it and register shortcuts (docs). Claude Code's Mods API has per-edit hooks, decorations, virtual text and a change event (thread). OpenCode's TUI plugin API can intercept keys.

What a plugin needs

# Need Priority
1 A plugin module that runs in process, declared in the manifest together with the capabilities it uses (shown at install next to the trust tier). must
2 Read the draft and the caret (offset), and write the draft with a caret position (setText(text, { cursor })). The write is tagged as the plugin's. must
3 A hook on each edit, before it is applied, with { text, cursor, start, end, inputText, key }. It may answer with the resulting { text, cursor, decorations }. Awaited, with a documented time budget. must
4 Key interception before the editor's own bindings, per key, able to consume the key (for example _ while the caret is on a note). must
5 Decorations: styled ranges over the draft (color, faint, bold, italic, underline, background) by offset, replaced on each edit, kept on the same characters while pending. The style must hold under the caret cell too. must
6 Virtual text: text drawn in the editor that is not in the buffer. Newlines allowed, wrapped to the editor width, never counted by offsets, never holding the caret, never submitted or saved to history. must
7 A paint-only call: repaint decorations and virtual text without writing text or moving the caret, for a result that arrives between keys. must
8 Events for changes no key made, with a cause: history recall, undo and redo, clear, paste, another plugin's write. A plugin must be able to tell its own writes. must
9 Call the session's model from a plugin (effort, max tokens, cancellation), so a plugin needs no key of its own. should
10 Timers, file watch, process spawn, HTTP and env read, through the same declared capabilities. should
11 The same API on every surface (TUI, VS Code, desktop, web), or a clear statement of which ones. should
12 Version negotiation: a minimum version or feature flags in the manifest, and a hook on an event the engine does not know is skipped with a warning, not fatal. should
13 Documented units: offsets in UTF-16 code units, and a width function that matches the editor's cell measure (renderers disagree on emoji with a variation selector). should
14 Per-project plugin scope (the docs already say it is not supported yet). later
15 Hot reload while developing a plugin. later

The smallest useful slice is 2, 3, 4, 5 and 8: read and write the draft with a caret, see each edit, intercept keys, and paint.

Lessons from other hosts, to save a round trip

  • A write must be able to set the caret. In Claude Code a write moves the caret to the end of the box, which breaks multi-line drafts (I have asked for a cursor option there).
  • Without a paint-only call (item 7), a plugin can only repaint between keys by writing text, which moves the caret.
  • Decide surface parity (item 11) up front. Claude Code mods draw in the terminal but not in the desktop app or Remote Control viewers (#99217, #101129).
  • Document the per-call time budget. A plugin author reported there that the budget the CLI enforces (50 ms) differs from the one the types document (10 s) (#101129).

Security

A plugin that runs in process has the process's reach. Declaring its capabilities in the manifest and showing them at install, next to the trust tier you already show, keeps the choice with the person. Hooks can stay fail-open.

I know feature PRs are not accepted here. I am happy to write a reference plugin against a preview, test it and report back, and to take an invitation if you would like help implementing it.

wkasekende avatar Oct 16 '26 21:10 wkasekende

A smaller version of this request. Kimi Code's terminal is built on pi-tui, and its editor is the same CustomEditor / Editor family Pi uses. Pi lets an extension replace that editor, and OpenCues runs on Pi today through exactly these APIs:

  • In-process extensions: a JS/TS module loaded at startup with an ExtensionAPI (Pi: pi.on('session_start', (event, ctx) => …)), behind whatever capability or trust gate you want.
  • ctx.ui.setEditorComponent((tui, theme, keybindings) => editor): the extension supplies the prompt editor, a subclass of the exported CustomEditor.
  • On that editor: handleInput(data) and render(width) to override, getText() / setText() / getCursor(), tui.requestRender(), and keybindings.matches(data, id).

With only that much exposed, Kimi Code would run OpenCues as Pi does, and nothing else in the original list would be needed. Three things we currently reach privately on Pi would make it robust: a public caret setter (line and column), one-step undo(), and the editor's wrap layout (lastWidth, scrollOffset).

Happy to test a build.

wkasekende avatar Oct 26 '26 23:10 wkasekende