121 lines
2.9 KiB
JavaScript
121 lines
2.9 KiB
JavaScript
const WebSocket = require('ws');
|
|
|
|
const USE_LLM = false;
|
|
|
|
const ws = new WebSocket('ws://localhost:5000/ws');
|
|
var nonceCounter = 1;
|
|
var nonceCallbacks = {};
|
|
var messageQueue = [];
|
|
var messageResolverQueue = [];
|
|
|
|
// Setup
|
|
|
|
ws.on('open', function open() {
|
|
const packet = JSON.stringify({ username: "test-user", password: "123456" });
|
|
ws.send(packet);
|
|
|
|
runTest();
|
|
});
|
|
|
|
ws.on('message', function incoming(rawMessage) {
|
|
const parsed = JSON.parse(rawMessage);
|
|
const callback = nonceCallbacks[parsed.nonce];
|
|
if (callback) {
|
|
callback(parsed);
|
|
} else {
|
|
if (messageResolverQueue.length > 0) {
|
|
const resolver = messageResolverQueue.shift();
|
|
resolver(parsed);
|
|
} else {
|
|
messageQueue.push(parsed);
|
|
}
|
|
}
|
|
});
|
|
|
|
async function send (action, message) {
|
|
const nonce = nonceCounter++;
|
|
let promise = new Promise((resolve, reject) => {
|
|
nonceCallbacks[nonce] = res => {
|
|
if (res.error) reject(Error(res.error));
|
|
else resolve(res);
|
|
};;
|
|
});
|
|
|
|
const packet = JSON.stringify({ nonce, action, ...message });
|
|
ws.send(packet);
|
|
return promise;
|
|
}
|
|
|
|
function waitMessage() {
|
|
return new Promise((resolve) => {
|
|
if (messageQueue.length > 0) {
|
|
resolve(messageQueue.shift());
|
|
} else {
|
|
messageResolverQueue.push(resolve);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
// Testing
|
|
|
|
async function runTest() {
|
|
console.log("Initial Chats",
|
|
await send('get-chats')
|
|
);
|
|
|
|
console.log("Create Agent",
|
|
await send('create-agent', {body: {
|
|
name: 'First Agent',
|
|
description: 'First agent does nothing',
|
|
kind: 'simple',
|
|
data: {}
|
|
}})
|
|
);
|
|
|
|
let agents = (await send('get-agents')).agents;
|
|
console.log("Get Agents", agents);
|
|
|
|
if (USE_LLM) {
|
|
let promptResponse = await send('send-agent-prompt', {
|
|
body: {
|
|
agentId: agents[0].id,
|
|
content: "Hello World!",
|
|
}
|
|
});
|
|
console.log("Prompt Response", promptResponse);
|
|
}
|
|
|
|
await testChats();
|
|
|
|
process.exit();
|
|
}
|
|
|
|
async function testChats () {
|
|
let response = await send('get-chats')
|
|
let chat = response.chats[0];
|
|
|
|
if (USE_LLM) {
|
|
let msgRes = await send('send-chat-message', {
|
|
body: {
|
|
chatId: chat.id,
|
|
content: "Whats up!",
|
|
}
|
|
});
|
|
console.log('message sent', msgRes);
|
|
console.log('Incoming:', await waitMessage());
|
|
|
|
// Multi turn conversation
|
|
await send('send-chat-message', {
|
|
body: {
|
|
chatId: chat.id,
|
|
content: "Spell the initials of my first message.",
|
|
}
|
|
});
|
|
console.log('Incoming:', await waitMessage());
|
|
|
|
console.log('Messages in Chat', chat.id, await send('get-chat-messages', {
|
|
body: {chatId: chat.id}
|
|
}));
|
|
}
|
|
} |