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.
- package/dist/agent.d.ts +32 -20
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +209 -24
- package/dist/constants/events.d.ts +28 -0
- package/dist/constants/events.d.ts.map +1 -0
- package/dist/constants/events.js +27 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/managers/aiManager.d.ts +34 -1
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +248 -132
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +7 -6
- package/dist/managers/hookManager.d.ts +13 -16
- package/dist/managers/hookManager.d.ts.map +1 -1
- package/dist/managers/hookManager.js +81 -44
- package/dist/managers/liveConfigManager.d.ts +58 -0
- package/dist/managers/liveConfigManager.d.ts.map +1 -0
- package/dist/managers/liveConfigManager.js +160 -0
- package/dist/managers/messageManager.d.ts +41 -24
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +168 -49
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +9 -3
- package/dist/managers/subagentManager.d.ts +51 -0
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +190 -19
- package/dist/services/aiService.d.ts +13 -5
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +350 -74
- package/dist/services/configurationWatcher.d.ts +120 -0
- package/dist/services/configurationWatcher.d.ts.map +1 -0
- package/dist/services/configurationWatcher.js +439 -0
- package/dist/services/fileWatcher.d.ts +69 -0
- package/dist/services/fileWatcher.d.ts.map +1 -0
- package/dist/services/fileWatcher.js +213 -0
- package/dist/services/hook.d.ts +91 -9
- package/dist/services/hook.d.ts.map +1 -1
- package/dist/services/hook.js +393 -43
- package/dist/services/jsonlHandler.d.ts +62 -0
- package/dist/services/jsonlHandler.d.ts.map +1 -0
- package/dist/services/jsonlHandler.js +257 -0
- package/dist/services/memory.d.ts +9 -0
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +81 -12
- package/dist/services/memoryStore.d.ts +81 -0
- package/dist/services/memoryStore.d.ts.map +1 -0
- package/dist/services/memoryStore.js +200 -0
- package/dist/services/session.d.ts +64 -49
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +310 -132
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +5 -4
- package/dist/tools/deleteFileTool.d.ts.map +1 -1
- package/dist/tools/deleteFileTool.js +2 -1
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +3 -2
- package/dist/tools/multiEditTool.d.ts.map +1 -1
- package/dist/tools/multiEditTool.js +4 -3
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +2 -1
- package/dist/tools/todoWriteTool.d.ts.map +1 -1
- package/dist/tools/todoWriteTool.js +3 -10
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +5 -6
- package/dist/types/commands.d.ts +4 -0
- package/dist/types/commands.d.ts.map +1 -1
- package/dist/types/core.d.ts +35 -0
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/environment.d.ts +42 -0
- package/dist/types/environment.d.ts.map +1 -0
- package/dist/types/environment.js +21 -0
- package/dist/types/hooks.d.ts +8 -2
- package/dist/types/hooks.d.ts.map +1 -1
- package/dist/types/hooks.js +8 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -0
- package/dist/types/memoryStore.d.ts +82 -0
- package/dist/types/memoryStore.d.ts.map +1 -0
- package/dist/types/memoryStore.js +7 -0
- package/dist/types/messaging.d.ts +21 -9
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/messaging.js +5 -1
- package/dist/types/session.d.ts +20 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +7 -0
- package/dist/utils/bashHistory.d.ts.map +1 -1
- package/dist/utils/bashHistory.js +27 -26
- package/dist/utils/cacheControlUtils.d.ts +121 -0
- package/dist/utils/cacheControlUtils.d.ts.map +1 -0
- package/dist/utils/cacheControlUtils.js +367 -0
- package/dist/utils/commandPathResolver.d.ts +52 -0
- package/dist/utils/commandPathResolver.d.ts.map +1 -0
- package/dist/utils/commandPathResolver.js +145 -0
- package/dist/utils/configPaths.d.ts +85 -0
- package/dist/utils/configPaths.d.ts.map +1 -0
- package/dist/utils/configPaths.js +121 -0
- package/dist/utils/configResolver.d.ts +37 -10
- package/dist/utils/configResolver.d.ts.map +1 -1
- package/dist/utils/configResolver.js +127 -23
- package/dist/utils/constants.d.ts +1 -1
- package/dist/utils/constants.js +1 -1
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
- package/dist/utils/convertMessagesForAPI.js +8 -13
- package/dist/utils/customCommands.d.ts.map +1 -1
- package/dist/utils/customCommands.js +66 -21
- package/dist/utils/fileUtils.d.ts +15 -0
- package/dist/utils/fileUtils.d.ts.map +1 -0
- package/dist/utils/fileUtils.js +61 -0
- package/dist/utils/globalLogger.d.ts +102 -0
- package/dist/utils/globalLogger.d.ts.map +1 -0
- package/dist/utils/globalLogger.js +136 -0
- package/dist/utils/hookMatcher.d.ts +1 -6
- package/dist/utils/hookMatcher.d.ts.map +1 -1
- package/dist/utils/mcpUtils.d.ts.map +1 -1
- package/dist/utils/mcpUtils.js +25 -3
- package/dist/utils/messageOperations.d.ts +27 -27
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +46 -36
- package/dist/utils/pathEncoder.d.ts +104 -0
- package/dist/utils/pathEncoder.d.ts.map +1 -0
- package/dist/utils/pathEncoder.js +272 -0
- package/dist/utils/subagentParser.d.ts.map +1 -1
- package/dist/utils/subagentParser.js +2 -1
- package/dist/utils/tokenCalculation.d.ts +26 -0
- package/dist/utils/tokenCalculation.d.ts.map +1 -0
- package/dist/utils/tokenCalculation.js +36 -0
- package/package.json +6 -3
- package/src/agent.ts +301 -37
- package/src/constants/events.ts +38 -0
- package/src/index.ts +2 -0
- package/src/managers/aiManager.ts +325 -173
- package/src/managers/backgroundBashManager.ts +7 -6
- package/src/managers/hookManager.ts +106 -84
- package/src/managers/liveConfigManager.ts +248 -0
- package/src/managers/messageManager.ts +237 -100
- package/src/managers/slashCommandManager.ts +9 -7
- package/src/managers/subagentManager.ts +284 -22
- package/src/services/aiService.ts +474 -83
- package/src/services/configurationWatcher.ts +622 -0
- package/src/services/fileWatcher.ts +301 -0
- package/src/services/hook.ts +538 -47
- package/src/services/jsonlHandler.ts +319 -0
- package/src/services/memory.ts +92 -12
- package/src/services/memoryStore.ts +279 -0
- package/src/services/session.ts +381 -157
- package/src/tools/bashTool.ts +5 -4
- package/src/tools/deleteFileTool.ts +2 -1
- package/src/tools/editTool.ts +3 -2
- package/src/tools/multiEditTool.ts +4 -3
- package/src/tools/readTool.ts +2 -1
- package/src/tools/todoWriteTool.ts +3 -11
- package/src/tools/writeTool.ts +7 -6
- package/src/types/commands.ts +6 -0
- package/src/types/core.ts +44 -0
- package/src/types/environment.ts +60 -0
- package/src/types/hooks.ts +21 -8
- package/src/types/index.ts +2 -0
- package/src/types/memoryStore.ts +94 -0
- package/src/types/messaging.ts +21 -10
- package/src/types/session.ts +25 -0
- package/src/utils/bashHistory.ts +27 -27
- package/src/utils/cacheControlUtils.ts +540 -0
- package/src/utils/commandPathResolver.ts +189 -0
- package/src/utils/configPaths.ts +163 -0
- package/src/utils/configResolver.ts +182 -22
- package/src/utils/constants.ts +1 -1
- package/src/utils/convertMessagesForAPI.ts +8 -14
- package/src/utils/customCommands.ts +90 -22
- package/src/utils/fileUtils.ts +65 -0
- package/src/utils/globalLogger.ts +145 -0
- package/src/utils/hookMatcher.ts +1 -12
- package/src/utils/mcpUtils.ts +34 -3
- package/src/utils/messageOperations.ts +77 -60
- package/src/utils/pathEncoder.ts +379 -0
- package/src/utils/subagentParser.ts +2 -1
- package/src/utils/tokenCalculation.ts +43 -0
- package/src/types/index.ts.backup +0 -357
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import type { SubagentConfiguration } from "../utils/subagentParser.js";
|
|
2
2
|
import type { Message, Logger, GatewayConfig, ModelConfig, Usage } from "../types/index.js";
|
|
3
|
+
import type { SessionData } from "../services/session.js";
|
|
3
4
|
import { AIManager } from "./aiManager.js";
|
|
4
5
|
import { MessageManager } from "./messageManager.js";
|
|
5
6
|
import { ToolManager } from "./toolManager.js";
|
|
7
|
+
import { HookManager } from "./hookManager.js";
|
|
8
|
+
import { UserMessageParams, type AgentToolBlockUpdateParams } from "../utils/messageOperations.js";
|
|
9
|
+
export interface SubagentManagerCallbacks {
|
|
10
|
+
/** Triggered when subagent adds user message */
|
|
11
|
+
onSubagentUserMessageAdded?: (subagentId: string, params: UserMessageParams) => void;
|
|
12
|
+
/** Triggered when subagent creates assistant message */
|
|
13
|
+
onSubagentAssistantMessageAdded?: (subagentId: string) => void;
|
|
14
|
+
/** Triggered during subagent content streaming updates */
|
|
15
|
+
onSubagentAssistantContentUpdated?: (subagentId: string, chunk: string, accumulated: string) => void;
|
|
16
|
+
/** Triggered when subagent tool block is updated */
|
|
17
|
+
onSubagentToolBlockUpdated?: (subagentId: string, params: AgentToolBlockUpdateParams) => void;
|
|
18
|
+
/** Triggered when subagent messages change */
|
|
19
|
+
onSubagentMessagesChange?: (subagentId: string, messages: Message[]) => void;
|
|
20
|
+
}
|
|
6
21
|
export interface SubagentInstance {
|
|
7
22
|
subagentId: string;
|
|
8
23
|
configuration: SubagentConfiguration;
|
|
@@ -11,15 +26,18 @@ export interface SubagentInstance {
|
|
|
11
26
|
toolManager: ToolManager;
|
|
12
27
|
status: "initializing" | "active" | "completed" | "error" | "aborted";
|
|
13
28
|
messages: Message[];
|
|
29
|
+
subagentType: string;
|
|
14
30
|
}
|
|
15
31
|
export interface SubagentManagerOptions {
|
|
16
32
|
workdir: string;
|
|
17
33
|
parentToolManager: ToolManager;
|
|
18
34
|
parentMessageManager: MessageManager;
|
|
35
|
+
callbacks?: SubagentManagerCallbacks;
|
|
19
36
|
logger?: Logger;
|
|
20
37
|
gatewayConfig: GatewayConfig;
|
|
21
38
|
modelConfig: ModelConfig;
|
|
22
39
|
tokenLimit: number;
|
|
40
|
+
hookManager?: HookManager;
|
|
23
41
|
onUsageAdded?: (usage: Usage) => void;
|
|
24
42
|
}
|
|
25
43
|
export declare class SubagentManager {
|
|
@@ -28,10 +46,12 @@ export declare class SubagentManager {
|
|
|
28
46
|
private workdir;
|
|
29
47
|
private parentToolManager;
|
|
30
48
|
private parentMessageManager;
|
|
49
|
+
private callbacks?;
|
|
31
50
|
private logger?;
|
|
32
51
|
private gatewayConfig;
|
|
33
52
|
private modelConfig;
|
|
34
53
|
private tokenLimit;
|
|
54
|
+
private hookManager?;
|
|
35
55
|
private onUsageAdded?;
|
|
36
56
|
constructor(options: SubagentManagerOptions);
|
|
37
57
|
/**
|
|
@@ -89,5 +109,36 @@ export declare class SubagentManager {
|
|
|
89
109
|
* Clean up all instances (for session end)
|
|
90
110
|
*/
|
|
91
111
|
cleanup(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Restore subagent instances from saved session data
|
|
114
|
+
* This method is called during agent initialization to restore previous subagent states
|
|
115
|
+
*/
|
|
116
|
+
restoreSubagentSessions(subagentSessions: Array<{
|
|
117
|
+
sessionData: SessionData;
|
|
118
|
+
subagentId: string;
|
|
119
|
+
configuration: SubagentConfiguration;
|
|
120
|
+
}>): Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Create subagent callbacks for a specific subagent ID
|
|
123
|
+
* Extracted to reuse in both create and restore flows
|
|
124
|
+
*/
|
|
125
|
+
private createSubagentCallbacks;
|
|
126
|
+
/**
|
|
127
|
+
* Update configuration for SubagentManager and all active subagents
|
|
128
|
+
* This method updates configuration for live config reload support
|
|
129
|
+
* @param newGatewayConfig - New gateway configuration
|
|
130
|
+
* @param newModelConfig - New model configuration
|
|
131
|
+
* @param newTokenLimit - New token limit
|
|
132
|
+
*/
|
|
133
|
+
updateConfiguration(newGatewayConfig: GatewayConfig, newModelConfig: ModelConfig, newTokenLimit: number): void;
|
|
134
|
+
/**
|
|
135
|
+
* Get current configuration for debugging
|
|
136
|
+
*/
|
|
137
|
+
getCurrentConfiguration(): {
|
|
138
|
+
gatewayConfig: GatewayConfig;
|
|
139
|
+
modelConfig: ModelConfig;
|
|
140
|
+
tokenLimit: number;
|
|
141
|
+
activeSubagents: number;
|
|
142
|
+
};
|
|
92
143
|
}
|
|
93
144
|
//# sourceMappingURL=subagentManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subagentManager.d.ts","sourceRoot":"","sources":["../../src/managers/subagentManager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,aAAa,EACb,WAAW,EACX,KAAK,EACN,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,
|
|
1
|
+
{"version":3,"file":"subagentManager.d.ts","sourceRoot":"","sources":["../../src/managers/subagentManager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,aAAa,EACb,WAAW,EACX,KAAK,EACN,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,E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,UAAU,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI;IAIxD;;OAEG;IACH,oBAAoB,CAClB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,GACjC,IAAI;IAOP;;OAEG;IACH,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAOhE;;OAEG;IACH,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAYzC;;OAEG;IACH,kBAAkB,IAAI,gBAAgB,EAAE;IAOxC;;OAEG;IACH,OAAO,IAAI,IAAI;IAIf;;;OAGG;IACG,uBAAuB,CAC3B,gBAAgB,EAAE,KAAK,CAAC;QACtB,WAAW,EAAE,WAAW,CAAC;QACzB,UAAU,EAAE,MAAM,CAAC;QACnB,aAAa,EAAE,qBAAqB,CAAC;KACtC,CAAC,GACD,OAAO,CAAC,IAAI,CAAC;IAsFhB;;;OAGG;IACH,OAAO,CAAC,uBAAuB;IAuD/B;;;;;;OAMG;IACH,mBAAmB,CACjB,gBAAgB,EAAE,aAAa,EAC/B,cAAc,EAAE,WAAW,EAC3B,aAAa,EAAE,MAAM,GACpB,IAAI;IA+CP;;OAEG;IACH,uBAAuB,IAAI;QACzB,aAAa,EAAE,aAAa,CAAC;QAC7B,WAAW,EAAE,WAAW,CAAC;QACzB,UAAU,EAAE,MAAM,CAAC;QACnB,eAAe,EAAE,MAAM,CAAC;KACzB;CAaF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "crypto";
|
|
2
2
|
import { AIManager } from "./aiManager.js";
|
|
3
|
-
import { MessageManager
|
|
3
|
+
import { MessageManager } from "./messageManager.js";
|
|
4
4
|
export class SubagentManager {
|
|
5
5
|
constructor(options) {
|
|
6
6
|
this.instances = new Map();
|
|
@@ -8,10 +8,12 @@ export class SubagentManager {
|
|
|
8
8
|
this.workdir = options.workdir;
|
|
9
9
|
this.parentToolManager = options.parentToolManager;
|
|
10
10
|
this.parentMessageManager = options.parentMessageManager;
|
|
11
|
+
this.callbacks = options.callbacks; // Store SubagentManagerCallbacks
|
|
11
12
|
this.logger = options.logger;
|
|
12
13
|
this.gatewayConfig = options.gatewayConfig;
|
|
13
14
|
this.modelConfig = options.modelConfig;
|
|
14
15
|
this.tokenLimit = options.tokenLimit;
|
|
16
|
+
this.hookManager = options.hookManager;
|
|
15
17
|
this.onUsageAdded = options.onUsageAdded;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
@@ -58,23 +60,14 @@ export class SubagentManager {
|
|
|
58
60
|
}
|
|
59
61
|
const subagentId = randomUUID();
|
|
60
62
|
// Create isolated MessageManager for the subagent
|
|
61
|
-
const subagentCallbacks =
|
|
62
|
-
// These callbacks will be handled by the parent agent
|
|
63
|
-
onMessagesChange: (messages) => {
|
|
64
|
-
const instance = this.instances.get(subagentId);
|
|
65
|
-
if (instance) {
|
|
66
|
-
instance.messages = messages;
|
|
67
|
-
// Update parent's subagent block with latest messages
|
|
68
|
-
this.parentMessageManager.updateSubagentBlock(subagentId, {
|
|
69
|
-
messages: messages,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
};
|
|
63
|
+
const subagentCallbacks = this.createSubagentCallbacks(subagentId);
|
|
74
64
|
const messageManager = new MessageManager({
|
|
75
65
|
callbacks: subagentCallbacks,
|
|
76
66
|
workdir: this.workdir,
|
|
77
67
|
logger: this.logger,
|
|
68
|
+
sessionType: "subagent",
|
|
69
|
+
parentSessionId: this.parentMessageManager.getSessionId(),
|
|
70
|
+
subagentType: parameters.subagent_type,
|
|
78
71
|
});
|
|
79
72
|
// Use the parent tool manager directly - tool restrictions will be handled by allowedTools parameter
|
|
80
73
|
const toolManager = this.parentToolManager;
|
|
@@ -89,6 +82,8 @@ export class SubagentManager {
|
|
|
89
82
|
logger: this.logger,
|
|
90
83
|
workdir: this.workdir,
|
|
91
84
|
systemPrompt: configuration.systemPrompt,
|
|
85
|
+
subagentType: parameters.subagent_type, // Pass subagent type for hook context
|
|
86
|
+
hookManager: this.hookManager,
|
|
92
87
|
gatewayConfig: this.gatewayConfig,
|
|
93
88
|
modelConfig: {
|
|
94
89
|
...this.modelConfig,
|
|
@@ -107,10 +102,11 @@ export class SubagentManager {
|
|
|
107
102
|
toolManager,
|
|
108
103
|
status: "initializing",
|
|
109
104
|
messages: [],
|
|
105
|
+
subagentType: parameters.subagent_type, // Store the subagent type
|
|
110
106
|
};
|
|
111
107
|
this.instances.set(subagentId, instance);
|
|
112
108
|
// Create subagent block in parent message manager
|
|
113
|
-
this.parentMessageManager.addSubagentBlock(subagentId, configuration.name, "active",
|
|
109
|
+
this.parentMessageManager.addSubagentBlock(subagentId, configuration.name, messageManager.getSessionId(), configuration, "active", parameters);
|
|
114
110
|
return instance;
|
|
115
111
|
}
|
|
116
112
|
/**
|
|
@@ -136,12 +132,11 @@ export class SubagentManager {
|
|
|
136
132
|
this.updateInstanceStatus(instance.subagentId, "aborted");
|
|
137
133
|
this.parentMessageManager.updateSubagentBlock(instance.subagentId, {
|
|
138
134
|
status: "aborted",
|
|
139
|
-
messages: instance.messages,
|
|
140
135
|
});
|
|
141
136
|
});
|
|
142
137
|
}
|
|
143
138
|
// Add the user's prompt as a message
|
|
144
|
-
instance.messageManager.addUserMessage(prompt);
|
|
139
|
+
instance.messageManager.addUserMessage({ content: prompt });
|
|
145
140
|
// Create allowed tools list - always exclude Task tool to prevent subagent recursion
|
|
146
141
|
let allowedTools = instance.configuration.tools;
|
|
147
142
|
// Always filter out the Task tool to prevent subagents from creating sub-subagents
|
|
@@ -192,11 +187,10 @@ export class SubagentManager {
|
|
|
192
187
|
// Extract text content from the last assistant message
|
|
193
188
|
const textBlocks = lastAssistantMessage.blocks.filter((block) => block.type === "text");
|
|
194
189
|
const response = textBlocks.map((block) => block.content).join("\n");
|
|
195
|
-
// Update status to completed and update parent
|
|
190
|
+
// Update status to completed and update parent
|
|
196
191
|
this.updateInstanceStatus(instance.subagentId, "completed");
|
|
197
192
|
this.parentMessageManager.updateSubagentBlock(instance.subagentId, {
|
|
198
193
|
status: "completed",
|
|
199
|
-
messages: messages,
|
|
200
194
|
});
|
|
201
195
|
return response || "Task completed with no text response";
|
|
202
196
|
}
|
|
@@ -256,4 +250,181 @@ export class SubagentManager {
|
|
|
256
250
|
cleanup() {
|
|
257
251
|
this.instances.clear();
|
|
258
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Restore subagent instances from saved session data
|
|
255
|
+
* This method is called during agent initialization to restore previous subagent states
|
|
256
|
+
*/
|
|
257
|
+
async restoreSubagentSessions(subagentSessions) {
|
|
258
|
+
for (const { sessionData, subagentId, configuration } of subagentSessions) {
|
|
259
|
+
try {
|
|
260
|
+
// Use the configuration from the SubagentBlock
|
|
261
|
+
// Create the subagent instance without executing - just restore the state
|
|
262
|
+
// Create MessageManager for the restored subagent
|
|
263
|
+
const subagentCallbacks = this.createSubagentCallbacks(subagentId);
|
|
264
|
+
const messageManager = new MessageManager({
|
|
265
|
+
callbacks: subagentCallbacks,
|
|
266
|
+
workdir: this.workdir,
|
|
267
|
+
logger: this.logger,
|
|
268
|
+
sessionType: "subagent",
|
|
269
|
+
parentSessionId: this.parentMessageManager.getSessionId(),
|
|
270
|
+
subagentType: configuration.name, // Use configuration name for restored sessions
|
|
271
|
+
});
|
|
272
|
+
// Use the parent tool manager
|
|
273
|
+
const toolManager = this.parentToolManager;
|
|
274
|
+
// Determine model to use
|
|
275
|
+
const modelToUse = configuration.model && configuration.model !== "inherit"
|
|
276
|
+
? configuration.model
|
|
277
|
+
: this.modelConfig.agentModel;
|
|
278
|
+
// Create AIManager for the restored subagent
|
|
279
|
+
const aiManager = new AIManager({
|
|
280
|
+
messageManager,
|
|
281
|
+
toolManager,
|
|
282
|
+
logger: this.logger,
|
|
283
|
+
workdir: this.workdir,
|
|
284
|
+
systemPrompt: configuration.systemPrompt,
|
|
285
|
+
subagentType: configuration.name, // Use configuration name as subagent type for restored instances
|
|
286
|
+
hookManager: this.hookManager,
|
|
287
|
+
gatewayConfig: this.gatewayConfig,
|
|
288
|
+
modelConfig: {
|
|
289
|
+
...this.modelConfig,
|
|
290
|
+
agentModel: modelToUse,
|
|
291
|
+
},
|
|
292
|
+
tokenLimit: this.tokenLimit,
|
|
293
|
+
callbacks: {
|
|
294
|
+
onUsageAdded: this.onUsageAdded,
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
// Create restored instance
|
|
298
|
+
const instance = {
|
|
299
|
+
subagentId,
|
|
300
|
+
configuration,
|
|
301
|
+
aiManager,
|
|
302
|
+
messageManager,
|
|
303
|
+
toolManager,
|
|
304
|
+
status: "completed", // Restored sessions are considered completed
|
|
305
|
+
messages: sessionData.messages,
|
|
306
|
+
subagentType: configuration.name, // Use configuration name as subagent type for restored instances
|
|
307
|
+
};
|
|
308
|
+
// IMPORTANT: Store instance in map BEFORE calling setMessages
|
|
309
|
+
// This ensures the callback can find the instance
|
|
310
|
+
this.instances.set(subagentId, instance);
|
|
311
|
+
// Now restore the session data including sessionId, which will trigger the callback chain
|
|
312
|
+
const sessionDataObj = {
|
|
313
|
+
id: sessionData.id,
|
|
314
|
+
messages: sessionData.messages,
|
|
315
|
+
version: "1.0.0",
|
|
316
|
+
metadata: {
|
|
317
|
+
workdir: this.workdir,
|
|
318
|
+
startedAt: new Date().toISOString(),
|
|
319
|
+
lastActiveAt: new Date().toISOString(),
|
|
320
|
+
latestTotalTokens: 0,
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
messageManager.initializeFromSession(sessionDataObj);
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
this.logger?.warn(`Failed to restore subagent session ${subagentId}:`, error);
|
|
327
|
+
// Continue with other sessions even if one fails
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Create subagent callbacks for a specific subagent ID
|
|
333
|
+
* Extracted to reuse in both create and restore flows
|
|
334
|
+
*/
|
|
335
|
+
createSubagentCallbacks(subagentId) {
|
|
336
|
+
return {
|
|
337
|
+
onUserMessageAdded: (params) => {
|
|
338
|
+
// Forward user message events to parent via SubagentManager callbacks
|
|
339
|
+
if (this.callbacks?.onSubagentUserMessageAdded) {
|
|
340
|
+
this.callbacks.onSubagentUserMessageAdded(subagentId, params);
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
onAssistantMessageAdded: () => {
|
|
344
|
+
// Forward assistant message events to parent via SubagentManager callbacks
|
|
345
|
+
if (this.callbacks?.onSubagentAssistantMessageAdded) {
|
|
346
|
+
this.callbacks.onSubagentAssistantMessageAdded(subagentId);
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
onAssistantContentUpdated: (chunk, accumulated) => {
|
|
350
|
+
// Forward assistant content updates to parent via SubagentManager callbacks
|
|
351
|
+
if (this.callbacks?.onSubagentAssistantContentUpdated) {
|
|
352
|
+
this.callbacks.onSubagentAssistantContentUpdated(subagentId, chunk, accumulated);
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
onToolBlockUpdated: (params) => {
|
|
356
|
+
// Forward tool block updates to parent via SubagentManager callbacks
|
|
357
|
+
if (this.callbacks?.onSubagentToolBlockUpdated) {
|
|
358
|
+
this.callbacks.onSubagentToolBlockUpdated(subagentId, params);
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
// These callbacks will be handled by the parent agent
|
|
362
|
+
onMessagesChange: (messages) => {
|
|
363
|
+
const instance = this.instances.get(subagentId);
|
|
364
|
+
if (instance) {
|
|
365
|
+
instance.messages = messages;
|
|
366
|
+
// Forward subagent message changes to parent via callbacks
|
|
367
|
+
if (this.callbacks?.onSubagentMessagesChange) {
|
|
368
|
+
this.callbacks.onSubagentMessagesChange(subagentId, messages);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
onSessionIdChange: (newSessionId) => {
|
|
373
|
+
// Update the subagent block with the new session ID
|
|
374
|
+
this.parentMessageManager.updateSubagentBlock(subagentId, {
|
|
375
|
+
sessionId: newSessionId,
|
|
376
|
+
});
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Update configuration for SubagentManager and all active subagents
|
|
382
|
+
* This method updates configuration for live config reload support
|
|
383
|
+
* @param newGatewayConfig - New gateway configuration
|
|
384
|
+
* @param newModelConfig - New model configuration
|
|
385
|
+
* @param newTokenLimit - New token limit
|
|
386
|
+
*/
|
|
387
|
+
updateConfiguration(newGatewayConfig, newModelConfig, newTokenLimit) {
|
|
388
|
+
this.logger?.info("Live Config: Updating SubagentManager configuration");
|
|
389
|
+
// Update stored configuration
|
|
390
|
+
this.gatewayConfig = newGatewayConfig;
|
|
391
|
+
this.modelConfig = newModelConfig;
|
|
392
|
+
this.tokenLimit = newTokenLimit;
|
|
393
|
+
// Update all active subagent AIManager instances
|
|
394
|
+
let updatedCount = 0;
|
|
395
|
+
for (const [subagentId, instance] of this.instances.entries()) {
|
|
396
|
+
if (instance.status === "active" || instance.status === "initializing") {
|
|
397
|
+
try {
|
|
398
|
+
// For subagents, we need to preserve their model if it was explicitly set
|
|
399
|
+
const subagentModelConfig = {
|
|
400
|
+
...newModelConfig,
|
|
401
|
+
// If subagent has its own model configured, preserve it
|
|
402
|
+
agentModel: instance.configuration.model &&
|
|
403
|
+
instance.configuration.model !== "inherit"
|
|
404
|
+
? instance.configuration.model
|
|
405
|
+
: newModelConfig.agentModel,
|
|
406
|
+
};
|
|
407
|
+
instance.aiManager.updateConfiguration(newGatewayConfig, subagentModelConfig, newTokenLimit);
|
|
408
|
+
updatedCount++;
|
|
409
|
+
this.logger?.debug(`Live Config: Updated configuration for subagent ${subagentId}`);
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
this.logger?.error(`Live Config: Failed to update configuration for subagent ${subagentId}: ${error.message}`);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
this.logger?.info(`Live Config: SubagentManager configuration updated - ${updatedCount} active subagents updated`);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Get current configuration for debugging
|
|
420
|
+
*/
|
|
421
|
+
getCurrentConfiguration() {
|
|
422
|
+
const activeSubagents = Array.from(this.instances.values()).filter((instance) => instance.status === "active" || instance.status === "initializing").length;
|
|
423
|
+
return {
|
|
424
|
+
gatewayConfig: { ...this.gatewayConfig },
|
|
425
|
+
modelConfig: { ...this.modelConfig },
|
|
426
|
+
tokenLimit: this.tokenLimit,
|
|
427
|
+
activeSubagents,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
259
430
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ChatCompletionMessageToolCall } from "openai/resources";
|
|
2
2
|
import { ChatCompletionMessageParam, ChatCompletionFunctionTool } from "openai/resources.js";
|
|
3
3
|
import type { GatewayConfig, ModelConfig } from "../types/index.js";
|
|
4
|
+
import { type ClaudeUsage } from "../utils/cacheControlUtils.js";
|
|
4
5
|
export interface CallAgentOptions {
|
|
5
6
|
gatewayConfig: GatewayConfig;
|
|
6
7
|
modelConfig: ModelConfig;
|
|
@@ -12,15 +13,22 @@ export interface CallAgentOptions {
|
|
|
12
13
|
tools?: ChatCompletionFunctionTool[];
|
|
13
14
|
model?: string;
|
|
14
15
|
systemPrompt?: string;
|
|
16
|
+
onContentUpdate?: (content: string) => void;
|
|
17
|
+
onToolUpdate?: (toolCall: {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
parameters: string;
|
|
21
|
+
parametersChunk?: string;
|
|
22
|
+
stage?: "start" | "streaming" | "running" | "end";
|
|
23
|
+
}) => void;
|
|
15
24
|
}
|
|
16
25
|
export interface CallAgentResult {
|
|
17
26
|
content?: string;
|
|
18
27
|
tool_calls?: ChatCompletionMessageToolCall[];
|
|
19
|
-
usage?:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
};
|
|
28
|
+
usage?: ClaudeUsage;
|
|
29
|
+
finish_reason?: "stop" | "length" | "tool_calls" | "content_filter" | "function_call" | null;
|
|
30
|
+
response_headers?: Record<string, string>;
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
24
32
|
}
|
|
25
33
|
export declare function callAgent(options: CallAgentOptions): Promise<CallAgentResult>;
|
|
26
34
|
export interface CompressMessagesOptions {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aiService.d.ts","sourceRoot":"","sources":["../../src/services/aiService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,
|
|
1
|
+
{"version":3,"file":"aiService.d.ts","sourceRoot":"","sources":["../../src/services/aiService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAGL,0BAA0B,EAC1B,0BAA0B,EAE3B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAKL,KAAK,WAAW,EACjB,MAAM,+BAA+B,CAAC;AAkEvC,MAAM,WAAW,gBAAgB;IAE/B,aAAa,EAAE,aAAa,CAAC;IAC7B,WAAW,EAAE,WAAW,CAAC;IAGzB,QAAQ,EAAE,0BAA0B,EAAE,CAAC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,0BAA0B,EAAE,CAAC;IACrC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IAGtB,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE;QACxB,EAAE,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