@reinsjs/lowering-fetch 0.6.8 → 0.7.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @reinsjs/lowering-fetch
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c47e62b: An image reference that reaches the lowering unhydrated (its blob was deleted, or no `BlobStore` is configured) is replaced with `[image unavailable]` and the event is recorded `lossy`, instead of sending a broken image. When the model takes no images the usual "this model does not accept images" placeholder is used, as for inline images. Loss matrices mention the new case.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [c47e62b]
12
+ - Updated dependencies [12d9343]
13
+ - @reinsjs/core@0.16.0
14
+
3
15
  ## 0.6.8
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -309,6 +309,8 @@ function omittedNote(kinds) {
309
309
  const names = mediaNouns(kinds);
310
310
  return names ? `the model takes no ${names}, so they are replaced with placeholder text` : void 0;
311
311
  }
312
+ var IMAGE_UNAVAILABLE = "[image unavailable]";
313
+ var IMAGE_UNAVAILABLE_NOTE = "an image stored by reference could not be loaded (blob unavailable: it was removed, or no BlobStore is configured), so it is replaced with placeholder text";
312
314
  var RELAY_DIALECT_NOTE = "relay dialect: video / audio travel in the image slot; official endpoints reject this, so declare video / audio only for endpoints that take it";
313
315
 
314
316
  // src/chat/loss-matrix.ts
@@ -378,8 +380,8 @@ var CHAT_LOSS_MATRIX = {
378
380
  ),
379
381
  lossy(
380
382
  "user",
381
- "moved after that batch of tool results (the order changes), untrusted content escaped, or images / video / audio replaced with placeholder text because the model takes none of that kind",
382
- "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take"
383
+ "moved after that batch of tool results (the order changes), untrusted content escaped, images / video / audio replaced with placeholder text because the model takes none of that kind, or an image reference whose blob is unavailable replaced with placeholder text",
384
+ "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take / it carries an image reference whose blob is unavailable"
383
385
  )
384
386
  ],
385
387
  "core.model_text": [
@@ -414,13 +416,13 @@ var CHAT_LOSS_MATRIX = {
414
416
  ),
415
417
  lossy(
416
418
  "tool+lifted-images",
417
- `a tool message takes text only, so it keeps a pointer where each image (or video / audio) was and they move into a user message right after the whole batch of tool messages (the order and the role change); that message labels them as tool output, with the tool name, call id and index, and keeps the result's untrusted marking; kinds the model does not take are replaced with placeholder text; ${RELAY_DIALECT_NOTE}`,
419
+ `a tool message takes text only, so it keeps a pointer where each image (or video / audio) was and they move into a user message right after the whole batch of tool messages (the order and the role change); that message labels them as tool output, with the tool name, call id and index, and keeps the result's untrusted marking; kinds the model does not take, and image references whose blob is unavailable, are replaced with placeholder text; ${RELAY_DIALECT_NOTE}`,
418
420
  "the result carries images / video / audio of a kind the model takes (chat.liftToolImages is on by default)"
419
421
  ),
420
422
  lossy(
421
423
  "tool-text-only",
422
- "a tool message takes text only, so images / video / audio are replaced with placeholder text",
423
- "the result carries images / video / audio and the model takes none of those kinds, or chat.liftToolImages is false"
424
+ "a tool message takes text only, so images / video / audio are replaced with placeholder text (an image reference whose blob is unavailable has no bytes to lift either)",
425
+ "the result carries images / video / audio and the model takes none of those kinds, or chat.liftToolImages is false, or it carries an image reference whose blob is unavailable"
424
426
  )
425
427
  ],
426
428
  "core.system_note": [
@@ -466,8 +468,8 @@ var ANTHROPIC_LOSS_MATRIX = {
466
468
  ),
467
469
  lossy2(
468
470
  "user",
469
- "moved after that batch of tool results (the order changes), untrusted content escaped, or images / video / audio replaced with placeholder text because the model takes none of that kind",
470
- "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take"
471
+ "moved after that batch of tool results (the order changes), untrusted content escaped, images / video / audio replaced with placeholder text because the model takes none of that kind, or an image reference whose blob is unavailable replaced with placeholder text",
472
+ "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take / it carries an image reference whose blob is unavailable"
471
473
  ),
472
474
  dropped2(
473
475
  "none",
@@ -513,7 +515,7 @@ var ANTHROPIC_LOSS_MATRIX = {
513
515
  ),
514
516
  lossy2(
515
517
  "tool_result",
516
- "untrusted content is escaped, or images / video / audio are replaced with placeholder text because the model takes none of that kind, or the referenced tool is absent from this request's tool table and its definition is expanded into text"
518
+ "untrusted content is escaped, or images / video / audio are replaced with placeholder text because the model takes none of that kind (or an image reference's blob is unavailable), or the referenced tool is absent from this request's tool table and its definition is expanded into text"
517
519
  ),
518
520
  exact2(
519
521
  "tool-reference",
@@ -554,6 +556,7 @@ var ANTHROPIC_LOSS_MATRIX = {
554
556
 
555
557
  // src/anthropic/to-request.ts
556
558
  import {
559
+ isImageRef,
557
560
  renderToolReference
558
561
  } from "@reinsjs/core";
559
562
 
@@ -758,6 +761,7 @@ function isPlainObject(v) {
758
761
  function blocksOf(parts, capabilities) {
759
762
  const omitted = /* @__PURE__ */ new Set();
760
763
  let relayed = false;
764
+ let unavailable = false;
761
765
  const blocks = [];
762
766
  for (const p of parts) {
763
767
  switch (p.type) {
@@ -771,6 +775,11 @@ function blocksOf(parts, capabilities) {
771
775
  case "video":
772
776
  case "audio":
773
777
  if (acceptsMedia(p.type, capabilities)) {
778
+ if (isImageRef(p)) {
779
+ unavailable = true;
780
+ blocks.push({ type: "text", text: IMAGE_UNAVAILABLE });
781
+ break;
782
+ }
774
783
  if (p.type !== "image") relayed = true;
775
784
  blocks.push({ type: "image", source: { type: "base64", media_type: p.mime, data: p.data } });
776
785
  } else {
@@ -784,7 +793,7 @@ function blocksOf(parts, capabilities) {
784
793
  }
785
794
  }
786
795
  }
787
- return { blocks, omitted, relayed };
796
+ return { blocks, omitted, relayed, unavailable };
788
797
  }
789
798
  function encodeAnthropicRequest(input) {
790
799
  const { model, capabilities } = input;
@@ -859,7 +868,7 @@ function encodeAnthropicRequest(input) {
859
868
  break;
860
869
  }
861
870
  openUser().push(...c.blocks);
862
- const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
871
+ const lossy4 = item.deferred || item.escaped || c.omitted.size > 0 || c.unavailable;
863
872
  land(
864
873
  landings,
865
874
  item.event,
@@ -868,6 +877,7 @@ function encodeAnthropicRequest(input) {
868
877
  item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
869
878
  item.escaped ? ESCAPED_NOTE : void 0,
870
879
  omittedNote(c.omitted),
880
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
871
881
  c.relayed ? RELAY_DIALECT_NOTE : void 0
872
882
  );
873
883
  break;
@@ -891,10 +901,11 @@ function encodeAnthropicRequest(input) {
891
901
  land(
892
902
  landings,
893
903
  item.event,
894
- aside || item.escaped || rest.omitted.size > 0 ? "lossy" : "exact",
904
+ aside || item.escaped || rest.omitted.size > 0 || rest.unavailable ? "lossy" : "exact",
895
905
  "tool-reference",
896
906
  aside ? REF_TEXT_ASIDE_NOTE : void 0,
897
907
  omittedNote(rest.omitted),
908
+ rest.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
898
909
  rest.relayed ? RELAY_DIALECT_NOTE : void 0,
899
910
  item.escaped ? ESCAPED_NOTE : void 0
900
911
  );
@@ -905,7 +916,7 @@ function encodeAnthropicRequest(input) {
905
916
  openUser().push(block);
906
917
  const unbound = refs.length > 0 && capabilities.deferredTools && trusted;
907
918
  const untrustedRefs = refs.length > 0 && capabilities.deferredTools && !trusted;
908
- const lossy4 = item.escaped || c.omitted.size > 0 || unbound;
919
+ const lossy4 = item.escaped || c.omitted.size > 0 || c.unavailable || unbound;
909
920
  land(
910
921
  landings,
911
922
  item.event,
@@ -914,6 +925,7 @@ function encodeAnthropicRequest(input) {
914
925
  unbound ? REF_UNBOUND_NOTE : void 0,
915
926
  untrustedRefs ? REF_UNTRUSTED_NOTE : void 0,
916
927
  omittedNote(c.omitted),
928
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
917
929
  c.relayed ? RELAY_DIALECT_NOTE : void 0,
918
930
  item.escaped ? ESCAPED_NOTE : void 0
919
931
  );
@@ -1324,6 +1336,7 @@ function errorText2(err) {
1324
1336
 
1325
1337
  // src/chat/to-request.ts
1326
1338
  import {
1339
+ isImageRef as isImageRef2,
1327
1340
  markUntrusted as markUntrusted2,
1328
1341
  renderToolReference as renderToolReference2
1329
1342
  } from "@reinsjs/core";
@@ -1340,6 +1353,7 @@ function land2(out, e, kind, landing, ...notes) {
1340
1353
  function userContent(parts, capabilities) {
1341
1354
  const omitted = /* @__PURE__ */ new Set();
1342
1355
  let relayed = false;
1356
+ let unavailable = false;
1343
1357
  const out = parts.map((p) => {
1344
1358
  switch (p.type) {
1345
1359
  case "text":
@@ -1353,6 +1367,10 @@ function userContent(parts, capabilities) {
1353
1367
  omitted.add(p.type);
1354
1368
  return { type: "text", text: mediaOmitted(p) };
1355
1369
  }
1370
+ if (isImageRef2(p)) {
1371
+ unavailable = true;
1372
+ return { type: "text", text: IMAGE_UNAVAILABLE };
1373
+ }
1356
1374
  if (p.type !== "image") relayed = true;
1357
1375
  return { type: "image_url", image_url: { url: `data:${p.mime};base64,${p.data}` } };
1358
1376
  default: {
@@ -1361,14 +1379,16 @@ function userContent(parts, capabilities) {
1361
1379
  }
1362
1380
  }
1363
1381
  });
1364
- if (out.length === 1 && out[0]?.type === "text") return { content: out[0].text, omitted, relayed };
1365
- return { content: out, omitted, relayed };
1382
+ if (out.length === 1 && out[0]?.type === "text")
1383
+ return { content: out[0].text, omitted, relayed, unavailable };
1384
+ return { content: out, omitted, relayed, unavailable };
1366
1385
  }
1367
1386
  function toolContent(parts, capabilities, lift) {
1368
1387
  const unsupported = /* @__PURE__ */ new Set();
1369
1388
  const textOnly = /* @__PURE__ */ new Set();
1370
1389
  const media = [];
1371
1390
  let relayed = false;
1391
+ let unavailable = false;
1372
1392
  const text = parts.map((p) => {
1373
1393
  switch (p.type) {
1374
1394
  case "text":
@@ -1379,6 +1399,10 @@ function toolContent(parts, capabilities, lift) {
1379
1399
  case "video":
1380
1400
  case "audio": {
1381
1401
  const accepted = acceptsMedia(p.type, capabilities);
1402
+ if (accepted && isImageRef2(p)) {
1403
+ unavailable = true;
1404
+ return IMAGE_UNAVAILABLE;
1405
+ }
1382
1406
  if (accepted && lift) {
1383
1407
  media.push(p);
1384
1408
  if (p.type !== "image") relayed = true;
@@ -1397,7 +1421,7 @@ function toolContent(parts, capabilities, lift) {
1397
1421
  }
1398
1422
  }
1399
1423
  }).join("\n");
1400
- return { text, media, unsupported, textOnly, relayed };
1424
+ return { text, media, unsupported, textOnly, relayed, unavailable };
1401
1425
  }
1402
1426
  function toolOmittedNote(kinds) {
1403
1427
  const names = mediaNouns(kinds);
@@ -1438,7 +1462,7 @@ function encodeChatRequest(input) {
1438
1462
  case "user": {
1439
1463
  const c = userContent(item.parts, capabilities);
1440
1464
  messages.push({ role: "user", content: c.content });
1441
- const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
1465
+ const lossy4 = item.deferred || item.escaped || c.omitted.size > 0 || c.unavailable;
1442
1466
  land2(
1443
1467
  landings,
1444
1468
  item.event,
@@ -1447,6 +1471,7 @@ function encodeChatRequest(input) {
1447
1471
  item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
1448
1472
  item.escaped ? ESCAPED_NOTE : void 0,
1449
1473
  omittedNote(c.omitted),
1474
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
1450
1475
  c.relayed ? RELAY_DIALECT_NOTE : void 0
1451
1476
  );
1452
1477
  break;
@@ -1467,7 +1492,7 @@ function encodeChatRequest(input) {
1467
1492
  });
1468
1493
  const mediaLifted = c.media.length > 0;
1469
1494
  if (mediaLifted) lifted.push(...liftedAttachment(item, c.media));
1470
- const dropped4 = c.unsupported.size > 0 || c.textOnly.size > 0;
1495
+ const dropped4 = c.unsupported.size > 0 || c.textOnly.size > 0 || c.unavailable;
1471
1496
  const lossy4 = item.escaped || item.isError || dropped4 || mediaLifted;
1472
1497
  land2(
1473
1498
  landings,
@@ -1478,6 +1503,7 @@ function encodeChatRequest(input) {
1478
1503
  item.isError ? "a Chat tool message has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
1479
1504
  toolOmittedNote(c.textOnly),
1480
1505
  omittedNote(c.unsupported),
1506
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
1481
1507
  mediaLifted ? c.media.every((m) => m.type === "image") ? "a tool message takes text only, so its images move into a user message after this batch of tool messages, labelled as tool output" : "a tool message takes text only, so its media move into a user message after this batch of tool messages, labelled as tool output" : void 0,
1482
1508
  c.relayed ? RELAY_DIALECT_NOTE : void 0,
1483
1509
  item.escaped ? ESCAPED_NOTE : void 0
@@ -2151,6 +2177,7 @@ function errorText3(err) {
2151
2177
 
2152
2178
  // src/responses/to-request.ts
2153
2179
  import {
2180
+ isImageRef as isImageRef3,
2154
2181
  renderToolReference as renderToolReference3
2155
2182
  } from "@reinsjs/core";
2156
2183
  var ERROR_PREFIX2 = "[tool error]\n";
@@ -2165,6 +2192,7 @@ function land3(out, e, kind, landing, ...notes) {
2165
2192
  function contentOf(parts, capabilities) {
2166
2193
  const omitted = /* @__PURE__ */ new Set();
2167
2194
  let relayed = false;
2195
+ let unavailable = false;
2168
2196
  const content = [];
2169
2197
  for (const p of parts) {
2170
2198
  switch (p.type) {
@@ -2178,6 +2206,11 @@ function contentOf(parts, capabilities) {
2178
2206
  case "video":
2179
2207
  case "audio":
2180
2208
  if (acceptsMedia(p.type, capabilities)) {
2209
+ if (isImageRef3(p)) {
2210
+ unavailable = true;
2211
+ content.push({ type: "input_text", text: IMAGE_UNAVAILABLE });
2212
+ break;
2213
+ }
2181
2214
  if (p.type !== "image") relayed = true;
2182
2215
  content.push({ type: "input_image", image_url: `data:${p.mime};base64,${p.data}`, detail: "auto" });
2183
2216
  } else {
@@ -2191,7 +2224,7 @@ function contentOf(parts, capabilities) {
2191
2224
  }
2192
2225
  }
2193
2226
  }
2194
- return { content, omitted, relayed };
2227
+ return { content, omitted, relayed, unavailable };
2195
2228
  }
2196
2229
  function argumentsOf2(args) {
2197
2230
  return typeof args === "string" ? args : JSON.stringify(args ?? {});
@@ -2241,7 +2274,7 @@ function encodeResponsesRequest(input) {
2241
2274
  break;
2242
2275
  }
2243
2276
  items.push({ role: "user", content: c.content });
2244
- const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
2277
+ const lossy4 = item.deferred || item.escaped || c.omitted.size > 0 || c.unavailable;
2245
2278
  land3(
2246
2279
  landings,
2247
2280
  item.event,
@@ -2250,6 +2283,7 @@ function encodeResponsesRequest(input) {
2250
2283
  item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
2251
2284
  item.escaped ? ESCAPED_NOTE : void 0,
2252
2285
  omittedNote(c.omitted),
2286
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
2253
2287
  c.relayed ? RELAY_DIALECT_NOTE : void 0
2254
2288
  );
2255
2289
  break;
@@ -2264,7 +2298,7 @@ function encodeResponsesRequest(input) {
2264
2298
  ...c.content.filter((b) => b.type === "input_image")
2265
2299
  ] : prefixed;
2266
2300
  items.push({ type: "function_call_output", call_id: item.toolCallId, output });
2267
- const lossy4 = item.escaped || item.isError || c.omitted.size > 0;
2301
+ const lossy4 = item.escaped || item.isError || c.omitted.size > 0 || c.unavailable;
2268
2302
  land3(
2269
2303
  landings,
2270
2304
  item.event,
@@ -2272,6 +2306,7 @@ function encodeResponsesRequest(input) {
2272
2306
  "function_call_output",
2273
2307
  item.isError ? "the Responses function_call_output has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
2274
2308
  omittedNote(c.omitted),
2309
+ c.unavailable ? IMAGE_UNAVAILABLE_NOTE : void 0,
2275
2310
  c.relayed ? RELAY_DIALECT_NOTE : void 0,
2276
2311
  item.escaped ? ESCAPED_NOTE : void 0
2277
2312
  );
@@ -2744,8 +2779,8 @@ var RESPONSES_LOSS_MATRIX = {
2744
2779
  ),
2745
2780
  lossy3(
2746
2781
  "user",
2747
- "moved after that batch of tool results (the order changes), untrusted content escaped, or images / video / audio replaced with placeholder text because the model takes none of that kind",
2748
- "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take"
2782
+ "moved after that batch of tool results (the order changes), untrusted content escaped, images / video / audio replaced with placeholder text because the model takes none of that kind, or an image reference whose blob is unavailable replaced with placeholder text",
2783
+ "the user speaks up before all tool results are in / the content holds a </untrusted / it carries images / video / audio the model does not take / it carries an image reference whose blob is unavailable"
2749
2784
  ),
2750
2785
  dropped3(
2751
2786
  "none",
@@ -2784,7 +2819,7 @@ var RESPONSES_LOSS_MATRIX = {
2784
2819
  ),
2785
2820
  lossy3(
2786
2821
  "function_call_output",
2787
- "isError is expressed with a [tool error] prefix (the protocol has no error flag), or untrusted content is escaped, or images / video / audio are replaced with placeholder text because the model takes none of that kind"
2822
+ "isError is expressed with a [tool error] prefix (the protocol has no error flag), or untrusted content is escaped, or images / video / audio are replaced with placeholder text because the model takes none of that kind (or an image reference's blob is unavailable)"
2788
2823
  )
2789
2824
  ],
2790
2825
  "core.system_note": [