| 49 | }; |
| 50 | |
| 51 | export class AiSdkApi implements BaseLlmApi { |
| 52 | private provider?: (modelId: string) => any; |
| 53 | private config: AiSdkConfig; |
| 54 | private providerId: string; |
| 55 | private modelId: string; |
| 56 | |
| 57 | constructor(config: AiSdkConfig) { |
| 58 | this.config = config; |
| 59 | if (!config.model) { |
| 60 | throw new Error( |
| 61 | "AI SDK provider requires a model in the format '<provider>/<model>' (e.g., 'openai/gpt-4o')", |
| 62 | ); |
| 63 | } |
| 64 | const [providerId, ...modelParts] = config.model.split("/"); |
| 65 | this.providerId = providerId; |
| 66 | this.modelId = modelParts.join("/"); |
| 67 | } |
| 68 | |
| 69 | private initializeProvider() { |
| 70 | if (this.provider) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | const createFn = PROVIDER_MAP[this.providerId]; |
| 75 | if (!createFn) { |
| 76 | const supportedProviders = Object.keys(PROVIDER_MAP).join(", "); |
| 77 | throw new Error( |
| 78 | `Unknown AI SDK provider: "${this.providerId}". ` + |
| 79 | `Supported providers: ${supportedProviders}. ` + |
| 80 | `To use a different provider, install the @ai-sdk/* package and add it to the provider map.`, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | const hasRequestOptions = |
| 85 | this.config.requestOptions && |
| 86 | (this.config.requestOptions.headers || |
| 87 | this.config.requestOptions.proxy || |
| 88 | this.config.requestOptions.caBundlePath || |
| 89 | this.config.requestOptions.clientCertificate || |
| 90 | this.config.requestOptions.extraBodyProperties); |
| 91 | |
| 92 | this.provider = createFn({ |
| 93 | apiKey: this.config.apiKey ?? "", |
| 94 | baseURL: this.config.apiBase, |
| 95 | fetch: hasRequestOptions |
| 96 | ? customFetch(this.config.requestOptions) |
| 97 | : undefined, |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | async chatCompletionNonStream( |
| 102 | body: ChatCompletionCreateParamsNonStreaming, |
| 103 | signal: AbortSignal, |
| 104 | ): Promise<ChatCompletion> { |
| 105 | this.initializeProvider(); |
| 106 | |
| 107 | const { generateText } = await import("ai"); |
| 108 | const { convertOpenAIMessagesToVercel } = await import( |
nothing calls this directly
no outgoing calls
no test coverage detected