Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(cli): honor conversation selectors in bot commands

Afloat427decacd168
diff --git a/server/headless/commands.ts b/server/headless/commands.ts
index f02038595a..351fdbfd71 100644
--- a/server/headless/commands.ts
+++ b/server/headless/commands.ts
@@ -205,9 +205,7 @@ export async function newChannel(ctx: VerbContext): Promise<VerbOutcome> {
export async function setModel(ctx: VerbContext): Promise<VerbOutcome> {
const selection = modelArgs(ctx, true);
- const explicit = flag(ctx.flags, "bot")?.trim();
- const target = explicit ? { type: "bot" as const, id: explicit, ...(flag(ctx.flags, "task") ? { taskId: flag(ctx.flags, "task")!.trim() } : {}) }
- : await resolveTarget(ctx, ctx.positionals[0], "bot");
+ const target = await resolveTarget(ctx, ctx.positionals[0], "bot");
const args = { bot_id: target.id, ...(target.taskId ? { task_id: target.taskId } : {}), ...selection };
const preview = dryRun(ctx, "set_bot_model", args);
if (preview) return preview;
@@ -216,13 +214,12 @@ export async function setModel(ctx: VerbContext): Promise<VerbOutcome> {
}
export async function edit(ctx: VerbContext): Promise<VerbOutcome> {
- const explicit = flag(ctx.flags, "bot")?.trim();
+ const named = Boolean(flag(ctx.flags, "bot") ?? flag(ctx.flags, "channel") ?? flag(ctx.flags, "thread"));
const [first, second] = ctx.positionals;
- const target: Target | undefined = explicit ? { type: "bot", id: explicit, ...(flag(ctx.flags, "task") ? { taskId: flag(ctx.flags, "task")!.trim() } : {}) } : undefined;
- if (!target && !first) throw usageError("a bot is required", "openmausbot edit BOT --message ID TEXT, or --bot ID");
+ if (!named && !first) throw usageError("a bot is required", "openmausbot edit BOT --message ID TEXT, or --bot ID or --thread ID");
const messageId = requiredFlag(ctx.flags, "message");
- const text = await readMessage(ctx, target ? first : second);
- const resolved = target ?? await resolveTarget(ctx, first, "bot");
+ const text = await readMessage(ctx, named ? first : second);
+ const resolved = await resolveTarget(ctx, named ? undefined : first, "bot");
const args = { bot_id: resolved.id, message_id: messageId, text, ...(resolved.taskId ? { task_id: resolved.taskId } : {}) };
const preview = dryRun(ctx, "edit_bot_message", args);
if (preview) return preview;
diff --git a/server/headless/edit.test.ts b/server/headless/edit.test.ts
new file mode 100644
index 0000000000..306364a982
--- /dev/null
+++ b/server/headless/edit.test.ts
@@ -0,0 +1,87 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { runHeadless } from "./main.ts";
+import type { HeadlessIo } from "./output.ts";
+
+function harness(verb: "edit" | "set-model" = "edit") {
+ const callTool = vi.fn(async () => ({ success: true }));
+ const request = vi.fn(async (path: string) => {
+ if (path !== "/api/bots?messages=0") throw new Error(`unexpected path ${path}`);
+ return {
+ bots: [{ id: "lead", name: "Lead", threadId: "active", tasks: [{ threadId: "active" }, { threadId: "earlier" }] }],
+ groups: [{ id: "room", name: "Room", threadId: "room-thread" }],
+ };
+ });
+ const run = async (argv: string[]) => {
+ const out: string[] = [];
+ const io: HeadlessIo = { out: (text) => out.push(text), err: () => undefined, readStdin: async () => "Corrected request" };
+ const options = verb === "edit" ? ["--message", "m1"] : ["--model", "claude/claude-sonnet-5-5"];
+ const code = await runHeadless([verb, ...argv, ...options, "--json"], {
+ env: { OPENMAUSBOT_URL: "http://127.0.0.1:19999" }, io, callTool: callTool as any, request: request as any,
+ });
+ return { code, result: JSON.parse(out[0]!) };
+ };
+ return { callTool, run };
+}
+
+describe("edit conversation selection", () => {
+ it.each([
+ ["Corrected request"],
+ ["--text", "Corrected request"],
+ ["-"],
+ ])("edits a named thread with message arguments %j", async (...message) => {
+ const h = harness();
+ expect((await h.run(["--thread", "earlier", ...message])).code).toBe(0);
+ expect(h.callTool).toHaveBeenCalledWith("edit_bot_message", {
+ bot_id: "lead", task_id: "earlier", message_id: "m1", text: "Corrected request",
+ }, expect.any(Function), undefined, undefined);
+ });
+
+ it.each([["Lead"], ["--bot", "lead"]])("keeps bot selection %j and --task", async (...target) => {
+ const h = harness();
+ expect((await h.run([...target, "--task", "earlier", "--text", "Corrected request"])).code).toBe(0);
+ expect(h.callTool).toHaveBeenCalledWith("edit_bot_message", {
+ bot_id: "lead", task_id: "earlier", message_id: "m1", text: "Corrected request",
+ }, expect.any(Function), undefined, undefined);
+ });
+
+ it("previews the selected thread without editing it", async () => {
+ const h = harness();
+ const preview = await h.run(["--thread", "earlier", "--text", "Corrected request", "--dry-run"]);
+ expect(preview.code).toBe(0);
+ expect(preview.result).toMatchObject({ dryRun: true, tool: "edit_bot_message", arguments: { bot_id: "lead", task_id: "earlier" } });
+ expect(h.callTool).not.toHaveBeenCalled();
+ });
+
+ it.each([["--thread", "room-thread"], ["--thread", "missing"], ["--bot", "lead", "--thread", "earlier"]])(
+ "refuses an invalid or conflicting target %j before editing", async (...target) => {
+ const h = harness();
+ expect((await h.run([...target, "--text", "Corrected request"])).code).toBe(2);
+ expect(h.callTool).not.toHaveBeenCalled();
+ },
+ );
+});
+
+describe("set-model conversation selection", () => {
+ it.each([
+ ["Lead", "--task", "earlier"],
+ ["--bot", "lead", "--task", "earlier"],
+ ["--thread", "earlier"],
+ ])("sets the selected bot thread's model with %j", async (...target) => {
+ const h = harness("set-model");
+ expect((await h.run(target)).code).toBe(0);
+ expect(h.callTool).toHaveBeenCalledWith("set_bot_model", {
+ bot_id: "lead", task_id: "earlier", instance_id: "claude", model: "claude-sonnet-5-5",
+ }, expect.any(Function), undefined, undefined);
+ });
+
+ it.each([
+ ["--bot", "lead", "--thread", "earlier"],
+ ["--bot", "lead", "--channel", "room"],
+ ["--bot", "lead", "Lead"],
+ ])("refuses conflicting target %j before changing the model", async (...target) => {
+ const h = harness("set-model");
+ expect((await h.run(target)).code).toBe(2);
+ expect(h.callTool).not.toHaveBeenCalled();
+ });
+});