(
clientOptions: {
model: string
fetchOverride?: Options['fetchOverride']
source: string
},
retryOptions: {
model: string
fallbackModel?: string
thinkingConfig: ThinkingConfig
fastMode?: boolean
signal: AbortSignal
initialConsecutive529Errors?: number
querySource?: QuerySource
},
paramsFromContext: (context: RetryContext) => BetaMessageStreamParams,
onAttempt: (attempt: number, start: number, maxOutputTokens: number) => void,
captureRequest: (params: BetaMessageStreamParams) => void,
/**
* Request ID of the failed streaming attempt this fallback is recovering
* from. Emitted in tengu_nonstreaming_fallback_error for funnel correlation.
*/
originatingRequestId?: string | null,
)
| 816 | * iterating to yield system messages, and returning the final BetaMessage. |
| 817 | */ |
| 818 | export async function* executeNonStreamingRequest( |
| 819 | clientOptions: { |
| 820 | model: string |
| 821 | fetchOverride?: Options['fetchOverride'] |
| 822 | source: string |
| 823 | }, |
| 824 | retryOptions: { |
| 825 | model: string |
| 826 | fallbackModel?: string |
| 827 | thinkingConfig: ThinkingConfig |
| 828 | fastMode?: boolean |
| 829 | signal: AbortSignal |
| 830 | initialConsecutive529Errors?: number |
| 831 | querySource?: QuerySource |
| 832 | }, |
| 833 | paramsFromContext: (context: RetryContext) => BetaMessageStreamParams, |
| 834 | onAttempt: (attempt: number, start: number, maxOutputTokens: number) => void, |
| 835 | captureRequest: (params: BetaMessageStreamParams) => void, |
| 836 | /** |
| 837 | * Request ID of the failed streaming attempt this fallback is recovering |
| 838 | * from. Emitted in tengu_nonstreaming_fallback_error for funnel correlation. |
| 839 | */ |
| 840 | originatingRequestId?: string | null, |
| 841 | ): AsyncGenerator<SystemAPIErrorMessage, BetaMessage> { |
| 842 | const fallbackTimeoutMs = getNonstreamingFallbackTimeoutMs() |
| 843 | const generator = withRetry( |
| 844 | () => |
| 845 | getAnthropicClient({ |
| 846 | maxRetries: 0, |
| 847 | model: clientOptions.model, |
| 848 | fetchOverride: clientOptions.fetchOverride, |
| 849 | source: clientOptions.source, |
| 850 | }), |
| 851 | async (anthropic, attempt, context) => { |
| 852 | const start = Date.now() |
| 853 | const retryParams = paramsFromContext(context) |
| 854 | captureRequest(retryParams) |
| 855 | onAttempt(attempt, start, retryParams.max_tokens) |
| 856 | |
| 857 | const adjustedParams = adjustParamsForNonStreaming( |
| 858 | retryParams, |
| 859 | MAX_NON_STREAMING_TOKENS, |
| 860 | ) |
| 861 | |
| 862 | try { |
| 863 | // biome-ignore lint/plugin: non-streaming API call |
| 864 | return await anthropic.beta.messages.create( |
| 865 | { |
| 866 | ...adjustedParams, |
| 867 | model: normalizeModelStringForAPI(adjustedParams.model), |
| 868 | }, |
| 869 | { |
| 870 | signal: retryOptions.signal, |
| 871 | timeout: fallbackTimeoutMs, |
| 872 | }, |
| 873 | ) |
| 874 | } catch (err) { |
| 875 | // User aborts are not errors — re-throw immediately without logging |
no test coverage detected