forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestLibrary.mjs
93 lines (79 loc) · 2.52 KB
/
testLibrary.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { $, fs, path, chalk } from 'zx';
import { DEFAULT_AGENT_ID, DEFAULT_CHARACTER } from './test1.mjs';
import { spawn } from 'node:child_process';
$.verbose = false; // Suppress command output unless there's an error
function projectRoot() {
return path.join(import.meta.dirname, "..");
}
async function runProcess(command, args = [], directory = projectRoot()) {
try {
const result = await $`cd ${directory} && ${command} ${args}`;
return result.stdout.trim();
} catch (error) {
throw new Error(`Command failed: ${error.message}`);
}
}
async function installProjectDependencies() {
console.log(chalk.blue('Installing dependencies...'));
return await runProcess('pnpm', ['install', '-r']);
}
async function buildProject() {
console.log(chalk.blue('Building project...'));
return await runProcess('pnpm', ['build']);
}
async function writeEnvFile(entries) {
const envContent = Object.entries(entries)
.map(([key, value]) => `${key}=${value}`)
.join('\n');
await fs.writeFile('.env', envContent);
}
async function startAgent(character = DEFAULT_CHARACTER) {
console.log(chalk.blue(`Starting agent for character: ${character}`));
const proc = spawn('pnpm', ['start', `--character=characters/${character}.character.json`, '--non-interactive'], { shell: true, "stdio": "inherit" });
log(`proc=${JSON.stringify(proc)}`);
// Wait for server to be ready
await new Promise(resolve => setTimeout(resolve, 20000));
return proc;
}
async function stopAgent(proc) {
console.log(chalk.blue('Stopping agent...'));
proc.kill('SIGTERM')
}
async function send(message) {
const endpoint = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
const payload = {
text: message,
userId: "user",
userName: "User"
};
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data[0].text;
} catch (error) {
throw new Error(`Failed to send message: ${error.message}`);
}
}
function log(message) {
console.log(message);
}
export {
projectRoot,
runProcess,
installProjectDependencies,
buildProject,
writeEnvFile,
startAgent,
stopAgent,
send,
log
}