@penclipai/adapter-utils 2026.401.0-canary.0
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/dist/billing.d.ts +2 -0
- package/dist/billing.d.ts.map +1 -0
- package/dist/billing.js +16 -0
- package/dist/billing.js.map +1 -0
- package/dist/billing.test.d.ts +2 -0
- package/dist/billing.test.d.ts.map +1 -0
- package/dist/billing.test.js +14 -0
- package/dist/billing.test.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/log-redaction.d.ts +9 -0
- package/dist/log-redaction.d.ts.map +1 -0
- package/dist/log-redaction.js +87 -0
- package/dist/log-redaction.js.map +1 -0
- package/dist/server-utils.d.ts +103 -0
- package/dist/server-utils.d.ts.map +1 -0
- package/dist/server-utils.js +744 -0
- package/dist/server-utils.js.map +1 -0
- package/dist/server-utils.test.d.ts +2 -0
- package/dist/server-utils.test.d.ts.map +1 -0
- package/dist/server-utils.test.js +32 -0
- package/dist/server-utils.test.js.map +1 -0
- package/dist/session-compaction.d.ts +25 -0
- package/dist/session-compaction.d.ts.map +1 -0
- package/dist/session-compaction.js +142 -0
- package/dist/session-compaction.js.map +1 -0
- package/dist/types.d.ts +335 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { constants as fsConstants, promises as fs } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const runningProcesses = new Map();
|
|
5
|
+
export const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
|
|
6
|
+
export const MAX_EXCERPT_BYTES = 32 * 1024;
|
|
7
|
+
const SENSITIVE_ENV_KEY = /(key|token|secret|password|passwd|authorization|cookie)/i;
|
|
8
|
+
const PAPERCLIP_SKILL_ROOT_RELATIVE_CANDIDATES = [
|
|
9
|
+
"../../skills",
|
|
10
|
+
"../../../../../skills",
|
|
11
|
+
];
|
|
12
|
+
const BUNDLED_PAPERCLIP_SKILL_KEY_PREFIX = "penclipai/paperclip-cn/";
|
|
13
|
+
const LEGACY_BUNDLED_PAPERCLIP_SKILL_KEY_PREFIXES = [
|
|
14
|
+
"paperclipai/paperclip/",
|
|
15
|
+
"penclipai/paperclip/",
|
|
16
|
+
];
|
|
17
|
+
function canonicalizeBundledPaperclipSkillKey(value) {
|
|
18
|
+
if (!value)
|
|
19
|
+
return null;
|
|
20
|
+
const trimmed = value.trim();
|
|
21
|
+
if (!trimmed)
|
|
22
|
+
return null;
|
|
23
|
+
if (trimmed.startsWith(BUNDLED_PAPERCLIP_SKILL_KEY_PREFIX))
|
|
24
|
+
return trimmed;
|
|
25
|
+
for (const legacyPrefix of LEGACY_BUNDLED_PAPERCLIP_SKILL_KEY_PREFIXES) {
|
|
26
|
+
if (trimmed.startsWith(legacyPrefix)) {
|
|
27
|
+
return `${BUNDLED_PAPERCLIP_SKILL_KEY_PREFIX}${trimmed.slice(legacyPrefix.length)}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return trimmed;
|
|
31
|
+
}
|
|
32
|
+
function normalizePathSlashes(value) {
|
|
33
|
+
return value.replaceAll("\\", "/");
|
|
34
|
+
}
|
|
35
|
+
function isMaintainerOnlySkillTarget(candidate) {
|
|
36
|
+
return normalizePathSlashes(candidate).includes("/.agents/skills/");
|
|
37
|
+
}
|
|
38
|
+
function skillLocationLabel(value) {
|
|
39
|
+
if (typeof value !== "string")
|
|
40
|
+
return null;
|
|
41
|
+
const trimmed = value.trim();
|
|
42
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
43
|
+
}
|
|
44
|
+
function buildManagedSkillOrigin(entry) {
|
|
45
|
+
if (entry.required) {
|
|
46
|
+
return {
|
|
47
|
+
origin: "paperclip_required",
|
|
48
|
+
originLabel: "Required by Paperclip",
|
|
49
|
+
readOnly: false,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
origin: "company_managed",
|
|
54
|
+
originLabel: "Managed by Paperclip",
|
|
55
|
+
readOnly: false,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function resolveInstalledEntryTarget(skillsHome, entryName, dirent, linkedPath) {
|
|
59
|
+
const fullPath = path.join(skillsHome, entryName);
|
|
60
|
+
if (dirent.isSymbolicLink()) {
|
|
61
|
+
return {
|
|
62
|
+
targetPath: linkedPath ? path.resolve(path.dirname(fullPath), linkedPath) : null,
|
|
63
|
+
kind: "symlink",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (dirent.isDirectory()) {
|
|
67
|
+
return { targetPath: fullPath, kind: "directory" };
|
|
68
|
+
}
|
|
69
|
+
return { targetPath: fullPath, kind: "file" };
|
|
70
|
+
}
|
|
71
|
+
export function parseObject(value) {
|
|
72
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
73
|
+
return {};
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
export function asString(value, fallback) {
|
|
78
|
+
return typeof value === "string" && value.length > 0 ? value : fallback;
|
|
79
|
+
}
|
|
80
|
+
export function asNumber(value, fallback) {
|
|
81
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
82
|
+
}
|
|
83
|
+
export function asBoolean(value, fallback) {
|
|
84
|
+
return typeof value === "boolean" ? value : fallback;
|
|
85
|
+
}
|
|
86
|
+
export function asStringArray(value) {
|
|
87
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
88
|
+
}
|
|
89
|
+
export function parseJson(value) {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(value);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export function appendWithCap(prev, chunk, cap = MAX_CAPTURE_BYTES) {
|
|
98
|
+
const combined = prev + chunk;
|
|
99
|
+
return combined.length > cap ? combined.slice(combined.length - cap) : combined;
|
|
100
|
+
}
|
|
101
|
+
export function resolvePathValue(obj, dottedPath) {
|
|
102
|
+
const parts = dottedPath.split(".");
|
|
103
|
+
let cursor = obj;
|
|
104
|
+
for (const part of parts) {
|
|
105
|
+
if (typeof cursor !== "object" || cursor === null || Array.isArray(cursor)) {
|
|
106
|
+
return "";
|
|
107
|
+
}
|
|
108
|
+
cursor = cursor[part];
|
|
109
|
+
}
|
|
110
|
+
if (cursor === null || cursor === undefined)
|
|
111
|
+
return "";
|
|
112
|
+
if (typeof cursor === "string")
|
|
113
|
+
return cursor;
|
|
114
|
+
if (typeof cursor === "number" || typeof cursor === "boolean")
|
|
115
|
+
return String(cursor);
|
|
116
|
+
try {
|
|
117
|
+
return JSON.stringify(cursor);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return "";
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export function renderTemplate(template, data) {
|
|
124
|
+
return template.replace(/{{\s*([a-zA-Z0-9_.-]+)\s*}}/g, (_, path) => resolvePathValue(data, path));
|
|
125
|
+
}
|
|
126
|
+
export function joinPromptSections(sections, separator = "\n\n") {
|
|
127
|
+
return sections
|
|
128
|
+
.map((value) => (typeof value === "string" ? value.trim() : ""))
|
|
129
|
+
.filter(Boolean)
|
|
130
|
+
.join(separator);
|
|
131
|
+
}
|
|
132
|
+
export function redactEnvForLogs(env) {
|
|
133
|
+
const redacted = {};
|
|
134
|
+
for (const [key, value] of Object.entries(env)) {
|
|
135
|
+
redacted[key] = SENSITIVE_ENV_KEY.test(key) ? "***REDACTED***" : value;
|
|
136
|
+
}
|
|
137
|
+
return redacted;
|
|
138
|
+
}
|
|
139
|
+
export function buildInvocationEnvForLogs(env, options = {}) {
|
|
140
|
+
const merged = { ...env };
|
|
141
|
+
const runtimeEnv = options.runtimeEnv ?? {};
|
|
142
|
+
for (const key of options.includeRuntimeKeys ?? []) {
|
|
143
|
+
if (key in merged)
|
|
144
|
+
continue;
|
|
145
|
+
const value = runtimeEnv[key];
|
|
146
|
+
if (typeof value !== "string" || value.length === 0)
|
|
147
|
+
continue;
|
|
148
|
+
merged[key] = value;
|
|
149
|
+
}
|
|
150
|
+
const resolvedCommand = options.resolvedCommand?.trim();
|
|
151
|
+
if (resolvedCommand) {
|
|
152
|
+
merged[options.resolvedCommandEnvKey ?? "PAPERCLIP_RESOLVED_COMMAND"] = resolvedCommand;
|
|
153
|
+
}
|
|
154
|
+
return redactEnvForLogs(merged);
|
|
155
|
+
}
|
|
156
|
+
export function buildPaperclipEnv(agent) {
|
|
157
|
+
const resolveHostForUrl = (rawHost) => {
|
|
158
|
+
const host = rawHost.trim();
|
|
159
|
+
if (!host || host === "0.0.0.0" || host === "::")
|
|
160
|
+
return "localhost";
|
|
161
|
+
if (host.includes(":") && !host.startsWith("[") && !host.endsWith("]"))
|
|
162
|
+
return `[${host}]`;
|
|
163
|
+
return host;
|
|
164
|
+
};
|
|
165
|
+
const vars = {
|
|
166
|
+
PAPERCLIP_AGENT_ID: agent.id,
|
|
167
|
+
PAPERCLIP_COMPANY_ID: agent.companyId,
|
|
168
|
+
};
|
|
169
|
+
const runtimeHost = resolveHostForUrl(process.env.PAPERCLIP_LISTEN_HOST ?? process.env.HOST ?? "localhost");
|
|
170
|
+
const runtimePort = process.env.PAPERCLIP_LISTEN_PORT ?? process.env.PORT ?? "3100";
|
|
171
|
+
const apiUrl = process.env.PAPERCLIP_API_URL ?? `http://${runtimeHost}:${runtimePort}`;
|
|
172
|
+
vars.PAPERCLIP_API_URL = apiUrl;
|
|
173
|
+
return vars;
|
|
174
|
+
}
|
|
175
|
+
export function defaultPathForPlatform() {
|
|
176
|
+
if (process.platform === "win32") {
|
|
177
|
+
return "C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\Wbem";
|
|
178
|
+
}
|
|
179
|
+
return "/usr/local/bin:/opt/homebrew/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin";
|
|
180
|
+
}
|
|
181
|
+
function windowsPathExts(env) {
|
|
182
|
+
return (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean);
|
|
183
|
+
}
|
|
184
|
+
async function pathExists(candidate) {
|
|
185
|
+
try {
|
|
186
|
+
await fs.access(candidate, process.platform === "win32" ? fsConstants.F_OK : fsConstants.X_OK);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function resolveCommandPath(command, cwd, env) {
|
|
194
|
+
const hasPathSeparator = command.includes("/") || command.includes("\\");
|
|
195
|
+
if (hasPathSeparator) {
|
|
196
|
+
const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
|
|
197
|
+
return (await pathExists(absolute)) ? absolute : null;
|
|
198
|
+
}
|
|
199
|
+
const pathValue = env.PATH ?? env.Path ?? "";
|
|
200
|
+
const delimiter = process.platform === "win32" ? ";" : ":";
|
|
201
|
+
const dirs = pathValue.split(delimiter).filter(Boolean);
|
|
202
|
+
const exts = process.platform === "win32" ? windowsPathExts(env) : [""];
|
|
203
|
+
const hasExtension = process.platform === "win32" && path.extname(command).length > 0;
|
|
204
|
+
for (const dir of dirs) {
|
|
205
|
+
const candidates = process.platform === "win32"
|
|
206
|
+
? hasExtension
|
|
207
|
+
? [path.join(dir, command)]
|
|
208
|
+
: [...exts.map((ext) => path.join(dir, `${command}${ext}`)), path.join(dir, command)]
|
|
209
|
+
: [path.join(dir, command)];
|
|
210
|
+
for (const candidate of candidates) {
|
|
211
|
+
if (await pathExists(candidate))
|
|
212
|
+
return candidate;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
export async function resolveCommandForLogs(command, cwd, env) {
|
|
218
|
+
return (await resolveCommandPath(command, cwd, env)) ?? command;
|
|
219
|
+
}
|
|
220
|
+
function quoteForCmd(arg) {
|
|
221
|
+
if (!arg.length)
|
|
222
|
+
return '""';
|
|
223
|
+
const escaped = arg.replace(/"/g, '""');
|
|
224
|
+
return /[\s"&<>|^()]/.test(escaped) ? `"${escaped}"` : escaped;
|
|
225
|
+
}
|
|
226
|
+
async function readShebangTokens(executable) {
|
|
227
|
+
try {
|
|
228
|
+
const handle = await fs.open(executable, "r");
|
|
229
|
+
try {
|
|
230
|
+
const buffer = Buffer.alloc(512);
|
|
231
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
232
|
+
const firstLine = buffer.toString("utf8", 0, bytesRead).split(/\r?\n/u, 1)[0]?.trim() ?? "";
|
|
233
|
+
if (!firstLine.startsWith("#!"))
|
|
234
|
+
return null;
|
|
235
|
+
const shebang = firstLine.slice(2).trim();
|
|
236
|
+
return shebang ? shebang.split(/\s+/u).filter(Boolean) : null;
|
|
237
|
+
}
|
|
238
|
+
finally {
|
|
239
|
+
await handle.close();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async function resolveShebangSpawnTarget(executable, cwd, env) {
|
|
247
|
+
const tokens = await readShebangTokens(executable);
|
|
248
|
+
if (!tokens || tokens.length === 0)
|
|
249
|
+
return null;
|
|
250
|
+
let [interpreter, ...interpreterArgs] = tokens;
|
|
251
|
+
const interpreterBase = path.posix.basename(interpreter).toLowerCase();
|
|
252
|
+
if (interpreterBase === "env" || interpreterBase === "env.exe") {
|
|
253
|
+
if (interpreterArgs[0] === "-S")
|
|
254
|
+
interpreterArgs = interpreterArgs.slice(1);
|
|
255
|
+
interpreter = interpreterArgs.shift() ?? "";
|
|
256
|
+
}
|
|
257
|
+
const normalizedBase = path.posix.basename(interpreter).toLowerCase().replace(/\.exe$/u, "");
|
|
258
|
+
const resolvedInterpreter = normalizedBase === "node"
|
|
259
|
+
? process.execPath
|
|
260
|
+
: normalizedBase === "sh"
|
|
261
|
+
? await resolveCommandPath("bash", cwd, env)
|
|
262
|
+
: normalizedBase
|
|
263
|
+
? await resolveCommandPath(normalizedBase, cwd, env)
|
|
264
|
+
: null;
|
|
265
|
+
if (!resolvedInterpreter)
|
|
266
|
+
return null;
|
|
267
|
+
return {
|
|
268
|
+
command: resolvedInterpreter,
|
|
269
|
+
args: [...interpreterArgs, executable],
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
async function resolveSpawnTarget(command, args, cwd, env) {
|
|
273
|
+
const resolved = await resolveCommandPath(command, cwd, env);
|
|
274
|
+
const executable = resolved ?? command;
|
|
275
|
+
if (process.platform !== "win32") {
|
|
276
|
+
return { command: executable, args };
|
|
277
|
+
}
|
|
278
|
+
if (/\.(cmd|bat)$/i.test(executable)) {
|
|
279
|
+
const shell = env.ComSpec || process.env.ComSpec || "cmd.exe";
|
|
280
|
+
const commandLine = [quoteForCmd(executable), ...args.map(quoteForCmd)].join(" ");
|
|
281
|
+
return {
|
|
282
|
+
command: shell,
|
|
283
|
+
args: ["/d", "/s", "/c", commandLine],
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
const shebangTarget = await resolveShebangSpawnTarget(executable, cwd, env);
|
|
287
|
+
if (shebangTarget) {
|
|
288
|
+
return {
|
|
289
|
+
command: shebangTarget.command,
|
|
290
|
+
args: [...shebangTarget.args, ...args],
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
return { command: executable, args };
|
|
294
|
+
}
|
|
295
|
+
export function ensurePathInEnv(env) {
|
|
296
|
+
if (typeof env.PATH === "string" && env.PATH.length > 0)
|
|
297
|
+
return env;
|
|
298
|
+
if (typeof env.Path === "string" && env.Path.length > 0)
|
|
299
|
+
return env;
|
|
300
|
+
return { ...env, PATH: defaultPathForPlatform() };
|
|
301
|
+
}
|
|
302
|
+
export async function ensureAbsoluteDirectory(cwd, opts = {}) {
|
|
303
|
+
if (!path.isAbsolute(cwd)) {
|
|
304
|
+
throw new Error(`Working directory must be an absolute path: "${cwd}"`);
|
|
305
|
+
}
|
|
306
|
+
const assertDirectory = async () => {
|
|
307
|
+
const stats = await fs.stat(cwd);
|
|
308
|
+
if (!stats.isDirectory()) {
|
|
309
|
+
throw new Error(`Working directory is not a directory: "${cwd}"`);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
try {
|
|
313
|
+
await assertDirectory();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
catch (err) {
|
|
317
|
+
const code = err.code;
|
|
318
|
+
if (!opts.createIfMissing || code !== "ENOENT") {
|
|
319
|
+
if (code === "ENOENT") {
|
|
320
|
+
throw new Error(`Working directory does not exist: "${cwd}"`);
|
|
321
|
+
}
|
|
322
|
+
throw err instanceof Error ? err : new Error(String(err));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
await fs.mkdir(cwd, { recursive: true });
|
|
327
|
+
await assertDirectory();
|
|
328
|
+
}
|
|
329
|
+
catch (err) {
|
|
330
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
331
|
+
throw new Error(`Could not create working directory "${cwd}": ${reason}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
export async function resolvePaperclipSkillsDir(moduleDir, additionalCandidates = []) {
|
|
335
|
+
const candidates = [
|
|
336
|
+
...PAPERCLIP_SKILL_ROOT_RELATIVE_CANDIDATES.map((relativePath) => path.resolve(moduleDir, relativePath)),
|
|
337
|
+
...additionalCandidates.map((candidate) => path.resolve(candidate)),
|
|
338
|
+
];
|
|
339
|
+
const seenRoots = new Set();
|
|
340
|
+
for (const root of candidates) {
|
|
341
|
+
if (seenRoots.has(root))
|
|
342
|
+
continue;
|
|
343
|
+
seenRoots.add(root);
|
|
344
|
+
const isDirectory = await fs.stat(root).then((stats) => stats.isDirectory()).catch(() => false);
|
|
345
|
+
if (isDirectory)
|
|
346
|
+
return root;
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
export async function listPaperclipSkillEntries(moduleDir, additionalCandidates = []) {
|
|
351
|
+
const root = await resolvePaperclipSkillsDir(moduleDir, additionalCandidates);
|
|
352
|
+
if (!root)
|
|
353
|
+
return [];
|
|
354
|
+
try {
|
|
355
|
+
const entries = await fs.readdir(root, { withFileTypes: true });
|
|
356
|
+
return entries
|
|
357
|
+
.filter((entry) => entry.isDirectory())
|
|
358
|
+
.map((entry) => ({
|
|
359
|
+
key: `${BUNDLED_PAPERCLIP_SKILL_KEY_PREFIX}${entry.name}`,
|
|
360
|
+
runtimeName: entry.name,
|
|
361
|
+
source: path.join(root, entry.name),
|
|
362
|
+
required: true,
|
|
363
|
+
requiredReason: "Bundled Paperclip skills are always available for local adapters.",
|
|
364
|
+
}));
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return [];
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
export async function readInstalledSkillTargets(skillsHome) {
|
|
371
|
+
const entries = await fs.readdir(skillsHome, { withFileTypes: true }).catch(() => []);
|
|
372
|
+
const out = new Map();
|
|
373
|
+
for (const entry of entries) {
|
|
374
|
+
const fullPath = path.join(skillsHome, entry.name);
|
|
375
|
+
const linkedPath = entry.isSymbolicLink() ? await fs.readlink(fullPath).catch(() => null) : null;
|
|
376
|
+
out.set(entry.name, resolveInstalledEntryTarget(skillsHome, entry.name, entry, linkedPath));
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
export function buildPersistentSkillSnapshot(options) {
|
|
381
|
+
const { adapterType, availableEntries, desiredSkills, installed, skillsHome, locationLabel, installedDetail, missingDetail, externalConflictDetail, externalDetail, } = options;
|
|
382
|
+
const canonicalDesiredSkills = Array.from(new Set(desiredSkills
|
|
383
|
+
.map((desiredSkill) => canonicalizeBundledPaperclipSkillKey(desiredSkill))
|
|
384
|
+
.filter((desiredSkill) => Boolean(desiredSkill))));
|
|
385
|
+
const availableByKey = new Map(availableEntries.flatMap((entry) => {
|
|
386
|
+
const canonicalKey = canonicalizeBundledPaperclipSkillKey(entry.key) ?? entry.key;
|
|
387
|
+
return canonicalKey === entry.key
|
|
388
|
+
? [[entry.key, entry]]
|
|
389
|
+
: [[entry.key, entry], [canonicalKey, entry]];
|
|
390
|
+
}));
|
|
391
|
+
const desiredSet = new Set(canonicalDesiredSkills);
|
|
392
|
+
const entries = [];
|
|
393
|
+
const warnings = [...(options.warnings ?? [])];
|
|
394
|
+
for (const available of availableEntries) {
|
|
395
|
+
const installedEntry = installed.get(available.runtimeName) ?? null;
|
|
396
|
+
const desired = desiredSet.has(canonicalizeBundledPaperclipSkillKey(available.key) ?? available.key);
|
|
397
|
+
let state = "available";
|
|
398
|
+
let managed = false;
|
|
399
|
+
let detail = null;
|
|
400
|
+
if (installedEntry?.targetPath === available.source) {
|
|
401
|
+
managed = true;
|
|
402
|
+
state = desired ? "installed" : "stale";
|
|
403
|
+
detail = installedDetail ?? null;
|
|
404
|
+
}
|
|
405
|
+
else if (installedEntry) {
|
|
406
|
+
state = "external";
|
|
407
|
+
detail = desired ? externalConflictDetail : externalDetail;
|
|
408
|
+
}
|
|
409
|
+
else if (desired) {
|
|
410
|
+
state = "missing";
|
|
411
|
+
detail = missingDetail;
|
|
412
|
+
}
|
|
413
|
+
entries.push({
|
|
414
|
+
key: available.key,
|
|
415
|
+
runtimeName: available.runtimeName,
|
|
416
|
+
desired,
|
|
417
|
+
managed,
|
|
418
|
+
state,
|
|
419
|
+
sourcePath: available.source,
|
|
420
|
+
targetPath: path.join(skillsHome, available.runtimeName),
|
|
421
|
+
detail,
|
|
422
|
+
required: Boolean(available.required),
|
|
423
|
+
requiredReason: available.requiredReason ?? null,
|
|
424
|
+
...buildManagedSkillOrigin(available),
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
for (const desiredSkill of canonicalDesiredSkills) {
|
|
428
|
+
if (availableByKey.has(desiredSkill))
|
|
429
|
+
continue;
|
|
430
|
+
warnings.push(`Desired skill "${desiredSkill}" is not available from the Paperclip skills directory.`);
|
|
431
|
+
entries.push({
|
|
432
|
+
key: desiredSkill,
|
|
433
|
+
runtimeName: null,
|
|
434
|
+
desired: true,
|
|
435
|
+
managed: true,
|
|
436
|
+
state: "missing",
|
|
437
|
+
sourcePath: null,
|
|
438
|
+
targetPath: null,
|
|
439
|
+
detail: "Paperclip cannot find this skill in the local runtime skills directory.",
|
|
440
|
+
origin: "external_unknown",
|
|
441
|
+
originLabel: "External or unavailable",
|
|
442
|
+
readOnly: false,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
for (const [name, installedEntry] of installed.entries()) {
|
|
446
|
+
if (availableEntries.some((entry) => entry.runtimeName === name))
|
|
447
|
+
continue;
|
|
448
|
+
entries.push({
|
|
449
|
+
key: name,
|
|
450
|
+
runtimeName: name,
|
|
451
|
+
desired: false,
|
|
452
|
+
managed: false,
|
|
453
|
+
state: "external",
|
|
454
|
+
origin: "user_installed",
|
|
455
|
+
originLabel: "User-installed",
|
|
456
|
+
locationLabel: skillLocationLabel(locationLabel),
|
|
457
|
+
readOnly: true,
|
|
458
|
+
sourcePath: null,
|
|
459
|
+
targetPath: installedEntry.targetPath ?? path.join(skillsHome, name),
|
|
460
|
+
detail: externalDetail,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
entries.sort((left, right) => left.key.localeCompare(right.key));
|
|
464
|
+
return {
|
|
465
|
+
adapterType,
|
|
466
|
+
supported: true,
|
|
467
|
+
mode: "persistent",
|
|
468
|
+
desiredSkills: canonicalDesiredSkills,
|
|
469
|
+
entries,
|
|
470
|
+
warnings,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function normalizeConfiguredPaperclipRuntimeSkills(value) {
|
|
474
|
+
if (!Array.isArray(value))
|
|
475
|
+
return [];
|
|
476
|
+
const out = [];
|
|
477
|
+
for (const rawEntry of value) {
|
|
478
|
+
const entry = parseObject(rawEntry);
|
|
479
|
+
const key = canonicalizeBundledPaperclipSkillKey(asString(entry.key, asString(entry.name, ""))) ?? "";
|
|
480
|
+
const runtimeName = asString(entry.runtimeName, asString(entry.name, "")).trim();
|
|
481
|
+
const source = asString(entry.source, "").trim();
|
|
482
|
+
if (!key || !runtimeName || !source)
|
|
483
|
+
continue;
|
|
484
|
+
out.push({
|
|
485
|
+
key,
|
|
486
|
+
runtimeName,
|
|
487
|
+
source,
|
|
488
|
+
required: asBoolean(entry.required, false),
|
|
489
|
+
requiredReason: typeof entry.requiredReason === "string" && entry.requiredReason.trim().length > 0
|
|
490
|
+
? entry.requiredReason.trim()
|
|
491
|
+
: null,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
return out;
|
|
495
|
+
}
|
|
496
|
+
export async function readPaperclipRuntimeSkillEntries(config, moduleDir, additionalCandidates = []) {
|
|
497
|
+
const configuredEntries = normalizeConfiguredPaperclipRuntimeSkills(config.paperclipRuntimeSkills);
|
|
498
|
+
if (configuredEntries.length > 0)
|
|
499
|
+
return configuredEntries;
|
|
500
|
+
return listPaperclipSkillEntries(moduleDir, additionalCandidates);
|
|
501
|
+
}
|
|
502
|
+
export async function readPaperclipSkillMarkdown(moduleDir, skillKey) {
|
|
503
|
+
const normalized = skillKey.trim().toLowerCase();
|
|
504
|
+
if (!normalized)
|
|
505
|
+
return null;
|
|
506
|
+
const entries = await listPaperclipSkillEntries(moduleDir);
|
|
507
|
+
const match = entries.find((entry) => entry.key === normalized);
|
|
508
|
+
if (!match)
|
|
509
|
+
return null;
|
|
510
|
+
try {
|
|
511
|
+
return await fs.readFile(path.join(match.source, "SKILL.md"), "utf8");
|
|
512
|
+
}
|
|
513
|
+
catch {
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
export function readPaperclipSkillSyncPreference(config) {
|
|
518
|
+
const raw = config.paperclipSkillSync;
|
|
519
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
520
|
+
return { explicit: false, desiredSkills: [] };
|
|
521
|
+
}
|
|
522
|
+
const syncConfig = raw;
|
|
523
|
+
const desiredValues = syncConfig.desiredSkills;
|
|
524
|
+
const desired = Array.isArray(desiredValues)
|
|
525
|
+
? desiredValues
|
|
526
|
+
.filter((value) => typeof value === "string")
|
|
527
|
+
.map((value) => value.trim())
|
|
528
|
+
.filter(Boolean)
|
|
529
|
+
: [];
|
|
530
|
+
return {
|
|
531
|
+
explicit: Object.prototype.hasOwnProperty.call(raw, "desiredSkills"),
|
|
532
|
+
desiredSkills: Array.from(new Set(desired)),
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function canonicalizeDesiredPaperclipSkillReference(reference, availableEntries) {
|
|
536
|
+
const normalizedReference = reference.trim().toLowerCase();
|
|
537
|
+
if (!normalizedReference)
|
|
538
|
+
return "";
|
|
539
|
+
const canonicalBundledReference = canonicalizeBundledPaperclipSkillKey(normalizedReference);
|
|
540
|
+
if (canonicalBundledReference) {
|
|
541
|
+
const exactCanonicalKey = availableEntries.find((entry) => entry.key.trim().toLowerCase() === canonicalBundledReference);
|
|
542
|
+
if (exactCanonicalKey)
|
|
543
|
+
return exactCanonicalKey.key;
|
|
544
|
+
}
|
|
545
|
+
const exactKey = availableEntries.find((entry) => entry.key.trim().toLowerCase() === normalizedReference);
|
|
546
|
+
if (exactKey)
|
|
547
|
+
return exactKey.key;
|
|
548
|
+
const byRuntimeName = availableEntries.filter((entry) => typeof entry.runtimeName === "string" && entry.runtimeName.trim().toLowerCase() === normalizedReference);
|
|
549
|
+
if (byRuntimeName.length === 1)
|
|
550
|
+
return byRuntimeName[0].key;
|
|
551
|
+
const slugMatches = availableEntries.filter((entry) => entry.key.trim().toLowerCase().split("/").pop() === normalizedReference);
|
|
552
|
+
if (slugMatches.length === 1)
|
|
553
|
+
return slugMatches[0].key;
|
|
554
|
+
return canonicalBundledReference ?? normalizedReference;
|
|
555
|
+
}
|
|
556
|
+
export function resolvePaperclipDesiredSkillNames(config, availableEntries) {
|
|
557
|
+
const preference = readPaperclipSkillSyncPreference(config);
|
|
558
|
+
const requiredSkills = availableEntries
|
|
559
|
+
.filter((entry) => entry.required)
|
|
560
|
+
.map((entry) => entry.key);
|
|
561
|
+
if (!preference.explicit) {
|
|
562
|
+
return Array.from(new Set(requiredSkills));
|
|
563
|
+
}
|
|
564
|
+
const desiredSkills = preference.desiredSkills
|
|
565
|
+
.map((reference) => canonicalizeDesiredPaperclipSkillReference(reference, availableEntries))
|
|
566
|
+
.filter(Boolean);
|
|
567
|
+
return Array.from(new Set([...requiredSkills, ...desiredSkills]));
|
|
568
|
+
}
|
|
569
|
+
export function writePaperclipSkillSyncPreference(config, desiredSkills) {
|
|
570
|
+
const next = { ...config };
|
|
571
|
+
const raw = next.paperclipSkillSync;
|
|
572
|
+
const current = typeof raw === "object" && raw !== null && !Array.isArray(raw)
|
|
573
|
+
? { ...raw }
|
|
574
|
+
: {};
|
|
575
|
+
current.desiredSkills = Array.from(new Set(desiredSkills
|
|
576
|
+
.map((value) => value.trim())
|
|
577
|
+
.filter(Boolean)));
|
|
578
|
+
next.paperclipSkillSync = current;
|
|
579
|
+
return next;
|
|
580
|
+
}
|
|
581
|
+
export async function ensurePaperclipSkillSymlink(source, target, linkSkill = (linkSource, linkTarget) => fs.symlink(linkSource, linkTarget)) {
|
|
582
|
+
const existing = await fs.lstat(target).catch(() => null);
|
|
583
|
+
if (!existing) {
|
|
584
|
+
await linkSkill(source, target);
|
|
585
|
+
return "created";
|
|
586
|
+
}
|
|
587
|
+
if (!existing.isSymbolicLink()) {
|
|
588
|
+
return "skipped";
|
|
589
|
+
}
|
|
590
|
+
const linkedPath = await fs.readlink(target).catch(() => null);
|
|
591
|
+
if (!linkedPath)
|
|
592
|
+
return "skipped";
|
|
593
|
+
const resolvedLinkedPath = path.resolve(path.dirname(target), linkedPath);
|
|
594
|
+
if (resolvedLinkedPath === source) {
|
|
595
|
+
return "skipped";
|
|
596
|
+
}
|
|
597
|
+
const linkedPathExists = await fs.stat(resolvedLinkedPath).then(() => true).catch(() => false);
|
|
598
|
+
if (linkedPathExists) {
|
|
599
|
+
return "skipped";
|
|
600
|
+
}
|
|
601
|
+
await fs.unlink(target);
|
|
602
|
+
await linkSkill(source, target);
|
|
603
|
+
return "repaired";
|
|
604
|
+
}
|
|
605
|
+
export async function removeMaintainerOnlySkillSymlinks(skillsHome, allowedSkillNames) {
|
|
606
|
+
const allowed = new Set(Array.from(allowedSkillNames));
|
|
607
|
+
try {
|
|
608
|
+
const entries = await fs.readdir(skillsHome, { withFileTypes: true });
|
|
609
|
+
const removed = [];
|
|
610
|
+
for (const entry of entries) {
|
|
611
|
+
if (allowed.has(entry.name))
|
|
612
|
+
continue;
|
|
613
|
+
const target = path.join(skillsHome, entry.name);
|
|
614
|
+
const existing = await fs.lstat(target).catch(() => null);
|
|
615
|
+
if (!existing?.isSymbolicLink())
|
|
616
|
+
continue;
|
|
617
|
+
const linkedPath = await fs.readlink(target).catch(() => null);
|
|
618
|
+
if (!linkedPath)
|
|
619
|
+
continue;
|
|
620
|
+
const resolvedLinkedPath = path.isAbsolute(linkedPath)
|
|
621
|
+
? linkedPath
|
|
622
|
+
: path.resolve(path.dirname(target), linkedPath);
|
|
623
|
+
if (!isMaintainerOnlySkillTarget(linkedPath) &&
|
|
624
|
+
!isMaintainerOnlySkillTarget(resolvedLinkedPath)) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
await fs.unlink(target);
|
|
628
|
+
removed.push(entry.name);
|
|
629
|
+
}
|
|
630
|
+
return removed;
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
return [];
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
export async function ensureCommandResolvable(command, cwd, env) {
|
|
637
|
+
const resolved = await resolveCommandPath(command, cwd, env);
|
|
638
|
+
if (resolved)
|
|
639
|
+
return;
|
|
640
|
+
if (command.includes("/") || command.includes("\\")) {
|
|
641
|
+
const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
|
|
642
|
+
throw new Error(`Command is not executable: "${command}" (resolved: "${absolute}")`);
|
|
643
|
+
}
|
|
644
|
+
throw new Error(`Command not found in PATH: "${command}"`);
|
|
645
|
+
}
|
|
646
|
+
export async function runChildProcess(runId, command, args, opts) {
|
|
647
|
+
const onLogError = opts.onLogError ?? ((err, id, msg) => console.warn({ err, runId: id }, msg));
|
|
648
|
+
return new Promise((resolve, reject) => {
|
|
649
|
+
const rawMerged = { ...process.env, ...opts.env };
|
|
650
|
+
// Strip Claude Code nesting-guard env vars so spawned `claude` processes
|
|
651
|
+
// don't refuse to start with "cannot be launched inside another session".
|
|
652
|
+
// These vars leak in when the Paperclip server itself is started from
|
|
653
|
+
// within a Claude Code session (e.g. `npx penclip run` in a terminal
|
|
654
|
+
// owned by Claude Code) or when cron inherits a contaminated shell env.
|
|
655
|
+
const CLAUDE_CODE_NESTING_VARS = [
|
|
656
|
+
"CLAUDECODE",
|
|
657
|
+
"CLAUDE_CODE_ENTRYPOINT",
|
|
658
|
+
"CLAUDE_CODE_SESSION",
|
|
659
|
+
"CLAUDE_CODE_PARENT_SESSION",
|
|
660
|
+
];
|
|
661
|
+
for (const key of CLAUDE_CODE_NESTING_VARS) {
|
|
662
|
+
delete rawMerged[key];
|
|
663
|
+
}
|
|
664
|
+
const mergedEnv = ensurePathInEnv(rawMerged);
|
|
665
|
+
void resolveSpawnTarget(command, args, opts.cwd, mergedEnv)
|
|
666
|
+
.then((target) => {
|
|
667
|
+
const child = spawn(target.command, target.args, {
|
|
668
|
+
cwd: opts.cwd,
|
|
669
|
+
env: mergedEnv,
|
|
670
|
+
shell: false,
|
|
671
|
+
stdio: [opts.stdin != null ? "pipe" : "ignore", "pipe", "pipe"],
|
|
672
|
+
});
|
|
673
|
+
const startedAt = new Date().toISOString();
|
|
674
|
+
if (opts.stdin != null && child.stdin) {
|
|
675
|
+
child.stdin.write(opts.stdin);
|
|
676
|
+
child.stdin.end();
|
|
677
|
+
}
|
|
678
|
+
if (typeof child.pid === "number" && child.pid > 0 && opts.onSpawn) {
|
|
679
|
+
void opts.onSpawn({ pid: child.pid, startedAt }).catch((err) => {
|
|
680
|
+
onLogError(err, runId, "failed to record child process metadata");
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
runningProcesses.set(runId, { child, graceSec: opts.graceSec });
|
|
684
|
+
let timedOut = false;
|
|
685
|
+
let stdout = "";
|
|
686
|
+
let stderr = "";
|
|
687
|
+
let logChain = Promise.resolve();
|
|
688
|
+
const timeout = opts.timeoutSec > 0
|
|
689
|
+
? setTimeout(() => {
|
|
690
|
+
timedOut = true;
|
|
691
|
+
child.kill("SIGTERM");
|
|
692
|
+
setTimeout(() => {
|
|
693
|
+
if (!child.killed) {
|
|
694
|
+
child.kill("SIGKILL");
|
|
695
|
+
}
|
|
696
|
+
}, Math.max(1, opts.graceSec) * 1000);
|
|
697
|
+
}, opts.timeoutSec * 1000)
|
|
698
|
+
: null;
|
|
699
|
+
child.stdout?.on("data", (chunk) => {
|
|
700
|
+
const text = String(chunk);
|
|
701
|
+
stdout = appendWithCap(stdout, text);
|
|
702
|
+
logChain = logChain
|
|
703
|
+
.then(() => opts.onLog("stdout", text))
|
|
704
|
+
.catch((err) => onLogError(err, runId, "failed to append stdout log chunk"));
|
|
705
|
+
});
|
|
706
|
+
child.stderr?.on("data", (chunk) => {
|
|
707
|
+
const text = String(chunk);
|
|
708
|
+
stderr = appendWithCap(stderr, text);
|
|
709
|
+
logChain = logChain
|
|
710
|
+
.then(() => opts.onLog("stderr", text))
|
|
711
|
+
.catch((err) => onLogError(err, runId, "failed to append stderr log chunk"));
|
|
712
|
+
});
|
|
713
|
+
child.on("error", (err) => {
|
|
714
|
+
if (timeout)
|
|
715
|
+
clearTimeout(timeout);
|
|
716
|
+
runningProcesses.delete(runId);
|
|
717
|
+
const errno = err.code;
|
|
718
|
+
const pathValue = mergedEnv.PATH ?? mergedEnv.Path ?? "";
|
|
719
|
+
const msg = errno === "ENOENT"
|
|
720
|
+
? `Failed to start command "${command}" in "${opts.cwd}". Verify adapter command, working directory, and PATH (${pathValue}).`
|
|
721
|
+
: `Failed to start command "${command}" in "${opts.cwd}": ${err.message}`;
|
|
722
|
+
reject(new Error(msg));
|
|
723
|
+
});
|
|
724
|
+
child.on("close", (code, signal) => {
|
|
725
|
+
if (timeout)
|
|
726
|
+
clearTimeout(timeout);
|
|
727
|
+
runningProcesses.delete(runId);
|
|
728
|
+
void logChain.finally(() => {
|
|
729
|
+
resolve({
|
|
730
|
+
exitCode: code,
|
|
731
|
+
signal,
|
|
732
|
+
timedOut,
|
|
733
|
+
stdout,
|
|
734
|
+
stderr,
|
|
735
|
+
pid: child.pid ?? null,
|
|
736
|
+
startedAt,
|
|
737
|
+
});
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
})
|
|
741
|
+
.catch(reject);
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
//# sourceMappingURL=server-utils.js.map
|