@operad-io/bot 0.1.1 → 0.3.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/src/setup.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Setup guides in a bot (ADR 0010 D7, ADR 0030 D15): any plugin that ships a
3
+ * guide can be set up by a bot's agent. The plugin's check says what is
4
+ * missing, its guide says how to fix it, and the entries here hand the two to
5
+ * the agent: a command it calls, an action a button runs and a chat command.
6
+ */
7
+
8
+ import { isAbsolute, resolve } from "node:path";
9
+ import { chatData, sendMessage } from "@operad-io/chat";
10
+ import type { ActionTypeDefinition, CommandContext, SetupGuide, SetupReader } from "@operad-io/engine";
11
+ import { BotError, BOT_TYPE, readBots } from "./bot.ts";
12
+
13
+ /** Every guide's title: `Setup` and the plugin's name, capitalised (Setup Dev, Setup Telegram). */
14
+ export function setupTitle(plugin: string): string {
15
+ return `Setup ${plugin.charAt(0).toUpperCase()}${plugin.slice(1)}`;
16
+ }
17
+
18
+ /** The guide's text with the bot and the target filled in. */
19
+ export function renderGuide(text: string, bot: string, target: string): string {
20
+ return text.split("{bot}").join(bot).split("{target}").join(target);
21
+ }
22
+
23
+ /** The line a bot chat starts with, so a sentence is enough to start any plugin's setup. */
24
+ export const SETUP_INSTRUCTION =
25
+ "To set up any other plugin that has a setup guide (Setup <name>), call the operad `command` tool with plugin `bot`, verb `setup`, my bot and the plugin's name: it answers with what is missing and the steps. Nothing is turned on until I approve the proposal in Pending.";
26
+
27
+ /** The plugin's guide, or the reason there is none. */
28
+ export async function guideOf(setup: SetupReader | undefined, plugin: string): Promise<SetupGuide> {
29
+ const found = await setup?.get(plugin);
30
+ if (found === undefined) {
31
+ const known = (await setup?.list())?.map((item) => item.plugin) ?? [];
32
+ throw new BotError(
33
+ `setup: the plugin "${plugin}" has no setup guide${known.length === 0 ? "" : `; plugins with one: ${known.join(", ")}`}`,
34
+ );
35
+ }
36
+ return found;
37
+ }
38
+
39
+ /** The text Setup Dev starts a chat with: the `dev` plugin's guide for one repository. */
40
+ export async function setupDevPrompt(setup: SetupReader | undefined, bot: string, repo: string): Promise<string> {
41
+ return renderGuide((await guideOf(setup, "dev")).text, bot, repo);
42
+ }
43
+
44
+ /**
45
+ * What the agent is given for a plugin: what its check finds missing, then the
46
+ * guide. A plugin with nothing missing and no target gets one sentence saying so.
47
+ */
48
+ export async function setupPrompt(
49
+ setup: SetupReader | undefined,
50
+ bot: string,
51
+ plugin: string,
52
+ target: string,
53
+ ): Promise<string> {
54
+ const guide = await guideOf(setup, plugin);
55
+ const title = setupTitle(plugin);
56
+ if (guide.missing.length === 0 && target.length === 0) {
57
+ return `${title}: nothing is missing, ${plugin} is set up. Tell me so and stop unless I ask for something to change.`;
58
+ }
59
+ const lines = [`${title}. What the plugin's own check finds missing now:`];
60
+ if (guide.missing.length === 0) lines.push("- nothing (it is set up); the target below still needs its steps");
61
+ for (const item of guide.missing) lines.push(`- ${item.name}: ${item.description}`);
62
+ lines.push("", "Follow only the steps of the guide that fix these items.", "");
63
+ return `${lines.join("\n")}\n${renderGuide(guide.text, bot, target)}`;
64
+ }
65
+
66
+ const USAGE = "usage: operad bot setup <bot> <plugin> [target]";
67
+
68
+ /** `bot setup <bot> <plugin> [target]`: the steps for the agent. */
69
+ export async function runSetup(ctx: CommandContext, argv: readonly string[]): Promise<string> {
70
+ const [bot, plugin, target] = argv;
71
+ if (bot === undefined || plugin === undefined || argv.length > 3) throw new BotError(USAGE);
72
+ if (readBots(ctx.home)[bot] === undefined) throw new BotError(`there is no bot "${bot}"`);
73
+ const where = target === undefined ? "" : isAbsolute(target) ? target : resolve(ctx.cwd, target);
74
+ return setupPrompt(ctx.setup, bot, plugin, where);
75
+ }
76
+
77
+ /** The chat command `/setup <plugin> [target]`: sends the plugin's guide to this chat, so its agent follows it. */
78
+ export const chatSetupAction: ActionTypeDefinition = {
79
+ name: "setup",
80
+ on: "chat",
81
+ command: "Set up a plugin: the bot's agent follows its setup guide. /setup <plugin> [target]",
82
+ args: {
83
+ type: "object",
84
+ properties: {
85
+ plugin: { type: "string", minLength: 1, description: "The plugin to set up, such as dev." },
86
+ target: {
87
+ type: "string",
88
+ description: "What the guide works on, when it needs one: for dev the repository's directory.",
89
+ },
90
+ },
91
+ required: ["plugin"],
92
+ },
93
+ when: (state) => botOf(state) !== undefined && chatData(state).closed !== true,
94
+ why: (object) => {
95
+ if (botOf(object.data) === undefined) return `${object.id} is not a bot chat; /setup needs a bot`;
96
+ return chatData(object.data).closed === true ? `${object.id} is closed; it takes no commands` : undefined;
97
+ },
98
+ async run(ctx, args) {
99
+ const bot = botOf(ctx.object.data);
100
+ if (bot === undefined) throw new BotError(`${ctx.object.id} is not a bot chat; /setup needs a bot`);
101
+ const given = args as { readonly plugin: string; readonly target?: string };
102
+ const target = given.target?.trim() ?? "";
103
+ const where = target.length === 0 ? "" : isAbsolute(target) ? target : resolve(ctx.cwd, target);
104
+ const prompt = await setupPrompt(ctx.setup, bot, given.plugin.trim(), where);
105
+ const sent = await sendMessage(ctx, ctx.object.id, prompt);
106
+ return `${setupTitle(given.plugin.trim())}: ${sent}`;
107
+ },
108
+ };
109
+
110
+ function botOf(data: Readonly<Record<string, unknown>>): string | undefined {
111
+ const parent = chatData(data).parent ?? "";
112
+ return parent.startsWith(`${BOT_TYPE}:`) ? parent.slice(BOT_TYPE.length + 1) : undefined;
113
+ }