chore: remove dead code and unused token counter package

- Delete dead ESC listener code and bubbletea/time imports from agent
- Remove internal/tokens/ package (empty stubs and trivial estimator)
- Inline token estimation into usage_tracker as unexported helper
- Remove unused EstimateAndUpdateUsageFromText dead method
- Remove 9 unsupported provider env var entries from registry
This commit is contained in:
Ed Zynda
2026-02-25 20:35:19 +03:00
parent e62ce679fe
commit 72fa1ff029
7 changed files with 5 additions and 194 deletions
-4
View File
@@ -16,7 +16,6 @@ import (
"github.com/mark3labs/mcphost/internal/hooks"
"github.com/mark3labs/mcphost/internal/models"
"github.com/mark3labs/mcphost/internal/session"
"github.com/mark3labs/mcphost/internal/tokens"
"github.com/mark3labs/mcphost/internal/tools"
"github.com/mark3labs/mcphost/internal/ui"
"github.com/spf13/cobra"
@@ -362,9 +361,6 @@ func runMCPHost(ctx context.Context) error {
}
func runNormalMode(ctx context.Context) error {
// Initialize token counters
tokens.InitializeTokenCounters()
// Validate flag combinations
if quietFlag && promptFlag == "" {
return fmt.Errorf("--quiet flag can only be used with --prompt/-p")
-71
View File
@@ -4,9 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"
tea "charm.land/bubbletea/v2"
"charm.land/fantasy"
"github.com/mark3labs/mcphost/internal/config"
@@ -355,72 +353,3 @@ func (a *Agent) GetModel() fantasy.LanguageModel {
func (a *Agent) Close() error {
return a.toolManager.Close()
}
// escListenerModel is a simple Bubble Tea model for ESC key detection
type escListenerModel struct {
escPressed chan bool
}
func (m escListenerModel) Init() tea.Cmd {
return nil
}
func (m escListenerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg, ok := msg.(tea.KeyPressMsg); ok {
if msg.String() == "esc" {
select {
case m.escPressed <- true:
default:
}
return m, tea.Quit
}
}
return m, nil
}
func (m escListenerModel) View() tea.View {
return tea.NewView("")
}
// listenForESC listens for ESC key press using Bubble Tea and returns true if detected
func (a *Agent) listenForESC(stopChan chan bool, readyChan chan bool) bool {
escPressed := make(chan bool, 1)
model := escListenerModel{
escPressed: escPressed,
}
p := tea.NewProgram(model, tea.WithoutRenderer())
go func() {
if _, err := p.Run(); err != nil {
select {
case escPressed <- false:
default:
}
}
}()
go func() {
time.Sleep(10 * time.Millisecond)
select {
case readyChan <- true:
default:
}
}()
select {
case <-stopChan:
p.Kill()
time.Sleep(50 * time.Millisecond)
return false
case pressed := <-escPressed:
p.Kill()
time.Sleep(50 * time.Millisecond)
return pressed
case <-time.After(30 * time.Second):
p.Kill()
time.Sleep(50 * time.Millisecond)
return false
}
}
-9
View File
@@ -53,15 +53,6 @@ var providerEnvVars = map[string][]string{
"bedrock": {"AWS_ACCESS_KEY_ID"},
"google-vertex-anthropic": {"GOOGLE_APPLICATION_CREDENTIALS"},
"ollama": {},
"mistral": {"MISTRAL_API_KEY"},
"groq": {"GROQ_API_KEY"},
"deepseek": {"DEEPSEEK_API_KEY"},
"xai": {"XAI_API_KEY"},
"fireworks": {"FIREWORKS_API_KEY"},
"together": {"TOGETHER_API_KEY"},
"perplexity": {"PERPLEXITY_API_KEY"},
"alibaba": {"DASHSCOPE_API_KEY"},
"cohere": {"COHERE_API_KEY"},
}
// ModelsRegistry provides validation and information about models.
-16
View File
@@ -1,16 +0,0 @@
// Package tokens provides token counting and estimation functionality for
// various language model providers. It includes utilities for estimating
// token counts in text, as well as provider-specific implementations for
// more accurate token counting.
//
// The package supports multiple approaches to token counting:
// - Quick estimation using character-based heuristics
// - Provider-specific tokenizers for accurate counts
// - Initialization functions for setting up token counters
//
// Token counting is essential for:
// - Managing API rate limits
// - Calculating costs for API usage
// - Ensuring prompts fit within model context windows
// - Optimizing prompt engineering and response handling
package tokens
-24
View File
@@ -1,24 +0,0 @@
package tokens
// EstimateTokens estimates the number of tokens in the given text string.
// It uses a rough approximation of 4 characters per token, which is a common
// heuristic for most language models. This function provides a quick estimation
// without requiring model-specific tokenizers.
//
// The estimation may not be accurate for all models or text types, particularly
// for texts with many special characters, non-English languages, or code snippets.
// For more accurate token counting, use model-specific tokenizers when available.
//
// Parameters:
// - text: The input text string to estimate tokens for
//
// Returns:
// - int: The estimated number of tokens in the text
//
// Example:
//
// count := EstimateTokens("Hello, world!") // Returns approximately 3
func EstimateTokens(text string) int {
// Rough approximation: ~4 characters per token for most models
return len(text) / 4
}
-52
View File
@@ -1,52 +0,0 @@
package tokens
// InitializeTokenCounters registers all available token counters for various
// language model providers. This function should be called during application
// startup to ensure that token counting functionality is available for all
// supported models.
//
// Currently, this function is a placeholder for future provider-specific
// token counter implementations. As new providers are added (OpenAI, Anthropic,
// Google, etc.), their respective token counters will be registered here.
//
// This function does not require any API keys and will only initialize
// counters that can work without authentication.
//
// Example:
//
// func main() {
// tokens.InitializeTokenCounters()
// // Token counting is now available
// }
func InitializeTokenCounters() {
// Future provider-specific counters can be registered here
}
// InitializeTokenCountersWithKeys registers token counters for various language
// model providers using the provided API keys. This function enables more
// accurate token counting by allowing access to provider-specific tokenization
// endpoints or libraries that require authentication.
//
// This function should be called during application startup after API keys
// have been loaded from configuration or environment variables. It will
// initialize token counters for providers where API keys are available,
// enabling precise token counting that matches the provider's actual
// tokenization logic.
//
// The function will silently skip providers for which no API keys are
// configured, allowing the application to continue with partial token
// counting capabilities.
//
// Future implementations will accept provider-specific API keys through
// parameters or read them from a configuration context.
//
// Example:
//
// func main() {
// // Load API keys from environment or config
// tokens.InitializeTokenCountersWithKeys()
// // Provider-specific token counting is now available
// }
func InitializeTokenCountersWithKeys() {
// Future provider-specific counters can be registered here
}
+5 -18
View File
@@ -8,7 +8,6 @@ import (
"image/color"
"github.com/mark3labs/mcphost/internal/models"
"github.com/mark3labs/mcphost/internal/tokens"
)
// UsageStats encapsulates detailed token usage and cost breakdown for a single
@@ -64,13 +63,9 @@ func NewUsageTracker(modelInfo *models.ModelInfo, provider string, width int, is
}
}
// EstimateTokens provides a rough estimate of the number of tokens in the given text.
// This uses a simple heuristic of approximately 4 characters per token, which is a
// reasonable approximation for most models but not precise. Actual token counts may vary
// significantly based on the specific tokenizer used by each model.
func EstimateTokens(text string) int {
// Rough approximation: ~4 characters per token for most models
// This is not accurate but gives a reasonable estimate
// estimateTokens provides a rough estimate of the number of tokens in the given text.
// Uses a simple heuristic of ~4 characters per token.
func estimateTokens(text string) int {
return len(text) / 4
}
@@ -126,16 +121,8 @@ func (ut *UsageTracker) UpdateUsage(inputTokens, outputTokens, cacheReadTokens,
// the usage statistics. This method is used when actual token counts are not available
// from the API response.
func (ut *UsageTracker) EstimateAndUpdateUsage(inputText, outputText string) {
inputTokens := tokens.EstimateTokens(inputText)
outputTokens := tokens.EstimateTokens(outputText)
ut.UpdateUsage(inputTokens, outputTokens, 0, 0)
}
// EstimateAndUpdateUsageFromText is an alias for EstimateAndUpdateUsage, providing
// backward compatibility. It estimates token counts from text and updates usage statistics.
func (ut *UsageTracker) EstimateAndUpdateUsageFromText(inputText, outputText string) {
inputTokens := tokens.EstimateTokens(inputText)
outputTokens := tokens.EstimateTokens(outputText)
inputTokens := estimateTokens(inputText)
outputTokens := estimateTokens(outputText)
ut.UpdateUsage(inputTokens, outputTokens, 0, 0)
}