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.
import type { RuntimeModel } from "../copilot";
/**
* The one model call the router makes, kept apart from the routing logic so that logic stays a pure
* function the tests drive without a network. This reuses the deployment's own model and key — the
* same ones the built-in coworkers answer on — so a router is never a second thing to configure.
*
* It throws on a missing key or a bad response on purpose: the router treats a throw as "not sure"
* and lands on the default, so failure here is a soft landing, not an error a person sees.
*/
export function createModelCompleter(deps: {
model: RuntimeModel;
resolveApiKey: () => Promise<string | null>;
}): (prompt: string, signal?: AbortSignal) => Promise<string> {
return async (prompt: string, signal?: AbortSignal) => {
signal?.throwIfAborted();
const key = await deps.resolveApiKey();
signal?.throwIfAborted();
if (!key) throw new Error("no model 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),
},
);
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 = 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;
};
}
/**
* Where this deployment's `/chat/completions` actually is.
*
* `/v1` USED TO BE APPENDED UNCONDITIONALLY, and that was wrong for every deployment that set the
* variable. `.env.example` and `docs/configuration.md` both document it with the version in it
* (`https://gateway.internal/v1`), because that is the shape the AI SDK wants: it takes `baseURL`
* verbatim and asks for `/chat/completions` under it, which is how the built-in Bots reach the same
* endpoint. Appending here produced `/v1/v1/chat/completions`, so on a gateway — the entire reason
* the variable exists — every call from this function 404'd.
*
* That failure was invisible, which is the worst part. The router treats a throw as "not sure" and
* lands on the default coworker, so a deployment behind a gateway silently stopped routing and
* nothing anywhere said why. Tool selection reads through the same function and would have failed
* the same way, offering the whole catalogue on the deployments most likely to have a big one.
*
* So the version segment is added only when the configured URL does not already end in one, and the
* unset case keeps the public API's own `https://api.openai.com/v1`.
*/
export function chatCompletionsUrl(
environment: Record<string, string | undefined>,
): string {
const base = (environment.OPENAI_BASE_URL?.trim() || "https://api.openai.com")
// A trailing slash is the difference between `/v1` and `/v1/`, and no more than that.
.replace(/\/+$/, "");
return /\/v\d+$/.test(base)
? `${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`;
}