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
Addednew 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({
13 + it(
"parses slash commands and arguments", ()
=> {
14 + expect(
parseComposerInput(
"/mode terse now")).
toEqual({
18 + text:
"/mode terse now",
23 + it(
"parses skill commands", ()
=> {
24 + expect(
parseComposerInput(
"/skill:brutal make it short")).
toEqual({
26 + command:
"skill:brutal",
27 + args:
"make it short",
28 + text:
"/skill:brutal make it short",
33 + it(
"extracts at-references from prompts", ()
=> {
34 + expect(
parseComposerInput(
"read @README.md and @docs/spec.md please")).
toEqual({
36 + text:
"read @README.md and @docs/spec.md please",
37 + refs: [
"README.md",
"docs/spec.md"],
41 + it(
"parses single-bang bash", ()
=> {
42 + expect(
parseComposerInput(
"!git status --short")).
toEqual({
44 + command:
"git status --short",
45 + excludeFromContext:
false,
50 + it(
"parses double-bang bash as excluded from context", ()
=> {
51 + expect(
parseComposerInput(
"!!node --version")).
toEqual({
53 + command:
"node --version",
54 + excludeFromContext:
true,
extensions/ui/composer.ts
Addednew file mode 100644 1 +export type ComposerParseResult = 17 + excludeFromContext
: boolean;
21 +const REF_RE
= /(^|\s)@([^\s]+)/g;
23 +export function parseComposerInput(input
: string)
: ComposerParseResult {
24 + const text
= input.
trim();
25 + if (text.
startsWith(
"!!")) {
28 + command: text.
slice(
2).
trimStart(),
29 + excludeFromContext:
true,
33 + if (text.
startsWith(
"!")) {
36 + command: text.
slice(
1).
trimStart(),
37 + excludeFromContext:
false,
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();
54 + return { kind:
"prompt", text, refs:
refs(text) };
57 +function refs(text
: string)
: string[] {
58 + return [
...text.
matchAll(REF_RE)].
map((match)
=> match[
2]);