agentgui 1.0.154 → 1.0.155

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/database.js +11 -0
  2. package/package.json +1 -1
  3. package/server.js +56 -10
package/database.js CHANGED
@@ -430,6 +430,17 @@ export const queries = {
430
430
  });
431
431
  },
432
432
 
433
+ getLastUserMessage(conversationId) {
434
+ const stmt = prep(
435
+ "SELECT * FROM messages WHERE conversationId = ? AND role = 'user' ORDER BY created_at DESC LIMIT 1"
436
+ );
437
+ const msg = stmt.get(conversationId);
438
+ if (msg && typeof msg.content === 'string') {
439
+ try { msg.content = JSON.parse(msg.content); } catch (_) {}
440
+ }
441
+ return msg || null;
442
+ },
443
+
433
444
  getPaginatedMessages(conversationId, limit = 50, offset = 0) {
434
445
  const countStmt = prep('SELECT COUNT(*) as count FROM messages WHERE conversationId = ?');
435
446
  const total = countStmt.get(conversationId).count;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentgui",
3
- "version": "1.0.154",
3
+ "version": "1.0.155",
4
4
  "description": "Multi-agent ACP client with real-time communication",
5
5
  "type": "module",
6
6
  "main": "server.js",
package/server.js CHANGED
@@ -1228,29 +1228,75 @@ function recoverStaleSessions() {
1228
1228
  try {
1229
1229
  const staleSessions = queries.getActiveSessions ? queries.getActiveSessions() : [];
1230
1230
  const now = Date.now();
1231
- let recoveredCount = 0;
1231
+ let resumedCount = 0;
1232
+ let failedCount = 0;
1233
+
1232
1234
  for (const session of staleSessions) {
1233
1235
  if (activeExecutions.has(session.conversationId)) continue;
1234
1236
  const sessionAge = now - session.started_at;
1235
1237
  if (sessionAge < STALE_SESSION_MIN_AGE_MS) continue;
1238
+
1236
1239
  queries.updateSession(session.id, {
1237
1240
  status: 'error',
1238
- error: 'Agent died unexpectedly (server restart)',
1241
+ error: 'Server restarted - resuming',
1239
1242
  completed_at: now
1240
1243
  });
1241
- queries.setIsStreaming(session.conversationId, false);
1244
+
1245
+ const conv = queries.getConversation(session.conversationId);
1246
+ if (!conv) {
1247
+ queries.setIsStreaming(session.conversationId, false);
1248
+ failedCount++;
1249
+ continue;
1250
+ }
1251
+
1252
+ const lastMsg = queries.getLastUserMessage(session.conversationId);
1253
+ if (!lastMsg || !conv.claudeSessionId) {
1254
+ queries.setIsStreaming(session.conversationId, false);
1255
+ debugLog(`[RECOVERY] Conv ${session.conversationId}: no user message or no claudeSessionId, cannot resume`);
1256
+ broadcastSync({
1257
+ type: 'streaming_error',
1258
+ sessionId: session.id,
1259
+ conversationId: session.conversationId,
1260
+ error: 'Server restarted - could not resume (missing context)',
1261
+ recoverable: false,
1262
+ timestamp: now
1263
+ });
1264
+ failedCount++;
1265
+ continue;
1266
+ }
1267
+
1268
+ const content = typeof lastMsg.content === 'string' ? lastMsg.content : JSON.stringify(lastMsg.content);
1269
+ const agentId = conv.agentType || conv.agentId || 'claude-code';
1270
+
1271
+ debugLog(`[RECOVERY] Resuming conv ${session.conversationId} with claudeSessionId=${conv.claudeSessionId}`);
1272
+
1273
+ const newSession = queries.createSession(session.conversationId);
1274
+ queries.createEvent('session.created', {
1275
+ messageId: lastMsg.id,
1276
+ sessionId: newSession.id,
1277
+ retryReason: 'server_restart'
1278
+ }, session.conversationId, newSession.id);
1279
+
1242
1280
  broadcastSync({
1243
- type: 'streaming_error',
1244
- sessionId: session.id,
1281
+ type: 'streaming_start',
1282
+ sessionId: newSession.id,
1245
1283
  conversationId: session.conversationId,
1246
- error: 'Agent died unexpectedly (server restart)',
1247
- recoverable: false,
1284
+ messageId: lastMsg.id,
1285
+ agentId,
1248
1286
  timestamp: now
1249
1287
  });
1250
- recoveredCount++;
1288
+
1289
+ processMessageWithStreaming(session.conversationId, lastMsg.id, newSession.id, content, agentId)
1290
+ .catch(err => debugLog(`[RECOVERY] Resume error for ${session.conversationId}: ${err.message}`));
1291
+
1292
+ resumedCount++;
1293
+ }
1294
+
1295
+ if (resumedCount > 0) {
1296
+ console.log(`[RECOVERY] Resumed ${resumedCount} conversation(s) from previous run`);
1251
1297
  }
1252
- if (recoveredCount > 0) {
1253
- console.log(`[RECOVERY] Recovered ${recoveredCount} stale active session(s)`);
1298
+ if (failedCount > 0) {
1299
+ console.log(`[RECOVERY] Failed to resume ${failedCount} conversation(s)`);
1254
1300
  }
1255
1301
  } catch (err) {
1256
1302
  console.error('[RECOVERY] Stale session recovery error:', err.message);