feat(commands): Enable @file processing in TOML commands (#6716)

This commit is contained in:
Abhi
2025-08-27 23:22:21 -04:00
committed by GitHub
parent 529c2649b8
commit bfdddcbd99
27 changed files with 1836 additions and 331 deletions

View File

@@ -8,6 +8,7 @@ import type {
GenerateContentResponse,
PartListUnion,
Part,
PartUnion,
} from '@google/genai';
/**
@@ -87,3 +88,82 @@ export function getResponseText(
}
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 if ('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[] {
if (!textToAppend) {
return prompt;
}
if (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;
}