import type { AllowanceMeters, QuotaTier, UsageLimits } from "../domain/quota.js"; const TIER_ORDER: readonly QuotaTier[] = ["high-volume", "free", "balanced", "scarce"]; // Keyed by the exact `tier` value that opencode_list_agents reports per model, // so the reading agent can match the JSON value to its guidance literally. const TIER_GUIDE: Record = { "high-volume": "output ≤ $0.75 per 1M tokens — cheapest per request: exploration, mechanical edits, boilerplate, docs, tests, simple fixes", free: "$0 (free model) — zero budget cost; use freely for high-volume work", balanced: "output $0.75–$3 per 1M tokens — standard features, non-trivial debugging, code review", scarce: "output > $3 per 1M tokens — architecture, cross-cutting refactors, hard debugging; use only for the step that needs it", }; /** * Render the live half of the quota guidance: where the numbers came from and * the real shared allowance every model spends against. * * The per-model numbers deliberately stay OUT of the instructions — * opencode_list_agents reports them per model, keyed by the real model id, * which is the only form the agent can act on. */ export function formatUsageLimits(limits: UsageLimits): string { const lines = [ `OpenCode Go allowance — source: ${limits.source} (checked ${limits.fetchedAt.slice(0, 10)})`, ]; if (limits.plan && limits.allowance) { lines.push(`Plan: ${limits.plan}.`); lines.push(...renderMeters(limits.allowance)); } else { lines.push( `Live meters unavailable: ${limits.allowanceReason ?? "no allowance reported"}.`, "Connect OpenCode Go in opencode (/connect) or set OPENCODE_GO_API_KEY to report the real plan and meters.", ); } lines.push( "One shared dollar allowance across all models — an expensive model drains it in far fewer requests.", ); return lines.join("\n"); } function renderMeters(meters: AllowanceMeters): string[] { const rows: string[] = []; const specs: readonly (readonly [keyof AllowanceMeters, string])[] = [ ["month", "Month"], ["week", "Week"], ["fiveHour", "5h window"], ]; for (const [key, label] of specs) { const window = meters[key]; if (!window) continue; rows.push( `- ${label}: ${usd(window.used)} used of ${usd(window.limit)} (resets ${window.resetsAt})`, ); } return rows; } function usd(value: number): string { return `$${value.toFixed(2)}`; } /** Render the static tier legend, one line per `tier` value list_agents emits. */ export function formatTierGuide(): string { return TIER_ORDER.map((tier) => `- ${tier}: ${TIER_GUIDE[tier]}`).join("\n"); }