The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(agent,code): show REAL consumed context in agent + code modes (was estimate)
purpledf2151e94c33
diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.tsindex a7e674c9..da6d8c9f 100644--- a/src/api/providers/openai-provider.ts+++ b/src/api/providers/openai-provider.ts@@ -284,7 +284,7 @@ export class OpenAIProvider implements ProviderClient {messages: ChatMessage[],tools: ToolDefinition[],options?: ChatOptions,- ): Promise<{ content: string; toolCalls: ToolCall[] }> {+ ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number }> {const body: Record<string, any> = {model,messages: messages.map(m => this.toOpenAIMessage(m)),@@ -337,9 +337,13 @@ export class OpenAIProvider implements ProviderClient {},}))+ // Real consumed-context usage (non-streaming response carries it directly).+ const usage = (data as { usage?: { prompt_tokens?: number; completion_tokens?: number } }).usagereturn {content: choice?.message?.content || '',toolCalls,+ promptEvalCount: usage?.prompt_tokens,+ evalCount: usage?.completion_tokens,}}diff --git a/src/api/providers/types.ts b/src/api/providers/types.tsindex 4b29389c..c8e87257 100644--- a/src/api/providers/types.ts+++ b/src/api/providers/types.ts@@ -152,13 +152,15 @@ export interface ProviderClient {options?: ChatOptions): AsyncGenerator<ChatStreamChunk>- /** Non-streaming chat with tool calling support. */+ /** Non-streaming chat with tool calling support. The optional+ * promptEvalCount/evalCount carry real token usage (consumed context) so+ * the agent/code TokenCounter can show the true fill, not a char/4 estimate. */chatWithTools(model: string,messages: ChatMessage[],tools: ToolDefinition[],options?: ChatOptions- ): Promise<{ content: string; toolCalls: ToolCall[] }>+ ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number }>/** List available models from this provider. */listModels(): Promise<ProviderModel[]>diff --git a/src/components/chat/CodexView.tsx b/src/components/chat/CodexView.tsxindex b3a4a0c9..963f6471 100644--- a/src/components/chat/CodexView.tsx+++ b/src/components/chat/CodexView.tsx@@ -6,6 +6,7 @@ import { ToolCallBlock } from './ToolCallBlock'import { ThinkingBlock } from './ThinkingBlock'import { MarkdownRenderer } from './MarkdownRenderer'import { TokenCounter } from './TokenCounter'+import { ContextDropdown } from './ContextDropdown'import { MemoryDebugToggle } from './MemoryDebugPanel'import { RealtimeCounter } from './RealtimeCounter'import { PluginsDropdown } from './PluginsDropdown'@@ -169,6 +170,7 @@ export function CodexView() {<span>New</span></button><TokenCounter />+ <ContextDropdown /><MemoryDebugToggle /><PluginsDropdown /></div>diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.tsindex 9ce05fee..e1d1d33c 100644--- a/src/hooks/useAgentChat.ts+++ b/src/hooks/useAgentChat.ts@@ -572,7 +572,7 @@ export function useAgentChat() {function: { name: t.name, description: t.description, parameters: t.inputSchema },}))- let turn!: { content: string; toolCalls: ToolCall[]; thinking?: string }+ let turn!: { content: string; toolCalls: ToolCall[]; thinking?: string; promptEvalCount?: number; evalCount?: number }if (providerId === 'ollama') {// Streaming path — parity with desktop Codex. Without this// the user stared at a frozen chat for 30-90 s while Gemma@@ -676,6 +676,18 @@ export function useAgentChat() {toolCalls = turn.toolCallsturnContent = turn.content || ''+ // Real consumed-context usage for THIS turn (system + tools + RAG ++ // history + input). The agent loop runs multiple model calls; the+ // latest one has the fullest prompt, so storing each turn (last wins)+ // keeps the TokenCounter on the true current fill instead of a char/4+ // estimate. Ollama reports it natively; openai providers via usage.+ if (turn.promptEvalCount || turn.evalCount) {+ useChatStore.getState().updateMessageUsage(convId!, assistantMessage.id, {+ promptTokens: turn.promptEvalCount || 0,+ completionTokens: turn.evalCount || 0,+ totalTokens: (turn.promptEvalCount || 0) + (turn.evalCount || 0),+ })+ }// Native thinking field from Ollamaif ((turn as any).thinking) turnThinking = (turn as any).thinkingdiff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.tsindex 7be61aa7..ff04585a 100644--- a/src/hooks/useCodex.ts+++ b/src/hooks/useCodex.ts@@ -603,7 +603,7 @@ export function useCodex() {// updates so the "Hello, I am the Coding Agent…" line never// lands in the chat. The post-stream echoDetected branch then drops// the buffer entirely and forces a silent retry.- let turn: { content: string; toolCalls: ToolCall[]; thinking: string }+ let turn: { content: string; toolCalls: ToolCall[]; thinking: string; promptEvalCount?: number; evalCount?: number }const liveContent = (c: string) => {if (echoRetriesRemaining > 0 && isSystemPromptEcho(c)) returnuseChatStore.getState().updateMessageContent(convId!, assistantMsg.id, fullContent ? fullContent + '\n\n' + c : c)@@ -643,6 +643,17 @@ export function useCodex() {}toolCalls = turn.toolCallsturnContent = turn.content || ''+ // Real consumed-context usage for THIS coding turn (system + tools ++ // RAG + file context + history). Multiple model calls run per task;+ // the latest has the fullest prompt, so store each (last wins) to+ // keep the TokenCounter on the true fill, not a char/4 estimate.+ if (turn.promptEvalCount || turn.evalCount) {+ useChatStore.getState().updateMessageUsage(convId!, assistantMsg.id, {+ promptTokens: turn.promptEvalCount || 0,+ completionTokens: turn.evalCount || 0,+ totalTokens: (turn.promptEvalCount || 0) + (turn.evalCount || 0),+ })+ }void diagLog('streamWithTools-return', {iter: i,toolCallsCount: toolCalls.length,diff --git a/src/lib/ollama-stream-tools.ts b/src/lib/ollama-stream-tools.tsindex 9f44abee..9fad63ef 100644--- a/src/lib/ollama-stream-tools.ts+++ b/src/lib/ollama-stream-tools.ts@@ -30,7 +30,7 @@ export async function streamOllamaChatWithTools(options: { temperature?: number; thinking?: boolean; maxTokens?: number; contextWindow?: number; signal?: AbortSignal },onContent: (content: string) => void,onThinking: (thinking: string) => void,-): Promise<{ content: string; toolCalls: ToolCall[]; thinking: string }> {+): Promise<{ content: string; toolCalls: ToolCall[]; thinking: string; promptEvalCount: number; evalCount: number }> {const ollamaMessages = messages.map((m) => {const msg: Record<string, any> = { role: m.role, content: m.content }if (m.tool_calls) msg.tool_calls = m.tool_calls@@ -91,6 +91,12 @@ export async function streamOllamaChatWithTools(let content = ''let thinking = ''let toolCalls: ToolCall[] = []+ // Real token usage from the final Ollama chunk (top-level, not in `message`).+ // prompt_eval_count is the FULL consumed context (system + tools + RAG ++ // history) for THIS turn — the agent/code loop stores the latest so the+ // TokenCounter shows 100% real usage instead of a char/4 estimate.+ let promptEvalCount = 0+ let evalCount = 0while (true) {if (options.signal?.aborted) {@@ -130,6 +136,8 @@ export async function streamOllamaChatWithTools(]}}+ if (typeof j.prompt_eval_count === 'number') promptEvalCount = j.prompt_eval_count+ if (typeof j.eval_count === 'number') evalCount = j.eval_count} catch {// partial JSON line — skip}@@ -158,10 +166,12 @@ export async function streamOllamaChatWithTools(thinking += j.message.thinkingonThinking(thinking)}+ if (typeof j.prompt_eval_count === 'number') promptEvalCount = j.prompt_eval_count+ if (typeof j.eval_count === 'number') evalCount = j.eval_count} catch {// ignore tail-buffer parse errors}}- return { content, toolCalls, thinking }+ return { content, toolCalls, thinking, promptEvalCount, evalCount }}