Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -21,7 +21,7 @@ app.use(express.static(path.join(__dirname, 'public')));
|
|
| 21 |
// --- LOGGER HELPER ---
|
| 22 |
const log = {
|
| 23 |
info: (msg, id = "SYS") => console.log(`[${new Date().toISOString()}] [INFO] [${id}] ${msg}`),
|
| 24 |
-
warn: (msg, id = "SYS") => console.warn(`[${new Date().toISOString()}] ⚠️
|
| 25 |
error: (msg, err, id = "SYS") => console.error(`[${new Date().toISOString()}] ❌ [ERROR] [${id}] ${msg}`, err?.message || err, err?.stack || "")
|
| 26 |
};
|
| 27 |
|
|
@@ -77,13 +77,14 @@ async function initDB() {
|
|
| 77 |
|
| 78 |
const rowsToUpsert = toSync.map(id => {
|
| 79 |
const chat = memoryChats[id];
|
|
|
|
| 80 |
chat.updatedAt = new Date().toISOString();
|
| 81 |
return {
|
| 82 |
id: chat.id, title: chat.title, totalTokens: chat.totalTokens,
|
| 83 |
inputTokens: chat.inputTokens, outputTokens: chat.outputTokens,
|
| 84 |
messages: chat.messages, updatedAt: chat.updatedAt
|
| 85 |
};
|
| 86 |
-
});
|
| 87 |
|
| 88 |
if (rowsToUpsert.length > 0) {
|
| 89 |
const { error } = await supabase.from('chats').upsert(rowsToUpsert);
|
|
@@ -237,7 +238,6 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 237 |
|
| 238 |
historicalMessages.push({ role: "user", content: contentBlock });
|
| 239 |
|
| 240 |
-
// THE FIX: Uncapped Token limit for Claude 3.7 to allow massive reasoning + coding
|
| 241 |
const commandMaxTokens = model.includes("claude") ? 64000 : 8192;
|
| 242 |
|
| 243 |
const command = new ConverseStreamCommand({
|
|
@@ -245,10 +245,6 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 245 |
system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
|
| 246 |
messages: historicalMessages,
|
| 247 |
inferenceConfig: { maxTokens: commandMaxTokens, temperature: 1 },
|
| 248 |
-
/* additionalModelRequestFields: model.includes("claude") ? {
|
| 249 |
-
thinking: { type: "adaptive" }
|
| 250 |
-
} : undefined
|
| 251 |
-
*/
|
| 252 |
});
|
| 253 |
|
| 254 |
const response = await bedrockClient.send(command, { abortSignal: abortController.signal });
|
|
@@ -265,7 +261,6 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 265 |
safeWrite(delta.text);
|
| 266 |
}
|
| 267 |
}
|
| 268 |
-
// Log exactly why it stopped (e.g. max_tokens vs end_turn)
|
| 269 |
if (chunk.messageStop) {
|
| 270 |
log.info(`Stream stopped. Reason: ${chunk.messageStop.stopReason}`, id);
|
| 271 |
}
|
|
@@ -306,4 +301,6 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 306 |
}
|
| 307 |
});
|
| 308 |
|
| 309 |
-
app.listen(PORT, '0.0.0.0', () => log.info(`AI Server live on http://localhost:${PORT}`));
|
|
|
|
|
|
|
|
|
| 21 |
// --- LOGGER HELPER ---
|
| 22 |
const log = {
|
| 23 |
info: (msg, id = "SYS") => console.log(`[${new Date().toISOString()}] [INFO] [${id}] ${msg}`),
|
| 24 |
+
warn: (msg, id = "SYS") => console.warn(`[${new Date().toISOString()}] ⚠️[WARN][${id}] ${msg}`),
|
| 25 |
error: (msg, err, id = "SYS") => console.error(`[${new Date().toISOString()}] ❌ [ERROR] [${id}] ${msg}`, err?.message || err, err?.stack || "")
|
| 26 |
};
|
| 27 |
|
|
|
|
| 77 |
|
| 78 |
const rowsToUpsert = toSync.map(id => {
|
| 79 |
const chat = memoryChats[id];
|
| 80 |
+
if (!chat) return null;
|
| 81 |
chat.updatedAt = new Date().toISOString();
|
| 82 |
return {
|
| 83 |
id: chat.id, title: chat.title, totalTokens: chat.totalTokens,
|
| 84 |
inputTokens: chat.inputTokens, outputTokens: chat.outputTokens,
|
| 85 |
messages: chat.messages, updatedAt: chat.updatedAt
|
| 86 |
};
|
| 87 |
+
}).filter(Boolean);
|
| 88 |
|
| 89 |
if (rowsToUpsert.length > 0) {
|
| 90 |
const { error } = await supabase.from('chats').upsert(rowsToUpsert);
|
|
|
|
| 238 |
|
| 239 |
historicalMessages.push({ role: "user", content: contentBlock });
|
| 240 |
|
|
|
|
| 241 |
const commandMaxTokens = model.includes("claude") ? 64000 : 8192;
|
| 242 |
|
| 243 |
const command = new ConverseStreamCommand({
|
|
|
|
| 245 |
system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
|
| 246 |
messages: historicalMessages,
|
| 247 |
inferenceConfig: { maxTokens: commandMaxTokens, temperature: 1 },
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
});
|
| 249 |
|
| 250 |
const response = await bedrockClient.send(command, { abortSignal: abortController.signal });
|
|
|
|
| 261 |
safeWrite(delta.text);
|
| 262 |
}
|
| 263 |
}
|
|
|
|
| 264 |
if (chunk.messageStop) {
|
| 265 |
log.info(`Stream stopped. Reason: ${chunk.messageStop.stopReason}`, id);
|
| 266 |
}
|
|
|
|
| 301 |
}
|
| 302 |
});
|
| 303 |
|
| 304 |
+
app.listen(PORT, '0.0.0.0', () => log.info(`AI Server live on http://localhost:${PORT}`));
|
| 305 |
+
|
| 306 |
+
// --- HTML FRONTEND ENDS HERE (Served via res.send or specific index.html if needed) ---
|