这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 11 additions & 4 deletions src/agent/grok-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ui/components/loading-spinner.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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>
);
Expand Down
17 changes: 17 additions & 0 deletions src/utils/token-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading