@soft-boy/agentschema 0.4.0 → 0.5.1
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/README.md +11 -5
- package/dist/bin/agentschema.d.ts +2 -0
- package/dist/bin/agentschema.js +333 -0
- package/dist/lib/channels.d.ts +74 -0
- package/dist/lib/channels.js +403 -0
- package/dist/lib/check.d.ts +10 -0
- package/dist/lib/check.js +411 -0
- package/dist/lib/files.d.ts +7 -0
- package/{lib → dist/lib}/files.js +10 -14
- package/dist/lib/index.d.ts +24 -0
- package/{lib → dist/lib}/index.js +4 -13
- package/dist/lib/instance.d.ts +41 -0
- package/dist/lib/instance.js +74 -0
- package/dist/lib/markdown.d.ts +11 -0
- package/dist/lib/markdown.js +38 -0
- package/dist/lib/scaffold.d.ts +15 -0
- package/dist/lib/scaffold.js +46 -0
- package/dist/lib/scheduled.d.ts +56 -0
- package/dist/lib/scheduled.js +185 -0
- package/dist/lib/skills.d.ts +28 -0
- package/dist/lib/skills.js +55 -0
- package/dist/lib/stream.d.ts +30 -0
- package/dist/lib/stream.js +56 -0
- package/dist/lib/system-prompt.d.ts +16 -0
- package/{lib → dist/lib}/system-prompt.js +14 -15
- package/dist/lib/tools.d.ts +127 -0
- package/dist/lib/tools.js +465 -0
- package/dist/lib/vaults.d.ts +55 -0
- package/dist/lib/vaults.js +143 -0
- package/dist/src/chat.d.ts +15 -0
- package/dist/src/chat.js +159 -0
- package/dist/src/check.d.ts +2 -0
- package/dist/src/check.js +14 -0
- package/dist/src/cred-menu.d.ts +17 -0
- package/dist/src/cred-menu.js +193 -0
- package/dist/src/cred.d.ts +54 -0
- package/dist/src/cred.js +252 -0
- package/dist/src/custom-tools.d.ts +3 -0
- package/dist/src/custom-tools.js +18 -0
- package/dist/src/delete.d.ts +9 -0
- package/dist/src/delete.js +27 -0
- package/dist/src/deploy.d.ts +62 -0
- package/dist/src/deploy.js +442 -0
- package/dist/src/dotenv.d.ts +7 -0
- package/dist/src/dotenv.js +54 -0
- package/dist/src/folder.d.ts +5 -0
- package/{src → dist/src}/folder.js +30 -28
- package/dist/src/init.d.ts +11 -0
- package/dist/src/init.js +51 -0
- package/dist/src/list.d.ts +3 -0
- package/dist/src/list.js +46 -0
- package/dist/src/lock.d.ts +10 -0
- package/dist/src/lock.js +37 -0
- package/dist/src/mcp.d.ts +11 -0
- package/dist/src/mcp.js +79 -0
- package/dist/src/node.d.ts +14 -0
- package/dist/src/oauth.d.ts +64 -0
- package/dist/src/oauth.js +208 -0
- package/dist/src/prompts.d.ts +18 -0
- package/dist/src/prompts.js +166 -0
- package/dist/src/session.d.ts +58 -0
- package/dist/src/session.js +200 -0
- package/dist/src/tools-menu.d.ts +8 -0
- package/dist/src/tools-menu.js +173 -0
- package/package.json +21 -9
- package/bin/agentschema.js +0 -326
- package/lib/channels.js +0 -338
- package/lib/check.js +0 -387
- package/lib/instance.js +0 -75
- package/lib/markdown.js +0 -42
- package/lib/scaffold.js +0 -51
- package/lib/scheduled.js +0 -175
- package/lib/skills.js +0 -58
- package/lib/stream.js +0 -50
- package/lib/tools.js +0 -327
- package/lib/vaults.js +0 -142
- package/src/chat.js +0 -156
- package/src/check.js +0 -14
- package/src/cred-menu.js +0 -197
- package/src/cred.js +0 -263
- package/src/custom-tools.js +0 -17
- package/src/delete.js +0 -25
- package/src/deploy.js +0 -441
- package/src/dotenv.js +0 -50
- package/src/init.js +0 -54
- package/src/list.js +0 -49
- package/src/lock.js +0 -40
- package/src/mcp.js +0 -76
- package/src/oauth.js +0 -215
- package/src/prompts.js +0 -159
- package/src/session.js +0 -194
- package/src/tools-menu.js +0 -163
- /package/{src → dist/src}/node.js +0 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { parseDocument } from "yaml";
|
|
2
|
+
export class FileError extends Error {
|
|
3
|
+
}
|
|
4
|
+
// Splits a Markdown file into YAML frontmatter (between `---` fences) and body.
|
|
5
|
+
export function parseMarkdown(text, file) {
|
|
6
|
+
const normalized = normalize(text);
|
|
7
|
+
if (!normalized.startsWith("---\n")) {
|
|
8
|
+
throw new FileError(`${file}: must start with YAML frontmatter between --- lines`);
|
|
9
|
+
}
|
|
10
|
+
const closing = /^---[ \t]*$/m.exec(normalized.slice(4));
|
|
11
|
+
if (!closing)
|
|
12
|
+
throw new FileError(`${file}: frontmatter is not closed with a --- line`);
|
|
13
|
+
const end = 4 + closing.index;
|
|
14
|
+
const afterFence = normalized.slice(end + closing[0].length);
|
|
15
|
+
const doc = parseDocument(normalized.slice(4, end));
|
|
16
|
+
if (doc.errors.length > 0) {
|
|
17
|
+
// The YAML library counts lines from the start of the frontmatter; +1 for the opening fence.
|
|
18
|
+
const err = doc.errors[0];
|
|
19
|
+
const line = err.linePos?.[0]?.line;
|
|
20
|
+
const where = line ? `:${line + 1}` : "";
|
|
21
|
+
throw new FileError(`${file}${where}: invalid YAML: ${err.message.split("\n")[0]}`);
|
|
22
|
+
}
|
|
23
|
+
const data = doc.toJS() ?? {};
|
|
24
|
+
if (typeof data !== "object" || Array.isArray(data)) {
|
|
25
|
+
throw new FileError(`${file}: frontmatter must be a YAML mapping (key: value pairs)`);
|
|
26
|
+
}
|
|
27
|
+
return { data, body: afterFence.trim(), raw: text };
|
|
28
|
+
}
|
|
29
|
+
// Edits a file's frontmatter through `edit(doc)`, keeping its comments and the rest of the file.
|
|
30
|
+
// Returns the new text.
|
|
31
|
+
export function editFrontmatter(text, edit) {
|
|
32
|
+
const normalized = normalize(text);
|
|
33
|
+
const end = 4 + /^---[ \t]*$/m.exec(normalized.slice(4)).index;
|
|
34
|
+
const doc = parseDocument(normalized.slice(4, end));
|
|
35
|
+
edit(doc);
|
|
36
|
+
return `---\n${doc.toString({ lineWidth: 0 })}${normalized.slice(end)}`;
|
|
37
|
+
}
|
|
38
|
+
const normalize = (text) => text.replace(/^/, "").replace(/\r\n/g, "\n");
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const MODELS: string[];
|
|
2
|
+
export declare const NETWORKING: string[];
|
|
3
|
+
export interface Answers {
|
|
4
|
+
name: string;
|
|
5
|
+
model: string;
|
|
6
|
+
system: string;
|
|
7
|
+
tools: boolean;
|
|
8
|
+
permission: string;
|
|
9
|
+
environment: string;
|
|
10
|
+
networking: string;
|
|
11
|
+
packageManagers: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare function defaultAnswers(name: string): Answers;
|
|
14
|
+
export declare function scaffold(answers: Answers): Record<string, string>;
|
|
15
|
+
export declare function builtInFile(names: string[], enabled?: string[], permission?: string): string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
import { AGENT_FILE, COMPUTER_FILE } from "./instance.js";
|
|
3
|
+
import { COMPUTER_TOOLS, TOOLSET, TOOLSET_FILE, TOOLS_DIR } from "./tools.js";
|
|
4
|
+
export const MODELS = ["claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5"];
|
|
5
|
+
export const NETWORKING = ["limited", "unrestricted"];
|
|
6
|
+
// The answers used for a new agent called `name`, when a question is skipped.
|
|
7
|
+
export function defaultAnswers(name) {
|
|
8
|
+
const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "agent";
|
|
9
|
+
return {
|
|
10
|
+
name: slug.split("-").map((w) => w[0].toUpperCase() + w.slice(1)).join(" "),
|
|
11
|
+
model: MODELS[0],
|
|
12
|
+
system: "You are a helpful assistant.",
|
|
13
|
+
tools: true,
|
|
14
|
+
permission: "auto",
|
|
15
|
+
environment: slug,
|
|
16
|
+
networking: "limited",
|
|
17
|
+
packageManagers: true,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// Returns { path: text } for a set of answers: agent.md, env.md, and tools/agent_toolset_20260401.md
|
|
21
|
+
// listing every computer tool when the agent gets them.
|
|
22
|
+
export function scaffold(answers) {
|
|
23
|
+
const agent = { name: answers.name, model: answers.model };
|
|
24
|
+
const networking = { type: answers.networking };
|
|
25
|
+
if (answers.networking === "limited")
|
|
26
|
+
networking.allow_package_managers = answers.packageManagers;
|
|
27
|
+
const env = { name: answers.environment, config: { type: "cloud", networking } };
|
|
28
|
+
const access = answers.networking === "unrestricted"
|
|
29
|
+
? "unrestricted network access"
|
|
30
|
+
: answers.packageManagers ? "limited networking and package manager access" : "limited networking";
|
|
31
|
+
const files = {
|
|
32
|
+
[AGENT_FILE]: `---\n${stringify(agent)}---\n\n${answers.system.trim()}\n`,
|
|
33
|
+
[COMPUTER_FILE]: `---\n${stringify(env)}---\nCloud container with ${access}.\n`,
|
|
34
|
+
};
|
|
35
|
+
if (answers.tools) {
|
|
36
|
+
files[`${TOOLS_DIR}/${TOOLSET_FILE}`] = builtInFile(COMPUTER_TOOLS, COMPUTER_TOOLS, answers.permission);
|
|
37
|
+
}
|
|
38
|
+
return files;
|
|
39
|
+
}
|
|
40
|
+
// The computer toolset file, with a row for each of `names`: on if it's in `enabled`, each with
|
|
41
|
+
// `permission`, so what's chosen is there to read and change. `auto` is what the API recommends for
|
|
42
|
+
// these tools: Anthropic judges each call, and asks when it can't tell.
|
|
43
|
+
export function builtInFile(names, enabled = names, permission = "auto") {
|
|
44
|
+
const rows = names.map((name) => `| \`${name}\` | \`${enabled.includes(name)}\` | \`${permission}\` |`).join("\n");
|
|
45
|
+
return `---\ntype: ${TOOLSET}\n---\n\nThe tools Claude runs in the agent's container. Set Enabled to false to turn a tool off.\n\n## Tools\n\n| Name | Enabled | Permission |\n|---|---|---|\n${rows}\n`;
|
|
46
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type FileMap } from "./files.ts";
|
|
2
|
+
import { type MarkdownFile } from "./markdown.ts";
|
|
3
|
+
import { type Tool } from "./tools.ts";
|
|
4
|
+
export declare const SCHEDULED_DIR = "scheduled";
|
|
5
|
+
export interface ScheduledTask extends MarkdownFile {
|
|
6
|
+
file: string;
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ReadScheduledResult {
|
|
10
|
+
tasks: ScheduledTask[];
|
|
11
|
+
errors: string[];
|
|
12
|
+
}
|
|
13
|
+
export interface TaskSettings {
|
|
14
|
+
name: string;
|
|
15
|
+
schedule: string;
|
|
16
|
+
timezone: string;
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
budgetCents: number | null;
|
|
19
|
+
description: string | null;
|
|
20
|
+
message: string;
|
|
21
|
+
}
|
|
22
|
+
export interface TaskBody {
|
|
23
|
+
name: string;
|
|
24
|
+
initial_events: {
|
|
25
|
+
type: "user.message";
|
|
26
|
+
content: {
|
|
27
|
+
type: "text";
|
|
28
|
+
text: string;
|
|
29
|
+
}[];
|
|
30
|
+
}[];
|
|
31
|
+
schedule: {
|
|
32
|
+
type: "cron";
|
|
33
|
+
expression: string;
|
|
34
|
+
timezone: string;
|
|
35
|
+
};
|
|
36
|
+
budget?: {
|
|
37
|
+
type: "limit";
|
|
38
|
+
max_list_cost: {
|
|
39
|
+
amount: string;
|
|
40
|
+
currency: "USD";
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
description?: string;
|
|
44
|
+
}
|
|
45
|
+
export declare function readScheduled(files: FileMap): ReadScheduledResult;
|
|
46
|
+
export declare function cronProblem(expression: unknown): string | null;
|
|
47
|
+
export declare function isTimeZone(value: unknown): boolean;
|
|
48
|
+
export declare function checkScheduled(tasks: ScheduledTask[], { tools }?: {
|
|
49
|
+
tools?: Tool[];
|
|
50
|
+
}, errors?: string[], warnings?: string[]): {
|
|
51
|
+
errors: string[];
|
|
52
|
+
warnings: string[];
|
|
53
|
+
};
|
|
54
|
+
export declare function taskSettings({ name, data, body }: Pick<ScheduledTask, "name" | "data" | "body">): TaskSettings;
|
|
55
|
+
export declare function taskBody(task: Pick<ScheduledTask, "name" | "data" | "body">): TaskBody;
|
|
56
|
+
export declare const taskKey: (task: Pick<ScheduledTask, "file">) => string;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { pathsUnder, textOf } from "./files.js";
|
|
2
|
+
import { FileError, parseMarkdown } from "./markdown.js";
|
|
3
|
+
import { AGENTBOOK, MCP_TOOLSET, TOOLSET } from "./tools.js";
|
|
4
|
+
// A scheduled task runs the agent on a timetable, with the same message each time, like "say good
|
|
5
|
+
// morning in #gm" every weekday at 8. Each file in scheduled/ is one, named after its file, and it
|
|
6
|
+
// becomes a Managed Agents deployment: Anthropic starts each run, so nothing needs to be running here.
|
|
7
|
+
export const SCHEDULED_DIR = "scheduled";
|
|
8
|
+
const FIELDS = ["schedule", "timezone", "enabled", "budget", "description"];
|
|
9
|
+
const MAX_DESCRIPTION = 2048;
|
|
10
|
+
// Each part of a cron expression, and the numbers it takes. Day of the week is 0 to 7, where both 0
|
|
11
|
+
// and 7 are Sunday.
|
|
12
|
+
const CRON_PARTS = [
|
|
13
|
+
["minute", 0, 59],
|
|
14
|
+
["hour", 0, 23],
|
|
15
|
+
["day of the month", 1, 31],
|
|
16
|
+
["month", 1, 12],
|
|
17
|
+
["day of the week", 0, 7],
|
|
18
|
+
];
|
|
19
|
+
// Reads scheduled/: each .md file directly inside it is a task named after the file. No scheduled/
|
|
20
|
+
// folder means no tasks. Returns { tasks, errors }; a file that can't be parsed has an entry in errors.
|
|
21
|
+
export function readScheduled(files) {
|
|
22
|
+
if (SCHEDULED_DIR in files)
|
|
23
|
+
return { tasks: [], errors: [`${SCHEDULED_DIR}: not a directory`] };
|
|
24
|
+
const tasks = [];
|
|
25
|
+
const errors = [];
|
|
26
|
+
const reportedFolders = new Set();
|
|
27
|
+
for (const file of pathsUnder(files, SCHEDULED_DIR)) {
|
|
28
|
+
const [, item, ...rest] = file.split("/");
|
|
29
|
+
if (rest.length > 0) {
|
|
30
|
+
const folder = `${SCHEDULED_DIR}/${item}`;
|
|
31
|
+
if (!reportedFolders.has(folder))
|
|
32
|
+
errors.push(`${folder}: ${SCHEDULED_DIR}/ holds task files, not folders`);
|
|
33
|
+
reportedFolders.add(folder);
|
|
34
|
+
}
|
|
35
|
+
else if (item.endsWith(".md")) {
|
|
36
|
+
try {
|
|
37
|
+
tasks.push({ file, name: item.slice(0, -".md".length), ...parseMarkdown(textOf(files[file]), file) });
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
if (!(err instanceof FileError))
|
|
41
|
+
throw err;
|
|
42
|
+
errors.push(err.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { tasks, errors };
|
|
47
|
+
}
|
|
48
|
+
// What's wrong with a cron expression, or null. Only the standard five parts are allowed, with
|
|
49
|
+
// numbers, ranges, lists, steps and *, which is what Managed Agents accepts.
|
|
50
|
+
export function cronProblem(expression) {
|
|
51
|
+
if (typeof expression !== "string")
|
|
52
|
+
return `must be a cron expression in quotes, like "0 8 * * 1-5"`;
|
|
53
|
+
const parts = expression.trim().split(/\s+/);
|
|
54
|
+
if (parts.length !== 5)
|
|
55
|
+
return `must have five parts, for the minute, hour, day of the month, month and day of the week, like "0 8 * * 1-5"`;
|
|
56
|
+
for (const [i, part] of parts.entries()) {
|
|
57
|
+
const [what, min, max] = CRON_PARTS[i];
|
|
58
|
+
for (const item of part.split(",")) {
|
|
59
|
+
const match = /^(?:\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(item);
|
|
60
|
+
if (!match)
|
|
61
|
+
return `has "${part}" for the ${what}; use numbers, a range like 1-5, a list like 1,3, a step like */15, or *`;
|
|
62
|
+
const [, from, to, step] = match;
|
|
63
|
+
const numbers = [from, to].filter((n) => n !== undefined).map(Number);
|
|
64
|
+
if (numbers.some((n) => n < min || n > max))
|
|
65
|
+
return `has "${part}" for the ${what}, which goes from ${min} to ${max}`;
|
|
66
|
+
if (to !== undefined && Number(from) > Number(to))
|
|
67
|
+
return `has "${part}" for the ${what}, a range that runs backwards`;
|
|
68
|
+
if (step !== undefined && Number(step) < 1)
|
|
69
|
+
return `has "${part}" for the ${what}, but a step must be at least 1`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
// Whether a time zone is one the IANA database knows, like America/Los_Angeles or UTC.
|
|
75
|
+
export function isTimeZone(value) {
|
|
76
|
+
if (typeof value !== "string" || value.trim() === "")
|
|
77
|
+
return false;
|
|
78
|
+
try {
|
|
79
|
+
new Intl.DateTimeFormat("en-US", { timeZone: value });
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Errors and warnings for the task files, in check's format. `tools` and `toolsets` are the agent's,
|
|
87
|
+
// to warn about tools nobody can answer during a run.
|
|
88
|
+
export function checkScheduled(tasks, { tools = [] } = {}, errors = [], warnings = []) {
|
|
89
|
+
const waiting = toolsThatWaitForSomeone(tools);
|
|
90
|
+
for (const task of tasks) {
|
|
91
|
+
const err = (msg) => errors.push(`${task.file}: ${msg}`);
|
|
92
|
+
const { data, body, name } = task;
|
|
93
|
+
// It's the deployment's name, and hosts use it to keep track of the task.
|
|
94
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(name))
|
|
95
|
+
err("the name may only use lowercase letters, digits, _ and -, up to 64 characters");
|
|
96
|
+
if (data.schedule === undefined)
|
|
97
|
+
err('`schedule` is required: a cron expression, like "0 8 * * 1-5" for 8 AM on weekdays');
|
|
98
|
+
else {
|
|
99
|
+
const problem = cronProblem(data.schedule);
|
|
100
|
+
if (problem)
|
|
101
|
+
err(`\`schedule\` ${problem}`);
|
|
102
|
+
}
|
|
103
|
+
if (data.timezone === undefined)
|
|
104
|
+
err("`timezone` is required, like America/Los_Angeles or UTC");
|
|
105
|
+
else if (!isTimeZone(data.timezone))
|
|
106
|
+
err(`\`timezone\` must be a time zone like America/Los_Angeles or UTC, and ${JSON.stringify(data.timezone)} isn't one`);
|
|
107
|
+
if (data.enabled !== undefined && typeof data.enabled !== "boolean")
|
|
108
|
+
err("`enabled` must be true or false");
|
|
109
|
+
if (data.budget !== undefined && budgetCents(data.budget) === null)
|
|
110
|
+
err("`budget` must be an amount in US dollars above 0, like 0.50");
|
|
111
|
+
if (data.description !== undefined && data.description !== null) {
|
|
112
|
+
if (typeof data.description !== "string")
|
|
113
|
+
err("`description` must be text");
|
|
114
|
+
else if (data.description.length > MAX_DESCRIPTION)
|
|
115
|
+
err(`\`description\` must be at most ${MAX_DESCRIPTION.toLocaleString("en-US")} characters`);
|
|
116
|
+
}
|
|
117
|
+
for (const key of Object.keys(data))
|
|
118
|
+
if (!FIELDS.includes(key))
|
|
119
|
+
err(`unknown field \`${key}\``);
|
|
120
|
+
if (body.trim() === "")
|
|
121
|
+
err("the body is empty; it's the message the agent starts each run with");
|
|
122
|
+
if (waiting.length > 0 && data.enabled !== false) {
|
|
123
|
+
warnings.push(`${task.file}: nobody's there to answer during a scheduled run, so a run that calls one of these waits until it's stopped: ${waiting.join(", ")}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return { errors, warnings };
|
|
127
|
+
}
|
|
128
|
+
// Dollars as the API's minor units, or null if it isn't an amount above 0 with at most two decimals.
|
|
129
|
+
function budgetCents(value) {
|
|
130
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0)
|
|
131
|
+
return null;
|
|
132
|
+
const cents = Math.round(value * 100);
|
|
133
|
+
return Math.abs(cents - value * 100) < 1e-6 && cents > 0 ? cents : null;
|
|
134
|
+
}
|
|
135
|
+
// The tools that stop a run until someone answers, which nobody does during a scheduled run: ones set to
|
|
136
|
+
// ask first, and custom and agentbook tools, which run wherever the session is being watched from.
|
|
137
|
+
function toolsThatWaitForSomeone(tools) {
|
|
138
|
+
const isOn = (row) => row.enabled.toLowerCase() === "true";
|
|
139
|
+
return tools.flatMap((tool) => {
|
|
140
|
+
const type = tool.data.type;
|
|
141
|
+
if (type === TOOLSET)
|
|
142
|
+
return (tool.rows ?? []).filter((row) => isOn(row) && row.permission === "always_ask").map((row) => row.name);
|
|
143
|
+
if (type === MCP_TOOLSET) {
|
|
144
|
+
const defaults = tool.data.default_config;
|
|
145
|
+
const fallback = defaults?.permission_policy?.type ?? "always_ask";
|
|
146
|
+
if (tool.rows?.length)
|
|
147
|
+
return tool.rows.filter((row) => isOn(row) && (row.permission || fallback) === "always_ask").map((row) => `${tool.name}.${row.name}`);
|
|
148
|
+
return fallback === "always_ask" && defaults?.enabled !== false ? [`every tool in ${tool.name}`] : [];
|
|
149
|
+
}
|
|
150
|
+
if (type === "custom")
|
|
151
|
+
return [tool.name];
|
|
152
|
+
if (type === AGENTBOOK)
|
|
153
|
+
return (tool.rows ?? []).filter(isOn).map((row) => `${tool.name}.${row.name}`);
|
|
154
|
+
return [];
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
// How a task behaves, with the defaults filled in, so that every host agrees on them.
|
|
158
|
+
export function taskSettings({ name, data, body }) {
|
|
159
|
+
return {
|
|
160
|
+
name,
|
|
161
|
+
schedule: data.schedule,
|
|
162
|
+
timezone: data.timezone,
|
|
163
|
+
enabled: data.enabled ?? true,
|
|
164
|
+
budgetCents: data.budget === undefined ? null : budgetCents(data.budget),
|
|
165
|
+
description: data.description ?? null,
|
|
166
|
+
message: body,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
// The parts of a deployment that come from the task's file. Whoever deploys it adds the agent, the
|
|
170
|
+
// environment and the vaults.
|
|
171
|
+
export function taskBody(task) {
|
|
172
|
+
const { name, schedule, timezone, budgetCents: cents, description, message } = taskSettings(task);
|
|
173
|
+
const body = {
|
|
174
|
+
name,
|
|
175
|
+
initial_events: [{ type: "user.message", content: [{ type: "text", text: message }] }],
|
|
176
|
+
schedule: { type: "cron", expression: schedule.trim(), timezone },
|
|
177
|
+
};
|
|
178
|
+
if (cents !== null)
|
|
179
|
+
body.budget = { type: "limit", max_list_cost: { amount: String(cents), currency: "USD" } };
|
|
180
|
+
if (description)
|
|
181
|
+
body.description = description;
|
|
182
|
+
return body;
|
|
183
|
+
}
|
|
184
|
+
// A task's key in the lock.
|
|
185
|
+
export const taskKey = (task) => `./${task.file}`;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type FileMap } from "./files.ts";
|
|
2
|
+
import type { Lock } from "./instance.ts";
|
|
3
|
+
import { type MarkdownFile } from "./markdown.ts";
|
|
4
|
+
export declare const SKILLS_DIR = "skills";
|
|
5
|
+
export declare const SKILL_FILE = "SKILL.md";
|
|
6
|
+
export declare const MAX_UPLOAD_BYTES: number;
|
|
7
|
+
export interface SkillFile {
|
|
8
|
+
rel: string;
|
|
9
|
+
bytes: Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
export interface Skill extends MarkdownFile {
|
|
12
|
+
name: string;
|
|
13
|
+
path: string;
|
|
14
|
+
file: string;
|
|
15
|
+
files: SkillFile[];
|
|
16
|
+
}
|
|
17
|
+
export interface ReadSkillsResult {
|
|
18
|
+
skills: Skill[];
|
|
19
|
+
errors: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface SkillRef {
|
|
22
|
+
type: "custom";
|
|
23
|
+
skill_id: string;
|
|
24
|
+
version: string;
|
|
25
|
+
}
|
|
26
|
+
export declare function readSkills(files: FileMap): ReadSkillsResult;
|
|
27
|
+
export declare const skillKey: (skill: Pick<Skill, "path">) => string;
|
|
28
|
+
export declare function skillRefs(skills?: Pick<Skill, "path">[], lock?: Lock | null): SkillRef[];
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { bytesOf, isIgnoredPath, pathsUnder, textOf } from "./files.js";
|
|
2
|
+
import { FileError, parseMarkdown } from "./markdown.js";
|
|
3
|
+
export const SKILLS_DIR = "skills";
|
|
4
|
+
export const SKILL_FILE = "SKILL.md";
|
|
5
|
+
export const MAX_UPLOAD_BYTES = 30 * 1024 * 1024;
|
|
6
|
+
// Reads skills/: each folder is a skill, with a SKILL.md at its top and any other files it needs,
|
|
7
|
+
// at any depth. Hidden files and node_modules are left out. Skills are sorted by name. No skills/
|
|
8
|
+
// folder means no skills.
|
|
9
|
+
// Returns { skills, errors }; a skill that can't be read has an entry in errors.
|
|
10
|
+
export function readSkills(files) {
|
|
11
|
+
if (SKILLS_DIR in files)
|
|
12
|
+
return { skills: [], errors: [`${SKILLS_DIR}: not a directory`] };
|
|
13
|
+
const folders = new Map();
|
|
14
|
+
const errors = [];
|
|
15
|
+
for (const file of pathsUnder(files, SKILLS_DIR)) {
|
|
16
|
+
const [, name, ...rest] = file.split("/");
|
|
17
|
+
if (rest.length === 0) {
|
|
18
|
+
if (name.endsWith(".md"))
|
|
19
|
+
errors.push(`${file}: each skill is a folder, like ${SKILLS_DIR}/my-skill/${SKILL_FILE}`);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (!folders.has(name))
|
|
23
|
+
folders.set(name, []);
|
|
24
|
+
folders.get(name).push({ rel: rest.join("/"), bytes: bytesOf(files[file]) });
|
|
25
|
+
}
|
|
26
|
+
const skills = [];
|
|
27
|
+
for (const [name, skillFiles] of folders) {
|
|
28
|
+
const path = `${SKILLS_DIR}/${name}`;
|
|
29
|
+
// Exact names only, so a skill behaves the same on a case-sensitive file system.
|
|
30
|
+
const main = skillFiles.find((f) => f.rel === SKILL_FILE);
|
|
31
|
+
if (!main) {
|
|
32
|
+
const wrongCase = skillFiles.find((f) => f.rel.toLowerCase() === SKILL_FILE.toLowerCase());
|
|
33
|
+
errors.push(`${path}: missing ${SKILL_FILE}${wrongCase ? ` (found ${wrongCase.rel}; the name must be uppercase)` : ""}`);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const parsed = parseMarkdown(textOf(main.bytes), `${path}/${SKILL_FILE}`);
|
|
38
|
+
skills.push({ name, path, file: `${path}/${SKILL_FILE}`, files: skillFiles, ...parsed });
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
if (!(err instanceof FileError))
|
|
42
|
+
throw err;
|
|
43
|
+
errors.push(err.message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { skills, errors };
|
|
47
|
+
}
|
|
48
|
+
// The lock file key for a skill.
|
|
49
|
+
export const skillKey = (skill) => `./${skill.path}`;
|
|
50
|
+
// The agent's references to the skills deployed from this folder, pinned to their versions, so
|
|
51
|
+
// that a session runs exactly what deploy last sent.
|
|
52
|
+
export function skillRefs(skills = [], lock) {
|
|
53
|
+
const entries = skills.map((skill) => lock?.resources?.[skillKey(skill)]).filter(Boolean);
|
|
54
|
+
return entries.map((entry) => ({ type: "custom", skill_id: entry.id, version: entry.version }));
|
|
55
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface ContentBlock {
|
|
2
|
+
type: string;
|
|
3
|
+
text?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface AgentMessageEvent {
|
|
6
|
+
type: "agent.message";
|
|
7
|
+
id: string;
|
|
8
|
+
content: ContentBlock[];
|
|
9
|
+
}
|
|
10
|
+
export interface StreamEvent {
|
|
11
|
+
type: string;
|
|
12
|
+
id?: string;
|
|
13
|
+
event_id?: string;
|
|
14
|
+
event?: {
|
|
15
|
+
type?: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
};
|
|
18
|
+
delta?: {
|
|
19
|
+
content?: {
|
|
20
|
+
type?: string;
|
|
21
|
+
text?: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
content?: ContentBlock[];
|
|
25
|
+
}
|
|
26
|
+
export interface TextStream {
|
|
27
|
+
handle(event: StreamEvent): boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare const agentMessageText: (event: Pick<AgentMessageEvent, "content">) => string;
|
|
30
|
+
export declare function textStream(write: (text: string) => void): TextStream;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The text of a complete agent.message event.
|
|
2
|
+
export const agentMessageText = (event) => event.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
3
|
+
// Passes the agent's text to `write` as it streams in, chunk by chunk. Deltas are best effort (the
|
|
4
|
+
// server may stop sending them under load), so the complete agent.message that follows is the
|
|
5
|
+
// record: whatever part of it the deltas didn't cover is written then.
|
|
6
|
+
export function textStream(write) {
|
|
7
|
+
// The agent.message being previewed. The `!`s below rely on deltas and agent.messages always
|
|
8
|
+
// having their ids, so that matching the preview's id means there is one.
|
|
9
|
+
let preview = null;
|
|
10
|
+
const close = () => {
|
|
11
|
+
if (preview?.text)
|
|
12
|
+
write("\n");
|
|
13
|
+
preview = null;
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
// Returns true when the event was written here, so it shouldn't be shown again.
|
|
17
|
+
handle(event) {
|
|
18
|
+
switch (event.type) {
|
|
19
|
+
case "event_start":
|
|
20
|
+
close();
|
|
21
|
+
if (event.event?.type === "agent.message")
|
|
22
|
+
preview = { id: event.event.id, text: "" };
|
|
23
|
+
return true;
|
|
24
|
+
case "event_delta": {
|
|
25
|
+
const content = event.delta?.content;
|
|
26
|
+
if (preview?.id !== event.event_id || content?.type !== "text" || !content.text)
|
|
27
|
+
return true;
|
|
28
|
+
preview.text += content.text;
|
|
29
|
+
write(content.text);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
case "agent.message": {
|
|
33
|
+
if (preview?.id !== event.id) {
|
|
34
|
+
close();
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const full = agentMessageText(event);
|
|
38
|
+
const shown = preview.text;
|
|
39
|
+
preview = null;
|
|
40
|
+
if (full.startsWith(shown)) {
|
|
41
|
+
if (full.length > 0)
|
|
42
|
+
write(`${full.slice(shown.length)}\n`);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
write(`\n${full}\n`);
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
default:
|
|
50
|
+
// A model request that errors or is interrupted ends without its agent.message.
|
|
51
|
+
close();
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type FileMap } from "./files.ts";
|
|
2
|
+
import type { MarkdownFile } from "./markdown.ts";
|
|
3
|
+
export declare const SYSTEM_PROMPT_DIR = "system_prompt";
|
|
4
|
+
export interface SystemPromptPart {
|
|
5
|
+
file: string;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ReadSystemPromptResult {
|
|
9
|
+
parts: SystemPromptPart[];
|
|
10
|
+
errors: string[];
|
|
11
|
+
}
|
|
12
|
+
export declare function readSystemPrompt(files: FileMap): ReadSystemPromptResult;
|
|
13
|
+
export declare function systemPromptOf({ agent, systemPrompt }: {
|
|
14
|
+
agent?: Pick<MarkdownFile, "body"> | null;
|
|
15
|
+
systemPrompt?: SystemPromptPart[];
|
|
16
|
+
}): string;
|
|
@@ -1,32 +1,31 @@
|
|
|
1
1
|
import { pathsUnder, textOf } from "./files.js";
|
|
2
|
-
|
|
3
2
|
// A long system prompt can be split into files in system_prompt/, which go after agent.md's body. Every
|
|
4
3
|
// file in the folder counts, at any depth, in the order `tree` lists them, so the folder reads top to
|
|
5
4
|
// bottom the way the prompt does: each folder's entries sorted by name, character by character, with a
|
|
6
5
|
// folder's files right where the folder sorts. Each file is taken as written, with no frontmatter.
|
|
7
6
|
export const SYSTEM_PROMPT_DIR = "system_prompt";
|
|
8
|
-
|
|
9
7
|
// Text that came from something other than a text file: decoding bytes that aren't UTF-8 leaves
|
|
10
8
|
// replacement characters, and binary files have NULs.
|
|
11
9
|
const isBinary = (text) => text.includes("\uFFFD") || text.includes("\u0000");
|
|
12
|
-
|
|
13
10
|
// Reads system_prompt/. Returns { parts, errors }, where each part is { file, text } in tree order.
|
|
14
11
|
// No system_prompt/ folder means no parts.
|
|
15
12
|
export function readSystemPrompt(files) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
if (SYSTEM_PROMPT_DIR in files)
|
|
14
|
+
return { parts: [], errors: [`${SYSTEM_PROMPT_DIR}: not a directory`] };
|
|
15
|
+
const parts = [];
|
|
16
|
+
const errors = [];
|
|
17
|
+
// pathsUnder already lists them in tree's order.
|
|
18
|
+
for (const file of pathsUnder(files, SYSTEM_PROMPT_DIR)) {
|
|
19
|
+
const text = textOf(files[file]);
|
|
20
|
+
if (isBinary(text))
|
|
21
|
+
errors.push(`${file}: isn't a text file, so it can't be part of the system prompt`);
|
|
22
|
+
else
|
|
23
|
+
parts.push({ file, text });
|
|
24
|
+
}
|
|
25
|
+
return { parts, errors };
|
|
26
26
|
}
|
|
27
|
-
|
|
28
27
|
// The whole system prompt: agent.md's body, then each file in system_prompt/, with a blank line between
|
|
29
28
|
// them. Empty files add nothing.
|
|
30
29
|
export function systemPromptOf({ agent, systemPrompt = [] }) {
|
|
31
|
-
|
|
30
|
+
return [agent?.body ?? "", ...systemPrompt.map((part) => part.text.trim())].filter(Boolean).join("\n\n");
|
|
32
31
|
}
|