openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
81 lines (80 loc) • 4.04 kB
JavaScript
import { r as resolveConfiguredContextTokenLimits } from "./context-resolution-DOFMNmJZ.mjs";
import { t as isLocalProviderEndpoint } from "./provider-attribution-DJbzqKsf.mjs";
//#region src/agents/context-window-guard.ts
const CONTEXT_WINDOW_HARD_MIN_TOKENS = 4e3;
const CONTEXT_WINDOW_WARN_BELOW_TOKENS = 8e3;
const CONTEXT_WINDOW_HARD_MIN_RATIO = .1;
const CONTEXT_WINDOW_WARN_BELOW_RATIO = .2;
function normalizePositiveInt(e) {
if (typeof e !== "number" || !Number.isFinite(e)) return null;
const t = Math.floor(e);
return t > 0 ? t : null;
}
/** Resolve the effective context window and source for one provider/model. */
function resolveContextWindowInfo(e) {
const t = resolveConfiguredContextTokenLimits({
cfg: e.cfg,
provider: e.provider,
model: e.modelId
}, normalizePositiveInt);
const n = t.effectiveConfiguredTokens ?? t.configuredContextWindow;
const r = normalizePositiveInt(e.modelContextTokens);
const i = normalizePositiveInt(t.fixedContextWindow) ?? (e.modelContextWindowSource === "synthetic" ? null : normalizePositiveInt(e.modelContextWindow));
const a = r && i ? Math.min(r, i) : r ?? i;
const o = e.modelContextWindowSource === "synthetic" ? normalizePositiveInt(e.modelContextWindow) : null;
const s = normalizePositiveInt(e.defaultTokens) ?? CONTEXT_WINDOW_WARN_BELOW_TOKENS;
const c = n ? {
tokens: n,
source: "modelsConfig"
} : a ? {
tokens: a,
source: "model"
} : {
tokens: o ?? s,
source: "default"
};
const l = e.modelServingContextWindow;
return l !== void 0 && l <= c.tokens ? {
tokens: l,
source: "model"
} : c;
}
/** Derive warning/block floors from the resolved model context window. */
function resolveContextWindowGuardThresholds(e) {
return {
hardMinTokens: Math.max(CONTEXT_WINDOW_HARD_MIN_TOKENS, Math.floor(e * CONTEXT_WINDOW_HARD_MIN_RATIO)),
warnBelowTokens: Math.max(CONTEXT_WINDOW_WARN_BELOW_TOKENS, Math.floor(e * CONTEXT_WINDOW_WARN_BELOW_RATIO))
};
}
/** Format a non-blocking low-context warning message. */
function formatContextWindowWarningMessage(e) {
const t = `low context window: ${e.provider}/${e.modelId} ctx=${e.guard.tokens} (warn<${e.guard.warnBelowTokens}) source=${e.guard.source}`;
if (!isLocalProviderEndpoint(e.runtimeBaseUrl)) return t;
if (e.guard.source === "modelsConfig") return `${t}; OpenClaw is using the configured model context limit for this model, so raise contextWindow/contextTokens if it is set too low`;
return `${t}; local/self-hosted runs work best at ${e.guard.warnBelowTokens}+ tokens and may show weaker tool use or more compaction until the server/model context limit is raised`;
}
/** Format a blocking context-window guard message. */
function formatContextWindowBlockMessage(e) {
const t = `Model context window too small (${e.guard.tokens} tokens; source=${e.guard.source}). Minimum is ${e.guard.hardMinTokens}.`;
if (!isLocalProviderEndpoint(e.runtimeBaseUrl)) return t;
if (e.guard.source === "modelsConfig") return `${t} OpenClaw is using the configured model context limit for this model. Raise contextWindow/contextTokens or choose a larger model.`;
return `${t} This looks like a local model endpoint. Raise the server/model context limit or choose a larger model. OpenClaw local/self-hosted runs work best at ${e.guard.warnBelowTokens}+ tokens.`;
}
/** Evaluate whether the resolved context window should warn or block. */
function evaluateContextWindowGuard(e) {
const t = normalizePositiveInt(e.info.tokens);
const n = t ?? 0;
const r = resolveContextWindowGuardThresholds(normalizePositiveInt(e.info.referenceTokens) ?? n);
const i = r.warnBelowTokens;
const a = Math.min(r.hardMinTokens, Math.max(n, CONTEXT_WINDOW_HARD_MIN_TOKENS));
return {
...e.info,
tokens: n,
hardMinTokens: a,
warnBelowTokens: i,
shouldWarn: !t || n < i,
shouldBlock: !t || n < a
};
}
//#endregion
export { resolveContextWindowInfo as a, formatContextWindowWarningMessage as i, evaluateContextWindowGuard as n, formatContextWindowBlockMessage as r, CONTEXT_WINDOW_HARD_MIN_TOKENS as t };