refine thinking part filtering

This commit is contained in:
Shantur Rathore
2025-10-30 16:31:13 +00:00
parent cf3f5a1209
commit 37a47699e3
4 changed files with 55 additions and 5 deletions

View File

@@ -31,3 +31,44 @@ export interface TextPart {
synthetic?: boolean
renderCache?: RenderCache
}
function hasTextSegment(segment: unknown): boolean {
if (typeof segment === "string") {
return segment.trim().length > 0
}
if (segment && typeof segment === "object") {
const maybeText = (segment as { text?: unknown }).text
if (typeof maybeText === "string") {
return maybeText.trim().length > 0
}
}
return false
}
export function partHasRenderableText(part: any): boolean {
if (!part || typeof part !== "object") {
return false
}
if (hasTextSegment(part.text)) {
return true
}
const contentArray = Array.isArray(part?.content) ? part.content : []
for (const item of contentArray) {
if (hasTextSegment(item)) {
return true
}
}
const thinkingContent = Array.isArray(part?.thinking?.content) ? part.thinking.content : []
for (const chunk of thinkingContent) {
if (hasTextSegment(chunk)) {
return true
}
}
return false
}