-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathtest-client.ts
More file actions
250 lines (211 loc) · 5.98 KB
/
test-client.ts
File metadata and controls
250 lines (211 loc) · 5.98 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* elizaOS GCP Cloud Run Test Client
*
* Interactive CLI client for testing the Cloud Run worker.
*
* Usage:
* bun run test-client.ts # Chat with local dev server
* bun run test-client.ts --url <url> # Custom URL
* bun run test-client.ts --check # Non-interactive availability check
*
* Examples:
* bun run test-client.ts
* bun run test-client.ts --url https://eliza-worker-abc123.run.app
*/
import * as readline from "node:readline";
interface ChatResponse {
response: string;
conversationId: string;
timestamp?: string;
}
interface InfoResponse {
name: string;
bio: string;
version: string;
powered_by: string;
endpoints: Record<string, string>;
}
interface HealthResponse {
status: string;
runtime: string;
version: string;
}
// Parse command line arguments
function parseArgs(): { checkOnly: boolean; url: string } {
const args = process.argv.slice(2);
let url = "http://localhost:8080";
let checkOnly = false;
for (let i = 0; i < args.length; i++) {
if ((args[i] === "--url" || args[i] === "-u") && args[i + 1]) {
url = args[i + 1];
i++;
} else if (args[i] === "--check") {
checkOnly = true;
} else if (args[i] === "--help" || args[i] === "-h") {
console.log(`
elizaOS GCP Cloud Run Test Client
Usage:
bun run test-client.ts [options]
Options:
--url, -u <url> Worker URL (default: http://localhost:8080)
--check Check worker availability, then exit without chat
--help, -h Show this help message
Examples:
# Local development (default port 8080)
bun run test-client.ts
# Connect to deployed Cloud Run service
bun run test-client.ts --url https://eliza-worker-abc123.run.app
Environment:
You can also set the URL via environment variable:
export ELIZA_WORKER_URL=https://your-service.run.app
bun run test-client.ts
`);
process.exit(0);
}
}
// Check environment variable as fallback
if (url === "http://localhost:8080" && process.env.ELIZA_WORKER_URL) {
url = process.env.ELIZA_WORKER_URL;
}
return { checkOnly, url };
}
async function getWorkerInfo(baseUrl: string): Promise<InfoResponse | null> {
try {
const response = await fetch(baseUrl, {
signal: AbortSignal.timeout(5000),
});
if (response.ok) {
return (await response.json()) as InfoResponse;
}
} catch {
return null;
}
return null;
}
async function getHealthStatus(
baseUrl: string,
): Promise<HealthResponse | null> {
try {
const response = await fetch(`${baseUrl}/health`, {
signal: AbortSignal.timeout(5000),
});
if (response.ok) {
return (await response.json()) as HealthResponse;
}
} catch {
return null;
}
return null;
}
async function sendMessage(
baseUrl: string,
message: string,
conversationId: string | null,
): Promise<ChatResponse> {
const response = await fetch(`${baseUrl}/chat`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message,
conversationId,
}),
});
if (!response.ok) {
const error = await response.text();
throw new Error(`API error: ${response.status} - ${error}`);
}
return (await response.json()) as ChatResponse;
}
async function main(): Promise<void> {
const { checkOnly, url } = parseArgs();
console.log("\n🚀 elizaOS GCP Cloud Run Test Client\n");
console.log(`📡 Connecting to: ${url}\n`);
// Check health status
const health = await getHealthStatus(url);
if (health) {
console.log(
`✅ Health: ${health.status} (${health.runtime} v${health.version})`,
);
}
// Check if worker is available
const info = await getWorkerInfo(url);
if (!info) {
if (checkOnly) {
console.log(`⚠️ Worker not available at ${url}`);
console.log(" Skipping integration tests (worker must be running)");
return;
}
console.error("❌ Could not connect to worker at", url);
console.error("\nMake sure the worker is running:");
console.error("\n Local:");
console.error(" cd packages/examples/gcp");
console.error(" bun run dev");
console.error("\n Or deploy to Cloud Run:");
console.error(
" gcloud run deploy eliza-worker --source . --region us-central1",
);
console.error("");
process.exit(1);
}
console.log(`\n🤖 Character: ${info.name}`);
console.log(`📖 Bio: ${info.bio}`);
console.log(`⚡ Powered by: ${info.powered_by}`);
if (checkOnly) {
console.log("\n✅ Worker availability check passed\n");
return;
}
console.log('\n💬 Chat with the agent (type "exit" or "quit" to leave)\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let conversationId: string | null = null;
const prompt = (): void => {
rl.question("You: ", async (input) => {
const text = input.trim();
if (
text.toLowerCase() === "exit" ||
text.toLowerCase() === "quit" ||
text.toLowerCase() === "q"
) {
console.log("\n👋 Goodbye!\n");
rl.close();
process.exit(0);
}
if (text.toLowerCase() === "clear") {
conversationId = null;
console.log("\n🔄 Conversation cleared. Starting fresh.\n");
prompt();
return;
}
if (text.toLowerCase() === "help") {
console.log(`
Commands:
exit, quit, q - Exit the client
clear - Clear conversation history
help - Show this help message
`);
prompt();
return;
}
if (!text) {
prompt();
return;
}
console.log("\n⏳ Thinking...");
const response = await sendMessage(url, text, conversationId);
conversationId = response.conversationId;
// Clear the "Thinking..." line
process.stdout.write("\x1b[1A\x1b[2K");
console.log(`${info.name}: ${response.response}\n`);
prompt();
});
};
prompt();
}
main().catch((err) => {
console.error("Fatal error:", err);
process.exit(1);
});