@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
package/src/index.ts
CHANGED
|
@@ -9,21 +9,34 @@
|
|
|
9
9
|
* built yet, and this is the one place it goes.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import { messageOf } from "@operad-io/engine";
|
|
12
13
|
import pkg from "../package.json" with { type: "json" };
|
|
13
14
|
import { mkdirSync } from "node:fs";
|
|
15
|
+
import { basename, isAbsolute, join, resolve } from "node:path";
|
|
14
16
|
import { fileURLToPath } from "node:url";
|
|
15
|
-
import { CHAT_TYPE, agentConditionals, type AgentChoices, chatData, createChat, sendMessage, startingEffort, startingModel, titleFromMessage } from "@operad-io/chat";
|
|
16
|
-
import { definePlugin, isRecord, type ActionArgsSchema, type ActionTypeDefinition, type JsonValue, type ObjectRow, type ObjectStore, type ObjectViews, type PluginStepResult, type SectionContent, type StepContext, type StoredObject } from "@operad-io/engine";
|
|
17
|
+
import { CHAT_TYPE, MESSAGE_EVENT, agentConditionals, type AgentChoices, type ScheduledChat, chatData, createChat, sendMessage, startingEffort, startingModel, titleFromMessage } from "@operad-io/chat";
|
|
18
|
+
import { definePlugin, isRecord, modelIds, type ActionArgSchema, type ActionArgsSchema, type ActionTypeDefinition, type HarnessModelInfo, type HarnessModels, type JsonValue, type ObjectRow, type ObjectStore, type ObjectViews, type PluginStepResult, type SectionContent, type StepContext, type StoredObject } from "@operad-io/engine";
|
|
17
19
|
import {
|
|
18
20
|
BOT_SETTINGS,
|
|
19
21
|
DEFAULT_BOT,
|
|
20
22
|
BOT_TYPE,
|
|
21
23
|
BotError,
|
|
24
|
+
botDir,
|
|
25
|
+
defaultBotHomeMigrated,
|
|
26
|
+
ensureBotMemory,
|
|
27
|
+
migrateDefaultBotDir,
|
|
22
28
|
readBots,
|
|
23
29
|
repositoryPaths,
|
|
24
30
|
resolveBot,
|
|
31
|
+
skillName,
|
|
32
|
+
workingDirectory,
|
|
33
|
+
type BotSettings,
|
|
25
34
|
type ResolvedBot,
|
|
26
35
|
} from "./bot.ts";
|
|
36
|
+
import { MEMORY_COMMIT_STEP, memoryNotes } from "@operad-io/memory";
|
|
37
|
+
import { createBot, knownMcpServers, knownSkills } from "./create.ts";
|
|
38
|
+
import { LOGIN_EVENT, LOGIN_SOURCE, loginAction, loginFlow, loginItems, runLogin as runLoginStep } from "./login.ts";
|
|
39
|
+
import { chatSetupAction, runSetup, setupDevPrompt, setupPrompt, setupTitle } from "./setup.ts";
|
|
27
40
|
import { ingestReports, runMcpCommand } from "./mcp-proposals.ts";
|
|
28
41
|
import {
|
|
29
42
|
PROPOSAL_TYPE,
|
|
@@ -31,8 +44,26 @@ import {
|
|
|
31
44
|
proposalItems,
|
|
32
45
|
proposalViews,
|
|
33
46
|
runSettingCommand,
|
|
47
|
+
runScheduleCommand,
|
|
48
|
+
runSetupDev,
|
|
34
49
|
runSkillCommand,
|
|
35
50
|
} from "./proposals.ts";
|
|
51
|
+
import {
|
|
52
|
+
SCHEDULE_SOURCE,
|
|
53
|
+
scheduleActions,
|
|
54
|
+
scheduledChatContext,
|
|
55
|
+
scheduledData,
|
|
56
|
+
scheduledMarkdown,
|
|
57
|
+
scheduledPrompts,
|
|
58
|
+
startScheduled,
|
|
59
|
+
} from "./schedule.ts";
|
|
60
|
+
import {
|
|
61
|
+
TIDY_TYPE,
|
|
62
|
+
startTidy,
|
|
63
|
+
tidyActions,
|
|
64
|
+
tidyItems,
|
|
65
|
+
tidyViews,
|
|
66
|
+
} from "./tidy.ts";
|
|
36
67
|
|
|
37
68
|
/** The step a chat calls at every reply to read its bot. */
|
|
38
69
|
export const SETTINGS_STEP = "bot/settings";
|
|
@@ -43,10 +74,12 @@ function rowOf(bot: ResolvedBot): Record<string, JsonValue> {
|
|
|
43
74
|
name: bot.name,
|
|
44
75
|
cwd: bot.cwd,
|
|
45
76
|
harness: bot.harness,
|
|
77
|
+
useOwnSettings: bot.useOwnSettings,
|
|
46
78
|
model: bot.model,
|
|
47
79
|
effort: bot.effort,
|
|
48
80
|
skills: [...bot.skills],
|
|
49
81
|
speaker: bot.speaker,
|
|
82
|
+
writeWiki: bot.writeWiki,
|
|
50
83
|
access: bot.access,
|
|
51
84
|
mcpServers: Object.keys(bot.mcpServers),
|
|
52
85
|
};
|
|
@@ -55,6 +88,50 @@ function rowOf(bot: ResolvedBot): Record<string, JsonValue> {
|
|
|
55
88
|
/** The harnesses that take MCP servers natively; any other does not mount a bot's servers. */
|
|
56
89
|
export const MCP_HARNESSES: readonly string[] = ["claude", "radian"];
|
|
57
90
|
|
|
91
|
+
/** Moves the old default bot home once, unless the default bot names its own directory. */
|
|
92
|
+
async function migrateDefaultHome(
|
|
93
|
+
store: ObjectStore,
|
|
94
|
+
home: string,
|
|
95
|
+
written: Readonly<Record<string, BotSettings>>,
|
|
96
|
+
): Promise<void> {
|
|
97
|
+
const settings = written[DEFAULT_BOT];
|
|
98
|
+
if (settings === undefined) return;
|
|
99
|
+
try {
|
|
100
|
+
if (workingDirectory(home, DEFAULT_BOT, settings) !== join(botDir(home), DEFAULT_BOT)) return;
|
|
101
|
+
} catch {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (defaultBotHomeMigrated(home)) return;
|
|
105
|
+
// A bot's directory may have a different name from the bot and may already be gone
|
|
106
|
+
// from the settings; both its stored cwd and its default path are kept in place.
|
|
107
|
+
const botPaths: string[] = [];
|
|
108
|
+
for (const [name, bot] of Object.entries(written)) {
|
|
109
|
+
if (name === DEFAULT_BOT) continue;
|
|
110
|
+
try {
|
|
111
|
+
botPaths.push(workingDirectory(home, name, bot));
|
|
112
|
+
} catch {
|
|
113
|
+
botPaths.push(join(botDir(home), name));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let cursor: string | undefined;
|
|
117
|
+
do {
|
|
118
|
+
const page = await store.list(BOT_TYPE, { ...(cursor === undefined ? {} : { cursor }) });
|
|
119
|
+
for (const object of page.objects) {
|
|
120
|
+
if (object.id === DEFAULT_BOT) continue;
|
|
121
|
+
const cwd = object.data.cwd;
|
|
122
|
+
botPaths.push(typeof cwd === "string" && cwd.length > 0 ? cwd : join(botDir(home), object.id));
|
|
123
|
+
}
|
|
124
|
+
cursor = page.next;
|
|
125
|
+
} while (cursor !== undefined);
|
|
126
|
+
const repositories = await repositoryPaths(store);
|
|
127
|
+
const grantFiles = [
|
|
128
|
+
join(home, "access-user.json"),
|
|
129
|
+
...botPaths.map((path) => join(path, "access-user.json")),
|
|
130
|
+
...repositories.map((path) => join(path, ".operad", "access-user.json")),
|
|
131
|
+
];
|
|
132
|
+
await migrateDefaultBotDir(home, { botNames: Object.keys(written), botPaths, grantFiles });
|
|
133
|
+
}
|
|
134
|
+
|
|
58
135
|
const GUARD = fileURLToPath(new URL("./guard.ts", import.meta.url));
|
|
59
136
|
|
|
60
137
|
/** What a harness starts for one of a bot's servers: the guard, which starts the server itself. */
|
|
@@ -73,6 +150,7 @@ export async function syncBots(
|
|
|
73
150
|
fallback: Readonly<Record<string, JsonValue>>,
|
|
74
151
|
): Promise<void> {
|
|
75
152
|
const written = readBots(home, fallback);
|
|
153
|
+
await migrateDefaultHome(store, home, written);
|
|
76
154
|
const repositories = await repositoryPaths(store);
|
|
77
155
|
for (const [name, settings] of Object.entries(written)) {
|
|
78
156
|
let data: Record<string, JsonValue>;
|
|
@@ -83,6 +161,10 @@ export async function syncBots(
|
|
|
83
161
|
data = { name, removed: false, problem: error.message };
|
|
84
162
|
}
|
|
85
163
|
const current = await store.get(BOT_TYPE, name);
|
|
164
|
+
// A client's scheduled prompts belong to the bot object, not to config.json.
|
|
165
|
+
if (Array.isArray(current?.data.scheduled)) {
|
|
166
|
+
data.scheduled = current.data.scheduled as JsonValue[];
|
|
167
|
+
}
|
|
86
168
|
if (current === undefined || JSON.stringify(current.data) !== JSON.stringify(data)) {
|
|
87
169
|
await store.put(BOT_TYPE, name, data, "bot");
|
|
88
170
|
}
|
|
@@ -106,7 +188,7 @@ export interface OpenChat {
|
|
|
106
188
|
readonly settings: Readonly<Record<string, JsonValue>>;
|
|
107
189
|
readonly harnesses: readonly string[];
|
|
108
190
|
readonly bot: string;
|
|
109
|
-
readonly models?: Readonly<Record<string,
|
|
191
|
+
readonly models?: Readonly<Record<string, HarnessModels>> | undefined;
|
|
110
192
|
readonly efforts?: Readonly<Record<string, { readonly list: readonly string[]; readonly default: string | null }>> | undefined;
|
|
111
193
|
readonly harness?: string | undefined;
|
|
112
194
|
readonly model?: string | undefined;
|
|
@@ -114,14 +196,18 @@ export interface OpenChat {
|
|
|
114
196
|
readonly title?: string | undefined;
|
|
115
197
|
/** The channels (`telegram:<chat>`) whose messages land in this chat. */
|
|
116
198
|
readonly channels?: readonly string[] | undefined;
|
|
199
|
+
/** The scheduled prompt that opens the chat; absent when a person opens it. */
|
|
200
|
+
readonly scheduled?: ScheduledChat | undefined;
|
|
117
201
|
readonly cause: string;
|
|
118
202
|
}
|
|
119
203
|
|
|
120
204
|
/** Opens a chat on a bot and returns its id. Refuses with a plain message. */
|
|
121
205
|
export async function openChat(input: OpenChat): Promise<string> {
|
|
122
|
-
const written = readBots(input.home, input.settings)
|
|
123
|
-
|
|
124
|
-
const
|
|
206
|
+
const written = readBots(input.home, input.settings);
|
|
207
|
+
await migrateDefaultHome(input.store, input.home, written);
|
|
208
|
+
const settings = written[input.bot];
|
|
209
|
+
if (settings === undefined) throw new BotError(`there is no bot "${input.bot}"`);
|
|
210
|
+
const bot = resolveBot(input.home, input.bot, settings, await repositoryPaths(input.store));
|
|
125
211
|
const harness = input.harness ?? bot.harness ?? input.harnesses[0];
|
|
126
212
|
if (harness === undefined) {
|
|
127
213
|
throw new BotError("no agent is loaded; load one to chat with the bot");
|
|
@@ -138,10 +224,15 @@ export async function openChat(input: OpenChat): Promise<string> {
|
|
|
138
224
|
model = startingModel(harness, input.models?.[harness], input.model, bot.model ?? undefined);
|
|
139
225
|
effort = startingEffort(harness, input.efforts?.[harness], input.effort, bot.effort ?? undefined);
|
|
140
226
|
} catch (error) {
|
|
141
|
-
throw new BotError(
|
|
227
|
+
throw new BotError(messageOf(error));
|
|
142
228
|
}
|
|
143
229
|
const effortDefault = input.efforts?.[harness]?.default ?? undefined;
|
|
230
|
+
// A scheduled prompt runs at full access (ADR 0030 D10); a chat a person
|
|
231
|
+
// opens starts at ask (ADR 0026 D15). A person may still change the level
|
|
232
|
+
// with `/access` afterwards.
|
|
233
|
+
const startingLevel = input.scheduled === undefined ? bot.accessLevel : "full";
|
|
144
234
|
mkdirSync(bot.cwd, { recursive: true });
|
|
235
|
+
ensureBotMemory(bot.cwd, input.home);
|
|
145
236
|
return createChat(
|
|
146
237
|
input.store,
|
|
147
238
|
{
|
|
@@ -152,9 +243,12 @@ export async function openChat(input: OpenChat): Promise<string> {
|
|
|
152
243
|
...(effort === undefined || effortDefault === undefined ? {} : { effortDefault }),
|
|
153
244
|
cwd: bot.cwd,
|
|
154
245
|
access: bot.access,
|
|
246
|
+
accessLevel: startingLevel,
|
|
247
|
+
accessDefault: startingLevel,
|
|
155
248
|
skills: bot.skills,
|
|
156
249
|
title: input.title ?? `Bot (${bot.name})`,
|
|
157
250
|
...(input.channels === undefined ? {} : { channels: input.channels }),
|
|
251
|
+
...(input.scheduled === undefined ? {} : { scheduled: input.scheduled }),
|
|
158
252
|
refresh: SETTINGS_STEP,
|
|
159
253
|
},
|
|
160
254
|
input.cause,
|
|
@@ -168,6 +262,7 @@ function chatArgs(
|
|
|
168
262
|
models: readonly string[] = [],
|
|
169
263
|
efforts: readonly string[] = [],
|
|
170
264
|
agents: readonly AgentChoices[] = [],
|
|
265
|
+
info?: Readonly<Record<string, HarnessModelInfo>> | undefined,
|
|
171
266
|
): ActionArgsSchema {
|
|
172
267
|
const first = harnesses[0];
|
|
173
268
|
const fallback = typeof botHarness === "string" && harnesses.includes(botHarness) ? botHarness : first;
|
|
@@ -184,6 +279,7 @@ function chatArgs(
|
|
|
184
279
|
model: {
|
|
185
280
|
type: "string",
|
|
186
281
|
...(models.length === 0 ? {} : { enum: [...models] }),
|
|
282
|
+
...(info === undefined ? {} : { info }),
|
|
187
283
|
description:
|
|
188
284
|
"The model the agent uses; one from that agent's list. The bot's model, else the agent's default, when left out. It can be changed later.",
|
|
189
285
|
},
|
|
@@ -213,16 +309,178 @@ function chatArgs(
|
|
|
213
309
|
};
|
|
214
310
|
}
|
|
215
311
|
|
|
312
|
+
/** The skill choices: a known skill by name, or a path such as `/...`, `~/...` or `skills/...`. */
|
|
313
|
+
function skillItem(known: readonly { readonly name: string }[]): ActionArgSchema {
|
|
314
|
+
const branches: ActionArgSchema[] = [];
|
|
315
|
+
const names = [...new Set(known.map((skill) => skill.name))].sort();
|
|
316
|
+
if (names.length > 0) {
|
|
317
|
+
branches.push({
|
|
318
|
+
type: "string",
|
|
319
|
+
enum: names,
|
|
320
|
+
description: "A skill another bot already mounts, or one in ~/.agents/skills.",
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
branches.push({
|
|
324
|
+
type: "string",
|
|
325
|
+
pattern: "^(?:.*[/\\\\].*|~.*)$",
|
|
326
|
+
description: "A path to a skill, a directory of skills, or a .agents directory.",
|
|
327
|
+
});
|
|
328
|
+
return branches.length === 1 ? (branches[0] as ActionArgSchema) : { oneOf: branches };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The arguments of `bot:create`, with the choices this home knows now. */
|
|
332
|
+
function createArgs(
|
|
333
|
+
home: string | undefined,
|
|
334
|
+
harnesses: readonly string[],
|
|
335
|
+
models: readonly string[],
|
|
336
|
+
efforts: readonly string[],
|
|
337
|
+
agents: readonly AgentChoices[],
|
|
338
|
+
info?: Readonly<Record<string, HarnessModelInfo>> | undefined,
|
|
339
|
+
): ActionArgsSchema {
|
|
340
|
+
const skills = home === undefined ? [] : knownSkills(home);
|
|
341
|
+
const servers = home === undefined ? [] : knownMcpServers(home);
|
|
342
|
+
const first = harnesses[0];
|
|
343
|
+
return {
|
|
344
|
+
type: "object",
|
|
345
|
+
description: "Creates a bot with exactly the skills and MCP servers chosen for it.",
|
|
346
|
+
properties: {
|
|
347
|
+
name: {
|
|
348
|
+
type: "string",
|
|
349
|
+
description:
|
|
350
|
+
"The new bot's name, used for its directory under ~/.operad/bot; lowercase letters, digits, - and _.",
|
|
351
|
+
},
|
|
352
|
+
agent: {
|
|
353
|
+
type: "string",
|
|
354
|
+
...(harnesses.length === 0 ? {} : { enum: [...harnesses] }),
|
|
355
|
+
...(first === undefined ? {} : { default: first }),
|
|
356
|
+
description: "The agent a new chat of this bot starts with.",
|
|
357
|
+
},
|
|
358
|
+
model: {
|
|
359
|
+
type: "string",
|
|
360
|
+
...(models.length === 0 ? {} : { enum: [...models] }),
|
|
361
|
+
...(info === undefined ? {} : { info }),
|
|
362
|
+
description: "The model a new chat starts with; one the chosen agent lists. Left out: the agent's default.",
|
|
363
|
+
},
|
|
364
|
+
effort: {
|
|
365
|
+
type: "string",
|
|
366
|
+
...(efforts.length === 0 ? {} : { enum: [...efforts] }),
|
|
367
|
+
description: "The effort a new chat starts with; one the chosen agent lists. Left out: the agent's default.",
|
|
368
|
+
},
|
|
369
|
+
speaker: {
|
|
370
|
+
type: "string",
|
|
371
|
+
minLength: 1,
|
|
372
|
+
description: "How this bot's chats label the model's messages; left out: the chat plugin's speaker.",
|
|
373
|
+
},
|
|
374
|
+
skills: {
|
|
375
|
+
type: "array",
|
|
376
|
+
items: skillItem(skills),
|
|
377
|
+
description:
|
|
378
|
+
"The skills exactly this bot has. A known skill is chosen by name, or a path is given; without it the bot has none. A path that is a directory of skills gives every skill inside it.",
|
|
379
|
+
},
|
|
380
|
+
mcpServers: {
|
|
381
|
+
type: "array",
|
|
382
|
+
items: { type: "string", ...(servers.length === 0 ? {} : { enum: servers.map((server) => server.key) }) },
|
|
383
|
+
description:
|
|
384
|
+
"MCP servers a person already approved on another bot, copied with their pin and digest. Without it the bot has none; a new server goes through bot mcp.",
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
required: ["name", "agent"],
|
|
388
|
+
...(agents.length === 0 ? {} : { allOf: agentConditionals(agents) }),
|
|
389
|
+
additionalProperties: false,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const createAction: ActionTypeDefinition = {
|
|
394
|
+
name: "create",
|
|
395
|
+
on: BOT_TYPE,
|
|
396
|
+
args: createArgs(undefined, [], [], [], []),
|
|
397
|
+
argsFor: (_object, env) => {
|
|
398
|
+
const info: Record<string, HarnessModelInfo> = {};
|
|
399
|
+
for (const name of env.harnesses ?? []) {
|
|
400
|
+
for (const [id, model] of Object.entries(env.models?.[name]?.info ?? {})) info[id] = model;
|
|
401
|
+
}
|
|
402
|
+
return createArgs(
|
|
403
|
+
env.home,
|
|
404
|
+
env.harnesses ?? [],
|
|
405
|
+
[...new Set((env.harnesses ?? []).flatMap((name) => modelIds(env.models?.[name])))],
|
|
406
|
+
[...new Set((env.harnesses ?? []).flatMap((name) => env.efforts?.[name]?.list ?? []))],
|
|
407
|
+
(env.harnesses ?? []).map((name) => ({
|
|
408
|
+
name,
|
|
409
|
+
models: env.models?.[name],
|
|
410
|
+
efforts: env.efforts?.[name],
|
|
411
|
+
preferredModel: null,
|
|
412
|
+
preferredEffort: null,
|
|
413
|
+
})),
|
|
414
|
+
Object.keys(info).length === 0 ? undefined : info,
|
|
415
|
+
);
|
|
416
|
+
},
|
|
417
|
+
when: (state, env) => state.removed !== true && (env.harnesses ?? []).length > 0,
|
|
418
|
+
async why(object, env) {
|
|
419
|
+
if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
|
|
420
|
+
if ((env.harnesses ?? []).length === 0) return "no agent is loaded; load one to create a bot";
|
|
421
|
+
return undefined;
|
|
422
|
+
},
|
|
423
|
+
async run(ctx, args) {
|
|
424
|
+
const input = args as {
|
|
425
|
+
readonly name?: string;
|
|
426
|
+
readonly agent?: string;
|
|
427
|
+
readonly model?: string;
|
|
428
|
+
readonly effort?: string;
|
|
429
|
+
readonly speaker?: string;
|
|
430
|
+
readonly skills?: readonly string[];
|
|
431
|
+
readonly mcpServers?: readonly string[];
|
|
432
|
+
};
|
|
433
|
+
const name = input.name ?? "";
|
|
434
|
+
const agent = input.agent ?? "";
|
|
435
|
+
if (!ctx.harnesses.includes(agent)) {
|
|
436
|
+
throw new BotError(
|
|
437
|
+
`agent "${agent}" is not loaded; the loaded ones are: ${ctx.harnesses.join(", ") || "none"}`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
if (input.model !== undefined) startingModel(agent, ctx.models?.[agent], input.model);
|
|
442
|
+
if (input.effort !== undefined) startingEffort(agent, ctx.efforts?.[agent], input.effort);
|
|
443
|
+
} catch (error) {
|
|
444
|
+
throw new BotError(messageOf(error));
|
|
445
|
+
}
|
|
446
|
+
const created = await createBot(
|
|
447
|
+
ctx.home,
|
|
448
|
+
{
|
|
449
|
+
name,
|
|
450
|
+
agent,
|
|
451
|
+
...(input.model === undefined ? {} : { model: input.model }),
|
|
452
|
+
...(input.effort === undefined ? {} : { effort: input.effort }),
|
|
453
|
+
...(input.speaker === undefined ? {} : { speaker: input.speaker }),
|
|
454
|
+
...(input.skills === undefined ? {} : { skills: input.skills }),
|
|
455
|
+
...(input.mcpServers === undefined ? {} : { mcpServers: input.mcpServers }),
|
|
456
|
+
},
|
|
457
|
+
{ cwd: ctx.cwd },
|
|
458
|
+
);
|
|
459
|
+
await syncBots(ctx.store, ctx.home, ctx.settings);
|
|
460
|
+
const secrets =
|
|
461
|
+
created.secrets.length === 0
|
|
462
|
+
? ""
|
|
463
|
+
: `; its MCP servers need ${created.secrets
|
|
464
|
+
.map((secret) => `${secret} (operad secret set bot ${secret})`)
|
|
465
|
+
.join(", ")}`;
|
|
466
|
+
return { text: `created bot "${name}"${secrets}`, opens: `${BOT_TYPE}:${name}` };
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
|
|
216
470
|
const chatAction: ActionTypeDefinition = {
|
|
217
471
|
name: "chat",
|
|
218
472
|
on: BOT_TYPE,
|
|
219
473
|
creates: "chat",
|
|
220
474
|
args: chatArgs([], undefined),
|
|
221
|
-
argsFor: (object, env) =>
|
|
222
|
-
|
|
475
|
+
argsFor: (object, env) => {
|
|
476
|
+
const info: Record<string, HarnessModelInfo> = {};
|
|
477
|
+
for (const name of env.harnesses ?? []) {
|
|
478
|
+
for (const [id, model] of Object.entries(env.models?.[name]?.info ?? {})) info[id] = model;
|
|
479
|
+
}
|
|
480
|
+
return chatArgs(
|
|
223
481
|
env.harnesses ?? [],
|
|
224
482
|
object.data.harness,
|
|
225
|
-
[...new Set((env.harnesses ?? []).flatMap((name) => env.models?.[name]
|
|
483
|
+
[...new Set((env.harnesses ?? []).flatMap((name) => modelIds(env.models?.[name])))],
|
|
226
484
|
[...new Set((env.harnesses ?? []).flatMap((name) => env.efforts?.[name]?.list ?? []))],
|
|
227
485
|
(env.harnesses ?? []).map((name) => ({
|
|
228
486
|
name,
|
|
@@ -231,7 +489,9 @@ const chatAction: ActionTypeDefinition = {
|
|
|
231
489
|
preferredModel: typeof object.data.model === "string" ? object.data.model : null,
|
|
232
490
|
preferredEffort: typeof object.data.effort === "string" ? object.data.effort : null,
|
|
233
491
|
})),
|
|
234
|
-
|
|
492
|
+
Object.keys(info).length === 0 ? undefined : info,
|
|
493
|
+
);
|
|
494
|
+
},
|
|
235
495
|
when: (state, env) => state.removed !== true && typeof state.problem !== "string" && (env.harnesses ?? []).length > 0,
|
|
236
496
|
async why(object, env) {
|
|
237
497
|
if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
|
|
@@ -248,6 +508,10 @@ const chatAction: ActionTypeDefinition = {
|
|
|
248
508
|
readonly channels?: readonly string[];
|
|
249
509
|
readonly message?: string;
|
|
250
510
|
};
|
|
511
|
+
const scheduled =
|
|
512
|
+
ctx.actor.via === SCHEDULE_SOURCE
|
|
513
|
+
? { bot: ctx.object.id, entry: ctx.actor.id }
|
|
514
|
+
: undefined;
|
|
251
515
|
const id = await openChat({
|
|
252
516
|
store: ctx.store,
|
|
253
517
|
home: ctx.home,
|
|
@@ -261,13 +525,109 @@ const chatAction: ActionTypeDefinition = {
|
|
|
261
525
|
effort: input.effort,
|
|
262
526
|
title: input.title ?? (input.message === undefined ? undefined : titleFromMessage(input.message)),
|
|
263
527
|
channels: input.channels,
|
|
528
|
+
...(scheduled === undefined ? {} : { scheduled }),
|
|
264
529
|
cause: "action:bot/chat",
|
|
265
530
|
});
|
|
266
|
-
const sent =
|
|
531
|
+
const sent =
|
|
532
|
+
input.message === undefined
|
|
533
|
+
? undefined
|
|
534
|
+
: await sendMessage(
|
|
535
|
+
ctx.actor.via === SCHEDULE_SOURCE ? { ...ctx, source: SCHEDULE_SOURCE } : ctx,
|
|
536
|
+
id,
|
|
537
|
+
input.message,
|
|
538
|
+
);
|
|
267
539
|
return { text: `opened chat ${id}${sent === undefined ? "" : `; ${sent}`}`, opens: `chat:${id}` };
|
|
268
540
|
},
|
|
269
541
|
};
|
|
270
542
|
|
|
543
|
+
// Setup Dev (ADR 0030 D15): opens a chat on the bot whose first message is the
|
|
544
|
+
// prepared prompt for one repository. The chat is an ordinary one; what the
|
|
545
|
+
// agent proposes is a pending item.
|
|
546
|
+
const setupDevAction: ActionTypeDefinition = {
|
|
547
|
+
name: "setup-dev",
|
|
548
|
+
on: BOT_TYPE,
|
|
549
|
+
creates: "chat",
|
|
550
|
+
args: {
|
|
551
|
+
type: "object",
|
|
552
|
+
description: "Opens a chat that has the bot's agent set up dev for a repository and propose the result.",
|
|
553
|
+
properties: {
|
|
554
|
+
path: {
|
|
555
|
+
type: "string",
|
|
556
|
+
minLength: 1,
|
|
557
|
+
description: "The directory of the repository: the root of a git work tree that has a remote.",
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
required: ["path"],
|
|
561
|
+
},
|
|
562
|
+
when: (state, env) => state.removed !== true && typeof state.problem !== "string" && (env.harnesses ?? []).length > 0,
|
|
563
|
+
async why(object, env) {
|
|
564
|
+
if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
|
|
565
|
+
if (typeof object.data.problem === "string") return `bot ${object.id}: ${object.data.problem}`;
|
|
566
|
+
if ((env.harnesses ?? []).length === 0) return `bot ${object.id}: no agent is loaded; load one to chat with it`;
|
|
567
|
+
return undefined;
|
|
568
|
+
},
|
|
569
|
+
async run(ctx, args) {
|
|
570
|
+
const repo = (args as { readonly path: string }).path.trim();
|
|
571
|
+
if (repo.length === 0) throw new BotError("setup-dev needs the repository's directory");
|
|
572
|
+
const where = isAbsolute(repo) ? repo : resolve(ctx.cwd, repo);
|
|
573
|
+
const id = await openChat({
|
|
574
|
+
store: ctx.store,
|
|
575
|
+
home: ctx.home,
|
|
576
|
+
settings: ctx.settings,
|
|
577
|
+
harnesses: ctx.harnesses,
|
|
578
|
+
models: ctx.models,
|
|
579
|
+
efforts: ctx.efforts,
|
|
580
|
+
bot: ctx.object.id,
|
|
581
|
+
title: `Setup Dev (${basename(where)})`,
|
|
582
|
+
cause: "action:bot/setup-dev",
|
|
583
|
+
});
|
|
584
|
+
const sent = await sendMessage(ctx, id, await setupDevPrompt(ctx.setup, ctx.object.id, where));
|
|
585
|
+
return { text: `opened chat ${id}; ${sent}`, opens: `chat:${id}` };
|
|
586
|
+
},
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
// Setup <plugin> (ADR 0010 D7): opens a chat on the bot whose first message is
|
|
590
|
+
// the plugin's guide, with what its check finds missing.
|
|
591
|
+
const setupAction: ActionTypeDefinition = {
|
|
592
|
+
name: "setup",
|
|
593
|
+
on: BOT_TYPE,
|
|
594
|
+
creates: "chat",
|
|
595
|
+
args: {
|
|
596
|
+
type: "object",
|
|
597
|
+
description: "Opens a chat that has the bot's agent set up a plugin by its guide.",
|
|
598
|
+
properties: {
|
|
599
|
+
plugin: { type: "string", minLength: 1, description: "The plugin to set up, such as dev." },
|
|
600
|
+
target: {
|
|
601
|
+
type: "string",
|
|
602
|
+
description: "What the guide works on, when it needs one: for dev the repository's directory.",
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
required: ["plugin"],
|
|
606
|
+
},
|
|
607
|
+
when: setupDevAction.when,
|
|
608
|
+
why: setupDevAction.why,
|
|
609
|
+
async run(ctx, args) {
|
|
610
|
+
const given = args as { readonly plugin: string; readonly target?: string };
|
|
611
|
+
const plugin = given.plugin.trim();
|
|
612
|
+
const target = given.target?.trim() ?? "";
|
|
613
|
+
const where = target.length === 0 ? "" : isAbsolute(target) ? target : resolve(ctx.cwd, target);
|
|
614
|
+
const prompt = await setupPrompt(ctx.setup, ctx.object.id, plugin, where);
|
|
615
|
+
const id = await openChat({
|
|
616
|
+
store: ctx.store,
|
|
617
|
+
home: ctx.home,
|
|
618
|
+
settings: ctx.settings,
|
|
619
|
+
harnesses: ctx.harnesses,
|
|
620
|
+
models: ctx.models,
|
|
621
|
+
efforts: ctx.efforts,
|
|
622
|
+
bot: ctx.object.id,
|
|
623
|
+
title: `${setupTitle(plugin)}${where.length === 0 ? "" : ` (${basename(where)})`}`,
|
|
624
|
+
cause: "action:bot/setup",
|
|
625
|
+
});
|
|
626
|
+
const sent = await sendMessage(ctx, id, prompt);
|
|
627
|
+
return { text: `opened chat ${id}; ${sent}`, opens: `chat:${id}` };
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
|
|
271
631
|
/**
|
|
272
632
|
* Step `bot/settings`: the working directory, access profile and skills of a
|
|
273
633
|
* chat's bot as they are now. The `chat` reply calls it every time, so
|
|
@@ -285,12 +645,15 @@ export async function runSettings(ctx: StepContext, params: unknown): Promise<Pl
|
|
|
285
645
|
const name = parent.slice(BOT_TYPE.length + 1);
|
|
286
646
|
try {
|
|
287
647
|
await ingestReports(ctx.store, ctx.home);
|
|
288
|
-
const
|
|
648
|
+
const bots = readBots(ctx.home, ctx.settings);
|
|
649
|
+
await migrateDefaultHome(ctx.store, ctx.home, bots);
|
|
650
|
+
const written = bots[name];
|
|
289
651
|
if (written === undefined) {
|
|
290
652
|
return { result: "failed", data: { message: `bot "${name}" is no longer in the settings` } };
|
|
291
653
|
}
|
|
292
654
|
const bot = resolveBot(ctx.home, name, written, await repositoryPaths(ctx.store));
|
|
293
655
|
mkdirSync(bot.cwd, { recursive: true });
|
|
656
|
+
ensureBotMemory(bot.cwd, ctx.home);
|
|
294
657
|
// Only the servers the bot lists, and only on a harness that takes MCP servers natively.
|
|
295
658
|
const mounts = MCP_HARNESSES.includes(chatData(stored.data).harness);
|
|
296
659
|
const mcpServers: Record<string, JsonValue> = {};
|
|
@@ -299,10 +662,24 @@ export async function runSettings(ctx: StepContext, params: unknown): Promise<Pl
|
|
|
299
662
|
}
|
|
300
663
|
return {
|
|
301
664
|
result: "done",
|
|
302
|
-
data: {
|
|
665
|
+
data: {
|
|
666
|
+
cwd: bot.cwd,
|
|
667
|
+
access: bot.access,
|
|
668
|
+
skills: [...bot.skills],
|
|
669
|
+
mcpServers,
|
|
670
|
+
useOwnSettings: bot.useOwnSettings,
|
|
671
|
+
// A chat that never chose a level follows the bot's setting; a scheduled chat keeps `full`.
|
|
672
|
+
...(chatData(stored.data).scheduled === undefined ? { accessDefault: bot.accessLevel } : {}),
|
|
673
|
+
// The memory index and the person's instructions ride on the message,
|
|
674
|
+
// whole when this session has not seen them, under ADR 0026 D18.
|
|
675
|
+
notes: memoryNotes(join(bot.cwd, "memory")) as unknown as JsonValue,
|
|
676
|
+
// The memory plugin records what this reply wrote, once the round ends.
|
|
677
|
+
...(ctx.stepLoaded !== undefined && ctx.stepLoaded(MEMORY_COMMIT_STEP) ? { after: MEMORY_COMMIT_STEP } : {}),
|
|
678
|
+
context: scheduledChatContext(),
|
|
679
|
+
},
|
|
303
680
|
};
|
|
304
681
|
} catch (error) {
|
|
305
|
-
return { result: "failed", data: { message:
|
|
682
|
+
return { result: "failed", data: { message: messageOf(error) } };
|
|
306
683
|
}
|
|
307
684
|
}
|
|
308
685
|
|
|
@@ -313,8 +690,11 @@ function botMarkdown(object: StoredObject): string {
|
|
|
313
690
|
if (typeof data.cwd === "string") lines.push(`Working directory: ${data.cwd}`);
|
|
314
691
|
if (typeof data.harness === "string") lines.push(`Default agent: ${data.harness}`);
|
|
315
692
|
if (typeof data.speaker === "string") lines.push(`Speaker: ${data.speaker}`);
|
|
316
|
-
const skills = Array.isArray(data.skills)
|
|
317
|
-
|
|
693
|
+
const skills = Array.isArray(data.skills)
|
|
694
|
+
? data.skills.filter((path): path is string => typeof path === "string")
|
|
695
|
+
: [];
|
|
696
|
+
lines.push(skills.length === 0 ? "No skills." : `Skills: ${skills.map(skillName).join(", ")}`);
|
|
697
|
+
lines.push(data.writeWiki === true ? "Wiki: read and write." : "Wiki: read only.");
|
|
318
698
|
const servers = Array.isArray(data.mcpServers) ? data.mcpServers : [];
|
|
319
699
|
if (servers.length > 0) {
|
|
320
700
|
lines.push(
|
|
@@ -322,6 +702,7 @@ function botMarkdown(object: StoredObject): string {
|
|
|
322
702
|
`They are mounted in chats on ${MCP_HARNESSES.join(" and ")}. A chat on pi does not mount them: pi takes no MCP servers yet, so it cannot use ${servers.join(", ")}.`,
|
|
323
703
|
);
|
|
324
704
|
}
|
|
705
|
+
lines.push(scheduledMarkdown(scheduledPrompts(data), Date.now()).join("\n"));
|
|
325
706
|
return lines.join("\n\n");
|
|
326
707
|
}
|
|
327
708
|
|
|
@@ -331,6 +712,9 @@ export const botViews: ObjectViews = {
|
|
|
331
712
|
id: object.id,
|
|
332
713
|
title: object.id,
|
|
333
714
|
state: object.data.removed === true ? "removed" : "open",
|
|
715
|
+
data: {
|
|
716
|
+
scheduled: scheduledData(scheduledPrompts(object.data), Date.now()) as unknown as JsonValue,
|
|
717
|
+
},
|
|
334
718
|
}),
|
|
335
719
|
sections: () => [{ id: "bot", title: "Bot" }],
|
|
336
720
|
load: (_ctx, object): SectionContent => ({ markdown: botMarkdown(object) }),
|
|
@@ -344,7 +728,7 @@ export function createBotPlugin() {
|
|
|
344
728
|
// The version comes from package.json so the two cannot drift apart.
|
|
345
729
|
version: pkg.version,
|
|
346
730
|
// Patch releases of chat only: a new minor may change what bot relies on.
|
|
347
|
-
requires: { plugins: { chat: "~0.
|
|
731
|
+
requires: { plugins: { chat: "~0.2.0" } },
|
|
348
732
|
settings: BOT_SETTINGS,
|
|
349
733
|
// A bot page is that bot's settings page; bots live under settings, not in a tab.
|
|
350
734
|
objectsUnderSettings: true,
|
|
@@ -367,9 +751,33 @@ export function createBotPlugin() {
|
|
|
367
751
|
{ name: "rejected", states: ["rejected"] },
|
|
368
752
|
],
|
|
369
753
|
},
|
|
754
|
+
{ name: TIDY_TYPE, index: ["bot", "state"] },
|
|
755
|
+
],
|
|
756
|
+
steps: [
|
|
757
|
+
{ name: "settings", results: ["done", "failed"], run: runSettings },
|
|
758
|
+
{ name: "login", results: ["done", "failed"], callsNamedByData: true, timeoutSecs: 700, run: runLoginStep },
|
|
759
|
+
],
|
|
760
|
+
flows: [loginFlow],
|
|
761
|
+
// A scheduled prompt opens a chat and sends its first message from the
|
|
762
|
+
// bot source; that message is not a person's, so the reply is a background run.
|
|
763
|
+
rules: [
|
|
764
|
+
{
|
|
765
|
+
source: SCHEDULE_SOURCE,
|
|
766
|
+
type: MESSAGE_EVENT,
|
|
767
|
+
start: { flow: "chat:reply" },
|
|
768
|
+
object: "$event.object",
|
|
769
|
+
mode: "auto" as const,
|
|
770
|
+
},
|
|
771
|
+
// The person's own login on a bot; it asks through the agent's login step.
|
|
772
|
+
{
|
|
773
|
+
source: LOGIN_SOURCE,
|
|
774
|
+
type: LOGIN_EVENT,
|
|
775
|
+
start: { flow: "bot:login" },
|
|
776
|
+
object: "$event.object",
|
|
777
|
+
mode: "auto" as const,
|
|
778
|
+
},
|
|
370
779
|
],
|
|
371
|
-
|
|
372
|
-
actions: [chatAction, ...proposalActions],
|
|
780
|
+
actions: [chatAction, setupDevAction, setupAction, chatSetupAction, createAction, loginAction, ...proposalActions, ...scheduleActions, ...tidyActions],
|
|
373
781
|
commands: [
|
|
374
782
|
{
|
|
375
783
|
name: "skill",
|
|
@@ -386,20 +794,56 @@ export function createBotPlugin() {
|
|
|
386
794
|
args: "<bot>",
|
|
387
795
|
run: runSettingCommand,
|
|
388
796
|
},
|
|
797
|
+
{
|
|
798
|
+
name: "setup-dev",
|
|
799
|
+
args: "<bot> <repository directory> [< dev.json]",
|
|
800
|
+
run: runSetupDev,
|
|
801
|
+
},
|
|
802
|
+
{
|
|
803
|
+
name: "setup",
|
|
804
|
+
args: "<bot> <plugin> [target]",
|
|
805
|
+
run: runSetup,
|
|
806
|
+
},
|
|
807
|
+
{
|
|
808
|
+
name: "schedule",
|
|
809
|
+
args: "<bot> add|edit|remove <name> [--words <text>] [--schedule <cron>] [--prompt <text>] [--model <model>] [--effort <effort>]",
|
|
810
|
+
run: runScheduleCommand,
|
|
811
|
+
},
|
|
389
812
|
],
|
|
390
813
|
// A proposal waits for no run; each one is a pending item of its bot.
|
|
391
814
|
standing: async (ctx) => {
|
|
392
815
|
await ingestReports(ctx.store, ctx.home);
|
|
393
|
-
|
|
816
|
+
await syncBots(ctx.store, ctx.home, ctx.settings);
|
|
817
|
+
return [...(await proposalItems(ctx.store)), ...(await tidyItems(ctx.store)), ...(await loginItems(ctx))];
|
|
818
|
+
},
|
|
819
|
+
// The bot is its own source for its scheduled prompts and its nightly
|
|
820
|
+
// memory tidy. It declares `start` while staying a domain plugin, so
|
|
821
|
+
// `serve` starts it like any source.
|
|
822
|
+
start: async (ctx) => {
|
|
823
|
+
await migrateDefaultHome(ctx.store, ctx.home, readBots(ctx.home, ctx.settings));
|
|
824
|
+
const scheduled = await startScheduled(ctx);
|
|
825
|
+
const tidy = await startTidy(ctx);
|
|
826
|
+
return {
|
|
827
|
+
stop: async () => {
|
|
828
|
+
await scheduled.stop();
|
|
829
|
+
await tidy.stop();
|
|
830
|
+
},
|
|
831
|
+
};
|
|
394
832
|
},
|
|
395
|
-
views: { [BOT_TYPE]: botViews, [PROPOSAL_TYPE]: proposalViews },
|
|
833
|
+
views: { [BOT_TYPE]: botViews, [PROPOSAL_TYPE]: proposalViews, [TIDY_TYPE]: tidyViews },
|
|
396
834
|
});
|
|
397
835
|
}
|
|
398
836
|
|
|
399
837
|
export default createBotPlugin();
|
|
400
838
|
|
|
401
839
|
export * from "./bot.ts";
|
|
840
|
+
export * from "./create.ts";
|
|
841
|
+
export * from "./login.ts";
|
|
402
842
|
export * from "./proposals.ts";
|
|
843
|
+
export * from "./setup-dev.ts";
|
|
844
|
+
export * from "./setup.ts";
|
|
845
|
+
export * from "./schedule.ts";
|
|
846
|
+
export * from "./tidy.ts";
|
|
403
847
|
export * from "./mcp.ts";
|
|
404
848
|
export * from "./mcp-proposals.ts";
|
|
405
849
|
export { runGuard, markedResult, MATERIAL_NOTE, reportPath } from "./guard.ts";
|