-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (81 loc) · 2.72 KB
/
Copy pathserver.js
File metadata and controls
96 lines (81 loc) · 2.72 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
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync, existsSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load .env file if exists (simple dotenv alternative)
const envPath = join(__dirname, '.env');
if (existsSync(envPath)) {
const envContent = readFileSync(envPath, 'utf-8');
for (const line of envContent.split('\n')) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
if (key && valueParts.length > 0) {
process.env[key.trim()] = valueParts.join('=').trim();
}
}
}
}
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static(join(__dirname, 'public')));
// API endpoint to create Tavus conversation
app.post('/api/conversation', async (req, res) => {
const apiKey = process.env.TAVUS_API_KEY;
if (!apiKey) {
console.error('TAVUS_API_KEY is not set in environment variables');
return res.status(500).json({
error: 'Server configuration error',
detail: 'API key not configured'
});
}
const { replica_id, persona_id } = req.body;
if (!replica_id || !persona_id) {
return res.status(400).json({
error: 'Missing required fields',
detail: 'replica_id and persona_id are required'
});
}
try {
const response = await fetch('https://tavusapi.com/v2/conversations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey
},
body: JSON.stringify({
replica_id,
persona_id,
custom_greeting: "Salut ! On pratique le français. Tu es prêt(e) ?",
conversational_context: "You are a friendly French tutor. Speak ONLY in French. Level: A2. Correct gently. Offer 2 short suggested replies each turn.",
properties: {
language: "french",
enable_closed_captions: true
}
})
});
const data = await response.json();
if (!response.ok) {
console.error('Tavus API error:', data);
return res.status(response.status).json({
error: 'Tavus API error',
detail: data.message || JSON.stringify(data)
});
}
console.log('Conversation created:', data.conversation_id);
res.json({ conversation_url: data.conversation_url });
} catch (error) {
console.error('Error creating conversation:', error);
res.status(500).json({
error: 'Failed to create conversation',
detail: error.message
});
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});