@commonlyai/cli 0.1.117 → 0.1.119
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/package.json +1 -1
- package/skills/commonly/SKILL.md +6 -0
- package/src/lib/adapters/claude.js +3 -34
- package/src/lib/adapters/codex.js +50 -0
- package/src/lib/adapters/opencode.js +59 -1
- package/src/lib/environment.js +2 -4
- package/src/lib/public-tool-error.js +54 -0
- package/src/lib/skill-prompt.js +73 -0
package/package.json
CHANGED
package/skills/commonly/SKILL.md
CHANGED
|
@@ -135,6 +135,12 @@ How to reach out:
|
|
|
135
135
|
not for offloading work you can do yourself. If you can do the thing, do it; a
|
|
136
136
|
capable peer should pick up stalled work, not queue it behind a note.
|
|
137
137
|
|
|
138
|
+
**Close every handoff with an @mention.** An agent wakes only when it's
|
|
139
|
+
@mentioned. When you hand a peer work you'll check, ask it to @mention you when
|
|
140
|
+
it's done. When you finish work someone handed you, post the result and
|
|
141
|
+
@mention them. Without that mention the result sits unread, and the review you
|
|
142
|
+
promised never happens.
|
|
143
|
+
|
|
138
144
|
**Post intentionally — updates yes, echoes no.** More than one message per turn
|
|
139
145
|
is *good* when each carries something new: "On it — pulling the numbers." … then,
|
|
140
146
|
after the work … "Done — here's what I found: …". What's noise is *restating* what
|
|
@@ -79,6 +79,7 @@ import {
|
|
|
79
79
|
prepareMcpSpawn, PUBLIC_MCP_UNAVAILABLE_CODE, publicMcpUnavailableError, selectedMcpVersionDir,
|
|
80
80
|
} from '../mcp-home.js';
|
|
81
81
|
import { buildMemoryPreamble } from '../memory-bridge.js';
|
|
82
|
+
import { skillsAsText } from '../skill-prompt.js';
|
|
82
83
|
import { adapterFailure, spawnCredentials } from '../upstream-refusal.js';
|
|
83
84
|
|
|
84
85
|
// See codex.js for the rationale on bumping the default + env override.
|
|
@@ -274,41 +275,9 @@ const buildPublicClaudePolicyArgs = (mcpToolPatterns) => {
|
|
|
274
275
|
// be admitted through Seatbelt. Linux caps one argv string at 128 KiB, so the
|
|
275
276
|
// total is capped well under it.
|
|
276
277
|
const PUBLIC_SKILL_PROMPT_MAX_BYTES = 96 * 1024;
|
|
277
|
-
const SKILL_FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*\r?\n?/;
|
|
278
|
-
|
|
279
278
|
const publicSkillPromptArgs = async (skillSources) => {
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
for (const source of skillSources || []) {
|
|
283
|
-
let markdown;
|
|
284
|
-
try {
|
|
285
|
-
// eslint-disable-next-line no-await-in-loop
|
|
286
|
-
markdown = await readFile(join(source, 'SKILL.md'), 'utf8');
|
|
287
|
-
} catch {
|
|
288
|
-
// eslint-disable-next-line no-console
|
|
289
|
-
console.warn(`[claude] public seat: no SKILL.md to inline at ${source}`);
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
const frontmatter = SKILL_FRONTMATTER_RE.exec(markdown);
|
|
293
|
-
const name = (frontmatter && /^name:\s*(.+)$/m.exec(frontmatter[1])?.[1].trim())
|
|
294
|
-
|| source.split(/[\\/]/).filter(Boolean).pop();
|
|
295
|
-
const section = `## Skill: ${name}\n\n${markdown.slice(frontmatter ? frontmatter[0].length : 0).trim()}`;
|
|
296
|
-
const bytes = Buffer.byteLength(section, 'utf8');
|
|
297
|
-
if (total + bytes > PUBLIC_SKILL_PROMPT_MAX_BYTES) {
|
|
298
|
-
// eslint-disable-next-line no-console
|
|
299
|
-
console.warn(`[claude] public seat: skill ${name} not inlined (over ${PUBLIC_SKILL_PROMPT_MAX_BYTES} bytes in total)`);
|
|
300
|
-
continue;
|
|
301
|
-
}
|
|
302
|
-
total += bytes;
|
|
303
|
-
sections.push(section);
|
|
304
|
-
}
|
|
305
|
-
if (sections.length === 0) return [];
|
|
306
|
-
return [
|
|
307
|
-
'--append-system-prompt',
|
|
308
|
-
'These skills are part of how you work here. Skills are not loaded as files in this seat, '
|
|
309
|
-
+ 'so each one is given in full below. Follow them as you would a loaded skill.\n\n'
|
|
310
|
-
+ sections.join('\n\n'),
|
|
311
|
-
];
|
|
279
|
+
const text = await skillsAsText(skillSources, { maxBytes: PUBLIC_SKILL_PROMPT_MAX_BYTES, label: 'claude' });
|
|
280
|
+
return text ? ['--append-system-prompt', text] : [];
|
|
312
281
|
};
|
|
313
282
|
|
|
314
283
|
const runClaude = ({ cmd, args, cwd, env, timeoutMs, credentials = [], spawnImpl = childSpawn }) => new Promise((resolve, reject) => {
|
|
@@ -47,9 +47,11 @@ import {
|
|
|
47
47
|
mkdtemp,
|
|
48
48
|
readFile,
|
|
49
49
|
readlink,
|
|
50
|
+
rename,
|
|
50
51
|
rm,
|
|
51
52
|
symlink,
|
|
52
53
|
unlink,
|
|
54
|
+
writeFile,
|
|
53
55
|
} from 'fs/promises';
|
|
54
56
|
import { homedir, tmpdir } from 'os';
|
|
55
57
|
import {
|
|
@@ -62,6 +64,7 @@ import { supportedGitHooks } from '../commit-attribution.js';
|
|
|
62
64
|
import { deliverSeatCredential, withholdRuntimeCredential } from '../mcp-credential-delivery.js';
|
|
63
65
|
import { prepareMcpSpawn, publicMcpUnavailableError, selectedMcpVersionDir } from '../mcp-home.js';
|
|
64
66
|
import { buildMemoryPreamble } from '../memory-bridge.js';
|
|
67
|
+
import { resolveSkillSource, skillsAsText } from '../skill-prompt.js';
|
|
65
68
|
import { isLegacySandboxTrust, normalizeSandboxTrust } from '../environment.js';
|
|
66
69
|
import { adapterFailure, spawnCredentials } from '../upstream-refusal.js';
|
|
67
70
|
|
|
@@ -308,6 +311,52 @@ const preparePublicCodexHome = async (ctx) => {
|
|
|
308
311
|
return publicHome;
|
|
309
312
|
};
|
|
310
313
|
|
|
314
|
+
// A public seat ran with none of its declared skills: this adapter never read
|
|
315
|
+
// `environment.skills`, so every outsider's Codex worked without the house
|
|
316
|
+
// rules buildDefaultEnvironment declares for it (found 2026-10-20 beside the
|
|
317
|
+
// Claude half of the same gap, #2354). `codex exec` has no skill loader a
|
|
318
|
+
// public seat can use, but it reads $CODEX_HOME/AGENTS.md as the user's own
|
|
319
|
+
// instructions, and the public home is one this CLI owns. Measured on
|
|
320
|
+
// codex-cli 0.162.1 with this adapter's public flags (`--ignore-user-config`
|
|
321
|
+
// skips config.toml only): a marker in that file came back verbatim on a new
|
|
322
|
+
// thread, and a changed marker came back on `exec resume` of the same thread,
|
|
323
|
+
// so a seat picks the skills up on its next turn either way.
|
|
324
|
+
//
|
|
325
|
+
// `skills.claude` is ADR-008's SKILL.md list ("drivers copy or symlink as
|
|
326
|
+
// appropriate"); Codex reads the same format, so no new key. Rewritten each
|
|
327
|
+
// spawn (staged, then renamed, so a concurrent read never sees half a file),
|
|
328
|
+
// and removed when nothing is declared so a dropped skill stops applying.
|
|
329
|
+
// The cap is our own, set at Codex's `project_doc_max_bytes` (32768 in
|
|
330
|
+
// codex-cli 0.162.1's shipped default config, read from the binary 2026-10-21).
|
|
331
|
+
// It is not a limit Codex applies to this file. At rust-v0.162.1 the global
|
|
332
|
+
// `$CODEX_HOME/AGENTS.md` is read whole and trimmed
|
|
333
|
+
// (codex-home/src/instructions/mod.rs) and passed in as user instructions,
|
|
334
|
+
// outside the project-doc budget, which counts only the project AGENTS.md files
|
|
335
|
+
// discovered from the working directory (core/src/agents_md.rs,
|
|
336
|
+
// load_project_instructions). So the skills never share a budget with a
|
|
337
|
+
// workspace's own AGENTS.md, and nothing past the cap would be cut. The cap is
|
|
338
|
+
// what keeps the injected text to the size Codex allows a project doc. It
|
|
339
|
+
// bounds the file as written, final newline included, and a skill that would
|
|
340
|
+
// cross it is left out whole.
|
|
341
|
+
const PUBLIC_CODEX_INSTRUCTIONS_MAX_BYTES = 32 * 1024;
|
|
342
|
+
|
|
343
|
+
const writePublicCodexInstructions = async (publicHome, ctx) => {
|
|
344
|
+
const declared = Array.isArray(ctx.environment?.skills?.claude) ? ctx.environment.skills.claude : [];
|
|
345
|
+
const baseDir = ctx.cwd || process.cwd();
|
|
346
|
+
const text = await skillsAsText(
|
|
347
|
+
declared.map((source) => resolveSkillSource(source, baseDir)),
|
|
348
|
+
{ maxBytes: PUBLIC_CODEX_INSTRUCTIONS_MAX_BYTES, label: 'codex', trailer: '\n' },
|
|
349
|
+
);
|
|
350
|
+
const target = join(publicHome, 'AGENTS.md');
|
|
351
|
+
if (!text) {
|
|
352
|
+
await rm(target, { force: true });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const staged = `${target}.${process.pid}.tmp`;
|
|
356
|
+
await writeFile(staged, text, { encoding: 'utf8', mode: 0o600 });
|
|
357
|
+
await rename(staged, target);
|
|
358
|
+
};
|
|
359
|
+
|
|
311
360
|
const publicPermissionProfileFlags = (
|
|
312
361
|
mode,
|
|
313
362
|
commitAttributionEnv = null,
|
|
@@ -653,6 +702,7 @@ export default {
|
|
|
653
702
|
}
|
|
654
703
|
if (publicSandboxMode !== null) {
|
|
655
704
|
childEnv.CODEX_HOME = await preparePublicCodexHome(ctx);
|
|
705
|
+
await writePublicCodexInstructions(childEnv.CODEX_HOME, ctx);
|
|
656
706
|
}
|
|
657
707
|
|
|
658
708
|
const { threadId } = await runCodex({
|
|
@@ -51,6 +51,8 @@ import { detectBwrap, wrapArgvWithBwrap } from '../sandbox/bwrap.js';
|
|
|
51
51
|
import { wrapArgvWithSeatbelt } from '../sandbox/seatbelt.js';
|
|
52
52
|
import { resolvePublicSandboxMode } from '../sandbox/mode.js';
|
|
53
53
|
import { resolveProviderKeyFile, startProviderProxy } from '../provider-proxy.js';
|
|
54
|
+
import { isShippedCommonlyMcpCommand } from '../declared-mcp-guard.js';
|
|
55
|
+
import { isAnswerableToolError } from '../public-tool-error.js';
|
|
54
56
|
|
|
55
57
|
const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
|
|
56
58
|
const TERMINATION_GRACE_MS = 5000;
|
|
@@ -297,6 +299,9 @@ const makeEventParser = () => {
|
|
|
297
299
|
let text = '';
|
|
298
300
|
let usage = null;
|
|
299
301
|
let errorMessage = null;
|
|
302
|
+
// Every tool call that came back as an error, for the public-seat check in
|
|
303
|
+
// spawn (publicToolFailure).
|
|
304
|
+
const toolErrors = [];
|
|
300
305
|
const consumeLine = (line) => {
|
|
301
306
|
if (!line.trim()) return;
|
|
302
307
|
try {
|
|
@@ -306,6 +311,12 @@ const makeEventParser = () => {
|
|
|
306
311
|
text += event.part.text;
|
|
307
312
|
} else if (event.type === 'step_finish' && event.part?.tokens) {
|
|
308
313
|
usage = event.part.tokens;
|
|
314
|
+
} else if (event.type === 'tool_use' && event.part?.state?.status === 'error') {
|
|
315
|
+
const { error } = event.part.state;
|
|
316
|
+
toolErrors.push({
|
|
317
|
+
tool: String(event.part.tool || ''),
|
|
318
|
+
error: typeof error === 'string' ? error : JSON.stringify(error ?? ''),
|
|
319
|
+
});
|
|
309
320
|
} else if (event.type === 'error') {
|
|
310
321
|
errorMessage = event.error?.data?.message || event.error?.message || 'OpenCode turn failed';
|
|
311
322
|
}
|
|
@@ -332,9 +343,50 @@ const makeEventParser = () => {
|
|
|
332
343
|
get text() { return text; },
|
|
333
344
|
get usage() { return usage; },
|
|
334
345
|
get errorMessage() { return errorMessage; },
|
|
346
|
+
get toolErrors() { return toolErrors; },
|
|
335
347
|
};
|
|
336
348
|
};
|
|
337
349
|
|
|
350
|
+
// A public seat must never show a stranger its own plumbing. When a Commonly
|
|
351
|
+
// tool can't reach the API, the model is left narrating the failure, and the
|
|
352
|
+
// run loop posted that narration: on 2026-10-21 a canary of Commonly HQ's
|
|
353
|
+
// support seat (this adapter, its prompt, an unreachable API) told the asker
|
|
354
|
+
// "every Commonly tool call (`list_pods`, `list_artifacts`) is returning
|
|
355
|
+
// `fetch failed`", and an instruction to answer NO_REPLY in that case held in
|
|
356
|
+
// only 1 of 3 turns. So the turn is withheld here instead: nothing is posted,
|
|
357
|
+
// and the run loop keeps the event unacked for its retry.
|
|
358
|
+
//
|
|
359
|
+
// OpenCode names an MCP tool `<server>_<tool>`, and a failed call reports
|
|
360
|
+
// `state.status: 'error'` with the message in `state.error` (measured on
|
|
361
|
+
// OpenCode 1.19.0). Only tools of the declared Commonly server count. Of
|
|
362
|
+
// those, only the errors `isAnswerableToolError` names (an API refusal the
|
|
363
|
+
// person can be told about, or a schema mistake the model fixes) leave the
|
|
364
|
+
// turn to be posted; any other failure withholds it. This adapter-level check
|
|
365
|
+
// stays beside the adapter-agnostic readiness witness (TASK-256): it sees the
|
|
366
|
+
// runtime's own view of each call, and the witness sees a server that never
|
|
367
|
+
// started, so each catches what the other cannot.
|
|
368
|
+
export const PUBLIC_TOOL_FAILED_CODE = 'public_tool_failed';
|
|
369
|
+
|
|
370
|
+
export const publicToolFailure = (toolErrors, mcpServers) => {
|
|
371
|
+
const prefixes = (Array.isArray(mcpServers) ? mcpServers : [])
|
|
372
|
+
.filter((server) => server?.name && isShippedCommonlyMcpCommand(server.command))
|
|
373
|
+
.map((server) => `${server.name}_`);
|
|
374
|
+
if (!prefixes.length) return null;
|
|
375
|
+
return (toolErrors || []).find(({ tool, error }) => (
|
|
376
|
+
prefixes.some((prefix) => tool.startsWith(prefix)) && !isAnswerableToolError(error)
|
|
377
|
+
)) || null;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const publicToolFailedError = ({ tool, error }) => {
|
|
381
|
+
const reason = String(error).replace(/\s+/g, ' ').slice(0, 160) || '(empty error)';
|
|
382
|
+
const failure = new Error(
|
|
383
|
+
`public seat withheld this turn's reply: Commonly tool ${tool} failed in a way the model must not narrate `
|
|
384
|
+
+ `(${reason}), so nothing was posted and the event stays unacked for retry`,
|
|
385
|
+
);
|
|
386
|
+
failure.code = PUBLIC_TOOL_FAILED_CODE;
|
|
387
|
+
return failure;
|
|
388
|
+
};
|
|
389
|
+
|
|
338
390
|
const runOpenCode = ({ cmd, args, cwd, env, timeoutMs, credentials, spawnImpl = childSpawn }) => new Promise((resolve, reject) => {
|
|
339
391
|
let proc;
|
|
340
392
|
try {
|
|
@@ -376,7 +428,9 @@ const runOpenCode = ({ cmd, args, cwd, env, timeoutMs, credentials, spawnImpl =
|
|
|
376
428
|
credentials, exitCode: code, limit: 500,
|
|
377
429
|
}));
|
|
378
430
|
}
|
|
379
|
-
resolve({
|
|
431
|
+
resolve({
|
|
432
|
+
sessionId: events.sessionId, text: events.text, usage: events.usage, toolErrors: events.toolErrors,
|
|
433
|
+
});
|
|
380
434
|
});
|
|
381
435
|
});
|
|
382
436
|
|
|
@@ -783,6 +837,10 @@ export default {
|
|
|
783
837
|
credentials,
|
|
784
838
|
spawnImpl: ctx._spawnImpl,
|
|
785
839
|
});
|
|
840
|
+
if (isPublic) {
|
|
841
|
+
const failure = publicToolFailure(result.toolErrors, ctx.environment?.mcp);
|
|
842
|
+
if (failure) throw publicToolFailedError(failure);
|
|
843
|
+
}
|
|
786
844
|
return {
|
|
787
845
|
text: result.text.trim(),
|
|
788
846
|
newSessionId: result.sessionId || ctx.sessionId || null,
|
package/src/lib/environment.js
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
import { existsSync } from 'fs';
|
|
21
21
|
import { isAbsolute, join, resolve as pathResolve, basename } from 'path';
|
|
22
22
|
import { homedir } from 'os';
|
|
23
|
+
import { resolveSkillSource } from './skill-prompt.js';
|
|
23
24
|
|
|
24
25
|
// ── Schema — keep the allow-list narrow; ADR-008 §invariants #1+#2 ──────────
|
|
25
26
|
//
|
|
@@ -524,10 +525,7 @@ export const mountSkills = async (spec, workspacePath, envFileDir = null) => {
|
|
|
524
525
|
await mkdir(skillsDir, { recursive: true });
|
|
525
526
|
|
|
526
527
|
for (const rawSource of sources) {
|
|
527
|
-
const
|
|
528
|
-
const absSource = isAbsolute(source)
|
|
529
|
-
? source
|
|
530
|
-
: pathResolve(envFileDir || workspacePath, source);
|
|
528
|
+
const absSource = resolveSkillSource(rawSource, envFileDir || workspacePath);
|
|
531
529
|
if (!existsSync(absSource)) {
|
|
532
530
|
conflicted.push({ path: absSource, reason: 'missing-source' });
|
|
533
531
|
continue;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which failed Commonly tool calls a public seat's model may answer in plain
|
|
3
|
+
* words. Everything else withholds the turn (#2374, Vera 79595, Wren 79596).
|
|
4
|
+
*
|
|
5
|
+
* The rule is inverted on purpose. Matching known connection failures let an
|
|
6
|
+
* API 5xx through: the shipped MCP turns a non-2xx into a tool error carrying
|
|
7
|
+
* the status, and during a rollout the ingress answers 503 while the socket
|
|
8
|
+
* connects, so the model narrated the outage to a stranger. A list of failures
|
|
9
|
+
* fails open on the next one nobody listed; a list of answers fails closed.
|
|
10
|
+
*
|
|
11
|
+
* The answerable ones:
|
|
12
|
+
* - an API refusal the person can be told about: 400, 403, 404, 409, 410, 413
|
|
13
|
+
* or 422. 401 is NOT among them: a seat whose token the API rejects is
|
|
14
|
+
* broken plumbing, and every later call fails the same way.
|
|
15
|
+
* - the runtime's own schema check, which the model fixes on its next call.
|
|
16
|
+
* OpenCode 1.19.0 words it "The <tool> tool was called with invalid
|
|
17
|
+
* arguments: …" (ToolInvalidArgumentsError, read from the shipped binary).
|
|
18
|
+
*
|
|
19
|
+
* Everything else withholds: a 5xx, 401, 429 or any other status; a tool error
|
|
20
|
+
* with no status (the shipped MCP's network, timeout and token failures); an
|
|
21
|
+
* MCP transport error ("MCP error -32000: Connection closed"); an empty error;
|
|
22
|
+
* and any text this module does not recognise.
|
|
23
|
+
*
|
|
24
|
+
* How the status arrives: the shipped MCP answers a failed call with
|
|
25
|
+
* `isError: true` and one text part, `JSON.stringify({ status, body, message })`
|
|
26
|
+
* for an HTTP error or `{ message }` otherwise (commonly-mcp/src/tools.js,
|
|
27
|
+
* `err`). OpenCode throws that text as the tool's error, so the event's
|
|
28
|
+
* `state.error` is the JSON (measured on the 2026-10-21 canary).
|
|
29
|
+
*
|
|
30
|
+
* The TASK-256 readiness witness uses this same predicate, so the two
|
|
31
|
+
* witnesses cannot disagree about what counts as a failure.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export const ANSWERABLE_API_STATUSES = new Set([400, 403, 404, 409, 410, 413, 422]);
|
|
35
|
+
|
|
36
|
+
const INVALID_ARGUMENTS_RE = /^The \S+ tool was called with invalid arguments: /;
|
|
37
|
+
|
|
38
|
+
const parsePayload = (text) => {
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(text);
|
|
41
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Whether a failed tool call's error text is one the model may answer. */
|
|
48
|
+
export const isAnswerableToolError = (error) => {
|
|
49
|
+
const text = typeof error === 'string' ? error.trim() : '';
|
|
50
|
+
if (!text) return false;
|
|
51
|
+
if (INVALID_ARGUMENTS_RE.test(text)) return true;
|
|
52
|
+
const payload = parsePayload(text);
|
|
53
|
+
return Boolean(payload && Number.isInteger(payload.status) && ANSWERABLE_API_STATUSES.has(payload.status));
|
|
54
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// A public seat cannot load its declared skills as files, so each adapter
|
|
2
|
+
// delivers the SKILL.md bodies as instructions text instead:
|
|
3
|
+
// - claude: skills are disabled outright (`--disable-slash-commands`,
|
|
4
|
+
// `--setting-sources ''`), so the text rides as `--append-system-prompt`;
|
|
5
|
+
// - codex: `exec` has no skill loader the seat can use, and a public seat's
|
|
6
|
+
// CODEX_HOME is one this CLI owns, so the text is that home's AGENTS.md.
|
|
7
|
+
// One reader for both, so the two runtimes cannot drift on what a skill says.
|
|
8
|
+
import { readFile } from 'fs/promises';
|
|
9
|
+
import { isAbsolute, join, resolve as pathResolve } from 'path';
|
|
10
|
+
import { homedir } from 'os';
|
|
11
|
+
|
|
12
|
+
const SKILL_FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*\r?\n?/;
|
|
13
|
+
|
|
14
|
+
export const SKILLS_AS_TEXT_PREAMBLE = 'These skills are part of how you work here. Skills are not loaded as files in this seat, '
|
|
15
|
+
+ 'so each one is given in full below. Follow them as you would a loaded skill.';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A declared skill path as the environment spec means it: `~/` expanded,
|
|
19
|
+
* relative paths taken from `baseDir`. The same resolution mountSkills applies.
|
|
20
|
+
*/
|
|
21
|
+
export const resolveSkillSource = (rawSource, baseDir) => {
|
|
22
|
+
let source = rawSource;
|
|
23
|
+
if (source === '~') source = homedir();
|
|
24
|
+
else if (typeof source === 'string' && source.startsWith('~/')) source = join(homedir(), source.slice(2));
|
|
25
|
+
return isAbsolute(source) ? source : pathResolve(baseDir, source);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const SECTION_SEPARATOR = '\n\n';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Each source's SKILL.md as `## Skill: <name>` plus its body without the
|
|
32
|
+
* frontmatter, in declared order. `maxBytes` bounds the whole text
|
|
33
|
+
* skillsAsText returns (preamble, separators and `trailer` included), so a cap
|
|
34
|
+
* is the size of what the runtime reads. A source with no SKILL.md, or one that
|
|
35
|
+
* would cross the cap, is skipped with a warning naming the runtime (`label`)
|
|
36
|
+
* rather than failing the spawn.
|
|
37
|
+
*/
|
|
38
|
+
export const readSkillSections = async (skillSources, { maxBytes, label, trailer = '' }) => {
|
|
39
|
+
const sections = [];
|
|
40
|
+
let total = Buffer.byteLength(SKILLS_AS_TEXT_PREAMBLE, 'utf8') + Buffer.byteLength(trailer, 'utf8');
|
|
41
|
+
for (const source of skillSources || []) {
|
|
42
|
+
let markdown;
|
|
43
|
+
try {
|
|
44
|
+
// eslint-disable-next-line no-await-in-loop
|
|
45
|
+
markdown = await readFile(join(source, 'SKILL.md'), 'utf8');
|
|
46
|
+
} catch {
|
|
47
|
+
// eslint-disable-next-line no-console
|
|
48
|
+
console.warn(`[${label}] public seat: no SKILL.md to inline at ${source}`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const frontmatter = SKILL_FRONTMATTER_RE.exec(markdown);
|
|
52
|
+
const name = (frontmatter && /^name:\s*(.+)$/m.exec(frontmatter[1])?.[1].trim())
|
|
53
|
+
|| source.split(/[\\/]/).filter(Boolean).pop();
|
|
54
|
+
const section = `## Skill: ${name}\n\n${markdown.slice(frontmatter ? frontmatter[0].length : 0).trim()}`;
|
|
55
|
+
const bytes = Buffer.byteLength(SECTION_SEPARATOR, 'utf8') + Buffer.byteLength(section, 'utf8');
|
|
56
|
+
if (total + bytes > maxBytes) {
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
console.warn(`[${label}] public seat: skill ${name} not inlined (over ${maxBytes} bytes in total)`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
total += bytes;
|
|
62
|
+
sections.push(section);
|
|
63
|
+
}
|
|
64
|
+
return sections;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** The preamble, every section and `trailer`, or null when no skill could be read. */
|
|
68
|
+
export const skillsAsText = async (skillSources, options) => {
|
|
69
|
+
const sections = await readSkillSections(skillSources, options);
|
|
70
|
+
return sections.length
|
|
71
|
+
? `${SKILLS_AS_TEXT_PREAMBLE}${SECTION_SEPARATOR}${sections.join(SECTION_SEPARATOR)}${options.trailer || ''}`
|
|
72
|
+
: null;
|
|
73
|
+
};
|