@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,1081 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --max-old-space-size=16384 --heapsnapshot-near-heap-limit=3 --report-on-fatalerror
|
|
2
|
+
import { Fr as registerSessionResourceCleanup, Lr as clampThinkingLevel, Mr as appendAssistantMessageDiagnostic, Nr as createAssistantMessageDiagnostic, Pr as formatThrownValue, Rr as getProviderEnvValue, Vr as registerApiProvider, zr as AssistantMessageEventStream } from "./server.js";
|
|
3
|
+
import { t as headersToRecord } from "./headers-m19bM8_9.mjs";
|
|
4
|
+
import { r as buildBaseOptions } from "./transform-messages-CUKh2_5w.mjs";
|
|
5
|
+
import { t as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-6hf3xB_x.mjs";
|
|
6
|
+
import { n as convertResponsesTools, r as processResponsesStream, t as convertResponsesMessages } from "./openai-responses-shared-BAoGGIxP.mjs";
|
|
7
|
+
//#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/utils/abort-signals.js
|
|
8
|
+
function combineAbortSignals(signals) {
|
|
9
|
+
const activeSignals = signals.filter((signal) => signal !== void 0);
|
|
10
|
+
if (activeSignals.length === 0) return { cleanup: () => {} };
|
|
11
|
+
if (activeSignals.length === 1) return {
|
|
12
|
+
signal: activeSignals[0],
|
|
13
|
+
cleanup: () => {}
|
|
14
|
+
};
|
|
15
|
+
const controller = new AbortController();
|
|
16
|
+
const listeners = [];
|
|
17
|
+
const abort = (signal) => {
|
|
18
|
+
if (!controller.signal.aborted) controller.abort(signal.reason);
|
|
19
|
+
};
|
|
20
|
+
for (const signal of activeSignals) {
|
|
21
|
+
if (signal.aborted) {
|
|
22
|
+
abort(signal);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
const listener = () => abort(signal);
|
|
26
|
+
signal.addEventListener("abort", listener, { once: true });
|
|
27
|
+
listeners.push({
|
|
28
|
+
signal,
|
|
29
|
+
listener
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
signal: controller.signal,
|
|
34
|
+
cleanup: () => {
|
|
35
|
+
for (const { signal, listener } of listeners) signal.removeEventListener("abort", listener);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#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/utils/node-http-proxy.js
|
|
41
|
+
const DEFAULT_PROXY_PORTS = {
|
|
42
|
+
ftp: 21,
|
|
43
|
+
gopher: 70,
|
|
44
|
+
http: 80,
|
|
45
|
+
https: 443,
|
|
46
|
+
ws: 80,
|
|
47
|
+
wss: 443
|
|
48
|
+
};
|
|
49
|
+
function getProxyEnv(key, env) {
|
|
50
|
+
const lowercaseKey = key.toLowerCase();
|
|
51
|
+
const uppercaseKey = key.toUpperCase();
|
|
52
|
+
return env?.[lowercaseKey] || env?.[uppercaseKey] || getProviderEnvValue(lowercaseKey) || getProviderEnvValue(uppercaseKey) || "";
|
|
53
|
+
}
|
|
54
|
+
function parseProxyTargetUrl(targetUrl) {
|
|
55
|
+
if (targetUrl instanceof URL) return targetUrl;
|
|
56
|
+
try {
|
|
57
|
+
return new URL(targetUrl);
|
|
58
|
+
} catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function shouldProxyHostname(hostname, port, env) {
|
|
63
|
+
const noProxy = getProxyEnv("no_proxy", env).toLowerCase();
|
|
64
|
+
if (!noProxy) return true;
|
|
65
|
+
if (noProxy === "*") return false;
|
|
66
|
+
return noProxy.split(/[,\s]/).every((proxy) => {
|
|
67
|
+
if (!proxy) return true;
|
|
68
|
+
const parsedProxy = proxy.match(/^(.+):(\d+)$/);
|
|
69
|
+
let proxyHostname = parsedProxy ? parsedProxy[1] : proxy;
|
|
70
|
+
const proxyPort = parsedProxy ? Number.parseInt(parsedProxy[2], 10) : 0;
|
|
71
|
+
if (proxyPort && proxyPort !== port) return true;
|
|
72
|
+
if (!/^[.*]/.test(proxyHostname)) return hostname !== proxyHostname;
|
|
73
|
+
if (proxyHostname.startsWith("*")) proxyHostname = proxyHostname.slice(1);
|
|
74
|
+
return !hostname.endsWith(proxyHostname);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function getProxyForUrl(targetUrl, env) {
|
|
78
|
+
const parsedUrl = parseProxyTargetUrl(targetUrl);
|
|
79
|
+
if (!parsedUrl?.protocol || !parsedUrl.host) return "";
|
|
80
|
+
const protocol = parsedUrl.protocol.split(":", 1)[0];
|
|
81
|
+
if (!shouldProxyHostname(parsedUrl.host.replace(/:\d*$/, ""), Number.parseInt(parsedUrl.port, 10) || DEFAULT_PROXY_PORTS[protocol] || 0, env)) return "";
|
|
82
|
+
let proxy = getProxyEnv(`${protocol}_proxy`, env) || getProxyEnv("all_proxy", env);
|
|
83
|
+
if (proxy && !proxy.includes("://")) proxy = `${protocol}://${proxy}`;
|
|
84
|
+
return proxy;
|
|
85
|
+
}
|
|
86
|
+
const UNSUPPORTED_PROXY_PROTOCOL_MESSAGE = "Unsupported proxy protocol. SOCKS and PAC proxy URLs are not supported; use an HTTP or HTTPS proxy URL.";
|
|
87
|
+
function resolveHttpProxyUrlForTarget(targetUrl, env) {
|
|
88
|
+
const proxy = getProxyForUrl(targetUrl, env);
|
|
89
|
+
if (!proxy) return;
|
|
90
|
+
let proxyUrl;
|
|
91
|
+
try {
|
|
92
|
+
proxyUrl = new URL(proxy);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw new Error(`Invalid proxy URL ${JSON.stringify(proxy)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
95
|
+
}
|
|
96
|
+
if (proxyUrl.protocol !== "http:" && proxyUrl.protocol !== "https:") throw new Error(`${UNSUPPORTED_PROXY_PROTOCOL_MESSAGE} Got ${proxyUrl.protocol}`);
|
|
97
|
+
return proxyUrl;
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#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-codex-responses.js
|
|
101
|
+
var __rewriteRelativeImportExtension = function(path, preserveJsx) {
|
|
102
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function(m, tsx, d, ext, cm) {
|
|
103
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : d + ext + "." + cm.toLowerCase() + "js";
|
|
104
|
+
});
|
|
105
|
+
return path;
|
|
106
|
+
};
|
|
107
|
+
let _os = null;
|
|
108
|
+
const dynamicImport = (specifier) => import(__rewriteRelativeImportExtension(specifier));
|
|
109
|
+
const NODE_OS_SPECIFIER = "node:os";
|
|
110
|
+
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) dynamicImport(NODE_OS_SPECIFIER).then((m) => {
|
|
111
|
+
_os = m;
|
|
112
|
+
});
|
|
113
|
+
const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
|
|
114
|
+
const JWT_CLAIM_PATH = "https://api.openai.com/auth";
|
|
115
|
+
const DEFAULT_MAX_RETRIES = 0;
|
|
116
|
+
const BASE_DELAY_MS = 1e3;
|
|
117
|
+
const DEFAULT_MAX_RETRY_DELAY_MS = 6e4;
|
|
118
|
+
const DEFAULT_SSE_HEADER_TIMEOUT_MS = 2e4;
|
|
119
|
+
const DEFAULT_WEBSOCKET_CONNECT_TIMEOUT_MS = 15e3;
|
|
120
|
+
const CODEX_TOOL_CALL_PROVIDERS = new Set([
|
|
121
|
+
"openai",
|
|
122
|
+
"openai-codex",
|
|
123
|
+
"opencode"
|
|
124
|
+
]);
|
|
125
|
+
const WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE = 1009;
|
|
126
|
+
const CODEX_RESPONSE_STATUSES = new Set([
|
|
127
|
+
"completed",
|
|
128
|
+
"incomplete",
|
|
129
|
+
"failed",
|
|
130
|
+
"cancelled",
|
|
131
|
+
"queued",
|
|
132
|
+
"in_progress"
|
|
133
|
+
]);
|
|
134
|
+
function isTerminalRateLimitError(errorText) {
|
|
135
|
+
return /GoUsageLimitError|FreeUsageLimitError|Monthly usage limit reached|available balance|insufficient_quota|out of budget|quota exceeded|billing/i.test(errorText);
|
|
136
|
+
}
|
|
137
|
+
function isRetryableError(status, errorText) {
|
|
138
|
+
if (status === 429 && isTerminalRateLimitError(errorText)) return false;
|
|
139
|
+
if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504) return true;
|
|
140
|
+
return /rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(errorText);
|
|
141
|
+
}
|
|
142
|
+
function getRetryAfterDelayMs(headers) {
|
|
143
|
+
const retryAfterMs = headers.get("retry-after-ms");
|
|
144
|
+
if (retryAfterMs !== null) {
|
|
145
|
+
const millis = Number(retryAfterMs);
|
|
146
|
+
if (Number.isFinite(millis)) return Math.max(0, millis);
|
|
147
|
+
}
|
|
148
|
+
const retryAfter = headers.get("retry-after");
|
|
149
|
+
if (!retryAfter) return;
|
|
150
|
+
const seconds = Number(retryAfter);
|
|
151
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
152
|
+
const date = Date.parse(retryAfter);
|
|
153
|
+
if (!Number.isNaN(date)) return Math.max(0, date - Date.now());
|
|
154
|
+
}
|
|
155
|
+
function capRetryDelayMs(delayMs, options) {
|
|
156
|
+
const maxRetryDelayMs = options?.maxRetryDelayMs ?? DEFAULT_MAX_RETRY_DELAY_MS;
|
|
157
|
+
return maxRetryDelayMs > 0 ? Math.min(delayMs, maxRetryDelayMs) : delayMs;
|
|
158
|
+
}
|
|
159
|
+
function sleep(ms, signal) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
if (signal?.aborted) {
|
|
162
|
+
reject(/* @__PURE__ */ new Error("Request was aborted"));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const timeout = setTimeout(resolve, ms);
|
|
166
|
+
signal?.addEventListener("abort", () => {
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
reject(/* @__PURE__ */ new Error("Request was aborted"));
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function normalizeTimeoutMs(value) {
|
|
173
|
+
if (value === void 0) return void 0;
|
|
174
|
+
if (!Number.isFinite(value) || value < 0) throw new Error(`Invalid timeoutMs: ${String(value)}`);
|
|
175
|
+
return Math.floor(value);
|
|
176
|
+
}
|
|
177
|
+
function createSSEHeaderTimeout() {
|
|
178
|
+
const controller = new AbortController();
|
|
179
|
+
let error;
|
|
180
|
+
const timeout = setTimeout(() => {
|
|
181
|
+
error = /* @__PURE__ */ new Error(`Codex SSE response headers timed out after ${DEFAULT_SSE_HEADER_TIMEOUT_MS}ms`);
|
|
182
|
+
controller.abort(error);
|
|
183
|
+
}, DEFAULT_SSE_HEADER_TIMEOUT_MS);
|
|
184
|
+
return {
|
|
185
|
+
signal: controller.signal,
|
|
186
|
+
clear: () => clearTimeout(timeout),
|
|
187
|
+
error: () => error
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const streamOpenAICodexResponses = (model, context, options) => {
|
|
191
|
+
const stream = new AssistantMessageEventStream();
|
|
192
|
+
(async () => {
|
|
193
|
+
const output = {
|
|
194
|
+
role: "assistant",
|
|
195
|
+
content: [],
|
|
196
|
+
api: "openai-codex-responses",
|
|
197
|
+
provider: model.provider,
|
|
198
|
+
model: model.id,
|
|
199
|
+
usage: {
|
|
200
|
+
input: 0,
|
|
201
|
+
output: 0,
|
|
202
|
+
cacheRead: 0,
|
|
203
|
+
cacheWrite: 0,
|
|
204
|
+
totalTokens: 0,
|
|
205
|
+
cost: {
|
|
206
|
+
input: 0,
|
|
207
|
+
output: 0,
|
|
208
|
+
cacheRead: 0,
|
|
209
|
+
cacheWrite: 0,
|
|
210
|
+
total: 0
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
stopReason: "stop",
|
|
214
|
+
timestamp: Date.now()
|
|
215
|
+
};
|
|
216
|
+
try {
|
|
217
|
+
const apiKey = options?.apiKey;
|
|
218
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
219
|
+
const accountId = extractAccountId(apiKey);
|
|
220
|
+
let body = buildRequestBody(model, context, options);
|
|
221
|
+
const nextBody = await options?.onPayload?.(body, model);
|
|
222
|
+
if (nextBody !== void 0) body = nextBody;
|
|
223
|
+
const websocketRequestId = options?.sessionId || createCodexRequestId();
|
|
224
|
+
const sseHeaders = buildSSEHeaders(model.headers, options?.headers, accountId, apiKey, options?.sessionId);
|
|
225
|
+
const websocketHeaders = buildWebSocketHeaders(model.headers, options?.headers, accountId, apiKey, websocketRequestId);
|
|
226
|
+
const bodyJson = JSON.stringify(body);
|
|
227
|
+
const idleTimeoutMs = normalizeTimeoutMs(options?.timeoutMs);
|
|
228
|
+
const websocketConnectTimeoutMs = normalizeTimeoutMs(options?.websocketConnectTimeoutMs);
|
|
229
|
+
const transport = options?.transport || "auto";
|
|
230
|
+
const websocketDisabledForSession = transport !== "sse" && isWebSocketSseFallbackActive(options?.sessionId);
|
|
231
|
+
if (websocketDisabledForSession) recordWebSocketSseFallback(options?.sessionId);
|
|
232
|
+
if (transport !== "sse" && !websocketDisabledForSession) {
|
|
233
|
+
let websocketStarted = false;
|
|
234
|
+
try {
|
|
235
|
+
await processWebSocketStream(resolveCodexWebSocketUrl(model.baseUrl), body, websocketHeaders, output, stream, model, () => {
|
|
236
|
+
websocketStarted = true;
|
|
237
|
+
}, idleTimeoutMs, websocketConnectTimeoutMs, options);
|
|
238
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
239
|
+
stream.push({
|
|
240
|
+
type: "done",
|
|
241
|
+
reason: output.stopReason,
|
|
242
|
+
message: output
|
|
243
|
+
});
|
|
244
|
+
stream.end();
|
|
245
|
+
return;
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (options?.signal?.aborted || isCodexNonTransportError(error)) throw error;
|
|
248
|
+
appendAssistantMessageDiagnostic(output, createAssistantMessageDiagnostic("provider_transport_failure", error, {
|
|
249
|
+
configuredTransport: transport,
|
|
250
|
+
fallbackTransport: websocketStarted ? void 0 : "sse",
|
|
251
|
+
eventsEmitted: websocketStarted,
|
|
252
|
+
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
253
|
+
requestBytes: new TextEncoder().encode(bodyJson).byteLength
|
|
254
|
+
}));
|
|
255
|
+
recordWebSocketFailure(options?.sessionId, error);
|
|
256
|
+
if (websocketStarted) throw error;
|
|
257
|
+
recordWebSocketSseFallback(options?.sessionId);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
let response;
|
|
261
|
+
let lastError;
|
|
262
|
+
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
263
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
264
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
265
|
+
try {
|
|
266
|
+
const headerTimeout = createSSEHeaderTimeout();
|
|
267
|
+
const combinedSignal = combineAbortSignals([options?.signal, headerTimeout.signal]);
|
|
268
|
+
try {
|
|
269
|
+
response = await fetch(resolveCodexUrl(model.baseUrl), {
|
|
270
|
+
method: "POST",
|
|
271
|
+
headers: sseHeaders,
|
|
272
|
+
body: bodyJson,
|
|
273
|
+
signal: combinedSignal.signal
|
|
274
|
+
});
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const timeoutError = headerTimeout.error();
|
|
277
|
+
throw timeoutError && !options?.signal?.aborted ? timeoutError : error;
|
|
278
|
+
} finally {
|
|
279
|
+
combinedSignal.cleanup();
|
|
280
|
+
headerTimeout.clear();
|
|
281
|
+
}
|
|
282
|
+
await options?.onResponse?.({
|
|
283
|
+
status: response.status,
|
|
284
|
+
headers: headersToRecord(response.headers)
|
|
285
|
+
}, model);
|
|
286
|
+
if (response.ok) break;
|
|
287
|
+
const errorText = await response.text();
|
|
288
|
+
if (attempt < maxRetries && isRetryableError(response.status, errorText)) {
|
|
289
|
+
const retryAfterDelayMs = getRetryAfterDelayMs(response.headers);
|
|
290
|
+
await sleep(retryAfterDelayMs === void 0 ? BASE_DELAY_MS * 2 ** attempt : response.status === 429 ? capRetryDelayMs(retryAfterDelayMs, options) : retryAfterDelayMs, options?.signal);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const info = await parseErrorResponse(new Response(errorText, {
|
|
294
|
+
status: response.status,
|
|
295
|
+
statusText: response.statusText
|
|
296
|
+
}));
|
|
297
|
+
throw new Error(info.friendlyMessage || info.message);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
if (error instanceof Error) {
|
|
300
|
+
if (error.name === "AbortError" || error.message === "Request was aborted") throw new Error("Request was aborted");
|
|
301
|
+
}
|
|
302
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
303
|
+
if (attempt < maxRetries && !lastError.message.includes("usage limit")) {
|
|
304
|
+
await sleep(BASE_DELAY_MS * 2 ** attempt, options?.signal);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
throw lastError;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (!response?.ok) throw lastError ?? /* @__PURE__ */ new Error("Failed after retries");
|
|
311
|
+
if (!response.body) throw new Error("No response body");
|
|
312
|
+
stream.push({
|
|
313
|
+
type: "start",
|
|
314
|
+
partial: output
|
|
315
|
+
});
|
|
316
|
+
await processStream(response, output, stream, model, options);
|
|
317
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
318
|
+
stream.push({
|
|
319
|
+
type: "done",
|
|
320
|
+
reason: output.stopReason,
|
|
321
|
+
message: output
|
|
322
|
+
});
|
|
323
|
+
stream.end();
|
|
324
|
+
} catch (error) {
|
|
325
|
+
for (const block of output.content) delete block.partialJson;
|
|
326
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
327
|
+
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
328
|
+
stream.push({
|
|
329
|
+
type: "error",
|
|
330
|
+
reason: output.stopReason,
|
|
331
|
+
error: output
|
|
332
|
+
});
|
|
333
|
+
stream.end();
|
|
334
|
+
}
|
|
335
|
+
})();
|
|
336
|
+
return stream;
|
|
337
|
+
};
|
|
338
|
+
const streamSimpleOpenAICodexResponses = (model, context, options) => {
|
|
339
|
+
const apiKey = options?.apiKey;
|
|
340
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
341
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
342
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
343
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning;
|
|
344
|
+
return streamOpenAICodexResponses(model, context, {
|
|
345
|
+
...base,
|
|
346
|
+
reasoningEffort
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
function register() {
|
|
350
|
+
registerApiProvider({
|
|
351
|
+
api: "openai-codex-responses",
|
|
352
|
+
stream: streamOpenAICodexResponses,
|
|
353
|
+
streamSimple: streamSimpleOpenAICodexResponses
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
function buildRequestBody(model, context, options) {
|
|
357
|
+
const messages = convertResponsesMessages(model, context, CODEX_TOOL_CALL_PROVIDERS, { includeSystemPrompt: false });
|
|
358
|
+
const body = {
|
|
359
|
+
model: model.id,
|
|
360
|
+
store: false,
|
|
361
|
+
stream: true,
|
|
362
|
+
instructions: context.systemPrompt || "You are a helpful assistant.",
|
|
363
|
+
input: messages,
|
|
364
|
+
text: { verbosity: options?.textVerbosity || "low" },
|
|
365
|
+
include: ["reasoning.encrypted_content"],
|
|
366
|
+
prompt_cache_key: clampOpenAIPromptCacheKey(options?.sessionId),
|
|
367
|
+
tool_choice: "auto",
|
|
368
|
+
parallel_tool_calls: true
|
|
369
|
+
};
|
|
370
|
+
if (options?.temperature !== void 0) body.temperature = options.temperature;
|
|
371
|
+
if (options?.serviceTier !== void 0) body.service_tier = options.serviceTier;
|
|
372
|
+
if (context.tools && context.tools.length > 0) body.tools = convertResponsesTools(context.tools, { strict: null });
|
|
373
|
+
if (options?.reasoningEffort !== void 0) {
|
|
374
|
+
const effort = options.reasoningEffort === "none" ? model.thinkingLevelMap?.off ?? "none" : model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
375
|
+
if (effort !== null) body.reasoning = {
|
|
376
|
+
effort,
|
|
377
|
+
summary: options.reasoningSummary ?? "auto"
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
return body;
|
|
381
|
+
}
|
|
382
|
+
function getServiceTierCostMultiplier(model, serviceTier) {
|
|
383
|
+
switch (serviceTier) {
|
|
384
|
+
case "flex": return .5;
|
|
385
|
+
case "priority": return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
386
|
+
default: return 1;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
function applyServiceTierPricing(usage, serviceTier, model) {
|
|
390
|
+
const multiplier = getServiceTierCostMultiplier(model, serviceTier);
|
|
391
|
+
if (multiplier === 1) return;
|
|
392
|
+
usage.cost.input *= multiplier;
|
|
393
|
+
usage.cost.output *= multiplier;
|
|
394
|
+
usage.cost.cacheRead *= multiplier;
|
|
395
|
+
usage.cost.cacheWrite *= multiplier;
|
|
396
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
397
|
+
}
|
|
398
|
+
function resolveCodexServiceTier(responseServiceTier, requestServiceTier) {
|
|
399
|
+
if (responseServiceTier === "default" && (requestServiceTier === "flex" || requestServiceTier === "priority")) return requestServiceTier;
|
|
400
|
+
return responseServiceTier ?? requestServiceTier;
|
|
401
|
+
}
|
|
402
|
+
function resolveCodexUrl(baseUrl) {
|
|
403
|
+
const normalized = (baseUrl && baseUrl.trim().length > 0 ? baseUrl : DEFAULT_CODEX_BASE_URL).replace(/\/+$/, "");
|
|
404
|
+
if (normalized.endsWith("/codex/responses")) return normalized;
|
|
405
|
+
if (normalized.endsWith("/codex")) return `${normalized}/responses`;
|
|
406
|
+
return `${normalized}/codex/responses`;
|
|
407
|
+
}
|
|
408
|
+
function resolveCodexWebSocketUrl(baseUrl) {
|
|
409
|
+
const url = new URL(resolveCodexUrl(baseUrl));
|
|
410
|
+
if (url.protocol === "https:") url.protocol = "wss:";
|
|
411
|
+
if (url.protocol === "http:") url.protocol = "ws:";
|
|
412
|
+
return url.toString();
|
|
413
|
+
}
|
|
414
|
+
async function processStream(response, output, stream, model, options) {
|
|
415
|
+
await processResponsesStream(mapCodexEvents(parseSSE(response, options?.signal)), output, stream, model, {
|
|
416
|
+
serviceTier: options?.serviceTier,
|
|
417
|
+
resolveServiceTier: resolveCodexServiceTier,
|
|
418
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model)
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
var CodexApiError = class extends Error {
|
|
422
|
+
code;
|
|
423
|
+
payload;
|
|
424
|
+
constructor(message, options) {
|
|
425
|
+
super(message);
|
|
426
|
+
this.name = "CodexApiError";
|
|
427
|
+
this.code = options?.code;
|
|
428
|
+
this.payload = options?.payload;
|
|
429
|
+
this.cause = options?.cause;
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
var CodexProtocolError = class extends Error {
|
|
433
|
+
payload;
|
|
434
|
+
constructor(message, options) {
|
|
435
|
+
super(message);
|
|
436
|
+
this.name = "CodexProtocolError";
|
|
437
|
+
this.payload = options?.payload;
|
|
438
|
+
this.cause = options?.cause;
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
function isCodexNonTransportError(error) {
|
|
442
|
+
return error instanceof CodexApiError || error instanceof CodexProtocolError;
|
|
443
|
+
}
|
|
444
|
+
async function* mapCodexEvents(events) {
|
|
445
|
+
for await (const event of events) {
|
|
446
|
+
const type = typeof event.type === "string" ? event.type : void 0;
|
|
447
|
+
if (!type) continue;
|
|
448
|
+
if (type === "error") {
|
|
449
|
+
const code = event.code || "";
|
|
450
|
+
throw new CodexApiError(`Codex error: ${event.message || code || JSON.stringify(event)}`, {
|
|
451
|
+
code: code || void 0,
|
|
452
|
+
payload: event
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
if (type === "response.failed") {
|
|
456
|
+
const response = event.response;
|
|
457
|
+
const code = response?.error?.code;
|
|
458
|
+
const message = response?.error?.message;
|
|
459
|
+
throw new CodexApiError(message || "Codex response failed", {
|
|
460
|
+
code,
|
|
461
|
+
payload: event
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
if (type === "response.done" || type === "response.completed" || type === "response.incomplete") {
|
|
465
|
+
const response = event.response;
|
|
466
|
+
const normalizedResponse = response ? {
|
|
467
|
+
...response,
|
|
468
|
+
status: normalizeCodexStatus(response.status)
|
|
469
|
+
} : response;
|
|
470
|
+
yield {
|
|
471
|
+
...event,
|
|
472
|
+
type: "response.completed",
|
|
473
|
+
response: normalizedResponse
|
|
474
|
+
};
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
yield event;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function normalizeCodexStatus(status) {
|
|
481
|
+
if (typeof status !== "string") return void 0;
|
|
482
|
+
return CODEX_RESPONSE_STATUSES.has(status) ? status : void 0;
|
|
483
|
+
}
|
|
484
|
+
async function* parseSSE(response, signal) {
|
|
485
|
+
if (!response.body) return;
|
|
486
|
+
const reader = response.body.getReader();
|
|
487
|
+
const decoder = new TextDecoder();
|
|
488
|
+
let buffer = "";
|
|
489
|
+
const onAbort = () => {
|
|
490
|
+
reader.cancel().catch(() => {});
|
|
491
|
+
};
|
|
492
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
493
|
+
try {
|
|
494
|
+
while (true) {
|
|
495
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
496
|
+
const { done, value } = await reader.read();
|
|
497
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
498
|
+
if (done) break;
|
|
499
|
+
buffer += decoder.decode(value, { stream: true });
|
|
500
|
+
let idx = buffer.indexOf("\n\n");
|
|
501
|
+
while (idx !== -1) {
|
|
502
|
+
const chunk = buffer.slice(0, idx);
|
|
503
|
+
buffer = buffer.slice(idx + 2);
|
|
504
|
+
const dataLines = chunk.split("\n").filter((l) => l.startsWith("data:")).map((l) => l.slice(5).trim());
|
|
505
|
+
if (dataLines.length > 0) {
|
|
506
|
+
const data = dataLines.join("\n").trim();
|
|
507
|
+
if (data && data !== "[DONE]") try {
|
|
508
|
+
yield JSON.parse(data);
|
|
509
|
+
} catch (cause) {
|
|
510
|
+
throw new CodexProtocolError(`Invalid Codex SSE JSON: ${formatThrownValue(cause)}`, {
|
|
511
|
+
cause,
|
|
512
|
+
payload: data
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
idx = buffer.indexOf("\n\n");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
} finally {
|
|
520
|
+
signal?.removeEventListener("abort", onAbort);
|
|
521
|
+
try {
|
|
522
|
+
await reader.cancel();
|
|
523
|
+
} catch {}
|
|
524
|
+
try {
|
|
525
|
+
reader.releaseLock();
|
|
526
|
+
} catch {}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const OPENAI_BETA_RESPONSES_WEBSOCKETS = "responses_websockets=2026-02-06";
|
|
530
|
+
const SESSION_WEBSOCKET_CACHE_TTL_MS = 300 * 1e3;
|
|
531
|
+
const websocketSessionCache = /* @__PURE__ */ new Map();
|
|
532
|
+
const websocketDebugStats = /* @__PURE__ */ new Map();
|
|
533
|
+
const websocketSseFallbackSessions = /* @__PURE__ */ new Set();
|
|
534
|
+
function getOrCreateWebSocketDebugStats(sessionId) {
|
|
535
|
+
let stats = websocketDebugStats.get(sessionId);
|
|
536
|
+
if (!stats) {
|
|
537
|
+
stats = {
|
|
538
|
+
requests: 0,
|
|
539
|
+
connectionsCreated: 0,
|
|
540
|
+
connectionsReused: 0,
|
|
541
|
+
cachedContextRequests: 0,
|
|
542
|
+
storeTrueRequests: 0,
|
|
543
|
+
fullContextRequests: 0,
|
|
544
|
+
deltaRequests: 0,
|
|
545
|
+
lastInputItems: 0,
|
|
546
|
+
websocketFailures: 0,
|
|
547
|
+
sseFallbacks: 0
|
|
548
|
+
};
|
|
549
|
+
websocketDebugStats.set(sessionId, stats);
|
|
550
|
+
}
|
|
551
|
+
return stats;
|
|
552
|
+
}
|
|
553
|
+
function getOpenAICodexWebSocketDebugStats(sessionId) {
|
|
554
|
+
const stats = websocketDebugStats.get(sessionId);
|
|
555
|
+
return stats ? { ...stats } : void 0;
|
|
556
|
+
}
|
|
557
|
+
function resetOpenAICodexWebSocketDebugStats(sessionId) {
|
|
558
|
+
if (sessionId) {
|
|
559
|
+
websocketDebugStats.delete(sessionId);
|
|
560
|
+
websocketSseFallbackSessions.delete(sessionId);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
websocketDebugStats.clear();
|
|
564
|
+
websocketSseFallbackSessions.clear();
|
|
565
|
+
}
|
|
566
|
+
function closeOpenAICodexWebSocketSessions(sessionId) {
|
|
567
|
+
const closeEntry = (entry) => {
|
|
568
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
569
|
+
closeWebSocketSilently(entry.socket, 1e3, "debug_close");
|
|
570
|
+
};
|
|
571
|
+
if (sessionId) {
|
|
572
|
+
const entry = websocketSessionCache.get(sessionId);
|
|
573
|
+
if (entry) closeEntry(entry);
|
|
574
|
+
websocketSessionCache.delete(sessionId);
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
for (const entry of websocketSessionCache.values()) closeEntry(entry);
|
|
578
|
+
websocketSessionCache.clear();
|
|
579
|
+
}
|
|
580
|
+
registerSessionResourceCleanup(closeOpenAICodexWebSocketSessions);
|
|
581
|
+
function isWebSocketSseFallbackActive(sessionId) {
|
|
582
|
+
return sessionId ? websocketSseFallbackSessions.has(sessionId) : false;
|
|
583
|
+
}
|
|
584
|
+
function recordWebSocketSseFallback(sessionId) {
|
|
585
|
+
if (!sessionId) return;
|
|
586
|
+
const stats = getOrCreateWebSocketDebugStats(sessionId);
|
|
587
|
+
stats.sseFallbacks++;
|
|
588
|
+
stats.websocketFallbackActive = isWebSocketSseFallbackActive(sessionId);
|
|
589
|
+
}
|
|
590
|
+
function recordWebSocketFailure(sessionId, error) {
|
|
591
|
+
if (!sessionId) return;
|
|
592
|
+
websocketSseFallbackSessions.add(sessionId);
|
|
593
|
+
const stats = getOrCreateWebSocketDebugStats(sessionId);
|
|
594
|
+
stats.websocketFailures++;
|
|
595
|
+
stats.lastWebSocketError = formatThrownValue(error);
|
|
596
|
+
stats.websocketFallbackActive = true;
|
|
597
|
+
}
|
|
598
|
+
let _cachedWebsocket = null;
|
|
599
|
+
async function getWebSocketConstructor(env) {
|
|
600
|
+
if (!env && _cachedWebsocket) return _cachedWebsocket;
|
|
601
|
+
if (typeof process !== "undefined" && process.versions?.bun) {
|
|
602
|
+
const WebSocketWithProxy = class extends WebSocket {
|
|
603
|
+
constructor(url, options) {
|
|
604
|
+
let _opts = {};
|
|
605
|
+
if (Array.isArray(options) || typeof options === "string") _opts = { protocols: options };
|
|
606
|
+
else _opts = { ...options };
|
|
607
|
+
const proxyUrl = resolveHttpProxyUrlForTarget(url.toString().replace(/^wss:/, "https:").replace(/^ws:/, "http:"), env);
|
|
608
|
+
super(url, {
|
|
609
|
+
..._opts,
|
|
610
|
+
...proxyUrl ? { proxy: proxyUrl.toString() } : {}
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
if (!env) _cachedWebsocket = WebSocketWithProxy;
|
|
615
|
+
return WebSocketWithProxy;
|
|
616
|
+
}
|
|
617
|
+
const ctor = globalThis.WebSocket;
|
|
618
|
+
if (typeof ctor !== "function") return null;
|
|
619
|
+
return ctor;
|
|
620
|
+
}
|
|
621
|
+
var WebSocketCloseError = class extends Error {
|
|
622
|
+
code;
|
|
623
|
+
reason;
|
|
624
|
+
wasClean;
|
|
625
|
+
constructor(message, options) {
|
|
626
|
+
super(message);
|
|
627
|
+
this.name = "WebSocketCloseError";
|
|
628
|
+
this.code = options?.code;
|
|
629
|
+
this.reason = options?.reason;
|
|
630
|
+
this.wasClean = options?.wasClean;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
function getWebSocketReadyState(socket) {
|
|
634
|
+
const readyState = socket.readyState;
|
|
635
|
+
return typeof readyState === "number" ? readyState : void 0;
|
|
636
|
+
}
|
|
637
|
+
function isWebSocketReusable(socket) {
|
|
638
|
+
const readyState = getWebSocketReadyState(socket);
|
|
639
|
+
return readyState === void 0 || readyState === 1;
|
|
640
|
+
}
|
|
641
|
+
function closeWebSocketSilently(socket, code = 1e3, reason = "done") {
|
|
642
|
+
try {
|
|
643
|
+
socket.close(code, reason);
|
|
644
|
+
} catch {}
|
|
645
|
+
}
|
|
646
|
+
function scheduleSessionWebSocketExpiry(sessionId, entry) {
|
|
647
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
648
|
+
entry.idleTimer = setTimeout(() => {
|
|
649
|
+
if (entry.busy) return;
|
|
650
|
+
closeWebSocketSilently(entry.socket, 1e3, "idle_timeout");
|
|
651
|
+
websocketSessionCache.delete(sessionId);
|
|
652
|
+
}, SESSION_WEBSOCKET_CACHE_TTL_MS);
|
|
653
|
+
}
|
|
654
|
+
async function connectWebSocket(url, headers, signal, connectTimeoutMs = DEFAULT_WEBSOCKET_CONNECT_TIMEOUT_MS, env) {
|
|
655
|
+
const WebSocketCtor = await getWebSocketConstructor(env);
|
|
656
|
+
if (!WebSocketCtor) throw new Error("WebSocket transport is not available in this runtime");
|
|
657
|
+
const wsHeaders = headersToRecord(headers);
|
|
658
|
+
delete wsHeaders["OpenAI-Beta"];
|
|
659
|
+
return new Promise((resolve, reject) => {
|
|
660
|
+
let settled = false;
|
|
661
|
+
let timeout;
|
|
662
|
+
let socket;
|
|
663
|
+
try {
|
|
664
|
+
socket = new WebSocketCtor(url, { headers: wsHeaders });
|
|
665
|
+
} catch (error) {
|
|
666
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const cleanup = () => {
|
|
670
|
+
if (timeout) {
|
|
671
|
+
clearTimeout(timeout);
|
|
672
|
+
timeout = void 0;
|
|
673
|
+
}
|
|
674
|
+
socket.removeEventListener("open", onOpen);
|
|
675
|
+
socket.removeEventListener("error", onError);
|
|
676
|
+
socket.removeEventListener("close", onClose);
|
|
677
|
+
signal?.removeEventListener("abort", onAbort);
|
|
678
|
+
};
|
|
679
|
+
const fail = (error, closeReason) => {
|
|
680
|
+
if (settled) return;
|
|
681
|
+
settled = true;
|
|
682
|
+
cleanup();
|
|
683
|
+
if (closeReason) closeWebSocketSilently(socket, 1e3, closeReason);
|
|
684
|
+
reject(error);
|
|
685
|
+
};
|
|
686
|
+
const onOpen = () => {
|
|
687
|
+
if (settled) return;
|
|
688
|
+
settled = true;
|
|
689
|
+
cleanup();
|
|
690
|
+
resolve(socket);
|
|
691
|
+
};
|
|
692
|
+
const onError = (event) => {
|
|
693
|
+
fail(extractWebSocketError(event));
|
|
694
|
+
};
|
|
695
|
+
const onClose = (event) => {
|
|
696
|
+
fail(extractWebSocketCloseError(event));
|
|
697
|
+
};
|
|
698
|
+
const onAbort = () => {
|
|
699
|
+
fail(/* @__PURE__ */ new Error("Request was aborted"), "aborted");
|
|
700
|
+
};
|
|
701
|
+
socket.addEventListener("open", onOpen);
|
|
702
|
+
socket.addEventListener("error", onError);
|
|
703
|
+
socket.addEventListener("close", onClose);
|
|
704
|
+
signal?.addEventListener("abort", onAbort);
|
|
705
|
+
if (connectTimeoutMs > 0) timeout = setTimeout(() => {
|
|
706
|
+
fail(/* @__PURE__ */ new Error(`WebSocket connect timeout after ${connectTimeoutMs}ms`), "connect_timeout");
|
|
707
|
+
}, connectTimeoutMs);
|
|
708
|
+
if (signal?.aborted) onAbort();
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
async function acquireWebSocket(url, headers, sessionId, signal, connectTimeoutMs, env) {
|
|
712
|
+
if (!sessionId) {
|
|
713
|
+
const socket = await connectWebSocket(url, headers, signal, connectTimeoutMs, env);
|
|
714
|
+
return {
|
|
715
|
+
socket,
|
|
716
|
+
reused: false,
|
|
717
|
+
release: () => closeWebSocketSilently(socket)
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
const cached = websocketSessionCache.get(sessionId);
|
|
721
|
+
if (cached) {
|
|
722
|
+
if (cached.idleTimer) {
|
|
723
|
+
clearTimeout(cached.idleTimer);
|
|
724
|
+
cached.idleTimer = void 0;
|
|
725
|
+
}
|
|
726
|
+
if (!cached.busy && isWebSocketReusable(cached.socket)) {
|
|
727
|
+
cached.busy = true;
|
|
728
|
+
return {
|
|
729
|
+
socket: cached.socket,
|
|
730
|
+
entry: cached,
|
|
731
|
+
reused: true,
|
|
732
|
+
release: ({ keep } = {}) => {
|
|
733
|
+
if (!keep || !isWebSocketReusable(cached.socket)) {
|
|
734
|
+
closeWebSocketSilently(cached.socket);
|
|
735
|
+
websocketSessionCache.delete(sessionId);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
cached.busy = false;
|
|
739
|
+
scheduleSessionWebSocketExpiry(sessionId, cached);
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
if (cached.busy) {
|
|
744
|
+
const socket = await connectWebSocket(url, headers, signal, connectTimeoutMs, env);
|
|
745
|
+
return {
|
|
746
|
+
socket,
|
|
747
|
+
reused: false,
|
|
748
|
+
release: () => {
|
|
749
|
+
closeWebSocketSilently(socket);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
if (!isWebSocketReusable(cached.socket)) {
|
|
754
|
+
closeWebSocketSilently(cached.socket);
|
|
755
|
+
websocketSessionCache.delete(sessionId);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
const socket = await connectWebSocket(url, headers, signal, connectTimeoutMs, env);
|
|
759
|
+
const entry = {
|
|
760
|
+
socket,
|
|
761
|
+
busy: true
|
|
762
|
+
};
|
|
763
|
+
websocketSessionCache.set(sessionId, entry);
|
|
764
|
+
return {
|
|
765
|
+
socket,
|
|
766
|
+
entry,
|
|
767
|
+
reused: false,
|
|
768
|
+
release: ({ keep } = {}) => {
|
|
769
|
+
if (!keep || !isWebSocketReusable(entry.socket)) {
|
|
770
|
+
closeWebSocketSilently(entry.socket);
|
|
771
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
772
|
+
if (websocketSessionCache.get(sessionId) === entry) websocketSessionCache.delete(sessionId);
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
entry.busy = false;
|
|
776
|
+
scheduleSessionWebSocketExpiry(sessionId, entry);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
function extractWebSocketError(event) {
|
|
781
|
+
if (event && typeof event === "object") {
|
|
782
|
+
const message = "message" in event ? event.message : void 0;
|
|
783
|
+
if (typeof message === "string" && message.length > 0) return new Error(message);
|
|
784
|
+
const nestedError = "error" in event ? event.error : void 0;
|
|
785
|
+
if (nestedError instanceof Error && nestedError.message.length > 0) return nestedError;
|
|
786
|
+
if (nestedError && typeof nestedError === "object" && "message" in nestedError) {
|
|
787
|
+
const nestedMessage = nestedError.message;
|
|
788
|
+
if (typeof nestedMessage === "string" && nestedMessage.length > 0) return new Error(nestedMessage);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
return /* @__PURE__ */ new Error("WebSocket error");
|
|
792
|
+
}
|
|
793
|
+
function extractWebSocketCloseError(event) {
|
|
794
|
+
if (event && typeof event === "object") {
|
|
795
|
+
const code = "code" in event ? event.code : void 0;
|
|
796
|
+
const reason = "reason" in event ? event.reason : void 0;
|
|
797
|
+
const wasClean = "wasClean" in event ? event.wasClean : void 0;
|
|
798
|
+
const codeText = typeof code === "number" ? ` ${code}` : "";
|
|
799
|
+
let reasonText = typeof reason === "string" && reason.length > 0 ? ` ${reason}` : "";
|
|
800
|
+
if (!reasonText && code === WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE) reasonText = " message too big";
|
|
801
|
+
return new WebSocketCloseError(`WebSocket closed${codeText}${reasonText}`.trim(), {
|
|
802
|
+
code: typeof code === "number" ? code : void 0,
|
|
803
|
+
reason: typeof reason === "string" && reason.length > 0 ? reason : void 0,
|
|
804
|
+
wasClean: typeof wasClean === "boolean" ? wasClean : void 0
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
return /* @__PURE__ */ new Error("WebSocket closed");
|
|
808
|
+
}
|
|
809
|
+
async function decodeWebSocketData(data) {
|
|
810
|
+
if (typeof data === "string") return data;
|
|
811
|
+
if (data instanceof ArrayBuffer) return new TextDecoder().decode(new Uint8Array(data));
|
|
812
|
+
if (ArrayBuffer.isView(data)) {
|
|
813
|
+
const view = data;
|
|
814
|
+
return new TextDecoder().decode(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
|
|
815
|
+
}
|
|
816
|
+
if (data && typeof data === "object" && "arrayBuffer" in data) {
|
|
817
|
+
const arrayBuffer = await data.arrayBuffer();
|
|
818
|
+
return new TextDecoder().decode(new Uint8Array(arrayBuffer));
|
|
819
|
+
}
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
async function* parseWebSocket(socket, signal, idleTimeoutMs) {
|
|
823
|
+
const queue = [];
|
|
824
|
+
let pending = null;
|
|
825
|
+
let done = false;
|
|
826
|
+
let failed = null;
|
|
827
|
+
let sawCompletion = false;
|
|
828
|
+
const wake = () => {
|
|
829
|
+
if (!pending) return;
|
|
830
|
+
const resolve = pending;
|
|
831
|
+
pending = null;
|
|
832
|
+
resolve();
|
|
833
|
+
};
|
|
834
|
+
const onMessage = (event) => {
|
|
835
|
+
(async () => {
|
|
836
|
+
let text = null;
|
|
837
|
+
try {
|
|
838
|
+
if (!event || typeof event !== "object" || !("data" in event)) return;
|
|
839
|
+
text = await decodeWebSocketData(event.data);
|
|
840
|
+
if (!text) return;
|
|
841
|
+
const parsed = JSON.parse(text);
|
|
842
|
+
const type = typeof parsed.type === "string" ? parsed.type : "";
|
|
843
|
+
if (type === "response.completed" || type === "response.done" || type === "response.incomplete") {
|
|
844
|
+
sawCompletion = true;
|
|
845
|
+
done = true;
|
|
846
|
+
}
|
|
847
|
+
queue.push(parsed);
|
|
848
|
+
wake();
|
|
849
|
+
} catch (cause) {
|
|
850
|
+
failed = new CodexProtocolError(`Invalid Codex WebSocket JSON: ${formatThrownValue(cause)}`, {
|
|
851
|
+
cause,
|
|
852
|
+
payload: text
|
|
853
|
+
});
|
|
854
|
+
done = true;
|
|
855
|
+
wake();
|
|
856
|
+
}
|
|
857
|
+
})();
|
|
858
|
+
};
|
|
859
|
+
const onError = (event) => {
|
|
860
|
+
failed = extractWebSocketError(event);
|
|
861
|
+
done = true;
|
|
862
|
+
wake();
|
|
863
|
+
};
|
|
864
|
+
const onClose = (event) => {
|
|
865
|
+
if (sawCompletion) {
|
|
866
|
+
done = true;
|
|
867
|
+
wake();
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (!failed) failed = extractWebSocketCloseError(event);
|
|
871
|
+
done = true;
|
|
872
|
+
wake();
|
|
873
|
+
};
|
|
874
|
+
const onAbort = () => {
|
|
875
|
+
failed = /* @__PURE__ */ new Error("Request was aborted");
|
|
876
|
+
done = true;
|
|
877
|
+
wake();
|
|
878
|
+
};
|
|
879
|
+
socket.addEventListener("message", onMessage);
|
|
880
|
+
socket.addEventListener("error", onError);
|
|
881
|
+
socket.addEventListener("close", onClose);
|
|
882
|
+
signal?.addEventListener("abort", onAbort);
|
|
883
|
+
try {
|
|
884
|
+
while (true) {
|
|
885
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
886
|
+
if (queue.length > 0) {
|
|
887
|
+
yield queue.shift();
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
if (done) break;
|
|
891
|
+
let timeout;
|
|
892
|
+
await new Promise((resolve, reject) => {
|
|
893
|
+
pending = resolve;
|
|
894
|
+
if (idleTimeoutMs !== void 0 && idleTimeoutMs > 0) timeout = setTimeout(() => {
|
|
895
|
+
const error = /* @__PURE__ */ new Error(`WebSocket idle timeout after ${idleTimeoutMs}ms`);
|
|
896
|
+
failed = error;
|
|
897
|
+
done = true;
|
|
898
|
+
pending = null;
|
|
899
|
+
closeWebSocketSilently(socket, 1e3, "idle_timeout");
|
|
900
|
+
reject(error);
|
|
901
|
+
}, idleTimeoutMs);
|
|
902
|
+
}).finally(() => {
|
|
903
|
+
if (timeout) clearTimeout(timeout);
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
if (failed) throw failed;
|
|
907
|
+
if (!sawCompletion) throw new Error("WebSocket stream closed before response.completed");
|
|
908
|
+
} finally {
|
|
909
|
+
socket.removeEventListener("message", onMessage);
|
|
910
|
+
socket.removeEventListener("error", onError);
|
|
911
|
+
socket.removeEventListener("close", onClose);
|
|
912
|
+
signal?.removeEventListener("abort", onAbort);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
function requestBodyWithoutInput(body) {
|
|
916
|
+
const { input: _input, previous_response_id: _previousResponseId, ...rest } = body;
|
|
917
|
+
return rest;
|
|
918
|
+
}
|
|
919
|
+
function responseInputsEqual(a, b) {
|
|
920
|
+
return JSON.stringify(a ?? []) === JSON.stringify(b ?? []);
|
|
921
|
+
}
|
|
922
|
+
function requestBodiesMatchExceptInput(a, b) {
|
|
923
|
+
return JSON.stringify(requestBodyWithoutInput(a)) === JSON.stringify(requestBodyWithoutInput(b));
|
|
924
|
+
}
|
|
925
|
+
function getCachedWebSocketInputDelta(body, continuation) {
|
|
926
|
+
if (!requestBodiesMatchExceptInput(body, continuation.lastRequestBody)) return;
|
|
927
|
+
const currentInput = body.input ?? [];
|
|
928
|
+
const baseline = [...continuation.lastRequestBody.input ?? [], ...continuation.lastResponseItems];
|
|
929
|
+
if (currentInput.length < baseline.length) return;
|
|
930
|
+
if (!responseInputsEqual(currentInput.slice(0, baseline.length), baseline)) return;
|
|
931
|
+
return currentInput.slice(baseline.length);
|
|
932
|
+
}
|
|
933
|
+
function buildCachedWebSocketRequestBody(entry, body) {
|
|
934
|
+
const continuation = entry.continuation;
|
|
935
|
+
if (!continuation) return body;
|
|
936
|
+
const delta = getCachedWebSocketInputDelta(body, continuation);
|
|
937
|
+
if (!delta || !continuation.lastResponseId) {
|
|
938
|
+
entry.continuation = void 0;
|
|
939
|
+
return body;
|
|
940
|
+
}
|
|
941
|
+
return {
|
|
942
|
+
...body,
|
|
943
|
+
previous_response_id: continuation.lastResponseId,
|
|
944
|
+
input: delta
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
async function* startWebSocketOutputOnFirstEvent(events, output, stream, onStart) {
|
|
948
|
+
let started = false;
|
|
949
|
+
for await (const event of events) {
|
|
950
|
+
if (!started) {
|
|
951
|
+
started = true;
|
|
952
|
+
onStart();
|
|
953
|
+
stream.push({
|
|
954
|
+
type: "start",
|
|
955
|
+
partial: output
|
|
956
|
+
});
|
|
957
|
+
}
|
|
958
|
+
yield event;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
async function processWebSocketStream(url, body, headers, output, stream, model, onStart, idleTimeoutMs, websocketConnectTimeoutMs, options) {
|
|
962
|
+
const { socket, entry, reused, release } = await acquireWebSocket(url, headers, options?.sessionId, options?.signal, websocketConnectTimeoutMs, options?.env);
|
|
963
|
+
let keepConnection = true;
|
|
964
|
+
const useCachedContext = options?.transport === "websocket-cached" || options?.transport === "auto";
|
|
965
|
+
const fullBody = body;
|
|
966
|
+
const requestBody = useCachedContext && entry ? buildCachedWebSocketRequestBody(entry, fullBody) : fullBody;
|
|
967
|
+
const stats = options?.sessionId ? getOrCreateWebSocketDebugStats(options.sessionId) : void 0;
|
|
968
|
+
if (stats) {
|
|
969
|
+
stats.requests++;
|
|
970
|
+
if (reused) stats.connectionsReused++;
|
|
971
|
+
else stats.connectionsCreated++;
|
|
972
|
+
if (useCachedContext) stats.cachedContextRequests++;
|
|
973
|
+
if (requestBody.store === true) stats.storeTrueRequests++;
|
|
974
|
+
stats.lastInputItems = requestBody.input?.length ?? 0;
|
|
975
|
+
if (requestBody.previous_response_id) {
|
|
976
|
+
stats.deltaRequests++;
|
|
977
|
+
stats.lastDeltaInputItems = requestBody.input?.length ?? 0;
|
|
978
|
+
stats.lastPreviousResponseId = requestBody.previous_response_id;
|
|
979
|
+
} else {
|
|
980
|
+
stats.fullContextRequests++;
|
|
981
|
+
stats.lastDeltaInputItems = void 0;
|
|
982
|
+
stats.lastPreviousResponseId = void 0;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
try {
|
|
986
|
+
socket.send(JSON.stringify({
|
|
987
|
+
type: "response.create",
|
|
988
|
+
...requestBody
|
|
989
|
+
}));
|
|
990
|
+
await processResponsesStream(startWebSocketOutputOnFirstEvent(mapCodexEvents(parseWebSocket(socket, options?.signal, idleTimeoutMs)), output, stream, onStart), output, stream, model, {
|
|
991
|
+
serviceTier: options?.serviceTier,
|
|
992
|
+
resolveServiceTier: resolveCodexServiceTier,
|
|
993
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model)
|
|
994
|
+
});
|
|
995
|
+
if (options?.signal?.aborted) keepConnection = false;
|
|
996
|
+
else if (useCachedContext && entry && output.responseId) {
|
|
997
|
+
const responseItems = convertResponsesMessages(model, { messages: [output] }, CODEX_TOOL_CALL_PROVIDERS, { includeSystemPrompt: false }).filter((item) => item.type !== "function_call_output");
|
|
998
|
+
entry.continuation = {
|
|
999
|
+
lastRequestBody: fullBody,
|
|
1000
|
+
lastResponseId: output.responseId,
|
|
1001
|
+
lastResponseItems: responseItems
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
} catch (error) {
|
|
1005
|
+
if (entry) entry.continuation = void 0;
|
|
1006
|
+
keepConnection = false;
|
|
1007
|
+
throw error;
|
|
1008
|
+
} finally {
|
|
1009
|
+
release({ keep: keepConnection });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
async function parseErrorResponse(response) {
|
|
1013
|
+
const raw = await response.text();
|
|
1014
|
+
let message = raw || response.statusText || "Request failed";
|
|
1015
|
+
let friendlyMessage;
|
|
1016
|
+
try {
|
|
1017
|
+
const err = JSON.parse(raw)?.error;
|
|
1018
|
+
if (err) {
|
|
1019
|
+
const code = err.code || err.type || "";
|
|
1020
|
+
if (/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(code) || response.status === 429) {
|
|
1021
|
+
const plan = err.plan_type ? ` (${err.plan_type.toLowerCase()} plan)` : "";
|
|
1022
|
+
const mins = err.resets_at ? Math.max(0, Math.round((err.resets_at * 1e3 - Date.now()) / 6e4)) : void 0;
|
|
1023
|
+
friendlyMessage = `You have hit your ChatGPT usage limit${plan}.${mins !== void 0 ? ` Try again in ~${mins} min.` : ""}`.trim();
|
|
1024
|
+
}
|
|
1025
|
+
message = err.message || friendlyMessage || message;
|
|
1026
|
+
}
|
|
1027
|
+
} catch {}
|
|
1028
|
+
return {
|
|
1029
|
+
message,
|
|
1030
|
+
friendlyMessage
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
function extractAccountId(token) {
|
|
1034
|
+
try {
|
|
1035
|
+
const parts = token.split(".");
|
|
1036
|
+
if (parts.length !== 3) throw new Error("Invalid token");
|
|
1037
|
+
const accountId = JSON.parse(atob(parts[1]))?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
|
|
1038
|
+
if (!accountId) throw new Error("No account ID in token");
|
|
1039
|
+
return accountId;
|
|
1040
|
+
} catch {
|
|
1041
|
+
throw new Error("Failed to extract accountId from token");
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
function createCodexRequestId() {
|
|
1045
|
+
if (typeof globalThis.crypto?.randomUUID === "function") return globalThis.crypto.randomUUID();
|
|
1046
|
+
return `codex_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
|
1047
|
+
}
|
|
1048
|
+
function buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token) {
|
|
1049
|
+
const headers = new Headers(initHeaders);
|
|
1050
|
+
for (const [key, value] of Object.entries(additionalHeaders || {})) headers.set(key, value);
|
|
1051
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
1052
|
+
headers.set("chatgpt-account-id", accountId);
|
|
1053
|
+
headers.set("originator", "pi");
|
|
1054
|
+
const userAgent = _os ? `pi (${_os.platform()} ${_os.release()}; ${_os.arch()})` : "pi (browser)";
|
|
1055
|
+
headers.set("User-Agent", userAgent);
|
|
1056
|
+
return headers;
|
|
1057
|
+
}
|
|
1058
|
+
function buildSSEHeaders(initHeaders, additionalHeaders, accountId, token, sessionId) {
|
|
1059
|
+
const headers = buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token);
|
|
1060
|
+
headers.set("OpenAI-Beta", "responses=experimental");
|
|
1061
|
+
headers.set("accept", "text/event-stream");
|
|
1062
|
+
headers.set("content-type", "application/json");
|
|
1063
|
+
if (sessionId) {
|
|
1064
|
+
headers.set("session-id", sessionId);
|
|
1065
|
+
headers.set("x-client-request-id", sessionId);
|
|
1066
|
+
}
|
|
1067
|
+
return headers;
|
|
1068
|
+
}
|
|
1069
|
+
function buildWebSocketHeaders(initHeaders, additionalHeaders, accountId, token, requestId) {
|
|
1070
|
+
const headers = buildBaseCodexHeaders(initHeaders, additionalHeaders, accountId, token);
|
|
1071
|
+
headers.delete("accept");
|
|
1072
|
+
headers.delete("content-type");
|
|
1073
|
+
headers.delete("OpenAI-Beta");
|
|
1074
|
+
headers.delete("openai-beta");
|
|
1075
|
+
headers.set("OpenAI-Beta", OPENAI_BETA_RESPONSES_WEBSOCKETS);
|
|
1076
|
+
headers.set("x-client-request-id", requestId);
|
|
1077
|
+
headers.set("session-id", requestId);
|
|
1078
|
+
return headers;
|
|
1079
|
+
}
|
|
1080
|
+
//#endregion
|
|
1081
|
+
export { closeOpenAICodexWebSocketSessions, getOpenAICodexWebSocketDebugStats, register, resetOpenAICodexWebSocketDebugStats, streamOpenAICodexResponses, streamSimpleOpenAICodexResponses };
|