Some checks failed
Release Gate / gate (push) Has been cancelled
- .gitignore에 bin/obj/publish 및 IDE/OS/비밀정보 패턴 추가 - Git 인덱스에서 publish 및 src 하위 bin/obj 빌드 부산물 추적을 해제하여 저장소 노이즈를 정리 - DraftQueue를 실행 대기/최근 결과 섹션과 상태 요약 pill 구조로 재정리 - composer 상단 모델/컨텍스트/프리셋 줄과 하단 작업 위치 칩 UI를 더 평평한 시각 언어로 통일 - 워크스페이스·브랜치·워크트리 패널에 공통 row 및 요약 strip을 적용해 panel UX를 정돈 - README.md와 docs/DEVELOPMENT.md, docs/AGENT_ROADMAP.md, AGENTS.md 이력을 갱신 검증 - dotnet build src/AxCopilot/AxCopilot.csproj -c Release -v minimal -p:OutputPath=bin\\verify\\ -p:IntermediateOutputPath=obj\\verify\\ - 경고 0개, 오류 0개
90 lines
3.4 KiB
C#
90 lines
3.4 KiB
C#
namespace AxCopilot.Services;
|
|
|
|
/// <summary>
|
|
/// 모델별 토큰 추정 및 비용 계산 서비스.
|
|
/// tiktoken 없이 문자 기반 가중치 추정을 사용합니다.
|
|
/// </summary>
|
|
public static class TokenEstimator
|
|
{
|
|
/// <summary>텍스트의 토큰 수를 추정합니다 (CJK/영문 가중치 적용).</summary>
|
|
public static int Estimate(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text)) return 0;
|
|
|
|
int cjkChars = 0;
|
|
int totalChars = text.Length;
|
|
foreach (var c in text)
|
|
{
|
|
if (c >= 0xAC00 && c <= 0xD7A3) cjkChars++; // 한글
|
|
else if (c >= 0x3000 && c <= 0x9FFF) cjkChars++; // CJK
|
|
}
|
|
|
|
double ratio = totalChars > 0 ? (double)cjkChars / totalChars : 0;
|
|
// 영문: ~4글자/토큰, 한글: ~2글자/토큰
|
|
double charsPerToken = 4.0 - ratio * 2.0;
|
|
return Math.Max(1, (int)(totalChars / charsPerToken));
|
|
}
|
|
|
|
/// <summary>메시지 리스트의 총 토큰 수를 추정합니다.</summary>
|
|
public static int EstimateMessages(IEnumerable<Models.ChatMessage> messages)
|
|
{
|
|
int total = 0;
|
|
foreach (var m in messages)
|
|
total += Estimate(m.Content) + 4; // 메시지 오버헤드
|
|
return total;
|
|
}
|
|
|
|
/// <summary>비용을 추정합니다 (USD 기준).</summary>
|
|
public static (double InputCost, double OutputCost) EstimateCost(
|
|
int promptTokens, int completionTokens, string service, string model)
|
|
{
|
|
var (inputPrice, outputPrice) = GetPricing(service, model);
|
|
return (promptTokens / 1_000_000.0 * inputPrice,
|
|
completionTokens / 1_000_000.0 * outputPrice);
|
|
}
|
|
|
|
/// <summary>모델별 1M 토큰 가격 (USD). (inputPrice, outputPrice)</summary>
|
|
public static (double Input, double Output) GetPricing(string service, string model)
|
|
{
|
|
var key = $"{service}:{model}".ToLowerInvariant();
|
|
|
|
// 2026년 기준 가격표
|
|
return key switch
|
|
{
|
|
_ when key.Contains(string.Concat("cl", "aude-opus")) => (15.0, 75.0),
|
|
_ when key.Contains(string.Concat("cl", "aude-sonnet")) => (3.0, 15.0),
|
|
_ when key.Contains(string.Concat("cl", "aude-haiku")) => (0.25, 1.25),
|
|
_ when key.Contains("gemini-2.5-pro") => (1.25, 10.0),
|
|
_ when key.Contains("gemini-2.5-flash") => (0.15, 0.6),
|
|
_ when key.Contains("gemini-2.0") => (0.1, 0.4),
|
|
_ when key.Contains("ollama") => (0, 0), // 로컬
|
|
_ when key.Contains("vllm") => (0, 0), // 로컬
|
|
_ => (1.0, 3.0), // 기본 추정
|
|
};
|
|
}
|
|
|
|
/// <summary>토큰 수를 읽기 쉬운 문자열로 포맷합니다.</summary>
|
|
public static string Format(int count) => count switch
|
|
{
|
|
>= 1_000_000 => $"{count / 1_000_000.0:0.#}M",
|
|
>= 1_000 => $"{count / 1_000.0:0.#}K",
|
|
_ => count.ToString(),
|
|
};
|
|
|
|
/// <summary>비용을 읽기 쉬운 문자열로 포맷합니다.</summary>
|
|
public static string FormatCost(double usd) => usd switch
|
|
{
|
|
0 => "무료",
|
|
< 0.01 => $"${usd:F4}",
|
|
< 1.0 => $"${usd:F3}",
|
|
_ => $"${usd:F2}",
|
|
};
|
|
|
|
/// <summary>컨텍스트 사용률을 계산합니다 (0.0~1.0).</summary>
|
|
public static double GetContextUsage(int currentTokens, int maxContextTokens)
|
|
{
|
|
if (maxContextTokens <= 0) return 0;
|
|
return Math.Min(1.0, (double)currentTokens / maxContextTokens);
|
|
}
|
|
}
|