fix issues with token counting (#33)

This commit is contained in:
Ismail Pelaseyed authored and GitHub committed 2025-07-23 14:54:47 +02:00
1 parent f020b1d4d8
commit 00f8d113df
4 files changed
+31 -6

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@vibe-kit/grok-cli",
"version": "0.0.5",
"version": "0.0.6",
"description": "An open-source AI agent that brings the power of Grok directly into your terminal.",
"main": "dist/index.js",
"bin": {
+11 -4
View File
@@ -325,7 +325,7 @@ Current working directory: ${process.cwd()}`,
this.messages.push({ role: "user", content: message });
// Calculate input tokens
const inputTokens = this.tokenCounter.countMessageTokens(
let inputTokens = this.tokenCounter.countMessageTokens(
this.messages as any
);
yield {
@@ -396,9 +396,9 @@ Current working directory: ${process.cwd()}`,
if (chunk.choices[0].delta?.content) {
accumulatedContent += chunk.choices[0].delta.content;
// Update token count in real-time
const currentOutputTokens =
this.tokenCounter.estimateStreamingTokens(accumulatedContent);
// Update token count in real-time including accumulated content and any tool calls
const currentOutputTokens = this.tokenCounter.estimateStreamingTokens(accumulatedContent) +
(accumulatedMessage.tool_calls ? this.tokenCounter.countTokens(JSON.stringify(accumulatedMessage.tool_calls)) : 0);
totalOutputTokens = currentOutputTokens;
yield {
@@ -483,6 +483,13 @@ Current working directory: ${process.cwd()}`,
});
}
// Update token count after processing all tool calls to include tool results
inputTokens = this.tokenCounter.countMessageTokens(this.messages as any);
yield {
type: "token_count",
tokenCount: inputTokens + totalOutputTokens,
};
// Continue the loop to get the next response (which might have more tool calls)
} else {
// No tool calls, we're done
+2 -1
View File
@@ -1,5 +1,6 @@
import React, { useState, useEffect } from "react";
import { Box, Text } from "ink";
import { formatTokenCount } from "../../utils/token-counter";
interface LoadingSpinnerProps {
isActive: boolean;
@@ -61,7 +62,7 @@ export function LoadingSpinner({ isActive, processingTime, tokenCount }: Loading
{spinnerFrames[spinnerFrame]} {loadingTexts[loadingTextIndex]}{" "}
</Text>
<Text color="gray">
({processingTime}s · ↑ {tokenCount} tokens · esc to interrupt)
({processingTime}s · ↑ {formatTokenCount(tokenCount)} tokens · esc to interrupt)
</Text>
</Box>
);
+17
View File
@@ -66,6 +66,23 @@ export class TokenCounter {
}
}
/**
* Format token count for display (e.g., 1.2k for 1200)
*/
export function formatTokenCount(count: number): string {
if (count <= 999) {
return count.toString();
}
if (count < 1_000_000) {
const k = count / 1000;
return k % 1 === 0 ? `${k}k` : `${k.toFixed(1)}k`;
}
const m = count / 1_000_000;
return m % 1 === 0 ? `${m}m` : `${m.toFixed(1)}m`;
}
/**
* Create a token counter instance
*/