UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.39 kB
import{t as isLocalProviderEndpoint}from"./provider-attribution-JD1bPk3I.mjs";import{r as resolveConfiguredContextTokenLimits}from"./context-resolution-COMl6YxU.mjs"; //!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}function resolveContextWindowInfo(e){const t=resolveConfiguredContextTokenLimits({cfg:e.cfg,provider:e.provider,model:e.modelId},normalizePositiveInt);const n=normalizePositiveInt(e.modelContextTokens);const r=t.configuredContextWindow;const i=t.effectiveConfiguredTokens??(r!==void 0&&(n===null||n>=r)?r:void 0);const a=normalizePositiveInt(t.fixedContextWindow)??(e.modelContextWindowSource===`synthetic`?null:normalizePositiveInt(e.modelContextWindow));const o=n&&a?Math.min(n,a):n??a;const s=e.modelContextWindowSource===`synthetic`?normalizePositiveInt(e.modelContextWindow):null;const c=normalizePositiveInt(e.defaultTokens)??CONTEXT_WINDOW_WARN_BELOW_TOKENS;const l=i?{tokens:i,source:`modelsConfig`}:o?{tokens:o,source:`model`}:{tokens:s??c,source:`default`};const u=e.modelServingContextWindow;return u!==void 0&&u<=l.tokens?{tokens:u,source:`model`}:l}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))}}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`}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.`}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}}export{resolveContextWindowInfo as a,formatContextWindowWarningMessage as i,evaluateContextWindowGuard as n,formatContextWindowBlockMessage as r,CONTEXT_WINDOW_HARD_MIN_TOKENS as t};