Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -50,7 +50,7 @@ const supabase = createClient(
|
|
| 50 |
|
| 51 |
let memoryChats = {};
|
| 52 |
let dirtyChats = new Set();
|
| 53 |
-
const activeGenerations = new Map();
|
| 54 |
|
| 55 |
async function initDB() {
|
| 56 |
try {
|
|
@@ -150,7 +150,6 @@ app.delete('/api/chats/:id', async (req, res) => {
|
|
| 150 |
res.json({ success: true });
|
| 151 |
});
|
| 152 |
|
| 153 |
-
// --- STOP GENERATION ENDPOINT ---
|
| 154 |
app.post('/api/chats/:id/stop', (req, res) => {
|
| 155 |
const { id } = req.params;
|
| 156 |
log.info("User requested to stop generation.", id);
|
|
@@ -193,7 +192,6 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 193 |
memoryChats[id].isGenerating = true;
|
| 194 |
dirtyChats.add(id);
|
| 195 |
|
| 196 |
-
// Create Abort Controller for this generation
|
| 197 |
const abortController = new AbortController();
|
| 198 |
activeGenerations.set(id, abortController);
|
| 199 |
|
|
@@ -239,19 +237,21 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 239 |
|
| 240 |
historicalMessages.push({ role: "user", content: contentBlock });
|
| 241 |
|
|
|
|
|
|
|
|
|
|
| 242 |
const command = new ConverseStreamCommand({
|
| 243 |
modelId: bedrockModelId,
|
| 244 |
system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
|
| 245 |
messages: historicalMessages,
|
| 246 |
-
inferenceConfig: { maxTokens:
|
| 247 |
additionalModelRequestFields: model.includes("claude") ? {
|
| 248 |
-
thinking: { type: "adaptive" }
|
| 249 |
} : undefined
|
| 250 |
});
|
| 251 |
|
| 252 |
-
// Pass abortSignal to allow stopping mid-generation
|
| 253 |
const response = await bedrockClient.send(command, { abortSignal: abortController.signal });
|
| 254 |
-
log.info(
|
| 255 |
|
| 256 |
for await (const chunk of response.stream) {
|
| 257 |
if (chunk.contentBlockDelta) {
|
|
@@ -264,6 +264,10 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 264 |
safeWrite(delta.text);
|
| 265 |
}
|
| 266 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
if (chunk.metadata && chunk.metadata.usage) {
|
| 268 |
streamInputTokens = chunk.metadata.usage.inputTokens || 0;
|
| 269 |
streamOutputTokens = chunk.metadata.usage.outputTokens || 0;
|
|
@@ -286,11 +290,9 @@ app.post('/api/chats/:id/stream', async (req, res) => {
|
|
| 286 |
} finally {
|
| 287 |
activeGenerations.delete(id);
|
| 288 |
|
| 289 |
-
// Save tokens generated up to point of failure/abort
|
| 290 |
memoryChats[id].inputTokens += streamInputTokens;
|
| 291 |
memoryChats[id].outputTokens += streamOutputTokens;
|
| 292 |
memoryChats[id].totalTokens += streamTotalTokens;
|
| 293 |
-
|
| 294 |
memoryChats[id].isGenerating = false;
|
| 295 |
dirtyChats.add(id);
|
| 296 |
|
|
|
|
| 50 |
|
| 51 |
let memoryChats = {};
|
| 52 |
let dirtyChats = new Set();
|
| 53 |
+
const activeGenerations = new Map();
|
| 54 |
|
| 55 |
async function initDB() {
|
| 56 |
try {
|
|
|
|
| 150 |
res.json({ success: true });
|
| 151 |
});
|
| 152 |
|
|
|
|
| 153 |
app.post('/api/chats/:id/stop', (req, res) => {
|
| 154 |
const { id } = req.params;
|
| 155 |
log.info("User requested to stop generation.", id);
|
|
|
|
| 192 |
memoryChats[id].isGenerating = true;
|
| 193 |
dirtyChats.add(id);
|
| 194 |
|
|
|
|
| 195 |
const abortController = new AbortController();
|
| 196 |
activeGenerations.set(id, abortController);
|
| 197 |
|
|
|
|
| 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({
|
| 244 |
modelId: bedrockModelId,
|
| 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 |
const response = await bedrockClient.send(command, { abortSignal: abortController.signal });
|
| 254 |
+
log.info(`Bedrock connected successfully (Max Tokens: ${commandMaxTokens}), streaming chunks...`, id);
|
| 255 |
|
| 256 |
for await (const chunk of response.stream) {
|
| 257 |
if (chunk.contentBlockDelta) {
|
|
|
|
| 264 |
safeWrite(delta.text);
|
| 265 |
}
|
| 266 |
}
|
| 267 |
+
// Log exactly why it stopped (e.g. max_tokens vs end_turn)
|
| 268 |
+
if (chunk.messageStop) {
|
| 269 |
+
log.info(`Stream stopped. Reason: ${chunk.messageStop.stopReason}`, id);
|
| 270 |
+
}
|
| 271 |
if (chunk.metadata && chunk.metadata.usage) {
|
| 272 |
streamInputTokens = chunk.metadata.usage.inputTokens || 0;
|
| 273 |
streamOutputTokens = chunk.metadata.usage.outputTokens || 0;
|
|
|
|
| 290 |
} finally {
|
| 291 |
activeGenerations.delete(id);
|
| 292 |
|
|
|
|
| 293 |
memoryChats[id].inputTokens += streamInputTokens;
|
| 294 |
memoryChats[id].outputTokens += streamOutputTokens;
|
| 295 |
memoryChats[id].totalTokens += streamTotalTokens;
|
|
|
|
| 296 |
memoryChats[id].isGenerating = false;
|
| 297 |
dirtyChats.add(id);
|
| 298 |
|