@danypops/papyrus 0.28.3 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/extension/src/playbook-bridge.ts +55 -65
- package/package.json +1 -1
|
@@ -1,90 +1,80 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* playbook-bridge.ts — materializes active Papyrus Playbooks as
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* playbook-bridge.ts — materializes active Papyrus Playbooks as their own /playbook:name slash
|
|
3
|
+
* commands, one per playbook, the same one-entry-per-item autocomplete experience Pi's own
|
|
4
|
+
* /skill:name gives real skills.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* /skill:name itself is a hardcoded core mechanism (Pi's interactive mode builds it directly
|
|
7
|
+
* from its own skill loader) -- not something an extension can retarget to a different prefix.
|
|
8
|
+
* pi.registerCommand(name, ...) accepts any string, colons included, so "playbook:<slug>"
|
|
9
|
+
* registers and invokes as literally /playbook:<slug> -- a real, supported extension API, no
|
|
10
|
+
* core touched.
|
|
11
|
+
*
|
|
12
|
+
* Real limitation: ExtensionAPI has no unregisterCommand. A disabled or renamed playbook's old
|
|
13
|
+
* /playbook:<slug> command lingers until a full Pi restart -- registerCommand can only add or
|
|
14
|
+
* overwrite, never remove. Mitigated two ways: registrations refresh on every resources_discover
|
|
15
|
+
* (session start and /reload), so a renamed playbook's NEW slug appears promptly even though the
|
|
16
|
+
* old one lingers; and each command's handler re-fetches the live playbook by id at invocation
|
|
17
|
+
* time rather than baking in stale content, so even a lingering stale name fails cleanly with a
|
|
18
|
+
* real error instead of running deleted content.
|
|
12
19
|
*/
|
|
13
|
-
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
14
|
-
import { homedir } from "node:os";
|
|
15
|
-
import { join } from "node:path";
|
|
16
20
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
17
21
|
import type { Artifact } from "../../src/domain/artifact.ts";
|
|
18
22
|
import { callService } from "./service-client.ts";
|
|
19
23
|
|
|
20
24
|
const PLAYBOOK_BRIDGE_MAX_PLAYBOOKS = 100;
|
|
21
|
-
const PLAYBOOK_BRIDGE_DESCRIPTION_MAX_CHARACTERS = 1000;
|
|
22
25
|
|
|
23
26
|
function slugify(title: string): string {
|
|
24
27
|
const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 64);
|
|
25
28
|
return slug.length > 0 ? slug : "playbook";
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
return join(base, "papyrus", "playbooks");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function stringList(value: unknown): string[] {
|
|
34
|
-
return Array.isArray(value) ? value.filter((entry): entry is string => typeof entry === "string") : [];
|
|
31
|
+
async function activePlaybooks(): Promise<Artifact[]> {
|
|
32
|
+
return callService<Record<string, unknown>, Artifact[]>("playbooks.list", { status: "active", limit: PLAYBOOK_BRIDGE_MAX_PLAYBOOKS });
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const tools = stringList(playbook.extra["tools"]);
|
|
41
|
-
const description = trigger.replace(/\n/g, " ").slice(0, PLAYBOOK_BRIDGE_DESCRIPTION_MAX_CHARACTERS);
|
|
42
|
-
return [
|
|
43
|
-
"---",
|
|
44
|
-
`name: ${slugify(playbook.title)}`,
|
|
45
|
-
`description: ${description}`,
|
|
46
|
-
"---",
|
|
47
|
-
"",
|
|
48
|
-
`# ${playbook.title}`,
|
|
49
|
-
"",
|
|
50
|
-
`Materialized from a live Papyrus playbook; edits here are lost on the next refresh. Edit the playbook itself instead (the playbooks tool, action=update), then /reload.`,
|
|
51
|
-
"",
|
|
52
|
-
`Trigger: ${trigger}`,
|
|
53
|
-
"",
|
|
54
|
-
...(playbook.body ? [`Context: ${playbook.body}`, ""] : []),
|
|
55
|
-
...(steps.length > 0 ? ["## Steps", "", ...steps.map((step, index) => `${index + 1}. ${step}`), ""] : []),
|
|
56
|
-
...(tools.length > 0 ? [`Tools: ${tools.join(", ")}`, ""] : []),
|
|
57
|
-
].join("\n");
|
|
35
|
+
/** Exported for direct testing without a real ExtensionAPI. */
|
|
36
|
+
export function playbookCommandName(title: string): string {
|
|
37
|
+
return `playbook:${slugify(title)}`;
|
|
58
38
|
}
|
|
59
39
|
|
|
60
|
-
/** Exported for direct testing without a real ExtensionAPI. */
|
|
61
|
-
export async function
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (usedSlugs.has(slug)) slug = `${slug}-${playbook.id.slice(0, 8)}`; // a real title collision, not the common case
|
|
72
|
-
usedSlugs.add(slug);
|
|
73
|
-
const skillDir = join(dir, slug);
|
|
74
|
-
mkdirSync(skillDir, { recursive: true });
|
|
75
|
-
const filePath = join(skillDir, "SKILL.md");
|
|
76
|
-
writeFileSync(filePath, playbookSkillMarkdown(playbook), "utf8");
|
|
77
|
-
paths.push(filePath);
|
|
78
|
-
}
|
|
79
|
-
return paths;
|
|
40
|
+
/** Exported for direct testing without a real ExtensionAPI: what would be registered right now. */
|
|
41
|
+
export async function planPlaybookCommandRegistrations(): Promise<Array<{ name: string; id: string; title: string; trigger: string }>> {
|
|
42
|
+
const playbooks = await activePlaybooks();
|
|
43
|
+
const usedNames = new Set<string>();
|
|
44
|
+
return playbooks.map((playbook) => {
|
|
45
|
+
let name = playbookCommandName(playbook.title);
|
|
46
|
+
if (usedNames.has(name)) name = `${name}-${playbook.id.slice(0, 8)}`; // a real title collision, not the common case
|
|
47
|
+
usedNames.add(name);
|
|
48
|
+
const trigger = typeof playbook.extra["trigger"] === "string" ? playbook.extra["trigger"] : "manual invocation";
|
|
49
|
+
return { name, id: playbook.id, title: playbook.title, trigger };
|
|
50
|
+
});
|
|
80
51
|
}
|
|
81
52
|
|
|
82
53
|
export function registerPlaybookBridge(pi: ExtensionAPI): void {
|
|
83
|
-
|
|
54
|
+
const refresh = async () => {
|
|
84
55
|
try {
|
|
85
|
-
|
|
56
|
+
const registrations = await planPlaybookCommandRegistrations();
|
|
57
|
+
for (const { name, id, title, trigger } of registrations) {
|
|
58
|
+
pi.registerCommand(name, {
|
|
59
|
+
description: trigger,
|
|
60
|
+
handler: async (_args, ctx) => {
|
|
61
|
+
try {
|
|
62
|
+
// Re-fetched live, not captured at registration time: a lingering stale
|
|
63
|
+
// command (renamed or disabled since, since registerCommand can't be
|
|
64
|
+
// unregistered) must fail cleanly, never run deleted/stale content.
|
|
65
|
+
const invocation = await callService<Record<string, unknown>, string>("playbooks.invoke", { id });
|
|
66
|
+
ctx.ui.setEditorText(invocation);
|
|
67
|
+
ctx.ui.notify(`"${title}" invocation placed in the editor`, "info");
|
|
68
|
+
} catch (error) {
|
|
69
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
86
74
|
} catch {
|
|
87
|
-
|
|
75
|
+
// A Papyrus daemon hiccup must never break Pi's own resource discovery -- degrades to
|
|
76
|
+
// "no new/updated playbook commands this cycle", not a broken session start.
|
|
88
77
|
}
|
|
89
|
-
}
|
|
78
|
+
};
|
|
79
|
+
pi.on("resources_discover", async () => { await refresh(); return {}; });
|
|
90
80
|
}
|
package/package.json
CHANGED