Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(ai): Codex DOES do tool calling — implement it instead of announcing it cannot

Rahmancccedc7fe561
diff --git a/app/api/assistant/route.ts b/app/api/assistant/route.ts
index 769a901e..28ab9d94 100644
--- a/app/api/assistant/route.ts
+++ b/app/api/assistant/route.ts
@@ -216,8 +216,11 @@ export async function POST(req: Request) {
const emit = (event: string, data: unknown) => safeEnqueue(sse(event, data));
try {
if (isCodex && codexBundle) {
- // Chat-only OAuth path (no tools) through the ChatGPT Codex backend.
- await streamCodex({ bundle: codexBundle, model: codexModel, messages: rawMessages, system: sys, signal: req.signal, emit });
+ // ChatGPT Codex OAuth backend (Responses API). It carries tools like any
+ // other provider — this branch used to drop them and call itself
+ // "chat-only", which is why the UI advertised a tool catalog the model
+ // never received.
+ await streamCodex({ bundle: codexBundle, model: codexModel, messages: rawMessages, tools, system: sys, signal: req.signal, emit });
} else if (resolved?.protocol === "anthropic") {
ai = anthropic.messages.stream(
{
diff --git a/app/api/config/route.ts b/app/api/config/route.ts
index 1cb7905c..648ec8de 100644
--- a/app/api/config/route.ts
+++ b/app/api/config/route.ts
@@ -53,13 +53,6 @@ export async function GET() {
return NextResponse.json({
provider,
- // Whether the ACTIVE provider path can carry function calls at all. The
- // ChatGPT Codex OAuth backend cannot — app/api/assistant/route.ts takes a
- // chat-only branch for it and drops `tools` entirely. Until this existed the
- // UI advertised "22 tools" and "Alfa acts on your real VPS" on that provider
- // while the model correctly answered that it had none, which reads as the
- // assistant being broken rather than the provider being chat-only.
- toolsSupported: provider !== "openai-codex",
model: cfg.model || defaultModelFor(provider),
hasApiKey: key.length > 0,
apiKeyMasked: stored ? mask(stored) : "",
diff --git a/frontend/slices/assistant/components/agent-switcher.tsx b/frontend/slices/assistant/components/agent-switcher.tsx
index 109389ca..43497aba 100644
--- a/frontend/slices/assistant/components/agent-switcher.tsx
+++ b/frontend/slices/assistant/components/agent-switcher.tsx
@@ -9,7 +9,6 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import type { AIStore } from "../lib/store";
-import { useToolsSupported } from "../lib/use-tools-supported";
import { OS_TOOLS } from "../lib/tools";
import { GlyphTile } from "./agent-avatar";
@@ -17,19 +16,14 @@ import { GlyphTile } from "./agent-avatar";
// persona the system context for the next reply; "New agent" opens the editor.
export function AgentSwitcher({ store, onNew }: { store: AIStore; onNew: () => void }) {
const a = store.activeAgent;
- const toolsSupported = useToolsSupported();
return (
<DropdownMenu>
<DropdownMenuTrigger className="flex items-center gap-2 rounded-lg p-1 pr-2 outline-none hover:bg-accent [@media(pointer:coarse)]:min-h-[44px] [@media(pointer:coarse)]:min-w-[44px]">
<GlyphTile glyph={a.glyph} color={a.color} size={30} />
<div className="text-left leading-tight">
<div className="text-[13px] font-semibold">{a.name}</div>
- {/* OS_TOOLS.length is what the CATALOG holds, which is not what the model
- receives: a chat-only provider gets none of them. Printing the catalog
- size unconditionally is how the header came to say "22 tools" next to an
- assistant that correctly reported having zero. */}
<div className="text-[10px] text-muted-foreground">
- {toolsSupported ? `${OS_TOOLS.length} tools` : "chat only"}
+ {OS_TOOLS.length} tools
</div>
</div>
<ChevronDown className="size-3.5 text-muted-foreground" />
diff --git a/frontend/slices/assistant/components/chat-panel.tsx b/frontend/slices/assistant/components/chat-panel.tsx
index 8100a3c5..a45fddce 100644
--- a/frontend/slices/assistant/components/chat-panel.tsx
+++ b/frontend/slices/assistant/components/chat-panel.tsx
@@ -12,7 +12,6 @@ import {
import { ScrollArea } from "@/components/ui/scroll-area";
import { cn } from "@/lib/utils";
import { errText } from "./error-text";
-import { useToolsSupported } from "../lib/use-tools-supported";
import {
registerAlfaRunner,
updateAlfa,
@@ -69,7 +68,6 @@ export function ChatPanel({
}) {
const api = useOsApi();
const ios = useActiveShell().id === "ios";
- const toolsSupported = useToolsSupported();
// The display list lives in the SHARED Alfa store, not in this component. That
// is what makes the per-app Alfa sheet show this very conversation instead of a
// second, disconnected one — and it is less memory, not more: one array for
@@ -228,22 +226,12 @@ export function ChatPanel({
<div
className={cn(
"px-3 py-1 text-center text-[11px] font-medium",
- !toolsSupported
- ? "bg-amber-500/10 text-amber-600 dark:text-amber-400"
- : api.mode === "live"
- ? "bg-destructive/10 text-destructive"
- : "bg-muted text-muted-foreground",
+ api.mode === "live" ? "bg-destructive/10 text-destructive" : "bg-muted text-muted-foreground",
)}
>
- {/* Three states, not two. `api.mode` only says which DATA source the tools
- would hit — it says nothing about whether the model can call one, and on
- a chat-only provider the old two-state banner promised a VPS the
- assistant could not touch. */}
- {!toolsSupported
- ? "CHAT ONLY — this provider cannot call tools. Switch provider in Settings → AI to let Alfa act."
- : api.mode === "live"
- ? "● LIVE — Alfa acts on your real VPS; every change needs your approval"
- : "MOCK — actions are simulated (switch to Live in Settings → Server)"}
+ {api.mode === "live"
+ ? "● LIVE — Alfa acts on your real VPS; every change needs your approval"
+ : "MOCK — actions are simulated (switch to Live in Settings → Server)"}
</div>
{messages.length === 0 ? (
<div className="flex-1 overflow-y-auto">
diff --git a/frontend/slices/assistant/lib/use-tools-supported.ts b/frontend/slices/assistant/lib/use-tools-supported.ts
deleted file mode 100644
index 6cd5fb6f..00000000
--- a/frontend/slices/assistant/lib/use-tools-supported.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-"use client";
-
-import { useEffect, useState } from "react";
-import { IS_DEMO } from "@/lib/demo";
-
-// Can the ACTIVE provider actually call tools?
-//
-// Not a cosmetic question. app/api/assistant/route.ts takes a chat-only branch for
-// the ChatGPT Codex OAuth backend and drops the `tools` array entirely, so on that
-// provider every tool the UI advertises is unreachable. Before this, the header
-// claimed "22 tools" and the banner claimed "Alfa acts on your real VPS" while the
-// model answered, correctly, that it had none — which reads as a broken assistant
-// rather than a chat-only provider.
-//
-// Optimistic default: true. A provider that CAN use tools is the common case, and
-// flashing a "chat only" warning for one frame on every open would be its own lie.
-// The GET is cached at module scope because several surfaces ask (chat panel
-// banner, agent switcher badge) and the answer changes only when the owner picks a
-// different provider in Settings.
-
-let cached: Promise<boolean> | null = null;
-
-function fetchSupported(): Promise<boolean> {
- cached ??= fetch("/api/config", { cache: "no-store" })
- .then((r) => (r.ok ? r.json() : null))
- .then((c: { toolsSupported?: boolean } | null) => c?.toolsSupported !== false)
- .catch(() => true); // unreachable config is not evidence of a chat-only provider
- return cached;
-}
-
-/** Call after changing the provider so the next read re-asks. */
-export function invalidateToolsSupported(): void {
- cached = null;
-}
-
-export function useToolsSupported(): boolean {
- const [supported, setSupported] = useState(true);
- useEffect(() => {
- if (IS_DEMO) return; // demo makes no /api calls; its tools are simulated anyway
- let alive = true;
- void fetchSupported().then((v) => alive && setSupported(v));
- return () => {
- alive = false;
- };
- }, []);
- return supported;
-}
diff --git a/lib/ai/codex-stream.test.ts b/lib/ai/codex-stream.test.ts
new file mode 100644
index 00000000..bcbb879a
--- /dev/null
+++ b/lib/ai/codex-stream.test.ts
@@ -0,0 +1,208 @@
+import { describe, expect, it, vi, afterEach } from "vitest";
+import { streamCodex } from "./codex-stream";
+import type { OaMsg, OaTool } from "./openai-stream";
+
+// Regression suite for the branch that used to declare itself "chat-only". It was
+// not: the ChatGPT Codex backend takes tools over the Responses API, our request
+// simply never sent them — so the UI advertised a catalog the model never saw.
+
+const realFetch = globalThis.fetch;
+afterEach(() => {
+ globalThis.fetch = realFetch;
+});
+
+const sse = (...events: unknown[]) =>
+ new Response(events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join(""), {
+ status: 200,
+ headers: { "content-type": "text/event-stream" },
+ });
+
+type SentBody = { tools?: { type: string; name?: string; function?: unknown; input_schema?: unknown }[]; input?: Record<string, unknown>[]; store?: unknown };
+
+function capture(body: Response) {
+ const calls: RequestInit[] = [];
+ globalThis.fetch = ((_u: unknown, init: RequestInit) => {
+ calls.push(init);
+ return Promise.resolve(body);
+ }) as unknown as typeof fetch;
+ return (): SentBody => JSON.parse(String(calls[0]?.body ?? "{}")) as SentBody;
+}
+
+const run = async (o: Partial<Parameters<typeof streamCodex>[0]> = {}) => {
+ const events: [string, unknown][] = [];
+ await streamCodex({
+ bundle: { access: "t", accountId: "a" } as never,
+ model: "gpt-5",
+ messages: [{ role: "user", text: "hi" }],
+ system: "sys",
+ signal: new AbortController().signal,
+ emit: (e, d) => events.push([e, d]),
+ ...o,
+ });
+ return events;
+};
+
+const TOOL: OaTool = {
+ name: "fs.list",
+ description: "List a directory",
+ input_schema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] },
+};
+
+describe("tool declaration", () => {
+ it("sends tools in the FLAT Responses shape, not Anthropic's or chat-completions'", async () => {
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({ tools: [TOOL] });
+ const body = sent();
+ expect(body.tools).toEqual([
+ {
+ type: "function",
+ name: "fs_list", // dots are illegal on the wire — see the rename test below
+ description: "List a directory",
+ strict: false,
+ parameters: TOOL.input_schema,
+ },
+ ]);
+ // The two shapes that are NOT this, and each produce an opaque 400.
+ expect(body.tools?.[0].function).toBeUndefined();
+ expect(body.tools?.[0].input_schema).toBeUndefined();
+ });
+
+ it("underscores a dotted tool name — the API rejects dots outright", async () => {
+ // Live 400: "Invalid 'tools[0].name': Expected a string that matches the
+ // pattern '^[a-zA-Z0-9_-]+$'". Our catalog is dot.case and Anthropic accepts
+ // it, so this only ever surfaces against the real backend — every mocked test
+ // passed with the dots still in place.
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({ tools: [TOOL] });
+ expect(sent().tools?.[0].name).toBe("fs_list");
+ });
+
+ it("underscores the replayed call name too, or the turn 400s", async () => {
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({
+ tools: [TOOL],
+ messages: [
+ { role: "user", text: "go" },
+ { role: "assistant", toolUses: [{ id: "c1", name: "fs.list", input: {} }] },
+ { role: "tool", results: [{ id: "c1", content: "ok" }] },
+ ],
+ });
+ const fc = (sent().input ?? []).find((i) => i.type === "function_call");
+ expect(fc?.name).toBe("fs_list");
+ });
+
+ it("omits `tools` entirely when there are none — an empty array is not the same", async () => {
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run();
+ expect(sent().tools).toBeUndefined();
+ });
+
+ it("always sends store:false, which the backend requires", async () => {
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({ tools: [TOOL] });
+ expect(sent().store).toBe(false);
+ });
+});
+
+describe("tool round-trip", () => {
+ it("replays a call as function_call and its result as function_call_output", async () => {
+ // Both items, keyed by the same call_id. Dropping either 400s the next turn
+ // with "No tool output found for function call".
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ const messages: OaMsg[] = [
+ { role: "user", text: "list ~" },
+ { role: "assistant", toolUses: [{ id: "call_1", name: "fs.list", input: { path: "~" } }] },
+ { role: "tool", results: [{ id: "call_1", content: "a\nb" }] },
+ ];
+ await run({ messages, tools: [TOOL] });
+ const input = sent().input as Record<string, unknown>[];
+ expect(input).toContainEqual({
+ type: "function_call",
+ call_id: "call_1",
+ name: "fs_list",
+ arguments: JSON.stringify({ path: "~" }),
+ });
+ expect(input).toContainEqual({ type: "function_call_output", call_id: "call_1", output: "a\nb" });
+ });
+});
+
+describe("reading calls back", () => {
+ const call = (over: Record<string, unknown> = {}) => ({
+ type: "function_call",
+ status: "completed",
+ call_id: "c1",
+ name: "fs.list",
+ arguments: '{"path":"~"}',
+ ...over,
+ });
+
+ it("maps the wire name back to the catalog name", async () => {
+ // The model answers with what we declared (fs_list); everything downstream —
+ // the approval card, the executor, the audit trail — keys on fs.list.
+ capture(sse({ type: "response.output_item.done", item: call({ name: "fs_list" }) }));
+ const ev = await run({ tools: [TOOL] });
+ expect(ev).toContainEqual(["tool_use", { id: "c1", name: "fs.list", input: { path: "~" } }]);
+ });
+
+ it("leaves an undeclared name alone rather than inventing a dot", async () => {
+ capture(sse({ type: "response.output_item.done", item: call({ name: "mystery_tool" }) }));
+ const ev = await run({ tools: [TOOL] });
+ expect(ev).toContainEqual(["tool_use", { id: "c1", name: "mystery_tool", input: { path: "~" } }]);
+ });
+
+ it("emits tool_use and reports stopReason tool_use", async () => {
+ capture(sse({ type: "response.output_item.done", item: call({ name: "fs_list" }) }));
+ const ev = await run({ tools: [TOOL] });
+ expect(ev).toContainEqual(["tool_use", { id: "c1", name: "fs.list", input: { path: "~" } }]);
+ // The agent loop keys off this — end_turn here strands the result mid-task.
+ expect(ev.at(-1)).toEqual(["done", { stopReason: "tool_use" }]);
+ });
+
+ it("does NOT emit the same call twice when response.completed repeats it", async () => {
+ capture(
+ sse(
+ { type: "response.output_item.done", item: call() },
+ { type: "response.completed", response: { output: [call()] } },
+ ),
+ );
+ const ev = await run({ tools: [TOOL] });
+ expect(ev.filter(([e]) => e === "tool_use")).toHaveLength(1);
+ });
+
+ it("ignores a partial item — its arguments are still streaming", async () => {
+ capture(sse({ type: "response.output_item.done", item: call({ status: "in_progress", arguments: '{"pa' }) }));
+ const ev = await run({ tools: [TOOL] });
+ expect(ev.some(([e]) => e === "tool_use")).toBe(false);
+ expect(ev.at(-1)).toEqual(["done", { stopReason: "end_turn" }]);
+ });
+
+ it("surfaces a call with malformed arguments rather than dropping the turn", async () => {
+ capture(sse({ type: "response.output_item.done", item: call({ arguments: "{not json" }) }));
+ const ev = await run({ tools: [TOOL] });
+ expect(ev).toContainEqual(["tool_use", { id: "c1", name: "fs.list", input: {} }]);
+ });
+
+ it("still streams plain text with no tools declared", async () => {
+ capture(sse({ type: "response.output_text.delta", delta: "hello" }));
+ const ev = await run();
+ expect(ev[0]).toEqual(["delta", "hello"]);
+ expect(ev.at(-1)).toEqual(["done", { stopReason: "end_turn" }]);
+ });
+});
+
+describe("provider-executed built-ins", () => {
+ it("declares nothing extra by default — a provider-run tool bills the owner", async () => {
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({ tools: [TOOL] });
+ expect(sent().tools).toHaveLength(1);
+ });
+
+ it("adds an opted-in built-in by type alone, and ignores an unknown one", async () => {
+ vi.stubEnv("OS_CODEX_BUILTIN_TOOLS", "image_generation, not_a_tool");
+ const sent = capture(sse({ type: "response.completed", response: { output: [] } }));
+ await run({ tools: [TOOL] });
+ const tools = sent().tools ?? [];
+ expect(tools.map((t) => t.type)).toEqual(["function", "image_generation"]);
+ vi.unstubAllEnvs();
+ });
+});
diff --git a/lib/ai/codex-stream.ts b/lib/ai/codex-stream.ts
index ad86d12d..483e9efc 100644
--- a/lib/ai/codex-stream.ts
+++ b/lib/ai/codex-stream.ts
@@ -1,31 +1,130 @@
import type { OAuthBundle } from "@/lib/config/store";
import { CODEX } from "./oauth/codex";
-import type { OaMsg } from "./openai-stream";
+import type { OaMsg, OaTool } from "./openai-stream";
// Stream a chat via the ChatGPT "Codex" backend Responses API. Unlike the OpenAI
// platform (/chat/completions), this is the consumer backend: the OAuth bearer +
// the account id from the token, the Responses request shape, and SSE events named
-// `response.output_text.delta`. Chat-only — no tools. Emits the same delta|done vocab
-// the other streamers do, so the client stays provider-agnostic.
+// `response.output_text.delta`. Emits the same delta|tool_use|done vocab the other
+// streamers do, so the client stays provider-agnostic.
+//
+// THIS PATH USED TO DROP `tools` AND WAS COMMENTED "chat-only — no tools". That was
+// wrong, and it made the whole assistant look broken: the UI advertised its catalog
+// while the model correctly reported having none. The backend supports tools fine —
+// our request just never carried them. Verified against Hermes, which drives the
+// SAME endpoint with tools (agent/codex_responses_adapter.py).
+//
+// The Responses tool shape is neither Anthropic's nor Chat Completions': it is FLAT.
+// Anthropic { name, description, input_schema }
+// Chat Completions { type:"function", function:{ name, description, parameters } }
+// Responses { type:"function", name, description, strict, parameters }
+// Getting this wrong is a 400 with no useful body, which is most of why the
+// chat-only shortcut looked reasonable at the time.
+
+/** Provider-EXECUTED tools: declared by type alone, no schema, the provider runs
+ * them and streams the result back. `image_generation` is the one worth having —
+ * it is why "Codex can't do tools" was visibly false to anyone who had used it.
+ * Kept as an allowlist rather than pass-through: an unknown type is a 400. */
+const BUILTIN_TOOL_TYPES = new Set([
+ "web_search",
+ "web_search_preview",
+ "file_search",
+ "code_interpreter",
+ "image_generation",
+ "computer_use_preview",
+ "local_shell",
+]);
+
+type ResponsesTool =
+ | { type: "function"; name: string; description: string; strict: boolean; parameters: Record<string, unknown> }
+ | { type: string };
+
+/** Built-ins the owner opted into, e.g. `OS_CODEX_BUILTIN_TOOLS=image_generation`.
+ * Off by default: a provider-run tool bills to the owner's ChatGPT account and
+ * returns content this shell does not render yet, so it is opt-in rather than a
+ * surprise on the next reply. */
+function builtinTools(): ResponsesTool[] {
+ return (process.env.OS_CODEX_BUILTIN_TOOLS ?? "")
+ .split(/[\s,]+/)
+ .filter((t) => BUILTIN_TOOL_TYPES.has(t))
+ .map((type) => ({ type }));
+}
+
+/** The Responses API enforces `^[a-zA-Z0-9_-]+$` on a tool name — NO DOTS. Our
+ * catalog is dot.case (`fs.list`, `exec.run`), which Anthropic accepts and this
+ * backend rejects with a 400 naming only `tools[0].name`. So the wire name is
+ * underscored and mapped back when the call returns; the model never sees the
+ * difference and nothing downstream has to know.
+ * Only a live request finds this: every unit test that mocks fetch passes with
+ * dots still in place. */
+const wireName = (name: string) => name.replace(/[^a-zA-Z0-9_-]/g, "_");
+
+const toResponsesTool = (t: OaTool): ResponsesTool => ({
+ type: "function",
+ name: wireName(t.name),
+ description: t.description ?? "",
+ // strict:false deliberately — strict mode requires every property to be
+ // `required` and `additionalProperties:false`, which the host-tool catalog does
+ // not satisfy (most tools have optional args). A strict:true schema mismatch is
+ // rejected at request time, not at call time.
+ strict: false,
+ parameters: t.input_schema,
+});
+
+/** Our neutral message list → Responses `input` items.
+ * A tool round-trip is TWO item types, not a role: the assistant's call is a
+ * `function_call` item and the result is a `function_call_output` item, both keyed
+ * by the same call_id. Dropping either one makes the next turn 400 with
+ * "No tool output found for function call". */
+function toResponsesInput(messages: OaMsg[]): Record<string, unknown>[] {
+ const items: Record<string, unknown>[] = [];
+ for (const m of messages) {
+ if (m.role === "tool") {
+ for (const r of m.results) {
+ items.push({ type: "function_call_output", call_id: r.id, output: r.content });
+ }
+ continue;
+ }
+ const text = ("text" in m && m.text) || "";
+ if (text.trim()) {
+ items.push({
+ type: "message",
+ role: m.role,
+ content: [{ type: m.role === "assistant" ? "output_text" : "input_text", text }],
+ });
+ }
+ if (m.role === "assistant") {
+ for (const u of m.toolUses ?? []) {
+ items.push({
+ type: "function_call",
+ call_id: u.id,
+ // Same rename as the declaration, or the replayed call names a tool the
+ // request never declared and the turn 400s.
+ name: wireName(u.name),
+ arguments: JSON.stringify(u.input ?? {}),
+ });
+ }
+ }
+ }
+ return items;
+}
+
export async function streamCodex(opts: {
bundle: OAuthBundle;
model: string;
messages: OaMsg[];
+ tools?: OaTool[];
system: string;
signal: AbortSignal;
emit: (event: "delta" | "tool_use" | "done", data: unknown) => void;
}): Promise<void> {
- const { bundle, model, messages, system, signal, emit } = opts;
+ const { bundle, model, messages, tools, system, signal, emit } = opts;
if (signal.aborted) return;
- const input = messages
- .filter((m): m is Extract<OaMsg, { role: "user" | "assistant" }> => m.role === "user" || m.role === "assistant")
- .map((m) => ({
- type: "message" as const,
- role: m.role,
- content: [{ type: m.role === "assistant" ? "output_text" : "input_text", text: ("text" in m && m.text) || "" }],
- }))
- .filter((m) => m.content[0].text.trim());
+ const declared: ResponsesTool[] = [...(tools ?? []).map(toResponsesTool), ...builtinTools()];
+ // wire name → the name the rest of the app uses. Built from the DECLARED set, so
+ // a genuine underscore name can never be rewritten into someone else's dot name.
+ const realName = new Map((tools ?? []).map((t) => [wireName(t.name), t.name]));
const res = await fetch(`${CODEX.apiBase}/responses`, {
method: "POST",
@@ -38,7 +137,15 @@ export async function streamCodex(opts: {
accept: "text/event-stream",
"content-type": "application/json",
},
- body: JSON.stringify({ model, store: false, stream: true, instructions: system, input }),
+ body: JSON.stringify({
+ model,
+ // The backend REQUIRES store:false — it rejects anything else outright.
+ store: false,
+ stream: true,
+ instructions: system,
+ input: toResponsesInput(messages),
+ ...(declared.length ? { tools: declared, parallel_tool_calls: false } : {}),
+ }),
signal,
});
if (!res.ok || !res.body) {
@@ -49,6 +156,9 @@ export async function streamCodex(opts: {
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = "";
+ let sawToolCall = false;
+ const seen = new Set<string>();
+
while (true) {
if (signal.aborted) {
await reader.cancel().catch(() => {});
@@ -64,14 +174,67 @@ export async function streamCodex(opts: {
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
if (!data || data === "[DONE]") continue;
- let ev: { type?: string; delta?: string };
+ let ev: { type?: string; delta?: string; item?: ResponsesItem; response?: { output?: ResponsesItem[] } };
try {
ev = JSON.parse(data);
} catch {
continue;
}
- if (ev.type === "response.output_text.delta" && typeof ev.delta === "string") emit("delta", ev.delta);
+ if (ev.type === "response.output_text.delta" && typeof ev.delta === "string") {
+ emit("delta", ev.delta);
+ continue;
+ }
+ // A completed function_call arrives as an output_item; `response.completed`
+ // repeats the full output, so both are scanned and deduped by call_id — the
+ // model would otherwise be answered twice for one call.
+ if (ev.type === "response.output_item.done" && ev.item) {
+ if (emitCall(ev.item, seen, realName, emit)) sawToolCall = true;
+ } else if (ev.type === "response.completed") {
+ for (const item of ev.response?.output ?? []) {
+ if (emitCall(item, seen, realName, emit)) sawToolCall = true;
+ }
+ }
}
}
- emit("done", { stopReason: "end_turn" });
+ // The agent loop keys entirely off stopReason: reporting end_turn after a call
+ // would strand the tool result and end the run mid-task.
+ emit("done", { stopReason: sawToolCall ? "tool_use" : "end_turn" });
+}
+
+type ResponsesItem = {
+ type?: string;
+ status?: string;
+ id?: string;
+ call_id?: string;
+ name?: string;
+ arguments?: unknown;
+};
+
+/** Emit one `function_call` item as a neutral tool_use. Returns whether it did. */
+function emitCall(
+ item: ResponsesItem,
+ seen: Set<string>,
+ realName: Map<string, string>,
+ emit: (e: "tool_use", d: unknown) => void,
+): boolean {
+ if (item?.type !== "function_call") return false;
+ // Partial items stream before the arguments are complete; acting on one would
+ // call the tool with truncated JSON.
+ if (item.status && ["queued", "in_progress", "incomplete"].includes(item.status)) return false;
+ const id = (item.call_id ?? item.id ?? "").trim();
+ const wire = (item.name ?? "").trim();
+ const name = realName.get(wire) ?? wire;
+ if (!id || !name || seen.has(id)) return false;
+ seen.add(id);
+ let input: Record<string, unknown> = {};
+ try {
+ const raw = typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments ?? {});
+ const parsed: unknown = JSON.parse(raw || "{}");
+ if (parsed && typeof parsed === "object") input = parsed as Record<string, unknown>;
+ } catch {
+ // Malformed arguments: still surface the call so the loop can answer with an
+ // error the model can correct, rather than silently dropping the turn.
+ }
+ emit("tool_use", { id, name, input });
+ return true;
}