All files / src/shared status-bar.ts

98.66% Statements 74/75
93.33% Branches 28/30
90% Functions 9/10
98.64% Lines 73/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 16914x 14x 14x               16x 1x   15x 11x   4x                                   14x       14x 14x 14x       12x 9x   3x                 2x 2x 2x 2x 2x 2x       1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x         8x             8x 8x 8x 8x 8x   8x 8x   56x   8x                   8x   8x 8x 8x   8x 8x     8x 8x 8x 8x 8x 8x     8x 8x 8x 8x     8x   8x         8x     8x       8x 8x   8x 1x 7x 1x   6x   8x 8x       1x      
import * as vscode from "vscode";
import { ConfigManager } from "./config";
import {
  PROVIDER_DISPLAY_NAME,
  REFRESH_MODELS_COMMAND_ID,
  STATUS_BAR_DEFAULT_TEXT,
  STATUS_BAR_ERROR_TEXT,
} from "./constants";
 
function formatTokenCount(tokens: number): string {
  if (tokens >= 1000000) {
    return `${(tokens / 1000000).toFixed(1)}M`;
  }
  if (tokens >= 1000) {
    return `${(tokens / 1000).toFixed(1)}k`;
  }
  return String(tokens);
}
 
export interface TokenBreakdown {
  modelName: string;
  systemPrompt: number;
  tools: number;
  userMessages: number;
  assistantMessages: number;
  toolCalls: number;
  toolResults: number;
  images: number;
  actualPromptTokens?: number;
  actualCompletionTokens?: number;
  output?: number;
  contextWindow: number;
}
 
export class StatusBarManager {
  private readonly item: vscode.StatusBarItem;
 
  constructor() {
    this.item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
    this.item.command = REFRESH_MODELS_COMMAND_ID;
    this.item.tooltip = `Click to refresh ${PROVIDER_DISPLAY_NAME} models`;
  }
 
  private refreshVisibility(): void {
    if (!ConfigManager.getUiConfig().showStatusBarItem) {
      this.item.hide();
    } else {
      this.item.show();
    }
  }
 
  public updateVisibility(): void {
    this.refreshVisibility();
  }
 
  showOk(modelCount: number): void {
    this.item.text = `$(zap) ${PROVIDER_DISPLAY_NAME}: ${modelCount} models`;
    this.item.command = REFRESH_MODELS_COMMAND_ID;
    this.item.tooltip = `Click to refresh ${PROVIDER_DISPLAY_NAME} models`;
    this.item.color = undefined;
    this.item.backgroundColor = undefined;
    this.refreshVisibility();
  }
 
  showRefreshing(): void {
    this.item.text = STATUS_BAR_DEFAULT_TEXT;
    this.item.command = REFRESH_MODELS_COMMAND_ID;
    this.item.tooltip = `Refreshing ${PROVIDER_DISPLAY_NAME} models...`;
    this.item.color = undefined;
    this.item.backgroundColor = undefined;
    this.refreshVisibility();
  }
 
  showError(message: string): void {
    this.item.text = STATUS_BAR_ERROR_TEXT;
    this.item.command = REFRESH_MODELS_COMMAND_ID;
    this.item.tooltip = `${PROVIDER_DISPLAY_NAME} Error: ${message}`;
    this.item.color = undefined;
    this.item.backgroundColor = undefined;
    this.refreshVisibility();
  }
 
  showTokenBreakdown(breakdown: TokenBreakdown): void {
    const estimatedInput =
      breakdown.systemPrompt +
      breakdown.tools +
      breakdown.userMessages +
      breakdown.assistantMessages +
      breakdown.toolCalls +
      breakdown.toolResults +
      breakdown.images;
    const inputTokens = breakdown.actualPromptTokens ?? estimatedInput;
    const outputTokens = breakdown.actualCompletionTokens ?? breakdown.output;
    const totalTokens = outputTokens === undefined ? undefined : inputTokens + outputTokens;
    const contextWindow = breakdown.contextWindow;
    const percentage = contextWindow > 0 ? (inputTokens / contextWindow) * 100 : 0;
 
    const hasActual = breakdown.actualPromptTokens !== undefined;
    const scaleFactor = hasActual && estimatedInput > 0 ? inputTokens / estimatedInput : 1;
 
    const scale = (value: number): number => Math.round(value * scaleFactor);
 
    const cat = {
      systemPrompt: scale(breakdown.systemPrompt),
      tools: scale(breakdown.tools),
      userMessages: scale(breakdown.userMessages),
      assistantMessages: scale(breakdown.assistantMessages),
      toolCalls: scale(breakdown.toolCalls),
      toolResults: scale(breakdown.toolResults),
      images: scale(breakdown.images),
    };
 
    this.item.text = `$(zap) ${breakdown.modelName}: ${formatTokenCount(inputTokens)}/${formatTokenCount(contextWindow)}`;
 
    const md = new vscode.MarkdownString();
    md.isTrusted = true;
    md.supportThemeIcons = true;
 
    md.appendMarkdown(`**$(zap) ${PROVIDER_DISPLAY_NAME} — ${breakdown.modelName}**\n\n`);
    md.appendMarkdown(
      `**${inputTokens.toLocaleString()} / ${contextWindow.toLocaleString()} tokens** (${percentage.toFixed(1)}%)\n\n`,
    );
    md.appendMarkdown(`| Category | Tokens |\n`);
    md.appendMarkdown(`|---|---|\n`);
    md.appendMarkdown(`| System Prompt (estimated) | ${cat.systemPrompt.toLocaleString()} |\n`);
    md.appendMarkdown(`| Tools (definitions, estimated) | ${cat.tools.toLocaleString()} |\n`);
    md.appendMarkdown(`| User Messages (estimated) | ${cat.userMessages.toLocaleString()} |\n`);
    md.appendMarkdown(
      `| Assistant Messages (estimated) | ${cat.assistantMessages.toLocaleString()} |\n`,
    );
    md.appendMarkdown(`| Tool Calls (estimated) | ${cat.toolCalls.toLocaleString()} |\n`);
    md.appendMarkdown(`| Tool Results (estimated) | ${cat.toolResults.toLocaleString()} |\n`);
    md.appendMarkdown(`| Images / Media (estimated) | ${cat.images.toLocaleString()} |\n`);
    md.appendMarkdown(
      `| **Input Total${hasActual ? " *(actual)*" : ""}** | **${inputTokens.toLocaleString()}** |\n`,
    );
    const outputIsActual = breakdown.actualCompletionTokens !== undefined;
    const outputLabel =
      outputTokens === undefined
        ? "Output (completion)"
        : outputIsActual
          ? "Output (completion)"
          : "Output (completion) (estimated)";
    md.appendMarkdown(
      `| ${outputLabel} | ${outputTokens === undefined ? "Not reported" : outputTokens.toLocaleString()} |\n`,
    );
    md.appendMarkdown(
      `| **Total Used** | **${totalTokens === undefined ? "Not available" : totalTokens.toLocaleString()}** |\n`,
    );
 
    this.item.tooltip = md;
    this.item.command = REFRESH_MODELS_COMMAND_ID;
 
    if (percentage >= 95) {
      this.item.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
    } else if (percentage >= 80) {
      this.item.backgroundColor = new vscode.ThemeColor("statusBarItem.warningBackground");
    } else {
      this.item.backgroundColor = undefined;
    }
    this.item.color = undefined;
    this.refreshVisibility();
  }
 
  dispose(): void {
    this.item.dispose();
  }
}