@usagetap/sdk 1.0.0 → 1.1.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 +53 -16
- package/dist/adapters/anthropic.cjs +943 -0
- package/dist/adapters/anthropic.cjs.map +1 -0
- package/dist/adapters/anthropic.d.cts +81 -0
- package/dist/adapters/anthropic.d.ts +81 -0
- package/dist/adapters/anthropic.mjs +940 -0
- package/dist/adapters/anthropic.mjs.map +1 -0
- package/dist/adapters/openai.cjs +601 -17
- package/dist/adapters/openai.cjs.map +1 -1
- package/dist/adapters/openai.d.cts +57 -2
- package/dist/adapters/openai.d.ts +57 -2
- package/dist/adapters/openai.mjs +601 -18
- 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 +943 -0
- package/dist/anthropic/index.cjs.map +1 -0
- package/dist/anthropic/index.d.cts +2 -0
- package/dist/anthropic/index.d.ts +2 -0
- package/dist/anthropic/index.mjs +940 -0
- package/dist/anthropic/index.mjs.map +1 -0
- package/dist/{client-BHNMYvlO.d.cts → client-BA-QlnRq.d.cts} +32 -1
- package/dist/{client-BHNMYvlO.d.ts → client-BA-QlnRq.d.ts} +32 -1
- package/dist/express/index.cjs +597 -17
- 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 +597 -17
- package/dist/express/index.mjs.map +1 -1
- package/dist/index.cjs +77 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +76 -10
- package/dist/index.mjs.map +1 -1
- package/dist/openai/index.cjs +601 -17
- 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 +601 -18
- package/dist/openai/index.mjs.map +1 -1
- package/package.json +21 -1
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-BA-QlnRq.cjs';
|
|
2
|
+
export { _ as AllowedEntitlements, H as BalanceSummary, k as BeginCallOptions, c as BeginCallResponseBody, v as ChangePlanOptions, w as ChangePlanRequest, x as ChangePlanResponseBody, y as ChangePlanStrategy, s as CheckUsageOptions, t as CheckUsageRequest, u as CheckUsageResponseBody, C as CreateCustomerOptions, q as CreateCustomerRequest, r as CreateCustomerResponseBody, D as CustomMeterSlot, F as EndCallOptions, E as EndCallRequest, G as EndCallResponseBody, L as EntitlementHints, a9 as IdempotencyMetadata, I as IncrementCustomMeterOptions, z as IncrementCustomMeterRequest, A as IncrementCustomMeterResponseBody, a6 as LimitType, O as MeterSnapshot, N as MeterSummary, Q as MeteredUsage, a8 as ModelHints, M as PlanSummary, i as PromptCompressionInput, m as PromptCompressionOptions, P as PromptCompressionProvider, l as PromptCompressionRequest, n as PromptCompressionResponseBody, j as PromptCompressionResult, o as PromptCompressionStandaloneOptions, a as PromptCompressionTelemetry, T as RateLimitsSnapshot, R as RecordPromptCompressionRequest, X as RemainingRatios, Z as RequestedEntitlements, $ as RetryOptions, Y as RollingCallsRateLimitState, S as SpendVelocitySnapshot, J as SpendVelocityWindow, K as SpendVelocityWindowKey, a7 as SubscriptionSnapshot, aa as UsageMetricEvent, a0 as UsageTapClientOptions, a1 as UsageTapErrorResponse, a4 as UsageTapLogEntry, a2 as UsageTapResultEnvelope, a3 as UsageTapResultStatus, b as UsageTapSuccessResponse, V as VendorHints, a5 as WithUsageContext, W as WithUsageOptions, d as compressPrompt, e as compressPromptHeuristic, f as compressPromptToon, g as estimatePromptTokens, p as protect, h as protectPromptText } from './client-BA-QlnRq.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 {
|
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-BA-QlnRq.js';
|
|
2
|
+
export { _ as AllowedEntitlements, H as BalanceSummary, k as BeginCallOptions, c as BeginCallResponseBody, v as ChangePlanOptions, w as ChangePlanRequest, x as ChangePlanResponseBody, y as ChangePlanStrategy, s as CheckUsageOptions, t as CheckUsageRequest, u as CheckUsageResponseBody, C as CreateCustomerOptions, q as CreateCustomerRequest, r as CreateCustomerResponseBody, D as CustomMeterSlot, F as EndCallOptions, E as EndCallRequest, G as EndCallResponseBody, L as EntitlementHints, a9 as IdempotencyMetadata, I as IncrementCustomMeterOptions, z as IncrementCustomMeterRequest, A as IncrementCustomMeterResponseBody, a6 as LimitType, O as MeterSnapshot, N as MeterSummary, Q as MeteredUsage, a8 as ModelHints, M as PlanSummary, i as PromptCompressionInput, m as PromptCompressionOptions, P as PromptCompressionProvider, l as PromptCompressionRequest, n as PromptCompressionResponseBody, j as PromptCompressionResult, o as PromptCompressionStandaloneOptions, a as PromptCompressionTelemetry, T as RateLimitsSnapshot, R as RecordPromptCompressionRequest, X as RemainingRatios, Z as RequestedEntitlements, $ as RetryOptions, Y as RollingCallsRateLimitState, S as SpendVelocitySnapshot, J as SpendVelocityWindow, K as SpendVelocityWindowKey, a7 as SubscriptionSnapshot, aa as UsageMetricEvent, a0 as UsageTapClientOptions, a1 as UsageTapErrorResponse, a4 as UsageTapLogEntry, a2 as UsageTapResultEnvelope, a3 as UsageTapResultStatus, b as UsageTapSuccessResponse, V as VendorHints, a5 as WithUsageContext, W as WithUsageOptions, d as compressPrompt, e as compressPromptHeuristic, f as compressPromptToon, g as estimatePromptTokens, p as protect, h as protectPromptText } from './client-BA-QlnRq.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 {
|
package/dist/index.mjs
CHANGED
|
@@ -115,6 +115,13 @@ async function runWithRetry(operation, options, shouldRetry, onSchedule, signal)
|
|
|
115
115
|
|
|
116
116
|
// src/prompt-compression.ts
|
|
117
117
|
var DEFAULT_TTC_ENDPOINT = "https://api.thetokencompany.com/v1/compress";
|
|
118
|
+
var DEFAULT_TTC_MODEL = "bear-2";
|
|
119
|
+
var DEFAULT_TTC_AGGRESSIVENESS = 0.2;
|
|
120
|
+
var PROTECTED_TEXT_PATTERN = /<ttc_safe>[\s\S]*?<\/ttc_safe>|<usagetap_safe>[\s\S]*?<\/usagetap_safe>/g;
|
|
121
|
+
function protectPromptText(text) {
|
|
122
|
+
return `<ttc_safe>${text}</ttc_safe>`;
|
|
123
|
+
}
|
|
124
|
+
var protect = protectPromptText;
|
|
118
125
|
async function compressPrompt(options) {
|
|
119
126
|
try {
|
|
120
127
|
if (options.provider === "thetokencompany" || options.tokenCompanyApiKey) {
|
|
@@ -177,6 +184,12 @@ async function compressWithTheTokenCompany(options) {
|
|
|
177
184
|
}
|
|
178
185
|
const original = stableStringifyInput(options.input);
|
|
179
186
|
const heuristic = compressPromptHeuristic(options.input);
|
|
187
|
+
const input = typeof heuristic.compressedInput === "string" ? heuristic.compressedInput : stableStringifyInput(heuristic.compressedInput);
|
|
188
|
+
const model = options.tokenCompanyModel ?? DEFAULT_TTC_MODEL;
|
|
189
|
+
const aggressiveness = options.tokenCompanyAggressiveness ?? DEFAULT_TTC_AGGRESSIVENESS;
|
|
190
|
+
if (typeof aggressiveness !== "number" || !Number.isFinite(aggressiveness) || aggressiveness < 0 || aggressiveness > 1) {
|
|
191
|
+
throw new Error("tokenCompanyAggressiveness must be between 0.0 and 1.0");
|
|
192
|
+
}
|
|
180
193
|
const response = await fetchCandidate(
|
|
181
194
|
options.tokenCompanyEndpoint ?? DEFAULT_TTC_ENDPOINT,
|
|
182
195
|
{
|
|
@@ -185,7 +198,12 @@ async function compressWithTheTokenCompany(options) {
|
|
|
185
198
|
authorization: `Bearer ${options.tokenCompanyApiKey}`,
|
|
186
199
|
"content-type": "application/json"
|
|
187
200
|
},
|
|
188
|
-
body: JSON.stringify({
|
|
201
|
+
body: JSON.stringify({
|
|
202
|
+
model,
|
|
203
|
+
input,
|
|
204
|
+
compression_settings: { aggressiveness },
|
|
205
|
+
...options.tokenCompanyAppId ? { app_id: options.tokenCompanyAppId } : {}
|
|
206
|
+
}),
|
|
189
207
|
signal: options.signal
|
|
190
208
|
}
|
|
191
209
|
);
|
|
@@ -293,6 +311,20 @@ function compressValue(value, techniques, options) {
|
|
|
293
311
|
return value;
|
|
294
312
|
}
|
|
295
313
|
function compressText(value, techniques, options) {
|
|
314
|
+
const protectedSpans = [];
|
|
315
|
+
const text = value.replace(PROTECTED_TEXT_PATTERN, (match) => {
|
|
316
|
+
const placeholder = `__USAGETAP_PROTECTED_${protectedSpans.length}__`;
|
|
317
|
+
protectedSpans.push(match);
|
|
318
|
+
techniques.add("protected-text");
|
|
319
|
+
return placeholder;
|
|
320
|
+
});
|
|
321
|
+
const compressed = compressTextWithoutProtection(text, techniques, options);
|
|
322
|
+
return protectedSpans.reduce(
|
|
323
|
+
(output, span, index) => output.replace(`__USAGETAP_PROTECTED_${index}__`, span),
|
|
324
|
+
compressed
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
function compressTextWithoutProtection(value, techniques, options) {
|
|
296
328
|
const fencePattern = /```([\w-]+)?\n([\s\S]*?)```/g;
|
|
297
329
|
const parts = [];
|
|
298
330
|
let cursor = 0;
|
|
@@ -586,7 +618,7 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
|
|
|
586
618
|
var SDK_HEADER = "x-usage-sdk";
|
|
587
619
|
var USER_AGENT = "UsageTapClient";
|
|
588
620
|
var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
|
|
589
|
-
var SDK_VERSION = "1.
|
|
621
|
+
var SDK_VERSION = "1.1.0" ;
|
|
590
622
|
var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
591
623
|
var UsageTapClient = class {
|
|
592
624
|
apiKey;
|
|
@@ -603,6 +635,9 @@ var UsageTapClient = class {
|
|
|
603
635
|
autoIdempotency;
|
|
604
636
|
tokenCompanyApiKey;
|
|
605
637
|
tokenCompanyEndpoint;
|
|
638
|
+
tokenCompanyModel;
|
|
639
|
+
tokenCompanyAggressiveness;
|
|
640
|
+
tokenCompanyAppId;
|
|
606
641
|
constructor(options) {
|
|
607
642
|
if (!options) {
|
|
608
643
|
throw new UsageTapError(
|
|
@@ -650,6 +685,9 @@ var UsageTapClient = class {
|
|
|
650
685
|
this.autoIdempotency = options.autoIdempotency ?? true;
|
|
651
686
|
this.tokenCompanyApiKey = options.tokenCompanyApiKey;
|
|
652
687
|
this.tokenCompanyEndpoint = options.tokenCompanyEndpoint;
|
|
688
|
+
this.tokenCompanyModel = options.tokenCompanyModel;
|
|
689
|
+
this.tokenCompanyAggressiveness = options.tokenCompanyAggressiveness;
|
|
690
|
+
this.tokenCompanyAppId = options.tokenCompanyAppId;
|
|
653
691
|
}
|
|
654
692
|
async beginCall(request, options = {}) {
|
|
655
693
|
const idempotencyKey = request.idempotencyKey ?? request.idempotency ?? (this.autoIdempotency ? this.idempotencyGenerator() : void 0);
|
|
@@ -679,17 +717,15 @@ var UsageTapClient = class {
|
|
|
679
717
|
"promptCompress requires callId"
|
|
680
718
|
);
|
|
681
719
|
}
|
|
682
|
-
const result = await
|
|
683
|
-
input: request.input,
|
|
720
|
+
const result = await this.compressPromptInput(request.input, {
|
|
684
721
|
provider: request.provider,
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
722
|
+
tokenCompanyModel: request.tokenCompanyModel,
|
|
723
|
+
tokenCompanyAggressiveness: request.tokenCompanyAggressiveness,
|
|
724
|
+
tokenCompanyAppId: request.tokenCompanyAppId,
|
|
688
725
|
signal: options.signal
|
|
689
726
|
});
|
|
690
727
|
try {
|
|
691
|
-
await this.
|
|
692
|
-
COMPRESS_PROMPT_PATH,
|
|
728
|
+
await this.recordPromptCompression(
|
|
693
729
|
{
|
|
694
730
|
callId: request.callId,
|
|
695
731
|
promptCompression: this.toPromptCompressionTelemetry(result)
|
|
@@ -708,6 +744,36 @@ var UsageTapClient = class {
|
|
|
708
744
|
};
|
|
709
745
|
}
|
|
710
746
|
}
|
|
747
|
+
async compressPromptInput(input, options = {}) {
|
|
748
|
+
return compressPrompt({
|
|
749
|
+
input,
|
|
750
|
+
provider: options.provider,
|
|
751
|
+
tokenCompanyApiKey: this.tokenCompanyApiKey,
|
|
752
|
+
tokenCompanyEndpoint: this.tokenCompanyEndpoint,
|
|
753
|
+
tokenCompanyModel: options.tokenCompanyModel ?? this.tokenCompanyModel,
|
|
754
|
+
tokenCompanyAggressiveness: options.tokenCompanyAggressiveness ?? this.tokenCompanyAggressiveness,
|
|
755
|
+
tokenCompanyAppId: options.tokenCompanyAppId ?? this.tokenCompanyAppId,
|
|
756
|
+
fetchImpl: this.fetchImpl,
|
|
757
|
+
signal: options.signal,
|
|
758
|
+
failOpen: options.failOpen
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
async recordPromptCompression(request, options = {}) {
|
|
762
|
+
if (!request?.callId) {
|
|
763
|
+
throw new UsageTapError(
|
|
764
|
+
"USAGETAP_BAD_REQUEST",
|
|
765
|
+
"recordPromptCompression requires callId"
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
return this.request(
|
|
769
|
+
COMPRESS_PROMPT_PATH,
|
|
770
|
+
{
|
|
771
|
+
callId: request.callId,
|
|
772
|
+
promptCompression: request.promptCompression
|
|
773
|
+
},
|
|
774
|
+
options
|
|
775
|
+
);
|
|
776
|
+
}
|
|
711
777
|
async endCall(request, options = {}) {
|
|
712
778
|
if (!request?.callId) {
|
|
713
779
|
throw new UsageTapError(
|
|
@@ -1533,6 +1599,6 @@ async function finalizeCall(callState, usageTap, error, usage) {
|
|
|
1533
1599
|
}
|
|
1534
1600
|
}
|
|
1535
1601
|
|
|
1536
|
-
export { UsageTapClient, UsageTapError, compressPrompt, compressPromptHeuristic, compressPromptToon, createIdempotencyKey, estimatePromptTokens, isUsageTapError, wrapFetch };
|
|
1602
|
+
export { UsageTapClient, UsageTapError, compressPrompt, compressPromptHeuristic, compressPromptToon, createIdempotencyKey, estimatePromptTokens, isUsageTapError, protect, protectPromptText, wrapFetch };
|
|
1537
1603
|
//# sourceMappingURL=index.mjs.map
|
|
1538
1604
|
//# sourceMappingURL=index.mjs.map
|