This commit is contained in:
2026-08-22 00:41:17 +02:00
parent d06a93f759
commit 9250528442
18 changed files with 48210 additions and 53 deletions
+29 -5
View File
@@ -43,29 +43,53 @@ namespace AnotherReplayReader.Utils
public static int EstimateTokens(string text) =>
(int)Math.Ceiling(AIAnalyze.EstimateTokenCount(text).EstimatedTokenCount * EstimatorSafetyFactor);
/// <summary>请求护栏:超过硬上限返回 Block,超过软预算返回 Warn,否则 null。</summary>
/// <summary>请求护栏:只按 prompt 估算检查(保留给旧调用/测试使用)。</summary>
public static ContextCheckResult CheckPromptUsage(
int estimatedPromptTokens,
AiProvider provider,
AiModel model)
{
return CheckUsage(estimatedPromptTokens, provider, model, includeOutputHeadroom: false);
}
/// <summary>请求护栏:把输出/推理余量也算入总用量。</summary>
public static ContextCheckResult CheckRequestUsage(
int estimatedPromptTokens,
AiProvider provider,
AiModel model)
{
return CheckUsage(estimatedPromptTokens, provider, model, includeOutputHeadroom: true);
}
private static ContextCheckResult CheckUsage(
int estimatedPromptTokens,
AiProvider provider,
AiModel model,
bool includeOutputHeadroom)
{
var headroom = includeOutputHeadroom ? GetOutputHeadroom(provider, model) : 0;
var estimatedTotal = estimatedPromptTokens + headroom;
if (model.ContextLength > 0)
{
var hardLimit = (int)(model.ContextLength * HardUsageRatio);
if (estimatedPromptTokens > hardLimit)
if (estimatedTotal > hardLimit)
{
return new ContextCheckResult(
true,
$"估算输入 {estimatedPromptTokens:N0} token 超过模型上下文 {model.ContextLength:N0} 的 90%,已拒绝发起请求。请改用更长上下文的模型,或缩短操作记录。");
includeOutputHeadroom
? $"估算用量(输入 {estimatedPromptTokens:N0} + 输出余量 {headroom:N0} = {estimatedTotal:N0})超过模型上下文 {model.ContextLength:N0} 的 90%,已拒绝发起请求。请改用更长上下文的模型,或缩短操作记录。"
: $"估算输入 {estimatedPromptTokens:N0} token 超过模型上下文 {model.ContextLength:N0} 的 90%,已拒绝发起请求。请改用更长上下文的模型,或缩短操作记录。");
}
}
var budget = GetContextBudget(model);
if (budget > 0 && estimatedPromptTokens > budget)
if (budget > 0 && estimatedTotal > budget)
{
return new ContextCheckResult(
false,
$"估算输入 {estimatedPromptTokens:N0} token 超过上下文预算 {budget:N0}(可在模型设置中调整),长录像将自动分段,超出部分会被压缩。");
includeOutputHeadroom
? $"估算用量(输入 {estimatedPromptTokens:N0} + 输出余量 {headroom:N0} = {estimatedTotal:N0})超过上下文预算 {budget:N0}(可在模型设置中调整)。"
: $"估算输入 {estimatedPromptTokens:N0} token 超过上下文预算 {budget:N0}(可在模型设置中调整),长录像将自动分段,超出部分会被压缩。");
}
return ContextCheckResult.Ok;
}