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.

feat(ai): parse and propagate provider usage from stream turn

Abdurrahman Fakhrul110effac629d
diff --git a/lib/ai/agent-loop.ts b/lib/ai/agent-loop.ts
index ccf65aff..9f142b19 100644
--- a/lib/ai/agent-loop.ts
+++ b/lib/ai/agent-loop.ts
@@ -1,4 +1,5 @@
-import { streamAgentTurn, type AgentMsg, type AiTool } from "./stream";
+import { streamAgentTurn, type AgentMsg, type AiTool, type ProviderUsage } from "./stream";
+import { addProviderUsage } from "./provider-usage.mjs";
// Generic tool-execution contract. `invoke` runs one tool call and returns a
// short, model-readable outcome; where the side-effect happens (a client store,
@@ -13,6 +14,13 @@ export type AgentEvents = {
onTool: (name: string, input: Record<string, unknown>, outcome: ToolOutcome) => void;
};
+export type AgentLoopResult = {
+ history: AgentMsg[];
+ text: string;
+ outcome: "completed" | "stopped" | "limit";
+ usage?: ProviderUsage;
+};
+
// Drives a function-calling conversation: stream a turn → run any tool_use via
// `invoke` → feed the results back → repeat until the model stops calling tools
// (or the turn cap is hit). Generic over the tool set + executor, so the image
@@ -28,13 +36,17 @@ export async function runToolAgent(
maxTurns = 8,
system?: string,
signal?: AbortSignal,
-): Promise<{ history: AgentMsg[]; text: string; outcome: "completed" | "stopped" | "limit" }> {
+): Promise<AgentLoopResult> {
const msgs = [...history];
let lastText = "";
let outcome: "completed" | "stopped" | "limit" = "limit";
+ let totalUsage: ProviderUsage | undefined = undefined;
for (let i = 0; i < maxTurns; i++) {
if (signal?.aborted) { outcome = "stopped"; break; }
- const { text, toolUses } = await streamAgentTurn(msgs, tools, ev.onDelta, signal, system);
+ const { text, toolUses, usage } = await streamAgentTurn(msgs, tools, ev.onDelta, signal, system);
+ if (usage) {
+ totalUsage = addProviderUsage(totalUsage, usage) as ProviderUsage;
+ }
msgs.push({ role: "assistant", text, toolUses });
if (text) lastText = text;
if (toolUses.length === 0) { outcome = signal?.aborted ? "stopped" : "completed"; break; }
@@ -51,7 +63,12 @@ export async function runToolAgent(
}
msgs.push({ role: "tool", results });
}
- return { history: msgs, text: lastText, outcome: signal?.aborted ? "stopped" : outcome };
+ return {
+ history: msgs,
+ text: lastText,
+ outcome: signal?.aborted ? "stopped" : outcome,
+ ...(totalUsage ? { usage: totalUsage } : {}),
+ };
}
-export type { AgentMsg, AiTool } from "./stream";
+export type { AgentMsg, AiTool, ProviderUsage } from "./stream";
diff --git a/lib/ai/stream.test.ts b/lib/ai/stream.test.ts
new file mode 100644
index 00000000..e5309587
--- /dev/null
+++ b/lib/ai/stream.test.ts
@@ -0,0 +1,84 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { streamAgentTurn } from "./stream";
+
+const realFetch = globalThis.fetch;
+
+afterEach(() => {
+ globalThis.fetch = realFetch;
+});
+
+function sseResponse(body: string, status = 200): Response {
+ return new Response(new TextEncoder().encode(body), {
+ status,
+ headers: { "content-type": "text/event-stream" },
+ });
+}
+
+describe("streamAgentTurn", () => {
+ it("parses usage from the done event when present", async () => {
+ const sse = [
+ "event: delta\ndata: \"Hello world\"\n\n",
+ "event: done\ndata: {\"stopReason\":\"end_turn\",\"usage\":{\"inputTokens\":42,\"outputTokens\":18,\"cacheReadTokens\":10}}\n\n",
+ ].join("");
+
+ globalThis.fetch = vi.fn().mockResolvedValue(sseResponse(sse));
+
+ const deltas: string[] = [];
+ const turn = await streamAgentTurn([], [], (c) => deltas.push(c));
+
+ expect(turn.text).toBe("Hello world");
+ expect(deltas).toEqual(["Hello world"]);
+ expect(turn.stopReason).toBe("end_turn");
+ expect(turn.usage).toEqual({
+ inputTokens: 42,
+ outputTokens: 18,
+ cacheReadTokens: 10,
+ });
+ });
+
+ it("leaves usage undefined when absent from the done event", async () => {
+ const sse = [
+ "event: delta\ndata: \"No usage here\"\n\n",
+ "event: done\ndata: {\"stopReason\":\"end_turn\"}\n\n",
+ ].join("");
+
+ globalThis.fetch = vi.fn().mockResolvedValue(sseResponse(sse));
+
+ const turn = await streamAgentTurn([], [], () => {});
+
+ expect(turn.text).toBe("No usage here");
+ expect(turn.stopReason).toBe("end_turn");
+ expect(turn.usage).toBeUndefined();
+ });
+
+ it("leaves usage undefined when done payload has usage: null", async () => {
+ const sse = [
+ "event: done\ndata: {\"stopReason\":\"end_turn\",\"usage\":null}\n\n",
+ ].join("");
+
+ globalThis.fetch = vi.fn().mockResolvedValue(sseResponse(sse));
+
+ const turn = await streamAgentTurn([], [], () => {});
+
+ expect(turn.usage).toBeUndefined();
+ });
+
+ it("throws on error event in stream", async () => {
+ const sse = [
+ "event: error\ndata: \"provider_overloaded\"\n\n",
+ ].join("");
+
+ globalThis.fetch = vi.fn().mockResolvedValue(sseResponse(sse));
+
+ await expect(streamAgentTurn([], [], () => {})).rejects.toThrow("provider_overloaded");
+ });
+
+ it("throws when fetch response is not ok", async () => {
+ globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: "unauthorized" }), {
+ status: 401,
+ headers: { "content-type": "application/json" },
+ }));
+
+ await expect(streamAgentTurn([], [], () => {})).rejects.toThrow("unauthorized");
+ });
+});
diff --git a/lib/ai/stream.ts b/lib/ai/stream.ts
index 216b9924..acb951b0 100644
--- a/lib/ai/stream.ts
+++ b/lib/ai/stream.ts
@@ -11,7 +11,24 @@ export type AgentMsg =
| { role: "user"; text: string }
| { role: "assistant"; text?: string; toolUses?: AiToolUse[] }
| { role: "tool"; results: { id: string; content: string; isError?: boolean }[] };
-export type AgentTurn = { text: string; toolUses: AiToolUse[]; stopReason: string | null };
+export type ProviderUsage = {
+ accountingMode?: string;
+ apiCalls?: number;
+ inputTokens?: number;
+ outputTokens?: number;
+ totalTokens?: number;
+ cacheReadTokens?: number;
+ cacheWriteTokens?: number;
+ reasoningTokens?: number;
+ detailCoverage?: Record<string, number>;
+};
+
+export type AgentTurn = {
+ text: string;
+ toolUses: AiToolUse[];
+ stopReason: string | null;
+ usage?: ProviderUsage;
+};
// One assistant turn with tools. Streams text via `onDelta`, then resolves with
// the accumulated text + any tool_use blocks the model emitted (which the caller
@@ -39,6 +56,7 @@ export async function streamAgentTurn(
let text = "";
const toolUses: AiToolUse[] = [];
let stopReason: string | null = null;
+ let usage: ProviderUsage | undefined = undefined;
while (true) {
if (signal?.aborted) { await reader.cancel().catch(() => {}); break; }
const { done, value } = await reader.read();
@@ -57,8 +75,19 @@ export async function streamAgentTurn(
if (event === "delta") { const t = JSON.parse(data) as string; text += t; onDelta(t); }
else if (event === "tool_use") toolUses.push(JSON.parse(data) as AiToolUse);
else if (event === "error") throw new Error(JSON.parse(data) as string);
- else if (event === "done") stopReason = (JSON.parse(data) as { stopReason: string }).stopReason ?? null;
+ else if (event === "done") {
+ const payload = JSON.parse(data) as { stopReason?: string; usage?: ProviderUsage };
+ stopReason = payload.stopReason ?? null;
+ if (payload.usage && typeof payload.usage === "object") {
+ usage = payload.usage;
+ }
+ }
}
}
- return { text, toolUses, stopReason };
+ return {
+ text,
+ toolUses,
+ stopReason,
+ ...(usage !== undefined ? { usage } : {}),
+ };
}