Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

tool-result-content.ts

31 lines1.1 KB

1import type { UIMessage } from '@tanstack/ai-react'
2​
3type ToolResultPart = Extract<
4 UIMessage['parts'][number],
5 { type: 'tool-result' }
6>
7​
8/** `string | Array<ContentPart>` — a tool result's raw content. */
9type ToolResultContent = ToolResultPart['content']
10​
11type ContentPartItem = Exclude<ToolResultContent, string>[number]
12​
13/**
14 * Reduce a tool-result part's `content` to a plain string for rendering.
15 *
16 * Tool results carry `string | Array<ContentPart>` (multimodal results are
17 * normalized to an array of content parts upstream). These demos render tool
18 * results as plain strings, so array content is flattened to the concatenation
19 * of its text parts; non-text parts (image, audio, video, document) have no
20 * string form here and are skipped.
21 */
22export function toolResultContentToString(content: ToolResultContent): string {
23 if (typeof content === 'string') return content
24 return content
25 .filter(
26 (part): part is Extract<ContentPartItem, { type: 'text' }> =>
27 part.type === 'text',
28 )
29 .map((part) => part.content)
30 .join('')