Download packages/core/src/utils/generateContentResponseUtilities.ts from Jimmi42/openCLI: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/Jimmi42/openCLI/resolve/main/packages/core/src/utils/generateContentResponseUtilities.ts
- Command line
-
hf download hf://Jimmi42/openCLI/packages/core/src/utils/generateContentResponseUtilities.ts
-
curl -L -o generateContentResponseUtilities.ts https://huggingface.co/Jimmi42/openCLI/resolve/main/packages/core/src/utils/generateContentResponseUtilities.ts
3.06 kB
| /** | |
| * @license | |
| * Copyright 2025 Google LLC | |
| * SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| import { GenerateContentResponse, Part, FunctionCall } from '@google/genai'; | |
| export function getResponseText( | |
| response: GenerateContentResponse, | |
| ): string | undefined { | |
| const parts = response.candidates?.[0]?.content?.parts; | |
| if (!parts) { | |
| return undefined; | |
| } | |
| const textSegments = parts | |
| .map((part) => part.text) | |
| .filter((text): text is string => typeof text === 'string'); | |
| if (textSegments.length === 0) { | |
| return undefined; | |
| } | |
| return textSegments.join(''); | |
| } | |
| export function getResponseTextFromParts(parts: Part[]): string | undefined { | |
| if (!parts) { | |
| return undefined; | |
| } | |
| const textSegments = parts | |
| .map((part) => part.text) | |
| .filter((text): text is string => typeof text === 'string'); | |
| if (textSegments.length === 0) { | |
| return undefined; | |
| } | |
| return textSegments.join(''); | |
| } | |
| export function getFunctionCalls( | |
| response: GenerateContentResponse, | |
| ): FunctionCall[] | undefined { | |
| const parts = response.candidates?.[0]?.content?.parts; | |
| if (!parts) { | |
| return undefined; | |
| } | |
| const functionCallParts = parts | |
| .filter((part) => !!part.functionCall) | |
| .map((part) => part.functionCall as FunctionCall); | |
| return functionCallParts.length > 0 ? functionCallParts : undefined; | |
| } | |
| export function getFunctionCallsFromParts( | |
| parts: Part[], | |
| ): FunctionCall[] | undefined { | |
| if (!parts) { | |
| return undefined; | |
| } | |
| const functionCallParts = parts | |
| .filter((part) => !!part.functionCall) | |
| .map((part) => part.functionCall as FunctionCall); | |
| return functionCallParts.length > 0 ? functionCallParts : undefined; | |
| } | |
| export function getFunctionCallsAsJson( | |
| response: GenerateContentResponse, | |
| ): string | undefined { | |
| const functionCalls = getFunctionCalls(response); | |
| if (!functionCalls) { | |
| return undefined; | |
| } | |
| return JSON.stringify(functionCalls, null, 2); | |
| } | |
| export function getFunctionCallsFromPartsAsJson( | |
| parts: Part[], | |
| ): string | undefined { | |
| const functionCalls = getFunctionCallsFromParts(parts); | |
| if (!functionCalls) { | |
| return undefined; | |
| } | |
| return JSON.stringify(functionCalls, null, 2); | |
| } | |
| export function getStructuredResponse( | |
| response: GenerateContentResponse, | |
| ): string | undefined { | |
| const textContent = getResponseText(response); | |
| const functionCallsJson = getFunctionCallsAsJson(response); | |
| if (textContent && functionCallsJson) { | |
| return `${textContent}\n${functionCallsJson}`; | |
| } | |
| if (textContent) { | |
| return textContent; | |
| } | |
| if (functionCallsJson) { | |
| return functionCallsJson; | |
| } | |
| return undefined; | |
| } | |
| export function getStructuredResponseFromParts( | |
| parts: Part[], | |
| ): string | undefined { | |
| const textContent = getResponseTextFromParts(parts); | |
| const functionCallsJson = getFunctionCallsFromPartsAsJson(parts); | |
| if (textContent && functionCallsJson) { | |
| return `${textContent}\n${functionCallsJson}`; | |
| } | |
| if (textContent) { | |
| return textContent; | |
| } | |
| if (functionCallsJson) { | |
| return functionCallsJson; | |
| } | |
| return undefined; | |
| } | |