@neetru/sdk 3.1.9 → 3.1.11
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/ai.cjs +304 -0
- package/dist/ai.cjs.map +1 -0
- package/dist/ai.d.cts +3 -0
- package/dist/ai.d.ts +3 -0
- package/dist/ai.mjs +302 -0
- package/dist/ai.mjs.map +1 -0
- package/dist/auth.cjs +283 -1
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +283 -1
- package/dist/auth.mjs.map +1 -1
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/index.cjs +286 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +285 -3
- package/dist/index.mjs.map +1 -1
- package/dist/mocks.cjs +52 -0
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.cts +20 -2
- package/dist/mocks.d.ts +20 -2
- package/dist/mocks.mjs +52 -1
- package/dist/mocks.mjs.map +1 -1
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/support.d.cts +1 -1
- package/dist/support.d.ts +1 -1
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/{types-DDiVzgCT.d.cts → types-Cog9S-tC.d.cts} +84 -1
- package/dist/{types-HWDOUmlC.d.ts → types-dagUxKYz.d.ts} +84 -1
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/package.json +6 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createNeetruClient } from './auth.cjs';
|
|
2
2
|
export { NeetruError, NeetruErrorCode } from './errors.cjs';
|
|
3
|
-
export { A as
|
|
4
|
-
export { DEV_FIXTURE_USER, MockAuth, MockAuthOptions, MockDb, MockEntitlements, MockSupport, MockUsage } from './mocks.cjs';
|
|
3
|
+
export { A as AiChatChoice, a as AiChatMessage, b as AiChatOptions, c as AiChatResult, d as AiChatUsage, e as AiNamespace, f as AiStreamHandle, g as AiStreamMeta, h as AuthNamespace, i as AuthStateListener, C as CatalogListOptions, j as CatalogListResponse, l as CheckoutIntentInfo, m as CheckoutIntentStatus, n as CheckoutNamespace, o as CheckoutStartInput, p as CheckoutStartResult, q as CheckoutTenantType, t as CreateTicketInput, D as DEFAULT_BASE_URL, u as DbBatchOp, v as DbChangeType, w as DbCollectionRef, x as DbDoc, y as DbDocRef, z as DbGetResult, B as DbListResult, E as DbNamespace, F as DbQuery, G as DbSqlLease, H as DbWhereFilter, J as EntitlementCheck, K as FieldSentinel, L as IncrementSentinel, M as ListNotificationsOptions, N as MockCheckout, O as MockNotifications, P as MockWebhooks, Q as NEETRU_SENTINEL_KEY, R as NeetruClient, S as NeetruClientConfig, T as NeetruDb, V as NeetruDbEngine, W as NeetruDbError, X as NeetruDbErrorCode, Y as NeetruDbOptions, Z as NeetruDbTransport, _ as NeetruEnv, $ as NeetruSqlClient, a0 as NeetruUser, a1 as NotificationScopeOptions, a2 as NotificationsNamespace, a3 as Product, a4 as ProductNotification, a5 as ProductNotificationSeverity, a6 as ProductPlan, a7 as ProductStatus, a9 as RedirectCallbackResult, aa as RegisterWebhookInput, ab as ResolvedConfig, ac as SendNotificationInput, ad as ServerTimestampSentinel, ae as SignInOptions, ag as SupportNamespace, ah as SupportSeverity, ai as SupportStatus, aj as SupportTicket, al as TelemetryEventAck, am as TelemetryEventInput, aq as UsageEventInput, ar as UsageNamespace, as as UsageQuota, at as VerifyTokenOptions, au as VerifyWebhookRequestOptions, av as VerifyWebhookRequestResult, aw as WebhookEndpoint, ax as WebhookEvent, ay as WebhookScopeOptions, az as WebhookTestResult, aA as WebhooksNamespace, aB as WithFieldSentinels, aC as createAiNamespace, aD as createCheckoutNamespace, aE as createNeetruDb, aF as createNotificationsNamespace, aH as createWebhooksNamespace, aJ as increment, aK as isFieldSentinel, aL as isIncrementSentinel, aM as isServerTimestampSentinel, aN as serverTimestamp, aO as verifyWebhookRequest, aP as verifyWebhookSignature } from './types-Cog9S-tC.cjs';
|
|
4
|
+
export { DEV_FIXTURE_USER, MockAi, MockAuth, MockAuthOptions, MockDb, MockEntitlements, MockSupport, MockUsage } from './mocks.cjs';
|
|
5
5
|
import 'drizzle-orm/node-postgres';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -42,6 +42,6 @@ declare function checkCoreVersionCompat(coreVersion: string): void;
|
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
/** Versão do SDK — exportada explicitamente pra detecção em runtime. */
|
|
45
|
-
declare const VERSION: "3.1.
|
|
45
|
+
declare const VERSION: "3.1.11";
|
|
46
46
|
|
|
47
47
|
export { VERSION, checkCoreVersionCompat };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createNeetruClient } from './auth.js';
|
|
2
2
|
export { NeetruError, NeetruErrorCode } from './errors.js';
|
|
3
|
-
export { A as
|
|
4
|
-
export { DEV_FIXTURE_USER, MockAuth, MockAuthOptions, MockDb, MockEntitlements, MockSupport, MockUsage } from './mocks.js';
|
|
3
|
+
export { A as AiChatChoice, a as AiChatMessage, b as AiChatOptions, c as AiChatResult, d as AiChatUsage, e as AiNamespace, f as AiStreamHandle, g as AiStreamMeta, h as AuthNamespace, i as AuthStateListener, C as CatalogListOptions, j as CatalogListResponse, l as CheckoutIntentInfo, m as CheckoutIntentStatus, n as CheckoutNamespace, o as CheckoutStartInput, p as CheckoutStartResult, q as CheckoutTenantType, t as CreateTicketInput, D as DEFAULT_BASE_URL, u as DbBatchOp, v as DbChangeType, w as DbCollectionRef, x as DbDoc, y as DbDocRef, z as DbGetResult, B as DbListResult, E as DbNamespace, F as DbQuery, G as DbSqlLease, H as DbWhereFilter, J as EntitlementCheck, K as FieldSentinel, L as IncrementSentinel, M as ListNotificationsOptions, N as MockCheckout, O as MockNotifications, P as MockWebhooks, Q as NEETRU_SENTINEL_KEY, R as NeetruClient, S as NeetruClientConfig, T as NeetruDb, V as NeetruDbEngine, W as NeetruDbError, X as NeetruDbErrorCode, Y as NeetruDbOptions, Z as NeetruDbTransport, _ as NeetruEnv, $ as NeetruSqlClient, a0 as NeetruUser, a1 as NotificationScopeOptions, a2 as NotificationsNamespace, a3 as Product, a4 as ProductNotification, a5 as ProductNotificationSeverity, a6 as ProductPlan, a7 as ProductStatus, a9 as RedirectCallbackResult, aa as RegisterWebhookInput, ab as ResolvedConfig, ac as SendNotificationInput, ad as ServerTimestampSentinel, ae as SignInOptions, ag as SupportNamespace, ah as SupportSeverity, ai as SupportStatus, aj as SupportTicket, al as TelemetryEventAck, am as TelemetryEventInput, aq as UsageEventInput, ar as UsageNamespace, as as UsageQuota, at as VerifyTokenOptions, au as VerifyWebhookRequestOptions, av as VerifyWebhookRequestResult, aw as WebhookEndpoint, ax as WebhookEvent, ay as WebhookScopeOptions, az as WebhookTestResult, aA as WebhooksNamespace, aB as WithFieldSentinels, aC as createAiNamespace, aD as createCheckoutNamespace, aE as createNeetruDb, aF as createNotificationsNamespace, aH as createWebhooksNamespace, aJ as increment, aK as isFieldSentinel, aL as isIncrementSentinel, aM as isServerTimestampSentinel, aN as serverTimestamp, aO as verifyWebhookRequest, aP as verifyWebhookSignature } from './types-dagUxKYz.js';
|
|
4
|
+
export { DEV_FIXTURE_USER, MockAi, MockAuth, MockAuthOptions, MockDb, MockEntitlements, MockSupport, MockUsage } from './mocks.js';
|
|
5
5
|
import 'drizzle-orm/node-postgres';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -42,6 +42,6 @@ declare function checkCoreVersionCompat(coreVersion: string): void;
|
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
/** Versão do SDK — exportada explicitamente pra detecção em runtime. */
|
|
45
|
-
declare const VERSION: "3.1.
|
|
45
|
+
declare const VERSION: "3.1.11";
|
|
46
46
|
|
|
47
47
|
export { VERSION, checkCoreVersionCompat };
|
package/dist/index.mjs
CHANGED
|
@@ -5594,6 +5594,235 @@ var MockNotifications = class {
|
|
|
5594
5594
|
}
|
|
5595
5595
|
};
|
|
5596
5596
|
|
|
5597
|
+
// src/ai.ts
|
|
5598
|
+
init_errors();
|
|
5599
|
+
init_http();
|
|
5600
|
+
var AI_CHAT_PATH = "/api/sdk/v1/ai/chat";
|
|
5601
|
+
var AI_NONSTREAM_TIMEOUT_MS = 12e4;
|
|
5602
|
+
function resolveAiBody(config, opts, stream) {
|
|
5603
|
+
if (!opts || typeof opts !== "object") {
|
|
5604
|
+
throw new NeetruError("validation_failed", "ai: options object is required");
|
|
5605
|
+
}
|
|
5606
|
+
if (typeof opts.model !== "string" || opts.model.length === 0) {
|
|
5607
|
+
throw new NeetruError("validation_failed", "ai: model is required");
|
|
5608
|
+
}
|
|
5609
|
+
if (!Array.isArray(opts.messages) || opts.messages.length === 0) {
|
|
5610
|
+
throw new NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
5611
|
+
}
|
|
5612
|
+
for (const m of opts.messages) {
|
|
5613
|
+
if (!m || typeof m !== "object" || typeof m.role !== "string" || m.role.length === 0 || typeof m.content !== "string") {
|
|
5614
|
+
throw new NeetruError(
|
|
5615
|
+
"validation_failed",
|
|
5616
|
+
"ai: each message must be { role: string, content: string }"
|
|
5617
|
+
);
|
|
5618
|
+
}
|
|
5619
|
+
}
|
|
5620
|
+
const productId = opts.productId ?? config.productId;
|
|
5621
|
+
const tenantId = opts.tenantId ?? config.tenantId;
|
|
5622
|
+
if (!productId) {
|
|
5623
|
+
throw new NeetruError(
|
|
5624
|
+
"validation_failed",
|
|
5625
|
+
"ai: productId required (pass to options or set on createNeetruClient)"
|
|
5626
|
+
);
|
|
5627
|
+
}
|
|
5628
|
+
if (!tenantId) {
|
|
5629
|
+
throw new NeetruError(
|
|
5630
|
+
"validation_failed",
|
|
5631
|
+
"ai: tenantId required (pass to options or set on createNeetruClient)"
|
|
5632
|
+
);
|
|
5633
|
+
}
|
|
5634
|
+
assertValidSdkId("productId", productId);
|
|
5635
|
+
assertValidSdkId("tenantId", tenantId);
|
|
5636
|
+
const body = {
|
|
5637
|
+
productId,
|
|
5638
|
+
tenantId,
|
|
5639
|
+
model: opts.model,
|
|
5640
|
+
messages: opts.messages,
|
|
5641
|
+
stream
|
|
5642
|
+
};
|
|
5643
|
+
if (opts.maxTokens !== void 0) {
|
|
5644
|
+
if (!Number.isInteger(opts.maxTokens) || opts.maxTokens <= 0) {
|
|
5645
|
+
throw new NeetruError("validation_failed", "ai: maxTokens must be a positive integer");
|
|
5646
|
+
}
|
|
5647
|
+
body.maxTokens = opts.maxTokens;
|
|
5648
|
+
}
|
|
5649
|
+
if (opts.temperature !== void 0) {
|
|
5650
|
+
if (typeof opts.temperature !== "number" || !Number.isFinite(opts.temperature)) {
|
|
5651
|
+
throw new NeetruError("validation_failed", "ai: temperature must be a finite number");
|
|
5652
|
+
}
|
|
5653
|
+
body.temperature = opts.temperature;
|
|
5654
|
+
}
|
|
5655
|
+
return body;
|
|
5656
|
+
}
|
|
5657
|
+
async function safeJson2(res) {
|
|
5658
|
+
try {
|
|
5659
|
+
const text = await res.text();
|
|
5660
|
+
if (!text) return void 0;
|
|
5661
|
+
return JSON.parse(text);
|
|
5662
|
+
} catch {
|
|
5663
|
+
return void 0;
|
|
5664
|
+
}
|
|
5665
|
+
}
|
|
5666
|
+
function statusToCode2(status) {
|
|
5667
|
+
if (status === 401) return "unauthorized";
|
|
5668
|
+
if (status === 403) return "forbidden";
|
|
5669
|
+
if (status === 404) return "not_found";
|
|
5670
|
+
if (status === 409) return "conflict";
|
|
5671
|
+
if (status === 422 || status === 400) return "validation_failed";
|
|
5672
|
+
if (status === 429) return "rate_limited";
|
|
5673
|
+
if (status >= 500) return "server_error";
|
|
5674
|
+
return "unknown";
|
|
5675
|
+
}
|
|
5676
|
+
function joinUrl(baseUrl, path) {
|
|
5677
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
5678
|
+
const p = path.startsWith("/") ? path : `/${path}`;
|
|
5679
|
+
return `${base}${p}`;
|
|
5680
|
+
}
|
|
5681
|
+
async function aiFetch(config, body, signal, stream) {
|
|
5682
|
+
if (!config.apiKey) {
|
|
5683
|
+
throw new NeetruError(
|
|
5684
|
+
"missing_api_key",
|
|
5685
|
+
"ai requires an apiKey. Pass it to createNeetruClient({ apiKey }) or set NEETRU_API_KEY env var."
|
|
5686
|
+
);
|
|
5687
|
+
}
|
|
5688
|
+
const url = joinUrl(config.baseUrl, AI_CHAT_PATH);
|
|
5689
|
+
const headers = {
|
|
5690
|
+
"content-type": "application/json",
|
|
5691
|
+
accept: stream ? "text/event-stream" : "application/json",
|
|
5692
|
+
authorization: `Bearer ${config.apiKey}`
|
|
5693
|
+
};
|
|
5694
|
+
const init = { method: "POST", headers, body: JSON.stringify(body) };
|
|
5695
|
+
if (stream) {
|
|
5696
|
+
if (signal) init.signal = signal;
|
|
5697
|
+
} else {
|
|
5698
|
+
init.signal = signal ?? AbortSignal.timeout(AI_NONSTREAM_TIMEOUT_MS);
|
|
5699
|
+
}
|
|
5700
|
+
let res;
|
|
5701
|
+
try {
|
|
5702
|
+
res = await config.fetch(url, init);
|
|
5703
|
+
} catch (err) {
|
|
5704
|
+
const message = err instanceof DOMException && err.name === "TimeoutError" ? `Network error: timeout after ${AI_NONSTREAM_TIMEOUT_MS / 1e3}s` : err instanceof DOMException && err.name === "AbortError" ? "Network error: request aborted" : `Network error: ${err instanceof Error ? err.message : "fetch failed"}`;
|
|
5705
|
+
throw new NeetruError("network_error", message);
|
|
5706
|
+
}
|
|
5707
|
+
const requestId = res.headers.get("x-request-id") ?? res.headers.get("x-correlation-id") ?? void 0;
|
|
5708
|
+
if (!res.ok) {
|
|
5709
|
+
const parsed = await safeJson2(res);
|
|
5710
|
+
let code = statusToCode2(res.status);
|
|
5711
|
+
let message = `HTTP ${res.status}`;
|
|
5712
|
+
if (parsed && typeof parsed === "object" && "error" in parsed) {
|
|
5713
|
+
const errField = parsed.error;
|
|
5714
|
+
if (typeof errField === "string") {
|
|
5715
|
+
message = errField;
|
|
5716
|
+
} else if (errField && typeof errField === "object") {
|
|
5717
|
+
if (typeof errField.code === "string") code = errField.code;
|
|
5718
|
+
if (typeof errField.message === "string") message = errField.message;
|
|
5719
|
+
}
|
|
5720
|
+
}
|
|
5721
|
+
throw new NeetruError(code, message, res.status, requestId);
|
|
5722
|
+
}
|
|
5723
|
+
const coreVersion = res.headers.get("x-neetru-version");
|
|
5724
|
+
if (coreVersion) checkCoreVersionCompat(coreVersion);
|
|
5725
|
+
return res;
|
|
5726
|
+
}
|
|
5727
|
+
function readMeta(res) {
|
|
5728
|
+
return {
|
|
5729
|
+
provider: res.headers.get("x-neetru-provider") ?? "",
|
|
5730
|
+
usageWarning: res.headers.get("x-neetru-ai-usage-warning") === "1"
|
|
5731
|
+
};
|
|
5732
|
+
}
|
|
5733
|
+
function parseSseData(rawEvent) {
|
|
5734
|
+
const dataLines = [];
|
|
5735
|
+
for (const line of rawEvent.split("\n")) {
|
|
5736
|
+
const trimmed = line.replace(/\r$/, "");
|
|
5737
|
+
if (trimmed.startsWith("data:")) {
|
|
5738
|
+
let v = trimmed.slice(5);
|
|
5739
|
+
if (v.startsWith(" ")) v = v.slice(1);
|
|
5740
|
+
dataLines.push(v);
|
|
5741
|
+
}
|
|
5742
|
+
}
|
|
5743
|
+
if (dataLines.length === 0) return null;
|
|
5744
|
+
const payload = dataLines.join("\n");
|
|
5745
|
+
if (payload === "[DONE]") return { done: true };
|
|
5746
|
+
try {
|
|
5747
|
+
const obj = JSON.parse(payload);
|
|
5748
|
+
const content = obj?.choices?.[0]?.delta?.content;
|
|
5749
|
+
if (typeof content === "string" && content.length > 0) return { content };
|
|
5750
|
+
return null;
|
|
5751
|
+
} catch {
|
|
5752
|
+
return null;
|
|
5753
|
+
}
|
|
5754
|
+
}
|
|
5755
|
+
function createAiNamespace(config) {
|
|
5756
|
+
return {
|
|
5757
|
+
async chat(opts) {
|
|
5758
|
+
const body = resolveAiBody(config, opts, false);
|
|
5759
|
+
const res = await aiFetch(config, body, opts.signal, false);
|
|
5760
|
+
const { provider: providerHeader, usageWarning } = readMeta(res);
|
|
5761
|
+
const raw = await safeJson2(res);
|
|
5762
|
+
if (!raw || raw.ok !== true || !Array.isArray(raw.choices)) {
|
|
5763
|
+
throw new NeetruError("invalid_response", "ai.chat response missing ok/choices");
|
|
5764
|
+
}
|
|
5765
|
+
return {
|
|
5766
|
+
choices: raw.choices,
|
|
5767
|
+
usage: raw.usage ?? { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
5768
|
+
model: raw.model ?? opts.model,
|
|
5769
|
+
provider: raw.provider ?? providerHeader ?? "",
|
|
5770
|
+
usageWarning
|
|
5771
|
+
};
|
|
5772
|
+
},
|
|
5773
|
+
stream(opts) {
|
|
5774
|
+
const body = resolveAiBody(config, opts, true);
|
|
5775
|
+
const responsePromise = aiFetch(config, body, opts.signal, true);
|
|
5776
|
+
const meta = responsePromise.then(readMeta);
|
|
5777
|
+
meta.catch(() => {
|
|
5778
|
+
});
|
|
5779
|
+
async function* iterate() {
|
|
5780
|
+
const res = await responsePromise;
|
|
5781
|
+
const stream = res.body;
|
|
5782
|
+
if (!stream) {
|
|
5783
|
+
throw new NeetruError("invalid_response", "ai.stream: response has no body");
|
|
5784
|
+
}
|
|
5785
|
+
const reader = stream.getReader();
|
|
5786
|
+
const decoder = new TextDecoder();
|
|
5787
|
+
let buffer = "";
|
|
5788
|
+
try {
|
|
5789
|
+
while (true) {
|
|
5790
|
+
const { done, value } = await reader.read();
|
|
5791
|
+
if (done) break;
|
|
5792
|
+
buffer += decoder.decode(value, { stream: true });
|
|
5793
|
+
let sep;
|
|
5794
|
+
while ((sep = buffer.indexOf("\n\n")) !== -1) {
|
|
5795
|
+
const rawEvent = buffer.slice(0, sep);
|
|
5796
|
+
buffer = buffer.slice(sep + 2);
|
|
5797
|
+
const parsed = parseSseData(rawEvent);
|
|
5798
|
+
if (parsed && "done" in parsed) return;
|
|
5799
|
+
if (parsed && "content" in parsed) yield parsed.content;
|
|
5800
|
+
}
|
|
5801
|
+
}
|
|
5802
|
+
const tail = buffer.trim();
|
|
5803
|
+
if (tail) {
|
|
5804
|
+
const parsed = parseSseData(tail);
|
|
5805
|
+
if (parsed && "content" in parsed) yield parsed.content;
|
|
5806
|
+
}
|
|
5807
|
+
} finally {
|
|
5808
|
+
try {
|
|
5809
|
+
reader.releaseLock();
|
|
5810
|
+
} catch {
|
|
5811
|
+
}
|
|
5812
|
+
try {
|
|
5813
|
+
await stream.cancel();
|
|
5814
|
+
} catch {
|
|
5815
|
+
}
|
|
5816
|
+
}
|
|
5817
|
+
}
|
|
5818
|
+
return {
|
|
5819
|
+
meta,
|
|
5820
|
+
[Symbol.asyncIterator]: iterate
|
|
5821
|
+
};
|
|
5822
|
+
}
|
|
5823
|
+
};
|
|
5824
|
+
}
|
|
5825
|
+
|
|
5597
5826
|
// src/mocks.ts
|
|
5598
5827
|
init_db_errors();
|
|
5599
5828
|
init_errors();
|
|
@@ -6194,6 +6423,57 @@ var MockDb = class {
|
|
|
6194
6423
|
this._store.clear();
|
|
6195
6424
|
}
|
|
6196
6425
|
};
|
|
6426
|
+
var MockAi = class {
|
|
6427
|
+
_calls = [];
|
|
6428
|
+
_reply(opts) {
|
|
6429
|
+
const last = [...opts.messages].reverse().find((m) => m.role === "user") ?? opts.messages[opts.messages.length - 1];
|
|
6430
|
+
const content = last?.content ?? "";
|
|
6431
|
+
return content ? `mock-reply: ${content}` : "mock-reply";
|
|
6432
|
+
}
|
|
6433
|
+
async chat(opts) {
|
|
6434
|
+
if (!opts || typeof opts.model !== "string" || opts.model.length === 0) {
|
|
6435
|
+
throw new NeetruError("validation_failed", "ai: model is required");
|
|
6436
|
+
}
|
|
6437
|
+
if (!Array.isArray(opts.messages) || opts.messages.length === 0) {
|
|
6438
|
+
throw new NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
6439
|
+
}
|
|
6440
|
+
this._calls.push({ model: opts.model, messages: opts.messages, stream: false });
|
|
6441
|
+
const text = this._reply(opts);
|
|
6442
|
+
const promptTokens = opts.messages.reduce((n, m) => n + (m.content?.length ?? 0), 0);
|
|
6443
|
+
const completionTokens = text.length;
|
|
6444
|
+
return {
|
|
6445
|
+
choices: [{ message: { role: "assistant", content: text }, finish_reason: "stop" }],
|
|
6446
|
+
usage: {
|
|
6447
|
+
prompt_tokens: promptTokens,
|
|
6448
|
+
completion_tokens: completionTokens,
|
|
6449
|
+
total_tokens: promptTokens + completionTokens
|
|
6450
|
+
},
|
|
6451
|
+
model: opts.model,
|
|
6452
|
+
provider: "mock",
|
|
6453
|
+
usageWarning: false
|
|
6454
|
+
};
|
|
6455
|
+
}
|
|
6456
|
+
stream(opts) {
|
|
6457
|
+
if (!opts || typeof opts.model !== "string" || opts.model.length === 0) {
|
|
6458
|
+
throw new NeetruError("validation_failed", "ai: model is required");
|
|
6459
|
+
}
|
|
6460
|
+
if (!Array.isArray(opts.messages) || opts.messages.length === 0) {
|
|
6461
|
+
throw new NeetruError("validation_failed", "ai: messages must be a non-empty array");
|
|
6462
|
+
}
|
|
6463
|
+
this._calls.push({ model: opts.model, messages: opts.messages, stream: true });
|
|
6464
|
+
const text = this._reply(opts);
|
|
6465
|
+
const parts = text.match(/\S+\s*/g) ?? [text];
|
|
6466
|
+
const meta = Promise.resolve({ provider: "mock", usageWarning: false });
|
|
6467
|
+
async function* iterate() {
|
|
6468
|
+
for (const p of parts) yield p;
|
|
6469
|
+
}
|
|
6470
|
+
return { meta, [Symbol.asyncIterator]: iterate };
|
|
6471
|
+
}
|
|
6472
|
+
/** Test helper — histórico de chamadas. */
|
|
6473
|
+
__getCalls() {
|
|
6474
|
+
return this._calls;
|
|
6475
|
+
}
|
|
6476
|
+
};
|
|
6197
6477
|
|
|
6198
6478
|
// src/auth.ts
|
|
6199
6479
|
function readEnvApiKey() {
|
|
@@ -6632,6 +6912,7 @@ function createNeetruClient(config = {}) {
|
|
|
6632
6912
|
const db = config.mocks?.db ?? createNeetruDb(resolved, config.db);
|
|
6633
6913
|
const webhooks = config.mocks?.webhooks ?? (isDev ? new MockWebhooks() : createWebhooksNamespace(resolved));
|
|
6634
6914
|
const notifications = config.mocks?.notifications ?? (isDev ? new MockNotifications() : createNotificationsNamespace(resolved));
|
|
6915
|
+
const ai = config.mocks?.ai ?? (isDev ? new MockAi() : createAiNamespace(resolved));
|
|
6635
6916
|
const client = Object.freeze({
|
|
6636
6917
|
config: resolved,
|
|
6637
6918
|
auth,
|
|
@@ -6643,7 +6924,8 @@ function createNeetruClient(config = {}) {
|
|
|
6643
6924
|
db,
|
|
6644
6925
|
checkout: createCheckoutNamespace(resolved),
|
|
6645
6926
|
webhooks,
|
|
6646
|
-
notifications
|
|
6927
|
+
notifications,
|
|
6928
|
+
ai
|
|
6647
6929
|
});
|
|
6648
6930
|
return client;
|
|
6649
6931
|
}
|
|
@@ -6652,8 +6934,8 @@ function createNeetruClient(config = {}) {
|
|
|
6652
6934
|
init_errors();
|
|
6653
6935
|
init_db_errors();
|
|
6654
6936
|
init_http();
|
|
6655
|
-
var VERSION = "3.1.
|
|
6937
|
+
var VERSION = "3.1.11";
|
|
6656
6938
|
|
|
6657
|
-
export { DEFAULT_BASE_URL, DEV_FIXTURE_USER, MockAuth, MockCheckout, MockDb, MockEntitlements, MockNotifications, MockSupport, MockUsage, MockWebhooks, NEETRU_SENTINEL_KEY, NeetruDbError, NeetruError, VERSION, checkCoreVersionCompat, createCheckoutNamespace, createNeetruClient, createNeetruDb, createNotificationsNamespace, createWebhooksNamespace, increment, isFieldSentinel, isIncrementSentinel, isServerTimestampSentinel, serverTimestamp, verifyWebhookRequest, verifyWebhookSignature };
|
|
6939
|
+
export { DEFAULT_BASE_URL, DEV_FIXTURE_USER, MockAi, MockAuth, MockCheckout, MockDb, MockEntitlements, MockNotifications, MockSupport, MockUsage, MockWebhooks, NEETRU_SENTINEL_KEY, NeetruDbError, NeetruError, VERSION, checkCoreVersionCompat, createAiNamespace, createCheckoutNamespace, createNeetruClient, createNeetruDb, createNotificationsNamespace, createWebhooksNamespace, increment, isFieldSentinel, isIncrementSentinel, isServerTimestampSentinel, serverTimestamp, verifyWebhookRequest, verifyWebhookSignature };
|
|
6658
6940
|
//# sourceMappingURL=index.mjs.map
|
|
6659
6941
|
//# sourceMappingURL=index.mjs.map
|