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.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
feat(assistant): render per-turn token usage and running thread total
Abdurrahman Fakhrul673acada3a7b
diff --git a/frontend/slices/assistant/components/chat-panel.tsx b/frontend/slices/assistant/components/chat-panel.tsxindex 06c818ff..6d957125 100644--- a/frontend/slices/assistant/components/chat-panel.tsx+++ b/frontend/slices/assistant/components/chat-panel.tsx@@ -5,7 +5,9 @@ import {useEffect,useImperativeHandle,useMemo,- useRef, type Ref+ useRef,+ useState,+ type Ref} from "react";import { ScrollArea } from "@/components/ui/scroll-area";import { cn } from "@/lib/utils";@@ -31,6 +33,7 @@ import { composeAutomationRequest } from "../lib/automation-request";import { useHostCommands, type HostToolUi } from "../host-tools/use-host-commands";import { type ChatMessage, type ToolCard } from "@/features/appshell";import { ChatTranscript } from "./chat-transcript";+import { TurnUsage, addUsage, type ProviderUsage } from "./turn-usage";import { EmptyState } from "./empty-state";import { useThreadPersistence } from "./use-thread-persistence";@@ -88,6 +91,8 @@ export function ChatPanel({// the registered runner, so a local copy plus an `if (streaming) return` guard in// send() meant every send routed through the store was silently dropped.const streaming = useAlfaBusy();+ const [turnUsage, setTurnUsage] = useState<ProviderUsage | null>(null);+ const [threadUsage, setThreadUsage] = useState<ProviderUsage | null>(null);const bottomRef = useRef<HTMLDivElement>(null);const abortRef = useRef<AbortController | null>(null);const agentRef = useRef(agent);@@ -175,7 +180,7 @@ export function ChatPanel({setAlfaOperation("Requesting response");let runOk = false;try {- const { history: next, outcome } = await runToolAgent(+ const { history: next, outcome, usage } = await runToolAgent(historyRef.current,tools,invoke,@@ -194,6 +199,10 @@ export function ChatPanel({ctrl.signal,);historyRef.current = next;+ if (usage) {+ setTurnUsage(usage);+ setThreadUsage((prev) => addUsage(prev, usage));+ }runOk = outcome === "completed";if (outcome === "limit") appendToLastAssistant((t) => t + "\n\nTurn limit reached. Execution has stopped; send a new message to continue.");} catch (err) {@@ -219,6 +228,21 @@ export function ChatPanel({useAlfaRunner(send, stop);+ const handleLoadThread = useCallback(+ (t: { id: string; createdAt: number; messages: unknown[]; history: unknown[] }) => {+ setTurnUsage(null);+ setThreadUsage(null);+ loadThread(t);+ },+ [loadThread],+ );++ const handleNewThread = useCallback(() => {+ setTurnUsage(null);+ setThreadUsage(null);+ newThread();+ }, [newThread]);+// Browser-local automations are execution recipes, not a second executor. One// click feeds their exact ordered tool intent back through Alfa's normal agent// loop, so reads use the same host bindings and every mutation still parks on@@ -228,8 +252,8 @@ export function ChatPanel({void sendToAlfa(composeAutomationRequest(auto, (runAgent ?? agentRef.current).name));},stop,- loadThread,- newThread,+ loadThread: handleLoadThread,+ newThread: handleNewThread,}));return (@@ -266,6 +290,7 @@ export function ChatPanel({</div></ScrollArea>)}+ <TurnUsage turnUsage={messages.length > 0 ? turnUsage : null} threadTotal={messages.length > 0 ? threadUsage : null} />{/* Through sendToAlfa, not send(): one entry point means one single-flightguard and one busy flag for every surface. */}<ChatComposer onSend={(t) => void sendToAlfa(t)} streaming={streaming} onStop={stop} />diff --git a/frontend/slices/assistant/components/turn-usage.tsx b/frontend/slices/assistant/components/turn-usage.tsxnew file mode 100644index 00000000..dca0127b--- /dev/null+++ b/frontend/slices/assistant/components/turn-usage.tsx@@ -0,0 +1,62 @@+"use client";++import { cn } from "@/lib/utils";+import type { ProviderUsage } from "@/lib/ai/stream";+import { addProviderUsage } from "@/lib/ai/provider-usage.mjs";++export function formatTurnUsage(usage?: ProviderUsage | null, label = "this turn"): string {+ if (!usage) return "";+ const input = (usage.inputTokens ?? 0).toLocaleString();+ const output = (usage.outputTokens ?? 0).toLocaleString();+ const parts = [`${label}: ${input} in`, `${output} out`];+ if (usage.cacheReadTokens != null) {+ parts.push(`${usage.cacheReadTokens.toLocaleString()} cached`);+ }+ return parts.join(" / ");+}++export function formatThreadUsage(total?: ProviderUsage | null): string {+ return formatTurnUsage(total, "thread total");+}++export function formatUsageSummary(turn?: ProviderUsage | null, total?: ProviderUsage | null): string {+ const turnStr = formatTurnUsage(turn);+ const totalStr = formatThreadUsage(total);+ if (turnStr && totalStr) return `${turnStr} · ${totalStr}`;+ return turnStr || totalStr;+}++export function addUsage(total?: ProviderUsage | null, next?: ProviderUsage | null): ProviderUsage {+ return addProviderUsage(total, next) as ProviderUsage;+}++export function TurnUsage({+ turnUsage,+ threadTotal,+ className,+}: {+ turnUsage?: ProviderUsage | null;+ threadTotal?: ProviderUsage | null;+ className?: string;+}) {+ const turnStr = formatTurnUsage(turnUsage);+ const totalStr = formatThreadUsage(threadTotal);+ if (!turnStr && !totalStr) return null;++ return (+ <div+ data-testid="turn-usage"+ aria-label="Token usage"+ className={cn(+ "flex flex-wrap items-center gap-x-2 gap-y-0.5 border-t border-border/40 bg-card/20 px-4 py-1 text-[11px] text-muted-foreground",+ className,+ )}+ >+ {turnStr ? <span>{turnStr}</span> : null}+ {turnStr && totalStr ? <span aria-hidden="true" className="opacity-50">·</span> : null}+ {totalStr ? <span>{totalStr}</span> : null}+ </div>+ );+}++export type { ProviderUsage };diff --git a/lib/ai/agent-loop.test.ts b/lib/ai/agent-loop.test.tsindex 490ee1f2..73ba5371 100644--- a/lib/ai/agent-loop.test.ts+++ b/lib/ai/agent-loop.test.ts@@ -1,7 +1,14 @@import { expect, it, vi } from "vitest";import { runToolAgent } from "./agent-loop";import { streamAgentTurn } from "./stream";+import {+ formatTurnUsage,+ formatThreadUsage,+ formatUsageSummary,+} from "../../frontend/slices/assistant/components/turn-usage";+vi.mock("./stream", () => ({ streamAgentTurn: vi.fn() }));+it("stops between tool calls and retains matching tool results", async () => {vi.mocked(streamAgentTurn).mockResolvedValue({ text: "", stopReason: null, toolUses: [{ id: "one", name: "fs.read", input: {} }, { id: "two", name: "fs.write", input: {} },@@ -13,8 +20,92 @@ it("stops between tool calls and retains matching tool results", async () => {expect(result.outcome).toBe("stopped");expect(result.history.at(-1)).toMatchObject({ role: "tool", results: [{ id: "one" }, { id: "two", isError: true }] });});+it("reports the turn cap as a limit instead of successful completion", async () => {vi.mocked(streamAgentTurn).mockResolvedValue({ text: "", stopReason: null, toolUses: [{ id: "one", name: "fs.read", input: {} }] });const result = await runToolAgent([], [], async () => ({ ok: true, result: "read" }), { onDelta: vi.fn(), onTool: vi.fn() }, 1);expect(result.outcome).toBe("limit");});++it("aggregates usage across multiple turns with addProviderUsage", async () => {+ vi.mocked(streamAgentTurn)+ .mockResolvedValueOnce({+ text: "calling tool",+ stopReason: "tool_use",+ toolUses: [{ id: "t1", name: "fs.read", input: { path: "a.txt" } }],+ usage: {+ inputTokens: 100,+ outputTokens: 20,+ cacheReadTokens: 15,+ accountingMode: "separate-cache-input-output",+ apiCalls: 1,+ },+ })+ .mockResolvedValueOnce({+ text: "done with tool",+ stopReason: "end_turn",+ toolUses: [],+ usage: {+ inputTokens: 50,+ outputTokens: 10,+ cacheReadTokens: 5,+ accountingMode: "separate-cache-input-output",+ apiCalls: 1,+ },+ });++ const invoke = vi.fn(async () => ({ ok: true, result: "content" }));+ const result = await runToolAgent([], [], invoke, { onDelta: vi.fn(), onTool: vi.fn() }, 8);++ expect(result.outcome).toBe("completed");+ expect(result.text).toBe("done with tool");+ expect(result.usage).toMatchObject({+ inputTokens: 150,+ outputTokens: 30,+ cacheReadTokens: 20,+ apiCalls: 2,+ });+});++it("leaves usage undefined if no turn returned usage", async () => {+ vi.mocked(streamAgentTurn).mockResolvedValueOnce({+ text: "done",+ stopReason: "end_turn",+ toolUses: [],+ });++ const result = await runToolAgent([], [], async () => ({ ok: true, result: "ok" }), { onDelta: vi.fn(), onTool: vi.fn() }, 1);+ expect(result.outcome).toBe("completed");+ expect(result.usage).toBeUndefined();+});++it("formats turn usage with cached tokens", () => {+ expect(formatTurnUsage({ inputTokens: 1200, outputTokens: 80, cacheReadTokens: 600 })).toBe(+ "this turn: 1,200 in / 80 out / 600 cached",+ );+});++it("formats turn usage without cached tokens when cacheReadTokens is absent", () => {+ expect(formatTurnUsage({ inputTokens: 100, outputTokens: 20 })).toBe(+ "this turn: 100 in / 20 out",+ );+});++it("formats thread total and summary cleanly", () => {+ expect(formatThreadUsage({ inputTokens: 5000, outputTokens: 350, cacheReadTokens: 2500 })).toBe(+ "thread total: 5,000 in / 350 out / 2,500 cached",+ );+ expect(+ formatUsageSummary(+ { inputTokens: 100, outputTokens: 20, cacheReadTokens: 10 },+ { inputTokens: 500, outputTokens: 100, cacheReadTokens: 50 },+ ),+ ).toBe("this turn: 100 in / 20 out / 10 cached · thread total: 500 in / 100 out / 50 cached");+});++it("returns empty string when usage is absent or null", () => {+ expect(formatTurnUsage(undefined)).toBe("");+ expect(formatTurnUsage(null)).toBe("");+ expect(formatThreadUsage(undefined)).toBe("");+ expect(formatUsageSummary(null, null)).toBe("");+});