repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui composer parser

40948a40d315cc48db1d74ce252a76c3f4b1f1ed

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

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/composer.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { parseComposerInput } from "../composer";
4 +describe("ui composer parser", () => {
5 + it("parses plain prompts", () => {
6 + expect(parseComposerInput("hello pi")).toEqual({
7 + kind: "prompt",
8 + text: "hello pi",
9 + refs: [],
10 + });
11 + });
13 + it("parses slash commands and arguments", () => {
14 + expect(parseComposerInput("/mode terse now")).toEqual({
15 + kind: "slash",
16 + command: "mode",
17 + args: "terse now",
18 + text: "/mode terse now",
19 + refs: [],
20 + });
21 + });
23 + it("parses skill commands", () => {
24 + expect(parseComposerInput("/skill:brutal make it short")).toEqual({
25 + kind: "slash",
26 + command: "skill:brutal",
27 + args: "make it short",
28 + text: "/skill:brutal make it short",
29 + refs: [],
30 + });
31 + });
33 + it("extracts at-references from prompts", () => {
34 + expect(parseComposerInput("read @README.md and @docs/spec.md please")).toEqual({
35 + kind: "prompt",
36 + text: "read @README.md and @docs/spec.md please",
37 + refs: ["README.md", "docs/spec.md"],
38 + });
39 + });
41 + it("parses single-bang bash", () => {
42 + expect(parseComposerInput("!git status --short")).toEqual({
43 + kind: "bash",
44 + command: "git status --short",
45 + excludeFromContext: false,
46 + refs: [],
47 + });
48 + });
50 + it("parses double-bang bash as excluded from context", () => {
51 + expect(parseComposerInput("!!node --version")).toEqual({
52 + kind: "bash",
53 + command: "node --version",
54 + excludeFromContext: true,
55 + refs: [],
56 + });
57 + });
58 +});

extensions/ui/composer.ts

Added
new file mode 100644
1 +export type ComposerParseResult =
2 + | {
3 + kind: "prompt";
4 + text: string;
5 + refs: string[];
6 + }
7 + | {
8 + kind: "slash";
9 + command: string;
10 + args: string;
11 + text: string;
12 + refs: string[];
13 + }
14 + | {
15 + kind: "bash";
16 + command: string;
17 + excludeFromContext: boolean;
18 + refs: string[];
19 + };
21 +const REF_RE = /(^|\s)@([^\s]+)/g;
23 +export function parseComposerInput(input: string): ComposerParseResult {
24 + const text = input.trim();
25 + if (text.startsWith("!!")) {
26 + return {
27 + kind: "bash",
28 + command: text.slice(2).trimStart(),
29 + excludeFromContext: true,
30 + refs: [],
31 + };
32 + }
33 + if (text.startsWith("!")) {
34 + return {
35 + kind: "bash",
36 + command: text.slice(1).trimStart(),
37 + excludeFromContext: false,
38 + refs: [],
39 + };
40 + }
41 + if (text.startsWith("/") && text.length > 1) {
42 + const body = text.slice(1);
43 + const space = body.search(/\s/);
44 + const command = space === -1 ? body : body.slice(0, space);
45 + const args = space === -1 ? "" : body.slice(space).trimStart();
46 + return {
47 + kind: "slash",
48 + command,
49 + args,
50 + text,
51 + refs: refs(text),
52 + };
53 + }
54 + return { kind: "prompt", text, refs: refs(text) };
57 +function refs(text: string): string[] {
58 + return [...text.matchAll(REF_RE)].map((match) => match[2]);