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