-
-
Notifications
You must be signed in to change notification settings - Fork 80.9k
Expand file tree
/
Copy pathskills-status.ts
More file actions
306 lines (283 loc) · 9.34 KB
/
Copy pathskills-status.ts
File metadata and controls
306 lines (283 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import path from "node:path";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { evaluateEntryRequirementsForCurrentPlatform } from "../shared/entry-status.js";
import type { RequirementConfigCheck, Requirements } from "../shared/requirements.js";
import { CONFIG_DIR } from "../utils.js";
import {
hasBinary,
isBundledSkillAllowed,
isConfigPathTruthy,
loadWorkspaceSkillEntries,
resolveBundledAllowlist,
resolveSkillConfig,
resolveSkillsInstallPreferences,
type SkillEntry,
type SkillEligibilityContext,
type SkillInstallSpec,
type SkillsInstallPreferences,
} from "./skills.js";
import { resolveEffectiveAgentSkillFilter } from "./skills/agent-filter.js";
import { resolveBundledSkillsContext } from "./skills/bundled-context.js";
import { resolveSkillSource } from "./skills/source.js";
export type SkillStatusConfigCheck = RequirementConfigCheck;
export type SkillInstallOption = {
id: string;
kind: SkillInstallSpec["kind"];
label: string;
bins: string[];
};
export type SkillStatusEntry = {
name: string;
description: string;
source: string;
bundled: boolean;
filePath: string;
baseDir: string;
skillKey: string;
primaryEnv?: string;
emoji?: string;
homepage?: string;
always: boolean;
disabled: boolean;
blockedByAllowlist: boolean;
blockedByAgentFilter: boolean;
eligible: boolean;
modelVisible: boolean;
userInvocable: boolean;
commandVisible: boolean;
requirements: Requirements;
missing: Requirements;
configChecks: SkillStatusConfigCheck[];
install: SkillInstallOption[];
};
export type SkillStatusReport = {
workspaceDir: string;
managedSkillsDir: string;
agentId?: string;
agentSkillFilter?: string[];
skills: SkillStatusEntry[];
};
function resolveSkillKey(entry: SkillEntry): string {
return entry.metadata?.skillKey ?? entry.skill.name;
}
function selectPreferredInstallSpec(
install: SkillInstallSpec[],
prefs: SkillsInstallPreferences,
): { spec: SkillInstallSpec; index: number } | undefined {
if (install.length === 0) {
return undefined;
}
const indexed = install.map((spec, index) => ({ spec, index }));
const findKind = (kind: SkillInstallSpec["kind"]) =>
indexed.find((item) => item.spec.kind === kind);
const brewSpec = findKind("brew");
const nodeSpec = findKind("node");
const goSpec = findKind("go");
const uvSpec = findKind("uv");
const downloadSpec = findKind("download");
const brewAvailable = hasBinary("brew");
// Table-driven preference chain; first match wins.
const pickers: Array<() => { spec: SkillInstallSpec; index: number } | undefined> = [
() => (prefs.preferBrew && brewAvailable ? brewSpec : undefined),
() => uvSpec,
() => nodeSpec,
// Only prefer brew when available to avoid guaranteed failure on Linux/Docker.
() => (brewAvailable ? brewSpec : undefined),
() => goSpec,
// Prefer download over an unavailable brew spec.
() => downloadSpec,
// Last resort: surface descriptive brew-missing error instead of "no installer found".
() => brewSpec,
() => indexed[0],
];
for (const pick of pickers) {
const selected = pick();
if (selected) {
return selected;
}
}
return undefined;
}
function normalizeInstallOptions(
entry: SkillEntry,
prefs: SkillsInstallPreferences,
): SkillInstallOption[] {
// If the skill is explicitly OS-scoped, don't surface install actions on unsupported platforms.
// (Installers run locally; remote OS eligibility is handled separately.)
const requiredOs = entry.metadata?.os ?? [];
if (requiredOs.length > 0 && !requiredOs.includes(process.platform)) {
return [];
}
const install = entry.metadata?.install ?? [];
if (install.length === 0) {
return [];
}
const platform = process.platform;
const filtered = install.filter((spec) => {
const osList = spec.os ?? [];
return osList.length === 0 || osList.includes(platform);
});
if (filtered.length === 0) {
return [];
}
const toOption = (spec: SkillInstallSpec, index: number): SkillInstallOption => {
const id = (spec.id ?? `${spec.kind}-${index}`).trim();
const bins = spec.bins ?? [];
let label = (spec.label ?? "").trim();
if (spec.kind === "node" && spec.package) {
label = `Install ${spec.package} (${prefs.nodeManager})`;
}
if (!label) {
if (spec.kind === "brew" && spec.formula) {
label = `Install ${spec.formula} (brew)`;
} else if (spec.kind === "node" && spec.package) {
label = `Install ${spec.package} (${prefs.nodeManager})`;
} else if (spec.kind === "go" && spec.module) {
label = `Install ${spec.module} (go)`;
} else if (spec.kind === "uv" && spec.package) {
label = `Install ${spec.package} (uv)`;
} else if (spec.kind === "download" && spec.url) {
const url = spec.url.trim();
const last = url.split("/").pop();
label = `Download ${last && last.length > 0 ? last : url}`;
} else {
label = "Run installer";
}
}
return { id, kind: spec.kind, label, bins };
};
const allDownloads = filtered.every((spec) => spec.kind === "download");
if (allDownloads) {
return filtered.map((spec, index) => toOption(spec, index));
}
const preferred = selectPreferredInstallSpec(filtered, prefs);
if (!preferred) {
return [];
}
return [toOption(preferred.spec, preferred.index)];
}
function isSkillVisibleInAvailableSkillsPrompt(entry: SkillEntry): boolean {
if (entry.exposure) {
return (
entry.exposure.includeInAvailableSkillsPrompt ||
!("includeInAvailableSkillsPrompt" in entry.exposure)
);
}
if (entry.invocation) {
return !entry.invocation.disableModelInvocation;
}
return !entry.skill.disableModelInvocation;
}
function isSkillUserInvocable(entry: SkillEntry): boolean {
if (entry.exposure) {
return entry.exposure.userInvocable || !("userInvocable" in entry.exposure);
}
if (entry.invocation) {
return entry.invocation.userInvocable || !("userInvocable" in entry.invocation);
}
return true;
}
function buildSkillStatus(
entry: SkillEntry,
config?: OpenClawConfig,
prefs?: SkillsInstallPreferences,
eligibility?: SkillEligibilityContext,
bundledNames?: Set<string>,
agentSkillFilter?: string[],
): SkillStatusEntry {
const skillKey = resolveSkillKey(entry);
const skillConfig = resolveSkillConfig(config, skillKey);
const disabled = skillConfig?.enabled === false;
const allowBundled = resolveBundledAllowlist(config);
const blockedByAllowlist = !isBundledSkillAllowed(entry, allowBundled);
const blockedByAgentFilter =
agentSkillFilter !== undefined && !agentSkillFilter.includes(entry.skill.name);
const always = entry.metadata?.always === true;
const isEnvSatisfied = (envName: string) =>
Boolean(
process.env[envName] ||
skillConfig?.env?.[envName] ||
(skillConfig?.apiKey && entry.metadata?.primaryEnv === envName),
);
const isConfigSatisfied = (pathStr: string) => isConfigPathTruthy(config, pathStr);
const skillSource = resolveSkillSource(entry.skill);
const bundled =
skillSource === "openclaw-bundled" ||
(skillSource === "unknown" && bundledNames?.has(entry.skill.name) === true);
const { emoji, homepage, required, missing, requirementsSatisfied, configChecks } =
evaluateEntryRequirementsForCurrentPlatform({
always,
entry,
hasLocalBin: hasBinary,
remote: eligibility?.remote,
isEnvSatisfied,
isConfigSatisfied,
});
const eligible = !disabled && !blockedByAllowlist && requirementsSatisfied;
const availableToAgent = eligible && !blockedByAgentFilter;
const userInvocable = isSkillUserInvocable(entry);
return {
name: entry.skill.name,
description: entry.skill.description,
source: skillSource,
bundled,
filePath: entry.skill.filePath,
baseDir: entry.skill.baseDir,
skillKey,
primaryEnv: entry.metadata?.primaryEnv,
emoji,
homepage,
always,
disabled,
blockedByAllowlist,
blockedByAgentFilter,
eligible,
modelVisible: availableToAgent && isSkillVisibleInAvailableSkillsPrompt(entry),
userInvocable,
commandVisible: availableToAgent && userInvocable,
requirements: required,
missing,
configChecks,
install: normalizeInstallOptions(entry, prefs ?? resolveSkillsInstallPreferences(config)),
};
}
export function buildWorkspaceSkillStatus(
workspaceDir: string,
opts?: {
config?: OpenClawConfig;
managedSkillsDir?: string;
entries?: SkillEntry[];
eligibility?: SkillEligibilityContext;
agentId?: string;
},
): SkillStatusReport {
const managedSkillsDir = opts?.managedSkillsDir ?? path.join(CONFIG_DIR, "skills");
const bundledContext = resolveBundledSkillsContext();
const agentSkillFilter = opts?.agentId
? resolveEffectiveAgentSkillFilter(opts.config, opts.agentId)
: undefined;
const skillEntries =
opts?.entries ??
loadWorkspaceSkillEntries(workspaceDir, {
config: opts?.config,
managedSkillsDir,
bundledSkillsDir: bundledContext.dir,
});
const prefs = resolveSkillsInstallPreferences(opts?.config);
return {
workspaceDir,
managedSkillsDir,
agentId: opts?.agentId,
agentSkillFilter,
skills: skillEntries.map((entry) =>
buildSkillStatus(
entry,
opts?.config,
prefs,
opts?.eligibility,
bundledContext.names,
agentSkillFilter,
),
),
};
}