(
c: TextContext,
requestData: RequestData,
contentResponse: boolean,
)
| 305 | } |
| 306 | |
| 307 | async function generateTextResponse( |
| 308 | c: TextContext, |
| 309 | requestData: RequestData, |
| 310 | contentResponse: boolean, |
| 311 | ): Promise<Response> { |
| 312 | syncTextEnvironment(c.env); |
| 313 | |
| 314 | try { |
| 315 | const communityEndpoint = c.var.model?.communityEndpoint; |
| 316 | const gatewayContext = communityEndpoint |
| 317 | ? await communityEndpointGatewayContext( |
| 318 | communityEndpoint, |
| 319 | c.var.model.definition, |
| 320 | requestData, |
| 321 | c.env.BETTER_AUTH_SECRET, |
| 322 | c.env.PORTKEY_GATEWAY_URL, |
| 323 | c.var.auth?.apiKey?.rawKey || "", |
| 324 | ) |
| 325 | : withGatewayContext(c, requestData); |
| 326 | const completion = await generateTextPortkey( |
| 327 | requestData.messages, |
| 328 | gatewayContext, |
| 329 | ); |
| 330 | completion.id = completion.id || generatePollinationsId(); |
| 331 | |
| 332 | if (completion.error) { |
| 333 | const errorObj = |
| 334 | typeof completion.error === "string" |
| 335 | ? { message: completion.error } |
| 336 | : completion.error; |
| 337 | const error = new Error( |
| 338 | errorObj.message || "Text generation failed", |
| 339 | ) as ServiceError; |
| 340 | if (typeof errorObj.status === "number") { |
| 341 | error.status = remapUpstreamStatus(errorObj.status); |
| 342 | error.upstreamStatus = errorObj.status; |
| 343 | } |
| 344 | error.details = errorObj.details; |
| 345 | throw error; |
| 346 | } |
| 347 | |
| 348 | if (requestData.stream) return sendTextStreamResponse(completion); |
| 349 | const fallbackModel = c.var.model?.resolved; |
| 350 | // Provider-reported cost is read post-response in track (clamp-and-alert |
| 351 | // in the registry) — malformed/absent cost never fails the request. |
| 352 | const trackingResponse = sendOpenAIResponse(completion, fallbackModel); |
| 353 | const publicCompletion = publicChatCompletion(completion); |
| 354 | if (contentResponse) { |
| 355 | c.var.track?.overrideResponseTracking(trackingResponse.clone()); |
| 356 | return sendTextContentResponse(publicCompletion, fallbackModel); |
| 357 | } |
| 358 | c.var.track?.overrideResponseTracking(trackingResponse.clone()); |
| 359 | return sendOpenAIResponse(publicCompletion, fallbackModel); |
| 360 | } catch (thrown: unknown) { |
| 361 | throwTextError(thrown as ServiceError, c); |
| 362 | } |
| 363 | } |
| 364 |
no test coverage detected