Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 6x 6x 6x 6x | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {
GenerateContentResponse,
PartListUnion,
Part,
PartUnion,
} from '@google/genai';
/**
* Converts a PartListUnion into a string.
* If verbose is true, includes summary representations of non-text parts.
*/
export function partToString(
value: PartListUnion,
options?: { verbose?: boolean },
): string {
Iif (!value) {
return '';
}
Iif (typeof value === 'string') {
return value;
}
Iif (Array.isArray(value)) {
return value.map((part) => partToString(part, options)).join('');
}
// Cast to Part, assuming it might contain project-specific fields
const part = value as Part & {
videoMetadata?: unknown;
thought?: string;
codeExecutionResult?: unknown;
executableCode?: unknown;
};
Iif (options?.verbose) {
Iif (part.videoMetadata !== undefined) {
return `[Video Metadata]`;
}
Iif (part.thought !== undefined) {
return `[Thought: ${part.thought}]`;
}
Iif (part.codeExecutionResult !== undefined) {
return `[Code Execution Result]`;
}
Iif (part.executableCode !== undefined) {
return `[Executable Code]`;
}
// Standard Part fields
Iif (part.fileData !== undefined) {
return `[File Data]`;
}
Iif (part.functionCall !== undefined) {
return `[Function Call: ${part.functionCall.name}]`;
}
Iif (part.functionResponse !== undefined) {
return `[Function Response: ${part.functionResponse.name}]`;
}
Iif (part.inlineData !== undefined) {
return `<${part.inlineData.mimeType}>`;
}
}
return part.text ?? '';
}
export function getResponseText(
response: GenerateContentResponse,
): string | null {
Iif (response.candidates && response.candidates.length > 0) {
const candidate = response.candidates[0];
Iif (
candidate.content &&
candidate.content.parts &&
candidate.content.parts.length > 0
) {
return candidate.content.parts
.filter((part) => part.text)
.map((part) => part.text)
.join('');
}
}
return null;
}
/**
* Asynchronously maps over a PartListUnion, applying a transformation function
* to the text content of each text-based part.
*
* @param parts The PartListUnion to process.
* @param transform A function that takes a string of text and returns a Promise
* resolving to an array of new PartUnions.
* @returns A Promise that resolves to a new array of PartUnions with the
* transformations applied.
*/
export async function flatMapTextParts(
parts: PartListUnion,
transform: (text: string) => Promise<PartUnion[]>,
): Promise<PartUnion[]> {
const result: PartUnion[] = [];
const partArray = Array.isArray(parts)
? parts
: typeof parts === 'string'
? [{ text: parts }]
: [parts];
for (const part of partArray) {
let textToProcess: string | undefined;
if (typeof part === 'string') {
textToProcess = part;
} else Iif ('text' in part) {
textToProcess = part.text;
}
if (textToProcess !== undefined) {
const transformedParts = await transform(textToProcess);
result.push(...transformedParts);
} else {
// Pass through non-text parts unmodified.
result.push(part);
}
}
return result;
}
/**
* Appends a string of text to the last text part of a prompt, or adds a new
* text part if the last part is not a text part.
*
* @param prompt The prompt to modify.
* @param textToAppend The text to append to the prompt.
* @param separator The separator to add between existing text and the new text.
* @returns The modified prompt.
*/
export function appendToLastTextPart(
prompt: PartUnion[],
textToAppend: string,
separator = '\n\n',
): PartUnion[] {
Iif (!textToAppend) {
return prompt;
}
Iif (prompt.length === 0) {
return [{ text: textToAppend }];
}
const newPrompt = [...prompt];
const lastPart = newPrompt.at(-1);
if (typeof lastPart === 'string') {
newPrompt[newPrompt.length - 1] = `${lastPart}${separator}${textToAppend}`;
} else if (lastPart && 'text' in lastPart) {
newPrompt[newPrompt.length - 1] = {
...lastPart,
text: `${lastPart.text}${separator}${textToAppend}`,
};
} else {
newPrompt.push({ text: `${separator}${textToAppend}` });
}
return newPrompt;
}
|