aboutsummaryrefslogtreecommitdiffstats
path: root/pi-ext/reload-hotkey.ts
blob: 833710c22b742261dbf2a8a0148bbda5270c9666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Reload Hotkey
 *
 * Convenience for rapid extension dev: a key that gets you to pi's built-in
 * /reload with minimal typing.
 *
 * WHY IT ONLY PREFILLS (can't fully auto-run):
 * There is no extension API that can invoke a reload directly. Confirmed in
 * pi's source:
 *   1. registerShortcut handlers receive a plain ExtensionContext, which has NO
 *      ctx.reload() — reload lives on ExtensionCommandContext (command handlers
 *      only, to avoid re-entrancy deadlocks).
 *   2. pi.sendUserMessage() delegates to prompt(text, {expandPromptTemplates:
 *      false}), and prompt() only runs slash-command handling when
 *      expandPromptTemplates is true. So a shortcut-sent "/reload" is delivered
 *      to the MODEL as literal text, never executed. (The reload_runtime tool
 *      snippet in the docs is broken for this reason.)
 *   3. The built-in /reload is handled inside the editor's onSubmit callback,
 *      which is not exposed to extensions, so we can't submit on your behalf.
 *
 * So the best we can do is drop "/reload" into the editor; you press Enter.
 * If a future pi exposes a command/reload API for shortcuts, wire it here.
 *
 * Change RELOAD_KEY below to taste (see keybindings.md for the key format).
 */

import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";

const RELOAD_KEY = "f5";
const RELOAD_TEXT = "/reload";

export default function (pi: ExtensionAPI) {
  pi.registerShortcut(RELOAD_KEY, {
    description: "Prefill /reload (press Enter to reload the pi runtime)",
    handler: (ctx) => {
      if (!ctx.hasUI) return;
      const current = ctx.ui.getEditorText?.() ?? "";
      if (current.trim() && current.trim() !== RELOAD_TEXT) {
        ctx.ui.notify(`Editor not empty — type ${RELOAD_TEXT} to reload`, "warning");
        return;
      }
      ctx.ui.setEditorText(RELOAD_TEXT);
      ctx.ui.notify("Press Enter to reload the pi runtime", "info");
    },
  });
}