-
Notifications
You must be signed in to change notification settings - Fork 0
feat(compat): model-invocable Skill tool — progressive disclosure via index block + on-demand body (#120) #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import type { SkillToolInput, SkillToolOutput } from './skill-tool.ts'; | ||
| import { SKILL_INVOCATION_CONTRACT, skillIndexBlock, skillTool } from './skill-tool.ts'; | ||
| import type { SkillDefinition } from './skills-loader.ts'; | ||
|
|
||
| function skill(partial: Partial<SkillDefinition> & { name: string }): SkillDefinition { | ||
| return { | ||
| name: partial.name, | ||
| description: partial.description ?? `desc for ${partial.name}`, | ||
| body: partial.body ?? `body for ${partial.name}`, | ||
| path: partial.path ?? `/repo/.claude/skills/${partial.name}/SKILL.md`, | ||
| extra: partial.extra ?? {}, | ||
| }; | ||
| } | ||
|
|
||
| function runExec(t: { execute?: unknown }, input: SkillToolInput): SkillToolOutput { | ||
| const exec = t.execute; | ||
| if (typeof exec !== 'function') throw new Error('tool has no execute'); | ||
| return ( | ||
| exec as (i: SkillToolInput, o: { toolCallId: string; messages: never[] }) => SkillToolOutput | ||
| )(input, { toolCallId: 'c', messages: [] }); | ||
| } | ||
|
|
||
| function textOf( | ||
| t: { toModelOutput?: unknown }, | ||
| input: SkillToolInput, | ||
| output: SkillToolOutput, | ||
| ): string { | ||
| const fn = t.toModelOutput; | ||
| if (typeof fn !== 'function') throw new Error('tool has no toModelOutput'); | ||
| const part = ( | ||
| fn as (o: { toolCallId: string; input: SkillToolInput; output: SkillToolOutput }) => { | ||
| type: string; | ||
| value: string; | ||
| } | ||
| )({ toolCallId: 'c', input, output }); | ||
| assert.equal(part.type, 'text'); | ||
| return part.value; | ||
| } | ||
|
|
||
| test('skillIndexBlock: contract preamble + one line per invocable skill; disabled excluded (issue #120)', () => { | ||
| const block = skillIndexBlock([ | ||
| skill({ name: 'repo-recon', description: 'map a repo before editing' }), | ||
| skill({ | ||
| name: 'secret', | ||
| description: 'hidden', | ||
| extra: { 'disable-model-invocation': 'true' }, | ||
| }), | ||
| ]); | ||
| assert.ok(block.startsWith(SKILL_INVOCATION_CONTRACT), 'the invocation contract is the preamble'); | ||
| assert.match(block, /<skills>\nrepo-recon: map a repo before editing\n<\/skills>/); | ||
| assert.equal(block.includes('secret'), false, 'disable-model-invocation excludes from the index'); | ||
| }); | ||
|
|
||
| test('skillIndexBlock: collapses a multi-line description to a single index line (issue #120)', () => { | ||
| const block = skillIndexBlock([skill({ name: 'x', description: 'line one\nline two' })]); | ||
| assert.match(block, /x: line one line two/); | ||
| assert.equal( | ||
| block.split('\n').filter((l) => l.startsWith('x:')).length, | ||
| 1, | ||
| 'exactly one line for the skill', | ||
| ); | ||
| }); | ||
|
|
||
| test('skillIndexBlock: empty or all-disabled input renders nothing (issue #120)', () => { | ||
| assert.equal(skillIndexBlock([]), ''); | ||
| assert.equal( | ||
| skillIndexBlock([skill({ name: 'z', extra: { 'disable-model-invocation': 'true' } })]), | ||
| '', | ||
| ); | ||
| }); | ||
|
|
||
| test('skillTool: a valid name returns the skill body and path, rendered as plain text (issue #120)', () => { | ||
| const t = skillTool([ | ||
| skill({ | ||
| name: 'triage', | ||
| body: 'read logs bottom-up', | ||
| path: '/r/.claude/skills/triage/SKILL.md', | ||
| }), | ||
| ]); | ||
| const out = runExec(t, { skill: 'triage' }); | ||
| assert.equal(out.ok, true); | ||
| if (!out.ok) return; | ||
| assert.equal(out.body, 'read logs bottom-up'); | ||
| assert.equal(out.path, '/r/.claude/skills/triage/SKILL.md'); | ||
| const text = textOf(t, { skill: 'triage' }, out); | ||
| assert.match(text, /^Skill: triage/); | ||
| assert.match(text, /read logs bottom-up/); | ||
| assert.ok(text.includes('/r/.claude/skills/triage/SKILL.md'), 'result carries the path'); | ||
| assert.match(text, /read them on demand with the Read tool/); | ||
| }); | ||
|
|
||
| test('skillTool: an unknown name returns an error naming the available skills — never throws (issue #120)', () => { | ||
| const t = skillTool([skill({ name: 'a' }), skill({ name: 'b' })]); | ||
| const out = runExec(t, { skill: 'nope' }); | ||
| assert.equal(out.ok, false); | ||
| if (out.ok) return; | ||
| assert.match(out.error, /Unknown skill "nope"/); | ||
| assert.match(out.error, /Available skills: a, b/); | ||
| assert.equal( | ||
| textOf(t, { skill: 'nope' }, out), | ||
| out.error, | ||
| 'the error text is what the model sees', | ||
| ); | ||
| }); | ||
|
|
||
| test('skillTool: a disabled skill is not in the matchable set (issue #120)', () => { | ||
| const t = skillTool([skill({ name: 'secret', extra: { 'disable-model-invocation': 'true' } })]); | ||
| const out = runExec(t, { skill: 'secret' }); | ||
| assert.equal(out.ok, false); | ||
| if (out.ok) return; | ||
| assert.match(out.error, /Available skills: \(none available\)/); | ||
| }); | ||
|
|
||
| test('skillTool: args are echoed into the result and its rendering (issue #120)', () => { | ||
| const t = skillTool([skill({ name: 'triage', body: 'B' })]); | ||
| const input = { skill: 'triage', args: 'job=e2e' }; | ||
| const out = runExec(t, input); | ||
| assert.equal(out.ok, true); | ||
| if (!out.ok) return; | ||
| assert.equal(out.args, 'job=e2e'); | ||
| assert.match(textOf(t, input, out), /Arguments: job=e2e/); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Model-facing surface for skills (issue #120). Two builders turn discovered `SkillDefinition`s | ||
| // (skills-loader.ts) into progressive disclosure: | ||
| // | ||
| // - `skillIndexBlock` renders the always-visible tier — one `<name>: <description>` line per skill | ||
| // plus the invocation contract — for the system prompt. Bodies never appear here. | ||
| // - `skillTool` is the AI-SDK tool a subagent calls to pull a skill's body into context on demand | ||
| // (the second tier); the body then directs on-demand reads of sibling `references/*` (the third). | ||
| // | ||
| // Both exclude skills whose `disable-model-invocation` frontmatter is `true`. Neither mounts anything | ||
| // itself — the caller passes the `skills` array, deciding what a given role may see (target-repo | ||
| // skills are untrusted and Worker-only; that wiring is a separate aitm-side issue). | ||
|
|
||
| import { type Tool, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { asString } from './frontmatter.ts'; | ||
| import type { SkillDefinition } from './skills-loader.ts'; | ||
|
|
||
| // Rendered as the index block's preamble: the blocking contract the model must follow. | ||
| export const SKILL_INVOCATION_CONTRACT = | ||
| "Skills extend you with task-specific procedures. Each line below is `<name>: <description>`. When a skill's description matches the task at hand, you MUST call the `Skill` tool with that exact name and follow the instructions it returns before producing your answer — this is blocking, not optional. Never claim to have applied a skill without calling the tool, and only invoke names that appear in this list."; | ||
|
|
||
| const skillInputSchema = z.object({ | ||
| skill: z.string().min(1).describe('The exact name of a skill from the skills index.'), | ||
| args: z | ||
| .string() | ||
| .optional() | ||
| .describe('Free-form input for the skill; echoed back in the result for its instructions.'), | ||
| }); | ||
| export type SkillToolInput = z.infer<typeof skillInputSchema>; | ||
| export type SkillToolOutput = | ||
| | { ok: true; name: string; path: string; body: string; args?: string } | ||
| | { ok: false; error: string }; | ||
|
|
||
| // A skill is model-invocable unless it opts out with `disable-model-invocation: true`. | ||
| function isModelInvocable(skill: SkillDefinition): boolean { | ||
| return asString(skill.extra['disable-model-invocation']).trim() !== 'true'; | ||
| } | ||
|
|
||
| const oneLine = (s: string): string => s.replace(/[\r\n]+/g, ' ').trim(); | ||
|
|
||
| // The always-visible index tier: the invocation contract plus one line per model-invocable skill. | ||
| // Returns '' when nothing is invocable, so a caller can drop the block entirely. | ||
| export function skillIndexBlock(skills: readonly SkillDefinition[]): string { | ||
| const invocable = skills.filter(isModelInvocable); | ||
| if (invocable.length === 0) return ''; | ||
| const lines = invocable.map((s) => `${s.name}: ${oneLine(s.description)}`); | ||
| return [SKILL_INVOCATION_CONTRACT, '<skills>', ...lines, '</skills>'].join('\n'); | ||
| } | ||
|
|
||
| // The `Skill` tool: exact-name lookup over the model-invocable set. A hit returns the skill's body | ||
| // and absolute path; a miss returns an error result naming the available skills (never throws). | ||
| export function skillTool( | ||
| skills: readonly SkillDefinition[], | ||
| ): Tool<SkillToolInput, SkillToolOutput> { | ||
| const invocable = skills.filter(isModelInvocable); | ||
| const byName = new Map(invocable.map((s) => [s.name, s])); | ||
| const available = invocable.map((s) => s.name); | ||
| return tool({ | ||
| description: | ||
| 'Load a skill by name to pull its full instructions into context, then follow them before answering. Only names present in the skills index are valid. `args` is optional free-form text passed through to the skill.', | ||
| inputSchema: skillInputSchema, | ||
| execute: (input: SkillToolInput): SkillToolOutput => { | ||
| const skill = byName.get(input.skill); | ||
| if (skill === undefined) { | ||
| const names = available.length > 0 ? available.join(', ') : '(none available)'; | ||
| return { ok: false, error: `Unknown skill "${input.skill}". Available skills: ${names}.` }; | ||
| } | ||
| return { | ||
| ok: true, | ||
| name: skill.name, | ||
| path: skill.path, | ||
| body: skill.body, | ||
| ...(input.args !== undefined ? { args: input.args } : {}), | ||
| }; | ||
| }, | ||
| toModelOutput: ({ output }) => ({ type: 'text', value: renderSkillResult(output) }), | ||
| }); | ||
| } | ||
|
|
||
| // Plain-text rendering so the body reads as instructions, not a JSON-escaped blob (issue #127). | ||
| function renderSkillResult(output: SkillToolOutput): string { | ||
| if (!output.ok) return output.error; | ||
| const header = | ||
| output.args !== undefined && output.args !== '' | ||
| ? `Skill: ${output.name}\nArguments: ${output.args}` | ||
| : `Skill: ${output.name}`; | ||
| return [ | ||
| header, | ||
| '', | ||
| output.body, | ||
| '', | ||
| `Files this skill references (e.g. references/*.md) live next to ${output.path}; read them on demand with the Read tool.`, | ||
| ].join('\n'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.