diff --git a/extensions/cli/src/services/ChatHistoryService.ts b/extensions/cli/src/services/ChatHistoryService.ts index 0ebc9de50..6fbc24e04 100644 --- a/extensions/cli/src/services/ChatHistoryService.ts +++ b/extensions/cli/src/services/ChatHistoryService.ts @@ -124,7 +124,11 @@ export class ChatHistoryService extends BaseService { /** * Add an assistant message to the history */ - addAssistantMessage(content: string, toolCalls?: any[]): ChatHistoryItem { + addAssistantMessage( + content: string, + toolCalls?: any[], + usage?: any, + ): ChatHistoryItem { const message: any = { role: "assistant", content, @@ -134,6 +138,10 @@ export class ChatHistoryService extends BaseService { message.toolCalls = toolCalls; } + if (usage) { + message.usage = usage; + } + const toolCallStates = toolCalls?.map((tc) => { const id = tc.id; const name = tc.function?.name ?? tc.name; diff --git a/extensions/cli/src/stream/handleToolCalls.ts b/extensions/cli/src/stream/handleToolCalls.ts index c4df3af65..2b3a020ba 100644 --- a/extensions/cli/src/stream/handleToolCalls.ts +++ b/extensions/cli/src/stream/handleToolCalls.ts @@ -30,6 +30,7 @@ export async function handleToolCalls( content: string, callbacks: StreamCallbacks | undefined, isHeadless: boolean, + usage?: any, ): Promise { const chatHistorySvc = services.chatHistory; const useService = @@ -38,15 +39,17 @@ export async function handleToolCalls( if (content) { if (useService) { // Service-driven: write assistant message via service - chatHistorySvc.addAssistantMessage(content); + chatHistorySvc.addAssistantMessage(content, undefined, usage); } else { // Fallback only when service is unavailable - chatHistory.push( - createHistoryItem({ - role: "assistant", - content, - }), - ); + const message: any = { + role: "assistant", + content, + }; + if (usage) { + message.usage = usage; + } + chatHistory.push(createHistoryItem(message)); } } return false; @@ -86,10 +89,14 @@ export async function handleToolCalls( chatHistorySvc.addAssistantMessage( assistantMessage.content || "", assistantMessage.toolCalls, + usage, ); } else { // Fallback only when service is unavailable - chatHistory.push(createHistoryItem(assistantMessage, [], toolCallStates)); + const messageWithUsage = usage + ? { ...assistantMessage, usage } + : assistantMessage; + chatHistory.push(createHistoryItem(messageWithUsage, [], toolCallStates)); } // First preprocess the tool calls diff --git a/extensions/cli/src/stream/streamChatResponse.ts b/extensions/cli/src/stream/streamChatResponse.ts index 098ff5a38..a68bf309c 100644 --- a/extensions/cli/src/stream/streamChatResponse.ts +++ b/extensions/cli/src/stream/streamChatResponse.ts @@ -201,6 +201,7 @@ export async function processStreamingResponse( finalContent: string; // Added field for final content only toolCalls: ToolCall[]; shouldContinue: boolean; + usage?: any; }> { const { model, @@ -344,6 +345,12 @@ export async function processStreamingResponse( }); const totalDuration = responseEndTime - requestStartTime; + // Enhance fullUsage with model and cost for saving to session + if (fullUsage) { + fullUsage.model = model.model; + fullUsage.cost_cents = Math.round(cost * 100); // Convert dollars to cents + } + logger.debug("Stream complete", { chunkCount, responseLength: aiResponse.length, @@ -382,6 +389,7 @@ export async function processStreamingResponse( finalContent: aiResponse, toolCalls: [], shouldContinue: false, + usage: fullUsage, }; } @@ -418,6 +426,7 @@ export async function processStreamingResponse( finalContent: finalContent, toolCalls: validToolCalls, shouldContinue: validToolCalls.length > 0, + usage: fullUsage, }; } @@ -478,7 +487,7 @@ export async function streamChatResponse( }); // Get response from LLM - const { content, toolCalls, shouldContinue } = + const { content, toolCalls, shouldContinue, usage } = await processStreamingResponse({ isHeadless, chatHistory, @@ -514,6 +523,7 @@ export async function streamChatResponse( content, callbacks, isHeadless, + usage, ); if (shouldReturn) {