Skip to content

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

fix(bot-api): the section lives in Bot settings, and its tests don't race

milind-soni0add5dd11d25
diff --git a/docs/bot-api.md b/docs/bot-api.md
index 87b8a0fead..bfb7c94e48 100644
--- a/docs/bot-api.md
+++ b/docs/bot-api.md
@@ -5,7 +5,7 @@ with a key and gets its reply. It is off by default, per bot.
## Turning it on
-In the bot's panel (Advanced: Bot settings → Access), **Bot API**: switch on **Let apps
+In Bot settings (the gear in the bot's panel; Advanced: Access), **Bot API**: switch on **Let apps
talk to this bot**. The card shows the command to paste, with the key filled in.
The key is shown that once and kept hashed; when the card is opened again the
command shows `YOUR_KEY` in its place. **New key** replaces the key (apps using
diff --git a/server/bot-api-host.test.ts b/server/bot-api-host.test.ts
index e3ae311210..9a6b5b34a0 100644
--- a/server/bot-api-host.test.ts
+++ b/server/bot-api-host.test.ts
@@ -2,7 +2,7 @@ import { mkdtempSync, rmSync } from "node:fs";
import { createServer, type Server } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { BotApiManager } from "./bot-api.ts";
import { BOT_API_DEFAULT_WAIT_MS, createBotApiHost, type BotApiHostDeps, type BotApiTurnOptions } from "./bot-api-host.ts";
@@ -211,16 +211,17 @@ describe("the Bot API door on the webhook receiver", () => {
});
it("answers 'working' when the reply takes longer than the wait, and 409 while that thread is still busy", async () => {
+ const finish: Array<() => void> = [];
const h = await harness((fake, _botId, _prompt, opts) => {
- setTimeout(() => { fake.store.appendMessage(opts.threadId, { role: "bot", kind: "text", text: "Late" }); opts.onSettled({ ok: true, text: "Late" }); }, 400);
- }, { waitMs: 150 });
+ finish.push(() => { fake.store.appendMessage(opts.threadId, { role: "bot", kind: "text", text: "Late" }); opts.onSettled({ ok: true, text: "Late" }); });
+ }, { waitMs: 50 });
const key = (await h.app("POST", "/api/bots/b1/bot-api", { enabled: true })).body.key as string;
const answer = await h.call("b1", key, { message: "Think hard" });
expect(answer).toEqual({ status: 202, body: { thread: expect.any(String), status: "working" } });
const threadId = answer.body.thread as string;
expect(await h.call("b1", key, { message: "Again", thread: threadId })).toEqual({ status: 409, body: { thread: threadId, status: "working" } });
expect(await h.call("b1", key, undefined, { method: "GET", query: `?thread=${threadId}` })).toEqual({ status: 202, body: { thread: threadId, status: "working" } });
- await new Promise((resolve) => setTimeout(resolve, 500));
+ finish.shift()!();
expect(await h.call("b1", key, undefined, { method: "GET", query: `?thread=${threadId}` })).toEqual({ status: 200, body: { reply: "Late", thread: threadId } });
// The caller may ask for a shorter wait, never a longer one.
const quick = await h.call("b1", key, { message: "Quick", wait: 0 });
@@ -253,7 +254,7 @@ describe("the Bot API door on the webhook receiver", () => {
const slow = await harness((fake, _botId, _prompt, opts) => { fake.bots.get("b1")!.tasks.find((task) => task.threadId === opts.threadId)!.busy = true; }, { waitMs: 10 });
const slowKey = (await slow.app("POST", "/api/bots/b1/bot-api", { enabled: true })).body.key as string;
slow.manager.admit = () => true;
- for (let i = 0; i < 50; i += 1) expect((await slow.call("b1", slowKey, { message: `slow ${i}` })).body).toMatchObject({ status: "working" });
+ for (let i = 0; i < 50; i += 1) expect((await slow.call("b1", slowKey, { message: `slow ${i}`, wait: 0 })).body).toMatchObject({ status: "working" });
expect((await slow.call("b1", slowKey, { message: "one too many" })).status).toBe(409);
});
@@ -348,13 +349,18 @@ describe("the Bot API door on the webhook receiver", () => {
it("answers within the proxy's idle limit by default, and a caller may ask for longer", async () => {
expect(BOT_API_DEFAULT_WAIT_MS).toBeLessThan(60_000);
+ const finish: Array<() => void> = [];
const h = await harness((fake, _botId, _prompt, opts) => {
- setTimeout(() => { fake.store.appendMessage(opts.threadId, { role: "bot", kind: "text", text: "Late" }); opts.onSettled({ ok: true, text: "Late" }); }, 300);
+ finish.push(() => { fake.store.appendMessage(opts.threadId, { role: "bot", kind: "text", text: "Late" }); opts.onSettled({ ok: true, text: "Late" }); });
}, { waitMs: 50 });
const key = (await h.app("POST", "/api/bots/b1/bot-api", { enabled: true })).body.key as string;
expect((await h.call("b1", key, { message: "Default" })).body).toMatchObject({ status: "working" });
- await new Promise((resolve) => setTimeout(resolve, 350));
- expect((await h.call("b1", key, { message: "Longer", wait: 5 })).body).toMatchObject({ reply: "Late" });
+ finish.shift()!();
+ const longer = h.call("b1", key, { message: "Longer", wait: 30 });
+ await vi.waitFor(() => expect(finish).toHaveLength(1));
+ await new Promise((resolve) => setTimeout(resolve, 120));
+ finish.shift()!();
+ expect((await longer).body).toMatchObject({ reply: "Late" });
expect((await h.call("b1", key, { message: "Too long", wait: 301 })).status).toBe(400);
});
diff --git a/src/components/BotSettingsDialog.simple.test.ts b/src/components/BotSettingsDialog.simple.test.ts
index b61556879f..f4825a9acc 100644
--- a/src/components/BotSettingsDialog.simple.test.ts
+++ b/src/components/BotSettingsDialog.simple.test.ts
@@ -404,7 +404,7 @@ describe("the bot panel in Simple mode", () => {
expect(shown(panel(makeBot(), "access"))).toEqual({ tab: "computer", page: undefined });
});
- it("shows no switch on any tab or on Bot settings: memory, upkeep, browser, record steps and Work until done live elsewhere", () => {
+ it("shows no switch on any tab, and on Bot settings only Bot API's: memory, upkeep, browser, record steps and Work until done live elsewhere", () => {
const bot = makeBot();
expect(switches(panel(bot).html)).toBe(0);
expect(panel(bot).html).not.toContain('aria-label="Work until done"');
@@ -415,7 +415,9 @@ describe("the bot panel in Simple mode", () => {
for (const text of ["Memory", "Upkeep", "Record"]) expect(rendered.html).not.toContain(`>${text}<`);
}
click(find(panel(bot), "data-panel-gear"));
- expect(switches(panel(bot).html)).toBe(0);
+ const settings = panel(bot).html;
+ expect(switches(settings)).toBe(1);
+ expect(settings).toContain('aria-label="Let apps talk to this bot"');
});
});
@@ -651,7 +653,8 @@ describe("the Bot settings page", () => {
.map((marker) => rendered.html.indexOf(marker));
expect(order.every((index) => index > -1)).toBe(true);
expect([...order].sort((a, b) => a - b)).toEqual(order);
- expect(switches(rendered.html)).toBe(0);
+ expect(switches(rendered.html)).toBe(1);
+ expect(rendered.html).toContain('aria-label="Let apps talk to this bot"');
expect(rendered.html.match(/pl-\[38px\]/g)).toHaveLength(4);
});
diff --git a/src/components/bot-panel/DetailsTab.tsx b/src/components/bot-panel/DetailsTab.tsx
index 87990eada8..0db7b97f14 100644
--- a/src/components/bot-panel/DetailsTab.tsx
+++ b/src/components/bot-panel/DetailsTab.tsx
@@ -6,7 +6,6 @@ import type { Bot } from "@/state/store";
import { useChatMoreItems } from "../ChatMoreMenu";
import { ModelPicker } from "../ModelPicker";
import type { SidebarMenuItem } from "../SidebarPopoverMenu";
-import { BotApiCard } from "../bot-settings/BotApiCard";
import type { useBotSettingsDerived } from "../bot-settings/useBotSettingsDerived";
import { BotConversations } from "./Conversations";
import type { SimplePageId } from "./page-registry";
@@ -85,8 +84,6 @@ export function DetailsTab({ bot, derived, onOpen }: {
</Card>
)}
- <BotApiCard bot={bot} webhooksLink className="mt-3.5" />
-
<TryAsking bot={bot} />
{conversationItems.length > 0 && (
diff --git a/src/components/bot-panel/SettingsPage.tsx b/src/components/bot-panel/SettingsPage.tsx
index f7ec5e6b3d..4744917ee9 100644
--- a/src/components/bot-panel/SettingsPage.tsx
+++ b/src/components/bot-panel/SettingsPage.tsx
@@ -13,6 +13,7 @@ import { ApprovalChip } from "../ComposerPlusMenu";
import { useDesktopCapabilities } from "../DesktopCapabilities";
import { DefaultBotHideRow } from "../DefaultBotControls";
import { SoulField } from "../SoulField";
+import { BotApiCard } from "../bot-settings/BotApiCard";
import { useBotApps } from "../bot-settings/useBotApps";
import type { useBotSettingsDerived } from "../bot-settings/useBotSettingsDerived";
import { useThreadApproval } from "../thread-approval";
@@ -191,6 +192,8 @@ export function SettingsPage({ bot, derived, onOpen, editingInstructions = false
</label>
</Card>
+ <BotApiCard bot={bot} webhooksLink className="mt-3.5" />
+
<DefaultBotHideRow bot={bot} />
{approval.dialogs}
</div>