@hintergrund/ui 0.1.6 → 0.1.7
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/Builder-CHLO8Qn6.js +192242 -0
- package/dist/admin.js +23465 -33024
- package/dist/anthropic-DnGfJCT8.js +5526 -0
- package/dist/azure-openai-responses-ixZ4DJpC.js +203 -0
- package/dist/builder.js +8 -0
- package/dist/client-Sh7wNjbG.js +5277 -0
- package/dist/github-copilot-headers-DWh_6bE3.js +35 -0
- package/dist/global-lQxDrO8J.js +3392 -0
- package/dist/google-B52djAfz.js +248 -0
- package/dist/google-shared-Vko-wWPR.js +11679 -0
- package/dist/google-vertex-0_fwJ0sa.js +285 -0
- package/dist/hash-DFlR084x.js +14 -0
- package/dist/headers-u7rXGJ9f.js +12 -0
- package/dist/json-parse-CMq4Lud0.js +93 -0
- package/dist/login.js +62 -100
- package/dist/mistral-DNoNjdHA.js +22932 -0
- package/dist/openai-codex-responses-7B9htNRA.js +700 -0
- package/dist/openai-completions-Dk7BLARV.js +535 -0
- package/dist/openai-responses-CTZa0siE.js +142 -0
- package/dist/openai-responses-shared-DGYGIZPG.js +324 -0
- package/dist/transform-messages-CkpQp6fJ.js +132 -0
- package/package.json +16 -6
- package/dist/global-zkva5W_x.js +0 -3150
|
@@ -0,0 +1,700 @@
|
|
|
1
|
+
import { y as P, E as Z, z as ee } from "./Builder-CHLO8Qn6.js";
|
|
2
|
+
import { h as N } from "./headers-u7rXGJ9f.js";
|
|
3
|
+
import { c as $, a as te, p as F } from "./openai-responses-shared-DGYGIZPG.js";
|
|
4
|
+
import { b as re } from "./transform-messages-CkpQp6fJ.js";
|
|
5
|
+
typeof globalThis.process > "u" && (globalThis.process = { env: {}, platform: "browser", version: "", versions: {}, browser: !0, nextTick: function(e) {
|
|
6
|
+
return Promise.resolve().then(e);
|
|
7
|
+
} });
|
|
8
|
+
const W = /* @__PURE__ */ new Set();
|
|
9
|
+
function se(e) {
|
|
10
|
+
return W.add(e), () => {
|
|
11
|
+
W.delete(e);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function _(e) {
|
|
15
|
+
return e instanceof Error ? e.message || e.name : typeof e == "string" ? e : String(e);
|
|
16
|
+
}
|
|
17
|
+
function oe(e) {
|
|
18
|
+
if (!(e instanceof Error))
|
|
19
|
+
return { name: "ThrownValue", message: _(e) };
|
|
20
|
+
const r = e.code;
|
|
21
|
+
return {
|
|
22
|
+
name: e.name || void 0,
|
|
23
|
+
message: e.message || e.name,
|
|
24
|
+
stack: e.stack,
|
|
25
|
+
code: typeof r == "string" || typeof r == "number" ? r : void 0
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function ne(e, r, t) {
|
|
29
|
+
return { type: e, timestamp: Date.now(), error: oe(r), details: t };
|
|
30
|
+
}
|
|
31
|
+
function ae(e, r) {
|
|
32
|
+
e.diagnostics = [...e.diagnostics ?? [], r];
|
|
33
|
+
}
|
|
34
|
+
let S = null;
|
|
35
|
+
const ie = (e) => import(e), ce = "node:os";
|
|
36
|
+
typeof process < "u" && (process.versions?.node || process.versions?.bun) && ie(ce).then((e) => {
|
|
37
|
+
S = e;
|
|
38
|
+
});
|
|
39
|
+
const ue = "https://chatgpt.com/backend-api", le = "https://api.openai.com/auth", R = 3, A = 1e3, J = /* @__PURE__ */ new Set(["openai", "openai-codex", "opencode"]), de = 1009, fe = /* @__PURE__ */ new Set([
|
|
40
|
+
"completed",
|
|
41
|
+
"incomplete",
|
|
42
|
+
"failed",
|
|
43
|
+
"cancelled",
|
|
44
|
+
"queued",
|
|
45
|
+
"in_progress"
|
|
46
|
+
]);
|
|
47
|
+
function pe(e, r) {
|
|
48
|
+
return e === 429 || e === 500 || e === 502 || e === 503 || e === 504 ? !0 : /rate.?limit|overloaded|service.?unavailable|upstream.?connect|connection.?refused/i.test(r);
|
|
49
|
+
}
|
|
50
|
+
function L(e, r) {
|
|
51
|
+
return new Promise((t, s) => {
|
|
52
|
+
if (r?.aborted) {
|
|
53
|
+
s(new Error("Request was aborted"));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const o = setTimeout(t, e);
|
|
57
|
+
r?.addEventListener("abort", () => {
|
|
58
|
+
clearTimeout(o), s(new Error("Request was aborted"));
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const ye = (e, r, t) => {
|
|
63
|
+
const s = new ee();
|
|
64
|
+
return (async () => {
|
|
65
|
+
const o = {
|
|
66
|
+
role: "assistant",
|
|
67
|
+
content: [],
|
|
68
|
+
api: "openai-codex-responses",
|
|
69
|
+
provider: e.provider,
|
|
70
|
+
model: e.id,
|
|
71
|
+
usage: {
|
|
72
|
+
input: 0,
|
|
73
|
+
output: 0,
|
|
74
|
+
cacheRead: 0,
|
|
75
|
+
cacheWrite: 0,
|
|
76
|
+
totalTokens: 0,
|
|
77
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
78
|
+
},
|
|
79
|
+
stopReason: "stop",
|
|
80
|
+
timestamp: Date.now()
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const n = t?.apiKey || P(e.provider) || "";
|
|
84
|
+
if (!n)
|
|
85
|
+
throw new Error(`No API key for provider: ${e.provider}`);
|
|
86
|
+
const i = Ne(n);
|
|
87
|
+
let a = we(e, r, t);
|
|
88
|
+
const c = await t?.onPayload?.(a, e);
|
|
89
|
+
c !== void 0 && (a = c);
|
|
90
|
+
const l = t?.sessionId || $e(), h = Fe(e.headers, t?.headers, i, n, t?.sessionId), g = Je(e.headers, t?.headers, i, n, l), d = JSON.stringify(a), b = t?.transport || "auto", f = b !== "sse" && j(t?.sessionId);
|
|
91
|
+
if (f && B(t?.sessionId), b !== "sse" && !f) {
|
|
92
|
+
let w = !1;
|
|
93
|
+
try {
|
|
94
|
+
if (await Me(he(e.baseUrl), a, g, o, s, e, () => {
|
|
95
|
+
w = !0;
|
|
96
|
+
}, t), t?.signal?.aborted)
|
|
97
|
+
throw new Error("Request was aborted");
|
|
98
|
+
s.push({
|
|
99
|
+
type: "done",
|
|
100
|
+
reason: o.stopReason,
|
|
101
|
+
message: o
|
|
102
|
+
}), s.end();
|
|
103
|
+
return;
|
|
104
|
+
} catch (p) {
|
|
105
|
+
if (t?.signal?.aborted || ge(p) || (ae(o, ne("provider_transport_failure", p, {
|
|
106
|
+
configuredTransport: b,
|
|
107
|
+
fallbackTransport: w ? void 0 : "sse",
|
|
108
|
+
eventsEmitted: w,
|
|
109
|
+
phase: w ? "after_message_stream_start" : "before_message_stream_start",
|
|
110
|
+
requestBytes: new TextEncoder().encode(d).byteLength
|
|
111
|
+
})), Re(t?.sessionId, p), w))
|
|
112
|
+
throw p;
|
|
113
|
+
B(t?.sessionId);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let u, y;
|
|
117
|
+
for (let w = 0; w <= R; w++) {
|
|
118
|
+
if (t?.signal?.aborted)
|
|
119
|
+
throw new Error("Request was aborted");
|
|
120
|
+
try {
|
|
121
|
+
if (u = await fetch(K(e.baseUrl), {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: h,
|
|
124
|
+
body: d,
|
|
125
|
+
signal: t?.signal
|
|
126
|
+
}), await t?.onResponse?.({ status: u.status, headers: N(u.headers) }, e), u.ok)
|
|
127
|
+
break;
|
|
128
|
+
const p = await u.text();
|
|
129
|
+
if (w < R && pe(u.status, p)) {
|
|
130
|
+
const Q = A * 2 ** w;
|
|
131
|
+
await L(Q, t?.signal);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const v = new Response(p, {
|
|
135
|
+
status: u.status,
|
|
136
|
+
statusText: u.statusText
|
|
137
|
+
}), I = await Pe(v);
|
|
138
|
+
throw new Error(I.friendlyMessage || I.message);
|
|
139
|
+
} catch (p) {
|
|
140
|
+
if (p instanceof Error && (p.name === "AbortError" || p.message === "Request was aborted"))
|
|
141
|
+
throw new Error("Request was aborted");
|
|
142
|
+
if (y = p instanceof Error ? p : new Error(String(p)), w < R && !y.message.includes("usage limit")) {
|
|
143
|
+
const v = A * 2 ** w;
|
|
144
|
+
await L(v, t?.signal);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
throw y;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!u?.ok)
|
|
151
|
+
throw y ?? new Error("Failed after retries");
|
|
152
|
+
if (!u.body)
|
|
153
|
+
throw new Error("No response body");
|
|
154
|
+
if (s.push({ type: "start", partial: o }), await me(u, o, s, e, t), t?.signal?.aborted)
|
|
155
|
+
throw new Error("Request was aborted");
|
|
156
|
+
s.push({ type: "done", reason: o.stopReason, message: o }), s.end();
|
|
157
|
+
} catch (n) {
|
|
158
|
+
for (const i of o.content)
|
|
159
|
+
delete i.partialJson;
|
|
160
|
+
o.stopReason = t?.signal?.aborted ? "aborted" : "error", o.errorMessage = n instanceof Error ? n.message : String(n), s.push({ type: "error", reason: o.stopReason, error: o }), s.end();
|
|
161
|
+
}
|
|
162
|
+
})(), s;
|
|
163
|
+
}, Ve = (e, r, t) => {
|
|
164
|
+
const s = t?.apiKey || P(e.provider);
|
|
165
|
+
if (!s)
|
|
166
|
+
throw new Error(`No API key for provider: ${e.provider}`);
|
|
167
|
+
const o = re(e, t, s), n = t?.reasoning ? Z(e, t.reasoning) : void 0;
|
|
168
|
+
return ye(e, r, {
|
|
169
|
+
...o,
|
|
170
|
+
reasoningEffort: n === "off" ? void 0 : n
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
function we(e, r, t) {
|
|
174
|
+
const s = $(e, r, J, {
|
|
175
|
+
includeSystemPrompt: !1
|
|
176
|
+
}), o = {
|
|
177
|
+
model: e.id,
|
|
178
|
+
store: !1,
|
|
179
|
+
stream: !0,
|
|
180
|
+
instructions: r.systemPrompt || "You are a helpful assistant.",
|
|
181
|
+
input: s,
|
|
182
|
+
text: { verbosity: t?.textVerbosity || "low" },
|
|
183
|
+
include: ["reasoning.encrypted_content"],
|
|
184
|
+
prompt_cache_key: t?.sessionId,
|
|
185
|
+
tool_choice: "auto",
|
|
186
|
+
parallel_tool_calls: !0
|
|
187
|
+
};
|
|
188
|
+
if (t?.temperature !== void 0 && (o.temperature = t.temperature), t?.serviceTier !== void 0 && (o.service_tier = t.serviceTier), r.tools && r.tools.length > 0 && (o.tools = te(r.tools, { strict: null })), t?.reasoningEffort !== void 0) {
|
|
189
|
+
const n = t.reasoningEffort === "none" ? e.thinkingLevelMap?.off ?? "none" : e.thinkingLevelMap?.[t.reasoningEffort] ?? t.reasoningEffort;
|
|
190
|
+
n !== null && (o.reasoning = {
|
|
191
|
+
effort: n,
|
|
192
|
+
summary: t.reasoningSummary ?? "auto"
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return o;
|
|
196
|
+
}
|
|
197
|
+
function be(e, r) {
|
|
198
|
+
switch (r) {
|
|
199
|
+
case "flex":
|
|
200
|
+
return 0.5;
|
|
201
|
+
case "priority":
|
|
202
|
+
return e.id === "gpt-5.5" ? 2.5 : 2;
|
|
203
|
+
default:
|
|
204
|
+
return 1;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function U(e, r, t) {
|
|
208
|
+
const s = be(t, r);
|
|
209
|
+
s !== 1 && (e.cost.input *= s, e.cost.output *= s, e.cost.cacheRead *= s, e.cost.cacheWrite *= s, e.cost.total = e.cost.input + e.cost.output + e.cost.cacheRead + e.cost.cacheWrite);
|
|
210
|
+
}
|
|
211
|
+
function H(e, r) {
|
|
212
|
+
return e === "default" && (r === "flex" || r === "priority") ? r : e ?? r;
|
|
213
|
+
}
|
|
214
|
+
function K(e) {
|
|
215
|
+
const t = (e && e.trim().length > 0 ? e : ue).replace(/\/+$/, "");
|
|
216
|
+
return t.endsWith("/codex/responses") ? t : t.endsWith("/codex") ? `${t}/responses` : `${t}/codex/responses`;
|
|
217
|
+
}
|
|
218
|
+
function he(e) {
|
|
219
|
+
const r = new URL(K(e));
|
|
220
|
+
return r.protocol === "https:" && (r.protocol = "wss:"), r.protocol === "http:" && (r.protocol = "ws:"), r.toString();
|
|
221
|
+
}
|
|
222
|
+
async function me(e, r, t, s, o) {
|
|
223
|
+
await F(z(Se(e)), r, t, s, {
|
|
224
|
+
serviceTier: o?.serviceTier,
|
|
225
|
+
resolveServiceTier: H,
|
|
226
|
+
applyServiceTierPricing: (n, i) => U(n, i, s)
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
class x extends Error {
|
|
230
|
+
code;
|
|
231
|
+
payload;
|
|
232
|
+
constructor(r, t) {
|
|
233
|
+
super(r), this.name = "CodexApiError", this.code = t?.code, this.payload = t?.payload, this.cause = t?.cause;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
class C extends Error {
|
|
237
|
+
payload;
|
|
238
|
+
constructor(r, t) {
|
|
239
|
+
super(r), this.name = "CodexProtocolError", this.payload = t?.payload, this.cause = t?.cause;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function ge(e) {
|
|
243
|
+
return e instanceof x || e instanceof C;
|
|
244
|
+
}
|
|
245
|
+
async function* z(e) {
|
|
246
|
+
for await (const r of e) {
|
|
247
|
+
const t = typeof r.type == "string" ? r.type : void 0;
|
|
248
|
+
if (t) {
|
|
249
|
+
if (t === "error") {
|
|
250
|
+
const s = r.code || "", o = r.message || "";
|
|
251
|
+
throw new x(`Codex error: ${o || s || JSON.stringify(r)}`, {
|
|
252
|
+
code: s || void 0,
|
|
253
|
+
payload: r
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (t === "response.failed") {
|
|
257
|
+
const s = r.response, o = s?.error?.code, n = s?.error?.message;
|
|
258
|
+
throw new x(n || "Codex response failed", { code: o, payload: r });
|
|
259
|
+
}
|
|
260
|
+
if (t === "response.done" || t === "response.completed" || t === "response.incomplete") {
|
|
261
|
+
const s = r.response, o = s && { ...s, status: Ee(s.status) };
|
|
262
|
+
yield { ...r, type: "response.completed", response: o };
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
yield r;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function Ee(e) {
|
|
270
|
+
if (typeof e == "string")
|
|
271
|
+
return fe.has(e) ? e : void 0;
|
|
272
|
+
}
|
|
273
|
+
async function* Se(e) {
|
|
274
|
+
if (!e.body)
|
|
275
|
+
return;
|
|
276
|
+
const r = e.body.getReader(), t = new TextDecoder();
|
|
277
|
+
let s = "";
|
|
278
|
+
try {
|
|
279
|
+
for (; ; ) {
|
|
280
|
+
const { done: o, value: n } = await r.read();
|
|
281
|
+
if (o)
|
|
282
|
+
break;
|
|
283
|
+
s += t.decode(n, { stream: !0 });
|
|
284
|
+
let i = s.indexOf(`
|
|
285
|
+
|
|
286
|
+
`);
|
|
287
|
+
for (; i !== -1; ) {
|
|
288
|
+
const a = s.slice(0, i);
|
|
289
|
+
s = s.slice(i + 2);
|
|
290
|
+
const c = a.split(`
|
|
291
|
+
`).filter((l) => l.startsWith("data:")).map((l) => l.slice(5).trim());
|
|
292
|
+
if (c.length > 0) {
|
|
293
|
+
const l = c.join(`
|
|
294
|
+
`).trim();
|
|
295
|
+
if (l && l !== "[DONE]")
|
|
296
|
+
try {
|
|
297
|
+
yield JSON.parse(l);
|
|
298
|
+
} catch (h) {
|
|
299
|
+
throw new C(`Invalid Codex SSE JSON: ${_(h)}`, {
|
|
300
|
+
cause: h,
|
|
301
|
+
payload: l
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
i = s.indexOf(`
|
|
306
|
+
|
|
307
|
+
`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
} finally {
|
|
311
|
+
try {
|
|
312
|
+
await r.cancel();
|
|
313
|
+
} catch {
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
r.releaseLock();
|
|
317
|
+
} catch {
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const ve = "responses_websockets=2026-02-06", ke = 300 * 1e3, m = /* @__PURE__ */ new Map(), q = /* @__PURE__ */ new Map(), V = /* @__PURE__ */ new Set();
|
|
322
|
+
function O(e) {
|
|
323
|
+
let r = q.get(e);
|
|
324
|
+
return r || (r = {
|
|
325
|
+
requests: 0,
|
|
326
|
+
connectionsCreated: 0,
|
|
327
|
+
connectionsReused: 0,
|
|
328
|
+
cachedContextRequests: 0,
|
|
329
|
+
storeTrueRequests: 0,
|
|
330
|
+
fullContextRequests: 0,
|
|
331
|
+
deltaRequests: 0,
|
|
332
|
+
lastInputItems: 0,
|
|
333
|
+
websocketFailures: 0,
|
|
334
|
+
sseFallbacks: 0
|
|
335
|
+
}, q.set(e, r)), r;
|
|
336
|
+
}
|
|
337
|
+
function _e(e) {
|
|
338
|
+
const r = (t) => {
|
|
339
|
+
t.idleTimer && clearTimeout(t.idleTimer), E(t.socket, 1e3, "debug_close");
|
|
340
|
+
};
|
|
341
|
+
if (e) {
|
|
342
|
+
const t = m.get(e);
|
|
343
|
+
t && r(t), m.delete(e);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
for (const t of m.values())
|
|
347
|
+
r(t);
|
|
348
|
+
m.clear();
|
|
349
|
+
}
|
|
350
|
+
se(_e);
|
|
351
|
+
function j(e) {
|
|
352
|
+
return e ? V.has(e) : !1;
|
|
353
|
+
}
|
|
354
|
+
function B(e) {
|
|
355
|
+
if (!e)
|
|
356
|
+
return;
|
|
357
|
+
const r = O(e);
|
|
358
|
+
r.sseFallbacks++, r.websocketFallbackActive = j(e);
|
|
359
|
+
}
|
|
360
|
+
function Re(e, r) {
|
|
361
|
+
if (!e)
|
|
362
|
+
return;
|
|
363
|
+
V.add(e);
|
|
364
|
+
const t = O(e);
|
|
365
|
+
t.websocketFailures++, t.lastWebSocketError = _(r), t.websocketFallbackActive = !0;
|
|
366
|
+
}
|
|
367
|
+
function Te() {
|
|
368
|
+
const e = globalThis.WebSocket;
|
|
369
|
+
return typeof e != "function" ? null : e;
|
|
370
|
+
}
|
|
371
|
+
class xe extends Error {
|
|
372
|
+
code;
|
|
373
|
+
reason;
|
|
374
|
+
wasClean;
|
|
375
|
+
constructor(r, t) {
|
|
376
|
+
super(r), this.name = "WebSocketCloseError", this.code = t?.code, this.reason = t?.reason, this.wasClean = t?.wasClean;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function Ce(e) {
|
|
380
|
+
const r = e.readyState;
|
|
381
|
+
return typeof r == "number" ? r : void 0;
|
|
382
|
+
}
|
|
383
|
+
function k(e) {
|
|
384
|
+
const r = Ce(e);
|
|
385
|
+
return r === void 0 || r === 1;
|
|
386
|
+
}
|
|
387
|
+
function E(e, r = 1e3, t = "done") {
|
|
388
|
+
try {
|
|
389
|
+
e.close(r, t);
|
|
390
|
+
} catch {
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function D(e, r) {
|
|
394
|
+
r.idleTimer && clearTimeout(r.idleTimer), r.idleTimer = setTimeout(() => {
|
|
395
|
+
r.busy || (E(r.socket, 1e3, "idle_timeout"), m.delete(e));
|
|
396
|
+
}, ke);
|
|
397
|
+
}
|
|
398
|
+
async function T(e, r, t) {
|
|
399
|
+
const s = Te();
|
|
400
|
+
if (!s)
|
|
401
|
+
throw new Error("WebSocket transport is not available in this runtime");
|
|
402
|
+
const o = N(r);
|
|
403
|
+
return delete o["OpenAI-Beta"], new Promise((n, i) => {
|
|
404
|
+
let a = !1, c;
|
|
405
|
+
try {
|
|
406
|
+
c = new s(e, { headers: o });
|
|
407
|
+
} catch (f) {
|
|
408
|
+
i(f instanceof Error ? f : new Error(String(f)));
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const l = () => {
|
|
412
|
+
a || (a = !0, b(), n(c));
|
|
413
|
+
}, h = (f) => {
|
|
414
|
+
const u = X(f);
|
|
415
|
+
a || (a = !0, b(), i(u));
|
|
416
|
+
}, g = (f) => {
|
|
417
|
+
const u = G(f);
|
|
418
|
+
a || (a = !0, b(), i(u));
|
|
419
|
+
}, d = () => {
|
|
420
|
+
a || (a = !0, b(), c.close(1e3, "aborted"), i(new Error("Request was aborted")));
|
|
421
|
+
}, b = () => {
|
|
422
|
+
c.removeEventListener("open", l), c.removeEventListener("error", h), c.removeEventListener("close", g), t?.removeEventListener("abort", d);
|
|
423
|
+
};
|
|
424
|
+
c.addEventListener("open", l), c.addEventListener("error", h), c.addEventListener("close", g), t?.addEventListener("abort", d);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
async function Oe(e, r, t, s) {
|
|
428
|
+
if (!t) {
|
|
429
|
+
const a = await T(e, r, s);
|
|
430
|
+
return {
|
|
431
|
+
socket: a,
|
|
432
|
+
reused: !1,
|
|
433
|
+
release: ({ keep: c } = {}) => {
|
|
434
|
+
if (c === !1) {
|
|
435
|
+
E(a);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
E(a);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
const o = m.get(t);
|
|
443
|
+
if (o) {
|
|
444
|
+
if (o.idleTimer && (clearTimeout(o.idleTimer), o.idleTimer = void 0), !o.busy && k(o.socket))
|
|
445
|
+
return o.busy = !0, {
|
|
446
|
+
socket: o.socket,
|
|
447
|
+
entry: o,
|
|
448
|
+
reused: !0,
|
|
449
|
+
release: ({ keep: a } = {}) => {
|
|
450
|
+
if (!a || !k(o.socket)) {
|
|
451
|
+
E(o.socket), m.delete(t);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
o.busy = !1, D(t, o);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
if (o.busy) {
|
|
458
|
+
const a = await T(e, r, s);
|
|
459
|
+
return {
|
|
460
|
+
socket: a,
|
|
461
|
+
reused: !1,
|
|
462
|
+
release: () => {
|
|
463
|
+
E(a);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
k(o.socket) || (E(o.socket), m.delete(t));
|
|
468
|
+
}
|
|
469
|
+
const n = await T(e, r, s), i = { socket: n, busy: !0 };
|
|
470
|
+
return m.set(t, i), {
|
|
471
|
+
socket: n,
|
|
472
|
+
entry: i,
|
|
473
|
+
reused: !1,
|
|
474
|
+
release: ({ keep: a } = {}) => {
|
|
475
|
+
if (!a || !k(i.socket)) {
|
|
476
|
+
E(i.socket), i.idleTimer && clearTimeout(i.idleTimer), m.get(t) === i && m.delete(t);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
i.busy = !1, D(t, i);
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
function X(e) {
|
|
484
|
+
if (e && typeof e == "object") {
|
|
485
|
+
const r = "message" in e ? e.message : void 0;
|
|
486
|
+
if (typeof r == "string" && r.length > 0)
|
|
487
|
+
return new Error(r);
|
|
488
|
+
const t = "error" in e ? e.error : void 0;
|
|
489
|
+
if (t instanceof Error && t.message.length > 0)
|
|
490
|
+
return t;
|
|
491
|
+
if (t && typeof t == "object" && "message" in t) {
|
|
492
|
+
const s = t.message;
|
|
493
|
+
if (typeof s == "string" && s.length > 0)
|
|
494
|
+
return new Error(s);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return new Error("WebSocket error");
|
|
498
|
+
}
|
|
499
|
+
function G(e) {
|
|
500
|
+
if (e && typeof e == "object") {
|
|
501
|
+
const r = "code" in e ? e.code : void 0, t = "reason" in e ? e.reason : void 0, s = "wasClean" in e ? e.wasClean : void 0, o = typeof r == "number" ? ` ${r}` : "";
|
|
502
|
+
let n = typeof t == "string" && t.length > 0 ? ` ${t}` : "";
|
|
503
|
+
return !n && r === de && (n = " message too big"), new xe(`WebSocket closed${o}${n}`.trim(), {
|
|
504
|
+
code: typeof r == "number" ? r : void 0,
|
|
505
|
+
reason: typeof t == "string" && t.length > 0 ? t : void 0,
|
|
506
|
+
wasClean: typeof s == "boolean" ? s : void 0
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
return new Error("WebSocket closed");
|
|
510
|
+
}
|
|
511
|
+
async function Ie(e) {
|
|
512
|
+
if (typeof e == "string")
|
|
513
|
+
return e;
|
|
514
|
+
if (e instanceof ArrayBuffer)
|
|
515
|
+
return new TextDecoder().decode(new Uint8Array(e));
|
|
516
|
+
if (ArrayBuffer.isView(e)) {
|
|
517
|
+
const r = e;
|
|
518
|
+
return new TextDecoder().decode(new Uint8Array(r.buffer, r.byteOffset, r.byteLength));
|
|
519
|
+
}
|
|
520
|
+
if (e && typeof e == "object" && "arrayBuffer" in e) {
|
|
521
|
+
const t = await e.arrayBuffer();
|
|
522
|
+
return new TextDecoder().decode(new Uint8Array(t));
|
|
523
|
+
}
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
async function* We(e, r) {
|
|
527
|
+
const t = [];
|
|
528
|
+
let s = null, o = !1, n = null, i = !1;
|
|
529
|
+
const a = () => {
|
|
530
|
+
if (!s)
|
|
531
|
+
return;
|
|
532
|
+
const d = s;
|
|
533
|
+
s = null, d();
|
|
534
|
+
}, c = (d) => {
|
|
535
|
+
(async () => {
|
|
536
|
+
let b = null;
|
|
537
|
+
try {
|
|
538
|
+
if (!d || typeof d != "object" || !("data" in d) || (b = await Ie(d.data), !b))
|
|
539
|
+
return;
|
|
540
|
+
const f = JSON.parse(b), u = typeof f.type == "string" ? f.type : "";
|
|
541
|
+
(u === "response.completed" || u === "response.done" || u === "response.incomplete") && (i = !0, o = !0), t.push(f), a();
|
|
542
|
+
} catch (f) {
|
|
543
|
+
n = new C(`Invalid Codex WebSocket JSON: ${_(f)}`, {
|
|
544
|
+
cause: f,
|
|
545
|
+
payload: b
|
|
546
|
+
}), o = !0, a();
|
|
547
|
+
}
|
|
548
|
+
})();
|
|
549
|
+
}, l = (d) => {
|
|
550
|
+
n = X(d), o = !0, a();
|
|
551
|
+
}, h = (d) => {
|
|
552
|
+
if (i) {
|
|
553
|
+
o = !0, a();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
n || (n = G(d)), o = !0, a();
|
|
557
|
+
}, g = () => {
|
|
558
|
+
n = new Error("Request was aborted"), o = !0, a();
|
|
559
|
+
};
|
|
560
|
+
e.addEventListener("message", c), e.addEventListener("error", l), e.addEventListener("close", h), r?.addEventListener("abort", g);
|
|
561
|
+
try {
|
|
562
|
+
for (; ; ) {
|
|
563
|
+
if (r?.aborted)
|
|
564
|
+
throw new Error("Request was aborted");
|
|
565
|
+
if (t.length > 0) {
|
|
566
|
+
yield t.shift();
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (o)
|
|
570
|
+
break;
|
|
571
|
+
await new Promise((d) => {
|
|
572
|
+
s = d;
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
if (n)
|
|
576
|
+
throw n;
|
|
577
|
+
if (!i)
|
|
578
|
+
throw new Error("WebSocket stream closed before response.completed");
|
|
579
|
+
} finally {
|
|
580
|
+
e.removeEventListener("message", c), e.removeEventListener("error", l), e.removeEventListener("close", h), r?.removeEventListener("abort", g);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function M(e) {
|
|
584
|
+
const { input: r, previous_response_id: t, ...s } = e;
|
|
585
|
+
return s;
|
|
586
|
+
}
|
|
587
|
+
function Ae(e, r) {
|
|
588
|
+
return JSON.stringify(e ?? []) === JSON.stringify(r ?? []);
|
|
589
|
+
}
|
|
590
|
+
function Le(e, r) {
|
|
591
|
+
return JSON.stringify(M(e)) === JSON.stringify(M(r));
|
|
592
|
+
}
|
|
593
|
+
function qe(e, r) {
|
|
594
|
+
if (!Le(e, r.lastRequestBody))
|
|
595
|
+
return;
|
|
596
|
+
const t = e.input ?? [], s = [...r.lastRequestBody.input ?? [], ...r.lastResponseItems];
|
|
597
|
+
if (t.length < s.length)
|
|
598
|
+
return;
|
|
599
|
+
const o = t.slice(0, s.length);
|
|
600
|
+
if (Ae(o, s))
|
|
601
|
+
return t.slice(s.length);
|
|
602
|
+
}
|
|
603
|
+
function Be(e, r) {
|
|
604
|
+
const t = e.continuation;
|
|
605
|
+
if (!t)
|
|
606
|
+
return r;
|
|
607
|
+
const s = qe(r, t);
|
|
608
|
+
return !s || !t.lastResponseId ? (e.continuation = void 0, r) : {
|
|
609
|
+
...r,
|
|
610
|
+
previous_response_id: t.lastResponseId,
|
|
611
|
+
input: s
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
async function* De(e, r, t, s) {
|
|
615
|
+
let o = !1;
|
|
616
|
+
for await (const n of e)
|
|
617
|
+
o || (o = !0, s(), t.push({ type: "start", partial: r })), yield n;
|
|
618
|
+
}
|
|
619
|
+
async function Me(e, r, t, s, o, n, i, a) {
|
|
620
|
+
const { socket: c, entry: l, reused: h, release: g } = await Oe(e, t, a?.sessionId, a?.signal);
|
|
621
|
+
let d = !0;
|
|
622
|
+
const b = a?.transport === "websocket-cached" || a?.transport === "auto", f = r, u = b && l ? Be(l, f) : f, y = a?.sessionId ? O(a.sessionId) : void 0;
|
|
623
|
+
y && (y.requests++, h ? y.connectionsReused++ : y.connectionsCreated++, b && y.cachedContextRequests++, u.store === !0 && y.storeTrueRequests++, y.lastInputItems = u.input?.length ?? 0, u.previous_response_id ? (y.deltaRequests++, y.lastDeltaInputItems = u.input?.length ?? 0, y.lastPreviousResponseId = u.previous_response_id) : (y.fullContextRequests++, y.lastDeltaInputItems = void 0, y.lastPreviousResponseId = void 0));
|
|
624
|
+
try {
|
|
625
|
+
if (c.send(JSON.stringify({ type: "response.create", ...u })), await F(De(z(We(c, a?.signal)), s, o, i), s, o, n, {
|
|
626
|
+
serviceTier: a?.serviceTier,
|
|
627
|
+
resolveServiceTier: H,
|
|
628
|
+
applyServiceTierPricing: (w, p) => U(w, p, n)
|
|
629
|
+
}), a?.signal?.aborted)
|
|
630
|
+
d = !1;
|
|
631
|
+
else if (b && l && s.responseId) {
|
|
632
|
+
const w = $(n, { messages: [s] }, J, {
|
|
633
|
+
includeSystemPrompt: !1
|
|
634
|
+
}).filter((p) => p.type !== "function_call_output");
|
|
635
|
+
l.continuation = {
|
|
636
|
+
lastRequestBody: f,
|
|
637
|
+
lastResponseId: s.responseId,
|
|
638
|
+
lastResponseItems: w
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
} catch (w) {
|
|
642
|
+
throw l && (l.continuation = void 0), d = !1, w;
|
|
643
|
+
} finally {
|
|
644
|
+
g({ keep: d });
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
async function Pe(e) {
|
|
648
|
+
const r = await e.text();
|
|
649
|
+
let t = r || e.statusText || "Request failed", s;
|
|
650
|
+
try {
|
|
651
|
+
const n = JSON.parse(r)?.error;
|
|
652
|
+
if (n) {
|
|
653
|
+
const i = n.code || n.type || "";
|
|
654
|
+
if (/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(i) || e.status === 429) {
|
|
655
|
+
const a = n.plan_type ? ` (${n.plan_type.toLowerCase()} plan)` : "", c = n.resets_at ? Math.max(0, Math.round((n.resets_at * 1e3 - Date.now()) / 6e4)) : void 0, l = c !== void 0 ? ` Try again in ~${c} min.` : "";
|
|
656
|
+
s = `You have hit your ChatGPT usage limit${a}.${l}`.trim();
|
|
657
|
+
}
|
|
658
|
+
t = n.message || s || t;
|
|
659
|
+
}
|
|
660
|
+
} catch {
|
|
661
|
+
}
|
|
662
|
+
return { message: t, friendlyMessage: s };
|
|
663
|
+
}
|
|
664
|
+
function Ne(e) {
|
|
665
|
+
try {
|
|
666
|
+
const r = e.split(".");
|
|
667
|
+
if (r.length !== 3)
|
|
668
|
+
throw new Error("Invalid token");
|
|
669
|
+
const s = JSON.parse(atob(r[1]))?.[le]?.chatgpt_account_id;
|
|
670
|
+
if (!s)
|
|
671
|
+
throw new Error("No account ID in token");
|
|
672
|
+
return s;
|
|
673
|
+
} catch {
|
|
674
|
+
throw new Error("Failed to extract accountId from token");
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
function $e() {
|
|
678
|
+
return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `codex_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
|
679
|
+
}
|
|
680
|
+
function Y(e, r, t, s) {
|
|
681
|
+
const o = new Headers(e);
|
|
682
|
+
for (const [i, a] of Object.entries(r || {}))
|
|
683
|
+
o.set(i, a);
|
|
684
|
+
o.set("Authorization", `Bearer ${s}`), o.set("chatgpt-account-id", t), o.set("originator", "pi");
|
|
685
|
+
const n = S ? `pi (${S.platform()} ${S.release()}; ${S.arch()})` : "pi (browser)";
|
|
686
|
+
return o.set("User-Agent", n), o;
|
|
687
|
+
}
|
|
688
|
+
function Fe(e, r, t, s, o) {
|
|
689
|
+
const n = Y(e, r, t, s);
|
|
690
|
+
return n.set("OpenAI-Beta", "responses=experimental"), n.set("accept", "text/event-stream"), n.set("content-type", "application/json"), o && (n.set("session_id", o), n.set("x-client-request-id", o)), n;
|
|
691
|
+
}
|
|
692
|
+
function Je(e, r, t, s, o) {
|
|
693
|
+
const n = Y(e, r, t, s);
|
|
694
|
+
return n.delete("accept"), n.delete("content-type"), n.delete("OpenAI-Beta"), n.delete("openai-beta"), n.set("OpenAI-Beta", ve), n.set("x-client-request-id", o), n.set("session_id", o), n;
|
|
695
|
+
}
|
|
696
|
+
export {
|
|
697
|
+
_e as closeOpenAICodexWebSocketSessions,
|
|
698
|
+
ye as streamOpenAICodexResponses,
|
|
699
|
+
Ve as streamSimpleOpenAICodexResponses
|
|
700
|
+
};
|