refactor entire frontend
This commit is contained in:
1 parent
49df72f2a1
commit
7b3326524e
17 files changed
+546
-96
No files matched your search
+131
@@ -0,0 +1,131 @@
|
||||
const WebSocket = require('ws');
|
||||
|
||||
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);
|
||||
|
||||
main();
|
||||
});
|
||||
|
||||
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 main() {
|
||||
const prompt = process.argv[2]
|
||||
let agents = (await send('get-agents')).agents;
|
||||
console.log("Agents", agents);
|
||||
|
||||
console.log("Send prompt:", prompt)
|
||||
let promptResponse = await send('send-agent-prompt', {
|
||||
body: {
|
||||
agentId: agents[0].id,
|
||||
content: "Hello World!",
|
||||
}
|
||||
});
|
||||
console.log("[AGENT]")
|
||||
console.log(promptResponse);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user