File size: 3,061 Bytes
40e575e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
/**
* @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;
}
|