Skip to content

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

fix(agents): set_main_thread refuses a non-string thread_id or non-boolean clear

AdityaUmale48f27f8f9c79
diff --git a/server/drivers/agents-main-thread-tool.test.ts b/server/drivers/agents-main-thread-tool.test.ts
new file mode 100644
index 0000000000..1eda96eb24
--- /dev/null
+++ b/server/drivers/agents-main-thread-tool.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+
+import type { ToolCallContext } from "./agents-call.ts";
+import type { Json } from "./agents-client.ts";
+import { callSetMainThreadTool } from "./agents-main-thread-tool.ts";
+
+async function run(args: Json) {
+ const sent: unknown[] = [];
+ const context: ToolCallContext = {
+ botId: "bot-a", threadId: "thread-here", externalRuntime: false, sharedComputers: false,
+ client: {
+ api: async (_path, init) => { sent.push(JSON.parse(String(init?.body))); return { title: "Daily" }; },
+ apiResponse: async () => ({ ok: true, status: 200, body: {} }),
+ },
+ };
+ return { result: await callSetMainThreadTool(args, context), sent };
+}
+
+describe("set_main_thread", () => {
+ it.each([{ thread_id: 42 }, { clear: "true" }])("refuses %j instead of choosing this thread", async (args) => {
+ const { result, sent } = await run(args);
+ expect(result.isError).toBe(true);
+ expect(result.text).toMatch(/^set_main_thread needs /);
+ expect(sent).toEqual([]);
+ });
+
+ it.each([{}, { thread_id: null }])("defaults to this thread when thread_id is %j", async (args) => {
+ const { result, sent } = await run(args);
+ expect(result.isError).toBeUndefined();
+ expect(sent).toEqual([{ fromBotId: "bot-a", fromThreadId: "thread-here" }]);
+ });
+
+ it("sends the chosen thread", async () => {
+ const { sent } = await run({ thread_id: " thread-daily " });
+ expect(sent).toEqual([{ fromBotId: "bot-a", fromThreadId: "thread-here", mainThreadId: "thread-daily" }]);
+ });
+});
diff --git a/server/drivers/agents-main-thread-tool.ts b/server/drivers/agents-main-thread-tool.ts
index 02c41fa5c8..cd8995d038 100644
--- a/server/drivers/agents-main-thread-tool.ts
+++ b/server/drivers/agents-main-thread-tool.ts
@@ -7,6 +7,10 @@ export const SET_MAIN_THREAD_TOOL = "set_main_thread";
export async function callSetMainThreadTool(args: Json, context: ToolCallContext): Promise<ToolCallResult> {
const clear = args.clear === true;
+ if (args.thread_id != null && typeof args.thread_id !== "string") {
+ return { text: "set_main_thread needs thread_id as a string from list_threads or start_thread; leave it out for this thread.", isError: true };
+ }
+ if (args.clear != null && typeof args.clear !== "boolean") return { text: "set_main_thread needs clear as true or false.", isError: true };
const threadId = typeof args.thread_id === "string" ? args.thread_id.trim() : "";
if (clear && threadId) return { text: "set_main_thread takes thread_id or clear, not both.", isError: true };
const r = await context.client.api("/api/internal/main-thread", {