({
body,
userId,
stripeCustomerId,
agentId,
fetch,
logger,
insertMessageBigquery,
}: {
body: ChatCompletionRequestBody
userId: string
stripeCustomerId?: string | null
agentId: string
fetch: typeof globalThis.fetch
logger: Logger
insertMessageBigquery: InsertMessageBigqueryFn
})
| 112 | } |
| 113 | |
| 114 | export async function handleSiliconFlowNonStream({ |
| 115 | body, |
| 116 | userId, |
| 117 | stripeCustomerId, |
| 118 | agentId, |
| 119 | fetch, |
| 120 | logger, |
| 121 | insertMessageBigquery, |
| 122 | }: { |
| 123 | body: ChatCompletionRequestBody |
| 124 | userId: string |
| 125 | stripeCustomerId?: string | null |
| 126 | agentId: string |
| 127 | fetch: typeof globalThis.fetch |
| 128 | logger: Logger |
| 129 | insertMessageBigquery: InsertMessageBigqueryFn |
| 130 | }) { |
| 131 | const originalModel = body.model |
| 132 | const startTime = new Date() |
| 133 | const { clientId, clientRequestId, costMode } = extractRequestMetadata({ body, logger }) |
| 134 | const auditRequest = createRequestAuditRecord(body) |
| 135 | |
| 136 | const response = await createSiliconFlowRequest({ body, originalModel, fetch }) |
| 137 | |
| 138 | if (!response.ok) { |
| 139 | throw await parseSiliconFlowError(response) |
| 140 | } |
| 141 | |
| 142 | const data = await response.json() |
| 143 | const content = data.choices?.[0]?.message?.content ?? '' |
| 144 | const reasoningText = data.choices?.[0]?.message?.reasoning_content ?? data.choices?.[0]?.message?.reasoning ?? '' |
| 145 | const usageData = extractUsageAndCost(data.usage) |
| 146 | |
| 147 | insertMessageToBigQuery({ |
| 148 | messageId: data.id, |
| 149 | userId, |
| 150 | startTime, |
| 151 | request: auditRequest, |
| 152 | reasoningText, |
| 153 | responseText: content, |
| 154 | usageData, |
| 155 | logger, |
| 156 | insertMessageBigquery, |
| 157 | }).catch((error) => { |
| 158 | logger.error({ error }, 'Failed to insert message into BigQuery') |
| 159 | }) |
| 160 | |
| 161 | const billedCredits = await consumeCreditsForMessage({ |
| 162 | messageId: data.id, |
| 163 | userId, |
| 164 | stripeCustomerId, |
| 165 | agentId, |
| 166 | clientId, |
| 167 | clientRequestId, |
| 168 | startTime, |
| 169 | model: originalModel, |
| 170 | reasoningText, |
| 171 | responseText: content, |
no test coverage detected