@zgeoff/skill-gate 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +84 -2
- package/dist/build-bash-skill-gate-output.d.ts +4 -0
- package/dist/build-skill-gate-output.d.ts +2 -0
- package/dist/collect-gate-skills.d.ts +2 -0
- package/dist/collect-loaded-skills.d.ts +2 -0
- package/dist/collect-written-paths.d.ts +2 -0
- package/dist/format-skill-list.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/parse-shell-commands.d.ts +2 -0
- package/dist/parse-skill-gate-rules.d.ts +2 -0
- package/dist/plan-bash-skill-gate.d.ts +2 -0
- package/dist/plan-skill-gate.d.ts +2 -0
- package/dist/run-skill-gate.d.ts +1 -0
- package/dist/types.d.ts +79 -0
- package/package.json +47 -4
- package/src/build-bash-skill-gate-output.ts +41 -0
- package/src/build-skill-gate-output.ts +54 -0
- package/src/collect-gate-skills.ts +19 -0
- package/src/collect-loaded-skills.ts +98 -0
- package/src/collect-written-paths.ts +451 -0
- package/src/format-skill-list.ts +10 -0
- package/src/index.ts +1 -0
- package/src/parse-shell-commands.ts +429 -0
- package/src/parse-skill-gate-rules.ts +72 -0
- package/src/plan-bash-skill-gate.ts +21 -0
- package/src/plan-skill-gate.ts +21 -0
- package/src/run-skill-gate.ts +235 -0
- package/src/types.ts +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zgeoff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zgeoff/skill-gate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The skill gate is a Claude Code `PreToolUse` hook that denies an edit until the session has loaded
|
|
4
|
+
every skill the path needs, and names the missing skills. It also warns when a Bash command writes a
|
|
5
|
+
gated path through a redirect, `tee`, `sed -i`, `cp`, `mv` or an inline script. The warning names
|
|
6
|
+
the skills to load and the edit tools, and the command still runs. The package holds no rules: each
|
|
7
|
+
repo keeps its own in `.claude/skill-gate.json`, and a repo without that file has no gate.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add -d @zgeoff/skill-gate
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Install it at the repo root, so that Bun resolves it from `.claude/hooks/`. The package runs on Bun.
|
|
16
|
+
|
|
17
|
+
## Entry point
|
|
18
|
+
|
|
19
|
+
The hook runs a file in the repo that calls the package, at
|
|
20
|
+
`.claude/hooks/skill-gate/skill-gate.ts`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { runSkillGate } from '@zgeoff/skill-gate';
|
|
24
|
+
|
|
25
|
+
await runSkillGate();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`runSkillGate` reads the hook payload from stdin, prints the hook output, and exits the process.
|
|
29
|
+
Register the entry point in `.claude/settings.json`:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"hooks": {
|
|
34
|
+
"PreToolUse": [
|
|
35
|
+
{
|
|
36
|
+
"matcher": "Edit|Write|MultiEdit|Bash",
|
|
37
|
+
"hooks": [
|
|
38
|
+
{
|
|
39
|
+
"type": "command",
|
|
40
|
+
"command": "bun \"$CLAUDE_PROJECT_DIR/.claude/hooks/skill-gate/skill-gate.ts\""
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Rules
|
|
50
|
+
|
|
51
|
+
`.claude/skill-gate.json` maps path globs to the skills each path needs. `match` is a glob over the
|
|
52
|
+
path from the repo root, and a path that holds an `ignore` segment is never gated. `Bun.Glob` misses
|
|
53
|
+
deep paths for a `**` inside a brace group, such as `{e2e/**,test/**}`, so write one gate per
|
|
54
|
+
pattern.
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"ignore": ["node_modules/", "dist/"],
|
|
59
|
+
"gates": [
|
|
60
|
+
{ "match": "**/*.test.ts", "skills": ["testing", "project-testing"] },
|
|
61
|
+
{ "match": "**/*.md", "skills": ["docs-writing"] }
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The gate skips a skill that a rule names but `.claude/skills/` lacks, and it still requires the
|
|
67
|
+
rule's other skills. A rules file that does not parse denies no edit. In both cases the hook warns
|
|
68
|
+
the user and the agent on every edit until the rules file is fixed.
|
|
69
|
+
|
|
70
|
+
## Which loads count
|
|
71
|
+
|
|
72
|
+
The gate counts the loads of the session that makes the edit. A subagent's tool call carries
|
|
73
|
+
`agent_id` beside the main session's `transcript_path`, and the gate reads that subagent's own
|
|
74
|
+
transcript, `<session>/subagents/agent-<agent_id>.jsonl` beside the main one. A subagent therefore
|
|
75
|
+
loads the skills itself, and the main session's loads never count for it. A compaction writes a
|
|
76
|
+
`compact_boundary` entry, and the loads before it no longer count: after a compaction, the gate
|
|
77
|
+
denies a gated edit until the session loads the skills again, and its denial says so.
|
|
78
|
+
|
|
79
|
+
## Bash commands
|
|
80
|
+
|
|
81
|
+
The gate blocks only Edit, Write and MultiEdit. For Bash it reads the common forms of a shell write
|
|
82
|
+
and warns, because a hook cannot parse every shell form: a deny built on a partial parser would
|
|
83
|
+
block ordinary commands or let other writes through. A command the parser misses runs with no
|
|
84
|
+
warning. Change a gated path only with Edit, Write or MultiEdit; the denial and the warning both say
|
|
85
|
+
so.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatSkillList(skills: readonly string[]): string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { runSkillGate } from './run-skill-gate.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runSkillGate(): Promise<never>;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface SkillGate {
|
|
2
|
+
readonly match: string;
|
|
3
|
+
readonly skills: readonly string[];
|
|
4
|
+
}
|
|
5
|
+
export interface SkillGateRules {
|
|
6
|
+
readonly ignore: readonly string[];
|
|
7
|
+
readonly gates: readonly SkillGate[];
|
|
8
|
+
}
|
|
9
|
+
export type SkillGateRulesResult = {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly rules: SkillGateRules;
|
|
12
|
+
} | {
|
|
13
|
+
readonly ok: false;
|
|
14
|
+
readonly error: string;
|
|
15
|
+
};
|
|
16
|
+
export interface UnknownSkill {
|
|
17
|
+
readonly match: string;
|
|
18
|
+
readonly skill: string;
|
|
19
|
+
}
|
|
20
|
+
export interface LoadedSkills {
|
|
21
|
+
readonly skills: readonly string[];
|
|
22
|
+
readonly compacted: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface SkillGateInput {
|
|
25
|
+
readonly rules: SkillGateRules;
|
|
26
|
+
readonly relativePath: string;
|
|
27
|
+
readonly loadedSkills: readonly string[];
|
|
28
|
+
readonly availableSkills: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
export interface ProjectPath {
|
|
31
|
+
readonly path: string;
|
|
32
|
+
readonly relativePath: string;
|
|
33
|
+
}
|
|
34
|
+
export interface BashSkillGateInput {
|
|
35
|
+
readonly rules: SkillGateRules;
|
|
36
|
+
readonly paths: readonly ProjectPath[];
|
|
37
|
+
readonly loadedSkills: readonly string[];
|
|
38
|
+
readonly availableSkills: readonly string[];
|
|
39
|
+
}
|
|
40
|
+
export interface BashSkillGatePlan {
|
|
41
|
+
readonly gatedPaths: readonly string[];
|
|
42
|
+
readonly missing: readonly string[];
|
|
43
|
+
}
|
|
44
|
+
export interface SkillGatePlan {
|
|
45
|
+
readonly missing: readonly string[];
|
|
46
|
+
readonly unknown: readonly UnknownSkill[];
|
|
47
|
+
}
|
|
48
|
+
export interface ShellWord {
|
|
49
|
+
readonly text: string;
|
|
50
|
+
readonly literal: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface ShellRedirect {
|
|
53
|
+
readonly operator: string;
|
|
54
|
+
readonly target: ShellWord;
|
|
55
|
+
}
|
|
56
|
+
export interface ShellCommand {
|
|
57
|
+
readonly words: readonly ShellWord[];
|
|
58
|
+
readonly redirects: readonly ShellRedirect[];
|
|
59
|
+
readonly stdin: string | null;
|
|
60
|
+
readonly enters: number;
|
|
61
|
+
readonly leaves: number;
|
|
62
|
+
}
|
|
63
|
+
export interface WrittenPath {
|
|
64
|
+
readonly path: string;
|
|
65
|
+
readonly fileName?: string;
|
|
66
|
+
}
|
|
67
|
+
export interface SkillGateOutputContext {
|
|
68
|
+
readonly compacted?: boolean;
|
|
69
|
+
readonly rulesError?: string;
|
|
70
|
+
}
|
|
71
|
+
export interface SkillGateHookOutput {
|
|
72
|
+
readonly systemMessage?: string;
|
|
73
|
+
readonly hookSpecificOutput: {
|
|
74
|
+
readonly hookEventName: 'PreToolUse';
|
|
75
|
+
readonly permissionDecision?: 'deny';
|
|
76
|
+
readonly permissionDecisionReason?: string;
|
|
77
|
+
readonly additionalContext?: string;
|
|
78
|
+
};
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/skill-gate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A Claude Code PreToolUse hook that denies an edit until the session loads the skills the path needs, and warns on a Bash command that writes a gated path",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"claude-code",
|
|
7
|
+
"hook",
|
|
8
|
+
"skills"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/zgeoff/tools.git",
|
|
14
|
+
"directory": "packages/skill-gate"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"!src/*.test.ts",
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./src/index.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "rm -rf dist && bun run build:outdir dist",
|
|
33
|
+
"build:outdir": "tsc -p tsconfig.build.json --outDir",
|
|
34
|
+
"prepare": "bun run build",
|
|
35
|
+
"typecheck": "tsc"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@zgeoff/std": "0.1.1",
|
|
39
|
+
"ts-pattern": "5.9.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/bun": "1.4.2",
|
|
43
|
+
"tiny-invariant": "1.3.3",
|
|
44
|
+
"typescript": "7.0.2"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"bun": ">=1.3"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { formatSkillList } from './format-skill-list.ts';
|
|
2
|
+
import type { SkillGateHookOutput } from './types.ts';
|
|
3
|
+
|
|
4
|
+
const RULES_PATH = '.claude/skill-gate.json';
|
|
5
|
+
|
|
6
|
+
// Builds the PreToolUse hook output for a Bash command, or null when it writes no gated path. The
|
|
7
|
+
// command still runs: the hook only reminds the agent, because a shell parser misses some writes.
|
|
8
|
+
export function buildBashSkillGateOutput(
|
|
9
|
+
gatedPaths: readonly string[],
|
|
10
|
+
missing: readonly string[],
|
|
11
|
+
context: { readonly compacted?: boolean } = {},
|
|
12
|
+
): SkillGateHookOutput | null {
|
|
13
|
+
if (gatedPaths.length === 0) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const subject =
|
|
18
|
+
gatedPaths.length === 1
|
|
19
|
+
? `${gatedPaths[0]} is a gated path, so change it`
|
|
20
|
+
: `${gatedPaths.join(', ')} are gated paths, so change them`;
|
|
21
|
+
|
|
22
|
+
const load = buildLoadInstruction(missing, context.compacted === true);
|
|
23
|
+
const rule = `${subject} with the Edit, Write or MultiEdit tool, never through Bash.`;
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
hookSpecificOutput: {
|
|
27
|
+
hookEventName: 'PreToolUse',
|
|
28
|
+
additionalContext: `skill-gate: this Bash command writes a gated path and runs anyway.${load} ${rule} ${RULES_PATH} lists the skills each path needs.`,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function buildLoadInstruction(missing: readonly string[], compacted: boolean): string {
|
|
34
|
+
if (missing.length === 0) {
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return compacted
|
|
39
|
+
? ` Load the ${formatSkillList(missing)} again with the Skill tool before the next change. This session was compacted, and a skill loaded before the compaction no longer counts.`
|
|
40
|
+
: ` Load the ${formatSkillList(missing)} with the Skill tool before the next change.`;
|
|
41
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { formatSkillList } from './format-skill-list.ts';
|
|
2
|
+
import type { SkillGateHookOutput, SkillGateOutputContext, SkillGatePlan } from './types.ts';
|
|
3
|
+
|
|
4
|
+
// Builds the PreToolUse hook output for one edit, or null when the edit goes ahead in silence. A
|
|
5
|
+
// missing skill denies the edit. A broken rules file or a rule that names a skill the repo does
|
|
6
|
+
// not have never denies: it warns the user and the agent, and the edit goes ahead.
|
|
7
|
+
export function buildSkillGateOutput(
|
|
8
|
+
filePath: string,
|
|
9
|
+
plan: SkillGatePlan,
|
|
10
|
+
context: SkillGateOutputContext = {},
|
|
11
|
+
): SkillGateHookOutput | null {
|
|
12
|
+
const warnings = [
|
|
13
|
+
...(context.rulesError === undefined
|
|
14
|
+
? []
|
|
15
|
+
: [`skill-gate: ${RULES_PATH} ${context.rulesError}.`]),
|
|
16
|
+
...plan.unknown.map(
|
|
17
|
+
(entry) =>
|
|
18
|
+
`skill-gate: the rule "${entry.match}" in ${RULES_PATH} names the skill "${entry.skill}", but .claude/skills/${entry.skill}/SKILL.md does not exist. Fix the rule or add the skill; until then the gate skips that skill.`,
|
|
19
|
+
),
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const warning = warnings.length === 0 ? undefined : warnings.join('\n');
|
|
23
|
+
|
|
24
|
+
if (plan.missing.length > 0) {
|
|
25
|
+
const reason =
|
|
26
|
+
context.compacted === true
|
|
27
|
+
? `Load the ${formatSkillList(plan.missing)} again with the Skill tool before editing ${filePath}, then retry the edit. This session was compacted, and a skill loaded before the compaction no longer counts. ${EDIT_TOOLS_NOTE} ${RULES_PATH} lists the skills each path needs.`
|
|
28
|
+
: `Load the ${formatSkillList(plan.missing)} with the Skill tool before editing ${filePath}, then retry the edit. ${EDIT_TOOLS_NOTE} ${RULES_PATH} lists the skills each path needs.`;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
...(warning === undefined ? {} : { systemMessage: warning }),
|
|
32
|
+
hookSpecificOutput: {
|
|
33
|
+
hookEventName: 'PreToolUse',
|
|
34
|
+
permissionDecision: 'deny',
|
|
35
|
+
permissionDecisionReason: warning === undefined ? reason : `${reason}\n${warning}`,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (warning === undefined) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
systemMessage: warning,
|
|
46
|
+
hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: warning },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const RULES_PATH = '.claude/skill-gate.json';
|
|
51
|
+
|
|
52
|
+
// A gated path changes only through these tools; the gate warns on a Bash write and blocks an edit.
|
|
53
|
+
const EDIT_TOOLS_NOTE =
|
|
54
|
+
'Change a gated path only with Edit, Write or MultiEdit, never through Bash.';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SkillGateRules } from './types.ts';
|
|
2
|
+
|
|
3
|
+
// Collects the skills that every gate matching a path names, in gate order and with repeats. A
|
|
4
|
+
// path that holds an ignore segment matches no gate.
|
|
5
|
+
export function collectGateSkills(rules: SkillGateRules, relativePath: string): readonly string[] {
|
|
6
|
+
if (rules.ignore.some((segment) => relativePath.includes(segment))) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return rules.gates
|
|
11
|
+
.filter((gate) => isGlobMatch(gate.match, relativePath))
|
|
12
|
+
.flatMap((gate) => gate.skills);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isGlobMatch(pattern: string, relativePath: string): boolean {
|
|
16
|
+
const glob = new Bun.Glob(pattern);
|
|
17
|
+
|
|
18
|
+
return glob.match(relativePath);
|
|
19
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { isRecord } from '@zgeoff/std';
|
|
2
|
+
import type { LoadedSkills } from './types.ts';
|
|
3
|
+
|
|
4
|
+
// Collects the skills a transcript loaded since its last compaction, which voids earlier loads even
|
|
5
|
+
// though Claude Code re-attaches their text. A skill loads through a Skill tool call that succeeded,
|
|
6
|
+
// or a slash command the user types; a subagent's transcript has no user to type one.
|
|
7
|
+
export function collectLoadedSkills(transcript: string): LoadedSkills {
|
|
8
|
+
const entries = transcript.split('\n').map((line) => parseEntry(line));
|
|
9
|
+
const lastBoundary = entries.findLastIndex((entry) => isCompactBoundary(entry));
|
|
10
|
+
const current = entries.slice(lastBoundary + 1);
|
|
11
|
+
const succeeded = collectSucceededToolUseIDs(current);
|
|
12
|
+
const skills = current.flatMap((entry) => collectEntrySkills(entry, succeeded));
|
|
13
|
+
|
|
14
|
+
return { skills: [...new Set(skills)], compacted: lastBoundary !== -1 };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function parseEntry(line: string): unknown {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(line);
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isCompactBoundary(entry: unknown): boolean {
|
|
26
|
+
return isRecord(entry) && entry['type'] === 'system' && entry['subtype'] === 'compact_boundary';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function collectSucceededToolUseIDs(entries: readonly unknown[]): readonly string[] {
|
|
30
|
+
return entries
|
|
31
|
+
.flatMap((entry) => collectContentBlocks(entry, 'user'))
|
|
32
|
+
.filter((block) => block['type'] === 'tool_result' && block['is_error'] !== true)
|
|
33
|
+
.flatMap((block) => (typeof block['tool_use_id'] === 'string' ? [block['tool_use_id']] : []));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function collectContentBlocks(
|
|
37
|
+
entry: unknown,
|
|
38
|
+
type: string,
|
|
39
|
+
): readonly Readonly<Record<string, unknown>>[] {
|
|
40
|
+
if (!isRecord(entry) || entry['type'] !== type || !isRecord(entry['message'])) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const content = entry['message']['content'];
|
|
45
|
+
|
|
46
|
+
return Array.isArray(content) ? content.filter((block: unknown) => isRecord(block)) : [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function collectEntrySkills(entry: unknown, succeeded: readonly string[]): readonly string[] {
|
|
50
|
+
const command = findSlashCommand(entry);
|
|
51
|
+
|
|
52
|
+
if (command !== undefined) {
|
|
53
|
+
return [command];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return collectContentBlocks(entry, 'assistant').flatMap((block) =>
|
|
57
|
+
collectSkillCall(block, succeeded),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function findSlashCommand(entry: unknown): string | undefined {
|
|
62
|
+
if (
|
|
63
|
+
!isRecord(entry) ||
|
|
64
|
+
entry['type'] !== 'user' ||
|
|
65
|
+
entry['isSidechain'] === true ||
|
|
66
|
+
!isRecord(entry['message'])
|
|
67
|
+
) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const content = entry['message']['content'];
|
|
72
|
+
|
|
73
|
+
return typeof content === 'string'
|
|
74
|
+
? SLASH_COMMAND_PATTERN.exec(content)?.groups?.['skill']
|
|
75
|
+
: undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// A slash command opens the user message with its tags, in either order.
|
|
79
|
+
const SLASH_COMMAND_PATTERN =
|
|
80
|
+
/^\s*(?:<command-message>[^<]*<\/command-message>\s*)?<command-name>\/(?<skill>[^<\s]+)<\/command-name>/u;
|
|
81
|
+
|
|
82
|
+
function collectSkillCall(
|
|
83
|
+
block: Readonly<Record<string, unknown>>,
|
|
84
|
+
succeeded: readonly string[],
|
|
85
|
+
): readonly string[] {
|
|
86
|
+
const input = block['input'];
|
|
87
|
+
const id = block['id'];
|
|
88
|
+
|
|
89
|
+
if (block['type'] !== 'tool_use' || block['name'] !== 'Skill' || !isRecord(input)) {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const skill = input['skill'];
|
|
94
|
+
|
|
95
|
+
return typeof skill === 'string' && typeof id === 'string' && succeeded.includes(id)
|
|
96
|
+
? [skill]
|
|
97
|
+
: [];
|
|
98
|
+
}
|