@operad-io/bot 0.1.1 → 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/proposals.ts
CHANGED
|
@@ -16,14 +16,13 @@ import {
|
|
|
16
16
|
rmSync,
|
|
17
17
|
writeFileSync,
|
|
18
18
|
} from "node:fs";
|
|
19
|
-
import { homedir } from "node:os";
|
|
20
19
|
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
21
20
|
import {
|
|
22
21
|
configPath,
|
|
22
|
+
isMissingFile,
|
|
23
23
|
isRecord,
|
|
24
24
|
messageOf,
|
|
25
25
|
prepareSettings,
|
|
26
|
-
writeFileAtomic,
|
|
27
26
|
type ActionTypeDefinition,
|
|
28
27
|
type CommandContext,
|
|
29
28
|
type JsonValue,
|
|
@@ -33,17 +32,28 @@ import {
|
|
|
33
32
|
type SectionContent,
|
|
34
33
|
type StoredObject,
|
|
35
34
|
} from "@operad-io/engine";
|
|
36
|
-
import { withLockFile } from "@operad-io/engine/platform";
|
|
35
|
+
import { withLockFile, writeFileAtomic } from "@operad-io/engine/platform";
|
|
37
36
|
import {
|
|
38
37
|
BOT_SETTINGS,
|
|
39
38
|
BOT_TYPE,
|
|
40
39
|
BotError,
|
|
40
|
+
listedSkills,
|
|
41
41
|
readBots,
|
|
42
|
-
|
|
42
|
+
workingDirectory,
|
|
43
43
|
type BotSettings,
|
|
44
44
|
type McpServerSettings,
|
|
45
45
|
} from "./bot.ts";
|
|
46
|
+
import { applyDev, devChangeOf, devReview, runSetupDevCommand, type DevChange, type PipedInput } from "./setup-dev.ts";
|
|
46
47
|
import { commandLine, compareTools, differenceLines, secretName, storedTools } from "./mcp.ts";
|
|
48
|
+
import {
|
|
49
|
+
applyScheduleChange,
|
|
50
|
+
checkPromptName,
|
|
51
|
+
checkSchedule,
|
|
52
|
+
scheduleChangeOf,
|
|
53
|
+
scheduledPrompts,
|
|
54
|
+
type ScheduleChange,
|
|
55
|
+
type ScheduledPrompt,
|
|
56
|
+
} from "./schedule.ts";
|
|
47
57
|
|
|
48
58
|
/** The object type of a proposal. It is internal: a person sees its pending item. */
|
|
49
59
|
export const PROPOSAL_TYPE = "bot-proposal";
|
|
@@ -78,7 +88,7 @@ export interface SettingsChange {
|
|
|
78
88
|
export interface ProposalData {
|
|
79
89
|
readonly [key: string]: unknown;
|
|
80
90
|
readonly bot: string;
|
|
81
|
-
readonly kind: "skill" | "settings" | "mcp";
|
|
91
|
+
readonly kind: "skill" | "settings" | "mcp" | "schedule" | "dev";
|
|
82
92
|
readonly title: string;
|
|
83
93
|
readonly state: ProposalState;
|
|
84
94
|
readonly createdAt: number;
|
|
@@ -89,6 +99,8 @@ export interface ProposalData {
|
|
|
89
99
|
} | undefined;
|
|
90
100
|
readonly settings?: SettingsChange | undefined;
|
|
91
101
|
readonly mcp?: McpChange | undefined;
|
|
102
|
+
readonly schedule?: ScheduleChange | undefined;
|
|
103
|
+
readonly dev?: DevChange | undefined;
|
|
92
104
|
}
|
|
93
105
|
|
|
94
106
|
/** A local MCP server a proposal registers, upgrades or lets through after a change of its tools. */
|
|
@@ -106,7 +118,11 @@ function dataOf(object: StoredObject): ProposalData {
|
|
|
106
118
|
const data = object.data;
|
|
107
119
|
if (
|
|
108
120
|
typeof data.bot !== "string" ||
|
|
109
|
-
(data.kind !== "skill" &&
|
|
121
|
+
(data.kind !== "skill" &&
|
|
122
|
+
data.kind !== "settings" &&
|
|
123
|
+
data.kind !== "mcp" &&
|
|
124
|
+
data.kind !== "schedule" &&
|
|
125
|
+
data.kind !== "dev") ||
|
|
110
126
|
typeof data.title !== "string" ||
|
|
111
127
|
(data.state !== "pending" && data.state !== "applied" && data.state !== "rejected") ||
|
|
112
128
|
typeof data.createdAt !== "number"
|
|
@@ -151,12 +167,6 @@ function fileOf(files: readonly SkillFile[], path: string): SkillFile | undefine
|
|
|
151
167
|
return files.find((file) => file.path === path);
|
|
152
168
|
}
|
|
153
169
|
|
|
154
|
-
function expandPath(path: string, base: string): string {
|
|
155
|
-
if (path === "~") return homedir();
|
|
156
|
-
if (path.startsWith("~/")) return join(homedir(), path.slice(2));
|
|
157
|
-
return isAbsolute(path) ? resolve(path) : resolve(base, path);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
170
|
/** The skill directory a bot already mounts under this name, if any. */
|
|
161
171
|
function existingSkillPath(
|
|
162
172
|
home: string,
|
|
@@ -165,37 +175,37 @@ function existingSkillPath(
|
|
|
165
175
|
): string | undefined {
|
|
166
176
|
const written = readBots(home)[bot];
|
|
167
177
|
if (written === undefined) return undefined;
|
|
168
|
-
const
|
|
169
|
-
for (const skill of written.skills ?? []) {
|
|
170
|
-
const path = expandPath(skill, cwd);
|
|
178
|
+
for (const path of mountedSkills(home, bot, written)) {
|
|
171
179
|
const expected = basename(path);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
180
|
+
let marker: string | undefined;
|
|
181
|
+
try {
|
|
182
|
+
marker = /^name:\s*(\S+)/m.exec(readFileSync(join(path, "SKILL.md"), "utf8"))?.[1];
|
|
183
|
+
} catch {
|
|
184
|
+
// A listed skill that vanished is skipped, like one that was never there.
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
175
187
|
if (expected === name || marker === name) return path;
|
|
176
188
|
}
|
|
177
189
|
return undefined;
|
|
178
190
|
}
|
|
179
191
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
bot
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (error instanceof BotError) throw error;
|
|
189
|
-
throw error;
|
|
192
|
+
/** The skills a bot's written paths mount now; a path with nothing to mount is skipped. */
|
|
193
|
+
function mountedSkills(home: string, bot: string, settings: BotSettings): string[] {
|
|
194
|
+
const cwd = workingDirectory(home, bot, settings);
|
|
195
|
+
const found: string[] = [];
|
|
196
|
+
for (const listed of settings.skills ?? []) {
|
|
197
|
+
for (const path of listedSkills(listed, cwd).directories) {
|
|
198
|
+
if (!found.includes(resolve(path))) found.push(resolve(path));
|
|
199
|
+
}
|
|
190
200
|
}
|
|
201
|
+
return found;
|
|
191
202
|
}
|
|
192
203
|
|
|
193
204
|
/** The directory a new skill of this bot goes into. */
|
|
194
205
|
export function newSkillPath(home: string, bot: string): string {
|
|
195
206
|
const written = readBots(home)[bot];
|
|
196
207
|
if (written === undefined) throw new BotError(`there is no bot "${bot}"`);
|
|
197
|
-
|
|
198
|
-
return join(cwd, "skills");
|
|
208
|
+
return join(workingDirectory(home, bot, written), "skills");
|
|
199
209
|
}
|
|
200
210
|
|
|
201
211
|
function pathInside(path: string, directory: string): boolean {
|
|
@@ -482,9 +492,66 @@ function mcpReview(bot: string, change: McpChange): string {
|
|
|
482
492
|
return out.join("\n");
|
|
483
493
|
}
|
|
484
494
|
|
|
495
|
+
function scheduleLines(entry: ScheduledPrompt, words: string | undefined): string[] {
|
|
496
|
+
const spoken = words === undefined || words.trim().length === 0 ? undefined : words.trim();
|
|
497
|
+
return [
|
|
498
|
+
`- Name: ${entry.name}`,
|
|
499
|
+
`- Schedule: ${spoken === undefined ? `\`${entry.schedule}\`` : `${spoken} (\`${entry.schedule}\`)`}`,
|
|
500
|
+
`- Model: ${entry.model ?? "the bot's model"}`,
|
|
501
|
+
`- Effort: ${entry.effort ?? "the bot's effort"}`,
|
|
502
|
+
...(entry.paused ? ["- Paused: yes"] : []),
|
|
503
|
+
];
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function schedulePromptLines(entry: ScheduledPrompt): string[] {
|
|
507
|
+
return [
|
|
508
|
+
"",
|
|
509
|
+
"### Prompt",
|
|
510
|
+
"",
|
|
511
|
+
"```",
|
|
512
|
+
...entry.prompt.replaceAll("\r\n", "\n").split("\n"),
|
|
513
|
+
"```",
|
|
514
|
+
];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** What a person reads before a scheduled prompt is added, changed or removed. */
|
|
518
|
+
function scheduleReview(bot: string, change: ScheduleChange): string {
|
|
519
|
+
const name = (change.after ?? change.before)?.name ?? "?";
|
|
520
|
+
const action =
|
|
521
|
+
change.action === "add" ? "adds" : change.action === "edit" ? "changes" : "removes";
|
|
522
|
+
const out = [
|
|
523
|
+
`## Scheduled prompt ${name} for bot ${bot}`,
|
|
524
|
+
"",
|
|
525
|
+
`This proposal ${action} a scheduled prompt on bot ${bot}. Approve applies it; reject leaves the bot as it is.`,
|
|
526
|
+
];
|
|
527
|
+
if (change.before !== null) {
|
|
528
|
+
out.push(
|
|
529
|
+
"",
|
|
530
|
+
"### Before",
|
|
531
|
+
"",
|
|
532
|
+
...scheduleLines(change.before, change.action === "remove" ? change.words : undefined),
|
|
533
|
+
...schedulePromptLines(change.before),
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
if (change.after !== null) {
|
|
537
|
+
out.push("", "### After", "", ...scheduleLines(change.after, change.words), ...schedulePromptLines(change.after));
|
|
538
|
+
}
|
|
539
|
+
return out.join("\n");
|
|
540
|
+
}
|
|
541
|
+
|
|
485
542
|
/** The review a person reads for a proposal. */
|
|
486
543
|
export function proposalReview(object: StoredObject): string {
|
|
487
544
|
const data = dataOf(object);
|
|
545
|
+
if (data.kind === "dev") {
|
|
546
|
+
const change = devChangeOf(data.dev);
|
|
547
|
+
return change === undefined ? "No repository is recorded for this proposal." : devReview(data.bot, change);
|
|
548
|
+
}
|
|
549
|
+
if (data.kind === "schedule") {
|
|
550
|
+
const change = scheduleChangeOf(data.schedule);
|
|
551
|
+
return change === undefined
|
|
552
|
+
? "No scheduled prompt is recorded for this proposal."
|
|
553
|
+
: scheduleReview(data.bot, change);
|
|
554
|
+
}
|
|
488
555
|
if (data.kind === "mcp") {
|
|
489
556
|
const change = mcpChangeOf(data.mcp);
|
|
490
557
|
return change === undefined ? "No MCP server is recorded for this proposal." : mcpReview(data.bot, change);
|
|
@@ -536,25 +603,10 @@ export function proposalReview(object: StoredObject): string {
|
|
|
536
603
|
return out.join("\n");
|
|
537
604
|
}
|
|
538
605
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
try {
|
|
542
|
-
parsed = JSON.parse(readFileSync(configPath(home), "utf8"));
|
|
543
|
-
} catch (error) {
|
|
544
|
-
if (isRecord(error) && error.code === "ENOENT") return {};
|
|
545
|
-
throw new BotError(`${configPath(home)}: cannot read the bot settings: ${messageOf(error)}`);
|
|
546
|
-
}
|
|
547
|
-
const plugins = isRecord(parsed) ? parsed.plugins : undefined;
|
|
548
|
-
const entry = isRecord(plugins) ? plugins.bot : undefined;
|
|
549
|
-
const settings = isRecord(entry) ? entry.settings : undefined;
|
|
550
|
-
const bots = isRecord(settings) ? settings.bots : undefined;
|
|
551
|
-
return isRecord(bots) ? { ...bots } : {};
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/** Writes the whole `bots` map, keeping the rest of config.json. */
|
|
555
|
-
async function writeBotsFile(
|
|
606
|
+
/** Reads, changes and writes the `bots` map under one lock, keeping the rest of config.json. */
|
|
607
|
+
async function changeBotsFile(
|
|
556
608
|
home: string,
|
|
557
|
-
bots:
|
|
609
|
+
change: (bots: Record<string, unknown>) => Record<string, unknown>,
|
|
558
610
|
): Promise<void> {
|
|
559
611
|
const file = configPath(home);
|
|
560
612
|
await withLockFile(`${file}.lock`, async () => {
|
|
@@ -564,7 +616,7 @@ async function writeBotsFile(
|
|
|
564
616
|
if (!isRecord(raw)) throw new BotError(`${file}: must be a JSON object`);
|
|
565
617
|
parsed = { ...raw };
|
|
566
618
|
} catch (error) {
|
|
567
|
-
if (!(
|
|
619
|
+
if (!isMissingFile(error)) {
|
|
568
620
|
if (error instanceof BotError) throw error;
|
|
569
621
|
throw new BotError(`${file}: cannot read the bot settings: ${messageOf(error)}`);
|
|
570
622
|
}
|
|
@@ -578,12 +630,21 @@ async function writeBotsFile(
|
|
|
578
630
|
? { ...current }
|
|
579
631
|
: {};
|
|
580
632
|
const settings = isRecord(entry.settings) ? entry.settings : {};
|
|
581
|
-
|
|
633
|
+
const bots = isRecord(settings.bots) ? { ...settings.bots } : {};
|
|
634
|
+
plugins.bot = { ...entry, settings: { ...settings, bots: change(bots) } };
|
|
582
635
|
parsed.plugins = plugins;
|
|
583
636
|
await writeFileAtomic(file, `${JSON.stringify(parsed, null, 2)}\n`);
|
|
584
637
|
});
|
|
585
638
|
}
|
|
586
639
|
|
|
640
|
+
/** Changes the `bots` map under the config lock, so two writers do not drop each other's change. */
|
|
641
|
+
export async function updateBotsFile(
|
|
642
|
+
home: string,
|
|
643
|
+
change: (bots: Record<string, unknown>) => Record<string, unknown>,
|
|
644
|
+
): Promise<void> {
|
|
645
|
+
await changeBotsFile(home, change);
|
|
646
|
+
}
|
|
647
|
+
|
|
587
648
|
export function validateBots(bots: Readonly<Record<string, unknown>>): void {
|
|
588
649
|
prepareSettings(BOT_SETTINGS, { bots }, "bot");
|
|
589
650
|
for (const name of Object.keys(bots)) validateName(name, "bot");
|
|
@@ -662,6 +723,126 @@ export async function proposeSettings(
|
|
|
662
723
|
return `proposed settings for bot ${bot}; review it in pending`;
|
|
663
724
|
}
|
|
664
725
|
|
|
726
|
+
/** What the chat's `bot schedule` command is asked to propose. */
|
|
727
|
+
export interface ScheduleProposalInput {
|
|
728
|
+
readonly action: "add" | "edit" | "remove";
|
|
729
|
+
readonly name: string;
|
|
730
|
+
readonly words?: string | undefined;
|
|
731
|
+
readonly schedule?: string | undefined;
|
|
732
|
+
readonly prompt?: string | undefined;
|
|
733
|
+
readonly model?: string | undefined;
|
|
734
|
+
readonly effort?: string | undefined;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function optionalText(value: string | undefined): string | undefined {
|
|
738
|
+
return value !== undefined && value.trim().length > 0 ? value : undefined;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Proposes adding, changing or removing one of a bot's scheduled prompts. The
|
|
743
|
+
* owner's direct actions apply at once; this path is the one a chat's agent
|
|
744
|
+
* uses, so nothing fires until a person approves the proposal.
|
|
745
|
+
*/
|
|
746
|
+
export async function proposeSchedule(
|
|
747
|
+
store: ObjectStore,
|
|
748
|
+
home: string,
|
|
749
|
+
bot: string,
|
|
750
|
+
input: ScheduleProposalInput,
|
|
751
|
+
): Promise<string> {
|
|
752
|
+
if (readBots(home)[bot] === undefined) throw new BotError(`there is no bot "${bot}"`);
|
|
753
|
+
const object = await store.get(BOT_TYPE, bot);
|
|
754
|
+
if (object === undefined) throw new BotError(`there is no bot "${bot}"`);
|
|
755
|
+
const name = checkPromptName(input.name);
|
|
756
|
+
const current = scheduledPrompts(object.data).find((entry) => entry.name === name);
|
|
757
|
+
if (input.action === "add" && current !== undefined) {
|
|
758
|
+
throw new BotError(`bot ${bot} already has a scheduled prompt "${name}"`);
|
|
759
|
+
}
|
|
760
|
+
if (input.action !== "add" && current === undefined) {
|
|
761
|
+
throw new BotError(`bot ${bot} has no scheduled prompt "${name}"`);
|
|
762
|
+
}
|
|
763
|
+
if (
|
|
764
|
+
input.action === "remove" &&
|
|
765
|
+
(input.schedule !== undefined ||
|
|
766
|
+
input.prompt !== undefined ||
|
|
767
|
+
input.model !== undefined ||
|
|
768
|
+
input.effort !== undefined)
|
|
769
|
+
) {
|
|
770
|
+
throw new BotError(
|
|
771
|
+
"removing a scheduled prompt takes only its name and, optionally, the schedule in words",
|
|
772
|
+
);
|
|
773
|
+
}
|
|
774
|
+
const words = optionalText(input.words);
|
|
775
|
+
if (words === undefined) {
|
|
776
|
+
throw new BotError(
|
|
777
|
+
"give the schedule in words with --words, so the review shows both it and the cron expression",
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
let after: ScheduledPrompt | null = null;
|
|
781
|
+
if (input.action === "add") {
|
|
782
|
+
if (input.schedule === undefined) {
|
|
783
|
+
throw new BotError("adding a scheduled prompt needs --schedule, a five-field cron expression");
|
|
784
|
+
}
|
|
785
|
+
const prompt = input.prompt === undefined ? "" : input.prompt.trim();
|
|
786
|
+
if (prompt.length === 0) throw new BotError("a scheduled prompt needs a prompt");
|
|
787
|
+
const model = optionalText(input.model);
|
|
788
|
+
const effort = optionalText(input.effort);
|
|
789
|
+
after = {
|
|
790
|
+
name,
|
|
791
|
+
schedule: checkSchedule(input.schedule),
|
|
792
|
+
prompt,
|
|
793
|
+
...(model === undefined ? {} : { model }),
|
|
794
|
+
...(effort === undefined ? {} : { effort }),
|
|
795
|
+
paused: false,
|
|
796
|
+
};
|
|
797
|
+
} else if (input.action === "edit" && current !== undefined) {
|
|
798
|
+
if (
|
|
799
|
+
input.schedule === undefined &&
|
|
800
|
+
input.prompt === undefined &&
|
|
801
|
+
input.model === undefined &&
|
|
802
|
+
input.effort === undefined
|
|
803
|
+
) {
|
|
804
|
+
throw new BotError(
|
|
805
|
+
"editing a scheduled prompt needs a field to change: --schedule, --prompt, --model or --effort",
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
const schedule = input.schedule === undefined ? current.schedule : checkSchedule(input.schedule);
|
|
809
|
+
const prompt = input.prompt === undefined ? current.prompt : input.prompt.trim();
|
|
810
|
+
if (prompt.length === 0) throw new BotError("a scheduled prompt needs a prompt");
|
|
811
|
+
const model = input.model === undefined ? current.model : optionalText(input.model);
|
|
812
|
+
const effort = input.effort === undefined ? current.effort : optionalText(input.effort);
|
|
813
|
+
after = {
|
|
814
|
+
name,
|
|
815
|
+
schedule,
|
|
816
|
+
prompt,
|
|
817
|
+
...(model === undefined ? {} : { model }),
|
|
818
|
+
...(effort === undefined ? {} : { effort }),
|
|
819
|
+
paused: current.paused,
|
|
820
|
+
...(schedule === current.schedule
|
|
821
|
+
? current.last === undefined
|
|
822
|
+
? {}
|
|
823
|
+
: { last: current.last }
|
|
824
|
+
: {}),
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
const change: ScheduleChange = {
|
|
828
|
+
action: input.action,
|
|
829
|
+
words: words.trim(),
|
|
830
|
+
before: current ?? null,
|
|
831
|
+
after,
|
|
832
|
+
};
|
|
833
|
+
const data: ProposalData = {
|
|
834
|
+
bot,
|
|
835
|
+
kind: "schedule",
|
|
836
|
+
title: `Scheduled prompt ${name}`,
|
|
837
|
+
state: "pending",
|
|
838
|
+
createdAt: Date.now(),
|
|
839
|
+
schedule: change,
|
|
840
|
+
};
|
|
841
|
+
const id = `p-${randomUUID()}`;
|
|
842
|
+
await store.put(PROPOSAL_TYPE, id, data as unknown as Record<string, JsonValue>, "command:bot/schedule");
|
|
843
|
+
return `proposed scheduled prompt "${name}" for bot ${bot}; review it in pending`;
|
|
844
|
+
}
|
|
845
|
+
|
|
665
846
|
function skillData(value: unknown): ProposalData["skill"] | undefined {
|
|
666
847
|
if (!isRecord(value) || typeof value.name !== "string") return undefined;
|
|
667
848
|
const before = value.before === null ? null : snapshotOf(value.before) ?? null;
|
|
@@ -691,34 +872,42 @@ function applySkill(skill: NonNullable<ProposalData["skill"]>): void {
|
|
|
691
872
|
}
|
|
692
873
|
|
|
693
874
|
async function applySettings(home: string, bot: string, after: Readonly<Record<string, JsonValue>>): Promise<void> {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
875
|
+
await updateBotsFile(home, (bots) => {
|
|
876
|
+
const next = { ...bots, [bot]: after };
|
|
877
|
+
validateBots(next);
|
|
878
|
+
return next;
|
|
879
|
+
});
|
|
697
880
|
}
|
|
698
881
|
|
|
699
882
|
async function applyMcp(home: string, bot: string, change: McpChange): Promise<void> {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
883
|
+
await updateBotsFile(home, (bots) => {
|
|
884
|
+
const raw = isRecord(bots[bot]) ? bots[bot] : {};
|
|
885
|
+
const servers = isRecord(raw.mcpServers) ? raw.mcpServers : {};
|
|
886
|
+
const next = {
|
|
887
|
+
...bots,
|
|
888
|
+
[bot]: { ...raw, mcpServers: { ...servers, [change.server]: change.after } },
|
|
889
|
+
};
|
|
890
|
+
validateBots(next);
|
|
891
|
+
return next;
|
|
892
|
+
});
|
|
706
893
|
}
|
|
707
894
|
|
|
708
895
|
async function ensureSkillListed(home: string, bot: string, path: string): Promise<void> {
|
|
709
896
|
const written = readBots(home)[bot];
|
|
710
897
|
if (written === undefined) throw new BotError(`there is no bot "${bot}"`);
|
|
711
|
-
if (
|
|
898
|
+
if (mountedSkills(home, bot, written).some((item) => resolve(item) === resolve(path))) {
|
|
712
899
|
return;
|
|
713
900
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
901
|
+
await updateBotsFile(home, (bots) => {
|
|
902
|
+
const raw = isRecord(bots[bot]) ? bots[bot] : {};
|
|
903
|
+
const skills = Array.isArray(raw.skills)
|
|
904
|
+
? raw.skills.filter((item): item is string => typeof item === "string")
|
|
905
|
+
: [];
|
|
906
|
+
if (skills.some((item) => resolve(item) === resolve(path))) return bots;
|
|
907
|
+
const next = { ...bots, [bot]: { ...raw, skills: [...skills, path] } };
|
|
908
|
+
validateBots(next);
|
|
909
|
+
return next;
|
|
910
|
+
});
|
|
722
911
|
}
|
|
723
912
|
|
|
724
913
|
/** Applies a pending proposal. A proposal already decided is left as it is. */
|
|
@@ -734,7 +923,15 @@ export async function applyProposal(
|
|
|
734
923
|
return `proposal ${id} is already ${data.state}; nothing to apply`;
|
|
735
924
|
}
|
|
736
925
|
const skill = skillData(data.skill);
|
|
737
|
-
if (data.kind === "
|
|
926
|
+
if (data.kind === "dev") {
|
|
927
|
+
const change = devChangeOf(data.dev);
|
|
928
|
+
if (change === undefined) throw new BotError(`proposal ${id} has no repository to set up`);
|
|
929
|
+
await applyDev(home, change);
|
|
930
|
+
} else if (data.kind === "schedule") {
|
|
931
|
+
const change = scheduleChangeOf(data.schedule);
|
|
932
|
+
if (change === undefined) throw new BotError(`proposal ${id} has no scheduled prompt to apply`);
|
|
933
|
+
await applyScheduleChange(store, data.bot, change);
|
|
934
|
+
} else if (data.kind === "mcp") {
|
|
738
935
|
const change = mcpChangeOf(data.mcp);
|
|
739
936
|
if (change === undefined) throw new BotError(`proposal ${id} has no MCP server to apply`);
|
|
740
937
|
await applyMcp(home, data.bot, change);
|
|
@@ -858,32 +1055,115 @@ export async function runSettingCommand(ctx: CommandContext, argv: readonly stri
|
|
|
858
1055
|
return proposeSettings(ctx.store, ctx.home, bot, patch);
|
|
859
1056
|
}
|
|
860
1057
|
|
|
861
|
-
/** The
|
|
1058
|
+
/** The `bot setup-dev` command: the steps for the agent, or with `dev.json` on stdin the proposal (ADR 0030 D15). */
|
|
1059
|
+
export async function runSetupDev(ctx: CommandContext, argv: readonly string[], piped?: PipedInput): Promise<string> {
|
|
1060
|
+
return runSetupDevCommand(ctx, argv, async (bot, change) => {
|
|
1061
|
+
const data: ProposalData = {
|
|
1062
|
+
bot,
|
|
1063
|
+
kind: "dev",
|
|
1064
|
+
title: `Setup Dev for ${basename(change.repo)}`,
|
|
1065
|
+
state: "pending",
|
|
1066
|
+
createdAt: Date.now(),
|
|
1067
|
+
dev: change,
|
|
1068
|
+
};
|
|
1069
|
+
const id = `p-${randomUUID()}`;
|
|
1070
|
+
await ctx.store.put(PROPOSAL_TYPE, id, data as unknown as Record<string, JsonValue>, "command:bot/setup-dev");
|
|
1071
|
+
return `proposed Setup Dev for ${change.repo}; review it in pending`;
|
|
1072
|
+
}, piped);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/** The usage line shared by the parser's refusals. */
|
|
1076
|
+
const SCHEDULE_USAGE =
|
|
1077
|
+
"usage: operad bot schedule <bot> add|edit|remove <name> [--words <text>] [--schedule <cron>] [--prompt <text>] [--model <model>] [--effort <effort>]";
|
|
1078
|
+
|
|
1079
|
+
/** Parses the `bot schedule` command: what the chat's agent calls to propose an entry. */
|
|
1080
|
+
export async function runScheduleCommand(
|
|
1081
|
+
ctx: CommandContext,
|
|
1082
|
+
argv: readonly string[],
|
|
1083
|
+
): Promise<string> {
|
|
1084
|
+
const positional: string[] = [];
|
|
1085
|
+
let words: string | undefined;
|
|
1086
|
+
let schedule: string | undefined;
|
|
1087
|
+
let prompt: string | undefined;
|
|
1088
|
+
let model: string | undefined;
|
|
1089
|
+
let effort: string | undefined;
|
|
1090
|
+
for (let at = 0; at < argv.length; at += 1) {
|
|
1091
|
+
const word = argv[at];
|
|
1092
|
+
if (word === undefined) continue;
|
|
1093
|
+
if (
|
|
1094
|
+
word === "--words" ||
|
|
1095
|
+
word === "--schedule" ||
|
|
1096
|
+
word === "--prompt" ||
|
|
1097
|
+
word === "--model" ||
|
|
1098
|
+
word === "--effort"
|
|
1099
|
+
) {
|
|
1100
|
+
const value = argv[at + 1];
|
|
1101
|
+
if (value === undefined) throw new BotError(`bot schedule: ${word} needs a value`);
|
|
1102
|
+
if (word === "--words") words = value;
|
|
1103
|
+
else if (word === "--schedule") schedule = value;
|
|
1104
|
+
else if (word === "--prompt") prompt = value;
|
|
1105
|
+
else if (word === "--model") model = value;
|
|
1106
|
+
else effort = value;
|
|
1107
|
+
at += 1;
|
|
1108
|
+
continue;
|
|
1109
|
+
}
|
|
1110
|
+
if (word.startsWith("--")) {
|
|
1111
|
+
throw new BotError(`bot schedule: there is no option ${word}; ${SCHEDULE_USAGE}`);
|
|
1112
|
+
}
|
|
1113
|
+
positional.push(word);
|
|
1114
|
+
}
|
|
1115
|
+
const bot = positional[0];
|
|
1116
|
+
const action = positional[1];
|
|
1117
|
+
const name = positional[2];
|
|
1118
|
+
if (
|
|
1119
|
+
bot === undefined ||
|
|
1120
|
+
name === undefined ||
|
|
1121
|
+
positional.length > 3 ||
|
|
1122
|
+
(action !== "add" && action !== "edit" && action !== "remove")
|
|
1123
|
+
) {
|
|
1124
|
+
throw new BotError(SCHEDULE_USAGE);
|
|
1125
|
+
}
|
|
1126
|
+
return proposeSchedule(ctx.store, ctx.home, bot, {
|
|
1127
|
+
action,
|
|
1128
|
+
name,
|
|
1129
|
+
words,
|
|
1130
|
+
schedule,
|
|
1131
|
+
prompt,
|
|
1132
|
+
model,
|
|
1133
|
+
effort,
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* The actions on a proposal. Approve comes first, so it is the main button of a
|
|
1139
|
+
* client; review is last because a proposal's Review section already shows what
|
|
1140
|
+
* it returns, and it stays for the command line and MCP.
|
|
1141
|
+
*/
|
|
862
1142
|
export const proposalActions: readonly ActionTypeDefinition[] = [
|
|
863
1143
|
{
|
|
864
|
-
name: "
|
|
1144
|
+
name: "approve",
|
|
865
1145
|
on: PROPOSAL_TYPE,
|
|
866
1146
|
when: (state) => state.state === "pending",
|
|
867
1147
|
async run(ctx) {
|
|
868
|
-
|
|
869
|
-
if (object === undefined) throw new BotError(`there is no proposal "${ctx.object.id}"`);
|
|
870
|
-
return proposalReview(object);
|
|
1148
|
+
return applyProposal(ctx.store, ctx.home, ctx.object.id);
|
|
871
1149
|
},
|
|
872
1150
|
},
|
|
873
1151
|
{
|
|
874
|
-
name: "
|
|
1152
|
+
name: "reject",
|
|
875
1153
|
on: PROPOSAL_TYPE,
|
|
876
1154
|
when: (state) => state.state === "pending",
|
|
877
1155
|
async run(ctx) {
|
|
878
|
-
return
|
|
1156
|
+
return rejectProposal(ctx.store, ctx.object.id);
|
|
879
1157
|
},
|
|
880
1158
|
},
|
|
881
1159
|
{
|
|
882
|
-
name: "
|
|
1160
|
+
name: "review",
|
|
883
1161
|
on: PROPOSAL_TYPE,
|
|
884
1162
|
when: (state) => state.state === "pending",
|
|
885
1163
|
async run(ctx) {
|
|
886
|
-
|
|
1164
|
+
const object = await ctx.store.get(PROPOSAL_TYPE, ctx.object.id);
|
|
1165
|
+
if (object === undefined) throw new BotError(`there is no proposal "${ctx.object.id}"`);
|
|
1166
|
+
return proposalReview(object);
|
|
887
1167
|
},
|
|
888
1168
|
},
|
|
889
1169
|
];
|