@operad-io/bot 0.1.0 → 0.2.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/README.md +1 -1
- package/bot.schema.json +40 -2
- package/package.json +5 -4
- package/src/bot.ts +588 -70
- package/src/chat-watch.ts +101 -0
- package/src/create.ts +230 -0
- package/src/index.ts +466 -22
- package/src/login.ts +226 -0
- package/src/mcp-proposals.ts +2 -2
- package/src/proposals.ts +358 -78
- package/src/schedule.ts +959 -0
- package/src/setup-dev.ts +254 -0
- package/src/setup.ts +113 -0
- package/src/tidy.ts +1018 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watching a chat's reply from a bot source: one read per check, no second
|
|
3
|
+
* check while one runs, and a stopped watch that no longer reads. Both the
|
|
4
|
+
* scheduled prompts and the nightly tidy end a reply this way.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { messageOf, type ObjectStore, type RunControl } from "@operad-io/engine";
|
|
8
|
+
import { CHAT_TYPE, readChat, writingReplyNow, type ChatRead } from "@operad-io/chat";
|
|
9
|
+
|
|
10
|
+
/** One chat's watch: how to stop it. */
|
|
11
|
+
interface ChatWatch {
|
|
12
|
+
readonly stop: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The watches of one bot source, and the work they and its polls started. */
|
|
16
|
+
export class ChatWatches {
|
|
17
|
+
private readonly watches = new Map<string, ChatWatch>();
|
|
18
|
+
private readonly work = new Set<Promise<void>>();
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly logger: { write(text: string): Promise<string> },
|
|
22
|
+
private readonly store: ObjectStore,
|
|
23
|
+
private readonly runs?: RunControl | undefined,
|
|
24
|
+
) {}
|
|
25
|
+
|
|
26
|
+
async log(message: string): Promise<void> {
|
|
27
|
+
await this.logger.write(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
track(work: Promise<void>): void {
|
|
31
|
+
const tracked = work
|
|
32
|
+
.catch((error: unknown) => this.log(messageOf(error)))
|
|
33
|
+
.finally(() => this.work.delete(tracked));
|
|
34
|
+
this.work.add(tracked);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Waits for the checks that are running now; a test calls this after a reply. */
|
|
38
|
+
async settled(): Promise<void> {
|
|
39
|
+
while (this.work.size > 0) await Promise.allSettled([...this.work]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
stopAll(): void {
|
|
43
|
+
for (const watch of [...this.watches.values()]) watch.stop();
|
|
44
|
+
this.watches.clear();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Watches one chat until `ready` answers a value: a reply that is still
|
|
49
|
+
* writing is not ready, and a watch stopped by its key or by `stopAll` reads
|
|
50
|
+
* no more. `done` runs once, after the watch stopped.
|
|
51
|
+
*/
|
|
52
|
+
watchChatReply<Value>(
|
|
53
|
+
key: string,
|
|
54
|
+
chatId: string,
|
|
55
|
+
ready: (read: ChatRead) => Promise<Value | undefined> | Value | undefined,
|
|
56
|
+
done: (value: Value) => Promise<void>,
|
|
57
|
+
): void {
|
|
58
|
+
if (this.watches.has(key)) return;
|
|
59
|
+
let running = false;
|
|
60
|
+
let again = false;
|
|
61
|
+
let stopped = false;
|
|
62
|
+
let unsubscribe: (() => void) | undefined;
|
|
63
|
+
const check = async (): Promise<void> => {
|
|
64
|
+
if (running) {
|
|
65
|
+
again = true;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (stopped) return;
|
|
69
|
+
running = true;
|
|
70
|
+
try {
|
|
71
|
+
const read = readChat(this.store, chatId);
|
|
72
|
+
// A reply that goes round again is not finished at its first message.
|
|
73
|
+
if ((await writingReplyNow(read.facts, read.messages, read.entries, this.runs)) !== undefined) return;
|
|
74
|
+
const value = await ready(read);
|
|
75
|
+
if (value === undefined) return;
|
|
76
|
+
stopped = true;
|
|
77
|
+
unsubscribe?.();
|
|
78
|
+
this.watches.delete(key);
|
|
79
|
+
await done(value);
|
|
80
|
+
} finally {
|
|
81
|
+
running = false;
|
|
82
|
+
if (again && !stopped) {
|
|
83
|
+
again = false;
|
|
84
|
+
this.track(check());
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const blocking: ChatWatch = {
|
|
89
|
+
stop: () => {
|
|
90
|
+
stopped = true;
|
|
91
|
+
unsubscribe?.();
|
|
92
|
+
this.watches.delete(key);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
this.watches.set(key, blocking);
|
|
96
|
+
unsubscribe = this.store.subscribe({ type: CHAT_TYPE, id: chatId }, {}, () => {
|
|
97
|
+
this.track(check());
|
|
98
|
+
});
|
|
99
|
+
this.track(check());
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/create.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Making a bot from what this home already knows: the skills other bots mount,
|
|
3
|
+
* the skills in the standard directories, and the MCP servers a person has
|
|
4
|
+
* already approved on another bot. Only what is chosen is written; nothing is
|
|
5
|
+
* inherited from `default`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import { join, resolve } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
BotError,
|
|
12
|
+
DEFAULT_BOT,
|
|
13
|
+
botDir,
|
|
14
|
+
expandPath,
|
|
15
|
+
listedSkills,
|
|
16
|
+
readBots,
|
|
17
|
+
resolveBot,
|
|
18
|
+
skillName,
|
|
19
|
+
validateBotName,
|
|
20
|
+
workingDirectory,
|
|
21
|
+
type BotSettings,
|
|
22
|
+
type McpServerSettings,
|
|
23
|
+
} from "./bot.ts";
|
|
24
|
+
import { secretName } from "./mcp.ts";
|
|
25
|
+
import { updateBotsFile, validateBots } from "./proposals.ts";
|
|
26
|
+
|
|
27
|
+
/** One skill this home already knows, by the name a bot page shows. */
|
|
28
|
+
export interface SkillChoice {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly path: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One MCP server a person approved on a bot, and the bot it came from. */
|
|
34
|
+
export interface McpServerChoice {
|
|
35
|
+
/** The value a person chooses: the server name, or `<bot>/<server>` when two bots differ. */
|
|
36
|
+
readonly key: string;
|
|
37
|
+
readonly bot: string;
|
|
38
|
+
readonly server: string;
|
|
39
|
+
readonly entry: McpServerSettings;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The working directory used to read a bot's relative skill paths, even when the bot is broken. */
|
|
43
|
+
function cwdOf(home: string, name: string, settings: BotSettings): string {
|
|
44
|
+
try {
|
|
45
|
+
return workingDirectory(home, name, settings);
|
|
46
|
+
} catch {
|
|
47
|
+
return join(botDir(home), name);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function addSkill(found: Map<string, SkillChoice>, path: string): void {
|
|
52
|
+
const normal = resolve(path);
|
|
53
|
+
if (found.has(normal)) return;
|
|
54
|
+
found.set(normal, { name: skillName(normal), path: normal });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The skills this home already knows: every bot's listed skills, including the
|
|
59
|
+
* ones inside a listed directory, and the skills in `~/.agents/skills` when it
|
|
60
|
+
* exists. A bot that cannot be used contributes the paths that can be read.
|
|
61
|
+
*/
|
|
62
|
+
export function knownSkills(home: string, userHome: string = homedir()): readonly SkillChoice[] {
|
|
63
|
+
const found = new Map<string, SkillChoice>();
|
|
64
|
+
for (const [name, settings] of Object.entries(readBots(home))) {
|
|
65
|
+
const cwd = cwdOf(home, name, settings);
|
|
66
|
+
for (const written of settings.skills ?? []) {
|
|
67
|
+
for (const path of listedSkills(written, cwd).directories) addSkill(found, path);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const standard = join(userHome, ".agents", "skills");
|
|
71
|
+
for (const path of listedSkills(standard, userHome).directories) addSkill(found, path);
|
|
72
|
+
return [...found.values()].sort(
|
|
73
|
+
(left, right) => left.name.localeCompare(right.name) || left.path.localeCompare(right.path),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The MCP servers approved on a bot. An entry is approved when its tool digest
|
|
79
|
+
* is set, which is what makes a chat mount it. A name approved with different
|
|
80
|
+
* entries on several bots is offered as `<bot>/<server>`.
|
|
81
|
+
*/
|
|
82
|
+
export function knownMcpServers(home: string): readonly McpServerChoice[] {
|
|
83
|
+
const grouped = new Map<string, { readonly bot: string; readonly entry: McpServerSettings }[]>();
|
|
84
|
+
for (const [bot, settings] of Object.entries(readBots(home))) {
|
|
85
|
+
for (const [server, entry] of Object.entries(settings.mcpServers ?? {})) {
|
|
86
|
+
if (entry.digest === undefined) continue;
|
|
87
|
+
const group = grouped.get(server) ?? [];
|
|
88
|
+
group.push({ bot, entry });
|
|
89
|
+
grouped.set(server, group);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const found: McpServerChoice[] = [];
|
|
93
|
+
for (const [server, group] of grouped) {
|
|
94
|
+
const same = new Set(group.map((item) => JSON.stringify(item.entry))).size === 1;
|
|
95
|
+
if (group.length === 1 || same) {
|
|
96
|
+
const first = group[0];
|
|
97
|
+
if (first !== undefined) found.push({ key: server, bot: first.bot, server, entry: first.entry });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
for (const item of group) {
|
|
101
|
+
found.push({ key: `${item.bot}/${server}`, bot: item.bot, server, entry: item.entry });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return found.sort((left, right) => left.key.localeCompare(right.key));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** One server entry as it was approved, copied field by field. */
|
|
108
|
+
function copyEntry(entry: McpServerSettings): McpServerSettings {
|
|
109
|
+
return {
|
|
110
|
+
command: entry.command,
|
|
111
|
+
...(entry.args === undefined ? {} : { args: [...entry.args] }),
|
|
112
|
+
...(entry.package === undefined ? {} : { package: entry.package }),
|
|
113
|
+
...(entry.version === undefined ? {} : { version: entry.version }),
|
|
114
|
+
...(entry.secrets === undefined ? {} : { secrets: [...entry.secrets] }),
|
|
115
|
+
...(entry.tools === undefined
|
|
116
|
+
? {}
|
|
117
|
+
: { tools: entry.tools.map((tool) => ({ name: tool.name, description: tool.description })) }),
|
|
118
|
+
...(entry.digest === undefined ? {} : { digest: entry.digest }),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function add(list: string[], value: string): void {
|
|
123
|
+
const normal = resolve(value);
|
|
124
|
+
if (!list.includes(normal)) list.push(normal);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface CreateBotInput {
|
|
128
|
+
readonly name: string;
|
|
129
|
+
readonly agent: string;
|
|
130
|
+
readonly model?: string | undefined;
|
|
131
|
+
readonly effort?: string | undefined;
|
|
132
|
+
readonly speaker?: string | undefined;
|
|
133
|
+
/** Known skills by name, or paths a bot's `skills` setting accepts. */
|
|
134
|
+
readonly skills?: readonly string[] | undefined;
|
|
135
|
+
/** Approved MCP servers, as their choice key. */
|
|
136
|
+
readonly mcpServers?: readonly string[] | undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface CreateBotOptions {
|
|
140
|
+
/** Where a relative skill path is read from; the home when absent. */
|
|
141
|
+
readonly cwd?: string | undefined;
|
|
142
|
+
readonly userHome?: string | undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** What creating a bot wrote, and the secrets its servers need to start. */
|
|
146
|
+
export interface CreatedBot {
|
|
147
|
+
readonly settings: BotSettings;
|
|
148
|
+
/** The secret names of the copied servers, as `<server>.<variable>`. */
|
|
149
|
+
readonly secrets: readonly string[];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Writes a new bot with exactly the given fields. The name is the same one a
|
|
154
|
+
* bot directory uses and an existing one is refused; skills are the known ones
|
|
155
|
+
* chosen or the paths given; each MCP server is copied from the bot that
|
|
156
|
+
* approved it. No setting comes from `default`.
|
|
157
|
+
*/
|
|
158
|
+
export async function createBot(
|
|
159
|
+
home: string,
|
|
160
|
+
input: CreateBotInput,
|
|
161
|
+
options: CreateBotOptions = {},
|
|
162
|
+
): Promise<CreatedBot> {
|
|
163
|
+
validateBotName(input.name);
|
|
164
|
+
if (input.agent.trim().length === 0) throw new BotError("a new bot needs an agent");
|
|
165
|
+
const bots = readBots(home);
|
|
166
|
+
if (bots[input.name] !== undefined) throw new BotError(`there is already a bot "${input.name}"`);
|
|
167
|
+
|
|
168
|
+
const known = knownSkills(home, options.userHome);
|
|
169
|
+
const byName = new Map<string, SkillChoice>();
|
|
170
|
+
const byPath = new Map<string, SkillChoice>();
|
|
171
|
+
for (const skill of known) {
|
|
172
|
+
if (!byName.has(skill.name)) byName.set(skill.name, skill);
|
|
173
|
+
byPath.set(resolve(skill.path), skill);
|
|
174
|
+
}
|
|
175
|
+
const cwd = options.cwd ?? home;
|
|
176
|
+
const skills: string[] = [];
|
|
177
|
+
const paths: string[] = [];
|
|
178
|
+
for (const value of input.skills ?? []) {
|
|
179
|
+
const chosen = byPath.get(resolve(value)) ?? byName.get(value);
|
|
180
|
+
if (chosen !== undefined) add(skills, chosen.path);
|
|
181
|
+
else paths.push(value);
|
|
182
|
+
}
|
|
183
|
+
for (const value of paths) {
|
|
184
|
+
const expanded = expandPath(value, cwd);
|
|
185
|
+
const listed = listedSkills(value, cwd);
|
|
186
|
+
if (listed.directories.length === 0 && !listed.agents) {
|
|
187
|
+
throw new BotError(`skill "${value}": there is no SKILL.md in ${expanded}`);
|
|
188
|
+
}
|
|
189
|
+
add(skills, expanded);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const knownServers = knownMcpServers(home);
|
|
193
|
+
const servers: Record<string, McpServerSettings> = {};
|
|
194
|
+
for (const value of input.mcpServers ?? []) {
|
|
195
|
+
const chosen = knownServers.find((item) => item.key === value);
|
|
196
|
+
if (chosen === undefined) {
|
|
197
|
+
throw new BotError(
|
|
198
|
+
`MCP server "${value}" is not approved on another bot; add it with operad bot mcp`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
servers[chosen.server] = copyEntry(chosen.entry);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const settings: Record<string, unknown> = { harness: input.agent };
|
|
205
|
+
if (input.model !== undefined) settings.model = input.model;
|
|
206
|
+
if (input.effort !== undefined) settings.effort = input.effort;
|
|
207
|
+
if (input.speaker !== undefined) settings.speaker = input.speaker;
|
|
208
|
+
if (skills.length > 0) settings.skills = skills;
|
|
209
|
+
if (Object.keys(servers).length > 0) settings.mcpServers = servers;
|
|
210
|
+
|
|
211
|
+
validateBots({ [input.name]: settings });
|
|
212
|
+
// Refuse a result that could not be used now, such as a skill that vanished.
|
|
213
|
+
resolveBot(home, input.name, settings as BotSettings);
|
|
214
|
+
|
|
215
|
+
await updateBotsFile(home, (written) => {
|
|
216
|
+
if (written[input.name] !== undefined) {
|
|
217
|
+
throw new BotError(`there is already a bot "${input.name}"`);
|
|
218
|
+
}
|
|
219
|
+
const next = { ...written, [input.name]: settings };
|
|
220
|
+
validateBots(next);
|
|
221
|
+
return next;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const secrets: string[] = [];
|
|
225
|
+
for (const [server, entry] of Object.entries(servers)) {
|
|
226
|
+
for (const variable of entry.secrets ?? []) secrets.push(secretName(server, variable));
|
|
227
|
+
}
|
|
228
|
+
return { settings: settings as BotSettings, secrets };
|
|
229
|
+
}
|
|
230
|
+
|