@annals/agent-mesh 0.18.5 → 0.19.0
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/dist/index.js +61 -37
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -572,6 +572,7 @@ var FileUploadReceiver = class {
|
|
|
572
572
|
});
|
|
573
573
|
});
|
|
574
574
|
this.peer.onDataChannel((dc) => {
|
|
575
|
+
log.debug("[WebRTC] Upload receiver: DataChannel opened");
|
|
575
576
|
dc.onMessage((msg) => {
|
|
576
577
|
if (typeof msg === "string") {
|
|
577
578
|
try {
|
|
@@ -591,6 +592,7 @@ var FileUploadReceiver = class {
|
|
|
591
592
|
}
|
|
592
593
|
try {
|
|
593
594
|
if (signal.signal_type === "offer" || signal.signal_type === "answer") {
|
|
595
|
+
log.debug(`[WebRTC] Upload receiver: setting remote ${signal.signal_type} (${signal.payload.length} chars)`);
|
|
594
596
|
this.peer.setRemoteDescription(signal.payload, signal.signal_type);
|
|
595
597
|
for (const c of this.pendingCandidates) {
|
|
596
598
|
this.peer.addRemoteCandidate(c.candidate, c.mid);
|
|
@@ -604,7 +606,8 @@ var FileUploadReceiver = class {
|
|
|
604
606
|
this.pendingCandidates.push({ candidate, mid });
|
|
605
607
|
}
|
|
606
608
|
}
|
|
607
|
-
} catch {
|
|
609
|
+
} catch (err) {
|
|
610
|
+
log.warn(`[WebRTC] Upload receiver signal error (${signal.signal_type}): ${err}`);
|
|
608
611
|
}
|
|
609
612
|
}
|
|
610
613
|
waitForCompletion(timeoutMs = 3e4) {
|
|
@@ -1505,10 +1508,7 @@ var BridgeManager = class {
|
|
|
1505
1508
|
}
|
|
1506
1509
|
async dispatchWithLocalQueue(opts) {
|
|
1507
1510
|
const { msg, handle, requestKey } = opts;
|
|
1508
|
-
const { session_id, request_id, content, attachments, client_id, with_files
|
|
1509
|
-
if (file_upload_offer) {
|
|
1510
|
-
this.registerPendingUpload(file_upload_offer, session_id, request_id);
|
|
1511
|
-
}
|
|
1511
|
+
const { session_id, request_id, content, attachments, client_id, with_files } = msg;
|
|
1512
1512
|
const state = this.requestDispatches.get(requestKey);
|
|
1513
1513
|
if (!state) return;
|
|
1514
1514
|
try {
|
|
@@ -1830,6 +1830,11 @@ var BridgeManager = class {
|
|
|
1830
1830
|
log.info(`WebRTC transfer registered: transfer=${offer.transfer_id.slice(0, 8)}... (${(zipBuffer.length / 1024).toFixed(1)} KB in memory)`);
|
|
1831
1831
|
}
|
|
1832
1832
|
handleRtcSignalRelay(msg) {
|
|
1833
|
+
if (msg.signal_type === "prepare-upload") {
|
|
1834
|
+
const offer = JSON.parse(msg.payload);
|
|
1835
|
+
this.registerPendingUpload(offer, "pre-upload", "pre-upload");
|
|
1836
|
+
return;
|
|
1837
|
+
}
|
|
1833
1838
|
const downloadEntry = this.pendingTransfers.get(msg.transfer_id);
|
|
1834
1839
|
if (downloadEntry) {
|
|
1835
1840
|
downloadEntry.targetAgentId = msg.from_agent_id;
|
|
@@ -1845,13 +1850,14 @@ var BridgeManager = class {
|
|
|
1845
1850
|
}
|
|
1846
1851
|
const uploadEntry = this.pendingUploads.get(msg.transfer_id);
|
|
1847
1852
|
if (uploadEntry) {
|
|
1853
|
+
log.debug(`[WebRTC] Routing ${msg.signal_type} signal to upload receiver: transfer=${msg.transfer_id.slice(0, 8)}...`);
|
|
1848
1854
|
void uploadEntry.receiver.handleSignal({
|
|
1849
1855
|
signal_type: msg.signal_type,
|
|
1850
1856
|
payload: msg.payload
|
|
1851
1857
|
});
|
|
1852
1858
|
return;
|
|
1853
1859
|
}
|
|
1854
|
-
log.debug(`No pending transfer for ${msg.transfer_id.slice(0, 8)}
|
|
1860
|
+
log.debug(`No pending transfer for ${msg.transfer_id.slice(0, 8)}... (active=${this.pendingTransfers.size}, dormant=${this.dormantTransfers.size}, upload=${this.pendingUploads.size})`);
|
|
1855
1861
|
}
|
|
1856
1862
|
// ========================================================
|
|
1857
1863
|
// Upload (Caller → Agent) WebRTC signaling
|
|
@@ -4163,8 +4169,7 @@ async function asyncChat(opts) {
|
|
|
4163
4169
|
},
|
|
4164
4170
|
body: JSON.stringify({
|
|
4165
4171
|
message: opts.message,
|
|
4166
|
-
mode: "async"
|
|
4167
|
-
...opts.fileUploadOffer ? { file_upload_offer: opts.fileUploadOffer } : {}
|
|
4172
|
+
mode: "async"
|
|
4168
4173
|
}),
|
|
4169
4174
|
signal: opts.signal
|
|
4170
4175
|
});
|
|
@@ -4234,8 +4239,7 @@ async function streamChat(opts) {
|
|
|
4234
4239
|
"Content-Type": "application/json"
|
|
4235
4240
|
},
|
|
4236
4241
|
body: JSON.stringify({
|
|
4237
|
-
message: opts.message
|
|
4238
|
-
...opts.fileUploadOffer ? { file_upload_offer: opts.fileUploadOffer } : {}
|
|
4242
|
+
message: opts.message
|
|
4239
4243
|
}),
|
|
4240
4244
|
signal: opts.signal
|
|
4241
4245
|
});
|
|
@@ -4372,8 +4376,6 @@ function registerChatCommand(program2) {
|
|
|
4372
4376
|
log.banner(`Chat with ${agentName}`);
|
|
4373
4377
|
console.log(`${GRAY}Type your message and press Enter. /upload <path> to send a file. /quit to exit.${RESET}
|
|
4374
4378
|
`);
|
|
4375
|
-
let pendingUploadOffer;
|
|
4376
|
-
let pendingUploadZipBuffer;
|
|
4377
4379
|
const rl = createInterface4({
|
|
4378
4380
|
input: process.stdin,
|
|
4379
4381
|
output: process.stdout,
|
|
@@ -4410,20 +4412,33 @@ function registerChatCommand(program2) {
|
|
|
4410
4412
|
}
|
|
4411
4413
|
try {
|
|
4412
4414
|
const prepared = prepareUploadFile(filePath);
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4415
|
+
const prepRes = await fetch(`${opts.baseUrl}/api/agents/${agentId}/rtc-signal`, {
|
|
4416
|
+
method: "POST",
|
|
4417
|
+
headers: {
|
|
4418
|
+
Authorization: `Bearer ${token}`,
|
|
4419
|
+
"Content-Type": "application/json"
|
|
4420
|
+
},
|
|
4421
|
+
body: JSON.stringify({
|
|
4422
|
+
transfer_id: prepared.offer.transfer_id,
|
|
4423
|
+
signal_type: "prepare-upload",
|
|
4424
|
+
payload: JSON.stringify(prepared.offer)
|
|
4425
|
+
})
|
|
4426
|
+
});
|
|
4427
|
+
if (!prepRes.ok) {
|
|
4428
|
+
log.error(`Failed to signal upload: HTTP ${prepRes.status}`);
|
|
4429
|
+
rl.prompt();
|
|
4430
|
+
return;
|
|
4431
|
+
}
|
|
4432
|
+
await sleep5(500);
|
|
4433
|
+
await chatWebrtcUpload(agentId, prepared.offer, prepared.zipBuffer, token, opts.baseUrl);
|
|
4434
|
+
log.info(`File uploaded. Type a message to continue.`);
|
|
4416
4435
|
} catch (err) {
|
|
4417
|
-
log.error(`
|
|
4436
|
+
log.error(`Upload failed: ${err.message}`);
|
|
4418
4437
|
}
|
|
4419
4438
|
rl.prompt();
|
|
4420
4439
|
return;
|
|
4421
4440
|
}
|
|
4422
4441
|
console.log("");
|
|
4423
|
-
const uploadOffer = pendingUploadOffer;
|
|
4424
|
-
const uploadZipBuffer = pendingUploadZipBuffer;
|
|
4425
|
-
pendingUploadOffer = void 0;
|
|
4426
|
-
pendingUploadZipBuffer = void 0;
|
|
4427
4442
|
try {
|
|
4428
4443
|
await streamChat({
|
|
4429
4444
|
agentId,
|
|
@@ -4431,12 +4446,8 @@ function registerChatCommand(program2) {
|
|
|
4431
4446
|
token,
|
|
4432
4447
|
baseUrl: opts.baseUrl,
|
|
4433
4448
|
showThinking: opts.thinking,
|
|
4434
|
-
mode
|
|
4435
|
-
fileUploadOffer: uploadOffer
|
|
4449
|
+
mode
|
|
4436
4450
|
});
|
|
4437
|
-
if (uploadOffer && uploadZipBuffer) {
|
|
4438
|
-
await chatWebrtcUpload(agentId, uploadOffer, uploadZipBuffer, token, opts.baseUrl);
|
|
4439
|
-
}
|
|
4440
4451
|
} catch (err) {
|
|
4441
4452
|
if (abortController.signal.aborted) return;
|
|
4442
4453
|
log.error(err.message);
|
|
@@ -5429,6 +5440,23 @@ function prepareFileForUpload(filePath) {
|
|
|
5429
5440
|
zipBuffer: Buffer.from(zipBuffer)
|
|
5430
5441
|
};
|
|
5431
5442
|
}
|
|
5443
|
+
async function sendPrepareUpload(agentId, offer, token) {
|
|
5444
|
+
const res = await fetch(`${DEFAULT_BASE_URL4}/api/agents/${agentId}/rtc-signal`, {
|
|
5445
|
+
method: "POST",
|
|
5446
|
+
headers: {
|
|
5447
|
+
Authorization: `Bearer ${token}`,
|
|
5448
|
+
"Content-Type": "application/json"
|
|
5449
|
+
},
|
|
5450
|
+
body: JSON.stringify({
|
|
5451
|
+
transfer_id: offer.transfer_id,
|
|
5452
|
+
signal_type: "prepare-upload",
|
|
5453
|
+
payload: JSON.stringify(offer)
|
|
5454
|
+
})
|
|
5455
|
+
});
|
|
5456
|
+
if (!res.ok) {
|
|
5457
|
+
throw new Error(`prepare-upload signal failed: HTTP ${res.status}`);
|
|
5458
|
+
}
|
|
5459
|
+
}
|
|
5432
5460
|
async function webrtcUpload(agentId, offer, zipBuffer, token, json) {
|
|
5433
5461
|
if (!json) {
|
|
5434
5462
|
log.info(`[WebRTC] Uploading file (${(offer.zip_size / 1024).toFixed(1)} KB)...`);
|
|
@@ -5523,8 +5551,7 @@ async function asyncCall(opts) {
|
|
|
5523
5551
|
body: JSON.stringify({
|
|
5524
5552
|
task_description: opts.taskDescription,
|
|
5525
5553
|
mode: "async",
|
|
5526
|
-
...opts.withFiles ? { with_files: true } : {}
|
|
5527
|
-
...opts.uploadOffer ? { file_upload_offer: opts.uploadOffer } : {}
|
|
5554
|
+
...opts.withFiles ? { with_files: true } : {}
|
|
5528
5555
|
}),
|
|
5529
5556
|
signal: opts.signal
|
|
5530
5557
|
});
|
|
@@ -5643,8 +5670,7 @@ async function streamCall(opts) {
|
|
|
5643
5670
|
},
|
|
5644
5671
|
body: JSON.stringify({
|
|
5645
5672
|
task_description: opts.taskDescription,
|
|
5646
|
-
...opts.withFiles ? { with_files: true } : {}
|
|
5647
|
-
...opts.uploadOffer ? { file_upload_offer: opts.uploadOffer } : {}
|
|
5673
|
+
...opts.withFiles ? { with_files: true } : {}
|
|
5648
5674
|
}),
|
|
5649
5675
|
signal: opts.signal
|
|
5650
5676
|
});
|
|
@@ -5827,6 +5853,11 @@ ${content}`;
|
|
|
5827
5853
|
const timeoutMs = parseInt(opts.timeout || "300", 10) * 1e3;
|
|
5828
5854
|
const abortController = new AbortController();
|
|
5829
5855
|
const timer = setTimeout(() => abortController.abort(), timeoutMs);
|
|
5856
|
+
if (uploadOffer && uploadZipBuffer) {
|
|
5857
|
+
await sendPrepareUpload(id, uploadOffer, token);
|
|
5858
|
+
await sleep6(500);
|
|
5859
|
+
await webrtcUpload(id, uploadOffer, uploadZipBuffer, token, opts.json);
|
|
5860
|
+
}
|
|
5830
5861
|
const callOpts = {
|
|
5831
5862
|
id,
|
|
5832
5863
|
name,
|
|
@@ -5836,20 +5867,13 @@ ${content}`;
|
|
|
5836
5867
|
json: opts.json,
|
|
5837
5868
|
outputFile: opts.outputFile,
|
|
5838
5869
|
signal: abortController.signal,
|
|
5839
|
-
withFiles: opts.withFiles
|
|
5840
|
-
uploadOffer
|
|
5870
|
+
withFiles: opts.withFiles
|
|
5841
5871
|
};
|
|
5842
5872
|
let result;
|
|
5843
5873
|
if (opts.stream) {
|
|
5844
|
-
if (uploadOffer && uploadZipBuffer) {
|
|
5845
|
-
void webrtcUpload(id, uploadOffer, uploadZipBuffer, token, opts.json);
|
|
5846
|
-
}
|
|
5847
5874
|
result = await streamCall(callOpts);
|
|
5848
5875
|
} else {
|
|
5849
5876
|
result = await asyncCall(callOpts);
|
|
5850
|
-
if (uploadOffer && uploadZipBuffer) {
|
|
5851
|
-
await webrtcUpload(id, uploadOffer, uploadZipBuffer, token, opts.json);
|
|
5852
|
-
}
|
|
5853
5877
|
}
|
|
5854
5878
|
clearTimeout(timer);
|
|
5855
5879
|
if (opts.rate && result.callId) {
|