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

Function handleSiliconFlowNonStream

web/src/llm-api/siliconflow.ts:114–190  ·  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

112}
113
114export 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,

Callers 1

postChatCompletionsFunction · 0.90

Calls 8

extractRequestMetadataFunction · 0.90
insertMessageToBigQueryFunction · 0.90
consumeCreditsForMessageFunction · 0.90
createRequestAuditRecordFunction · 0.85
createSiliconFlowRequestFunction · 0.85
parseSiliconFlowErrorFunction · 0.85
extractUsageAndCostFunction · 0.70
creditsToFakeCostFunction · 0.70

Tested by

no test coverage detected