@operad-io/bot 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yinghai Zhao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # @operad-io/bot
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **The bot plugin.** A bot is a working directory, a default agent, skills, a speaker and an access profile. It is the parent of its chats. Optional: turn it on with `operad plugin enable bot`.
4
+
5
+ This package is part of [operad](https://github.com/operad-io/operad). Most people install only [`@operad-io/cli`](https://www.npmjs.com/package/@operad-io/cli).
6
+
7
+ ## Documentation
8
+
9
+ - User guide: [docs/user/chats-and-bots.md](https://github.com/operad-io/operad/blob/main/docs/user/chats-and-bots.md)
10
+ - Website: <https://operad.io>
11
+ - Developer reference: `REFERENCE.md` in this directory
12
+
13
+ MIT licensed.
@@ -0,0 +1,159 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://operad.io/schemas/bot.schema.json",
4
+ "title": "operad bot settings",
5
+ "description": "The `bot` section of config.json, owned by the bot plugin.",
6
+ "type": "object",
7
+ "properties": {
8
+ "bots": {
9
+ "type": "object",
10
+ "description": "The bots, by name. `default` exists even when it is not written here.",
11
+ "additionalProperties": {
12
+ "type": "object",
13
+ "description": "One bot.",
14
+ "properties": {
15
+ "cwd": {
16
+ "type": "string",
17
+ "minLength": 1,
18
+ "description": "The working directory. Without it: ~/.operad/bot for `default`, ~/.operad/bot/<name> for any other. It must be inside ~/.operad/bot or outside the operad home."
19
+ },
20
+ "harness": {
21
+ "type": "string",
22
+ "minLength": 1,
23
+ "description": "The agent a new chat uses when it names none."
24
+ },
25
+ "skills": {
26
+ "type": "array",
27
+ "items": {
28
+ "type": "string",
29
+ "minLength": 1
30
+ },
31
+ "description": "Directories with a SKILL.md, given to the agent at every reply. A relative path is under the working directory."
32
+ },
33
+ "model": {
34
+ "type": "string",
35
+ "minLength": 1,
36
+ "description": "The model a new chat starts with; it must be one the chat's agent lists. Without it the agent's default."
37
+ },
38
+ "effort": {
39
+ "type": "string",
40
+ "minLength": 1,
41
+ "description": "The effort a new chat starts with; it must be one the chat's agent lists. Without it the agent's default."
42
+ },
43
+ "speaker": {
44
+ "type": "string",
45
+ "minLength": 1,
46
+ "description": "How a chat of this bot labels the model's messages in its transcript. Without it the chat plugin's speaker is used."
47
+ },
48
+ "mcpServers": {
49
+ "type": "object",
50
+ "description": "The local MCP servers this bot's chats mount, by name. A server is added only through `bot mcp`, which a person approves; the digest is of its tool names and descriptions, and a chat that finds other tools does not mount the server.",
51
+ "additionalProperties": {
52
+ "type": "object",
53
+ "description": "One local MCP server.",
54
+ "properties": {
55
+ "command": {
56
+ "type": "string",
57
+ "minLength": 1,
58
+ "description": "The program that starts the server."
59
+ },
60
+ "args": {
61
+ "type": "array",
62
+ "items": {
63
+ "type": "string"
64
+ },
65
+ "description": "Its arguments."
66
+ },
67
+ "package": {
68
+ "type": "string",
69
+ "minLength": 1,
70
+ "description": "The package the command runs, when it runs one."
71
+ },
72
+ "version": {
73
+ "type": "string",
74
+ "minLength": 1,
75
+ "description": "The pinned version of that package."
76
+ },
77
+ "secrets": {
78
+ "type": "array",
79
+ "items": {
80
+ "type": "string",
81
+ "minLength": 1
82
+ },
83
+ "description": "Environment variables the server needs. Each value is the bot plugin's secret `<server>.<variable>`, given to the server only when it starts."
84
+ },
85
+ "tools": {
86
+ "type": "array",
87
+ "description": "The tools the server offered when a person approved it.",
88
+ "items": {
89
+ "type": "object",
90
+ "properties": {
91
+ "name": {
92
+ "type": "string",
93
+ "minLength": 1
94
+ },
95
+ "description": {
96
+ "type": "string"
97
+ }
98
+ },
99
+ "additionalProperties": false
100
+ }
101
+ },
102
+ "digest": {
103
+ "type": "string",
104
+ "minLength": 1,
105
+ "description": "The digest of the tool names and descriptions."
106
+ }
107
+ },
108
+ "additionalProperties": false
109
+ }
110
+ },
111
+ "mcpAllow": {
112
+ "type": "array",
113
+ "items": {
114
+ "type": "string",
115
+ "minLength": 1
116
+ },
117
+ "description": "MCP tools that run without asking, as `<server>.<tool>` or `<server>.*`. A tool not listed counts as a write and asks first."
118
+ },
119
+ "access": {
120
+ "type": "object",
121
+ "description": "Added to the default access profile: the lists are appended and readOnly can be turned on. A bot can tighten the default, never loosen it.",
122
+ "properties": {
123
+ "denyPaths": {
124
+ "type": "array",
125
+ "items": {
126
+ "type": "string"
127
+ }
128
+ },
129
+ "denyCommands": {
130
+ "type": "array",
131
+ "items": {
132
+ "type": "string"
133
+ }
134
+ },
135
+ "askCommands": {
136
+ "type": "array",
137
+ "items": {
138
+ "type": "string"
139
+ }
140
+ },
141
+ "askPaths": {
142
+ "type": "array",
143
+ "items": {
144
+ "type": "string"
145
+ }
146
+ },
147
+ "readOnly": {
148
+ "type": "boolean"
149
+ }
150
+ },
151
+ "additionalProperties": false
152
+ }
153
+ },
154
+ "additionalProperties": false
155
+ }
156
+ }
157
+ },
158
+ "additionalProperties": false
159
+ }
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@operad-io/bot",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./src/index.ts"
7
+ },
8
+ "dependencies": {
9
+ "@operad-io/chat": "0.1.0",
10
+ "@operad-io/engine": "0.1.0",
11
+ "@operad-io/access-match": "0.1.0"
12
+ },
13
+ "description": "operad: the bot plugin",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/operad-io/operad.git",
17
+ "directory": "packages/bot"
18
+ },
19
+ "license": "MIT",
20
+ "files": [
21
+ "src",
22
+ "bot.schema.json",
23
+ "README.md",
24
+ "LICENSE"
25
+ ]
26
+ }
package/src/bot.ts ADDED
@@ -0,0 +1,384 @@
1
+ /**
2
+ * Bots: the `bot` section of `config.json`, read at every use, and what a bot
3
+ * means (its working directory, harness, skills, access profile and speaker).
4
+ * The kernel validates the section against {@link BOT_SETTINGS} when it loads;
5
+ * a reply reads the file again, so an edit applies at once.
6
+ */
7
+
8
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
9
+ import { homedir } from "node:os";
10
+ import { isAbsolute, join, relative, resolve } from "node:path";
11
+ import { MCP_REPORT_DIR } from "./mcp-constants.ts";
12
+ import { configPath, isRecord, prepareSettings, type ActionArgsSchema, type JsonValue, type ObjectStore } from "@operad-io/engine";
13
+
14
+ /** The object type of a bot. */
15
+ export const BOT_TYPE = "bot";
16
+
17
+ /** The bot every home has. */
18
+ export const DEFAULT_BOT = "default";
19
+
20
+ /** The directory of the bot inside the operad home. */
21
+ export const BOT_DIR = "bot";
22
+
23
+ const NAME = /^[a-z0-9][a-z0-9_-]*$/;
24
+
25
+ /** The JSON Schema of the `bot` section (also shipped as `bot.schema.json`). */
26
+ export const BOT_SETTINGS: ActionArgsSchema = {
27
+ type: "object",
28
+ properties: {
29
+ bots: {
30
+ type: "object",
31
+ editable: true,
32
+ description:
33
+ "The bots, by name. `default` exists even when it is not written here.",
34
+ additionalProperties: {
35
+ type: "object",
36
+ description: "One bot.",
37
+ properties: {
38
+ cwd: {
39
+ type: "string",
40
+ minLength: 1,
41
+ description:
42
+ "The working directory. Without it: ~/.operad/bot for `default`, ~/.operad/bot/<name> for any other. It must be inside ~/.operad/bot or outside the operad home.",
43
+ },
44
+ harness: {
45
+ type: "string",
46
+ minLength: 1,
47
+ description: "The agent a new chat uses when it names none.",
48
+ },
49
+ skills: {
50
+ type: "array",
51
+ items: { type: "string", minLength: 1 },
52
+ description:
53
+ "Directories with a SKILL.md, given to the agent at every reply. A relative path is under the working directory.",
54
+ },
55
+ model: {
56
+ type: "string",
57
+ minLength: 1,
58
+ description: "The model a new chat starts with; it must be one the chat's agent lists. Without it the agent's default.",
59
+ },
60
+ effort: {
61
+ type: "string",
62
+ minLength: 1,
63
+ description: "The effort a new chat starts with; it must be one the chat's agent lists. Without it the agent's default.",
64
+ },
65
+ speaker: {
66
+ type: "string",
67
+ minLength: 1,
68
+ description:
69
+ "How a chat of this bot labels the model's messages in its transcript. Without it the chat plugin's speaker is used.",
70
+ },
71
+ mcpServers: {
72
+ type: "object",
73
+ description:
74
+ "The local MCP servers this bot's chats mount, by name. A server is added only through `bot mcp`, which a person approves; the digest is of its tool names and descriptions, and a chat that finds other tools does not mount the server.",
75
+ additionalProperties: {
76
+ type: "object",
77
+ description: "One local MCP server.",
78
+ properties: {
79
+ command: { type: "string", minLength: 1, description: "The program that starts the server." },
80
+ args: { type: "array", items: { type: "string" }, description: "Its arguments." },
81
+ package: { type: "string", minLength: 1, description: "The package the command runs, when it runs one." },
82
+ version: { type: "string", minLength: 1, description: "The pinned version of that package." },
83
+ secrets: {
84
+ type: "array",
85
+ items: { type: "string", minLength: 1 },
86
+ description:
87
+ "Environment variables the server needs. Each value is the bot plugin's secret `<server>.<variable>`, given to the server only when it starts.",
88
+ },
89
+ tools: {
90
+ type: "array",
91
+ description: "The tools the server offered when a person approved it.",
92
+ items: {
93
+ type: "object",
94
+ properties: {
95
+ name: { type: "string", minLength: 1 },
96
+ description: { type: "string" },
97
+ },
98
+ additionalProperties: false,
99
+ },
100
+ },
101
+ digest: { type: "string", minLength: 1, description: "The digest of the tool names and descriptions." },
102
+ },
103
+ additionalProperties: false,
104
+ },
105
+ },
106
+ mcpAllow: {
107
+ type: "array",
108
+ items: { type: "string", minLength: 1 },
109
+ description:
110
+ "MCP tools that run without asking, as `<server>.<tool>` or `<server>.*`. A tool not listed counts as a write and asks first.",
111
+ },
112
+ access: {
113
+ type: "object",
114
+ description:
115
+ "Added to the default access profile: the lists are appended and readOnly can be turned on. A bot can tighten the default, never loosen it.",
116
+ properties: {
117
+ denyPaths: { type: "array", items: { type: "string" } },
118
+ denyCommands: { type: "array", items: { type: "string" } },
119
+ askCommands: { type: "array", items: { type: "string" } },
120
+ askPaths: { type: "array", items: { type: "string" } },
121
+ readOnly: { type: "boolean" },
122
+ },
123
+ additionalProperties: false,
124
+ },
125
+ },
126
+ additionalProperties: false,
127
+ },
128
+ },
129
+ },
130
+ additionalProperties: false,
131
+ };
132
+
133
+ /** One local MCP server of a bot, as written in its settings. */
134
+ export interface McpServerSettings {
135
+ readonly command: string;
136
+ readonly args?: readonly string[] | undefined;
137
+ readonly package?: string | undefined;
138
+ readonly version?: string | undefined;
139
+ readonly secrets?: readonly string[] | undefined;
140
+ readonly tools?: readonly { readonly name: string; readonly description: string }[] | undefined;
141
+ readonly digest?: string | undefined;
142
+ }
143
+
144
+ /** One bot as written. */
145
+ export interface BotSettings {
146
+ readonly mcpServers?: Readonly<Record<string, McpServerSettings>> | undefined;
147
+ readonly mcpAllow?: readonly string[] | undefined;
148
+ readonly cwd?: string;
149
+ readonly harness?: string;
150
+ readonly model?: string;
151
+ readonly effort?: string;
152
+ readonly skills?: readonly string[];
153
+ readonly speaker?: string;
154
+ readonly access?: Readonly<Record<string, JsonValue>>;
155
+ }
156
+
157
+ /** A bot as a chat uses it. */
158
+ export interface ResolvedBot {
159
+ readonly name: string;
160
+ readonly cwd: string;
161
+ readonly harness: string | null;
162
+ readonly model: string | null;
163
+ readonly effort: string | null;
164
+ /** Absolute directories, each with a SKILL.md. */
165
+ readonly skills: readonly string[];
166
+ readonly speaker: string | null;
167
+ readonly access: Readonly<Record<string, JsonValue>>;
168
+ /** The local MCP servers the bot lists, by name. */
169
+ readonly mcpServers: Readonly<Record<string, McpServerSettings>>;
170
+ }
171
+
172
+ /** A bot that cannot be used; the message says why. */
173
+ export class BotError extends Error {
174
+ constructor(message: string) {
175
+ super(message);
176
+ this.name = "BotError";
177
+ }
178
+ }
179
+
180
+ /** The `bot` working directory of a home. */
181
+ export function botDir(home: string): string {
182
+ return join(home, BOT_DIR);
183
+ }
184
+
185
+ /**
186
+ * The bots written in the home's `config.json`, with the default one added
187
+ * when absent. The file is read now; `fallback` (the settings the kernel
188
+ * loaded) stands in only when the file or its section is missing.
189
+ */
190
+ export function readBots(
191
+ home: string,
192
+ fallback: Readonly<Record<string, JsonValue>> = {},
193
+ ): Readonly<Record<string, BotSettings>> {
194
+ let section: unknown = fallback;
195
+ const path = configPath(home);
196
+ if (existsSync(path)) {
197
+ let parsed: unknown;
198
+ try {
199
+ parsed = JSON.parse(readFileSync(path, "utf8"));
200
+ } catch (error) {
201
+ throw new BotError(`${path}: cannot read the bot settings: ${String(error)}`);
202
+ }
203
+ const plugins = isRecord(parsed) ? parsed.plugins : undefined;
204
+ const entry = isRecord(plugins) ? plugins.bot : undefined;
205
+ if (isRecord(entry) && entry.settings !== undefined) section = entry.settings;
206
+ }
207
+ const settings = prepareSettings(BOT_SETTINGS, section, "bot");
208
+ const bots = isRecord(settings.bots) ? settings.bots : {};
209
+ const found: Record<string, BotSettings> = {};
210
+ for (const [name, value] of Object.entries(bots)) {
211
+ if (!NAME.test(name)) {
212
+ throw new BotError(
213
+ `bot "${name}": a name is lowercase letters, digits, "-" and "_", and starts with a letter or digit`,
214
+ );
215
+ }
216
+ found[name] = value as BotSettings;
217
+ }
218
+ if (found[DEFAULT_BOT] === undefined) found[DEFAULT_BOT] = {};
219
+ return found;
220
+ }
221
+
222
+ function expand(path: string, base: string): string {
223
+ if (path === "~") return homedir();
224
+ if (path.startsWith("~/")) return join(homedir(), path.slice(2));
225
+ return isAbsolute(path) ? resolve(path) : resolve(base, path);
226
+ }
227
+
228
+ function within(path: string, directory: string): boolean {
229
+ const rel = relative(directory, path);
230
+ return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
231
+ }
232
+
233
+ /**
234
+ * The default access profile of a bot: no credentials, no secrets, no operad
235
+ * command (the agent reaches operad through its tools). Everything in the
236
+ * operad home except the bot's own directory is denied: the names that are
237
+ * there now, and the ones operad keeps there. Paths are absolute, so a home
238
+ * other than `~/.operad` is covered too.
239
+ */
240
+ export function defaultAccess(home: string): Record<string, JsonValue> {
241
+ const inHome = new Set(["store", "secrets", "config.json", MCP_REPORT_DIR]);
242
+ if (existsSync(home)) {
243
+ for (const name of readdirSync(home)) if (name !== BOT_DIR) inHome.add(name);
244
+ }
245
+ return {
246
+ denyPaths: [
247
+ "~/.ssh",
248
+ "~/.gnupg",
249
+ "~/.aws",
250
+ "~/.config/gh",
251
+ "~/.radian",
252
+ "~/Library/Keychains",
253
+ "~/Library/Application Support/Google/Chrome",
254
+ "~/Library/Application Support/Firefox",
255
+ "~/Library/Safari",
256
+ ".git/config",
257
+ ".git/hooks",
258
+ ...[...inHome].sort().map((name) => join(home, name)),
259
+ ],
260
+ denyCommands: [
261
+ "operad",
262
+ "sudo",
263
+ "su",
264
+ "rm -rf /",
265
+ "rm -rf ~",
266
+ "mkfs",
267
+ "dd",
268
+ "diskutil",
269
+ "shutdown",
270
+ "reboot",
271
+ "launchctl",
272
+ "crontab -r",
273
+ "git push --force*",
274
+ "git push --delete",
275
+ "curl … | sh",
276
+ "wget … | sh",
277
+ "pkill",
278
+ "killall",
279
+ ],
280
+ askCommands: ["git push", "brew install", "npm install -g", "pip install", "osascript", "open"],
281
+ askPaths: ["~/.zshrc", "~/.*profile"],
282
+ };
283
+ }
284
+
285
+ function appended(base: JsonValue | undefined, extra: unknown): JsonValue[] {
286
+ const list = Array.isArray(base) ? [...base] : [];
287
+ if (Array.isArray(extra)) {
288
+ for (const item of extra) if (typeof item === "string" && !list.includes(item)) list.push(item);
289
+ }
290
+ return list;
291
+ }
292
+
293
+ /** The access profile of a bot: the default, tightened by what the bot adds. */
294
+ export function accessFor(
295
+ home: string,
296
+ settings: BotSettings,
297
+ repositories: readonly string[] = [],
298
+ skillPaths: readonly string[] = [],
299
+ ): Record<string, JsonValue> {
300
+ const access = defaultAccess(home);
301
+ // A repository is reached through operad's tools (dev's verbs), never by
302
+ // touching its files, so its path is denied to the agent. Skill directories
303
+ // are changed only through a proposal a person confirms, so they are denied
304
+ // to the agent too (the harness still receives their paths as skills).
305
+ access.denyPaths = appended(access.denyPaths, [...repositories, ...skillPaths]);
306
+ const own = settings.access ?? {};
307
+ for (const field of ["denyPaths", "denyCommands", "askCommands", "askPaths"] as const) {
308
+ access[field] = appended(access[field], own[field]);
309
+ }
310
+ if (own.readOnly === true) access.readOnly = true;
311
+ // The field puts MCP tools under the profile: one not listed asks first.
312
+ access.allowMcp = appended([], settings.mcpAllow);
313
+ return access;
314
+ }
315
+
316
+ /** The skill directories of a bot, checked now. */
317
+ function skillDirectories(settings: BotSettings, cwd: string): string[] {
318
+ return (settings.skills ?? []).map((written) => {
319
+ const path = expand(written, cwd);
320
+ const file = join(path, "SKILL.md");
321
+ if (!existsSync(file) || !statSync(file).isFile()) {
322
+ throw new BotError(`skill "${written}": there is no SKILL.md in ${path}`);
323
+ }
324
+ return path;
325
+ });
326
+ }
327
+
328
+ /** The working directory of a bot; refused when it would put the operad home in reach. */
329
+ export function workingDirectory(home: string, name: string, settings: BotSettings): string {
330
+ const root = botDir(home);
331
+ const cwd =
332
+ settings.cwd === undefined
333
+ ? name === DEFAULT_BOT
334
+ ? root
335
+ : join(root, name)
336
+ : expand(settings.cwd, root);
337
+ if (within(home, cwd)) {
338
+ throw new BotError(
339
+ `bot "${name}": the working directory ${cwd} is the operad home or holds it; use a subdirectory of ${root}`,
340
+ );
341
+ }
342
+ if (within(cwd, home) && !within(cwd, root)) {
343
+ throw new BotError(
344
+ `bot "${name}": the working directory ${cwd} is inside the operad home but outside ${root}`,
345
+ );
346
+ }
347
+ return cwd;
348
+ }
349
+
350
+ /** The paths of the repositories dev knows, from the object store. */
351
+ export async function repositoryPaths(store: ObjectStore): Promise<string[]> {
352
+ const found: string[] = [];
353
+ let cursor: string | undefined;
354
+ do {
355
+ const page = await store.list("repo", { ...(cursor === undefined ? {} : { cursor }) });
356
+ for (const object of page.objects) {
357
+ if (typeof object.data.path === "string" && object.data.path.length > 0) found.push(object.data.path);
358
+ }
359
+ cursor = page.next;
360
+ } while (cursor !== undefined);
361
+ return found;
362
+ }
363
+
364
+ /** One bot as a chat uses it now; the working directory is not created here. */
365
+ export function resolveBot(
366
+ home: string,
367
+ name: string,
368
+ settings: BotSettings,
369
+ repositories: readonly string[] = [],
370
+ ): ResolvedBot {
371
+ const cwd = workingDirectory(home, name, settings);
372
+ const skills = skillDirectories(settings, cwd);
373
+ return {
374
+ name,
375
+ cwd,
376
+ harness: settings.harness ?? null,
377
+ model: settings.model ?? null,
378
+ effort: settings.effort ?? null,
379
+ skills,
380
+ speaker: settings.speaker ?? null,
381
+ access: accessFor(home, settings, repositories, [...skills, join(cwd, "skills")]),
382
+ mcpServers: settings.mcpServers ?? {},
383
+ };
384
+ }