@unieai/rabi-skill 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +566 -0
- package/lib/types/index.d.ts +298 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -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/locale/zh.json +6 -0
- package/package.json +37 -4
- package/src/index.ts +868 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
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,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-skill
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Agent skill provider registry for the Rabi Harness
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import { Service } from "@unieai/cordis";
|
|
2
|
+
import { assertNever } from "@unieai/rabi-util-values";
|
|
3
|
+
import { NamedEntries, ScopedLayers, scopeChainOf, scopeOf } from "@unieai/rabi-scope";
|
|
4
|
+
import z from "@unieai/schemastery";
|
|
5
|
+
//#region lib/types/index.js
|
|
6
|
+
/**
|
|
7
|
+
* Agent skill provider registry.
|
|
8
|
+
*
|
|
9
|
+
* This package owns the Service Definition role of the skill capability seam.
|
|
10
|
+
* Concrete
|
|
11
|
+
* providers such as `@unieai/rabi-skill-filesystem` decide where skills come
|
|
12
|
+
* from; this service only merges provider catalogs, resolves the winning skill
|
|
13
|
+
* for a name, and exposes the winning summaries and definitions to consumers.
|
|
14
|
+
*
|
|
15
|
+
* @module @unieai/rabi-skill
|
|
16
|
+
*/
|
|
17
|
+
const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
18
|
+
const DEFAULT_COLLECT_CACHE_ENTRIES = 128;
|
|
19
|
+
const MAX_COLLECT_ATTEMPTS = 2;
|
|
20
|
+
const RUNTIME_PROVIDER = "runtime";
|
|
21
|
+
const RUNTIME_RANK = 250;
|
|
22
|
+
/** Standard precedence rank for packaged skill providers and local bundled roots. */
|
|
23
|
+
const BUNDLED_SKILL_RANK = 600;
|
|
24
|
+
/**
|
|
25
|
+
* Return whether a string is a valid kebab-case skill name.
|
|
26
|
+
* @param name - candidate skill name to validate.
|
|
27
|
+
* @returns whether the name matches the public skill-name grammar.
|
|
28
|
+
*/
|
|
29
|
+
function isSkillName(name) {
|
|
30
|
+
return SKILL_NAME.test(name);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Return whether a skill may be advertised to and loaded by a model.
|
|
34
|
+
* @param skill - skill metadata carrying resolved invocation controls.
|
|
35
|
+
* @returns whether the policy permits model invocation.
|
|
36
|
+
*/
|
|
37
|
+
function isModelInvocable(skill) {
|
|
38
|
+
return skill.invocation.modelInvocable;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Return whether a skill may be advertised to and loaded by a human-facing command.
|
|
42
|
+
* @param skill - skill metadata carrying resolved invocation controls.
|
|
43
|
+
* @returns whether the policy permits user invocation.
|
|
44
|
+
*/
|
|
45
|
+
function isUserInvocable(skill) {
|
|
46
|
+
return skill.invocation.userInvocable;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Render one loaded skill for the model. The output is shared verbatim by the
|
|
50
|
+
* `skill` tool result and the user-explicit invocation injection, so the model
|
|
51
|
+
* sees one canonical `<skill_content>` shape on both paths. The name rides an
|
|
52
|
+
* escaped attribute; the body is embedded verbatim (skills are trusted local
|
|
53
|
+
* content, and user-supplied invocation text stays outside this wrapper).
|
|
54
|
+
* @param skill - name, provider, optional resource base, and body to render.
|
|
55
|
+
* @returns the complete model-facing `<skill_content>` block.
|
|
56
|
+
*/
|
|
57
|
+
function renderSkillContent(skill) {
|
|
58
|
+
const resourceHint = renderResourceHint(skill);
|
|
59
|
+
return [
|
|
60
|
+
`<skill_content name="${escapeAttr(skill.name)}">`,
|
|
61
|
+
"<skill_resources>",
|
|
62
|
+
...resourceHint,
|
|
63
|
+
"</skill_resources>",
|
|
64
|
+
"",
|
|
65
|
+
"<skill_instructions>",
|
|
66
|
+
skill.content,
|
|
67
|
+
"</skill_instructions>",
|
|
68
|
+
"</skill_content>"
|
|
69
|
+
].join("\n");
|
|
70
|
+
}
|
|
71
|
+
function renderResourceHint(skill) {
|
|
72
|
+
const base = skill.resourceBase;
|
|
73
|
+
if (base === void 0) return [`Resources for this skill are managed by provider "${escapeText(skill.provider)}".`, "Load referenced resources only as needed."];
|
|
74
|
+
switch (base.kind) {
|
|
75
|
+
case "directory": return [`Base directory for this skill: ${escapeText(base.path)}`, "Resolve relative paths mentioned by this skill against the base directory before using them. Load referenced resources only as needed."];
|
|
76
|
+
case "url": return [`Base URL for this skill: ${escapeText(base.url)}`, "Resolve relative URLs mentioned by this skill against the base URL before using them. Load referenced resources only as needed."];
|
|
77
|
+
case "opaque": return [`Resources for this skill: ${escapeText(base.description)}`, "Load referenced resources only as needed."];
|
|
78
|
+
/* v8 ignore start -- SkillResourceBase is a closed union; a future kind must fail compilation here. */
|
|
79
|
+
default: return assertNever(base, "SkillResourceBase.kind");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function escapeAttr(value) {
|
|
83
|
+
return value.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Escape model-facing prose embedded inside skill markup so provider-supplied
|
|
87
|
+
* text cannot open or close framing tags.
|
|
88
|
+
* @param value - raw prose to embed.
|
|
89
|
+
* @returns the escaped text.
|
|
90
|
+
*/
|
|
91
|
+
function escapeText(value) {
|
|
92
|
+
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
93
|
+
}
|
|
94
|
+
/** One scope's complete skill-registry contribution. */
|
|
95
|
+
var SkillLayer = class {
|
|
96
|
+
/** Providers registered through contexts carrying this scope, insertion-ordered. */
|
|
97
|
+
providers;
|
|
98
|
+
/** Runtime skills registered through contexts carrying this scope. */
|
|
99
|
+
runtime = /* @__PURE__ */ new Map();
|
|
100
|
+
constructor(scope) {
|
|
101
|
+
this.providers = new NamedEntries((name) => /* @__PURE__ */ new Error(scope === void 0 ? `a skill provider named "${name}" is already registered` : `a skill provider named "${name}" is already registered in this scope`));
|
|
102
|
+
}
|
|
103
|
+
/** Whether every contribution table in this aggregate layer is empty. */
|
|
104
|
+
isEmpty() {
|
|
105
|
+
return this.providers.isEmpty() && this.runtime.size === 0;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Layered registry of skill providers, the host+per-scope shape the tools
|
|
110
|
+
* registry established. A registration files into the layer of its calling
|
|
111
|
+
* context's scope ({@link scopeOf}): host rows and repository plugins land in
|
|
112
|
+
* the global layer, while a plugin mounted by an agent preset's standing
|
|
113
|
+
* composition lands in that preset's layer. A read merges the global layer
|
|
114
|
+
* with the viewing scope's chain — the nearest layer's entry wins a duplicate
|
|
115
|
+
* name outright, and the rank order decides duplicates only within one layer.
|
|
116
|
+
* It exposes sorted invocation-neutral summaries and loads full skill bodies
|
|
117
|
+
* on demand.
|
|
118
|
+
*/
|
|
119
|
+
var SkillRegistry = class extends Service {
|
|
120
|
+
static Config = z.object({ collectCacheMaxEntries: z.number().default(DEFAULT_COLLECT_CACHE_ENTRIES) });
|
|
121
|
+
collectCacheMaxEntries;
|
|
122
|
+
layers = new ScopedLayers((scope) => new SkillLayer(scope), () => {
|
|
123
|
+
this.invalidateCache();
|
|
124
|
+
});
|
|
125
|
+
collectCache = /* @__PURE__ */ new Map();
|
|
126
|
+
revision = 0;
|
|
127
|
+
nextProviderOrder = 0;
|
|
128
|
+
/** Stable identities for cache keys; scope keys are opaque identity-compared objects. */
|
|
129
|
+
scopeIds = /* @__PURE__ */ new WeakMap();
|
|
130
|
+
nextScopeId = 1;
|
|
131
|
+
constructor(ctx, config = {}) {
|
|
132
|
+
super(ctx, "skills");
|
|
133
|
+
this.collectCacheMaxEntries = config.collectCacheMaxEntries ?? DEFAULT_COLLECT_CACHE_ENTRIES;
|
|
134
|
+
assertPositiveInteger("collectCacheMaxEntries", this.collectCacheMaxEntries);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Register a borrowed same-process provider synchronously during plugin
|
|
138
|
+
* apply, into the calling context's layer: a scoped context (an agent
|
|
139
|
+
* preset's standing mount) registers for that scope alone, an unscoped
|
|
140
|
+
* context registers globally. Duplicate names within one layer and reserved
|
|
141
|
+
* names throw; remote initialization belongs in `list()`. Fiber disposal
|
|
142
|
+
* unregisters the provider and invalidates catalog caches.
|
|
143
|
+
* @param create - synchronous factory receiving this registration's lifecycle and invalidation control.
|
|
144
|
+
* @returns the exact Cordis effect disposer that unregisters this provider;
|
|
145
|
+
* composite effects may yield it directly to preserve teardown ordering.
|
|
146
|
+
*/
|
|
147
|
+
registerProvider(create) {
|
|
148
|
+
const lifecycle = new AbortController();
|
|
149
|
+
let registration;
|
|
150
|
+
let provider;
|
|
151
|
+
const control = {
|
|
152
|
+
signal: lifecycle.signal,
|
|
153
|
+
invalidate: () => {
|
|
154
|
+
const active = registration;
|
|
155
|
+
if (active !== void 0 && active.layer.providers.get(active.name)?.provider === provider) this.invalidateCache();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
try {
|
|
159
|
+
provider = create(control);
|
|
160
|
+
const name = provider.name;
|
|
161
|
+
if (name === RUNTIME_PROVIDER) throw new Error(`"${RUNTIME_PROVIDER}" is reserved for runtime skill registrations`);
|
|
162
|
+
const order = this.nextProviderOrder;
|
|
163
|
+
this.nextProviderOrder += 1;
|
|
164
|
+
return this.layers.effect(this.ctx, (layer) => {
|
|
165
|
+
const undo = layer.providers.insert(name, {
|
|
166
|
+
provider,
|
|
167
|
+
order
|
|
168
|
+
});
|
|
169
|
+
registration = {
|
|
170
|
+
layer,
|
|
171
|
+
name
|
|
172
|
+
};
|
|
173
|
+
return () => {
|
|
174
|
+
registration = void 0;
|
|
175
|
+
undo();
|
|
176
|
+
lifecycle.abort(/* @__PURE__ */ new Error(`skill provider "${name}" disposed`));
|
|
177
|
+
};
|
|
178
|
+
}, { label: "skills.registerProvider()" });
|
|
179
|
+
} catch (error) {
|
|
180
|
+
lifecycle.abort(error);
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Register a borrowed readonly runtime skill into the calling context's
|
|
186
|
+
* layer. Project entries outrank runtime entries, which outrank user
|
|
187
|
+
* entries, within one layer. Same-name runtime entries in one layer are
|
|
188
|
+
* first-wins; a duplicate logs a warning and receives a no-op disposer so
|
|
189
|
+
* it cannot remove the winner.
|
|
190
|
+
* @param skill - the skill definition input; omitted invocation and provider fields receive defaults.
|
|
191
|
+
* @returns the exact Cordis effect disposer, preserving composite teardown order and invalidating caches.
|
|
192
|
+
*/
|
|
193
|
+
register(skill) {
|
|
194
|
+
validateRuntimeSkill(skill);
|
|
195
|
+
const scope = scopeOf(this.ctx);
|
|
196
|
+
const existingLayer = scope === void 0 ? this.layers.global : this.layers.peek(scope);
|
|
197
|
+
if (existingLayer !== void 0 && existingLayer.runtime.has(skill.name)) {
|
|
198
|
+
this.ctx.logger.warn(`runtime skill "${skill.name}" ignored because it is already registered`);
|
|
199
|
+
return () => {};
|
|
200
|
+
}
|
|
201
|
+
const definition = {
|
|
202
|
+
...skill,
|
|
203
|
+
invocation: skill.invocation ?? {
|
|
204
|
+
modelInvocable: true,
|
|
205
|
+
userInvocable: true
|
|
206
|
+
},
|
|
207
|
+
provider: skill.provider ?? RUNTIME_PROVIDER
|
|
208
|
+
};
|
|
209
|
+
return this.layers.effect(this.ctx, (layer) => {
|
|
210
|
+
layer.runtime.set(definition.name, definition);
|
|
211
|
+
return () => {
|
|
212
|
+
layer.runtime.delete(definition.name);
|
|
213
|
+
};
|
|
214
|
+
}, { label: "skills.register()" });
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* List invocation-neutral skill summaries for a workspace. Consumers apply
|
|
218
|
+
* model or user invocation policy at their operational boundary. Lookup
|
|
219
|
+
* options and provider candidates are readonly same-process values borrowed
|
|
220
|
+
* throughout discovery.
|
|
221
|
+
* @param options - view options; `scope` selects the viewing agent's layers, `cwd` selects project roots, and `signal` cancels discovery.
|
|
222
|
+
* @returns all sorted winning summaries.
|
|
223
|
+
*/
|
|
224
|
+
async list(options = {}) {
|
|
225
|
+
return (await this.snapshot(options)).skills;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Observe the current invocation-neutral catalog and whether discovery completed within a stable revision.
|
|
229
|
+
* Incomplete observations are never cached, allowing consumers to retain last-good state and
|
|
230
|
+
* retry on their next request boundary.
|
|
231
|
+
* @param options - view options; `scope` selects the viewing agent's layers, `cwd` selects project roots, and `signal` cancels discovery.
|
|
232
|
+
* @returns sorted summaries plus discovery-completeness state.
|
|
233
|
+
*/
|
|
234
|
+
async snapshot(options = {}) {
|
|
235
|
+
const collected = await this.collect(options);
|
|
236
|
+
return {
|
|
237
|
+
skills: [...collected.entries.values()].map((entry) => toSummary(entry.candidate)).sort(compareSkillSummary),
|
|
238
|
+
complete: collected.cacheable
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Load and validate the winning candidate, passing its opaque discovery locator back to the
|
|
243
|
+
* provider. Cancellation is rechecked after selection, including cache hits, and raced against
|
|
244
|
+
* loading so an uncooperative provider cannot hang the caller.
|
|
245
|
+
* @param name - kebab-case skill name.
|
|
246
|
+
* @param options - view options; `scope` selects the viewing agent's layers,
|
|
247
|
+
* `cwd` selects workspace-sensitive skills, and `signal` cancels work.
|
|
248
|
+
* @returns the full skill, including body content, or `undefined`.
|
|
249
|
+
*/
|
|
250
|
+
async get(name, options = {}) {
|
|
251
|
+
if (!isSkillName(name)) return void 0;
|
|
252
|
+
const collected = await this.collect(options);
|
|
253
|
+
throwIfAborted(options.signal);
|
|
254
|
+
const match = collected.entries.get(name);
|
|
255
|
+
if (match === void 0) return void 0;
|
|
256
|
+
const definition = await waitWithAbort(match.provider.get(match.candidate, options), options.signal);
|
|
257
|
+
if (definition === void 0) return void 0;
|
|
258
|
+
validateDefinition(definition);
|
|
259
|
+
if (definition.name !== match.candidate.name) {
|
|
260
|
+
this.invalidateEntry(match);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
return definition;
|
|
264
|
+
}
|
|
265
|
+
async collect(options) {
|
|
266
|
+
throwIfAborted(options.signal);
|
|
267
|
+
let attempt = 1;
|
|
268
|
+
while (true) {
|
|
269
|
+
const revision = this.revision;
|
|
270
|
+
const key = this.collectCacheKey(options.cwd, scopeChainOf(options.scope), revision);
|
|
271
|
+
const cached = this.collectCache.get(key);
|
|
272
|
+
if (cached !== void 0) return {
|
|
273
|
+
entries: cached,
|
|
274
|
+
cacheable: true
|
|
275
|
+
};
|
|
276
|
+
const result = await this.collectFresh(options);
|
|
277
|
+
throwIfAborted(options.signal);
|
|
278
|
+
if (revision !== this.revision) {
|
|
279
|
+
if (attempt < MAX_COLLECT_ATTEMPTS) {
|
|
280
|
+
attempt += 1;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
entries: result.entries,
|
|
285
|
+
cacheable: false
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
if (result.cacheable) {
|
|
289
|
+
this.collectCache.set(key, result.entries);
|
|
290
|
+
if (this.collectCache.size > this.collectCacheMaxEntries) {
|
|
291
|
+
const oldest = this.collectCache.keys().next();
|
|
292
|
+
this.collectCache.delete(oldest.value);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return result;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
async collectFresh(options) {
|
|
299
|
+
const layers = [this.layers.global, ...this.layers.chainLayers(options.scope)];
|
|
300
|
+
const merged = /* @__PURE__ */ new Map();
|
|
301
|
+
let cacheable = true;
|
|
302
|
+
for (const layer of layers) {
|
|
303
|
+
const collected = await this.collectLayer(layer, options);
|
|
304
|
+
if (!collected.cacheable) cacheable = false;
|
|
305
|
+
for (const entry of collected.entries) merged.set(entry.candidate.name, entry);
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
entries: merged,
|
|
309
|
+
cacheable
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async collectLayer(layer, options) {
|
|
313
|
+
const collected = await this.listLayerCandidates(layer, options);
|
|
314
|
+
collected.entries.sort(compareIndexedCandidates);
|
|
315
|
+
const seen = /* @__PURE__ */ new Set();
|
|
316
|
+
const result = [];
|
|
317
|
+
for (const entry of collected.entries) {
|
|
318
|
+
const skill = entry.candidate;
|
|
319
|
+
if (seen.has(skill.name)) {
|
|
320
|
+
this.ctx.logger.warn(`skill "${skill.name}" from ${skill.source} ignored because a higher-priority skill already exists`);
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
seen.add(skill.name);
|
|
324
|
+
result.push(entry);
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
entries: result,
|
|
328
|
+
cacheable: collected.cacheable
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
async listLayerCandidates(layer, options) {
|
|
332
|
+
throwIfAborted(options.signal);
|
|
333
|
+
const candidates = [];
|
|
334
|
+
let cacheable = true;
|
|
335
|
+
let runtimeOrder = 0;
|
|
336
|
+
for (const skill of [...layer.runtime.values()].sort((a, b) => compareCodePoints(a.name, b.name))) {
|
|
337
|
+
candidates.push({
|
|
338
|
+
candidate: runtimeCandidate(skill),
|
|
339
|
+
provider: RUNTIME_SKILL_PROVIDER,
|
|
340
|
+
providerOrder: -1,
|
|
341
|
+
localOrder: runtimeOrder,
|
|
342
|
+
layer
|
|
343
|
+
});
|
|
344
|
+
runtimeOrder += 1;
|
|
345
|
+
}
|
|
346
|
+
for (const { provider, order } of [...layer.providers.values()]) {
|
|
347
|
+
let localOrder = 0;
|
|
348
|
+
let output;
|
|
349
|
+
try {
|
|
350
|
+
output = await waitWithAbort(provider.list(options), options.signal);
|
|
351
|
+
} catch (error) {
|
|
352
|
+
if (options.signal?.aborted === true) throw toError(options.signal.reason);
|
|
353
|
+
cacheable = false;
|
|
354
|
+
this.ctx.logger.warn(`skill provider "${provider.name}" skipped: ${errorMessage(error)}`);
|
|
355
|
+
}
|
|
356
|
+
if (output === void 0) continue;
|
|
357
|
+
const observation = normalizeProviderObservation(output, provider.name);
|
|
358
|
+
if (!observation.complete) cacheable = false;
|
|
359
|
+
for (const candidate of observation.candidates) {
|
|
360
|
+
validateCandidate(candidate, provider.name);
|
|
361
|
+
candidates.push({
|
|
362
|
+
candidate,
|
|
363
|
+
provider,
|
|
364
|
+
providerOrder: order,
|
|
365
|
+
localOrder,
|
|
366
|
+
layer
|
|
367
|
+
});
|
|
368
|
+
localOrder += 1;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
entries: candidates,
|
|
373
|
+
cacheable
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
invalidateCache() {
|
|
377
|
+
this.revision += 1;
|
|
378
|
+
this.collectCache.clear();
|
|
379
|
+
this.notifyChange();
|
|
380
|
+
}
|
|
381
|
+
/** Invalidate after a stale definition load, only while the exact registration that produced the entry is still live. */
|
|
382
|
+
invalidateEntry(entry) {
|
|
383
|
+
/* v8 ignore else -- A definition load can outlive the exact provider registration it selected. */
|
|
384
|
+
if (entry.layer.providers.get(entry.provider.name)?.provider === entry.provider) this.invalidateCache();
|
|
385
|
+
}
|
|
386
|
+
scopeId(key) {
|
|
387
|
+
let id = this.scopeIds.get(key);
|
|
388
|
+
if (id === void 0) {
|
|
389
|
+
id = this.nextScopeId;
|
|
390
|
+
this.nextScopeId += 1;
|
|
391
|
+
this.scopeIds.set(key, id);
|
|
392
|
+
}
|
|
393
|
+
return id;
|
|
394
|
+
}
|
|
395
|
+
collectCacheKey(cwd, chain, revision) {
|
|
396
|
+
return JSON.stringify({
|
|
397
|
+
cwd,
|
|
398
|
+
scopes: chain.map((key) => this.scopeId(key)),
|
|
399
|
+
revision
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
/** Notify catalog observers without making their refresh work load-bearing. */
|
|
403
|
+
notifyChange() {
|
|
404
|
+
for (const callback of this.ctx.events.dispatch("emit", ["skills/change"])) try {
|
|
405
|
+
const returned = callback();
|
|
406
|
+
Promise.resolve(returned).catch((error) => {
|
|
407
|
+
this.ctx.logger.warn(`skills/change listener rejected: ${errorMessage(error)}`);
|
|
408
|
+
});
|
|
409
|
+
} catch (error) {
|
|
410
|
+
this.ctx.logger.warn(`skills/change listener threw: ${errorMessage(error)}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
function normalizeProviderObservation(output, providerName) {
|
|
415
|
+
if (Array.isArray(output)) return {
|
|
416
|
+
candidates: output,
|
|
417
|
+
complete: true
|
|
418
|
+
};
|
|
419
|
+
if (output === null || typeof output !== "object") throw invalidProviderObservation(providerName);
|
|
420
|
+
const observation = output;
|
|
421
|
+
if (!Array.isArray(observation.candidates) || typeof observation.complete !== "boolean") throw invalidProviderObservation(providerName);
|
|
422
|
+
return observation;
|
|
423
|
+
}
|
|
424
|
+
function invalidProviderObservation(providerName) {
|
|
425
|
+
return /* @__PURE__ */ new TypeError(`skill provider "${providerName}" list() must return an array or { candidates, complete } observation`);
|
|
426
|
+
}
|
|
427
|
+
const RUNTIME_SKILL_PROVIDER = {
|
|
428
|
+
name: RUNTIME_PROVIDER,
|
|
429
|
+
/* v8 ignore next -- Runtime skills are injected directly by the registry; this provider only owns `get()`. */
|
|
430
|
+
list() {
|
|
431
|
+
return Promise.resolve([]);
|
|
432
|
+
},
|
|
433
|
+
get(candidate) {
|
|
434
|
+
return Promise.resolve(candidate.locator);
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
function runtimeCandidate(skill) {
|
|
438
|
+
return {
|
|
439
|
+
name: skill.name,
|
|
440
|
+
description: skill.description,
|
|
441
|
+
...skill.whenToUse !== void 0 ? { whenToUse: skill.whenToUse } : {},
|
|
442
|
+
invocation: skill.invocation,
|
|
443
|
+
source: skill.source,
|
|
444
|
+
provider: skill.provider,
|
|
445
|
+
...skill.resourceBase !== void 0 ? { resourceBase: skill.resourceBase } : {},
|
|
446
|
+
rank: RUNTIME_RANK,
|
|
447
|
+
locator: skill,
|
|
448
|
+
...skill.path !== void 0 ? { path: skill.path } : {},
|
|
449
|
+
...skill.metadata !== void 0 ? { metadata: skill.metadata } : {}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
function validateCandidate(candidate, providerName) {
|
|
453
|
+
if (typeof candidate.name !== "string") throw new TypeError(`skill provider "${providerName}" returned a non-string skill name`);
|
|
454
|
+
if (!SKILL_NAME.test(candidate.name)) throw new Error(`skill provider "${providerName}" returned invalid skill name "${candidate.name}"`);
|
|
455
|
+
if (typeof candidate.description !== "string") throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string description`);
|
|
456
|
+
if (candidate.description.length === 0) throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" without a description`);
|
|
457
|
+
validateInvocation(candidate.invocation, `skill provider "${providerName}" returned skill "${candidate.name}"`);
|
|
458
|
+
if (candidate.whenToUse !== void 0 && typeof candidate.whenToUse !== "string") throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string whenToUse`);
|
|
459
|
+
if (typeof candidate.source !== "string") throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string source`);
|
|
460
|
+
if (typeof candidate.rank !== "number" || !Number.isFinite(candidate.rank)) throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" with an invalid rank`);
|
|
461
|
+
if (typeof candidate.provider !== "string") throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string provider`);
|
|
462
|
+
if (candidate.provider !== providerName) throw new Error(`skill provider "${providerName}" returned skill "${candidate.name}" for provider "${candidate.provider}"`);
|
|
463
|
+
if (candidate.path !== void 0 && typeof candidate.path !== "string") throw new TypeError(`skill provider "${providerName}" returned skill "${candidate.name}" with a non-string path`);
|
|
464
|
+
}
|
|
465
|
+
function validateRuntimeSkill(skill) {
|
|
466
|
+
if (!SKILL_NAME.test(skill.name)) throw new Error(`invalid skill name "${skill.name}"`);
|
|
467
|
+
if (skill.description.length === 0) throw new Error(`skill "${skill.name}" requires a description`);
|
|
468
|
+
validateInvocation(skill.invocation, `runtime skill "${skill.name}"`);
|
|
469
|
+
}
|
|
470
|
+
/** Validate a definition loaded from a provider-controlled parser or remote source. */
|
|
471
|
+
function validateDefinition(skill) {
|
|
472
|
+
const name = skill.name;
|
|
473
|
+
const description = skill.description;
|
|
474
|
+
const whenToUse = skill.whenToUse;
|
|
475
|
+
const invocation = skill.invocation;
|
|
476
|
+
const source = skill.source;
|
|
477
|
+
const provider = skill.provider;
|
|
478
|
+
const content = skill.content;
|
|
479
|
+
const path = skill.path;
|
|
480
|
+
if (typeof name !== "string") throw new TypeError("loaded skill name must be a string");
|
|
481
|
+
if (!SKILL_NAME.test(name)) throw new Error(`loaded skill has invalid name "${name}"`);
|
|
482
|
+
if (typeof description !== "string") throw new TypeError(`loaded skill "${name}" description must be a string`);
|
|
483
|
+
if (description.length === 0) throw new Error(`loaded skill "${name}" requires a description`);
|
|
484
|
+
validateInvocation(invocation, `loaded skill "${name}"`);
|
|
485
|
+
if (whenToUse !== void 0 && typeof whenToUse !== "string") throw new TypeError(`loaded skill "${name}" whenToUse must be a string`);
|
|
486
|
+
if (typeof source !== "string") throw new TypeError(`loaded skill "${name}" source must be a string`);
|
|
487
|
+
if (typeof provider !== "string") throw new TypeError(`loaded skill "${name}" provider must be a string`);
|
|
488
|
+
if (typeof content !== "string") throw new TypeError(`loaded skill "${name}" content must be a string`);
|
|
489
|
+
if (path !== void 0 && typeof path !== "string") throw new TypeError(`loaded skill "${name}" path must be a string`);
|
|
490
|
+
}
|
|
491
|
+
function toSummary(skill) {
|
|
492
|
+
const { name, description, whenToUse, invocation, source, provider, resourceBase } = skill;
|
|
493
|
+
return {
|
|
494
|
+
name,
|
|
495
|
+
...skill.path === void 0 ? {} : { path: skill.path },
|
|
496
|
+
description,
|
|
497
|
+
...whenToUse !== void 0 ? { whenToUse } : {},
|
|
498
|
+
invocation,
|
|
499
|
+
source,
|
|
500
|
+
provider,
|
|
501
|
+
...resourceBase !== void 0 ? { resourceBase } : {}
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
function validateInvocation(invocation, subject) {
|
|
505
|
+
if (invocation === void 0) return;
|
|
506
|
+
if (typeof invocation !== "object" || invocation === null || Array.isArray(invocation)) throw new TypeError(`${subject} with a non-object invocation policy`);
|
|
507
|
+
const policy = invocation;
|
|
508
|
+
if (typeof policy.modelInvocable !== "boolean") throw new TypeError(`${subject} with a non-boolean invocation.modelInvocable`);
|
|
509
|
+
if (typeof policy.userInvocable !== "boolean") throw new TypeError(`${subject} with a non-boolean invocation.userInvocable`);
|
|
510
|
+
}
|
|
511
|
+
function compareSkillSummary(left, right) {
|
|
512
|
+
return compareCodePoints(left.name, right.name);
|
|
513
|
+
}
|
|
514
|
+
function compareCodePoints(left, right) {
|
|
515
|
+
if (left < right) return -1;
|
|
516
|
+
if (left > right) return 1;
|
|
517
|
+
return 0;
|
|
518
|
+
}
|
|
519
|
+
function compareIndexedCandidates(left, right) {
|
|
520
|
+
return left.candidate.rank - right.candidate.rank || left.providerOrder - right.providerOrder || left.localOrder - right.localOrder;
|
|
521
|
+
}
|
|
522
|
+
function assertPositiveInteger(name, value, minimum = 1) {
|
|
523
|
+
if (!Number.isInteger(value) || value < minimum) throw new Error(`skill: ${name} must be an integer greater than or equal to ${minimum}`);
|
|
524
|
+
}
|
|
525
|
+
function waitWithAbort(promise, signal) {
|
|
526
|
+
if (signal === void 0) return promise;
|
|
527
|
+
throwIfAborted(signal);
|
|
528
|
+
return new Promise((resolve, reject) => {
|
|
529
|
+
const cleanup = () => {
|
|
530
|
+
signal.removeEventListener("abort", onAbort);
|
|
531
|
+
};
|
|
532
|
+
const onAbort = () => {
|
|
533
|
+
cleanup();
|
|
534
|
+
reject(toError(signal.reason));
|
|
535
|
+
};
|
|
536
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
537
|
+
promise.then((value) => {
|
|
538
|
+
cleanup();
|
|
539
|
+
resolve(value);
|
|
540
|
+
}, (error) => {
|
|
541
|
+
cleanup();
|
|
542
|
+
reject(toError(error));
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
/** Throw a total Error for an already-aborted lookup. */
|
|
547
|
+
function throwIfAborted(signal) {
|
|
548
|
+
if (signal?.aborted === true) throw toError(signal.reason);
|
|
549
|
+
}
|
|
550
|
+
/** Normalize an arbitrary abort or provider failure without trusting coercion. */
|
|
551
|
+
function toError(error) {
|
|
552
|
+
try {
|
|
553
|
+
if (error instanceof Error) return error;
|
|
554
|
+
} catch {}
|
|
555
|
+
return new Error(errorMessage(error));
|
|
556
|
+
}
|
|
557
|
+
/** Render an arbitrary provider failure without letting coercion escape containment. */
|
|
558
|
+
function errorMessage(error) {
|
|
559
|
+
try {
|
|
560
|
+
return String(error);
|
|
561
|
+
} catch {
|
|
562
|
+
return "[unrenderable thrown value]";
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
//#endregion
|
|
566
|
+
export { BUNDLED_SKILL_RANK, SkillRegistry, SkillRegistry as default, escapeText, isModelInvocable, isSkillName, isUserInvocable, renderSkillContent };
|