wave-agent-sdk 0.5.0 → 0.6.2
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 +14 -11
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +64 -151
- package/dist/constants/subagents.d.ts +5 -0
- package/dist/constants/subagents.d.ts.map +1 -0
- package/dist/constants/subagents.js +4 -0
- package/dist/constants/tools.d.ts +4 -1
- package/dist/constants/tools.d.ts.map +1 -1
- package/dist/constants/tools.js +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/managers/aiManager.d.ts +2 -5
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +43 -48
- package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
- package/dist/managers/backgroundTaskManager.js +63 -53
- package/dist/managers/foregroundTaskManager.d.ts.map +1 -1
- package/dist/managers/foregroundTaskManager.js +3 -2
- package/dist/managers/mcpManager.d.ts.map +1 -1
- package/dist/managers/messageManager.d.ts +13 -27
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +94 -89
- package/dist/managers/permissionManager.d.ts.map +1 -1
- package/dist/managers/permissionManager.js +25 -15
- package/dist/managers/planManager.d.ts +1 -1
- package/dist/managers/planManager.d.ts.map +1 -1
- package/dist/managers/planManager.js +2 -2
- package/dist/managers/reversionManager.d.ts.map +1 -1
- package/dist/managers/reversionManager.js +23 -2
- package/dist/managers/slashCommandManager.d.ts +3 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +8 -3
- package/dist/managers/subagentManager.d.ts +8 -14
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +46 -112
- package/dist/managers/toolManager.d.ts +11 -0
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +20 -2
- package/dist/{constants/prompts.d.ts → prompts/index.d.ts} +17 -15
- package/dist/prompts/index.d.ts.map +1 -0
- package/dist/prompts/index.js +309 -0
- package/dist/services/aiService.d.ts +0 -1
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +4 -140
- package/dist/services/memory.d.ts +0 -3
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +0 -59
- package/dist/services/session.d.ts +15 -1
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +57 -1
- package/dist/services/taskManager.d.ts +25 -0
- package/dist/services/taskManager.d.ts.map +1 -0
- package/dist/services/taskManager.js +164 -0
- package/dist/tools/askUserQuestion.d.ts.map +1 -1
- package/dist/tools/askUserQuestion.js +39 -25
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +13 -11
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +2 -1
- package/dist/tools/exitPlanMode.d.ts.map +1 -1
- package/dist/tools/exitPlanMode.js +26 -2
- package/dist/tools/globTool.d.ts.map +1 -1
- package/dist/tools/globTool.js +8 -2
- package/dist/tools/grepTool.d.ts.map +1 -1
- package/dist/tools/grepTool.js +17 -6
- package/dist/tools/lsTool.d.ts.map +1 -1
- package/dist/tools/lsTool.js +3 -1
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +16 -1
- package/dist/tools/taskManagementTools.d.ts +6 -0
- package/dist/tools/taskManagementTools.d.ts.map +1 -0
- package/dist/tools/taskManagementTools.js +461 -0
- package/dist/tools/taskOutputTool.d.ts.map +1 -1
- package/dist/tools/taskOutputTool.js +32 -8
- package/dist/tools/taskStopTool.d.ts.map +1 -1
- package/dist/tools/taskStopTool.js +7 -1
- package/dist/tools/taskTool.d.ts.map +1 -1
- package/dist/tools/taskTool.js +37 -2
- package/dist/tools/types.d.ts +11 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +9 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/messaging.d.ts +2 -18
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/processes.d.ts +16 -6
- package/dist/types/processes.d.ts.map +1 -1
- package/dist/types/tasks.d.ts +13 -0
- package/dist/types/tasks.d.ts.map +1 -0
- package/dist/types/tasks.js +1 -0
- package/dist/types/tools.d.ts +4 -1
- package/dist/types/tools.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.js +59 -44
- package/dist/utils/cacheControlUtils.d.ts.map +1 -1
- package/dist/utils/cacheControlUtils.js +18 -12
- package/dist/utils/constants.d.ts +0 -4
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +0 -4
- package/dist/utils/convertMessagesForAPI.js +2 -2
- package/dist/utils/editUtils.d.ts.map +1 -1
- package/dist/utils/editUtils.js +2 -2
- package/dist/utils/gitUtils.d.ts +7 -0
- package/dist/utils/gitUtils.d.ts.map +1 -0
- package/dist/utils/gitUtils.js +24 -0
- package/dist/utils/messageOperations.d.ts +3 -58
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +4 -146
- package/dist/utils/nameGenerator.d.ts +1 -1
- package/dist/utils/nameGenerator.d.ts.map +1 -1
- package/dist/utils/nameGenerator.js +19 -3
- package/package.json +1 -1
- package/src/agent.ts +86 -183
- package/src/constants/subagents.ts +4 -0
- package/src/constants/tools.ts +4 -1
- package/src/index.ts +1 -0
- package/src/managers/aiManager.ts +63 -70
- package/src/managers/backgroundTaskManager.ts +58 -54
- package/src/managers/foregroundTaskManager.ts +3 -2
- package/src/managers/mcpManager.ts +6 -3
- package/src/managers/messageManager.ts +126 -142
- package/src/managers/permissionManager.ts +32 -21
- package/src/managers/planManager.ts +2 -2
- package/src/managers/reversionManager.ts +26 -2
- package/src/managers/slashCommandManager.ts +12 -3
- package/src/managers/subagentManager.ts +60 -144
- package/src/managers/toolManager.ts +32 -2
- package/src/prompts/index.ts +366 -0
- package/src/services/aiService.ts +3 -145
- package/src/services/memory.ts +0 -72
- package/src/services/session.ts +73 -0
- package/src/services/taskManager.ts +195 -0
- package/src/tools/askUserQuestion.ts +51 -29
- package/src/tools/bashTool.ts +15 -17
- package/src/tools/editTool.ts +3 -1
- package/src/tools/exitPlanMode.ts +27 -3
- package/src/tools/globTool.ts +10 -2
- package/src/tools/grepTool.ts +17 -6
- package/src/tools/lsTool.ts +3 -1
- package/src/tools/readTool.ts +17 -1
- package/src/tools/taskManagementTools.ts +516 -0
- package/src/tools/taskOutputTool.ts +34 -12
- package/src/tools/taskStopTool.ts +7 -1
- package/src/tools/taskTool.ts +45 -1
- package/src/tools/types.ts +12 -0
- package/src/tools/writeTool.ts +9 -2
- package/src/types/index.ts +1 -0
- package/src/types/messaging.ts +1 -21
- package/src/types/processes.ts +18 -7
- package/src/types/tasks.ts +13 -0
- package/src/types/tools.ts +4 -1
- package/src/utils/builtinSubagents.ts +81 -45
- package/src/utils/cacheControlUtils.ts +26 -18
- package/src/utils/constants.ts +0 -5
- package/src/utils/convertMessagesForAPI.ts +2 -2
- package/src/utils/editUtils.ts +2 -6
- package/src/utils/gitUtils.ts +24 -0
- package/src/utils/messageOperations.ts +6 -229
- package/src/utils/nameGenerator.ts +20 -3
- package/dist/constants/prompts.d.ts.map +0 -1
- package/dist/constants/prompts.js +0 -118
- package/dist/tools/todoWriteTool.d.ts +0 -6
- package/dist/tools/todoWriteTool.d.ts.map +0 -1
- package/dist/tools/todoWriteTool.js +0 -220
- package/src/constants/prompts.ts +0 -155
- package/src/tools/todoWriteTool.ts +0 -257
package/src/agent.ts
CHANGED
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
SubagentManager,
|
|
10
10
|
type SubagentManagerCallbacks,
|
|
11
11
|
} from "./managers/subagentManager.js";
|
|
12
|
-
import * as memory from "./services/memory.js";
|
|
13
12
|
import { McpManager, type McpManagerCallbacks } from "./managers/mcpManager.js";
|
|
14
13
|
import { LspManager } from "./managers/lspManager.js";
|
|
15
14
|
import { BashManager } from "./managers/bashManager.js";
|
|
@@ -46,11 +45,11 @@ import { MemoryRuleManager } from "./managers/MemoryRuleManager.js";
|
|
|
46
45
|
import { LiveConfigManager } from "./managers/liveConfigManager.js";
|
|
47
46
|
import { configValidator } from "./utils/configValidator.js";
|
|
48
47
|
import { SkillManager } from "./managers/skillManager.js";
|
|
48
|
+
import { TaskManager } from "./services/taskManager.js";
|
|
49
49
|
import {
|
|
50
50
|
loadSessionFromJsonl,
|
|
51
51
|
handleSessionRestoration,
|
|
52
52
|
} from "./services/session.js";
|
|
53
|
-
import type { SubagentConfiguration } from "./utils/subagentParser.js";
|
|
54
53
|
import { setGlobalLogger } from "./utils/globalLogger.js";
|
|
55
54
|
import { ConfigurationService } from "./services/configurationService.js";
|
|
56
55
|
import * as fs from "fs/promises";
|
|
@@ -107,7 +106,12 @@ export interface AgentCallbacks
|
|
|
107
106
|
McpManagerCallbacks,
|
|
108
107
|
SubagentManagerCallbacks {
|
|
109
108
|
onTasksChange?: (tasks: BackgroundTask[]) => void;
|
|
109
|
+
onSessionTasksChange?: (tasks: import("./types/tasks.js").Task[]) => void;
|
|
110
110
|
onPermissionModeChange?: (mode: PermissionMode) => void;
|
|
111
|
+
onSubagentLatestTotalTokensChange?: (
|
|
112
|
+
subagentId: string,
|
|
113
|
+
tokens: number,
|
|
114
|
+
) => void;
|
|
111
115
|
onBackgroundCurrentTask?: () => void;
|
|
112
116
|
}
|
|
113
117
|
|
|
@@ -131,6 +135,7 @@ export class Agent {
|
|
|
131
135
|
private reversionManager: ReversionManager;
|
|
132
136
|
private memoryRuleManager: MemoryRuleManager; // Add memory rule manager instance
|
|
133
137
|
private liveConfigManager: LiveConfigManager; // Add live configuration manager
|
|
138
|
+
private taskManager: TaskManager;
|
|
134
139
|
private foregroundTaskManager: ForegroundTaskManager;
|
|
135
140
|
private configurationService: ConfigurationService; // Add configuration service
|
|
136
141
|
private workdir: string; // Working directory
|
|
@@ -208,6 +213,43 @@ export class Agent {
|
|
|
208
213
|
|
|
209
214
|
this.foregroundTaskManager = new ForegroundTaskManager();
|
|
210
215
|
|
|
216
|
+
// Initialize memory rule manager
|
|
217
|
+
this.memoryRuleManager = new MemoryRuleManager({
|
|
218
|
+
workdir: this.workdir,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Initialize MessageManager
|
|
222
|
+
this.messageManager = new MessageManager({
|
|
223
|
+
callbacks: {
|
|
224
|
+
...callbacks,
|
|
225
|
+
onSessionIdChange: (sessionId) => {
|
|
226
|
+
// When session ID changes (e.g. due to compression),
|
|
227
|
+
// we update the task manager to use the root session ID
|
|
228
|
+
// to ensure the task list remains consistent.
|
|
229
|
+
this.taskManager.setTaskListId(
|
|
230
|
+
this.messageManager.getRootSessionId(),
|
|
231
|
+
);
|
|
232
|
+
callbacks.onSessionIdChange?.(sessionId);
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
workdir: this.workdir,
|
|
236
|
+
logger: this.logger,
|
|
237
|
+
memoryRuleManager: this.memoryRuleManager,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Resolve taskListId once during construction to ensure stability
|
|
241
|
+
const resolvedTaskListId =
|
|
242
|
+
this.configurationService.getEnvironmentVars().WAVE_TASK_LIST_ID ||
|
|
243
|
+
process.env.WAVE_TASK_LIST_ID ||
|
|
244
|
+
this.messageManager.getRootSessionId();
|
|
245
|
+
|
|
246
|
+
this.taskManager = new TaskManager(resolvedTaskListId);
|
|
247
|
+
this.taskManager.on("tasksChange", async () => {
|
|
248
|
+
const tasks = await this.taskManager.listTasks();
|
|
249
|
+
this.options.callbacks?.onSessionTasksChange?.(tasks);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// Initialize BackgroundTaskManager
|
|
211
253
|
this.backgroundTaskManager = new BackgroundTaskManager({
|
|
212
254
|
callbacks: {
|
|
213
255
|
...callbacks,
|
|
@@ -243,20 +285,7 @@ export class Agent {
|
|
|
243
285
|
workdir: this.workdir,
|
|
244
286
|
});
|
|
245
287
|
|
|
246
|
-
//
|
|
247
|
-
this.memoryRuleManager = new MemoryRuleManager({
|
|
248
|
-
workdir: this.workdir,
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
// Initialize MessageManager
|
|
252
|
-
this.messageManager = new MessageManager({
|
|
253
|
-
callbacks,
|
|
254
|
-
workdir: this.workdir,
|
|
255
|
-
logger: this.logger,
|
|
256
|
-
memoryRuleManager: this.memoryRuleManager,
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
// Initialize ReversionManager
|
|
288
|
+
// ReversionManager depends on MessageManager
|
|
260
289
|
this.reversionManager = new ReversionManager(
|
|
261
290
|
new ReversionService(this.messageManager.getTranscriptPath()),
|
|
262
291
|
);
|
|
@@ -312,6 +341,7 @@ export class Agent {
|
|
|
312
341
|
reversionManager: this.reversionManager,
|
|
313
342
|
permissionMode: options.permissionMode, // Let PermissionManager handle defaultMode resolution
|
|
314
343
|
canUseToolCallback: canUseToolWithNotification,
|
|
344
|
+
taskManager: this.taskManager,
|
|
315
345
|
backgroundTaskManager: this.backgroundTaskManager,
|
|
316
346
|
foregroundTaskManager: this.foregroundTaskManager,
|
|
317
347
|
}); // Initialize tool registry with permission support
|
|
@@ -328,7 +358,6 @@ export class Agent {
|
|
|
328
358
|
this.subagentManager = new SubagentManager({
|
|
329
359
|
workdir: this.workdir,
|
|
330
360
|
parentToolManager: this.toolManager,
|
|
331
|
-
parentMessageManager: this.messageManager,
|
|
332
361
|
callbacks: {
|
|
333
362
|
onSubagentUserMessageAdded: callbacks.onSubagentUserMessageAdded,
|
|
334
363
|
onSubagentAssistantMessageAdded:
|
|
@@ -339,6 +368,8 @@ export class Agent {
|
|
|
339
368
|
callbacks.onSubagentAssistantReasoningUpdated,
|
|
340
369
|
onSubagentToolBlockUpdated: callbacks.onSubagentToolBlockUpdated,
|
|
341
370
|
onSubagentMessagesChange: callbacks.onSubagentMessagesChange,
|
|
371
|
+
onSubagentLatestTotalTokensChange:
|
|
372
|
+
callbacks.onSubagentLatestTotalTokensChange,
|
|
342
373
|
}, // Pass subagent callbacks for forwarding
|
|
343
374
|
logger: this.logger,
|
|
344
375
|
getGatewayConfig: () => this.getGatewayConfig(),
|
|
@@ -348,6 +379,7 @@ export class Agent {
|
|
|
348
379
|
hookManager: this.hookManager,
|
|
349
380
|
onUsageAdded: (usage) => this.addUsage(usage),
|
|
350
381
|
backgroundTaskManager: this.backgroundTaskManager,
|
|
382
|
+
taskManager: this.taskManager,
|
|
351
383
|
memoryRuleManager: this.memoryRuleManager,
|
|
352
384
|
});
|
|
353
385
|
|
|
@@ -355,6 +387,7 @@ export class Agent {
|
|
|
355
387
|
this.aiManager = new AIManager({
|
|
356
388
|
messageManager: this.messageManager,
|
|
357
389
|
toolManager: this.toolManager,
|
|
390
|
+
taskManager: this.taskManager,
|
|
358
391
|
logger: this.logger,
|
|
359
392
|
backgroundTaskManager: this.backgroundTaskManager,
|
|
360
393
|
hookManager: this.hookManager,
|
|
@@ -381,6 +414,7 @@ export class Agent {
|
|
|
381
414
|
messageManager: this.messageManager,
|
|
382
415
|
aiManager: this.aiManager,
|
|
383
416
|
backgroundTaskManager: this.backgroundTaskManager,
|
|
417
|
+
taskManager: this.taskManager,
|
|
384
418
|
workdir: this.workdir,
|
|
385
419
|
logger: this.logger,
|
|
386
420
|
});
|
|
@@ -455,10 +489,6 @@ export class Agent {
|
|
|
455
489
|
return this.messageManager.getlatestTotalTokens();
|
|
456
490
|
}
|
|
457
491
|
|
|
458
|
-
public get userInputHistory(): string[] {
|
|
459
|
-
return this.messageManager.getUserInputHistory();
|
|
460
|
-
}
|
|
461
|
-
|
|
462
492
|
/** Get working directory */
|
|
463
493
|
public get workingDirectory(): string {
|
|
464
494
|
return this.workdir;
|
|
@@ -777,90 +807,18 @@ export class Agent {
|
|
|
777
807
|
// Rebuild usage array from restored messages
|
|
778
808
|
this.rebuildUsageFromMessages(sessionToRestore?.messages || []);
|
|
779
809
|
|
|
780
|
-
|
|
781
|
-
await this.restoreSubagentSessions(sessionToRestore?.messages || []);
|
|
782
|
-
|
|
783
|
-
if (sessionToRestore)
|
|
810
|
+
if (sessionToRestore) {
|
|
784
811
|
this.messageManager.initializeFromSession(sessionToRestore);
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
/**
|
|
789
|
-
* Restore subagent sessions associated with the current main session
|
|
790
|
-
* This method is called after the main session is restored to load any subagent sessions
|
|
791
|
-
*/
|
|
792
|
-
private async restoreSubagentSessions(messages: Message[]): Promise<void> {
|
|
793
|
-
try {
|
|
794
|
-
// Only attempt to restore subagent sessions if we have messages (session was restored)
|
|
795
|
-
if (messages.length === 0) {
|
|
796
|
-
return;
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
// Extract sessionId -> subagentId mapping from SubagentBlocks
|
|
800
|
-
const subagentBlockMap = new Map<
|
|
801
|
-
string,
|
|
802
|
-
{ subagentId: string; configuration: SubagentConfiguration }
|
|
803
|
-
>(); // sessionId -> { subagentId, configuration }
|
|
804
|
-
|
|
805
|
-
for (const message of messages) {
|
|
806
|
-
if (message.role === "assistant" && message.blocks) {
|
|
807
|
-
for (const block of message.blocks) {
|
|
808
|
-
if (
|
|
809
|
-
block.type === "subagent" &&
|
|
810
|
-
block.sessionId &&
|
|
811
|
-
block.subagentId &&
|
|
812
|
-
block.configuration
|
|
813
|
-
) {
|
|
814
|
-
subagentBlockMap.set(block.sessionId, {
|
|
815
|
-
subagentId: block.subagentId,
|
|
816
|
-
configuration: block.configuration,
|
|
817
|
-
});
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
if (subagentBlockMap.size === 0) {
|
|
824
|
-
return; // No subagent blocks found
|
|
825
|
-
}
|
|
826
812
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
try {
|
|
831
|
-
const sessionData = await loadSessionFromJsonl(
|
|
832
|
-
sessionId,
|
|
833
|
-
this.messageManager.getWorkdir(),
|
|
834
|
-
"subagent",
|
|
835
|
-
);
|
|
836
|
-
if (sessionData) {
|
|
837
|
-
subagentSessions.push({
|
|
838
|
-
sessionData,
|
|
839
|
-
subagentId: blockData.subagentId, // Use the subagentId from SubagentBlock
|
|
840
|
-
configuration: blockData.configuration, // Include configuration
|
|
841
|
-
});
|
|
842
|
-
}
|
|
843
|
-
} catch (error) {
|
|
844
|
-
this.logger?.warn(
|
|
845
|
-
`Failed to load subagent session ${sessionId}:`,
|
|
846
|
-
error,
|
|
847
|
-
);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
|
|
851
|
-
if (subagentSessions.length > 0) {
|
|
852
|
-
this.logger?.debug(
|
|
853
|
-
`Found ${subagentSessions.length} subagent sessions to restore`,
|
|
813
|
+
// Update task manager with the root session ID to ensure continuity across compressions
|
|
814
|
+
this.taskManager.setTaskListId(
|
|
815
|
+
sessionToRestore.rootSessionId || sessionToRestore.id,
|
|
854
816
|
);
|
|
855
817
|
|
|
856
|
-
//
|
|
857
|
-
await this.
|
|
858
|
-
|
|
859
|
-
this.logger?.debug("Subagent sessions restored successfully");
|
|
818
|
+
// After session is initialized, load tasks for the session
|
|
819
|
+
const tasks = await this.taskManager.listTasks();
|
|
820
|
+
this.options.callbacks?.onSessionTasksChange?.(tasks);
|
|
860
821
|
}
|
|
861
|
-
} catch (error) {
|
|
862
|
-
this.logger?.warn("Failed to restore subagent sessions:", error);
|
|
863
|
-
// Don't throw error to prevent app startup failure
|
|
864
822
|
}
|
|
865
823
|
}
|
|
866
824
|
|
|
@@ -898,12 +856,18 @@ export class Agent {
|
|
|
898
856
|
this.abortMessage(); // Abort any running operations
|
|
899
857
|
this.subagentManager.cleanup(); // Clean up active subagents
|
|
900
858
|
|
|
901
|
-
// 5. Rebuild usage
|
|
859
|
+
// 5. Rebuild usage (in correct order)
|
|
902
860
|
this.rebuildUsageFromMessages(sessionData.messages);
|
|
903
|
-
await this.restoreSubagentSessions(sessionData.messages);
|
|
904
861
|
|
|
905
862
|
// 6. Initialize session state last
|
|
906
863
|
this.messageManager.initializeFromSession(sessionData);
|
|
864
|
+
|
|
865
|
+
// Update task manager with the root session ID to ensure continuity across compressions
|
|
866
|
+
this.taskManager.setTaskListId(sessionData.rootSessionId || sessionData.id);
|
|
867
|
+
|
|
868
|
+
// 7. Load tasks for the restored session
|
|
869
|
+
const tasks = await this.taskManager.listTasks();
|
|
870
|
+
this.options.callbacks?.onSessionTasksChange?.(tasks);
|
|
907
871
|
}
|
|
908
872
|
|
|
909
873
|
public abortAIMessage(): void {
|
|
@@ -912,12 +876,9 @@ export class Agent {
|
|
|
912
876
|
|
|
913
877
|
/** Execute bash command */
|
|
914
878
|
public async executeBashCommand(command: string): Promise<void> {
|
|
915
|
-
// Add user message to history (but not displayed in UI)
|
|
916
|
-
this.addToInputHistory(`!${command}`);
|
|
917
879
|
await this.bashManager?.executeCommand(command);
|
|
918
880
|
}
|
|
919
881
|
|
|
920
|
-
/** Clear messages and input history */
|
|
921
882
|
public clearMessages(): void {
|
|
922
883
|
this.messageManager.clearMessages();
|
|
923
884
|
}
|
|
@@ -929,11 +890,6 @@ export class Agent {
|
|
|
929
890
|
this.abortSlashCommand();
|
|
930
891
|
}
|
|
931
892
|
|
|
932
|
-
/** Add to input history */
|
|
933
|
-
private addToInputHistory(input: string): void {
|
|
934
|
-
this.messageManager.addToInputHistory(input);
|
|
935
|
-
}
|
|
936
|
-
|
|
937
893
|
/** Interrupt bash command execution */
|
|
938
894
|
public abortBashCommand(): void {
|
|
939
895
|
this.bashManager?.abortCommand();
|
|
@@ -964,12 +920,6 @@ export class Agent {
|
|
|
964
920
|
public async backgroundCurrentTask(): Promise<void> {
|
|
965
921
|
await this.foregroundTaskManager.backgroundCurrentTask();
|
|
966
922
|
this.options.callbacks?.onBackgroundCurrentTask?.();
|
|
967
|
-
|
|
968
|
-
// If there was no foreground task (e.g. a tool that doesn't register one),
|
|
969
|
-
// or even if there was, we should stop the AI recursion loop.
|
|
970
|
-
if (!this.foregroundTaskManager.hasActiveTasks()) {
|
|
971
|
-
this.aiManager.abortRecursion();
|
|
972
|
-
}
|
|
973
923
|
}
|
|
974
924
|
|
|
975
925
|
/** Destroy managers, clean up resources */
|
|
@@ -1061,8 +1011,6 @@ export class Agent {
|
|
|
1061
1011
|
// Execute valid slash command
|
|
1062
1012
|
await this.slashCommandManager.executeCommand(commandId, args);
|
|
1063
1013
|
|
|
1064
|
-
// Add slash command to history
|
|
1065
|
-
this.addToInputHistory(command);
|
|
1066
1014
|
return;
|
|
1067
1015
|
}
|
|
1068
1016
|
|
|
@@ -1071,9 +1019,6 @@ export class Agent {
|
|
|
1071
1019
|
}
|
|
1072
1020
|
|
|
1073
1021
|
// Handle normal AI message
|
|
1074
|
-
// Add user message to history
|
|
1075
|
-
this.addToInputHistory(content);
|
|
1076
|
-
|
|
1077
1022
|
// Add user message first, will automatically sync to UI
|
|
1078
1023
|
this.messageManager.addUserMessage({
|
|
1079
1024
|
content,
|
|
@@ -1130,67 +1075,6 @@ export class Agent {
|
|
|
1130
1075
|
}
|
|
1131
1076
|
}
|
|
1132
1077
|
|
|
1133
|
-
/** Save memory to project or user memory file */
|
|
1134
|
-
public async saveMemory(
|
|
1135
|
-
message: string,
|
|
1136
|
-
type: "project" | "user",
|
|
1137
|
-
): Promise<void> {
|
|
1138
|
-
try {
|
|
1139
|
-
// Ensure the message starts with # for memory functions
|
|
1140
|
-
const formattedMessage = message.startsWith("#")
|
|
1141
|
-
? message
|
|
1142
|
-
: `#${message}`;
|
|
1143
|
-
|
|
1144
|
-
if (type === "project") {
|
|
1145
|
-
await memory.addMemory(formattedMessage, this.workdir);
|
|
1146
|
-
// Update internal state after successful save
|
|
1147
|
-
this._projectMemoryContent = await memory.readMemoryFile(this.workdir);
|
|
1148
|
-
} else {
|
|
1149
|
-
await memory.addUserMemory(formattedMessage);
|
|
1150
|
-
// Update internal state after successful save
|
|
1151
|
-
this._userMemoryContent = await memory.getUserMemoryContent();
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
// Add successful MemoryBlock to the last assistant message
|
|
1155
|
-
const memoryText = message.substring(1).trim();
|
|
1156
|
-
const typeLabel = type === "project" ? "Project Memory" : "User Memory";
|
|
1157
|
-
const storagePath = "AGENTS.md";
|
|
1158
|
-
|
|
1159
|
-
const messages = this.messageManager.getMessages();
|
|
1160
|
-
const lastMessage = messages[messages.length - 1];
|
|
1161
|
-
if (lastMessage && lastMessage.role === "assistant") {
|
|
1162
|
-
lastMessage.blocks.push({
|
|
1163
|
-
type: "memory",
|
|
1164
|
-
content: `${typeLabel}: ${memoryText}`,
|
|
1165
|
-
isSuccess: true,
|
|
1166
|
-
memoryType: type,
|
|
1167
|
-
storagePath,
|
|
1168
|
-
});
|
|
1169
|
-
this.messageManager.setMessages(messages);
|
|
1170
|
-
}
|
|
1171
|
-
} catch (error) {
|
|
1172
|
-
// Add failed MemoryBlock to the last assistant message
|
|
1173
|
-
const memoryText = message.substring(1).trim();
|
|
1174
|
-
const typeLabel = type === "project" ? "Project Memory" : "User Memory";
|
|
1175
|
-
const storagePath = "AGENTS.md";
|
|
1176
|
-
const errorMessage =
|
|
1177
|
-
error instanceof Error ? error.message : String(error);
|
|
1178
|
-
|
|
1179
|
-
const messages = this.messageManager.getMessages();
|
|
1180
|
-
const lastMessage = messages[messages.length - 1];
|
|
1181
|
-
if (lastMessage && lastMessage.role === "assistant") {
|
|
1182
|
-
lastMessage.blocks.push({
|
|
1183
|
-
type: "memory",
|
|
1184
|
-
content: `${typeLabel}: ${memoryText} - Error: ${errorMessage}`,
|
|
1185
|
-
isSuccess: false,
|
|
1186
|
-
memoryType: type,
|
|
1187
|
-
storagePath,
|
|
1188
|
-
});
|
|
1189
|
-
this.messageManager.setMessages(messages);
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
1078
|
// ========== MCP Management Methods ==========
|
|
1195
1079
|
|
|
1196
1080
|
/** Get all MCP server status */
|
|
@@ -1268,6 +1152,18 @@ export class Agent {
|
|
|
1268
1152
|
*/
|
|
1269
1153
|
public async truncateHistory(index: number): Promise<void> {
|
|
1270
1154
|
await this.messageManager.truncateHistory(index, this.reversionManager);
|
|
1155
|
+
// After truncating history, the task list might have changed, so refresh it.
|
|
1156
|
+
await this.taskManager.refreshTasks();
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Get the full message thread including parent sessions
|
|
1161
|
+
*/
|
|
1162
|
+
public async getFullMessageThread(): Promise<{
|
|
1163
|
+
messages: Message[];
|
|
1164
|
+
sessionIds: string[];
|
|
1165
|
+
}> {
|
|
1166
|
+
return this.messageManager.getFullMessageThread();
|
|
1271
1167
|
}
|
|
1272
1168
|
|
|
1273
1169
|
/**
|
|
@@ -1338,7 +1234,7 @@ export class Agent {
|
|
|
1338
1234
|
private handlePlanModeTransition(mode: PermissionMode): void {
|
|
1339
1235
|
if (mode === "plan") {
|
|
1340
1236
|
this.planManager
|
|
1341
|
-
.getOrGeneratePlanFilePath()
|
|
1237
|
+
.getOrGeneratePlanFilePath(this.messageManager.getRootSessionId())
|
|
1342
1238
|
.then(({ path }) => {
|
|
1343
1239
|
this.logger?.debug("Plan file path generated", { path });
|
|
1344
1240
|
this.permissionManager.setPlanFilePath(path);
|
|
@@ -1350,4 +1246,11 @@ export class Agent {
|
|
|
1350
1246
|
this.permissionManager.setPlanFilePath(undefined);
|
|
1351
1247
|
}
|
|
1352
1248
|
}
|
|
1249
|
+
|
|
1250
|
+
/**
|
|
1251
|
+
* Get the current task list ID
|
|
1252
|
+
*/
|
|
1253
|
+
public get taskListId(): string {
|
|
1254
|
+
return this.taskManager.getTaskListId();
|
|
1255
|
+
}
|
|
1353
1256
|
}
|
package/src/constants/tools.ts
CHANGED
|
@@ -13,5 +13,8 @@ export const MULTI_EDIT_TOOL_NAME = "MultiEdit";
|
|
|
13
13
|
export const READ_TOOL_NAME = "Read";
|
|
14
14
|
export const SKILL_TOOL_NAME = "Skill";
|
|
15
15
|
export const TASK_TOOL_NAME = "Task";
|
|
16
|
-
export const
|
|
16
|
+
export const TASK_CREATE_TOOL_NAME = "TaskCreate";
|
|
17
|
+
export const TASK_GET_TOOL_NAME = "TaskGet";
|
|
18
|
+
export const TASK_UPDATE_TOOL_NAME = "TaskUpdate";
|
|
19
|
+
export const TASK_LIST_TOOL_NAME = "TaskList";
|
|
17
20
|
export const WRITE_TOOL_NAME = "Write";
|
package/src/index.ts
CHANGED
|
@@ -30,6 +30,7 @@ export * from "./utils/promptHistory.js";
|
|
|
30
30
|
export * from "./utils/stringUtils.js";
|
|
31
31
|
export * from "./utils/customCommands.js";
|
|
32
32
|
export * from "./utils/hookMatcher.js";
|
|
33
|
+
export * from "./utils/tokenCalculation.js";
|
|
33
34
|
|
|
34
35
|
// Export types
|
|
35
36
|
export * from "./types/index.js";
|