provider-error.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Schema } from "effect"
  2. import { LLMError, ProviderErrorEvent } from "./schema"
  3. const patterns = [
  4. /prompt is too long/i,
  5. /input is too long for requested model/i,
  6. /exceeds the context window/i,
  7. /input token count.*exceeds the maximum/i,
  8. /maximum prompt length is \d+/i,
  9. /reduce the length of the messages/i,
  10. /maximum context length is \d+ tokens/i,
  11. /exceeds the limit of \d+/i,
  12. /exceeds the available context size/i,
  13. /greater than the context length/i,
  14. /context window exceeds limit/i,
  15. /exceeded model token limit/i,
  16. /context[_ ]length[_ ]exceeded/i,
  17. /request entity too large/i,
  18. /context length is only \d+ tokens/i,
  19. /input length.*exceeds.*context length/i,
  20. /prompt too long; exceeded (?:max )?context length/i,
  21. /too large for model with \d+ maximum context length/i,
  22. /model_context_window_exceeded/i,
  23. ]
  24. export const isContextOverflow = (message: string) =>
  25. patterns.some((pattern) => pattern.test(message)) || /^4(00|13)\s*(status code)?\s*\(no body\)/i.test(message)
  26. export const isContextOverflowFailure = (failure: unknown) =>
  27. failure instanceof LLMError
  28. ? failure.reason._tag === "InvalidRequest" && failure.reason.classification === "context-overflow"
  29. : Schema.is(ProviderErrorEvent)(failure) && failure.classification === "context-overflow"