repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui rpc command adapters

18f9996c45a916ef61f6c5331615ee11f4c56d6c

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+100 / −0 text lines

Diff layout

extensions/ui/__tests__/command-adapter.test.ts

Added
new file mode 100644

+60 / −0 text lines

1import { describe, expect, it } from "vitest";
2import {
3 abortCurrent,
4 compactSession,
5 cycleModel,
6 cycleThinking,
7 runBash,
8 runSlashCommand,
9 sendPrompt,
10} from "../command-adapter";
12describe("ui rpc command adapters", () => {
13 it("sends normal prompts", async () => {
14 const rpc = fakeRpc();
15 await sendPrompt(rpc, "hello");
16 expect(rpc.sent).toEqual([{ type: "prompt", message: "hello" }]);
17 });
19 it("runs slash commands through prompt expansion", async () => {
20 const rpc = fakeRpc();
21 await runSlashCommand(rpc, "mode", "terse");
22 expect(rpc.sent).toEqual([{ type: "prompt", message: "/mode terse" }]);
23 });
25 it("runs bash and preserves context exclusion", async () => {
26 const rpc = fakeRpc();
27 await runBash(rpc, "git status", true);
28 expect(rpc.sent).toEqual([
29 { type: "bash", command: "git status", excludeFromContext: true },
30 ]);
31 });
33 it("cycles model and thinking", async () => {
34 const rpc = fakeRpc();
35 await cycleModel(rpc);
36 await cycleThinking(rpc);
37 expect(rpc.sent).toEqual([
38 { type: "cycle_model" },
39 { type: "cycle_thinking_level" },
40 ]);
41 });
43 it("compacts and aborts", async () => {
44 const rpc = fakeRpc();
45 await compactSession(rpc);
46 await abortCurrent(rpc);
47 expect(rpc.sent).toEqual([{ type: "compact" }, { type: "abort" }]);
48 });
49});
51function fakeRpc() {
52 const sent: unknown[] = [];
53 return {
54 sent,
55 send(request: unknown) {
56 sent.push(request);
57 return Promise.resolve({ type: "response", success: true });
58 },
59 };
60}

extensions/ui/command-adapter.ts

Added
new file mode 100644

+40 / −0 text lines

1import type { RpcClient, RpcResponse } from "./rpc";
2
3export type RpcSender = Pick<RpcClient, "send">;
4
5export function sendPrompt(rpc: RpcSender, text: string): Promise<RpcResponse> {
6 return rpc.send({ type: "prompt", message: text });
7}
8
9export function runSlashCommand(
10 rpc: RpcSender,
11 command: string,
12 args = "",
13): Promise<RpcResponse> {
14 const suffix = args.trim() ? ` ${args.trim()}` : "";
15 return sendPrompt(rpc, `/${command}${suffix}`);
16}
18export function runBash(
19 rpc: RpcSender,
20 command: string,
21 excludeFromContext: boolean,
22): Promise<RpcResponse> {
23 return rpc.send({ type: "bash", command, excludeFromContext });
24}
26export function cycleModel(rpc: RpcSender): Promise<RpcResponse> {
27 return rpc.send({ type: "cycle_model" });
28}
30export function cycleThinking(rpc: RpcSender): Promise<RpcResponse> {
31 return rpc.send({ type: "cycle_thinking_level" });
32}
34export function compactSession(rpc: RpcSender): Promise<RpcResponse> {
35 return rpc.send({ type: "compact" });
36}
38export function abortCurrent(rpc: RpcSender): Promise<RpcResponse> {
39 return rpc.send({ type: "abort" });
40}