(
params: Record<string, unknown>,
requestOptions: RequestOptions = {},
)
| 784 | const defaultHeaders = options.defaultHeaders ?? {} |
| 785 | |
| 786 | const create = ( |
| 787 | params: Record<string, unknown>, |
| 788 | requestOptions: RequestOptions = {}, |
| 789 | ) => { |
| 790 | const promise = (async (): Promise<ResponseEnvelope<unknown>> => { |
| 791 | const url = joinUrl(baseURL, 'chat/completions') |
| 792 | const body = { |
| 793 | model: params.model, |
| 794 | messages: anthropicMessagesToOpenAI(params.system, params.messages), |
| 795 | tools: anthropicToolsToOpenAI(params.tools), |
| 796 | tool_choice: anthropicToolChoiceToOpenAI(params.tool_choice), |
| 797 | stream: params.stream === true, |
| 798 | max_tokens: params.max_tokens, |
| 799 | ...(typeof params.temperature === 'number' |
| 800 | ? { temperature: params.temperature } |
| 801 | : {}), |
| 802 | ...(Array.isArray(params.stop_sequences) |
| 803 | ? { stop: params.stop_sequences } |
| 804 | : {}), |
| 805 | ...(params.stream === true ? { stream_options: { include_usage: true } } : {}), |
| 806 | } |
| 807 | |
| 808 | const response = await requestJson(fetchFn, url, { |
| 809 | method: 'POST', |
| 810 | headers: buildHeaders( |
| 811 | providerType, |
| 812 | options.authToken, |
| 813 | defaultHeaders, |
| 814 | requestOptions.headers, |
| 815 | ), |
| 816 | body: JSON.stringify(body), |
| 817 | signal: buildSignal(requestOptions.signal, requestOptions.timeout ?? options.timeout), |
| 818 | ...(options.fetchOptions as RequestInit | undefined), |
| 819 | }) |
| 820 | |
| 821 | const requestId = getRequestId(response.headers) |
| 822 | if (!response.ok) { |
| 823 | const rawBody = await response.text() |
| 824 | throw APIError.generate( |
| 825 | response.status, |
| 826 | parseJsonOrText(rawBody) as Record<string, unknown> | undefined, |
| 827 | undefined, |
| 828 | response.headers, |
| 829 | ) |
| 830 | } |
| 831 | |
| 832 | if (params.stream === true) { |
| 833 | return { |
| 834 | data: createOpenAIEventStream(response, String(params.model ?? '')), |
| 835 | request_id: requestId, |
| 836 | response, |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | const json = (await response.json()) as OpenAIChatCompletionResponse |
| 841 | return { |
| 842 | data: openAIResponseToAnthropicMessage( |
| 843 | json, |
nothing calls this directly
no test coverage detected