Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Honor Anthropic API keys in built-in and Mastra Bots (#614)

David McKay3af815a56222
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3872dd12..9dbe0b9b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,13 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### Built-in and Mastra Bots can run on Anthropic API keys
+
+The example built-in Bots and the Mastra Bot now use the selected model provider instead of
+assuming OpenAI. A deployment with `BOT_PROVIDER=anthropic`, `BOT_MODEL=claude-sonnet-4-5` and an
+Anthropic key routes built-in model calls, tool selection and Mastra runs through Anthropic's native
+API. Source startup no longer waits for the unused OpenAI-only sample when Anthropic is selected.
+
### The Agno Bot answers on an OpenAI key
Picked with an OpenAI key, the Agno Bot failed every run before reaching OpenAI. Agno sends a
diff --git a/agent-mastra/package.json b/agent-mastra/package.json
index 51541dd5..e9e57716 100644
--- a/agent-mastra/package.json
+++ b/agent-mastra/package.json
@@ -3,6 +3,7 @@
"private": true,
"type": "module",
"dependencies": {
+ "@ai-sdk/anthropic": "*",
"@ai-sdk/openai": "*",
"@mastra/core": "*",
"mastra": "*"
diff --git a/agent-mastra/src/mastra/index.test.ts b/agent-mastra/src/mastra/index.test.ts
index ea67b26c..93454bb1 100644
--- a/agent-mastra/src/mastra/index.test.ts
+++ b/agent-mastra/src/mastra/index.test.ts
@@ -205,3 +205,229 @@ describe("OpenBot Mastra listen port configuration", () => {
});
}
});
+
+describe("OpenBot Mastra provider requests", () => {
+ const choices: {
+ provider: string;
+ base: string;
+ model: string;
+ url: string;
+ apiKey?: string;
+ error?: string;
+ }[] = [
+ {
+ provider: "anthropic",
+ base: "",
+ model: "claude-sonnet-4-5",
+ url: "https://api.anthropic.com/v1/messages",
+ apiKey: "test-anthropic",
+ },
+ {
+ provider: "anthropic",
+ base: "http://anthropic.test/v1",
+ model: "claude-sonnet-4-5",
+ url: "http://anthropic.test/v1/messages",
+ apiKey: "test-anthropic",
+ },
+ {
+ provider: "",
+ base: "",
+ model: "gpt-5.5",
+ url: "https://api.openai.com/v1/responses",
+ apiKey: "test-openai",
+ },
+ {
+ provider: "openai",
+ base: " https://api.openai.com/v1/ ",
+ model: "gpt-5.5",
+ url: "https://api.openai.com/v1/responses",
+ apiKey: "test-openai",
+ },
+ {
+ provider: "",
+ base: "http://compatible.test/v1",
+ model: "llama3.1:8b",
+ url: "http://compatible.test/v1/chat/completions",
+ apiKey: "test-openai",
+ },
+ ...[
+ "http://anthropic.test",
+ " http://anthropic.test/proxy/ ",
+ "http://anthropic.test/proxy/v1/",
+ ].map((base) => ({
+ provider: "anthropic",
+ base,
+ model: "claude-sonnet-4-5",
+ apiKey: "test-anthropic",
+ url: base.includes("proxy")
+ ? "http://anthropic.test/proxy/v1/messages"
+ : "http://anthropic.test/v1/messages",
+ })),
+ ...[undefined, ""].map((apiKey) => ({
+ provider: "openai",
+ base: "http://compatible.test/v1",
+ model: "llama3.1:8b",
+ apiKey,
+ url: "http://compatible.test/v1/chat/completions",
+ })),
+ {
+ provider: "openai",
+ base: "",
+ model: "gpt-5.5",
+ url: "https://api.openai.com/v1/responses",
+ error: "OpenAI API key is missing",
+ },
+ ];
+
+ for (const choice of choices) {
+ test(`uses ${choice.base || "the default endpoint"} with ${choice.apiKey === undefined ? "no" : choice.apiKey || "an empty"} API key`, async () => {
+ const seen: { url: string | null; key: string | null; model: string }[] =
+ [];
+ const provider = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ async fetch(request) {
+ const body = await request.json();
+ seen.push({
+ url: request.headers.get("x-test-original-url"),
+ key: request.headers.get(
+ choice.provider === "anthropic" ? "x-api-key" : "authorization",
+ ),
+ model: body.model,
+ });
+ const path = new URL(request.url).pathname;
+ if (path.endsWith("/v1/messages")) {
+ return Response.json({
+ id: "msg",
+ type: "message",
+ role: "assistant",
+ model: body.model,
+ content: [{ type: "text", text: "hello" }],
+ stop_reason: "end_turn",
+ stop_sequence: null,
+ usage: { input_tokens: 1, output_tokens: 1 },
+ });
+ }
+ if (path === "/v1/chat/completions") {
+ return Response.json({
+ id: "chat",
+ object: "chat.completion",
+ created: 0,
+ model: body.model,
+ choices: [
+ {
+ index: 0,
+ finish_reason: "stop",
+ message: { role: "assistant", content: "hello" },
+ },
+ ],
+ usage: {
+ prompt_tokens: 1,
+ completion_tokens: 1,
+ total_tokens: 2,
+ },
+ });
+ }
+ if (path === "/v1/responses") {
+ return Response.json({
+ id: "resp",
+ object: "response",
+ created_at: 0,
+ model: body.model,
+ status: "completed",
+ output: [
+ {
+ id: "msg",
+ type: "message",
+ role: "assistant",
+ status: "completed",
+ content: [
+ { type: "output_text", text: "hello", annotations: [] },
+ ],
+ },
+ ],
+ usage: {
+ input_tokens: 1,
+ output_tokens: 1,
+ input_tokens_details: { cached_tokens: 0 },
+ output_tokens_details: { reasoning_tokens: 0 },
+ },
+ });
+ }
+ return new Response("unexpected provider route", { status: 404 });
+ },
+ });
+ const child = Bun.spawn(
+ [
+ Bun.argv[0],
+ "-e",
+ [
+ // Only HTTP is replaced: the real Mastra Agent and provider SDK build the request.
+ "const networkFetch = globalThis.fetch;",
+ "globalThis.fetch = (input, init) => {",
+ " const request = new Request(input, init);",
+ " const url = new URL(request.url);",
+ ' request.headers.set("x-test-original-url", request.url);',
+ ` return networkFetch(new Request(${JSON.stringify(provider.url.toString())} + url.pathname.slice(1) + url.search, request));`,
+ "};",
+ 'const { mastra } = await import("./agent-mastra/src/mastra/index.ts");',
+ 'const result = await mastra.getAgent("openbot").generate("Say hello");',
+ "console.log(JSON.stringify({ text: result.text }));",
+ ].join("\n"),
+ ],
+ {
+ env: {
+ PATH: process.env.PATH ?? "/opt/homebrew/bin:/usr/bin:/bin",
+ MASTRA_TELEMETRY_DISABLED: "true",
+ DO_NOT_TRACK: "1",
+ NODE_ENV: "test",
+ BOT_PROVIDER: choice.provider,
+ BOT_MODEL: choice.model,
+ ANTHROPIC_API_KEY:
+ choice.provider === "anthropic" ? choice.apiKey : "",
+ OPENAI_API_KEY:
+ choice.provider === "anthropic" ? "" : choice.apiKey,
+ ANTHROPIC_BASE_URL:
+ choice.provider === "anthropic" ? choice.base : "",
+ OPENAI_BASE_URL: choice.provider === "anthropic" ? "" : choice.base,
+ },
+ stdout: "pipe",
+ stderr: "pipe",
+ },
+ );
+ const timeout = setTimeout(() => child.kill(), 10_000);
+ try {
+ const [stdout, stderr, exitCode] = await Promise.all([
+ new Response(child.stdout).text(),
+ new Response(child.stderr).text(),
+ child.exited,
+ ]);
+ if (choice.error) {
+ expect(exitCode).not.toBe(0);
+ expect(stderr).toContain(choice.error);
+ expect(seen).toEqual([]);
+ return;
+ }
+ if (exitCode !== 0)
+ throw new Error(
+ `provider probe exited ${exitCode}\n${stdout}\n${stderr}`,
+ );
+ expect(stdout).toContain('"text":"hello"');
+ expect(seen).toEqual([
+ {
+ url: choice.url,
+ key:
+ choice.provider === "anthropic"
+ ? (choice.apiKey ?? null)
+ : `Bearer ${choice.apiKey?.trim() || "no-key-needed"}`,
+ model: choice.model,
+ },
+ ]);
+ } finally {
+ clearTimeout(timeout);
+ child.kill();
+ await provider.stop(true);
+ }
+ }, 15_000);
+ }
+});
diff --git a/agent-mastra/src/mastra/index.ts b/agent-mastra/src/mastra/index.ts
index 7af42f00..83e8e715 100644
--- a/agent-mastra/src/mastra/index.ts
+++ b/agent-mastra/src/mastra/index.ts
@@ -12,13 +12,43 @@
* translation belongs on OpenBot's side, in one place, where every remote Bot is governed the same
* way — not in each harness.
*/
-import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { Mastra } from "@mastra/core/mastra";
import { registerApiRoute } from "@mastra/core/server";
import { listenPort } from "../../../shared/listen-port";
-const model = process.env.BOT_MODEL?.trim() || "gpt-4o-mini";
+async function configuredModel() {
+ const provider = process.env.BOT_PROVIDER?.trim() || "openai";
+ const model =
+ process.env.BOT_MODEL?.trim() ||
+ (provider === "anthropic" ? "claude-sonnet-4-5" : "gpt-4o-mini");
+ const baseVariable =
+ provider === "anthropic" ? "ANTHROPIC_BASE_URL" : "OPENAI_BASE_URL";
+ const baseURL = process.env[baseVariable]?.trim();
+ // Provider modules create default clients at import, which reject Compose's empty overrides.
+ if (!baseURL) delete process.env[baseVariable];
+
+ if (provider === "anthropic") {
+ const { createAnthropic } = await import("@ai-sdk/anthropic");
+ // Other harnesses accept an Anthropic origin; AI SDK expects the /v1 API prefix.
+ const origin = (baseURL || "https://api.anthropic.com").replace(/\/+$/, "");
+ return createAnthropic({
+ baseURL: origin.endsWith("/v1") ? origin : `${origin}/v1`,
+ })(model);
+ }
+ const { createOpenAI } = await import("@ai-sdk/openai");
+ const compatible =
+ Boolean(baseURL) &&
+ baseURL?.replace(/\/+$/, "") !== "https://api.openai.com/v1";
+ const openai = createOpenAI({
+ baseURL: baseURL || "https://api.openai.com/v1",
+ apiKey: compatible
+ ? process.env.OPENAI_API_KEY?.trim() || "no-key-needed"
+ : undefined,
+ });
+ // Compatible endpoints commonly expose Chat Completions; OpenAI keeps its Responses default.
+ return compatible ? openai.chat(model) : openai(model);
+}
const port = listenPort(process.env.PORT, 4213);
if (!port.ok) throw new Error(port.reason);
@@ -79,7 +109,7 @@ const openbot = new Agent({
id: "openbot",
name: "openbot",
instructions: buildOpenBotInstructions,
- model: openai(model),
+ model: await configuredModel(),
});
/** The one header OpenBot's server sends, compared without leaking length through timing. */
diff --git a/prompt.txt b/prompt.txt
index 8ed2d420..846cd436 100644
--- a/prompt.txt
+++ b/prompt.txt
@@ -5,141 +5,189 @@ this repository; if something here disagrees with what you see, trust the reposi
ASK THIS BEFORE YOU DO ANYTHING ELSE
-There are two ways to have OpenBot and they are different jobs. Ask which one they want:
-
- A. They want to run it. One container, one command, nothing cloned and nothing built. The image
- carries the app, the API, Chromium, and its own PostgreSQL if asked.
- B. They want to make it theirs. The clone, run from source, with the Bots, channels and skills as
- configuration they edit. That is the rest of this file.
-
-"I want to try OpenBot" is A. Go to B only if they mean to change something: their own Bots, their
-own channels, their own skills, or the code. Somebody who wanted A and was walked through B has
-installed a toolchain and a container engine to see a screen that one command would have shown them.
-
-A. A LOCAL COPY, FROM THE PUBLISHED IMAGE
+There are three supported paths, and they are different jobs:
+
+ A. They have the OpenBot desktop app, or they want to try/run OpenBot locally without developer
+ tools. Use the desktop setup flow. It installs what it needs and asks for account sign-ins in
+ the window.
+ B. They want a server/container deployment. Use the published Docker image and an env file.
+ C. They want to change OpenBot itself or edit a tenant package from source. Use the clone.
+
+Most people asking to "try OpenBot" want A. Do not send them to Bun, Docker, Node, `npx`, or a
+clone unless they specifically mean B or C.
+
+OpenBot is a template to make your own and white-label, not a hosted product somebody signs up for.
+The desktop app and container run it locally; the clone is for changing the product, tenant package,
+Bots, channels, skills, or branding.
+
+A. DESKTOP APP: LOCAL SETUP WITHOUT DEVELOPER TOOLS
+
+Tell them to open the OpenBot desktop app and follow the screens:
+
+ 1. Set up OpenBot.
+ 2. Keep the default Bot unless they know they want a specific framework.
+ 3. Choose where OpenBot lives. This is the local deployment folder the app prepares.
+ 4. Click Install OpenBot.
+ 5. Connect an AI provider.
+ 6. Sign in to CopilotKit when the app asks.
+ 7. Start OpenBot.
+
+Do not tell desktop users to install Bun or run `bun install`. The desktop installer acquires the
+pinned Bun runtime itself when it is missing. Do not tell them to install Node or run
+`npx copilotkit`: the desktop flow signs in to CopilotKit and provisions the project key itself.
+Do not tell them to clone the repository: the desktop app fetches the released deployment tree and
+uses the release's digest-pinned container images.
+
+The desktop app uses an existing Docker/Podman runtime when one is already usable. If it needs to
+install Podman, the platform decides what that looks like: Windows uses a per-user installer, macOS
+asks for the normal administrator approval, and Linux uses the distribution package manager through
+the desktop authorization prompt. It also places a Compose provider when needed. If setup reports a
+Windows WSL or virtualization blocker, follow the exact sentence in the app; that blocker is the
+source of truth.
+
+The AI provider screen offers OpenAI, Anthropic, and one OpenAI-compatible endpoint row. A compatible
+endpoint means an endpoint or gateway that speaks the OpenAI API shape, such as Ollama, vLLM, Azure
+OpenAI, or a company gateway. Do not describe raw Bedrock as OpenAI-compatible unless there is a
+gateway in front of it that provides that API.
+
+Plan sign-in and API keys are not interchangeable. OpenAI plan sign-in is routed through the
+LangGraph/Codex path. Claude plan sign-in is routed through the Claude Agent SDK path. API keys are
+the broad framework path. Do not claim every framework can use both plan sign-ins.
+
+The desktop wizard's last built-in question proves that the selected Bot can answer. After it opens
+OpenBot, validate the main path with a real browser action and a visible chart, not just a text
+reply. If they are running an already-installed release, check its version first; source-tree fixes
+land in a desktop/container install only after that release is published. Ask a Bot to open a
+simple public site, report something visible on the page, and show the result as a bar or line
+chart. Passing means you saw the browser activity and the chart rendered in the transcript.
+
+B. SERVER OR ONE-CONTAINER DEPLOYMENT
+
+Use this when they are deploying a container, not when they are just trying the desktop app.
docker run -p 3001:3001 --env-file .env \
-e EMBEDDED_POSTGRES=on -v openbot-data:/var/lib/postgresql \
ghcr.io/copilotkit/openbot:latest
One port. The app is on 3001 as well, so open http://localhost:3001, not 3010. `latest` is the most
-recent release; a version tag such as `:v0.0.9` pins one.
+recent release; a version tag such as `:v0.0.13` pins one.
-The `.env` still carries the same values as B: INTELLIGENCE_API_KEY, a model key, a real
-KEY_ENCRYPTION_KEY, and either an identity provider or OPENBOT_SINGLE_USER=true. Start from
-`.env.example` in this repository, or write the handful by hand. `EMBEDDED_POSTGRES=on` runs
-PostgreSQL inside the container, creates the database and the `vector` extension the first time, and
-applies the migrations on every start, so there is nothing to provision and no password to manage.
-Point DATABASE_URL at a database you already run instead, and leave EMBEDDED_POSTGRES off, if you
-would rather. That database needs the `vector` extension enabled; the managed ones support it and
-none of them enable it for you.
+The env file still needs INTELLIGENCE_API_KEY, a model credential, a real KEY_ENCRYPTION_KEY, and
+either an identity provider or OPENBOT_SINGLE_USER=true. Start from `.env.example` in this
+repository, or write the handful by hand. `EMBEDDED_POSTGRES=on` runs PostgreSQL inside the
+container, creates the database and the `vector` extension the first time, and applies migrations on
+every start. With your own DATABASE_URL, leave EMBEDDED_POSTGRES off and enable the `vector`
+extension in that database.
Mount the volume at `/var/lib/postgresql`, the parent, not `/var/lib/postgresql/data`. `initdb` will
-not initialise into a directory that holds anything, and an ext4 volume arrives holding
-`lost+found`, so mounting the data directory itself leaves the cluster uncreated and the container
-serving nothing.
-
-WHAT THIS IMAGE DOES NOT CARRY: `agent-langgraph` and `agent-bot`, the two framework Bots, are
-separate images that `docker-compose.yml` starts. Leave MANAGED_AGENT_AG_UI_URL unset and the
-shipped Risk Analyst coworker is omitted rather than registered against a host that is not there. If
-their `.env` still has the laptop default `http://localhost:4201/ag-ui`, unset it. Somebody who
-wants those Bots wants B.
+not initialise into a directory that already contains `lost+found`.
-docs/deployment.md is the whole of this path, including the managed-database version, the sizing and
-the platform notes.
+The one-container image does not carry the separate framework Bot images that `docker-compose.yml`
+starts. Leave MANAGED_AGENT_AG_UI_URL unset unless a Bot endpoint is actually reachable from this
+container. If their `.env` still has the laptop default `http://localhost:4201/ag-ui`, unset it for
+this path. See docs/deployment.md for platform notes, sizing and migrations.
-B. MAKE IT THEIRS, FROM THE CLONE
+C. CLONE: CHANGE OPENBOT OR A TENANT PACKAGE
-OpenBot is a template to clone and make your own, not a product with a hosted version. It runs on
-the person's machine: their PostgreSQL, their model credential, their Bots.
+Use this only when they want to edit code or configuration in the repository.
-WHAT THEY NEED BEFORE STARTING
-- Bun 1.3.14. The repository pins it (`packageManager` in package.json).
-- Docker, running. `scripts/start.sh` brings up PostgreSQL with `docker compose`.
-- Node, for `npx`, to fetch the CopilotKit credentials below.
+Requirements for the clone path:
+- Bun 1.3.14. The repository pins it in package.json.
+- Docker CLI plus Docker Compose, or a compatible `docker` command/socket. `scripts/start.sh`
+ calls `docker compose` for PostgreSQL and shipped Bots; Podman works only when it is exposed
+ through that Docker-compatible command path.
+- Node/npx, only to fetch the CopilotKit key from the CLI.
- A CopilotKit account. Free is enough.
-- A model credential. `OPENAI_API_KEY` is the default; any OpenAI-compatible base URL works via
- `OPENAI_BASE_URL`.
+- A model credential. The current source tree supports OpenAI or Anthropic for the example built-in
+ Bots and the LangGraph framework Bot. Use the provider-specific key and model variables below.
+ Google is supported by the LangGraph framework Bot, not by the example built-in package model.
-THE ONLY TWO VALUES A PERSON HAS TO SUPPLY
-`.env.example` ships ten empty keys, which makes this look bigger than it is. `scripts/start.sh`
-fills most of them itself: it defaults COMPUTER_TOKEN and WORKER_SHARED_SECRET, and generates
-AGENT_TOOL_TOKEN and writes it back into `.env`. What it cannot invent is:
+The default OpenAI clone setup needs only two values from the person:
1. INTELLIGENCE_API_KEY the `cpk-...` runtime key
2. OPENAI_API_KEY the model credential
-Everything else in `.env.example` is either already correct or generated. Do not talk the person
-through the other eight.
+`scripts/start.sh` fills the local secrets it owns: COMPUTER_TOKEN and WORKER_SHARED_SECRET have dev
+defaults, and MANAGED_AGENT_TOKEN plus AGENT_TOOL_TOKEN are generated and written back into `.env`.
+Everything else in `.env.example` is either already correct, optional, or generated. Do not walk the
+person through every blank line.
-THE STEPS
+The clone commands are:
cp .env.example .env
npx --yes copilotkit@latest login # opens a browser
npx --yes copilotkit@latest project select # prints the cpk-... runtime key
-There is no licence step. `copilotkit license` still exists for a self-hosted Intelligence, and
+There is no licence step. `copilotkit license` still exists for self-hosted Intelligence, and
COPILOTKIT_LICENSE_TOKEN is honoured when set, but managed Intelligence needs only the cpk- key.
-Put the `cpk-...` key in `.env` as INTELLIGENCE_API_KEY. Put their model key in OPENAI_API_KEY.
+Put the `cpk-...` key in `.env` as INTELLIGENCE_API_KEY. For the default OpenAI path, put the
+OpenAI model key in OPENAI_API_KEY. For Anthropic, set all three lines together:
+
+ ANTHROPIC_API_KEY=...
+ BOT_PROVIDER=anthropic
+ BOT_MODEL=claude-sonnet-4-5
+
+That BOT_MODEL line is not optional in the clone path: docker-compose defaults BOT_MODEL to an
+OpenAI model. For a compatible endpoint, set OPENAI_BASE_URL, OPENAI_API_KEY if that endpoint needs
+one, and BOT_MODEL to the model name the endpoint publishes. Do not paste ChatGPT or Claude
+browser-session tokens into `.env`.
+
Then:
bun install
bash scripts/start.sh
-It starts Docker services, applies migrations, starts the API on 3001 and the app on 3010, and
-waits for each to answer its own health route before printing next steps. Open http://localhost:3010.
+It starts Docker services, applies migrations, starts the API on 3001, starts the app on 3010,
+starts the local routine worker, and waits for each service to answer its own health route. Open
+http://localhost:3010.
-`bash scripts/stop.sh` takes down everything it started, the Bots' computers included. The database,
-the files and the browser profiles are volumes, so nothing is deleted either way.
+`bash scripts/stop.sh` takes down the app, worker, API server, Docker services, and each Bot's
+computer. Pass `--keep-computers` only when they deliberately want browser sessions left signed in.
+Nothing is deleted either way: the database, files and browser profiles are volumes.
WHAT YOU MUST NOT DO FOR THEM
-- Do not run `copilotkit login`. It opens a browser and signs in as them. Tell them to run it.
-- Do not put their API keys into any file you did not just tell them about, and do not echo the
+- Do not run `copilotkit login`; it signs in as them. Tell them to run it.
+- Do not put their API keys into any file you did not just tell them about, and do not echo
values back.
-- Do not change INTELLIGENCE_API_URL or INTELLIGENCE_GATEWAY_WS_URL. The defaults in `.env.example`
- point at the managed service and are correct. Self-hosting Intelligence is an enterprise feature
- deployed by Helm chart and is not self-serve.
+- Do not change INTELLIGENCE_API_URL or INTELLIGENCE_GATEWAY_WS_URL unless they run Intelligence themselves.
-FAILURES YOU WILL ACTUALLY SEE, AND WHAT THEY MEAN
+FAILURES YOU WILL ACTUALLY SEE
The server refuses to start rather than running half-configured. The message names the variable.
- "CopilotKit Intelligence is required and is not configured. Missing: ..."
- One of the three Intelligence values is blank. Almost always INTELLIGENCE_API_KEY, from
- skipping `project select`.
+ Almost always INTELLIGENCE_API_KEY was skipped.
- "No identity provider is configured. Set GOOGLE_OAUTH_* ... or set OPENBOT_SINGLE_USER=true"
- `.env.example` already sets OPENBOT_SINGLE_USER=true, so this means it was removed or edited.
- On a laptop, put it back. It admits every request as one administrator, which is why it must be
- off anywhere other people can reach.
+ `.env.example` already sets OPENBOT_SINGLE_USER=true. On a laptop, put it back. On a shared URL,
+ configure real sign-in instead.
- "KEY_ENCRYPTION_KEY must be a base64-encoded 32-byte key"
- Generate one with `openssl rand -base64 32`. The value in `.env.example` is public and fine
- locally; the deployment refuses it in production on purpose.
+ Generate one with `openssl rand -base64 32`. The example value is local-only.
- "Model credential is not configured for <Bot>. Add the package credential or set OPENAI_API_KEY."
- This one appears in a conversation rather than at start-up, because the model is only needed
- when a Bot answers.
+ or the same sentence naming ANTHROPIC_API_KEY. Set the key for the provider the deployment is
+ actually using.
-- A port is taken: start.sh names the port and the process. Re-run with `APP_PORT=<free port>` or
- `SERVER_PORT=<free port>`, or stop the other process.
+- A port is taken: start.sh names the port and process. Re-run with APP_PORT=<free port> or
+ SERVER_PORT=<free port>, or stop the other process.
- `.env is missing`: they skipped `cp .env.example .env`.
HOW TO KNOW IT WORKED
- curl -s localhost:3001/api/capabilities
-Expect JSON with "mode":"intelligence". Then open the app and send a message to General Assistant.
-A reply means the model credential and Intelligence are both good. The app is on 3010 from the
-clone and on 3001 from the image.
+
+For a clone, `curl -s localhost:3001/api/capabilities` should return JSON with
+`"mode":"intelligence"`. Then use the app, not just curl: ask a Bot to open a public site in its
+browser, report something visible on the page, and show a small chart. A plain text/math answer is
+not enough for the main path because it does not prove browser tools or component rendering.
WHEN THEY WANT TO MAKE IT THEIRS
-The Bots, channels and skills are configuration, not code: `examples/fintech` is a worked example,
-pointed at by TENANT_PACKAGE_DIR. Copy it, edit `agents.yaml`, and point TENANT_PACKAGE_DIR at the
-copy. That is the fork, and it is the point of the repository.
-Watch the path: the server runs from `server/`, so TENANT_PACKAGE_DIR is relative to that, which is
-why the default reads `../examples/fintech` rather than `examples/fintech`.
+The Bots, channels and skills are configuration: `examples/fintech` is a worked example pointed at
+by TENANT_PACKAGE_DIR. Copy it, edit `agents.yaml`, and point TENANT_PACKAGE_DIR at the copy.
+The server runs from `server/`, so TENANT_PACKAGE_DIR is relative to that; the default is
+`../examples/fintech`, not `examples/fintech`.
Further reading: README.md, docs/development.md, docs/deployment.md, and docs/configuration.md for
every variable.
diff --git a/scripts/start-restart-guard.test.ts b/scripts/start-restart-guard.test.ts
index 0e2bcb9f..0373ff75 100644
--- a/scripts/start-restart-guard.test.ts
+++ b/scripts/start-restart-guard.test.ts
@@ -22,11 +22,20 @@ async function writeExecutable(path: string, contents: string) {
type Run = {
shell?: "bash" | "sh";
omitFromEnv?: readonly string[];
+ settings?: Record<string, string>;
+ processEnvironment?: Record<string, string>;
+ unavailableHealthPorts?: readonly number[];
};
async function runStartWithStaleServerProbe(
status: 401 | 404,
- { shell = "bash", omitFromEnv = [] }: Run = {},
+ {
+ shell = "bash",
+ omitFromEnv = [],
+ settings = {},
+ processEnvironment = {},
+ unavailableHealthPorts = [],
+ }: Run = {},
) {
const root =
await Bun.$`mktemp -d ${tmpdir()}/openbot-start-guard-XXXXXX`.text();
@@ -34,6 +43,8 @@ async function runStartWithStaleServerProbe(
const fakeBin = join(directory, "bin");
const scripts = join(directory, "scripts");
const logPath = join(directory, "pkill.log");
+ const dockerLogPath = join(directory, "docker.log");
+ const curlLogPath = join(directory, "curl.log");
await mkdir(fakeBin, { recursive: true });
await mkdir(scripts, { recursive: true });
const environment = [
@@ -51,7 +62,15 @@ async function runStartWithStaleServerProbe(
"MANAGED_AGENT_AG_UI_URL=http://localhost:4201/ag-ui",
"OPENBOT_ONE_COMPUTER_EACH=true",
"DATABASE_URL=postgres://openbot:openbot@localhost:5432/openbot",
- ].filter((line) => !omitFromEnv.some((key) => line.startsWith(`${key}=`)));
+ ].filter(
+ (line) =>
+ ![...omitFromEnv, ...Object.keys(settings)].some((key) =>
+ line.startsWith(`${key}=`),
+ ),
+ );
+ environment.push(
+ ...Object.entries(settings).map(([key, value]) => `${key}=${value}`),
+ );
await writeFile(join(directory, ".env"), `${environment.join("\n")}\n`);
await writeFile(
join(scripts, "start.sh"),
@@ -73,6 +92,10 @@ async function runStartWithStaleServerProbe(
join(fakeBin, "curl"),
`#!/usr/bin/env bash
args="$*"
+printf '%s\\n' "$args" >> "$CURL_LOG"
+for port in ${unavailableHealthPorts.join(" ")}; do
+ if [[ "$args" == *"http://localhost:$port/health"* ]]; then exit 7; fi
+done
if [[ "$args" == *"/internal/routines/run"* ]]; then
printf '${status}'
exit 0
@@ -92,6 +115,7 @@ exit 0
join(fakeBin, "docker"),
`#!/usr/bin/env bash
args="$*"
+printf '%s\\n' "$args" >> "$DOCKER_LOG"
if [[ "$args" == *"to_regclass('public.agent_profiles')"* ]]; then echo agent_profiles; fi
if [[ "$args" == *"to_regclass('public.agent_preferences')"* ]]; then echo agent_preferences; fi
exit 0
@@ -118,6 +142,9 @@ exit 0
env: {
PATH: `${fakeBin}:${process.env.PATH ?? ""}`,
PKILL_LOG: logPath,
+ DOCKER_LOG: dockerLogPath,
+ CURL_LOG: curlLogPath,
+ ...processEnvironment,
},
stdout: "pipe",
stderr: "pipe",
@@ -130,7 +157,13 @@ exit 0
const pkillLog = await Bun.file(logPath)
.text()
.catch(() => "");
- return { exitCode, stdout, stderr, pkillLog };
+ const dockerLog = await Bun.file(dockerLogPath)
+ .text()
+ .catch(() => "");
+ const curlLog = await Bun.file(curlLogPath)
+ .text()
+ .catch(() => "");
+ return { exitCode, stdout, stderr, pkillLog, dockerLog, curlLog };
} finally {
await rm(directory, { recursive: true, force: true });
}
@@ -225,3 +258,65 @@ describe("start.sh settings with no line in .env", () => {
expect(result.stdout).toContain("http://localhost:3010");
});
});
+
+describe("start.sh selected provider services", () => {
+ test.each([".env", "process environment"])(
+ "an Anthropic-only %s selection starts the managed Bot without the OpenAI-only sample",
+ async (source) => {
+ const selected = {
+ BOT_PROVIDER: "anthropic",
+ ANTHROPIC_API_KEY: "synthetic-anthropic-key",
+ };
+ const result = await runStartWithStaleServerProbe(401, {
+ settings: source === ".env" ? selected : { BOT_PROVIDER: "openai" },
+ processEnvironment: source === "process environment" ? selected : {},
+ unavailableHealthPorts: [4200],
+ });
+ expect({ exitCode: result.exitCode, stderr: result.stderr }).toEqual({
+ exitCode: 0,
+ stderr: "",
+ });
+ expect(result.dockerLog).toContain(
+ "compose up -d --build postgres supervisor agent-computer agent-langgraph",
+ );
+ expect(result.dockerLog).not.toContain("agent-bot");
+ expect(result.dockerLog).toContain("compose run --rm --build migrate");
+ expect(result.curlLog).toContain("http://localhost:4100/health");
+ expect(result.curlLog).toContain("http://localhost:4201/health");
+ expect(result.curlLog).not.toContain("http://localhost:4200/health");
+ expect(result.stdout).toContain("agent-bot: skipped for Anthropic");
+ expect(result.stdout).not.toContain("agent-bot ready");
+ expect(result.stdout).toContain("agent-langgraph ready");
+ expect(result.stdout).toContain(
+ "managed coworker endpoint: http://localhost:4201/ag-ui",
+ );
+ expect(result.stdout).toContain("Ready. http://localhost:3010");
+ },
+ );
+
+ test("the default OpenAI startup still starts and checks the legacy sample", async () => {
+ const result = await runStartWithStaleServerProbe(401);
+ expect(result.exitCode).toBe(0);
+ expect(result.dockerLog).toContain(
+ "compose up -d --build postgres supervisor agent-computer agent-bot agent-langgraph",
+ );
+ expect(result.curlLog).toContain("http://localhost:4200/health");
+ expect(result.stdout).toContain("agent-bot ready");
+ expect(result.stdout).not.toContain("agent-bot: skipped");
+ });
+
+ test("Anthropic startup still fails if its managed LangGraph Bot is unavailable", async () => {
+ const result = await runStartWithStaleServerProbe(401, {
+ settings: {
+ BOT_PROVIDER: "anthropic",
+ ANTHROPIC_API_KEY: "synthetic-anthropic-key",
+ },
+ unavailableHealthPorts: [4200, 4201],
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.curlLog).not.toContain("http://localhost:4200/health");
+ expect(result.curlLog).toContain("http://localhost:4201/health");
+ expect(result.stdout).toContain("agent-langgraph never became ready");
+ expect(result.stdout).not.toContain("Ready. http://localhost:3010");
+ });
+});
diff --git a/scripts/start.sh b/scripts/start.sh
index b01f676b..58c1c6b9 100755
--- a/scripts/start.sh
+++ b/scripts/start.sh
@@ -47,6 +47,7 @@ SERVER_PORT="$(setting SERVER_PORT 3001)"
COMPUTER_PORT="$(setting COMPUTER_PORT 4100)"
BOT_PORT="$(setting BOT_PORT 4200)"
LANGGRAPH_PORT="$(setting LANGGRAPH_PORT 4201)"
+BOT_PROVIDER="$(setting BOT_PROVIDER openai | tr '[:upper:]' '[:lower:]' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
SUPERVISOR_PORT="$(setting SUPERVISOR_PORT 4500)"
ONE_COMPUTER_EACH="${OPENBOT_ONE_COMPUTER_EACH:-true}"
export APP_PORT SERVER_PORT
@@ -222,7 +223,7 @@ if [ "$ONE_COMPUTER_EACH" = "true" ]; then
SERVICES+=(supervisor)
fi
#
-# Every Bot service, every run, whether or not it is already answering.
+# Every selected Bot service, every run, whether or not it is already answering.
#
# This used to skip one that answered its health route, which sounds like an optimisation and is
# actually a correctness bug: answering says the process is alive, not that its environment still
@@ -237,9 +238,15 @@ fi
# `docker compose up -d` is declarative and does nothing for a service whose configuration has not
# changed, so naming them all costs a comparison and buys the guarantee that what is running is what
# this run configured.
-for svc in agent-computer agent-bot agent-langgraph; do
- SERVICES+=("$svc")
-done
+SERVICES+=(agent-computer)
+# The managed coworker uses LangGraph. The separate legacy sample only accepts OpenAI keys,
+# so requiring it would prevent an Anthropic-only deployment from reaching its managed Bot.
+if [ "$BOT_PROVIDER" = "anthropic" ]; then
+ info " agent-bot: skipped for Anthropic (OpenAI-only sample)"
+else
+ SERVICES+=(agent-bot)
+fi
+SERVICES+=(agent-langgraph)
export SUPERVISOR_TOKEN COMPUTER_TOKEN WORKER_SHARED_SECRET
export COMPUTER_PORT BOT_PORT LANGGRAPH_PORT SUPERVISOR_PORT
@@ -250,7 +257,9 @@ if ! docker compose run --rm --build migrate >"$LOGS/migrate.log" 2>&1; then
exit 1
fi
wait_for "http://localhost:$COMPUTER_PORT/health" "agent-computer"
-wait_for "http://localhost:$BOT_PORT/health" "agent-bot"
+if [ "$BOT_PROVIDER" != "anthropic" ]; then
+ wait_for "http://localhost:$BOT_PORT/health" "agent-bot"
+fi
wait_for "http://localhost:$LANGGRAPH_PORT/health" "agent-langgraph"
for table in agent_profiles agent_preferences; do
diff --git a/server/src/copilot.ts b/server/src/copilot.ts
index 68639776..6c6043f1 100644
--- a/server/src/copilot.ts
+++ b/server/src/copilot.ts
@@ -150,25 +150,56 @@ export function standingRoleMessage(
}
export type RuntimeModel = {
- provider: "openai";
+ provider: "openai" | "anthropic";
defaultModel: string;
};
+/** Optional desktop environment values may be present but blank; SDKs treat them as URLs. */
+export function normalizeModelBaseUrls(
+ environment: Record<string, string | undefined> = process.env,
+): void {
+ for (const key of ["OPENAI_BASE_URL", "ANTHROPIC_BASE_URL"]) {
+ const value = environment[key]?.trim();
+ if (!value) {
+ delete environment[key];
+ } else if (key === "ANTHROPIC_BASE_URL") {
+ // The SDK appends only /messages; match the selector's versioned-base contract.
+ const base = value.replace(/\/+$/, "");
+ environment[key] = /\/v\d+$/.test(base) ? base : `${base}/v1`;
+ } else {
+ environment[key] = value;
+ }
+ }
+}
+
export function runtimeModelForEnvironment(
packageModel: RuntimeModel,
environment: Record<string, string | undefined> = process.env,
): RuntimeModel {
const selectedModel = environment.BOT_MODEL?.trim();
const selectedProvider = environment.BOT_PROVIDER?.trim().toLowerCase();
- const compatibleEndpoint =
- (!selectedProvider || selectedProvider === "openai") &&
- !!environment.OPENAI_BASE_URL?.trim();
+ // The desktop writes an empty provider when switching back to OpenAI. An absent
+ // choice leaves the tenant package authoritative.
+ const provider =
+ selectedProvider === "anthropic"
+ ? "anthropic"
+ : selectedProvider === "openai" || selectedProvider === ""
+ ? "openai"
+ : packageModel.provider;
+ const defaultModel =
+ provider === packageModel.provider
+ ? packageModel.defaultModel
+ : provider === "anthropic"
+ ? "claude-sonnet-4-5"
+ : "gpt-5.6-terra";
+ const selectedModelApplies =
+ provider === "anthropic" ||
+ ((!selectedProvider || selectedProvider === "openai") &&
+ !!environment.OPENAI_BASE_URL?.trim());
return {
- provider: packageModel.provider,
+ provider,
defaultModel:
- compatibleEndpoint && selectedModel
- ? selectedModel
- : packageModel.defaultModel,
+ selectedModelApplies && selectedModel ? selectedModel : defaultModel,
};
}
@@ -318,7 +349,7 @@ export function builtInAgentConfiguration(
// biome-ignore lint/correctness/useYield: this agent must fail when iteration starts.
factory: async function* () {
throw new Error(
- `Model credential is not configured for ${agent.name}. Add the package credential or set OPENAI_API_KEY.`,
+ `Model credential is not configured for ${agent.name}. Add the package credential or set ${model.provider === "anthropic" ? "ANTHROPIC_API_KEY" : "OPENAI_API_KEY"}.`,
);
},
};
diff --git a/server/src/credentials.ts b/server/src/credentials.ts
index f96aa394..f467fbed 100644
--- a/server/src/credentials.ts
+++ b/server/src/credentials.ts
@@ -130,7 +130,7 @@ export type CredentialStatusReader = {
export type ModelCredentialSecretReader = {
readModelSecret: (input: {
- provider: "openai";
+ provider: "openai" | "anthropic";
keyId: string;
}) => Promise<{ encryptedValue: string } | null>;
};
@@ -241,7 +241,7 @@ export async function decryptCredentialForUse(
export async function resolveModelApiKey(input: {
encryptionKey: string;
reader: ModelCredentialSecretReader;
- provider: "openai";
+ provider: "openai" | "anthropic";
keyId: string;
environment: Record<string, string | undefined>;
}) {
@@ -253,7 +253,10 @@ export async function resolveModelApiKey(input: {
return decryptSecret(input.encryptionKey, stored.encryptedValue);
}
- const environmentKey = input.environment.OPENAI_API_KEY?.trim();
+ const environmentKey =
+ input.environment[
+ input.provider === "anthropic" ? "ANTHROPIC_API_KEY" : "OPENAI_API_KEY"
+ ]?.trim();
return environmentKey || null;
}
diff --git a/server/src/index.ts b/server/src/index.ts
index e603fee3..cb25a193 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -70,6 +70,7 @@ import {
type IdentifyActor,
type IdentifyUser,
mountCopilotRuntime,
+ normalizeModelBaseUrls,
resolveRuntimeAgents,
runtimeModelForEnvironment,
type ToolSelection,
@@ -506,6 +507,7 @@ const stallGuard = createStallGuard({
auditStore: bootAuditStore,
});
+normalizeModelBaseUrls();
const runtimeModel = runtimeModelForEnvironment(tenantPackage.model);
const intentRouter = createIntentRouter({
@@ -515,7 +517,7 @@ const intentRouter = createIntentRouter({
resolveModelApiKey({
encryptionKey: config.keyEncryptionKey,
reader: credentialStore,
- provider: tenantPackage.model.provider,
+ provider: runtimeModel.provider,
keyId: tenantPackage.model.credentialSecretRef,
environment: process.env,
}),
@@ -534,7 +536,7 @@ const chooseSkills = createModelCompleter({
resolveModelApiKey({
encryptionKey: config.keyEncryptionKey,
reader: credentialStore,
- provider: tenantPackage.model.provider,
+ provider: runtimeModel.provider,
keyId: tenantPackage.model.credentialSecretRef,
environment: process.env,
}),
@@ -556,7 +558,7 @@ const resolveRuntimeModelApiKey = () =>
resolveModelApiKey({
encryptionKey: config.keyEncryptionKey,
reader: credentialStore,
- provider: tenantPackage.model.provider,
+ provider: runtimeModel.provider,
keyId: tenantPackage.model.credentialSecretRef,
environment: process.env,
});
diff --git a/server/src/routing/model.ts b/server/src/routing/model.ts
index 25a7e94c..cbece0da 100644
--- a/server/src/routing/model.ts
+++ b/server/src/routing/model.ts
@@ -17,41 +17,59 @@ export function createModelCompleter(deps: {
const key = await deps.resolveApiKey();
signal?.throwIfAborted();
if (!key) throw new Error("no model key");
- const response = await fetch(chatCompletionsUrl(process.env), {
- method: "POST",
- headers: {
- "content-type": "application/json",
- authorization: `Bearer ${key}`,
+ const anthropic = deps.model.provider === "anthropic";
+ const response = await fetch(
+ anthropic
+ ? anthropicMessagesUrl(process.env)
+ : chatCompletionsUrl(process.env),
+ {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ ...(anthropic
+ ? { "x-api-key": key, "anthropic-version": "2023-06-01" }
+ : { authorization: `Bearer ${key}` }),
+ },
+ body: JSON.stringify({
+ model: deps.model.defaultModel,
+ /*
+ * No temperature.
+ *
+ * It was zero, for a router that answers the same way twice. Reasoning models refuse the
+ * setting outright — "Unsupported value: 'temperature' does not support 0 with this model.
+ * Only the default (1) value is supported" — and this call treats a throw as "not sure", so
+ * every routing decision quietly became the default coworker and the roster was never
+ * consulted. A question naming Google Drive went to a Bot holding no Drive tools, which is
+ * the exact failure the roster exists to prevent, and nothing said so.
+ *
+ * Omitted rather than set per model, because a list of which models accept it is a list that
+ * goes stale. `response_format` and a prompt that asks for one object keep the answer tight,
+ * and the confidence floor still sends an unsure match to the default.
+ */
+ ...(anthropic
+ ? { max_tokens: 1024 }
+ : { response_format: { type: "json_object" } }),
+ messages: [{ role: "user", content: prompt }],
+ }),
+ signal: signal
+ ? AbortSignal.any([signal, AbortSignal.timeout(10_000)])
+ : AbortSignal.timeout(10_000),
},
- body: JSON.stringify({
- model: deps.model.defaultModel,
- /*
- * No temperature.
- *
- * It was zero, for a router that answers the same way twice. Reasoning models refuse the
- * setting outright — "Unsupported value: 'temperature' does not support 0 with this model.
- * Only the default (1) value is supported" — and this call treats a throw as "not sure", so
- * every routing decision quietly became the default coworker and the roster was never
- * consulted. A question naming Google Drive went to a Bot holding no Drive tools, which is
- * the exact failure the roster exists to prevent, and nothing said so.
- *
- * Omitted rather than set per model, because a list of which models accept it is a list that
- * goes stale. `response_format` and a prompt that asks for one object keep the answer tight,
- * and the confidence floor still sends an unsure match to the default.
- */
- response_format: { type: "json_object" },
- messages: [{ role: "user", content: prompt }],
- }),
- signal: signal
- ? AbortSignal.any([signal, AbortSignal.timeout(10_000)])
- : AbortSignal.timeout(10_000),
- });
+ );
if (!response.ok)
throw new Error(`router model answered ${response.status}`);
const body = (await response.json()) as {
choices?: { message?: { content?: unknown } }[];
+ content?: { type?: string; text?: unknown }[];
};
- const content = body.choices?.[0]?.message?.content;
+ const content = anthropic
+ ? body.content
+ ?.filter(
+ (block) => block.type === "text" && typeof block.text === "string",
+ )
+ .map((block) => block.text)
+ .join("") || undefined
+ : body.choices?.[0]?.message?.content;
if (typeof content !== "string")
throw new Error("router model returned no text");
return content;
@@ -86,3 +104,13 @@ export function chatCompletionsUrl(
? `${base}/chat/completions`
: `${base}/v1/chat/completions`;
}
+
+/** Native Anthropic endpoint, accepting the same versioned base URL as the runtime. */
+export function anthropicMessagesUrl(
+ environment: Record<string, string | undefined>,
+): string {
+ const base = (
+ environment.ANTHROPIC_BASE_URL?.trim() || "https://api.anthropic.com"
+ ).replace(/\/+$/, "");
+ return /\/v\d+$/.test(base) ? `${base}/messages` : `${base}/v1/messages`;
+}
diff --git a/server/src/tenant-package.ts b/server/src/tenant-package.ts
index e6cf9765..3a95b536 100644
--- a/server/src/tenant-package.ts
+++ b/server/src/tenant-package.ts
@@ -197,7 +197,7 @@ export type TenantPackage = {
omittedAgentIds: string[];
channels: TenantChannel[];
model: {
- provider: "openai";
+ provider: "openai" | "anthropic";
credentialSecretRef: string;
defaultModel: string;
};
@@ -482,8 +482,8 @@ export function validateTenantPackage(files: PackageFiles): TenantPackage {
},
);
const model = asRecord(modelYaml.model, "model");
- if (model.provider !== "openai") {
- throw new Error("model.provider must be openai");
+ if (model.provider !== "openai" && model.provider !== "anthropic") {
+ throw new Error("model.provider must be openai or anthropic");
}
const sources = asList(knowledgeYaml.sources, "knowledge.yaml sources").map(
(value) => {
@@ -511,7 +511,7 @@ export function validateTenantPackage(files: PackageFiles): TenantPackage {
omittedAgentIds: [...omittedAgentIds],
channels,
model: {
- provider: "openai",
+ provider: model.provider,
credentialSecretRef: requiredString(
model.credential_secret_ref,
"model.credential_secret_ref",
diff --git a/server/tests/copilot.test.ts b/server/tests/copilot.test.ts
index 8cf7f4fb..561094cf 100644
--- a/server/tests/copilot.test.ts
+++ b/server/tests/copilot.test.ts
@@ -159,7 +159,6 @@ describe("deployment model selection", () => {
{},
{ OPENAI_BASE_URL: "http://127.0.0.1:11434/v1", BOT_MODEL: " " },
{ BOT_MODEL: "selected-local-model" },
- { BOT_PROVIDER: "anthropic", BOT_MODEL: "claude-sonnet-4-5" },
])(
"package default remains the model without a compatible endpoint selection: %j",
async (environment) => {
diff --git a/server/tests/model-providers.test.ts b/server/tests/model-providers.test.ts
new file mode 100644
index 00000000..01e066de
--- /dev/null
+++ b/server/tests/model-providers.test.ts
@@ -0,0 +1,522 @@
+import { afterEach, describe, expect, spyOn, test } from "bun:test";
+import { LLMock } from "@copilotkit/aimock";
+import { z } from "zod";
+import {
+ normalizeModelBaseUrls,
+ resolveRuntimeAgents,
+ runtimeModelForEnvironment,
+} from "../src/copilot";
+import { encryptSecret, resolveModelApiKey } from "../src/credentials";
+import {
+ anthropicMessagesUrl,
+ createModelCompleter,
+} from "../src/routing/model";
+import { validateTenantPackage } from "../src/tenant-package";
+
+const encryptionKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
+const originalEnvironment = {
+ OPENAI_BASE_URL: process.env.OPENAI_BASE_URL,
+ ANTHROPIC_BASE_URL: process.env.ANTHROPIC_BASE_URL,
+};
+afterEach(() => {
+ for (const [key, value] of Object.entries(originalEnvironment)) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+});
+
+const packageModel = {
+ provider: "openai" as const,
+ defaultModel: "gpt-5.6-terra",
+};
+const anthropicModel = {
+ provider: "anthropic" as const,
+ defaultModel: "claude-sonnet-4-5",
+};
+
+describe("deployment API-key provider selection", () => {
+ test("normalizes optional SDK base URLs and native selector URLs", () => {
+ const environment = { OPENAI_BASE_URL: "", ANTHROPIC_BASE_URL: " " };
+ normalizeModelBaseUrls(environment);
+ expect(environment).toEqual({});
+ const custom = {
+ OPENAI_BASE_URL: " https://openai.example/v1 ",
+ ANTHROPIC_BASE_URL: " https://anthropic.example/v1 ",
+ };
+ normalizeModelBaseUrls(custom);
+ expect(custom).toEqual({
+ OPENAI_BASE_URL: "https://openai.example/v1",
+ ANTHROPIC_BASE_URL: "https://anthropic.example/v1",
+ });
+ for (const [base, expected] of [
+ ["https://gateway.example", "https://gateway.example/v1"],
+ ["https://gateway.example/proxy", "https://gateway.example/proxy/v1"],
+ [" https://gateway.example/proxy/ ", "https://gateway.example/proxy/v1"],
+ ["https://gateway.example/proxy/v1/", "https://gateway.example/proxy/v1"],
+ ["https://gateway.example/v2/", "https://gateway.example/v2"],
+ ]) {
+ const environment = { ANTHROPIC_BASE_URL: base };
+ normalizeModelBaseUrls(environment);
+ expect(environment.ANTHROPIC_BASE_URL).toBe(expected);
+ expect(anthropicMessagesUrl(environment)).toBe(`${expected}/messages`);
+ normalizeModelBaseUrls(environment);
+ expect(environment.ANTHROPIC_BASE_URL).toBe(expected);
+ }
+ for (const value of [undefined, "", " "]) {
+ expect(anthropicMessagesUrl({ ANTHROPIC_BASE_URL: value })).toBe(
+ "https://api.anthropic.com/v1/messages",
+ );
+ }
+ for (const value of [
+ "https://gateway.example",
+ "https://gateway.example/v1",
+ " https://gateway.example/v1/ ",
+ ]) {
+ expect(anthropicMessagesUrl({ ANTHROPIC_BASE_URL: value })).toBe(
+ "https://gateway.example/v1/messages",
+ );
+ }
+ });
+
+ test("accepts an Anthropic tenant package without changing its credential reference", () => {
+ const result = validateTenantPackage({
+ brand: "tenant: { id: provider-test, product_name: Provider Test }",
+ agents: "agents: []",
+ channels: "channels: []",
+ model:
+ "model: { provider: anthropic, credential_secret_ref: primary-model, default_model: claude-sonnet-4-5 }",
+ knowledge: "sources: []",
+ themeCss: "",
+ });
+ expect(result.model).toEqual({
+ ...anthropicModel,
+ credentialSecretRef: "primary-model",
+ });
+ });
+
+ test("the desktop Anthropic choice overrides the OpenAI package model", () => {
+ expect(
+ runtimeModelForEnvironment(packageModel, {
+ BOT_PROVIDER: " anthropic ",
+ BOT_MODEL: " claude-sonnet-4-5 ",
+ OPENAI_BASE_URL: "",
+ }),
+ ).toEqual(anthropicModel);
+ expect(
+ runtimeModelForEnvironment(packageModel, {
+ BOT_PROVIDER: "anthropic",
+ BOT_MODEL: "",
+ }),
+ ).toEqual(anthropicModel);
+ });
+
+ test("an unset choice preserves the package while an empty desktop provider selects OpenAI", () => {
+ expect(runtimeModelForEnvironment(anthropicModel, {})).toEqual(
+ anthropicModel,
+ );
+ expect(
+ runtimeModelForEnvironment(anthropicModel, {
+ BOT_PROVIDER: "",
+ BOT_MODEL: "",
+ }),
+ ).toEqual(packageModel);
+ expect(
+ runtimeModelForEnvironment(packageModel, {
+ BOT_MODEL: "unselected-model",
+ }),
+ ).toEqual(packageModel);
+ expect(
+ runtimeModelForEnvironment(packageModel, {
+ BOT_PROVIDER: "openai",
+ BOT_MODEL: "local-model",
+ OPENAI_BASE_URL: "http://localhost:11434/v1",
+ }),
+ ).toEqual({ provider: "openai", defaultModel: "local-model" });
+ });
+
+ test("credentials stay provider-scoped and a rotated stored key is read on the next call", async () => {
+ let encryptedValue = await encryptSecret(
+ encryptionKey,
+ "stored-anthropic-one",
+ );
+ const asked: unknown[] = [];
+ const resolve = () =>
+ resolveModelApiKey({
+ encryptionKey,
+ provider: "anthropic",
+ keyId: "primary-model",
+ reader: {
+ readModelSecret: async (input) => {
+ asked.push(input);
+ return { encryptedValue };
+ },
+ },
+ environment: {
+ OPENAI_API_KEY: "wrong-provider",
+ ANTHROPIC_API_KEY: "environment-anthropic",
+ },
+ });
+ expect(await resolve()).toBe("stored-anthropic-one");
+ encryptedValue = await encryptSecret(encryptionKey, "stored-anthropic-two");
+ expect(await resolve()).toBe("stored-anthropic-two");
+ expect(asked).toEqual(
+ Array(2).fill({ provider: "anthropic", keyId: "primary-model" }),
+ );
+ const noStored = {
+ encryptionKey,
+ provider: "anthropic" as const,
+ keyId: "primary-model",
+ reader: { readModelSecret: async () => null },
+ };
+ expect(
+ await resolveModelApiKey({
+ ...noStored,
+ environment: {
+ OPENAI_API_KEY: "wrong-provider",
+ ANTHROPIC_API_KEY: " synthetic-anthropic ",
+ },
+ }),
+ ).toBe("synthetic-anthropic");
+ expect(
+ await resolveModelApiKey({
+ ...noStored,
+ environment: { OPENAI_API_KEY: "wrong-provider" },
+ }),
+ ).toBeNull();
+ await expect(
+ resolveModelApiKey({
+ ...noStored,
+ reader: {
+ readModelSecret: async () => ({ encryptedValue: "corrupt" }),
+ },
+ environment: { ANTHROPIC_API_KEY: "must-not-fallback" },
+ }),
+ ).rejects.toThrow("Credential envelope is invalid");
+ });
+});
+
+test.each([
+ { suffix: "", prefix: "" },
+ { suffix: "/proxy", prefix: "/proxy" },
+ { suffix: "/v1", prefix: "" },
+ { suffix: "/proxy/v1/", prefix: "/proxy" },
+])(
+ "Anthropic built-ins execute tools with a rotated key and base suffix '$suffix'",
+ async ({ suffix, prefix }) => {
+ const mock = new LLMock();
+ const executed: unknown[] = [];
+ const keys: (string | null)[] = [];
+ const paths: string[] = [];
+ let proxy: ReturnType<typeof Bun.serve> | undefined;
+ try {
+ const base = await mock.start();
+ proxy = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ async fetch(request) {
+ keys.push(request.headers.get("x-api-key"));
+ const path = new URL(request.url).pathname;
+ paths.push(path);
+ return fetch(`${base}${path.slice(prefix.length)}`, {
+ method: "POST",
+ headers: request.headers,
+ body: await request.text(),
+ });
+ },
+ });
+ process.env.ANTHROPIC_BASE_URL = `${proxy.url.origin}${suffix}`;
+ normalizeModelBaseUrls();
+ process.env.OPENAI_BASE_URL = base;
+ mock.on({ hasToolResult: true }, { content: "The balance is 42." });
+ mock.onMessage(/balance/, {
+ toolCalls: [
+ {
+ id: "balance-call",
+ name: "read_balance",
+ arguments: { account: "demo" },
+ },
+ ],
+ });
+ const model = runtimeModelForEnvironment(packageModel, {
+ BOT_PROVIDER: "anthropic",
+ BOT_MODEL: "claude-sonnet-4-5",
+ });
+ let encryptedValue = await encryptSecret(
+ encryptionKey,
+ "stored-anthropic-one",
+ );
+ const resolve = () =>
+ resolveModelApiKey({
+ encryptionKey,
+ provider: model.provider,
+ keyId: "primary-model",
+ environment: {},
+ reader: { readModelSecret: async () => ({ encryptedValue }) },
+ });
+ for (const key of ["stored-anthropic-one", "stored-anthropic-two"]) {
+ encryptedValue = await encryptSecret(encryptionKey, key);
+ const agents = await resolveRuntimeAgents(
+ async () => [
+ {
+ id: "general-assistant",
+ name: "General Assistant",
+ type: "built_in",
+ systemPrompt: "Answer using the granted balance tool.",
+ },
+ ],
+ model,
+ resolve,
+ undefined,
+ async () => [
+ {
+ name: "read_balance",
+ ref: "bank/read_balance",
+ description: "Read the balance.",
+ parameters: z.object({ account: z.string() }),
+ execute: async (args) => {
+ executed.push(args);
+ return "42";
+ },
+ },
+ ],
+ );
+ const agent = agents["general-assistant"]?.clone();
+ if (!agent) throw new Error("The built-in agent was not constructed");
+ agent.addMessage({
+ id: "request",
+ role: "user",
+ content: "Read my balance.",
+ });
+ await agent.runAgent();
+ expect(agent.messages.at(-1)).toMatchObject({
+ role: "assistant",
+ content: "The balance is 42.",
+ });
+ }
+ expect(executed).toEqual([{ account: "demo" }, { account: "demo" }]);
+ const requests = mock.getRequests();
+ expect(requests).toHaveLength(4);
+ expect(requests.map((entry) => entry.path)).toEqual(
+ Array(4).fill("/v1/messages"),
+ );
+ expect(paths).toEqual(Array(4).fill(`${prefix}/v1/messages`));
+ expect(keys).toEqual([
+ "stored-anthropic-one",
+ "stored-anthropic-one",
+ "stored-anthropic-two",
+ "stored-anthropic-two",
+ ]);
+ expect(
+ requests.every((entry) => entry.body?.model === "claude-sonnet-4-5"),
+ ).toBe(true);
+ expect(requests[1]?.body?.messages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ role: "tool", content: "42" }),
+ ]),
+ );
+ } finally {
+ await proxy?.stop(true);
+ await mock.stop();
+ }
+ },
+);
+
+test.each([
+ {
+ provider: "anthropic" as const,
+ defaultModel: "claude-sonnet-4-5",
+ expectedUrl: "https://api.anthropic.com/v1/messages",
+ },
+ {
+ provider: "openai" as const,
+ defaultModel: "gpt-5.6-terra",
+ expectedUrl: "https://api.openai.com/v1/responses",
+ },
+])(
+ "blank base URLs reach the official $provider endpoint through the real SDK",
+ async ({ expectedUrl, ...model }) => {
+ const mock = new LLMock();
+ const originalFetch = globalThis.fetch;
+ const urls: string[] = [];
+ const keys: (string | null)[] = [];
+ let interception:
+ | ReturnType<typeof spyOn<typeof globalThis, "fetch">>
+ | undefined;
+ try {
+ const base = await mock.start();
+ mock.onMessage("Hello", { content: "Hello back." });
+ process.env.ANTHROPIC_BASE_URL = "";
+ process.env.OPENAI_BASE_URL = "";
+ normalizeModelBaseUrls();
+ // Keep the SDK's request intact while redirecting only its transport to local fake HTTP.
+ interception = spyOn(globalThis, "fetch").mockImplementation(
+ (input, init) => {
+ const request = new Request(input, init);
+ urls.push(request.url);
+ keys.push(
+ request.headers.get(
+ model.provider === "anthropic" ? "x-api-key" : "authorization",
+ ),
+ );
+ return originalFetch(`${base}${new URL(request.url).pathname}`, {
+ method: request.method,
+ headers: request.headers,
+ body: request.body,
+ });
+ },
+ );
+ const agents = await resolveRuntimeAgents(
+ async () => [
+ {
+ id: "general-assistant",
+ name: "General Assistant",
+ type: "built_in",
+ systemPrompt: "Answer simply.",
+ },
+ ],
+ model,
+ async () => "synthetic-key",
+ );
+ const agent = agents["general-assistant"]?.clone();
+ if (!agent) throw new Error("The built-in agent was not constructed");
+ agent.addMessage({ id: "request", role: "user", content: "Hello" });
+ await agent.runAgent();
+ expect(agent.messages.at(-1)).toMatchObject({
+ role: "assistant",
+ content: "Hello back.",
+ });
+ expect(urls).toEqual([expectedUrl]);
+ expect(keys).toEqual([
+ model.provider === "anthropic"
+ ? "synthetic-key"
+ : "Bearer synthetic-key",
+ ]);
+ } finally {
+ interception?.mockRestore();
+ await mock.stop();
+ }
+ },
+);
+
+test("the production selector speaks native Anthropic and rereads its key", async () => {
+ const seen: {
+ path: string;
+ key: string | null;
+ authorization: string | null;
+ version: string | null;
+ body: Record<string, unknown>;
+ }[] = [];
+ const server = Bun.serve({
+ port: 0,
+ hostname: "127.0.0.1",
+ async fetch(request) {
+ seen.push({
+ path: new URL(request.url).pathname,
+ key: request.headers.get("x-api-key"),
+ authorization: request.headers.get("authorization"),
+ version: request.headers.get("anthropic-version"),
+ body: await request.json(),
+ });
+ return Response.json({
+ content: [
+ { type: "thinking", thinking: "private" },
+ { type: "text", text: '{"skills":' },
+ { type: "text", text: '["bank"]}' },
+ ],
+ });
+ },
+ });
+ try {
+ process.env.ANTHROPIC_BASE_URL = `${server.url.origin}/v1/`;
+ process.env.OPENAI_BASE_URL = `${server.url.origin}/v1`;
+ let key = "synthetic-one";
+ const complete = createModelCompleter({
+ model: anthropicModel,
+ resolveApiKey: async () => key,
+ });
+ expect(await complete("Choose bank tools; return JSON.")).toBe(
+ '{"skills":["bank"]}',
+ );
+ key = "synthetic-two";
+ expect(await complete("Choose bank tools; return JSON.")).toBe(
+ '{"skills":["bank"]}',
+ );
+ expect(seen.map((entry) => entry.key)).toEqual([
+ "synthetic-one",
+ "synthetic-two",
+ ]);
+ for (const entry of seen) {
+ expect(entry.path).toBe("/v1/messages");
+ expect(entry.authorization).toBeNull();
+ expect(entry.version).toBe("2023-06-01");
+ expect(entry.body).toMatchObject({
+ model: "claude-sonnet-4-5",
+ max_tokens: expect.any(Number),
+ messages: [
+ { role: "user", content: "Choose bank tools; return JSON." },
+ ],
+ });
+ expect(entry.body).not.toHaveProperty("response_format");
+ expect(entry.body).not.toHaveProperty("temperature");
+ }
+ } finally {
+ await server.stop(true);
+ }
+});
+
+async function bounded<T>(promise: Promise<T>, boundary: string): Promise<T> {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ try {
+ return await Promise.race([
+ promise,
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(
+ () => reject(new Error(`Fixture ${boundary} timed out`)),
+ 1500,
+ );
+ }),
+ ]);
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+test("cancelling an Anthropic selector closes its in-flight HTTP request", async () => {
+ const entered = Promise.withResolvers<void>();
+ const closed = Promise.withResolvers<void>();
+ const release = Promise.withResolvers<void>();
+ const server = Bun.serve({
+ port: 0,
+ hostname: "127.0.0.1",
+ async fetch(request) {
+ await request.text();
+ request.signal.addEventListener("abort", () => closed.resolve(), {
+ once: true,
+ });
+ entered.resolve();
+ await release.promise;
+ return Response.json({ content: [{ type: "text", text: "late reply" }] });
+ },
+ });
+ try {
+ process.env.ANTHROPIC_BASE_URL = server.url.origin;
+ process.env.OPENAI_BASE_URL = server.url.origin;
+ const controller = new AbortController();
+ const complete = createModelCompleter({
+ model: anthropicModel,
+ resolveApiKey: async () => "synthetic-key",
+ });
+ const run = complete("Choose tools.", controller.signal);
+ const outcome = run.then(
+ () => ({ rejected: false }),
+ () => ({ rejected: true }),
+ );
+ await bounded(entered.promise, "request arrival");
+ controller.abort();
+ expect(await bounded(outcome, "fetch abort")).toEqual({ rejected: true });
+ await bounded(closed.promise, "server abort");
+ } finally {
+ release.resolve();
+ await server.stop(true);
+ }
+}, 5000);