openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
227 lines (226 loc) • 7.61 kB
JavaScript
import { r as resolveOsHomeDir } from "./home-dir-zhqynwRJ.mjs";
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs";
import { b as uniqueStrings, m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { t as pruneMapToMaxSize } from "./map-size-BBr20tbc.mjs";
import { c as readFileDescriptorBoundedSync, o as openRootFileSync, r as isRootFileMissingFailure } from "./boundary-file-read-BfaFLDqt.mjs";
import { r as resolvePathViaExistingAncestorSync, s as safeRealpathSync } from "./boundary-path-C2gXEiXe.mjs";
import { t as resolveConfigDir } from "./config-dir-DMqvtq2W.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import { t as CONFIG_DIR } from "./utils-C8YECn2Q.mjs";
import { r as resolveActivePluginInstallRoots, t as hasActivePluginInstallRoots } from "./install-root-context-CeM6pu6m.mjs";
import { t as parseSkillFrontmatter } from "./frontmatter-CetkUJdW.mjs";
import { n as resolveWorkshopSkillsDir } from "./skills-root-DFNOcDAR.mjs";
import { t as materializeSkill } from "./skill-materializer-CtM9HNkG.mjs";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
//#region src/skills/loading/symlink-targets.ts
function resolveAllowedSkillSymlinkTargetRealPaths(e) {
const t = normalizeTrimmedStringList(e?.skills?.load?.allowSymlinkTargets).map((e) => tryRealpath(resolveUserPath(e))).filter((e) => Boolean(e));
return uniqueStrings(t);
}
function tryRealpath(e) {
try {
return safeRealpathSync(resolvePathViaExistingAncestorSync(e));
} catch {
return null;
}
}
//#endregion
//#region src/skills/loading/skill-paths.ts
function resolvePluginSkillsDir() {
const e = hasActivePluginInstallRoots() ? resolveActivePluginInstallRoots().stateDir : CONFIG_DIR;
return path.join(e, "plugin-skills");
}
/** Workspace sync excludes repository internals and installed dependencies at every depth. */
function shouldSyncSkillPath(e) {
const t = path.basename(e);
return t !== ".git" && t !== "node_modules";
}
function resolveSkillsUserHomeDir() {
return resolveOsHomeDir(process.env, os.homedir);
}
function resolveNativeUserHomeDir() {
try {
return path.resolve(os.homedir());
} catch {
return;
}
}
function resolveCompactHomePrefixes() {
const e = [resolveSkillsUserHomeDir(), resolveNativeUserHomeDir()].filter((e) => Boolean(e)).map((e) => path.resolve(e));
const t = e.map((e) => tryRealpath(e)).filter((e) => Boolean(e));
return uniqueStrings([...e, ...t]).toSorted((e, t) => t.length - e.length).flatMap(compactHomePrefixesForHome);
}
/** Compact prompt-facing skill paths while preserving managed paths that `~` cannot reach. */
function compactPromptSkills(e, t = {}) {
const n = resolveCompactHomePrefixes();
if (n.length === 0) return e;
const r = resolvePreservedPromptSkillPathRoots(t);
const i = resolvePromptTildeRoots();
return e.map((e) => ({
...e,
filePath: shouldPreservePromptSkillPath(e.filePath, r, i) ? e.filePath : compactHomePath(e.filePath, n)
}));
}
function resolvePreservedPromptSkillPathRoots(e) {
const t = resolveConfigDir();
const n = [
...e.config && e.agentId ? [resolveWorkshopSkillsDir(e.config, e.agentId)] : [],
path.resolve(t, "skills"),
path.resolve(t, "plugin-skills")
];
const r = n.map((e) => tryRealpath(e)).filter((e) => Boolean(e));
return uniqueStrings([...n, ...r]);
}
function resolvePromptTildeRoots() {
const e = resolveNativeUserHomeDir();
if (!e) return [];
const t = path.resolve(e);
if (isContainerStateHomeWherePromptTildeEscapes(t)) return [];
const n = tryRealpath(t);
return uniqueStrings([t, ...n ? [n] : []]);
}
function isContainerStateHomeWherePromptTildeEscapes(e) {
const t = path.resolve(resolveConfigDir());
return e === "/data" && (t === "/data/.openclaw" || isPathInside("/data/.openclaw", t));
}
function shouldPreservePromptSkillPath(e, t, n) {
const r = path.resolve(e);
if (!t.some((e) => isPathInside(e, r))) return false;
return !n.some((e) => isPathInside(e, r));
}
function compactHomePath(e, t) {
for (const n of t) if (e.startsWith(n)) {
const t = e.slice(n.length);
return "~/" + (n.includes("\\") ? t.replace(/\\/g, "/") : t);
}
return e;
}
function compactHomePrefixesForHome(e) {
const t = [e.endsWith(path.sep) ? e : e + path.sep];
if (e.includes("\\") && !e.endsWith("\\")) t.push(e + "\\");
return t;
}
function compactSkillPath(e) {
return compactHomePath(e, resolveCompactHomePrefixes());
}
//#endregion
//#region src/skills/loading/local-loader.ts
const MAX_CACHED_SKILL_CONTENT_CHARS = 16384;
const MAX_CACHED_SKILL_CONTENTS = 256;
const localSkillContentCache = /* @__PURE__ */ new Map();
function readSkillFileSync(e) {
const t = openRootFileSync({
absolutePath: e.filePath,
rootPath: e.rootRealPath,
rootRealPath: e.rootRealPath,
boundaryLabel: "skill root",
rejectSymlinks: false,
rejectHardlinks: e.rejectHardlinks !== false
});
if (!t.ok) {
if (!isRootFileMissingFailure(t)) {
const n = t.error instanceof Error ? t.error.message : `failed to open skill file (${t.reason})`;
e.onDiagnostic?.({
kind: t.reason === "validation" ? "invalid" : "read",
path: e.filePath,
message: n
});
}
return null;
}
try {
return e.maxBytes === void 0 ? fs.readFileSync(t.fd, "utf8") : readFileDescriptorBoundedSync(t.fd, e.maxBytes).toString("utf8");
} catch (t) {
const n = t instanceof Error ? t.message : "failed to read skill file";
e.onDiagnostic?.({
kind: t instanceof RangeError ? "invalid" : "read",
path: e.filePath,
message: n
});
return null;
} finally {
fs.closeSync(t.fd);
}
}
function loadSingleSkillDirectory(e) {
const t = path.join(e.skillDir, "SKILL.md");
const n = readSkillFileSync({
rootRealPath: e.rootRealPath,
filePath: t,
maxBytes: e.maxBytes,
rejectHardlinks: e.rejectHardlinks,
onDiagnostic: e.onDiagnostic
});
if (n === null) return null;
const r = path.basename(e.skillDir).trim();
const i = path.resolve(t);
const a = path.resolve(e.skillDir);
let o = localSkillContentCache.get(n);
if (o && !o.frontmatter.name?.trim() && o.skill.name !== r) o = void 0;
if (!o) {
let s;
try {
s = parseSkillFrontmatter(n);
} catch (n) {
const r = n instanceof Error ? n.message : "failed to parse skill frontmatter";
e.onDiagnostic?.({
kind: "invalid",
path: t,
message: r
});
return null;
}
const c = s.name?.trim() || r;
const l = s.description?.trim();
if (!c || !l) {
e.onDiagnostic?.({
kind: "invalid",
path: t,
message: !c ? "name is required" : "description is required"
});
return null;
}
o = {
skill: materializeSkill({
content: n,
frontmatter: s,
name: c,
description: l,
filePath: i,
baseDir: a,
source: e.source,
sourceOptions: {
source: e.source,
scope: "project",
origin: "top-level"
}
}),
frontmatter: s
};
}
if (n.length <= MAX_CACHED_SKILL_CONTENT_CHARS) {
localSkillContentCache.delete(n);
localSkillContentCache.set(n, o);
pruneMapToMaxSize(localSkillContentCache, MAX_CACHED_SKILL_CONTENTS);
}
return {
skill: {
...o.skill,
filePath: i,
baseDir: a,
source: e.source,
sourceInfo: {
...o.skill.sourceInfo,
path: i,
baseDir: a,
source: e.source
}
},
frontmatter: { ...o.frontmatter },
content: n
};
}
//#endregion
export { resolveSkillsUserHomeDir as a, tryRealpath as c, resolvePluginSkillsDir as i, compactPromptSkills as n, shouldSyncSkillPath as o, compactSkillPath as r, resolveAllowedSkillSymlinkTargetRealPaths as s, loadSingleSkillDirectory as t };