agentgui 1.0.263 → 1.0.264
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 +2 -1
- package/lib/ipfs-downloader.js +148 -0
- package/package.json +1 -1
- package/server.js +1 -0
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
|
-
|
|
1297
|
+
const record = this.getIpfsCid(cid);
|
|
1298
|
+
return record ? record.id : id;
|
|
1298
1299
|
},
|
|
1299
1300
|
|
|
1300
1301
|
getIpfsCid(cid) {
|
package/lib/ipfs-downloader.js
CHANGED
|
@@ -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
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
|
|