@sema-agent/core
Version:
Stateless, task-oriented AI agent core
19 lines (18 loc) • 1.17 kB
JavaScript
import { providerErrorObject } from "./errors.js";
const INPUT_TOO_LONG_ANCHORED_RE = /^(?:prompt is too long|input is too long|this model's maximum context length is |context.{0,3}length|context window (?:limit|exceeded)|reached its context window|too many tokens|maximum context|input length and `max_tokens` exceed context limit|(?:the )?request(?: \([^)]{0,40}\))? exceeds|(?:the )?input token count(?: \([^)]{0,40}\))? exceeds|requested tokens(?: \([^)]{0,40}\))? exceed|your input exceeds|(?:the )?number of tokens in the request exceeds)/i;
const INPUT_TOO_LONG_STATUSES = new Set([400, 413, 422]);
export function startsWithInputTooLongAnchor(diagnostic) {
return INPUT_TOO_LONG_ANCHORED_RE.test(diagnostic.trimStart());
}
export function classifyInputTooLong(status, body) {
if (!INPUT_TOO_LONG_STATUSES.has(status))
return "no";
if (status === 413)
return "yes";
const err = providerErrorObject(body);
if (err === undefined)
return "unknown";
if (err.code === "context_length_exceeded")
return "yes";
return typeof err.message === "string" && startsWithInputTooLongAnchor(err.message) ? "yes" : "no";
}