52 lines
2.4 KiB
TypeScript
52 lines
2.4 KiB
TypeScript
import type { AssistantMessage } from "../types.js";
|
|
/**
|
|
* Check if an assistant message represents a context overflow error.
|
|
*
|
|
* This handles two cases:
|
|
* 1. Error-based overflow: Most providers return stopReason "error" with a
|
|
* specific error message pattern.
|
|
* 2. Silent overflow: Some providers accept overflow requests and return
|
|
* successfully. For these, we check if usage.input exceeds the context window.
|
|
*
|
|
* ## Reliability by Provider
|
|
*
|
|
* **Reliable detection (returns error with detectable message):**
|
|
* - Anthropic: "prompt is too long: X tokens > Y maximum"
|
|
* - OpenAI (Completions & Responses): "exceeds the context window"
|
|
* - Google Gemini: "input token count exceeds the maximum"
|
|
* - xAI (Grok): "maximum prompt length is X but request contains Y"
|
|
* - Groq: "reduce the length of the messages"
|
|
* - Cerebras: 400/413 status code (no body)
|
|
* - Mistral: 400/413 status code (no body)
|
|
* - OpenRouter (all backends): "maximum context length is X tokens"
|
|
* - llama.cpp: "exceeds the available context size"
|
|
* - LM Studio: "greater than the context length"
|
|
* - Kimi For Coding: "exceeded model token limit: X (requested: Y)"
|
|
*
|
|
* **Unreliable detection:**
|
|
* - z.ai: Sometimes accepts overflow silently (detectable via usage.input > contextWindow),
|
|
* sometimes returns rate limit errors. Pass contextWindow param to detect silent overflow.
|
|
* - Ollama: Silently truncates input without error. Cannot be detected via this function.
|
|
* The response will have usage.input < expected, but we don't know the expected value.
|
|
*
|
|
* ## Custom Providers
|
|
*
|
|
* If you've added custom models via settings.json, this function may not detect
|
|
* overflow errors from those providers. To add support:
|
|
*
|
|
* 1. Send a request that exceeds the model's context window
|
|
* 2. Check the errorMessage in the response
|
|
* 3. Create a regex pattern that matches the error
|
|
* 4. The pattern should be added to OVERFLOW_PATTERNS in this file, or
|
|
* check the errorMessage yourself before calling this function
|
|
*
|
|
* @param message - The assistant message to check
|
|
* @param contextWindow - Optional context window size for detecting silent overflow (z.ai)
|
|
* @returns true if the message indicates a context overflow
|
|
*/
|
|
export declare function isContextOverflow(message: AssistantMessage, contextWindow?: number): boolean;
|
|
/**
|
|
* Get the overflow patterns for testing purposes.
|
|
*/
|
|
export declare function getOverflowPatterns(): RegExp[];
|
|
//# sourceMappingURL=overflow.d.ts.map
|