@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.cjs
CHANGED
|
@@ -117,6 +117,13 @@ async function runWithRetry(operation, options, shouldRetry, onSchedule, signal)
|
|
|
117
117
|
|
|
118
118
|
// src/prompt-compression.ts
|
|
119
119
|
var DEFAULT_TTC_ENDPOINT = "https://api.thetokencompany.com/v1/compress";
|
|
120
|
+
var DEFAULT_TTC_MODEL = "bear-2";
|
|
121
|
+
var DEFAULT_TTC_AGGRESSIVENESS = 0.2;
|
|
122
|
+
var PROTECTED_TEXT_PATTERN = /<ttc_safe>[\s\S]*?<\/ttc_safe>|<usagetap_safe>[\s\S]*?<\/usagetap_safe>/g;
|
|
123
|
+
function protectPromptText(text) {
|
|
124
|
+
return `<ttc_safe>${text}</ttc_safe>`;
|
|
125
|
+
}
|
|
126
|
+
var protect = protectPromptText;
|
|
120
127
|
async function compressPrompt(options) {
|
|
121
128
|
try {
|
|
122
129
|
if (options.provider === "thetokencompany" || options.tokenCompanyApiKey) {
|
|
@@ -179,6 +186,12 @@ async function compressWithTheTokenCompany(options) {
|
|
|
179
186
|
}
|
|
180
187
|
const original = stableStringifyInput(options.input);
|
|
181
188
|
const heuristic = compressPromptHeuristic(options.input);
|
|
189
|
+
const input = typeof heuristic.compressedInput === "string" ? heuristic.compressedInput : stableStringifyInput(heuristic.compressedInput);
|
|
190
|
+
const model = options.tokenCompanyModel ?? DEFAULT_TTC_MODEL;
|
|
191
|
+
const aggressiveness = options.tokenCompanyAggressiveness ?? DEFAULT_TTC_AGGRESSIVENESS;
|
|
192
|
+
if (typeof aggressiveness !== "number" || !Number.isFinite(aggressiveness) || aggressiveness < 0 || aggressiveness > 1) {
|
|
193
|
+
throw new Error("tokenCompanyAggressiveness must be between 0.0 and 1.0");
|
|
194
|
+
}
|
|
182
195
|
const response = await fetchCandidate(
|
|
183
196
|
options.tokenCompanyEndpoint ?? DEFAULT_TTC_ENDPOINT,
|
|
184
197
|
{
|
|
@@ -187,7 +200,12 @@ async function compressWithTheTokenCompany(options) {
|
|
|
187
200
|
authorization: `Bearer ${options.tokenCompanyApiKey}`,
|
|
188
201
|
"content-type": "application/json"
|
|
189
202
|
},
|
|
190
|
-
body: JSON.stringify({
|
|
203
|
+
body: JSON.stringify({
|
|
204
|
+
model,
|
|
205
|
+
input,
|
|
206
|
+
compression_settings: { aggressiveness },
|
|
207
|
+
...options.tokenCompanyAppId ? { app_id: options.tokenCompanyAppId } : {}
|
|
208
|
+
}),
|
|
191
209
|
signal: options.signal
|
|
192
210
|
}
|
|
193
211
|
);
|
|
@@ -295,6 +313,20 @@ function compressValue(value, techniques, options) {
|
|
|
295
313
|
return value;
|
|
296
314
|
}
|
|
297
315
|
function compressText(value, techniques, options) {
|
|
316
|
+
const protectedSpans = [];
|
|
317
|
+
const text = value.replace(PROTECTED_TEXT_PATTERN, (match) => {
|
|
318
|
+
const placeholder = `__USAGETAP_PROTECTED_${protectedSpans.length}__`;
|
|
319
|
+
protectedSpans.push(match);
|
|
320
|
+
techniques.add("protected-text");
|
|
321
|
+
return placeholder;
|
|
322
|
+
});
|
|
323
|
+
const compressed = compressTextWithoutProtection(text, techniques, options);
|
|
324
|
+
return protectedSpans.reduce(
|
|
325
|
+
(output, span, index) => output.replace(`__USAGETAP_PROTECTED_${index}__`, span),
|
|
326
|
+
compressed
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
function compressTextWithoutProtection(value, techniques, options) {
|
|
298
330
|
const fencePattern = /```([\w-]+)?\n([\s\S]*?)```/g;
|
|
299
331
|
const parts = [];
|
|
300
332
|
let cursor = 0;
|
|
@@ -588,7 +620,7 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
|
|
|
588
620
|
var SDK_HEADER = "x-usage-sdk";
|
|
589
621
|
var USER_AGENT = "UsageTapClient";
|
|
590
622
|
var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
|
|
591
|
-
var SDK_VERSION = "1.
|
|
623
|
+
var SDK_VERSION = "1.1.0" ;
|
|
592
624
|
var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
593
625
|
var UsageTapClient = class {
|
|
594
626
|
apiKey;
|
|
@@ -605,6 +637,9 @@ var UsageTapClient = class {
|
|
|
605
637
|
autoIdempotency;
|
|
606
638
|
tokenCompanyApiKey;
|
|
607
639
|
tokenCompanyEndpoint;
|
|
640
|
+
tokenCompanyModel;
|
|
641
|
+
tokenCompanyAggressiveness;
|
|
642
|
+
tokenCompanyAppId;
|
|
608
643
|
constructor(options) {
|
|
609
644
|
if (!options) {
|
|
610
645
|
throw new UsageTapError(
|
|
@@ -652,6 +687,9 @@ var UsageTapClient = class {
|
|
|
652
687
|
this.autoIdempotency = options.autoIdempotency ?? true;
|
|
653
688
|
this.tokenCompanyApiKey = options.tokenCompanyApiKey;
|
|
654
689
|
this.tokenCompanyEndpoint = options.tokenCompanyEndpoint;
|
|
690
|
+
this.tokenCompanyModel = options.tokenCompanyModel;
|
|
691
|
+
this.tokenCompanyAggressiveness = options.tokenCompanyAggressiveness;
|
|
692
|
+
this.tokenCompanyAppId = options.tokenCompanyAppId;
|
|
655
693
|
}
|
|
656
694
|
async beginCall(request, options = {}) {
|
|
657
695
|
const idempotencyKey = request.idempotencyKey ?? request.idempotency ?? (this.autoIdempotency ? this.idempotencyGenerator() : void 0);
|
|
@@ -681,17 +719,15 @@ var UsageTapClient = class {
|
|
|
681
719
|
"promptCompress requires callId"
|
|
682
720
|
);
|
|
683
721
|
}
|
|
684
|
-
const result = await
|
|
685
|
-
input: request.input,
|
|
722
|
+
const result = await this.compressPromptInput(request.input, {
|
|
686
723
|
provider: request.provider,
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
724
|
+
tokenCompanyModel: request.tokenCompanyModel,
|
|
725
|
+
tokenCompanyAggressiveness: request.tokenCompanyAggressiveness,
|
|
726
|
+
tokenCompanyAppId: request.tokenCompanyAppId,
|
|
690
727
|
signal: options.signal
|
|
691
728
|
});
|
|
692
729
|
try {
|
|
693
|
-
await this.
|
|
694
|
-
COMPRESS_PROMPT_PATH,
|
|
730
|
+
await this.recordPromptCompression(
|
|
695
731
|
{
|
|
696
732
|
callId: request.callId,
|
|
697
733
|
promptCompression: this.toPromptCompressionTelemetry(result)
|
|
@@ -710,6 +746,36 @@ var UsageTapClient = class {
|
|
|
710
746
|
};
|
|
711
747
|
}
|
|
712
748
|
}
|
|
749
|
+
async compressPromptInput(input, options = {}) {
|
|
750
|
+
return compressPrompt({
|
|
751
|
+
input,
|
|
752
|
+
provider: options.provider,
|
|
753
|
+
tokenCompanyApiKey: this.tokenCompanyApiKey,
|
|
754
|
+
tokenCompanyEndpoint: this.tokenCompanyEndpoint,
|
|
755
|
+
tokenCompanyModel: options.tokenCompanyModel ?? this.tokenCompanyModel,
|
|
756
|
+
tokenCompanyAggressiveness: options.tokenCompanyAggressiveness ?? this.tokenCompanyAggressiveness,
|
|
757
|
+
tokenCompanyAppId: options.tokenCompanyAppId ?? this.tokenCompanyAppId,
|
|
758
|
+
fetchImpl: this.fetchImpl,
|
|
759
|
+
signal: options.signal,
|
|
760
|
+
failOpen: options.failOpen
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
async recordPromptCompression(request, options = {}) {
|
|
764
|
+
if (!request?.callId) {
|
|
765
|
+
throw new UsageTapError(
|
|
766
|
+
"USAGETAP_BAD_REQUEST",
|
|
767
|
+
"recordPromptCompression requires callId"
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
return this.request(
|
|
771
|
+
COMPRESS_PROMPT_PATH,
|
|
772
|
+
{
|
|
773
|
+
callId: request.callId,
|
|
774
|
+
promptCompression: request.promptCompression
|
|
775
|
+
},
|
|
776
|
+
options
|
|
777
|
+
);
|
|
778
|
+
}
|
|
713
779
|
async endCall(request, options = {}) {
|
|
714
780
|
if (!request?.callId) {
|
|
715
781
|
throw new UsageTapError(
|
|
@@ -1543,6 +1609,8 @@ exports.compressPromptToon = compressPromptToon;
|
|
|
1543
1609
|
exports.createIdempotencyKey = createIdempotencyKey;
|
|
1544
1610
|
exports.estimatePromptTokens = estimatePromptTokens;
|
|
1545
1611
|
exports.isUsageTapError = isUsageTapError;
|
|
1612
|
+
exports.protect = protect;
|
|
1613
|
+
exports.protectPromptText = protectPromptText;
|
|
1546
1614
|
exports.wrapFetch = wrapFetch;
|
|
1547
1615
|
//# sourceMappingURL=index.cjs.map
|
|
1548
1616
|
//# sourceMappingURL=index.cjs.map
|