-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopilotkit-runtime-server.js
More file actions
73 lines (61 loc) · 2.09 KB
/
copilotkit-runtime-server.js
File metadata and controls
73 lines (61 loc) · 2.09 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
/**
* CopilotKit Runtime Server
*
* This server acts as a bridge between the CopilotKit frontend and your AG-UI backend.
* It's needed because Vite (unlike Next.js) doesn't have API routes.
*/
const express = require('express');
const cors = require('cors');
const { CopilotRuntime, ExperimentalEmptyAdapter, copilotRuntimeNodeHttpEndpoint } = require('@copilotkit/runtime');
const { HttpAgent } = require('@ag-ui/client');
const app = express();
const PORT = 3001;
// Enable CORS for frontend
app.use(cors({
origin: 'http://localhost:5173',
credentials: true
}));
app.use(express.json());
// Create the CopilotRuntime with AG-UI agent connection
const serviceAdapter = new ExperimentalEmptyAdapter();
const runtime = new CopilotRuntime({
agents: {
// Connect to your FastAPI AG-UI endpoint
"orchestrator_agent": new HttpAgent({ url: "http://localhost:8000/copilot/" })
}
});
// Add request logging middleware
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
});
// CopilotKit endpoint
app.all('/copilotkit', async (req, res) => {
console.log('📨 CopilotKit request received');
console.log('Method:', req.method);
console.log('Operation:', req.body?.operationName);
try {
const handler = copilotRuntimeNodeHttpEndpoint({
runtime,
serviceAdapter,
endpoint: '/copilotkit'
});
await handler(req, res);
console.log('✅ Request handled successfully');
} catch (error) {
console.error('❌ Error handling request:', error);
console.error('Error stack:', error.stack);
if (!res.headersSent) {
res.status(500).json({ error: error.message, stack: error.stack });
}
}
});
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', message: 'CopilotKit Runtime Server' });
});
app.listen(PORT, () => {
console.log('🚀 CopilotKit Runtime Server running on http://localhost:' + PORT);
console.log('📡 Connecting to AG-UI backend at http://localhost:8000/copilot/');
console.log('🎯 Frontend should connect to http://localhost:3001/copilotkit');
});