(error: APIError)
| 548 | } |
| 549 | |
| 550 | export function parseMaxTokensContextOverflowError(error: APIError): |
| 551 | | { |
| 552 | inputTokens: number |
| 553 | maxTokens: number |
| 554 | contextLimit: number |
| 555 | } |
| 556 | | undefined { |
| 557 | if (error.status !== 400 || !error.message) { |
| 558 | return undefined |
| 559 | } |
| 560 | |
| 561 | if ( |
| 562 | !error.message.includes( |
| 563 | 'input length and `max_tokens` exceed context limit', |
| 564 | ) |
| 565 | ) { |
| 566 | return undefined |
| 567 | } |
| 568 | |
| 569 | // Example format: "input length and `max_tokens` exceed context limit: 188059 + 20000 > 200000" |
| 570 | const regex = |
| 571 | /input length and `max_tokens` exceed context limit: (\d+) \+ (\d+) > (\d+)/ |
| 572 | const match = error.message.match(regex) |
| 573 | |
| 574 | if (!match || match.length !== 4) { |
| 575 | return undefined |
| 576 | } |
| 577 | |
| 578 | if (!match[1] || !match[2] || !match[3]) { |
| 579 | logError( |
| 580 | new Error( |
| 581 | 'Unable to parse max_tokens from max_tokens exceed context limit error message', |
| 582 | ), |
| 583 | ) |
| 584 | return undefined |
| 585 | } |
| 586 | const inputTokens = parseInt(match[1], 10) |
| 587 | const maxTokens = parseInt(match[2], 10) |
| 588 | const contextLimit = parseInt(match[3], 10) |
| 589 | |
| 590 | if (isNaN(inputTokens) || isNaN(maxTokens) || isNaN(contextLimit)) { |
| 591 | return undefined |
| 592 | } |
| 593 | |
| 594 | return { inputTokens, maxTokens, contextLimit } |
| 595 | } |
| 596 | |
| 597 | // TODO: Replace with a response header check once the API adds a dedicated |
| 598 | // header for fast-mode rejection (e.g., x-fast-mode-rejected). String-matching |
no test coverage detected