-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (50 loc) · 2 KB
/
Copy pathserver.js
File metadata and controls
61 lines (50 loc) · 2 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
const express = require('express');
const cors = require('cors');
const { CopilotRuntime, GoogleGenerativeAIAdapter, EmptyAdapter, copilotRuntimeNodeExpressEndpoint } = require('@copilotkit/runtime');
const app = express();
const port = process.env.PORT || 3001;
app.use(cors());
app.use(express.json());
// Add request logging middleware
app.use((req, res, next) => {
if (req.path.startsWith('/api/copilotkit')) {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
}
next();
});
// Initialize CopilotRuntime without any agents
// This ensures the serviceAdapter is used directly, not the agent system
const copilotRuntime = new CopilotRuntime({
agents: {}, // Empty agents object to disable agent system
});
// Set Gemini API key as environment variable (required by the adapter)
process.env.GOOGLE_GENERATIVE_AI_API_KEY = process.env.GOOGLE_GENERATIVE_AI_API_KEY ;
// Choose adapter based on whether Gemini API key is configured
let adapter;
try {
// Use GoogleGenerativeAIAdapter with Gemini
adapter = new GoogleGenerativeAIAdapter({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
model: process.env.GEMINI_MODEL || 'gemini-2.0-flash-exp' // Default Gemini model
});
console.log('Gemini adapter initialized with model:', process.env.GEMINI_MODEL || 'gemini-2.0-flash-exp');
} catch (error) {
console.error('Error initializing Gemini adapter:', error.message);
adapter = new EmptyAdapter();
}
// CopilotKit endpoint - handles chat completions
// Mount the endpoint handler - it will handle all routes under /api/copilotkit
app.use('/api/copilotkit', copilotRuntimeNodeExpressEndpoint({
runtime: copilotRuntime,
serviceAdapter: adapter,
endpoint: '/api/copilotkit',
basePath: '/api/copilotkit',
}));
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});
app.listen(port, () => {
console.log(`CopilotKit server running on http://localhost:${port}`);
console.log(`API endpoint: http://localhost:${port}/api/copilotkit`);
});