import { SendSmsResponse, ElksMessage, AccountInfo } from './elks-client.js';
/**
* Format SMS response for MCP tool output
*/
export const formatSmsResponse = (response: SendSmsResponse, isDryRun: boolean): string => {
const mode = isDryRun ? '๐งช DRY RUN' : '๐ฑ SENT';
const cost = response.estimated_cost ?
`Estimated cost: ${response.estimated_cost / 10000} SEK` :
response.cost ? `Cost: ${response.cost / 10000} SEK` : 'Cost: N/A';
let output = `${mode} SMS Status: ${response.status}\n`;
output += `To: ${response.to}\n`;
output += `From: ${response.from}\n`;
output += `Message: ${response.message}\n`;
output += `${cost}\n`;
if (response.parts) {
output += `Message parts: ${response.parts}\n`;
}
if (response.id) {
output += `Message ID: ${response.id}\n`;
}
if (isDryRun) {
output += '\nโ ๏ธ This was a test - no actual SMS was sent';
}
return output;
};
/**
* Format SMS message history for MCP tool output
*/
export const formatSmsHistory = (messages: ElksMessage[], limit: number): string => {
if (messages.length === 0) {
return '๐ญ No SMS messages found';
}
let output = `๐ฑ SMS Message History (${messages.length} of ${limit} requested)\n\n`;
messages.forEach((msg, index) => {
const direction = msg.direction === 'outbound' ? '๐ค Sent' : '๐ฅ Received';
const cost = msg.cost ? ` (${msg.cost / 10000} SEK)` : '';
const date = new Date(msg.created).toLocaleString();
output += `${index + 1}. ${direction}${cost}\n`;
output += ` To: ${msg.to}\n`;
output += ` From: ${msg.from}\n`;
output += ` Status: ${msg.status}\n`;
output += ` Created: ${date}\n`;
output += ` Message: ${msg.message}\n`;
if (msg.id) {
output += ` ID: ${msg.id}\n`;
}
output += '\n';
});
return output.trim();
};
/**
* Format account balance information for MCP tool output
*/
export const formatAccountBalance = (accountInfo: AccountInfo): string => {
const balanceInCurrency = accountInfo.balance / 10000;
const formattedBalance = `${balanceInCurrency.toFixed(2)} ${accountInfo.currency}`;
let output = `๐ฐ Account Balance Information\n\n`;
output += `Balance: ${formattedBalance}\n`;
output += `Account: ${accountInfo.displayname}\n`;
output += `Mobile: ${accountInfo.mobilenumber}\n`;
output += `Email: ${accountInfo.email}\n`;
output += `Currency: ${accountInfo.currency}\n`;
output += `Account ID: ${accountInfo.id}\n\n`;
// Add balance status indication
if (balanceInCurrency < 10) {
output += `โ ๏ธ Low balance warning: Consider adding funds to your account\n`;
output += `๐ก Note: Adding funds must be done through the 46elks web interface at https://dashboard.46elks.com/`;
} else {
output += `โ
Balance sufficient for SMS sending`;
}
return output;
};
/**
* Calculate and format delivery statistics from messages
*/
export const formatDeliveryStatistics = (messages: ElksMessage[]): string => {
if (messages.length === 0) {
return '๐ No messages found to analyze';
}
// Filter only outbound messages (we can't analyze delivery for inbound)
const outboundMessages = messages.filter(msg => msg.direction === 'outbound');
if (outboundMessages.length === 0) {
return '๐ No outbound messages found to analyze delivery statistics';
}
// Count messages by status
const statusCounts = outboundMessages.reduce((counts, msg) => {
counts[msg.status] = (counts[msg.status] || 0) + 1;
return counts;
}, {} as Record<string, number>);
// Calculate costs
const totalCost = outboundMessages
.filter(msg => msg.cost)
.reduce((sum, msg) => sum + (msg.cost || 0), 0) / 10000;
const averageCost = totalCost / Math.max(outboundMessages.filter(msg => msg.cost).length, 1);
// Calculate success rate
const deliveredCount = statusCounts.delivered || 0;
const sentCount = statusCounts.sent || 0;
const successfulMessages = deliveredCount + sentCount;
const successRate = Math.round((successfulMessages / outboundMessages.length) * 100);
// Build statistics output
let output = `๐ SMS Delivery Statistics\n\n`;
output += `๐ Summary (Last ${outboundMessages.length} outbound messages)\n`;
output += `Success Rate: ${successRate}% (${successfulMessages}/${outboundMessages.length})\n`;
output += `Total Cost: ${totalCost.toFixed(2)} SEK\n`;
output += `Average Cost: ${averageCost.toFixed(2)} SEK per SMS\n\n`;
output += `๐ Message Status Breakdown:\n`;
Object.entries(statusCounts).forEach(([status, count]) => {
const percentage = Math.round((count / outboundMessages.length) * 100);
const statusEmoji = getStatusEmoji(status);
output += `${statusEmoji} ${status}: ${count} (${percentage}%)\n`;
});
output += `\n๐
Analysis Period: ${new Date(outboundMessages[outboundMessages.length - 1].created).toLocaleDateString()} - ${new Date(outboundMessages[0].created).toLocaleDateString()}`;
// Add recommendations
if (successRate < 90) {
output += `\n\nโ ๏ธ Low success rate detected. Consider:\n`;
output += `โข Verifying phone number formats\n`;
output += `โข Checking message content for blocked words\n`;
output += `โข Ensuring recipient numbers are active`;
} else {
output += `\n\nโ
Good delivery performance!`;
}
return output;
};
/**
* Get emoji for message status
*/
function getStatusEmoji(status: string): string {
switch (status) {
case 'delivered': return 'โ
';
case 'sent': return '๐ค';
case 'failed': return 'โ';
case 'created': return '๐';
default: return 'โ';
}
}