@unieai/rabi-tool-skill 0.2.1-rc.3 → 0.2.2-rc.5
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/lib/index.js +28 -20
- package/lib/types/index.d.ts +2 -0
- package/locale/de.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/package.json +11 -8
- package/src/index.ts +451 -0
package/lib/index.js
CHANGED
|
@@ -708,6 +708,7 @@ new Set([
|
|
|
708
708
|
"team/member",
|
|
709
709
|
"team/message/delivered",
|
|
710
710
|
"team/message/queued",
|
|
711
|
+
"team/mode",
|
|
711
712
|
"team/task",
|
|
712
713
|
"todo/write",
|
|
713
714
|
"tool-workflow/agent-end",
|
|
@@ -941,41 +942,42 @@ function apply(ctx, config = {}) {
|
|
|
941
942
|
complete: true
|
|
942
943
|
};
|
|
943
944
|
signal.throwIfAborted();
|
|
944
|
-
if (!snapshot.complete) return decision;
|
|
945
945
|
const skills = snapshot.skills.filter(isModelInvocable);
|
|
946
946
|
const entries = catalogSourceEntries(skills, catalogDescriptionMaxLength);
|
|
947
|
-
const
|
|
947
|
+
const incomplete = !snapshot.complete;
|
|
948
|
+
const digest = digestCatalogEntries(entries, incomplete);
|
|
948
949
|
const history = catalogHistory(agent);
|
|
949
950
|
const existing = catalogMessage(decision.messages);
|
|
950
951
|
if (history.visibleDigest === digest) return existing === void 0 ? decision : {
|
|
951
952
|
...decision,
|
|
952
953
|
messages: decision.messages.filter((message) => message.id !== existing.message.id)
|
|
953
954
|
};
|
|
954
|
-
if (existing !== void 0 && digestCatalogEntries(existing.entries) === digest) return decision;
|
|
955
|
-
if (!history.published && skills.length === 0) return existing === void 0 ? decision : {
|
|
955
|
+
if (existing !== void 0 && digestCatalogEntries(existing.entries, existing.incomplete) === digest) return decision;
|
|
956
|
+
if (!history.published && skills.length === 0 && !incomplete) return existing === void 0 ? decision : {
|
|
956
957
|
...decision,
|
|
957
958
|
messages: decision.messages.filter((message) => message.id !== existing.message.id)
|
|
958
959
|
};
|
|
959
|
-
const catalog = history.published ? renderCatalogUpdate(entries) : renderCatalogMessage(entries);
|
|
960
|
+
const catalog = history.published ? renderCatalogUpdate(entries, incomplete) : renderCatalogMessage(entries, incomplete);
|
|
960
961
|
return {
|
|
961
962
|
...decision,
|
|
962
963
|
messages: existing === void 0 ? [...decision.messages, catalog] : decision.messages.map((message) => message.id === existing.message.id ? catalog : message)
|
|
963
964
|
};
|
|
964
965
|
});
|
|
965
966
|
}
|
|
966
|
-
function renderCatalogMessage(entries) {
|
|
967
|
+
function renderCatalogMessage(entries, incomplete) {
|
|
967
968
|
return createUserMessage({
|
|
968
969
|
content: [{
|
|
969
970
|
type: "text",
|
|
970
971
|
text: [
|
|
971
972
|
"<system-reminder>",
|
|
972
|
-
"A skill is a reusable set of task-specific instructions. The following skills are available in this session:",
|
|
973
|
+
entries.length === 0 ? "No skills could be confirmed in this discovery attempt." : "A skill is a reusable set of task-specific instructions. The following skills are available in this session:",
|
|
973
974
|
"",
|
|
974
975
|
"<available_skills>",
|
|
975
976
|
...renderCatalogEntries(entries),
|
|
976
977
|
"</available_skills>",
|
|
977
978
|
"",
|
|
978
|
-
|
|
979
|
+
...incomplete ? [incompleteCatalogNotice(), ""] : [],
|
|
980
|
+
entries.length === 0 ? "Wait for an updated catalog before calling the `skill` tool. Do not infer skill instructions from an empty catalog." : "If the user names a skill, or the task clearly matches a skill's description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill's instructions until it has been loaded.",
|
|
979
981
|
"A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill.",
|
|
980
982
|
"</system-reminder>"
|
|
981
983
|
].join("\n")
|
|
@@ -983,23 +985,25 @@ function renderCatalogMessage(entries) {
|
|
|
983
985
|
source: {
|
|
984
986
|
kind: "skill-catalog",
|
|
985
987
|
form: "catalog",
|
|
986
|
-
entries
|
|
988
|
+
entries,
|
|
989
|
+
...incomplete ? { incomplete: true } : {}
|
|
987
990
|
}
|
|
988
991
|
});
|
|
989
992
|
}
|
|
990
|
-
function renderCatalogUpdate(entries) {
|
|
991
|
-
const availability = entries.length === 0 ? ["No skills are currently available through the `skill` tool. Do not use names from earlier skill catalogs.", "A user may still invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool for it."] : ["Use only names in this replacement catalog. If the user names a listed skill, or the task clearly matches its description, call the `skill` tool with the exact name before acting.", "A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill."];
|
|
993
|
+
function renderCatalogUpdate(entries, incomplete) {
|
|
994
|
+
const availability = entries.length === 0 ? [incomplete ? "No skills could be confirmed in this discovery attempt. Do not use names from earlier skill catalogs." : "No skills are currently available through the `skill` tool. Do not use names from earlier skill catalogs.", "A user may still invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool for it."] : ["Use only names in this replacement catalog. If the user names a listed skill, or the task clearly matches its description, call the `skill` tool with the exact name before acting.", "A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill."];
|
|
992
995
|
return createUserMessage({
|
|
993
996
|
content: [{
|
|
994
997
|
type: "text",
|
|
995
998
|
text: [
|
|
996
999
|
"<system-reminder>",
|
|
997
|
-
"The available skill catalog changed. This complete catalog replaces every earlier available-skills list in this session:",
|
|
1000
|
+
incomplete ? "Skill discovery is incomplete. This catalog of currently available skills replaces every earlier available-skills list in this session:" : "The available skill catalog changed. This complete catalog replaces every earlier available-skills list in this session:",
|
|
998
1001
|
"",
|
|
999
1002
|
"<available_skills>",
|
|
1000
1003
|
...renderCatalogEntries(entries),
|
|
1001
1004
|
"</available_skills>",
|
|
1002
1005
|
"",
|
|
1006
|
+
...incomplete ? [incompleteCatalogNotice(), ""] : [],
|
|
1003
1007
|
...availability,
|
|
1004
1008
|
"</system-reminder>"
|
|
1005
1009
|
].join("\n")
|
|
@@ -1008,10 +1012,14 @@ function renderCatalogUpdate(entries) {
|
|
|
1008
1012
|
kind: "skill-catalog",
|
|
1009
1013
|
form: "catalog",
|
|
1010
1014
|
update: true,
|
|
1011
|
-
entries
|
|
1015
|
+
entries,
|
|
1016
|
+
...incomplete ? { incomplete: true } : {}
|
|
1012
1017
|
}
|
|
1013
1018
|
});
|
|
1014
1019
|
}
|
|
1020
|
+
function incompleteCatalogNotice() {
|
|
1021
|
+
return "Some skills could not be checked. Only confirmed skills are listed; a lower-priority definition of the same name may be selected when a preferred source is unavailable. Discovery will be retried on the next request.";
|
|
1022
|
+
}
|
|
1015
1023
|
/**
|
|
1016
1024
|
* Model-facing catalog lines, projected from the same entries the source records.
|
|
1017
1025
|
* The pseudo-XML escaping belongs to this frame, not to the published fact, so it
|
|
@@ -1022,13 +1030,12 @@ function renderCatalogEntries(entries) {
|
|
|
1022
1030
|
return entries.map((entry) => `- \`${entry.name}\`: ${escapeText(entry.description)}`);
|
|
1023
1031
|
}
|
|
1024
1032
|
/**
|
|
1025
|
-
* Catalog identity
|
|
1026
|
-
*
|
|
1027
|
-
* written for the model and must not decide whether a republish is needed.
|
|
1033
|
+
* Catalog identity includes durable entries and completeness so recovery clears
|
|
1034
|
+
* a discovery warning even when the entries are unchanged.
|
|
1028
1035
|
*/
|
|
1029
|
-
function digestCatalogEntries(entries) {
|
|
1036
|
+
function digestCatalogEntries(entries, incomplete = false) {
|
|
1030
1037
|
const canonical = entries.map((entry) => JSON.stringify([entry.name, entry.description])).join("\n");
|
|
1031
|
-
return createHash("sha256").update(canonical).digest("hex");
|
|
1038
|
+
return createHash("sha256").update(incomplete ? "incomplete\n" : "").update(canonical).digest("hex");
|
|
1032
1039
|
}
|
|
1033
1040
|
/**
|
|
1034
1041
|
* Entries of one durable catalog message, or undefined when the record is not a
|
|
@@ -1065,7 +1072,7 @@ function catalogHistory(agent) {
|
|
|
1065
1072
|
if (event.type !== "user/message" || event.data.source.kind !== "skill-catalog") continue;
|
|
1066
1073
|
const entries = readCatalogEntries(event.data.source);
|
|
1067
1074
|
if (entries === void 0) continue;
|
|
1068
|
-
const digest = digestCatalogEntries(entries);
|
|
1075
|
+
const digest = digestCatalogEntries(entries, event.data.source.incomplete === true);
|
|
1069
1076
|
published = true;
|
|
1070
1077
|
if (visible.has(event.seq)) return {
|
|
1071
1078
|
visibleDigest: digest,
|
|
@@ -1080,7 +1087,8 @@ function catalogMessage(messages) {
|
|
|
1080
1087
|
const entries = readCatalogEntries(message.source);
|
|
1081
1088
|
if (entries !== void 0) return {
|
|
1082
1089
|
message,
|
|
1083
|
-
entries
|
|
1090
|
+
entries,
|
|
1091
|
+
incomplete: message.source.incomplete === true
|
|
1084
1092
|
};
|
|
1085
1093
|
}
|
|
1086
1094
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface SkillCatalogSource {
|
|
|
18
18
|
readonly form: 'catalog';
|
|
19
19
|
/** Marks a replacement catalog rather than this session's first publication. */
|
|
20
20
|
readonly update?: true;
|
|
21
|
+
/** Some discovery failed; entries contain only skills found in this observation. Absence means complete. */
|
|
22
|
+
readonly incomplete?: true;
|
|
21
23
|
/** Exactly the entries this message published, in catalog order. */
|
|
22
24
|
readonly entries: readonly {
|
|
23
25
|
readonly name: string;
|
package/locale/de.json
ADDED
package/locale/es.json
ADDED
package/locale/fr.json
ADDED
package/locale/ja.json
ADDED
package/locale/pt.json
ADDED
package/locale/ru.json
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-tool-skill",
|
|
3
3
|
"description": "Model-facing skill loading tool for the Rabi Harness",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2-rc.5",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -21,18 +21,21 @@
|
|
|
21
21
|
"locale/*.json",
|
|
22
22
|
"lib/index.js",
|
|
23
23
|
"lib/types/**/*.d.ts",
|
|
24
|
+
"lib/types/**/*.d.ts.map",
|
|
25
|
+
"lib/**/*.js.map",
|
|
26
|
+
"src",
|
|
24
27
|
"README.md"
|
|
25
28
|
],
|
|
26
29
|
"license": "MIT",
|
|
27
30
|
"peerDependencies": {
|
|
28
|
-
"@unieai/rabi-agent": "0.2.
|
|
29
|
-
"@unieai/rabi-llm": "0.2.
|
|
30
|
-
"@unieai/rabi-skill": "0.2.
|
|
31
|
-
"@unieai/rabi-tools": "0.2.
|
|
32
|
-
"@unieai/cordis": "4.0
|
|
33
|
-
"@unieai/rabi-working-directory": "0.2.
|
|
31
|
+
"@unieai/rabi-agent": "0.2.2-rc.5",
|
|
32
|
+
"@unieai/rabi-llm": "0.2.2-rc.5",
|
|
33
|
+
"@unieai/rabi-skill": "0.2.2-rc.5",
|
|
34
|
+
"@unieai/rabi-tools": "0.2.2-rc.5",
|
|
35
|
+
"@unieai/cordis": "4.1.0",
|
|
36
|
+
"@unieai/rabi-working-directory": "0.2.2-rc.5"
|
|
34
37
|
},
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"@unieai/schemastery": "3.
|
|
39
|
+
"@unieai/schemastery": "3.19.0"
|
|
37
40
|
}
|
|
38
41
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable session skill catalog and model-facing `skill` loader tool.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-skill
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {} from '@unieai/rabi-working-directory'
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
import type { Context } from '@unieai/cordis'
|
|
10
|
+
import z from '@unieai/schemastery'
|
|
11
|
+
import type { Agent, PreStepDecision } from '@unieai/rabi-agent'
|
|
12
|
+
import { defineTool } from '@unieai/rabi-tools'
|
|
13
|
+
import { createUserMessage } from '@unieai/rabi-llm'
|
|
14
|
+
import { SessionSeq, type UserMessage } from '@unieai/rabi-session'
|
|
15
|
+
import {
|
|
16
|
+
escapeText,
|
|
17
|
+
isModelInvocable,
|
|
18
|
+
isSkillName,
|
|
19
|
+
isUserInvocable,
|
|
20
|
+
renderSkillContent,
|
|
21
|
+
type SkillInvocationSource,
|
|
22
|
+
type SkillSummary,
|
|
23
|
+
} from '@unieai/rabi-skill'
|
|
24
|
+
|
|
25
|
+
export const name = 'tool-skill'
|
|
26
|
+
export const inject = ['agents', 'tools', 'skills', 'workingDirectory']
|
|
27
|
+
|
|
28
|
+
const DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH = 500
|
|
29
|
+
/**
|
|
30
|
+
* Durable provider and item records for one published session skill catalog. The catalog is a
|
|
31
|
+
* `catalog`-form context, so it records the entries it published beside the
|
|
32
|
+
* model-facing prose: a consumer presenting the list must not re-parse the
|
|
33
|
+
* `<available_skills>` block, whose framing exists for the model.
|
|
34
|
+
*/
|
|
35
|
+
export interface SkillCatalogSource {
|
|
36
|
+
readonly kind: 'skill-catalog'
|
|
37
|
+
readonly form: 'catalog'
|
|
38
|
+
/** Marks a replacement catalog rather than this session's first publication. */
|
|
39
|
+
readonly update?: true
|
|
40
|
+
/** Some discovery failed; entries contain only skills found in this observation. Absence means complete. */
|
|
41
|
+
readonly incomplete?: true
|
|
42
|
+
/** Exactly the entries this message published, in catalog order. */
|
|
43
|
+
readonly entries: readonly { readonly name: string; readonly description: string }[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare module '@unieai/rabi-llm' {
|
|
47
|
+
interface MessageSourceMap {
|
|
48
|
+
'skill-catalog': SkillCatalogSource
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Durable entry list mirroring the rendered catalog lines, for non-model consumers. */
|
|
53
|
+
function catalogSourceEntries(
|
|
54
|
+
skills: SkillSummary[],
|
|
55
|
+
descriptionMaxLength: number,
|
|
56
|
+
): SkillCatalogSource['entries'] {
|
|
57
|
+
return skills.map(skill => ({
|
|
58
|
+
name: skill.name,
|
|
59
|
+
description: catalogDescription(skill.description, descriptionMaxLength),
|
|
60
|
+
}))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Model-facing skill catalog configuration. */
|
|
64
|
+
export interface Config {
|
|
65
|
+
/** Maximum normalized description length rendered in the session catalog; minimum 3. */
|
|
66
|
+
catalogDescriptionMaxLength?: number
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Validate and default the model-facing skill catalog configuration. */
|
|
70
|
+
export const Config: z<Config> = z.object({
|
|
71
|
+
catalogDescriptionMaxLength: z.number().default(DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH),
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Register the model-facing skill loader and its visibility-matched
|
|
76
|
+
* durable session catalog. The catalog is emitted only when the calling agent
|
|
77
|
+
* resolves this plugin's exact tool registration; a restriction or scoped
|
|
78
|
+
* same-name shadow therefore removes both the schema and its call guidance.
|
|
79
|
+
*/
|
|
80
|
+
export function apply(ctx: Context, config: Config = {}): void {
|
|
81
|
+
const catalogDescriptionMaxLength = config.catalogDescriptionMaxLength ?? DEFAULT_CATALOG_DESCRIPTION_MAX_LENGTH
|
|
82
|
+
assertPositiveInteger('catalogDescriptionMaxLength', catalogDescriptionMaxLength, 3)
|
|
83
|
+
|
|
84
|
+
const skillTool = defineTool({
|
|
85
|
+
name: 'skill',
|
|
86
|
+
description: 'Load the full instructions for a skill. Call it before acting on a task that names or clearly matches a skill in the session skill catalog.',
|
|
87
|
+
parameters: {
|
|
88
|
+
name: { type: 'string', required: true, description: 'The exact skill name from the available skills list.' },
|
|
89
|
+
},
|
|
90
|
+
output: {
|
|
91
|
+
schema: {
|
|
92
|
+
type: 'object',
|
|
93
|
+
additionalProperties: false,
|
|
94
|
+
properties: {
|
|
95
|
+
name: { type: 'string', required: true },
|
|
96
|
+
provider: { type: 'string', required: true },
|
|
97
|
+
resourceBase: {
|
|
98
|
+
oneOf: [
|
|
99
|
+
{
|
|
100
|
+
type: 'object',
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
properties: {
|
|
103
|
+
kind: { type: 'string', required: true, const: 'directory' },
|
|
104
|
+
path: { type: 'string', required: true },
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: 'object',
|
|
109
|
+
additionalProperties: false,
|
|
110
|
+
properties: {
|
|
111
|
+
kind: { type: 'string', required: true, const: 'url' },
|
|
112
|
+
url: { type: 'string', required: true },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
type: 'object',
|
|
117
|
+
additionalProperties: false,
|
|
118
|
+
properties: {
|
|
119
|
+
kind: { type: 'string', required: true, const: 'opaque' },
|
|
120
|
+
description: { type: 'string', required: true },
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
content: { type: 'string', required: true },
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
render: (_args, value) => [{ type: 'text', text: renderSkillContent(value) }],
|
|
129
|
+
},
|
|
130
|
+
async execute(args, exec) {
|
|
131
|
+
if (!isSkillName(args.name)) {
|
|
132
|
+
throw new Error(`invalid skill name "${args.name}"`)
|
|
133
|
+
}
|
|
134
|
+
// The agent is its own scope key, so the lookup resolves the layered
|
|
135
|
+
// registry exactly as this agent's composition sees it.
|
|
136
|
+
const cwd = exec.agent === undefined ? undefined : await ctx.workingDirectory.ensure(exec.agent, exec.signal)
|
|
137
|
+
const lookup = { cwd, signal: exec.signal, scope: exec.agent }
|
|
138
|
+
const summary = (await ctx.skills.list(lookup)).find(skill => skill.name === args.name)
|
|
139
|
+
if (!summary) {
|
|
140
|
+
throw new Error(`skill "${args.name}" is unknown or no longer available`)
|
|
141
|
+
}
|
|
142
|
+
if (!isModelInvocable(summary)) {
|
|
143
|
+
throw new Error(`skill "${args.name}" is not available for model invocation`)
|
|
144
|
+
}
|
|
145
|
+
const skill = await ctx.skills.get(args.name, lookup)
|
|
146
|
+
if (!skill) {
|
|
147
|
+
throw new Error(`skill "${args.name}" is unknown or no longer available`)
|
|
148
|
+
}
|
|
149
|
+
if (!isModelInvocable(skill)) {
|
|
150
|
+
throw new Error(`skill "${args.name}" is not available for model invocation`)
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
name: skill.name,
|
|
154
|
+
provider: skill.provider,
|
|
155
|
+
...skill.resourceBase !== undefined ? {
|
|
156
|
+
resourceBase: { ...skill.resourceBase },
|
|
157
|
+
} : {},
|
|
158
|
+
content: skill.content,
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
presentCall(args) {
|
|
162
|
+
return { card: 'generic', title: `Load skill ${args.name}`, kind: 'read', rawInput: args.name }
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
ctx.tools.register(skillTool)
|
|
166
|
+
|
|
167
|
+
// User-explicit skill invocation: a claimed user message whose first line
|
|
168
|
+
// starts with `/<name>` naming a user-invocable skill is a deterministic
|
|
169
|
+
// load gesture. The rendered body enters this step as injected
|
|
170
|
+
// instructions context appended after every other injection — background
|
|
171
|
+
// first (workspace rules, runtime policy, the catalog), the material the
|
|
172
|
+
// model must act on last, closest to its answer. Registration order makes
|
|
173
|
+
// that placement deterministic: this listener registers before the catalog
|
|
174
|
+
// listener, so the waterfall hands it the catalog-bearing list to extend.
|
|
175
|
+
// Only `source.kind === 'user'` messages are scanned — external text
|
|
176
|
+
// cannot forge the gesture — and a token naming no user-invocable skill
|
|
177
|
+
// stays ordinary prose (the command registry is a different closed
|
|
178
|
+
// namespace, resolved client-side before a line ever becomes a prompt).
|
|
179
|
+
// This is the only entry point for `disable-model-invocation` skills; the
|
|
180
|
+
// catalog and the `skill` tool below never see them.
|
|
181
|
+
ctx.on('agent/pre-step', async (
|
|
182
|
+
{ agent, messages, signal },
|
|
183
|
+
next,
|
|
184
|
+
): Promise<PreStepDecision> => {
|
|
185
|
+
const decision = await next()
|
|
186
|
+
if (decision.kind === 'reject') return decision
|
|
187
|
+
const names = invokedSkillNames(messages)
|
|
188
|
+
if (names.length === 0) return decision
|
|
189
|
+
signal.throwIfAborted()
|
|
190
|
+
const lookup = { cwd: await ctx.workingDirectory.ensure(agent, signal), signal, scope: agent }
|
|
191
|
+
const injections: UserMessage[] = []
|
|
192
|
+
for (const name of names) {
|
|
193
|
+
const skill = await ctx.skills.get(name, lookup)
|
|
194
|
+
signal.throwIfAborted()
|
|
195
|
+
// Unknown names and user-disabled skills stay plain prose: the
|
|
196
|
+
// gesture was never a claim this boundary recognizes. The check sits
|
|
197
|
+
// on the loaded definition — the single lookup that produces what is
|
|
198
|
+
// actually injected.
|
|
199
|
+
if (skill === undefined || !isUserInvocable(skill)) continue
|
|
200
|
+
const source: SkillInvocationSource = { kind: 'skill-invocation', name, form: 'instructions' }
|
|
201
|
+
injections.push(createUserMessage({
|
|
202
|
+
content: [{ type: 'text', text: renderSkillContent(skill) }],
|
|
203
|
+
source,
|
|
204
|
+
}))
|
|
205
|
+
}
|
|
206
|
+
if (injections.length === 0) return decision
|
|
207
|
+
return { ...decision, messages: [...decision.messages, ...injections] }
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
// Register after the tool so reverse teardown removes guidance first. Exact definition
|
|
211
|
+
// identity prevents a scoped shadow merely named `skill` from inheriting this catalog.
|
|
212
|
+
//
|
|
213
|
+
// The comparison is against the definition this plugin registered, not against
|
|
214
|
+
// a lookup of its own name: `register()` files into the CALLING context's
|
|
215
|
+
// scope, so a plugin mounted inside an agent preset registers for that agent
|
|
216
|
+
// alone and an unscoped lookup correctly finds nothing.
|
|
217
|
+
ctx.on('agent/pre-step', async (
|
|
218
|
+
{ agent, signal },
|
|
219
|
+
next,
|
|
220
|
+
): Promise<PreStepDecision> => {
|
|
221
|
+
const decision = await next()
|
|
222
|
+
if (decision.kind === 'reject') return decision
|
|
223
|
+
signal.throwIfAborted()
|
|
224
|
+
const toolVisible = ctx.tools.get(skillTool.name, agent) === skillTool
|
|
225
|
+
const snapshot = toolVisible
|
|
226
|
+
? await ctx.skills.snapshot({ cwd: await ctx.workingDirectory.ensure(agent, signal), signal, scope: agent })
|
|
227
|
+
: { skills: [], complete: true }
|
|
228
|
+
signal.throwIfAborted()
|
|
229
|
+
const skills = snapshot.skills.filter(isModelInvocable)
|
|
230
|
+
const entries = catalogSourceEntries(skills, catalogDescriptionMaxLength)
|
|
231
|
+
const incomplete = !snapshot.complete
|
|
232
|
+
const digest = digestCatalogEntries(entries, incomplete)
|
|
233
|
+
const history = catalogHistory(agent)
|
|
234
|
+
const existing = catalogMessage(decision.messages)
|
|
235
|
+
if (history.visibleDigest === digest) {
|
|
236
|
+
return existing === undefined
|
|
237
|
+
? decision
|
|
238
|
+
: { ...decision, messages: decision.messages.filter(message => message.id !== existing.message.id) }
|
|
239
|
+
}
|
|
240
|
+
if (existing !== undefined && digestCatalogEntries(existing.entries, existing.incomplete) === digest) return decision
|
|
241
|
+
if (!history.published && skills.length === 0 && !incomplete) {
|
|
242
|
+
return existing === undefined
|
|
243
|
+
? decision
|
|
244
|
+
: { ...decision, messages: decision.messages.filter(message => message.id !== existing.message.id) }
|
|
245
|
+
}
|
|
246
|
+
const catalog = history.published
|
|
247
|
+
? renderCatalogUpdate(entries, incomplete)
|
|
248
|
+
: renderCatalogMessage(entries, incomplete)
|
|
249
|
+
return {
|
|
250
|
+
...decision,
|
|
251
|
+
messages: existing === undefined
|
|
252
|
+
? [...decision.messages, catalog]
|
|
253
|
+
: decision.messages.map(message => message.id === existing.message.id ? catalog : message),
|
|
254
|
+
}
|
|
255
|
+
})
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function renderCatalogMessage(entries: SkillCatalogSource['entries'], incomplete: boolean): UserMessage {
|
|
259
|
+
return createUserMessage({
|
|
260
|
+
content: [{
|
|
261
|
+
type: 'text',
|
|
262
|
+
text: [
|
|
263
|
+
'<system-reminder>',
|
|
264
|
+
entries.length === 0
|
|
265
|
+
? 'No skills could be confirmed in this discovery attempt.'
|
|
266
|
+
: 'A skill is a reusable set of task-specific instructions. The following skills are available in this session:',
|
|
267
|
+
'',
|
|
268
|
+
'<available_skills>',
|
|
269
|
+
...renderCatalogEntries(entries),
|
|
270
|
+
'</available_skills>',
|
|
271
|
+
'',
|
|
272
|
+
...incomplete ? [incompleteCatalogNotice(), ''] : [],
|
|
273
|
+
entries.length === 0
|
|
274
|
+
? 'Wait for an updated catalog before calling the `skill` tool. Do not infer skill instructions from an empty catalog.'
|
|
275
|
+
: "If the user names a skill, or the task clearly matches a skill's description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill's instructions until it has been loaded.",
|
|
276
|
+
'A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill.',
|
|
277
|
+
'</system-reminder>',
|
|
278
|
+
].join('\n'),
|
|
279
|
+
}],
|
|
280
|
+
source: {
|
|
281
|
+
kind: 'skill-catalog',
|
|
282
|
+
form: 'catalog',
|
|
283
|
+
entries,
|
|
284
|
+
...incomplete ? { incomplete: true } : {},
|
|
285
|
+
},
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function renderCatalogUpdate(entries: SkillCatalogSource['entries'], incomplete: boolean): UserMessage {
|
|
290
|
+
const availability = entries.length === 0
|
|
291
|
+
? [
|
|
292
|
+
incomplete
|
|
293
|
+
? 'No skills could be confirmed in this discovery attempt. Do not use names from earlier skill catalogs.'
|
|
294
|
+
: 'No skills are currently available through the `skill` tool. Do not use names from earlier skill catalogs.',
|
|
295
|
+
'A user may still invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool for it.',
|
|
296
|
+
]
|
|
297
|
+
: [
|
|
298
|
+
'Use only names in this replacement catalog. If the user names a listed skill, or the task clearly matches its description, call the `skill` tool with the exact name before acting.',
|
|
299
|
+
'A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill.',
|
|
300
|
+
]
|
|
301
|
+
return createUserMessage({
|
|
302
|
+
content: [{
|
|
303
|
+
type: 'text',
|
|
304
|
+
text: [
|
|
305
|
+
'<system-reminder>',
|
|
306
|
+
incomplete
|
|
307
|
+
? 'Skill discovery is incomplete. This catalog of currently available skills replaces every earlier available-skills list in this session:'
|
|
308
|
+
: 'The available skill catalog changed. This complete catalog replaces every earlier available-skills list in this session:',
|
|
309
|
+
'',
|
|
310
|
+
'<available_skills>',
|
|
311
|
+
...renderCatalogEntries(entries),
|
|
312
|
+
'</available_skills>',
|
|
313
|
+
'',
|
|
314
|
+
...incomplete ? [incompleteCatalogNotice(), ''] : [],
|
|
315
|
+
...availability,
|
|
316
|
+
'</system-reminder>',
|
|
317
|
+
].join('\n'),
|
|
318
|
+
}],
|
|
319
|
+
source: {
|
|
320
|
+
kind: 'skill-catalog',
|
|
321
|
+
form: 'catalog',
|
|
322
|
+
update: true,
|
|
323
|
+
entries,
|
|
324
|
+
...incomplete ? { incomplete: true } : {},
|
|
325
|
+
},
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function incompleteCatalogNotice(): string {
|
|
330
|
+
return 'Some skills could not be checked. Only confirmed skills are listed; a lower-priority definition of the same name may be selected when a preferred source is unavailable. Discovery will be retried on the next request.'
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Model-facing catalog lines, projected from the same entries the source records.
|
|
335
|
+
* The pseudo-XML escaping belongs to this frame, not to the published fact, so it
|
|
336
|
+
* is applied here and never stored. Names are `isSkillName`-validated and carry
|
|
337
|
+
* no escapable character.
|
|
338
|
+
*/
|
|
339
|
+
function renderCatalogEntries(entries: SkillCatalogSource['entries']): string[] {
|
|
340
|
+
return entries.map(entry => `- \`${entry.name}\`: ${escapeText(entry.description)}`)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Catalog identity includes durable entries and completeness so recovery clears
|
|
345
|
+
* a discovery warning even when the entries are unchanged.
|
|
346
|
+
*/
|
|
347
|
+
function digestCatalogEntries(entries: SkillCatalogSource['entries'], incomplete = false): string {
|
|
348
|
+
// JSON per entry rather than a separator character: every separator is itself
|
|
349
|
+
// a legal description character, so only quoting makes the boundary exact.
|
|
350
|
+
const canonical = entries.map(entry => JSON.stringify([entry.name, entry.description])).join('\n')
|
|
351
|
+
return createHash('sha256')
|
|
352
|
+
.update(incomplete ? 'incomplete\n' : '')
|
|
353
|
+
.update(canonical)
|
|
354
|
+
.digest('hex')
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Entries of one durable catalog message, or undefined when the record is not a
|
|
359
|
+
* usable catalog.
|
|
360
|
+
*
|
|
361
|
+
* `agent.session.snapshotEvents()` may contain a resumed, forked, or externally written seed,
|
|
362
|
+
* and seed validation only guarantees a source object with a non-empty `kind`;
|
|
363
|
+
* no per-kind field is checked there. An unreadable record is therefore treated
|
|
364
|
+
* as "not this plugin's catalog" — the posture the replaced content digest had —
|
|
365
|
+
* rather than throwing inside the step listener, which would fail every
|
|
366
|
+
* subsequent turn of that session.
|
|
367
|
+
*/
|
|
368
|
+
function readCatalogEntries(source: unknown): SkillCatalogSource['entries'] | undefined {
|
|
369
|
+
const entries = (source as { entries?: unknown }).entries
|
|
370
|
+
if (!Array.isArray(entries)) return undefined
|
|
371
|
+
const readable: { name: string; description: string }[] = []
|
|
372
|
+
for (const entry of entries as readonly unknown[]) {
|
|
373
|
+
if (typeof entry !== 'object' || entry === null) return undefined
|
|
374
|
+
const { name, description } = entry as { name?: unknown; description?: unknown }
|
|
375
|
+
if (typeof name !== 'string' || name === '' || typeof description !== 'string') return undefined
|
|
376
|
+
readable.push({ name, description })
|
|
377
|
+
}
|
|
378
|
+
return readable
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function catalogHistory(agent: Agent): { visibleDigest?: string; published: boolean } {
|
|
382
|
+
const visible = new Set(agent.session.surface.nodes)
|
|
383
|
+
let published = false
|
|
384
|
+
for (let index = agent.session.seq - 1; index >= 0; index -= 1) {
|
|
385
|
+
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
|
|
386
|
+
const event = agent.session.eventAt(SessionSeq(index))
|
|
387
|
+
if (event === undefined) {
|
|
388
|
+
throw new Error(`skill catalog cannot read seq ${String(index)} below the current Session length`)
|
|
389
|
+
}
|
|
390
|
+
if (event.type !== 'user/message' || event.data.source.kind !== 'skill-catalog') continue
|
|
391
|
+
const entries = readCatalogEntries(event.data.source)
|
|
392
|
+
if (entries === undefined) continue
|
|
393
|
+
const digest = digestCatalogEntries(entries, event.data.source.incomplete === true)
|
|
394
|
+
published = true
|
|
395
|
+
if (visible.has(event.seq)) return { visibleDigest: digest, published }
|
|
396
|
+
}
|
|
397
|
+
return { published }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function catalogMessage(
|
|
401
|
+
messages: readonly UserMessage[],
|
|
402
|
+
): { message: UserMessage; entries: SkillCatalogSource['entries']; incomplete: boolean } | undefined {
|
|
403
|
+
for (const message of messages) {
|
|
404
|
+
if (message.source.kind !== 'skill-catalog') continue
|
|
405
|
+
const entries = readCatalogEntries(message.source)
|
|
406
|
+
if (entries !== undefined) return { message, entries, incomplete: message.source.incomplete === true }
|
|
407
|
+
}
|
|
408
|
+
return undefined
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function catalogDescription(value: string, maxLength: number): string {
|
|
412
|
+
const normalized = value.replaceAll(/\s+/g, ' ').trim()
|
|
413
|
+
return normalized.length <= maxLength ? normalized : `${normalized.slice(0, maxLength - 3)}...`
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function assertPositiveInteger(name: string, value: number, minimum = 1): void {
|
|
417
|
+
if (!Number.isInteger(value) || value < minimum) {
|
|
418
|
+
throw new Error(`tool-skill: ${name} must be an integer greater than or equal to ${minimum}`)
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* A whitespace-bounded `/name` token (the public skill-name grammar) anywhere
|
|
424
|
+
* in the text — the same word-boundary shape the transcript chip decoration
|
|
425
|
+
* uses, so a gesture reads as one wherever it sits in the sentence. A second
|
|
426
|
+
* `/` or any non-boundary character breaks the match, which keeps file paths
|
|
427
|
+
* (`/usr/bin`) and fractions (`5/8`) out.
|
|
428
|
+
*/
|
|
429
|
+
const SKILL_GESTURE = /(^|\s)\/([a-z0-9]+(?:-[a-z0-9]+)*)(?=\s|$)/g
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* `/name` gesture tokens from the claimed user messages, deduplicated in
|
|
433
|
+
* first-seen order. Every text block of direct user input is scanned; no
|
|
434
|
+
* other source can forge a gesture.
|
|
435
|
+
* @param messages - the step's claimed batch.
|
|
436
|
+
* @returns candidate skill names, unvalidated against the registry.
|
|
437
|
+
*/
|
|
438
|
+
function invokedSkillNames(messages: readonly UserMessage[]): string[] {
|
|
439
|
+
const names: string[] = []
|
|
440
|
+
for (const message of messages) {
|
|
441
|
+
if ((message.source as { kind?: unknown }).kind !== 'user') continue
|
|
442
|
+
for (const block of message.content) {
|
|
443
|
+
if (block.type !== 'text') continue
|
|
444
|
+
for (const match of block.text.matchAll(SKILL_GESTURE)) {
|
|
445
|
+
const name = match[2]
|
|
446
|
+
if (name !== undefined && !names.includes(name)) names.push(name)
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return names
|
|
451
|
+
}
|