`;
+ html = `";
- // Handle ToolResultMessage separately
- if (role === "toolResult") {
- const isError = message.isError;
- html += `
`;
- }
- // Handle string content (for user messages)
- else if (typeof message.content === "string") {
- const text = escapeHtml(message.content);
- html += `
${text.replace(/\n/g, "
")}
`;
} else {
- // Handle array content
- for (const content of message.content) {
- if (typeof content === "string") {
- // Handle legacy string content
- const text = escapeHtml(content);
- html += `
${text.replace(/\n/g, "
")}
`;
- } else if (content.type === "text") {
- // Format text with markdown-like rendering
- const text = escapeHtml(content.text);
- html += `
${text.replace(/\n/g, "
")}
`;
- } else if (content.type === "thinking") {
- html += `
`;
- html += `Thinking...
`;
- html += `${escapeHtml(content.thinking).replace(/\n/g, "
")}
`;
- html += ` `;
- } else if (content.type === "toolCall") {
- html += `
`;
- } else if (content.type === "image") {
- const imageData = content.data;
- const mimeType = content.mimeType || "image/png";
- html += `

`;
+ // Generic tool
+ html = ``;
+ html += `
`;
+
+ if (result) {
+ const output = getTextOutput();
+ if (output) {
+ html += `
`;
+ }
+ }
+ }
+
+ return { html, bgColor };
+}
+
+/**
+ * Format a message as HTML (matching TUI component styling)
+ */
+function formatMessage(message: Message, toolResultsMap: Map
): string {
+ let html = "";
+
+ if (message.role === "user") {
+ const userMsg = message as UserMessage;
+ let textContent = "";
+
+ if (typeof userMsg.content === "string") {
+ textContent = userMsg.content;
+ } else {
+ const textBlocks = userMsg.content.filter((c) => c.type === "text");
+ textContent = textBlocks.map((c: any) => c.text).join("");
+ }
+
+ if (textContent.trim()) {
+ html += `${escapeHtml(textContent).replace(/\n/g, "
")}
`;
+ }
+ } else if (message.role === "assistant") {
+ const assistantMsg = message as AssistantMessage;
+
+ // Render text and thinking content
+ for (const content of assistantMsg.content) {
+ if (content.type === "text" && content.text.trim()) {
+ html += `${escapeHtml(content.text.trim()).replace(/\n/g, "
")}
`;
+ } else if (content.type === "thinking" && content.thinking.trim()) {
+ html += `${escapeHtml(content.thinking.trim()).replace(/\n/g, "
")}
`;
+ }
+ }
+
+ // Render tool calls with their results
+ for (const content of assistantMsg.content) {
+ if (content.type === "toolCall") {
+ const toolResult = toolResultsMap.get(content.id);
+ const { html: toolHtml, bgColor } = formatToolExecution(content.name, content.arguments, toolResult);
+ html += `${toolHtml}
`;
+ }
+ }
+
+ // Show error/abort status if no tool calls
+ const hasToolCalls = assistantMsg.content.some((c) => c.type === "toolCall");
+ if (!hasToolCalls) {
+ if (assistantMsg.stopReason === "aborted") {
+ html += 'Aborted
';
+ } else if (assistantMsg.stopReason === "error") {
+ const errorMsg = assistantMsg.errorMessage || "Unknown error";
+ html += `Error: ${escapeHtml(errorMsg)}
`;
}
}
}
- html += `