ccusage 12.3.2 → 13.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.
@@ -1,27 +1,3 @@
1
- import { logger } from "./logger-BfzGx5lS.js";
2
- import { createRequire } from "node:module";
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
- key = keys[i];
13
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
21
- value: mod,
22
- enumerable: true
23
- }) : target, mod));
24
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
25
1
  var util;
26
2
  (function(util$1) {
27
3
  util$1.assertEqual = (_) => {};
@@ -3587,11 +3563,26 @@ const nullableType = ZodNullable.create;
3587
3563
  const preprocessType = ZodEffects.createWithPreprocess;
3588
3564
  const pipelineType = ZodPipeline.create;
3589
3565
  /**
3590
- * URL for LiteLLM's model pricing and context window data
3566
+ * Branded Zod schemas for type safety using Zod's built-in brand functionality
3591
3567
  */
3592
- const LITELLM_PRICING_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
3568
+ const modelNameSchema = stringType().min(1, "Model name cannot be empty").brand();
3569
+ const sessionIdSchema = stringType().min(1, "Session ID cannot be empty").brand();
3570
+ const requestIdSchema = stringType().min(1, "Request ID cannot be empty").brand();
3571
+ const messageIdSchema = stringType().min(1, "Message ID cannot be empty").brand();
3572
+ const isoTimestampSchema = stringType().regex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/, "Invalid ISO timestamp").brand();
3573
+ const dailyDateSchema = stringType().regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format").brand();
3574
+ const activityDateSchema = stringType().regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format").brand();
3575
+ const monthlyDateSchema = stringType().regex(/^\d{4}-\d{2}$/, "Date must be in YYYY-MM format").brand();
3576
+ const filterDateSchema = stringType().regex(/^\d{8}$/, "Date must be in YYYYMMDD format").brand();
3577
+ const projectPathSchema = stringType().min(1, "Project path cannot be empty").brand();
3578
+ const versionSchema = stringType().regex(/^\d+\.\d+\.\d+/, "Invalid version format").brand();
3579
+ const createSessionId = (value) => sessionIdSchema.parse(value);
3580
+ const createDailyDate = (value) => dailyDateSchema.parse(value);
3581
+ const createMonthlyDate = (value) => monthlyDateSchema.parse(value);
3582
+ const createProjectPath = (value) => projectPathSchema.parse(value);
3593
3583
  /**
3594
- * Zod schema for validating date strings in YYYYMMDD format
3584
+ * Legacy schema for backward compatibility
3585
+ * @deprecated Use filterDateSchema instead for better type safety
3595
3586
  */
3596
3587
  const dateSchema = stringType().regex(/^\d{8}$/, "Date must be in YYYYMMDD format");
3597
3588
  /**
@@ -3618,288 +3609,4 @@ const modelPricingSchema = objectType({
3618
3609
  cache_creation_input_token_cost: numberType().optional(),
3619
3610
  cache_read_input_token_cost: numberType().optional()
3620
3611
  });
3621
- var require_usingCtx = __commonJSMin((exports, module) => {
3622
- function _usingCtx() {
3623
- var r = "function" == typeof SuppressedError ? SuppressedError : function(r$1, e$1) {
3624
- var n$1 = Error();
3625
- return n$1.name = "SuppressedError", n$1.error = r$1, n$1.suppressed = e$1, n$1;
3626
- }, e = {}, n = [];
3627
- function using(r$1, e$1) {
3628
- if (null != e$1) {
3629
- if (Object(e$1) !== e$1) throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
3630
- if (r$1) var o = e$1[Symbol.asyncDispose || Symbol["for"]("Symbol.asyncDispose")];
3631
- if (void 0 === o && (o = e$1[Symbol.dispose || Symbol["for"]("Symbol.dispose")], r$1)) var t = o;
3632
- if ("function" != typeof o) throw new TypeError("Object is not disposable.");
3633
- t && (o = function o$1() {
3634
- try {
3635
- t.call(e$1);
3636
- } catch (r$2) {
3637
- return Promise.reject(r$2);
3638
- }
3639
- }), n.push({
3640
- v: e$1,
3641
- d: o,
3642
- a: r$1
3643
- });
3644
- } else r$1 && n.push({
3645
- d: e$1,
3646
- a: r$1
3647
- });
3648
- return e$1;
3649
- }
3650
- return {
3651
- e,
3652
- u: using.bind(null, !1),
3653
- a: using.bind(null, !0),
3654
- d: function d() {
3655
- var o, t = this.e, s = 0;
3656
- function next() {
3657
- for (; o = n.pop();) try {
3658
- if (!o.a && 1 === s) return s = 0, n.push(o), Promise.resolve().then(next);
3659
- if (o.d) {
3660
- var r$1 = o.d.call(o.v);
3661
- if (o.a) return s |= 2, Promise.resolve(r$1).then(next, err);
3662
- } else s |= 1;
3663
- } catch (r$2) {
3664
- return err(r$2);
3665
- }
3666
- if (1 === s) return t !== e ? Promise.reject(t) : Promise.resolve();
3667
- if (t !== e) throw t;
3668
- }
3669
- function err(n$1) {
3670
- return t = t !== e ? new r(n$1, t) : n$1, next();
3671
- }
3672
- return next();
3673
- }
3674
- };
3675
- }
3676
- module.exports = _usingCtx, module.exports.__esModule = true, module.exports["default"] = module.exports;
3677
- });
3678
- var import_usingCtx = __toESM(require_usingCtx(), 1);
3679
- /**
3680
- * Fetches and caches model pricing information from LiteLLM
3681
- * Implements Disposable pattern for automatic resource cleanup
3682
- */
3683
- var PricingFetcher = class {
3684
- cachedPricing = null;
3685
- offline;
3686
- /**
3687
- * Creates a new PricingFetcher instance
3688
- * @param offline - Whether to use pre-fetched pricing data instead of fetching from API
3689
- */
3690
- constructor(offline = false) {
3691
- this.offline = offline;
3692
- }
3693
- /**
3694
- * Implements Disposable interface for automatic cleanup
3695
- */
3696
- [Symbol.dispose]() {
3697
- this.clearCache();
3698
- }
3699
- /**
3700
- * Clears the cached pricing data
3701
- */
3702
- clearCache() {
3703
- this.cachedPricing = null;
3704
- }
3705
- /**
3706
- * Ensures pricing data is loaded, either from cache or by fetching
3707
- * @returns Map of model names to pricing information
3708
- */
3709
- async ensurePricingLoaded() {
3710
- if (this.cachedPricing != null) return this.cachedPricing;
3711
- if (this.offline) {
3712
- const pricing = new Map(Object.entries({
3713
- "claude-instant-1": {
3714
- "input_cost_per_token": 163e-8,
3715
- "output_cost_per_token": 551e-8
3716
- },
3717
- "claude-instant-1.2": {
3718
- "input_cost_per_token": 163e-9,
3719
- "output_cost_per_token": 551e-9
3720
- },
3721
- "claude-2": {
3722
- "input_cost_per_token": 8e-6,
3723
- "output_cost_per_token": 24e-6
3724
- },
3725
- "claude-2.1": {
3726
- "input_cost_per_token": 8e-6,
3727
- "output_cost_per_token": 24e-6
3728
- },
3729
- "claude-3-haiku-20240307": {
3730
- "input_cost_per_token": 25e-8,
3731
- "output_cost_per_token": 125e-8,
3732
- "cache_creation_input_token_cost": 3e-7,
3733
- "cache_read_input_token_cost": 3e-8
3734
- },
3735
- "claude-3-5-haiku-20241022": {
3736
- "input_cost_per_token": 8e-7,
3737
- "output_cost_per_token": 4e-6,
3738
- "cache_creation_input_token_cost": 1e-6,
3739
- "cache_read_input_token_cost": 8e-8
3740
- },
3741
- "claude-3-5-haiku-latest": {
3742
- "input_cost_per_token": 1e-6,
3743
- "output_cost_per_token": 5e-6,
3744
- "cache_creation_input_token_cost": 125e-8,
3745
- "cache_read_input_token_cost": 1e-7
3746
- },
3747
- "claude-3-opus-latest": {
3748
- "input_cost_per_token": 15e-6,
3749
- "output_cost_per_token": 75e-6,
3750
- "cache_creation_input_token_cost": 1875e-8,
3751
- "cache_read_input_token_cost": 15e-7
3752
- },
3753
- "claude-3-opus-20240229": {
3754
- "input_cost_per_token": 15e-6,
3755
- "output_cost_per_token": 75e-6,
3756
- "cache_creation_input_token_cost": 1875e-8,
3757
- "cache_read_input_token_cost": 15e-7
3758
- },
3759
- "claude-3-sonnet-20240229": {
3760
- "input_cost_per_token": 3e-6,
3761
- "output_cost_per_token": 15e-6
3762
- },
3763
- "claude-3-5-sonnet-latest": {
3764
- "input_cost_per_token": 3e-6,
3765
- "output_cost_per_token": 15e-6,
3766
- "cache_creation_input_token_cost": 375e-8,
3767
- "cache_read_input_token_cost": 3e-7
3768
- },
3769
- "claude-3-5-sonnet-20240620": {
3770
- "input_cost_per_token": 3e-6,
3771
- "output_cost_per_token": 15e-6,
3772
- "cache_creation_input_token_cost": 375e-8,
3773
- "cache_read_input_token_cost": 3e-7
3774
- },
3775
- "claude-opus-4-20250514": {
3776
- "input_cost_per_token": 15e-6,
3777
- "output_cost_per_token": 75e-6,
3778
- "cache_creation_input_token_cost": 1875e-8,
3779
- "cache_read_input_token_cost": 15e-7
3780
- },
3781
- "claude-sonnet-4-20250514": {
3782
- "input_cost_per_token": 3e-6,
3783
- "output_cost_per_token": 15e-6,
3784
- "cache_creation_input_token_cost": 375e-8,
3785
- "cache_read_input_token_cost": 3e-7
3786
- },
3787
- "claude-4-opus-20250514": {
3788
- "input_cost_per_token": 15e-6,
3789
- "output_cost_per_token": 75e-6,
3790
- "cache_creation_input_token_cost": 1875e-8,
3791
- "cache_read_input_token_cost": 15e-7
3792
- },
3793
- "claude-4-sonnet-20250514": {
3794
- "input_cost_per_token": 3e-6,
3795
- "output_cost_per_token": 15e-6,
3796
- "cache_creation_input_token_cost": 375e-8,
3797
- "cache_read_input_token_cost": 3e-7
3798
- },
3799
- "claude-3-7-sonnet-latest": {
3800
- "input_cost_per_token": 3e-6,
3801
- "output_cost_per_token": 15e-6,
3802
- "cache_creation_input_token_cost": 375e-8,
3803
- "cache_read_input_token_cost": 3e-7
3804
- },
3805
- "claude-3-7-sonnet-20250219": {
3806
- "input_cost_per_token": 3e-6,
3807
- "output_cost_per_token": 15e-6,
3808
- "cache_creation_input_token_cost": 375e-8,
3809
- "cache_read_input_token_cost": 3e-7
3810
- },
3811
- "claude-3-5-sonnet-20241022": {
3812
- "input_cost_per_token": 3e-6,
3813
- "output_cost_per_token": 15e-6,
3814
- "cache_creation_input_token_cost": 375e-8,
3815
- "cache_read_input_token_cost": 3e-7
3816
- }
3817
- }));
3818
- this.cachedPricing = pricing;
3819
- return pricing;
3820
- }
3821
- try {
3822
- logger.warn("Fetching latest model pricing from LiteLLM...");
3823
- const response = await fetch(LITELLM_PRICING_URL);
3824
- if (!response.ok) throw new Error(`Failed to fetch pricing data: ${response.statusText}`);
3825
- const data = await response.json();
3826
- const pricing = /* @__PURE__ */ new Map();
3827
- for (const [modelName, modelData] of Object.entries(data)) if (typeof modelData === "object" && modelData !== null) {
3828
- const parsed = modelPricingSchema.safeParse(modelData);
3829
- if (parsed.success) pricing.set(modelName, parsed.data);
3830
- }
3831
- this.cachedPricing = pricing;
3832
- logger.info(`Loaded pricing for ${pricing.size} models`);
3833
- return pricing;
3834
- } catch (error) {
3835
- logger.error("Failed to fetch model pricing:", error);
3836
- throw new Error("Could not fetch model pricing data");
3837
- }
3838
- }
3839
- /**
3840
- * Fetches all available model pricing data
3841
- * @returns Map of model names to pricing information
3842
- */
3843
- async fetchModelPricing() {
3844
- return this.ensurePricingLoaded();
3845
- }
3846
- /**
3847
- * Gets pricing information for a specific model with fallback matching
3848
- * Tries exact match first, then provider prefixes, then partial matches
3849
- * @param modelName - Name of the model to get pricing for
3850
- * @returns Model pricing information or null if not found
3851
- */
3852
- async getModelPricing(modelName) {
3853
- const pricing = await this.ensurePricingLoaded();
3854
- const directMatch = pricing.get(modelName);
3855
- if (directMatch != null) return directMatch;
3856
- const variations = [
3857
- modelName,
3858
- `anthropic/${modelName}`,
3859
- `claude-3-5-${modelName}`,
3860
- `claude-3-${modelName}`,
3861
- `claude-${modelName}`
3862
- ];
3863
- for (const variant of variations) {
3864
- const match = pricing.get(variant);
3865
- if (match != null) return match;
3866
- }
3867
- const lowerModel = modelName.toLowerCase();
3868
- for (const [key, value] of pricing) if (key.toLowerCase().includes(lowerModel) || lowerModel.includes(key.toLowerCase())) return value;
3869
- return null;
3870
- }
3871
- /**
3872
- * Calculates the cost for given token usage and model
3873
- * @param tokens - Token usage breakdown
3874
- * @param tokens.input_tokens - Number of input tokens
3875
- * @param tokens.output_tokens - Number of output tokens
3876
- * @param tokens.cache_creation_input_tokens - Number of cache creation tokens
3877
- * @param tokens.cache_read_input_tokens - Number of cache read tokens
3878
- * @param modelName - Name of the model used
3879
- * @returns Total cost in USD
3880
- */
3881
- async calculateCostFromTokens(tokens, modelName) {
3882
- const pricing = await this.getModelPricing(modelName);
3883
- if (pricing == null) return 0;
3884
- return this.calculateCostFromPricing(tokens, pricing);
3885
- }
3886
- /**
3887
- * Calculates cost from token usage and pricing information
3888
- * @param tokens - Token usage breakdown
3889
- * @param tokens.input_tokens - Number of input tokens
3890
- * @param tokens.output_tokens - Number of output tokens
3891
- * @param tokens.cache_creation_input_tokens - Number of cache creation tokens
3892
- * @param tokens.cache_read_input_tokens - Number of cache read tokens
3893
- * @param pricing - Model pricing rates
3894
- * @returns Total cost in USD
3895
- */
3896
- calculateCostFromPricing(tokens, pricing) {
3897
- let cost = 0;
3898
- if (pricing.input_cost_per_token != null) cost += tokens.input_tokens * pricing.input_cost_per_token;
3899
- if (pricing.output_cost_per_token != null) cost += tokens.output_tokens * pricing.output_cost_per_token;
3900
- if (tokens.cache_creation_input_tokens != null && pricing.cache_creation_input_token_cost != null) cost += tokens.cache_creation_input_tokens * pricing.cache_creation_input_token_cost;
3901
- if (tokens.cache_read_input_tokens != null && pricing.cache_read_input_token_cost != null) cost += tokens.cache_read_input_tokens * pricing.cache_read_input_token_cost;
3902
- return cost;
3903
- }
3904
- };
3905
- export { CostModes, PricingFetcher, SortOrders, ZodFirstPartyTypeKind, ZodOptional, ZodType, __commonJSMin, __require, __toESM, arrayType, booleanType, dateSchema, discriminatedUnionType, enumType, literalType, numberType, objectType, optionalType, recordType, require_usingCtx, stringType, unionType, unknownType };
3612
+ export { CostModes, SortOrders, ZodFirstPartyTypeKind, ZodOptional, ZodType, activityDateSchema, arrayType, booleanType, createDailyDate, createMonthlyDate, createProjectPath, createSessionId, dailyDateSchema, dateSchema, discriminatedUnionType, enumType, isoTimestampSchema, literalType, messageIdSchema, modelNameSchema, modelPricingSchema, monthlyDateSchema, numberType, objectType, optionalType, projectPathSchema, recordType, requestIdSchema, sessionIdSchema, stringType, unionType, unknownType, versionSchema };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ccusage",
3
3
  "type": "module",
4
- "version": "12.3.2",
4
+ "version": "13.0.0",
5
5
  "description": "Usage analysis tool for Claude Code",
6
6
  "author": "ryoppippi",
7
7
  "license": "MIT",
File without changes