import type { ImageContent, ToolResultMessage } from "@earendil-works/pi-ai"; import type { ToolCall } from "@berry/api/web.ts"; import { imageResult } from "@berry/api/content.ts"; const EMPTY: readonly ImageContent[] = []; export function isImageResult(value: unknown): value is ImageContent { return imageResult(value).errors === undefined; } /** The text and images of a model-facing result, including output produced before a failure. */ export function toolResult( message: ToolResultMessage | null, ): Pick { if (message === null) return { result: null, error: null, images: EMPTY }; const text = message.content.flatMap((block) => block.type === "text" && block.text ? [block.text] : [] ).join( "\n", ); return { result: message.isError ? null : text, error: message.isError ? text : null, images: message.content.filter((block) => block.type === "image"), }; }