terminal-chat-ui
Version:
Shared UI components for terminal-based chat interfaces using Theater actors
97 lines (96 loc) • 2.76 kB
JavaScript
/**
* Message formatting utilities
*/
/**
* Get default message prefixes based on UI variant
*/
export function getMessagePrefixes(variant = 'default') {
const base = {
user: '👤 You: ',
assistant: 'Assistant: ',
system: 'ℹ️ system: ',
tool: '[tool] '
};
switch (variant) {
case 'git':
return { ...base, system: '[git] ', assistant: 'Git Assistant: ' };
case 'chat':
return { ...base, assistant: 'Chat: ' };
default:
return base;
}
}
/**
* Get content color for message role
*/
export function getMessageColor(role, variant = 'default') {
const colors = {
user: 'gray',
assistant: 'white',
system: 'gray',
tool: 'magenta',
error: 'red'
};
return colors[role] || 'white';
}
/**
* Format message content for display
*/
export function formatMessageContent(content, maxLineLength) {
if (!maxLineLength)
return content;
return content
.split('\n')
.map(line => {
if (line.length <= maxLineLength)
return line;
// Simple word wrapping
const words = line.split(' ');
const wrappedLines = [];
let currentLine = '';
for (const word of words) {
if ((currentLine + ' ' + word).length <= maxLineLength) {
currentLine = currentLine ? currentLine + ' ' + word : word;
}
else {
if (currentLine)
wrappedLines.push(currentLine);
currentLine = word;
}
}
if (currentLine)
wrappedLines.push(currentLine);
return wrappedLines.join('\n');
})
.join('\n');
}
/**
* Extract tool information from message content
*/
export function extractToolInfo(content) {
// Simple tool detection - could be enhanced based on message format
const toolPattern = /\[tool\]\s*(\w+)(?::\s*(.+))?/;
const match = content.match(toolPattern);
if (match) {
const [, toolName, argsString] = match;
const toolArgs = argsString ? argsString.split(/\s+/) : [];
return { toolName, toolArgs };
}
return {};
}
/**
* Filter messages based on criteria
*/
export function filterMessages(messages, criteria) {
return messages.filter(message => {
if (criteria.role && message.role !== criteria.role)
return false;
if (criteria.status && message.status !== criteria.status)
return false;
if (criteria.hideEmpty && !message.content.trim())
return false;
if (criteria.hideSystem && message.role === 'system')
return false;
return true;
});
}