@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.
Files changed (73) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/README.md +66 -1
  3. package/dist/ai.cjs +221 -177
  4. package/dist/ai.d.cts +1 -1
  5. package/dist/ai.d.ts +1 -1
  6. package/dist/ai.mjs +221 -177
  7. package/dist/auth.cjs +1051 -868
  8. package/dist/auth.d.cts +1 -1
  9. package/dist/auth.d.ts +1 -1
  10. package/dist/auth.mjs +1048 -865
  11. package/dist/catalog.cjs +164 -97
  12. package/dist/catalog.d.cts +1 -1
  13. package/dist/catalog.d.ts +1 -1
  14. package/dist/catalog.mjs +164 -97
  15. package/dist/checkout.cjs +182 -115
  16. package/dist/checkout.d.cts +1 -1
  17. package/dist/checkout.d.ts +1 -1
  18. package/dist/checkout.mjs +181 -114
  19. package/dist/db-react.d.cts +1 -1
  20. package/dist/db-react.d.ts +1 -1
  21. package/dist/db.cjs +437 -345
  22. package/dist/db.d.cts +1 -1
  23. package/dist/db.d.ts +1 -1
  24. package/dist/db.mjs +455 -363
  25. package/dist/entitlements.cjs +17 -10
  26. package/dist/entitlements.d.cts +1 -1
  27. package/dist/entitlements.d.ts +1 -1
  28. package/dist/entitlements.mjs +17 -10
  29. package/dist/errors.cjs +15 -8
  30. package/dist/errors.d.cts +24 -3
  31. package/dist/errors.d.ts +24 -3
  32. package/dist/errors.mjs +14 -7
  33. package/dist/firestore-compat.d.cts +1 -1
  34. package/dist/firestore-compat.d.ts +1 -1
  35. package/dist/index.cjs +1544 -1190
  36. package/dist/index.d.cts +177 -3
  37. package/dist/index.d.ts +177 -3
  38. package/dist/index.mjs +1676 -1329
  39. package/dist/mobile.cjs +400 -181
  40. package/dist/mobile.d.cts +59 -4
  41. package/dist/mobile.d.ts +59 -4
  42. package/dist/mobile.mjs +400 -181
  43. package/dist/mocks.cjs +9 -2
  44. package/dist/mocks.d.cts +1 -1
  45. package/dist/mocks.d.ts +1 -1
  46. package/dist/mocks.mjs +9 -2
  47. package/dist/notifications.cjs +197 -130
  48. package/dist/notifications.d.cts +1 -1
  49. package/dist/notifications.d.ts +1 -1
  50. package/dist/notifications.mjs +196 -129
  51. package/dist/react.cjs +303 -282
  52. package/dist/react.d.cts +1 -1
  53. package/dist/react.d.ts +1 -1
  54. package/dist/react.mjs +295 -274
  55. package/dist/support.cjs +299 -229
  56. package/dist/support.d.cts +8 -4
  57. package/dist/support.d.ts +8 -4
  58. package/dist/support.mjs +298 -228
  59. package/dist/telemetry.cjs +234 -167
  60. package/dist/telemetry.d.cts +1 -1
  61. package/dist/telemetry.d.ts +1 -1
  62. package/dist/telemetry.mjs +234 -167
  63. package/dist/{types-Dx0IrOSF.d.ts → types-BYPk_3HR.d.ts} +104 -17
  64. package/dist/{types-Bx4kTLmk.d.cts → types-yv3EDKg2.d.cts} +104 -17
  65. package/dist/usage.cjs +183 -116
  66. package/dist/usage.d.cts +1 -1
  67. package/dist/usage.d.ts +1 -1
  68. package/dist/usage.mjs +183 -116
  69. package/dist/webhooks.cjs +215 -148
  70. package/dist/webhooks.d.cts +1 -1
  71. package/dist/webhooks.d.ts +1 -1
  72. package/dist/webhooks.mjs +212 -145
  73. 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
- constructor(e, r, s, n, i) {
16
- super(r), this.name = "NeetruError", this.code = e, this.status = s, this.requestId = n, this.reason = i, Object.setPrototypeOf(this, t.prototype);
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 z = {
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 b(t) {
70
+ function A(t) {
27
71
  return t.split("+")[0] ?? t;
28
72
  }
29
- function B(t, e) {
30
- let r = b(t).split(".").map(Number), s = b(e).split(".").map(Number), n = Math.max(r.length, s.length);
31
- for (let i = 0; i < n; i++) {
32
- let a = r[i] ?? 0, o = s[i] ?? 0;
33
- if (a !== o) return a - o;
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 G() {
81
+ function Q() {
38
82
  try {
39
- return "3.7.0";
83
+ return "3.8.1";
40
84
  } catch {
41
85
  return;
42
86
  }
43
87
  }
44
- function J() {
45
- let t = G();
88
+ function ee() {
89
+ let t = Q();
46
90
  return t ? parseInt(t.split(".")[0] ?? "3", 10) : 3;
47
91
  }
48
- var w = /* @__PURE__ */ new Set();
49
- function E(t) {
92
+ var R = /* @__PURE__ */ new Set();
93
+ function k(t) {
50
94
  if (!t) return;
51
- let e = J(), r = z[e];
52
- if (!r) return;
53
- let s = b(t);
54
- if (B(s, r) < 0) {
55
- let n = `${e}:${s}`;
56
- w.has(n) || (w.add(n), console.warn(
57
- `[Neetru SDK] Aten\xE7\xE3o: Core vers\xE3o "${t}" \xE9 anterior ao m\xEDnimo recomendado "${r}" para SDK major ${e}. Alguns recursos podem n\xE3o funcionar corretamente. Atualize o Core para a vers\xE3o mais recente.`
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 x(t) {
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 k = /^[a-z0-9][a-z0-9_-]{0,62}$/i;
67
- function A(t, e) {
68
- if (typeof e != "string" || !k.test(e))
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 ${k.source} (lowercase alphanumeric, _, -; first char must be alphanumeric; max 63 chars)`
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 V = "/api/sdk/v1/ai/chat", T = 12e4, _ = 32768, R = 32, v = 4194304, S = 2048, Y = /^data:image\/(?:png|jpe?g|gif|webp);base64,[A-Za-z0-9+/]+={0,2}$/, Z = /^https:\/\/[^\s]+$/, Q = /* @__PURE__ */ new Set(["low", "high", "auto"]), ee = /* @__PURE__ */ new Set(["system", "user", "assistant"]), M = 100, j = 128, C = 32768, N = 0, I = 2, O = 200, P = 20, $ = 12 * 1024 * 1024, q = 5e4, F = 16, D = 500, te = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), ne = /^[a-zA-Z0-9_-]+$/;
77
- function c(t) {
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 re(t) {
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 se(t) {
129
+ function ue(t) {
86
130
  let e = t.indexOf(",");
87
131
  if (e === -1) return 0;
88
- let r = t.slice(e + 1), s = r.endsWith("==") ? 2 : r.endsWith("=") ? 1 : 0;
89
- return Math.max(0, Math.floor(r.length * 3 / 4) - s);
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 ie(t) {
92
- let e = 0, r = 0, s = 0;
135
+ function de(t) {
136
+ let e = 0, i = 0, r = 0;
93
137
  for (let n of t) {
94
- let i = n?.content;
95
- if (Array.isArray(i))
96
- for (let a of i) {
97
- if (e++, e > O)
98
- return `ai: total content parts exceeds ${O} per request`;
99
- if (a && typeof a == "object" && a.type === "image_url") {
100
- if (r++, r > P)
101
- return `ai: total images exceeds ${P} per request`;
102
- let o = a.image_url?.url;
103
- if (typeof o == "string" && o.startsWith("data:") && (s += se(o), s > $))
104
- return `ai: aggregate image bytes exceeds ${$} per request`;
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 oe(t) {
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 > q)
122
- return `ai: responseFormat json_schema.schema exceeds ${q} serialized chars`;
123
- let r = 0, s = (n, i) => {
124
- if (i > F)
125
- return `ai: responseFormat json_schema.schema exceeds max depth ${F}`;
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 a of n) {
128
- let o = s(a, i + 1);
129
- if (o) return o;
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 a of Object.getOwnPropertyNames(n)) {
135
- if (te.has(a))
136
- return `ai: responseFormat json_schema.schema has forbidden key: ${a}`;
137
- if (r++, r > D)
138
- return `ai: responseFormat json_schema.schema exceeds ${D} keys`;
139
- let o = s(n[a], i + 1);
140
- if (o) return o;
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 s(t, 0);
188
+ return r(t, 0);
145
189
  }
146
- function ae(t) {
147
- (typeof t != "string" || t.length === 0) && c("ai: image_url.url must be a non-empty string");
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 > v && c(`ai: image_url.url (data:) exceeds ${v} chars`), Y.test(e) || c("ai: image_url.url data: must be data:image/<png|jpeg|jpg|gif|webp>;base64,<...>");
151
- let r = e.slice(e.indexOf(",") + 1);
152
- re(r) || c("ai: image_url.url data: has invalid base64 (length/padding)");
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 > S && c(`ai: image_url.url (https:) exceeds ${S} chars`), Z.test(e) || c("ai: image_url.url https: is invalid");
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
- c("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
203
+ a("ai: image_url.url must be a data:image/...;base64 or https:// URL (other schemes are rejected)");
160
204
  }
161
- function ce(t, e) {
205
+ function me(t, e) {
162
206
  if (typeof t == "string") {
163
- t.length === 0 && c("ai: message content string must be non-empty"), t.length > _ && c(`ai: message content exceeds ${_} chars`);
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) && c("ai: message content must be a non-empty string or array of parts"), t.length > R && c(`ai: message content has more than ${R} parts`);
167
- for (let r of t) {
168
- (!r || typeof r != "object") && c("ai: each content part must be an object");
169
- let s = r.type;
170
- if (s === "text") {
171
- let n = r.text;
172
- (typeof n != "string" || n.length === 0) && c("ai: text part must have non-empty { text: string }"), n.length > _ && c(`ai: text part exceeds ${_} chars`);
173
- } else if (s === "image_url") {
174
- e !== "user" && c(`ai: image_url is only allowed in role 'user' messages (got '${e}')`);
175
- let n = r.image_url;
176
- (!n || typeof n != "object") && c("ai: image_url part must have { image_url: { url } }"), ae(n.url);
177
- let i = n.detail;
178
- i !== void 0 && (typeof i != "string" || !Q.has(i)) && c("ai: image_url.detail must be 'low' | 'high' | 'auto'");
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
- c("ai: content part type must be 'text' or 'image_url'");
224
+ a("ai: content part type must be 'text' or 'image_url'");
181
225
  }
182
226
  }
183
- function ue(t) {
184
- (!t || typeof t != "object") && c("ai: responseFormat must be an 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 r = t.json_schema;
189
- (!r || typeof r != "object") && c("ai: responseFormat json_schema requires { json_schema: { name } }");
190
- let s = r, n = s.name;
191
- if ((typeof n != "string" || n.length === 0) && c("ai: responseFormat json_schema.name is required"), n.length > 64 && c("ai: responseFormat json_schema.name exceeds 64 chars"), ne.test(n) || c("ai: responseFormat json_schema.name must match a-zA-Z0-9_-"), s.description !== void 0 && (typeof s.description != "string" && c("ai: responseFormat json_schema.description must be a string"), s.description.length > 1e3 && c("ai: responseFormat json_schema.description exceeds 1000 chars")), s.strict !== void 0 && typeof s.strict != "boolean" && c("ai: responseFormat json_schema.strict must be a boolean"), s.schema !== void 0) {
192
- let i = oe(s.schema);
193
- i && c(i);
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
- c("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
241
+ a("ai: responseFormat.type must be 'text' | 'json_object' | 'json_schema'");
198
242
  }
199
243
  }
200
- function U(t, e, r) {
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 > j)
206
- throw new u("validation_failed", `ai: model exceeds ${j} chars`);
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 > M)
210
- throw new u("validation_failed", `ai: messages exceeds ${M} per request`);
211
- for (let o of e.messages) {
212
- if (!o || typeof o != "object" || typeof o.role != "string" || !ee.has(o.role))
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
- ce(o.content, o.role);
261
+ me(c.content, c.role);
218
262
  }
219
- let s = ie(e.messages);
220
- if (s) throw new u("validation_failed", s);
221
- let n = e.productId ?? t.productId, i = e.tenantId ?? t.tenantId;
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 (!i)
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
- A("productId", n), A("tenantId", i);
233
- let a = {
276
+ b("productId", n), b("tenantId", o);
277
+ let s = {
234
278
  productId: n,
235
- tenantId: i,
279
+ tenantId: o,
236
280
  model: e.model,
237
281
  messages: e.messages,
238
- stream: r
282
+ stream: i
239
283
  };
240
284
  if (e.maxTokens !== void 0) {
241
- if (!Number.isInteger(e.maxTokens) || e.maxTokens <= 0 || e.maxTokens > C)
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 <= ${C}`
288
+ `ai: maxTokens must be a positive integer <= ${O}`
245
289
  );
246
- a.maxTokens = e.maxTokens;
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 < N || e.temperature > I)
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 ${N} and ${I}`
296
+ `ai: temperature must be a finite number between ${$} and ${P}`
253
297
  );
254
- a.temperature = e.temperature;
298
+ s.temperature = e.temperature;
255
299
  }
256
- return e.responseFormat !== void 0 && (ue(e.responseFormat), a.responseFormat = e.responseFormat), a;
300
+ return e.responseFormat !== void 0 && (pe(e.responseFormat), s.responseFormat = e.responseFormat), s;
257
301
  }
258
- async function X(t) {
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 de(t, e) {
267
- let r = t.replace(/\/+$/, ""), s = e.startsWith("/") ? e : `/${e}`;
268
- return `${r}${s}`;
310
+ function ge(t, e) {
311
+ let i = t.replace(/\/+$/, ""), r = e.startsWith("/") ? e : `/${e}`;
312
+ return `${i}${r}`;
269
313
  }
270
- async function L(t, e, r, s) {
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 = de(t.baseUrl, V), a = { method: "POST", headers: {
320
+ let n = ge(t.baseUrl, te), { headers: o, correlationId: s } = x({
277
321
  "content-type": "application/json",
278
- accept: s ? "text/event-stream" : "application/json",
322
+ accept: r ? "text/event-stream" : "application/json",
279
323
  authorization: `Bearer ${t.apiKey}`
280
- }, body: JSON.stringify(e) };
281
- s ? r && (a.signal = r) : a.signal = r ?? AbortSignal.timeout(T);
282
- let o;
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
- o = await t.fetch(n, a);
328
+ l = await t.fetch(n, c);
285
329
  } catch (d) {
286
- let f = d instanceof DOMException && d.name === "TimeoutError" ? `Network error: timeout after ${T / 1e3}s` : d instanceof DOMException && d.name === "AbortError" ? "Network error: request aborted" : `Network error: ${d instanceof Error ? d.message : "fetch failed"}`;
287
- throw new u("network_error", f);
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 g = o.headers.get("x-request-id") ?? o.headers.get("x-correlation-id") ?? void 0;
290
- if (!o.ok) {
291
- let d = await X(o), f = x(o.status), m = `HTTP ${o.status}`;
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 l = d.error;
294
- typeof l == "string" ? m = l : l && typeof l == "object" && (typeof l.code == "string" && (f = l.code), typeof l.message == "string" && (m = l.message));
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(f, m, o.status, g);
340
+ throw new u(m, g, l.status, y, void 0, s);
297
341
  }
298
- let h = o.headers.get("x-neetru-version");
299
- return h && E(h), o;
342
+ let p = l.headers.get("x-neetru-version");
343
+ return p && k(p), l;
300
344
  }
301
- function H(t) {
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 K(t) {
351
+ function W(t) {
308
352
  let e = [];
309
- for (let s of t.split(`
353
+ for (let r of t.split(`
310
354
  `)) {
311
- let n = s.replace(/\r$/, "");
355
+ let n = r.replace(/\r$/, "");
312
356
  if (n.startsWith("data:")) {
313
- let i = n.slice(5);
314
- i.startsWith(" ") && (i = i.slice(1)), e.push(i);
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 r = e.join(`
362
+ let i = e.join(`
319
363
  `);
320
- if (r === "[DONE]") return { done: true };
364
+ if (i === "[DONE]") return { done: true };
321
365
  try {
322
- let n = JSON.parse(r)?.choices?.[0]?.delta?.content;
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 ye(t) {
372
+ function ke(t) {
329
373
  return {
330
374
  async chat(e) {
331
- let r = U(t, e, false), s = await L(t, r, e.signal, false), { provider: n, usageWarning: i } = H(s), a = await X(s);
332
- if (!a || a.ok !== true || !Array.isArray(a.choices))
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: a.choices,
336
- usage: a.usage ?? { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
337
- model: a.model ?? e.model,
338
- provider: a.provider ?? n ?? "",
339
- usageWarning: i
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 r = U(t, e, true), s = L(t, r, e.signal, true), n = s.then(H);
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* i() {
347
- let o = (await s).body;
348
- if (!o)
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 g = o.getReader(), h = new TextDecoder(), d = "";
394
+ let l = c.getReader(), y = new TextDecoder(), p = "";
351
395
  try {
352
396
  for (; ; ) {
353
- let { done: m, value: l } = await g.read();
397
+ let { done: m, value: g } = await l.read();
354
398
  if (m) break;
355
- d += h.decode(l, { stream: !0 });
356
- let y;
357
- for (; (y = d.indexOf(`
399
+ p += y.decode(g, { stream: !0 });
400
+ let f;
401
+ for (; (f = p.indexOf(`
358
402
 
359
403
  `)) !== -1; ) {
360
- let W = d.slice(0, y);
361
- d = d.slice(y + 2);
362
- let p = K(W);
363
- if (p && "done" in p) return;
364
- p && "content" in p && (yield p.content);
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 f = d.trim();
368
- if (f) {
369
- let m = K(f);
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
- g.releaseLock();
418
+ l.releaseLock();
375
419
  } catch {
376
420
  }
377
421
  try {
378
- await o.cancel();
422
+ await c.cancel();
379
423
  } catch {
380
424
  }
381
425
  }
382
426
  }
383
427
  return {
384
428
  meta: n,
385
- [Symbol.asyncIterator]: i
429
+ [Symbol.asyncIterator]: o
386
430
  };
387
431
  }
388
432
  };
389
433
  }
390
434
 
391
- export { ye as createAiNamespace };
435
+ export { ke as createAiNamespace };