wave-agent-sdk 0.0.6 → 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 (180) hide show
  1. package/dist/agent.d.ts +32 -20
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +209 -24
  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 +248 -132
  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 +13 -16
  16. package/dist/managers/hookManager.d.ts.map +1 -1
  17. package/dist/managers/hookManager.js +81 -44
  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 +41 -24
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +168 -49
  24. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  25. package/dist/managers/slashCommandManager.js +9 -3
  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 +190 -19
  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/todoWriteTool.d.ts.map +1 -1
  64. package/dist/tools/todoWriteTool.js +3 -10
  65. package/dist/tools/writeTool.d.ts.map +1 -1
  66. package/dist/tools/writeTool.js +5 -6
  67. package/dist/types/commands.d.ts +4 -0
  68. package/dist/types/commands.d.ts.map +1 -1
  69. package/dist/types/core.d.ts +35 -0
  70. package/dist/types/core.d.ts.map +1 -1
  71. package/dist/types/environment.d.ts +42 -0
  72. package/dist/types/environment.d.ts.map +1 -0
  73. package/dist/types/environment.js +21 -0
  74. package/dist/types/hooks.d.ts +8 -2
  75. package/dist/types/hooks.d.ts.map +1 -1
  76. package/dist/types/hooks.js +8 -2
  77. package/dist/types/index.d.ts +2 -0
  78. package/dist/types/index.d.ts.map +1 -1
  79. package/dist/types/index.js +2 -0
  80. package/dist/types/memoryStore.d.ts +82 -0
  81. package/dist/types/memoryStore.d.ts.map +1 -0
  82. package/dist/types/memoryStore.js +7 -0
  83. package/dist/types/messaging.d.ts +21 -9
  84. package/dist/types/messaging.d.ts.map +1 -1
  85. package/dist/types/messaging.js +5 -1
  86. package/dist/types/session.d.ts +20 -0
  87. package/dist/types/session.d.ts.map +1 -0
  88. package/dist/types/session.js +7 -0
  89. package/dist/utils/bashHistory.d.ts.map +1 -1
  90. package/dist/utils/bashHistory.js +27 -26
  91. package/dist/utils/cacheControlUtils.d.ts +121 -0
  92. package/dist/utils/cacheControlUtils.d.ts.map +1 -0
  93. package/dist/utils/cacheControlUtils.js +367 -0
  94. package/dist/utils/commandPathResolver.d.ts +52 -0
  95. package/dist/utils/commandPathResolver.d.ts.map +1 -0
  96. package/dist/utils/commandPathResolver.js +145 -0
  97. package/dist/utils/configPaths.d.ts +85 -0
  98. package/dist/utils/configPaths.d.ts.map +1 -0
  99. package/dist/utils/configPaths.js +121 -0
  100. package/dist/utils/configResolver.d.ts +37 -10
  101. package/dist/utils/configResolver.d.ts.map +1 -1
  102. package/dist/utils/configResolver.js +127 -23
  103. package/dist/utils/constants.d.ts +1 -1
  104. package/dist/utils/constants.js +1 -1
  105. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  106. package/dist/utils/convertMessagesForAPI.js +8 -13
  107. package/dist/utils/customCommands.d.ts.map +1 -1
  108. package/dist/utils/customCommands.js +66 -21
  109. package/dist/utils/fileUtils.d.ts +15 -0
  110. package/dist/utils/fileUtils.d.ts.map +1 -0
  111. package/dist/utils/fileUtils.js +61 -0
  112. package/dist/utils/globalLogger.d.ts +102 -0
  113. package/dist/utils/globalLogger.d.ts.map +1 -0
  114. package/dist/utils/globalLogger.js +136 -0
  115. package/dist/utils/hookMatcher.d.ts +1 -6
  116. package/dist/utils/hookMatcher.d.ts.map +1 -1
  117. package/dist/utils/mcpUtils.d.ts.map +1 -1
  118. package/dist/utils/mcpUtils.js +25 -3
  119. package/dist/utils/messageOperations.d.ts +27 -27
  120. package/dist/utils/messageOperations.d.ts.map +1 -1
  121. package/dist/utils/messageOperations.js +46 -36
  122. package/dist/utils/pathEncoder.d.ts +104 -0
  123. package/dist/utils/pathEncoder.d.ts.map +1 -0
  124. package/dist/utils/pathEncoder.js +272 -0
  125. package/dist/utils/subagentParser.d.ts.map +1 -1
  126. package/dist/utils/subagentParser.js +2 -1
  127. package/dist/utils/tokenCalculation.d.ts +26 -0
  128. package/dist/utils/tokenCalculation.d.ts.map +1 -0
  129. package/dist/utils/tokenCalculation.js +36 -0
  130. package/package.json +6 -3
  131. package/src/agent.ts +301 -37
  132. package/src/constants/events.ts +38 -0
  133. package/src/index.ts +2 -0
  134. package/src/managers/aiManager.ts +325 -173
  135. package/src/managers/backgroundBashManager.ts +7 -6
  136. package/src/managers/hookManager.ts +106 -84
  137. package/src/managers/liveConfigManager.ts +248 -0
  138. package/src/managers/messageManager.ts +237 -100
  139. package/src/managers/slashCommandManager.ts +9 -7
  140. package/src/managers/subagentManager.ts +284 -22
  141. package/src/services/aiService.ts +474 -83
  142. package/src/services/configurationWatcher.ts +622 -0
  143. package/src/services/fileWatcher.ts +301 -0
  144. package/src/services/hook.ts +538 -47
  145. package/src/services/jsonlHandler.ts +319 -0
  146. package/src/services/memory.ts +92 -12
  147. package/src/services/memoryStore.ts +279 -0
  148. package/src/services/session.ts +381 -157
  149. package/src/tools/bashTool.ts +5 -4
  150. package/src/tools/deleteFileTool.ts +2 -1
  151. package/src/tools/editTool.ts +3 -2
  152. package/src/tools/multiEditTool.ts +4 -3
  153. package/src/tools/readTool.ts +2 -1
  154. package/src/tools/todoWriteTool.ts +3 -11
  155. package/src/tools/writeTool.ts +7 -6
  156. package/src/types/commands.ts +6 -0
  157. package/src/types/core.ts +44 -0
  158. package/src/types/environment.ts +60 -0
  159. package/src/types/hooks.ts +21 -8
  160. package/src/types/index.ts +2 -0
  161. package/src/types/memoryStore.ts +94 -0
  162. package/src/types/messaging.ts +21 -10
  163. package/src/types/session.ts +25 -0
  164. package/src/utils/bashHistory.ts +27 -27
  165. package/src/utils/cacheControlUtils.ts +540 -0
  166. package/src/utils/commandPathResolver.ts +189 -0
  167. package/src/utils/configPaths.ts +163 -0
  168. package/src/utils/configResolver.ts +182 -22
  169. package/src/utils/constants.ts +1 -1
  170. package/src/utils/convertMessagesForAPI.ts +8 -14
  171. package/src/utils/customCommands.ts +90 -22
  172. package/src/utils/fileUtils.ts +65 -0
  173. package/src/utils/globalLogger.ts +145 -0
  174. package/src/utils/hookMatcher.ts +1 -12
  175. package/src/utils/mcpUtils.ts +34 -3
  176. package/src/utils/messageOperations.ts +77 -60
  177. package/src/utils/pathEncoder.ts +379 -0
  178. package/src/utils/subagentParser.ts +2 -1
  179. package/src/utils/tokenCalculation.ts +43 -0
  180. package/src/types/index.ts.backup +0 -357
@@ -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
+ }
@@ -1,357 +0,0 @@
1
- import type { ChildProcess } from "child_process";
2
-
3
- /**
4
- * Logger interface definition
5
- * Compatible with OpenAI package Logger interface
6
- */
7
- export interface Logger {
8
- error: (...args: unknown[]) => void;
9
- warn: (...args: unknown[]) => void;
10
- info: (...args: unknown[]) => void;
11
- debug: (...args: unknown[]) => void;
12
- }
13
-
14
- export interface Message {
15
- role: "user" | "assistant";
16
- blocks: MessageBlock[];
17
- usage?: Usage; // Usage data for this message's AI operation (assistant messages only)
18
- }
19
-
20
- export type MessageBlock =
21
- | TextBlock
22
- | ErrorBlock
23
- | ToolBlock
24
- | ImageBlock
25
- | DiffBlock
26
- | CommandOutputBlock
27
- | CompressBlock
28
- | MemoryBlock
29
- | CustomCommandBlock
30
- | SubagentBlock;
31
-
32
- export interface TextBlock {
33
- type: "text";
34
- content: string;
35
- }
36
-
37
- export interface ErrorBlock {
38
- type: "error";
39
- content: string;
40
- }
41
-
42
- export interface ToolBlock {
43
- type: "tool";
44
- parameters?: string;
45
- result?: string;
46
- shortResult?: string; // Add shortResult field
47
- images?: Array<{
48
- // Add image data support
49
- data: string; // Base64 encoded image data
50
- mediaType?: string; // Media type of the image
51
- }>;
52
- id?: string;
53
- name?: string;
54
- isRunning?: boolean; // Mark if tool is actually executing
55
- success?: boolean;
56
- error?: string | Error;
57
- compactParams?: string; // Compact parameter display
58
- }
59
-
60
- export interface ImageBlock {
61
- type: "image";
62
- imageUrls?: string[];
63
- }
64
-
65
- export interface DiffBlock {
66
- type: "diff";
67
- path: string;
68
- diffResult: Array<{
69
- value: string;
70
- added?: boolean;
71
- removed?: boolean;
72
- }>;
73
- }
74
-
75
- export interface CommandOutputBlock {
76
- type: "command_output";
77
- command: string;
78
- output: string;
79
- isRunning: boolean;
80
- exitCode: number | null;
81
- }
82
-
83
- export interface CompressBlock {
84
- type: "compress";
85
- content: string;
86
- }
87
-
88
- export interface MemoryBlock {
89
- type: "memory";
90
- content: string;
91
- isSuccess: boolean;
92
- memoryType?: "project" | "user"; // Memory type
93
- storagePath?: string; // Storage path text
94
- }
95
-
96
- export interface CustomCommandBlock {
97
- type: "custom_command";
98
- commandName: string;
99
- content: string; // Complete command content, used when passing to AI
100
- originalInput?: string; // Original user input, used for UI display (e.g., "/fix-issue 123 high")
101
- }
102
-
103
- export interface SubagentBlock {
104
- type: "subagent";
105
- subagentId: string;
106
- subagentName: string;
107
- status: "active" | "completed" | "error" | "aborted";
108
- messages: Message[];
109
- }
110
-
111
- export interface AIRequest {
112
- content: string;
113
- files: unknown[];
114
- }
115
-
116
- export interface AIResponse {
117
- content: string;
118
- status: "success" | "error";
119
- error?: string;
120
- }
121
-
122
- // MCP related types
123
- export interface McpServerConfig {
124
- command: string;
125
- args?: string[];
126
- env?: Record<string, string>;
127
- }
128
-
129
- export interface McpConfig {
130
- mcpServers: Record<string, McpServerConfig>;
131
- }
132
-
133
- export interface McpTool {
134
- name: string;
135
- description?: string;
136
- inputSchema: Record<string, unknown>;
137
- }
138
-
139
- export interface McpServerStatus {
140
- name: string;
141
- config: McpServerConfig;
142
- status: "disconnected" | "connected" | "connecting" | "error";
143
- tools?: McpTool[];
144
- toolCount?: number;
145
- capabilities?: string[];
146
- lastConnected?: number;
147
- error?: string;
148
- }
149
-
150
- // Background bash shell related types
151
- export interface BackgroundShell {
152
- id: string;
153
- process: ChildProcess;
154
- command: string;
155
- startTime: number;
156
- status: "running" | "completed" | "killed";
157
- stdout: string;
158
- stderr: string;
159
- exitCode?: number;
160
- runtime?: number;
161
- }
162
-
163
- // Slash Command related types
164
- export interface SlashCommand {
165
- id: string;
166
- name: string;
167
- description: string;
168
- handler: (args?: string) => Promise<void> | void;
169
- }
170
-
171
- export interface CustomSlashCommandConfig {
172
- allowedTools?: string[];
173
- model?: string;
174
- description?: string;
175
- }
176
-
177
- export interface CustomSlashCommand {
178
- id: string;
179
- name: string;
180
- description?: string; // Add description field
181
- filePath: string;
182
- content: string;
183
- config?: CustomSlashCommandConfig;
184
- }
185
-
186
- // Skill related types
187
- export interface SkillMetadata {
188
- name: string;
189
- description: string;
190
- type: "personal" | "project";
191
- skillPath: string;
192
- }
193
-
194
- export interface Skill extends SkillMetadata {
195
- content: string;
196
- frontmatter: SkillFrontmatter;
197
- isValid: boolean;
198
- errors: string[];
199
- }
200
-
201
- export interface SkillFrontmatter {
202
- name: string;
203
- description: string;
204
- [key: string]: unknown;
205
- }
206
-
207
- export interface SkillCollection {
208
- type: "personal" | "project";
209
- basePath: string;
210
- skills: Map<string, SkillMetadata>;
211
- errors: SkillError[];
212
- }
213
-
214
- export interface SkillError {
215
- skillPath: string;
216
- message: string;
217
- }
218
-
219
- export interface SkillValidationResult {
220
- isValid: boolean;
221
- skill?: Skill;
222
- errors: string[];
223
- }
224
-
225
- export interface SkillDiscoveryResult {
226
- personalSkills: Map<string, SkillMetadata>;
227
- projectSkills: Map<string, SkillMetadata>;
228
- errors: SkillError[];
229
- }
230
-
231
- export interface SkillInvocationContext {
232
- skillName: string;
233
- }
234
-
235
- export interface SkillToolArgs {
236
- skill_name: string;
237
- }
238
-
239
- export interface SkillManagerOptions {
240
- personalSkillsPath?: string;
241
- scanTimeout?: number;
242
- logger?: Logger;
243
- }
244
-
245
- export interface ParsedSkillFile {
246
- frontmatter: SkillFrontmatter;
247
- content: string;
248
- skillMetadata: SkillMetadata;
249
- validationErrors: string[];
250
- isValid: boolean;
251
- }
252
-
253
- export interface SkillParseOptions {
254
- validateMetadata?: boolean;
255
- basePath?: string;
256
- }
257
-
258
- export const SKILL_DEFAULTS = {
259
- PERSONAL_SKILLS_DIR: ".wave/skills",
260
- PROJECT_SKILLS_DIR: ".wave/skills",
261
- SKILL_FILE_NAME: "SKILL.md",
262
- MAX_NAME_LENGTH: 64,
263
- MAX_DESCRIPTION_LENGTH: 1024,
264
- MIN_DESCRIPTION_LENGTH: 1,
265
- NAME_PATTERN: /^[a-z0-9-]+$/,
266
- MAX_METADATA_CACHE: 1000,
267
- MAX_CONTENT_CACHE: 100,
268
- SCAN_TIMEOUT: 5000,
269
- LOAD_TIMEOUT: 2000,
270
- } as const;
271
-
272
- // Configuration types for Agent Constructor Configuration feature
273
- export interface GatewayConfig {
274
- apiKey: string;
275
- baseURL: string;
276
- }
277
-
278
- export interface ModelConfig {
279
- agentModel: string;
280
- fastModel: string;
281
- }
282
-
283
- export interface ConfigurationResolver {
284
- /**
285
- * Resolves gateway configuration from constructor args and environment
286
- * @param apiKey - API key from constructor (optional)
287
- * @param baseURL - Base URL from constructor (optional)
288
- * @returns Resolved gateway configuration
289
- * @throws Error if required configuration is missing after fallbacks
290
- */
291
- resolveGatewayConfig(apiKey?: string, baseURL?: string): GatewayConfig;
292
-
293
- /**
294
- * Resolves model configuration with fallbacks
295
- * @param agentModel - Agent model from constructor (optional)
296
- * @param fastModel - Fast model from constructor (optional)
297
- * @returns Resolved model configuration with defaults
298
- */
299
- resolveModelConfig(agentModel?: string, fastModel?: string): ModelConfig;
300
-
301
- /**
302
- * Resolves token limit with fallbacks
303
- * @param constructorLimit - Token limit from constructor (optional)
304
- * @returns Resolved token limit
305
- */
306
- resolveTokenLimit(constructorLimit?: number): number;
307
- }
308
-
309
- export interface ConfigurationValidator {
310
- /**
311
- * Validates gateway configuration
312
- * @param config - Configuration to validate
313
- * @throws Error with descriptive message if invalid
314
- */
315
- validateGatewayConfig(config: GatewayConfig): void;
316
-
317
- /**
318
- * Validates token limit value
319
- * @param tokenLimit - Token limit to validate
320
- * @throws Error if invalid
321
- */
322
- validateTokenLimit(tokenLimit: number): void;
323
- }
324
-
325
- export class ConfigurationError extends Error {
326
- constructor(
327
- message: string,
328
- public readonly field: string,
329
- public readonly provided?: unknown,
330
- ) {
331
- super(message);
332
- this.name = "ConfigurationError";
333
- }
334
- }
335
-
336
- /**
337
- * Usage statistics for AI operations
338
- * Extends OpenAI's Usage format with additional tracking fields
339
- */
340
- export interface Usage {
341
- prompt_tokens: number; // Tokens used in prompts
342
- completion_tokens: number; // Tokens generated in completions
343
- total_tokens: number; // Sum of prompt + completion tokens
344
- model?: string; // Model used for the operation (e.g., "gpt-4", "gpt-3.5-turbo")
345
- operation_type?: "agent" | "compress"; // Type of operation that generated usage
346
- }
347
-
348
- // Standard error messages
349
- export const CONFIG_ERRORS = {
350
- MISSING_API_KEY:
351
- "Gateway configuration requires apiKey. Provide via constructor or AIGW_TOKEN environment variable.",
352
- MISSING_BASE_URL:
353
- "Gateway configuration requires baseURL. Provide via constructor or AIGW_URL environment variable.",
354
- INVALID_TOKEN_LIMIT: "Token limit must be a positive integer.",
355
- EMPTY_API_KEY: "API key cannot be empty string.",
356
- EMPTY_BASE_URL: "Base URL cannot be empty string.",
357
- } as const;