@reinsjs/lowering-fetch 0.5.2 → 0.6.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 +26 -0
- package/README.md +9 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +228 -99
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -273,6 +273,44 @@ function errorText(err) {
|
|
|
273
273
|
return String(err);
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
+
// src/media.ts
|
|
277
|
+
function acceptsMedia(kind, capabilities) {
|
|
278
|
+
switch (kind) {
|
|
279
|
+
case "image":
|
|
280
|
+
return capabilities.images;
|
|
281
|
+
case "video":
|
|
282
|
+
return capabilities.video === true;
|
|
283
|
+
case "audio":
|
|
284
|
+
return capabilities.audio === true;
|
|
285
|
+
default: {
|
|
286
|
+
const _exhaustive = kind;
|
|
287
|
+
return _exhaustive;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function durationText(sec) {
|
|
292
|
+
return `${Number.isInteger(sec) ? sec : sec.toFixed(1)} s`;
|
|
293
|
+
}
|
|
294
|
+
function mediaOmitted(p) {
|
|
295
|
+
if (p.type === "image") return "[image omitted: this model does not accept images]";
|
|
296
|
+
const detail = p.durationSec !== void 0 ? `${p.mime}, ${durationText(p.durationSec)}` : p.mime;
|
|
297
|
+
return `[${p.type} omitted: this model does not accept ${p.type} (${detail})]`;
|
|
298
|
+
}
|
|
299
|
+
var KIND_NOUN = {
|
|
300
|
+
image: "images",
|
|
301
|
+
video: "video clips",
|
|
302
|
+
audio: "audio clips"
|
|
303
|
+
};
|
|
304
|
+
function mediaNouns(kinds) {
|
|
305
|
+
if (kinds.size === 0) return void 0;
|
|
306
|
+
return ["image", "video", "audio"].filter((k) => kinds.has(k)).map((k) => KIND_NOUN[k]).join(" / ");
|
|
307
|
+
}
|
|
308
|
+
function omittedNote(kinds) {
|
|
309
|
+
const names = mediaNouns(kinds);
|
|
310
|
+
return names ? `the model takes no ${names}, so they are replaced with placeholder text` : void 0;
|
|
311
|
+
}
|
|
312
|
+
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
|
+
|
|
276
314
|
// src/chat/loss-matrix.ts
|
|
277
315
|
var exact = (landing, note, when) => {
|
|
278
316
|
const spec = { kind: "exact", landing };
|
|
@@ -323,11 +361,14 @@ var NOT_SENT = {
|
|
|
323
361
|
};
|
|
324
362
|
var CHAT_LOSS_MATRIX = {
|
|
325
363
|
"core.user_message": [
|
|
326
|
-
exact(
|
|
364
|
+
exact(
|
|
365
|
+
"user",
|
|
366
|
+
`images, video and audio the model takes go in image_url parts as data URIs; ${RELAY_DIALECT_NOTE}`
|
|
367
|
+
),
|
|
327
368
|
lossy(
|
|
328
369
|
"user",
|
|
329
|
-
"moved after that batch of tool results (the order changes), untrusted content escaped, or images replaced with placeholder text because the model takes none",
|
|
330
|
-
"the user speaks up before all tool results are in / the content holds a </untrusted / it carries images the model does not take"
|
|
370
|
+
"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",
|
|
371
|
+
"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"
|
|
331
372
|
)
|
|
332
373
|
],
|
|
333
374
|
"core.model_text": [
|
|
@@ -361,13 +402,13 @@ var CHAT_LOSS_MATRIX = {
|
|
|
361
402
|
),
|
|
362
403
|
lossy(
|
|
363
404
|
"tool+lifted-images",
|
|
364
|
-
|
|
365
|
-
"the result carries images
|
|
405
|
+
`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}`,
|
|
406
|
+
"the result carries images / video / audio of a kind the model takes (chat.liftToolImages is on by default)"
|
|
366
407
|
),
|
|
367
408
|
lossy(
|
|
368
409
|
"tool-text-only",
|
|
369
|
-
"a tool message takes text only, so images are replaced with placeholder text",
|
|
370
|
-
"the result carries images and the model takes
|
|
410
|
+
"a tool message takes text only, so images / video / audio are replaced with placeholder text",
|
|
411
|
+
"the result carries images / video / audio and the model takes none of those kinds, or chat.liftToolImages is false"
|
|
371
412
|
)
|
|
372
413
|
],
|
|
373
414
|
"core.system_note": [
|
|
@@ -407,11 +448,14 @@ var lossy2 = (landing, note, when) => when ? { kind: "lossy", landing, note, whe
|
|
|
407
448
|
var dropped2 = (landing, note, when) => when ? { kind: "dropped", landing, note, when } : { kind: "dropped", landing, note };
|
|
408
449
|
var ANTHROPIC_LOSS_MATRIX = {
|
|
409
450
|
"core.user_message": [
|
|
410
|
-
exact2(
|
|
451
|
+
exact2(
|
|
452
|
+
"user",
|
|
453
|
+
`images, video and audio the model takes go in image blocks with their own media_type; ${RELAY_DIALECT_NOTE}`
|
|
454
|
+
),
|
|
411
455
|
lossy2(
|
|
412
456
|
"user",
|
|
413
|
-
"moved after that batch of tool results (the order changes), untrusted content escaped, or images replaced with placeholder text because the model takes none",
|
|
414
|
-
"the user speaks up before all tool results are in / the content holds a </untrusted / it carries images the model does not take"
|
|
457
|
+
"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",
|
|
458
|
+
"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"
|
|
415
459
|
),
|
|
416
460
|
dropped2(
|
|
417
461
|
"none",
|
|
@@ -453,11 +497,11 @@ var ANTHROPIC_LOSS_MATRIX = {
|
|
|
453
497
|
"core.tool_result": [
|
|
454
498
|
exact2(
|
|
455
499
|
"tool_result",
|
|
456
|
-
|
|
500
|
+
`in the user message right after the assistant that holds the tool_use; is_error is passed through; tool reference parts are expanded into text when the native capability is missing or the result is not trusted as system; video and audio the model takes go in image blocks (${RELAY_DIALECT_NOTE})`
|
|
457
501
|
),
|
|
458
502
|
lossy2(
|
|
459
503
|
"tool_result",
|
|
460
|
-
"untrusted content is escaped, or images are replaced with placeholder text because the model takes none, or the referenced tool is absent from this request's tool table and its definition is expanded into text"
|
|
504
|
+
"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"
|
|
461
505
|
),
|
|
462
506
|
exact2(
|
|
463
507
|
"tool-reference",
|
|
@@ -683,7 +727,6 @@ ${summary}`;
|
|
|
683
727
|
|
|
684
728
|
// src/anthropic/to-request.ts
|
|
685
729
|
var MAX_ANTHROPIC_BREAKPOINTS = 4;
|
|
686
|
-
var IMAGE_OMITTED = "[image omitted: this model does not accept images]";
|
|
687
730
|
var EMPTY_NOTE = "the content is empty and Anthropic rejects empty text blocks, so the whole message is not sent";
|
|
688
731
|
var MOVED_NOTE = "moved to just after the next user message (a system message must follow a user message, and be followed by an assistant message or the end)";
|
|
689
732
|
var REF_TEXT_ASIDE_NOTE = "the result's text segments move to a text block after this batch of tool_result in the same user message (a reference inside a tool_result cannot be mixed with text, and tool_result must come first in a user message)";
|
|
@@ -698,22 +741,36 @@ function land(out, e, kind, landing, ...notes) {
|
|
|
698
741
|
function isPlainObject(v) {
|
|
699
742
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
700
743
|
}
|
|
701
|
-
function blocksOf(parts,
|
|
702
|
-
|
|
744
|
+
function blocksOf(parts, capabilities) {
|
|
745
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
746
|
+
let relayed = false;
|
|
703
747
|
const blocks = [];
|
|
704
748
|
for (const p of parts) {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
749
|
+
switch (p.type) {
|
|
750
|
+
case "text":
|
|
751
|
+
if (p.text.length > 0) blocks.push({ type: "text", text: p.text });
|
|
752
|
+
break;
|
|
753
|
+
case "tool_reference":
|
|
754
|
+
blocks.push({ type: "text", text: renderToolReference(p) });
|
|
755
|
+
break;
|
|
756
|
+
case "image":
|
|
757
|
+
case "video":
|
|
758
|
+
case "audio":
|
|
759
|
+
if (acceptsMedia(p.type, capabilities)) {
|
|
760
|
+
if (p.type !== "image") relayed = true;
|
|
761
|
+
blocks.push({ type: "image", source: { type: "base64", media_type: p.mime, data: p.data } });
|
|
762
|
+
} else {
|
|
763
|
+
omitted.add(p.type);
|
|
764
|
+
blocks.push({ type: "text", text: mediaOmitted(p) });
|
|
765
|
+
}
|
|
766
|
+
break;
|
|
767
|
+
default: {
|
|
768
|
+
const _exhaustive = p;
|
|
769
|
+
void _exhaustive;
|
|
770
|
+
}
|
|
714
771
|
}
|
|
715
772
|
}
|
|
716
|
-
return { blocks,
|
|
773
|
+
return { blocks, omitted, relayed };
|
|
717
774
|
}
|
|
718
775
|
function encodeAnthropicRequest(input) {
|
|
719
776
|
const { model, capabilities } = input;
|
|
@@ -782,13 +839,13 @@ function encodeAnthropicRequest(input) {
|
|
|
782
839
|
for (const item of input.ir) {
|
|
783
840
|
switch (item.kind) {
|
|
784
841
|
case "user": {
|
|
785
|
-
const c = blocksOf(item.parts, capabilities
|
|
842
|
+
const c = blocksOf(item.parts, capabilities);
|
|
786
843
|
if (c.blocks.length === 0) {
|
|
787
844
|
land(landings, item.event, "dropped", "none", EMPTY_NOTE);
|
|
788
845
|
break;
|
|
789
846
|
}
|
|
790
847
|
openUser().push(...c.blocks);
|
|
791
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
848
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
792
849
|
land(
|
|
793
850
|
landings,
|
|
794
851
|
item.event,
|
|
@@ -796,7 +853,8 @@ function encodeAnthropicRequest(input) {
|
|
|
796
853
|
"user",
|
|
797
854
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
798
855
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
799
|
-
c.
|
|
856
|
+
omittedNote(c.omitted),
|
|
857
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
800
858
|
);
|
|
801
859
|
break;
|
|
802
860
|
}
|
|
@@ -811,7 +869,7 @@ function encodeAnthropicRequest(input) {
|
|
|
811
869
|
block.content = refs.map((r) => ({ type: "tool_reference", tool_name: r.name }));
|
|
812
870
|
const rest = blocksOf(
|
|
813
871
|
item.parts.filter((p) => p.type !== "tool_reference"),
|
|
814
|
-
capabilities
|
|
872
|
+
capabilities
|
|
815
873
|
);
|
|
816
874
|
openUser().push(block);
|
|
817
875
|
asides.push(...rest.blocks);
|
|
@@ -819,20 +877,21 @@ function encodeAnthropicRequest(input) {
|
|
|
819
877
|
land(
|
|
820
878
|
landings,
|
|
821
879
|
item.event,
|
|
822
|
-
aside || item.escaped || rest.
|
|
880
|
+
aside || item.escaped || rest.omitted.size > 0 ? "lossy" : "exact",
|
|
823
881
|
"tool-reference",
|
|
824
882
|
aside ? REF_TEXT_ASIDE_NOTE : void 0,
|
|
825
|
-
rest.
|
|
883
|
+
omittedNote(rest.omitted),
|
|
884
|
+
rest.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
826
885
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
827
886
|
);
|
|
828
887
|
break;
|
|
829
888
|
}
|
|
830
|
-
const c = blocksOf(item.parts, capabilities
|
|
889
|
+
const c = blocksOf(item.parts, capabilities);
|
|
831
890
|
if (c.blocks.length > 0) block.content = c.blocks;
|
|
832
891
|
openUser().push(block);
|
|
833
892
|
const unbound = refs.length > 0 && capabilities.deferredTools && trusted;
|
|
834
893
|
const untrustedRefs = refs.length > 0 && capabilities.deferredTools && !trusted;
|
|
835
|
-
const lossy4 = item.escaped || c.
|
|
894
|
+
const lossy4 = item.escaped || c.omitted.size > 0 || unbound;
|
|
836
895
|
land(
|
|
837
896
|
landings,
|
|
838
897
|
item.event,
|
|
@@ -840,7 +899,8 @@ function encodeAnthropicRequest(input) {
|
|
|
840
899
|
"tool_result",
|
|
841
900
|
unbound ? REF_UNBOUND_NOTE : void 0,
|
|
842
901
|
untrustedRefs ? REF_UNTRUSTED_NOTE : void 0,
|
|
843
|
-
c.
|
|
902
|
+
omittedNote(c.omitted),
|
|
903
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
844
904
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
845
905
|
);
|
|
846
906
|
break;
|
|
@@ -1044,6 +1104,9 @@ function capabilitiesOf(model) {
|
|
|
1044
1104
|
api: model.api,
|
|
1045
1105
|
parallelTools: true,
|
|
1046
1106
|
images: model.images ?? false,
|
|
1107
|
+
// 视频 / 音频只由宿主对中转站模型声明(方言,见 media.ts),缺省不收
|
|
1108
|
+
video: model.video ?? false,
|
|
1109
|
+
audio: model.audio ?? false,
|
|
1047
1110
|
contextWindow: model.contextWindow,
|
|
1048
1111
|
maxOutputTokens: model.maxOutputTokens,
|
|
1049
1112
|
taskBudget: false,
|
|
@@ -1250,9 +1313,9 @@ import {
|
|
|
1250
1313
|
markUntrusted as markUntrusted2,
|
|
1251
1314
|
renderToolReference as renderToolReference2
|
|
1252
1315
|
} from "@reinsjs/core";
|
|
1253
|
-
var IMAGE_OMITTED2 = "[image omitted: this model does not accept images]";
|
|
1254
1316
|
var TOOL_IMAGE_OMITTED = "[image omitted: tool messages carry text only]";
|
|
1255
|
-
var
|
|
1317
|
+
var toolMediaOmitted = (kind) => kind === "image" ? TOOL_IMAGE_OMITTED : `[${kind} omitted: tool messages carry text only]`;
|
|
1318
|
+
var toolMediaPointer = (kind, n) => `[${kind} ${n} attached in the next user message]`;
|
|
1256
1319
|
var ERROR_PREFIX = "[tool error]\n";
|
|
1257
1320
|
function land2(out, e, kind, landing, ...notes) {
|
|
1258
1321
|
const note = notes.filter((n) => Boolean(n)).join("; ");
|
|
@@ -1260,41 +1323,80 @@ function land2(out, e, kind, landing, ...notes) {
|
|
|
1260
1323
|
note ? { eventId: e.id, type: e.type, kind, landing, note } : { eventId: e.id, type: e.type, kind, landing }
|
|
1261
1324
|
);
|
|
1262
1325
|
}
|
|
1263
|
-
function userContent(parts,
|
|
1264
|
-
|
|
1326
|
+
function userContent(parts, capabilities) {
|
|
1327
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
1328
|
+
let relayed = false;
|
|
1265
1329
|
const out = parts.map((p) => {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1330
|
+
switch (p.type) {
|
|
1331
|
+
case "text":
|
|
1332
|
+
return { type: "text", text: p.text };
|
|
1333
|
+
case "tool_reference":
|
|
1334
|
+
return { type: "text", text: renderToolReference2(p) };
|
|
1335
|
+
case "image":
|
|
1336
|
+
case "video":
|
|
1337
|
+
case "audio":
|
|
1338
|
+
if (!acceptsMedia(p.type, capabilities)) {
|
|
1339
|
+
omitted.add(p.type);
|
|
1340
|
+
return { type: "text", text: mediaOmitted(p) };
|
|
1341
|
+
}
|
|
1342
|
+
if (p.type !== "image") relayed = true;
|
|
1343
|
+
return { type: "image_url", image_url: { url: `data:${p.mime};base64,${p.data}` } };
|
|
1344
|
+
default: {
|
|
1345
|
+
const _exhaustive = p;
|
|
1346
|
+
return _exhaustive;
|
|
1347
|
+
}
|
|
1271
1348
|
}
|
|
1272
|
-
return { type: "image_url", image_url: { url: `data:${p.mime};base64,${p.data}` } };
|
|
1273
1349
|
});
|
|
1274
|
-
if (out.length === 1 && out[0]?.type === "text") return { content: out[0].text,
|
|
1275
|
-
return { content: out,
|
|
1276
|
-
}
|
|
1277
|
-
function toolContent(parts, lift) {
|
|
1278
|
-
|
|
1279
|
-
const
|
|
1350
|
+
if (out.length === 1 && out[0]?.type === "text") return { content: out[0].text, omitted, relayed };
|
|
1351
|
+
return { content: out, omitted, relayed };
|
|
1352
|
+
}
|
|
1353
|
+
function toolContent(parts, capabilities, lift) {
|
|
1354
|
+
const unsupported = /* @__PURE__ */ new Set();
|
|
1355
|
+
const textOnly = /* @__PURE__ */ new Set();
|
|
1356
|
+
const media = [];
|
|
1357
|
+
let relayed = false;
|
|
1280
1358
|
const text = parts.map((p) => {
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1359
|
+
switch (p.type) {
|
|
1360
|
+
case "text":
|
|
1361
|
+
return p.text;
|
|
1362
|
+
case "tool_reference":
|
|
1363
|
+
return renderToolReference2(p);
|
|
1364
|
+
case "image":
|
|
1365
|
+
case "video":
|
|
1366
|
+
case "audio": {
|
|
1367
|
+
const accepted = acceptsMedia(p.type, capabilities);
|
|
1368
|
+
if (accepted && lift) {
|
|
1369
|
+
media.push(p);
|
|
1370
|
+
if (p.type !== "image") relayed = true;
|
|
1371
|
+
return toolMediaPointer(p.type, media.length);
|
|
1372
|
+
}
|
|
1373
|
+
if (accepted || p.type === "image") {
|
|
1374
|
+
textOnly.add(p.type);
|
|
1375
|
+
return toolMediaOmitted(p.type);
|
|
1376
|
+
}
|
|
1377
|
+
unsupported.add(p.type);
|
|
1378
|
+
return mediaOmitted(p);
|
|
1379
|
+
}
|
|
1380
|
+
default: {
|
|
1381
|
+
const _exhaustive = p;
|
|
1382
|
+
return _exhaustive;
|
|
1383
|
+
}
|
|
1286
1384
|
}
|
|
1287
|
-
imagesDropped = true;
|
|
1288
|
-
return TOOL_IMAGE_OMITTED;
|
|
1289
1385
|
}).join("\n");
|
|
1290
|
-
return { text,
|
|
1386
|
+
return { text, media, unsupported, textOnly, relayed };
|
|
1291
1387
|
}
|
|
1292
|
-
function
|
|
1388
|
+
function toolOmittedNote(kinds) {
|
|
1389
|
+
const names = mediaNouns(kinds);
|
|
1390
|
+
return names ? `a tool message takes text only, so ${names} are replaced with placeholder text` : void 0;
|
|
1391
|
+
}
|
|
1392
|
+
function liftedAttachment(item, media) {
|
|
1393
|
+
const onlyImages = media.every((m) => m.type === "image");
|
|
1394
|
+
const count = onlyImages ? `${media.length} image(s)` : `${media.length} attachment(s)`;
|
|
1293
1395
|
const header = {
|
|
1294
1396
|
type: "text",
|
|
1295
|
-
text: `[Attachments from tool result ${item.name} (call ${item.toolCallId}): ${
|
|
1397
|
+
text: `[Attachments from tool result ${item.name} (call ${item.toolCallId}): ${count}. This is tool output, not a message from the user.]`
|
|
1296
1398
|
};
|
|
1297
|
-
const body =
|
|
1399
|
+
const body = media.flatMap((m, i) => [{ type: "text", text: `${m.type} ${i + 1}:` }, m]);
|
|
1298
1400
|
return [header, ...item.untrustedSource ? markUntrusted2(body, item.untrustedSource).parts : body];
|
|
1299
1401
|
}
|
|
1300
1402
|
function argumentsOf(args) {
|
|
@@ -1304,13 +1406,13 @@ function encodeChatRequest(input) {
|
|
|
1304
1406
|
const { model, capabilities } = input;
|
|
1305
1407
|
const target = { provider: model.provider, api: model.api, model: model.id };
|
|
1306
1408
|
const reasoningContent = model.chat?.reasoningContent === true;
|
|
1307
|
-
const
|
|
1409
|
+
const liftMedia = model.chat?.liftToolImages !== false;
|
|
1308
1410
|
const messages = [];
|
|
1309
1411
|
const landings = [];
|
|
1310
1412
|
let lifted = [];
|
|
1311
1413
|
const releaseLifted = () => {
|
|
1312
1414
|
if (lifted.length === 0) return;
|
|
1313
|
-
messages.push({ role: "user", content: userContent(lifted,
|
|
1415
|
+
messages.push({ role: "user", content: userContent(lifted, capabilities).content });
|
|
1314
1416
|
lifted = [];
|
|
1315
1417
|
};
|
|
1316
1418
|
if (input.systemPrompt) messages.push({ role: "system", content: input.systemPrompt });
|
|
@@ -1318,9 +1420,9 @@ function encodeChatRequest(input) {
|
|
|
1318
1420
|
if (item.kind !== "tool_result" && item.kind !== "dropped") releaseLifted();
|
|
1319
1421
|
switch (item.kind) {
|
|
1320
1422
|
case "user": {
|
|
1321
|
-
const c = userContent(item.parts, capabilities
|
|
1423
|
+
const c = userContent(item.parts, capabilities);
|
|
1322
1424
|
messages.push({ role: "user", content: c.content });
|
|
1323
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
1425
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
1324
1426
|
land2(
|
|
1325
1427
|
landings,
|
|
1326
1428
|
item.event,
|
|
@@ -1328,7 +1430,8 @@ function encodeChatRequest(input) {
|
|
|
1328
1430
|
"user",
|
|
1329
1431
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
1330
1432
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
1331
|
-
c.
|
|
1433
|
+
omittedNote(c.omitted),
|
|
1434
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
1332
1435
|
);
|
|
1333
1436
|
break;
|
|
1334
1437
|
}
|
|
@@ -1337,23 +1440,27 @@ function encodeChatRequest(input) {
|
|
|
1337
1440
|
break;
|
|
1338
1441
|
}
|
|
1339
1442
|
case "tool_result": {
|
|
1340
|
-
const c = toolContent(item.parts,
|
|
1443
|
+
const c = toolContent(item.parts, capabilities, liftMedia);
|
|
1341
1444
|
messages.push({
|
|
1342
1445
|
role: "tool",
|
|
1343
1446
|
tool_call_id: item.toolCallId,
|
|
1344
1447
|
content: item.isError ? ERROR_PREFIX + c.text : c.text
|
|
1345
1448
|
});
|
|
1346
|
-
const
|
|
1347
|
-
if (
|
|
1348
|
-
const
|
|
1449
|
+
const mediaLifted = c.media.length > 0;
|
|
1450
|
+
if (mediaLifted) lifted.push(...liftedAttachment(item, c.media));
|
|
1451
|
+
const dropped4 = c.unsupported.size > 0 || c.textOnly.size > 0;
|
|
1452
|
+
const lossy4 = item.escaped || item.isError || dropped4 || mediaLifted;
|
|
1349
1453
|
land2(
|
|
1350
1454
|
landings,
|
|
1351
1455
|
item.event,
|
|
1352
1456
|
lossy4 ? "lossy" : "exact",
|
|
1353
|
-
|
|
1457
|
+
// 有上浮就记上浮(另有被换占位的种类在 note 里说);只有占位才记 tool-text-only
|
|
1458
|
+
mediaLifted ? "tool+lifted-images" : dropped4 ? "tool-text-only" : "tool",
|
|
1354
1459
|
item.isError ? "a Chat tool message has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
|
|
1355
|
-
c.
|
|
1356
|
-
|
|
1460
|
+
toolOmittedNote(c.textOnly),
|
|
1461
|
+
omittedNote(c.unsupported),
|
|
1462
|
+
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,
|
|
1463
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
1357
1464
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
1358
1465
|
);
|
|
1359
1466
|
break;
|
|
@@ -1584,7 +1691,8 @@ var BUILTIN_MODELS = [
|
|
|
1584
1691
|
contextWindow: 1e6,
|
|
1585
1692
|
maxOutputTokens: 384e3,
|
|
1586
1693
|
reasoning: true,
|
|
1587
|
-
|
|
1694
|
+
// 官方 /models 的 input_modalities 只有 text;带图请求回 200 但图被静默丢掉(BUG-261105-1,spikes/media-parts P 组)
|
|
1695
|
+
images: false,
|
|
1588
1696
|
cost: { input: 1.32, output: 3.96, cacheRead: 0.044, cacheWrite: 0 },
|
|
1589
1697
|
chat: { reasoningContent: true }
|
|
1590
1698
|
},
|
|
@@ -2017,7 +2125,6 @@ function errorText3(err) {
|
|
|
2017
2125
|
import {
|
|
2018
2126
|
renderToolReference as renderToolReference3
|
|
2019
2127
|
} from "@reinsjs/core";
|
|
2020
|
-
var IMAGE_OMITTED3 = "[image omitted: this model does not accept images]";
|
|
2021
2128
|
var ERROR_PREFIX2 = "[tool error]\n";
|
|
2022
2129
|
var EMPTY_NOTE2 = "the content is empty, so no empty message is sent";
|
|
2023
2130
|
var ENCRYPTED_INCLUDE = "reasoning.encrypted_content";
|
|
@@ -2027,22 +2134,36 @@ function land3(out, e, kind, landing, ...notes) {
|
|
|
2027
2134
|
note ? { eventId: e.id, type: e.type, kind, landing, note } : { eventId: e.id, type: e.type, kind, landing }
|
|
2028
2135
|
);
|
|
2029
2136
|
}
|
|
2030
|
-
function contentOf(parts,
|
|
2031
|
-
|
|
2137
|
+
function contentOf(parts, capabilities) {
|
|
2138
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
2139
|
+
let relayed = false;
|
|
2032
2140
|
const content = [];
|
|
2033
2141
|
for (const p of parts) {
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2142
|
+
switch (p.type) {
|
|
2143
|
+
case "text":
|
|
2144
|
+
if (p.text.length > 0) content.push({ type: "input_text", text: p.text });
|
|
2145
|
+
break;
|
|
2146
|
+
case "tool_reference":
|
|
2147
|
+
content.push({ type: "input_text", text: renderToolReference3(p) });
|
|
2148
|
+
break;
|
|
2149
|
+
case "image":
|
|
2150
|
+
case "video":
|
|
2151
|
+
case "audio":
|
|
2152
|
+
if (acceptsMedia(p.type, capabilities)) {
|
|
2153
|
+
if (p.type !== "image") relayed = true;
|
|
2154
|
+
content.push({ type: "input_image", image_url: `data:${p.mime};base64,${p.data}`, detail: "auto" });
|
|
2155
|
+
} else {
|
|
2156
|
+
omitted.add(p.type);
|
|
2157
|
+
content.push({ type: "input_text", text: mediaOmitted(p) });
|
|
2158
|
+
}
|
|
2159
|
+
break;
|
|
2160
|
+
default: {
|
|
2161
|
+
const _exhaustive = p;
|
|
2162
|
+
void _exhaustive;
|
|
2163
|
+
}
|
|
2043
2164
|
}
|
|
2044
2165
|
}
|
|
2045
|
-
return { content,
|
|
2166
|
+
return { content, omitted, relayed };
|
|
2046
2167
|
}
|
|
2047
2168
|
function argumentsOf2(args) {
|
|
2048
2169
|
return typeof args === "string" ? args : JSON.stringify(args ?? {});
|
|
@@ -2086,13 +2207,13 @@ function encodeResponsesRequest(input) {
|
|
|
2086
2207
|
for (const item of input.ir) {
|
|
2087
2208
|
switch (item.kind) {
|
|
2088
2209
|
case "user": {
|
|
2089
|
-
const c = contentOf(item.parts, capabilities
|
|
2210
|
+
const c = contentOf(item.parts, capabilities);
|
|
2090
2211
|
if (c.content.length === 0) {
|
|
2091
2212
|
land3(landings, item.event, "dropped", "none", EMPTY_NOTE2);
|
|
2092
2213
|
break;
|
|
2093
2214
|
}
|
|
2094
2215
|
items.push({ role: "user", content: c.content });
|
|
2095
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
2216
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
2096
2217
|
land3(
|
|
2097
2218
|
landings,
|
|
2098
2219
|
item.event,
|
|
@@ -2100,12 +2221,13 @@ function encodeResponsesRequest(input) {
|
|
|
2100
2221
|
"user",
|
|
2101
2222
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
2102
2223
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
2103
|
-
c.
|
|
2224
|
+
omittedNote(c.omitted),
|
|
2225
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
2104
2226
|
);
|
|
2105
2227
|
break;
|
|
2106
2228
|
}
|
|
2107
2229
|
case "tool_result": {
|
|
2108
|
-
const c = contentOf(item.parts, capabilities
|
|
2230
|
+
const c = contentOf(item.parts, capabilities);
|
|
2109
2231
|
const hasImage = c.content.some((b) => b.type === "input_image");
|
|
2110
2232
|
const text = c.content.filter((b) => b.type === "input_text").map((b) => b.text).join("\n");
|
|
2111
2233
|
const prefixed = item.isError ? ERROR_PREFIX2 + text : text;
|
|
@@ -2114,14 +2236,15 @@ function encodeResponsesRequest(input) {
|
|
|
2114
2236
|
...c.content.filter((b) => b.type === "input_image")
|
|
2115
2237
|
] : prefixed;
|
|
2116
2238
|
items.push({ type: "function_call_output", call_id: item.toolCallId, output });
|
|
2117
|
-
const lossy4 = item.escaped || item.isError || c.
|
|
2239
|
+
const lossy4 = item.escaped || item.isError || c.omitted.size > 0;
|
|
2118
2240
|
land3(
|
|
2119
2241
|
landings,
|
|
2120
2242
|
item.event,
|
|
2121
2243
|
lossy4 ? "lossy" : "exact",
|
|
2122
2244
|
"function_call_output",
|
|
2123
2245
|
item.isError ? "the Responses function_call_output has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
|
|
2124
|
-
c.
|
|
2246
|
+
omittedNote(c.omitted),
|
|
2247
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
2125
2248
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
2126
2249
|
);
|
|
2127
2250
|
break;
|
|
@@ -2511,6 +2634,8 @@ function definitionOf(provider, id, api, opts) {
|
|
|
2511
2634
|
maxOutputTokens: opts.maxOutputTokens,
|
|
2512
2635
|
reasoning: opts.reasoning,
|
|
2513
2636
|
images: opts.images,
|
|
2637
|
+
video: opts.video,
|
|
2638
|
+
audio: opts.audio,
|
|
2514
2639
|
cost: opts.cost,
|
|
2515
2640
|
midConversationSystem: opts.midConversationSystem,
|
|
2516
2641
|
auth: opts.auth,
|
|
@@ -2536,11 +2661,12 @@ function chatCompletions(id, opts) {
|
|
|
2536
2661
|
return bound(opts.provider, id, definitionOf(opts.provider, id, "openai-chat", opts), opts);
|
|
2537
2662
|
}
|
|
2538
2663
|
function deepseek(id, opts) {
|
|
2664
|
+
const unknown = findBuiltin("deepseek", id, "openai-chat") === void 0;
|
|
2539
2665
|
return chatCompletions(id, {
|
|
2540
2666
|
provider: "deepseek",
|
|
2541
2667
|
baseUrl: "https://api.deepseek.com",
|
|
2542
2668
|
reasoning: true,
|
|
2543
|
-
images: true,
|
|
2669
|
+
...unknown ? { images: true } : {},
|
|
2544
2670
|
...opts,
|
|
2545
2671
|
chat: { reasoningContent: true, ...opts.chat }
|
|
2546
2672
|
});
|
|
@@ -2584,11 +2710,14 @@ var lossy3 = (landing, note, when) => when ? { kind: "lossy", landing, note, whe
|
|
|
2584
2710
|
var dropped3 = (landing, note, when) => when ? { kind: "dropped", landing, note, when } : { kind: "dropped", landing, note };
|
|
2585
2711
|
var RESPONSES_LOSS_MATRIX = {
|
|
2586
2712
|
"core.user_message": [
|
|
2587
|
-
exact3(
|
|
2713
|
+
exact3(
|
|
2714
|
+
"user",
|
|
2715
|
+
`a user message in input, as input_text / input_image blocks (video and audio the model takes also go in input_image; never input_audio, which the relay silently drops); ${RELAY_DIALECT_NOTE}`
|
|
2716
|
+
),
|
|
2588
2717
|
lossy3(
|
|
2589
2718
|
"user",
|
|
2590
|
-
"moved after that batch of tool results (the order changes), untrusted content escaped, or images replaced with placeholder text because the model takes none",
|
|
2591
|
-
"the user speaks up before all tool results are in / the content holds a </untrusted / it carries images the model does not take"
|
|
2719
|
+
"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",
|
|
2720
|
+
"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"
|
|
2592
2721
|
),
|
|
2593
2722
|
dropped3(
|
|
2594
2723
|
"none",
|
|
@@ -2623,11 +2752,11 @@ var RESPONSES_LOSS_MATRIX = {
|
|
|
2623
2752
|
"core.tool_result": [
|
|
2624
2753
|
exact3(
|
|
2625
2754
|
"function_call_output",
|
|
2626
|
-
|
|
2755
|
+
`matched by call_id; output is a string for text, or an array of content blocks when images / video / audio are present (all in input_image); tool reference parts are expanded into text (Responses has no deferred-loading landing); ${RELAY_DIALECT_NOTE}`
|
|
2627
2756
|
),
|
|
2628
2757
|
lossy3(
|
|
2629
2758
|
"function_call_output",
|
|
2630
|
-
"isError is expressed with a [tool error] prefix (the protocol has no error flag), or untrusted content is escaped, or images are replaced with placeholder text because the model takes none"
|
|
2759
|
+
"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"
|
|
2631
2760
|
)
|
|
2632
2761
|
],
|
|
2633
2762
|
"core.system_note": [
|