-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathhandler.ts
More file actions
321 lines (283 loc) · 7.57 KB
/
handler.ts
File metadata and controls
321 lines (283 loc) · 7.57 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* GCP Cloud Run handler for elizaOS chat worker
*
* This Cloud Run service processes chat messages and returns AI responses
* using the elizaOS runtime with OpenAI as the LLM provider.
*/
import {
createServer,
type IncomingMessage,
type ServerResponse,
} from "node:http";
import {
AgentRuntime,
ChannelType,
type Character,
createCharacter,
createMessageMemory,
stringToUuid,
type UUID,
} from "@elizaos/core";
import { v4 as uuidv4 } from "uuid";
// Types for request/response
interface ChatRequest {
message: string;
userId?: string;
conversationId?: string;
}
interface ChatResponse {
response: string;
conversationId: string;
timestamp: string;
}
interface HealthResponse {
status: "healthy" | "unhealthy";
runtime: string;
version: string;
}
interface InfoResponse {
name: string;
bio: string;
version: string;
powered_by: string;
endpoints: Record<string, string>;
}
// Character configuration from environment
function getCharacter(): Character {
return createCharacter({
name: process.env.CHARACTER_NAME ?? "Eliza",
bio: process.env.CHARACTER_BIO ?? "A helpful AI assistant.",
system:
process.env.CHARACTER_SYSTEM ??
"You are a helpful, concise AI assistant. Respond thoughtfully to user messages.",
});
}
// Singleton runtime instance
let runtime: AgentRuntime | null = null;
let initializationPromise: Promise<void> | null = null;
/**
* Initialize the elizaOS runtime (lazy, singleton pattern)
*/
async function initializeRuntime(): Promise<AgentRuntime> {
if (runtime) {
return runtime;
}
if (initializationPromise) {
await initializationPromise;
if (!runtime) {
throw new Error("elizaOS runtime initialization failed");
}
return runtime;
}
initializationPromise = (async () => {
console.log("Initializing elizaOS runtime...");
const character = getCharacter();
const [{ default: sqlPlugin }, { openaiPlugin }] = await Promise.all([
import("@elizaos/plugin-sql"),
import("@elizaos/plugin-openai"),
]);
runtime = new AgentRuntime({
character,
plugins: [sqlPlugin, openaiPlugin],
});
await runtime.initialize();
console.log("elizaOS runtime initialized successfully");
})();
await initializationPromise;
if (!runtime) {
throw new Error("elizaOS runtime initialization failed");
}
return runtime;
}
/**
* Parse JSON request body
*/
async function parseBody<T>(req: IncomingMessage): Promise<T> {
return new Promise((resolve, reject) => {
let body = "";
req.on("data", (chunk: Buffer) => {
body += chunk.toString();
});
req.on("end", () => {
try {
resolve(JSON.parse(body) as T);
} catch {
reject(new Error("Invalid JSON"));
}
});
req.on("error", reject);
});
}
/**
* Parse and validate chat request
*/
function validateChatRequest(body: Record<string, unknown>): ChatRequest {
if (typeof body.message !== "string" || !body.message.trim()) {
throw new Error("Message is required and must be a non-empty string");
}
return {
message: body.message.trim(),
userId: typeof body.userId === "string" ? body.userId : undefined,
conversationId:
typeof body.conversationId === "string" ? body.conversationId : undefined,
};
}
/**
* Send JSON response
*/
function sendJson(res: ServerResponse, statusCode: number, body: object): void {
res.writeHead(statusCode, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
});
res.end(JSON.stringify(body));
}
/**
* Handle chat message using full elizaOS runtime
*/
async function handleChat(request: ChatRequest): Promise<ChatResponse> {
const rt = await initializeRuntime();
// Generate IDs (using same pattern as AWS Lambda)
const userId = uuidv4() as UUID;
const conversationId =
request.conversationId ??
`conv-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
const roomId = stringToUuid(conversationId);
const worldId = stringToUuid("cloud-run-world");
// Ensure connection exists
await rt.ensureConnection({
entityId: userId,
roomId,
worldId,
userName: "User",
source: "gcp-cloud-run",
channelId: conversationId,
serverId: "cloud-run-worker",
type: ChannelType.DM,
} as Parameters<typeof rt.ensureConnection>[0]);
// Create message memory
const message = createMessageMemory({
id: uuidv4() as UUID,
entityId: userId,
roomId,
content: {
text: request.message,
source: "client_chat",
channelType: ChannelType.DM,
},
});
// Process message and collect response
let responseText = "";
await rt.messageService?.handleMessage(rt, message, async (content) => {
if (content?.text) {
responseText += content.text;
}
return [];
});
return {
response:
responseText || "I apologize, but I could not generate a response.",
conversationId,
timestamp: new Date().toISOString(),
};
}
/**
* Handle health check
*/
function handleHealth(): HealthResponse {
return {
status: "healthy",
runtime: "elizaos-typescript",
version: "1.0.0",
};
}
/**
* Handle info endpoint
*/
function handleInfo(): InfoResponse {
const character = getCharacter();
const bio = character.bio;
const bioStr = Array.isArray(bio)
? bio.join(" ")
: (bio ?? "A helpful AI assistant.");
return {
name: character.name ?? "elizaos",
bio: bioStr,
version: "1.0.0",
powered_by: "elizaOS",
endpoints: {
"POST /chat": "Send a message and receive a response",
"GET /health": "Health check endpoint",
"GET /": "This info endpoint",
},
};
}
/**
* Request router
*/
async function handleRequest(
req: IncomingMessage,
res: ServerResponse,
): Promise<void> {
const url = new URL(
req.url ?? "/",
`http://${req.headers.host ?? "localhost"}`,
);
const path = url.pathname;
const method = req.method ?? "GET";
console.log(`${method} ${path}`);
// Handle CORS preflight
if (method === "OPTIONS") {
sendJson(res, 200, { message: "OK" });
return;
}
// Info endpoint
if (path === "/" && method === "GET") {
sendJson(res, 200, handleInfo());
return;
}
// Health check
if (path === "/health" && method === "GET") {
sendJson(res, 200, handleHealth());
return;
}
// Chat endpoint
if (path === "/chat" && method === "POST") {
const body = await parseBody<Record<string, unknown>>(req);
const request = validateChatRequest(body);
const response = await handleChat(request);
sendJson(res, 200, response);
return;
}
// Not found
sendJson(res, 404, { error: "Not found", code: "NOT_FOUND" });
}
// Start server
const PORT = parseInt(process.env.PORT ?? "8080", 10);
const server = createServer((req, res) => {
handleRequest(req, res).catch((err) => {
console.error("Unhandled error:", err);
if (!res.headersSent) {
sendJson(res, 500, {
error: "Internal server error",
code: "INTERNAL_ERROR",
});
}
});
});
server.listen(PORT, "0.0.0.0", () => {
console.log(`🚀 elizaOS Cloud Run worker started on port ${PORT}`);
console.log(`📍 Health check: http://localhost:${PORT}/health`);
console.log(`💬 Chat endpoint: http://localhost:${PORT}/chat`);
});
// Graceful shutdown
process.on("SIGTERM", () => {
console.log("Received SIGTERM, shutting down...");
server.close(() => {
console.log("Server closed");
process.exit(0);
});
});
export { handleChat, handleHealth, handleInfo, handleRequest };