wave-agent-sdk 0.0.7 → 0.0.8

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 (172) hide show
  1. package/dist/agent.d.ts +32 -20
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +202 -20
  4. package/dist/constants/events.d.ts +28 -0
  5. package/dist/constants/events.d.ts.map +1 -0
  6. package/dist/constants/events.js +27 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -0
  10. package/dist/managers/aiManager.d.ts +34 -1
  11. package/dist/managers/aiManager.d.ts.map +1 -1
  12. package/dist/managers/aiManager.js +243 -128
  13. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  14. package/dist/managers/backgroundBashManager.js +7 -6
  15. package/dist/managers/hookManager.d.ts +9 -4
  16. package/dist/managers/hookManager.d.ts.map +1 -1
  17. package/dist/managers/hookManager.js +62 -30
  18. package/dist/managers/liveConfigManager.d.ts +58 -0
  19. package/dist/managers/liveConfigManager.d.ts.map +1 -0
  20. package/dist/managers/liveConfigManager.js +160 -0
  21. package/dist/managers/messageManager.d.ts +38 -13
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +163 -30
  24. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  25. package/dist/managers/slashCommandManager.js +4 -1
  26. package/dist/managers/subagentManager.d.ts +51 -0
  27. package/dist/managers/subagentManager.d.ts.map +1 -1
  28. package/dist/managers/subagentManager.js +189 -18
  29. package/dist/services/aiService.d.ts +13 -5
  30. package/dist/services/aiService.d.ts.map +1 -1
  31. package/dist/services/aiService.js +350 -74
  32. package/dist/services/configurationWatcher.d.ts +120 -0
  33. package/dist/services/configurationWatcher.d.ts.map +1 -0
  34. package/dist/services/configurationWatcher.js +439 -0
  35. package/dist/services/fileWatcher.d.ts +69 -0
  36. package/dist/services/fileWatcher.d.ts.map +1 -0
  37. package/dist/services/fileWatcher.js +213 -0
  38. package/dist/services/hook.d.ts +91 -9
  39. package/dist/services/hook.d.ts.map +1 -1
  40. package/dist/services/hook.js +393 -43
  41. package/dist/services/jsonlHandler.d.ts +62 -0
  42. package/dist/services/jsonlHandler.d.ts.map +1 -0
  43. package/dist/services/jsonlHandler.js +257 -0
  44. package/dist/services/memory.d.ts +9 -0
  45. package/dist/services/memory.d.ts.map +1 -1
  46. package/dist/services/memory.js +81 -12
  47. package/dist/services/memoryStore.d.ts +81 -0
  48. package/dist/services/memoryStore.d.ts.map +1 -0
  49. package/dist/services/memoryStore.js +200 -0
  50. package/dist/services/session.d.ts +64 -49
  51. package/dist/services/session.d.ts.map +1 -1
  52. package/dist/services/session.js +310 -132
  53. package/dist/tools/bashTool.d.ts.map +1 -1
  54. package/dist/tools/bashTool.js +5 -4
  55. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  56. package/dist/tools/deleteFileTool.js +2 -1
  57. package/dist/tools/editTool.d.ts.map +1 -1
  58. package/dist/tools/editTool.js +3 -2
  59. package/dist/tools/multiEditTool.d.ts.map +1 -1
  60. package/dist/tools/multiEditTool.js +4 -3
  61. package/dist/tools/readTool.d.ts.map +1 -1
  62. package/dist/tools/readTool.js +2 -1
  63. package/dist/tools/writeTool.d.ts.map +1 -1
  64. package/dist/tools/writeTool.js +5 -6
  65. package/dist/types/commands.d.ts +4 -0
  66. package/dist/types/commands.d.ts.map +1 -1
  67. package/dist/types/core.d.ts +35 -0
  68. package/dist/types/core.d.ts.map +1 -1
  69. package/dist/types/environment.d.ts +42 -0
  70. package/dist/types/environment.d.ts.map +1 -0
  71. package/dist/types/environment.js +21 -0
  72. package/dist/types/hooks.d.ts +8 -2
  73. package/dist/types/hooks.d.ts.map +1 -1
  74. package/dist/types/hooks.js +8 -2
  75. package/dist/types/index.d.ts +2 -0
  76. package/dist/types/index.d.ts.map +1 -1
  77. package/dist/types/index.js +2 -0
  78. package/dist/types/memoryStore.d.ts +82 -0
  79. package/dist/types/memoryStore.d.ts.map +1 -0
  80. package/dist/types/memoryStore.js +7 -0
  81. package/dist/types/messaging.d.ts +14 -2
  82. package/dist/types/messaging.d.ts.map +1 -1
  83. package/dist/types/session.d.ts +20 -0
  84. package/dist/types/session.d.ts.map +1 -0
  85. package/dist/types/session.js +7 -0
  86. package/dist/utils/bashHistory.d.ts.map +1 -1
  87. package/dist/utils/bashHistory.js +27 -26
  88. package/dist/utils/cacheControlUtils.d.ts +121 -0
  89. package/dist/utils/cacheControlUtils.d.ts.map +1 -0
  90. package/dist/utils/cacheControlUtils.js +367 -0
  91. package/dist/utils/commandPathResolver.d.ts +52 -0
  92. package/dist/utils/commandPathResolver.d.ts.map +1 -0
  93. package/dist/utils/commandPathResolver.js +145 -0
  94. package/dist/utils/configPaths.d.ts +85 -0
  95. package/dist/utils/configPaths.d.ts.map +1 -0
  96. package/dist/utils/configPaths.js +121 -0
  97. package/dist/utils/configResolver.d.ts +37 -10
  98. package/dist/utils/configResolver.d.ts.map +1 -1
  99. package/dist/utils/configResolver.js +127 -23
  100. package/dist/utils/constants.d.ts +1 -1
  101. package/dist/utils/constants.js +1 -1
  102. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  103. package/dist/utils/convertMessagesForAPI.js +7 -5
  104. package/dist/utils/customCommands.d.ts.map +1 -1
  105. package/dist/utils/customCommands.js +66 -21
  106. package/dist/utils/fileUtils.d.ts +15 -0
  107. package/dist/utils/fileUtils.d.ts.map +1 -0
  108. package/dist/utils/fileUtils.js +61 -0
  109. package/dist/utils/globalLogger.d.ts +102 -0
  110. package/dist/utils/globalLogger.d.ts.map +1 -0
  111. package/dist/utils/globalLogger.js +136 -0
  112. package/dist/utils/mcpUtils.d.ts.map +1 -1
  113. package/dist/utils/mcpUtils.js +25 -3
  114. package/dist/utils/messageOperations.d.ts +20 -8
  115. package/dist/utils/messageOperations.d.ts.map +1 -1
  116. package/dist/utils/messageOperations.js +25 -16
  117. package/dist/utils/pathEncoder.d.ts +104 -0
  118. package/dist/utils/pathEncoder.d.ts.map +1 -0
  119. package/dist/utils/pathEncoder.js +272 -0
  120. package/dist/utils/subagentParser.d.ts.map +1 -1
  121. package/dist/utils/subagentParser.js +2 -1
  122. package/dist/utils/tokenCalculation.d.ts +26 -0
  123. package/dist/utils/tokenCalculation.d.ts.map +1 -0
  124. package/dist/utils/tokenCalculation.js +36 -0
  125. package/package.json +6 -3
  126. package/src/agent.ts +298 -34
  127. package/src/constants/events.ts +38 -0
  128. package/src/index.ts +2 -0
  129. package/src/managers/aiManager.ts +323 -170
  130. package/src/managers/backgroundBashManager.ts +7 -6
  131. package/src/managers/hookManager.ts +83 -40
  132. package/src/managers/liveConfigManager.ts +248 -0
  133. package/src/managers/messageManager.ts +230 -63
  134. package/src/managers/slashCommandManager.ts +4 -1
  135. package/src/managers/subagentManager.ts +283 -21
  136. package/src/services/aiService.ts +474 -83
  137. package/src/services/configurationWatcher.ts +622 -0
  138. package/src/services/fileWatcher.ts +301 -0
  139. package/src/services/hook.ts +538 -47
  140. package/src/services/jsonlHandler.ts +319 -0
  141. package/src/services/memory.ts +92 -12
  142. package/src/services/memoryStore.ts +279 -0
  143. package/src/services/session.ts +381 -157
  144. package/src/tools/bashTool.ts +5 -4
  145. package/src/tools/deleteFileTool.ts +2 -1
  146. package/src/tools/editTool.ts +3 -2
  147. package/src/tools/multiEditTool.ts +4 -3
  148. package/src/tools/readTool.ts +2 -1
  149. package/src/tools/writeTool.ts +7 -6
  150. package/src/types/commands.ts +6 -0
  151. package/src/types/core.ts +44 -0
  152. package/src/types/environment.ts +60 -0
  153. package/src/types/hooks.ts +21 -8
  154. package/src/types/index.ts +2 -0
  155. package/src/types/memoryStore.ts +94 -0
  156. package/src/types/messaging.ts +14 -2
  157. package/src/types/session.ts +25 -0
  158. package/src/utils/bashHistory.ts +27 -27
  159. package/src/utils/cacheControlUtils.ts +540 -0
  160. package/src/utils/commandPathResolver.ts +189 -0
  161. package/src/utils/configPaths.ts +163 -0
  162. package/src/utils/configResolver.ts +182 -22
  163. package/src/utils/constants.ts +1 -1
  164. package/src/utils/convertMessagesForAPI.ts +7 -5
  165. package/src/utils/customCommands.ts +90 -22
  166. package/src/utils/fileUtils.ts +65 -0
  167. package/src/utils/globalLogger.ts +145 -0
  168. package/src/utils/mcpUtils.ts +34 -3
  169. package/src/utils/messageOperations.ts +42 -20
  170. package/src/utils/pathEncoder.ts +379 -0
  171. package/src/utils/subagentParser.ts +2 -1
  172. package/src/utils/tokenCalculation.ts +43 -0
@@ -0,0 +1,379 @@
1
+ /**
2
+ * Path encoding utility for converting working directory paths to filesystem-safe names
3
+ * Handles cross-platform directory name encoding for project-based session organization
4
+ */
5
+
6
+ import { resolve, join } from "path";
7
+ import { createHash } from "crypto";
8
+ import { realpath, mkdir } from "fs/promises";
9
+ import { homedir, platform } from "os";
10
+
11
+ /**
12
+ * Project directory information
13
+ */
14
+ export interface ProjectDirectory {
15
+ readonly originalPath: string;
16
+ readonly encodedName: string;
17
+ readonly encodedPath: string;
18
+ readonly pathHash?: string; // For collision resolution
19
+ readonly isSymbolicLink: boolean;
20
+ }
21
+
22
+ /**
23
+ * Path encoding configuration options
24
+ */
25
+ export interface PathEncodingOptions {
26
+ maxLength?: number; // Default: 200 characters
27
+ pathSeparatorReplacement?: string; // Default: '-'
28
+ spaceReplacement?: string; // Default: '_'
29
+ invalidCharReplacement?: string; // Default: '_'
30
+ preserveCase?: boolean; // Default: false (convert to lowercase)
31
+ hashLength?: number; // Default: 8 characters
32
+ }
33
+
34
+ /**
35
+ * Platform-specific filesystem constraints
36
+ */
37
+ export interface FilesystemConstraints {
38
+ readonly maxDirectoryNameLength: number;
39
+ readonly maxPathLength: number;
40
+ readonly invalidCharacters: string[];
41
+ readonly reservedNames: string[];
42
+ readonly caseSensitive: boolean;
43
+ }
44
+
45
+ /**
46
+ * Path validation result
47
+ */
48
+ export interface PathValidationResult {
49
+ readonly isValid: boolean;
50
+ readonly errors: string[];
51
+ readonly warnings: string[];
52
+ readonly suggestedFix?: string;
53
+ }
54
+
55
+ /**
56
+ * PathEncoder class for converting working directory paths to filesystem-safe names
57
+ */
58
+ export class PathEncoder {
59
+ private readonly options: Required<PathEncodingOptions>;
60
+ private readonly constraints: FilesystemConstraints;
61
+
62
+ constructor(options: PathEncodingOptions = {}) {
63
+ this.options = {
64
+ maxLength: options.maxLength ?? 200,
65
+ pathSeparatorReplacement: options.pathSeparatorReplacement ?? "-",
66
+ spaceReplacement: options.spaceReplacement ?? "_",
67
+ invalidCharReplacement: options.invalidCharReplacement ?? "_",
68
+ preserveCase: options.preserveCase ?? false,
69
+ hashLength: options.hashLength ?? 8,
70
+ };
71
+ this.constraints = this.getFilesystemConstraints();
72
+ }
73
+
74
+ /**
75
+ * Encode a working directory path to a filesystem-safe directory name
76
+ */
77
+ async encode(originalPath: string): Promise<string> {
78
+ // Resolve symbolic links and normalize path
79
+ const resolvedPath = await this.resolvePath(originalPath);
80
+ return this.encodeSync(resolvedPath);
81
+ }
82
+
83
+ /**
84
+ * Synchronously encode a path to a filesystem-safe directory name
85
+ * Note: Does not resolve symbolic links - use encode() for full path resolution
86
+ */
87
+ encodeSync(pathToEncode: string): string {
88
+ // Convert to safe directory name
89
+ let encoded = pathToEncode;
90
+
91
+ // Remove leading slash to avoid empty directory names
92
+ if (encoded.startsWith("/")) {
93
+ encoded = encoded.substring(1);
94
+ }
95
+
96
+ // Replace path separators with hyphens
97
+ encoded = encoded.replace(/[/\\]/g, this.options.pathSeparatorReplacement);
98
+
99
+ // Replace spaces with underscores
100
+ encoded = encoded.replace(/\s+/g, this.options.spaceReplacement);
101
+
102
+ // Replace invalid characters with underscores
103
+ const escapedChars = this.constraints.invalidCharacters
104
+ .map((c) => `\\${c}`)
105
+ .join("");
106
+ const invalidChars = new RegExp(`[${escapedChars}]`, "g");
107
+ encoded = encoded.replace(
108
+ invalidChars,
109
+ this.options.invalidCharReplacement,
110
+ );
111
+
112
+ // Convert to lowercase unless preserveCase is true
113
+ if (!this.options.preserveCase) {
114
+ encoded = encoded.toLowerCase();
115
+ }
116
+
117
+ // Handle length limit with hash
118
+ if (encoded.length > this.options.maxLength) {
119
+ const hash = this.generateHash(pathToEncode, this.options.hashLength);
120
+ const maxBaseLength =
121
+ this.options.maxLength - this.options.hashLength - 1; // -1 for separator
122
+ encoded = `${encoded.substring(0, maxBaseLength)}-${hash}`;
123
+ }
124
+
125
+ return encoded;
126
+ }
127
+
128
+ /**
129
+ * Decode an encoded directory name back to original path (limited functionality)
130
+ * Note: This is best-effort as encoding is lossy
131
+ */
132
+ async decode(encodedName: string): Promise<string | null> {
133
+ return this.decodeSync(encodedName);
134
+ }
135
+
136
+ /**
137
+ * Synchronously decode an encoded directory name back to original path (limited functionality)
138
+ * Note: This is best-effort as encoding is lossy
139
+ */
140
+ decodeSync(encodedName: string): string | null {
141
+ // This is a simplified version - full reversal is not always possible
142
+ // due to lossy encoding (case changes, character replacements, hashing)
143
+
144
+ // Check if this has a hash suffix
145
+ const hashPattern = new RegExp(`-[a-f0-9]{${this.options.hashLength}}$`);
146
+ if (hashPattern.test(encodedName)) {
147
+ // Cannot reliably decode hashed paths
148
+ return null;
149
+ }
150
+
151
+ // Attempt basic reversal
152
+ let decoded = encodedName;
153
+
154
+ // Reverse path separator replacement
155
+ decoded = decoded.replace(
156
+ new RegExp(this.options.pathSeparatorReplacement, "g"),
157
+ "/",
158
+ );
159
+
160
+ // Reverse space replacement
161
+ decoded = decoded.replace(
162
+ new RegExp(this.options.spaceReplacement, "g"),
163
+ " ",
164
+ );
165
+
166
+ // Add leading slash
167
+ decoded = `/${decoded}`;
168
+
169
+ return decoded;
170
+ }
171
+
172
+ /**
173
+ * Resolve symbolic links and normalize path before encoding
174
+ */
175
+ async resolvePath(path: string): Promise<string> {
176
+ try {
177
+ // Expand tilde to home directory
178
+ const expandedPath = this.expandTilde(path);
179
+
180
+ // Resolve to absolute path
181
+ const absolutePath = resolve(expandedPath);
182
+
183
+ // Resolve symbolic links
184
+ const resolvedPath = await realpath(absolutePath);
185
+
186
+ return resolvedPath;
187
+ } catch (error) {
188
+ throw new Error(`Failed to resolve path "${path}": ${error}`);
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Create project directory entity from original path
194
+ */
195
+ async createProjectDirectory(
196
+ originalPath: string,
197
+ baseSessionDir: string,
198
+ ): Promise<ProjectDirectory> {
199
+ // Resolve the original path and check for symbolic links
200
+ const expandedPath = this.expandTilde(originalPath);
201
+ const absolutePath = resolve(expandedPath);
202
+
203
+ let resolvedPath: string;
204
+ let isSymbolicLink = false;
205
+
206
+ try {
207
+ resolvedPath = await realpath(absolutePath);
208
+ isSymbolicLink = resolvedPath !== absolutePath;
209
+ } catch {
210
+ // If realpath fails, use the absolute path
211
+ resolvedPath = absolutePath;
212
+ }
213
+
214
+ // Encode the resolved path
215
+ const encodedName = await this.encode(resolvedPath);
216
+ const encodedPath = join(baseSessionDir, encodedName);
217
+
218
+ // Generate hash if encoding resulted in truncation
219
+ let pathHash: string | undefined;
220
+ if (resolvedPath.length > this.options.maxLength) {
221
+ pathHash = this.generateHash(resolvedPath, this.options.hashLength);
222
+ }
223
+
224
+ // Ensure the encoded directory exists
225
+ try {
226
+ await mkdir(encodedPath, { recursive: true });
227
+ } catch {
228
+ // Ignore errors if directory already exists
229
+ }
230
+
231
+ return {
232
+ originalPath: resolvedPath,
233
+ encodedName,
234
+ encodedPath,
235
+ pathHash,
236
+ isSymbolicLink,
237
+ };
238
+ }
239
+
240
+ /**
241
+ * Validate that an encoded name is filesystem-safe
242
+ */
243
+ validateEncodedName(encodedName: string): boolean {
244
+ // Check length
245
+ if (encodedName.length > this.constraints.maxDirectoryNameLength) {
246
+ return false;
247
+ }
248
+
249
+ // Check for invalid characters
250
+ for (const char of this.constraints.invalidCharacters) {
251
+ if (encodedName.includes(char)) {
252
+ return false;
253
+ }
254
+ }
255
+
256
+ // Check for reserved names
257
+ const lowerName = encodedName.toLowerCase();
258
+ if (
259
+ this.constraints.reservedNames.some(
260
+ (reserved) => reserved.toLowerCase() === lowerName,
261
+ )
262
+ ) {
263
+ return false;
264
+ }
265
+
266
+ // Check for empty or dots-only names
267
+ if (!encodedName.trim() || /^\.+$/.test(encodedName)) {
268
+ return false;
269
+ }
270
+
271
+ return true;
272
+ }
273
+
274
+ /**
275
+ * Handle encoding collisions by generating unique names
276
+ */
277
+ resolveCollision(baseName: string, existingNames: Set<string>): string {
278
+ if (!existingNames.has(baseName)) {
279
+ return baseName;
280
+ }
281
+
282
+ // Try numbered suffixes first
283
+ for (let i = 1; i <= 999; i++) {
284
+ const candidate = `${baseName}-${i}`;
285
+ if (!existingNames.has(candidate)) {
286
+ return candidate;
287
+ }
288
+ }
289
+
290
+ // If all numbered suffixes are taken, use hash
291
+ const hash = this.generateHash(
292
+ baseName + Date.now(),
293
+ this.options.hashLength,
294
+ );
295
+ return `${baseName}-${hash}`;
296
+ }
297
+
298
+ /**
299
+ * Get platform-specific filesystem constraints
300
+ */
301
+ private getFilesystemConstraints(): FilesystemConstraints {
302
+ const currentPlatform = platform();
303
+
304
+ switch (currentPlatform) {
305
+ case "win32":
306
+ return {
307
+ maxDirectoryNameLength: 255,
308
+ maxPathLength: 260,
309
+ invalidCharacters: ["<", ">", ":", '"', "|", "?", "*"],
310
+ reservedNames: [
311
+ "CON",
312
+ "PRN",
313
+ "AUX",
314
+ "NUL",
315
+ "COM1",
316
+ "COM2",
317
+ "COM3",
318
+ "COM4",
319
+ "COM5",
320
+ "COM6",
321
+ "COM7",
322
+ "COM8",
323
+ "COM9",
324
+ "LPT1",
325
+ "LPT2",
326
+ "LPT3",
327
+ "LPT4",
328
+ "LPT5",
329
+ "LPT6",
330
+ "LPT7",
331
+ "LPT8",
332
+ "LPT9",
333
+ ],
334
+ caseSensitive: false,
335
+ };
336
+ case "darwin":
337
+ return {
338
+ maxDirectoryNameLength: 255,
339
+ maxPathLength: 1024,
340
+ invalidCharacters: [":"],
341
+ reservedNames: [],
342
+ caseSensitive: false, // HFS+ is case-insensitive by default
343
+ };
344
+ default: // Linux and other Unix-like systems
345
+ return {
346
+ maxDirectoryNameLength: 255,
347
+ maxPathLength: 4096,
348
+ invalidCharacters: ["\0"],
349
+ reservedNames: [],
350
+ caseSensitive: true,
351
+ };
352
+ }
353
+ }
354
+
355
+ /**
356
+ * Generate hash for collision resolution
357
+ */
358
+ private generateHash(input: string, length: number): string {
359
+ return createHash("sha256")
360
+ .update(input)
361
+ .digest("hex")
362
+ .substring(0, length);
363
+ }
364
+
365
+ /**
366
+ * Expand tilde (~) to home directory
367
+ */
368
+ private expandTilde(path: string): string {
369
+ if (path.startsWith("~/") || path === "~") {
370
+ return path.replace(/^~/, homedir());
371
+ }
372
+ return path;
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Default PathEncoder instance
378
+ */
379
+ export const pathEncoder = new PathEncoder();
@@ -1,5 +1,6 @@
1
1
  import { readFileSync, readdirSync, statSync } from "fs";
2
2
  import { join, extname } from "path";
3
+ import { logger } from "./globalLogger.js";
3
4
 
4
5
  export interface SubagentConfiguration {
5
6
  name: string;
@@ -172,7 +173,7 @@ function scanSubagentDirectory(
172
173
  configurations.push(config);
173
174
  } catch (parseError) {
174
175
  // Log error but continue with other files
175
- console.warn(
176
+ logger.warn(
176
177
  `Warning: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
177
178
  );
178
179
  }
@@ -0,0 +1,43 @@
1
+ import type { Usage } from "../types/index.js";
2
+
3
+ /**
4
+ * Calculate comprehensive total tokens including cache-related tokens
5
+ *
6
+ * This function computes the true total token cost by including:
7
+ * - Base total_tokens (prompt + completion)
8
+ * - Cache read tokens (cost savings indicator)
9
+ * - Cache creation tokens (cache investment)
10
+ *
11
+ * For accurate cost tracking with Claude models that support cache control.
12
+ *
13
+ * @param usage - Usage statistics from AI operation
14
+ * @returns Comprehensive total including all cache-related tokens
15
+ */
16
+ export function calculateComprehensiveTotalTokens(usage: Usage): number {
17
+ const baseTokens = usage.total_tokens;
18
+ const cacheReadTokens = usage.cache_read_input_tokens || 0;
19
+ const cacheCreateTokens = usage.cache_creation_input_tokens || 0;
20
+
21
+ return baseTokens + cacheReadTokens + cacheCreateTokens;
22
+ }
23
+
24
+ /**
25
+ * Extract the latest total tokens from the last message with usage data
26
+ * Uses comprehensive calculation that includes cache tokens for accurate tracking
27
+ *
28
+ * @param messages - Array of messages to search
29
+ * @returns Comprehensive total tokens from the most recent usage data, or 0 if none found
30
+ */
31
+ export function extractLatestTotalTokens(
32
+ messages: Array<{ usage?: Usage }>,
33
+ ): number {
34
+ // Find the last message with usage data (iterate backwards for efficiency)
35
+ for (let i = messages.length - 1; i >= 0; i--) {
36
+ const message = messages[i];
37
+ if (message.usage) {
38
+ return calculateComprehensiveTotalTokens(message.usage);
39
+ }
40
+ }
41
+
42
+ return 0; // No usage data found
43
+ }