feat(core): add partUtils module with unit tests (#4575)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
HyeongHo Jun
2025-07-23 08:57:06 +09:00
committed by GitHub
parent 487debe525
commit a00f1bb916
5 changed files with 255 additions and 175 deletions

View File

@@ -12,7 +12,7 @@ import {
} from '@google/genai';
import { GeminiClient } from '../core/client.js';
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
import { PartListUnion } from '@google/genai';
import { getResponseText, partToString } from './partUtils.js';
/**
* A function that summarizes the result of a tool execution.
@@ -40,40 +40,6 @@ export const defaultSummarizer: Summarizer = (
_abortSignal: AbortSignal,
) => Promise.resolve(JSON.stringify(result.llmContent));
// TODO: Move both these functions to utils
function partToString(part: PartListUnion): string {
if (!part) {
return '';
}
if (typeof part === 'string') {
return part;
}
if (Array.isArray(part)) {
return part.map(partToString).join('');
}
if ('text' in part) {
return part.text ?? '';
}
return '';
}
function getResponseText(response: GenerateContentResponse): string | null {
if (response.candidates && response.candidates.length > 0) {
const candidate = response.candidates[0];
if (
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;
}
const SUMMARIZE_TOOL_OUTPUT_PROMPT = `Summarize the following tool output to be a maximum of {maxOutputTokens} tokens. The summary should be concise and capture the main points of the tool output.
The summarization should be done based on the content that is provided. Here are the basic rules to follow: