@neetru/sdk 3.7.0 → 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +75 -0
- package/README.md +66 -1
- package/dist/ai.cjs +221 -177
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +221 -177
- package/dist/auth.cjs +1051 -868
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +1048 -865
- package/dist/catalog.cjs +164 -97
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +164 -97
- package/dist/checkout.cjs +182 -115
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +181 -114
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.cjs +437 -345
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +455 -363
- package/dist/entitlements.cjs +17 -10
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +17 -10
- package/dist/errors.cjs +15 -8
- package/dist/errors.d.cts +24 -3
- package/dist/errors.d.ts +24 -3
- package/dist/errors.mjs +14 -7
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +1544 -1190
- package/dist/index.d.cts +177 -3
- package/dist/index.d.ts +177 -3
- package/dist/index.mjs +1676 -1329
- package/dist/mobile.cjs +400 -181
- package/dist/mobile.d.cts +59 -4
- package/dist/mobile.d.ts +59 -4
- package/dist/mobile.mjs +400 -181
- package/dist/mocks.cjs +9 -2
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +9 -2
- package/dist/notifications.cjs +197 -130
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +196 -129
- package/dist/react.cjs +303 -282
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +295 -274
- package/dist/support.cjs +299 -229
- package/dist/support.d.cts +8 -4
- package/dist/support.d.ts +8 -4
- package/dist/support.mjs +298 -228
- package/dist/telemetry.cjs +234 -167
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +234 -167
- package/dist/{types-Dx0IrOSF.d.ts → types-BYPk_3HR.d.ts} +104 -17
- package/dist/{types-Bx4kTLmk.d.cts → types-yv3EDKg2.d.cts} +104 -17
- package/dist/usage.cjs +183 -116
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +183 -116
- package/dist/webhooks.cjs +215 -148
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +212 -145
- package/package.json +2 -1
package/dist/ai.mjs
CHANGED
|
@@ -12,102 +12,146 @@ var u = class t extends Error {
|
|
|
12
12
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
13
13
|
*/
|
|
14
14
|
reason;
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
17
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
18
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
19
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
20
|
+
*/
|
|
21
|
+
correlationId;
|
|
22
|
+
constructor(e, i, r, n, o, s) {
|
|
23
|
+
super(i), this.name = "NeetruError", this.code = e, this.status = r, this.requestId = n, this.reason = o, this.correlationId = s, Object.setPrototypeOf(this, t.prototype);
|
|
17
24
|
}
|
|
18
25
|
};
|
|
19
26
|
|
|
27
|
+
// src/correlation.ts
|
|
28
|
+
var w = "x-correlation-id";
|
|
29
|
+
var V = /^[A-Za-z0-9._:\-]+$/;
|
|
30
|
+
function E(t) {
|
|
31
|
+
return typeof t == "string" && t.length > 0 && t.length <= 128 && V.test(t);
|
|
32
|
+
}
|
|
33
|
+
function J() {
|
|
34
|
+
let t = globalThis.crypto;
|
|
35
|
+
try {
|
|
36
|
+
if (t && typeof t.randomUUID == "function") return t.randomUUID();
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
let e = new Uint8Array(16), i = false;
|
|
40
|
+
try {
|
|
41
|
+
t && typeof t.getRandomValues == "function" && (t.getRandomValues(e), i = !0);
|
|
42
|
+
} catch {
|
|
43
|
+
i = false;
|
|
44
|
+
}
|
|
45
|
+
if (!i)
|
|
46
|
+
for (let n = 0; n < e.length; n++) e[n] = Math.floor(Math.random() * 256);
|
|
47
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
48
|
+
let r = Array.from(e, (n) => n.toString(16).padStart(2, "0")).join("");
|
|
49
|
+
return `${r.slice(0, 8)}-${r.slice(8, 12)}-${r.slice(12, 16)}-${r.slice(16, 20)}-${r.slice(20)}`;
|
|
50
|
+
}
|
|
51
|
+
function x(t, e) {
|
|
52
|
+
let i, r = {};
|
|
53
|
+
for (let [o, s] of Object.entries(t)) {
|
|
54
|
+
if (o.toLowerCase() === w) {
|
|
55
|
+
E(s) && (i = s);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
r[o] = s;
|
|
59
|
+
}
|
|
60
|
+
let n = i ?? (E(e) ? e : void 0) ?? J();
|
|
61
|
+
return r[w] = n, { headers: r, correlationId: n };
|
|
62
|
+
}
|
|
63
|
+
|
|
20
64
|
// src/http.ts
|
|
21
|
-
var
|
|
65
|
+
var Y = {
|
|
22
66
|
// SDK major 3.x requer Core ≥ 2026.05.1 (suporte a X-Neetru-Version header,
|
|
23
67
|
// runTransaction write-after-read, verifyWebhookRequest, webhooks 3.0+)
|
|
24
68
|
3: "2026.05.1"
|
|
25
69
|
};
|
|
26
|
-
function
|
|
70
|
+
function A(t) {
|
|
27
71
|
return t.split("+")[0] ?? t;
|
|
28
72
|
}
|
|
29
|
-
function
|
|
30
|
-
let
|
|
31
|
-
for (let
|
|
32
|
-
let
|
|
33
|
-
if (
|
|
73
|
+
function Z(t, e) {
|
|
74
|
+
let i = A(t).split(".").map(Number), r = A(e).split(".").map(Number), n = Math.max(i.length, r.length);
|
|
75
|
+
for (let o = 0; o < n; o++) {
|
|
76
|
+
let s = i[o] ?? 0, c = r[o] ?? 0;
|
|
77
|
+
if (s !== c) return s - c;
|
|
34
78
|
}
|
|
35
79
|
return 0;
|
|
36
80
|
}
|
|
37
|
-
function
|
|
81
|
+
function Q() {
|
|
38
82
|
try {
|
|
39
|
-
return "3.
|
|
83
|
+
return "3.8.1";
|
|
40
84
|
} catch {
|
|
41
85
|
return;
|
|
42
86
|
}
|
|
43
87
|
}
|
|
44
|
-
function
|
|
45
|
-
let t =
|
|
88
|
+
function ee() {
|
|
89
|
+
let t = Q();
|
|
46
90
|
return t ? parseInt(t.split(".")[0] ?? "3", 10) : 3;
|
|
47
91
|
}
|
|
48
|
-
var
|
|
49
|
-
function
|
|
92
|
+
var R = /* @__PURE__ */ new Set();
|
|
93
|
+
function k(t) {
|
|
50
94
|
if (!t) return;
|
|
51
|
-
let e =
|
|
52
|
-
if (!
|
|
53
|
-
let
|
|
54
|
-
if (
|
|
55
|
-
let n = `${e}:${
|
|
56
|
-
|
|
57
|
-
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${t}" \xE9 anterior ao m\xEDnimo recomendado "${
|
|
95
|
+
let e = ee(), i = Y[e];
|
|
96
|
+
if (!i) return;
|
|
97
|
+
let r = A(t);
|
|
98
|
+
if (Z(r, i) < 0) {
|
|
99
|
+
let n = `${e}:${r}`;
|
|
100
|
+
R.has(n) || (R.add(n), console.warn(
|
|
101
|
+
`[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${t}" \xE9 anterior ao m\xEDnimo recomendado "${i}" para SDK major ${e}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
|
|
58
102
|
));
|
|
59
103
|
}
|
|
60
104
|
}
|
|
61
|
-
function
|
|
105
|
+
function T(t) {
|
|
62
106
|
return t === 401 ? "unauthorized" : t === 403 ? "forbidden" : t === 404 ? "not_found" : t === 409 ? "conflict" : t === 410 ? "gone" : t === 422 || t === 400 ? "validation_failed" : t === 429 ? "rate_limited" : t >= 500 ? "server_error" : "unknown";
|
|
63
107
|
}
|
|
64
108
|
|
|
65
109
|
// src/validators.ts
|
|
66
|
-
var
|
|
67
|
-
function
|
|
68
|
-
if (typeof e != "string" || !
|
|
110
|
+
var S = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
|
|
111
|
+
function b(t, e) {
|
|
112
|
+
if (typeof e != "string" || !S.test(e))
|
|
69
113
|
throw new u(
|
|
70
114
|
"validation_failed",
|
|
71
|
-
`${t} must match ${
|
|
115
|
+
`${t} must match ${S.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
|
|
72
116
|
);
|
|
73
117
|
}
|
|
74
118
|
|
|
75
119
|
// src/ai.ts
|
|
76
|
-
var
|
|
77
|
-
function
|
|
120
|
+
var te = "/api/sdk/v1/ai/chat", v = 12e4, _ = 32768, C = 32, I = 4194304, M = 2048, ne = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, re = /^https:\/\/[^\s]+$/, ie = /* @__PURE__ */ new Set(["low", "high", "auto"]), oe = /* @__PURE__ */ new Set(["system", "user", "assistant"]), j = 100, N = 128, O = 32768, $ = 0, P = 2, D = 200, q = 20, F = 12 * 1024 * 1024, L = 5e4, U = 16, H = 500, se = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), ae = /^[a-zA-Z0-9_-]+$/;
|
|
121
|
+
function a(t) {
|
|
78
122
|
throw new u("validation_failed", t);
|
|
79
123
|
}
|
|
80
|
-
function
|
|
124
|
+
function ce(t) {
|
|
81
125
|
if (t.length === 0 || t.length % 4 !== 0) return false;
|
|
82
126
|
let e = t.endsWith("==") ? 2 : t.endsWith("=") ? 1 : 0;
|
|
83
127
|
return !(e > 0 ? t.slice(0, t.length - e) : t).includes("=");
|
|
84
128
|
}
|
|
85
|
-
function
|
|
129
|
+
function ue(t) {
|
|
86
130
|
let e = t.indexOf(",");
|
|
87
131
|
if (e === -1) return 0;
|
|
88
|
-
let
|
|
89
|
-
return Math.max(0, Math.floor(
|
|
132
|
+
let i = t.slice(e + 1), r = i.endsWith("==") ? 2 : i.endsWith("=") ? 1 : 0;
|
|
133
|
+
return Math.max(0, Math.floor(i.length * 3 / 4) - r);
|
|
90
134
|
}
|
|
91
|
-
function
|
|
92
|
-
let e = 0,
|
|
135
|
+
function de(t) {
|
|
136
|
+
let e = 0, i = 0, r = 0;
|
|
93
137
|
for (let n of t) {
|
|
94
|
-
let
|
|
95
|
-
if (Array.isArray(
|
|
96
|
-
for (let
|
|
97
|
-
if (e++, e >
|
|
98
|
-
return `ai: total content parts exceeds ${
|
|
99
|
-
if (
|
|
100
|
-
if (
|
|
101
|
-
return `ai: total images exceeds ${
|
|
102
|
-
let
|
|
103
|
-
if (typeof
|
|
104
|
-
return `ai: aggregate image bytes exceeds ${
|
|
138
|
+
let o = n?.content;
|
|
139
|
+
if (Array.isArray(o))
|
|
140
|
+
for (let s of o) {
|
|
141
|
+
if (e++, e > D)
|
|
142
|
+
return `ai: total content parts exceeds ${D} per request`;
|
|
143
|
+
if (s && typeof s == "object" && s.type === "image_url") {
|
|
144
|
+
if (i++, i > q)
|
|
145
|
+
return `ai: total images exceeds ${q} per request`;
|
|
146
|
+
let c = s.image_url?.url;
|
|
147
|
+
if (typeof c == "string" && c.startsWith("data:") && (r += ue(c), r > F))
|
|
148
|
+
return `ai: aggregate image bytes exceeds ${F} per request`;
|
|
105
149
|
}
|
|
106
150
|
}
|
|
107
151
|
}
|
|
108
152
|
return null;
|
|
109
153
|
}
|
|
110
|
-
function
|
|
154
|
+
function le(t) {
|
|
111
155
|
if (t === void 0) return null;
|
|
112
156
|
if (t === null || typeof t != "object")
|
|
113
157
|
return "ai: responseFormat json_schema.schema must be an object";
|
|
@@ -118,144 +162,144 @@ function oe(t) {
|
|
|
118
162
|
return "ai: responseFormat json_schema.schema is not serializable";
|
|
119
163
|
}
|
|
120
164
|
if (e === void 0) return "ai: responseFormat json_schema.schema is invalid";
|
|
121
|
-
if (e.length >
|
|
122
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
123
|
-
let
|
|
124
|
-
if (
|
|
125
|
-
return `ai: responseFormat json_schema.schema exceeds max depth ${
|
|
165
|
+
if (e.length > L)
|
|
166
|
+
return `ai: responseFormat json_schema.schema exceeds ${L} serialized chars`;
|
|
167
|
+
let i = 0, r = (n, o) => {
|
|
168
|
+
if (o > U)
|
|
169
|
+
return `ai: responseFormat json_schema.schema exceeds max depth ${U}`;
|
|
126
170
|
if (Array.isArray(n)) {
|
|
127
|
-
for (let
|
|
128
|
-
let
|
|
129
|
-
if (
|
|
171
|
+
for (let s of n) {
|
|
172
|
+
let c = r(s, o + 1);
|
|
173
|
+
if (c) return c;
|
|
130
174
|
}
|
|
131
175
|
return null;
|
|
132
176
|
}
|
|
133
177
|
if (n && typeof n == "object")
|
|
134
|
-
for (let
|
|
135
|
-
if (
|
|
136
|
-
return `ai: responseFormat json_schema.schema has forbidden key: ${
|
|
137
|
-
if (
|
|
138
|
-
return `ai: responseFormat json_schema.schema exceeds ${
|
|
139
|
-
let
|
|
140
|
-
if (
|
|
178
|
+
for (let s of Object.getOwnPropertyNames(n)) {
|
|
179
|
+
if (se.has(s))
|
|
180
|
+
return `ai: responseFormat json_schema.schema has forbidden key: ${s}`;
|
|
181
|
+
if (i++, i > H)
|
|
182
|
+
return `ai: responseFormat json_schema.schema exceeds ${H} keys`;
|
|
183
|
+
let c = r(n[s], o + 1);
|
|
184
|
+
if (c) return c;
|
|
141
185
|
}
|
|
142
186
|
return null;
|
|
143
187
|
};
|
|
144
|
-
return
|
|
188
|
+
return r(t, 0);
|
|
145
189
|
}
|
|
146
|
-
function
|
|
147
|
-
(typeof t != "string" || t.length === 0) &&
|
|
190
|
+
function fe(t) {
|
|
191
|
+
(typeof t != "string" || t.length === 0) && a("ai: image_url.url must be a non-empty string");
|
|
148
192
|
let e = t;
|
|
149
193
|
if (e.startsWith("data:")) {
|
|
150
|
-
e.length >
|
|
151
|
-
let
|
|
152
|
-
|
|
194
|
+
e.length > I && a(`ai: image_url.url (data:) exceeds ${I} chars`), ne.test(e) || a("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
|
|
195
|
+
let i = e.slice(e.indexOf(",") + 1);
|
|
196
|
+
ce(i) || a("ai: image_url.url data: has invalid base64 (length/padding)");
|
|
153
197
|
return;
|
|
154
198
|
}
|
|
155
199
|
if (e.startsWith("https://")) {
|
|
156
|
-
e.length >
|
|
200
|
+
e.length > M && a(`ai: image_url.url (https:) exceeds ${M} chars`), re.test(e) || a("ai: image_url.url https: is invalid");
|
|
157
201
|
return;
|
|
158
202
|
}
|
|
159
|
-
|
|
203
|
+
a("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
|
|
160
204
|
}
|
|
161
|
-
function
|
|
205
|
+
function me(t, e) {
|
|
162
206
|
if (typeof t == "string") {
|
|
163
|
-
t.length === 0 &&
|
|
207
|
+
t.length === 0 && a("ai: message content string must be non-empty"), t.length > _ && a(`ai: message content exceeds ${_} chars`);
|
|
164
208
|
return;
|
|
165
209
|
}
|
|
166
|
-
(!Array.isArray(t) || t.length === 0) &&
|
|
167
|
-
for (let
|
|
168
|
-
(!
|
|
169
|
-
let
|
|
170
|
-
if (
|
|
171
|
-
let n =
|
|
172
|
-
(typeof n != "string" || n.length === 0) &&
|
|
173
|
-
} else if (
|
|
174
|
-
e !== "user" &&
|
|
175
|
-
let n =
|
|
176
|
-
(!n || typeof n != "object") &&
|
|
177
|
-
let
|
|
178
|
-
|
|
210
|
+
(!Array.isArray(t) || t.length === 0) && a("ai: message content must be a non-empty string or array of parts"), t.length > C && a(`ai: message content has more than ${C} parts`);
|
|
211
|
+
for (let i of t) {
|
|
212
|
+
(!i || typeof i != "object") && a("ai: each content part must be an object");
|
|
213
|
+
let r = i.type;
|
|
214
|
+
if (r === "text") {
|
|
215
|
+
let n = i.text;
|
|
216
|
+
(typeof n != "string" || n.length === 0) && a("ai: text part must have non-empty { text: string }"), n.length > _ && a(`ai: text part exceeds ${_} chars`);
|
|
217
|
+
} else if (r === "image_url") {
|
|
218
|
+
e !== "user" && a(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
|
|
219
|
+
let n = i.image_url;
|
|
220
|
+
(!n || typeof n != "object") && a("ai: image_url part must have { image_url: { url } }"), fe(n.url);
|
|
221
|
+
let o = n.detail;
|
|
222
|
+
o !== void 0 && (typeof o != "string" || !ie.has(o)) && a("ai: image_url.detail must be 'low' | 'high' | 'auto'");
|
|
179
223
|
} else
|
|
180
|
-
|
|
224
|
+
a("ai: content part type must be 'text' or 'image_url'");
|
|
181
225
|
}
|
|
182
226
|
}
|
|
183
|
-
function
|
|
184
|
-
(!t || typeof t != "object") &&
|
|
227
|
+
function pe(t) {
|
|
228
|
+
(!t || typeof t != "object") && a("ai: responseFormat must be an object");
|
|
185
229
|
let e = t.type;
|
|
186
230
|
if (!(e === "text" || e === "json_object")) {
|
|
187
231
|
if (e === "json_schema") {
|
|
188
|
-
let
|
|
189
|
-
(!
|
|
190
|
-
let
|
|
191
|
-
if ((typeof n != "string" || n.length === 0) &&
|
|
192
|
-
let
|
|
193
|
-
|
|
232
|
+
let i = t.json_schema;
|
|
233
|
+
(!i || typeof i != "object") && a("ai: responseFormat json_schema requires { json_schema: { name } }");
|
|
234
|
+
let r = i, n = r.name;
|
|
235
|
+
if ((typeof n != "string" || n.length === 0) && a("ai: responseFormat json_schema.name is required"), n.length > 64 && a("ai: responseFormat json_schema.name exceeds 64 chars"), ae.test(n) || a("ai: responseFormat json_schema.name must match a-zA-Z0-9_-"), r.description !== void 0 && (typeof r.description != "string" && a("ai: responseFormat json_schema.description must be a string"), r.description.length > 1e3 && a("ai: responseFormat json_schema.description exceeds 1000 chars")), r.strict !== void 0 && typeof r.strict != "boolean" && a("ai: responseFormat json_schema.strict must be a boolean"), r.schema !== void 0) {
|
|
236
|
+
let o = le(r.schema);
|
|
237
|
+
o && a(o);
|
|
194
238
|
}
|
|
195
239
|
return;
|
|
196
240
|
}
|
|
197
|
-
|
|
241
|
+
a("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
|
|
198
242
|
}
|
|
199
243
|
}
|
|
200
|
-
function
|
|
244
|
+
function K(t, e, i) {
|
|
201
245
|
if (!e || typeof e != "object")
|
|
202
246
|
throw new u("validation_failed", "ai: options object is required");
|
|
203
247
|
if (typeof e.model != "string" || e.model.length === 0)
|
|
204
248
|
throw new u("validation_failed", "ai: model is required");
|
|
205
|
-
if (e.model.length >
|
|
206
|
-
throw new u("validation_failed", `ai: model exceeds ${
|
|
249
|
+
if (e.model.length > N)
|
|
250
|
+
throw new u("validation_failed", `ai: model exceeds ${N} chars`);
|
|
207
251
|
if (!Array.isArray(e.messages) || e.messages.length === 0)
|
|
208
252
|
throw new u("validation_failed", "ai: messages must be a non-empty array");
|
|
209
|
-
if (e.messages.length >
|
|
210
|
-
throw new u("validation_failed", `ai: messages exceeds ${
|
|
211
|
-
for (let
|
|
212
|
-
if (!
|
|
253
|
+
if (e.messages.length > j)
|
|
254
|
+
throw new u("validation_failed", `ai: messages exceeds ${j} per request`);
|
|
255
|
+
for (let c of e.messages) {
|
|
256
|
+
if (!c || typeof c != "object" || typeof c.role != "string" || !oe.has(c.role))
|
|
213
257
|
throw new u(
|
|
214
258
|
"validation_failed",
|
|
215
259
|
"ai: each message role must be 'system' | 'user' | 'assistant'"
|
|
216
260
|
);
|
|
217
|
-
|
|
261
|
+
me(c.content, c.role);
|
|
218
262
|
}
|
|
219
|
-
let
|
|
220
|
-
if (
|
|
221
|
-
let n = e.productId ?? t.productId,
|
|
263
|
+
let r = de(e.messages);
|
|
264
|
+
if (r) throw new u("validation_failed", r);
|
|
265
|
+
let n = e.productId ?? t.productId, o = e.tenantId ?? t.tenantId;
|
|
222
266
|
if (!n)
|
|
223
267
|
throw new u(
|
|
224
268
|
"validation_failed",
|
|
225
269
|
"ai: productId required (pass to options or set on createNeetruClient)"
|
|
226
270
|
);
|
|
227
|
-
if (!
|
|
271
|
+
if (!o)
|
|
228
272
|
throw new u(
|
|
229
273
|
"validation_failed",
|
|
230
274
|
"ai: tenantId required (pass to options or set on createNeetruClient)"
|
|
231
275
|
);
|
|
232
|
-
|
|
233
|
-
let
|
|
276
|
+
b("productId", n), b("tenantId", o);
|
|
277
|
+
let s = {
|
|
234
278
|
productId: n,
|
|
235
|
-
tenantId:
|
|
279
|
+
tenantId: o,
|
|
236
280
|
model: e.model,
|
|
237
281
|
messages: e.messages,
|
|
238
|
-
stream:
|
|
282
|
+
stream: i
|
|
239
283
|
};
|
|
240
284
|
if (e.maxTokens !== void 0) {
|
|
241
|
-
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens >
|
|
285
|
+
if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > O)
|
|
242
286
|
throw new u(
|
|
243
287
|
"validation_failed",
|
|
244
|
-
`ai: maxTokens must be a positive integer <= ${
|
|
288
|
+
`ai: maxTokens must be a positive integer <= ${O}`
|
|
245
289
|
);
|
|
246
|
-
|
|
290
|
+
s.maxTokens = e.maxTokens;
|
|
247
291
|
}
|
|
248
292
|
if (e.temperature !== void 0) {
|
|
249
|
-
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature <
|
|
293
|
+
if (typeof e.temperature != "number" || !Number.isFinite(e.temperature) || e.temperature < $ || e.temperature > P)
|
|
250
294
|
throw new u(
|
|
251
295
|
"validation_failed",
|
|
252
|
-
`ai: temperature must be a finite number between ${
|
|
296
|
+
`ai: temperature must be a finite number between ${$} and ${P}`
|
|
253
297
|
);
|
|
254
|
-
|
|
298
|
+
s.temperature = e.temperature;
|
|
255
299
|
}
|
|
256
|
-
return e.responseFormat !== void 0 && (
|
|
300
|
+
return e.responseFormat !== void 0 && (pe(e.responseFormat), s.responseFormat = e.responseFormat), s;
|
|
257
301
|
}
|
|
258
|
-
async function
|
|
302
|
+
async function B(t) {
|
|
259
303
|
try {
|
|
260
304
|
let e = await t.text();
|
|
261
305
|
return e ? JSON.parse(e) : void 0;
|
|
@@ -263,129 +307,129 @@ async function X(t) {
|
|
|
263
307
|
return;
|
|
264
308
|
}
|
|
265
309
|
}
|
|
266
|
-
function
|
|
267
|
-
let
|
|
268
|
-
return `${
|
|
310
|
+
function ge(t, e) {
|
|
311
|
+
let i = t.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
|
|
312
|
+
return `${i}${r}`;
|
|
269
313
|
}
|
|
270
|
-
async function
|
|
314
|
+
async function X(t, e, i, r) {
|
|
271
315
|
if (!t.apiKey)
|
|
272
316
|
throw new u(
|
|
273
317
|
"missing_api_key",
|
|
274
318
|
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
275
319
|
);
|
|
276
|
-
let n =
|
|
320
|
+
let n = ge(t.baseUrl, te), { headers: o, correlationId: s } = x({
|
|
277
321
|
"content-type": "application/json",
|
|
278
|
-
accept:
|
|
322
|
+
accept: r ? "text/event-stream" : "application/json",
|
|
279
323
|
authorization: `Bearer ${t.apiKey}`
|
|
280
|
-
}, body: JSON.stringify(e) };
|
|
281
|
-
|
|
282
|
-
let
|
|
324
|
+
}), c = { method: "POST", headers: o, body: JSON.stringify(e) };
|
|
325
|
+
r ? i && (c.signal = i) : c.signal = i ?? AbortSignal.timeout(v);
|
|
326
|
+
let l;
|
|
283
327
|
try {
|
|
284
|
-
|
|
328
|
+
l = await t.fetch(n, c);
|
|
285
329
|
} catch (d) {
|
|
286
|
-
let
|
|
287
|
-
throw new u("network_error",
|
|
330
|
+
let m = d instanceof DOMException && d.name === "TimeoutError" ? `Network error: timeout after ${v / 1e3}s` : d instanceof DOMException && d.name === "AbortError" ? "Network error: request aborted" : `Network error: ${d instanceof Error ? d.message : "fetch failed"}`;
|
|
331
|
+
throw new u("network_error", m, void 0, void 0, void 0, s);
|
|
288
332
|
}
|
|
289
|
-
let
|
|
290
|
-
if (!
|
|
291
|
-
let d = await
|
|
333
|
+
let y = l.headers.get("x-request-id") ?? l.headers.get("x-correlation-id") ?? void 0;
|
|
334
|
+
if (!l.ok) {
|
|
335
|
+
let d = await B(l), m = T(l.status), g = `HTTP ${l.status}`;
|
|
292
336
|
if (d && typeof d == "object" && "error" in d) {
|
|
293
|
-
let
|
|
294
|
-
typeof
|
|
337
|
+
let f = d.error;
|
|
338
|
+
typeof f == "string" ? g = f : f && typeof f == "object" && (typeof f.code == "string" && (m = f.code), typeof f.message == "string" && (g = f.message));
|
|
295
339
|
}
|
|
296
|
-
throw new u(
|
|
340
|
+
throw new u(m, g, l.status, y, void 0, s);
|
|
297
341
|
}
|
|
298
|
-
let
|
|
299
|
-
return
|
|
342
|
+
let p = l.headers.get("x-neetru-version");
|
|
343
|
+
return p && k(p), l;
|
|
300
344
|
}
|
|
301
|
-
function
|
|
345
|
+
function z(t) {
|
|
302
346
|
return {
|
|
303
347
|
provider: t.headers.get("x-neetru-provider") ?? "",
|
|
304
348
|
usageWarning: t.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
305
349
|
};
|
|
306
350
|
}
|
|
307
|
-
function
|
|
351
|
+
function W(t) {
|
|
308
352
|
let e = [];
|
|
309
|
-
for (let
|
|
353
|
+
for (let r of t.split(`
|
|
310
354
|
`)) {
|
|
311
|
-
let n =
|
|
355
|
+
let n = r.replace(/\r$/, "");
|
|
312
356
|
if (n.startsWith("data:")) {
|
|
313
|
-
let
|
|
314
|
-
|
|
357
|
+
let o = n.slice(5);
|
|
358
|
+
o.startsWith(" ") && (o = o.slice(1)), e.push(o);
|
|
315
359
|
}
|
|
316
360
|
}
|
|
317
361
|
if (e.length === 0) return null;
|
|
318
|
-
let
|
|
362
|
+
let i = e.join(`
|
|
319
363
|
`);
|
|
320
|
-
if (
|
|
364
|
+
if (i === "[DONE]") return { done: true };
|
|
321
365
|
try {
|
|
322
|
-
let n = JSON.parse(
|
|
366
|
+
let n = JSON.parse(i)?.choices?.[0]?.delta?.content;
|
|
323
367
|
return typeof n == "string" && n.length > 0 ? { content: n } : null;
|
|
324
368
|
} catch {
|
|
325
369
|
return null;
|
|
326
370
|
}
|
|
327
371
|
}
|
|
328
|
-
function
|
|
372
|
+
function ke(t) {
|
|
329
373
|
return {
|
|
330
374
|
async chat(e) {
|
|
331
|
-
let
|
|
332
|
-
if (!
|
|
375
|
+
let i = K(t, e, false), r = await X(t, i, e.signal, false), { provider: n, usageWarning: o } = z(r), s = await B(r);
|
|
376
|
+
if (!s || s.ok !== true || !Array.isArray(s.choices))
|
|
333
377
|
throw new u("invalid_response", "ai.chat response missing ok/choices");
|
|
334
378
|
return {
|
|
335
|
-
choices:
|
|
336
|
-
usage:
|
|
337
|
-
model:
|
|
338
|
-
provider:
|
|
339
|
-
usageWarning:
|
|
379
|
+
choices: s.choices,
|
|
380
|
+
usage: s.usage ?? { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
381
|
+
model: s.model ?? e.model,
|
|
382
|
+
provider: s.provider ?? n ?? "",
|
|
383
|
+
usageWarning: o
|
|
340
384
|
};
|
|
341
385
|
},
|
|
342
386
|
stream(e) {
|
|
343
|
-
let
|
|
387
|
+
let i = K(t, e, true), r = X(t, i, e.signal, true), n = r.then(z);
|
|
344
388
|
n.catch(() => {
|
|
345
389
|
});
|
|
346
|
-
async function*
|
|
347
|
-
let
|
|
348
|
-
if (!
|
|
390
|
+
async function* o() {
|
|
391
|
+
let c = (await r).body;
|
|
392
|
+
if (!c)
|
|
349
393
|
throw new u("invalid_response", "ai.stream: response has no body");
|
|
350
|
-
let
|
|
394
|
+
let l = c.getReader(), y = new TextDecoder(), p = "";
|
|
351
395
|
try {
|
|
352
396
|
for (; ; ) {
|
|
353
|
-
let { done: m, value:
|
|
397
|
+
let { done: m, value: g } = await l.read();
|
|
354
398
|
if (m) break;
|
|
355
|
-
|
|
356
|
-
let
|
|
357
|
-
for (; (
|
|
399
|
+
p += y.decode(g, { stream: !0 });
|
|
400
|
+
let f;
|
|
401
|
+
for (; (f = p.indexOf(`
|
|
358
402
|
|
|
359
403
|
`)) !== -1; ) {
|
|
360
|
-
let
|
|
361
|
-
|
|
362
|
-
let
|
|
363
|
-
if (
|
|
364
|
-
|
|
404
|
+
let G = p.slice(0, f);
|
|
405
|
+
p = p.slice(f + 2);
|
|
406
|
+
let h = W(G);
|
|
407
|
+
if (h && "done" in h) return;
|
|
408
|
+
h && "content" in h && (yield h.content);
|
|
365
409
|
}
|
|
366
410
|
}
|
|
367
|
-
let
|
|
368
|
-
if (
|
|
369
|
-
let m =
|
|
411
|
+
let d = p.trim();
|
|
412
|
+
if (d) {
|
|
413
|
+
let m = W(d);
|
|
370
414
|
m && "content" in m && (yield m.content);
|
|
371
415
|
}
|
|
372
416
|
} finally {
|
|
373
417
|
try {
|
|
374
|
-
|
|
418
|
+
l.releaseLock();
|
|
375
419
|
} catch {
|
|
376
420
|
}
|
|
377
421
|
try {
|
|
378
|
-
await
|
|
422
|
+
await c.cancel();
|
|
379
423
|
} catch {
|
|
380
424
|
}
|
|
381
425
|
}
|
|
382
426
|
}
|
|
383
427
|
return {
|
|
384
428
|
meta: n,
|
|
385
|
-
[Symbol.asyncIterator]:
|
|
429
|
+
[Symbol.asyncIterator]: o
|
|
386
430
|
};
|
|
387
431
|
}
|
|
388
432
|
};
|
|
389
433
|
}
|
|
390
434
|
|
|
391
|
-
export {
|
|
435
|
+
export { ke as createAiNamespace };
|