({
body,
userId,
stripeCustomerId,
agentId,
fetch,
logger,
insertMessageBigquery,
}: {
body: ChatCompletionRequestBody
userId: string
stripeCustomerId?: string | null
agentId: string
fetch: typeof globalThis.fetch
logger: Logger
insertMessageBigquery: InsertMessageBigqueryFn
})
| 147 | } |
| 148 | |
| 149 | export async function handleCanopyWaveNonStream({ |
| 150 | body, |
| 151 | userId, |
| 152 | stripeCustomerId, |
| 153 | agentId, |
| 154 | fetch, |
| 155 | logger, |
| 156 | insertMessageBigquery, |
| 157 | }: { |
| 158 | body: ChatCompletionRequestBody |
| 159 | userId: string |
| 160 | stripeCustomerId?: string | null |
| 161 | agentId: string |
| 162 | fetch: typeof globalThis.fetch |
| 163 | logger: Logger |
| 164 | insertMessageBigquery: InsertMessageBigqueryFn |
| 165 | }) { |
| 166 | const originalModel = body.model |
| 167 | const startTime = new Date() |
| 168 | const { clientId, clientRequestId, costMode } = extractRequestMetadata({ body, logger }) |
| 169 | const auditRequest = createRequestAuditRecord(body) |
| 170 | |
| 171 | const response = await createCanopyWaveRequest({ body, originalModel, fetch }) |
| 172 | |
| 173 | if (!response.ok) { |
| 174 | throw await parseCanopyWaveError(response) |
| 175 | } |
| 176 | |
| 177 | const data = await response.json() |
| 178 | const content = data.choices?.[0]?.message?.content ?? '' |
| 179 | const reasoningText = data.choices?.[0]?.message?.reasoning_content ?? data.choices?.[0]?.message?.reasoning ?? '' |
| 180 | const usageData = extractUsageAndCost(data.usage, originalModel) |
| 181 | |
| 182 | insertMessageToBigQuery({ |
| 183 | messageId: data.id, |
| 184 | userId, |
| 185 | startTime, |
| 186 | request: auditRequest, |
| 187 | reasoningText, |
| 188 | responseText: content, |
| 189 | usageData, |
| 190 | logger, |
| 191 | insertMessageBigquery, |
| 192 | }).catch((error) => { |
| 193 | logger.error({ error }, 'Failed to insert message into BigQuery') |
| 194 | }) |
| 195 | |
| 196 | const billedCredits = await consumeCreditsForMessage({ |
| 197 | messageId: data.id, |
| 198 | userId, |
| 199 | stripeCustomerId, |
| 200 | agentId, |
| 201 | clientId, |
| 202 | clientRequestId, |
| 203 | startTime, |
| 204 | model: originalModel, |
| 205 | reasoningText, |
| 206 | responseText: content, |
no test coverage detected