(error: APIError)
| 591 | } |
| 592 | |
| 593 | export function parseMaxTokensContextOverflowError(error: APIError): |
| 594 | | { |
| 595 | inputTokens: number |
| 596 | maxTokens: number |
| 597 | contextLimit: number |
| 598 | } |
| 599 | | undefined { |
| 600 | if (error.status !== 400 || !error.message) { |
| 601 | return undefined |
| 602 | } |
| 603 | |
| 604 | if ( |
| 605 | !error.message.includes( |
| 606 | 'input length and `max_tokens` exceed context limit', |
| 607 | ) |
| 608 | ) { |
| 609 | return undefined |
| 610 | } |
| 611 | |
| 612 | // Example format: "input length and `max_tokens` exceed context limit: 188059 + 20000 > 200000" |
| 613 | const regex = |
| 614 | /input length and `max_tokens` exceed context limit: (\d+) \+ (\d+) > (\d+)/ |
| 615 | const match = error.message.match(regex) |
| 616 | |
| 617 | if (!match || match.length !== 4) { |
| 618 | return undefined |
| 619 | } |
| 620 | |
| 621 | if (!match[1] || !match[2] || !match[3]) { |
| 622 | logError( |
| 623 | new Error( |
| 624 | 'Unable to parse max_tokens from max_tokens exceed context limit error message', |
| 625 | ), |
| 626 | ) |
| 627 | return undefined |
| 628 | } |
| 629 | const inputTokens = parseInt(match[1], 10) |
| 630 | const maxTokens = parseInt(match[2], 10) |
| 631 | const contextLimit = parseInt(match[3], 10) |
| 632 | |
| 633 | if (isNaN(inputTokens) || isNaN(maxTokens) || isNaN(contextLimit)) { |
| 634 | return undefined |
| 635 | } |
| 636 | |
| 637 | return { inputTokens, maxTokens, contextLimit } |
| 638 | } |
| 639 | |
| 640 | // TODO: Replace with a response header check once the API adds a dedicated |
| 641 | // header for fast-mode rejection (e.g., x-fast-mode-rejected). String-matching |
no test coverage detected