( params: PromptAiSdkStructuredInput<T>, )
| 786 | } |
| 787 | |
| 788 | export async function promptAiSdkStructured<T>( |
| 789 | params: PromptAiSdkStructuredInput<T>, |
| 790 | ): PromptAiSdkStructuredOutput<T> { |
| 791 | const { logger } = params |
| 792 | |
| 793 | if (params.signal.aborted) { |
| 794 | logger.info( |
| 795 | { |
| 796 | userId: params.userId, |
| 797 | userInputId: params.userInputId, |
| 798 | }, |
| 799 | 'Skipping structured prompt due to canceled user input', |
| 800 | ) |
| 801 | return promptAborted('User cancelled input') |
| 802 | } |
| 803 | const modelParams: ModelRequestParams = { |
| 804 | apiKey: params.apiKey, |
| 805 | model: params.model, |
| 806 | skipChatGptOAuth: true, // Always use Codebuff backend for non-streaming |
| 807 | } |
| 808 | const { model: aiSDKModel } = await getModelForRequest(modelParams) |
| 809 | |
| 810 | const response = await generateObject<z.ZodType<T>, 'object'>({ |
| 811 | ...params, |
| 812 | prompt: undefined, |
| 813 | model: aiSDKModel, |
| 814 | output: 'object', |
| 815 | messages: convertCbToModelMessages(params), |
| 816 | providerOptions: getProviderOptions({ |
| 817 | ...params, |
| 818 | agentProviderOptions: params.agentProviderOptions, |
| 819 | cacheDebugCorrelation: params.cacheDebugCorrelation, |
| 820 | }), |
| 821 | }) |
| 822 | |
| 823 | emitCacheDebugProviderRequest({ |
| 824 | callback: params.onCacheDebugProviderRequestBuilt, |
| 825 | provider: getModelProvider(aiSDKModel), |
| 826 | rawBody: response.request?.body, |
| 827 | }) |
| 828 | emitCacheDebugUsage({ |
| 829 | callback: params.onCacheDebugUsageReceived, |
| 830 | usage: response.usage, |
| 831 | }) |
| 832 | |
| 833 | const content = response.object |
| 834 | |
| 835 | const providerMetadata = response.providerMetadata ?? {} |
| 836 | let costOverrideDollars: number | undefined |
| 837 | if (providerMetadata.codebuff) { |
| 838 | if (providerMetadata.codebuff.usage) { |
| 839 | const openrouterUsage = providerMetadata.codebuff |
| 840 | .usage as OpenRouterUsageAccounting |
| 841 | |
| 842 | costOverrideDollars = |
| 843 | (openrouterUsage.cost ?? 0) + |
| 844 | (openrouterUsage.costDetails?.upstreamInferenceCost ?? 0) |
| 845 | } |
no test coverage detected