xonecas.io
64875
Mc

mini-coder / files

mini-coder / src / tui / usage.ts

1.7 KB
1import type { Api, Message, Model, Tool } from "@earendil-works/pi-ai";
2import { estimateContextTokens as estimate, estimateTextTokens } from "@earendil-works/pi-ai/utils/estimate";
3import { dim, red, yellow } from "./styles.ts";
4
5/**
6 * Rough token count for the context the next request would send, used only for
7 * display. Delegates to `pi-ai`'s estimator, which anchors on the last
8 * provider-reported usage and estimates the messages after it. Before any usage
9 * exists there is no anchor, so the system prompt and tools are added explicitly.
10 */
11export function estimateContextTokens(messages: Message[], systemPrompt: string, tools: Tool[]): number {
12  const tokens = estimate(messages);
13  if (tokens.lastUsageIndex !== null) return tokens.tokens;
14  return tokens.tokens + estimateTextTokens(systemPrompt) + estimateTextTokens(JSON.stringify(tools));
15}
16
17/** `812`, `12.3k`, `1.24M` — trailing zeros trimmed. */
18function formatTokens(n: number): string {
19  if (n < 1000) return String(n);
20  const scaled = n < 1_000_000 ? n / 1000 : n / 1_000_000;
21  const unit = n < 1_000_000 ? "k" : "M";
22  return `${scaled.toFixed(unit === "k" ? 1 : 2).replace(/\.?0+$/, "")}${unit}`;
23}
24
25/**
26 * The context row: dim, yellow as the window nears, red once the next request
27 * could not carry a maximum-size reply.
28 */
29export function contextUsageLine(used: number, model: Model<Api>): string {
30  const percent = (used / model.contextWindow) * 100;
31  const sizes = `${formatTokens(used)}/${formatTokens(model.contextWindow)}`;
32  if (used + model.maxTokens > model.contextWindow) return red(`ctx full · ${sizes}`);
33  const text = `ctx ${sizes} · ${Math.round(percent)}%`;
34  return percent >= 85 ? yellow(text) : dim(text);
35}