File size: 5,052 Bytes
e706de2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | /**
* Exercise 5: Build a Message Formatter
*
* Goal: Create a function that formats messages for console display
*
* Requirements:
* - Format each message type differently
* - Include timestamp in readable format
* - Add visual indicators (without emojis)
* - Truncate long messages (>100 chars)
* - Show message metadata
*
* Example Output:
* [10:30:45] SYSTEM: You are a helpful assistant
* [10:30:46] HUMAN: What's the weather?
* [10:30:47] AI: Let me check that for you...
*/
import { HumanMessage, AIMessage, SystemMessage, ToolMessage } from '../../../../src/index.js';
/**
* TODO: Implement formatMessage
*
* Should return a formatted string with:
* - Timestamp in [HH:MM:SS] format
* - Message type in UPPERCASE
* - Content (truncated if too long)
*
* @param {BaseMessage} message - The message to format
* @param {Object} options - Formatting options
* @returns {string} Formatted message string
*/
function formatMessage(message, options = {}) {
const maxLength = options.maxLength || 100;
// TODO: Extract timestamp and format as [HH:MM:SS]
// TODO: Get message type in uppercase
// TODO: Truncate content if longer than maxLength
// TODO: Return formatted string
}
/**
* TODO: Implement formatConversation
*
* Formats an entire conversation with proper spacing
*
* @param {Array<BaseMessage>} messages - Array of messages
* @returns {string} Formatted conversation
*/
function formatConversation(messages) {
// TODO: Format each message
// TODO: Join with newlines
// TODO: Add conversation header/footer
}
/**
* BONUS: Implement colorized output for terminal
*
* Add ANSI color codes for different message types:
* - System: Blue
* - Human: Green
* - AI: Cyan
* - Tool: Yellow
*/
function formatMessageWithColor(message, options = {}) {
// TODO: Add ANSI color codes based on message type
// TODO: Use formatMessage internally
}
// ============================================================================
// Tests
// ============================================================================
async function runTests() {
console.log('π§ͺ Testing Message Formatter...\n');
try {
// Test 1: Basic formatting
console.log('Test 1: Basic message formatting');
const msg1 = new HumanMessage("Hello, how are you?");
const formatted1 = formatMessage(msg1);
console.log(` Output: ${formatted1}`);
console.assert(formatted1.includes('HUMAN'), 'Should include message type');
console.assert(formatted1.includes('Hello'), 'Should include content');
console.log('β
Basic formatting works\n');
// Test 2: Long message truncation
console.log('Test 2: Long message truncation');
const longContent = 'A'.repeat(150);
const msg2 = new AIMessage(longContent);
const formatted2 = formatMessage(msg2, { maxLength: 50 });
console.log(` Output length: ${formatted2.length}`);
console.assert(formatted2.length < 100, 'Should truncate long messages');
console.log('β
Truncation works\n');
// Test 3: Different message types
console.log('Test 3: Different message types');
const messages = [
new SystemMessage("You are helpful"),
new HumanMessage("Hi"),
new AIMessage("Hello!"),
new ToolMessage("result", "tool_123")
];
messages.forEach(msg => {
const formatted = formatMessage(msg);
console.log(` ${formatted}`);
});
console.log('β
All message types format correctly\n');
// Test 4: Conversation formatting
console.log('Test 4: Full conversation formatting');
const conversation = [
new SystemMessage("You are a helpful assistant"),
new HumanMessage("What's 2+2?"),
new AIMessage("2+2 equals 4")
];
const formattedConv = formatConversation(conversation);
console.log(formattedConv);
console.assert(formattedConv.split('\n').length >= 3, 'Should have multiple lines');
console.log('β
Conversation formatting works\n');
// Test 5: Timestamp format
console.log('Test 5: Timestamp format');
const msg5 = new HumanMessage("Test");
const formatted5 = formatMessage(msg5);
const timestampRegex = /\[\d{2}:\d{2}:\d{2}\]/;
console.assert(timestampRegex.test(formatted5), 'Should have [HH:MM:SS] timestamp');
console.log('β
Timestamp format is correct\n');
console.log('π All tests passed!');
} catch (error) {
console.error('β Test failed:', error.message);
console.error(error.stack);
}
}
// Run tests
if (import.meta.url === `file://${process.argv[1]}`) {
runTests();
}
export { formatMessage, formatConversation, formatMessageWithColor }; |