(msg: Message, options: ExportOptions)
| 2 | import { extractTextContent } from "../utils"; |
| 3 | |
| 4 | function renderMessage(msg: Message, options: ExportOptions): string { |
| 5 | const parts: string[] = []; |
| 6 | |
| 7 | const roleLabel = |
| 8 | msg.role === "user" |
| 9 | ? "**User**" |
| 10 | : msg.role === "assistant" |
| 11 | ? "**Assistant**" |
| 12 | : `**${msg.role}**`; |
| 13 | |
| 14 | parts.push(`### ${roleLabel}`); |
| 15 | |
| 16 | if (options.includeTimestamps) { |
| 17 | parts.push(`_${new Date(msg.createdAt).toLocaleString()}_\n`); |
| 18 | } |
| 19 | |
| 20 | if (typeof msg.content === "string") { |
| 21 | parts.push(msg.content); |
| 22 | } else { |
| 23 | for (const block of msg.content) { |
| 24 | if (block.type === "text") { |
| 25 | parts.push(block.text); |
| 26 | } else if (block.type === "tool_use" && options.includeToolUse) { |
| 27 | parts.push( |
| 28 | `\`\`\`tool-use\n// Tool: ${block.name}\n${JSON.stringify(block.input, null, 2)}\n\`\`\`` |
| 29 | ); |
| 30 | } else if (block.type === "tool_result" && options.includeToolUse) { |
| 31 | const raw = |
| 32 | typeof block.content === "string" |
| 33 | ? block.content |
| 34 | : extractTextContent(block.content); |
| 35 | const truncated = |
| 36 | !options.includeFileContents && raw.length > 500 |
| 37 | ? raw.slice(0, 500) + "\n…[truncated]" |
| 38 | : raw; |
| 39 | parts.push( |
| 40 | `\`\`\`tool-result${block.is_error ? " error" : ""}\n${truncated}\n\`\`\`` |
| 41 | ); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return parts.join("\n\n"); |
| 47 | } |
| 48 | |
| 49 | export function toMarkdown(conv: Conversation, options: ExportOptions): string { |
| 50 | const lines: string[] = [ |
no test coverage detected