@usagetap/sdk 1.2.0 → 1.3.2
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 +710 -735
- package/dist/adapters/anthropic.cjs +1609 -5
- package/dist/adapters/anthropic.cjs.map +1 -1
- package/dist/adapters/anthropic.d.cts +30 -2
- package/dist/adapters/anthropic.d.ts +30 -2
- package/dist/adapters/anthropic.mjs +1608 -6
- package/dist/adapters/anthropic.mjs.map +1 -1
- package/dist/adapters/openai.cjs +1659 -5
- package/dist/adapters/openai.cjs.map +1 -1
- package/dist/adapters/openai.d.cts +37 -2
- package/dist/adapters/openai.d.ts +37 -2
- package/dist/adapters/openai.mjs +1658 -6
- package/dist/adapters/openai.mjs.map +1 -1
- package/dist/adapters/openrouter.cjs.map +1 -1
- package/dist/adapters/openrouter.d.cts +1 -1
- package/dist/adapters/openrouter.d.ts +1 -1
- package/dist/adapters/openrouter.mjs.map +1 -1
- package/dist/anthropic/index.cjs +1609 -5
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.cts +2 -2
- package/dist/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.mjs +1608 -6
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/{client-EMXt9_fA.d.cts → client-BD8O2J8Z.d.cts} +30 -4
- package/dist/{client-EMXt9_fA.d.ts → client-BD8O2J8Z.d.ts} +30 -4
- package/dist/express/index.cjs +19 -0
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.d.cts +1 -1
- package/dist/express/index.d.ts +1 -1
- package/dist/express/index.mjs +19 -0
- package/dist/express/index.mjs.map +1 -1
- package/dist/index.cjs +48 -19
- 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 +48 -20
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.cjs +1659 -5
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.cts +2 -2
- package/dist/openai/index.d.ts +2 -2
- package/dist/openai/index.mjs +1658 -6
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openrouter/index.cjs +3024 -0
- package/dist/openrouter/index.cjs.map +1 -0
- package/dist/openrouter/index.d.cts +4 -0
- package/dist/openrouter/index.d.ts +4 -0
- package/dist/openrouter/index.mjs +3019 -0
- package/dist/openrouter/index.mjs.map +1 -0
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.mjs.map +1 -1
- package/package.json +182 -124
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { U as UsageTapClient, B as BeginCallRequest } from './client-
|
|
2
|
-
export {
|
|
1
|
+
import { U as UsageTapClient, B as BeginCallRequest } from './client-BD8O2J8Z.cjs';
|
|
2
|
+
export { a7 as AllowedEntitlements, T as BalanceSummary, s as BeginCallOptions, c as BeginCallResponseBody, G as ChangePlanOptions, H as ChangePlanRequest, I as ChangePlanResponseBody, J as ChangePlanStrategy, A as CheckUsageOptions, D as CheckUsageRequest, F as CheckUsageResponseBody, C as CreateCustomerOptions, y as CreateCustomerRequest, z as CreateCustomerResponseBody, O as CustomMeterSlot, Q as EndCallOptions, E as EndCallRequest, S as EndCallResponseBody, _ as EntitlementHints, ai as IdempotencyMetadata, K as IncrementCustomMeterOptions, L as IncrementCustomMeterRequest, N as IncrementCustomMeterResponseBody, af as LimitType, M as MeterRequest, a1 as MeterSnapshot, a0 as MeterSummary, a2 as MeteredUsage, ah as ModelHints, $ as PlanSummary, l as PromptCompressionInput, o as PromptCompressionMessageRole, m as PromptCompressionMessagesAggressiveness, n as PromptCompressionMessagesInput, v as PromptCompressionMessagesOptions, u as PromptCompressionOptions, P as PromptCompressionProvider, t as PromptCompressionRequest, w as PromptCompressionResponseBody, q as PromptCompressionResult, r as PromptCompressionRoleAggressiveness, x as PromptCompressionStandaloneOptions, a as PromptCompressionTelemetry, a3 as RateLimitsSnapshot, R as RecordPromptCompressionRequest, a4 as RemainingRatios, a6 as RequestedEntitlements, a8 as RetryOptions, a5 as RollingCallsRateLimitState, X as SpendVelocitySnapshot, Y as SpendVelocityWindow, Z as SpendVelocityWindowKey, ag as SubscriptionSnapshot, aj as UsageMetricEvent, a9 as UsageTapClientOptions, d as UsageTapCompressOptions, e as UsageTapCompressResult, aa as UsageTapErrorResponse, ad as UsageTapLogEntry, ab as UsageTapResultEnvelope, ac as UsageTapResultStatus, b as UsageTapSuccessResponse, V as VendorHints, ae as WithUsageContext, W as WithUsageOptions, f as compressPrompt, h as compressPromptHeuristic, g as compressPromptMessages, i as compressPromptToon, j as estimatePromptTokens, p as protect, k as protectPromptText } from './client-BD8O2J8Z.cjs';
|
|
3
3
|
|
|
4
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";
|
|
5
5
|
interface UsageTapErrorInit {
|
|
@@ -65,4 +65,4 @@ declare function wrapFetch(usageTap: UsageTapClient, options: WrapFetchOptions):
|
|
|
65
65
|
|
|
66
66
|
declare function createIdempotencyKey(): string;
|
|
67
67
|
|
|
68
|
-
export { BeginCallRequest, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WrapFetchContext, type WrapFetchOptions, createIdempotencyKey, isUsageTapError, wrapFetch };
|
|
68
|
+
export { BeginCallRequest, UsageTapClient as UsageTap, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WrapFetchContext, type WrapFetchOptions, createIdempotencyKey, isUsageTapError, wrapFetch };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { U as UsageTapClient, B as BeginCallRequest } from './client-
|
|
2
|
-
export {
|
|
1
|
+
import { U as UsageTapClient, B as BeginCallRequest } from './client-BD8O2J8Z.js';
|
|
2
|
+
export { a7 as AllowedEntitlements, T as BalanceSummary, s as BeginCallOptions, c as BeginCallResponseBody, G as ChangePlanOptions, H as ChangePlanRequest, I as ChangePlanResponseBody, J as ChangePlanStrategy, A as CheckUsageOptions, D as CheckUsageRequest, F as CheckUsageResponseBody, C as CreateCustomerOptions, y as CreateCustomerRequest, z as CreateCustomerResponseBody, O as CustomMeterSlot, Q as EndCallOptions, E as EndCallRequest, S as EndCallResponseBody, _ as EntitlementHints, ai as IdempotencyMetadata, K as IncrementCustomMeterOptions, L as IncrementCustomMeterRequest, N as IncrementCustomMeterResponseBody, af as LimitType, M as MeterRequest, a1 as MeterSnapshot, a0 as MeterSummary, a2 as MeteredUsage, ah as ModelHints, $ as PlanSummary, l as PromptCompressionInput, o as PromptCompressionMessageRole, m as PromptCompressionMessagesAggressiveness, n as PromptCompressionMessagesInput, v as PromptCompressionMessagesOptions, u as PromptCompressionOptions, P as PromptCompressionProvider, t as PromptCompressionRequest, w as PromptCompressionResponseBody, q as PromptCompressionResult, r as PromptCompressionRoleAggressiveness, x as PromptCompressionStandaloneOptions, a as PromptCompressionTelemetry, a3 as RateLimitsSnapshot, R as RecordPromptCompressionRequest, a4 as RemainingRatios, a6 as RequestedEntitlements, a8 as RetryOptions, a5 as RollingCallsRateLimitState, X as SpendVelocitySnapshot, Y as SpendVelocityWindow, Z as SpendVelocityWindowKey, ag as SubscriptionSnapshot, aj as UsageMetricEvent, a9 as UsageTapClientOptions, d as UsageTapCompressOptions, e as UsageTapCompressResult, aa as UsageTapErrorResponse, ad as UsageTapLogEntry, ab as UsageTapResultEnvelope, ac as UsageTapResultStatus, b as UsageTapSuccessResponse, V as VendorHints, ae as WithUsageContext, W as WithUsageOptions, f as compressPrompt, h as compressPromptHeuristic, g as compressPromptMessages, i as compressPromptToon, j as estimatePromptTokens, p as protect, k as protectPromptText } from './client-BD8O2J8Z.js';
|
|
3
3
|
|
|
4
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";
|
|
5
5
|
interface UsageTapErrorInit {
|
|
@@ -65,4 +65,4 @@ declare function wrapFetch(usageTap: UsageTapClient, options: WrapFetchOptions):
|
|
|
65
65
|
|
|
66
66
|
declare function createIdempotencyKey(): string;
|
|
67
67
|
|
|
68
|
-
export { BeginCallRequest, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WrapFetchContext, type WrapFetchOptions, createIdempotencyKey, isUsageTapError, wrapFetch };
|
|
68
|
+
export { BeginCallRequest, UsageTapClient as UsageTap, UsageTapClient, UsageTapError, type UsageTapErrorCode, type UsageTapErrorInit, type WrapFetchContext, type WrapFetchOptions, createIdempotencyKey, isUsageTapError, wrapFetch };
|
package/dist/index.mjs
CHANGED
|
@@ -768,7 +768,8 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
|
|
|
768
768
|
var SDK_HEADER = "x-usage-sdk";
|
|
769
769
|
var USER_AGENT = "UsageTapClient";
|
|
770
770
|
var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
|
|
771
|
-
var
|
|
771
|
+
var DEFAULT_BASE_URL = "https://api.usagetap.com";
|
|
772
|
+
var SDK_VERSION = "1.3.2" ;
|
|
772
773
|
var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
773
774
|
var UsageTapClient = class {
|
|
774
775
|
apiKey;
|
|
@@ -795,23 +796,13 @@ var UsageTapClient = class {
|
|
|
795
796
|
usageTapCompressionMessagesEndpoint;
|
|
796
797
|
usageTapCompressionModel;
|
|
797
798
|
usageTapCompressionAggressiveness;
|
|
798
|
-
constructor(options) {
|
|
799
|
-
|
|
799
|
+
constructor(options = {}) {
|
|
800
|
+
const apiKey = options.apiKey?.trim() || readEnvironmentVariable("USAGETAP_API_KEY");
|
|
801
|
+
const baseUrl = options.baseUrl?.trim() || readEnvironmentVariable("USAGETAP_BASE_URL") || DEFAULT_BASE_URL;
|
|
802
|
+
if (!apiKey) {
|
|
800
803
|
throw new UsageTapError(
|
|
801
804
|
"USAGETAP_BAD_REQUEST",
|
|
802
|
-
"UsageTapClient
|
|
803
|
-
);
|
|
804
|
-
}
|
|
805
|
-
if (!options.apiKey) {
|
|
806
|
-
throw new UsageTapError(
|
|
807
|
-
"USAGETAP_BAD_REQUEST",
|
|
808
|
-
"UsageTapClient requires an apiKey"
|
|
809
|
-
);
|
|
810
|
-
}
|
|
811
|
-
if (!options.baseUrl) {
|
|
812
|
-
throw new UsageTapError(
|
|
813
|
-
"USAGETAP_BAD_REQUEST",
|
|
814
|
-
"UsageTapClient requires a baseUrl"
|
|
805
|
+
"UsageTapClient requires an apiKey or the USAGETAP_API_KEY environment variable"
|
|
815
806
|
);
|
|
816
807
|
}
|
|
817
808
|
if (HAS_WINDOW && !options.allowBrowser) {
|
|
@@ -827,9 +818,9 @@ var UsageTapClient = class {
|
|
|
827
818
|
"A global fetch implementation was not found. Pass fetchImpl in UsageTapClientOptions."
|
|
828
819
|
);
|
|
829
820
|
}
|
|
830
|
-
const normalizedBaseUrl = normalizeBaseUrl(
|
|
821
|
+
const normalizedBaseUrl = normalizeBaseUrl(baseUrl);
|
|
831
822
|
this.baseUrl = new URL(normalizedBaseUrl);
|
|
832
|
-
this.apiKey =
|
|
823
|
+
this.apiKey = apiKey;
|
|
833
824
|
this.fetchImpl = wrapFetchImplementation(fetchCandidate, !options.fetchImpl);
|
|
834
825
|
this.defaultFeature = options.defaultFeature;
|
|
835
826
|
this.defaultTags = options.defaultTags?.length ? dedupeStrings(options.defaultTags) : void 0;
|
|
@@ -847,7 +838,7 @@ var UsageTapClient = class {
|
|
|
847
838
|
this.aggressiveness = options.aggressiveness;
|
|
848
839
|
this.tokenCompanyAggressiveness = options.tokenCompanyAggressiveness;
|
|
849
840
|
this.tokenCompanyAppId = options.tokenCompanyAppId;
|
|
850
|
-
this.usageTapCompressionApiKey = options.usageTapCompressionApiKey ??
|
|
841
|
+
this.usageTapCompressionApiKey = options.usageTapCompressionApiKey ?? apiKey;
|
|
851
842
|
this.usageTapCompressionEndpoint = options.usageTapCompressionEndpoint;
|
|
852
843
|
this.usageTapCompressionMessagesEndpoint = options.usageTapCompressionMessagesEndpoint;
|
|
853
844
|
this.usageTapCompressionModel = options.usageTapCompressionModel;
|
|
@@ -939,6 +930,30 @@ var UsageTapClient = class {
|
|
|
939
930
|
failOpen: options.failOpen
|
|
940
931
|
});
|
|
941
932
|
}
|
|
933
|
+
/**
|
|
934
|
+
* Compress text with UsageTap's hosted compression service.
|
|
935
|
+
*
|
|
936
|
+
* This is the short, standalone path. It does not create a metered call and
|
|
937
|
+
* fails open to the original text unless failOpen is explicitly disabled.
|
|
938
|
+
*/
|
|
939
|
+
async compress(text, options = {}) {
|
|
940
|
+
if (typeof text !== "string") {
|
|
941
|
+
throw new UsageTapError(
|
|
942
|
+
"USAGETAP_BAD_REQUEST",
|
|
943
|
+
"compress requires text"
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
const result = await this.compressPromptInput(text, {
|
|
947
|
+
...options,
|
|
948
|
+
provider: "usagetap"
|
|
949
|
+
});
|
|
950
|
+
const output = typeof result.compressedInput === "string" ? result.compressedInput : text;
|
|
951
|
+
return {
|
|
952
|
+
...result,
|
|
953
|
+
compressedInput: output,
|
|
954
|
+
output
|
|
955
|
+
};
|
|
956
|
+
}
|
|
942
957
|
async compressPromptMessages(input, options = {}) {
|
|
943
958
|
return compressPromptMessages({
|
|
944
959
|
input,
|
|
@@ -1192,6 +1207,14 @@ var UsageTapClient = class {
|
|
|
1192
1207
|
}
|
|
1193
1208
|
return handlerResult;
|
|
1194
1209
|
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Meter one operation. Pass only a customer ID for the common path, or the
|
|
1212
|
+
* existing begin-call request object when feature, tags, or entitlements are needed.
|
|
1213
|
+
*/
|
|
1214
|
+
async meter(request, handler, options = {}) {
|
|
1215
|
+
const beginRequest = typeof request === "string" ? { customerId: request } : request;
|
|
1216
|
+
return this.withUsage(beginRequest, handler, options);
|
|
1217
|
+
}
|
|
1195
1218
|
toPromptCompressionTelemetry(result) {
|
|
1196
1219
|
return {
|
|
1197
1220
|
provider: result.provider,
|
|
@@ -1504,6 +1527,11 @@ function sanitizeDetails(payload) {
|
|
|
1504
1527
|
if (payload.error) details.error = payload.error;
|
|
1505
1528
|
return Object.keys(details).length ? details : void 0;
|
|
1506
1529
|
}
|
|
1530
|
+
function readEnvironmentVariable(name) {
|
|
1531
|
+
const runtime = globalThis;
|
|
1532
|
+
const value = runtime.process?.env?.[name]?.trim();
|
|
1533
|
+
return value || void 0;
|
|
1534
|
+
}
|
|
1507
1535
|
function normalizeBaseUrl(baseUrl) {
|
|
1508
1536
|
const trimmed = baseUrl.trim();
|
|
1509
1537
|
if (!trimmed) return trimmed;
|
|
@@ -1793,6 +1821,6 @@ async function finalizeCall(callState, usageTap, error, usage) {
|
|
|
1793
1821
|
}
|
|
1794
1822
|
}
|
|
1795
1823
|
|
|
1796
|
-
export { UsageTapClient, UsageTapError, compressPrompt, compressPromptHeuristic, compressPromptMessages, compressPromptToon, createIdempotencyKey, estimatePromptTokens, isUsageTapError, protect, protectPromptText, wrapFetch };
|
|
1824
|
+
export { UsageTapClient as UsageTap, UsageTapClient, UsageTapError, compressPrompt, compressPromptHeuristic, compressPromptMessages, compressPromptToon, createIdempotencyKey, estimatePromptTokens, isUsageTapError, protect, protectPromptText, wrapFetch };
|
|
1797
1825
|
//# sourceMappingURL=index.mjs.map
|
|
1798
1826
|
//# sourceMappingURL=index.mjs.map
|