@fias/plugin-dev-harness 1.5.12 → 1.5.14
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/server/static/harness.js +17 -87
- package/package.json +1 -1
|
@@ -375,99 +375,29 @@
|
|
|
375
375
|
return;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
|
-
// Live mode:
|
|
379
|
-
//
|
|
380
|
-
//
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
})
|
|
386
|
-
|
|
387
|
-
return fetch(config.apiUrl + '/developer/bridge', {
|
|
388
|
-
method: 'POST',
|
|
389
|
-
headers: {
|
|
390
|
-
'Content-Type': 'application/json',
|
|
391
|
-
Authorization: 'Bearer ' + config.authToken,
|
|
392
|
-
'X-Plugin-Permissions': config.permissions.join(','),
|
|
393
|
-
},
|
|
394
|
-
body: JSON.stringify({
|
|
395
|
-
type: data.type,
|
|
396
|
-
payload: Object.assign({}, data.payload, { stream: true }),
|
|
397
|
-
}),
|
|
398
|
-
});
|
|
399
|
-
})
|
|
378
|
+
// Live mode: get the full response via non-streaming endpoint, then
|
|
379
|
+
// simulate streaming in the browser. The Express SSE proxy batches all
|
|
380
|
+
// tokens into a single chunk due to Node.js TCP buffering on localhost,
|
|
381
|
+
// so browser-side simulation is the only reliable approach.
|
|
382
|
+
fetch('/api/bridge', {
|
|
383
|
+
method: 'POST',
|
|
384
|
+
headers: { 'Content-Type': 'application/json' },
|
|
385
|
+
body: JSON.stringify({ type: data.type, payload: data.payload }),
|
|
386
|
+
})
|
|
400
387
|
.then(function (response) {
|
|
401
388
|
if (!response.ok) {
|
|
402
389
|
return response.json().then(function (err) {
|
|
403
|
-
throw new Error(
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
var contentType = response.headers.get('content-type') || '';
|
|
408
|
-
logMessage('info', 'Live SSE response content-type: ' + contentType);
|
|
409
|
-
if (contentType.indexOf('text/event-stream') === -1) {
|
|
410
|
-
logMessage('warn', 'Not SSE — falling back to JSON');
|
|
411
|
-
return response.json().then(function (result) {
|
|
412
|
-
sendToPlugin({ type: 'response', messageId: data.messageId, payload: result });
|
|
390
|
+
throw new Error(err.error || 'Bridge call failed');
|
|
413
391
|
});
|
|
414
392
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
var tokenCount = 0;
|
|
422
|
-
|
|
423
|
-
function pump() {
|
|
424
|
-
return reader.read().then(function (chunk) {
|
|
425
|
-
if (chunk.done) {
|
|
426
|
-
logMessage('info', 'Stream complete — ' + tokenCount + ' tokens sent');
|
|
427
|
-
sendToPlugin({
|
|
428
|
-
type: 'response',
|
|
429
|
-
messageId: data.messageId,
|
|
430
|
-
payload: { output: fullOutput },
|
|
431
|
-
});
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
buffer += decoder.decode(chunk.value, { stream: true });
|
|
436
|
-
var lines = buffer.split('\n');
|
|
437
|
-
buffer = lines.pop() || '';
|
|
438
|
-
|
|
439
|
-
for (var i = 0; i < lines.length; i++) {
|
|
440
|
-
var line = lines[i];
|
|
441
|
-
if (line.indexOf('data: ') !== 0) continue;
|
|
442
|
-
var lineData = line.slice(6);
|
|
443
|
-
if (lineData === '[DONE]') continue;
|
|
444
|
-
try {
|
|
445
|
-
var parsed = JSON.parse(lineData);
|
|
446
|
-
if (parsed.error) throw new Error(parsed.error);
|
|
447
|
-
if (parsed.text) {
|
|
448
|
-
fullOutput += parsed.text;
|
|
449
|
-
tokenCount++;
|
|
450
|
-
sendToPlugin({ type: 'stream_token', messageId: data.messageId, text: parsed.text });
|
|
451
|
-
if (tokenCount <= 3) logMessage('info', 'stream_token #' + tokenCount + ': "' + parsed.text.substring(0, 20) + '"');
|
|
452
|
-
}
|
|
453
|
-
if (parsed.done) {
|
|
454
|
-
if (parsed.output) fullOutput = parsed.output;
|
|
455
|
-
if (parsed.metadata && parsed.metadata.cost > 0) {
|
|
456
|
-
logMessage('cost', 'Credits used: ' + parsed.metadata.cost.toFixed(4));
|
|
457
|
-
fetchCredits();
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
} catch (e) {
|
|
461
|
-
if (e instanceof SyntaxError) continue;
|
|
462
|
-
throw e;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
return pump();
|
|
467
|
-
});
|
|
393
|
+
return response.json();
|
|
394
|
+
})
|
|
395
|
+
.then(function (result) {
|
|
396
|
+
if (result.metadata && result.metadata.cost > 0) {
|
|
397
|
+
logMessage('cost', 'Credits used: ' + result.metadata.cost.toFixed(4));
|
|
398
|
+
fetchCredits();
|
|
468
399
|
}
|
|
469
|
-
|
|
470
|
-
return pump();
|
|
400
|
+
simulateStreamInBrowser(data.messageId, result.output, result.metadata);
|
|
471
401
|
})
|
|
472
402
|
.catch(function (err) {
|
|
473
403
|
logMessage('error', err.message);
|