Open History to see the last commit
1import { CONTEXT_OVERFLOW_CODE } from '@tanstack/ai'
2import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
3import { OpenRouterError } from '@openrouter/sdk/models/errors'
4
5/**
6 * `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a
7 * context overflow. OpenRouter has no error code for it. It answers 400 with
8 * this text for every upstream provider, so the text is the only signal.
9 */
10export function toOpenRouterRunErrorPayload(
11 error: unknown,
12 fallbackMessage: string,
13): { message: string; code: string | undefined } {
14 const payload = toRunErrorPayload(error, fallbackMessage)
15 return error instanceof OpenRouterError &&
16 error.statusCode === 400 &&
17 payload.message.includes('maximum context length')
18 ? { ...payload, code: CONTEXT_OVERFLOW_CODE }
19 : payload
20}