MCPcopy Create free account
hub / github.com/pollinations/pollinations / generateTextResponse

Function generateTextResponse

gen.pollinations.ai/src/text/handler.ts:307–363  ·  view source on GitHub ↗
(
    c: TextContext,
    requestData: RequestData,
    contentResponse: boolean,
)

Source from the content-addressed store, hash-verified

305}
306
307async 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

Callers 3

handleTextContentLocalFunction · 0.85
handleSimpleTextLocalFunction · 0.85

Calls 12

remapUpstreamStatusFunction · 0.90
syncTextEnvironmentFunction · 0.85
withGatewayContextFunction · 0.85
generateTextPortkeyFunction · 0.85
generatePollinationsIdFunction · 0.85
sendTextStreamResponseFunction · 0.85
sendOpenAIResponseFunction · 0.85
publicChatCompletionFunction · 0.85
sendTextContentResponseFunction · 0.85
throwTextErrorFunction · 0.85
cloneMethod · 0.65

Tested by

no test coverage detected