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
refactor(assistant): move chat turn-usage state into use-turn-usage hook
Abdurrahman Fakhrul86255ec00001
diff --git a/frontend/slices/assistant/components/chat-panel.tsx b/frontend/slices/assistant/components/chat-panel.tsxindex 6d957125..6c8b97b3 100644--- a/frontend/slices/assistant/components/chat-panel.tsx+++ b/frontend/slices/assistant/components/chat-panel.tsx@@ -6,7 +6,6 @@ import {useImperativeHandle,useMemo,useRef,- useState,type Ref} from "react";import { ScrollArea } from "@/components/ui/scroll-area";@@ -33,7 +32,8 @@ 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 { TurnUsage } from "./turn-usage";+import { useTurnUsage } from "./use-turn-usage";import { EmptyState } from "./empty-state";import { useThreadPersistence } from "./use-thread-persistence";@@ -91,8 +91,7 @@ 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 { turnUsage, threadUsage, record, reset } = useTurnUsage();const bottomRef = useRef<HTMLDivElement>(null);const abortRef = useRef<AbortController | null>(null);const agentRef = useRef(agent);@@ -126,10 +125,6 @@ export function ChatPanel({const ui = useMemo<HostToolUi>(() => ({ pushCard, updateCard, requestApproval }), [pushCard, updateCard, requestApproval]);const { tools, invoke } = useHostCommands(ui);- const resolve = useCallback((id: string, approve: boolean, remember: boolean) => {- resolveAlfaApproval(id, approve, remember);- }, []);-const stop = useCallback(() => {stopAlfaRunState();abortRef.current?.abort();@@ -199,10 +194,7 @@ export function ChatPanel({ctrl.signal,);historyRef.current = next;- if (usage) {- setTurnUsage(usage);- setThreadUsage((prev) => addUsage(prev, usage));- }+ record(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) {@@ -223,37 +215,20 @@ export function ChatPanel({});}},- [tools, invoke, api.mode, persist, setMessages],+ [tools, invoke, api.mode, persist, record, setMessages],);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// the same approval card. This keeps automation safety DRY with ordinary chat.useImperativeHandle(ref, () => ({- runSteps(auto, runAgent) {- void sendToAlfa(composeAutomationRequest(auto, (runAgent ?? agentRef.current).name));- },+ runSteps: (auto, runAgent) => void sendToAlfa(composeAutomationRequest(auto, (runAgent ?? agentRef.current).name)),stop,- loadThread: handleLoadThread,- newThread: handleNewThread,+ loadThread: (t) => { reset(); loadThread(t); },+ newThread: () => { reset(); newThread(); },}));return (@@ -285,7 +260,7 @@ export function ChatPanel({onContextMenu={(e) => e.stopPropagation()}className={cn("flex flex-col p-4", ios ? "gap-1.5" : "gap-4")}>- <ChatTranscript messages={messages} ios={ios} onResolve={resolve}/>+ <ChatTranscript messages={messages} ios={ios} onResolve={(id, approve, remember) => resolveAlfaApproval(id, approve, remember)}/><div ref={bottomRef} /></div></ScrollArea>diff --git a/frontend/slices/assistant/components/use-turn-usage.ts b/frontend/slices/assistant/components/use-turn-usage.tsnew file mode 100644index 00000000..f80192e6--- /dev/null+++ b/frontend/slices/assistant/components/use-turn-usage.ts@@ -0,0 +1,23 @@+"use client";++import { useCallback, useState } from "react";+import { addUsage, type ProviderUsage } from "./turn-usage";++// Per-turn and per-thread token accounting for the Alfa chat. Extracted from+// ChatPanel (which was tripping max-lines) so the panel stays about the run loop+// and this state has one home: `record` folds each turn's usage into the thread+// total, `reset` clears both when the conversation switches.+export function useTurnUsage() {+ const [turnUsage, setTurnUsage] = useState<ProviderUsage | null>(null);+ const [threadUsage, setThreadUsage] = useState<ProviderUsage | null>(null);+ const record = useCallback((usage?: ProviderUsage | null) => {+ if (!usage) return;+ setTurnUsage(usage);+ setThreadUsage((prev) => addUsage(prev, usage));+ }, []);+ const reset = useCallback(() => {+ setTurnUsage(null);+ setThreadUsage(null);+ }, []);+ return { turnUsage, threadUsage, record, reset };+}