@reinsjs/lowering-fetch 0.5.3 → 0.6.1
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 +37 -0
- package/README.md +9 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +235 -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 };
|
|
@@ -308,6 +346,12 @@ var NOT_SENT = {
|
|
|
308
346
|
"a snapshot of the tool table; the loop announces changes to it in a separate system_note"
|
|
309
347
|
)
|
|
310
348
|
],
|
|
349
|
+
"core.tool_started": [
|
|
350
|
+
dropped(
|
|
351
|
+
"none",
|
|
352
|
+
"marks that a call began executing, for resume and stop handling; the model learns the outcome from tool_result"
|
|
353
|
+
)
|
|
354
|
+
],
|
|
311
355
|
"core.error": [
|
|
312
356
|
dropped(
|
|
313
357
|
"none",
|
|
@@ -323,11 +367,14 @@ var NOT_SENT = {
|
|
|
323
367
|
};
|
|
324
368
|
var CHAT_LOSS_MATRIX = {
|
|
325
369
|
"core.user_message": [
|
|
326
|
-
exact(
|
|
370
|
+
exact(
|
|
371
|
+
"user",
|
|
372
|
+
`images, video and audio the model takes go in image_url parts as data URIs; ${RELAY_DIALECT_NOTE}`
|
|
373
|
+
),
|
|
327
374
|
lossy(
|
|
328
375
|
"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"
|
|
376
|
+
"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",
|
|
377
|
+
"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
378
|
)
|
|
332
379
|
],
|
|
333
380
|
"core.model_text": [
|
|
@@ -361,13 +408,13 @@ var CHAT_LOSS_MATRIX = {
|
|
|
361
408
|
),
|
|
362
409
|
lossy(
|
|
363
410
|
"tool+lifted-images",
|
|
364
|
-
|
|
365
|
-
"the result carries images
|
|
411
|
+
`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}`,
|
|
412
|
+
"the result carries images / video / audio of a kind the model takes (chat.liftToolImages is on by default)"
|
|
366
413
|
),
|
|
367
414
|
lossy(
|
|
368
415
|
"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
|
|
416
|
+
"a tool message takes text only, so images / video / audio are replaced with placeholder text",
|
|
417
|
+
"the result carries images / video / audio and the model takes none of those kinds, or chat.liftToolImages is false"
|
|
371
418
|
)
|
|
372
419
|
],
|
|
373
420
|
"core.system_note": [
|
|
@@ -407,11 +454,14 @@ var lossy2 = (landing, note, when) => when ? { kind: "lossy", landing, note, whe
|
|
|
407
454
|
var dropped2 = (landing, note, when) => when ? { kind: "dropped", landing, note, when } : { kind: "dropped", landing, note };
|
|
408
455
|
var ANTHROPIC_LOSS_MATRIX = {
|
|
409
456
|
"core.user_message": [
|
|
410
|
-
exact2(
|
|
457
|
+
exact2(
|
|
458
|
+
"user",
|
|
459
|
+
`images, video and audio the model takes go in image blocks with their own media_type; ${RELAY_DIALECT_NOTE}`
|
|
460
|
+
),
|
|
411
461
|
lossy2(
|
|
412
462
|
"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"
|
|
463
|
+
"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",
|
|
464
|
+
"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
465
|
),
|
|
416
466
|
dropped2(
|
|
417
467
|
"none",
|
|
@@ -453,11 +503,11 @@ var ANTHROPIC_LOSS_MATRIX = {
|
|
|
453
503
|
"core.tool_result": [
|
|
454
504
|
exact2(
|
|
455
505
|
"tool_result",
|
|
456
|
-
|
|
506
|
+
`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
507
|
),
|
|
458
508
|
lossy2(
|
|
459
509
|
"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"
|
|
510
|
+
"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
511
|
),
|
|
462
512
|
exact2(
|
|
463
513
|
"tool-reference",
|
|
@@ -652,6 +702,7 @@ function eventsToIr(input) {
|
|
|
652
702
|
case "core.memory_op":
|
|
653
703
|
case "core.handoff":
|
|
654
704
|
case "core.tools_bound":
|
|
705
|
+
case "core.tool_started":
|
|
655
706
|
case "core.error":
|
|
656
707
|
items.push({ kind: "dropped", event: e, note: NOT_SENT_NOTE });
|
|
657
708
|
break;
|
|
@@ -683,7 +734,6 @@ ${summary}`;
|
|
|
683
734
|
|
|
684
735
|
// src/anthropic/to-request.ts
|
|
685
736
|
var MAX_ANTHROPIC_BREAKPOINTS = 4;
|
|
686
|
-
var IMAGE_OMITTED = "[image omitted: this model does not accept images]";
|
|
687
737
|
var EMPTY_NOTE = "the content is empty and Anthropic rejects empty text blocks, so the whole message is not sent";
|
|
688
738
|
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
739
|
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 +748,36 @@ function land(out, e, kind, landing, ...notes) {
|
|
|
698
748
|
function isPlainObject(v) {
|
|
699
749
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
700
750
|
}
|
|
701
|
-
function blocksOf(parts,
|
|
702
|
-
|
|
751
|
+
function blocksOf(parts, capabilities) {
|
|
752
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
753
|
+
let relayed = false;
|
|
703
754
|
const blocks = [];
|
|
704
755
|
for (const p of parts) {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
756
|
+
switch (p.type) {
|
|
757
|
+
case "text":
|
|
758
|
+
if (p.text.length > 0) blocks.push({ type: "text", text: p.text });
|
|
759
|
+
break;
|
|
760
|
+
case "tool_reference":
|
|
761
|
+
blocks.push({ type: "text", text: renderToolReference(p) });
|
|
762
|
+
break;
|
|
763
|
+
case "image":
|
|
764
|
+
case "video":
|
|
765
|
+
case "audio":
|
|
766
|
+
if (acceptsMedia(p.type, capabilities)) {
|
|
767
|
+
if (p.type !== "image") relayed = true;
|
|
768
|
+
blocks.push({ type: "image", source: { type: "base64", media_type: p.mime, data: p.data } });
|
|
769
|
+
} else {
|
|
770
|
+
omitted.add(p.type);
|
|
771
|
+
blocks.push({ type: "text", text: mediaOmitted(p) });
|
|
772
|
+
}
|
|
773
|
+
break;
|
|
774
|
+
default: {
|
|
775
|
+
const _exhaustive = p;
|
|
776
|
+
void _exhaustive;
|
|
777
|
+
}
|
|
714
778
|
}
|
|
715
779
|
}
|
|
716
|
-
return { blocks,
|
|
780
|
+
return { blocks, omitted, relayed };
|
|
717
781
|
}
|
|
718
782
|
function encodeAnthropicRequest(input) {
|
|
719
783
|
const { model, capabilities } = input;
|
|
@@ -782,13 +846,13 @@ function encodeAnthropicRequest(input) {
|
|
|
782
846
|
for (const item of input.ir) {
|
|
783
847
|
switch (item.kind) {
|
|
784
848
|
case "user": {
|
|
785
|
-
const c = blocksOf(item.parts, capabilities
|
|
849
|
+
const c = blocksOf(item.parts, capabilities);
|
|
786
850
|
if (c.blocks.length === 0) {
|
|
787
851
|
land(landings, item.event, "dropped", "none", EMPTY_NOTE);
|
|
788
852
|
break;
|
|
789
853
|
}
|
|
790
854
|
openUser().push(...c.blocks);
|
|
791
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
855
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
792
856
|
land(
|
|
793
857
|
landings,
|
|
794
858
|
item.event,
|
|
@@ -796,7 +860,8 @@ function encodeAnthropicRequest(input) {
|
|
|
796
860
|
"user",
|
|
797
861
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
798
862
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
799
|
-
c.
|
|
863
|
+
omittedNote(c.omitted),
|
|
864
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
800
865
|
);
|
|
801
866
|
break;
|
|
802
867
|
}
|
|
@@ -811,7 +876,7 @@ function encodeAnthropicRequest(input) {
|
|
|
811
876
|
block.content = refs.map((r) => ({ type: "tool_reference", tool_name: r.name }));
|
|
812
877
|
const rest = blocksOf(
|
|
813
878
|
item.parts.filter((p) => p.type !== "tool_reference"),
|
|
814
|
-
capabilities
|
|
879
|
+
capabilities
|
|
815
880
|
);
|
|
816
881
|
openUser().push(block);
|
|
817
882
|
asides.push(...rest.blocks);
|
|
@@ -819,20 +884,21 @@ function encodeAnthropicRequest(input) {
|
|
|
819
884
|
land(
|
|
820
885
|
landings,
|
|
821
886
|
item.event,
|
|
822
|
-
aside || item.escaped || rest.
|
|
887
|
+
aside || item.escaped || rest.omitted.size > 0 ? "lossy" : "exact",
|
|
823
888
|
"tool-reference",
|
|
824
889
|
aside ? REF_TEXT_ASIDE_NOTE : void 0,
|
|
825
|
-
rest.
|
|
890
|
+
omittedNote(rest.omitted),
|
|
891
|
+
rest.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
826
892
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
827
893
|
);
|
|
828
894
|
break;
|
|
829
895
|
}
|
|
830
|
-
const c = blocksOf(item.parts, capabilities
|
|
896
|
+
const c = blocksOf(item.parts, capabilities);
|
|
831
897
|
if (c.blocks.length > 0) block.content = c.blocks;
|
|
832
898
|
openUser().push(block);
|
|
833
899
|
const unbound = refs.length > 0 && capabilities.deferredTools && trusted;
|
|
834
900
|
const untrustedRefs = refs.length > 0 && capabilities.deferredTools && !trusted;
|
|
835
|
-
const lossy4 = item.escaped || c.
|
|
901
|
+
const lossy4 = item.escaped || c.omitted.size > 0 || unbound;
|
|
836
902
|
land(
|
|
837
903
|
landings,
|
|
838
904
|
item.event,
|
|
@@ -840,7 +906,8 @@ function encodeAnthropicRequest(input) {
|
|
|
840
906
|
"tool_result",
|
|
841
907
|
unbound ? REF_UNBOUND_NOTE : void 0,
|
|
842
908
|
untrustedRefs ? REF_UNTRUSTED_NOTE : void 0,
|
|
843
|
-
c.
|
|
909
|
+
omittedNote(c.omitted),
|
|
910
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
844
911
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
845
912
|
);
|
|
846
913
|
break;
|
|
@@ -1044,6 +1111,9 @@ function capabilitiesOf(model) {
|
|
|
1044
1111
|
api: model.api,
|
|
1045
1112
|
parallelTools: true,
|
|
1046
1113
|
images: model.images ?? false,
|
|
1114
|
+
// 视频 / 音频只由宿主对中转站模型声明(方言,见 media.ts),缺省不收
|
|
1115
|
+
video: model.video ?? false,
|
|
1116
|
+
audio: model.audio ?? false,
|
|
1047
1117
|
contextWindow: model.contextWindow,
|
|
1048
1118
|
maxOutputTokens: model.maxOutputTokens,
|
|
1049
1119
|
taskBudget: false,
|
|
@@ -1250,9 +1320,9 @@ import {
|
|
|
1250
1320
|
markUntrusted as markUntrusted2,
|
|
1251
1321
|
renderToolReference as renderToolReference2
|
|
1252
1322
|
} from "@reinsjs/core";
|
|
1253
|
-
var IMAGE_OMITTED2 = "[image omitted: this model does not accept images]";
|
|
1254
1323
|
var TOOL_IMAGE_OMITTED = "[image omitted: tool messages carry text only]";
|
|
1255
|
-
var
|
|
1324
|
+
var toolMediaOmitted = (kind) => kind === "image" ? TOOL_IMAGE_OMITTED : `[${kind} omitted: tool messages carry text only]`;
|
|
1325
|
+
var toolMediaPointer = (kind, n) => `[${kind} ${n} attached in the next user message]`;
|
|
1256
1326
|
var ERROR_PREFIX = "[tool error]\n";
|
|
1257
1327
|
function land2(out, e, kind, landing, ...notes) {
|
|
1258
1328
|
const note = notes.filter((n) => Boolean(n)).join("; ");
|
|
@@ -1260,41 +1330,80 @@ function land2(out, e, kind, landing, ...notes) {
|
|
|
1260
1330
|
note ? { eventId: e.id, type: e.type, kind, landing, note } : { eventId: e.id, type: e.type, kind, landing }
|
|
1261
1331
|
);
|
|
1262
1332
|
}
|
|
1263
|
-
function userContent(parts,
|
|
1264
|
-
|
|
1333
|
+
function userContent(parts, capabilities) {
|
|
1334
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
1335
|
+
let relayed = false;
|
|
1265
1336
|
const out = parts.map((p) => {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1337
|
+
switch (p.type) {
|
|
1338
|
+
case "text":
|
|
1339
|
+
return { type: "text", text: p.text };
|
|
1340
|
+
case "tool_reference":
|
|
1341
|
+
return { type: "text", text: renderToolReference2(p) };
|
|
1342
|
+
case "image":
|
|
1343
|
+
case "video":
|
|
1344
|
+
case "audio":
|
|
1345
|
+
if (!acceptsMedia(p.type, capabilities)) {
|
|
1346
|
+
omitted.add(p.type);
|
|
1347
|
+
return { type: "text", text: mediaOmitted(p) };
|
|
1348
|
+
}
|
|
1349
|
+
if (p.type !== "image") relayed = true;
|
|
1350
|
+
return { type: "image_url", image_url: { url: `data:${p.mime};base64,${p.data}` } };
|
|
1351
|
+
default: {
|
|
1352
|
+
const _exhaustive = p;
|
|
1353
|
+
return _exhaustive;
|
|
1354
|
+
}
|
|
1271
1355
|
}
|
|
1272
|
-
return { type: "image_url", image_url: { url: `data:${p.mime};base64,${p.data}` } };
|
|
1273
1356
|
});
|
|
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
|
|
1357
|
+
if (out.length === 1 && out[0]?.type === "text") return { content: out[0].text, omitted, relayed };
|
|
1358
|
+
return { content: out, omitted, relayed };
|
|
1359
|
+
}
|
|
1360
|
+
function toolContent(parts, capabilities, lift) {
|
|
1361
|
+
const unsupported = /* @__PURE__ */ new Set();
|
|
1362
|
+
const textOnly = /* @__PURE__ */ new Set();
|
|
1363
|
+
const media = [];
|
|
1364
|
+
let relayed = false;
|
|
1280
1365
|
const text = parts.map((p) => {
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1366
|
+
switch (p.type) {
|
|
1367
|
+
case "text":
|
|
1368
|
+
return p.text;
|
|
1369
|
+
case "tool_reference":
|
|
1370
|
+
return renderToolReference2(p);
|
|
1371
|
+
case "image":
|
|
1372
|
+
case "video":
|
|
1373
|
+
case "audio": {
|
|
1374
|
+
const accepted = acceptsMedia(p.type, capabilities);
|
|
1375
|
+
if (accepted && lift) {
|
|
1376
|
+
media.push(p);
|
|
1377
|
+
if (p.type !== "image") relayed = true;
|
|
1378
|
+
return toolMediaPointer(p.type, media.length);
|
|
1379
|
+
}
|
|
1380
|
+
if (accepted || p.type === "image") {
|
|
1381
|
+
textOnly.add(p.type);
|
|
1382
|
+
return toolMediaOmitted(p.type);
|
|
1383
|
+
}
|
|
1384
|
+
unsupported.add(p.type);
|
|
1385
|
+
return mediaOmitted(p);
|
|
1386
|
+
}
|
|
1387
|
+
default: {
|
|
1388
|
+
const _exhaustive = p;
|
|
1389
|
+
return _exhaustive;
|
|
1390
|
+
}
|
|
1286
1391
|
}
|
|
1287
|
-
imagesDropped = true;
|
|
1288
|
-
return TOOL_IMAGE_OMITTED;
|
|
1289
1392
|
}).join("\n");
|
|
1290
|
-
return { text,
|
|
1393
|
+
return { text, media, unsupported, textOnly, relayed };
|
|
1394
|
+
}
|
|
1395
|
+
function toolOmittedNote(kinds) {
|
|
1396
|
+
const names = mediaNouns(kinds);
|
|
1397
|
+
return names ? `a tool message takes text only, so ${names} are replaced with placeholder text` : void 0;
|
|
1291
1398
|
}
|
|
1292
|
-
function liftedAttachment(item,
|
|
1399
|
+
function liftedAttachment(item, media) {
|
|
1400
|
+
const onlyImages = media.every((m) => m.type === "image");
|
|
1401
|
+
const count = onlyImages ? `${media.length} image(s)` : `${media.length} attachment(s)`;
|
|
1293
1402
|
const header = {
|
|
1294
1403
|
type: "text",
|
|
1295
|
-
text: `[Attachments from tool result ${item.name} (call ${item.toolCallId}): ${
|
|
1404
|
+
text: `[Attachments from tool result ${item.name} (call ${item.toolCallId}): ${count}. This is tool output, not a message from the user.]`
|
|
1296
1405
|
};
|
|
1297
|
-
const body =
|
|
1406
|
+
const body = media.flatMap((m, i) => [{ type: "text", text: `${m.type} ${i + 1}:` }, m]);
|
|
1298
1407
|
return [header, ...item.untrustedSource ? markUntrusted2(body, item.untrustedSource).parts : body];
|
|
1299
1408
|
}
|
|
1300
1409
|
function argumentsOf(args) {
|
|
@@ -1304,13 +1413,13 @@ function encodeChatRequest(input) {
|
|
|
1304
1413
|
const { model, capabilities } = input;
|
|
1305
1414
|
const target = { provider: model.provider, api: model.api, model: model.id };
|
|
1306
1415
|
const reasoningContent = model.chat?.reasoningContent === true;
|
|
1307
|
-
const
|
|
1416
|
+
const liftMedia = model.chat?.liftToolImages !== false;
|
|
1308
1417
|
const messages = [];
|
|
1309
1418
|
const landings = [];
|
|
1310
1419
|
let lifted = [];
|
|
1311
1420
|
const releaseLifted = () => {
|
|
1312
1421
|
if (lifted.length === 0) return;
|
|
1313
|
-
messages.push({ role: "user", content: userContent(lifted,
|
|
1422
|
+
messages.push({ role: "user", content: userContent(lifted, capabilities).content });
|
|
1314
1423
|
lifted = [];
|
|
1315
1424
|
};
|
|
1316
1425
|
if (input.systemPrompt) messages.push({ role: "system", content: input.systemPrompt });
|
|
@@ -1318,9 +1427,9 @@ function encodeChatRequest(input) {
|
|
|
1318
1427
|
if (item.kind !== "tool_result" && item.kind !== "dropped") releaseLifted();
|
|
1319
1428
|
switch (item.kind) {
|
|
1320
1429
|
case "user": {
|
|
1321
|
-
const c = userContent(item.parts, capabilities
|
|
1430
|
+
const c = userContent(item.parts, capabilities);
|
|
1322
1431
|
messages.push({ role: "user", content: c.content });
|
|
1323
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
1432
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
1324
1433
|
land2(
|
|
1325
1434
|
landings,
|
|
1326
1435
|
item.event,
|
|
@@ -1328,7 +1437,8 @@ function encodeChatRequest(input) {
|
|
|
1328
1437
|
"user",
|
|
1329
1438
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
1330
1439
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
1331
|
-
c.
|
|
1440
|
+
omittedNote(c.omitted),
|
|
1441
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
1332
1442
|
);
|
|
1333
1443
|
break;
|
|
1334
1444
|
}
|
|
@@ -1337,23 +1447,27 @@ function encodeChatRequest(input) {
|
|
|
1337
1447
|
break;
|
|
1338
1448
|
}
|
|
1339
1449
|
case "tool_result": {
|
|
1340
|
-
const c = toolContent(item.parts,
|
|
1450
|
+
const c = toolContent(item.parts, capabilities, liftMedia);
|
|
1341
1451
|
messages.push({
|
|
1342
1452
|
role: "tool",
|
|
1343
1453
|
tool_call_id: item.toolCallId,
|
|
1344
1454
|
content: item.isError ? ERROR_PREFIX + c.text : c.text
|
|
1345
1455
|
});
|
|
1346
|
-
const
|
|
1347
|
-
if (
|
|
1348
|
-
const
|
|
1456
|
+
const mediaLifted = c.media.length > 0;
|
|
1457
|
+
if (mediaLifted) lifted.push(...liftedAttachment(item, c.media));
|
|
1458
|
+
const dropped4 = c.unsupported.size > 0 || c.textOnly.size > 0;
|
|
1459
|
+
const lossy4 = item.escaped || item.isError || dropped4 || mediaLifted;
|
|
1349
1460
|
land2(
|
|
1350
1461
|
landings,
|
|
1351
1462
|
item.event,
|
|
1352
1463
|
lossy4 ? "lossy" : "exact",
|
|
1353
|
-
|
|
1464
|
+
// 有上浮就记上浮(另有被换占位的种类在 note 里说);只有占位才记 tool-text-only
|
|
1465
|
+
mediaLifted ? "tool+lifted-images" : dropped4 ? "tool-text-only" : "tool",
|
|
1354
1466
|
item.isError ? "a Chat tool message has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
|
|
1355
|
-
c.
|
|
1356
|
-
|
|
1467
|
+
toolOmittedNote(c.textOnly),
|
|
1468
|
+
omittedNote(c.unsupported),
|
|
1469
|
+
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,
|
|
1470
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
1357
1471
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
1358
1472
|
);
|
|
1359
1473
|
break;
|
|
@@ -1584,7 +1698,8 @@ var BUILTIN_MODELS = [
|
|
|
1584
1698
|
contextWindow: 1e6,
|
|
1585
1699
|
maxOutputTokens: 384e3,
|
|
1586
1700
|
reasoning: true,
|
|
1587
|
-
|
|
1701
|
+
// 官方 /models 的 input_modalities 只有 text;带图请求回 200 但图被静默丢掉(BUG-261105-1,spikes/media-parts P 组)
|
|
1702
|
+
images: false,
|
|
1588
1703
|
cost: { input: 1.32, output: 3.96, cacheRead: 0.044, cacheWrite: 0 },
|
|
1589
1704
|
chat: { reasoningContent: true }
|
|
1590
1705
|
},
|
|
@@ -2017,7 +2132,6 @@ function errorText3(err) {
|
|
|
2017
2132
|
import {
|
|
2018
2133
|
renderToolReference as renderToolReference3
|
|
2019
2134
|
} from "@reinsjs/core";
|
|
2020
|
-
var IMAGE_OMITTED3 = "[image omitted: this model does not accept images]";
|
|
2021
2135
|
var ERROR_PREFIX2 = "[tool error]\n";
|
|
2022
2136
|
var EMPTY_NOTE2 = "the content is empty, so no empty message is sent";
|
|
2023
2137
|
var ENCRYPTED_INCLUDE = "reasoning.encrypted_content";
|
|
@@ -2027,22 +2141,36 @@ function land3(out, e, kind, landing, ...notes) {
|
|
|
2027
2141
|
note ? { eventId: e.id, type: e.type, kind, landing, note } : { eventId: e.id, type: e.type, kind, landing }
|
|
2028
2142
|
);
|
|
2029
2143
|
}
|
|
2030
|
-
function contentOf(parts,
|
|
2031
|
-
|
|
2144
|
+
function contentOf(parts, capabilities) {
|
|
2145
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
2146
|
+
let relayed = false;
|
|
2032
2147
|
const content = [];
|
|
2033
2148
|
for (const p of parts) {
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2149
|
+
switch (p.type) {
|
|
2150
|
+
case "text":
|
|
2151
|
+
if (p.text.length > 0) content.push({ type: "input_text", text: p.text });
|
|
2152
|
+
break;
|
|
2153
|
+
case "tool_reference":
|
|
2154
|
+
content.push({ type: "input_text", text: renderToolReference3(p) });
|
|
2155
|
+
break;
|
|
2156
|
+
case "image":
|
|
2157
|
+
case "video":
|
|
2158
|
+
case "audio":
|
|
2159
|
+
if (acceptsMedia(p.type, capabilities)) {
|
|
2160
|
+
if (p.type !== "image") relayed = true;
|
|
2161
|
+
content.push({ type: "input_image", image_url: `data:${p.mime};base64,${p.data}`, detail: "auto" });
|
|
2162
|
+
} else {
|
|
2163
|
+
omitted.add(p.type);
|
|
2164
|
+
content.push({ type: "input_text", text: mediaOmitted(p) });
|
|
2165
|
+
}
|
|
2166
|
+
break;
|
|
2167
|
+
default: {
|
|
2168
|
+
const _exhaustive = p;
|
|
2169
|
+
void _exhaustive;
|
|
2170
|
+
}
|
|
2043
2171
|
}
|
|
2044
2172
|
}
|
|
2045
|
-
return { content,
|
|
2173
|
+
return { content, omitted, relayed };
|
|
2046
2174
|
}
|
|
2047
2175
|
function argumentsOf2(args) {
|
|
2048
2176
|
return typeof args === "string" ? args : JSON.stringify(args ?? {});
|
|
@@ -2086,13 +2214,13 @@ function encodeResponsesRequest(input) {
|
|
|
2086
2214
|
for (const item of input.ir) {
|
|
2087
2215
|
switch (item.kind) {
|
|
2088
2216
|
case "user": {
|
|
2089
|
-
const c = contentOf(item.parts, capabilities
|
|
2217
|
+
const c = contentOf(item.parts, capabilities);
|
|
2090
2218
|
if (c.content.length === 0) {
|
|
2091
2219
|
land3(landings, item.event, "dropped", "none", EMPTY_NOTE2);
|
|
2092
2220
|
break;
|
|
2093
2221
|
}
|
|
2094
2222
|
items.push({ role: "user", content: c.content });
|
|
2095
|
-
const lossy4 = item.deferred || item.escaped || c.
|
|
2223
|
+
const lossy4 = item.deferred || item.escaped || c.omitted.size > 0;
|
|
2096
2224
|
land3(
|
|
2097
2225
|
landings,
|
|
2098
2226
|
item.event,
|
|
@@ -2100,12 +2228,13 @@ function encodeResponsesRequest(input) {
|
|
|
2100
2228
|
"user",
|
|
2101
2229
|
item.deferred ? `the user message sits between a tool call and its results; ${DEFERRED_NOTE}` : void 0,
|
|
2102
2230
|
item.escaped ? ESCAPED_NOTE : void 0,
|
|
2103
|
-
c.
|
|
2231
|
+
omittedNote(c.omitted),
|
|
2232
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0
|
|
2104
2233
|
);
|
|
2105
2234
|
break;
|
|
2106
2235
|
}
|
|
2107
2236
|
case "tool_result": {
|
|
2108
|
-
const c = contentOf(item.parts, capabilities
|
|
2237
|
+
const c = contentOf(item.parts, capabilities);
|
|
2109
2238
|
const hasImage = c.content.some((b) => b.type === "input_image");
|
|
2110
2239
|
const text = c.content.filter((b) => b.type === "input_text").map((b) => b.text).join("\n");
|
|
2111
2240
|
const prefixed = item.isError ? ERROR_PREFIX2 + text : text;
|
|
@@ -2114,14 +2243,15 @@ function encodeResponsesRequest(input) {
|
|
|
2114
2243
|
...c.content.filter((b) => b.type === "input_image")
|
|
2115
2244
|
] : prefixed;
|
|
2116
2245
|
items.push({ type: "function_call_output", call_id: item.toolCallId, output });
|
|
2117
|
-
const lossy4 = item.escaped || item.isError || c.
|
|
2246
|
+
const lossy4 = item.escaped || item.isError || c.omitted.size > 0;
|
|
2118
2247
|
land3(
|
|
2119
2248
|
landings,
|
|
2120
2249
|
item.event,
|
|
2121
2250
|
lossy4 ? "lossy" : "exact",
|
|
2122
2251
|
"function_call_output",
|
|
2123
2252
|
item.isError ? "the Responses function_call_output has no error flag, so isError is expressed with a [tool error] prefix" : void 0,
|
|
2124
|
-
c.
|
|
2253
|
+
omittedNote(c.omitted),
|
|
2254
|
+
c.relayed ? RELAY_DIALECT_NOTE : void 0,
|
|
2125
2255
|
item.escaped ? ESCAPED_NOTE : void 0
|
|
2126
2256
|
);
|
|
2127
2257
|
break;
|
|
@@ -2511,6 +2641,8 @@ function definitionOf(provider, id, api, opts) {
|
|
|
2511
2641
|
maxOutputTokens: opts.maxOutputTokens,
|
|
2512
2642
|
reasoning: opts.reasoning,
|
|
2513
2643
|
images: opts.images,
|
|
2644
|
+
video: opts.video,
|
|
2645
|
+
audio: opts.audio,
|
|
2514
2646
|
cost: opts.cost,
|
|
2515
2647
|
midConversationSystem: opts.midConversationSystem,
|
|
2516
2648
|
auth: opts.auth,
|
|
@@ -2536,11 +2668,12 @@ function chatCompletions(id, opts) {
|
|
|
2536
2668
|
return bound(opts.provider, id, definitionOf(opts.provider, id, "openai-chat", opts), opts);
|
|
2537
2669
|
}
|
|
2538
2670
|
function deepseek(id, opts) {
|
|
2671
|
+
const unknown = findBuiltin("deepseek", id, "openai-chat") === void 0;
|
|
2539
2672
|
return chatCompletions(id, {
|
|
2540
2673
|
provider: "deepseek",
|
|
2541
2674
|
baseUrl: "https://api.deepseek.com",
|
|
2542
2675
|
reasoning: true,
|
|
2543
|
-
images: true,
|
|
2676
|
+
...unknown ? { images: true } : {},
|
|
2544
2677
|
...opts,
|
|
2545
2678
|
chat: { reasoningContent: true, ...opts.chat }
|
|
2546
2679
|
});
|
|
@@ -2584,11 +2717,14 @@ var lossy3 = (landing, note, when) => when ? { kind: "lossy", landing, note, whe
|
|
|
2584
2717
|
var dropped3 = (landing, note, when) => when ? { kind: "dropped", landing, note, when } : { kind: "dropped", landing, note };
|
|
2585
2718
|
var RESPONSES_LOSS_MATRIX = {
|
|
2586
2719
|
"core.user_message": [
|
|
2587
|
-
exact3(
|
|
2720
|
+
exact3(
|
|
2721
|
+
"user",
|
|
2722
|
+
`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}`
|
|
2723
|
+
),
|
|
2588
2724
|
lossy3(
|
|
2589
2725
|
"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"
|
|
2726
|
+
"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",
|
|
2727
|
+
"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
2728
|
),
|
|
2593
2729
|
dropped3(
|
|
2594
2730
|
"none",
|
|
@@ -2623,11 +2759,11 @@ var RESPONSES_LOSS_MATRIX = {
|
|
|
2623
2759
|
"core.tool_result": [
|
|
2624
2760
|
exact3(
|
|
2625
2761
|
"function_call_output",
|
|
2626
|
-
|
|
2762
|
+
`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
2763
|
),
|
|
2628
2764
|
lossy3(
|
|
2629
2765
|
"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"
|
|
2766
|
+
"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
2767
|
)
|
|
2632
2768
|
],
|
|
2633
2769
|
"core.system_note": [
|