MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / handleOpenCodeZenNonStream

Function handleOpenCodeZenNonStream

web/src/llm-api/opencode-zen.ts:241–324  ·  view source on GitHub ↗
({
  body,
  userId,
  stripeCustomerId,
  agentId,
  fetch,
  logger,
  insertMessageBigquery,
}: {
  body: ChatCompletionRequestBody
  userId: string
  stripeCustomerId?: string | null
  agentId: string
  fetch: typeof globalThis.fetch
  logger: Logger
  insertMessageBigquery: InsertMessageBigqueryFn
})

Source from the content-addressed store, hash-verified

239}
240
241export async function handleOpenCodeZenNonStream({
242 body,
243 userId,
244 stripeCustomerId,
245 agentId,
246 fetch,
247 logger,
248 insertMessageBigquery,
249}: {
250 body: ChatCompletionRequestBody
251 userId: string
252 stripeCustomerId?: string | null
253 agentId: string
254 fetch: typeof globalThis.fetch
255 logger: Logger
256 insertMessageBigquery: InsertMessageBigqueryFn
257}) {
258 const originalModel = body.model
259 const startTime = new Date()
260 const { clientId, clientRequestId, costMode } = extractRequestMetadata({
261 body,
262 logger,
263 })
264 const auditRequest = createRequestAuditRecord(body)
265
266 const response = await createOpenCodeZenRequest({
267 body,
268 originalModel,
269 fetch,
270 })
271 if (!response.ok) {
272 throw await parseOpenCodeZenError(response)
273 }
274
275 const data = await response.json()
276 const content = data.choices?.[0]?.message?.content ?? ''
277 const reasoningText =
278 data.choices?.[0]?.message?.reasoning_content ??
279 data.choices?.[0]?.message?.reasoning ??
280 ''
281 const usageData = extractUsageAndCost(data.usage, originalModel)
282
283 insertMessageToBigQuery({
284 messageId: data.id,
285 userId,
286 startTime,
287 request: auditRequest,
288 reasoningText,
289 responseText: content,
290 usageData,
291 logger,
292 insertMessageBigquery,
293 }).catch((error) => {
294 logger.error({ error }, 'Failed to insert message into BigQuery')
295 })
296
297 const billedCredits = await consumeCreditsForMessage({
298 messageId: data.id,

Callers 1

postChatCompletionsFunction · 0.90

Calls 8

extractRequestMetadataFunction · 0.90
insertMessageToBigQueryFunction · 0.90
consumeCreditsForMessageFunction · 0.90
createRequestAuditRecordFunction · 0.85
createOpenCodeZenRequestFunction · 0.85
parseOpenCodeZenErrorFunction · 0.85
extractUsageAndCostFunction · 0.70
creditsToFakeCostFunction · 0.70

Tested by

no test coverage detected