mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 16:28:26 +00:00
fix: cap context window resolution (#6187) (thanks @iamEvanYT)
This commit is contained in:
@@ -11,9 +11,7 @@ export type ContextWindowInfo = {
|
||||
};
|
||||
|
||||
function normalizePositiveInt(value: unknown): number | null {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) {
|
||||
return null;
|
||||
}
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) return null;
|
||||
const int = Math.floor(value);
|
||||
return int > 0 ? int : null;
|
||||
}
|
||||
@@ -25,11 +23,6 @@ export function resolveContextWindowInfo(params: {
|
||||
modelContextWindow?: number;
|
||||
defaultTokens: number;
|
||||
}): ContextWindowInfo {
|
||||
const fromModel = normalizePositiveInt(params.modelContextWindow);
|
||||
if (fromModel) {
|
||||
return { tokens: fromModel, source: "model" };
|
||||
}
|
||||
|
||||
const fromModelsConfig = (() => {
|
||||
const providers = params.cfg?.models?.providers as
|
||||
| Record<string, { models?: Array<{ id?: string; contextWindow?: number }> }>
|
||||
@@ -39,16 +32,19 @@ export function resolveContextWindowInfo(params: {
|
||||
const match = models.find((m) => m?.id === params.modelId);
|
||||
return normalizePositiveInt(match?.contextWindow);
|
||||
})();
|
||||
if (fromModelsConfig) {
|
||||
return { tokens: fromModelsConfig, source: "modelsConfig" };
|
||||
const fromModel = normalizePositiveInt(params.modelContextWindow);
|
||||
const baseInfo = fromModelsConfig
|
||||
? { tokens: fromModelsConfig, source: "modelsConfig" as const }
|
||||
: fromModel
|
||||
? { tokens: fromModel, source: "model" as const }
|
||||
: { tokens: Math.floor(params.defaultTokens), source: "default" as const };
|
||||
|
||||
const capTokens = normalizePositiveInt(params.cfg?.agents?.defaults?.contextTokens);
|
||||
if (capTokens && capTokens < baseInfo.tokens) {
|
||||
return { tokens: capTokens, source: "agentContextTokens" };
|
||||
}
|
||||
|
||||
const fromAgentConfig = normalizePositiveInt(params.cfg?.agents?.defaults?.contextTokens);
|
||||
if (fromAgentConfig) {
|
||||
return { tokens: fromAgentConfig, source: "agentContextTokens" };
|
||||
}
|
||||
|
||||
return { tokens: Math.floor(params.defaultTokens), source: "default" };
|
||||
return baseInfo;
|
||||
}
|
||||
|
||||
export type ContextWindowGuardResult = ContextWindowInfo & {
|
||||
|
||||
Reference in New Issue
Block a user