@usagetap/sdk 0.4.0 → 0.6.0

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 (39) hide show
  1. package/README.md +26 -10
  2. package/dist/adapters/openai.d.cts +135 -2
  3. package/dist/adapters/openai.d.ts +135 -2
  4. package/dist/adapters/{openai.js → openai.mjs} +2 -2
  5. package/dist/adapters/openai.mjs.map +1 -0
  6. package/dist/adapters/openrouter.d.cts +2 -1
  7. package/dist/adapters/openrouter.d.ts +2 -1
  8. package/dist/adapters/{openrouter.js → openrouter.mjs} +2 -2
  9. package/dist/adapters/openrouter.mjs.map +1 -0
  10. package/dist/{openai-Ch7hN2vD.d.cts → client-DAY6cR0C.d.cts} +1 -134
  11. package/dist/{openai-Ch7hN2vD.d.ts → client-DAY6cR0C.d.ts} +1 -134
  12. package/dist/express/index.cjs +691 -0
  13. package/dist/express/index.cjs.map +1 -0
  14. package/dist/express/index.d.cts +93 -0
  15. package/dist/express/index.d.ts +93 -0
  16. package/dist/express/index.mjs +688 -0
  17. package/dist/express/index.mjs.map +1 -0
  18. package/dist/index.cjs +1 -778
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +3 -94
  21. package/dist/index.d.ts +3 -94
  22. package/dist/{index.js → index.mjs} +4 -773
  23. package/dist/index.mjs.map +1 -0
  24. package/dist/openai/index.cjs +775 -0
  25. package/dist/openai/index.cjs.map +1 -0
  26. package/dist/openai/index.d.cts +4 -0
  27. package/dist/openai/index.d.ts +4 -0
  28. package/dist/openai/index.mjs +768 -0
  29. package/dist/openai/index.mjs.map +1 -0
  30. package/dist/react/index.cjs.map +1 -1
  31. package/dist/react/index.d.cts +1 -1
  32. package/dist/react/index.d.ts +1 -1
  33. package/dist/react/{index.js → index.mjs} +2 -2
  34. package/dist/react/index.mjs.map +1 -0
  35. package/package.json +23 -1
  36. package/dist/adapters/openai.js.map +0 -1
  37. package/dist/adapters/openrouter.js.map +0 -1
  38. package/dist/index.js.map +0 -1
  39. package/dist/react/index.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- import { U as UsageTapClient, B as BeginCallRequest, W as WrapOpenAIContext, a as WrapOpenAIOptions, w as wrapOpenAI, p as pipeToResponse } from './openai-Ch7hN2vD.js';
2
- export { H as AllowedEntitlements, z as BalanceSummary, m as BeginCallOptions, n as BeginCallResponseBody, r as CheckUsageOptions, u as CheckUsageRequest, v as CheckUsageResponseBody, C as CreateCustomerOptions, o as CreateCustomerRequest, q as CreateCustomerResponseBody, E as EndCallOptions, x as EndCallRequest, y as EndCallResponseBody, A as EntitlementHints, a1 as IdempotencyMetadata, _ as LimitType, D as MeterSnapshot, M as MeterSummary, F as MeteredUsage, a0 as ModelHints, N as NodeResponseLike, O as OpenAIAdapter, b as OpenAIAdapterInit, d as OpenAIInvokeParams, e as OpenAIInvokeResult, f as OpenAIStreamParams, g as OpenAIStreamResult, P as PlanSummary, R as RemainingRatios, G as RequestedEntitlements, I as RetryOptions, S as StreamMode, l as StreamOpenAIRouteOptions, h as StreamToResponseOptions, $ as SubscriptionSnapshot, J as UsageTapClientOptions, L as UsageTapErrorResponse, X as UsageTapLogEntry, Q as UsageTapResultEnvelope, T as UsageTapResultStatus, K as UsageTapSuccessResponse, V as VendorHints, Y as WithUsageContext, Z as WithUsageOptions, i as WrapOpenAICallOptions, j as WrapOpenAIResponseCallOptions, k as WrappedOpenAI, c as createOpenAIAdapter, s as streamOpenAIRoute, t as toNextResponse } from './openai-Ch7hN2vD.js';
3
- export { createOpenRouterAdapter } from './adapters/openrouter.js';
4
- import OpenAI from 'openai';
5
- import { Request, Response, NextFunction } from 'express';
6
- export { Message, UseChatWithUsageOptions, UseChatWithUsageReturn } from './react/index.js';
1
+ import { U as UsageTapClient, B as BeginCallRequest } from './client-DAY6cR0C.js';
2
+ export { A as AllowedEntitlements, k as BalanceSummary, c as BeginCallOptions, b as BeginCallResponseBody, f as CheckUsageOptions, g as CheckUsageRequest, h as CheckUsageResponseBody, C as CreateCustomerOptions, d as CreateCustomerRequest, e as CreateCustomerResponseBody, i as EndCallOptions, E as EndCallRequest, j as EndCallResponseBody, l as EntitlementHints, I as IdempotencyMetadata, L as LimitType, m as MeterSnapshot, M as MeterSummary, n as MeteredUsage, w as ModelHints, P as PlanSummary, R as RemainingRatios, o as RequestedEntitlements, p as RetryOptions, S as SubscriptionSnapshot, q as UsageTapClientOptions, r as UsageTapErrorResponse, u as UsageTapLogEntry, s as UsageTapResultEnvelope, t as UsageTapResultStatus, a as UsageTapSuccessResponse, V as VendorHints, v as WithUsageContext, W as WithUsageOptions } from './client-DAY6cR0C.js';
7
3
 
8
4
  type UsageTapErrorCode = "USAGETAP_BROWSER_RUNTIME" | "USAGETAP_AUTH_ERROR" | "USAGETAP_BAD_REQUEST" | "USAGETAP_INVALID_RESPONSE" | "USAGETAP_NETWORK_ERROR" | "USAGETAP_RATE_LIMITED" | "USAGETAP_SERVER_ERROR" | "USAGETAP_RETRY_EXHAUSTED" | "USAGETAP_END_CALL_ERROR";
9
5
  interface UsageTapErrorInit {
@@ -67,93 +63,6 @@ interface WrapFetchOptions {
67
63
  */
68
64
  declare function wrapFetch(usageTap: UsageTapClient, options: WrapFetchOptions): typeof fetch;
69
65
 
70
- interface ExpressUsageTapContext {
71
- /**
72
- * Get a wrapped OpenAI client for this request.
73
- * The client automatically tracks usage with the provided context.
74
- */
75
- openai: (client: OpenAI, context?: Partial<WrapOpenAIContext>, options?: WrapOpenAIOptions) => ReturnType<typeof wrapOpenAI>;
76
- /**
77
- * Pipe an OpenAI stream directly to the Express response.
78
- * Automatically sets appropriate headers and finalizes usage.
79
- */
80
- pipeToResponse: typeof pipeToResponse;
81
- }
82
- declare global {
83
- namespace Express {
84
- interface Request {
85
- usageTap?: ExpressUsageTapContext;
86
- }
87
- }
88
- }
89
- interface WithUsageMiddlewareOptions {
90
- /**
91
- * Function to extract customer ID from the request.
92
- * Can read from headers, JWT tokens, session, etc.
93
- */
94
- getCustomerId: (req: Request) => string | Promise<string>;
95
- /**
96
- * Optional: Extract feature name from request
97
- */
98
- getFeature?: (req: Request) => string | undefined | Promise<string | undefined>;
99
- /**
100
- * Optional: Extract tags from request
101
- */
102
- getTags?: (req: Request) => string[] | undefined | Promise<string[] | undefined>;
103
- /**
104
- * Optional: Default context to merge with request-specific context
105
- */
106
- defaultContext?: Partial<Omit<WrapOpenAIContext, "customerId">>;
107
- }
108
- /**
109
- * Express middleware that adds UsageTap context to requests.
110
- *
111
- * @example
112
- * ```ts
113
- * const app = express();
114
- *
115
- * const usageTap = new UsageTapClient({
116
- * apiKey: process.env.USAGETAP_API_KEY!,
117
- * baseUrl: process.env.USAGETAP_BASE_URL!,
118
- * });
119
- *
120
- * app.use(withUsageMiddleware(usageTap, {
121
- * getCustomerId: (req) => req.user.id,
122
- * getFeature: (req) => req.path.split('/')[1],
123
- * }));
124
- *
125
- * app.post('/chat', async (req, res) => {
126
- * const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
127
- * const ai = req.usageTap!.openai(openai);
128
- *
129
- * const stream = await ai.chat.completions.create({
130
- * model: "gpt-4o-mini",
131
- * messages: req.body.messages,
132
- * stream: true,
133
- * });
134
- *
135
- * req.usageTap!.pipeToResponse(stream, res);
136
- * });
137
- * ```
138
- */
139
- declare function withUsageMiddleware(usageTap: UsageTapClient, options: WithUsageMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => Promise<void>;
140
- /**
141
- * Simpler middleware that only requires customer ID extraction.
142
- *
143
- * @example
144
- * ```ts
145
- * app.use(withUsage(usageTap, (req) => req.user.id));
146
- *
147
- * app.post('/chat', async (req, res) => {
148
- * const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
149
- * const ai = req.usageTap!.openai(openai, { feature: "chat" });
150
- * const completion = await ai.chat.completions.create({...});
151
- * res.json(completion);
152
- * });
153
- * ```
154
- */
155
- declare function withUsage(usageTap: UsageTapClient, getCustomerId: (req: Request) => string | Promise<string>): (req: Request, res: Response, next: NextFunction) => Promise<void>;
156
-
157
66
  declare function createIdempotencyKey(): string;
158
67
 
159
- export { BeginCallRequest, type ExpressUsageTapContext, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WithUsageMiddlewareOptions, type WrapFetchContext, type WrapFetchOptions, WrapOpenAIContext, WrapOpenAIOptions, createIdempotencyKey, isUsageTapError, pipeToResponse, withUsage, withUsageMiddleware, wrapFetch, wrapOpenAI };
68
+ export { BeginCallRequest, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WrapFetchContext, type WrapFetchOptions, createIdempotencyKey, isUsageTapError, wrapFetch };