wave-agent-sdk 0.0.4 → 0.0.6
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/dist/agent.d.ts +63 -9
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +103 -27
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/managers/aiManager.d.ts +5 -2
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +121 -53
- package/dist/managers/backgroundBashManager.d.ts +1 -1
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/{hooks/manager.d.ts → managers/hookManager.d.ts} +26 -7
- package/dist/managers/hookManager.d.ts.map +1 -0
- package/dist/{hooks/manager.js → managers/hookManager.js} +108 -18
- package/dist/managers/mcpManager.d.ts +1 -1
- package/dist/managers/mcpManager.d.ts.map +1 -1
- package/dist/managers/mcpManager.js +5 -5
- package/dist/managers/messageManager.d.ts +29 -5
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +33 -12
- package/dist/managers/skillManager.d.ts +1 -1
- package/dist/managers/skillManager.d.ts.map +1 -1
- package/dist/managers/skillManager.js +3 -3
- package/dist/managers/slashCommandManager.d.ts +1 -1
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +1 -1
- package/dist/managers/subagentManager.d.ts +9 -12
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +43 -45
- package/dist/managers/toolManager.d.ts +1 -1
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/services/aiService.d.ts +10 -2
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +25 -4
- package/dist/services/hook.d.ts +56 -0
- package/dist/services/hook.d.ts.map +1 -0
- package/dist/services/hook.js +276 -0
- package/dist/services/memory.js +3 -3
- package/dist/services/session.d.ts +65 -16
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +85 -34
- package/dist/tools/bashTool.js +2 -2
- package/dist/tools/deleteFileTool.js +1 -1
- package/dist/tools/editTool.js +1 -1
- package/dist/tools/multiEditTool.js +2 -2
- package/dist/tools/taskTool.d.ts.map +1 -1
- package/dist/tools/taskTool.js +7 -3
- package/dist/tools/writeTool.js +1 -1
- package/dist/types/commands.d.ts +24 -0
- package/dist/types/commands.d.ts.map +1 -0
- package/dist/types/commands.js +5 -0
- package/dist/types/config.d.ts +13 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +5 -0
- package/dist/types/core.d.ts +38 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/{types.js → types/core.js} +4 -13
- package/dist/{hooks/types.d.ts → types/hooks.d.ts} +2 -1
- package/dist/types/hooks.d.ts.map +1 -0
- package/dist/types/index.d.ts +20 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/types/mcp.d.ts +28 -0
- package/dist/types/mcp.d.ts.map +1 -0
- package/dist/types/mcp.js +5 -0
- package/dist/types/messaging.d.ts +80 -0
- package/dist/types/messaging.d.ts.map +1 -0
- package/dist/types/messaging.js +5 -0
- package/dist/types/processes.d.ts +17 -0
- package/dist/types/processes.d.ts.map +1 -0
- package/dist/types/processes.js +5 -0
- package/dist/types/skills.d.ts +78 -0
- package/dist/types/skills.d.ts.map +1 -0
- package/dist/types/skills.js +17 -0
- package/dist/utils/configResolver.d.ts +1 -1
- package/dist/utils/configResolver.d.ts.map +1 -1
- package/dist/utils/configResolver.js +1 -1
- package/dist/utils/configValidator.d.ts +1 -1
- package/dist/utils/configValidator.d.ts.map +1 -1
- package/dist/utils/configValidator.js +1 -1
- package/dist/utils/convertMessagesForAPI.d.ts +1 -1
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
- package/dist/utils/customCommands.d.ts +1 -1
- package/dist/utils/customCommands.d.ts.map +1 -1
- package/dist/{hooks/matcher.d.ts → utils/hookMatcher.d.ts} +1 -1
- package/dist/utils/hookMatcher.d.ts.map +1 -0
- package/dist/utils/markdownParser.d.ts +1 -1
- package/dist/utils/markdownParser.d.ts.map +1 -1
- package/dist/utils/mcpUtils.d.ts +1 -1
- package/dist/utils/mcpUtils.d.ts.map +1 -1
- package/dist/utils/messageOperations.d.ts +7 -2
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +18 -1
- package/dist/utils/skillParser.d.ts +1 -1
- package/dist/utils/skillParser.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/agent.ts +150 -50
- package/src/index.ts +3 -4
- package/src/managers/aiManager.ts +282 -164
- package/src/managers/backgroundBashManager.ts +1 -1
- package/src/{hooks/manager.ts → managers/hookManager.ts} +163 -28
- package/src/managers/mcpManager.ts +6 -6
- package/src/managers/messageManager.ts +69 -10
- package/src/managers/skillManager.ts +4 -4
- package/src/managers/slashCommandManager.ts +6 -2
- package/src/managers/subagentManager.ts +58 -53
- package/src/managers/toolManager.ts +1 -1
- package/src/services/aiService.ts +37 -7
- package/src/services/hook.ts +360 -0
- package/src/services/memory.ts +3 -3
- package/src/services/session.ts +99 -33
- package/src/tools/bashTool.ts +2 -2
- package/src/tools/deleteFileTool.ts +1 -1
- package/src/tools/editTool.ts +1 -1
- package/src/tools/multiEditTool.ts +2 -2
- package/src/tools/taskTool.ts +13 -5
- package/src/tools/writeTool.ts +1 -1
- package/src/types/commands.ts +26 -0
- package/src/types/config.ts +14 -0
- package/src/types/core.ts +49 -0
- package/src/{hooks/types.ts → types/hooks.ts} +1 -0
- package/src/types/index.ts +23 -0
- package/src/{types.ts → types/index.ts.backup} +13 -0
- package/src/types/mcp.ts +31 -0
- package/src/types/messaging.ts +103 -0
- package/src/types/processes.ts +18 -0
- package/src/types/skills.ts +91 -0
- package/src/utils/configResolver.ts +1 -1
- package/src/utils/configValidator.ts +5 -1
- package/src/utils/convertMessagesForAPI.ts +1 -1
- package/src/utils/customCommands.ts +1 -1
- package/src/utils/markdownParser.ts +1 -1
- package/src/utils/mcpUtils.ts +1 -1
- package/src/utils/messageOperations.ts +22 -1
- package/src/utils/skillParser.ts +1 -1
- package/dist/hooks/executor.d.ts +0 -56
- package/dist/hooks/executor.d.ts.map +0 -1
- package/dist/hooks/executor.js +0 -312
- package/dist/hooks/index.d.ts +0 -17
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/index.js +0 -14
- package/dist/hooks/manager.d.ts.map +0 -1
- package/dist/hooks/matcher.d.ts.map +0 -1
- package/dist/hooks/settings.d.ts +0 -46
- package/dist/hooks/settings.d.ts.map +0 -1
- package/dist/hooks/settings.js +0 -100
- package/dist/hooks/types.d.ts.map +0 -1
- package/dist/types.d.ts +0 -276
- package/dist/types.d.ts.map +0 -1
- package/src/hooks/executor.ts +0 -440
- package/src/hooks/index.ts +0 -52
- package/src/hooks/settings.ts +0 -129
- /package/dist/{hooks/types.js → types/hooks.js} +0 -0
- /package/dist/{hooks/matcher.js → utils/hookMatcher.js} +0 -0
- /package/src/{hooks/matcher.ts → utils/hookMatcher.ts} +0 -0
package/dist/agent.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type MessageManagerCallbacks } from "./managers/messageManager.js";
|
|
2
2
|
import { type McpManagerCallbacks } from "./managers/mcpManager.js";
|
|
3
3
|
import { type BackgroundBashManagerCallbacks } from "./managers/backgroundBashManager.js";
|
|
4
|
-
import type { SlashCommand, CustomSlashCommand } from "./types.js";
|
|
5
|
-
import type { Message, Logger, McpServerStatus } from "./types.js";
|
|
4
|
+
import type { SlashCommand, CustomSlashCommand } from "./types/index.js";
|
|
5
|
+
import type { Message, Logger, McpServerStatus, Usage } from "./types/index.js";
|
|
6
6
|
/**
|
|
7
7
|
* Configuration options for Agent instances
|
|
8
8
|
*
|
|
@@ -25,6 +25,12 @@ export interface AgentOptions {
|
|
|
25
25
|
workdir?: string;
|
|
26
26
|
/**Optional custom system prompt - if provided, replaces default system prompt */
|
|
27
27
|
systemPrompt?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Optional custom directory for session file storage
|
|
30
|
+
* @default join(homedir(), ".wave", "sessions")
|
|
31
|
+
* @example "/path/to/custom/sessions"
|
|
32
|
+
*/
|
|
33
|
+
sessionDir?: string;
|
|
28
34
|
}
|
|
29
35
|
export interface AgentCallbacks extends MessageManagerCallbacks, BackgroundBashManagerCallbacks, McpManagerCallbacks {
|
|
30
36
|
}
|
|
@@ -41,20 +47,35 @@ export declare class Agent {
|
|
|
41
47
|
private hookManager;
|
|
42
48
|
private workdir;
|
|
43
49
|
private systemPrompt?;
|
|
50
|
+
private _usages;
|
|
44
51
|
private gatewayConfig;
|
|
45
52
|
private modelConfig;
|
|
46
53
|
private tokenLimit;
|
|
47
54
|
/**
|
|
48
55
|
* Agent constructor - handles configuration resolution and validation
|
|
49
56
|
*
|
|
50
|
-
* IMPORTANT:
|
|
51
|
-
*
|
|
57
|
+
* IMPORTANT: This constructor is private. Use Agent.create() instead for proper
|
|
58
|
+
* async initialization. Keep this constructor's signature exactly the same as
|
|
59
|
+
* Agent.create() to maintain API consistency.
|
|
52
60
|
*
|
|
53
61
|
* @param options - Configuration options for the Agent instance
|
|
62
|
+
* @param options.sessionDir - Optional custom directory for session storage
|
|
54
63
|
*/
|
|
55
|
-
constructor(
|
|
64
|
+
private constructor();
|
|
56
65
|
get sessionId(): string;
|
|
57
66
|
get messages(): Message[];
|
|
67
|
+
get usages(): Usage[];
|
|
68
|
+
get sessionFilePath(): string;
|
|
69
|
+
/**
|
|
70
|
+
* Rebuild usage array from messages containing usage metadata
|
|
71
|
+
* Called during session restoration to reconstruct usage tracking
|
|
72
|
+
*/
|
|
73
|
+
private rebuildUsageFromMessages;
|
|
74
|
+
/**
|
|
75
|
+
* Add usage data to the tracking array and trigger callbacks
|
|
76
|
+
* @param usage Usage data from AI operations
|
|
77
|
+
*/
|
|
78
|
+
private addUsage;
|
|
58
79
|
get latestTotalTokens(): number;
|
|
59
80
|
get userInputHistory(): string[];
|
|
60
81
|
/** Get working directory */
|
|
@@ -82,6 +103,43 @@ export declare class Agent {
|
|
|
82
103
|
* @param options - Same AgentOptions interface used by constructor
|
|
83
104
|
* @returns Promise<Agent> - Fully initialized Agent instance
|
|
84
105
|
*/
|
|
106
|
+
/**
|
|
107
|
+
* Create a new Agent instance with async initialization
|
|
108
|
+
*
|
|
109
|
+
* This is the recommended way to create Agent instances. The constructor is private
|
|
110
|
+
* to ensure proper async initialization of all components.
|
|
111
|
+
*
|
|
112
|
+
* @param options - Configuration options for the Agent instance
|
|
113
|
+
* @param options.apiKey - API key for the AI service (or set WAVE_API_KEY env var)
|
|
114
|
+
* @param options.baseURL - Base URL for the AI service (or set WAVE_BASE_URL env var)
|
|
115
|
+
* @param options.sessionDir - Optional custom directory for session file storage.
|
|
116
|
+
* If not provided, defaults to ~/.wave/sessions/. Can be relative or absolute path.
|
|
117
|
+
* Examples: "./app-sessions", "/var/myapp/sessions", "~/Documents/sessions"
|
|
118
|
+
* @param options.callbacks - Optional callbacks for various Agent events
|
|
119
|
+
* @param options.restoreSessionId - Optional session ID to restore from
|
|
120
|
+
* @param options.continueLastSession - Whether to continue the last session automatically
|
|
121
|
+
* @param options.logger - Optional custom logger implementation
|
|
122
|
+
* @param options.messages - Optional initial messages for testing convenience
|
|
123
|
+
* @param options.workdir - Working directory (defaults to process.cwd())
|
|
124
|
+
* @param options.systemPrompt - Optional custom system prompt
|
|
125
|
+
* @returns Promise that resolves to initialized Agent instance
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // Basic usage with default session directory
|
|
130
|
+
* const agent = await Agent.create({
|
|
131
|
+
* apiKey: 'your-api-key',
|
|
132
|
+
* baseURL: 'https://api.example.com'
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // Custom session directory
|
|
136
|
+
* const agent = await Agent.create({
|
|
137
|
+
* apiKey: 'your-api-key',
|
|
138
|
+
* baseURL: 'https://api.example.com',
|
|
139
|
+
* sessionDir: './app-sessions'
|
|
140
|
+
* });
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
85
143
|
static create(options: AgentOptions): Promise<Agent>;
|
|
86
144
|
/** Private initialization method, handles async initialization logic */
|
|
87
145
|
private initialize;
|
|
@@ -98,10 +156,6 @@ export declare class Agent {
|
|
|
98
156
|
abortBashCommand(): void;
|
|
99
157
|
/** Interrupt slash command execution */
|
|
100
158
|
abortSlashCommand(): void;
|
|
101
|
-
/** Interrupt all subagent execution */
|
|
102
|
-
abortSubagents(): void;
|
|
103
|
-
/** Interrupt specific subagent execution */
|
|
104
|
-
abortSubagent(subagentId: string): boolean;
|
|
105
159
|
/** Destroy managers, clean up resources */
|
|
106
160
|
destroy(): Promise<void>;
|
|
107
161
|
sendMessage(content: string, images?: Array<{
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,8BAA8B,CAAC;AAKtC,OAAO,EAAc,KAAK,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEhF,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAE7C,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,8BAA8B,CAAC;AAKtC,OAAO,EAAc,KAAK,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEhF,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAE7C,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,e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AAiB;IAIzB,uCAAuC;IAChC,gBAAgB,IAAI,IAAI;IAI/B,wCAAwC;IACjC,iBAAiB,IAAI,IAAI;IAIhC,2CAA2C;IAC9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAaxB,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC,GACjD,OAAO,CAAC,IAAI,CAAC;IAmFhB,iDAAiD;IACpC,UAAU,CACrB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,SAAS,GAAG,MAAM,GACvB,OAAO,CAAC,IAAI,CAAC;IAqChB,gCAAgC;IACzB,aAAa,IAAI,eAAe,EAAE;IAIzC,yBAAyB;IACZ,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInE,4BAA4B;IACf,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMtE,uCAAuC;IAChC,gBAAgB,IAAI,YAAY,EAAE;IAIzC,oCAAoC;IAC7B,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIlD,6BAA6B;IACtB,oBAAoB,IAAI,IAAI;IAInC,iCAAiC;IAC1B,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS;IAI1E,8BAA8B;IACvB,iBAAiB,IAAI,kBAAkB,EAAE;CAGjD"}
|
package/dist/agent.js
CHANGED
|
@@ -7,7 +7,7 @@ import { McpManager } from "./managers/mcpManager.js";
|
|
|
7
7
|
import { BashManager } from "./managers/bashManager.js";
|
|
8
8
|
import { BackgroundBashManager, } from "./managers/backgroundBashManager.js";
|
|
9
9
|
import { SlashCommandManager } from "./managers/slashCommandManager.js";
|
|
10
|
-
import { HookManager } from "./
|
|
10
|
+
import { HookManager } from "./managers/hookManager.js";
|
|
11
11
|
import { configResolver } from "./utils/configResolver.js";
|
|
12
12
|
import { configValidator } from "./utils/configValidator.js";
|
|
13
13
|
import { SkillManager } from "./managers/skillManager.js";
|
|
@@ -15,14 +15,17 @@ export class Agent {
|
|
|
15
15
|
/**
|
|
16
16
|
* Agent constructor - handles configuration resolution and validation
|
|
17
17
|
*
|
|
18
|
-
* IMPORTANT:
|
|
19
|
-
*
|
|
18
|
+
* IMPORTANT: This constructor is private. Use Agent.create() instead for proper
|
|
19
|
+
* async initialization. Keep this constructor's signature exactly the same as
|
|
20
|
+
* Agent.create() to maintain API consistency.
|
|
20
21
|
*
|
|
21
22
|
* @param options - Configuration options for the Agent instance
|
|
23
|
+
* @param options.sessionDir - Optional custom directory for session storage
|
|
22
24
|
*/
|
|
23
25
|
constructor(options) {
|
|
24
26
|
this.bashManager = null;
|
|
25
|
-
|
|
27
|
+
this._usages = []; // Usage tracking array
|
|
28
|
+
const { callbacks = {}, logger, workdir, systemPrompt, sessionDir, } = options;
|
|
26
29
|
// Resolve configuration from constructor args and environment variables
|
|
27
30
|
const gatewayConfig = configResolver.resolveGatewayConfig(options.apiKey, options.baseURL);
|
|
28
31
|
const modelConfig = configResolver.resolveModelConfig(options.agentModel, options.fastModel);
|
|
@@ -47,12 +50,13 @@ export class Agent {
|
|
|
47
50
|
mcpManager: this.mcpManager,
|
|
48
51
|
logger: this.logger,
|
|
49
52
|
}); // Initialize tool registry, pass MCP manager
|
|
50
|
-
this.hookManager = new HookManager(this.workdir, undefined,
|
|
53
|
+
this.hookManager = new HookManager(this.workdir, undefined, this.logger); // Initialize hooks manager
|
|
51
54
|
// Initialize MessageManager
|
|
52
55
|
this.messageManager = new MessageManager({
|
|
53
56
|
callbacks,
|
|
54
57
|
workdir: this.workdir,
|
|
55
58
|
logger: this.logger,
|
|
59
|
+
sessionDir,
|
|
56
60
|
});
|
|
57
61
|
// Initialize subagent manager with all dependencies in constructor
|
|
58
62
|
// IMPORTANT: Must be initialized AFTER MessageManager
|
|
@@ -64,6 +68,7 @@ export class Agent {
|
|
|
64
68
|
gatewayConfig,
|
|
65
69
|
modelConfig,
|
|
66
70
|
tokenLimit,
|
|
71
|
+
onUsageAdded: (usage) => this.addUsage(usage),
|
|
67
72
|
});
|
|
68
73
|
// Initialize AI manager with resolved configuration
|
|
69
74
|
this.aiManager = new AIManager({
|
|
@@ -72,7 +77,12 @@ export class Agent {
|
|
|
72
77
|
logger: this.logger,
|
|
73
78
|
backgroundBashManager: this.backgroundBashManager,
|
|
74
79
|
hookManager: this.hookManager,
|
|
75
|
-
callbacks
|
|
80
|
+
callbacks: {
|
|
81
|
+
...callbacks,
|
|
82
|
+
onUsageAdded: (usage) => {
|
|
83
|
+
this.addUsage(usage);
|
|
84
|
+
},
|
|
85
|
+
},
|
|
76
86
|
workdir: this.workdir,
|
|
77
87
|
systemPrompt: this.systemPrompt,
|
|
78
88
|
gatewayConfig: this.gatewayConfig,
|
|
@@ -99,6 +109,34 @@ export class Agent {
|
|
|
99
109
|
get messages() {
|
|
100
110
|
return this.messageManager.getMessages();
|
|
101
111
|
}
|
|
112
|
+
get usages() {
|
|
113
|
+
return [...this._usages]; // Return copy to prevent external modification
|
|
114
|
+
}
|
|
115
|
+
get sessionFilePath() {
|
|
116
|
+
return this.messageManager.getTranscriptPath();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Rebuild usage array from messages containing usage metadata
|
|
120
|
+
* Called during session restoration to reconstruct usage tracking
|
|
121
|
+
*/
|
|
122
|
+
rebuildUsageFromMessages() {
|
|
123
|
+
this._usages = [];
|
|
124
|
+
this.messages.forEach((message) => {
|
|
125
|
+
if (message.role === "assistant" && message.usage) {
|
|
126
|
+
this._usages.push(message.usage);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
// Trigger callback after rebuilding usage array
|
|
130
|
+
this.messageManager.triggerUsageChange();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Add usage data to the tracking array and trigger callbacks
|
|
134
|
+
* @param usage Usage data from AI operations
|
|
135
|
+
*/
|
|
136
|
+
addUsage(usage) {
|
|
137
|
+
this._usages.push(usage);
|
|
138
|
+
this.messageManager.triggerUsageChange();
|
|
139
|
+
}
|
|
102
140
|
get latestTotalTokens() {
|
|
103
141
|
return this.messageManager.getlatestTotalTokens();
|
|
104
142
|
}
|
|
@@ -138,6 +176,43 @@ export class Agent {
|
|
|
138
176
|
* @param options - Same AgentOptions interface used by constructor
|
|
139
177
|
* @returns Promise<Agent> - Fully initialized Agent instance
|
|
140
178
|
*/
|
|
179
|
+
/**
|
|
180
|
+
* Create a new Agent instance with async initialization
|
|
181
|
+
*
|
|
182
|
+
* This is the recommended way to create Agent instances. The constructor is private
|
|
183
|
+
* to ensure proper async initialization of all components.
|
|
184
|
+
*
|
|
185
|
+
* @param options - Configuration options for the Agent instance
|
|
186
|
+
* @param options.apiKey - API key for the AI service (or set WAVE_API_KEY env var)
|
|
187
|
+
* @param options.baseURL - Base URL for the AI service (or set WAVE_BASE_URL env var)
|
|
188
|
+
* @param options.sessionDir - Optional custom directory for session file storage.
|
|
189
|
+
* If not provided, defaults to ~/.wave/sessions/. Can be relative or absolute path.
|
|
190
|
+
* Examples: "./app-sessions", "/var/myapp/sessions", "~/Documents/sessions"
|
|
191
|
+
* @param options.callbacks - Optional callbacks for various Agent events
|
|
192
|
+
* @param options.restoreSessionId - Optional session ID to restore from
|
|
193
|
+
* @param options.continueLastSession - Whether to continue the last session automatically
|
|
194
|
+
* @param options.logger - Optional custom logger implementation
|
|
195
|
+
* @param options.messages - Optional initial messages for testing convenience
|
|
196
|
+
* @param options.workdir - Working directory (defaults to process.cwd())
|
|
197
|
+
* @param options.systemPrompt - Optional custom system prompt
|
|
198
|
+
* @returns Promise that resolves to initialized Agent instance
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* // Basic usage with default session directory
|
|
203
|
+
* const agent = await Agent.create({
|
|
204
|
+
* apiKey: 'your-api-key',
|
|
205
|
+
* baseURL: 'https://api.example.com'
|
|
206
|
+
* });
|
|
207
|
+
*
|
|
208
|
+
* // Custom session directory
|
|
209
|
+
* const agent = await Agent.create({
|
|
210
|
+
* apiKey: 'your-api-key',
|
|
211
|
+
* baseURL: 'https://api.example.com',
|
|
212
|
+
* sessionDir: './app-sessions'
|
|
213
|
+
* });
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
141
216
|
static async create(options) {
|
|
142
217
|
// Create Agent instance - configuration resolution and validation now happens in constructor
|
|
143
218
|
const instance = new Agent(options);
|
|
@@ -178,7 +253,7 @@ export class Agent {
|
|
|
178
253
|
// Initialize hooks configuration
|
|
179
254
|
try {
|
|
180
255
|
// Load hooks configuration from user and project settings
|
|
181
|
-
this.logger?.
|
|
256
|
+
this.logger?.debug("Loading hooks configuration...");
|
|
182
257
|
this.hookManager.loadConfigurationFromSettings();
|
|
183
258
|
this.logger?.debug("Hooks system initialized successfully");
|
|
184
259
|
}
|
|
@@ -190,10 +265,14 @@ export class Agent {
|
|
|
190
265
|
if (options?.messages) {
|
|
191
266
|
// If messages are provided, use them directly (useful for testing)
|
|
192
267
|
this.messageManager.setMessages(options.messages);
|
|
268
|
+
// Rebuild usage array from restored messages
|
|
269
|
+
this.rebuildUsageFromMessages();
|
|
193
270
|
}
|
|
194
271
|
else {
|
|
195
272
|
// Otherwise, handle session restoration
|
|
196
273
|
await this.messageManager.handleSessionRestoration(options?.restoreSessionId, options?.continueLastSession);
|
|
274
|
+
// Rebuild usage array from restored messages
|
|
275
|
+
this.rebuildUsageFromMessages();
|
|
197
276
|
}
|
|
198
277
|
}
|
|
199
278
|
abortAIMessage() {
|
|
@@ -211,10 +290,9 @@ export class Agent {
|
|
|
211
290
|
}
|
|
212
291
|
/** Unified interrupt method, interrupts both AI messages and command execution */
|
|
213
292
|
abortMessage() {
|
|
214
|
-
this.abortAIMessage();
|
|
293
|
+
this.abortAIMessage(); // This will abort tools including Task tool (subagents)
|
|
215
294
|
this.abortBashCommand();
|
|
216
295
|
this.abortSlashCommand();
|
|
217
|
-
this.abortSubagents();
|
|
218
296
|
}
|
|
219
297
|
/** Add to input history */
|
|
220
298
|
addToInputHistory(input) {
|
|
@@ -228,21 +306,12 @@ export class Agent {
|
|
|
228
306
|
abortSlashCommand() {
|
|
229
307
|
this.slashCommandManager.abortCurrentCommand();
|
|
230
308
|
}
|
|
231
|
-
/** Interrupt all subagent execution */
|
|
232
|
-
abortSubagents() {
|
|
233
|
-
this.subagentManager.abortAllInstances();
|
|
234
|
-
}
|
|
235
|
-
/** Interrupt specific subagent execution */
|
|
236
|
-
abortSubagent(subagentId) {
|
|
237
|
-
return this.subagentManager.abortInstance(subagentId);
|
|
238
|
-
}
|
|
239
309
|
/** Destroy managers, clean up resources */
|
|
240
310
|
async destroy() {
|
|
241
|
-
this.messageManager.saveSession();
|
|
242
|
-
this.abortAIMessage();
|
|
311
|
+
await this.messageManager.saveSession();
|
|
312
|
+
this.abortAIMessage(); // This will abort tools including Task tool (subagents)
|
|
243
313
|
this.abortBashCommand();
|
|
244
314
|
this.abortSlashCommand();
|
|
245
|
-
this.abortSubagents();
|
|
246
315
|
// Cleanup background bash manager
|
|
247
316
|
this.backgroundBashManager.cleanup();
|
|
248
317
|
// Cleanup MCP connections
|
|
@@ -272,10 +341,15 @@ export class Agent {
|
|
|
272
341
|
// Handle normal AI message
|
|
273
342
|
// Add user message to history
|
|
274
343
|
this.addToInputHistory(content);
|
|
275
|
-
//
|
|
344
|
+
// Add user message first, will automatically sync to UI
|
|
345
|
+
this.messageManager.addUserMessage(content, images?.map((img) => ({
|
|
346
|
+
path: img.path,
|
|
347
|
+
mimeType: img.mimeType,
|
|
348
|
+
})));
|
|
349
|
+
// Execute UserPromptSubmit hooks after adding the user message
|
|
276
350
|
if (this.hookManager) {
|
|
277
351
|
try {
|
|
278
|
-
await this.hookManager.executeHooks("UserPromptSubmit", {
|
|
352
|
+
const hookResults = await this.hookManager.executeHooks("UserPromptSubmit", {
|
|
279
353
|
event: "UserPromptSubmit",
|
|
280
354
|
projectDir: this.workdir,
|
|
281
355
|
timestamp: new Date(),
|
|
@@ -285,17 +359,19 @@ export class Agent {
|
|
|
285
359
|
cwd: this.workdir,
|
|
286
360
|
userPrompt: content,
|
|
287
361
|
});
|
|
362
|
+
// Process hook results and determine if we should continue
|
|
363
|
+
const processResult = this.hookManager.processHookResults("UserPromptSubmit", hookResults, this.messageManager);
|
|
364
|
+
// If hook processing indicates we should block (exit code 2), stop here
|
|
365
|
+
if (processResult.shouldBlock) {
|
|
366
|
+
this.logger?.info("UserPromptSubmit hook blocked prompt processing with error:", processResult.errorMessage);
|
|
367
|
+
return; // Don't send to AI
|
|
368
|
+
}
|
|
288
369
|
}
|
|
289
370
|
catch (error) {
|
|
290
371
|
this.logger?.warn("UserPromptSubmit hooks execution failed:", error);
|
|
291
372
|
// Continue processing even if hooks fail
|
|
292
373
|
}
|
|
293
374
|
}
|
|
294
|
-
// Add user message, will automatically sync to UI
|
|
295
|
-
this.messageManager.addUserMessage(content, images?.map((img) => ({
|
|
296
|
-
path: img.path,
|
|
297
|
-
mimeType: img.mimeType,
|
|
298
|
-
})));
|
|
299
375
|
// Send AI message
|
|
300
376
|
await this.aiManager.sendAIMessage();
|
|
301
377
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./services/aiService.js";
|
|
2
2
|
export * from "./services/memory.js";
|
|
3
3
|
export * from "./services/session.js";
|
|
4
|
+
export * from "./services/hook.js";
|
|
4
5
|
export * from "./agent.js";
|
|
5
6
|
export * from "./utils/bashHistory.js";
|
|
6
7
|
export * from "./utils/convertMessagesForAPI.js";
|
|
@@ -10,6 +11,6 @@ export * from "./utils/messageOperations.js";
|
|
|
10
11
|
export * from "./utils/path.js";
|
|
11
12
|
export * from "./utils/stringUtils.js";
|
|
12
13
|
export * from "./utils/customCommands.js";
|
|
13
|
-
export * from "./
|
|
14
|
-
export * from "./types.js";
|
|
14
|
+
export * from "./utils/hookMatcher.js";
|
|
15
|
+
export * from "./types/index.js";
|
|
15
16
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,YAAY,CAAC;AAG3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,kCAAkC,CAAC;AACjD,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AAGvC,cAAc,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
export * from "./services/aiService.js";
|
|
3
3
|
export * from "./services/memory.js";
|
|
4
4
|
export * from "./services/session.js";
|
|
5
|
+
export * from "./services/hook.js";
|
|
5
6
|
// Export main agent
|
|
6
7
|
export * from "./agent.js";
|
|
7
8
|
// Export all utilities
|
|
@@ -13,7 +14,6 @@ export * from "./utils/messageOperations.js";
|
|
|
13
14
|
export * from "./utils/path.js";
|
|
14
15
|
export * from "./utils/stringUtils.js";
|
|
15
16
|
export * from "./utils/customCommands.js";
|
|
16
|
-
|
|
17
|
-
export * from "./hooks/index.js";
|
|
17
|
+
export * from "./utils/hookMatcher.js";
|
|
18
18
|
// Export types
|
|
19
|
-
export * from "./types.js";
|
|
19
|
+
export * from "./types/index.js";
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { Logger, GatewayConfig, ModelConfig } from "../types.js";
|
|
1
|
+
import type { Logger, GatewayConfig, ModelConfig, Usage } from "../types/index.js";
|
|
2
2
|
import type { ToolManager } from "./toolManager.js";
|
|
3
3
|
import type { MessageManager } from "./messageManager.js";
|
|
4
4
|
import type { BackgroundBashManager } from "./backgroundBashManager.js";
|
|
5
|
-
import type { HookManager } from "
|
|
5
|
+
import type { HookManager } from "./hookManager.js";
|
|
6
6
|
export interface AIManagerCallbacks {
|
|
7
7
|
onCompressionStateChange?: (isCompressing: boolean) => void;
|
|
8
|
+
onUsageAdded?: (usage: Usage) => void;
|
|
8
9
|
}
|
|
9
10
|
export interface AIManagerOptions {
|
|
10
11
|
messageManager: MessageManager;
|
|
@@ -53,10 +54,12 @@ export declare class AIManager {
|
|
|
53
54
|
}): Promise<void>;
|
|
54
55
|
/**
|
|
55
56
|
* Execute Stop hooks when AI response cycle completes
|
|
57
|
+
* @returns Promise<boolean> - true if should continue conversation, false if should stop
|
|
56
58
|
*/
|
|
57
59
|
private executeStopHooks;
|
|
58
60
|
/**
|
|
59
61
|
* Execute PreToolUse hooks before tool execution
|
|
62
|
+
* Returns true if hooks allow tool execution, false if blocked
|
|
60
63
|
*/
|
|
61
64
|
private executePreToolUseHooks;
|
|
62
65
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiManager.d.ts","sourceRoot":"","sources":["../../src/managers/aiManager.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"aiManager.d.ts","sourceRoot":"","sources":["../../src/managers/aiManager.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,MAAM,EACN,aAAa,EACb,WAAW,EACX,KAAK,EACN,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,KAAK,EAAE,cAAc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