(
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,
)
| 839 | * iterating to yield system messages, and returning the final BetaMessage. |
| 840 | */ |
| 841 | export async function* executeNonStreamingRequest( |
| 842 | clientOptions: { |
| 843 | model: string |
| 844 | fetchOverride?: Options['fetchOverride'] |
| 845 | source: string |
| 846 | }, |
| 847 | retryOptions: { |
| 848 | model: string |
| 849 | fallbackModel?: string |
| 850 | thinkingConfig: ThinkingConfig |
| 851 | fastMode?: boolean |
| 852 | signal: AbortSignal |
| 853 | initialConsecutive529Errors?: number |
| 854 | querySource?: QuerySource |
| 855 | }, |
| 856 | paramsFromContext: (context: RetryContext) => BetaMessageStreamParams, |
| 857 | onAttempt: (attempt: number, start: number, maxOutputTokens: number) => void, |
| 858 | captureRequest: (params: BetaMessageStreamParams) => void, |
| 859 | /** |
| 860 | * Request ID of the failed streaming attempt this fallback is recovering |
| 861 | * from. Emitted in tengu_nonstreaming_fallback_error for funnel correlation. |
| 862 | */ |
| 863 | originatingRequestId?: string | null, |
| 864 | ): AsyncGenerator<SystemAPIErrorMessage, BetaMessage> { |
| 865 | const fallbackTimeoutMs = getNonstreamingFallbackTimeoutMs() |
| 866 | const generator = withRetry( |
| 867 | () => |
| 868 | getAnthropicClient({ |
| 869 | maxRetries: 0, |
| 870 | model: clientOptions.model, |
| 871 | fetchOverride: clientOptions.fetchOverride, |
| 872 | source: clientOptions.source, |
| 873 | }), |
| 874 | async (anthropic, attempt, context) => { |
| 875 | const start = Date.now() |
| 876 | const retryParams = paramsFromContext(context) |
| 877 | captureRequest(retryParams) |
| 878 | onAttempt(attempt, start, retryParams.max_tokens) |
| 879 | |
| 880 | const adjustedParams = adjustParamsForNonStreaming( |
| 881 | retryParams, |
| 882 | MAX_NON_STREAMING_TOKENS, |
| 883 | ) |
| 884 | |
| 885 | try { |
| 886 | return await anthropic.beta.messages.create( |
| 887 | { |
| 888 | ...adjustedParams, |
| 889 | model: normalizeModelStringForAPI(adjustedParams.model), |
| 890 | }, |
| 891 | { |
| 892 | signal: retryOptions.signal, |
| 893 | timeout: fallbackTimeoutMs, |
| 894 | }, |
| 895 | ) |
| 896 | } catch (err) { |
| 897 | // User aborts are not errors — re-throw immediately without logging |
| 898 | if (err instanceof APIUserAbortError) throw err |
no test coverage detected