libfx 0.0.12 → 0.0.13-dev.1415.g607b1632fff6
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/README.md +196 -30
- package/fetch-cleanup.js +12 -0
- package/fx-core.wasm +0 -0
- package/fx-sdk.js +494 -149
- package/fx-term.wasm +0 -0
- package/libfx.darwin-arm64.node +0 -0
- package/libfx.darwin-x64.node +0 -0
- package/libfx.linux-arm64.node +0 -0
- package/libfx.linux-x64.node +0 -0
- package/node.cjs +658 -201
- package/node.js +138 -45
- package/package.json +1 -1
package/node.js
CHANGED
|
@@ -7,6 +7,7 @@ import { isAbsolute, resolve } from "node:path";
|
|
|
7
7
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
8
|
import { CoreOutput } from "./core-output.js";
|
|
9
9
|
import { loadModule, withModuleFailure } from "./wasm-module.js";
|
|
10
|
+
import { fetchCleanupAction, cleanupTimeoutMs, cleanupByteLimit } from "./fetch-cleanup.js";
|
|
10
11
|
import {
|
|
11
12
|
createFxAgent as createWasmAgent,
|
|
12
13
|
createFxTerminal as createWasmTerminal,
|
|
@@ -20,7 +21,7 @@ import {
|
|
|
20
21
|
|
|
21
22
|
export { encodeXtermKeyEvent, fxSdkApiVersion, listModels, supportsJspi, xtermAdapter };
|
|
22
23
|
export const libfxApiVersion = 2;
|
|
23
|
-
const nativeCoreApiVersion =
|
|
24
|
+
const nativeCoreApiVersion = 4;
|
|
24
25
|
|
|
25
26
|
const fetchOperationStale = 0;
|
|
26
27
|
const fetchOperationApplied = 1;
|
|
@@ -346,7 +347,12 @@ export async function getBackendInfo(value = {}) {
|
|
|
346
347
|
}
|
|
347
348
|
|
|
348
349
|
function createNativeCoreRuntime(addon, options) {
|
|
349
|
-
const { apiKey, model, effort, fast, gatewayChatUrl } = options;
|
|
350
|
+
const { apiKey, model, effort, fast, ultrafast, gatewayChatUrl } = options;
|
|
351
|
+
if (ultrafast !== undefined && addon.supportsUltrafast !== true) {
|
|
352
|
+
const error = new Error("native addon does not support the ultrafast option");
|
|
353
|
+
error.code = "LIBFX_NATIVE_CAPABILITY_UNAVAILABLE";
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
350
356
|
const core = addon.createCore({
|
|
351
357
|
apiKey,
|
|
352
358
|
home: options.home ?? homedir(),
|
|
@@ -354,6 +360,7 @@ function createNativeCoreRuntime(addon, options) {
|
|
|
354
360
|
...(model === undefined ? {} : { model }),
|
|
355
361
|
...(effort === undefined ? {} : { effort }),
|
|
356
362
|
...(fast === undefined ? {} : { fast }),
|
|
363
|
+
...(ultrafast === undefined ? {} : { ultrafast }),
|
|
357
364
|
...(gatewayChatUrl === undefined ? {} : { gatewayChatUrl }),
|
|
358
365
|
});
|
|
359
366
|
let readyFd;
|
|
@@ -377,79 +384,158 @@ function createNativeCoreRuntime(addon, options) {
|
|
|
377
384
|
let settled = false;
|
|
378
385
|
let fetchState = null;
|
|
379
386
|
const exited = new Promise((resolve) => { exitedResolve = resolve; });
|
|
387
|
+
function refreshFetch(state) {
|
|
388
|
+
if (settled) return;
|
|
389
|
+
if (typeof addon.coreFetchDisposition !== "function") {
|
|
390
|
+
state.active = addon.coreFetchActive(core, state.handle);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
// One native lock observes consumption and retirement together.
|
|
394
|
+
const disposition = addon.coreFetchDisposition(core, state.handle);
|
|
395
|
+
if (disposition !== 0 && disposition !== 1 && disposition !== 2) {
|
|
396
|
+
throw new Error(`invalid native fetch disposition ${disposition}`);
|
|
397
|
+
}
|
|
398
|
+
state.consumed ||= disposition === 2;
|
|
399
|
+
state.active = disposition === 1;
|
|
400
|
+
}
|
|
401
|
+
function cleanupAction(state) {
|
|
402
|
+
return fetchCleanupAction({ ...state, alive: !settled,
|
|
403
|
+
age: state.drainStarted === null ? 0 : performance.now() - state.drainStarted });
|
|
404
|
+
}
|
|
405
|
+
function beginDrain(state) {
|
|
406
|
+
if (state.drainStarted !== null) return;
|
|
407
|
+
state.drainStarted = performance.now();
|
|
408
|
+
state.timer = setTimeout(() => state.controller.abort(), cleanupTimeoutMs);
|
|
409
|
+
}
|
|
380
410
|
const abortHostEffects = () => {
|
|
381
|
-
fetchState
|
|
382
|
-
|
|
411
|
+
if (fetchState) {
|
|
412
|
+
fetchState.canceled = true;
|
|
413
|
+
fetchState.controller.abort();
|
|
414
|
+
}
|
|
415
|
+
if (!settled) { try { addon.abortCoreFetch(core); } catch {} }
|
|
383
416
|
};
|
|
384
417
|
const finish = (code, error) => {
|
|
385
418
|
if (settled) return;
|
|
419
|
+
const state = fetchState;
|
|
420
|
+
if (state) {
|
|
421
|
+
refreshFetch(state);
|
|
422
|
+
state.closing = true;
|
|
423
|
+
state.failed ||= code !== 0 || error !== undefined;
|
|
424
|
+
}
|
|
386
425
|
settled = true;
|
|
387
426
|
outputError = error;
|
|
388
427
|
output.close();
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
428
|
+
const destroy = () => {
|
|
429
|
+
try { addon.destroyCore(core); } catch {}
|
|
430
|
+
readySocket.destroy();
|
|
431
|
+
void readyClosed.then(() => exitedResolve(code));
|
|
432
|
+
};
|
|
433
|
+
if (state && cleanupAction(state) === "drain") {
|
|
434
|
+
beginDrain(state);
|
|
435
|
+
void state.done.then(destroy);
|
|
436
|
+
} else {
|
|
437
|
+
if (state && cleanupAction(state) !== "done") state.controller.abort();
|
|
438
|
+
destroy();
|
|
439
|
+
}
|
|
393
440
|
};
|
|
394
|
-
const pumpFetch = async (request) => {
|
|
441
|
+
const pumpFetch = async (request, body) => {
|
|
395
442
|
const controller = new AbortController();
|
|
396
|
-
|
|
443
|
+
let complete, reader;
|
|
444
|
+
const state = { handle: request.handle, controller, consumed: false, active: true,
|
|
445
|
+
canceled: false, failed: false, closing: false, eof: false, discarded: 0,
|
|
446
|
+
drainStarted: null, timer: null, done: new Promise(resolve => { complete = resolve; }) };
|
|
397
447
|
fetchState = state;
|
|
448
|
+
let onAbort;
|
|
449
|
+
const aborted = new Promise((_, reject) => {
|
|
450
|
+
onAbort = () => reject(new DOMException("Aborted", "AbortError"));
|
|
451
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
452
|
+
});
|
|
453
|
+
void aborted.catch(() => {});
|
|
454
|
+
const wait = task => Promise.race([task, aborted]);
|
|
398
455
|
try {
|
|
399
|
-
const
|
|
456
|
+
const responseTask = Promise.resolve().then(() => (options.fetch ?? globalThis.fetch)(request.url, {
|
|
400
457
|
method: request.method,
|
|
401
458
|
headers: new Headers(JSON.parse(request.headers).map(({ name, value }) => [name, value])),
|
|
402
|
-
body:
|
|
459
|
+
body: body.length ? body : undefined,
|
|
403
460
|
signal: controller.signal,
|
|
461
|
+
})).then(response => {
|
|
462
|
+
if (controller.signal.aborted) {
|
|
463
|
+
void response.body?.cancel().catch(() => {});
|
|
464
|
+
throw new DOMException("Aborted", "AbortError");
|
|
465
|
+
}
|
|
466
|
+
return response;
|
|
404
467
|
});
|
|
405
|
-
const
|
|
406
|
-
if (
|
|
407
|
-
|
|
408
|
-
if (
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
468
|
+
const response = await wait(responseTask);
|
|
469
|
+
if (settled && !state.consumed) throw new DOMException("Aborted", "AbortError");
|
|
470
|
+
const started = settled ? fetchOperationStale : addon.startCoreFetchResponse(core, state.handle, response.status);
|
|
471
|
+
if (started !== fetchOperationApplied && started !== fetchOperationStale) throw new Error(`invalid native fetch start result ${started}`);
|
|
472
|
+
state.active = started === fetchOperationApplied;
|
|
473
|
+
reader = response.body?.getReader();
|
|
474
|
+
while (reader) {
|
|
475
|
+
refreshFetch(state);
|
|
476
|
+
const action = cleanupAction(state);
|
|
477
|
+
if (action === "abort") { controller.abort(); throw new DOMException("Aborted", "AbortError"); }
|
|
478
|
+
if (action === "drain") beginDrain(state);
|
|
479
|
+
const { done, value } = await wait(reader.read());
|
|
480
|
+
if (done) break;
|
|
481
|
+
const buffer = Buffer.from(value);
|
|
482
|
+
options.onTransportChunk?.(buffer.length);
|
|
483
|
+
let offset = 0;
|
|
484
|
+
while (offset < buffer.length) {
|
|
485
|
+
refreshFetch(state);
|
|
486
|
+
const next = cleanupAction(state);
|
|
487
|
+
if (next === "drain") {
|
|
488
|
+
beginDrain(state);
|
|
489
|
+
state.discarded += buffer.length - offset;
|
|
490
|
+
if (state.discarded >= cleanupByteLimit) controller.abort();
|
|
491
|
+
break;
|
|
492
|
+
}
|
|
493
|
+
if (next !== "forward") { controller.abort(); throw new DOMException("Aborted", "AbortError"); }
|
|
494
|
+
const end = Math.min(offset + 64 * 1024, buffer.length);
|
|
495
|
+
const pushed = addon.pushCoreFetchResponse(core, state.handle, buffer.subarray(offset, end));
|
|
496
|
+
if (pushed === fetchOperationApplied) { offset = end; continue; }
|
|
497
|
+
if (pushed === fetchOperationStale) {
|
|
498
|
+
refreshFetch(state);
|
|
499
|
+
if (cleanupAction(state) === "drain") continue;
|
|
500
|
+
controller.abort(); throw new DOMException("Aborted", "AbortError");
|
|
423
501
|
}
|
|
502
|
+
if (pushed !== fetchOperationBackpressure) throw new Error(`invalid native fetch push result ${pushed}`);
|
|
503
|
+
await wait(new Promise(resolve => setTimeout(resolve, 2)));
|
|
424
504
|
}
|
|
425
505
|
}
|
|
426
|
-
|
|
427
|
-
if (
|
|
428
|
-
|
|
506
|
+
state.eof = true;
|
|
507
|
+
if (!settled && state.active) {
|
|
508
|
+
const finished = addon.finishCoreFetch(core, state.handle);
|
|
509
|
+
if (finished !== fetchOperationApplied && finished !== fetchOperationStale) throw new Error(`invalid native fetch finish result ${finished}`);
|
|
429
510
|
}
|
|
430
511
|
} catch (error) {
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
} catch {}
|
|
512
|
+
if (!settled && !controller.signal.aborted) {
|
|
513
|
+
state.failed = true;
|
|
514
|
+
try { if (addon.coreFetchActive(core, state.handle)) addon.failCoreFetch(core, state.handle); } catch {}
|
|
435
515
|
}
|
|
436
516
|
} finally {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
517
|
+
clearTimeout(state.timer);
|
|
518
|
+
controller.signal.removeEventListener("abort", onAbort);
|
|
519
|
+
if (reader) {
|
|
520
|
+
if (!state.eof) { try { void reader.cancel().catch(() => {}); } catch {} }
|
|
521
|
+
try { reader.releaseLock(); } catch {}
|
|
440
522
|
}
|
|
523
|
+
complete();
|
|
524
|
+
if (fetchState === state) { fetchState = null; queueMicrotask(drainReady); }
|
|
441
525
|
}
|
|
442
526
|
};
|
|
443
527
|
function drainReady() {
|
|
444
528
|
if (settled) return;
|
|
445
529
|
try {
|
|
446
530
|
if (fetchState) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
531
|
+
refreshFetch(fetchState);
|
|
532
|
+
const action = cleanupAction(fetchState);
|
|
533
|
+
if (action === "drain") beginDrain(fetchState);
|
|
534
|
+
if (action === "abort") fetchState.controller.abort();
|
|
450
535
|
} else {
|
|
536
|
+
// The core hands over JSON metadata and the raw request body separately.
|
|
451
537
|
const fetchRequest = addon.takeCoreFetch(core);
|
|
452
|
-
if (fetchRequest) void pumpFetch(JSON.parse(fetchRequest.toString("utf8")));
|
|
538
|
+
if (fetchRequest) void pumpFetch(JSON.parse(fetchRequest.request.toString("utf8")), fetchRequest.body);
|
|
453
539
|
}
|
|
454
540
|
if (addon.coreExitCode(core) !== 0) {
|
|
455
541
|
finish(1, new Error("native output delivery failed"));
|
|
@@ -494,6 +580,12 @@ function createNativeCoreRuntime(addon, options) {
|
|
|
494
580
|
exited,
|
|
495
581
|
get error() { return outputError; },
|
|
496
582
|
write(data) { addon.writeCore(core, Buffer.from(data)); },
|
|
583
|
+
// The addon copies attachment bytes before returning.
|
|
584
|
+
writeAttachment(id, bytes) {
|
|
585
|
+
addon.writeCoreAttachment(core, id, Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
|
|
586
|
+
},
|
|
587
|
+
takeAttachment(id) { return addon.takeCoreAttachment(core, id); },
|
|
588
|
+
discardAttachments() { addon.discardCoreAttachments(core); },
|
|
497
589
|
closeStdin() { addon.closeCore(core); },
|
|
498
590
|
abortHostEffects,
|
|
499
591
|
abort(error) {
|
|
@@ -533,7 +625,8 @@ async function createWithFallback(surface, nativeMethod, wasmFactory, defaultWas
|
|
|
533
625
|
} catch (error) {
|
|
534
626
|
nativeError = error;
|
|
535
627
|
if (backend === "native" || error?.code === "LIBFX_MODEL_UNSUPPORTED_FAST" ||
|
|
536
|
-
error?.code === "LIBFX_MODEL_UNSUPPORTED_EFFORT"
|
|
628
|
+
error?.code === "LIBFX_MODEL_UNSUPPORTED_EFFORT" ||
|
|
629
|
+
typeof error?.code === "string" && error.code.startsWith("LIBFX_MODEL_METADATA_")) throw error;
|
|
537
630
|
}
|
|
538
631
|
}
|
|
539
632
|
if (backend === "native") {
|