@usagetap/sdk 0.10.0 → 1.0.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/dist/index.mjs CHANGED
@@ -113,9 +113,468 @@ async function runWithRetry(operation, options, shouldRetry, onSchedule, signal)
113
113
  throw lastError instanceof Error ? lastError : new Error(String(lastError));
114
114
  }
115
115
 
116
+ // src/prompt-compression.ts
117
+ var DEFAULT_TTC_ENDPOINT = "https://api.thetokencompany.com/v1/compress";
118
+ async function compressPrompt(options) {
119
+ try {
120
+ if (options.provider === "thetokencompany" || options.tokenCompanyApiKey) {
121
+ return await compressWithTheTokenCompany(options);
122
+ }
123
+ if (options.provider === "toon") {
124
+ return compressPromptToon(options.input);
125
+ }
126
+ return compressPromptHeuristic(options.input);
127
+ } catch (error) {
128
+ if (options.failOpen === false) {
129
+ throw error;
130
+ }
131
+ return createPromptCompressionFallback(
132
+ options.input,
133
+ options.provider ?? (options.tokenCompanyApiKey ? "thetokencompany" : "heuristic"),
134
+ error
135
+ );
136
+ }
137
+ }
138
+ function compressPromptHeuristic(input) {
139
+ const original = stableStringifyInput(input);
140
+ const techniques = /* @__PURE__ */ new Set();
141
+ const compressedInput = compressValue(input, techniques, { allowToonString: false });
142
+ const compressed = stableStringifyInput(compressedInput);
143
+ const chosenInput = compressed.length <= original.length ? compressedInput : input;
144
+ const chosen = compressed.length <= original.length ? compressed : original;
145
+ if (!techniques.size) {
146
+ techniques.add("no-op");
147
+ }
148
+ return buildResult(
149
+ input,
150
+ chosenInput,
151
+ "heuristic",
152
+ original,
153
+ chosen,
154
+ Array.from(techniques)
155
+ );
156
+ }
157
+ function compressPromptToon(input) {
158
+ const original = stableStringifyInput(input);
159
+ const compressedInput = typeof input === "string" ? compressText(input, /* @__PURE__ */ new Set(), { allowToonString: true }) : encodeToon(input);
160
+ const compressed = stableStringifyInput(compressedInput);
161
+ return buildResult(input, compressedInput, "toon", original, compressed, [
162
+ "toon",
163
+ "json-minify"
164
+ ]);
165
+ }
166
+ async function compressWithTheTokenCompany(options) {
167
+ if (!options.tokenCompanyApiKey) {
168
+ throw new Error(
169
+ "tokenCompanyApiKey is required when provider is thetokencompany"
170
+ );
171
+ }
172
+ const fetchCandidate = options.fetchImpl ?? globalThis.fetch;
173
+ if (typeof fetchCandidate !== "function") {
174
+ throw new Error(
175
+ "A fetch implementation is required for The Token Company compression"
176
+ );
177
+ }
178
+ const original = stableStringifyInput(options.input);
179
+ const heuristic = compressPromptHeuristic(options.input);
180
+ const response = await fetchCandidate(
181
+ options.tokenCompanyEndpoint ?? DEFAULT_TTC_ENDPOINT,
182
+ {
183
+ method: "POST",
184
+ headers: {
185
+ authorization: `Bearer ${options.tokenCompanyApiKey}`,
186
+ "content-type": "application/json"
187
+ },
188
+ body: JSON.stringify({ input: heuristic.compressedInput }),
189
+ signal: options.signal
190
+ }
191
+ );
192
+ if (!response.ok) {
193
+ throw new Error(
194
+ `The Token Company compression failed with HTTP ${response.status}`
195
+ );
196
+ }
197
+ const payload = await response.json();
198
+ const tokenCompanyResult = normalizeTheTokenCompanyCompressResponse(payload);
199
+ const compressedInput = payload.compressedInput ?? payload.compressed ?? tokenCompanyResult?.output ?? payload.output ?? payload.text;
200
+ if (compressedInput === void 0) {
201
+ throw new Error("The Token Company response did not include compressed content");
202
+ }
203
+ const compressed = stableStringifyInput(compressedInput);
204
+ const tokenCounts = tokenCompanyResult ? {
205
+ originalTokens: tokenCompanyResult.input_tokens,
206
+ compressedTokens: tokenCompanyResult.output_tokens,
207
+ savedTokens: tokenCompanyResult.tokens_saved
208
+ } : void 0;
209
+ return buildResult(
210
+ options.input,
211
+ compressedInput,
212
+ "thetokencompany",
213
+ original,
214
+ compressed,
215
+ [...heuristic.techniques, "thetokencompany"],
216
+ tokenCounts
217
+ );
218
+ }
219
+ function normalizeTheTokenCompanyCompressResponse(data) {
220
+ if (typeof data.output !== "string" || typeof data.output_tokens !== "number") {
221
+ return void 0;
222
+ }
223
+ const inputTokens = typeof data.input_tokens === "number" ? data.input_tokens : data.original_input_tokens;
224
+ if (typeof inputTokens !== "number") {
225
+ return void 0;
226
+ }
227
+ const tokensSaved = typeof data.tokens_saved === "number" ? data.tokens_saved : inputTokens - data.output_tokens;
228
+ const compressionRatio = typeof data.compression_ratio === "number" ? data.compression_ratio : data.output_tokens === 0 ? 0 : inputTokens / data.output_tokens;
229
+ return {
230
+ output: data.output,
231
+ output_tokens: data.output_tokens,
232
+ input_tokens: inputTokens,
233
+ tokens_saved: tokensSaved,
234
+ compression_ratio: compressionRatio
235
+ };
236
+ }
237
+ function createPromptCompressionFallback(input, provider = "heuristic", error) {
238
+ const original = stableStringifyInput(input);
239
+ const techniques = ["fallback-original"];
240
+ if (error) {
241
+ techniques.push("compression-error");
242
+ }
243
+ return buildResult(input, input, provider, original, original, techniques);
244
+ }
245
+ function buildResult(input, compressedInput, provider, original, compressed, techniques, tokenCounts) {
246
+ const originalCharacters = original.length;
247
+ const compressedCharacters = compressed.length;
248
+ const savedCharacters = Math.max(
249
+ 0,
250
+ originalCharacters - compressedCharacters
251
+ );
252
+ const originalTokens = tokenCounts?.originalTokens ?? estimatePromptTokens(original);
253
+ const compressedTokens = tokenCounts?.compressedTokens ?? estimatePromptTokens(compressed);
254
+ const savedTokens = Math.max(
255
+ 0,
256
+ tokenCounts?.savedTokens ?? originalTokens - compressedTokens
257
+ );
258
+ return {
259
+ input,
260
+ compressedInput,
261
+ provider,
262
+ originalCharacters,
263
+ compressedCharacters,
264
+ savedCharacters,
265
+ originalTokens,
266
+ compressedTokens,
267
+ savedTokens,
268
+ tokenSavingsRatio: originalTokens > 0 ? savedTokens / originalTokens : 0,
269
+ savingsRatio: originalCharacters > 0 ? savedCharacters / originalCharacters : 0,
270
+ techniques
271
+ };
272
+ }
273
+ function estimatePromptTokens(input) {
274
+ const text = typeof input === "string" ? input : stableStringifyInput(input);
275
+ return text.match(/[\p{L}\p{N}]+|[^\s]/gu)?.length ?? 0;
276
+ }
277
+ function compressValue(value, techniques, options) {
278
+ if (typeof value === "string") return compressText(value, techniques, options);
279
+ if (Array.isArray(value)) {
280
+ techniques.add("json-minify");
281
+ return value.map((item) => compressValue(item, techniques, options));
282
+ }
283
+ if (value && typeof value === "object") {
284
+ techniques.add("json-minify");
285
+ return Object.keys(value).reduce((acc, key) => {
286
+ const child = value[key];
287
+ if (child !== void 0) {
288
+ acc[key] = compressValue(child, techniques, options);
289
+ }
290
+ return acc;
291
+ }, {});
292
+ }
293
+ return value;
294
+ }
295
+ function compressText(value, techniques, options) {
296
+ const fencePattern = /```([\w-]+)?\n([\s\S]*?)```/g;
297
+ const parts = [];
298
+ let cursor = 0;
299
+ let match;
300
+ while ((match = fencePattern.exec(value)) !== null) {
301
+ const before = value.slice(cursor, match.index);
302
+ const compressedBefore = compressPlainTextAndEmbeddedJson(
303
+ before,
304
+ techniques,
305
+ options
306
+ );
307
+ if (compressedBefore) parts.push(compressedBefore);
308
+ const lang = match[1]?.toLowerCase();
309
+ const code = cleanCodeBlock(match[2] ?? "");
310
+ const compressedCode = lang === "json" ? compressJsonText(code, techniques, options) : void 0;
311
+ if (compressedCode?.format === "toon") {
312
+ parts.push(`\`\`\`toon
313
+ ${compressedCode.text}
314
+ \`\`\``);
315
+ } else if (compressedCode?.format === "json") {
316
+ parts.push(`\`\`\`json
317
+ ${compressedCode.text}
318
+ \`\`\``);
319
+ } else {
320
+ if (code !== match[2]) {
321
+ techniques.add("code-whitespace");
322
+ }
323
+ parts.push(lang ? `\`\`\`${lang}
324
+ ${code}
325
+ \`\`\`` : `\`\`\`
326
+ ${code}
327
+ \`\`\``);
328
+ }
329
+ cursor = match.index + match[0].length;
330
+ }
331
+ const after = compressPlainTextAndEmbeddedJson(value.slice(cursor), techniques, options);
332
+ if (after) parts.push(after);
333
+ return parts.join("\n").trim();
334
+ }
335
+ function compressPlainText(value, techniques) {
336
+ const compressed = value.split("\n").map((line) => line.trim()).filter((line) => line).join("\n").replace(/[ \t]{2,}/g, " ").trim();
337
+ if (compressed !== value.trim()) {
338
+ techniques.add("text-whitespace");
339
+ }
340
+ return compressed;
341
+ }
342
+ function compressPlainTextAndEmbeddedJson(value, techniques, options) {
343
+ const normalized = compressPlainText(value, techniques);
344
+ return compressEmbeddedJson(normalized, techniques, options);
345
+ }
346
+ function cleanCodeBlock(code) {
347
+ const lines = code.replace(/\r\n/g, "\n").split("\n");
348
+ while (lines.length && lines[0].trim() === "") lines.shift();
349
+ while (lines.length && lines[lines.length - 1].trim() === "") lines.pop();
350
+ const commonIndent = lines.filter((line) => line.trim()).reduce((min, line) => {
351
+ const indent = /^[ \t]*/.exec(line)?.[0].length ?? 0;
352
+ return min === void 0 ? indent : Math.min(min, indent);
353
+ }, void 0);
354
+ return lines.map((line) => commonIndent ? line.slice(commonIndent) : line).join("\n").replace(/[ \t]+$/gm, "");
355
+ }
356
+ function stableStringifyInput(input) {
357
+ if (typeof input === "string") return input;
358
+ return JSON.stringify(input) ?? String(input);
359
+ }
360
+ function compressJsonText(text, techniques, options) {
361
+ const parsed = safeParseJson(text);
362
+ if (parsed === void 0) {
363
+ return void 0;
364
+ }
365
+ const compactJson = JSON.stringify(parsed);
366
+ const candidates = [
367
+ { format: "json", text: compactJson }
368
+ ];
369
+ if (options.allowToonString || shouldUseToonForJson(parsed)) {
370
+ candidates.push({ format: "toon", text: encodeToon(parsed) });
371
+ }
372
+ const originalLength = text.trim().length;
373
+ const best = candidates.reduce(
374
+ (winner, candidate) => candidate.text.length < winner.text.length ? candidate : winner
375
+ );
376
+ if (best.text.length >= originalLength) {
377
+ return void 0;
378
+ }
379
+ techniques.add(best.format === "toon" ? "embedded-json-toon" : "embedded-json-minify");
380
+ return best;
381
+ }
382
+ function compressEmbeddedJson(text, techniques, options) {
383
+ let result = "";
384
+ let cursor = 0;
385
+ while (cursor < text.length) {
386
+ const start = findNextJsonStart(text, cursor);
387
+ if (start < 0) {
388
+ result += text.slice(cursor);
389
+ break;
390
+ }
391
+ result += text.slice(cursor, start);
392
+ const span = findBalancedJsonSpan(text, start);
393
+ if (!span) {
394
+ result += text[start];
395
+ cursor = start + 1;
396
+ continue;
397
+ }
398
+ const candidate = compressJsonText(span.text, techniques, options);
399
+ if (candidate) {
400
+ result += candidate.text;
401
+ } else {
402
+ result += span.text;
403
+ }
404
+ cursor = span.end;
405
+ }
406
+ return result;
407
+ }
408
+ function findNextJsonStart(text, from) {
409
+ const objectStart = text.indexOf("{", from);
410
+ const arrayStart = text.indexOf("[", from);
411
+ if (objectStart < 0) return arrayStart;
412
+ if (arrayStart < 0) return objectStart;
413
+ return Math.min(objectStart, arrayStart);
414
+ }
415
+ function findBalancedJsonSpan(text, start) {
416
+ const opener = text[start];
417
+ const closer = opener === "{" ? "}" : opener === "[" ? "]" : void 0;
418
+ if (!closer) return void 0;
419
+ const stack = [closer];
420
+ let inString = false;
421
+ let escaped = false;
422
+ for (let index = start + 1; index < text.length; index += 1) {
423
+ const char = text[index];
424
+ if (inString) {
425
+ if (escaped) {
426
+ escaped = false;
427
+ } else if (char === "\\") {
428
+ escaped = true;
429
+ } else if (char === '"') {
430
+ inString = false;
431
+ }
432
+ continue;
433
+ }
434
+ if (char === '"') {
435
+ inString = true;
436
+ continue;
437
+ }
438
+ if (char === "{" || char === "[") {
439
+ stack.push(char === "{" ? "}" : "]");
440
+ continue;
441
+ }
442
+ if (char === stack[stack.length - 1]) {
443
+ stack.pop();
444
+ if (!stack.length) {
445
+ const end = index + 1;
446
+ return { text: text.slice(start, end), end };
447
+ }
448
+ }
449
+ }
450
+ return void 0;
451
+ }
452
+ function safeParseJson(text) {
453
+ try {
454
+ return JSON.parse(text);
455
+ } catch {
456
+ return void 0;
457
+ }
458
+ }
459
+ function shouldUseToonForJson(value) {
460
+ if (Array.isArray(value)) {
461
+ return isUniformObjectArray(value) || value.some(shouldUseToonForJson);
462
+ }
463
+ if (isPlainObject(value)) {
464
+ return Object.values(value).some(shouldUseToonForJson);
465
+ }
466
+ return false;
467
+ }
468
+ function encodeToon(value, indent = 0) {
469
+ if (isPrimitive(value)) {
470
+ return scalarToToon(value);
471
+ }
472
+ if (Array.isArray(value)) {
473
+ return encodeArrayToon(value, indent);
474
+ }
475
+ if (isPlainObject(value)) {
476
+ const lines = [];
477
+ for (const [key, child] of Object.entries(value)) {
478
+ lines.push(...encodePropertyToon(key, child, indent));
479
+ }
480
+ return lines.join("\n");
481
+ }
482
+ return scalarToToon(String(value));
483
+ }
484
+ function encodePropertyToon(key, value, indent) {
485
+ const prefix = " ".repeat(indent);
486
+ const toonKey = keyToToon(key);
487
+ if (isPrimitive(value)) {
488
+ return [`${prefix}${toonKey}: ${scalarToToon(value)}`];
489
+ }
490
+ if (Array.isArray(value)) {
491
+ if (value.every(isPrimitive)) {
492
+ return [`${prefix}${toonKey}[${value.length}]: ${value.map(scalarToToon).join(",")}`];
493
+ }
494
+ if (isUniformObjectArray(value)) {
495
+ const fields = Object.keys(value[0]);
496
+ const header = `${prefix}${toonKey}[${value.length}]{${fields.map(keyToToon).join(",")}}:`;
497
+ const rows = value.map(
498
+ (item) => `${" ".repeat(indent + 2)}${fields.map(
499
+ (field) => scalarToToon(item[field])
500
+ ).join(",")}`
501
+ );
502
+ return [header, ...rows];
503
+ }
504
+ return [
505
+ `${prefix}${toonKey}[${value.length}]:`,
506
+ ...value.flatMap((item, index) => {
507
+ if (isPrimitive(item)) {
508
+ return [`${" ".repeat(indent + 2)}- ${scalarToToon(item)}`];
509
+ }
510
+ return [
511
+ `${" ".repeat(indent + 2)}- item${index}:`,
512
+ ...encodeToon(item, indent + 4).split("\n")
513
+ ];
514
+ })
515
+ ];
516
+ }
517
+ return [`${prefix}${toonKey}:`, ...encodeToon(value, indent + 2).split("\n")];
518
+ }
519
+ function encodeArrayToon(value, indent) {
520
+ if (value.every(isPrimitive)) {
521
+ return `[${value.length}]: ${value.map(scalarToToon).join(",")}`;
522
+ }
523
+ if (isUniformObjectArray(value)) {
524
+ const fields = Object.keys(value[0]);
525
+ return [
526
+ `[${value.length}]{${fields.map(keyToToon).join(",")}}:`,
527
+ ...value.map(
528
+ (item) => `${" ".repeat(indent + 2)}${fields.map(
529
+ (field) => scalarToToon(item[field])
530
+ ).join(",")}`
531
+ )
532
+ ].join("\n");
533
+ }
534
+ return value.flatMap((item, index) => [
535
+ `${" ".repeat(indent)}- item${index}:`,
536
+ ...encodeToon(item, indent + 2).split("\n")
537
+ ]).join("\n");
538
+ }
539
+ function isUniformObjectArray(value) {
540
+ if (!value.length || !value.every(isPlainObject)) {
541
+ return false;
542
+ }
543
+ const fields = Object.keys(value[0]);
544
+ if (!fields.length) {
545
+ return false;
546
+ }
547
+ return value.every((item) => {
548
+ const record = item;
549
+ const itemFields = Object.keys(record);
550
+ return itemFields.length === fields.length && fields.every((field) => itemFields.includes(field) && isPrimitive(record[field]));
551
+ });
552
+ }
553
+ function isPlainObject(value) {
554
+ return typeof value === "object" && value !== null && !Array.isArray(value);
555
+ }
556
+ function isPrimitive(value) {
557
+ return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
558
+ }
559
+ function keyToToon(key) {
560
+ return /^[A-Za-z_][A-Za-z0-9_-]*$/.test(key) ? key : JSON.stringify(key);
561
+ }
562
+ function scalarToToon(value) {
563
+ if (value === null) return "null";
564
+ if (typeof value === "number" || typeof value === "boolean") {
565
+ return String(value);
566
+ }
567
+ const text = String(value);
568
+ if (text && !/^(true|false|null|-?\d+(?:\.\d+)?)$/i.test(text) && /^[A-Za-z0-9_./@-]+(?: [A-Za-z0-9_./@-]+)*$/.test(text)) {
569
+ return text;
570
+ }
571
+ return JSON.stringify(text);
572
+ }
573
+
116
574
  // src/client.ts
117
575
  var CALL_BEGIN_PATH = "call_begin";
118
576
  var CALL_END_PATH = "call_end";
577
+ var COMPRESS_PROMPT_PATH = "compress_prompt";
119
578
  var CHECK_USAGE_PATH = "customers/{customerId}/usage";
120
579
  var CREATE_CUSTOMER_PATH = "customers";
121
580
  var CHANGE_PLAN_PATH = "customers/{customerId}/change_plan";
@@ -127,7 +586,7 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
127
586
  var SDK_HEADER = "x-usage-sdk";
128
587
  var USER_AGENT = "UsageTapClient";
129
588
  var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
130
- var SDK_VERSION = "0.10.0" ;
589
+ var SDK_VERSION = "1.0.0" ;
131
590
  var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
132
591
  var UsageTapClient = class {
133
592
  apiKey;
@@ -142,6 +601,8 @@ var UsageTapClient = class {
142
601
  metricFn;
143
602
  authHeader;
144
603
  autoIdempotency;
604
+ tokenCompanyApiKey;
605
+ tokenCompanyEndpoint;
145
606
  constructor(options) {
146
607
  if (!options) {
147
608
  throw new UsageTapError(
@@ -187,6 +648,8 @@ var UsageTapClient = class {
187
648
  this.metricFn = options.onUsageMetric;
188
649
  this.authHeader = options.useApiKeyHeader ? API_KEY_HEADER : AUTH_HEADER;
189
650
  this.autoIdempotency = options.autoIdempotency ?? true;
651
+ this.tokenCompanyApiKey = options.tokenCompanyApiKey;
652
+ this.tokenCompanyEndpoint = options.tokenCompanyEndpoint;
190
653
  }
191
654
  async beginCall(request, options = {}) {
192
655
  const idempotencyKey = request.idempotencyKey ?? request.idempotency ?? (this.autoIdempotency ? this.idempotencyGenerator() : void 0);
@@ -209,6 +672,42 @@ var UsageTapClient = class {
209
672
  );
210
673
  return response;
211
674
  }
675
+ async promptCompress(request, options = {}) {
676
+ if (!request?.callId) {
677
+ throw new UsageTapError(
678
+ "USAGETAP_BAD_REQUEST",
679
+ "promptCompress requires callId"
680
+ );
681
+ }
682
+ const result = await compressPrompt({
683
+ input: request.input,
684
+ provider: request.provider,
685
+ tokenCompanyApiKey: this.tokenCompanyApiKey,
686
+ tokenCompanyEndpoint: this.tokenCompanyEndpoint,
687
+ fetchImpl: this.fetchImpl,
688
+ signal: options.signal
689
+ });
690
+ try {
691
+ await this.request(
692
+ COMPRESS_PROMPT_PATH,
693
+ {
694
+ callId: request.callId,
695
+ promptCompression: this.toPromptCompressionTelemetry(result)
696
+ },
697
+ options
698
+ );
699
+ return { ...result, callId: request.callId };
700
+ } catch (error) {
701
+ return {
702
+ ...createPromptCompressionFallback(
703
+ request.input,
704
+ request.provider ?? result.provider,
705
+ error
706
+ ),
707
+ callId: request.callId
708
+ };
709
+ }
710
+ }
212
711
  async endCall(request, options = {}) {
213
712
  if (!request?.callId) {
214
713
  throw new UsageTapError(
@@ -433,6 +932,20 @@ var UsageTapClient = class {
433
932
  }
434
933
  return handlerResult;
435
934
  }
935
+ toPromptCompressionTelemetry(result) {
936
+ return {
937
+ provider: result.provider,
938
+ originalCharacters: result.originalCharacters,
939
+ compressedCharacters: result.compressedCharacters,
940
+ savedCharacters: result.savedCharacters,
941
+ originalTokens: result.originalTokens,
942
+ compressedTokens: result.compressedTokens,
943
+ savedTokens: result.savedTokens,
944
+ tokenSavingsRatio: result.tokenSavingsRatio,
945
+ savingsRatio: result.savingsRatio,
946
+ techniques: result.techniques
947
+ };
948
+ }
436
949
  async request(path, payload, options) {
437
950
  const url = new URL(path, this.baseUrl).toString();
438
951
  const body = payload !== void 0 ? JSON.stringify(payload) : void 0;
@@ -1020,6 +1533,6 @@ async function finalizeCall(callState, usageTap, error, usage) {
1020
1533
  }
1021
1534
  }
1022
1535
 
1023
- export { UsageTapClient, UsageTapError, createIdempotencyKey, isUsageTapError, wrapFetch };
1536
+ export { UsageTapClient, UsageTapError, compressPrompt, compressPromptHeuristic, compressPromptToon, createIdempotencyKey, estimatePromptTokens, isUsageTapError, wrapFetch };
1024
1537
  //# sourceMappingURL=index.mjs.map
1025
1538
  //# sourceMappingURL=index.mjs.map