llama-stack-mirror/llama_stack/ui/lib/format-message-content.ts
ehhuang 56e5ddb39f
feat(ui): add views for Responses (#2293)
# What does this PR do?
* Add responses list and detail views
* Refactored components to be shared as much as possible between chat
completions and responses

## Test Plan
<img width="2014" alt="image"
src="https://github.com/user-attachments/assets/6dee12ea-8876-4351-a6eb-2338058466ef"
/>
<img width="2021" alt="image"
src="https://github.com/user-attachments/assets/6c7c71b8-25b7-4199-9c57-6960be5580c8"
/>

added tests
2025-05-28 09:51:22 -07:00

65 lines
1.7 KiB
TypeScript

import { ChatMessage, ChatMessageContentPart } from "@/lib/types";
import { formatToolCallToString } from "@/lib/format-tool-call";
export function extractTextFromContentPart(
content: string | ChatMessageContentPart[] | null | undefined,
): string {
if (content === null || content === undefined) {
return "";
}
if (typeof content === "string") {
return content;
} else if (Array.isArray(content)) {
const parts: string[] = [];
for (const item of content) {
if (
item &&
typeof item === "object" &&
item.type === "text" &&
typeof item.text === "string"
) {
parts.push(item.text);
} else if (
item &&
typeof item === "object" &&
item.type === "image_url"
) {
parts.push("[Image]"); // Placeholder for images
} else if (typeof item === "string") {
// Handle cases where an array might contain plain strings
parts.push(item);
}
}
return parts.join(" ");
} else {
return content;
}
}
export function extractDisplayableText(
message: ChatMessage | undefined | null,
): string {
if (!message) {
return "";
}
const textPart = extractTextFromContentPart(message.content);
let toolCallPart = "";
if (
message.tool_calls &&
Array.isArray(message.tool_calls) &&
message.tool_calls.length > 0
) {
// For summary, usually the first tool call is sufficient
toolCallPart = formatToolCallToString(message.tool_calls[0]);
}
if (textPart && toolCallPart) {
return `${textPart} ${toolCallPart}`;
} else if (toolCallPart) {
return toolCallPart;
} else {
return textPart; // textPart will be "" if message.content was initially null/undefined/empty array etc.
}
}