-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
292 lines (258 loc) · 11.1 KB
/
server.js
File metadata and controls
292 lines (258 loc) · 11.1 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
import 'dotenv/config';
import express from 'express';
import path from 'path';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import { OctavusClient, toSSEStream } from '@octavus/server-sdk';
import {
deriveTitle,
readJsonFile,
writeJsonFile,
buildSessionInput,
buildSessionRecord,
filterModels,
} from './lib/helpers.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SESSIONS_FILE = path.join(__dirname, 'chat-sessions.json');
const CONFIG_FILE = path.join(__dirname, 'chat-config.json');
const MODELS_FILE = path.join(__dirname, 'current-models.txt');
const app = express();
const PORT = Number.parseInt(process.env.PORT ?? '3000', 10) || 3000;
// ── Octavus client ────────────────────────────────────────────
const octavus = new OctavusClient({
baseUrl: process.env.OCTAVUS_API_URL,
apiKey: process.env.OCTAVUS_API_KEY,
});
const AGENT_ID = process.env.OCTAVUS_AGENT_ID;
// ── Middleware ────────────────────────────────────────────────
app.use(express.json());
app.use('/design-system', express.static(path.join(__dirname, 'design-system')));
app.use(express.static(path.join(__dirname, 'public')));
// ── Config ────────────────────────────────────────────────────
const readConfig = () => readJsonFile(CONFIG_FILE);
app.get('/api/config', async (_req, res) => {
res.json(await readConfig());
});
// ── Models ─────────────────────────────────────────────────────
app.get('/api/models', async (_req, res) => {
try {
const [raw, config] = await Promise.all([
fs.readFile(MODELS_FILE, 'utf8'),
readConfig(),
]);
res.json({ models: filterModels(raw, config.allowedModels, config.allowedModelFamilies) });
} catch {
res.json({ models: [] });
}
});
// ── Session file helpers ──────────────────────────────────────
const readSessionsFile = () => readJsonFile(SESSIONS_FILE, { sessions: [] });
const writeSessionsFile = (data) => writeJsonFile(SESSIONS_FILE, data);
async function createNewSession(options = {}) {
const config = await readConfig();
const input = buildSessionInput(options, config);
console.log('[session] Creating with input:', JSON.stringify(input));
const sessionId = await octavus.agentSessions.create(AGENT_ID, input);
const record = buildSessionRecord(sessionId);
const data = await readSessionsFile();
// With history hidden there is only ever one conversation; drop the rest.
data.sessions = config.hideHistory ? [record] : [...data.sessions, record];
await writeSessionsFile(data);
return record;
}
// ── GET /api/sessions ─────────────────────────────────────────
// Lists all sessions (id, title, timestamps) sorted newest first.
app.get('/api/sessions', async (req, res) => {
const data = await readSessionsFile();
const list = data.sessions
.map((s) => ({
session_id: s.session_id,
title: deriveTitle(s.messages),
created_at: s.created_at,
updated_at: s.updated_at,
}))
.sort((a, b) => new Date(b.updated_at || b.created_at) - new Date(a.updated_at || a.created_at));
res.json({ sessions: list });
});
// ── GET /api/session ──────────────────────────────────────────
// Returns a specific session by ?id=, the most recently updated, or creates one.
app.get('/api/session', async (req, res) => {
if (!AGENT_ID) {
return res.status(503).json({ error: 'OCTAVUS_AGENT_ID is not configured' });
}
const data = await readSessionsFile();
const config = await readConfig();
// Load a specific session when ?id= is provided
if (req.query.id) {
const session = data.sessions.find((s) => s.session_id === req.query.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
return res.json({ sessionId: session.session_id, messages: session.messages });
}
// Resume the most recently updated session
if (data.sessions.length > 0) {
const latest = data.sessions.reduce((a, b) =>
(a.updated_at || a.created_at) > (b.updated_at || b.created_at) ? a : b,
);
// With history hidden, keep only the resumed session; discard any others.
if (config.hideHistory && data.sessions.length > 1) {
data.sessions = [latest];
await writeSessionsFile(data);
}
return res.json({ sessionId: latest.session_id, messages: latest.messages });
}
// No stored session — create one
try {
const record = await createNewSession();
res.json({ sessionId: record.session_id, messages: [] });
} catch (err) {
console.error('[session] Error creating session:', err);
res.status(500).json({ error: 'Failed to create session' });
}
});
// ── POST /api/sessions ────────────────────────────────────────
// Creates a brand-new Octavus session and adds it to the file.
app.post('/api/sessions', async (req, res) => {
if (!AGENT_ID) {
return res.status(503).json({ error: 'OCTAVUS_AGENT_ID is not configured' });
}
try {
const record = await createNewSession({
model: req.body.model,
temperature: req.body.temperature,
thinking: req.body.thinking,
});
res.json({ sessionId: record.session_id, messages: [] });
} catch (err) {
console.error('[sessions] Error creating session:', err);
res.status(500).json({ error: 'Failed to create session' });
}
});
// ── DELETE /api/sessions/:sessionId ──────────────────────────
app.delete('/api/sessions/:sessionId', async (req, res) => {
const { sessionId } = req.params;
const data = await readSessionsFile();
data.sessions = data.sessions.filter((s) => s.session_id !== sessionId);
await writeSessionsFile(data);
res.json({ ok: true });
});
// ── POST /api/session/fork ────────────────────────────────────
// Creates a new Octavus session, seeds it with the supplied messages, and
// removes the old session record. Used by regenerate / edit-and-resend.
app.post('/api/session/fork', async (req, res) => {
const { oldSessionId, messages, model, temperature, thinking } = req.body;
if (!oldSessionId || !Array.isArray(messages)) {
return res.status(400).json({ error: 'oldSessionId and messages[] are required' });
}
try {
const record = await createNewSession({ model, temperature, thinking });
const data = await readSessionsFile();
const idx = data.sessions.findIndex((s) => s.session_id === record.session_id);
if (idx >= 0) {
data.sessions[idx].messages = messages;
data.sessions[idx].updated_at = new Date().toISOString();
}
data.sessions = data.sessions.filter((s) => s.session_id !== oldSessionId);
await writeSessionsFile(data);
res.json({ sessionId: record.session_id });
} catch (err) {
console.error('[session/fork] Error:', err);
res.status(500).json({ error: 'Failed to fork session' });
}
});
// ── POST /api/session/save ────────────────────────────────────
// Receives the full serialised message list and writes it into chat-sessions.json.
app.post('/api/session/save', async (req, res) => {
const { sessionId, messages } = req.body;
if (!sessionId || !Array.isArray(messages)) {
return res.status(400).json({ error: 'sessionId and messages[] are required' });
}
try {
const data = await readSessionsFile();
const config = await readConfig();
const idx = data.sessions.findIndex((s) => s.session_id === sessionId);
const now = new Date().toISOString();
if (idx >= 0) {
data.sessions[idx].messages = messages;
data.sessions[idx].updated_at = now;
} else {
data.sessions.push({
session_id: sessionId,
created_at: now,
updated_at: now,
messages,
selected_submission: null,
});
}
// With history hidden, only the current conversation is ever persisted.
if (config.hideHistory) {
data.sessions = data.sessions.filter((s) => s.session_id === sessionId);
}
await writeSessionsFile(data);
res.json({ ok: true });
} catch (err) {
console.error('[session/save] Error:', err);
res.status(500).json({ error: 'Failed to save session' });
}
});
// ── POST /api/upload-urls ─────────────────────────────────────
// Proxies presigned S3 upload URL requests to Octavus.
app.post('/api/upload-urls', async (req, res) => {
const { sessionId, files } = req.body;
if (!sessionId || !Array.isArray(files)) {
return res.status(400).json({ error: 'sessionId and files[] are required' });
}
try {
const result = await octavus.files.getUploadUrls(sessionId, files);
res.json(result);
} catch (err) {
console.error('[upload-urls] Error:', err);
res.status(500).json({ error: 'Failed to get upload URLs' });
}
});
// ── POST /api/trigger ─────────────────────────────────────────
// Attaches to an existing session and streams the agent response as SSE.
app.post('/api/trigger', async (req, res) => {
const { sessionId, ...payload } = req.body;
if (!sessionId) {
return res.status(400).json({ error: 'sessionId is required' });
}
const session = octavus.agentSessions.attach(sessionId);
const events = session.execute(payload);
const stream = toSSEStream(events);
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
res.write(value);
}
} catch (err) {
console.error('[trigger] Stream error:', err);
} finally {
reader.releaseLock();
res.end();
}
});
if (process.env.NODE_ENV !== 'test') {
const server = app.listen(PORT, () => {
console.log(`ChatCPT running at http://localhost:${PORT}`);
if (!AGENT_ID) {
console.warn('[WARN] OCTAVUS_AGENT_ID is not set — chat will not work until it is configured.');
}
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(
`Port ${PORT} is already in use. Stop the other dev server (or any app on that port), or run with a different port, e.g. PORT=3001 npm run dev`,
);
} else {
console.error(err);
}
process.exit(1);
});
}
export { app };