agentgui 1.0.263 → 1.0.265

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.
package/database.js CHANGED
@@ -1294,7 +1294,8 @@ export const queries = {
1294
1294
  ON CONFLICT(cid) DO UPDATE SET last_accessed_at = ?, success_count = success_count + 1
1295
1295
  `);
1296
1296
  stmt.run(id, cid, modelName, modelType, modelHash, gatewayUrl, now, now, now);
1297
- return id;
1297
+ const record = this.getIpfsCid(cid);
1298
+ return record ? record.id : id;
1298
1299
  },
1299
1300
 
1300
1301
  getIpfsCid(cid) {
@@ -306,6 +306,154 @@ class IPFSDownloader {
306
306
 
307
307
  return true;
308
308
  }
309
+
310
+ async downloadWithProgress(url, destination, onProgress = null) {
311
+ const dir = path.dirname(destination);
312
+ if (!fs.existsSync(dir)) {
313
+ fs.mkdirSync(dir, { recursive: true });
314
+ }
315
+
316
+ let bytesDownloaded = 0;
317
+ let totalBytes = 0;
318
+ let lastProgressTime = Date.now();
319
+ let lastProgressBytes = 0;
320
+ const speeds = [];
321
+ let gatewayIndex = 0;
322
+ let retryCount = 0;
323
+
324
+ const emitProgress = () => {
325
+ const now = Date.now();
326
+ const deltaTime = (now - lastProgressTime) / 1000;
327
+ const deltaBytes = bytesDownloaded - lastProgressBytes;
328
+ const speed = deltaTime > 0 ? Math.round(deltaBytes / deltaTime) : 0;
329
+
330
+ if (speed > 0) {
331
+ speeds.push(speed);
332
+ if (speeds.length > 10) speeds.shift();
333
+ }
334
+
335
+ const avgSpeed = speeds.length > 0 ? Math.round(speeds.reduce((a, b) => a + b, 0) / speeds.length) : 0;
336
+ const eta = avgSpeed > 0 && totalBytes > bytesDownloaded ? Math.round((totalBytes - bytesDownloaded) / avgSpeed) : 0;
337
+
338
+ if (onProgress) {
339
+ onProgress({
340
+ bytesDownloaded,
341
+ bytesRemaining: Math.max(0, totalBytes - bytesDownloaded),
342
+ totalBytes,
343
+ downloadSpeed: avgSpeed,
344
+ eta,
345
+ retryCount,
346
+ currentGateway: url,
347
+ status: bytesDownloaded >= totalBytes ? 'completed' : 'downloading',
348
+ percentComplete: totalBytes > 0 ? Math.round((bytesDownloaded / totalBytes) * 100) : 0,
349
+ timestamp: now
350
+ });
351
+ }
352
+
353
+ lastProgressTime = now;
354
+ lastProgressBytes = bytesDownloaded;
355
+ };
356
+
357
+ return new Promise((resolve, reject) => {
358
+ const attemptDownload = (gateway) => {
359
+ if (onProgress) {
360
+ onProgress({
361
+ bytesDownloaded: 0,
362
+ bytesRemaining: 0,
363
+ totalBytes: 0,
364
+ downloadSpeed: 0,
365
+ eta: 0,
366
+ retryCount,
367
+ currentGateway: gateway,
368
+ status: 'connecting',
369
+ percentComplete: 0,
370
+ timestamp: Date.now()
371
+ });
372
+ }
373
+
374
+ const protocol = gateway.startsWith('https') ? https : http;
375
+ protocol.get(gateway, { timeout: CONFIG.TIMEOUT_MS }, (res) => {
376
+ if ([301, 302, 307, 308].includes(res.statusCode)) {
377
+ const location = res.headers.location;
378
+ if (location) return attemptDownload(location);
379
+ }
380
+
381
+ if (res.statusCode !== 200) {
382
+ res.resume();
383
+ if (gatewayIndex < GATEWAYS.length - 1) {
384
+ retryCount++;
385
+ gatewayIndex++;
386
+ return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
387
+ }
388
+ return reject(new Error(`HTTP ${res.statusCode}`));
389
+ }
390
+
391
+ totalBytes = parseInt(res.headers['content-length'], 10) || 0;
392
+ bytesDownloaded = 0;
393
+ lastProgressBytes = 0;
394
+ lastProgressTime = Date.now();
395
+
396
+ const file = fs.createWriteStream(destination);
397
+ let lastEmit = Date.now();
398
+
399
+ res.on('data', (chunk) => {
400
+ bytesDownloaded += chunk.length;
401
+ const now = Date.now();
402
+ if (now - lastEmit >= 200) {
403
+ emitProgress();
404
+ lastEmit = now;
405
+ }
406
+ });
407
+
408
+ res.on('end', () => {
409
+ emitProgress();
410
+ file.destroy();
411
+ resolve({ destination, bytesDownloaded, success: true });
412
+ });
413
+
414
+ res.on('error', (err) => {
415
+ file.destroy();
416
+ fs.unlink(destination, () => {});
417
+ if (gatewayIndex < GATEWAYS.length - 1) {
418
+ retryCount++;
419
+ gatewayIndex++;
420
+ return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
421
+ }
422
+ reject(err);
423
+ });
424
+
425
+ file.on('error', (err) => {
426
+ res.destroy();
427
+ fs.unlink(destination, () => {});
428
+ if (gatewayIndex < GATEWAYS.length - 1) {
429
+ retryCount++;
430
+ gatewayIndex++;
431
+ return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
432
+ }
433
+ reject(err);
434
+ });
435
+
436
+ res.pipe(file);
437
+ }).on('timeout', () => {
438
+ if (gatewayIndex < GATEWAYS.length - 1) {
439
+ retryCount++;
440
+ gatewayIndex++;
441
+ return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
442
+ }
443
+ reject(new Error('Download timeout'));
444
+ }).on('error', (err) => {
445
+ if (gatewayIndex < GATEWAYS.length - 1) {
446
+ retryCount++;
447
+ gatewayIndex++;
448
+ return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
449
+ }
450
+ reject(err);
451
+ });
452
+ };
453
+
454
+ attemptDownload(GATEWAYS[0]);
455
+ });
456
+ }
309
457
  }
310
458
 
311
459
  export default new IPFSDownloader();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentgui",
3
- "version": "1.0.263",
3
+ "version": "1.0.265",
4
4
  "description": "Multi-agent ACP client with real-time communication",
5
5
  "type": "module",
6
6
  "main": "server.js",
package/server.js CHANGED
@@ -11,6 +11,7 @@ import { createRequire } from 'module';
11
11
  import { OAuth2Client } from 'google-auth-library';
12
12
  import { queries } from './database.js';
13
13
  import { runClaudeWithStreaming } from './lib/claude-runner.js';
14
+ import IPFSDownloader from './lib/ipfs-downloader.js';
14
15
 
15
16
  const ttsTextAccumulators = new Map();
16
17
 
@@ -672,7 +672,11 @@
672
672
  }
673
673
  });
674
674
  window.addEventListener('conversation-selected', function(e) {
675
- currentConversationId = e.detail.conversationId;
675
+ var newConversationId = e.detail.conversationId;
676
+ if (currentConversationId && currentConversationId !== newConversationId) {
677
+ unsubscribeFromConversation();
678
+ }
679
+ currentConversationId = newConversationId;
676
680
  stopSpeaking();
677
681
  spokenChunks = new Set();
678
682
  renderedSeqs = new Set();
@@ -715,9 +719,11 @@
715
719
  _voiceBreakNext = false;
716
720
  if (!conversationId) {
717
721
  showVoiceEmpty(container);
722
+ unsubscribeFromConversation();
718
723
  return;
719
724
  }
720
725
  isLoadingHistory = true;
726
+ subscribeToConversation(conversationId);
721
727
  fetch(BASE + '/api/conversations/' + conversationId + '/chunks')
722
728
  .then(function(res) { return res.json(); })
723
729
  .then(function(data) {
@@ -751,6 +757,20 @@
751
757
  });
752
758
  }
753
759
 
760
+ function subscribeToConversation(conversationId) {
761
+ if (!conversationId || typeof agentGUIClient === 'undefined' || !agentGUIClient || !agentGUIClient.wsManager) {
762
+ return;
763
+ }
764
+ agentGUIClient.wsManager.sendMessage({ type: 'subscribe', conversationId: conversationId, timestamp: Date.now() });
765
+ }
766
+
767
+ function unsubscribeFromConversation() {
768
+ if (typeof agentGUIClient === 'undefined' || !agentGUIClient || !agentGUIClient.wsManager || !currentConversationId) {
769
+ return;
770
+ }
771
+ agentGUIClient.wsManager.sendMessage({ type: 'unsubscribe', conversationId: currentConversationId, timestamp: Date.now() });
772
+ }
773
+
754
774
  function showVoiceEmpty(container) {
755
775
  container.innerHTML = '<div class="voice-empty"><div class="voice-empty-icon"><svg viewBox="0 0 24 24" width="64" height="64" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/><line x1="8" y1="23" x2="16" y2="23"/></svg></div><div>Hold the microphone button to record.<br>Release to transcribe. Tap Send to submit.<br>New responses will be read aloud.</div></div>';
756
776
  }
@@ -770,6 +790,7 @@
770
790
  function deactivate() {
771
791
  voiceActive = false;
772
792
  stopSpeaking();
793
+ unsubscribeFromConversation();
773
794
  }
774
795
 
775
796
  function escapeHtml(text) {