-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.mjs
217 lines (186 loc) · 6.79 KB
/
server.mjs
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
import express from 'express';
import path from 'path';
import fs from 'fs/promises';
import open from 'open';
import http from 'http';
import { WebSocketServer, WebSocket } from 'ws';
import clipboardy from 'clipboardy';
import { fileURLToPath } from 'url';
import { promises as fsPromises } from 'fs';
import multer from 'multer';
import AdmZip from 'adm-zip';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Initialize Express app
const app = express();
const port = 3000;
// Ensure uploads directory exists
const uploadsDir = path.join(__dirname, 'uploads');
fs.mkdir(uploadsDir, { recursive: true }).catch(console.error);
// Set up multer for file uploads
const upload = multer({ dest: uploadsDir });
// Serve static files from the current directory
app.use(express.static(__dirname));
// Serve index.html for the root route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// New route to get animation files
app.get('/animations', async (req, res) => {
const animationsDir = path.join(__dirname, 'animations');
try {
const files = await fs.readdir(animationsDir);
const fbxFiles = files.filter(file => file.endsWith('.fbx'));
res.json(fbxFiles);
} catch (err) {
console.error('Error reading animations directory:', err);
res.status(500).json({ error: 'Unable to read animations directory' });
}
});
// Serve Vapi UMD bundle
app.get('/vapi-web-bundle.min.js', (req, res) => {
res.sendFile(path.join(__dirname, 'node_modules', '@vapi-ai', 'web', 'dist', 'vapi-web-bundle.min.js'));
});
// Load or create settings.json
const settingsPath = path.join(__dirname, 'settings.json');
let settings = { clipboardAccess: false };
async function loadOrCreateSettings() {
try {
await fs.access(settingsPath);
const data = await fs.readFile(settingsPath, 'utf8');
settings = JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
} else {
console.error('Error accessing settings file:', error);
}
}
}
// Call this function before setting up routes
await loadOrCreateSettings();
// Modify the /settings route
app.get('/settings', (req, res) => {
res.sendFile(path.join(__dirname, 'settings.html'));
});
// Add a new route to get and set the clipboard access setting
app.get('/api/settings/clipboard', (req, res) => {
res.json({ clipboardAccess: settings.clipboardAccess });
});
app.post('/api/settings/clipboard', express.json(), async (req, res) => {
settings.clipboardAccess = req.body.clipboardAccess;
try {
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
res.json({ success: true });
} catch (error) {
console.error('Error writing settings:', error);
res.status(500).json({ error: 'Unable to update settings' });
}
});
// Modify the /api/settings route
app.get('/api/settings', async (req, res) => {
try {
const settingsData = await fs.readFile(settingsPath, 'utf8');
res.json(JSON.parse(settingsData));
} catch (error) {
console.error('Error reading settings:', error);
res.status(500).json({ error: 'Unable to read settings' });
}
});
app.post('/api/settings', express.json(), async (req, res) => {
try {
const currentSettings = { ...settings };
// Update all possible settings
const possibleSettings = [
'clipboardAccess', 'vapiPublicKey', 'vapiPrivateKey',
'showTime', 'timeFormat', 'freeCamera', 'sceneDebug',
'dragDropSupport', 'vrmDebug', 'animationPicker', 'idleAnimation',
'characterName', 'assistantID', 'settingsIconToggle', 'assistantShortcut' // Add assistantShortcut here
];
possibleSettings.forEach(setting => {
if (req.body[setting] !== undefined) {
currentSettings[setting] = req.body[setting];
}
});
await fs.writeFile(settingsPath, JSON.stringify(currentSettings, null, 2));
settings = currentSettings;
res.json({ success: true });
} catch (error) {
console.error('Error updating settings:', error);
res.status(500).json({ error: 'Unable to update settings' });
}
});
// Add a new route to get character information
app.get('/api/characters', async (req, res) => {
const charactersDir = path.join(__dirname, 'characters');
try {
const files = await fsPromises.readdir(charactersDir);
const characters = files
.filter(file => file.endsWith('.vrm'))
.map(file => {
const name = path.parse(file).name;
const imagePath = files.includes(`${name}.png`)
? `/characters/${name}.png`
: '/images/Character_Card_Background.png';
return { name, imagePath };
});
res.json(characters);
} catch (err) {
console.error('Error reading characters directory:', err);
res.status(500).json({ error: 'Unable to read characters directory' });
}
});
// Add a new route to handle character uploads
app.post('/api/upload-characters', upload.array('characters'), async (req, res) => {
try {
for (const file of req.files) {
const oldPath = file.path;
const fileExtension = path.extname(file.originalname).toLowerCase();
if (fileExtension === '.zip') {
// Extract zip file
const zip = new AdmZip(oldPath);
zip.extractAllTo(path.join(__dirname, 'characters'), true);
} else if (['.png', '.vrm'].includes(fileExtension)) {
// Move png and vrm files
const newPath = path.join(__dirname, 'characters', file.originalname);
await fs.rename(oldPath, newPath);
}
// We're no longer attempting to delete the temporary file
}
res.json({ success: true });
} catch (error) {
console.error('Error processing uploaded files:', error);
res.status(500).json({ error: 'Failed to process uploaded files' });
}
});
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => {
console.log('Client disconnected');
});
});
let lastClipboardContent = '';
// Modify the clipboard checking interval
const checkClipboard = () => {
if (settings.clipboardAccess) {
clipboardy.read().then(text => {
if (text !== lastClipboardContent) {
console.log('Clipboard changed:', text);
lastClipboardContent = text;
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'clipboard', content: text }));
}
});
}
}).catch(console.error);
}
};
setInterval(checkClipboard, 1000);
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
console.log(`Settings page available at http://localhost:${port}/settings`);
open(`http://localhost:${port}`);
});