vibezcheck 0.5.5 → 0.5.6

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/bin/examples.js CHANGED
File without changes
package/bin/vibezcheck.js CHANGED
File without changes
@@ -4,6 +4,45 @@ import { C as CustomerParam } from '../types-Btf2OhIr.mjs';
4
4
  import { V as VibezMeter } from '../client-BzwEHQlC.mjs';
5
5
  import 'stripe';
6
6
 
7
+ /**
8
+ * ✦ VibezCheck Universal Telemetry Response Stream Injector
9
+ *
10
+ * Transmits real-time verified token counts, exact dollar costs, profit margins,
11
+ * and model telemetry from the server into AI SDK useChat() across all versions:
12
+ * - AI SDK v5/v6/v7 (UI Message Stream): Injects `data-vibezcheck` and `message-metadata` SSE events
13
+ * - AI SDK v4 (Data Stream Protocol): Injects `2:[{"type":"vibezcheck", ...}]` message annotation lines
14
+ * - Custom / Text Streams: Injects clean telemetry without breaking stream parsers
15
+ */
16
+ interface ToResponseOptions {
17
+ /** Custom ResponseInit (status, statusText, headers) */
18
+ init?: ResponseInit;
19
+ /** Additional HTTP headers */
20
+ headers?: HeadersInit;
21
+ /** Explicit telemetry payload override */
22
+ telemetry?: any;
23
+ }
24
+ /**
25
+ * Transforms an AI SDK stream result (or standard Response) into a client-ready HTTP Response
26
+ * that transmits both AI completion chunks and verified VibezCheck telemetry.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { streamText } from 'ai';
31
+ * import { openai } from '@ai-sdk/openai';
32
+ * import { vibezcheck } from 'vibezcheck';
33
+ *
34
+ * export async function POST(req: Request) {
35
+ * const { messages } = await req.json();
36
+ * const result = streamText({
37
+ * model: vibezcheck(openai('gpt-4o'), { customer: 'cus_123' }),
38
+ * messages,
39
+ * });
40
+ * return vibezcheck.toResponse(result);
41
+ * }
42
+ * ```
43
+ */
44
+ declare function toResponse(result: any, options?: ToResponseOptions): Response;
45
+
7
46
  interface VibezCheckModelOptions extends WithBillingOptions {
8
47
  /** OpenAI / Anthropic / AI Gateway API Key override */
9
48
  apiKey?: string;
@@ -69,6 +108,7 @@ declare function createVibezModel(modelOrId: any, options?: VibezCheckModelOptio
69
108
  declare function createVibezSession(sessionOptions?: VibezSessionOptions): VibezSession;
70
109
  declare const vibezcheck: typeof createVibezModel & {
71
110
  session: typeof createVibezSession;
111
+ toResponse: typeof toResponse;
72
112
  };
73
113
 
74
- export { type VibezCheckModelOptions, type VibezSession, type VibezSessionOptions, WithBillingOptions, createVibezModel, createVibezSession, vibezcheck };
114
+ export { type ToResponseOptions, type VibezCheckModelOptions, type VibezSession, type VibezSessionOptions, WithBillingOptions, createVibezModel, createVibezSession, toResponse, vibezcheck };
@@ -4,6 +4,45 @@ import { C as CustomerParam } from '../types-Btf2OhIr.js';
4
4
  import { V as VibezMeter } from '../client-CbO6vLUe.js';
5
5
  import 'stripe';
6
6
 
7
+ /**
8
+ * ✦ VibezCheck Universal Telemetry Response Stream Injector
9
+ *
10
+ * Transmits real-time verified token counts, exact dollar costs, profit margins,
11
+ * and model telemetry from the server into AI SDK useChat() across all versions:
12
+ * - AI SDK v5/v6/v7 (UI Message Stream): Injects `data-vibezcheck` and `message-metadata` SSE events
13
+ * - AI SDK v4 (Data Stream Protocol): Injects `2:[{"type":"vibezcheck", ...}]` message annotation lines
14
+ * - Custom / Text Streams: Injects clean telemetry without breaking stream parsers
15
+ */
16
+ interface ToResponseOptions {
17
+ /** Custom ResponseInit (status, statusText, headers) */
18
+ init?: ResponseInit;
19
+ /** Additional HTTP headers */
20
+ headers?: HeadersInit;
21
+ /** Explicit telemetry payload override */
22
+ telemetry?: any;
23
+ }
24
+ /**
25
+ * Transforms an AI SDK stream result (or standard Response) into a client-ready HTTP Response
26
+ * that transmits both AI completion chunks and verified VibezCheck telemetry.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { streamText } from 'ai';
31
+ * import { openai } from '@ai-sdk/openai';
32
+ * import { vibezcheck } from 'vibezcheck';
33
+ *
34
+ * export async function POST(req: Request) {
35
+ * const { messages } = await req.json();
36
+ * const result = streamText({
37
+ * model: vibezcheck(openai('gpt-4o'), { customer: 'cus_123' }),
38
+ * messages,
39
+ * });
40
+ * return vibezcheck.toResponse(result);
41
+ * }
42
+ * ```
43
+ */
44
+ declare function toResponse(result: any, options?: ToResponseOptions): Response;
45
+
7
46
  interface VibezCheckModelOptions extends WithBillingOptions {
8
47
  /** OpenAI / Anthropic / AI Gateway API Key override */
9
48
  apiKey?: string;
@@ -69,6 +108,7 @@ declare function createVibezModel(modelOrId: any, options?: VibezCheckModelOptio
69
108
  declare function createVibezSession(sessionOptions?: VibezSessionOptions): VibezSession;
70
109
  declare const vibezcheck: typeof createVibezModel & {
71
110
  session: typeof createVibezSession;
111
+ toResponse: typeof toResponse;
72
112
  };
73
113
 
74
- export { type VibezCheckModelOptions, type VibezSession, type VibezSessionOptions, WithBillingOptions, createVibezModel, createVibezSession, vibezcheck };
114
+ export { type ToResponseOptions, type VibezCheckModelOptions, type VibezSession, type VibezSessionOptions, WithBillingOptions, createVibezModel, createVibezSession, toResponse, vibezcheck };
@@ -33,6 +33,7 @@ __export(ai_sdk_exports, {
33
33
  createVibezModel: () => createVibezModel,
34
34
  createVibezSession: () => createVibezSession,
35
35
  meteredModel: () => meteredModel,
36
+ toResponse: () => toResponse,
36
37
  vibezcheck: () => vibezcheck,
37
38
  withBilling: () => withBilling
38
39
  });
@@ -1417,6 +1418,7 @@ function withBilling(model, optionsOrCustomer, extraOptions = {}) {
1417
1418
  }
1418
1419
  }
1419
1420
  scheduleFlush();
1421
+ return event;
1420
1422
  };
1421
1423
  const handler = {
1422
1424
  get(target, prop, receiver) {
@@ -1479,7 +1481,22 @@ function withBilling(model, optionsOrCustomer, extraOptions = {}) {
1479
1481
  const chunkUsage = (chunk.type === "finish" ? chunk.usage : null) || chunk.usage || chunk.token_usage || chunk.usageMetadata;
1480
1482
  if (chunkUsage) {
1481
1483
  streamCompleted = true;
1482
- handleUsage(chunkUsage);
1484
+ const event = handleUsage(chunkUsage);
1485
+ if (chunk.type === "finish" && event) {
1486
+ chunk.providerMetadata = {
1487
+ ...chunk.providerMetadata,
1488
+ vibezcheck: {
1489
+ type: "vibezcheck",
1490
+ cost: event.cost,
1491
+ usage: event.usage,
1492
+ model: modelId,
1493
+ provider,
1494
+ customerId,
1495
+ customerEmail,
1496
+ timestamp: event.timestamp
1497
+ }
1498
+ };
1499
+ }
1483
1500
  }
1484
1501
  },
1485
1502
  flush() {
@@ -1534,6 +1551,142 @@ function withBilling(model, optionsOrCustomer, extraOptions = {}) {
1534
1551
  }
1535
1552
  var meteredModel = withBilling;
1536
1553
 
1554
+ // src/ai-sdk/to-response.ts
1555
+ function toResponse(result, options) {
1556
+ let baseResponse;
1557
+ if (typeof result?.toUIMessageStreamResponse === "function") {
1558
+ baseResponse = result.toUIMessageStreamResponse(options?.init);
1559
+ } else if (typeof result?.toDataStreamResponse === "function") {
1560
+ baseResponse = result.toDataStreamResponse(options?.init);
1561
+ } else if (typeof result?.toTextStreamResponse === "function") {
1562
+ baseResponse = result.toTextStreamResponse(options?.init);
1563
+ } else if (result instanceof Response) {
1564
+ baseResponse = result;
1565
+ } else if (result && typeof result === "object" && result.body && result.headers) {
1566
+ baseResponse = result;
1567
+ } else {
1568
+ baseResponse = new Response(result, options?.init);
1569
+ }
1570
+ if (!baseResponse.body) {
1571
+ return baseResponse;
1572
+ }
1573
+ const contentType = baseResponse.headers.get("content-type") || "";
1574
+ const isSSE = contentType.includes("text/event-stream") || baseResponse.headers.has("x-vercel-ai-ui-message-stream");
1575
+ const isDataStream = baseResponse.headers.has("x-vercel-ai-data-stream");
1576
+ const encoder = new TextEncoder();
1577
+ const decoder = new TextDecoder();
1578
+ let injected = false;
1579
+ const resolveTelemetry = async () => {
1580
+ if (options?.telemetry) return options.telemetry;
1581
+ if (result?.providerMetadata) {
1582
+ try {
1583
+ const meta = await Promise.race([
1584
+ result.providerMetadata,
1585
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 800))
1586
+ ]);
1587
+ if (meta?.vibezcheck) {
1588
+ return meta.vibezcheck;
1589
+ }
1590
+ } catch {
1591
+ }
1592
+ }
1593
+ if (result?.usage) {
1594
+ try {
1595
+ const u = await Promise.race([
1596
+ result.usage,
1597
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 800))
1598
+ ]);
1599
+ if (u) {
1600
+ const inputTokens = u.promptTokens ?? u.inputTokens ?? 0;
1601
+ const outputTokens = u.completionTokens ?? u.outputTokens ?? 0;
1602
+ const reasoningTokens = u.reasoningTokens ?? u.completionTokensDetails?.reasoningTokens;
1603
+ return {
1604
+ type: "vibezcheck",
1605
+ usage: {
1606
+ inputTokens,
1607
+ outputTokens,
1608
+ totalTokens: inputTokens + outputTokens,
1609
+ reasoningTokens
1610
+ }
1611
+ };
1612
+ }
1613
+ } catch {
1614
+ }
1615
+ }
1616
+ return null;
1617
+ };
1618
+ const transformStream = new TransformStream({
1619
+ async transform(chunk, controller) {
1620
+ const text = decoder.decode(chunk, { stream: true });
1621
+ if (isSSE && text.includes("[DONE]") && !injected) {
1622
+ injected = true;
1623
+ const telemetry = await resolveTelemetry();
1624
+ if (telemetry) {
1625
+ const ssePayload = `data: ${JSON.stringify({ type: "data-vibezcheck", data: telemetry })}
1626
+
1627
+ data: ${JSON.stringify({ type: "message-metadata", messageMetadata: { vibezcheck: telemetry } })}
1628
+
1629
+ `;
1630
+ const doneIndex = text.indexOf("data: [DONE]");
1631
+ if (doneIndex !== -1) {
1632
+ const before = text.slice(0, doneIndex);
1633
+ const after = text.slice(doneIndex);
1634
+ if (before) controller.enqueue(encoder.encode(before));
1635
+ controller.enqueue(encoder.encode(ssePayload));
1636
+ controller.enqueue(encoder.encode(after));
1637
+ return;
1638
+ } else {
1639
+ controller.enqueue(encoder.encode(ssePayload));
1640
+ }
1641
+ }
1642
+ } else if (isDataStream && (text.includes("d:{") || text.includes("d: {")) && !injected) {
1643
+ injected = true;
1644
+ const telemetry = await resolveTelemetry();
1645
+ if (telemetry) {
1646
+ const annotationLine = `2:[${JSON.stringify(telemetry)}]
1647
+ `;
1648
+ controller.enqueue(encoder.encode(annotationLine));
1649
+ }
1650
+ }
1651
+ controller.enqueue(chunk);
1652
+ },
1653
+ async flush(controller) {
1654
+ if (!injected) {
1655
+ injected = true;
1656
+ const telemetry = await resolveTelemetry();
1657
+ if (telemetry) {
1658
+ if (isSSE) {
1659
+ const ssePayload = `data: ${JSON.stringify({ type: "data-vibezcheck", data: telemetry })}
1660
+
1661
+ data: ${JSON.stringify({ type: "message-metadata", messageMetadata: { vibezcheck: telemetry } })}
1662
+
1663
+ `;
1664
+ controller.enqueue(encoder.encode(ssePayload));
1665
+ } else {
1666
+ const annotationLine = `2:[${JSON.stringify(telemetry)}]
1667
+ `;
1668
+ controller.enqueue(encoder.encode(annotationLine));
1669
+ }
1670
+ }
1671
+ }
1672
+ }
1673
+ });
1674
+ const mergedHeaders = new Headers(baseResponse.headers);
1675
+ if (options?.headers) {
1676
+ const extraHeaders = new Headers(options.headers);
1677
+ extraHeaders.forEach((value, key) => {
1678
+ mergedHeaders.set(key, value);
1679
+ });
1680
+ }
1681
+ const status = options?.init?.status ?? baseResponse.status;
1682
+ const statusText = options?.init?.statusText ?? baseResponse.statusText;
1683
+ return new Response(baseResponse.body.pipeThrough(transformStream), {
1684
+ status,
1685
+ statusText,
1686
+ headers: mergedHeaders
1687
+ });
1688
+ }
1689
+
1537
1690
  // src/ai-sdk/declarative.ts
1538
1691
  function createVibezModel(modelOrId, options = {}) {
1539
1692
  if (typeof modelOrId === "object" && modelOrId !== null) {
@@ -1708,13 +1861,15 @@ function createVibezSession(sessionOptions = {}) {
1708
1861
  };
1709
1862
  }
1710
1863
  var vibezcheck = Object.assign(createVibezModel, {
1711
- session: createVibezSession
1864
+ session: createVibezSession,
1865
+ toResponse
1712
1866
  });
1713
1867
  // Annotate the CommonJS export names for ESM import in node:
1714
1868
  0 && (module.exports = {
1715
1869
  createVibezModel,
1716
1870
  createVibezSession,
1717
1871
  meteredModel,
1872
+ toResponse,
1718
1873
  vibezcheck,
1719
1874
  withBilling
1720
1875
  });