@diogonzafe/tokenwatch 0.8.0 → 0.9.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.
@@ -1,4 +1,4 @@
1
- import { d as IStorage, U as UsageEntry } from './index-CFBI-1ab.cjs';
1
+ import { d as IStorage, U as UsageEntry } from './index-fD5QLTWg.cjs';
2
2
 
3
3
  /**
4
4
  * IStorage adapter for PostgreSQL using the `pg` driver.
@@ -1,4 +1,4 @@
1
- import { d as IStorage, U as UsageEntry } from './index-CFBI-1ab.js';
1
+ import { d as IStorage, U as UsageEntry } from './index-fD5QLTWg.js';
2
2
 
3
3
  /**
4
4
  * IStorage adapter for PostgreSQL using the `pg` driver.
package/dist/cli.js CHANGED
@@ -509,6 +509,40 @@ function calculateCost(inputTokens, outputTokens, price, cachedTokens = 0, cache
509
509
  return regularInputCost + cachedReadCost + cacheCreationCost + outputCost;
510
510
  }
511
511
 
512
+ // src/exporters/cloud.ts
513
+ var DEFAULT_ENDPOINT = "https://api.tokenwatch.dev/v1/ingest";
514
+ var CloudExporter = class {
515
+ constructor(apiKey, endpoint) {
516
+ this.apiKey = apiKey;
517
+ this.endpoint = endpoint ?? DEFAULT_ENDPOINT;
518
+ }
519
+ apiKey;
520
+ endpoint;
521
+ export(entry) {
522
+ fetch(this.endpoint, {
523
+ method: "POST",
524
+ headers: {
525
+ "Content-Type": "application/json",
526
+ Authorization: `Bearer ${this.apiKey}`
527
+ },
528
+ body: JSON.stringify({
529
+ model: entry.model,
530
+ inputTokens: entry.inputTokens,
531
+ outputTokens: entry.outputTokens,
532
+ reasoningTokens: entry.reasoningTokens ?? 0,
533
+ cachedTokens: entry.cachedTokens ?? 0,
534
+ cacheCreationTokens: entry.cacheCreationTokens ?? 0,
535
+ costUSD: entry.costUSD,
536
+ sessionId: entry.sessionId,
537
+ userId: entry.userId,
538
+ feature: entry.feature,
539
+ timestamp: entry.timestamp
540
+ })
541
+ }).catch(() => {
542
+ });
543
+ }
544
+ };
545
+
512
546
  // src/core/suggestions.ts
513
547
  var PROVIDER_PREFIXES = ["gpt-", "claude-", "gemini-", "deepseek-"];
514
548
  function getProviderPrefix(model) {
@@ -1994,7 +2028,9 @@ var TrackerConfigSchema = z.object({
1994
2028
  mode: z.enum(["once", "always"]).optional().default("once")
1995
2029
  }).optional(),
1996
2030
  exporter: z.custom((v) => v !== null && typeof v === "object" && typeof v.export === "function").optional(),
1997
- appId: z.string().optional()
2031
+ appId: z.string().optional(),
2032
+ cloudApiKey: z.string().optional(),
2033
+ cloudEndpoint: z.string().url().optional()
1998
2034
  });
1999
2035
  function createTracker(config = {}) {
2000
2036
  const parsed = TrackerConfigSchema.safeParse(config);
@@ -2014,9 +2050,12 @@ ${issues}`);
2014
2050
  suggestions,
2015
2051
  anomalyDetection,
2016
2052
  exporter,
2017
- appId
2053
+ appId,
2054
+ cloudApiKey,
2055
+ cloudEndpoint
2018
2056
  } = parsed.data;
2019
2057
  const storage = typeof storageOption === "object" ? storageOption : createStorage(storageOption);
2058
+ const cloudExporter = cloudApiKey ? new CloudExporter(cloudApiKey, cloudEndpoint) : null;
2020
2059
  let remotePrices;
2021
2060
  let pricesUpdatedAt = bundledUpdatedAt;
2022
2061
  if (syncPrices) {
@@ -2077,6 +2116,9 @@ ${issues}`);
2077
2116
  Promise.resolve(exporter.export(full)).catch(() => {
2078
2117
  });
2079
2118
  }
2119
+ if (cloudExporter) {
2120
+ cloudExporter.export(full);
2121
+ }
2080
2122
  maybeFireAlerts(full);
2081
2123
  if (anomalyDetection) maybeDetectAnomaly(full);
2082
2124
  if (suggestions) {