|
| 1 | +import type { ChatMessageExtended } from '@/shared/types/store' |
| 2 | + |
| 3 | +/** |
| 4 | + * Formats message to markdown format for copying |
| 5 | + */ |
| 6 | +export function formatMessageToMarkdown(message: ChatMessageExtended): string { |
| 7 | + const lines: string[] = [] |
| 8 | + |
| 9 | + // Message header |
| 10 | + if (message.role === 'user') { |
| 11 | + lines.push('## 👤 User\n') |
| 12 | + } else if (message.role === 'assistant') { |
| 13 | + lines.push('## 🤖 Assistant\n') |
| 14 | + } else { |
| 15 | + lines.push('## System\n') |
| 16 | + } |
| 17 | + |
| 18 | + // Main content |
| 19 | + if (message.content && message.content.length > 0) { |
| 20 | + message.content.forEach((content) => { |
| 21 | + // content can be string or ReasoningStep object |
| 22 | + if (typeof content === 'string') { |
| 23 | + lines.push(content) |
| 24 | + } else if (content && typeof content === 'object') { |
| 25 | + // If it's an object (ReasoningStep), convert to string |
| 26 | + lines.push(JSON.stringify(content, null, 2)) |
| 27 | + } |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + // Agent Reasoning (if exists) |
| 32 | + if (message.agentReasoning) { |
| 33 | + lines.push('\n### 🧠 Reasoning\n') |
| 34 | + |
| 35 | + if (message.agentReasoning.reasoning) { |
| 36 | + lines.push('**Reasoning:**') |
| 37 | + lines.push(String(message.agentReasoning.reasoning)) |
| 38 | + lines.push('') |
| 39 | + } |
| 40 | + |
| 41 | + if (message.agentReasoning.steps && Array.isArray(message.agentReasoning.steps)) { |
| 42 | + lines.push('**Steps:**\n') |
| 43 | + message.agentReasoning.steps.forEach((step: any, index: number) => { |
| 44 | + lines.push(`${index + 1}. **${step.action || step.title || 'Action'}**`) |
| 45 | + if (step.reasoning) { |
| 46 | + lines.push(` - Reasoning: ${step.reasoning}`) |
| 47 | + } |
| 48 | + if (step.result || step.content) { |
| 49 | + lines.push(` - Result: ${step.result || step.content}`) |
| 50 | + } |
| 51 | + lines.push('') |
| 52 | + }) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Tool History (if exists) |
| 57 | + if (message.toolHistory && message.toolHistory.length > 0) { |
| 58 | + lines.push('\n### 🔧 Tool History\n') |
| 59 | + message.toolHistory.forEach((tool, index) => { |
| 60 | + lines.push(`#### Tool ${index + 1}: ${tool.tool_name || 'Unknown'}`) |
| 61 | + if (tool.tool_call_id) { |
| 62 | + lines.push(`- Call ID: \`${tool.tool_call_id}\``) |
| 63 | + } |
| 64 | + lines.push('- Content:') |
| 65 | + lines.push('```') |
| 66 | + lines.push(tool.content || '') |
| 67 | + lines.push('```') |
| 68 | + lines.push('') |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + // Timestamp |
| 73 | + if (message.timestamp) { |
| 74 | + const date = new Date(message.timestamp) |
| 75 | + lines.push(`\n---`) |
| 76 | + lines.push(`*${date.toLocaleString()}*`) |
| 77 | + } |
| 78 | + |
| 79 | + return lines.join('\n') |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Copies text to clipboard |
| 84 | + */ |
| 85 | +export async function copyToClipboard(text: string): Promise<boolean> { |
| 86 | + try { |
| 87 | + // Modern API |
| 88 | + if (navigator.clipboard && navigator.clipboard.writeText) { |
| 89 | + await navigator.clipboard.writeText(text) |
| 90 | + return true |
| 91 | + } |
| 92 | + |
| 93 | + // Fallback for old browsers |
| 94 | + const textArea = document.createElement('textarea') |
| 95 | + textArea.value = text |
| 96 | + textArea.style.position = 'fixed' |
| 97 | + textArea.style.left = '-999999px' |
| 98 | + textArea.style.top = '-999999px' |
| 99 | + document.body.appendChild(textArea) |
| 100 | + textArea.focus() |
| 101 | + textArea.select() |
| 102 | + |
| 103 | + const successful = document.execCommand('copy') |
| 104 | + document.body.removeChild(textArea) |
| 105 | + |
| 106 | + return successful |
| 107 | + } catch (error) { |
| 108 | + console.error('Failed to copy to clipboard:', error) |
| 109 | + return false |
| 110 | + } |
| 111 | +} |
0 commit comments