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/node.cjs CHANGED
@@ -178,6 +178,20 @@ function loadModule(input) {
178
178
  return pending;
179
179
  }
180
180
 
181
+ // sdk/fetch-cleanup.js
182
+ var cleanupTimeoutMs = 100;
183
+ var cleanupByteLimit = 64 * 1024;
184
+ function fetchCleanupAction(state, maxAge = cleanupTimeoutMs, maxBytes = cleanupByteLimit) {
185
+ if (state.eof)
186
+ return "done";
187
+ if (state.canceled || state.failed)
188
+ return "abort";
189
+ if (state.consumed) {
190
+ return state.age >= maxAge || state.discarded >= maxBytes ? "abort" : "drain";
191
+ }
192
+ return state.alive && state.active && !state.closing ? "forward" : "abort";
193
+ }
194
+
181
195
  // sdk/fx-sdk.js
182
196
  var encoder = new TextEncoder;
183
197
  var decoder = new TextDecoder;
@@ -185,6 +199,9 @@ var strictDecoder = new TextDecoder("utf-8", { fatal: true });
185
199
  var workspaceInfoLimit = 4 * 1024;
186
200
  var workspaceCommandLimit = 64 * 1024;
187
201
  var workspaceOutputLimit = 64 * 1024;
202
+ var workspaceInstructionFileLimit = 1024 * 1024;
203
+ var workspaceInstructionPathLimit = 4 * 1024;
204
+ var workspaceReadTimeoutMs = 1e4;
188
205
  var maxInstructionsBytes = 64 * 1024;
189
206
  var maxApiKeyBytes = 64 * 1024;
190
207
  var maxModelBytes = 1024;
@@ -198,7 +215,12 @@ var maxUnreadEventBytes = 1024 * 1024;
198
215
  var maxUnreadEvents = 256;
199
216
  var maxPromptImages = 8;
200
217
  var maxPromptImageDataBytes = 5 * 1024 * 1024;
201
- var maxPromptImagesBytes = 8 * 1024 * 1024;
218
+ var maxPromptImagesDataBytes = 8 * 1024 * 1024;
219
+ var maxPromptImageBytes = maxPromptImageDataBytes / 4 * 3;
220
+ var maxPromptImagesBytes = maxPromptImagesDataBytes / 4 * 3;
221
+ var maxPendingAttachments = 8;
222
+ var maxOutboundAttachments = 4;
223
+ var maxCheckpointBytes = 4 * 1024 * 1024;
202
224
  var maxPromptFrameBytes = 8 * 1024 * 1024;
203
225
  var promptFrameEnvelopeBytes = 128;
204
226
  var maxSteeringMessageBytes = 64 * 1024;
@@ -255,6 +277,14 @@ function normalizeFast(value) {
255
277
  }
256
278
  return value;
257
279
  }
280
+ function normalizeUltrafast(value) {
281
+ if (value === undefined)
282
+ return;
283
+ if (typeof value !== "boolean") {
284
+ throw new TypeError("ultrafast must be a boolean");
285
+ }
286
+ return value;
287
+ }
258
288
  function normalizeAgentOptions(value) {
259
289
  if (!value || typeof value !== "object" || Array.isArray(value)) {
260
290
  throw new TypeError("createFxAgent() options must be an object");
@@ -265,24 +295,29 @@ function normalizeAgentOptions(value) {
265
295
  }
266
296
  options.apiKey = boundedString(options.apiKey, "apiKey", maxApiKeyBytes, true);
267
297
  if (options.model !== null && typeof options.model === "object" && !Array.isArray(options.model)) {
268
- if (Object.hasOwn(options, "effort") || Object.hasOwn(options, "fast")) {
269
- throw new TypeError("model options cannot be mixed with top-level effort or fast");
298
+ if (Object.hasOwn(options, "effort") || Object.hasOwn(options, "fast") || Object.hasOwn(options, "ultrafast")) {
299
+ throw new TypeError("model options cannot be mixed with top-level effort, fast, or ultrafast");
270
300
  }
271
301
  const model = options.model;
272
302
  for (const name of Object.keys(model)) {
273
- if (name !== "id" && name !== "effort" && name !== "fast") {
303
+ if (name !== "id" && name !== "effort" && name !== "fast" && name !== "ultrafast") {
274
304
  throw new TypeError(`unsupported model option: ${name}`);
275
305
  }
276
306
  }
277
307
  options.model = boundedString(model.id, "model.id", maxModelBytes, true);
278
308
  options.effort = normalizeEffort(model.effort);
279
309
  options.fast = normalizeFast(model.fast);
310
+ options.ultrafast = normalizeUltrafast(model.ultrafast);
280
311
  } else {
281
312
  options.model = boundedString(options.model, "model", maxModelBytes, false);
282
313
  options.effort = normalizeEffort(options.effort);
283
314
  options.fast = normalizeFast(options.fast);
315
+ options.ultrafast = normalizeUltrafast(options.ultrafast);
284
316
  }
285
317
  validateGatewayChatUrl(options.gatewayChatUrl);
318
+ if (options.resizeImage !== undefined && typeof options.resizeImage !== "function") {
319
+ throw new TypeError("resizeImage must be a function");
320
+ }
286
321
  return options;
287
322
  }
288
323
  function agentEnvironment(options) {
@@ -291,13 +326,14 @@ function agentEnvironment(options) {
291
326
  ...options.model === undefined ? {} : { FX_MODEL: options.model },
292
327
  ...options.effort === undefined ? {} : { FX_EFFORT: options.effort },
293
328
  ...options.fast === undefined ? {} : { FX_FAST: options.fast ? "true" : "false" },
329
+ ...options.ultrafast === undefined ? {} : { FX_ULTRAFAST: options.ultrafast ? "true" : "false" },
294
330
  ...options.gatewayChatUrl === undefined ? {} : { FX_GATEWAY_CHAT_URL: options.gatewayChatUrl }
295
331
  };
296
332
  }
297
333
  function agentRpcError(response) {
298
334
  const error = new Error(response.message);
299
335
  const data = response.data;
300
- if (data && ["LIBFX_MODEL_UNSUPPORTED_EFFORT", "LIBFX_MODEL_UNSUPPORTED_FAST"].includes(data.code) && typeof data.model === "string" && data.capability === (data.code === "LIBFX_MODEL_UNSUPPORTED_FAST" ? "fast" : "effort")) {
336
+ if (data && ["LIBFX_MODEL_UNSUPPORTED_EFFORT", "LIBFX_MODEL_UNSUPPORTED_FAST", "LIBFX_MODEL_UNSUPPORTED_ULTRAFAST"].includes(data.code) && typeof data.model === "string" && data.capability === (data.code === "LIBFX_MODEL_UNSUPPORTED_FAST" ? "fast" : data.code === "LIBFX_MODEL_UNSUPPORTED_ULTRAFAST" ? "ultrafast" : "effort")) {
301
337
  error.code = data.code;
302
338
  error.model = data.model;
303
339
  error.capability = data.capability;
@@ -402,13 +438,20 @@ function validWorkspacePath(path) {
402
438
  return false;
403
439
  return path.slice(1).split("/").every((part) => part && part !== "." && part !== "..");
404
440
  }
441
+ function workspacePathInside(root, path) {
442
+ return root === "/" ? path.startsWith("/") : path === root || path.startsWith(`${root}/`);
443
+ }
444
+ function workspaceInstructionPathAllowed(info, path) {
445
+ return validWorkspacePath(path) && path.endsWith("/AGENTS.md") && (workspacePathInside(info.root, path) || workspacePathInside(info.home, path));
446
+ }
405
447
  function prepareWorkspaceAdapter(workspace) {
406
448
  if (workspace == null)
407
449
  return { present: false, valid: false };
408
450
  try {
409
451
  const info = workspace.info;
410
452
  const permission = workspace.permission;
411
- if (!info || typeof workspace.exec !== "function" || info.version !== 1 || !validWorkspacePath(info.root) || !validWorkspacePath(info.cwd) || !validWorkspacePath(info.home) || info.cwd !== info.root || info.gitAvailable !== false || info.ephemeral !== true || permission !== "allow-sandboxed" && permission !== "prompt") {
453
+ const readFile = workspace.readFile;
454
+ if (!info || typeof workspace.exec !== "function" || info.version !== 1 || readFile !== undefined && typeof readFile !== "function" || !validWorkspacePath(info.root) || !validWorkspacePath(info.cwd) || !validWorkspacePath(info.home) || info.cwd !== info.root || info.gitAvailable !== false || info.ephemeral !== true || permission !== "allow-sandboxed" && permission !== "prompt") {
412
455
  return { present: true, valid: false };
413
456
  }
414
457
  const value = {
@@ -418,7 +461,8 @@ function prepareWorkspaceAdapter(workspace) {
418
461
  home: info.home,
419
462
  git: false,
420
463
  ephemeral: true,
421
- permission
464
+ permission,
465
+ instruction_files: typeof readFile === "function"
422
466
  };
423
467
  const encoded = encoder.encode(JSON.stringify(value));
424
468
  if (encoded.length > workspaceInfoLimit)
@@ -660,7 +704,10 @@ function createRuntime(options) {
660
704
  const steering = [];
661
705
  let steeringBytes = 0;
662
706
  let steeringOpen = false;
663
- const workspaceExecs = new Set;
707
+ const inboundAttachments = new Map;
708
+ const outboundAttachments = new Map;
709
+ let nextOutboundAttachment = 1;
710
+ const workspaceOperations = new Set;
664
711
  const workspace = prepareWorkspaceAdapter(options.workspace);
665
712
  const args = ["fx", ...options.args || []];
666
713
  const env = Object.entries(options.env || {}).map(([key, value]) => `${key}=${value}`);
@@ -983,6 +1030,37 @@ function createRuntime(options) {
983
1030
  steeringBytes -= value.length;
984
1031
  return value.length;
985
1032
  }
1033
+ function writeAttachment(id, data) {
1034
+ if (inboundAttachments.size >= maxPendingAttachments)
1035
+ throw new Error("attachment table is full");
1036
+ inboundAttachments.set(id, data.slice());
1037
+ }
1038
+ function attachmentSize(id) {
1039
+ return inboundAttachments.get(id >>> 0)?.length ?? -1;
1040
+ }
1041
+ function attachmentTake(id, outputPtr, outputCap) {
1042
+ const key = id >>> 0;
1043
+ const value = inboundAttachments.get(key);
1044
+ if (!value)
1045
+ return -1;
1046
+ if (value.length > 0) {
1047
+ const output = checkedBytes(outputPtr, outputCap);
1048
+ if (!output || value.length > output.length)
1049
+ return -1;
1050
+ output.set(value);
1051
+ }
1052
+ inboundAttachments.delete(key);
1053
+ return value.length;
1054
+ }
1055
+ function attachmentPut(inputPtr, inputLen) {
1056
+ const input = checkedBytes(inputPtr, inputLen);
1057
+ if (!input || outboundAttachments.size >= maxOutboundAttachments)
1058
+ return -1;
1059
+ const id = nextOutboundAttachment;
1060
+ nextOutboundAttachment = id === 2147483647 ? 1 : id + 1;
1061
+ outboundAttachments.set(id, input.slice());
1062
+ return id;
1063
+ }
986
1064
  let pendingHostToolResult = null;
987
1065
  function hostToolCall(namePtr, nameLen, argumentsPtr, argumentsLen, outputPtr, outputCap, statusPtr) {
988
1066
  pendingHostToolResult = null;
@@ -1238,33 +1316,13 @@ function createRuntime(options) {
1238
1316
  }
1239
1317
  if (command.includes("\x00"))
1240
1318
  return Promise.resolve(-4);
1241
- const controller = new AbortController;
1242
- let resolveAbort;
1243
- const aborted = new Promise((resolve) => {
1244
- resolveAbort = resolve;
1245
- });
1246
- const state = {
1247
- controller,
1248
- status: null,
1249
- abort(status) {
1250
- if (this.status !== null)
1251
- return;
1252
- this.status = status;
1253
- resolveAbort(status);
1254
- controller.abort(new DOMException(status === -5 ? "workspace command timed out" : "workspace command aborted", status === -5 ? "TimeoutError" : "AbortError"));
1255
- }
1256
- };
1257
- workspaceExecs.add(state);
1258
- const timer = setTimeout(() => state.abort(-5), timeoutMs);
1259
- const execution = Promise.resolve().then(() => workspace.adapter.exec({
1319
+ return runWorkspaceOperation(timeoutMs, "workspace command", (signal) => workspace.adapter.exec({
1260
1320
  command,
1261
1321
  cwd: workspace.info.cwd,
1262
- signal: controller.signal,
1322
+ signal,
1263
1323
  timeoutMs,
1264
1324
  outputLimitBytes: workspaceOutputLimit
1265
- })).then((value) => {
1266
- if (state.status !== null)
1267
- return state.status;
1325
+ }), (value) => {
1268
1326
  if (!value || !Number.isInteger(value.exitCode) || value.exitCode < -2147483648 || value.exitCode > 2147483647 || typeof value.stdout !== "string" || typeof value.stderr !== "string")
1269
1327
  return -1;
1270
1328
  const stdout = encoder.encode(value.stdout);
@@ -1297,6 +1355,77 @@ function createRuntime(options) {
1297
1355
  view.setUint32(24, stderr.length, true);
1298
1356
  view.setUint32(28, copied < stdout.length + stderr.length ? 1 : 0, true);
1299
1357
  return 0;
1358
+ });
1359
+ }
1360
+ function workspaceReadFile(pathPtr, pathLen, outPtr, outCap, resultPtr) {
1361
+ if (!workspace.present || workspace.valid && typeof workspace.adapter.readFile !== "function") {
1362
+ return Promise.resolve(-2);
1363
+ }
1364
+ if (!workspace.valid)
1365
+ return Promise.resolve(-4);
1366
+ const pathBytes = checkedBytes(pathPtr, pathLen);
1367
+ if (!pathBytes || !checkedBytes(outPtr, outCap) || !checkedBytes(resultPtr, 8) || pathLen > workspaceInstructionPathLimit || outCap > workspaceInstructionFileLimit) {
1368
+ return Promise.resolve(-4);
1369
+ }
1370
+ let path;
1371
+ try {
1372
+ path = strictDecoder.decode(pathBytes);
1373
+ } catch {
1374
+ return Promise.resolve(-4);
1375
+ }
1376
+ if (!workspaceInstructionPathAllowed(workspace.info, path))
1377
+ return Promise.resolve(-4);
1378
+ return runWorkspaceOperation(workspaceReadTimeoutMs, "workspace read", (signal) => workspace.adapter.readFile({
1379
+ path,
1380
+ signal
1381
+ }), (value) => {
1382
+ if (value == null)
1383
+ return -6;
1384
+ let content;
1385
+ if (typeof value === "string") {
1386
+ content = encoder.encode(value);
1387
+ } else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
1388
+ content = value instanceof ArrayBuffer ? new Uint8Array(value) : new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
1389
+ try {
1390
+ strictDecoder.decode(content);
1391
+ } catch {
1392
+ return -7;
1393
+ }
1394
+ } else {
1395
+ return -1;
1396
+ }
1397
+ if (content.length > 4294967295)
1398
+ return -1;
1399
+ const copied = Math.min(content.length, outCap);
1400
+ bytes(outPtr, copied).set(content.subarray(0, copied));
1401
+ const view = new DataView(memory().buffer, resultPtr, 8);
1402
+ view.setUint32(0, content.length, true);
1403
+ view.setUint32(4, copied, true);
1404
+ return 0;
1405
+ });
1406
+ }
1407
+ function runWorkspaceOperation(timeoutMs, label, start, finish) {
1408
+ const controller = new AbortController;
1409
+ let resolveAbort;
1410
+ const aborted = new Promise((resolve) => {
1411
+ resolveAbort = resolve;
1412
+ });
1413
+ const state = {
1414
+ status: null,
1415
+ abort(status) {
1416
+ if (this.status !== null)
1417
+ return;
1418
+ this.status = status;
1419
+ resolveAbort(status);
1420
+ controller.abort(new DOMException(status === -5 ? `${label} timed out` : `${label} aborted`, status === -5 ? "TimeoutError" : "AbortError"));
1421
+ }
1422
+ };
1423
+ workspaceOperations.add(state);
1424
+ const timer = setTimeout(() => state.abort(-5), timeoutMs);
1425
+ const execution = Promise.resolve().then(() => start(controller.signal)).then((value) => {
1426
+ if (state.status !== null)
1427
+ return state.status;
1428
+ return finish(value);
1300
1429
  }).catch((error) => {
1301
1430
  if (state.status !== null)
1302
1431
  return state.status;
@@ -1308,14 +1437,14 @@ function createRuntime(options) {
1308
1437
  });
1309
1438
  return Promise.race([execution, aborted]).finally(() => {
1310
1439
  clearTimeout(timer);
1311
- workspaceExecs.delete(state);
1440
+ workspaceOperations.delete(state);
1312
1441
  });
1313
1442
  }
1314
1443
  function abortHostEffects() {
1315
1444
  pendingHostToolResult = null;
1316
1445
  streams.forEach((state) => state.controller.abort(abortReason));
1317
1446
  httpRequests.forEach((controller) => controller.abort(abortReason));
1318
- workspaceExecs.forEach((state) => state.abort(-3));
1447
+ workspaceOperations.forEach((state) => state.abort(-3));
1319
1448
  }
1320
1449
  const unavailable = () => 52;
1321
1450
  const wasi = {
@@ -1421,6 +1550,7 @@ function createRuntime(options) {
1421
1550
  },
1422
1551
  fx_workspace_info: workspaceInfo,
1423
1552
  fx_workspace_exec: new WebAssembly.Suspending(workspaceExec),
1553
+ fx_workspace_read_file: new WebAssembly.Suspending(workspaceReadFile),
1424
1554
  fx_http_stream_open: streamOpen,
1425
1555
  fx_http_stream_status: new WebAssembly.Suspending(streamStatus),
1426
1556
  fx_http_stream_next: new WebAssembly.Suspending(streamNext),
@@ -1446,6 +1576,9 @@ function createRuntime(options) {
1446
1576
  steeringOpen = false;
1447
1577
  clearSteering();
1448
1578
  },
1579
+ fx_attachment_size: attachmentSize,
1580
+ fx_attachment_take: attachmentTake,
1581
+ fx_attachment_put: attachmentPut,
1449
1582
  fx_open_url: new WebAssembly.Suspending(openUrl),
1450
1583
  fx_oauth_session_load: new WebAssembly.Suspending(oauthSessionLoad),
1451
1584
  fx_oauth_session_commit: new WebAssembly.Suspending(oauthSessionCommit),
@@ -1476,6 +1609,15 @@ function createRuntime(options) {
1476
1609
  write(data) {
1477
1610
  stdin.push(typeof data === "string" ? encoder.encode(data) : data);
1478
1611
  },
1612
+ writeAttachment,
1613
+ takeAttachment(id) {
1614
+ const value = outboundAttachments.get(id) ?? null;
1615
+ outboundAttachments.delete(id);
1616
+ return value;
1617
+ },
1618
+ discardAttachments() {
1619
+ inboundAttachments.clear();
1620
+ },
1479
1621
  wake() {
1480
1622
  stdin.wake();
1481
1623
  },
@@ -1659,45 +1801,122 @@ function blobByteLength(value) {
1659
1801
  return null;
1660
1802
  }
1661
1803
  }
1662
- function normalizePromptInput(input) {
1804
+ function normalizeImageSourceRef(value, name) {
1805
+ boundedString(value, `${name} sourceRef`, 512, false);
1806
+ if (value !== undefined && /[\x00-\x1f\x7f\uD800-\uDFFF]/u.test(value)) {
1807
+ throw new TypeError(`${name} sourceRef must be valid UTF-8 without ASCII controls`);
1808
+ }
1809
+ return value;
1810
+ }
1811
+ function omitReferencedImageData(blocks) {
1812
+ return blocks.map((block) => block.type !== "image" || block.sourceRef === undefined ? block : {
1813
+ type: "image",
1814
+ mimeType: block.mimeType,
1815
+ sourceRef: block.sourceRef
1816
+ });
1817
+ }
1818
+ function promptImageDataBytes(prompt) {
1819
+ return prompt.reduce((total, block) => {
1820
+ if (block.type !== "image")
1821
+ return total;
1822
+ if (typeof block.data === "string")
1823
+ return total + block.data.length;
1824
+ const byteLength = block.bytes?.byteLength ?? block.byteLength ?? blobByteLength(block.data) ?? 0;
1825
+ return total + Math.ceil(byteLength / 3) * 4;
1826
+ }, 0);
1827
+ }
1828
+ function byteView(value) {
1829
+ if (value instanceof ArrayBuffer)
1830
+ return new Uint8Array(value);
1831
+ if (ArrayBuffer.isView(value))
1832
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
1833
+ return null;
1834
+ }
1835
+ var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1836
+ function canonicalBase64ByteLength(value) {
1837
+ if (value.length === 0 || value.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(value))
1838
+ return -1;
1839
+ const groups = value.length / 4;
1840
+ if (value.endsWith("==")) {
1841
+ return (base64Alphabet.indexOf(value[value.length - 3]) & 15) === 0 ? groups * 3 - 2 : -1;
1842
+ }
1843
+ if (value.endsWith("=")) {
1844
+ return (base64Alphabet.indexOf(value[value.length - 2]) & 3) === 0 ? groups * 3 - 1 : -1;
1845
+ }
1846
+ return groups * 3;
1847
+ }
1848
+ function requireImageMimeType(mimeType, index) {
1849
+ if (typeof mimeType !== "string" || mimeType.length === 0 || mimeType.length > 128) {
1850
+ throw new TypeError(`image prompt block ${index} requires a mimeType`);
1851
+ }
1852
+ }
1853
+ function checkImageByteLength(byteLength, index) {
1854
+ if (byteLength > maxPromptImageBytes) {
1855
+ throw new RangeError(`image prompt block ${index} exceeds the ${maxPromptImageBytes} byte per-image libfx limit`);
1856
+ }
1857
+ }
1858
+ function checkPromptImagesByteLength(byteLength) {
1859
+ if (byteLength > maxPromptImagesBytes) {
1860
+ throw new RangeError(`prompt images exceed the ${maxPromptImagesBytes} byte libfx limit`);
1861
+ }
1862
+ }
1863
+ function normalizePromptImage(block, index) {
1864
+ const sourceRef = normalizeImageSourceRef(block.sourceRef, `image prompt block ${index}`);
1865
+ const reference = sourceRef === undefined ? {} : { sourceRef };
1866
+ if (block.data === undefined && sourceRef !== undefined) {
1867
+ requireImageMimeType(block.mimeType, index);
1868
+ return { type: "image", mimeType: block.mimeType, ...reference };
1869
+ }
1870
+ const size = blobByteLength(block.data);
1871
+ if (size !== null) {
1872
+ const mimeType = block.data.type;
1873
+ if (block.mimeType !== undefined && block.mimeType !== mimeType) {
1874
+ throw new TypeError(`image prompt block ${index} mimeType disagrees with Blob.type`);
1875
+ }
1876
+ requireImageMimeType(mimeType, index);
1877
+ if (!Number.isSafeInteger(size) || size <= 0) {
1878
+ throw new TypeError(`image prompt block ${index} requires a non-empty Blob with a valid size`);
1879
+ }
1880
+ return { type: "image", source: "blob", data: block.data, mimeType, byteLength: size, ...reference };
1881
+ }
1882
+ if (typeof block.data === "string" && block.data.length > 0) {
1883
+ requireImageMimeType(block.mimeType, index);
1884
+ const byteLength = canonicalBase64ByteLength(block.data);
1885
+ if (byteLength <= 0)
1886
+ throw new TypeError(`image prompt block ${index} requires canonical base64 data`);
1887
+ return { type: "image", source: "base64", data: block.data, mimeType: block.mimeType, byteLength, ...reference };
1888
+ }
1889
+ const bytes = typeof block.data === "string" ? null : byteView(block.data);
1890
+ if (!bytes || bytes.byteLength === 0) {
1891
+ throw new TypeError(`image prompt block ${index} requires base64 data, bytes, or a Blob`);
1892
+ }
1893
+ requireImageMimeType(block.mimeType, index);
1894
+ return { type: "image", source: "bytes", data: bytes, mimeType: block.mimeType, byteLength: bytes.byteLength, ...reference };
1895
+ }
1896
+ function normalizePromptInput(input, { deferImageLimits = false } = {}) {
1663
1897
  if (typeof input === "string")
1664
1898
  return [{ type: "text", text: input }];
1665
1899
  if (!Array.isArray(input))
1666
1900
  throw new TypeError("prompt input must be a string or an array of prompt blocks");
1667
1901
  let imageCount = 0;
1668
1902
  let imageBytes = 0;
1669
- return input.map((block, index) => {
1903
+ let prompt = input.map((block, index) => {
1670
1904
  if (!block || typeof block !== "object")
1671
1905
  throw new TypeError(`prompt block ${index} must be an object`);
1672
1906
  if (block.type === "image") {
1673
- const size = blobByteLength(block.data);
1674
- const blob = size !== null;
1675
- if (!blob && (typeof block.data !== "string" || block.data.length === 0)) {
1676
- throw new TypeError(`image prompt block ${index} requires base64 data or a Blob`);
1677
- }
1678
- const mimeType = blob ? block.data.type : block.mimeType;
1679
- if (blob && block.mimeType !== undefined && block.mimeType !== mimeType) {
1680
- throw new TypeError(`image prompt block ${index} mimeType disagrees with Blob.type`);
1681
- }
1682
- if (typeof mimeType !== "string" || mimeType.length === 0 || mimeType.length > 128) {
1683
- throw new TypeError(`image prompt block ${index} requires a mimeType`);
1684
- }
1685
- if (blob && (!Number.isSafeInteger(size) || size <= 0)) {
1686
- throw new TypeError(`image prompt block ${index} requires a non-empty Blob with a valid size`);
1687
- }
1688
- const encodedLength = blob ? Math.ceil(size / 3) * 4 : block.data.length;
1689
- if (encodedLength > maxPromptImageDataBytes) {
1690
- throw new RangeError(`image prompt block ${index} exceeds the ${maxPromptImageDataBytes} byte per-image libfx limit`);
1691
- }
1907
+ const image = normalizePromptImage(block, index);
1692
1908
  imageCount += 1;
1693
1909
  if (imageCount > maxPromptImages) {
1694
1910
  throw new RangeError(`prompt cannot contain more than ${maxPromptImages} images`);
1695
1911
  }
1696
- imageBytes += encodedLength;
1697
- if (imageBytes > maxPromptImagesBytes) {
1698
- throw new RangeError(`prompt images exceed the ${maxPromptImagesBytes} byte libfx frame limit`);
1912
+ if (!deferImageLimits) {
1913
+ if (image.byteLength > maxPromptImageBytes && image.sourceRef !== undefined) {
1914
+ return omitReferencedImageData([image])[0];
1915
+ }
1916
+ checkImageByteLength(image.byteLength ?? 0, index);
1917
+ imageBytes += image.byteLength ?? 0;
1699
1918
  }
1700
- return { type: "image", data: block.data, mimeType };
1919
+ return image;
1701
1920
  }
1702
1921
  if (block.type === "text") {
1703
1922
  if (typeof block.text !== "string")
@@ -1714,52 +1933,89 @@ function normalizePromptInput(input) {
1714
1933
  }
1715
1934
  throw new TypeError(`unsupported prompt block type: ${String(block.type)}`);
1716
1935
  });
1936
+ if (!deferImageLimits) {
1937
+ if (imageBytes > maxPromptImagesBytes) {
1938
+ prompt = omitReferencedImageData(prompt);
1939
+ imageBytes = prompt.reduce((total, block) => total + (block.type === "image" ? block.byteLength ?? 0 : 0), 0);
1940
+ }
1941
+ checkPromptImagesByteLength(imageBytes);
1942
+ if (promptFrameSize(prompt) > maxPromptFrameBytes)
1943
+ prompt = omitReferencedImageData(prompt);
1944
+ }
1945
+ return prompt;
1946
+ }
1947
+ function promptFrameSize(prompt, countImages = true) {
1948
+ const encodedImageBytes = countImages ? promptImageDataBytes(prompt) : 0;
1949
+ const projected = prompt.map((block) => block.type !== "image" ? block : {
1950
+ type: "image",
1951
+ mimeType: block.mimeType,
1952
+ ...block.sourceRef === undefined ? {} : { sourceRef: block.sourceRef },
1953
+ ...block.data === undefined && block.bytes === undefined ? {} : { _meta: { fx: { attachment: 4294967295 } } }
1954
+ });
1955
+ return encoder.encode(JSON.stringify({ sessionId: "", prompt: projected })).length + encodedImageBytes + promptFrameEnvelopeBytes;
1956
+ }
1957
+ function checkPromptFrameSize(prompt, countImages) {
1958
+ if (promptFrameSize(prompt, countImages) > maxPromptFrameBytes) {
1959
+ throw new RangeError(`prompt exceeds the ${maxPromptFrameBytes} byte libfx frame limit`);
1960
+ }
1717
1961
  }
1718
- function promptFrameSize(prompt) {
1719
- let blobDataBytes = 0;
1720
- const projected = prompt.map((block) => {
1721
- if (block.type !== "image" || typeof block.data === "string")
1722
- return block;
1723
- const size = blobByteLength(block.data);
1724
- if (!Number.isSafeInteger(size))
1725
- throw new TypeError("image prompt requires a valid Blob size");
1726
- blobDataBytes += Math.ceil(size / 3) * 4;
1727
- return { type: "image", data: "", mimeType: block.mimeType };
1962
+ function preparePromptImages(blocks) {
1963
+ return blocks.map((block) => block.type !== "image" || block.data === undefined ? block : {
1964
+ type: "image",
1965
+ mimeType: block.mimeType,
1966
+ bytes: block.source === "base64" ? base64ToBytes(block.data) : block.data,
1967
+ ...block.sourceRef === undefined ? {} : { sourceRef: block.sourceRef }
1728
1968
  });
1729
- return encoder.encode(JSON.stringify({ sessionId: "", prompt: projected })).length + blobDataBytes + promptFrameEnvelopeBytes;
1730
1969
  }
1731
- async function materializePromptBlobs(blocks, isCancelled) {
1732
- const encoded = [];
1970
+ function resizedPromptImage(value, index) {
1971
+ const bytes = byteView(value?.bytes);
1972
+ if (!bytes || bytes.byteLength === 0 || typeof value.mimeType !== "string" || value.mimeType.length === 0 || value.mimeType.length > 128) {
1973
+ throw new TypeError(`resizeImage must return non-empty bytes and a mimeType for image prompt block ${index}`);
1974
+ }
1975
+ return { bytes: bytes.slice(), mimeType: value.mimeType };
1976
+ }
1977
+ async function materializePromptImages(blocks, isCancelled, resizeImage) {
1978
+ let prepared = [];
1733
1979
  let imageBytes = 0;
1734
- for (const [index, block] of blocks.entries()) {
1980
+ for (let index = 0;index < blocks.length; index++) {
1735
1981
  if (isCancelled())
1736
1982
  return null;
1737
- if (block.type !== "image") {
1738
- encoded.push(block);
1983
+ const block = blocks[index];
1984
+ if (block.type !== "image" || block.data === undefined) {
1985
+ prepared.push(block);
1739
1986
  continue;
1740
1987
  }
1741
- if (typeof block.data === "string") {
1742
- imageBytes += block.data.length;
1743
- if (imageBytes > maxPromptImagesBytes) {
1744
- throw new RangeError(`prompt images exceed the ${maxPromptImagesBytes} byte libfx frame limit`);
1745
- }
1746
- encoded.push(block);
1747
- continue;
1748
- }
1749
- const bytes = new Uint8Array(await block.data.arrayBuffer());
1988
+ let image = block.source === "blob" ? { mimeType: block.mimeType, bytes: new Uint8Array(await block.data.arrayBuffer()) } : preparePromptImages([block])[0];
1750
1989
  if (isCancelled())
1751
1990
  return null;
1752
- const encodedLength = Math.ceil(bytes.length / 3) * 4;
1753
- if (encodedLength > maxPromptImageDataBytes) {
1754
- throw new RangeError(`image prompt block ${index} exceeds the ${maxPromptImageDataBytes} byte per-image libfx limit`);
1991
+ if (resizeImage) {
1992
+ image = resizedPromptImage(await resizeImage({ bytes: image.bytes, mimeType: image.mimeType }), index);
1993
+ if (isCancelled())
1994
+ return null;
1755
1995
  }
1756
- imageBytes += encodedLength;
1996
+ image = {
1997
+ type: "image",
1998
+ mimeType: image.mimeType,
1999
+ bytes: image.bytes,
2000
+ ...block.sourceRef === undefined ? {} : { sourceRef: block.sourceRef }
2001
+ };
2002
+ if (image.bytes.byteLength > maxPromptImageBytes && image.sourceRef !== undefined) {
2003
+ image = omitReferencedImageData([image])[0];
2004
+ }
2005
+ checkImageByteLength(image.bytes?.byteLength ?? 0, index);
2006
+ imageBytes += image.bytes?.byteLength ?? 0;
2007
+ prepared.push(image);
1757
2008
  if (imageBytes > maxPromptImagesBytes) {
1758
- throw new RangeError(`prompt images exceed the ${maxPromptImagesBytes} byte libfx frame limit`);
2009
+ prepared = omitReferencedImageData(prepared);
2010
+ blocks = omitReferencedImageData(blocks);
2011
+ imageBytes = prepared.reduce((total, entry) => total + (entry.bytes?.byteLength ?? 0), 0);
1759
2012
  }
1760
- encoded.push({ type: "image", data: bytesToBase64(bytes), mimeType: block.mimeType });
2013
+ checkPromptImagesByteLength(imageBytes);
1761
2014
  }
1762
- return normalizePromptInput(encoded);
2015
+ if (promptFrameSize(prepared) > maxPromptFrameBytes)
2016
+ prepared = omitReferencedImageData(prepared);
2017
+ checkPromptFrameSize(prepared, true);
2018
+ return prepared;
1763
2019
  }
1764
2020
  function normalizeSteeringInput(input) {
1765
2021
  const blocks = normalizePromptInput(input);
@@ -1846,18 +2102,31 @@ function hostToolContent(value) {
1846
2102
  if (typeof value.text !== "string" || !Array.isArray(value.images) || value.images.length > 8) {
1847
2103
  throw new TypeError("invalid typed tool result");
1848
2104
  }
1849
- let imageBytes = 0;
1850
- const images = value.images.map((image) => {
1851
- if (image?.type !== "image" || typeof image.data !== "string" || typeof image.mimeType !== "string" || image.mimeType.length > 128 || image.data.length > 5 * 1024 * 1024) {
2105
+ let images = value.images.map((image) => {
2106
+ if (image?.type !== "image")
2107
+ throw new TypeError("invalid tool image");
2108
+ const sourceRef = normalizeImageSourceRef(image.sourceRef, "tool image");
2109
+ const referenceOnly = image.data === undefined && sourceRef !== undefined;
2110
+ if (!referenceOnly && typeof image.data !== "string" || typeof image.mimeType !== "string" || image.mimeType.length === 0 || image.mimeType.length > 128 || image.data?.length > maxPromptImageDataBytes && sourceRef === undefined) {
1852
2111
  throw new TypeError("invalid tool image");
1853
2112
  }
1854
- imageBytes += image.data.length;
1855
- if (imageBytes > 8 * 1024 * 1024)
1856
- throw new RangeError("tool images exceed the result limit");
1857
- return { type: "image", data: image.data, mimeType: image.mimeType };
2113
+ return {
2114
+ type: "image",
2115
+ ...!referenceOnly && image.data.length <= maxPromptImageDataBytes ? { data: image.data } : {},
2116
+ mimeType: image.mimeType,
2117
+ ...sourceRef === undefined ? {} : { sourceRef }
2118
+ };
1858
2119
  });
1859
- const content = JSON.stringify({ text: value.text, images });
1860
- if (new TextEncoder().encode(content).length > 8 * 1024 * 1024)
2120
+ if (promptImageDataBytes(images) > maxPromptImagesDataBytes)
2121
+ images = omitReferencedImageData(images);
2122
+ if (promptImageDataBytes(images) > maxPromptImagesDataBytes)
2123
+ throw new RangeError("tool images exceed the result limit");
2124
+ let content = JSON.stringify({ text: value.text, images });
2125
+ if (encoder.encode(content).length > maxPromptImagesDataBytes) {
2126
+ images = omitReferencedImageData(images);
2127
+ content = JSON.stringify({ text: value.text, images });
2128
+ }
2129
+ if (encoder.encode(content).length > maxPromptImagesDataBytes)
1861
2130
  throw new RangeError("typed tool result exceeds the result limit");
1862
2131
  return { content, rich: true, isError: value.isError === true };
1863
2132
  }
@@ -1880,13 +2149,6 @@ function checkpointBytes(value) {
1880
2149
  }
1881
2150
  throw new TypeError("checkpoint must be an ArrayBuffer or typed array");
1882
2151
  }
1883
- function bytesToBase64(value) {
1884
- let binary = "";
1885
- for (let offset = 0;offset < value.length; offset += 32768) {
1886
- binary += String.fromCharCode(...value.subarray(offset, offset + 32768));
1887
- }
1888
- return btoa(binary);
1889
- }
1890
2152
  function base64ToBytes(value) {
1891
2153
  const binary = atob(value);
1892
2154
  const bytes = new Uint8Array(binary.length);
@@ -1899,6 +2161,9 @@ async function createFxAgent(options = {}) {
1899
2161
  const hostTools = normalizeHostTools(options.tools);
1900
2162
  const instructions = normalizeInstructions(options.instructions);
1901
2163
  const initialCheckpoint = checkpointBytes(options.checkpoint);
2164
+ if (initialCheckpoint && initialCheckpoint.byteLength > maxCheckpointBytes) {
2165
+ throw new Error("libfx checkpoint is too large");
2166
+ }
1902
2167
  const pending = new Map;
1903
2168
  let nextId = 1;
1904
2169
  let sessionId = null;
@@ -2061,11 +2326,49 @@ async function createFxAgent(options = {}) {
2061
2326
  reject(error);
2062
2327
  }
2063
2328
  });
2329
+ let nextAttachmentId = 1;
2330
+ const attachBytes = (payloads) => {
2331
+ if (typeof runtime.writeAttachment !== "function")
2332
+ throw new Error("fx runtime does not accept binary attachments");
2333
+ runtime.discardAttachments?.();
2334
+ return payloads.map((bytes) => {
2335
+ const id = nextAttachmentId;
2336
+ nextAttachmentId = id === 2147483647 ? 1 : id + 1;
2337
+ runtime.writeAttachment(id, bytes);
2338
+ return id;
2339
+ });
2340
+ };
2341
+ let checkpointTail = null;
2342
+ let pendingCheckpoints = 0;
2343
+ async function takeCheckpoint() {
2344
+ if (closing)
2345
+ throw new Error("fx agent is closed");
2346
+ if (activeTurn)
2347
+ throw new Error("cannot checkpoint while a prompt is active");
2348
+ const response = await request("libfx/checkpoint", { sessionId });
2349
+ const id = response?.checkpointAttachment;
2350
+ const bytes = Number.isSafeInteger(id) && id > 0 ? runtime.takeAttachment?.(id) : null;
2351
+ if (!(bytes instanceof Uint8Array) || bytes.byteLength === 0)
2352
+ throw new Error("fx returned an invalid checkpoint");
2353
+ return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2354
+ }
2355
+ const sendPrompt = (blocks) => {
2356
+ const images = blocks.filter((block) => block.type === "image" && block.bytes !== undefined);
2357
+ const ids = images.length ? attachBytes(images.map((block) => block.bytes)) : [];
2358
+ let next = 0;
2359
+ const prompt = blocks.map((block) => block.type !== "image" ? block : {
2360
+ type: "image",
2361
+ mimeType: block.mimeType,
2362
+ ...block.sourceRef === undefined ? {} : { sourceRef: block.sourceRef },
2363
+ ...block.bytes === undefined ? {} : { _meta: { fx: { attachment: ids[next++] } } }
2364
+ });
2365
+ return request("session/prompt", { sessionId, prompt });
2366
+ };
2064
2367
  runtime.exited.then((code) => {
2065
2368
  closing = true;
2066
2369
  const error = runtime.error ?? new Error(`fx-core exited with code ${code} before completing the ACP request`);
2067
2370
  coreExitError = error;
2068
- activeTurn?.failPendingBlob(error);
2371
+ activeTurn?.failImagePrep(error);
2069
2372
  for (const waiter of pending.values())
2070
2373
  waiter.reject(error);
2071
2374
  pending.clear();
@@ -2105,6 +2408,10 @@ async function createFxAgent(options = {}) {
2105
2408
  if (cancelled || closing)
2106
2409
  return;
2107
2410
  const response = { jsonrpc: "2.0", id: message.id, result: { content, isError, ...rich ? { contentType: "rich" } : {} } };
2411
+ if (rich && encoder.encode(JSON.stringify(response)).length + 1 > maxPromptFrameBytes) {
2412
+ const result = JSON.parse(content);
2413
+ response.result.content = JSON.stringify({ ...result, images: omitReferencedImageData(result.images) });
2414
+ }
2108
2415
  if (encoder.encode(JSON.stringify(response)).length + 1 > 8 * 1024 * 1024) {
2109
2416
  response.result = { content: "Host tool result exceeded the response frame limit", isError: true };
2110
2417
  }
@@ -2130,10 +2437,8 @@ async function createFxAgent(options = {}) {
2130
2437
  const sessionResult = await request("libfx/new");
2131
2438
  sessionId = sessionResult.sessionId;
2132
2439
  if (initialCheckpoint) {
2133
- await request("libfx/restore", {
2134
- sessionId,
2135
- checkpoint: bytesToBase64(initialCheckpoint)
2136
- });
2440
+ const [checkpointAttachment] = attachBytes([initialCheckpoint]);
2441
+ await request("libfx/restore", { sessionId, checkpointAttachment });
2137
2442
  }
2138
2443
  } catch (error) {
2139
2444
  closing = true;
@@ -2156,15 +2461,20 @@ async function createFxAgent(options = {}) {
2156
2461
  throw new Error("a prompt is already in progress for this session");
2157
2462
  return normalizeTurn(startTurn(input, promptOptions));
2158
2463
  },
2159
- async checkpoint() {
2160
- if (closing)
2161
- throw new Error("fx agent is closed");
2162
- if (activeTurn)
2163
- throw new Error("cannot checkpoint while a prompt is active");
2164
- const response = await request("libfx/checkpoint", { sessionId });
2165
- if (typeof response?.checkpoint !== "string")
2166
- throw new Error("fx returned an invalid checkpoint");
2167
- return base64ToBytes(response.checkpoint);
2464
+ checkpoint() {
2465
+ const previous = pendingCheckpoints > 0 ? checkpointTail : null;
2466
+ pendingCheckpoints++;
2467
+ let release;
2468
+ checkpointTail = new Promise((resolve) => {
2469
+ release = resolve;
2470
+ });
2471
+ const run = previous ? previous.then(takeCheckpoint) : takeCheckpoint();
2472
+ const settle = () => {
2473
+ pendingCheckpoints--;
2474
+ release();
2475
+ };
2476
+ run.then(settle, settle);
2477
+ return run;
2168
2478
  },
2169
2479
  async close() {
2170
2480
  if (closing) {
@@ -2288,11 +2598,12 @@ async function createFxAgent(options = {}) {
2288
2598
  return { inputTruncated: true, inputPreview: decoder.decode(utf8Prefix(bytes, maxToolStartInputBytes)) };
2289
2599
  }
2290
2600
  function startTurn(input, promptOptions) {
2291
- const prompt = normalizePromptInput(input);
2292
- const hasBlobs = prompt.some((block) => block.type === "image" && typeof block.data !== "string");
2293
- if (promptFrameSize(prompt) > maxPromptFrameBytes) {
2294
- throw new RangeError(`prompt exceeds the ${maxPromptFrameBytes} byte libfx frame limit`);
2295
- }
2601
+ const resizeImage = options.resizeImage;
2602
+ const normalized = normalizePromptInput(input, { deferImageLimits: resizeImage !== undefined });
2603
+ const hasPixels = normalized.some((block) => block.type === "image" && block.data !== undefined);
2604
+ const asyncImages = hasPixels && (resizeImage !== undefined || normalized.some((block) => block.type === "image" && block.source === "blob"));
2605
+ checkPromptFrameSize(normalized, resizeImage === undefined);
2606
+ const prompt = asyncImages ? normalized.map((block) => block.type === "image" && block.source === "bytes" ? { ...block, data: block.data.slice() } : block) : preparePromptImages(normalized);
2296
2607
  const signal = promptOptions.signal;
2297
2608
  if (signal !== undefined && (typeof signal?.addEventListener !== "function" || typeof signal?.removeEventListener !== "function"))
2298
2609
  throw new TypeError("prompt signal must be an AbortSignal");
@@ -2304,10 +2615,10 @@ async function createFxAgent(options = {}) {
2304
2615
  let terminalError;
2305
2616
  let reportedPressure = false;
2306
2617
  let discardedBytes = 0;
2307
- let cancelBlobRead = null;
2308
- let rejectBlobRead = null;
2618
+ let cancelImagePrep = null;
2619
+ let rejectImagePrep = null;
2309
2620
  let resolvePromptStart = null;
2310
- const promptStarted = hasBlobs ? new Promise((resolve) => {
2621
+ const promptStarted = asyncImages ? new Promise((resolve) => {
2311
2622
  resolvePromptStart = resolve;
2312
2623
  }) : null;
2313
2624
  let pendingSteeringCount = 0;
@@ -2348,10 +2659,10 @@ async function createFxAgent(options = {}) {
2348
2659
  get cancelled() {
2349
2660
  return cancelled;
2350
2661
  },
2351
- failPendingBlob(error) {
2352
- rejectBlobRead?.(error);
2353
- rejectBlobRead = null;
2354
- cancelBlobRead = null;
2662
+ failImagePrep(error) {
2663
+ rejectImagePrep?.(error);
2664
+ rejectImagePrep = null;
2665
+ cancelImagePrep = null;
2355
2666
  },
2356
2667
  promptWritten() {
2357
2668
  resolvePromptStart?.(true);
@@ -2404,9 +2715,9 @@ async function createFxAgent(options = {}) {
2404
2715
  if (finished || cancelled)
2405
2716
  return;
2406
2717
  cancelled = true;
2407
- cancelBlobRead?.();
2408
- cancelBlobRead = null;
2409
- rejectBlobRead = null;
2718
+ cancelImagePrep?.();
2719
+ cancelImagePrep = null;
2720
+ rejectImagePrep = null;
2410
2721
  resolvePromptStart?.(false);
2411
2722
  resolvePromptStart = null;
2412
2723
  runtime.closeSteering?.();
@@ -2455,31 +2766,37 @@ async function createFxAgent(options = {}) {
2455
2766
  runtime.openSteering?.();
2456
2767
  const abort = () => turn.cancel();
2457
2768
  signal?.addEventListener("abort", abort, { once: true });
2458
- const blobReadCancelled = hasBlobs ? new Promise((resolve, reject) => {
2459
- cancelBlobRead = () => resolve(null);
2460
- rejectBlobRead = reject;
2769
+ const imagePrepCancelled = asyncImages ? new Promise((resolve, reject) => {
2770
+ cancelImagePrep = () => resolve(null);
2771
+ rejectImagePrep = reject;
2461
2772
  }) : null;
2462
- const response = hasBlobs ? Promise.race([
2463
- Promise.resolve().then(() => materializePromptBlobs(prompt, () => cancelled || closing)),
2464
- blobReadCancelled
2465
- ]).then((encodedPrompt) => {
2466
- cancelBlobRead = null;
2467
- rejectBlobRead = null;
2468
- if (coreExitError && !cancelled)
2469
- throw coreExitError;
2470
- if (encodedPrompt === null || cancelled || closing) {
2471
- resolvePromptStart?.(false);
2472
- resolvePromptStart = null;
2473
- return { stopReason: "cancelled" };
2474
- }
2475
- if (promptFrameSize(encodedPrompt) > maxPromptFrameBytes) {
2476
- throw new RangeError(`prompt exceeds the ${maxPromptFrameBytes} byte libfx frame limit`);
2773
+ let response;
2774
+ if (asyncImages) {
2775
+ response = Promise.race([
2776
+ Promise.resolve().then(() => materializePromptImages(prompt, () => cancelled || closing, resizeImage)),
2777
+ imagePrepCancelled
2778
+ ]).then((prepared) => {
2779
+ cancelImagePrep = null;
2780
+ rejectImagePrep = null;
2781
+ if (coreExitError && !cancelled)
2782
+ throw coreExitError;
2783
+ if (prepared === null || cancelled || closing) {
2784
+ resolvePromptStart?.(false);
2785
+ resolvePromptStart = null;
2786
+ return { stopReason: "cancelled" };
2787
+ }
2788
+ return sendPrompt(prepared);
2789
+ });
2790
+ } else {
2791
+ try {
2792
+ response = sendPrompt(prompt);
2793
+ } catch (error) {
2794
+ response = Promise.reject(error);
2477
2795
  }
2478
- return request("session/prompt", { sessionId, prompt: encodedPrompt });
2479
- }) : request("session/prompt", { sessionId, prompt });
2796
+ }
2480
2797
  turn.result = response.then((value) => ({ stopReason: cancelled ? "cancelled" : value.stopReason, usage: value.usage })).catch((error) => {
2481
- cancelBlobRead = null;
2482
- rejectBlobRead = null;
2798
+ cancelImagePrep = null;
2799
+ rejectImagePrep = null;
2483
2800
  resolvePromptStart?.(error);
2484
2801
  resolvePromptStart = null;
2485
2802
  if (cancelled && error.message === "Cancelled")
@@ -2513,7 +2830,7 @@ async function createFxAgent(options = {}) {
2513
2830
 
2514
2831
  // sdk/node.js
2515
2832
  var libfxApiVersion = 2;
2516
- var nativeCoreApiVersion = 3;
2833
+ var nativeCoreApiVersion = 4;
2517
2834
  var fetchOperationStale = 0;
2518
2835
  var fetchOperationApplied = 1;
2519
2836
  var fetchOperationBackpressure = 2;
@@ -2804,7 +3121,12 @@ async function getBackendInfo(value = {}) {
2804
3121
  }
2805
3122
  }
2806
3123
  function createNativeCoreRuntime(addon, options) {
2807
- const { apiKey, model, effort, fast, gatewayChatUrl } = options;
3124
+ const { apiKey, model, effort, fast, ultrafast, gatewayChatUrl } = options;
3125
+ if (ultrafast !== undefined && addon.supportsUltrafast !== true) {
3126
+ const error = new Error("native addon does not support the ultrafast option");
3127
+ error.code = "LIBFX_NATIVE_CAPABILITY_UNAVAILABLE";
3128
+ throw error;
3129
+ }
2808
3130
  const core = addon.createCore({
2809
3131
  apiKey,
2810
3132
  home: options.home ?? import_node_os.homedir(),
@@ -2812,6 +3134,7 @@ function createNativeCoreRuntime(addon, options) {
2812
3134
  ...model === undefined ? {} : { model },
2813
3135
  ...effort === undefined ? {} : { effort },
2814
3136
  ...fast === undefined ? {} : { fast },
3137
+ ...ultrafast === undefined ? {} : { ultrafast },
2815
3138
  ...gatewayChatUrl === undefined ? {} : { gatewayChatUrl }
2816
3139
  });
2817
3140
  let readyFd;
@@ -2839,73 +3162,195 @@ function createNativeCoreRuntime(addon, options) {
2839
3162
  const exited = new Promise((resolve) => {
2840
3163
  exitedResolve = resolve;
2841
3164
  });
3165
+ function refreshFetch(state) {
3166
+ if (settled)
3167
+ return;
3168
+ if (typeof addon.coreFetchDisposition !== "function") {
3169
+ state.active = addon.coreFetchActive(core, state.handle);
3170
+ return;
3171
+ }
3172
+ const disposition = addon.coreFetchDisposition(core, state.handle);
3173
+ if (disposition !== 0 && disposition !== 1 && disposition !== 2) {
3174
+ throw new Error(`invalid native fetch disposition ${disposition}`);
3175
+ }
3176
+ state.consumed ||= disposition === 2;
3177
+ state.active = disposition === 1;
3178
+ }
3179
+ function cleanupAction(state) {
3180
+ return fetchCleanupAction({
3181
+ ...state,
3182
+ alive: !settled,
3183
+ age: state.drainStarted === null ? 0 : performance.now() - state.drainStarted
3184
+ });
3185
+ }
3186
+ function beginDrain(state) {
3187
+ if (state.drainStarted !== null)
3188
+ return;
3189
+ state.drainStarted = performance.now();
3190
+ state.timer = setTimeout(() => state.controller.abort(), cleanupTimeoutMs);
3191
+ }
2842
3192
  const abortHostEffects = () => {
2843
- fetchState?.controller.abort();
2844
- try {
2845
- addon.abortCoreFetch(core);
2846
- } catch {}
3193
+ if (fetchState) {
3194
+ fetchState.canceled = true;
3195
+ fetchState.controller.abort();
3196
+ }
3197
+ if (!settled) {
3198
+ try {
3199
+ addon.abortCoreFetch(core);
3200
+ } catch {}
3201
+ }
2847
3202
  };
2848
3203
  const finish = (code, error) => {
2849
3204
  if (settled)
2850
3205
  return;
3206
+ const state = fetchState;
3207
+ if (state) {
3208
+ refreshFetch(state);
3209
+ state.closing = true;
3210
+ state.failed ||= code !== 0 || error !== undefined;
3211
+ }
2851
3212
  settled = true;
2852
3213
  outputError = error;
2853
3214
  output.close();
2854
- abortHostEffects();
2855
- try {
2856
- addon.destroyCore(core);
2857
- } catch {}
2858
- readySocket.destroy();
2859
- readyClosed.then(() => exitedResolve(code));
3215
+ const destroy = () => {
3216
+ try {
3217
+ addon.destroyCore(core);
3218
+ } catch {}
3219
+ readySocket.destroy();
3220
+ readyClosed.then(() => exitedResolve(code));
3221
+ };
3222
+ if (state && cleanupAction(state) === "drain") {
3223
+ beginDrain(state);
3224
+ state.done.then(destroy);
3225
+ } else {
3226
+ if (state && cleanupAction(state) !== "done")
3227
+ state.controller.abort();
3228
+ destroy();
3229
+ }
2860
3230
  };
2861
- const pumpFetch = async (request) => {
3231
+ const pumpFetch = async (request, body) => {
2862
3232
  const controller = new AbortController;
2863
- const state = { handle: request.handle, controller };
3233
+ let complete, reader;
3234
+ const state = {
3235
+ handle: request.handle,
3236
+ controller,
3237
+ consumed: false,
3238
+ active: true,
3239
+ canceled: false,
3240
+ failed: false,
3241
+ closing: false,
3242
+ eof: false,
3243
+ discarded: 0,
3244
+ drainStarted: null,
3245
+ timer: null,
3246
+ done: new Promise((resolve) => {
3247
+ complete = resolve;
3248
+ })
3249
+ };
2864
3250
  fetchState = state;
3251
+ let onAbort;
3252
+ const aborted = new Promise((_, reject) => {
3253
+ onAbort = () => reject(new DOMException("Aborted", "AbortError"));
3254
+ controller.signal.addEventListener("abort", onAbort, { once: true });
3255
+ });
3256
+ aborted.catch(() => {});
3257
+ const wait = (task) => Promise.race([task, aborted]);
2865
3258
  try {
2866
- const response = await (options.fetch ?? globalThis.fetch)(request.url, {
3259
+ const responseTask = Promise.resolve().then(() => (options.fetch ?? globalThis.fetch)(request.url, {
2867
3260
  method: request.method,
2868
3261
  headers: new Headers(JSON.parse(request.headers).map(({ name, value }) => [name, value])),
2869
- body: request.body?.length ? Buffer.from(request.body, "base64") : undefined,
3262
+ body: body.length ? body : undefined,
2870
3263
  signal: controller.signal
3264
+ })).then((response) => {
3265
+ if (controller.signal.aborted) {
3266
+ response.body?.cancel().catch(() => {});
3267
+ throw new DOMException("Aborted", "AbortError");
3268
+ }
3269
+ return response;
2871
3270
  });
2872
- const started = addon.startCoreFetchResponse(core, state.handle, response.status);
2873
- if (started === fetchOperationStale)
2874
- return;
2875
- if (started !== fetchOperationApplied)
3271
+ const response = await wait(responseTask);
3272
+ if (settled && !state.consumed)
3273
+ throw new DOMException("Aborted", "AbortError");
3274
+ const started = settled ? fetchOperationStale : addon.startCoreFetchResponse(core, state.handle, response.status);
3275
+ if (started !== fetchOperationApplied && started !== fetchOperationStale)
2876
3276
  throw new Error(`invalid native fetch start result ${started}`);
2877
- if (response.body) {
2878
- for await (const chunk of response.body) {
2879
- const buffer = Buffer.from(chunk);
2880
- options.onTransportChunk?.(buffer.length);
2881
- let offset = 0;
2882
- while (offset < buffer.length) {
2883
- const end = Math.min(offset + 65536, buffer.length);
2884
- const pushed = addon.pushCoreFetchResponse(core, state.handle, buffer.subarray(offset, end));
2885
- if (pushed === fetchOperationApplied) {
2886
- offset = end;
3277
+ state.active = started === fetchOperationApplied;
3278
+ reader = response.body?.getReader();
3279
+ while (reader) {
3280
+ refreshFetch(state);
3281
+ const action = cleanupAction(state);
3282
+ if (action === "abort") {
3283
+ controller.abort();
3284
+ throw new DOMException("Aborted", "AbortError");
3285
+ }
3286
+ if (action === "drain")
3287
+ beginDrain(state);
3288
+ const { done, value } = await wait(reader.read());
3289
+ if (done)
3290
+ break;
3291
+ const buffer = Buffer.from(value);
3292
+ options.onTransportChunk?.(buffer.length);
3293
+ let offset = 0;
3294
+ while (offset < buffer.length) {
3295
+ refreshFetch(state);
3296
+ const next = cleanupAction(state);
3297
+ if (next === "drain") {
3298
+ beginDrain(state);
3299
+ state.discarded += buffer.length - offset;
3300
+ if (state.discarded >= cleanupByteLimit)
3301
+ controller.abort();
3302
+ break;
3303
+ }
3304
+ if (next !== "forward") {
3305
+ controller.abort();
3306
+ throw new DOMException("Aborted", "AbortError");
3307
+ }
3308
+ const end = Math.min(offset + 65536, buffer.length);
3309
+ const pushed = addon.pushCoreFetchResponse(core, state.handle, buffer.subarray(offset, end));
3310
+ if (pushed === fetchOperationApplied) {
3311
+ offset = end;
3312
+ continue;
3313
+ }
3314
+ if (pushed === fetchOperationStale) {
3315
+ refreshFetch(state);
3316
+ if (cleanupAction(state) === "drain")
2887
3317
  continue;
2888
- }
2889
- if (pushed === fetchOperationStale)
2890
- return;
2891
- if (pushed !== fetchOperationBackpressure)
2892
- throw new Error(`invalid native fetch push result ${pushed}`);
2893
- await new Promise((resolve) => setTimeout(resolve, 2));
3318
+ controller.abort();
3319
+ throw new DOMException("Aborted", "AbortError");
2894
3320
  }
3321
+ if (pushed !== fetchOperationBackpressure)
3322
+ throw new Error(`invalid native fetch push result ${pushed}`);
3323
+ await wait(new Promise((resolve) => setTimeout(resolve, 2)));
2895
3324
  }
2896
3325
  }
2897
- const finished = addon.finishCoreFetch(core, state.handle);
2898
- if (finished !== fetchOperationApplied && finished !== fetchOperationStale) {
2899
- throw new Error(`invalid native fetch finish result ${finished}`);
3326
+ state.eof = true;
3327
+ if (!settled && state.active) {
3328
+ const finished = addon.finishCoreFetch(core, state.handle);
3329
+ if (finished !== fetchOperationApplied && finished !== fetchOperationStale)
3330
+ throw new Error(`invalid native fetch finish result ${finished}`);
2900
3331
  }
2901
3332
  } catch (error) {
2902
- if (error?.name !== "AbortError" || !controller.signal.aborted) {
3333
+ if (!settled && !controller.signal.aborted) {
3334
+ state.failed = true;
2903
3335
  try {
2904
3336
  if (addon.coreFetchActive(core, state.handle))
2905
3337
  addon.failCoreFetch(core, state.handle);
2906
3338
  } catch {}
2907
3339
  }
2908
3340
  } finally {
3341
+ clearTimeout(state.timer);
3342
+ controller.signal.removeEventListener("abort", onAbort);
3343
+ if (reader) {
3344
+ if (!state.eof) {
3345
+ try {
3346
+ reader.cancel().catch(() => {});
3347
+ } catch {}
3348
+ }
3349
+ try {
3350
+ reader.releaseLock();
3351
+ } catch {}
3352
+ }
3353
+ complete();
2909
3354
  if (fetchState === state) {
2910
3355
  fetchState = null;
2911
3356
  queueMicrotask(drainReady);
@@ -2917,13 +3362,16 @@ function createNativeCoreRuntime(addon, options) {
2917
3362
  return;
2918
3363
  try {
2919
3364
  if (fetchState) {
2920
- if (!fetchState.controller.signal.aborted && !addon.coreFetchActive(core, fetchState.handle)) {
3365
+ refreshFetch(fetchState);
3366
+ const action = cleanupAction(fetchState);
3367
+ if (action === "drain")
3368
+ beginDrain(fetchState);
3369
+ if (action === "abort")
2921
3370
  fetchState.controller.abort();
2922
- }
2923
3371
  } else {
2924
3372
  const fetchRequest = addon.takeCoreFetch(core);
2925
3373
  if (fetchRequest)
2926
- pumpFetch(JSON.parse(fetchRequest.toString("utf8")));
3374
+ pumpFetch(JSON.parse(fetchRequest.request.toString("utf8")), fetchRequest.body);
2927
3375
  }
2928
3376
  if (addon.coreExitCode(core) !== 0) {
2929
3377
  finish(1, new Error("native output delivery failed"));
@@ -2984,6 +3432,15 @@ function createNativeCoreRuntime(addon, options) {
2984
3432
  write(data) {
2985
3433
  addon.writeCore(core, Buffer.from(data));
2986
3434
  },
3435
+ writeAttachment(id, bytes) {
3436
+ addon.writeCoreAttachment(core, id, Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
3437
+ },
3438
+ takeAttachment(id) {
3439
+ return addon.takeCoreAttachment(core, id);
3440
+ },
3441
+ discardAttachments() {
3442
+ addon.discardCoreAttachments(core);
3443
+ },
2987
3444
  closeStdin() {
2988
3445
  addon.closeCore(core);
2989
3446
  },
@@ -3028,7 +3485,7 @@ async function createWithFallback(surface, nativeMethod, wasmFactory, defaultWas
3028
3485
  return await native.backend[nativeMethod](runtimeOptions);
3029
3486
  } catch (error) {
3030
3487
  nativeError = error;
3031
- if (backend === "native" || error?.code === "LIBFX_MODEL_UNSUPPORTED_FAST" || error?.code === "LIBFX_MODEL_UNSUPPORTED_EFFORT")
3488
+ if (backend === "native" || error?.code === "LIBFX_MODEL_UNSUPPORTED_FAST" || error?.code === "LIBFX_MODEL_UNSUPPORTED_EFFORT" || typeof error?.code === "string" && error.code.startsWith("LIBFX_MODEL_METADATA_"))
3032
3489
  throw error;
3033
3490
  }
3034
3491
  }