|
| 1 | +/** |
| 2 | + * Utility functions for formatting conversation messages for console output |
| 3 | + */ |
| 4 | + |
| 5 | +export interface ConversationMessage { |
| 6 | + role: string; |
| 7 | + content: any; |
| 8 | + toolInvocations?: any[]; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Format a conversation array into human-readable console output |
| 13 | + */ |
| 14 | +export function formatConversation(messages: ConversationMessage[], prompt?: string): string[] { |
| 15 | + const output: string[] = []; |
| 16 | + |
| 17 | + // Add horizontal line at the start |
| 18 | + output.push(formatText('─'.repeat(60))); |
| 19 | + |
| 20 | + // Include initial prompt if provided |
| 21 | + if (prompt) { |
| 22 | + output.push(formatRole('user')); |
| 23 | + output.push(formatText(prompt)); |
| 24 | + output.push(''); |
| 25 | + } |
| 26 | + |
| 27 | + messages.forEach(msg => { |
| 28 | + // Format role header |
| 29 | + const roleDisplay = formatRole(msg.role); |
| 30 | + output.push(roleDisplay); |
| 31 | + |
| 32 | + // Format content |
| 33 | + const contentLines = formatContent(msg.content); |
| 34 | + output.push(...contentLines); |
| 35 | + |
| 36 | + // Handle legacy tool invocations |
| 37 | + if (msg.toolInvocations && msg.toolInvocations.length > 0) { |
| 38 | + msg.toolInvocations.forEach(tool => { |
| 39 | + const toolLine = formatToolCall(tool.toolName, tool.args); |
| 40 | + output.push(toolLine); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + // Add space between messages |
| 45 | + output.push(''); |
| 46 | + }); |
| 47 | + |
| 48 | + // Add horizontal line at the end |
| 49 | + output.push(formatText('─'.repeat(60))); |
| 50 | + |
| 51 | + return output; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Format role with appropriate colors |
| 56 | + */ |
| 57 | +export function formatRole(role: string): string { |
| 58 | + switch (role) { |
| 59 | + case 'user': |
| 60 | + return '\x1b[32m[USER]\x1b[0m'; // Green |
| 61 | + case 'assistant': |
| 62 | + return '\x1b[36m[ASSISTANT]\x1b[0m'; // Cyan |
| 63 | + case 'system': |
| 64 | + return '\x1b[33m[SYSTEM]\x1b[0m'; // Yellow |
| 65 | + case 'tool': |
| 66 | + return '\x1b[35m[TOOL]\x1b[0m'; // Magenta |
| 67 | + default: |
| 68 | + return `\x1b[37m[${role.toUpperCase()}]\x1b[0m`; // White |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Format text content with darker color |
| 74 | + */ |
| 75 | +export function formatText(text: string): string { |
| 76 | + return `\x1b[90m${text}\x1b[0m`; // Dark gray/dim |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Format content based on type |
| 81 | + */ |
| 82 | +export function formatContent(content: any): string[] { |
| 83 | + const lines: string[] = []; |
| 84 | + |
| 85 | + if (typeof content === 'string') { |
| 86 | + lines.push(formatText(content)); |
| 87 | + } else if (Array.isArray(content)) { |
| 88 | + // Handle content array (multimodal messages) |
| 89 | + content.forEach((part: any) => { |
| 90 | + if (part.type === 'text') { |
| 91 | + lines.push(formatText(part.text)); |
| 92 | + } else if (part.type === 'tool-call') { |
| 93 | + const toolLine = formatToolCall(part.toolName, part.args); |
| 94 | + lines.push(toolLine); |
| 95 | + } else if (part.type === 'tool-result') { |
| 96 | + const resultLine = formatToolResult(part.result); |
| 97 | + lines.push(resultLine); |
| 98 | + } |
| 99 | + }); |
| 100 | + } else { |
| 101 | + // Fallback for other content types |
| 102 | + lines.push(formatText(JSON.stringify(content))); |
| 103 | + } |
| 104 | + |
| 105 | + return lines; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Format tool call |
| 110 | + */ |
| 111 | +export function formatToolCall(toolName: string, args: any): string { |
| 112 | + const argsStr = JSON.stringify(args); |
| 113 | + return formatText(`🔧 Tool Call: ${toolName}(${argsStr})`); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Format tool result |
| 118 | + */ |
| 119 | +export function formatToolResult(result: any): string { |
| 120 | + const resultStr = JSON.stringify(result); |
| 121 | + const truncated = resultStr.length > 200 ? `${resultStr.substring(0, 200)}...` : resultStr; |
| 122 | + return formatText(`✅ Tool Result: ${truncated}`); |
| 123 | +} |
0 commit comments