@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.
Files changed (52) hide show
  1. package/README.md +710 -735
  2. package/dist/adapters/anthropic.cjs +1609 -5
  3. package/dist/adapters/anthropic.cjs.map +1 -1
  4. package/dist/adapters/anthropic.d.cts +30 -2
  5. package/dist/adapters/anthropic.d.ts +30 -2
  6. package/dist/adapters/anthropic.mjs +1608 -6
  7. package/dist/adapters/anthropic.mjs.map +1 -1
  8. package/dist/adapters/openai.cjs +1659 -5
  9. package/dist/adapters/openai.cjs.map +1 -1
  10. package/dist/adapters/openai.d.cts +37 -2
  11. package/dist/adapters/openai.d.ts +37 -2
  12. package/dist/adapters/openai.mjs +1658 -6
  13. package/dist/adapters/openai.mjs.map +1 -1
  14. package/dist/adapters/openrouter.cjs.map +1 -1
  15. package/dist/adapters/openrouter.d.cts +1 -1
  16. package/dist/adapters/openrouter.d.ts +1 -1
  17. package/dist/adapters/openrouter.mjs.map +1 -1
  18. package/dist/anthropic/index.cjs +1609 -5
  19. package/dist/anthropic/index.cjs.map +1 -1
  20. package/dist/anthropic/index.d.cts +2 -2
  21. package/dist/anthropic/index.d.ts +2 -2
  22. package/dist/anthropic/index.mjs +1608 -6
  23. package/dist/anthropic/index.mjs.map +1 -1
  24. package/dist/{client-EMXt9_fA.d.cts → client-BD8O2J8Z.d.cts} +30 -4
  25. package/dist/{client-EMXt9_fA.d.ts → client-BD8O2J8Z.d.ts} +30 -4
  26. package/dist/express/index.cjs +19 -0
  27. package/dist/express/index.cjs.map +1 -1
  28. package/dist/express/index.d.cts +1 -1
  29. package/dist/express/index.d.ts +1 -1
  30. package/dist/express/index.mjs +19 -0
  31. package/dist/express/index.mjs.map +1 -1
  32. package/dist/index.cjs +48 -19
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +3 -3
  35. package/dist/index.d.ts +3 -3
  36. package/dist/index.mjs +48 -20
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/openai/index.cjs +1659 -5
  39. package/dist/openai/index.cjs.map +1 -1
  40. package/dist/openai/index.d.cts +2 -2
  41. package/dist/openai/index.d.ts +2 -2
  42. package/dist/openai/index.mjs +1658 -6
  43. package/dist/openai/index.mjs.map +1 -1
  44. package/dist/openrouter/index.cjs +3024 -0
  45. package/dist/openrouter/index.cjs.map +1 -0
  46. package/dist/openrouter/index.d.cts +4 -0
  47. package/dist/openrouter/index.d.ts +4 -0
  48. package/dist/openrouter/index.mjs +3019 -0
  49. package/dist/openrouter/index.mjs.map +1 -0
  50. package/dist/react/index.cjs.map +1 -1
  51. package/dist/react/index.mjs.map +1 -1
  52. package/package.json +182 -124
package/dist/index.cjs CHANGED
@@ -770,7 +770,8 @@ var IDEMPOTENCY_HEADER = "idempotency-key";
770
770
  var SDK_HEADER = "x-usage-sdk";
771
771
  var USER_AGENT = "UsageTapClient";
772
772
  var CANONICAL_MEDIA_TYPE = "application/vnd.usagetap.v1+json";
773
- var SDK_VERSION = "1.2.0" ;
773
+ var DEFAULT_BASE_URL = "https://api.usagetap.com";
774
+ var SDK_VERSION = "1.3.2" ;
774
775
  var HAS_WINDOW = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
775
776
  var UsageTapClient = class {
776
777
  apiKey;
@@ -797,23 +798,13 @@ var UsageTapClient = class {
797
798
  usageTapCompressionMessagesEndpoint;
798
799
  usageTapCompressionModel;
799
800
  usageTapCompressionAggressiveness;
800
- constructor(options) {
801
- if (!options) {
801
+ constructor(options = {}) {
802
+ const apiKey = options.apiKey?.trim() || readEnvironmentVariable("USAGETAP_API_KEY");
803
+ const baseUrl = options.baseUrl?.trim() || readEnvironmentVariable("USAGETAP_BASE_URL") || DEFAULT_BASE_URL;
804
+ if (!apiKey) {
802
805
  throw new UsageTapError(
803
806
  "USAGETAP_BAD_REQUEST",
804
- "UsageTapClient options are required"
805
- );
806
- }
807
- if (!options.apiKey) {
808
- throw new UsageTapError(
809
- "USAGETAP_BAD_REQUEST",
810
- "UsageTapClient requires an apiKey"
811
- );
812
- }
813
- if (!options.baseUrl) {
814
- throw new UsageTapError(
815
- "USAGETAP_BAD_REQUEST",
816
- "UsageTapClient requires a baseUrl"
807
+ "UsageTapClient requires an apiKey or the USAGETAP_API_KEY environment variable"
817
808
  );
818
809
  }
819
810
  if (HAS_WINDOW && !options.allowBrowser) {
@@ -829,9 +820,9 @@ var UsageTapClient = class {
829
820
  "A global fetch implementation was not found. Pass fetchImpl in UsageTapClientOptions."
830
821
  );
831
822
  }
832
- const normalizedBaseUrl = normalizeBaseUrl(options.baseUrl);
823
+ const normalizedBaseUrl = normalizeBaseUrl(baseUrl);
833
824
  this.baseUrl = new URL(normalizedBaseUrl);
834
- this.apiKey = options.apiKey;
825
+ this.apiKey = apiKey;
835
826
  this.fetchImpl = wrapFetchImplementation(fetchCandidate, !options.fetchImpl);
836
827
  this.defaultFeature = options.defaultFeature;
837
828
  this.defaultTags = options.defaultTags?.length ? dedupeStrings(options.defaultTags) : void 0;
@@ -849,7 +840,7 @@ var UsageTapClient = class {
849
840
  this.aggressiveness = options.aggressiveness;
850
841
  this.tokenCompanyAggressiveness = options.tokenCompanyAggressiveness;
851
842
  this.tokenCompanyAppId = options.tokenCompanyAppId;
852
- this.usageTapCompressionApiKey = options.usageTapCompressionApiKey ?? options.apiKey;
843
+ this.usageTapCompressionApiKey = options.usageTapCompressionApiKey ?? apiKey;
853
844
  this.usageTapCompressionEndpoint = options.usageTapCompressionEndpoint;
854
845
  this.usageTapCompressionMessagesEndpoint = options.usageTapCompressionMessagesEndpoint;
855
846
  this.usageTapCompressionModel = options.usageTapCompressionModel;
@@ -941,6 +932,30 @@ var UsageTapClient = class {
941
932
  failOpen: options.failOpen
942
933
  });
943
934
  }
935
+ /**
936
+ * Compress text with UsageTap's hosted compression service.
937
+ *
938
+ * This is the short, standalone path. It does not create a metered call and
939
+ * fails open to the original text unless failOpen is explicitly disabled.
940
+ */
941
+ async compress(text, options = {}) {
942
+ if (typeof text !== "string") {
943
+ throw new UsageTapError(
944
+ "USAGETAP_BAD_REQUEST",
945
+ "compress requires text"
946
+ );
947
+ }
948
+ const result = await this.compressPromptInput(text, {
949
+ ...options,
950
+ provider: "usagetap"
951
+ });
952
+ const output = typeof result.compressedInput === "string" ? result.compressedInput : text;
953
+ return {
954
+ ...result,
955
+ compressedInput: output,
956
+ output
957
+ };
958
+ }
944
959
  async compressPromptMessages(input, options = {}) {
945
960
  return compressPromptMessages({
946
961
  input,
@@ -1194,6 +1209,14 @@ var UsageTapClient = class {
1194
1209
  }
1195
1210
  return handlerResult;
1196
1211
  }
1212
+ /**
1213
+ * Meter one operation. Pass only a customer ID for the common path, or the
1214
+ * existing begin-call request object when feature, tags, or entitlements are needed.
1215
+ */
1216
+ async meter(request, handler, options = {}) {
1217
+ const beginRequest = typeof request === "string" ? { customerId: request } : request;
1218
+ return this.withUsage(beginRequest, handler, options);
1219
+ }
1197
1220
  toPromptCompressionTelemetry(result) {
1198
1221
  return {
1199
1222
  provider: result.provider,
@@ -1506,6 +1529,11 @@ function sanitizeDetails(payload) {
1506
1529
  if (payload.error) details.error = payload.error;
1507
1530
  return Object.keys(details).length ? details : void 0;
1508
1531
  }
1532
+ function readEnvironmentVariable(name) {
1533
+ const runtime = globalThis;
1534
+ const value = runtime.process?.env?.[name]?.trim();
1535
+ return value || void 0;
1536
+ }
1509
1537
  function normalizeBaseUrl(baseUrl) {
1510
1538
  const trimmed = baseUrl.trim();
1511
1539
  if (!trimmed) return trimmed;
@@ -1795,6 +1823,7 @@ async function finalizeCall(callState, usageTap, error, usage) {
1795
1823
  }
1796
1824
  }
1797
1825
 
1826
+ exports.UsageTap = UsageTapClient;
1798
1827
  exports.UsageTapClient = UsageTapClient;
1799
1828
  exports.UsageTapError = UsageTapError;
1800
1829
  exports.compressPrompt = compressPrompt;