( params: ParamsOf<PromptAiSdkFn>, )
| 718 | } |
| 719 | |
| 720 | export async function promptAiSdk( |
| 721 | params: ParamsOf<PromptAiSdkFn>, |
| 722 | ): ReturnType<PromptAiSdkFn> { |
| 723 | const { logger } = params |
| 724 | |
| 725 | if (params.signal.aborted) { |
| 726 | logger.info( |
| 727 | { |
| 728 | userId: params.userId, |
| 729 | userInputId: params.userInputId, |
| 730 | }, |
| 731 | 'Skipping prompt due to canceled user input', |
| 732 | ) |
| 733 | return promptAborted('User cancelled input') |
| 734 | } |
| 735 | |
| 736 | const modelParams: ModelRequestParams = { |
| 737 | apiKey: params.apiKey, |
| 738 | model: params.model, |
| 739 | skipChatGptOAuth: true, // Always use Codebuff backend for non-streaming |
| 740 | } |
| 741 | const { model: aiSDKModel } = await getModelForRequest(modelParams) |
| 742 | |
| 743 | const response = await generateText({ |
| 744 | ...params, |
| 745 | prompt: undefined, |
| 746 | model: aiSDKModel, |
| 747 | messages: convertCbToModelMessages(params), |
| 748 | providerOptions: getProviderOptions({ |
| 749 | ...params, |
| 750 | agentProviderOptions: params.agentProviderOptions, |
| 751 | cacheDebugCorrelation: params.cacheDebugCorrelation, |
| 752 | }), |
| 753 | }) |
| 754 | emitCacheDebugProviderRequest({ |
| 755 | callback: params.onCacheDebugProviderRequestBuilt, |
| 756 | provider: getModelProvider(aiSDKModel), |
| 757 | rawBody: response.request?.body, |
| 758 | }) |
| 759 | emitCacheDebugUsage({ |
| 760 | callback: params.onCacheDebugUsageReceived, |
| 761 | usage: response.usage, |
| 762 | }) |
| 763 | const content = response.text |
| 764 | |
| 765 | const providerMetadata = response.providerMetadata ?? {} |
| 766 | let costOverrideDollars: number | undefined |
| 767 | if (providerMetadata.codebuff) { |
| 768 | if (providerMetadata.codebuff.usage) { |
| 769 | const openrouterUsage = providerMetadata.codebuff |
| 770 | .usage as OpenRouterUsageAccounting |
| 771 | |
| 772 | costOverrideDollars = |
| 773 | (openrouterUsage.cost ?? 0) + |
| 774 | (openrouterUsage.costDetails?.upstreamInferenceCost ?? 0) |
| 775 | } |
| 776 | } |
| 777 |
no test coverage detected