@usagetap/sdk 0.1.0 → 0.2.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.
- package/README.md +17 -1
- package/dist/adapters/openai.d.cts +1 -1
- package/dist/adapters/openai.d.ts +1 -1
- package/dist/adapters/openrouter.d.cts +1 -1
- package/dist/adapters/openrouter.d.ts +1 -1
- package/dist/index.cjs +75 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +75 -1
- package/dist/index.js.map +1 -1
- package/dist/{openai-CKyw08rB.d.cts → openai-CeptbEGH.d.cts} +23 -1
- package/dist/{openai-CKyw08rB.d.ts → openai-CeptbEGH.d.ts} +23 -1
- package/package.json +6 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { U as UsageTapClient, B as BeginCallRequest, W as WrapOpenAIContext, a as WrapOpenAIOptions, w as wrapOpenAI, p as pipeToResponse } from './openai-
|
|
2
|
-
export {
|
|
1
|
+
import { U as UsageTapClient, B as BeginCallRequest, W as WrapOpenAIContext, a as WrapOpenAIOptions, w as wrapOpenAI, p as pipeToResponse } from './openai-CeptbEGH.cjs';
|
|
2
|
+
export { D as AllowedEntitlements, v as BalanceSummary, m as BeginCallOptions, n as BeginCallResponseBody, C as CheckUsageOptions, o as CheckUsageRequest, q as CheckUsageResponseBody, E as EndCallOptions, r as EndCallRequest, u as EndCallResponseBody, x as EntitlementHints, _ as IdempotencyMetadata, X as LimitType, y as MeterSnapshot, M as MeterSummary, z as MeteredUsage, Z 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, A as RequestedEntitlements, F as RetryOptions, S as StreamMode, l as StreamOpenAIRouteOptions, h as StreamToResponseOptions, Y as SubscriptionSnapshot, G as UsageTapClientOptions, I as UsageTapErrorResponse, L as UsageTapLogEntry, J as UsageTapResultEnvelope, K as UsageTapResultStatus, H as UsageTapSuccessResponse, V as VendorHints, Q as WithUsageContext, T as WithUsageOptions, i as WrapOpenAICallOptions, j as WrapOpenAIResponseCallOptions, k as WrappedOpenAI, c as createOpenAIAdapter, s as streamOpenAIRoute, t as toNextResponse } from './openai-CeptbEGH.cjs';
|
|
3
3
|
export { createOpenRouterAdapter } from './adapters/openrouter.cjs';
|
|
4
4
|
import OpenAI from 'openai';
|
|
5
5
|
import { Request, Response, NextFunction } from 'express';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { U as UsageTapClient, B as BeginCallRequest, W as WrapOpenAIContext, a as WrapOpenAIOptions, w as wrapOpenAI, p as pipeToResponse } from './openai-
|
|
2
|
-
export {
|
|
1
|
+
import { U as UsageTapClient, B as BeginCallRequest, W as WrapOpenAIContext, a as WrapOpenAIOptions, w as wrapOpenAI, p as pipeToResponse } from './openai-CeptbEGH.js';
|
|
2
|
+
export { D as AllowedEntitlements, v as BalanceSummary, m as BeginCallOptions, n as BeginCallResponseBody, C as CheckUsageOptions, o as CheckUsageRequest, q as CheckUsageResponseBody, E as EndCallOptions, r as EndCallRequest, u as EndCallResponseBody, x as EntitlementHints, _ as IdempotencyMetadata, X as LimitType, y as MeterSnapshot, M as MeterSummary, z as MeteredUsage, Z 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, A as RequestedEntitlements, F as RetryOptions, S as StreamMode, l as StreamOpenAIRouteOptions, h as StreamToResponseOptions, Y as SubscriptionSnapshot, G as UsageTapClientOptions, I as UsageTapErrorResponse, L as UsageTapLogEntry, J as UsageTapResultEnvelope, K as UsageTapResultStatus, H as UsageTapSuccessResponse, V as VendorHints, Q as WithUsageContext, T as WithUsageOptions, i as WrapOpenAICallOptions, j as WrapOpenAIResponseCallOptions, k as WrappedOpenAI, c as createOpenAIAdapter, s as streamOpenAIRoute, t as toNextResponse } from './openai-CeptbEGH.js';
|
|
3
3
|
export { createOpenRouterAdapter } from './adapters/openrouter.js';
|
|
4
4
|
import OpenAI from 'openai';
|
|
5
5
|
import { Request, Response, NextFunction } from 'express';
|
package/dist/index.js
CHANGED
|
@@ -116,6 +116,7 @@ async function runWithRetry(operation, options, shouldRetry, onSchedule, signal)
|
|
|
116
116
|
// src/client.ts
|
|
117
117
|
var CALL_BEGIN_PATH = "call_begin";
|
|
118
118
|
var CALL_END_PATH = "call_end";
|
|
119
|
+
var CHECK_USAGE_PATH = "customers/{customerId}/usage";
|
|
119
120
|
var AUTH_HEADER = "authorization";
|
|
120
121
|
var API_KEY_HEADER = "x-api-key";
|
|
121
122
|
var CORRELATION_HEADER = "x-usage-correlation-id";
|
|
@@ -123,7 +124,7 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
|
|
|
123
124
|
var SDK_HEADER = "x-usage-sdk";
|
|
124
125
|
var USER_AGENT = "UsageTapClient";
|
|
125
126
|
var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
|
|
126
|
-
var SDK_VERSION = "0.
|
|
127
|
+
var SDK_VERSION = "0.2.0" ;
|
|
127
128
|
var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
128
129
|
var UsageTapClient = class {
|
|
129
130
|
apiKey;
|
|
@@ -217,6 +218,23 @@ var UsageTapClient = class {
|
|
|
217
218
|
);
|
|
218
219
|
return response;
|
|
219
220
|
}
|
|
221
|
+
async checkUsage(request, options = {}) {
|
|
222
|
+
if (!request?.customerId) {
|
|
223
|
+
throw new UsageTapError(
|
|
224
|
+
"USAGETAP_BAD_REQUEST",
|
|
225
|
+
"checkUsage requires customerId"
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const path = CHECK_USAGE_PATH.replace(
|
|
229
|
+
"{customerId}",
|
|
230
|
+
encodeURIComponent(request.customerId)
|
|
231
|
+
);
|
|
232
|
+
const response = await this.requestGet(
|
|
233
|
+
path,
|
|
234
|
+
options
|
|
235
|
+
);
|
|
236
|
+
return response;
|
|
237
|
+
}
|
|
220
238
|
async withUsage(beginRequest, handler, options = {}) {
|
|
221
239
|
const idempotencyKey = beginRequest.idempotency ?? (this.autoIdempotency ? this.idempotencyGenerator() : void 0);
|
|
222
240
|
const beginPayload = idempotencyKey ? { ...beginRequest, idempotency: idempotencyKey } : { ...beginRequest };
|
|
@@ -336,6 +354,62 @@ var UsageTapClient = class {
|
|
|
336
354
|
throw error;
|
|
337
355
|
});
|
|
338
356
|
}
|
|
357
|
+
async requestGet(path, options) {
|
|
358
|
+
const url = new URL(path, this.baseUrl).toString();
|
|
359
|
+
const headers = this.composeHeaders(void 0, options);
|
|
360
|
+
const resolvedRetry = resolveRetryOptions(
|
|
361
|
+
this.retryDefaults,
|
|
362
|
+
options.retries
|
|
363
|
+
);
|
|
364
|
+
const startTime = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
365
|
+
return runWithRetry(
|
|
366
|
+
async (attempt) => {
|
|
367
|
+
const startedAt = startTime();
|
|
368
|
+
this.log({
|
|
369
|
+
event: "request:start",
|
|
370
|
+
path,
|
|
371
|
+
attempt,
|
|
372
|
+
correlationId: options.correlationId
|
|
373
|
+
});
|
|
374
|
+
const response = await this.performFetch({
|
|
375
|
+
url,
|
|
376
|
+
method: "GET",
|
|
377
|
+
headers,
|
|
378
|
+
signal: options.signal
|
|
379
|
+
});
|
|
380
|
+
this.log({
|
|
381
|
+
event: "request:success",
|
|
382
|
+
path,
|
|
383
|
+
attempt,
|
|
384
|
+
correlationId: response.correlationId,
|
|
385
|
+
elapsedMs: startTime() - startedAt
|
|
386
|
+
});
|
|
387
|
+
return response;
|
|
388
|
+
},
|
|
389
|
+
resolvedRetry,
|
|
390
|
+
(error) => this.shouldRetry(error),
|
|
391
|
+
(attempt, delayMs, error) => {
|
|
392
|
+
this.log({
|
|
393
|
+
event: "retry:scheduled",
|
|
394
|
+
path,
|
|
395
|
+
attempt,
|
|
396
|
+
correlationId: options.correlationId,
|
|
397
|
+
error,
|
|
398
|
+
elapsedMs: delayMs
|
|
399
|
+
});
|
|
400
|
+
},
|
|
401
|
+
options.signal
|
|
402
|
+
).catch((error) => {
|
|
403
|
+
this.log({
|
|
404
|
+
event: "retry:exhausted",
|
|
405
|
+
path,
|
|
406
|
+
attempt: resolvedRetry.maxAttempts,
|
|
407
|
+
correlationId: options.correlationId,
|
|
408
|
+
error
|
|
409
|
+
});
|
|
410
|
+
throw error;
|
|
411
|
+
});
|
|
412
|
+
}
|
|
339
413
|
async performFetch(init) {
|
|
340
414
|
let response;
|
|
341
415
|
try {
|