LinkUp-P2P-Chat/commands.js

105 lines
3.4 KiB
JavaScript
Raw Normal View History

import b4a from 'b4a';
import fs from 'fs';
const agentAvatarPath = './assets/agent.png';
let agentAvatar = '';
// Load the agent avatar once when the module is imported
if (fs.existsSync(agentAvatarPath)) {
const avatarBuffer = fs.readFileSync(agentAvatarPath);
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
}
2024-06-14 01:10:23 -04:00
export default async function handleCommand(command, context) {
2024-07-07 22:39:31 -04:00
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom, listFiles, deleteFile, servePort } = context;
2024-06-20 00:46:24 -04:00
console.log("Context received in handleCommand:", context); // Add logging
2024-06-14 01:10:23 -04:00
const args = command.trim().split(' ');
const cmd = args[0].toLowerCase();
const restArgs = args.slice(1).join(' ');
2024-06-20 00:46:24 -04:00
console.log("Command received:", cmd); // Add logging
console.log("Current topic:", currentTopic); // Add logging to check the current topic
2024-06-14 01:10:23 -04:00
switch (cmd) {
2024-07-07 22:40:22 -04:00
case '>clear':
2024-06-20 00:46:24 -04:00
clearMessages(currentTopic);
break;
2024-07-07 22:40:22 -04:00
case '>ping':
2024-06-20 00:46:24 -04:00
addMessage('LinkUp', 'pong', agentAvatar, currentTopic);
break;
2024-07-07 22:40:22 -04:00
case '>help':
addMessage('LinkUp', 'Available commands:\n- >clear\n- >ping\n- >help\n- >join [topic]\n- >leave\n- >create [alias]\n- >list-files', agentAvatar, currentTopic);
2024-06-14 01:10:23 -04:00
break;
2024-07-07 22:40:22 -04:00
case '>join':
2024-06-14 01:10:23 -04:00
if (restArgs) {
2024-06-20 00:46:24 -04:00
await joinRoom(currentTopic, restArgs);
2024-06-14 01:10:23 -04:00
} else {
2024-07-07 22:40:22 -04:00
addMessage('LinkUp', 'Usage: >join [topic]', agentAvatar, currentTopic);
2024-06-14 01:10:23 -04:00
}
break;
2024-07-07 22:40:22 -04:00
case '>leave':
2024-06-20 00:46:24 -04:00
leaveRoom(currentTopic);
2024-06-14 01:10:23 -04:00
break;
2024-07-07 22:40:22 -04:00
case '>create':
2024-06-14 01:28:08 -04:00
if (restArgs) {
2024-06-20 00:46:24 -04:00
await createRoom(currentTopic, restArgs);
2024-06-14 01:28:08 -04:00
} else {
2024-07-07 22:40:22 -04:00
addMessage('LinkUp', 'Usage: >create [alias]', agentAvatar, currentTopic);
2024-06-14 01:28:08 -04:00
}
break;
2024-07-07 22:40:22 -04:00
case '>list-files':
const files = await listFiles();
const fileList = files.length > 0 ? files.map(file => `- ${file}`).join('\n') : 'No files available';
2024-06-20 00:46:24 -04:00
addMessage('LinkUp', `Available files:\n${fileList}`, agentAvatar, currentTopic);
2024-07-07 22:39:31 -04:00
// Render the file list with delete buttons
renderFileList(files, deleteFile, servePort);
break;
default:
2024-06-20 00:46:24 -04:00
addMessage('LinkUp', `Unknown command: ${cmd}`, agentAvatar, currentTopic);
console.log('Unknown command:', command);
}
2024-06-14 01:10:23 -04:00
}
2024-07-07 22:39:31 -04:00
function renderFileList(files, deleteFile, servePort) {
const container = document.querySelector('#messages');
if (!container) {
console.error('Element #messages not found');
return;
}
const fileList = document.createElement('ul');
files.forEach(file => {
const listItem = document.createElement('li');
const fileButton = document.createElement('button');
fileButton.textContent = file.trim();
fileButton.onclick = () => downloadFile(file.trim(), servePort);
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = async () => {
await deleteFile(file);
listItem.remove();
};
listItem.appendChild(fileButton);
listItem.appendChild(deleteButton);
fileList.appendChild(listItem);
});
container.appendChild(fileList);
}
function downloadFile(filename, servePort) {
const url = `http://localhost:${servePort}/files/${filename}`;
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}