pi-ext
bugabingas pi extensions
owned by bugabinga
Add ui browser keybindings
9308a6437ecbb7bc71a0b5a7161ba9e0295d0743
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
Browse filesCompare with HEAD
Changed files (5)
extensions/ui/__tests__/runtime.test.ts
Modified130 130 expect.
objectContaining({ type:
"prompt", message:
"hello" }),
134 + it(
"routes bash and browser control actions to rpc",
async ()
=> {
135 + const sent
: unknown[]
= [];
136 + const runtime
= await createUiRuntime({
137 + sessionLabel:
"ui: test",
138 + address:
"127.0.0.1",
140 + openBrowser:
false,
141 + sessionManager:
fakeSessionManager(),
142 + startRpcBridge: ({ sessionPath })
=> fakeRpc(sent, sessionPath),
144 + runtimes.
push(runtime);
145 + const url
= new URL(runtime.
status().url
?? "");
146 + const socketUrl
= new URL(
"/ws", url);
147 + socketUrl.
protocol = "ws:";
148 + socketUrl.searchParams.
set(
"token", url.searchParams.
get(
"token")
?? "");
149 + const socket
= new WebSocket(socketUrl);
150 + await new Promise((resolve)
=> 151 + socket.
addEventListener(
"open", resolve, { once:
true }),
154 + socket.
send(
JSON.
stringify({ type:
"prompt", text:
"!!git status" }));
155 + socket.
send(
JSON.
stringify({ type:
"cycle_model" }));
156 + socket.
send(
JSON.
stringify({ type:
"abort" }));
157 + await new Promise((resolve)
=> setTimeout(resolve,
20));
160 + expect(sent).
toContainEqual({
162 + command:
"git status",
163 + excludeFromContext:
true,
165 + expect(sent).
toContainEqual({ type:
"cycle_model" });
166 + expect(sent).
toContainEqual({ type:
"abort" });
135 170 function fakeSessionManager() {
extensions/ui/__tests__/web-composer.test.ts
Modified 1 1 import { describe, expect, it }
from "vitest";
5 6 }
from "../web/components/composer.js";
33 34 expect(
moveMenuIndex(
0,
-1,
3)).
toBe(
2);
34 35 expect(
moveMenuIndex(
0,
1,
0)).
toBe(
0);
38 + it(
"maps composer keybindings", ()
=> {
39 + expect(
composerKeyAction({ key:
"Enter" })).
toBe(
"submit");
40 + expect(
composerKeyAction({ key:
"Enter", shiftKey:
true })).
toBe(
"newline");
41 + expect(
composerKeyAction({ key:
"Escape" })).
toBe(
"abort");
42 + expect(
composerKeyAction({ key:
"o", ctrlKey:
true })).
toBe(
45 + expect(
composerKeyAction({ key:
"p", ctrlKey:
true })).
toBe(
"cycle-model");
47 + composerKeyAction({ key:
"Arrow" + "Down", menuVisible:
true }),
48 + ).
toBe(
"menu-next");
extensions/ui/runtime.ts
Modified 1 1 import path
from "node:path";
8 +}
from "./command-adapter";
9 +import { parseComposerInput }
from "./composer";
2 10 import { EventStore }
from "./events";
3 11 import { chooseLanAddress }
from "./network";
4 12 import { openBrowser }
from "./open";
155 163 rpc: RpcClient
| undefined,
156 164 publish: (input
: Parameters<
EventStore[
"append"]>[
0])
=> unknown,
157 165 ):
Promise<void> {
158 - if (!isPromptMessage(message)) return;
159 - publish({ type:
"message", role:
"user", text: message.text });
160 - await rpc?.send({ type:
"prompt", message: message.
text });
166 + if (!rpc || !isBrowserMessage(message)) return;
167 + if (message.type ===
"abort") {
168 + await abortCurrent(rpc);
171 + state:
"rpc-connected",
172 + message:
"abort requested",
176 + if (message.type ===
"cycle_model" || message.type ===
"cycle-model") {
177 + await cycleModel(rpc);
180 + state:
"rpc-connected",
181 + message:
"model cycle requested",
185 + if (message.type ===
"toggle-expand") {
186 + publish({ type:
"log", level:
"info", message:
"toggle expand requested" });
189 + const parsed
= parseComposerInput(message.
text);
190 + if (parsed.kind ===
"bash") {
191 + publish({ type:
"message", role:
"user", text: message.
text });
192 + await runBash(rpc, parsed.command, parsed.excludeFromContext);
195 + if (parsed.kind ===
"slash") {
196 + publish({ type:
"message", role:
"user", text: parsed.
text });
197 + await runSlashCommand(rpc, parsed.command, parsed.args);
200 + publish({ type:
"message", role:
"user", text: parsed.text });
201 + await
sendPrompt(rpc, parsed.text);
163 -function isPromptMessage(
165 -)
: value
is {
type:
"prompt";
text: string } {
204 +type BrowserMessage = 205 + | { type
: "prompt"; text
: string }
206 + | { type
: "abort" }
207 + | { type
: "cycle_model" }
208 + | { type
: "cycle-model" }
209 + | { type
: "toggle-expand" };
211 +function isBrowserMessage(value
: unknown)
: value
is BrowserMessage {
212 + if (
!value
|| typeof value
!== "object")
return false;
213 + const type
= (value
as { type
?: unknown }).
type;
214 + if (type
=== "prompt")
215 + return typeof (value
as { text
?: unknown }).
text === "string";
168 - typeof value
=== "object" && 169 - (value
as { type
?: unknown }).
type === "prompt" && 170 - typeof (value
as { text
?: unknown }).
text === "string" 217 + type
=== "abort" || 218 + type
=== "cycle_model" || 219 + type
=== "cycle-model" || 220 + type
=== "toggle-expand" extensions/ui/web/app.js.ts
Modified61 61 input.
addEventListener(
'input', renderCommandMenu);
62 62 input.
addEventListener(
'keydown', event
=> {
63 - if (menu.hidden)
return;
64 - if (
event.key
=== 'ArrowDown') {
63 + const action
= composerKeyAction({
65 + ctrlKey:
event.ctrlKey,
66 + shiftKey:
event.shiftKey,
67 + menuVisible:
!menu.hidden,
69 + if (action
=== 'menu-next') {
65 70 event.
preventDefault();
66 71 selectedCommandIndex
= moveMenuIndex(selectedCommandIndex,
1, visibleCommands.
length);
67 72 renderCommandMenu();
69 - if (
event.key
=== 'ArrowUp') {
74 + if (action
=== 'menu-prev') {
70 75 event.
preventDefault();
71 76 selectedCommandIndex
= moveMenuIndex(selectedCommandIndex,
-1, visibleCommands.
length);
72 77 renderCommandMenu();
74 - if (
event.key
=== 'Tab') {
79 + if (action
=== 'menu-select') {
75 80 event.
preventDefault();
76 81 selectCommand(selectedCommandIndex);
83 + if (action
=== 'abort' || action
=== 'cycle-model' || action
=== 'toggle-expand') {
84 + event.
preventDefault();
85 + socket?.
send(
JSON.
stringify({ type: action }));
86 + status.textContent
= action;
88 + if (action
=== 'submit') {
89 + event.
preventDefault();
90 + form.
requestSubmit();
80 94 form.
addEventListener(
'submit', event
=> {
extensions/ui/web/components/composer.js.ts
Modified 1 1 import type { UiCommandInfo }
from "../../protocol";
3 +export type ComposerKeyAction = 14 +export type ComposerKeyInput = {
18 + menuVisible
?: boolean;
3 21 export function filterCommandItems(
4 22 commands
: UiCommandInfo[],
17 35 return (current
+ delta
+ count)
% count;
38 +export function composerKeyAction(input
: ComposerKeyInput)
: ComposerKeyAction {
39 + const normalized
= input.key.
toLowerCase();
40 + if (input.menuVisible
&& input.key
=== "ArrowDown")
return "menu-next";
41 + if (input.menuVisible
&& input.key
=== "ArrowUp")
return "menu-prev";
42 + if (input.menuVisible
&& input.key
=== "Tab")
return "menu-select";
43 + if (input.ctrlKey
&& normalized
=== "o")
return "toggle-expand";
44 + if (input.ctrlKey
&& normalized
=== "p")
return "cycle-model";
45 + if (input.key
=== "Escape")
return "abort";
46 + if (input.key
=== "Enter" && input.shiftKey)
return "newline";
47 + if (input.key
=== "Enter")
return "submit";
20 51 export function renderComposerComponentJs()
: string {
21 52 return `function filterCommandItems(commands, input) { 22 53 const prefix = input.startsWith('/') ? input.slice(1).toLowerCase() : ''; 26 57 if (count
<= 0)
return 0;
27 58 return (current
+ delta
+ count)
% count;
60 +function composerKeyAction(input) {
61 + const normalized
= input.key.
toLowerCase();
62 + if (input.menuVisible
&& input.key
=== 'ArrowDown')
return 'menu-next';
63 + if (input.menuVisible
&& input.key
=== 'ArrowUp')
return 'menu-prev';
64 + if (input.menuVisible
&& input.key
=== 'Tab')
return 'menu-select';
65 + if (input.ctrlKey
&& normalized
=== 'o')
return 'toggle-expand';
66 + if (input.ctrlKey
&& normalized
=== 'p')
return 'cycle-model';
67 + if (input.key
=== 'Escape')
return 'abort';
68 + if (input.key
=== 'Enter' && input.shiftKey)
return 'newline';
69 + if (input.key
=== 'Enter')
return 'submit';
29 72 function renderCommandMenu() {
30 73 menu.innerHTML
= '';
31 74 if (
!input.
value.
startsWith(
'/')) {