@deslop/workbench 0.0.360 → 0.0.381
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/dist/anthropic-C02QVQ9u.mjs +6313 -0
- package/dist/azure-openai-responses-CUFLNwdy.mjs +207 -0
- package/dist/client/assets/{(home)-DVKAaI4b.js → (home)-BL3S5BVq.js} +1 -1
- package/dist/client/assets/-workbench-terminal-YB9hQXjr.js +145 -0
- package/dist/client/assets/agent-BmwIVlVZ.js +1 -0
- package/dist/client/assets/agent-CE67X_hb.js +2 -0
- package/dist/client/assets/{arc-CuHXSNPr.js → arc-BjSQqbzd.js} +1 -1
- package/dist/client/assets/architectureDiagram-3BPJPVTR-CTWT1AFv.js +36 -0
- package/dist/client/assets/{blockDiagram-GPEHLZMM-D3jiL-uN.js → blockDiagram-GPEHLZMM-CISQUfO9.js} +1 -1
- package/dist/client/assets/{c4Diagram-AAUBKEIU-bA47L7u8.js → c4Diagram-AAUBKEIU-Ct3OGD9a.js} +1 -1
- package/dist/client/assets/channel-S3CdaK6m.js +1 -0
- package/dist/client/assets/{chunk-2J33WTMH-optplvap.js → chunk-2J33WTMH-BUMgKaV6.js} +1 -1
- package/dist/client/assets/{chunk-3OPIFGDE-k7mTnOn5.js → chunk-3OPIFGDE-BlIdNnu8.js} +1 -1
- package/dist/client/assets/{chunk-4BX2VUAB-Co5qfYem.js → chunk-4BX2VUAB-CiBu5cRv.js} +1 -1
- package/dist/client/assets/{chunk-55IACEB6-7AWIUH7L.js → chunk-55IACEB6-nU8KvKB3.js} +1 -1
- package/dist/client/assets/{chunk-5ZQYHXKU-Bub4jU0C.js → chunk-5ZQYHXKU-DbN9DSns.js} +1 -1
- package/dist/client/assets/{chunk-727SXJPM-DcofuxtV.js → chunk-727SXJPM-DkPu5ecB.js} +2 -2
- package/dist/client/assets/{chunk-AQP2D5EJ-CBwGmuY5.js → chunk-AQP2D5EJ-B8lrSg7a.js} +1 -1
- package/dist/client/assets/{chunk-BSJP7CBP-BInTsaMb.js → chunk-BSJP7CBP-C450xCVa.js} +1 -1
- package/dist/client/assets/{chunk-CSCIHK7Q-BTHSw3Fl.js → chunk-CSCIHK7Q-DoGVP1BZ.js} +2 -2
- package/dist/client/assets/{chunk-FMBD7UC4-BQD4ioRx.js → chunk-FMBD7UC4-cqorOok8.js} +1 -1
- package/dist/client/assets/{chunk-KSCS5N6A-CUDSaUx0.js → chunk-KSCS5N6A-dbmnxO_p.js} +1 -1
- package/dist/client/assets/{chunk-L5ZTLDWV-D0-tvK0S.js → chunk-L5ZTLDWV-D9P-YjsA.js} +1 -1
- package/dist/client/assets/{chunk-LZXEDZCA-BkJ5xlLM.js → chunk-LZXEDZCA-BdVT_AWc.js} +2 -2
- package/dist/client/assets/{chunk-ND2GUHAM-COdW9HuZ.js → chunk-ND2GUHAM-CmHIi1wD.js} +1 -1
- package/dist/client/assets/{chunk-NZK2D7GU-Byrnkfto.js → chunk-NZK2D7GU-hkTOm2qR.js} +1 -1
- package/dist/client/assets/{chunk-O5CBEL6O-C_CProRX.js → chunk-O5CBEL6O-D1fDxsSH.js} +1 -1
- package/dist/client/assets/chunk-QZHKN3VN-BS_CF_l3.js +1 -0
- package/dist/client/assets/chunk-WU5MYG2G-CtG2KJlt.js +1 -0
- package/dist/client/assets/{chunk-XPW4576I-DWr3727k.js → chunk-XPW4576I-BjBK3ctW.js} +2 -2
- package/dist/client/assets/classDiagram-4FO5ZUOK-BdTSUdNF.js +1 -0
- package/dist/client/assets/classDiagram-v2-Q7XG4LA2-BdTSUdNF.js +1 -0
- package/dist/client/assets/compiler-runtime-CLAvuQ-D.js +1 -0
- package/dist/client/assets/cose-bilkent-S5V4N54A-BGt2II16.js +1 -0
- package/dist/client/assets/{cytoscape.esm-FqbQrHcz.js → cytoscape.esm-Kic9WIU3.js} +2 -2
- package/dist/client/assets/{dagre-BM42HDAG-Bwiv8kFL.js → dagre-BM42HDAG-CN7Tha00.js} +1 -1
- package/dist/client/assets/{diagram-2AECGRRQ-BY9KXQPi.js → diagram-2AECGRRQ-DnobmroZ.js} +1 -1
- package/dist/client/assets/{diagram-5GNKFQAL-C-jmG8LL.js → diagram-5GNKFQAL-D6NqAeoi.js} +1 -1
- package/dist/client/assets/{diagram-KO2AKTUF-qDHWGr5P.js → diagram-KO2AKTUF-y8QX4vEL.js} +1 -1
- package/dist/client/assets/{diagram-LMA3HP47-Dg8zuIuL.js → diagram-LMA3HP47-DAEPoAHp.js} +1 -1
- package/dist/client/assets/{diagram-OG6HWLK6-DupmLmKo.js → diagram-OG6HWLK6-B3KiYvxM.js} +1 -1
- package/dist/client/assets/dialog-B0kBnYwI.js +153 -0
- package/dist/client/assets/diff-BP9B4x7R.js +2 -0
- package/dist/client/assets/diff-OmMK6IL0.js +221 -0
- package/dist/client/assets/dist-D9sYb5Oa.js +1 -0
- package/dist/client/assets/{erDiagram-TEJ5UH35-DcYoMsDA.js → erDiagram-TEJ5UH35-CbzdEpkS.js} +1 -1
- package/dist/client/assets/external-link-D7Q28KpR.js +1 -0
- package/dist/client/assets/fallbacks-DwGmG_0c.js +16 -0
- package/dist/client/assets/{flowDiagram-I6XJVG4X-xLfWiyaL.js → flowDiagram-I6XJVG4X-C2H7PQev.js} +1 -1
- package/dist/client/assets/ganttDiagram-6RSMTGT7-BZql2QFP.js +292 -0
- package/dist/client/assets/{gitGraphDiagram-PVQCEYII-DE7nmb1q.js → gitGraphDiagram-PVQCEYII-d2_5HIIo.js} +1 -1
- package/dist/client/assets/{index-D1icept3.js → index-Cc-cDoXa.js} +4 -4
- package/dist/client/assets/index-DT_QSnKQ.css +2 -0
- package/dist/client/assets/{infoDiagram-5YYISTIA-CUzdfhph.js → infoDiagram-5YYISTIA-DaDFMb3D.js} +1 -1
- package/dist/client/assets/input-CEh4Uy0l.js +1 -0
- package/dist/client/assets/{ishikawaDiagram-YF4QCWOH-D3h5fT3O.js → ishikawaDiagram-YF4QCWOH-7tWPtPSR.js} +1 -1
- package/dist/client/assets/{journeyDiagram-JHISSGLW-DXqP7IZk.js → journeyDiagram-JHISSGLW-CYDeiigr.js} +1 -1
- package/dist/client/assets/{kanban-definition-UN3LZRKU-BBdz3OGi.js → kanban-definition-UN3LZRKU-BFoF4JfY.js} +3 -3
- package/dist/client/assets/{katex-Vhh-h91d.js → katex-CddkPoXu.js} +1 -1
- package/dist/client/assets/{line-D2nnU070.js → line-CcJ0IOWh.js} +1 -1
- package/dist/client/assets/{linear-BifnMVyn.js → linear-Ct8JHF0c.js} +1 -1
- package/dist/client/assets/mermaid-parser.core-CKHoAf1J.js +2 -2
- package/dist/client/assets/{mindmap-definition-RKZ34NQL-BAbACAeS.js → mindmap-definition-RKZ34NQL-DTgZ_Z5F.js} +1 -1
- package/dist/client/assets/{pieDiagram-4H26LBE5-YjWBEaV3.js → pieDiagram-4H26LBE5-BmpbjVUG.js} +1 -1
- package/dist/client/assets/pierre-dark-vibrant-D4RhcSIK.js +1 -0
- package/dist/client/assets/pierre-light-vibrant-B76X7i5Y.js +1 -0
- package/dist/client/assets/portless-4_iTgOr5.js +1 -0
- package/dist/client/assets/portless-qyhmpSUj.js +2 -0
- package/dist/client/assets/{quadrantDiagram-W4KKPZXB-0qpR8bhI.js → quadrantDiagram-W4KKPZXB-BncnX7WL.js} +1 -1
- package/dist/client/assets/{requirementDiagram-4Y6WPE33-UxrH60_h.js → requirementDiagram-4Y6WPE33-C4viwYDb.js} +1 -1
- package/dist/client/assets/resizable-A9BpOcmt.js +1 -0
- package/dist/client/assets/route-Da0uHKH9.js +45 -0
- package/dist/client/assets/route-ULg8h9n0.js +2 -0
- package/dist/client/assets/run-CCCofwDU.js +2 -0
- package/dist/client/assets/run-D7w-8aQA.js +1 -0
- package/dist/client/assets/{sankeyDiagram-5OEKKPKP-CtfCq9EV.js → sankeyDiagram-5OEKKPKP-cs1kWOqp.js} +1 -1
- package/dist/client/assets/{sequenceDiagram-3UESZ5HK-BgLwmDnV.js → sequenceDiagram-3UESZ5HK-D7Bj1Dg6.js} +1 -1
- package/dist/client/assets/sonner-DgKrvTU1.js +1 -0
- package/dist/client/assets/src-DzxfdEt6.js +1 -0
- package/dist/client/assets/state-_2CmL5IM.js +2 -0
- package/dist/client/assets/{stateDiagram-AJRCARHV--lvnWH2s.js → stateDiagram-AJRCARHV-xBL-crbl.js} +1 -1
- package/dist/client/assets/stateDiagram-v2-BHNVJYJU-ZbAyaVmU.js +1 -0
- package/dist/client/assets/terminal-DJ8tjXNh.js +1 -0
- package/dist/client/assets/terminal-Dx10AUUW.js +2 -0
- package/dist/client/assets/{timeline-definition-PNZ67QCA-BbIMEZmJ.js → timeline-definition-PNZ67QCA-Bs3JkoMQ.js} +2 -2
- package/dist/client/assets/triangle-alert-cDPYgb7f.js +1 -0
- package/dist/client/assets/{vennDiagram-CIIHVFJN-ClrNZ6mc.js → vennDiagram-CIIHVFJN-D25dPloy.js} +1 -1
- package/dist/client/assets/{wardleyDiagram-YWT4CUSO-B104yVOC.js → wardleyDiagram-YWT4CUSO-B7X6xZZ7.js} +1 -1
- package/dist/client/assets/{xychartDiagram-2RQKCTM6-BBRSu-ZO.js → xychartDiagram-2RQKCTM6-Cgc-2R8L.js} +1 -1
- package/dist/client/index.html +13 -14
- package/dist/completionchunk-DDfxP5hO.mjs +5196 -0
- package/dist/dist-CsBDFjO2.mjs +1163 -0
- package/dist/{execAsync-BeHTdVKh.mjs → execAsync-B-b5b4hb.mjs} +2 -2
- package/dist/from-DGwFTZmN.mjs +3845 -0
- package/dist/{getMachineId-bsd-8V0SSHI2.mjs → getMachineId-bsd-C-aUDBo5.mjs} +4 -3
- package/dist/{getMachineId-darwin-fwMRmXNE.mjs → getMachineId-darwin-CQg0u9gI.mjs} +4 -3
- package/dist/{getMachineId-linux-BglPdw6k.mjs → getMachineId-linux-DQmpbzHz.mjs} +3 -2
- package/dist/{getMachineId-unsupported-B6r8oWEG.mjs → getMachineId-unsupported-BT4Te_jg.mjs} +3 -2
- package/dist/{getMachineId-win-Bvbq3C3K.mjs → getMachineId-win-zoUdci1a.mjs} +4 -3
- package/dist/github-copilot-headers-B--aVYl2.mjs +39 -0
- package/dist/google-DHtiCcEt.mjs +348 -0
- package/dist/google-shared-BT0mQb3v.mjs +25358 -0
- package/dist/google-vertex-CRkz0MgQ.mjs +391 -0
- package/dist/hash-CDrrQIkY.mjs +17 -0
- package/dist/headers-m19bM8_9.mjs +9 -0
- package/dist/mistral-DXtHhhUc.mjs +36806 -0
- package/dist/multipart-parser-6y-zJbR0.mjs +295 -0
- package/dist/openai-CUH8rb7p.mjs +6671 -0
- package/dist/openai-codex-responses-BSLixEC8.mjs +1081 -0
- package/dist/openai-completions-CE-LDmL4.mjs +735 -0
- package/dist/openai-prompt-cache-6hf3xB_x.mjs +9 -0
- package/dist/openai-responses-41QiypX_.mjs +217 -0
- package/dist/openai-responses-shared-BAoGGIxP.mjs +415 -0
- package/dist/openrouter-CDq1vETJ.mjs +125 -0
- package/dist/otel-ugdFQeJO.mjs +23177 -0
- package/dist/photon_rs-D6BNoOSO.mjs +4253 -0
- package/dist/rolldown-runtime-EQORzF3F.mjs +38 -0
- package/dist/sanitize-unicode-CPcy0g31.mjs +27 -0
- package/dist/server.js +247457 -56643
- package/dist/src-BHoXDvRK.mjs +1178 -0
- package/dist/transform-messages-CUKh2_5w.mjs +188 -0
- package/package.json +4 -3
- package/dist/client/assets/-workbench-terminal-CoufoT41.js +0 -145
- package/dist/client/assets/agent-B0AsYIb3.js +0 -2
- package/dist/client/assets/agent-DCVbbaGs.js +0 -1
- package/dist/client/assets/architectureDiagram-3BPJPVTR-hs4IN-ZW.js +0 -36
- package/dist/client/assets/button-Dz6Az3h_.js +0 -16
- package/dist/client/assets/channel-CHiC1IgU.js +0 -1
- package/dist/client/assets/chunk-QZHKN3VN-SFi3gsXX.js +0 -1
- package/dist/client/assets/chunk-WU5MYG2G-OzrGGim9.js +0 -1
- package/dist/client/assets/classDiagram-4FO5ZUOK-DvZGz7Ek.js +0 -1
- package/dist/client/assets/classDiagram-v2-Q7XG4LA2-DvZGz7Ek.js +0 -1
- package/dist/client/assets/compiler-runtime-rGeCHUuX.js +0 -1
- package/dist/client/assets/cose-bilkent-S5V4N54A-BzitJXTQ.js +0 -1
- package/dist/client/assets/diff-BqnMP7Ys.js +0 -217
- package/dist/client/assets/diff-CLnQZZVT.js +0 -2
- package/dist/client/assets/dist-lRkD-JPF.js +0 -1
- package/dist/client/assets/external-link-DPI4TAq4.js +0 -1
- package/dist/client/assets/fallbacks-uU4M5NOw.js +0 -1
- package/dist/client/assets/ganttDiagram-6RSMTGT7-hA3inn7u.js +0 -292
- package/dist/client/assets/index-DNWcrdqu.css +0 -2
- package/dist/client/assets/input-group-1-P7Y_75.js +0 -153
- package/dist/client/assets/loader-circle-BxillczB.js +0 -1
- package/dist/client/assets/portless-ClPWjTC4.js +0 -1
- package/dist/client/assets/portless-QJD7ZmA9.js +0 -2
- package/dist/client/assets/resizable-BaJ5ba6-.js +0 -1
- package/dist/client/assets/route-C-L2dkin.js +0 -45
- package/dist/client/assets/route-b5JXfzPO.js +0 -2
- package/dist/client/assets/run-C7DiKMnt.js +0 -1
- package/dist/client/assets/run-Dt91HvlA.js +0 -2
- package/dist/client/assets/src-oaGqIoNi.js +0 -1
- package/dist/client/assets/state-CVjPP5QG.js +0 -2
- package/dist/client/assets/stateDiagram-v2-BHNVJYJU-BggoNPhO.js +0 -1
- package/dist/client/assets/terminal-C4sv4xT5.js +0 -2
- package/dist/client/assets/terminal-kxE_eoTZ.js +0 -1
- package/dist/client/assets/triangle-alert-CTEZG9zF.js +0 -1
- /package/dist/client/assets/{chunk-QTnfLwEv.js → rolldown-runtime-QTnfLwEv.js} +0 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --max-old-space-size=16384 --heapsnapshot-near-heap-limit=3 --report-on-fatalerror
|
|
2
|
+
function clampOpenAIPromptCacheKey(key) {
|
|
3
|
+
if (key === void 0) return void 0;
|
|
4
|
+
const chars = Array.from(key);
|
|
5
|
+
if (chars.length <= 64) return key;
|
|
6
|
+
return chars.slice(0, 64).join("");
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { clampOpenAIPromptCacheKey as t };
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --max-old-space-size=16384 --heapsnapshot-near-heap-limit=3 --report-on-fatalerror
|
|
2
|
+
import { Lr as clampThinkingLevel, Rr as getProviderEnvValue, Vr as registerApiProvider, zr as AssistantMessageEventStream } from "./server.js";
|
|
3
|
+
import { t as headersToRecord } from "./headers-m19bM8_9.mjs";
|
|
4
|
+
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, r as isCloudflareProvider, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-B--aVYl2.mjs";
|
|
5
|
+
import { r as buildBaseOptions } from "./transform-messages-CUKh2_5w.mjs";
|
|
6
|
+
import { n as OpenAI } from "./openai-CUH8rb7p.mjs";
|
|
7
|
+
import { t as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-6hf3xB_x.mjs";
|
|
8
|
+
import { n as convertResponsesTools, r as processResponsesStream, t as convertResponsesMessages } from "./openai-responses-shared-BAoGGIxP.mjs";
|
|
9
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-ai@0.79.8_@modelcontextprotocol+sdk@1.29.0_zod@4.4.3__ws@8.21.0_zod@4.4.3/node_modules/@earendil-works/pi-ai/dist/providers/openai-responses.js
|
|
10
|
+
const OPENAI_TOOL_CALL_PROVIDERS = new Set([
|
|
11
|
+
"openai",
|
|
12
|
+
"openai-codex",
|
|
13
|
+
"opencode"
|
|
14
|
+
]);
|
|
15
|
+
/**
|
|
16
|
+
* Resolve cache retention preference.
|
|
17
|
+
* Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
|
|
18
|
+
*/
|
|
19
|
+
function resolveCacheRetention(cacheRetention, env) {
|
|
20
|
+
if (cacheRetention) return cacheRetention;
|
|
21
|
+
if (getProviderEnvValue("PI_CACHE_RETENTION", env) === "long") return "long";
|
|
22
|
+
return "short";
|
|
23
|
+
}
|
|
24
|
+
function getCompat(model) {
|
|
25
|
+
return {
|
|
26
|
+
supportsDeveloperRole: model.compat?.supportsDeveloperRole ?? true,
|
|
27
|
+
sendSessionIdHeader: model.compat?.sendSessionIdHeader ?? true,
|
|
28
|
+
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function getPromptCacheRetention(compat, cacheRetention) {
|
|
32
|
+
return cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0;
|
|
33
|
+
}
|
|
34
|
+
function formatOpenAIResponsesError(error) {
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
const status = error.status;
|
|
37
|
+
const statusCode = typeof status === "number" ? status : void 0;
|
|
38
|
+
if (statusCode !== void 0) return `OpenAI API error (${statusCode}): ${error.message}`;
|
|
39
|
+
return error.message;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
return JSON.stringify(error);
|
|
43
|
+
} catch {
|
|
44
|
+
return String(error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Generate function for OpenAI Responses API
|
|
49
|
+
*/
|
|
50
|
+
const streamOpenAIResponses = (model, context, options) => {
|
|
51
|
+
const stream = new AssistantMessageEventStream();
|
|
52
|
+
(async () => {
|
|
53
|
+
const output = {
|
|
54
|
+
role: "assistant",
|
|
55
|
+
content: [],
|
|
56
|
+
api: model.api,
|
|
57
|
+
provider: model.provider,
|
|
58
|
+
model: model.id,
|
|
59
|
+
usage: {
|
|
60
|
+
input: 0,
|
|
61
|
+
output: 0,
|
|
62
|
+
cacheRead: 0,
|
|
63
|
+
cacheWrite: 0,
|
|
64
|
+
totalTokens: 0,
|
|
65
|
+
cost: {
|
|
66
|
+
input: 0,
|
|
67
|
+
output: 0,
|
|
68
|
+
cacheRead: 0,
|
|
69
|
+
cacheWrite: 0,
|
|
70
|
+
total: 0
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
stopReason: "stop",
|
|
74
|
+
timestamp: Date.now()
|
|
75
|
+
};
|
|
76
|
+
try {
|
|
77
|
+
const apiKey = options?.apiKey;
|
|
78
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
79
|
+
const cacheSessionId = resolveCacheRetention(options?.cacheRetention, options?.env) === "none" ? void 0 : options?.sessionId;
|
|
80
|
+
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId, options?.env);
|
|
81
|
+
let params = buildParams(model, context, options);
|
|
82
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
83
|
+
if (nextParams !== void 0) params = nextParams;
|
|
84
|
+
const requestOptions = {
|
|
85
|
+
...options?.signal ? { signal: options.signal } : {},
|
|
86
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
87
|
+
maxRetries: options?.maxRetries ?? 0
|
|
88
|
+
};
|
|
89
|
+
const { data: openaiStream, response } = await client.responses.create(params, requestOptions).withResponse();
|
|
90
|
+
await options?.onResponse?.({
|
|
91
|
+
status: response.status,
|
|
92
|
+
headers: headersToRecord(response.headers)
|
|
93
|
+
}, model);
|
|
94
|
+
stream.push({
|
|
95
|
+
type: "start",
|
|
96
|
+
partial: output
|
|
97
|
+
});
|
|
98
|
+
await processResponsesStream(openaiStream, output, stream, model, {
|
|
99
|
+
serviceTier: options?.serviceTier,
|
|
100
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model)
|
|
101
|
+
});
|
|
102
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
103
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error("An unknown error occurred");
|
|
104
|
+
stream.push({
|
|
105
|
+
type: "done",
|
|
106
|
+
reason: output.stopReason,
|
|
107
|
+
message: output
|
|
108
|
+
});
|
|
109
|
+
stream.end();
|
|
110
|
+
} catch (error) {
|
|
111
|
+
for (const block of output.content) {
|
|
112
|
+
delete block.index;
|
|
113
|
+
delete block.partialJson;
|
|
114
|
+
}
|
|
115
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
116
|
+
output.errorMessage = formatOpenAIResponsesError(error);
|
|
117
|
+
stream.push({
|
|
118
|
+
type: "error",
|
|
119
|
+
reason: output.stopReason,
|
|
120
|
+
error: output
|
|
121
|
+
});
|
|
122
|
+
stream.end();
|
|
123
|
+
}
|
|
124
|
+
})();
|
|
125
|
+
return stream;
|
|
126
|
+
};
|
|
127
|
+
const streamSimpleOpenAIResponses = (model, context, options) => {
|
|
128
|
+
const apiKey = options?.apiKey;
|
|
129
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
130
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
131
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
132
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning;
|
|
133
|
+
return streamOpenAIResponses(model, context, {
|
|
134
|
+
...base,
|
|
135
|
+
reasoningEffort
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
function register() {
|
|
139
|
+
registerApiProvider({
|
|
140
|
+
api: "openai-responses",
|
|
141
|
+
stream: streamOpenAIResponses,
|
|
142
|
+
streamSimple: streamSimpleOpenAIResponses
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function createClient(model, context, apiKey, optionsHeaders, sessionId, env) {
|
|
146
|
+
const compat = getCompat(model);
|
|
147
|
+
const headers = { ...model.headers };
|
|
148
|
+
if (model.provider === "github-copilot") {
|
|
149
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
150
|
+
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
151
|
+
messages: context.messages,
|
|
152
|
+
hasImages
|
|
153
|
+
});
|
|
154
|
+
Object.assign(headers, copilotHeaders);
|
|
155
|
+
}
|
|
156
|
+
if (sessionId) {
|
|
157
|
+
if (compat.sendSessionIdHeader) headers.session_id = sessionId;
|
|
158
|
+
headers["x-client-request-id"] = sessionId;
|
|
159
|
+
}
|
|
160
|
+
if (optionsHeaders) Object.assign(headers, optionsHeaders);
|
|
161
|
+
const defaultHeaders = model.provider === "cloudflare-ai-gateway" ? {
|
|
162
|
+
...headers,
|
|
163
|
+
Authorization: headers.Authorization ?? null,
|
|
164
|
+
"cf-aig-authorization": `Bearer ${apiKey}`
|
|
165
|
+
} : headers;
|
|
166
|
+
return new OpenAI({
|
|
167
|
+
apiKey,
|
|
168
|
+
baseURL: isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model, env) : model.baseUrl,
|
|
169
|
+
dangerouslyAllowBrowser: true,
|
|
170
|
+
defaultHeaders
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function buildParams(model, context, options) {
|
|
174
|
+
const messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS);
|
|
175
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
176
|
+
const compat = getCompat(model);
|
|
177
|
+
const params = {
|
|
178
|
+
model: model.id,
|
|
179
|
+
input: messages,
|
|
180
|
+
stream: true,
|
|
181
|
+
prompt_cache_key: cacheRetention === "none" ? void 0 : clampOpenAIPromptCacheKey(options?.sessionId),
|
|
182
|
+
prompt_cache_retention: getPromptCacheRetention(compat, cacheRetention),
|
|
183
|
+
store: false
|
|
184
|
+
};
|
|
185
|
+
if (options?.maxTokens) params.max_output_tokens = options?.maxTokens;
|
|
186
|
+
if (options?.temperature !== void 0) params.temperature = options?.temperature;
|
|
187
|
+
if (options?.serviceTier !== void 0) params.service_tier = options.serviceTier;
|
|
188
|
+
if (context.tools && context.tools.length > 0) params.tools = convertResponsesTools(context.tools);
|
|
189
|
+
if (model.reasoning) {
|
|
190
|
+
if (options?.reasoningEffort || options?.reasoningSummary) {
|
|
191
|
+
params.reasoning = {
|
|
192
|
+
effort: options?.reasoningEffort ? model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort : "medium",
|
|
193
|
+
summary: options?.reasoningSummary || "auto"
|
|
194
|
+
};
|
|
195
|
+
params.include = ["reasoning.encrypted_content"];
|
|
196
|
+
} else if (model.provider !== "github-copilot" && model.thinkingLevelMap?.off !== null) params.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
|
|
197
|
+
}
|
|
198
|
+
return params;
|
|
199
|
+
}
|
|
200
|
+
function getServiceTierCostMultiplier(model, serviceTier) {
|
|
201
|
+
switch (serviceTier) {
|
|
202
|
+
case "flex": return .5;
|
|
203
|
+
case "priority": return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
204
|
+
default: return 1;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function applyServiceTierPricing(usage, serviceTier, model) {
|
|
208
|
+
const multiplier = getServiceTierCostMultiplier(model, serviceTier);
|
|
209
|
+
if (multiplier === 1) return;
|
|
210
|
+
usage.cost.input *= multiplier;
|
|
211
|
+
usage.cost.output *= multiplier;
|
|
212
|
+
usage.cost.cacheRead *= multiplier;
|
|
213
|
+
usage.cost.cacheWrite *= multiplier;
|
|
214
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
export { register, streamOpenAIResponses, streamSimpleOpenAIResponses };
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --max-old-space-size=16384 --heapsnapshot-near-heap-limit=3 --report-on-fatalerror
|
|
2
|
+
import { Ir as calculateCost, jr as parseStreamingJson } from "./server.js";
|
|
3
|
+
import { t as sanitizeSurrogates } from "./sanitize-unicode-CPcy0g31.mjs";
|
|
4
|
+
import { t as transformMessages } from "./transform-messages-CUKh2_5w.mjs";
|
|
5
|
+
import { t as shortHash } from "./hash-CDrrQIkY.mjs";
|
|
6
|
+
//#region ../../node_modules/.pnpm/@earendil-works+pi-ai@0.79.8_@modelcontextprotocol+sdk@1.29.0_zod@4.4.3__ws@8.21.0_zod@4.4.3/node_modules/@earendil-works/pi-ai/dist/providers/openai-responses-shared.js
|
|
7
|
+
function encodeTextSignatureV1(id, phase) {
|
|
8
|
+
const payload = {
|
|
9
|
+
v: 1,
|
|
10
|
+
id
|
|
11
|
+
};
|
|
12
|
+
if (phase) payload.phase = phase;
|
|
13
|
+
return JSON.stringify(payload);
|
|
14
|
+
}
|
|
15
|
+
function parseTextSignature(signature) {
|
|
16
|
+
if (!signature) return void 0;
|
|
17
|
+
if (signature.startsWith("{")) try {
|
|
18
|
+
const parsed = JSON.parse(signature);
|
|
19
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
20
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") return {
|
|
21
|
+
id: parsed.id,
|
|
22
|
+
phase: parsed.phase
|
|
23
|
+
};
|
|
24
|
+
return { id: parsed.id };
|
|
25
|
+
}
|
|
26
|
+
} catch {}
|
|
27
|
+
return { id: signature };
|
|
28
|
+
}
|
|
29
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
30
|
+
const messages = [];
|
|
31
|
+
const normalizeIdPart = (part) => {
|
|
32
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
33
|
+
return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
|
|
34
|
+
};
|
|
35
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
36
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
37
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
38
|
+
};
|
|
39
|
+
const normalizeToolCallId = (id, _targetModel, source) => {
|
|
40
|
+
if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
|
|
41
|
+
if (!id.includes("|")) return normalizeIdPart(id);
|
|
42
|
+
const [callId, itemId] = id.split("|");
|
|
43
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
44
|
+
let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
45
|
+
if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
46
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
47
|
+
};
|
|
48
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
49
|
+
if ((options?.includeSystemPrompt ?? true) && context.systemPrompt) {
|
|
50
|
+
const compat = model.compat;
|
|
51
|
+
const role = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
52
|
+
messages.push({
|
|
53
|
+
role,
|
|
54
|
+
content: sanitizeSurrogates(context.systemPrompt)
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
let msgIndex = 0;
|
|
58
|
+
for (const msg of transformedMessages) {
|
|
59
|
+
if (msg.role === "user") if (typeof msg.content === "string") messages.push({
|
|
60
|
+
role: "user",
|
|
61
|
+
content: [{
|
|
62
|
+
type: "input_text",
|
|
63
|
+
text: sanitizeSurrogates(msg.content)
|
|
64
|
+
}]
|
|
65
|
+
});
|
|
66
|
+
else {
|
|
67
|
+
const content = msg.content.map((item) => {
|
|
68
|
+
if (item.type === "text") return {
|
|
69
|
+
type: "input_text",
|
|
70
|
+
text: sanitizeSurrogates(item.text)
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
type: "input_image",
|
|
74
|
+
detail: "auto",
|
|
75
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
if (content.length === 0) continue;
|
|
79
|
+
messages.push({
|
|
80
|
+
role: "user",
|
|
81
|
+
content
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else if (msg.role === "assistant") {
|
|
85
|
+
const output = [];
|
|
86
|
+
const assistantMsg = msg;
|
|
87
|
+
const isDifferentModel = assistantMsg.model !== model.id && assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
88
|
+
let textBlockIndex = 0;
|
|
89
|
+
for (const block of msg.content) if (block.type === "thinking") {
|
|
90
|
+
if (block.thinkingSignature) {
|
|
91
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
92
|
+
output.push(reasoningItem);
|
|
93
|
+
}
|
|
94
|
+
} else if (block.type === "text") {
|
|
95
|
+
const textBlock = block;
|
|
96
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
97
|
+
const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
|
|
98
|
+
textBlockIndex++;
|
|
99
|
+
let msgId = parsedSignature?.id;
|
|
100
|
+
if (!msgId) msgId = fallbackMessageId;
|
|
101
|
+
else if (msgId.length > 64) msgId = `msg_${shortHash(msgId)}`;
|
|
102
|
+
output.push({
|
|
103
|
+
type: "message",
|
|
104
|
+
role: "assistant",
|
|
105
|
+
content: [{
|
|
106
|
+
type: "output_text",
|
|
107
|
+
text: sanitizeSurrogates(textBlock.text),
|
|
108
|
+
annotations: []
|
|
109
|
+
}],
|
|
110
|
+
status: "completed",
|
|
111
|
+
id: msgId,
|
|
112
|
+
phase: parsedSignature?.phase
|
|
113
|
+
});
|
|
114
|
+
} else if (block.type === "toolCall") {
|
|
115
|
+
const toolCall = block;
|
|
116
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
117
|
+
let itemId = itemIdRaw;
|
|
118
|
+
if (isDifferentModel && itemId?.startsWith("fc_")) itemId = void 0;
|
|
119
|
+
output.push({
|
|
120
|
+
type: "function_call",
|
|
121
|
+
id: itemId,
|
|
122
|
+
call_id: callId,
|
|
123
|
+
name: toolCall.name,
|
|
124
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (output.length === 0) continue;
|
|
128
|
+
messages.push(...output);
|
|
129
|
+
} else if (msg.role === "toolResult") {
|
|
130
|
+
const textResult = msg.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
131
|
+
const hasImages = msg.content.some((c) => c.type === "image");
|
|
132
|
+
const hasText = textResult.length > 0;
|
|
133
|
+
const [callId] = msg.toolCallId.split("|");
|
|
134
|
+
let output;
|
|
135
|
+
if (hasImages && model.input.includes("image")) {
|
|
136
|
+
const contentParts = [];
|
|
137
|
+
if (hasText) contentParts.push({
|
|
138
|
+
type: "input_text",
|
|
139
|
+
text: sanitizeSurrogates(textResult)
|
|
140
|
+
});
|
|
141
|
+
for (const block of msg.content) if (block.type === "image") contentParts.push({
|
|
142
|
+
type: "input_image",
|
|
143
|
+
detail: "auto",
|
|
144
|
+
image_url: `data:${block.mimeType};base64,${block.data}`
|
|
145
|
+
});
|
|
146
|
+
output = contentParts;
|
|
147
|
+
} else output = sanitizeSurrogates(hasText ? textResult : "(see attached image)");
|
|
148
|
+
messages.push({
|
|
149
|
+
type: "function_call_output",
|
|
150
|
+
call_id: callId,
|
|
151
|
+
output
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
msgIndex++;
|
|
155
|
+
}
|
|
156
|
+
return messages;
|
|
157
|
+
}
|
|
158
|
+
function convertResponsesTools(tools, options) {
|
|
159
|
+
const strict = options?.strict === void 0 ? false : options.strict;
|
|
160
|
+
return tools.map((tool) => ({
|
|
161
|
+
type: "function",
|
|
162
|
+
name: tool.name,
|
|
163
|
+
description: tool.description,
|
|
164
|
+
parameters: tool.parameters,
|
|
165
|
+
strict
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
169
|
+
let currentItem = null;
|
|
170
|
+
let currentBlock = null;
|
|
171
|
+
const blocks = output.content;
|
|
172
|
+
const blockIndex = () => blocks.length - 1;
|
|
173
|
+
for await (const event of openaiStream) if (event.type === "response.created") output.responseId = event.response.id;
|
|
174
|
+
else if (event.type === "response.output_item.added") {
|
|
175
|
+
const item = event.item;
|
|
176
|
+
if (item.type === "reasoning") {
|
|
177
|
+
currentItem = item;
|
|
178
|
+
currentBlock = {
|
|
179
|
+
type: "thinking",
|
|
180
|
+
thinking: ""
|
|
181
|
+
};
|
|
182
|
+
output.content.push(currentBlock);
|
|
183
|
+
stream.push({
|
|
184
|
+
type: "thinking_start",
|
|
185
|
+
contentIndex: blockIndex(),
|
|
186
|
+
partial: output
|
|
187
|
+
});
|
|
188
|
+
} else if (item.type === "message") {
|
|
189
|
+
currentItem = item;
|
|
190
|
+
currentBlock = {
|
|
191
|
+
type: "text",
|
|
192
|
+
text: ""
|
|
193
|
+
};
|
|
194
|
+
output.content.push(currentBlock);
|
|
195
|
+
stream.push({
|
|
196
|
+
type: "text_start",
|
|
197
|
+
contentIndex: blockIndex(),
|
|
198
|
+
partial: output
|
|
199
|
+
});
|
|
200
|
+
} else if (item.type === "function_call") {
|
|
201
|
+
currentItem = item;
|
|
202
|
+
currentBlock = {
|
|
203
|
+
type: "toolCall",
|
|
204
|
+
id: `${item.call_id}|${item.id}`,
|
|
205
|
+
name: item.name,
|
|
206
|
+
arguments: {},
|
|
207
|
+
partialJson: item.arguments || ""
|
|
208
|
+
};
|
|
209
|
+
output.content.push(currentBlock);
|
|
210
|
+
stream.push({
|
|
211
|
+
type: "toolcall_start",
|
|
212
|
+
contentIndex: blockIndex(),
|
|
213
|
+
partial: output
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
} else if (event.type === "response.reasoning_summary_part.added") {
|
|
217
|
+
if (currentItem && currentItem.type === "reasoning") {
|
|
218
|
+
currentItem.summary = currentItem.summary || [];
|
|
219
|
+
currentItem.summary.push(event.part);
|
|
220
|
+
}
|
|
221
|
+
} else if (event.type === "response.reasoning_summary_text.delta") {
|
|
222
|
+
if (currentItem?.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
223
|
+
currentItem.summary = currentItem.summary || [];
|
|
224
|
+
const lastPart = currentItem.summary[currentItem.summary.length - 1];
|
|
225
|
+
if (lastPart) {
|
|
226
|
+
currentBlock.thinking += event.delta;
|
|
227
|
+
lastPart.text += event.delta;
|
|
228
|
+
stream.push({
|
|
229
|
+
type: "thinking_delta",
|
|
230
|
+
contentIndex: blockIndex(),
|
|
231
|
+
delta: event.delta,
|
|
232
|
+
partial: output
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
237
|
+
if (currentItem?.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
238
|
+
currentItem.summary = currentItem.summary || [];
|
|
239
|
+
const lastPart = currentItem.summary[currentItem.summary.length - 1];
|
|
240
|
+
if (lastPart) {
|
|
241
|
+
currentBlock.thinking += "\n\n";
|
|
242
|
+
lastPart.text += "\n\n";
|
|
243
|
+
stream.push({
|
|
244
|
+
type: "thinking_delta",
|
|
245
|
+
contentIndex: blockIndex(),
|
|
246
|
+
delta: "\n\n",
|
|
247
|
+
partial: output
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
252
|
+
if (currentItem?.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
253
|
+
currentBlock.thinking += event.delta;
|
|
254
|
+
stream.push({
|
|
255
|
+
type: "thinking_delta",
|
|
256
|
+
contentIndex: blockIndex(),
|
|
257
|
+
delta: event.delta,
|
|
258
|
+
partial: output
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
} else if (event.type === "response.content_part.added") {
|
|
262
|
+
if (currentItem?.type === "message") {
|
|
263
|
+
currentItem.content = currentItem.content || [];
|
|
264
|
+
if (event.part.type === "output_text" || event.part.type === "refusal") currentItem.content.push(event.part);
|
|
265
|
+
}
|
|
266
|
+
} else if (event.type === "response.output_text.delta") {
|
|
267
|
+
if (currentItem?.type === "message" && currentBlock?.type === "text") {
|
|
268
|
+
if (!currentItem.content || currentItem.content.length === 0) continue;
|
|
269
|
+
const lastPart = currentItem.content[currentItem.content.length - 1];
|
|
270
|
+
if (lastPart?.type === "output_text") {
|
|
271
|
+
currentBlock.text += event.delta;
|
|
272
|
+
lastPart.text += event.delta;
|
|
273
|
+
stream.push({
|
|
274
|
+
type: "text_delta",
|
|
275
|
+
contentIndex: blockIndex(),
|
|
276
|
+
delta: event.delta,
|
|
277
|
+
partial: output
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
} else if (event.type === "response.refusal.delta") {
|
|
282
|
+
if (currentItem?.type === "message" && currentBlock?.type === "text") {
|
|
283
|
+
if (!currentItem.content || currentItem.content.length === 0) continue;
|
|
284
|
+
const lastPart = currentItem.content[currentItem.content.length - 1];
|
|
285
|
+
if (lastPart?.type === "refusal") {
|
|
286
|
+
currentBlock.text += event.delta;
|
|
287
|
+
lastPart.refusal += event.delta;
|
|
288
|
+
stream.push({
|
|
289
|
+
type: "text_delta",
|
|
290
|
+
contentIndex: blockIndex(),
|
|
291
|
+
delta: event.delta,
|
|
292
|
+
partial: output
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
297
|
+
if (currentItem?.type === "function_call" && currentBlock?.type === "toolCall") {
|
|
298
|
+
currentBlock.partialJson += event.delta;
|
|
299
|
+
currentBlock.arguments = parseStreamingJson(currentBlock.partialJson);
|
|
300
|
+
stream.push({
|
|
301
|
+
type: "toolcall_delta",
|
|
302
|
+
contentIndex: blockIndex(),
|
|
303
|
+
delta: event.delta,
|
|
304
|
+
partial: output
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
308
|
+
if (currentItem?.type === "function_call" && currentBlock?.type === "toolCall") {
|
|
309
|
+
const previousPartialJson = currentBlock.partialJson;
|
|
310
|
+
currentBlock.partialJson = event.arguments;
|
|
311
|
+
currentBlock.arguments = parseStreamingJson(currentBlock.partialJson);
|
|
312
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
313
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
314
|
+
if (delta.length > 0) stream.push({
|
|
315
|
+
type: "toolcall_delta",
|
|
316
|
+
contentIndex: blockIndex(),
|
|
317
|
+
delta,
|
|
318
|
+
partial: output
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} else if (event.type === "response.output_item.done") {
|
|
323
|
+
const item = event.item;
|
|
324
|
+
if (item.type === "reasoning" && currentBlock?.type === "thinking") {
|
|
325
|
+
const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
326
|
+
const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
|
|
327
|
+
currentBlock.thinking = summaryText || contentText || currentBlock.thinking;
|
|
328
|
+
currentBlock.thinkingSignature = JSON.stringify(item);
|
|
329
|
+
stream.push({
|
|
330
|
+
type: "thinking_end",
|
|
331
|
+
contentIndex: blockIndex(),
|
|
332
|
+
content: currentBlock.thinking,
|
|
333
|
+
partial: output
|
|
334
|
+
});
|
|
335
|
+
currentBlock = null;
|
|
336
|
+
} else if (item.type === "message" && currentBlock?.type === "text") {
|
|
337
|
+
currentBlock.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
338
|
+
currentBlock.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
339
|
+
stream.push({
|
|
340
|
+
type: "text_end",
|
|
341
|
+
contentIndex: blockIndex(),
|
|
342
|
+
content: currentBlock.text,
|
|
343
|
+
partial: output
|
|
344
|
+
});
|
|
345
|
+
currentBlock = null;
|
|
346
|
+
} else if (item.type === "function_call") {
|
|
347
|
+
const args = currentBlock?.type === "toolCall" && currentBlock.partialJson ? parseStreamingJson(currentBlock.partialJson) : parseStreamingJson(item.arguments || "{}");
|
|
348
|
+
let toolCall;
|
|
349
|
+
if (currentBlock?.type === "toolCall") {
|
|
350
|
+
currentBlock.arguments = args;
|
|
351
|
+
delete currentBlock.partialJson;
|
|
352
|
+
toolCall = currentBlock;
|
|
353
|
+
} else toolCall = {
|
|
354
|
+
type: "toolCall",
|
|
355
|
+
id: `${item.call_id}|${item.id}`,
|
|
356
|
+
name: item.name,
|
|
357
|
+
arguments: args
|
|
358
|
+
};
|
|
359
|
+
currentBlock = null;
|
|
360
|
+
stream.push({
|
|
361
|
+
type: "toolcall_end",
|
|
362
|
+
contentIndex: blockIndex(),
|
|
363
|
+
toolCall,
|
|
364
|
+
partial: output
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
} else if (event.type === "response.completed") {
|
|
368
|
+
const response = event.response;
|
|
369
|
+
if (response?.id) output.responseId = response.id;
|
|
370
|
+
if (response?.usage) {
|
|
371
|
+
const cachedTokens = response.usage.input_tokens_details?.cached_tokens || 0;
|
|
372
|
+
output.usage = {
|
|
373
|
+
input: (response.usage.input_tokens || 0) - cachedTokens,
|
|
374
|
+
output: response.usage.output_tokens || 0,
|
|
375
|
+
cacheRead: cachedTokens,
|
|
376
|
+
cacheWrite: 0,
|
|
377
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
378
|
+
cost: {
|
|
379
|
+
input: 0,
|
|
380
|
+
output: 0,
|
|
381
|
+
cacheRead: 0,
|
|
382
|
+
cacheWrite: 0,
|
|
383
|
+
total: 0
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
calculateCost(model, output.usage);
|
|
388
|
+
if (options?.applyServiceTierPricing) {
|
|
389
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
|
|
390
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
391
|
+
}
|
|
392
|
+
output.stopReason = mapStopReason(response?.status);
|
|
393
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") output.stopReason = "toolUse";
|
|
394
|
+
} else if (event.type === "error") throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
395
|
+
else if (event.type === "response.failed") {
|
|
396
|
+
const error = event.response?.error;
|
|
397
|
+
const details = event.response?.incomplete_details;
|
|
398
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
399
|
+
throw new Error(msg);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function mapStopReason(status) {
|
|
403
|
+
if (!status) return "stop";
|
|
404
|
+
switch (status) {
|
|
405
|
+
case "completed": return "stop";
|
|
406
|
+
case "incomplete": return "length";
|
|
407
|
+
case "failed":
|
|
408
|
+
case "cancelled": return "error";
|
|
409
|
+
case "in_progress":
|
|
410
|
+
case "queued": return "stop";
|
|
411
|
+
default: throw new Error(`Unhandled stop reason: ${status}`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
415
|
+
export { convertResponsesTools as n, processResponsesStream as r, convertResponsesMessages as t };
|