@dtmd/temper 0.0.25 → 0.0.26
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/README.md +14 -12
- package/dist/src/assembly.d.ts +45 -11
- package/dist/src/assembly.js +115 -7
- package/dist/src/builtins.d.ts +433 -103
- package/dist/src/builtins.js +771 -167
- package/dist/src/claude-code.d.ts +6 -3
- package/dist/src/claude-code.js +2 -2
- package/dist/src/contract.d.ts +153 -44
- package/dist/src/contract.js +123 -34
- package/dist/src/counsel.d.ts +11 -0
- package/dist/src/counsel.js +68 -0
- package/dist/src/declarations.d.ts +108 -2
- package/dist/src/declarations.js +606 -100
- package/dist/src/dial.d.ts +3 -3
- package/dist/src/dial.js +21 -5
- package/dist/src/emit.d.ts +12 -14
- package/dist/src/emit.js +123 -47
- package/dist/src/generated/AbsenceGateRow.d.ts +16 -0
- package/dist/src/generated/AbsenceGateRow.js +2 -0
- package/dist/src/generated/AreaMode.d.ts +5 -0
- package/dist/src/generated/AreaMode.js +2 -0
- package/dist/src/generated/AreaRow.d.ts +20 -0
- package/dist/src/generated/AreaRow.js +1 -0
- package/dist/src/generated/AssemblyFactRow.d.ts +17 -4
- package/dist/src/generated/BoundRow.d.ts +9 -4
- package/dist/src/generated/CedeRow.d.ts +14 -0
- package/dist/src/generated/CedeRow.js +2 -0
- package/dist/src/generated/CharsetRow.d.ts +14 -3
- package/dist/src/generated/ClauseRow.d.ts +23 -21
- package/dist/src/generated/CollectionAddressRow.d.ts +1 -1
- package/dist/src/generated/Declarations.d.ts +24 -9
- package/dist/src/generated/FeatureValue.d.ts +3 -3
- package/dist/src/generated/Features.d.ts +3 -3
- package/dist/src/generated/KindFactRow.d.ts +41 -8
- package/dist/src/generated/Payload.d.ts +3 -5
- package/dist/src/generated/PayloadMember.d.ts +7 -0
- package/dist/src/generated/RunsRow.d.ts +17 -0
- package/dist/src/generated/RunsRow.js +2 -0
- package/dist/src/generated/Section.d.ts +2 -2
- package/dist/src/generated/SectionContainsRow.d.ts +1 -1
- package/dist/src/generated/TypecheckRow.d.ts +20 -0
- package/dist/src/generated/TypecheckRow.js +2 -0
- package/dist/src/generated/Verifier.d.ts +8 -2
- package/dist/src/generated/index.d.ts +6 -0
- package/dist/src/index.d.ts +20 -15
- package/dist/src/index.js +10 -9
- package/dist/src/kind.d.ts +382 -52
- package/dist/src/kind.js +461 -49
- package/dist/src/lockfile.d.ts +39 -0
- package/dist/src/lockfile.js +265 -0
- package/dist/src/member-address.d.ts +15 -3
- package/dist/src/member-address.js +23 -6
- package/dist/src/needs.d.ts +25 -15
- package/dist/src/needs.js +10 -11
- package/dist/src/patterns.d.ts +112 -0
- package/dist/src/patterns.js +90 -0
- package/dist/src/pipe.d.ts +16 -0
- package/dist/src/pipe.js +39 -0
- package/dist/src/playbook.d.ts +203 -0
- package/dist/src/playbook.js +239 -0
- package/dist/src/prose.d.ts +69 -12
- package/dist/src/prose.js +125 -13
- package/dist/src/reference.d.ts +70 -0
- package/dist/src/reference.js +368 -0
- package/package.json +13 -4
- package/schemas/agent.json +23 -0
- package/schemas/command.json +5 -0
- package/schemas/output-style.json +4 -0
- package/schemas/rule.json +33 -0
- package/schemas/skill.json +63 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `@dtmd/temper/patterns` subpath — temper's default harness as shapes the consumer
|
|
3
|
+
* hydrates. Each export is a typed constructor: temper supplies the structure, wiring,
|
|
4
|
+
* edges and rendering; the consumer supplies the content. A required parameter is where
|
|
5
|
+
* the consumer's own content lives and has no default; an optional one is mechanics,
|
|
6
|
+
* with a default.
|
|
7
|
+
*
|
|
8
|
+
* These are the **mechanics** shapes — what using temper well takes in any harness:
|
|
9
|
+
*
|
|
10
|
+
* - {@link gateHooks} — the three gate hooks: the session-start report and the guard's
|
|
11
|
+
* two edges.
|
|
12
|
+
* - {@link typecheck} — the compiler `emit` runs over the program before running it.
|
|
13
|
+
* - {@link reference} — the emitted harness reference, its description hydratable and its
|
|
14
|
+
* topics derived from what is declared.
|
|
15
|
+
*
|
|
16
|
+
* And the central pattern, {@link playbook}: one area of guidance as a path rule, a routing
|
|
17
|
+
* skill and its topic files.
|
|
18
|
+
*
|
|
19
|
+
* The SDK counsel rule is not a shape here: `emit` synthesizes it over every program,
|
|
20
|
+
* patterns or not, so there is nothing to compose. {@link defaultPatterns} composes every shape
|
|
21
|
+
* with its defaults, and the playbooks it is given, into the parts `harness()` takes. It carries no taste — no memory
|
|
22
|
+
* content, no conventions — and declares no telemetry: a tap is opt-in, the consumer's
|
|
23
|
+
* own `require` entry with a `telemetry` verifier.
|
|
24
|
+
*/
|
|
25
|
+
import { GUARD_MATCHER, gateHook, tool as toolMember } from "./builtins.js";
|
|
26
|
+
import { reference } from "./reference.js";
|
|
27
|
+
export { reference } from "./reference.js";
|
|
28
|
+
export { playbook } from "./playbook.js";
|
|
29
|
+
/**
|
|
30
|
+
* The tool-name matcher the guard's `PostToolUse` edge binds: a Bash write carries no file
|
|
31
|
+
* path, so the guard judges the tree the call left (`src/install.rs`'s `BASH_MATCHER`).
|
|
32
|
+
*/
|
|
33
|
+
const SHELL_MATCHER = "Bash";
|
|
34
|
+
/**
|
|
35
|
+
* The three gate hooks: the advisory report at `SessionStart`, the write-boundary guard
|
|
36
|
+
* at `PreToolUse` over the file-writing tools, and the guard's shell edge at
|
|
37
|
+
* `PostToolUse` over `Bash`. Each is a `gateHook(...)` member, so the engine renders its
|
|
38
|
+
* command; the harness's `mode` decides how firmly the guard binds.
|
|
39
|
+
*
|
|
40
|
+
* # Throws
|
|
41
|
+
* If `tool` is not a `tool(...)` member (`gateHook`).
|
|
42
|
+
*/
|
|
43
|
+
export function gateHooks(init = {}) {
|
|
44
|
+
const tool = init.tool === undefined ? {} : { tool: init.tool };
|
|
45
|
+
const claims = (event) => {
|
|
46
|
+
const names = init.satisfies?.[event];
|
|
47
|
+
return names === undefined ? {} : { satisfies: names };
|
|
48
|
+
};
|
|
49
|
+
return [
|
|
50
|
+
gateHook({ event: "SessionStart", ...tool, ...claims("SessionStart") }),
|
|
51
|
+
gateHook({ event: "PreToolUse", matcher: GUARD_MATCHER, ...tool, ...claims("PreToolUse") }),
|
|
52
|
+
gateHook({ event: "PostToolUse", matcher: SHELL_MATCHER, ...tool, ...claims("PostToolUse") }),
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The program's typecheck — the compiler `emit` runs before the program, refusing on a
|
|
57
|
+
* type error. Its `tool` is a member the harness composes in `members`, as {@link defaultPatterns}
|
|
58
|
+
* does.
|
|
59
|
+
*
|
|
60
|
+
* # Throws
|
|
61
|
+
* If `tool` is not a `tool(...)` member.
|
|
62
|
+
*/
|
|
63
|
+
export function typecheck(init) {
|
|
64
|
+
if (init.tool !== undefined && init.tool.kind !== toolMember.key) {
|
|
65
|
+
throw new Error("typecheck: `tool` names the compiler the typecheck runs — pass a `tool(...)` member, " +
|
|
66
|
+
"or omit it for the program's own `tsc`.");
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
tool: init.tool ?? toolMember({ package: "typescript", bin: "tsc" }),
|
|
70
|
+
project: init.project,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Compose every mechanics shape with its defaults: the typecheck and its compiler, the
|
|
75
|
+
* three gate hooks and any tool they run, the harness reference, then every playbook given.
|
|
76
|
+
* A tool two shapes share is composed once.
|
|
77
|
+
*/
|
|
78
|
+
export function defaultPatterns(options) {
|
|
79
|
+
const checked = typecheck(options.typecheck);
|
|
80
|
+
const tools = [...new Set([checked.tool, ...(options.gates?.tool === undefined ? [] : [options.gates.tool])])];
|
|
81
|
+
return {
|
|
82
|
+
members: [
|
|
83
|
+
...tools,
|
|
84
|
+
...gateHooks(options.gates),
|
|
85
|
+
reference(options.reference),
|
|
86
|
+
...(options.playbooks ?? []).flatMap((playbook) => playbook.members),
|
|
87
|
+
],
|
|
88
|
+
typecheck: checked,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one pipe — the engine's face of the SDK, never the author's. `temper emit` runs
|
|
3
|
+
* the program's module through {@link pipe}: the module's default export is its harness,
|
|
4
|
+
* {@link emit} compiles it, and the seam goes to stdout for the engine to read. A program
|
|
5
|
+
* therefore carries no plumbing of its own and never calls an emit of its own; it ends
|
|
6
|
+
* `export default harness({ ... })`.
|
|
7
|
+
*/
|
|
8
|
+
export type { EmitResult, RegistrationFact, SettingsResidue } from "./emit.js";
|
|
9
|
+
export { emit } from "./emit.js";
|
|
10
|
+
/**
|
|
11
|
+
* The program at `entry` (a path), compiled: its module's default export, refused loud
|
|
12
|
+
* unless it is a composed harness.
|
|
13
|
+
*/
|
|
14
|
+
export declare function programSeam(entry: string): Promise<string>;
|
|
15
|
+
/** Run the program at `entry` and print its seam to stdout — the whole of the engine's run. */
|
|
16
|
+
export declare function pipe(entry: string): Promise<void>;
|
package/dist/src/pipe.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one pipe — the engine's face of the SDK, never the author's. `temper emit` runs
|
|
3
|
+
* the program's module through {@link pipe}: the module's default export is its harness,
|
|
4
|
+
* {@link emit} compiles it, and the seam goes to stdout for the engine to read. A program
|
|
5
|
+
* therefore carries no plumbing of its own and never calls an emit of its own; it ends
|
|
6
|
+
* `export default harness({ ... })`.
|
|
7
|
+
*/
|
|
8
|
+
import { pathToFileURL } from "node:url";
|
|
9
|
+
import { emit } from "./emit.js";
|
|
10
|
+
export { emit } from "./emit.js";
|
|
11
|
+
/**
|
|
12
|
+
* Whether `value` is a composed {@link Harness} — the shape `harness()` returns. A
|
|
13
|
+
* structural read: the seam's own refusals judge the rest.
|
|
14
|
+
*/
|
|
15
|
+
function isHarness(value) {
|
|
16
|
+
if (typeof value !== "object" || value === null) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const candidate = value;
|
|
20
|
+
return Array.isArray(candidate.members) && Array.isArray(candidate.expect) && Array.isArray(candidate.admit);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The program at `entry` (a path), compiled: its module's default export, refused loud
|
|
24
|
+
* unless it is a composed harness.
|
|
25
|
+
*/
|
|
26
|
+
export async function programSeam(entry) {
|
|
27
|
+
const module = (await import(pathToFileURL(entry).href));
|
|
28
|
+
const program = module.default;
|
|
29
|
+
if (!isHarness(program)) {
|
|
30
|
+
const found = program === undefined ? "no default export" : `a default export of type ${typeof program}`;
|
|
31
|
+
throw new Error(`${entry} exports no harness as its default (found ${found}): ` +
|
|
32
|
+
"end the program's module with `export default harness({ ... })`.");
|
|
33
|
+
}
|
|
34
|
+
return emit(program).seam;
|
|
35
|
+
}
|
|
36
|
+
/** Run the program at `entry` and print its seam to stdout — the whole of the engine's run. */
|
|
37
|
+
export async function pipe(entry) {
|
|
38
|
+
process.stdout.write(await programSeam(entry));
|
|
39
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The playbook — one area of guidance emitted across the surfaces Claude Code loads for
|
|
3
|
+
* it: a `rule` holding the area's standing facts, and a `skill` whose `SKILL.md` routes to
|
|
4
|
+
* one `supporting-doc` topic per procedure, every reference between them a typed mention.
|
|
5
|
+
* The consumer supplies the content (the name, the facts, the description, the topics);
|
|
6
|
+
* the shape supplies the wiring and the wording, whose router is a default the consumer
|
|
7
|
+
* replaces.
|
|
8
|
+
*
|
|
9
|
+
* Its axes: the trigger the area loads on, who may invoke its skill, its topics, its
|
|
10
|
+
* enforcement attachments, its execution and the agents that preload it. Execution and
|
|
11
|
+
* preloading are typed so an author sees what the shape could do, and refused until they
|
|
12
|
+
* are offered.
|
|
13
|
+
*/
|
|
14
|
+
import type { Clause } from "./contract.js";
|
|
15
|
+
import type { Member } from "./kind.js";
|
|
16
|
+
import type { Mentionable, Prose, Text } from "./prose.js";
|
|
17
|
+
import type { AreaMode, AreaRow } from "./generated/index.js";
|
|
18
|
+
/**
|
|
19
|
+
* Who may invoke the playbook's skill: `"model-and-user"` (the default) leaves both
|
|
20
|
+
* channels live; `"user"` withholds it from Claude, so only `/name` runs it; `"model"`
|
|
21
|
+
* hides it from the `/` menu, so only Claude loads it.
|
|
22
|
+
*/
|
|
23
|
+
export type PlaybookInvocation = "model-and-user" | "user" | "model";
|
|
24
|
+
/**
|
|
25
|
+
* A playbook bound to the files of its area: its rule loads the first time Claude reads a
|
|
26
|
+
* file its `paths` match.
|
|
27
|
+
*/
|
|
28
|
+
export interface PathsTrigger {
|
|
29
|
+
/** The globs naming the area's files — the rule's `paths`. */
|
|
30
|
+
readonly paths: readonly string[];
|
|
31
|
+
/**
|
|
32
|
+
* When the skill joins the skill listing: `"always"` (the default), so a question asked
|
|
33
|
+
* before any file is read can find it, or `"on-touch"`, gating the listing on the same
|
|
34
|
+
* `paths` — it spends no listing budget until the area is read, and is undiscoverable
|
|
35
|
+
* until then.
|
|
36
|
+
*/
|
|
37
|
+
readonly listing?: "always" | "on-touch";
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A playbook nested in a subdirectory: its rule lands in `<dir>/.claude/rules/` and its
|
|
41
|
+
* skill in `<dir>/.claude/skills/`, both loaded on demand once Claude works in that
|
|
42
|
+
* directory (code.claude.com/docs/en/memory, "Organize rules with `.claude/rules/`";
|
|
43
|
+
* code.claude.com/docs/en/skills, "Load skills in monorepos and subdirectories";
|
|
44
|
+
* retrieved 2026-11-24/25). Both members are named by the directory's segments joined
|
|
45
|
+
* with `:` ahead of the playbook's name — `apps/web` and `deploy` are `apps:web:deploy`.
|
|
46
|
+
*/
|
|
47
|
+
export interface NestedTrigger {
|
|
48
|
+
/** The subdirectory, relative to the harness root — `apps/web`. */
|
|
49
|
+
readonly nested: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* What a playbook loads on — a {@link PathsTrigger} or a {@link NestedTrigger}; absent,
|
|
53
|
+
* its rule loads at launch.
|
|
54
|
+
*/
|
|
55
|
+
export type PlaybookTrigger = PathsTrigger | NestedTrigger;
|
|
56
|
+
/**
|
|
57
|
+
* How the skill runs once invoked — inline in the session, or forked to an agent whose
|
|
58
|
+
* prompt its body becomes. Declared but not yet offered: setting it refuses.
|
|
59
|
+
*/
|
|
60
|
+
export type PlaybookExecution = "inline" | {
|
|
61
|
+
readonly fork: string;
|
|
62
|
+
};
|
|
63
|
+
/** One procedure the skill routes to — its own file beside `SKILL.md`. */
|
|
64
|
+
export interface PlaybookTopic {
|
|
65
|
+
/** The topic's file stem — `<name>.md` beside `SKILL.md`. */
|
|
66
|
+
readonly name: string;
|
|
67
|
+
/** The moment the router sends Claude to it — "deploying to staging". */
|
|
68
|
+
readonly when: string;
|
|
69
|
+
/** The procedure itself. */
|
|
70
|
+
readonly prose: Prose;
|
|
71
|
+
}
|
|
72
|
+
/** What the router renderer receives: the skill's name and a link per topic. */
|
|
73
|
+
export interface RouterView {
|
|
74
|
+
readonly name: string;
|
|
75
|
+
readonly topics: readonly {
|
|
76
|
+
readonly name: string;
|
|
77
|
+
readonly when: string;
|
|
78
|
+
/** The topic's path from `SKILL.md` — a typed mention, never a hand-typed path. */
|
|
79
|
+
readonly link: Mentionable;
|
|
80
|
+
}[];
|
|
81
|
+
}
|
|
82
|
+
/** What the pointer renderer receives: who may invoke the skill, and a mention of it. */
|
|
83
|
+
export interface PointerView {
|
|
84
|
+
readonly invocation: PlaybookInvocation;
|
|
85
|
+
readonly skill: Mentionable;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The playbook's wording: `router` renders the skill's `SKILL.md` body, `pointer` the
|
|
89
|
+
* line the rule closes on, sending Claude from the area's facts to its procedures.
|
|
90
|
+
*/
|
|
91
|
+
export interface PlaybookRenderer {
|
|
92
|
+
readonly router: (view: RouterView) => Text;
|
|
93
|
+
readonly pointer: (view: PointerView) => Text;
|
|
94
|
+
}
|
|
95
|
+
/** The requirements a playbook's rule and its skill each fill — the consumer's own domains. */
|
|
96
|
+
export interface PlaybookSatisfies {
|
|
97
|
+
readonly rule?: readonly string[];
|
|
98
|
+
readonly skill?: readonly string[];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The guard attachment: temper's own `guard` reads every pending edit's path against the
|
|
102
|
+
* area's paths, and `"point"` sends an edit there to the area's rule while `"refuse"`
|
|
103
|
+
* denies it naming the rule.
|
|
104
|
+
*/
|
|
105
|
+
export interface PlaybookGuard {
|
|
106
|
+
/** What the guard does with an edit on the area. */
|
|
107
|
+
readonly mode: AreaMode;
|
|
108
|
+
/**
|
|
109
|
+
* The {@link tool} the guard hook runs temper as — the same tool the harness's own gate
|
|
110
|
+
* hooks run, so the two hooks are one. Absent, the temper package's bin.
|
|
111
|
+
*/
|
|
112
|
+
readonly tool?: Member;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* What must always be true of the area, held by the harness rather than by a prose MUST.
|
|
116
|
+
* Permission rule syntax is Claude Code's (code.claude.com/docs/en/permissions, retrieved
|
|
117
|
+
* 2026-11-26).
|
|
118
|
+
*/
|
|
119
|
+
export interface PlaybookAttachments {
|
|
120
|
+
/**
|
|
121
|
+
* Clauses judged over the area's members — its rule, its skill and its topics. They
|
|
122
|
+
* attach to a requirement the playbook publishes, named `playbook:<name>`, that every
|
|
123
|
+
* one of those members satisfies, so the lock records them over exactly that selection.
|
|
124
|
+
*/
|
|
125
|
+
readonly clauses?: readonly Clause[];
|
|
126
|
+
/**
|
|
127
|
+
* The paths that need an owner present: each gitignore-style glob, relative to the
|
|
128
|
+
* harness root, derives the `ask` rule `Edit(/<glob>)` — `Edit` covers every built-in
|
|
129
|
+
* tool that edits files, and a leading `/` anchors the pattern at the project
|
|
130
|
+
* ("Read and Edit", "How path patterns match").
|
|
131
|
+
*/
|
|
132
|
+
readonly fence?: readonly string[];
|
|
133
|
+
/**
|
|
134
|
+
* The area's task scripts, each the command exactly as its procedure invokes it —
|
|
135
|
+
* `./scripts/deploy.sh staging`. Each derives the allow rule `Bash(<command>)`, which
|
|
136
|
+
* matches that one command and nothing wider ("Wildcard patterns": a rule with no `*`
|
|
137
|
+
* matches one exact command).
|
|
138
|
+
*/
|
|
139
|
+
readonly tasks?: readonly string[];
|
|
140
|
+
/**
|
|
141
|
+
* The guard hook over the area's paths — a `PreToolUse` gate hook over the file-writing
|
|
142
|
+
* tools, the same hook the harness's own gates place, so the edge still runs one
|
|
143
|
+
* command. Needs an area with paths: a paths or a nested trigger.
|
|
144
|
+
*/
|
|
145
|
+
readonly guard?: PlaybookGuard;
|
|
146
|
+
}
|
|
147
|
+
/** What {@link playbook} takes. */
|
|
148
|
+
export interface PlaybookInit {
|
|
149
|
+
/** The area's name — the rule's file stem and the skill's directory. */
|
|
150
|
+
readonly name: string;
|
|
151
|
+
/**
|
|
152
|
+
* The skill's description — how a session finds it. Required even for a path-triggered
|
|
153
|
+
* playbook, since its rule loads only on a read and never before.
|
|
154
|
+
*/
|
|
155
|
+
readonly description: string;
|
|
156
|
+
/** The area's standing facts — the rule's body, before its pointer to the skill. */
|
|
157
|
+
readonly facts: Text;
|
|
158
|
+
/** The procedures, one topic file each. At least one. */
|
|
159
|
+
readonly topics: readonly PlaybookTopic[];
|
|
160
|
+
/** What the area loads on; absent, its rule loads at launch. */
|
|
161
|
+
readonly trigger?: PlaybookTrigger;
|
|
162
|
+
/** Who may invoke the skill; absent, `"model-and-user"`. */
|
|
163
|
+
readonly invocation?: PlaybookInvocation;
|
|
164
|
+
/**
|
|
165
|
+
* The requirements the area fills, per surface — each the `satisfies` edge of that
|
|
166
|
+
* member. Per surface because a requirement may bind the kind that fills it: the
|
|
167
|
+
* standing facts fill one domain, the procedures another. Absent, none.
|
|
168
|
+
*/
|
|
169
|
+
readonly satisfies?: PlaybookSatisfies;
|
|
170
|
+
/** Replaces the default wording, either half. */
|
|
171
|
+
readonly render?: Partial<PlaybookRenderer>;
|
|
172
|
+
/** What the harness holds true of the area; absent, nothing. */
|
|
173
|
+
readonly attachments?: PlaybookAttachments;
|
|
174
|
+
/** Declared but not yet offered — setting it refuses. */
|
|
175
|
+
readonly execution?: PlaybookExecution;
|
|
176
|
+
/** The agents that preload the skill. Declared but not yet offered — setting it refuses. */
|
|
177
|
+
readonly preloadedBy?: readonly Member[];
|
|
178
|
+
}
|
|
179
|
+
/** The playbook's members — spread `members` into `harness({ members })`. */
|
|
180
|
+
export interface Playbook {
|
|
181
|
+
readonly rule: Member;
|
|
182
|
+
readonly skill: Member;
|
|
183
|
+
readonly topics: readonly Member[];
|
|
184
|
+
/** The guard attachment's hook, when the playbook declares one. */
|
|
185
|
+
readonly guard?: Member;
|
|
186
|
+
/** The rule, the skill, its topics, then its guard hook. */
|
|
187
|
+
readonly members: readonly Member[];
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The area a guarded playbook's rule records on the lock, or `undefined` for every other
|
|
191
|
+
* member. The one reader is the declaration erasure (`declarations.ts`).
|
|
192
|
+
*/
|
|
193
|
+
export declare function playbookArea(member: Member): AreaRow | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Declare a playbook: its rule, its routing skill and its topics, with its attachments.
|
|
196
|
+
*
|
|
197
|
+
* # Throws
|
|
198
|
+
* If `execution` or `preloadedBy` is set (not yet offered), if `description` is blank, if
|
|
199
|
+
* there is no topic, if two topics share a name, if a nested directory is no plain
|
|
200
|
+
* relative path, if a fence path or task command is malformed, or if a guard attachment is
|
|
201
|
+
* declared on a playbook with no trigger or with a tool that is not a `tool(...)` member.
|
|
202
|
+
*/
|
|
203
|
+
export declare function playbook(init: PlaybookInit): Playbook;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The playbook — one area of guidance emitted across the surfaces Claude Code loads for
|
|
3
|
+
* it: a `rule` holding the area's standing facts, and a `skill` whose `SKILL.md` routes to
|
|
4
|
+
* one `supporting-doc` topic per procedure, every reference between them a typed mention.
|
|
5
|
+
* The consumer supplies the content (the name, the facts, the description, the topics);
|
|
6
|
+
* the shape supplies the wiring and the wording, whose router is a default the consumer
|
|
7
|
+
* replaces.
|
|
8
|
+
*
|
|
9
|
+
* Its axes: the trigger the area loads on, who may invoke its skill, its topics, its
|
|
10
|
+
* enforcement attachments, its execution and the agents that preload it. Execution and
|
|
11
|
+
* preloading are typed so an author sees what the shape could do, and refused until they
|
|
12
|
+
* are offered.
|
|
13
|
+
*/
|
|
14
|
+
import { GUARD_MATCHER, gateHook, rule, skill, supportingDoc } from "./builtins.js";
|
|
15
|
+
import { memberAddress, nestedAddress } from "./member-address.js";
|
|
16
|
+
import { bash, capability } from "./needs.js";
|
|
17
|
+
import { blocks, joined, linked, linkedText, mentionOf } from "./prose.js";
|
|
18
|
+
/** Where a not-yet-offered axis is asked for. */
|
|
19
|
+
const REQUEST_AT = "https://github.com/duct-tape-and-markdown/temper/issues";
|
|
20
|
+
/** Each guarded playbook's rule mapped to the area its guard binds — the fact
|
|
21
|
+
* {@link playbookArea} reads, keyed by the member so the rule's fields stay exactly its
|
|
22
|
+
* frontmatter. */
|
|
23
|
+
const guardedAreas = new WeakMap();
|
|
24
|
+
/**
|
|
25
|
+
* The area a guarded playbook's rule records on the lock, or `undefined` for every other
|
|
26
|
+
* member. The one reader is the declaration erasure (`declarations.ts`).
|
|
27
|
+
*/
|
|
28
|
+
export function playbookArea(member) {
|
|
29
|
+
return guardedAreas.get(member);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The paths a guard attachment binds: a paths trigger's own globs, or everything beneath a
|
|
33
|
+
* nested trigger's directory.
|
|
34
|
+
*
|
|
35
|
+
* # Throws
|
|
36
|
+
* If the playbook has no trigger, since an area loaded at launch names no paths to guard.
|
|
37
|
+
*/
|
|
38
|
+
function guardedPaths(name, trigger) {
|
|
39
|
+
if (trigger !== undefined && "paths" in trigger)
|
|
40
|
+
return [...trigger.paths];
|
|
41
|
+
if (trigger !== undefined)
|
|
42
|
+
return [`${trigger.nested}/**`];
|
|
43
|
+
throw new Error(`playbook \`${name}\`: a guard attachment binds the edits on the area's paths, and a playbook ` +
|
|
44
|
+
"with no trigger names none — give it a `paths` or `nested` trigger, or drop `guard`.");
|
|
45
|
+
}
|
|
46
|
+
/** The default router: a heading, then one line per topic naming its moment and its file. */
|
|
47
|
+
function defaultRouter(view) {
|
|
48
|
+
return linkedText(joined([
|
|
49
|
+
`# ${view.name}\n\nOpen the one topic the step at hand needs:\n`,
|
|
50
|
+
...view.topics.map((topic) => linked `- When ${topic.when}: read [${topic.name}](${topic.link}).`),
|
|
51
|
+
"",
|
|
52
|
+
], "\n"));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The default pointer. A user-only skill is withheld from Claude, so the rule asks the
|
|
56
|
+
* user to run it rather than sending Claude to a skill it cannot load.
|
|
57
|
+
*/
|
|
58
|
+
function defaultPointer(view) {
|
|
59
|
+
// A nested skill's mention already renders its invocation, slash and all (`/apps/web:x`).
|
|
60
|
+
const slash = view.skill.display.startsWith("/") ? "" : "/";
|
|
61
|
+
const line = view.invocation === "user"
|
|
62
|
+
? linked `This area's procedures run only when the user invokes them: when one is needed, ask the user to run \`${slash}${view.skill}\`.`
|
|
63
|
+
: linked `For a procedure in this area, use the \`${view.skill}\` skill: it routes to one topic per procedure.`;
|
|
64
|
+
return linkedText(line);
|
|
65
|
+
}
|
|
66
|
+
/** The refusal of an axis the shape declares but does not offer yet. */
|
|
67
|
+
function notYetOffered(name, axis, field) {
|
|
68
|
+
return new Error(`playbook \`${name}\`: ${axis} is declared but not yet offered — remove \`${field}\`, ` +
|
|
69
|
+
`or request it at ${REQUEST_AT}.`);
|
|
70
|
+
}
|
|
71
|
+
/** The skill fields an invocation sets. */
|
|
72
|
+
function invocationFields(invocation) {
|
|
73
|
+
switch (invocation) {
|
|
74
|
+
case "model-and-user":
|
|
75
|
+
return {};
|
|
76
|
+
case "user":
|
|
77
|
+
return { "disable-model-invocation": true };
|
|
78
|
+
case "model":
|
|
79
|
+
return { "user-invocable": false };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The placement a trigger gives the playbook named `name`: a nested trigger prefixes both
|
|
84
|
+
* members with its directory's segments and declares where each lands under it.
|
|
85
|
+
*
|
|
86
|
+
* # Throws
|
|
87
|
+
* If the nested directory is no plain relative path, or names a `.claude` folder.
|
|
88
|
+
*/
|
|
89
|
+
function placementOf(name, trigger) {
|
|
90
|
+
if (trigger === undefined || !("nested" in trigger))
|
|
91
|
+
return { prefix: "" };
|
|
92
|
+
const segments = trigger.nested.split("/");
|
|
93
|
+
const plain = segments.every((segment) => segment !== "" && segment !== "." && segment !== ".." && segment !== ".claude" && !/[\\:*?]/.test(segment));
|
|
94
|
+
if (!plain) {
|
|
95
|
+
throw new Error(`playbook \`${name}\`: nested directory \`${trigger.nested}\` is no plain relative path — ` +
|
|
96
|
+
"name the subdirectory from the harness root, `/`-joined, with no `.`, `..`, `.claude`, `:` or glob segment.");
|
|
97
|
+
}
|
|
98
|
+
const dir = segments.join("/");
|
|
99
|
+
return {
|
|
100
|
+
prefix: `${segments.join(":")}:`,
|
|
101
|
+
rule: `${dir}/.claude/rules/${name}.md`,
|
|
102
|
+
skill: `${dir}/.claude/skills/${name}/SKILL.md`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The `ask` capabilities a fence derives, one `Edit(/<glob>)` rule per path.
|
|
107
|
+
*
|
|
108
|
+
* # Throws
|
|
109
|
+
* If a path is blank, anchored (`/`, `//`, `~`), or climbs out of the harness root (`..`).
|
|
110
|
+
*/
|
|
111
|
+
function fenceNeeds(name, fence) {
|
|
112
|
+
return fence.map((path) => {
|
|
113
|
+
const relative = path.trim() !== "" && !/^[/~]/.test(path) && !path.split("/").includes("..");
|
|
114
|
+
if (!relative) {
|
|
115
|
+
throw new Error(`playbook \`${name}\`: fence path \`${path}\` is no glob relative to the harness root — ` +
|
|
116
|
+
"write it the way a `paths` trigger names files, with no leading `/`, `~` or `..` segment.");
|
|
117
|
+
}
|
|
118
|
+
return capability(`Edit(/${path})`, { list: "ask" });
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The allow capabilities the task scripts derive, one exact `Bash(<command>)` rule each.
|
|
123
|
+
*
|
|
124
|
+
* # Throws
|
|
125
|
+
* If a command is blank, carries a `*` (which would widen the rule past the invocation), or
|
|
126
|
+
* chains commands with a shell separator (Claude Code matches each subcommand on its own,
|
|
127
|
+
* so a compound rule matches nothing — "Compound commands").
|
|
128
|
+
*/
|
|
129
|
+
function taskNeeds(name, tasks) {
|
|
130
|
+
return tasks.map((command) => {
|
|
131
|
+
if (command.trim() === "" || command !== command.trim()) {
|
|
132
|
+
throw new Error(`playbook \`${name}\`: task \`${command}\` is blank or padded — write the command as invoked.`);
|
|
133
|
+
}
|
|
134
|
+
if (command.includes("*")) {
|
|
135
|
+
throw new Error(`playbook \`${name}\`: task \`${command}\` carries a \`*\` — a task's allow rule is the command ` +
|
|
136
|
+
"exactly as invoked, never a wildcard.");
|
|
137
|
+
}
|
|
138
|
+
if (/[;|&\n]/.test(command)) {
|
|
139
|
+
throw new Error(`playbook \`${name}\`: task \`${command}\` chains commands — Claude Code matches each subcommand ` +
|
|
140
|
+
"on its own, so allowlist one script per task.");
|
|
141
|
+
}
|
|
142
|
+
return bash(command);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Declare a playbook: its rule, its routing skill and its topics, with its attachments.
|
|
147
|
+
*
|
|
148
|
+
* # Throws
|
|
149
|
+
* If `execution` or `preloadedBy` is set (not yet offered), if `description` is blank, if
|
|
150
|
+
* there is no topic, if two topics share a name, if a nested directory is no plain
|
|
151
|
+
* relative path, if a fence path or task command is malformed, or if a guard attachment is
|
|
152
|
+
* declared on a playbook with no trigger or with a tool that is not a `tool(...)` member.
|
|
153
|
+
*/
|
|
154
|
+
export function playbook(init) {
|
|
155
|
+
const { name } = init;
|
|
156
|
+
if (init.execution !== undefined)
|
|
157
|
+
throw notYetOffered(name, "execution", "execution");
|
|
158
|
+
if (init.preloadedBy !== undefined)
|
|
159
|
+
throw notYetOffered(name, "preloading", "preloadedBy");
|
|
160
|
+
if (init.description.trim() === "") {
|
|
161
|
+
throw new Error(`playbook \`${name}\`: \`description\` is how a session finds the skill before any file is read — write one.`);
|
|
162
|
+
}
|
|
163
|
+
if (init.topics.length === 0) {
|
|
164
|
+
throw new Error(`playbook \`${name}\`: a playbook routes to at least one topic — declare its procedures.`);
|
|
165
|
+
}
|
|
166
|
+
const seen = new Set();
|
|
167
|
+
for (const topic of init.topics) {
|
|
168
|
+
if (seen.has(topic.name))
|
|
169
|
+
throw new Error(`playbook \`${name}\`: two topics are named \`${topic.name}\`.`);
|
|
170
|
+
seen.add(topic.name);
|
|
171
|
+
}
|
|
172
|
+
const invocation = init.invocation ?? "model-and-user";
|
|
173
|
+
const router = init.render?.router ?? defaultRouter;
|
|
174
|
+
const pointer = init.render?.pointer ?? defaultPointer;
|
|
175
|
+
const trigger = init.trigger;
|
|
176
|
+
const byPaths = trigger !== undefined && "paths" in trigger ? trigger : undefined;
|
|
177
|
+
const paths = byPaths === undefined ? undefined : [...byPaths.paths];
|
|
178
|
+
const gated = paths !== undefined && byPaths?.listing === "on-touch";
|
|
179
|
+
const placement = placementOf(name, trigger);
|
|
180
|
+
const id = `${placement.prefix}${name}`;
|
|
181
|
+
const attached = init.attachments ?? {};
|
|
182
|
+
const fence = fenceNeeds(name, attached.fence ?? []);
|
|
183
|
+
const tasks = taskNeeds(name, attached.tasks ?? []);
|
|
184
|
+
const clauses = attached.clauses ?? [];
|
|
185
|
+
// The area requirement exists only to carry the attached clauses over the area's members.
|
|
186
|
+
const area = clauses.length === 0 ? undefined : `playbook:${id}`;
|
|
187
|
+
const claims = (names) => {
|
|
188
|
+
const all = [...(names ?? []), ...(area === undefined ? [] : [area])];
|
|
189
|
+
return all.length === 0 ? {} : { satisfies: all };
|
|
190
|
+
};
|
|
191
|
+
const needs = (list) => (list.length === 0 ? {} : { needs: list });
|
|
192
|
+
const requires = () => area === undefined
|
|
193
|
+
? {}
|
|
194
|
+
: {
|
|
195
|
+
requires: {
|
|
196
|
+
[area]: {
|
|
197
|
+
prose: `What must always be true of the \`${id}\` playbook's area — its rule, its skill and its topics.`,
|
|
198
|
+
clauses,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
const fields = {
|
|
203
|
+
name: id,
|
|
204
|
+
description: init.description,
|
|
205
|
+
...claims(init.satisfies?.skill),
|
|
206
|
+
...needs(tasks),
|
|
207
|
+
...invocationFields(invocation),
|
|
208
|
+
...(gated ? { paths } : {}),
|
|
209
|
+
...(placement.skill === undefined ? {} : { location: placement.skill }),
|
|
210
|
+
};
|
|
211
|
+
const draft = skill(fields);
|
|
212
|
+
const host = memberAddress(draft);
|
|
213
|
+
const body = router({
|
|
214
|
+
name,
|
|
215
|
+
topics: init.topics.map((topic) => ({
|
|
216
|
+
name: topic.name,
|
|
217
|
+
when: topic.when,
|
|
218
|
+
link: { address: nestedAddress(host, supportingDoc.key, topic.name), display: topic.name, form: "path" },
|
|
219
|
+
})),
|
|
220
|
+
});
|
|
221
|
+
const routing = skill({ ...fields, prose: body });
|
|
222
|
+
const topics = init.topics.map((topic) => supportingDoc({ name: topic.name, host: routing, ...claims(undefined), prose: topic.prose }));
|
|
223
|
+
const standing = rule({
|
|
224
|
+
name: id,
|
|
225
|
+
...claims(init.satisfies?.rule),
|
|
226
|
+
...requires(),
|
|
227
|
+
...needs(fence),
|
|
228
|
+
...(paths === undefined ? {} : { paths }),
|
|
229
|
+
...(placement.rule === undefined ? {} : { location: placement.rule }),
|
|
230
|
+
prose: blocks(init.facts, pointer({ invocation, skill: mentionOf(routing) })),
|
|
231
|
+
});
|
|
232
|
+
if (attached.guard === undefined) {
|
|
233
|
+
return { rule: standing, skill: routing, topics, members: [standing, routing, ...topics] };
|
|
234
|
+
}
|
|
235
|
+
const { mode, tool } = attached.guard;
|
|
236
|
+
guardedAreas.set(standing, { rule: memberAddress(standing), paths: guardedPaths(name, trigger), mode });
|
|
237
|
+
const guard = gateHook({ event: "PreToolUse", matcher: GUARD_MATCHER, ...(tool === undefined ? {} : { tool }) });
|
|
238
|
+
return { rule: standing, skill: routing, topics, guard, members: [standing, routing, ...topics, guard] };
|
|
239
|
+
}
|