@pmndre/pine 0.0.0-stage → 0.22.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/README.md +226 -2
- package/bin/pine.js +27 -0
- package/docs/SKILL.md +234 -0
- package/docs/references/agents.md +454 -0
- package/docs/references/coding.md +311 -0
- package/docs/references/computer.md +212 -0
- package/docs/references/configuration.md +310 -0
- package/docs/references/events.md +267 -0
- package/docs/references/mcp.md +420 -0
- package/docs/references/operations.md +394 -0
- package/docs/references/overview.md +195 -0
- package/docs/references/security.md +896 -0
- package/docs/references/setup.md +385 -0
- package/docs/references/sources.md +772 -0
- package/docs/references/stack.md +121 -0
- package/docs/references/tui.md +575 -0
- package/examples/home/.env.example +7 -0
- package/examples/home/USER.md +4 -0
- package/examples/home/agents/main/agent.json +4 -0
- package/examples/home/agents/main/workspace/AGENTS.md +14 -0
- package/examples/home/agents/main/workspace/MEMORY.md +4 -0
- package/examples/home/agents/main/workspace/SOUL.md +4 -0
- package/examples/home/models.json +62 -0
- package/examples/home/pine.json +34 -0
- package/examples/home/skills/README.txt +7 -0
- package/examples/launchd/com.pine.daemon.plist +32 -0
- package/examples/systemd/pine.service +22 -0
- package/mcp/browser.d.ts +99 -0
- package/mcp/browser.js +496 -0
- package/mcp/commands.d.ts +20 -0
- package/mcp/commands.js +88 -0
- package/mcp/computer.d.ts +139 -0
- package/mcp/computer.js +698 -0
- package/mcp/computer.swift +989 -0
- package/mcp/lib.d.ts +23 -0
- package/mcp/lib.js +52 -0
- package/mcp/web.d.ts +6 -0
- package/mcp/web.js +140 -0
- package/package.json +32 -4
- package/src/access.d.ts +112 -0
- package/src/access.js +438 -0
- package/src/actions.d.ts +75 -0
- package/src/actions.js +167 -0
- package/src/admin.d.ts +167 -0
- package/src/admin.js +625 -0
- package/src/agent-admin.d.ts +24 -0
- package/src/agent-admin.js +55 -0
- package/src/agents.d.ts +540 -0
- package/src/agents.js +2776 -0
- package/src/background.d.ts +29 -0
- package/src/background.js +100 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +777 -0
- package/src/computer.d.ts +30 -0
- package/src/computer.js +32 -0
- package/src/config.d.ts +396 -0
- package/src/config.js +549 -0
- package/src/dev/claude-model.d.ts +37 -0
- package/src/dev/claude-model.js +204 -0
- package/src/env-file.d.ts +4 -0
- package/src/env-file.js +40 -0
- package/src/goals.d.ts +133 -0
- package/src/goals.js +316 -0
- package/src/http.d.ts +46 -0
- package/src/http.js +509 -0
- package/src/limiter.d.ts +32 -0
- package/src/limiter.js +84 -0
- package/src/login.d.ts +23 -0
- package/src/login.js +130 -0
- package/src/manual.d.ts +61 -0
- package/src/manual.js +443 -0
- package/src/mcp-config.d.ts +49 -0
- package/src/mcp-config.js +182 -0
- package/src/models.d.ts +90 -0
- package/src/models.js +274 -0
- package/src/net.d.ts +50 -0
- package/src/net.js +150 -0
- package/src/pine.d.ts +32 -0
- package/src/pine.js +134 -0
- package/src/programs.d.ts +123 -0
- package/src/programs.js +412 -0
- package/src/router.d.ts +56 -0
- package/src/router.js +164 -0
- package/src/sandbox-fs.d.ts +34 -0
- package/src/sandbox-fs.js +186 -0
- package/src/sandbox-linux.d.ts +96 -0
- package/src/sandbox-linux.js +297 -0
- package/src/sandbox-setup.d.ts +70 -0
- package/src/sandbox-setup.js +216 -0
- package/src/security.d.ts +238 -0
- package/src/security.js +660 -0
- package/src/skills.d.ts +67 -0
- package/src/skills.js +163 -0
- package/src/snapshots.d.ts +68 -0
- package/src/snapshots.js +306 -0
- package/src/source-types.d.ts +61 -0
- package/src/source-types.js +1 -0
- package/src/sources/command.d.ts +4 -0
- package/src/sources/command.js +77 -0
- package/src/sources/folder.d.ts +6 -0
- package/src/sources/folder.js +116 -0
- package/src/sources/ftp.d.ts +4 -0
- package/src/sources/ftp.js +307 -0
- package/src/sources/http.d.ts +4 -0
- package/src/sources/http.js +201 -0
- package/src/sources/mqtt.d.ts +18 -0
- package/src/sources/mqtt.js +97 -0
- package/src/sources/telegram.d.ts +22 -0
- package/src/sources/telegram.js +283 -0
- package/src/sources/webhook.d.ts +4 -0
- package/src/sources/webhook.js +52 -0
- package/src/sources.d.ts +344 -0
- package/src/sources.js +966 -0
- package/src/store.d.ts +99 -0
- package/src/store.js +277 -0
- package/src/timers.d.ts +83 -0
- package/src/timers.js +318 -0
- package/src/tools.d.ts +11 -0
- package/src/tools.js +1297 -0
- package/src/tui/app.d.ts +305 -0
- package/src/tui/app.js +3195 -0
- package/src/tui/client.d.ts +236 -0
- package/src/tui/client.js +131 -0
- package/src/tui/code-pane.d.ts +68 -0
- package/src/tui/code-pane.js +535 -0
- package/src/tui/highlight.d.ts +8 -0
- package/src/tui/highlight.js +86 -0
- package/src/tui/picture.d.ts +206 -0
- package/src/tui/picture.js +801 -0
- package/src/tui/run.d.ts +8 -0
- package/src/tui/run.js +97 -0
- package/src/tui/theme.d.ts +32 -0
- package/src/tui/theme.js +128 -0
- package/src/tui/undo.d.ts +5 -0
- package/src/tui/undo.js +50 -0
- package/src/tui/voice.d.ts +49 -0
- package/src/tui/voice.js +155 -0
- package/src/tui/voice.swift +223 -0
- package/src/types.d.ts +139 -0
- package/src/types.js +1 -0
- package/src/undo.d.ts +224 -0
- package/src/undo.js +465 -0
- package/src/update.d.ts +35 -0
- package/src/update.js +130 -0
- package/src/util.d.ts +65 -0
- package/src/util.js +171 -0
- package/src/watching.d.ts +20 -0
- package/src/watching.js +68 -0
- package/src/workspace-view.d.ts +72 -0
- package/src/workspace-view.js +221 -0
package/src/admin.js
ADDED
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
// What the main agent administers besides what every agent sets up for itself: the agents
|
|
2
|
+
// themselves and pine's settings. Nothing here is free text into a file. A change is a few named
|
|
3
|
+
// values, each checked on its own; it is written, the whole configuration is read back through
|
|
4
|
+
// the normal loader, and what does not pass is undone. The tools that call this are in
|
|
5
|
+
// src/tools.ts, and each of them asks the owner first.
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { AGENT_TOOLS } from "./agent-admin.js";
|
|
10
|
+
import { loadConfig, mainAgentId, MODES, PINE_ROOT, RESERVED_TOPICS, unsandboxed } from "./config.js";
|
|
11
|
+
import { unsetEnv } from "./env-file.js";
|
|
12
|
+
import { editJson } from "./mcp-config.js";
|
|
13
|
+
import { isInside, realish } from "./security.js";
|
|
14
|
+
import { errText, matchesAny, parseDuration } from "./util.js";
|
|
15
|
+
export const THINKING = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
16
|
+
const folder = (given) => {
|
|
17
|
+
const text = given.trim();
|
|
18
|
+
const full = text === "~" ? os.homedir() : text.startsWith("~/") ? path.join(os.homedir(), text.slice(2)) : text;
|
|
19
|
+
if (!path.isAbsolute(full))
|
|
20
|
+
throw new Error(`"${given}" is not a full path. A folder is given from the root, for example ${path.join(os.homedir(), "Documents")}`);
|
|
21
|
+
return realish(full);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Check a change to an agent and say what it does, line by line, with what makes it wider.
|
|
25
|
+
* Nothing is written. Throws with what is wrong and how it would be right.
|
|
26
|
+
*/
|
|
27
|
+
export function checkAgentChange(cfg, agent, change, models) {
|
|
28
|
+
const lines = [];
|
|
29
|
+
const warnings = [];
|
|
30
|
+
const steps = [];
|
|
31
|
+
const main = agent.id === mainAgentId(cfg);
|
|
32
|
+
if (change.description !== undefined) {
|
|
33
|
+
const text = change.description.replace(/\s+/g, " ").trim();
|
|
34
|
+
if (!text)
|
|
35
|
+
throw new Error("a description is one line on what the agent is for; it cannot be empty");
|
|
36
|
+
lines.push(`description: ${text}`);
|
|
37
|
+
steps.push((raw) => (raw.description = text));
|
|
38
|
+
}
|
|
39
|
+
if (change.model !== undefined) {
|
|
40
|
+
const none = change.model === "default" || change.model === "";
|
|
41
|
+
if (!none && !models.includes(change.model))
|
|
42
|
+
throw new Error(`there is no model "${change.model}". Models: ${models.slice(0, 40).join(", ")}`);
|
|
43
|
+
lines.push(`model: ${agent.model ?? `${cfg.defaults.model} (the default)`} -> ${none ? `${cfg.defaults.model} (the default)` : change.model}`);
|
|
44
|
+
steps.push((raw) => (none ? delete raw.model : (raw.model = change.model)));
|
|
45
|
+
}
|
|
46
|
+
if (change.thinking !== undefined) {
|
|
47
|
+
if (!THINKING.includes(change.thinking))
|
|
48
|
+
throw new Error(`"thinking" is one of ${THINKING.join(", ")}`);
|
|
49
|
+
lines.push(`reasoning: ${agent.thinking} -> ${change.thinking}`);
|
|
50
|
+
steps.push((raw) => (raw.thinking = change.thinking));
|
|
51
|
+
}
|
|
52
|
+
if (change.mode !== undefined) {
|
|
53
|
+
if (change.mode !== "default" && !MODES.includes(change.mode))
|
|
54
|
+
throw new Error(`"mode" is one of ${MODES.join(", ")}, or default`);
|
|
55
|
+
const next = change.mode === "default" ? (cfg.defaults.mode ?? (main ? "auto" : "safe")) : change.mode;
|
|
56
|
+
lines.push(`mode: ${agent.mode} -> ${next}${change.mode === "default" ? " (the default)" : ""}`);
|
|
57
|
+
if (next === "bypass")
|
|
58
|
+
warnings.push("in bypass mode it never asks, and its shell reaches your whole account and the network");
|
|
59
|
+
else if (next === "auto" && !unsandboxed(agent.level))
|
|
60
|
+
warnings.push("in auto mode its shell and file tools work without a sandbox: they reach your whole user account and the network in your own conversations");
|
|
61
|
+
steps.push((raw) => {
|
|
62
|
+
if (change.mode === "default")
|
|
63
|
+
delete raw.mode;
|
|
64
|
+
else
|
|
65
|
+
raw.mode = change.mode;
|
|
66
|
+
// What said this before the mode did. Left in place, it would decide again.
|
|
67
|
+
if (raw.sandbox && typeof raw.sandbox === "object")
|
|
68
|
+
delete raw.sandbox.scope;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (change.tools !== undefined) {
|
|
72
|
+
const unknown = change.tools.filter((t) => !AGENT_TOOLS.includes(t));
|
|
73
|
+
if (unknown.length)
|
|
74
|
+
throw new Error(`unknown tool${unknown.length === 1 ? "" : "s"} ${unknown.join(", ")}. An agent can have: ${AGENT_TOOLS.join(", ")}`);
|
|
75
|
+
lines.push(`built-in tools: ${(agent.tools ?? AGENT_TOOLS).join(", ")} -> ${change.tools.join(", ") || "none"}`);
|
|
76
|
+
steps.push((raw) => (raw.tools = change.tools));
|
|
77
|
+
}
|
|
78
|
+
if (change.delegates !== undefined) {
|
|
79
|
+
const unknown = change.delegates.filter((d) => d !== "*" && (!cfg.agents[d] || d === agent.id));
|
|
80
|
+
if (unknown.length)
|
|
81
|
+
throw new Error(`"delegates" names ${unknown.join(", ")}, which ${unknown.length === 1 ? "is" : "are"} not another agent. Agents: ${Object.keys(cfg.agents).filter((id) => id !== agent.id).join(", ") || "none"}; "*" is every agent`);
|
|
82
|
+
lines.push(`may hand work to: ${agent.delegates.join(", ") || "nobody"} -> ${change.delegates.join(", ") || "nobody"}`);
|
|
83
|
+
steps.push((raw) => (raw.delegates = change.delegates));
|
|
84
|
+
}
|
|
85
|
+
if (change.network !== undefined) {
|
|
86
|
+
lines.push(`network from its shell: ${agent.sandbox.network ? "yes" : "no"} -> ${change.network ? "yes" : "no"}`);
|
|
87
|
+
if (change.network && !agent.sandbox.network)
|
|
88
|
+
warnings.push("with the network its commands can send what it reads anywhere");
|
|
89
|
+
steps.push((raw) => {
|
|
90
|
+
raw.sandbox = typeof raw.sandbox === "object" && raw.sandbox ? raw.sandbox : {};
|
|
91
|
+
if (change.network)
|
|
92
|
+
raw.sandbox.network = true;
|
|
93
|
+
else
|
|
94
|
+
delete raw.sandbox.network;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const closed = (p) => {
|
|
98
|
+
const about = (root) => isInside(root, p) || isInside(p, root);
|
|
99
|
+
const other = Object.values(cfg.agents).find((a) => a.id !== agent.id && isInside(realish(a.workspace), p));
|
|
100
|
+
if (other)
|
|
101
|
+
return `that is the workspace of the agent ${other.id}; agents exchange work with delegate, not through each other's files`;
|
|
102
|
+
if (about(realish(cfg.home)))
|
|
103
|
+
return "pine's own folder stays closed to every agent";
|
|
104
|
+
if (about(realish(PINE_ROOT)))
|
|
105
|
+
return "pine's own code is not given to an agent";
|
|
106
|
+
return Object.values(cfg.agents).some((a) => a.id !== agent.id && about(realish(a.workspace))) ? "another agent's workspace lies in it" : undefined;
|
|
107
|
+
};
|
|
108
|
+
const adds = { read: (change.read ?? []).map(folder), write: (change.write ?? []).map(folder) };
|
|
109
|
+
const drops = (change.forget ?? []).map(folder);
|
|
110
|
+
for (const kind of ["read", "write"]) {
|
|
111
|
+
for (const p of adds[kind]) {
|
|
112
|
+
const no = closed(p);
|
|
113
|
+
if (no)
|
|
114
|
+
throw new Error(`${p} cannot be opened for ${agent.id}: ${no}`);
|
|
115
|
+
if (!fs.existsSync(p))
|
|
116
|
+
throw new Error(`${p} does not exist. Give a folder that is there`);
|
|
117
|
+
lines.push(`may ${kind === "write" ? "read and change" : "read"}: ${p}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const p of drops) {
|
|
121
|
+
if (![...agent.sandbox.read, ...agent.sandbox.write].some((x) => realish(x) === p))
|
|
122
|
+
throw new Error(`${agent.id} has no folder ${p} to forget. It has: ${[...new Set([...agent.sandbox.read, ...agent.sandbox.write])].join(", ") || "none"}`);
|
|
123
|
+
lines.push(`no longer: ${p}`);
|
|
124
|
+
}
|
|
125
|
+
if (adds.read.length || adds.write.length || drops.length) {
|
|
126
|
+
steps.push((raw) => {
|
|
127
|
+
raw.sandbox = typeof raw.sandbox === "object" && raw.sandbox ? raw.sandbox : {};
|
|
128
|
+
const next = (list, add) => [...new Set([...(Array.isArray(list) ? list.map(String) : []), ...add])].filter((x) => !drops.includes(realish(path.resolve(agent.dir, x))));
|
|
129
|
+
const read = next(raw.sandbox.read, adds.read);
|
|
130
|
+
const write = next(raw.sandbox.write, adds.write);
|
|
131
|
+
if (read.length)
|
|
132
|
+
raw.sandbox.read = read;
|
|
133
|
+
else
|
|
134
|
+
delete raw.sandbox.read;
|
|
135
|
+
if (write.length)
|
|
136
|
+
raw.sandbox.write = write;
|
|
137
|
+
else
|
|
138
|
+
delete raw.sandbox.write;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const instructions = change.instructions?.trim();
|
|
142
|
+
if (change.instructions !== undefined) {
|
|
143
|
+
if (!instructions)
|
|
144
|
+
throw new Error("instructions cannot be empty: they are who the agent is and how it works");
|
|
145
|
+
lines.push(`instructions (its AGENTS.md) replaced by:\n${instructions.length > 1200 ? `${instructions.slice(0, 1200)}...` : instructions}`);
|
|
146
|
+
}
|
|
147
|
+
const soul = change.soul?.trim();
|
|
148
|
+
if (change.soul !== undefined) {
|
|
149
|
+
if (!soul)
|
|
150
|
+
throw new Error("soul cannot be empty: it is who the agent is, its character and tone");
|
|
151
|
+
lines.push(`character and tone (its SOUL.md) replaced by:\n${soul.length > 1200 ? `${soul.slice(0, 1200)}...` : soul}`);
|
|
152
|
+
}
|
|
153
|
+
if (!lines.length)
|
|
154
|
+
throw new Error("nothing to change: give at least one of description, instructions, soul, model, thinking, mode, tools, delegates, network, read, write, forget");
|
|
155
|
+
return { lines, warnings, json: (raw) => steps.forEach((s) => s(raw)), instructions, soul };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Write a checked change and take it over. The agent's file is read back through the loader
|
|
159
|
+
* with everything else: if the configuration does not load, the file is as it was and nothing
|
|
160
|
+
* has changed.
|
|
161
|
+
*/
|
|
162
|
+
export function applyAgentChange(cfg, agent, checked) {
|
|
163
|
+
const file = path.join(agent.dir, "agent.json");
|
|
164
|
+
const before = fs.readFileSync(file, "utf8");
|
|
165
|
+
try {
|
|
166
|
+
editJson(file, checked.json);
|
|
167
|
+
const fresh = loadConfig(cfg.home).agents[agent.id];
|
|
168
|
+
if (!fresh)
|
|
169
|
+
throw new Error("the agent was not found after the change");
|
|
170
|
+
// The same object stays: sessions, sources and schedules hold on to it.
|
|
171
|
+
Object.assign(agent, fresh);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
fs.writeFileSync(file, before);
|
|
175
|
+
throw new Error(`not changed: ${errText(e)}`);
|
|
176
|
+
}
|
|
177
|
+
// The workspace is the agent's to write. A file of that name is taken away and a new one
|
|
178
|
+
// made, never written through: what lies there could be a link to somewhere else.
|
|
179
|
+
for (const [name, text] of [["AGENTS.md", checked.instructions], ["SOUL.md", checked.soul]]) {
|
|
180
|
+
if (text === undefined)
|
|
181
|
+
continue;
|
|
182
|
+
const target = path.join(agent.workspace, name);
|
|
183
|
+
fs.rmSync(target, { force: true });
|
|
184
|
+
fs.writeFileSync(target, `${text}\n`, { mode: 0o600, flag: "wx" });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** The parts of the configuration that name an agent and stand in the way of removing it, or are corrected when it goes. */
|
|
188
|
+
export function namedBy(cfg, id) {
|
|
189
|
+
return {
|
|
190
|
+
routes: cfg.routes.filter((r) => r.agent === id).map((r) => r.name),
|
|
191
|
+
delegators: Object.values(cfg.agents).filter((a) => a.id !== id && a.delegates.includes(id)).map((a) => a.id),
|
|
192
|
+
sources: Object.entries(cfg.sources).filter(([, s]) => s.agents?.includes(id)).map(([name]) => name),
|
|
193
|
+
servers: Object.entries(cfg.mcp).filter(([, s]) => s.agents?.includes(id)).map(([name]) => name),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** Take an agent's name out of the lists that name it: other agents' delegates, and the sources and servers of all agents that are only for some. */
|
|
197
|
+
export function unname(cfg, id) {
|
|
198
|
+
const named = namedBy(cfg, id);
|
|
199
|
+
for (const other of named.delegators) {
|
|
200
|
+
const a = cfg.agents[other];
|
|
201
|
+
a.delegates = a.delegates.filter((d) => d !== id);
|
|
202
|
+
editJson(path.join(a.dir, "agent.json"), (raw) => {
|
|
203
|
+
if (Array.isArray(raw.delegates))
|
|
204
|
+
raw.delegates = raw.delegates.filter((d) => d !== id);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (named.sources.length || named.servers.length) {
|
|
208
|
+
editJson(path.join(cfg.home, "pine.json"), (raw) => {
|
|
209
|
+
for (const name of named.sources)
|
|
210
|
+
if (Array.isArray(raw.sources?.[name]?.agents))
|
|
211
|
+
raw.sources[name].agents = raw.sources[name].agents.filter((a) => a !== id);
|
|
212
|
+
for (const name of named.servers)
|
|
213
|
+
if (Array.isArray(raw.mcp?.[name]?.agents))
|
|
214
|
+
raw.mcp[name].agents = raw.mcp[name].agents.filter((a) => a !== id);
|
|
215
|
+
});
|
|
216
|
+
for (const name of named.sources)
|
|
217
|
+
cfg.sources[name].agents = cfg.sources[name].agents.filter((a) => a !== id);
|
|
218
|
+
for (const name of named.servers)
|
|
219
|
+
cfg.mcp[name].agents = cfg.mcp[name].agents.filter((a) => a !== id);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Take what is left of an agent out of pine's files and store, so that nothing of it remains:
|
|
224
|
+
* the routes that wake it, its name in the lists of others, the secrets pine kept for its
|
|
225
|
+
* sources and servers, what is stored under its name (marks, lists, turns, tasks, pins, open
|
|
226
|
+
* tabs), the recorded states of its folders, its private home and its folder. This is the part
|
|
227
|
+
* that needs no running daemon: `AgentManager.removeAgent` stops what runs and then calls it,
|
|
228
|
+
* and `pine agent remove` calls it alone when no daemon runs.
|
|
229
|
+
*
|
|
230
|
+
* The folder is moved aside first and the configuration that is left is read back. If that
|
|
231
|
+
* does not load, the folder stays in `removed/` and `problem` says why. With `keepFiles` it
|
|
232
|
+
* stays there on purpose. A workspace outside pine's home is the owner's folder and is left.
|
|
233
|
+
*/
|
|
234
|
+
export function eraseAgent(cfg, store, id, keepFiles = false) {
|
|
235
|
+
const agent = cfg.agents[id];
|
|
236
|
+
if (!agent)
|
|
237
|
+
throw new Error(`there is no agent "${id}"`);
|
|
238
|
+
if (id === mainAgentId(cfg))
|
|
239
|
+
throw new Error("the main agent administers pine and cannot be removed");
|
|
240
|
+
// The keys pine put into .env for its sources and servers. One the owner wrote there under a name of their own stays.
|
|
241
|
+
let raw = "";
|
|
242
|
+
try {
|
|
243
|
+
raw = fs.readFileSync(path.join(agent.dir, "agent.json"), "utf8");
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// no file, no keys
|
|
247
|
+
}
|
|
248
|
+
unsetEnv(cfg.home, [...new Set([...raw.matchAll(/\$\{(PINE_(?:SOURCE|MCP)_[A-Z0-9_]+)\}/g)].map((m) => m[1]))]);
|
|
249
|
+
const routes = cfg.routes.filter((r) => r.agent === id).map((r) => r.name);
|
|
250
|
+
for (const name of routes)
|
|
251
|
+
dropRoute(cfg, name);
|
|
252
|
+
unname(cfg, id);
|
|
253
|
+
for (const prefix of ["taint", "keep", "folder", "todos", "turns", "undone"])
|
|
254
|
+
store.kv.forget(`${prefix}:${id}:`);
|
|
255
|
+
for (const key of store.kv.keys("timer:"))
|
|
256
|
+
if (key.endsWith(`@${id}`))
|
|
257
|
+
store.kv.set(key, null);
|
|
258
|
+
for (const key of store.kv.keys("source:"))
|
|
259
|
+
if (key.slice("source:".length).split(":")[0].endsWith(`@${id}`))
|
|
260
|
+
store.kv.forget(key);
|
|
261
|
+
const pinned = store.kv.get("conversations:pinned");
|
|
262
|
+
if (Array.isArray(pinned) && pinned.some((x) => x.startsWith(`${id}:`)))
|
|
263
|
+
store.kv.set("conversations:pinned", pinned.filter((x) => !x.startsWith(`${id}:`)));
|
|
264
|
+
const ui = store.kv.get("ui:tabs");
|
|
265
|
+
if (ui && Array.isArray(ui.tabs) && ui.tabs.some((t) => t.agent === id))
|
|
266
|
+
store.kv.set("ui:tabs", { ...ui, tabs: ui.tabs.filter((t) => t.agent !== id) });
|
|
267
|
+
store.tasks.forgetAgent(id);
|
|
268
|
+
// What was recorded of its folders for undo.
|
|
269
|
+
const workspace = realish(agent.workspace);
|
|
270
|
+
const dir = realish(agent.dir);
|
|
271
|
+
const snapshots = path.join(cfg.dataDir, "snapshots");
|
|
272
|
+
for (const name of fs.existsSync(snapshots) ? fs.readdirSync(snapshots) : []) {
|
|
273
|
+
let root = "";
|
|
274
|
+
try {
|
|
275
|
+
root = fs.readFileSync(path.join(snapshots, name, "pine-folder"), "utf8").trim();
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (isInside(workspace, root) || isInside(dir, root))
|
|
281
|
+
fs.rmSync(path.join(snapshots, name), { recursive: true, force: true });
|
|
282
|
+
}
|
|
283
|
+
for (const base of [path.join(cfg.dataDir, "programs"), path.join(cfg.dataDir, "browser-local")]) {
|
|
284
|
+
for (const name of fs.existsSync(base) ? fs.readdirSync(base) : [])
|
|
285
|
+
if (decodeURIComponent(name).startsWith(`${id}:`))
|
|
286
|
+
fs.rmSync(path.join(base, name), { recursive: true, force: true });
|
|
287
|
+
}
|
|
288
|
+
// What its event sources kept on disk ("name@agent").
|
|
289
|
+
const kept = path.join(cfg.dataDir, "sources");
|
|
290
|
+
for (const name of fs.existsSync(kept) ? fs.readdirSync(kept) : [])
|
|
291
|
+
if (name.endsWith(`@${id}`))
|
|
292
|
+
fs.rmSync(path.join(kept, name), { recursive: true, force: true });
|
|
293
|
+
const outside = !isInside(dir, workspace);
|
|
294
|
+
const aside = path.join(cfg.home, "removed", `${id}-${new Date().toISOString().replace(/[:.]/g, "-")}`);
|
|
295
|
+
fs.mkdirSync(path.dirname(aside), { recursive: true, mode: 0o700 });
|
|
296
|
+
fs.renameSync(agent.dir, aside);
|
|
297
|
+
delete cfg.agents[id];
|
|
298
|
+
fs.rmSync(path.join(cfg.home, "sandbox", id), { recursive: true, force: true });
|
|
299
|
+
let problem;
|
|
300
|
+
try {
|
|
301
|
+
loadConfig(cfg.home);
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
problem = errText(e);
|
|
305
|
+
}
|
|
306
|
+
const keep = keepFiles || problem !== undefined;
|
|
307
|
+
if (!keep) {
|
|
308
|
+
fs.rmSync(aside, { recursive: true, force: true });
|
|
309
|
+
// The folder that held it for a moment, if nothing else is kept there.
|
|
310
|
+
if (!fs.readdirSync(path.dirname(aside)).length)
|
|
311
|
+
fs.rmdirSync(path.dirname(aside));
|
|
312
|
+
}
|
|
313
|
+
return { routes, kept: keep ? aside : undefined, workspaceLeft: outside ? agent.workspace : undefined, problem };
|
|
314
|
+
}
|
|
315
|
+
const object = (text, what) => {
|
|
316
|
+
if (text === undefined || text.trim() === "")
|
|
317
|
+
return undefined;
|
|
318
|
+
let v;
|
|
319
|
+
try {
|
|
320
|
+
v = JSON.parse(text);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
throw new Error(`"${what}" must be JSON, for example {"payload.open": true}`);
|
|
324
|
+
}
|
|
325
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
326
|
+
throw new Error(`"${what}" must be a JSON object, for example {"payload.open": true}`);
|
|
327
|
+
return v;
|
|
328
|
+
};
|
|
329
|
+
/** Check a proposed route and say what it does, line by line. Nothing is written. */
|
|
330
|
+
export function checkRoute(cfg, input, models) {
|
|
331
|
+
const name = String(input.name ?? "").trim();
|
|
332
|
+
if (!/^[a-z][a-z0-9_-]{0,39}$/.test(name))
|
|
333
|
+
throw new Error(`"${name}" is not a usable route name: lowercase letters, digits, - and _, starting with a letter`);
|
|
334
|
+
if (cfg.routes.some((r) => r.name === name))
|
|
335
|
+
throw new Error(`a route called "${name}" exists already. remove_route takes it out; then add it again with what you want changed`);
|
|
336
|
+
const on = String(input.on ?? "").split(",").map((x) => x.trim()).filter(Boolean);
|
|
337
|
+
if (!on.length)
|
|
338
|
+
throw new Error('"on" is the topic the route listens to, for example source/house/message. The sources tool shows a source\'s topic');
|
|
339
|
+
const entry = { name, on: on.length === 1 ? on[0] : on };
|
|
340
|
+
const lines = [`on ${on.join(", ")}`];
|
|
341
|
+
const when = object(input.when, "when");
|
|
342
|
+
if (when) {
|
|
343
|
+
entry.when = when;
|
|
344
|
+
lines.push(`only when ${JSON.stringify(when)}`);
|
|
345
|
+
}
|
|
346
|
+
for (const [key, given] of [["throttleMs", input.throttle], ["debounceMs", input.debounce]]) {
|
|
347
|
+
if (given === undefined || given === "")
|
|
348
|
+
continue;
|
|
349
|
+
try {
|
|
350
|
+
if (!(parseDuration(given) > 0))
|
|
351
|
+
throw new Error("not a time");
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
throw new Error(`"${key === "throttleMs" ? "throttle" : "debounce"}" is a time such as 10s or 5m`);
|
|
355
|
+
}
|
|
356
|
+
entry[key] = given;
|
|
357
|
+
lines.push(key === "throttleMs" ? `after one event, ignore the next for ${given}` : `wait until ${given} have passed without a new event, then handle the last one`);
|
|
358
|
+
}
|
|
359
|
+
const set = object(input.set, "set");
|
|
360
|
+
if (set) {
|
|
361
|
+
entry.set = set;
|
|
362
|
+
lines.push(`set shared state: ${JSON.stringify(set)}`);
|
|
363
|
+
}
|
|
364
|
+
if (input.call_prompt) {
|
|
365
|
+
if (input.call_model && !models.includes(input.call_model))
|
|
366
|
+
throw new Error(`there is no model "${input.call_model}". Models: ${models.slice(0, 40).join(", ")}`);
|
|
367
|
+
entry.call = { ...(input.call_model ? { model: input.call_model } : {}), ...(input.call_system ? { system: input.call_system } : {}), prompt: input.call_prompt };
|
|
368
|
+
lines.push(`one model call without tools${input.call_model ? ` (${input.call_model})` : ""}: ${input.call_prompt}`);
|
|
369
|
+
}
|
|
370
|
+
else if (input.call_system || input.call_model)
|
|
371
|
+
throw new Error('a model call needs "call_prompt": what the model is asked');
|
|
372
|
+
if (input.publish_topic) {
|
|
373
|
+
const topic = input.publish_topic.trim();
|
|
374
|
+
if (matchesAny(RESERVED_TOPICS, topic))
|
|
375
|
+
throw new Error(`"${topic}" is a topic only pine itself publishes on. Pick one of your own, for example cam/classified`);
|
|
376
|
+
let payload;
|
|
377
|
+
if (input.publish_payload !== undefined && input.publish_payload !== "") {
|
|
378
|
+
try {
|
|
379
|
+
payload = JSON.parse(input.publish_payload);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
payload = input.publish_payload;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
entry.publish = { topic, ...(payload !== undefined ? { payload } : {}) };
|
|
386
|
+
lines.push(`publish the event ${topic}${payload !== undefined ? ` with ${JSON.stringify(payload)}` : ""}${topic.startsWith("out/") ? " (this sends through a source)" : ""}`);
|
|
387
|
+
}
|
|
388
|
+
else if (input.publish_payload)
|
|
389
|
+
throw new Error('publishing needs "publish_topic"');
|
|
390
|
+
if (input.agent) {
|
|
391
|
+
if (!cfg.agents[input.agent])
|
|
392
|
+
throw new Error(`there is no agent "${input.agent}". Agents: ${Object.keys(cfg.agents).join(", ")}`);
|
|
393
|
+
if (entry.call)
|
|
394
|
+
throw new Error("a route makes a model call or wakes an agent, not both. Let the call publish its result, and a second route wake the agent on that topic");
|
|
395
|
+
entry.agent = input.agent;
|
|
396
|
+
if (input.prompt)
|
|
397
|
+
entry.prompt = input.prompt;
|
|
398
|
+
if (input.session)
|
|
399
|
+
entry.session = input.session;
|
|
400
|
+
lines.push(`wake the agent ${input.agent}${input.session ? ` (conversation: ${input.session})` : ""}${input.prompt ? ` with: ${input.prompt}` : " with the event"}`);
|
|
401
|
+
}
|
|
402
|
+
else if (input.prompt || input.session)
|
|
403
|
+
throw new Error('"prompt" and "session" belong to "agent": name the agent the route wakes');
|
|
404
|
+
if (!entry.set && !entry.call && !entry.publish && !entry.agent)
|
|
405
|
+
throw new Error("a route has to do something: give at least one of agent (wake an agent), set (write shared state), publish_topic (publish another event), call_prompt (one model call)");
|
|
406
|
+
let wider;
|
|
407
|
+
if (input.trust !== undefined && input.trust !== "") {
|
|
408
|
+
if (input.trust !== "owner" && input.trust !== "untrusted")
|
|
409
|
+
throw new Error('"trust" is owner or untrusted. Leave it out: an event keeps the trust it came with');
|
|
410
|
+
entry.trust = input.trust;
|
|
411
|
+
if (input.trust === "owner") {
|
|
412
|
+
lines.push("treat every event on it as your own words");
|
|
413
|
+
wider = `events on ${on.join(", ")} are then treated as coming from you: whoever can make such an event gets the agent's full tool set`;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return { entry, lines, wider };
|
|
417
|
+
}
|
|
418
|
+
/** Write a checked route into pine.json and take it over. If the configuration does not load with it, the file is as it was. */
|
|
419
|
+
export function addRoute(cfg, entry) {
|
|
420
|
+
const file = path.join(cfg.home, "pine.json");
|
|
421
|
+
const before = fs.readFileSync(file, "utf8");
|
|
422
|
+
try {
|
|
423
|
+
editJson(file, (raw) => {
|
|
424
|
+
raw.routes = Array.isArray(raw.routes) ? raw.routes : [];
|
|
425
|
+
raw.routes.push(entry);
|
|
426
|
+
});
|
|
427
|
+
const made = loadConfig(cfg.home).routes.find((r) => r.name === entry.name);
|
|
428
|
+
if (!made)
|
|
429
|
+
throw new Error("the route was not found after writing it");
|
|
430
|
+
cfg.routes.push(made);
|
|
431
|
+
return made;
|
|
432
|
+
}
|
|
433
|
+
catch (e) {
|
|
434
|
+
fs.writeFileSync(file, before);
|
|
435
|
+
throw new Error(`not added: ${errText(e)}`);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/** Take a route out of pine.json by its name. The routes that stay keep their names, also those that had none of their own. */
|
|
439
|
+
export function dropRoute(cfg, name) {
|
|
440
|
+
if (!cfg.routes.some((r) => r.name === name))
|
|
441
|
+
return false;
|
|
442
|
+
editJson(path.join(cfg.home, "pine.json"), (raw) => {
|
|
443
|
+
if (!Array.isArray(raw.routes))
|
|
444
|
+
return;
|
|
445
|
+
const named = raw.routes.map((r, i) => (r && typeof r === "object" && r.name === undefined ? { name: `route${i}`, ...r } : r));
|
|
446
|
+
raw.routes = named.filter((r) => r?.name !== name);
|
|
447
|
+
if (!raw.routes.length)
|
|
448
|
+
delete raw.routes;
|
|
449
|
+
});
|
|
450
|
+
cfg.routes.splice(0, cfg.routes.length, ...cfg.routes.filter((r) => r.name !== name));
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
/** The routes as a list for the agent. */
|
|
454
|
+
export function routesText(cfg) {
|
|
455
|
+
const does = (r) => [r.set ? `sets ${JSON.stringify(r.set)}` : "", r.call ? `asks a model: ${r.call.prompt}` : "", r.publish ? `publishes ${r.publish.topic}` : "", r.agent ? `wakes ${r.agent}${r.session ? ` (conversation: ${r.session})` : ""}${r.prompt ? ` with: ${r.prompt}` : ""}` : ""].filter(Boolean).join("; ");
|
|
456
|
+
return cfg.routes.map((r) => `- ${r.name}: on ${r.on.join(", ")}${r.when ? ` when ${JSON.stringify(r.when)}` : ""}${r.throttleMs ? ` · at most one per ${ms(r.throttleMs)}` : ""}${r.debounceMs ? ` · after ${ms(r.debounceMs)} of quiet` : ""}${r.trust === "owner" ? " · its events count as the owner's words" : ""}\n ${does(r)}`).join("\n");
|
|
457
|
+
}
|
|
458
|
+
/** What an agent has, as its files say it: what goes with it when it is removed. */
|
|
459
|
+
export function holdingsOf(cfg, id) {
|
|
460
|
+
const agent = cfg.agents[id];
|
|
461
|
+
if (!agent)
|
|
462
|
+
throw new Error(`there is no agent "${id}"`);
|
|
463
|
+
const names = (dir) => (fs.existsSync(dir) ? fs.readdirSync(dir).filter((n) => !n.startsWith(".")) : []);
|
|
464
|
+
return {
|
|
465
|
+
conversations: names(path.join(agent.dir, "sessions")).filter((n) => !/\.reset-\d+$/.test(n) && !n.endsWith("~u") && !n.startsWith("_t")).sort((a, b) => Number(b === "main") - Number(a === "main") || a.localeCompare(b)),
|
|
466
|
+
sources: Object.keys(agent.sources),
|
|
467
|
+
servers: Object.keys(agent.mcp),
|
|
468
|
+
schedules: agent.schedules.map((s) => s.name),
|
|
469
|
+
skills: names(path.join(agent.dir, "skills")),
|
|
470
|
+
routes: cfg.routes.filter((r) => r.agent === id).map((r) => r.name),
|
|
471
|
+
files: countFiles(agent.workspace),
|
|
472
|
+
outside: !isInside(realish(agent.dir), realish(agent.workspace)),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/** How many files lie in a folder, without following links. Stops counting at a limit. */
|
|
476
|
+
export function countFiles(dir, limit = 5000) {
|
|
477
|
+
let n = 0;
|
|
478
|
+
const walk = (d) => {
|
|
479
|
+
let entries = [];
|
|
480
|
+
try {
|
|
481
|
+
entries = fs.readdirSync(d, { withFileTypes: true });
|
|
482
|
+
}
|
|
483
|
+
catch {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
for (const e of entries) {
|
|
487
|
+
if (n >= limit)
|
|
488
|
+
return;
|
|
489
|
+
if (e.isDirectory())
|
|
490
|
+
walk(path.join(d, e.name));
|
|
491
|
+
else
|
|
492
|
+
n++;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
walk(dir);
|
|
496
|
+
return n;
|
|
497
|
+
}
|
|
498
|
+
const ms = (n) => (n % 60_000 === 0 ? `${n / 60_000}m` : n % 1000 === 0 ? `${n / 1000}s` : `${n}ms`);
|
|
499
|
+
/**
|
|
500
|
+
* The settings in pine.json that the main agent can change. Not here on purpose: the control
|
|
501
|
+
* API (address, port, token), secrets, and the lists that have tools of their own (sources, MCP
|
|
502
|
+
* servers, skills, schedules, source types, model servers, agents).
|
|
503
|
+
*/
|
|
504
|
+
export const SETTINGS = {
|
|
505
|
+
"defaults.model": { about: "The model of every agent that has none of its own, as provider/model.", kind: "model", now: (c) => String(c.defaults.model ?? "none"), live: true },
|
|
506
|
+
"defaults.thinking": { about: "How much an agent reasons before it answers, unless it says otherwise.", kind: { one: THINKING }, now: (c) => c.defaults.thinking, live: true },
|
|
507
|
+
"defaults.mode": {
|
|
508
|
+
about: "The mode of every agent that names none: auto (no sandbox: your whole account and the network; critical actions are checked), safe (in the sandbox: its own folders), manual (in the sandbox, every action waits), bypass (no sandbox, nothing waits), plan (nothing is done). Not set: auto for the main agent, safe for the others.",
|
|
509
|
+
kind: { one: [...MODES] },
|
|
510
|
+
now: (c) => c.defaults.mode ?? "not set (main: auto, others: safe)",
|
|
511
|
+
live: true,
|
|
512
|
+
wider: (v) => (v === "bypass" ? "in bypass mode no agent asks, and every agent's shell reaches your whole account and the network" : v === "auto" ? "every agent that names no mode then works without a sandbox: its shell and file tools reach your whole account and the network" : undefined),
|
|
513
|
+
},
|
|
514
|
+
"defaults.bashTimeoutSec": { about: "Seconds after which a shell command is ended.", kind: { from: 5, to: 86_400 }, now: (c) => String(c.defaults.bashTimeoutSec), live: false },
|
|
515
|
+
"defaults.taskTimeoutSec": { about: "Seconds after which a task is ended.", kind: { from: 30, to: 604_800 }, now: (c) => String(c.defaults.taskTimeoutSec), live: false },
|
|
516
|
+
"limits.maxConcurrentRuns": { about: "How many agents work at the same time. On one local model, more than it can serve side by side only makes each slower.", kind: { from: 1, to: 64 }, now: (c) => String(c.limits.maxConcurrentRuns), live: false },
|
|
517
|
+
"limits.maxDelegationDepth": { about: "How many times a task can be handed on from agent to agent.", kind: { from: 0, to: 10 }, now: (c) => String(c.limits.maxDelegationDepth), live: true },
|
|
518
|
+
"limits.maxDownloadMb": { about: "The largest file the download tool fetches, in megabytes.", kind: { from: 1, to: 100_000 }, now: (c) => String(c.limits.maxDownloadMb), live: true },
|
|
519
|
+
"defaults.judge": { about: "The model that judges whether a goal is met, as provider/model. Not set: the agent's own.", kind: "model", now: (c) => c.defaults.judge ?? "not set (the agent's own model)", live: true },
|
|
520
|
+
"security.askAccess": { about: "Whether pine asks your owner itself when a call of an agent needs a file or folder outside its sandbox, or the network.", kind: "boolean", now: (c) => String(c.security.askAccess), live: true },
|
|
521
|
+
"security.approvalTimeout": { about: "How long a question to your owner waits before it counts as not answered, for example 5m.", kind: "duration", now: (c) => ms(c.security.approvalTimeoutMs), live: false },
|
|
522
|
+
"security.taint": {
|
|
523
|
+
about: "How long a conversation counts as having read outside content: session (until it is reset), run (for that one task), off (never).",
|
|
524
|
+
kind: { one: ["session", "run", "off"] },
|
|
525
|
+
now: (c) => c.security.taint,
|
|
526
|
+
live: true,
|
|
527
|
+
wider: (v) => (v === "off" ? "with off, outside content is never marked, and nothing that depends on the mark protects you" : v === "run" ? "with run, the mark is gone with the next message although what was read is still in the conversation" : undefined),
|
|
528
|
+
},
|
|
529
|
+
"security.bypassWhenTainted": {
|
|
530
|
+
about: "Whether bypass mode stays in force in a conversation that has read outside content.",
|
|
531
|
+
kind: "boolean",
|
|
532
|
+
now: (c) => String(c.security.bypassWhenTainted),
|
|
533
|
+
live: true,
|
|
534
|
+
wider: (v) => (v === true ? "an agent in bypass mode then acts without any question on whatever a web page or a message told it" : undefined),
|
|
535
|
+
},
|
|
536
|
+
};
|
|
537
|
+
const kindText = (k) => (k === "model" ? "a model, as provider/model" : k === "boolean" ? "true or false" : k === "duration" ? "a time such as 90s or 5m" : "one" in k ? k.one.join(", ") : `a number from ${k.from} to ${k.to}`);
|
|
538
|
+
/** The settings as a list for the agent: name, what is in force, what it is. */
|
|
539
|
+
export function settingsText(cfg) {
|
|
540
|
+
return Object.entries(SETTINGS)
|
|
541
|
+
.map(([key, s]) => `${key} = ${s.now(cfg)}\n ${s.about} Value: ${kindText(s.kind)}.${s.live ? "" : " A change holds from the next start of pine."}`)
|
|
542
|
+
.join("\n");
|
|
543
|
+
}
|
|
544
|
+
/** Check a value for a setting. "default" (or nothing) means: take the entry out, so that pine's default holds. */
|
|
545
|
+
export function checkSetting(cfg, key, given, models) {
|
|
546
|
+
const setting = SETTINGS[key];
|
|
547
|
+
if (!setting)
|
|
548
|
+
throw new Error(`there is no setting "${key}" that you can change. The settings tool lists them: ${Object.keys(SETTINGS).join(", ")}. What an agent has (sources, MCP servers, skills, schedules) and the agents themselves are changed with their own tools.`);
|
|
549
|
+
const raw = typeof given === "string" ? given.trim() : given;
|
|
550
|
+
if (raw === undefined || raw === null || raw === "" || raw === "default")
|
|
551
|
+
return { key, setting, value: undefined, text: "pine's default" };
|
|
552
|
+
const k = setting.kind;
|
|
553
|
+
let value;
|
|
554
|
+
if (k === "model") {
|
|
555
|
+
if (!models.includes(String(raw)))
|
|
556
|
+
throw new Error(`there is no model "${raw}". Models: ${models.slice(0, 40).join(", ")}`);
|
|
557
|
+
value = String(raw);
|
|
558
|
+
}
|
|
559
|
+
else if (k === "boolean") {
|
|
560
|
+
if (raw !== true && raw !== false && raw !== "true" && raw !== "false")
|
|
561
|
+
throw new Error(`${key} is true or false`);
|
|
562
|
+
value = raw === true || raw === "true";
|
|
563
|
+
}
|
|
564
|
+
else if (k === "duration") {
|
|
565
|
+
let n = Number.NaN;
|
|
566
|
+
try {
|
|
567
|
+
n = parseDuration(String(raw));
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
// said below
|
|
571
|
+
}
|
|
572
|
+
if (!(n >= 1000))
|
|
573
|
+
throw new Error(`${key} is a time of at least a second, for example 90s or 5m`);
|
|
574
|
+
value = String(raw);
|
|
575
|
+
}
|
|
576
|
+
else if ("one" in k) {
|
|
577
|
+
if (!k.one.includes(String(raw)))
|
|
578
|
+
throw new Error(`${key} is one of: ${k.one.join(", ")}`);
|
|
579
|
+
value = String(raw);
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
const n = Number(raw);
|
|
583
|
+
if (!Number.isInteger(n) || n < k.from || n > k.to)
|
|
584
|
+
throw new Error(`${key} is a whole number from ${k.from} to ${k.to}`);
|
|
585
|
+
value = n;
|
|
586
|
+
}
|
|
587
|
+
return { key, setting, value, text: String(value), wider: setting.wider?.(value) };
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Write a checked setting into pine.json and take it over. The file is read back through the
|
|
591
|
+
* loader; if the configuration does not load, the file is as it was.
|
|
592
|
+
*/
|
|
593
|
+
export function applySetting(cfg, checked) {
|
|
594
|
+
const file = path.join(cfg.home, "pine.json");
|
|
595
|
+
const before = fs.readFileSync(file, "utf8");
|
|
596
|
+
const [section, name] = checked.key.split(".");
|
|
597
|
+
let fresh;
|
|
598
|
+
try {
|
|
599
|
+
editJson(file, (raw) => {
|
|
600
|
+
raw[section] = typeof raw[section] === "object" && raw[section] ? raw[section] : {};
|
|
601
|
+
if (checked.value === undefined)
|
|
602
|
+
delete raw[section][name];
|
|
603
|
+
else
|
|
604
|
+
raw[section][name] = checked.value;
|
|
605
|
+
});
|
|
606
|
+
fresh = loadConfig(cfg.home);
|
|
607
|
+
}
|
|
608
|
+
catch (e) {
|
|
609
|
+
fs.writeFileSync(file, before);
|
|
610
|
+
throw new Error(`not changed: ${errText(e)}`);
|
|
611
|
+
}
|
|
612
|
+
if (!checked.setting.live)
|
|
613
|
+
return;
|
|
614
|
+
Object.assign(cfg.defaults, fresh.defaults);
|
|
615
|
+
Object.assign(cfg.limits, fresh.limits);
|
|
616
|
+
// What a question to the owner waits is read once at the start: it keeps its value until then.
|
|
617
|
+
Object.assign(cfg.security, { ...fresh.security, approvalTimeoutMs: cfg.security.approvalTimeoutMs });
|
|
618
|
+
// What an agent takes from the defaults when it does not say.
|
|
619
|
+
if (checked.key === "defaults.thinking")
|
|
620
|
+
for (const a of Object.values(cfg.agents))
|
|
621
|
+
a.thinking = fresh.agents[a.id]?.thinking ?? a.thinking;
|
|
622
|
+
if (checked.key === "defaults.mode")
|
|
623
|
+
for (const a of Object.values(cfg.agents))
|
|
624
|
+
Object.assign(a, { mode: fresh.agents[a.id]?.mode ?? a.mode, level: fresh.agents[a.id]?.level ?? a.level, modeSet: fresh.agents[a.id]?.modeSet ?? a.modeSet });
|
|
625
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type AgentConfig, type PineConfig } from "./config.ts";
|
|
2
|
+
/** The built-in tools an agent can be given. */
|
|
3
|
+
export declare const AGENT_TOOLS: string[];
|
|
4
|
+
export interface NewAgent {
|
|
5
|
+
id: string;
|
|
6
|
+
/** One line: what it is for. Other agents see it when they decide whom to hand work to. */
|
|
7
|
+
description: string;
|
|
8
|
+
/** How it works: its AGENTS.md, in its workspace, which it can change itself from then on. */
|
|
9
|
+
instructions: string;
|
|
10
|
+
model?: string;
|
|
11
|
+
tools?: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create an agent while the daemon runs: its folder with agent.json, and a workspace with its
|
|
15
|
+
* instructions (AGENTS.md) and the other files it keeps itself, as they come with a new home. The files are written first and then read back through the normal loader, so a new
|
|
16
|
+
* agent is checked exactly like one that was there at start; if it does not pass, the folder is
|
|
17
|
+
* taken away again. Nothing but these few lines goes into its agent.json.
|
|
18
|
+
*/
|
|
19
|
+
/** What is wrong with a proposed agent, as an error; nothing is written. Returns the cleaned-up values. */
|
|
20
|
+
export declare function checkNewAgent(cfg: PineConfig, spec: NewAgent): {
|
|
21
|
+
description: string;
|
|
22
|
+
tools: string[];
|
|
23
|
+
};
|
|
24
|
+
export declare function createAgent(cfg: PineConfig, spec: NewAgent): AgentConfig;
|