using System; using System.Collections.Generic; using AxCopilot.Models; namespace AxCopilot.Services; public static class TokenEstimator { public static int Estimate(string text) { if (string.IsNullOrEmpty(text)) { return 0; } int num = 0; int length = text.Length; foreach (char c in text) { if (c >= '가' && c <= '힣') { num++; } else if (c >= '\u3000' && c <= '鿿') { num++; } } double num2 = ((length > 0) ? ((double)num / (double)length) : 0.0); double num3 = 4.0 - num2 * 2.0; return Math.Max(1, (int)((double)length / num3)); } public static int EstimateMessages(IEnumerable messages) { int num = 0; foreach (ChatMessage message in messages) { num += Estimate(message.Content) + 4; } return num; } public static (double InputCost, double OutputCost) EstimateCost(int promptTokens, int completionTokens, string service, string model) { var (num, num2) = GetPricing(service, model); return (InputCost: (double)promptTokens / 1000000.0 * num, OutputCost: (double)completionTokens / 1000000.0 * num2); } public static (double Input, double Output) GetPricing(string service, string model) { string text = (service + ":" + model).ToLowerInvariant(); string text2 = text; if (1 == 0) { } (double, double) result = ((!text.Contains("claude-opus")) ? ((!text.Contains("claude-sonnet")) ? ((!text.Contains("claude-haiku")) ? ((!text.Contains("gemini-2.5-pro")) ? ((!text.Contains("gemini-2.5-flash")) ? ((!text.Contains("gemini-2.0")) ? ((!text.Contains("ollama")) ? ((!text.Contains("vllm")) ? (1.0, 3.0) : (0.0, 0.0)) : (0.0, 0.0)) : (0.1, 0.4)) : (0.15, 0.6)) : (1.25, 10.0)) : (0.25, 1.25)) : (3.0, 15.0)) : (15.0, 75.0)); if (1 == 0) { } return result; } public static string Format(int count) { if (1 == 0) { } string result = ((count >= 1000000) ? $"{(double)count / 1000000.0:0.#}M" : ((count < 1000) ? count.ToString() : $"{(double)count / 1000.0:0.#}k")); if (1 == 0) { } return result; } public static string FormatCost(double usd) { if (1 == 0) { } string result = ((usd == 0.0) ? "무료" : ((usd < 0.01) ? $"${usd:F4}" : ((!(usd < 1.0)) ? $"${usd:F2}" : $"${usd:F3}"))); if (1 == 0) { } return result; } public static double GetContextUsage(int currentTokens, int maxContextTokens) { if (maxContextTokens <= 0) { return 0.0; } return Math.Min(1.0, (double)currentTokens / (double)maxContextTokens); } }