privateer-agent 0.15.4 → 0.15.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/bin/boot-bundle.d.mts +2 -0
- package/bin/boot-bundle.mjs +44 -0
- package/bin/fresh-supervisor.d.mts +5 -1
- package/bin/fresh-supervisor.mjs +47 -10
- package/bin/privateer-launch.mjs +27 -3
- package/bin/privateer-subagent.mjs +5 -2
- package/bin/privateer-verify.mjs +12 -0
- package/bin/session-recovery.d.mts +53 -0
- package/bin/session-recovery.mjs +240 -0
- package/bin/session-seal-preload.mjs +17 -0
- package/bin/session-seal.d.mts +25 -0
- package/bin/session-seal.mjs +242 -0
- package/dist-boot/boot.mjs +3 -0
- package/dist-boot/c-27JKSMWL.mjs +1460 -0
- package/dist-boot/c-2BMRFV24.mjs +269 -0
- package/dist-boot/c-2CTF6JA3.mjs +166 -0
- package/dist-boot/c-2G3BBHRJ.mjs +84 -0
- package/dist-boot/c-2IDLKXX6.mjs +250 -0
- package/dist-boot/c-2NJD5VYM.mjs +8 -0
- package/dist-boot/c-2S5EEQPW.mjs +1602 -0
- package/dist-boot/c-2WRDCUVI.mjs +25 -0
- package/dist-boot/c-2YN3M256.mjs +14 -0
- package/dist-boot/c-37GM3GMU.mjs +187 -0
- package/dist-boot/c-3STKRSVO.mjs +2480 -0
- package/dist-boot/c-3TPV6TQQ.mjs +2320 -0
- package/dist-boot/c-3UVHLQDJ.mjs +225 -0
- package/dist-boot/c-42PUUZYC.mjs +697 -0
- package/dist-boot/c-4D5UMWVW.mjs +683 -0
- package/dist-boot/c-4ETZRMTA.mjs +46 -0
- package/dist-boot/c-4QT25BD5.mjs +1482 -0
- package/dist-boot/c-4WVR32AP.mjs +2579 -0
- package/dist-boot/c-5EMM567G.mjs +152 -0
- package/dist-boot/c-5FA4CV63.mjs +145 -0
- package/dist-boot/c-5GV3X3RB.mjs +14 -0
- package/dist-boot/c-5JCEPRIY.mjs +52 -0
- package/dist-boot/c-5NDKIIWT.mjs +16 -0
- package/dist-boot/c-5QL3AR26.mjs +4665 -0
- package/dist-boot/c-5XY6MFKN.mjs +5142 -0
- package/dist-boot/c-65UZIZC7.mjs +2897 -0
- package/dist-boot/c-6FUXKHOK.mjs +1969 -0
- package/dist-boot/c-6JHWQE4V.mjs +18242 -0
- package/dist-boot/c-6LOBR4I6.mjs +5280 -0
- package/dist-boot/c-6QHOZXKQ.mjs +14394 -0
- package/dist-boot/c-6ZO2SADQ.mjs +178 -0
- package/dist-boot/c-6ZORVVZP.mjs +1449 -0
- package/dist-boot/c-76QH4UCP.mjs +2441 -0
- package/dist-boot/c-77GO5L5Y.mjs +20982 -0
- package/dist-boot/c-7M23XFTN.mjs +5052 -0
- package/dist-boot/c-7TC367E2.mjs +32 -0
- package/dist-boot/c-7UOG7POX.mjs +1524 -0
- package/dist-boot/c-7VHP3FLN.mjs +376 -0
- package/dist-boot/c-A73JHZP2.mjs +13207 -0
- package/dist-boot/c-ABXULAFL.mjs +71 -0
- package/dist-boot/c-ANDD2AQO.mjs +74 -0
- package/dist-boot/c-AOY77AZL.mjs +1430 -0
- package/dist-boot/c-AXEPZ22I.mjs +108 -0
- package/dist-boot/c-B2Y7VNHU.mjs +427 -0
- package/dist-boot/c-B7VJUH73.mjs +77 -0
- package/dist-boot/c-BFW6CDVL.mjs +24 -0
- package/dist-boot/c-BHNIF2AR.mjs +188 -0
- package/dist-boot/c-BKF2O6BL.mjs +23 -0
- package/dist-boot/c-BOZXFTZN.mjs +8 -0
- package/dist-boot/c-BRNYJWZL.mjs +28 -0
- package/dist-boot/c-C3N4Z2HO.mjs +93 -0
- package/dist-boot/c-C4IV7ZFG.mjs +717 -0
- package/dist-boot/c-C5NK6UYV.mjs +793 -0
- package/dist-boot/c-C67644MB.mjs +1659 -0
- package/dist-boot/c-C6S2YB53.mjs +620 -0
- package/dist-boot/c-CDBEJSBF.mjs +37 -0
- package/dist-boot/c-CG47HBKU.mjs +435 -0
- package/dist-boot/c-CKCTWCT5.mjs +51 -0
- package/dist-boot/c-CN77DCGF.mjs +60 -0
- package/dist-boot/c-CRBXNKFO.mjs +1795 -0
- package/dist-boot/c-CRIHASQ6.mjs +973 -0
- package/dist-boot/c-CU4NGAUO.mjs +3786 -0
- package/dist-boot/c-CWRFLGQU.mjs +1883 -0
- package/dist-boot/c-CYQB2USO.mjs +32 -0
- package/dist-boot/c-DEE67DJO.mjs +379 -0
- package/dist-boot/c-DIFDBYOP.mjs +1258 -0
- package/dist-boot/c-DOSGVXDZ.mjs +34879 -0
- package/dist-boot/c-DR7XQI32.mjs +381 -0
- package/dist-boot/c-DTGPSB46.mjs +14666 -0
- package/dist-boot/c-DTHDNA4P.mjs +320 -0
- package/dist-boot/c-E4Z3Y5B5.mjs +3864 -0
- package/dist-boot/c-E66Z2TI7.mjs +203 -0
- package/dist-boot/c-EBU6RLRV.mjs +1527 -0
- package/dist-boot/c-EBZZNGSF.mjs +432 -0
- package/dist-boot/c-EC3LJ2KF.mjs +1032 -0
- package/dist-boot/c-EQBKXWNX.mjs +15829 -0
- package/dist-boot/c-EUEPIW4S.mjs +574 -0
- package/dist-boot/c-EX4YVWAT.mjs +378 -0
- package/dist-boot/c-EYZGSAJU.mjs +98 -0
- package/dist-boot/c-F75VRKY4.mjs +172 -0
- package/dist-boot/c-FH4NXKHV.mjs +177 -0
- package/dist-boot/c-FHNUCTEM.mjs +140 -0
- package/dist-boot/c-FJ5T42DS.mjs +5349 -0
- package/dist-boot/c-FPCNRL7G.mjs +1728 -0
- package/dist-boot/c-G4DLN7Q4.mjs +8 -0
- package/dist-boot/c-G56GYQFZ.mjs +2507 -0
- package/dist-boot/c-GLIOERDQ.mjs +1200 -0
- package/dist-boot/c-GMJ5HUBM.mjs +14 -0
- package/dist-boot/c-H3HSGMAQ.mjs +461 -0
- package/dist-boot/c-HIKTO6P7.mjs +275 -0
- package/dist-boot/c-I2MCJ34O.mjs +464 -0
- package/dist-boot/c-I3C6LBW4.mjs +765 -0
- package/dist-boot/c-I54OKQHW.mjs +387 -0
- package/dist-boot/c-IFSP3R4D.mjs +80 -0
- package/dist-boot/c-IZK77VB7.mjs +171 -0
- package/dist-boot/c-J3Y25UGE.mjs +54 -0
- package/dist-boot/c-J5IESS7W.mjs +27750 -0
- package/dist-boot/c-JJWZBY7L.mjs +254 -0
- package/dist-boot/c-JKTFXTGF.mjs +463 -0
- package/dist-boot/c-JN57AXQ4.mjs +77 -0
- package/dist-boot/c-KFIO6LUW.mjs +1096 -0
- package/dist-boot/c-KIEMEFWW.mjs +478 -0
- package/dist-boot/c-KIVXWBYD.mjs +28 -0
- package/dist-boot/c-KP6CRIP4.mjs +580 -0
- package/dist-boot/c-L26VOJX4.mjs +11558 -0
- package/dist-boot/c-L5GHWG5G.mjs +5988 -0
- package/dist-boot/c-M7NSQAKK.mjs +353 -0
- package/dist-boot/c-MCEWOPBW.mjs +56 -0
- package/dist-boot/c-MGPIEG7I.mjs +20 -0
- package/dist-boot/c-NREXJV3M.mjs +393 -0
- package/dist-boot/c-O5PP6CTX.mjs +162 -0
- package/dist-boot/c-OBQDHSXM.mjs +1774 -0
- package/dist-boot/c-OLQHHDD4.mjs +21 -0
- package/dist-boot/c-OXW4JQQH.mjs +319 -0
- package/dist-boot/c-P3D7ZUWN.mjs +1288 -0
- package/dist-boot/c-P4PQZVV5.mjs +948 -0
- package/dist-boot/c-P63T3XL2.mjs +26373 -0
- package/dist-boot/c-P6N5DQGZ.mjs +194 -0
- package/dist-boot/c-P7MLKQFC.mjs +58 -0
- package/dist-boot/c-PB3PZJL5.mjs +1276 -0
- package/dist-boot/c-PTN4S6G2.mjs +1259 -0
- package/dist-boot/c-PYCE5J7U.mjs +1083 -0
- package/dist-boot/c-Q4HYCRLB.mjs +19410 -0
- package/dist-boot/c-QUXMETGG.mjs +68 -0
- package/dist-boot/c-RDMJZCOX.mjs +356 -0
- package/dist-boot/c-ROGJK76M.mjs +17 -0
- package/dist-boot/c-RQJJ26T4.mjs +708 -0
- package/dist-boot/c-RSKCUPJF.mjs +7559 -0
- package/dist-boot/c-RVJU4O6P.mjs +37 -0
- package/dist-boot/c-S2RVHR3M.mjs +165 -0
- package/dist-boot/c-SKMK3PEE.mjs +91120 -0
- package/dist-boot/c-TI7SM4EG.mjs +20 -0
- package/dist-boot/c-TILJ6GXD.mjs +1235 -0
- package/dist-boot/c-TT6BCUJD.mjs +114 -0
- package/dist-boot/c-U4ITOXAN.mjs +2107 -0
- package/dist-boot/c-UGERXVO2.mjs +7120 -0
- package/dist-boot/c-UPBH5AXY.mjs +1363 -0
- package/dist-boot/c-US2PUNZL.mjs +597 -0
- package/dist-boot/c-UU4WZPPE.mjs +20 -0
- package/dist-boot/c-UWVIYPQS.mjs +39 -0
- package/dist-boot/c-V4RHQBPQ.mjs +17683 -0
- package/dist-boot/c-V5MRVK47.mjs +351 -0
- package/dist-boot/c-V6CL266R.mjs +1755 -0
- package/dist-boot/c-VJTMFKYE.mjs +14975 -0
- package/dist-boot/c-VJTRUKIB.mjs +773 -0
- package/dist-boot/c-W4IFSHPW.mjs +43 -0
- package/dist-boot/c-WETGB2CF.mjs +113 -0
- package/dist-boot/c-WO3QSZUU.mjs +1337 -0
- package/dist-boot/c-WQLH73TY.mjs +242 -0
- package/dist-boot/c-XL735JW5.mjs +500 -0
- package/dist-boot/c-Y7HADL5B.mjs +1107 -0
- package/dist-boot/c-YGQHSVSK.mjs +41 -0
- package/dist-boot/c-YMP2AMEB.mjs +10350 -0
- package/dist-boot/c-YRDO2WEF.mjs +12012 -0
- package/dist-boot/c-YUSTRATJ.mjs +8 -0
- package/dist-boot/c-Z2262R22.mjs +2697 -0
- package/dist-boot/c-Z4ALQMJC.mjs +709 -0
- package/dist-boot/c-Z5ZUQVUY.mjs +62 -0
- package/dist-boot/c-ZDZUKXF7.mjs +874 -0
- package/dist-boot/c-ZJYLGIC3.mjs +32 -0
- package/dist-boot/c-ZNA5CDMI.mjs +77 -0
- package/dist-boot/c-ZSO6DI3M.mjs +204 -0
- package/dist-boot/c-ZWUVHKMK.mjs +100 -0
- package/dist-boot/c-ZY4MESEW.mjs +832 -0
- package/dist-boot/c-ZZ5REI4L.mjs +329 -0
- package/dist-boot/cli.mjs +51 -0
- package/dist-boot/ext-anthropic-attribution.mjs +207 -0
- package/dist-boot/ext-pi-background-tasks.mjs +7234 -0
- package/dist-boot/ext-pi-subagents.mjs +37 -0
- package/dist-boot/ext-privateer-account.mjs +4426 -0
- package/dist-boot/ext-privateer-brand.mjs +5274 -0
- package/dist-boot/ext-privateer-codemode.mjs +37 -0
- package/dist-boot/ext-privateer-computer.mjs +626 -0
- package/dist-boot/ext-privateer-connect.mjs +1862 -0
- package/dist-boot/ext-privateer-context.mjs +323 -0
- package/dist-boot/ext-privateer-desktop.mjs +92 -0
- package/dist-boot/ext-privateer-final-audit.mjs +48 -0
- package/dist-boot/ext-privateer-fresh.mjs +81 -0
- package/dist-boot/ext-privateer-gate.mjs +5959 -0
- package/dist-boot/ext-privateer-hints.mjs +236 -0
- package/dist-boot/ext-privateer-media.mjs +2626 -0
- package/dist-boot/ext-privateer-models.mjs +3397 -0
- package/dist-boot/ext-privateer-posture.mjs +3006 -0
- package/dist-boot/ext-privateer-privacy.mjs +4578 -0
- package/dist-boot/ext-privateer-recovery.mjs +287 -0
- package/dist-boot/ext-privateer-speak.mjs +4754 -0
- package/dist-boot/ext-privateer-terminals.mjs +725 -0
- package/dist-boot/ext-privateer-tools.mjs +528 -0
- package/dist-boot/ext-privateer-truncation.mjs +74 -0
- package/dist-boot/ext-privateer-update.mjs +210 -0
- package/dist-boot/ext-privateer-vision.mjs +148 -0
- package/dist-boot/ext-privateer-web.mjs +543 -0
- package/dist-boot/ext-rpiv-ask-user-question.mjs +710 -0
- package/dist-boot/manifest.json +9 -0
- package/extensions/privateer-brand.ts +3 -1
- package/extensions/privateer-context.ts +4 -2
- package/extensions/privateer-final-audit.ts +17 -0
- package/extensions/privateer-gate.ts +55 -9
- package/extensions/privateer-recovery.ts +175 -0
- package/extensions/privateer-web.ts +4 -4
- package/package.json +3 -1
- package/patches/@earendil-works+pi-coding-agent+1.3.0.patch +199 -2
- package/patches/pi-background-tasks+2.6.10.patch +91 -7
- package/src/cli/chat.ts +7 -1
- package/src/config/moatManifest.json +2 -0
- package/src/config/whatsNew.json +4 -4
- package/src/context.ts +33 -5
- package/src/engine/finalAudit.ts +38 -0
- package/src/engine/stallWatchdog.ts +8 -1
- package/src/providers/account.ts +34 -3
- package/src/remote/attentionPush.ts +40 -0
- package/src/remote/liveTaskSession.ts +7 -1
- package/src/remote/relayClient.ts +38 -4
- package/src/remote/remoteBridge.ts +17 -0
- package/src/remote/transcriptSnapshot.ts +104 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
import{createRequire as __pvCR}from"node:module";import{fileURLToPath as __pvF}from"node:url";import{dirname as __pvD}from"node:path";const require=__pvCR(import.meta.url);const __pvUrl=(r)=>new URL(r,import.meta.url).href;const __pvFile=(r)=>__pvF(new URL(r,import.meta.url));
|
|
2
|
+
import {
|
|
3
|
+
shortHash
|
|
4
|
+
} from "./c-BKF2O6BL.mjs";
|
|
5
|
+
import {
|
|
6
|
+
appendGrammarToolInputJsonDelta,
|
|
7
|
+
getGrammarToolInput,
|
|
8
|
+
getJsonSchemaToolParameters,
|
|
9
|
+
resolveGrammarConstrainedSampling,
|
|
10
|
+
resolveJsonSchemaStrictSampling,
|
|
11
|
+
transformMessages
|
|
12
|
+
} from "./c-C6S2YB53.mjs";
|
|
13
|
+
import {
|
|
14
|
+
sanitizeSurrogates
|
|
15
|
+
} from "./c-5GV3X3RB.mjs";
|
|
16
|
+
import {
|
|
17
|
+
parseStreamingJson,
|
|
18
|
+
parseToolCallArguments
|
|
19
|
+
} from "./c-DEE67DJO.mjs";
|
|
20
|
+
import {
|
|
21
|
+
calculateCost,
|
|
22
|
+
getSystemMessageText,
|
|
23
|
+
renderSystemMessageUpdate,
|
|
24
|
+
resolveTranscript,
|
|
25
|
+
resolveTranscriptTools
|
|
26
|
+
} from "./c-WO3QSZUU.mjs";
|
|
27
|
+
import {
|
|
28
|
+
__name
|
|
29
|
+
} from "./c-CN77DCGF.mjs";
|
|
30
|
+
|
|
31
|
+
// node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
|
|
32
|
+
function encodeTextSignatureV1(id, phase) {
|
|
33
|
+
const payload = { v: 1, id };
|
|
34
|
+
if (phase)
|
|
35
|
+
payload.phase = phase;
|
|
36
|
+
return JSON.stringify(payload);
|
|
37
|
+
}
|
|
38
|
+
__name(encodeTextSignatureV1, "encodeTextSignatureV1");
|
|
39
|
+
function parseTextSignature(signature) {
|
|
40
|
+
if (!signature)
|
|
41
|
+
return void 0;
|
|
42
|
+
if (signature.startsWith("{")) {
|
|
43
|
+
try {
|
|
44
|
+
const parsed = JSON.parse(signature);
|
|
45
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
46
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") {
|
|
47
|
+
return { id: parsed.id, phase: parsed.phase };
|
|
48
|
+
}
|
|
49
|
+
return { id: parsed.id };
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { id: signature };
|
|
55
|
+
}
|
|
56
|
+
__name(parseTextSignature, "parseTextSignature");
|
|
57
|
+
function convertToolResultOutput(model, content) {
|
|
58
|
+
const textResult = content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
59
|
+
const images = content.filter((c) => c.type === "image");
|
|
60
|
+
const hasText = textResult.length > 0;
|
|
61
|
+
if (images.length === 0 || !model.input.includes("image")) {
|
|
62
|
+
return sanitizeSurrogates(hasText ? textResult : images.length > 0 ? "(see attached image)" : "(no tool output)");
|
|
63
|
+
}
|
|
64
|
+
const output = [];
|
|
65
|
+
if (hasText) {
|
|
66
|
+
output.push({ type: "input_text", text: sanitizeSurrogates(textResult) });
|
|
67
|
+
}
|
|
68
|
+
for (const image of images) {
|
|
69
|
+
output.push({
|
|
70
|
+
type: "input_image",
|
|
71
|
+
detail: "auto",
|
|
72
|
+
image_url: `data:${image.mimeType};base64,${image.data}`
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return output;
|
|
76
|
+
}
|
|
77
|
+
__name(convertToolResultOutput, "convertToolResultOutput");
|
|
78
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
79
|
+
const normalizedContext = resolveTranscript(context, options?.supportsMidConvoSystemMessages);
|
|
80
|
+
const messages = [];
|
|
81
|
+
const normalizeIdPart = /* @__PURE__ */ __name((part) => {
|
|
82
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
83
|
+
const normalized = sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
|
84
|
+
return normalized.replace(/_+$/, "");
|
|
85
|
+
}, "normalizeIdPart");
|
|
86
|
+
const buildForeignResponsesItemId = /* @__PURE__ */ __name((itemId) => {
|
|
87
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
88
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
89
|
+
}, "buildForeignResponsesItemId");
|
|
90
|
+
const normalizeToolCallId = /* @__PURE__ */ __name((id, _targetModel, source) => {
|
|
91
|
+
if (!allowedToolCallProviders.has(model.provider))
|
|
92
|
+
return normalizeIdPart(id);
|
|
93
|
+
if (!id.includes("|"))
|
|
94
|
+
return normalizeIdPart(id);
|
|
95
|
+
const [callId, itemId] = id.split("|");
|
|
96
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
97
|
+
const isForeignToolCall = source.provider !== model.provider || source.api !== model.api;
|
|
98
|
+
let normalizedItemId = isForeignToolCall ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
99
|
+
if (!normalizedItemId.startsWith("fc_")) {
|
|
100
|
+
normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
101
|
+
}
|
|
102
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
103
|
+
}, "normalizeToolCallId");
|
|
104
|
+
const transformedMessages = transformMessages(normalizedContext.messages, model, normalizeToolCallId);
|
|
105
|
+
const transcriptTools = resolveTranscriptTools(normalizedContext.messages, (options?.supportsAdditionalTools ?? false) || (options?.supportsToolSearch ?? false));
|
|
106
|
+
const appendSystemToolAdditions = /* @__PURE__ */ __name((message, seed) => {
|
|
107
|
+
const tools = transcriptTools.anchorsAdditions ? message.toolsAdded ?? [] : [];
|
|
108
|
+
if (tools.length === 0)
|
|
109
|
+
return;
|
|
110
|
+
if (options?.supportsAdditionalTools) {
|
|
111
|
+
messages.push({
|
|
112
|
+
type: "additional_tools",
|
|
113
|
+
role: "developer",
|
|
114
|
+
tools: convertResponsesTools(tools, options.toolOptions)
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!options?.supportsToolSearch)
|
|
119
|
+
return;
|
|
120
|
+
const names = tools.map((tool) => tool.name);
|
|
121
|
+
const callId = `pi_tool_load_${shortHash(`${seed}:${names.join(",")}`)}`;
|
|
122
|
+
messages.push({
|
|
123
|
+
type: "tool_search_call",
|
|
124
|
+
call_id: callId,
|
|
125
|
+
execution: "client",
|
|
126
|
+
status: "completed",
|
|
127
|
+
arguments: { query: names.join(" "), limit: names.length }
|
|
128
|
+
});
|
|
129
|
+
messages.push({
|
|
130
|
+
type: "tool_search_output",
|
|
131
|
+
call_id: callId,
|
|
132
|
+
execution: "client",
|
|
133
|
+
status: "completed",
|
|
134
|
+
tools: convertResponsesTools(tools, { ...options.toolOptions, toolSearchResult: true })
|
|
135
|
+
});
|
|
136
|
+
}, "appendSystemToolAdditions");
|
|
137
|
+
const includeInitialSystemMessage = options?.includeSystemPrompt ?? true;
|
|
138
|
+
const pushConfigurationUpdate = /* @__PURE__ */ __name((effort) => {
|
|
139
|
+
messages.push({
|
|
140
|
+
type: "configuration_update",
|
|
141
|
+
reasoning: { effort }
|
|
142
|
+
});
|
|
143
|
+
}, "pushConfigurationUpdate");
|
|
144
|
+
const compat = model.compat;
|
|
145
|
+
const instructionRole = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
146
|
+
let msgIndex = 0;
|
|
147
|
+
let sourceIndex = 0;
|
|
148
|
+
for (const msg of transformedMessages) {
|
|
149
|
+
const isLeadingSystemMessage = sourceIndex++ === 0 && msg.role === "system";
|
|
150
|
+
if (msg.role === "system") {
|
|
151
|
+
if (!isLeadingSystemMessage)
|
|
152
|
+
appendSystemToolAdditions(msg, `system:${msgIndex}`);
|
|
153
|
+
if (!isLeadingSystemMessage || includeInitialSystemMessage) {
|
|
154
|
+
const text = isLeadingSystemMessage ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
155
|
+
if (text.length > 0) {
|
|
156
|
+
messages.push({ role: instructionRole, content: sanitizeSurrogates(text) });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} else if (msg.role === "user") {
|
|
160
|
+
if (typeof msg.content === "string") {
|
|
161
|
+
messages.push({
|
|
162
|
+
role: "user",
|
|
163
|
+
content: [{ type: "input_text", text: sanitizeSurrogates(msg.content) }]
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
const content = msg.content.map((item) => {
|
|
167
|
+
if (item.type === "text") {
|
|
168
|
+
return {
|
|
169
|
+
type: "input_text",
|
|
170
|
+
text: sanitizeSurrogates(item.text)
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
type: "input_image",
|
|
175
|
+
detail: "auto",
|
|
176
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
if (content.length === 0)
|
|
180
|
+
continue;
|
|
181
|
+
messages.push({
|
|
182
|
+
role: "user",
|
|
183
|
+
content
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
} else if (msg.role === "assistant") {
|
|
187
|
+
const output = [];
|
|
188
|
+
const assistantMsg = msg;
|
|
189
|
+
const isSameProviderAndApi = assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
190
|
+
const isSameModel = isSameProviderAndApi && assistantMsg.model === model.id;
|
|
191
|
+
const isDifferentModel = isSameProviderAndApi && assistantMsg.model !== model.id;
|
|
192
|
+
let textBlockIndex = 0;
|
|
193
|
+
for (const block of msg.content) {
|
|
194
|
+
if (block.type === "thinking") {
|
|
195
|
+
if (block.thinkingSignature) {
|
|
196
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
197
|
+
output.push(reasoningItem);
|
|
198
|
+
}
|
|
199
|
+
} else if (block.type === "text") {
|
|
200
|
+
const textBlock = block;
|
|
201
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
202
|
+
const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
|
|
203
|
+
textBlockIndex++;
|
|
204
|
+
let msgId = parsedSignature?.id;
|
|
205
|
+
if (!msgId) {
|
|
206
|
+
msgId = fallbackMessageId;
|
|
207
|
+
} else if (msgId.length > 64) {
|
|
208
|
+
msgId = `msg_${shortHash(msgId)}`;
|
|
209
|
+
}
|
|
210
|
+
output.push({
|
|
211
|
+
type: "message",
|
|
212
|
+
role: "assistant",
|
|
213
|
+
content: [{ type: "output_text", text: sanitizeSurrogates(textBlock.text), annotations: [] }],
|
|
214
|
+
status: "completed",
|
|
215
|
+
id: msgId,
|
|
216
|
+
phase: parsedSignature?.phase
|
|
217
|
+
});
|
|
218
|
+
} else if (block.type === "toolCall") {
|
|
219
|
+
const toolCall = block;
|
|
220
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
221
|
+
const customInputProperty = options?.grammarToolInputProperties?.get(toolCall.name);
|
|
222
|
+
let itemId = itemIdRaw;
|
|
223
|
+
const itemIdPrefix = customInputProperty === void 0 ? "fc_" : "ctc_";
|
|
224
|
+
if (isDifferentModel || !itemId?.startsWith(itemIdPrefix)) {
|
|
225
|
+
itemId = void 0;
|
|
226
|
+
}
|
|
227
|
+
if (customInputProperty !== void 0) {
|
|
228
|
+
output.push({
|
|
229
|
+
type: "custom_tool_call",
|
|
230
|
+
id: itemId,
|
|
231
|
+
call_id: callId,
|
|
232
|
+
name: toolCall.name,
|
|
233
|
+
input: sanitizeSurrogates(getGrammarToolInput(toolCall.name, toolCall.arguments, customInputProperty)),
|
|
234
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
235
|
+
});
|
|
236
|
+
} else {
|
|
237
|
+
output.push({
|
|
238
|
+
type: "function_call",
|
|
239
|
+
id: itemId,
|
|
240
|
+
call_id: callId,
|
|
241
|
+
name: toolCall.name,
|
|
242
|
+
arguments: JSON.stringify(toolCall.arguments),
|
|
243
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (output.length === 0)
|
|
249
|
+
continue;
|
|
250
|
+
if (options?.configurationUpdateEffort !== void 0 && isSameModel && msg.providerThinkingLevel !== void 0) {
|
|
251
|
+
pushConfigurationUpdate(msg.providerThinkingLevel);
|
|
252
|
+
}
|
|
253
|
+
messages.push(...output);
|
|
254
|
+
} else if (msg.role === "toolResult") {
|
|
255
|
+
const [callId] = msg.toolCallId.split("|");
|
|
256
|
+
const output = convertToolResultOutput(model, msg.content);
|
|
257
|
+
if (options?.grammarToolInputProperties?.has(msg.toolName)) {
|
|
258
|
+
messages.push({
|
|
259
|
+
type: "custom_tool_call_output",
|
|
260
|
+
call_id: callId,
|
|
261
|
+
output
|
|
262
|
+
});
|
|
263
|
+
} else {
|
|
264
|
+
messages.push({
|
|
265
|
+
type: "function_call_output",
|
|
266
|
+
call_id: callId,
|
|
267
|
+
output
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (!isLeadingSystemMessage)
|
|
272
|
+
msgIndex++;
|
|
273
|
+
}
|
|
274
|
+
if (options?.configurationUpdateEffort !== void 0)
|
|
275
|
+
pushConfigurationUpdate(options.configurationUpdateEffort);
|
|
276
|
+
return messages;
|
|
277
|
+
}
|
|
278
|
+
__name(convertResponsesMessages, "convertResponsesMessages");
|
|
279
|
+
function convertResponsesTools(tools, options) {
|
|
280
|
+
const defaultStrict = options?.strict === void 0 ? false : options.strict;
|
|
281
|
+
const supportsStrictMode = options?.supportsStrictMode ?? true;
|
|
282
|
+
const supportsOpenAIGrammarTools = options?.supportsOpenAIGrammarTools ?? false;
|
|
283
|
+
return tools.map((tool) => {
|
|
284
|
+
const grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);
|
|
285
|
+
if (grammar) {
|
|
286
|
+
return {
|
|
287
|
+
type: "custom",
|
|
288
|
+
name: tool.name,
|
|
289
|
+
description: tool.description,
|
|
290
|
+
format: {
|
|
291
|
+
type: "grammar",
|
|
292
|
+
syntax: grammar.format,
|
|
293
|
+
definition: grammar.definition
|
|
294
|
+
},
|
|
295
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
const constrainedStrict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode);
|
|
299
|
+
const strict = constrainedStrict ?? defaultStrict;
|
|
300
|
+
const functionTool = {
|
|
301
|
+
type: "function",
|
|
302
|
+
name: tool.name,
|
|
303
|
+
description: tool.description,
|
|
304
|
+
parameters: getJsonSchemaToolParameters(tool, strict === true),
|
|
305
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
306
|
+
};
|
|
307
|
+
if (supportsStrictMode) {
|
|
308
|
+
functionTool.strict = strict;
|
|
309
|
+
}
|
|
310
|
+
return functionTool;
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
__name(convertResponsesTools, "convertResponsesTools");
|
|
314
|
+
function getCustomToolCallInput(block) {
|
|
315
|
+
const property = block.customInput?.property;
|
|
316
|
+
if (property === void 0)
|
|
317
|
+
return "";
|
|
318
|
+
const value = block.arguments[property];
|
|
319
|
+
return typeof value === "string" ? value : "";
|
|
320
|
+
}
|
|
321
|
+
__name(getCustomToolCallInput, "getCustomToolCallInput");
|
|
322
|
+
function appendCustomToolCallInput(block, nextInput, close) {
|
|
323
|
+
const customInput = block.customInput;
|
|
324
|
+
if (!customInput)
|
|
325
|
+
return void 0;
|
|
326
|
+
const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
|
|
327
|
+
block.arguments = { [customInput.property]: nextInput };
|
|
328
|
+
return delta;
|
|
329
|
+
}
|
|
330
|
+
__name(appendCustomToolCallInput, "appendCustomToolCallInput");
|
|
331
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
332
|
+
let sawTerminalResponseEvent = false;
|
|
333
|
+
const outputSlots = /* @__PURE__ */ new Map();
|
|
334
|
+
const reasoningBlocksById = /* @__PURE__ */ new Map();
|
|
335
|
+
const applyMessagePhaseStopReason = /* @__PURE__ */ __name((item) => {
|
|
336
|
+
if (item.type === "message" && item.phase === "final_answer") {
|
|
337
|
+
output.stopReason = "stop";
|
|
338
|
+
}
|
|
339
|
+
}, "applyMessagePhaseStopReason");
|
|
340
|
+
const getSlot = /* @__PURE__ */ __name((outputIndex, type) => {
|
|
341
|
+
const slot = outputSlots.get(outputIndex);
|
|
342
|
+
return slot?.type === type ? slot : void 0;
|
|
343
|
+
}, "getSlot");
|
|
344
|
+
const pushToolCallDelta = /* @__PURE__ */ __name((slot, delta) => {
|
|
345
|
+
if (delta === void 0)
|
|
346
|
+
return;
|
|
347
|
+
stream.push({
|
|
348
|
+
type: "toolcall_delta",
|
|
349
|
+
contentIndex: slot.contentIndex,
|
|
350
|
+
delta,
|
|
351
|
+
partial: output
|
|
352
|
+
});
|
|
353
|
+
}, "pushToolCallDelta");
|
|
354
|
+
const createSlot = /* @__PURE__ */ __name((outputIndex, item) => {
|
|
355
|
+
if (item.type === "reasoning") {
|
|
356
|
+
const block = { type: "thinking", thinking: "" };
|
|
357
|
+
output.content.push(block);
|
|
358
|
+
const slot = {
|
|
359
|
+
type: "thinking",
|
|
360
|
+
block,
|
|
361
|
+
contentIndex: output.content.length - 1
|
|
362
|
+
};
|
|
363
|
+
outputSlots.set(outputIndex, slot);
|
|
364
|
+
stream.push({ type: "thinking_start", contentIndex: slot.contentIndex, partial: output });
|
|
365
|
+
return slot;
|
|
366
|
+
}
|
|
367
|
+
if (item.type === "message") {
|
|
368
|
+
applyMessagePhaseStopReason(item);
|
|
369
|
+
const block = { type: "text", text: "" };
|
|
370
|
+
output.content.push(block);
|
|
371
|
+
const slot = { type: "text", block, contentIndex: output.content.length - 1 };
|
|
372
|
+
outputSlots.set(outputIndex, slot);
|
|
373
|
+
stream.push({ type: "text_start", contentIndex: slot.contentIndex, partial: output });
|
|
374
|
+
return slot;
|
|
375
|
+
}
|
|
376
|
+
if (item.type === "function_call") {
|
|
377
|
+
const block = {
|
|
378
|
+
type: "toolCall",
|
|
379
|
+
id: `${item.call_id}|${item.id}`,
|
|
380
|
+
name: item.name,
|
|
381
|
+
arguments: {},
|
|
382
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
383
|
+
partialJson: item.arguments || ""
|
|
384
|
+
};
|
|
385
|
+
output.content.push(block);
|
|
386
|
+
const slot = {
|
|
387
|
+
type: "toolCall",
|
|
388
|
+
block,
|
|
389
|
+
contentIndex: output.content.length - 1
|
|
390
|
+
};
|
|
391
|
+
outputSlots.set(outputIndex, slot);
|
|
392
|
+
stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
|
|
393
|
+
return slot;
|
|
394
|
+
}
|
|
395
|
+
if (item.type === "custom_tool_call") {
|
|
396
|
+
const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
|
|
397
|
+
const input = item.input || "";
|
|
398
|
+
const block = {
|
|
399
|
+
type: "toolCall",
|
|
400
|
+
id: `${item.call_id}|${item.id}`,
|
|
401
|
+
name: item.name,
|
|
402
|
+
arguments: { [inputProperty]: input },
|
|
403
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
404
|
+
customInput: {
|
|
405
|
+
property: inputProperty,
|
|
406
|
+
jsonBuffer: { input: "", started: false, closed: false }
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
output.content.push(block);
|
|
410
|
+
const slot = {
|
|
411
|
+
type: "toolCall",
|
|
412
|
+
block,
|
|
413
|
+
contentIndex: output.content.length - 1
|
|
414
|
+
};
|
|
415
|
+
outputSlots.set(outputIndex, slot);
|
|
416
|
+
stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
|
|
417
|
+
return slot;
|
|
418
|
+
}
|
|
419
|
+
return void 0;
|
|
420
|
+
}, "createSlot");
|
|
421
|
+
const getOrCreateSlot = /* @__PURE__ */ __name((outputIndex, item) => {
|
|
422
|
+
return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
|
|
423
|
+
}, "getOrCreateSlot");
|
|
424
|
+
const backfillReasoningSignatures = /* @__PURE__ */ __name((responseOutput) => {
|
|
425
|
+
for (const item of responseOutput) {
|
|
426
|
+
if (item.type !== "reasoning" || !item.encrypted_content)
|
|
427
|
+
continue;
|
|
428
|
+
const block = reasoningBlocksById.get(item.id);
|
|
429
|
+
if (!block?.thinkingSignature)
|
|
430
|
+
continue;
|
|
431
|
+
const storedItem = JSON.parse(block.thinkingSignature);
|
|
432
|
+
if (storedItem.encrypted_content)
|
|
433
|
+
continue;
|
|
434
|
+
block.thinkingSignature = JSON.stringify({
|
|
435
|
+
...storedItem,
|
|
436
|
+
encrypted_content: item.encrypted_content
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}, "backfillReasoningSignatures");
|
|
440
|
+
const finalizeResponse = /* @__PURE__ */ __name((response) => {
|
|
441
|
+
sawTerminalResponseEvent = true;
|
|
442
|
+
backfillReasoningSignatures(response.output ?? []);
|
|
443
|
+
if (response?.id) {
|
|
444
|
+
output.responseId = response.id;
|
|
445
|
+
}
|
|
446
|
+
if (response?.usage) {
|
|
447
|
+
const inputDetails = response.usage.input_tokens_details;
|
|
448
|
+
const cachedTokens = inputDetails?.cached_tokens || 0;
|
|
449
|
+
const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
|
|
450
|
+
output.usage = {
|
|
451
|
+
// OpenAI includes cached and cache-write tokens in input_tokens, so subtract both.
|
|
452
|
+
input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
|
|
453
|
+
output: response.usage.output_tokens || 0,
|
|
454
|
+
cacheRead: cachedTokens,
|
|
455
|
+
cacheWrite: cacheWriteTokens,
|
|
456
|
+
reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
|
|
457
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
458
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
calculateCost(model, output.usage);
|
|
462
|
+
if (options?.applyServiceTierPricing) {
|
|
463
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
|
|
464
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
465
|
+
}
|
|
466
|
+
const status = response?.status;
|
|
467
|
+
const incompleteDetails = response?.incomplete_details;
|
|
468
|
+
const incompleteReason = typeof incompleteDetails?.reason === "string" ? incompleteDetails.reason : void 0;
|
|
469
|
+
output.rawStopReason = incompleteReason ? `${status}.${incompleteReason}` : status;
|
|
470
|
+
const mappedStop = mapStopReason(status, incompleteReason);
|
|
471
|
+
output.stopReason = mappedStop.stopReason;
|
|
472
|
+
if (mappedStop.errorMessage === void 0)
|
|
473
|
+
delete output.errorMessage;
|
|
474
|
+
else
|
|
475
|
+
output.errorMessage = mappedStop.errorMessage;
|
|
476
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
|
477
|
+
output.stopReason = "toolUse";
|
|
478
|
+
}
|
|
479
|
+
}, "finalizeResponse");
|
|
480
|
+
for await (const event of openaiStream) {
|
|
481
|
+
await options?.onProviderStreamEvent?.(event, model);
|
|
482
|
+
if (event.type === "response.created") {
|
|
483
|
+
output.responseId = event.response.id;
|
|
484
|
+
} else if (event.type === "response.output_item.added") {
|
|
485
|
+
createSlot(event.output_index, event.item);
|
|
486
|
+
} else if (event.type === "response.reasoning_summary_text.delta") {
|
|
487
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
488
|
+
if (!slot)
|
|
489
|
+
continue;
|
|
490
|
+
slot.block.thinking += event.delta;
|
|
491
|
+
stream.push({
|
|
492
|
+
type: "thinking_delta",
|
|
493
|
+
contentIndex: slot.contentIndex,
|
|
494
|
+
delta: event.delta,
|
|
495
|
+
partial: output
|
|
496
|
+
});
|
|
497
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
498
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
499
|
+
if (!slot)
|
|
500
|
+
continue;
|
|
501
|
+
slot.block.thinking += "\n\n";
|
|
502
|
+
stream.push({
|
|
503
|
+
type: "thinking_delta",
|
|
504
|
+
contentIndex: slot.contentIndex,
|
|
505
|
+
delta: "\n\n",
|
|
506
|
+
partial: output
|
|
507
|
+
});
|
|
508
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
509
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
510
|
+
if (!slot)
|
|
511
|
+
continue;
|
|
512
|
+
slot.block.thinking += event.delta;
|
|
513
|
+
stream.push({
|
|
514
|
+
type: "thinking_delta",
|
|
515
|
+
contentIndex: slot.contentIndex,
|
|
516
|
+
delta: event.delta,
|
|
517
|
+
partial: output
|
|
518
|
+
});
|
|
519
|
+
} else if (event.type === "response.output_text.delta") {
|
|
520
|
+
const slot = getSlot(event.output_index, "text");
|
|
521
|
+
if (!slot)
|
|
522
|
+
continue;
|
|
523
|
+
slot.block.text += event.delta;
|
|
524
|
+
stream.push({
|
|
525
|
+
type: "text_delta",
|
|
526
|
+
contentIndex: slot.contentIndex,
|
|
527
|
+
delta: event.delta,
|
|
528
|
+
partial: output
|
|
529
|
+
});
|
|
530
|
+
} else if (event.type === "response.refusal.delta") {
|
|
531
|
+
const slot = getSlot(event.output_index, "text");
|
|
532
|
+
if (!slot)
|
|
533
|
+
continue;
|
|
534
|
+
slot.block.text += event.delta;
|
|
535
|
+
stream.push({
|
|
536
|
+
type: "text_delta",
|
|
537
|
+
contentIndex: slot.contentIndex,
|
|
538
|
+
delta: event.delta,
|
|
539
|
+
partial: output
|
|
540
|
+
});
|
|
541
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
542
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
543
|
+
if (!slot || slot.block.partialJson === void 0)
|
|
544
|
+
continue;
|
|
545
|
+
slot.block.partialJson += event.delta;
|
|
546
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
547
|
+
pushToolCallDelta(slot, event.delta);
|
|
548
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
549
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
550
|
+
if (!slot || slot.block.partialJson === void 0)
|
|
551
|
+
continue;
|
|
552
|
+
const previousPartialJson = slot.block.partialJson;
|
|
553
|
+
slot.block.partialJson = event.arguments;
|
|
554
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
555
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
556
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
557
|
+
if (delta.length > 0)
|
|
558
|
+
pushToolCallDelta(slot, delta);
|
|
559
|
+
}
|
|
560
|
+
} else if (event.type === "response.custom_tool_call_input.delta") {
|
|
561
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
562
|
+
if (!slot || !slot.block.customInput)
|
|
563
|
+
continue;
|
|
564
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, getCustomToolCallInput(slot.block) + event.delta, false));
|
|
565
|
+
} else if (event.type === "response.custom_tool_call_input.done") {
|
|
566
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
567
|
+
if (!slot || !slot.block.customInput)
|
|
568
|
+
continue;
|
|
569
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
|
|
570
|
+
} else if (event.type === "response.output_item.done") {
|
|
571
|
+
const item = event.item;
|
|
572
|
+
applyMessagePhaseStopReason(item);
|
|
573
|
+
const slot = getOrCreateSlot(event.output_index, item);
|
|
574
|
+
if (item.type === "reasoning" && slot?.type === "thinking") {
|
|
575
|
+
const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
576
|
+
const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
|
|
577
|
+
slot.block.thinking = summaryText || contentText || slot.block.thinking;
|
|
578
|
+
slot.block.thinkingSignature = JSON.stringify(item);
|
|
579
|
+
reasoningBlocksById.set(item.id, slot.block);
|
|
580
|
+
stream.push({
|
|
581
|
+
type: "thinking_end",
|
|
582
|
+
contentIndex: slot.contentIndex,
|
|
583
|
+
content: slot.block.thinking,
|
|
584
|
+
partial: output
|
|
585
|
+
});
|
|
586
|
+
outputSlots.delete(event.output_index);
|
|
587
|
+
} else if (item.type === "message" && slot?.type === "text") {
|
|
588
|
+
slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
589
|
+
slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
590
|
+
stream.push({
|
|
591
|
+
type: "text_end",
|
|
592
|
+
contentIndex: slot.contentIndex,
|
|
593
|
+
content: slot.block.text,
|
|
594
|
+
partial: output
|
|
595
|
+
});
|
|
596
|
+
outputSlots.delete(event.output_index);
|
|
597
|
+
} else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== void 0) {
|
|
598
|
+
Object.assign(slot.block, parseToolCallArguments(item.arguments || slot.block.partialJson));
|
|
599
|
+
if (item.namespace !== void 0)
|
|
600
|
+
slot.block.namespace = item.namespace;
|
|
601
|
+
delete slot.block.partialJson;
|
|
602
|
+
stream.push({
|
|
603
|
+
type: "toolcall_end",
|
|
604
|
+
contentIndex: slot.contentIndex,
|
|
605
|
+
toolCall: slot.block,
|
|
606
|
+
partial: output
|
|
607
|
+
});
|
|
608
|
+
outputSlots.delete(event.output_index);
|
|
609
|
+
} else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
|
|
610
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? getCustomToolCallInput(slot.block), true));
|
|
611
|
+
if (item.namespace !== void 0)
|
|
612
|
+
slot.block.namespace = item.namespace;
|
|
613
|
+
delete slot.block.customInput;
|
|
614
|
+
stream.push({
|
|
615
|
+
type: "toolcall_end",
|
|
616
|
+
contentIndex: slot.contentIndex,
|
|
617
|
+
toolCall: slot.block,
|
|
618
|
+
partial: output
|
|
619
|
+
});
|
|
620
|
+
outputSlots.delete(event.output_index);
|
|
621
|
+
}
|
|
622
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") {
|
|
623
|
+
finalizeResponse(event.response);
|
|
624
|
+
} else if (event.type === "error") {
|
|
625
|
+
throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
626
|
+
} else if (event.type === "response.failed") {
|
|
627
|
+
sawTerminalResponseEvent = true;
|
|
628
|
+
output.rawStopReason = event.response?.status;
|
|
629
|
+
const error = event.response?.error;
|
|
630
|
+
const details = event.response?.incomplete_details;
|
|
631
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
632
|
+
throw new Error(msg);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (!sawTerminalResponseEvent) {
|
|
636
|
+
throw new Error("OpenAI Responses stream ended before a terminal response event");
|
|
637
|
+
}
|
|
638
|
+
if (output.stopReason === "toolUse") {
|
|
639
|
+
for (const block of output.content) {
|
|
640
|
+
if (block.type !== "toolCall")
|
|
641
|
+
continue;
|
|
642
|
+
const toolCall = block;
|
|
643
|
+
if (toolCall.partialJson !== void 0 || toolCall.customInput !== void 0) {
|
|
644
|
+
throw new Error(`OpenAI Responses stream completed with an unfinished tool call: ${toolCall.name} (${toolCall.id})`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
__name(processResponsesStream, "processResponsesStream");
|
|
650
|
+
function mapStopReason(status, incompleteReason) {
|
|
651
|
+
if (!status)
|
|
652
|
+
return { stopReason: "stop" };
|
|
653
|
+
switch (status) {
|
|
654
|
+
case "completed":
|
|
655
|
+
return { stopReason: "stop" };
|
|
656
|
+
case "incomplete":
|
|
657
|
+
if (incompleteReason === "max_output_tokens") {
|
|
658
|
+
return { stopReason: "length" };
|
|
659
|
+
}
|
|
660
|
+
return {
|
|
661
|
+
stopReason: "error",
|
|
662
|
+
errorMessage: incompleteReason ? `Response incomplete: ${incompleteReason}` : "Response incomplete without a provider reason"
|
|
663
|
+
};
|
|
664
|
+
case "failed":
|
|
665
|
+
case "cancelled":
|
|
666
|
+
return { stopReason: "error" };
|
|
667
|
+
// These two are wonky ...
|
|
668
|
+
case "in_progress":
|
|
669
|
+
case "queued":
|
|
670
|
+
return { stopReason: "stop" };
|
|
671
|
+
default: {
|
|
672
|
+
const _exhaustive = status;
|
|
673
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
__name(mapStopReason, "mapStopReason");
|
|
678
|
+
|
|
679
|
+
export {
|
|
680
|
+
convertResponsesMessages,
|
|
681
|
+
convertResponsesTools,
|
|
682
|
+
processResponsesStream
|
|
683
|
+
};
|