@codehz/ai 0.1.7 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ai",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "exports": {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Provider usage → canonical Usage 映射
3
3
  *
4
- * best-effort 提取 reasoning / cache / billable 等扩展字段。
4
+ * best-effort 提取 reasoning / cache 等扩展字段。
5
5
  */
6
6
 
7
7
  import type { Usage } from "../types/index.js";
@@ -20,27 +20,6 @@ function record(obj: Record<string, number | undefined>): Partial<Usage> {
20
20
  return out;
21
21
  }
22
22
 
23
- function billableFromOpenAIStyle(
24
- inputTokens: number | undefined,
25
- outputTokens: number | undefined,
26
- cachedInputTokens: number | undefined,
27
- reasoningTokens: number | undefined,
28
- ): Pick<Usage, "billableInputTokens" | "billableOutputTokens"> {
29
- let billableInputTokens: number | undefined;
30
- if (inputTokens !== undefined) {
31
- billableInputTokens =
32
- cachedInputTokens !== undefined ? Math.max(0, inputTokens - cachedInputTokens) : inputTokens;
33
- }
34
-
35
- let billableOutputTokens: number | undefined;
36
- if (outputTokens !== undefined) {
37
- billableOutputTokens =
38
- reasoningTokens !== undefined ? Math.max(0, outputTokens - reasoningTokens) : outputTokens;
39
- }
40
-
41
- return record({ billableInputTokens, billableOutputTokens });
42
- }
43
-
44
23
  /** OpenAI Chat Completions `usage` */
45
24
  export function usageFromChatCompletions(raw: {
46
25
  prompt_tokens?: number;
@@ -63,7 +42,6 @@ export function usageFromChatCompletions(raw: {
63
42
  totalTokens,
64
43
  cachedInputTokens,
65
44
  reasoningTokens,
66
- ...billableFromOpenAIStyle(inputTokens, outputTokens, cachedInputTokens, reasoningTokens),
67
45
  });
68
46
  }
69
47
 
@@ -90,7 +68,6 @@ export function usageFromOpenAIResponses(raw: {
90
68
  totalTokens,
91
69
  cachedInputTokens,
92
70
  reasoningTokens,
93
- ...billableFromOpenAIStyle(inputTokens, outputTokens, cachedInputTokens, reasoningTokens),
94
71
  });
95
72
  }
96
73
 
@@ -102,22 +79,17 @@ export function usageFromAnthropicMessages(raw: {
102
79
  cache_read_input_tokens?: number;
103
80
  [key: string]: unknown;
104
81
  }): Partial<Usage> {
105
- const inputTokens = num(raw.input_tokens);
82
+ const uncachedInputTokens = num(raw.input_tokens);
106
83
  const outputTokens = num(raw.output_tokens);
107
84
  const cacheWriteInputTokens = num(raw.cache_creation_input_tokens);
108
85
  const cachedInputTokens = num(raw.cache_read_input_tokens);
109
86
 
110
- const inputParts = [inputTokens, cacheWriteInputTokens, cachedInputTokens].filter(
87
+ const inputParts = [uncachedInputTokens, cacheWriteInputTokens, cachedInputTokens].filter(
111
88
  (n): n is number => n !== undefined,
112
89
  );
113
- const summedInput = inputParts.length > 0 ? inputParts.reduce((sum, n) => sum + n, 0) : undefined;
90
+ const inputTokens = inputParts.length > 0 ? inputParts.reduce((sum, n) => sum + n, 0) : undefined;
114
91
  const totalTokens =
115
- summedInput !== undefined && outputTokens !== undefined ? summedInput + outputTokens : undefined;
116
-
117
- let billableInputTokens: number | undefined;
118
- if (inputTokens !== undefined || cacheWriteInputTokens !== undefined) {
119
- billableInputTokens = (inputTokens ?? 0) + (cacheWriteInputTokens ?? 0);
120
- }
92
+ inputTokens !== undefined && outputTokens !== undefined ? inputTokens + outputTokens : undefined;
121
93
 
122
94
  return record({
123
95
  inputTokens,
@@ -125,12 +97,10 @@ export function usageFromAnthropicMessages(raw: {
125
97
  totalTokens,
126
98
  cachedInputTokens,
127
99
  cacheWriteInputTokens,
128
- billableInputTokens,
129
- billableOutputTokens: outputTokens,
130
100
  });
131
101
  }
132
102
 
133
- /** Ollama 流式 chunk(无 cache / reasoning 细分时仅填基础与 billable 镜像) */
103
+ /** Ollama 流式 chunk */
134
104
  export function usageFromOllama(raw: {
135
105
  prompt_eval_count?: number;
136
106
  eval_count?: number;
@@ -144,7 +114,5 @@ export function usageFromOllama(raw: {
144
114
  inputTokens,
145
115
  outputTokens,
146
116
  totalTokens,
147
- billableInputTokens: inputTokens,
148
- billableOutputTokens: outputTokens,
149
117
  });
150
118
  }
@@ -24,10 +24,7 @@ export type Usage = {
24
24
  cachedInputTokens?: number;
25
25
  /** Tokens written to prompt cache (Anthropic cache_creation_input_tokens) */
26
26
  cacheWriteInputTokens?: number;
27
- /** Best-effort billable input (full-rate input; excludes discounted cache reads where known) */
28
- billableInputTokens?: number;
29
- /** Best-effort billable output (non-reasoning slice when provider gives reasoning breakdown) */
30
- billableOutputTokens?: number;
27
+
31
28
  };
32
29
 
33
30
  export type BillingInfo = {