sema-core 1.0.11 → 1.0.13
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/README.md +6 -11
- package/dist/core/Conversation.d.ts +6 -1
- package/dist/core/Conversation.d.ts.map +1 -1
- package/dist/core/Conversation.js +72 -28
- package/dist/core/Conversation.js.map +1 -1
- package/dist/core/RunTools.d.ts.map +1 -1
- package/dist/core/RunTools.js +1 -1
- package/dist/core/RunTools.js.map +1 -1
- package/dist/core/SemaCore.d.ts +16 -0
- package/dist/core/SemaCore.d.ts.map +1 -1
- package/dist/core/SemaCore.js +30 -3
- package/dist/core/SemaCore.js.map +1 -1
- package/dist/core/SemaEngine.d.ts +2 -18
- package/dist/core/SemaEngine.d.ts.map +1 -1
- package/dist/core/SemaEngine.js +101 -131
- package/dist/core/SemaEngine.js.map +1 -1
- package/dist/events/EventSystem.d.ts.map +1 -1
- package/dist/events/EventSystem.js +6 -2
- package/dist/events/EventSystem.js.map +1 -1
- package/dist/events/types.d.ts +60 -13
- package/dist/events/types.d.ts.map +1 -1
- package/dist/events/types.js +0 -1
- package/dist/events/types.js.map +1 -1
- package/dist/manager/ConfManager.d.ts.map +1 -1
- package/dist/manager/ConfManager.js +3 -1
- package/dist/manager/ConfManager.js.map +1 -1
- package/dist/manager/CronManager.d.ts +52 -0
- package/dist/manager/CronManager.d.ts.map +1 -0
- package/dist/manager/CronManager.js +426 -0
- package/dist/manager/CronManager.js.map +1 -0
- package/dist/manager/PermissionManager.d.ts +1 -1
- package/dist/manager/PermissionManager.d.ts.map +1 -1
- package/dist/manager/PermissionManager.js +63 -12
- package/dist/manager/PermissionManager.js.map +1 -1
- package/dist/manager/StateManager.d.ts +79 -11
- package/dist/manager/StateManager.d.ts.map +1 -1
- package/dist/manager/StateManager.js +287 -4
- package/dist/manager/StateManager.js.map +1 -1
- package/dist/manager/TaskManager.d.ts +106 -0
- package/dist/manager/TaskManager.d.ts.map +1 -0
- package/dist/manager/TaskManager.js +664 -0
- package/dist/manager/TaskManager.js.map +1 -0
- package/dist/services/agents/defaultBuiltInAgentsConfs.js +3 -3
- package/dist/services/agents/defaultBuiltInAgentsConfs.js.map +1 -1
- package/dist/services/agents/systemReminder.d.ts.map +1 -1
- package/dist/services/agents/systemReminder.js +8 -118
- package/dist/services/agents/systemReminder.js.map +1 -1
- package/dist/services/api/adapt/anthropic.js +2 -2
- package/dist/services/api/adapt/anthropic.js.map +1 -1
- package/dist/services/api/adapt/openai.js +2 -2
- package/dist/services/api/adapt/openai.js.map +1 -1
- package/dist/services/api/adapt/util.d.ts +1 -1
- package/dist/services/api/adapt/util.d.ts.map +1 -1
- package/dist/services/api/adapt/util.js +2 -2
- package/dist/services/api/adapt/util.js.map +1 -1
- package/dist/services/api/cache.d.ts.map +1 -1
- package/dist/services/api/cache.js +1 -3
- package/dist/services/api/cache.js.map +1 -1
- package/dist/services/api/queryLLM.d.ts.map +1 -1
- package/dist/services/api/queryLLM.js +5 -0
- package/dist/services/api/queryLLM.js.map +1 -1
- package/dist/services/commands/runCommand.d.ts.map +1 -1
- package/dist/services/commands/runCommand.js +12 -0
- package/dist/services/commands/runCommand.js.map +1 -1
- package/dist/services/mcp/MCPManager.d.ts.map +1 -1
- package/dist/services/mcp/MCPManager.js +6 -1
- package/dist/services/mcp/MCPManager.js.map +1 -1
- package/dist/services/plugins/pluginsManager.d.ts.map +1 -1
- package/dist/services/plugins/pluginsManager.js +8 -1
- package/dist/services/plugins/pluginsManager.js.map +1 -1
- package/dist/services/rules/rulesManager.d.ts +51 -0
- package/dist/services/rules/rulesManager.d.ts.map +1 -0
- package/dist/services/rules/rulesManager.js +192 -0
- package/dist/services/rules/rulesManager.js.map +1 -0
- package/dist/tools/Agent/Agent.d.ts +19 -11
- package/dist/tools/Agent/Agent.d.ts.map +1 -1
- package/dist/tools/Agent/Agent.js +249 -72
- package/dist/tools/Agent/Agent.js.map +1 -1
- package/dist/tools/Agent/prompt.d.ts.map +1 -1
- package/dist/tools/Agent/prompt.js +7 -9
- package/dist/tools/Agent/prompt.js.map +1 -1
- package/dist/tools/Bash/Bash.d.ts +21 -11
- package/dist/tools/Bash/Bash.d.ts.map +1 -1
- package/dist/tools/Bash/Bash.js +74 -17
- package/dist/tools/Bash/Bash.js.map +1 -1
- package/dist/tools/Bash/prompt.d.ts.map +1 -1
- package/dist/tools/Bash/prompt.js +53 -70
- package/dist/tools/Bash/prompt.js.map +1 -1
- package/dist/tools/Bash/utils.d.ts +5 -1
- package/dist/tools/Bash/utils.d.ts.map +1 -1
- package/dist/tools/Bash/utils.js +24 -4
- package/dist/tools/Bash/utils.js.map +1 -1
- package/dist/tools/CronCreate/CronCreate.d.ts +68 -0
- package/dist/tools/CronCreate/CronCreate.d.ts.map +1 -0
- package/dist/tools/CronCreate/CronCreate.js +75 -0
- package/dist/tools/CronCreate/CronCreate.js.map +1 -0
- package/dist/tools/CronCreate/prompt.d.ts +3 -0
- package/dist/tools/CronCreate/prompt.d.ts.map +1 -0
- package/dist/tools/CronCreate/prompt.js +41 -0
- package/dist/tools/CronCreate/prompt.js.map +1 -0
- package/dist/tools/CronDelete/CronDelete.d.ts +45 -0
- package/dist/tools/CronDelete/CronDelete.d.ts.map +1 -0
- package/dist/tools/CronDelete/CronDelete.js +51 -0
- package/dist/tools/CronDelete/CronDelete.js.map +1 -0
- package/dist/tools/CronDelete/prompt.d.ts +3 -0
- package/dist/tools/CronDelete/prompt.d.ts.map +1 -0
- package/dist/tools/CronDelete/prompt.js +6 -0
- package/dist/tools/CronDelete/prompt.js.map +1 -0
- package/dist/tools/CronList/CronList.d.ts +33 -0
- package/dist/tools/CronList/CronList.d.ts.map +1 -0
- package/dist/tools/CronList/CronList.js +55 -0
- package/dist/tools/CronList/CronList.js.map +1 -0
- package/dist/tools/CronList/prompt.d.ts +3 -0
- package/dist/tools/CronList/prompt.d.ts.map +1 -0
- package/dist/tools/CronList/prompt.js +6 -0
- package/dist/tools/CronList/prompt.js.map +1 -0
- package/dist/tools/Edit/Edit.d.ts.map +1 -1
- package/dist/tools/Edit/Edit.js +7 -3
- package/dist/tools/Edit/Edit.js.map +1 -1
- package/dist/tools/Edit/utils.d.ts.map +1 -1
- package/dist/tools/Edit/utils.js +14 -5
- package/dist/tools/Edit/utils.js.map +1 -1
- package/dist/tools/Grep/Grep.d.ts +4 -4
- package/dist/tools/NotebookEdit/NotebookEdit.js +1 -1
- package/dist/tools/NotebookEdit/NotebookEdit.js.map +1 -1
- package/dist/tools/Read/Read.d.ts +74 -3
- package/dist/tools/Read/Read.d.ts.map +1 -1
- package/dist/tools/Read/Read.js +188 -18
- package/dist/tools/Read/Read.js.map +1 -1
- package/dist/tools/Read/prompt.d.ts +1 -1
- package/dist/tools/Read/prompt.d.ts.map +1 -1
- package/dist/tools/Read/prompt.js +2 -1
- package/dist/tools/Read/prompt.js.map +1 -1
- package/dist/tools/Skill/Skill.d.ts +2 -1
- package/dist/tools/Skill/Skill.d.ts.map +1 -1
- package/dist/tools/Skill/Skill.js +3 -2
- package/dist/tools/Skill/Skill.js.map +1 -1
- package/dist/tools/TaskCreate/TaskCreate.d.ts +54 -0
- package/dist/tools/TaskCreate/TaskCreate.d.ts.map +1 -0
- package/dist/tools/TaskCreate/TaskCreate.js +51 -0
- package/dist/tools/TaskCreate/TaskCreate.js.map +1 -0
- package/dist/tools/TaskCreate/prompt.d.ts +3 -0
- package/dist/tools/TaskCreate/prompt.d.ts.map +1 -0
- package/dist/tools/TaskCreate/prompt.js +45 -0
- package/dist/tools/TaskCreate/prompt.js.map +1 -0
- package/dist/tools/TaskGet/TaskGet.d.ts +34 -0
- package/dist/tools/TaskGet/TaskGet.d.ts.map +1 -0
- package/dist/tools/TaskGet/TaskGet.js +57 -0
- package/dist/tools/TaskGet/TaskGet.js.map +1 -0
- package/dist/tools/TaskGet/prompt.d.ts +3 -0
- package/dist/tools/TaskGet/prompt.d.ts.map +1 -0
- package/dist/tools/TaskGet/prompt.js +26 -0
- package/dist/tools/TaskGet/prompt.js.map +1 -0
- package/dist/tools/TaskList/TaskList.d.ts +27 -0
- package/dist/tools/TaskList/TaskList.d.ts.map +1 -0
- package/dist/tools/TaskList/TaskList.js +56 -0
- package/dist/tools/TaskList/TaskList.js.map +1 -0
- package/dist/tools/TaskList/prompt.d.ts +3 -0
- package/dist/tools/TaskList/prompt.d.ts.map +1 -0
- package/dist/tools/TaskList/prompt.js +25 -0
- package/dist/tools/TaskList/prompt.js.map +1 -0
- package/dist/tools/TaskOutput/TaskOutput.d.ts +58 -0
- package/dist/tools/TaskOutput/TaskOutput.d.ts.map +1 -0
- package/dist/tools/TaskOutput/TaskOutput.js +95 -0
- package/dist/tools/TaskOutput/TaskOutput.js.map +1 -0
- package/dist/tools/TaskStop/TaskStop.d.ts +50 -0
- package/dist/tools/TaskStop/TaskStop.d.ts.map +1 -0
- package/dist/tools/TaskStop/TaskStop.js +69 -0
- package/dist/tools/TaskStop/TaskStop.js.map +1 -0
- package/dist/tools/TaskUpdate/TaskUpdate.d.ts +88 -0
- package/dist/tools/TaskUpdate/TaskUpdate.d.ts.map +1 -0
- package/dist/tools/TaskUpdate/TaskUpdate.js +115 -0
- package/dist/tools/TaskUpdate/TaskUpdate.js.map +1 -0
- package/dist/tools/TaskUpdate/prompt.d.ts +3 -0
- package/dist/tools/TaskUpdate/prompt.d.ts.map +1 -0
- package/dist/tools/TaskUpdate/prompt.js +80 -0
- package/dist/tools/TaskUpdate/prompt.js.map +1 -0
- package/dist/tools/WebFetch/WebFetch.d.ts +62 -0
- package/dist/tools/WebFetch/WebFetch.d.ts.map +1 -0
- package/dist/tools/WebFetch/WebFetch.js +143 -0
- package/dist/tools/WebFetch/WebFetch.js.map +1 -0
- package/dist/tools/WebFetch/prompt.d.ts +4 -0
- package/dist/tools/WebFetch/prompt.d.ts.map +1 -0
- package/dist/tools/WebFetch/prompt.js +38 -0
- package/dist/tools/WebFetch/prompt.js.map +1 -0
- package/dist/tools/WebFetch/utils.d.ts +20 -0
- package/dist/tools/WebFetch/utils.d.ts.map +1 -0
- package/dist/tools/WebFetch/utils.js +223 -0
- package/dist/tools/WebFetch/utils.js.map +1 -0
- package/dist/tools/Write/Write.js +1 -1
- package/dist/tools/Write/Write.js.map +1 -1
- package/dist/tools/base/Tool.d.ts +1 -0
- package/dist/tools/base/Tool.d.ts.map +1 -1
- package/dist/tools/base/tools.d.ts +4 -2
- package/dist/tools/base/tools.d.ts.map +1 -1
- package/dist/tools/base/tools.js +61 -14
- package/dist/tools/base/tools.js.map +1 -1
- package/dist/types/cron.d.ts +21 -0
- package/dist/types/cron.d.ts.map +1 -0
- package/dist/types/cron.js +6 -0
- package/dist/types/cron.js.map +1 -0
- package/dist/types/index.d.ts +9 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +4 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/rule.d.ts +8 -0
- package/dist/types/rule.d.ts.map +1 -0
- package/dist/types/rule.js +3 -0
- package/dist/types/rule.js.map +1 -0
- package/dist/types/task.d.ts +93 -0
- package/dist/types/task.d.ts.map +1 -0
- package/dist/types/task.js +3 -0
- package/dist/types/task.js.map +1 -0
- package/dist/types/todoTask.d.ts +29 -0
- package/dist/types/todoTask.d.ts.map +1 -0
- package/dist/types/todoTask.js +6 -0
- package/dist/types/todoTask.js.map +1 -0
- package/dist/util/assembleTools.d.ts +6 -0
- package/dist/util/assembleTools.d.ts.map +1 -0
- package/dist/util/assembleTools.js +17 -0
- package/dist/util/assembleTools.js.map +1 -0
- package/dist/util/btw.d.ts +6 -0
- package/dist/util/btw.d.ts.map +1 -0
- package/dist/util/btw.js +80 -0
- package/dist/util/btw.js.map +1 -0
- package/dist/util/compact.d.ts +16 -4
- package/dist/util/compact.d.ts.map +1 -1
- package/dist/util/compact.js +12 -18
- package/dist/util/compact.js.map +1 -1
- package/dist/util/cron.d.ts +17 -0
- package/dist/util/cron.d.ts.map +1 -0
- package/dist/util/cron.js +82 -0
- package/dist/util/cron.js.map +1 -0
- package/dist/util/file.d.ts +16 -0
- package/dist/util/file.d.ts.map +1 -1
- package/dist/util/file.js +55 -0
- package/dist/util/file.js.map +1 -1
- package/dist/util/fileReference.d.ts.map +1 -1
- package/dist/util/fileReference.js +10 -1
- package/dist/util/fileReference.js.map +1 -1
- package/dist/util/format.d.ts +6 -0
- package/dist/util/format.d.ts.map +1 -1
- package/dist/util/format.js +17 -0
- package/dist/util/format.js.map +1 -1
- package/dist/util/history.d.ts +4 -1
- package/dist/util/history.d.ts.map +1 -1
- package/dist/util/history.js +9 -5
- package/dist/util/history.js.map +1 -1
- package/dist/util/inputQueue.d.ts +19 -0
- package/dist/util/inputQueue.d.ts.map +1 -0
- package/dist/util/inputQueue.js +30 -0
- package/dist/util/inputQueue.js.map +1 -0
- package/dist/util/message.d.ts +4 -0
- package/dist/util/message.d.ts.map +1 -1
- package/dist/util/message.js +27 -0
- package/dist/util/message.js.map +1 -1
- package/dist/util/pdf.d.ts +83 -0
- package/dist/util/pdf.d.ts.map +1 -0
- package/dist/util/pdf.js +472 -0
- package/dist/util/pdf.js.map +1 -0
- package/dist/util/process.d.ts +10 -0
- package/dist/util/process.d.ts.map +1 -0
- package/dist/util/process.js +88 -0
- package/dist/util/process.js.map +1 -0
- package/dist/util/savePath.d.ts +0 -11
- package/dist/util/savePath.d.ts.map +1 -1
- package/dist/util/savePath.js +0 -17
- package/dist/util/savePath.js.map +1 -1
- package/dist/util/shell.d.ts +10 -1
- package/dist/util/shell.d.ts.map +1 -1
- package/dist/util/shell.js +89 -34
- package/dist/util/shell.js.map +1 -1
- package/dist/util/topic.d.ts +7 -0
- package/dist/util/topic.d.ts.map +1 -1
- package/dist/util/topic.js +40 -0
- package/dist/util/topic.js.map +1 -1
- package/package.json +2 -1
- package/dist/services/command/customCommands.d.ts +0 -1
- package/dist/services/command/customCommands.d.ts.map +0 -1
- package/dist/services/command/customCommands.js +0 -197
- package/dist/services/command/customCommands.js.map +0 -1
- package/dist/services/command/runCommand.d.ts +0 -18
- package/dist/services/command/runCommand.d.ts.map +0 -1
- package/dist/services/command/runCommand.js +0 -147
- package/dist/services/command/runCommand.js.map +0 -1
- package/dist/services/plugins/customCommands.d.ts +0 -29
- package/dist/services/plugins/customCommands.d.ts.map +0 -1
- package/dist/services/plugins/customCommands.js +0 -227
- package/dist/services/plugins/customCommands.js.map +0 -1
- package/dist/services/skill/skillLoader.d.ts +0 -17
- package/dist/services/skill/skillLoader.d.ts.map +0 -1
- package/dist/services/skill/skillLoader.js +0 -122
- package/dist/services/skill/skillLoader.js.map +0 -1
- package/dist/services/skill/skillParser.d.ts +0 -15
- package/dist/services/skill/skillParser.d.ts.map +0 -1
- package/dist/services/skill/skillParser.js +0 -66
- package/dist/services/skill/skillParser.js.map +0 -1
- package/dist/services/skill/skillRegistry.d.ts +0 -34
- package/dist/services/skill/skillRegistry.d.ts.map +0 -1
- package/dist/services/skill/skillRegistry.js +0 -98
- package/dist/services/skill/skillRegistry.js.map +0 -1
- package/dist/services/skill/skillsManager.d.ts +0 -79
- package/dist/services/skill/skillsManager.d.ts.map +0 -1
- package/dist/services/skill/skillsManager.js +0 -289
- package/dist/services/skill/skillsManager.js.map +0 -1
- package/dist/tools/Task/Task.d.ts +0 -59
- package/dist/tools/Task/Task.d.ts.map +0 -1
- package/dist/tools/Task/Task.js +0 -212
- package/dist/tools/Task/Task.js.map +0 -1
- package/dist/tools/Task/prompt.d.ts +0 -6
- package/dist/tools/Task/prompt.d.ts.map +0 -1
- package/dist/tools/Task/prompt.js +0 -75
- package/dist/tools/Task/prompt.js.map +0 -1
- package/dist/util/frontmatter.d.ts +0 -35
- package/dist/util/frontmatter.d.ts.map +0 -1
- package/dist/util/frontmatter.js +0 -119
- package/dist/util/frontmatter.js.map +0 -1
- package/dist/util/rules.d.ts +0 -6
- package/dist/util/rules.d.ts.map +0 -1
- package/dist/util/rules.js +0 -162
- package/dist/util/rules.js.map +0 -1
- package/dist/util/systemReminder.d.ts +0 -10
- package/dist/util/systemReminder.d.ts.map +0 -1
- package/dist/util/systemReminder.js +0 -177
- package/dist/util/systemReminder.js.map +0 -1
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.TaskManager = void 0;
|
|
37
|
+
exports.getTaskManager = getTaskManager;
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const crypto = __importStar(require("crypto"));
|
|
41
|
+
const child_process_1 = require("child_process");
|
|
42
|
+
const platform_1 = require("../util/platform");
|
|
43
|
+
const log_1 = require("../util/log");
|
|
44
|
+
const EventSystem_1 = require("../events/EventSystem");
|
|
45
|
+
const process_1 = require("../util/process");
|
|
46
|
+
class TaskManager {
|
|
47
|
+
constructor() {
|
|
48
|
+
this.tasks = new Map();
|
|
49
|
+
this.watchers = new Map();
|
|
50
|
+
this.notifyCallback = null;
|
|
51
|
+
}
|
|
52
|
+
setNotifyCallback(cb) {
|
|
53
|
+
this.notifyCallback = cb;
|
|
54
|
+
}
|
|
55
|
+
getTask(taskId) {
|
|
56
|
+
return this.tasks.get(taskId);
|
|
57
|
+
}
|
|
58
|
+
getTasks() {
|
|
59
|
+
return Array.from(this.tasks.values());
|
|
60
|
+
}
|
|
61
|
+
getTaskList() {
|
|
62
|
+
return Array.from(this.tasks.values())
|
|
63
|
+
.filter(t => !t.foreground)
|
|
64
|
+
.map(t => ({
|
|
65
|
+
taskId: t.taskId,
|
|
66
|
+
pid: t.pid,
|
|
67
|
+
filepath: t.filepath,
|
|
68
|
+
status: t.status,
|
|
69
|
+
type: t.type,
|
|
70
|
+
command: t.command,
|
|
71
|
+
agentType: t.agentType,
|
|
72
|
+
foreground: t.foreground,
|
|
73
|
+
startTime: t.startTime,
|
|
74
|
+
endTime: t.endTime,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
getRunningTasks() {
|
|
78
|
+
return Array.from(this.tasks.values()).filter(t => t.status === 'running');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 订阅任务的流式输出(UI 打开任务详情面板时调用)
|
|
82
|
+
* 立即补发已有输出,后续增量实时推送
|
|
83
|
+
* @returns unwatch 函数,UI 关闭面板时调用
|
|
84
|
+
*/
|
|
85
|
+
watchTask(taskId, onDelta) {
|
|
86
|
+
if (!this.watchers.has(taskId)) {
|
|
87
|
+
this.watchers.set(taskId, new Set());
|
|
88
|
+
}
|
|
89
|
+
this.watchers.get(taskId).add(onDelta);
|
|
90
|
+
// 补发已有输出
|
|
91
|
+
const record = this.tasks.get(taskId);
|
|
92
|
+
if (record?.output) {
|
|
93
|
+
onDelta(record.output);
|
|
94
|
+
}
|
|
95
|
+
return () => {
|
|
96
|
+
this.watchers.get(taskId)?.delete(onDelta);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 为 run_in_background=true 的命令 spawn 独立进程
|
|
101
|
+
*/
|
|
102
|
+
spawnBashTask(command, toolUseId, agentContext) {
|
|
103
|
+
const running = this.getRunningTasks();
|
|
104
|
+
if (running.length >= TaskManager.MAX_RUNNING_TASKS) {
|
|
105
|
+
throw new Error(`Maximum number of running background tasks (${TaskManager.MAX_RUNNING_TASKS}) reached. Stop or wait for existing tasks to complete before starting new ones.`);
|
|
106
|
+
}
|
|
107
|
+
(0, process_1.ensureTaskDir)();
|
|
108
|
+
const taskId = crypto.randomBytes(4).toString('hex');
|
|
109
|
+
const filepath = path.join(process_1.TASK_OUTPUT_DIR, `${taskId}.output`);
|
|
110
|
+
// 初始化输出文件
|
|
111
|
+
fs.writeFileSync(filepath, '');
|
|
112
|
+
const record = {
|
|
113
|
+
taskId,
|
|
114
|
+
type: 'Bash',
|
|
115
|
+
command,
|
|
116
|
+
toolUseId,
|
|
117
|
+
filepath,
|
|
118
|
+
status: 'running',
|
|
119
|
+
output: '',
|
|
120
|
+
startTime: Date.now(),
|
|
121
|
+
};
|
|
122
|
+
this.tasks.set(taskId, record);
|
|
123
|
+
const { bin, args } = (0, process_1.getShellForSpawn)();
|
|
124
|
+
const childProcess = (0, child_process_1.spawn)(bin, [...args, command], {
|
|
125
|
+
cwd: process.cwd(),
|
|
126
|
+
env: process.env,
|
|
127
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
128
|
+
...(platform_1.IS_WIN ? { windowsHide: true } : {}),
|
|
129
|
+
});
|
|
130
|
+
record._process = childProcess;
|
|
131
|
+
record.pid = childProcess.pid;
|
|
132
|
+
(0, log_1.logInfo)(`[TaskManager] spawnBashTask taskId=${taskId} pid=${childProcess.pid} command=${command}`);
|
|
133
|
+
// emit task:start
|
|
134
|
+
(0, EventSystem_1.getEventBus)().emit('task:start', { taskId, pid: childProcess.pid, command, filepath, status: record.status, type: 'Bash' });
|
|
135
|
+
const appendChunk = (chunk) => {
|
|
136
|
+
record.output += chunk;
|
|
137
|
+
// 限制内存上限
|
|
138
|
+
if (record.output.length > process_1.MAX_OUTPUT_SIZE) {
|
|
139
|
+
record.output = record.output.slice(-process_1.MAX_OUTPUT_SIZE);
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
fs.appendFileSync(filepath, chunk);
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
(0, log_1.logWarn)(`[TaskManager] appendFileSync 失败: ${e}`);
|
|
146
|
+
}
|
|
147
|
+
this._notifyWatchers(taskId, chunk);
|
|
148
|
+
};
|
|
149
|
+
childProcess.stdout?.on('data', (data) => appendChunk(data.toString()));
|
|
150
|
+
childProcess.stderr?.on('data', (data) => appendChunk(data.toString()));
|
|
151
|
+
childProcess.on('exit', (code) => {
|
|
152
|
+
const exitCode = code ?? 1;
|
|
153
|
+
(0, log_1.logInfo)(`[TaskManager] task ${taskId} exited with code ${exitCode}`);
|
|
154
|
+
this._finishTask(record, exitCode);
|
|
155
|
+
});
|
|
156
|
+
childProcess.on('error', (error) => {
|
|
157
|
+
(0, log_1.logError)(`[TaskManager] task ${taskId} process error: ${error}`);
|
|
158
|
+
appendChunk(`\n[Process error: ${error.message}]`);
|
|
159
|
+
this._finishTask(record, 1);
|
|
160
|
+
});
|
|
161
|
+
return { taskId, filepath };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 接管超时的持久 shell 进程
|
|
165
|
+
*/
|
|
166
|
+
takeoverTask(ctx, command, toolUseId, agentContext) {
|
|
167
|
+
const running = this.getRunningTasks();
|
|
168
|
+
if (running.length >= TaskManager.MAX_RUNNING_TASKS) {
|
|
169
|
+
(0, process_1.killProcess)(ctx.shellProcess);
|
|
170
|
+
throw new Error(`Maximum number of running background tasks (${TaskManager.MAX_RUNNING_TASKS}) reached. Cannot transfer timed-out command to background.`);
|
|
171
|
+
}
|
|
172
|
+
(0, process_1.ensureTaskDir)();
|
|
173
|
+
const taskId = crypto.randomBytes(4).toString('hex');
|
|
174
|
+
const filepath = path.join(process_1.TASK_OUTPUT_DIR, `${taskId}.output`);
|
|
175
|
+
// 将超时前的部分输出写入文件
|
|
176
|
+
fs.writeFileSync(filepath, ctx.partialOutput);
|
|
177
|
+
const record = {
|
|
178
|
+
taskId,
|
|
179
|
+
type: 'Bash',
|
|
180
|
+
command,
|
|
181
|
+
toolUseId,
|
|
182
|
+
filepath,
|
|
183
|
+
status: 'running',
|
|
184
|
+
output: ctx.partialOutput,
|
|
185
|
+
_shellProcess: ctx.shellProcess,
|
|
186
|
+
startTime: Date.now(),
|
|
187
|
+
};
|
|
188
|
+
this.tasks.set(taskId, record);
|
|
189
|
+
const takeoverPid = ctx.shellProcess.pid;
|
|
190
|
+
record.pid = takeoverPid;
|
|
191
|
+
(0, log_1.logInfo)(`[TaskManager] takeoverTask taskId=${taskId} pid=${takeoverPid} command=${command}`);
|
|
192
|
+
(0, EventSystem_1.getEventBus)().emit('task:start', { taskId, pid: takeoverPid, command, filepath, status: record.status, type: 'Bash' });
|
|
193
|
+
// 记录已读取的文件偏移量
|
|
194
|
+
let stdoutOffset = ctx.partialOutput.length;
|
|
195
|
+
let stderrOffset = 0;
|
|
196
|
+
const pollTimer = setInterval(() => {
|
|
197
|
+
try {
|
|
198
|
+
// 检查 shell 进程是否已退出(异常退出)
|
|
199
|
+
const shellExited = ctx.shellProcess.exitCode !== null;
|
|
200
|
+
// 读取 stdout 增量
|
|
201
|
+
if (fs.existsSync(ctx.stdoutFile)) {
|
|
202
|
+
const content = fs.readFileSync(ctx.stdoutFile).toString('utf8');
|
|
203
|
+
if (content.length > stdoutOffset) {
|
|
204
|
+
const chunk = content.slice(stdoutOffset);
|
|
205
|
+
stdoutOffset = content.length;
|
|
206
|
+
record.output += chunk;
|
|
207
|
+
if (record.output.length > process_1.MAX_OUTPUT_SIZE) {
|
|
208
|
+
record.output = record.output.slice(-process_1.MAX_OUTPUT_SIZE);
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
fs.appendFileSync(filepath, chunk);
|
|
212
|
+
}
|
|
213
|
+
catch { }
|
|
214
|
+
this._notifyWatchers(taskId, chunk);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// 读取 stderr 增量
|
|
218
|
+
if (fs.existsSync(ctx.stderrFile)) {
|
|
219
|
+
const content = fs.readFileSync(ctx.stderrFile).toString('utf8');
|
|
220
|
+
if (content.length > stderrOffset) {
|
|
221
|
+
const chunk = content.slice(stderrOffset);
|
|
222
|
+
stderrOffset = content.length;
|
|
223
|
+
record.output += chunk;
|
|
224
|
+
if (record.output.length > process_1.MAX_OUTPUT_SIZE) {
|
|
225
|
+
record.output = record.output.slice(-process_1.MAX_OUTPUT_SIZE);
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
fs.appendFileSync(filepath, chunk);
|
|
229
|
+
}
|
|
230
|
+
catch { }
|
|
231
|
+
this._notifyWatchers(taskId, chunk);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// 检查 statusFile:命令正常完成
|
|
235
|
+
if (fs.existsSync(ctx.statusFile) && fs.statSync(ctx.statusFile).size > 0) {
|
|
236
|
+
const exitCodeStr = fs.readFileSync(ctx.statusFile, 'utf8').trim();
|
|
237
|
+
const exitCode = Number(exitCodeStr) || 0;
|
|
238
|
+
// 先读完最终输出(防止 shell exit handler 删文件后读到空)
|
|
239
|
+
if (fs.existsSync(ctx.stdoutFile)) {
|
|
240
|
+
const content = fs.readFileSync(ctx.stdoutFile).toString('utf8');
|
|
241
|
+
if (content.length > stdoutOffset) {
|
|
242
|
+
const chunk = content.slice(stdoutOffset);
|
|
243
|
+
record.output += chunk;
|
|
244
|
+
try {
|
|
245
|
+
fs.appendFileSync(filepath, chunk);
|
|
246
|
+
}
|
|
247
|
+
catch { }
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (fs.existsSync(ctx.stderrFile)) {
|
|
251
|
+
const content = fs.readFileSync(ctx.stderrFile).toString('utf8');
|
|
252
|
+
if (content.length > stderrOffset) {
|
|
253
|
+
const chunk = content.slice(stderrOffset);
|
|
254
|
+
record.output += chunk;
|
|
255
|
+
try {
|
|
256
|
+
fs.appendFileSync(filepath, chunk);
|
|
257
|
+
}
|
|
258
|
+
catch { }
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
clearInterval(pollTimer);
|
|
262
|
+
record._pollTimer = undefined;
|
|
263
|
+
// kill 旧 shell
|
|
264
|
+
(0, process_1.killProcess)(ctx.shellProcess);
|
|
265
|
+
this._finishTask(record, exitCode);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
// shell 异常退出且没有 statusFile → 失败
|
|
269
|
+
if (shellExited) {
|
|
270
|
+
clearInterval(pollTimer);
|
|
271
|
+
record._pollTimer = undefined;
|
|
272
|
+
this._finishTask(record, ctx.shellProcess.exitCode ?? 1);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
(0, log_1.logWarn)(`[TaskManager] takeoverTask poll error: ${error}`);
|
|
278
|
+
}
|
|
279
|
+
}, 200);
|
|
280
|
+
record._pollTimer = pollTimer;
|
|
281
|
+
return { taskId, filepath };
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* 注册前台 Agent 任务(不启动独立执行,仅创建 record 用于转后台)
|
|
285
|
+
*/
|
|
286
|
+
registerForegroundAgent(taskId, description, toolUseId, abortController, unlinkAbort, agentType) {
|
|
287
|
+
(0, process_1.ensureTaskDir)();
|
|
288
|
+
const filepath = path.join(process_1.TASK_OUTPUT_DIR, `${taskId}.output`);
|
|
289
|
+
fs.writeFileSync(filepath, '');
|
|
290
|
+
const record = {
|
|
291
|
+
taskId,
|
|
292
|
+
type: 'Agent',
|
|
293
|
+
command: description,
|
|
294
|
+
toolUseId,
|
|
295
|
+
filepath,
|
|
296
|
+
status: 'running',
|
|
297
|
+
output: '',
|
|
298
|
+
foreground: true,
|
|
299
|
+
_abortController: abortController,
|
|
300
|
+
_unlinkAbort: unlinkAbort,
|
|
301
|
+
startTime: Date.now(),
|
|
302
|
+
agentType,
|
|
303
|
+
};
|
|
304
|
+
this.tasks.set(taskId, record);
|
|
305
|
+
(0, log_1.logInfo)(`[TaskManager] registerForegroundAgent taskId=${taskId} description=${description}`);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* 设置前台 agent 的转后台 resolve 回调
|
|
309
|
+
*/
|
|
310
|
+
setTransferResolve(taskId, resolve) {
|
|
311
|
+
const record = this.tasks.get(taskId);
|
|
312
|
+
if (record) {
|
|
313
|
+
record._transferResolve = resolve;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* 将指定前台 agent 转为后台运行
|
|
318
|
+
* 1. 解除子AC与主AC的联动
|
|
319
|
+
* 2. resolve transferSignal,让 Agent.ts 的 Promise.race 立刻返回
|
|
320
|
+
* 3. 标记 foreground=false
|
|
321
|
+
*/
|
|
322
|
+
transferToBackground(taskId) {
|
|
323
|
+
const record = this.tasks.get(taskId);
|
|
324
|
+
if (!record || !record.foreground || record.status !== 'running') {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
// 解除与主AC的联动
|
|
328
|
+
if (record._unlinkAbort) {
|
|
329
|
+
record._unlinkAbort();
|
|
330
|
+
record._unlinkAbort = undefined;
|
|
331
|
+
}
|
|
332
|
+
// 标记为后台
|
|
333
|
+
record.foreground = false;
|
|
334
|
+
// 触发 transferSignal,让 Promise.race 立刻返回
|
|
335
|
+
if (record._transferResolve) {
|
|
336
|
+
record._transferResolve();
|
|
337
|
+
record._transferResolve = undefined;
|
|
338
|
+
}
|
|
339
|
+
(0, log_1.logInfo)(`[TaskManager] transferToBackground taskId=${taskId}`);
|
|
340
|
+
(0, EventSystem_1.getEventBus)().emit('task:transfer', { taskId, from: 'foreground', to: 'background' });
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* 将所有前台 agent 转为后台
|
|
345
|
+
* @returns 被转的 taskId 列表
|
|
346
|
+
*/
|
|
347
|
+
transferAllForeground() {
|
|
348
|
+
const transferred = [];
|
|
349
|
+
for (const record of this.tasks.values()) {
|
|
350
|
+
if (record.foreground && record.status === 'running') {
|
|
351
|
+
if (this.transferToBackground(record.taskId)) {
|
|
352
|
+
transferred.push(record.taskId);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return transferred;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* 完结 Agent 任务记录(供 Agent.ts 在前台/转后台完成时调用)
|
|
360
|
+
*/
|
|
361
|
+
finalizeTask(taskId, exitCode, output, usage) {
|
|
362
|
+
const record = this.tasks.get(taskId);
|
|
363
|
+
if (!record || record.status !== 'running')
|
|
364
|
+
return;
|
|
365
|
+
if (output !== undefined)
|
|
366
|
+
record.output = output;
|
|
367
|
+
if (usage)
|
|
368
|
+
record.usage = usage;
|
|
369
|
+
this._finishTask(record, exitCode);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* 为后台 Agent 创建任务记录并异步执行
|
|
373
|
+
* @param taskId 复用 Agent.ts 已生成的 taskId
|
|
374
|
+
* @param description 任务描述(用于通知摘要)
|
|
375
|
+
* @param toolUseId 触发该任务的 tool_use_id
|
|
376
|
+
* @param executeFn 实际执行函数,接收独立 AbortController,返回结果文本和可选的 usage 统计
|
|
377
|
+
*/
|
|
378
|
+
spawnAgentTask(taskId, description, toolUseId, executeFn, agentType) {
|
|
379
|
+
const running = this.getRunningTasks();
|
|
380
|
+
if (running.length >= TaskManager.MAX_RUNNING_TASKS) {
|
|
381
|
+
throw new Error(`Maximum number of running background tasks (${TaskManager.MAX_RUNNING_TASKS}) reached. Stop or wait for existing tasks to complete before starting new ones.`);
|
|
382
|
+
}
|
|
383
|
+
(0, process_1.ensureTaskDir)();
|
|
384
|
+
const filepath = path.join(process_1.TASK_OUTPUT_DIR, `${taskId}.output`);
|
|
385
|
+
fs.writeFileSync(filepath, '');
|
|
386
|
+
const abortController = new AbortController();
|
|
387
|
+
const record = {
|
|
388
|
+
taskId,
|
|
389
|
+
type: 'Agent',
|
|
390
|
+
command: description,
|
|
391
|
+
toolUseId,
|
|
392
|
+
filepath,
|
|
393
|
+
status: 'running',
|
|
394
|
+
output: '',
|
|
395
|
+
_abortController: abortController,
|
|
396
|
+
startTime: Date.now(),
|
|
397
|
+
agentType,
|
|
398
|
+
};
|
|
399
|
+
this.tasks.set(taskId, record);
|
|
400
|
+
(0, log_1.logInfo)(`[TaskManager] spawnAgentTask taskId=${taskId} description=${description}`);
|
|
401
|
+
(0, EventSystem_1.getEventBus)().emit('task:start', { taskId, command: description, filepath: '', status: record.status, type: 'Agent', agentType });
|
|
402
|
+
const promise = executeFn(abortController).then((raw) => {
|
|
403
|
+
const result = typeof raw === 'string' ? raw : raw.result;
|
|
404
|
+
const usage = typeof raw === 'string' ? undefined : raw.usage;
|
|
405
|
+
record.output = result;
|
|
406
|
+
if (usage)
|
|
407
|
+
record.usage = usage;
|
|
408
|
+
try {
|
|
409
|
+
fs.writeFileSync(filepath, result);
|
|
410
|
+
}
|
|
411
|
+
catch (e) {
|
|
412
|
+
(0, log_1.logWarn)(`[TaskManager] writeFileSync failed for agent task: ${e}`);
|
|
413
|
+
}
|
|
414
|
+
this._finishTask(record, 0);
|
|
415
|
+
}).catch((error) => {
|
|
416
|
+
const isAborted = abortController.signal.aborted;
|
|
417
|
+
const msg = isAborted
|
|
418
|
+
? '[Agent interrupted]'
|
|
419
|
+
: `[Agent error: ${error instanceof Error ? error.message : String(error)}]`;
|
|
420
|
+
if (!record.output)
|
|
421
|
+
record.output = msg;
|
|
422
|
+
try {
|
|
423
|
+
fs.appendFileSync(filepath, msg);
|
|
424
|
+
}
|
|
425
|
+
catch { }
|
|
426
|
+
this._finishTask(record, isAborted ? 0 : 1);
|
|
427
|
+
});
|
|
428
|
+
record._promise = promise;
|
|
429
|
+
return { taskId, filepath };
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* 停止所有正在运行的任务
|
|
433
|
+
*/
|
|
434
|
+
stopAllTasks() {
|
|
435
|
+
const running = this.getRunningTasks();
|
|
436
|
+
let count = 0;
|
|
437
|
+
for (const record of running) {
|
|
438
|
+
if (this.stopTask(record.taskId)) {
|
|
439
|
+
count++;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return count;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* 停止指定任务
|
|
446
|
+
*/
|
|
447
|
+
stopTask(taskId) {
|
|
448
|
+
const record = this.tasks.get(taskId);
|
|
449
|
+
if (!record)
|
|
450
|
+
return false;
|
|
451
|
+
// 清理定时器
|
|
452
|
+
if (record._pollTimer) {
|
|
453
|
+
clearInterval(record._pollTimer);
|
|
454
|
+
record._pollTimer = undefined;
|
|
455
|
+
}
|
|
456
|
+
// kill 进程 / abort agent
|
|
457
|
+
let killed = false;
|
|
458
|
+
if (record._process) {
|
|
459
|
+
killed = (0, process_1.killProcess)(record._process);
|
|
460
|
+
}
|
|
461
|
+
if (record._shellProcess) {
|
|
462
|
+
killed = (0, process_1.killProcess)(record._shellProcess) || killed;
|
|
463
|
+
}
|
|
464
|
+
if (record._abortController) {
|
|
465
|
+
record._abortController.abort();
|
|
466
|
+
killed = true;
|
|
467
|
+
}
|
|
468
|
+
if (!killed) {
|
|
469
|
+
(0, log_1.logWarn)(`[TaskManager] stopTask taskId=${taskId} kill failed`);
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
record.status = 'killed';
|
|
473
|
+
record.endTime = Date.now();
|
|
474
|
+
(0, log_1.logInfo)(`[TaskManager] stopTask taskId=${taskId} pid=${record.pid}`);
|
|
475
|
+
(0, EventSystem_1.getEventBus)().emit('task:end', {
|
|
476
|
+
taskId,
|
|
477
|
+
status: 'killed',
|
|
478
|
+
summary: this._formatTaskSummary(record, 'killed'),
|
|
479
|
+
});
|
|
480
|
+
// Agent 被中止时也发送 task-notification(前台任务除外,其结果由 Agent.ts yield 返回)
|
|
481
|
+
if (record.type === 'Agent' && !record.foreground) {
|
|
482
|
+
this._notify(record);
|
|
483
|
+
}
|
|
484
|
+
this._pruneFinishedTasks();
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* 等待任务完成(Promise)
|
|
489
|
+
* @param onChunk 每次有新增输出时回调,参数为 delta(增量内容)
|
|
490
|
+
*/
|
|
491
|
+
waitForTask(taskId, timeout = 30000, onChunk, abortSignal) {
|
|
492
|
+
return new Promise((resolve) => {
|
|
493
|
+
const record = this.tasks.get(taskId);
|
|
494
|
+
if (!record) {
|
|
495
|
+
resolve({ taskId, type: 'Bash', command: '', toolUseId: '', filepath: '', status: 'failed', output: '', startTime: 0 });
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (record.status !== 'running') {
|
|
499
|
+
resolve(record);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
let unwatch;
|
|
503
|
+
if (onChunk) {
|
|
504
|
+
unwatch = this.watchTask(taskId, onChunk);
|
|
505
|
+
}
|
|
506
|
+
const cleanup = () => {
|
|
507
|
+
if (unwatch)
|
|
508
|
+
unwatch();
|
|
509
|
+
clearTimeout(timer);
|
|
510
|
+
(0, EventSystem_1.getEventBus)().off('task:end', listener);
|
|
511
|
+
};
|
|
512
|
+
const timer = setTimeout(() => {
|
|
513
|
+
cleanup();
|
|
514
|
+
resolve(record);
|
|
515
|
+
}, timeout);
|
|
516
|
+
const listener = (data) => {
|
|
517
|
+
if (data.taskId === taskId) {
|
|
518
|
+
cleanup();
|
|
519
|
+
resolve(record);
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
(0, EventSystem_1.getEventBus)().on('task:end', listener);
|
|
523
|
+
// 中断支持:停止等待,不影响后台任务本身
|
|
524
|
+
if (abortSignal) {
|
|
525
|
+
if (abortSignal.aborted) {
|
|
526
|
+
cleanup();
|
|
527
|
+
resolve(record);
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
abortSignal.addEventListener('abort', () => {
|
|
531
|
+
cleanup();
|
|
532
|
+
resolve(record);
|
|
533
|
+
}, { once: true });
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* 清理所有资源
|
|
539
|
+
*/
|
|
540
|
+
dispose() {
|
|
541
|
+
for (const record of this.tasks.values()) {
|
|
542
|
+
if (record.status !== 'running')
|
|
543
|
+
continue;
|
|
544
|
+
try {
|
|
545
|
+
if (record._pollTimer) {
|
|
546
|
+
clearInterval(record._pollTimer);
|
|
547
|
+
record._pollTimer = undefined;
|
|
548
|
+
}
|
|
549
|
+
let killed = false;
|
|
550
|
+
if (record._process) {
|
|
551
|
+
killed = (0, process_1.killProcess)(record._process);
|
|
552
|
+
}
|
|
553
|
+
if (record._shellProcess) {
|
|
554
|
+
killed = (0, process_1.killProcess)(record._shellProcess) || killed;
|
|
555
|
+
}
|
|
556
|
+
if (record._abortController) {
|
|
557
|
+
record._abortController.abort();
|
|
558
|
+
killed = true;
|
|
559
|
+
}
|
|
560
|
+
if (!killed) {
|
|
561
|
+
(0, log_1.logWarn)(`[TaskManager] dispose: kill failed for taskId=${record.taskId} pid=${record.pid}`);
|
|
562
|
+
}
|
|
563
|
+
record.status = 'killed';
|
|
564
|
+
(0, EventSystem_1.getEventBus)().emit('task:end', {
|
|
565
|
+
taskId: record.taskId,
|
|
566
|
+
status: 'killed',
|
|
567
|
+
summary: this._formatTaskSummary(record, 'killed'),
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
catch (error) {
|
|
571
|
+
(0, log_1.logError)(`[TaskManager] dispose: error cleaning up taskId=${record.taskId}: ${error}`);
|
|
572
|
+
record.status = 'killed';
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
this.tasks.clear();
|
|
576
|
+
this.watchers.clear();
|
|
577
|
+
}
|
|
578
|
+
_notifyWatchers(taskId, delta) {
|
|
579
|
+
const cbs = this.watchers.get(taskId);
|
|
580
|
+
if (!cbs?.size)
|
|
581
|
+
return;
|
|
582
|
+
cbs.forEach(cb => cb(delta));
|
|
583
|
+
}
|
|
584
|
+
_formatTaskSummary(record, status) {
|
|
585
|
+
const cmd = record.command.length > 50
|
|
586
|
+
? record.command.slice(0, 50) + '...'
|
|
587
|
+
: record.command;
|
|
588
|
+
return `${record.type} "${cmd}" ${status}`;
|
|
589
|
+
}
|
|
590
|
+
_finishTask(record, exitCode) {
|
|
591
|
+
if (record.status !== 'running')
|
|
592
|
+
return;
|
|
593
|
+
record.status = exitCode === 0 ? 'completed' : 'failed';
|
|
594
|
+
record.exitCode = exitCode;
|
|
595
|
+
record.endTime = Date.now();
|
|
596
|
+
// 清理 watchers
|
|
597
|
+
this.watchers.delete(record.taskId);
|
|
598
|
+
(0, EventSystem_1.getEventBus)().emit('task:end', {
|
|
599
|
+
taskId: record.taskId,
|
|
600
|
+
status: record.status,
|
|
601
|
+
summary: this._formatTaskSummary(record, record.status),
|
|
602
|
+
});
|
|
603
|
+
// 前台任务正常完成,不需要 notify(结果由 Agent.ts yield 返回)
|
|
604
|
+
// 只有后台任务(包括转后台的)才 notify
|
|
605
|
+
if (!record.foreground) {
|
|
606
|
+
this._notify(record);
|
|
607
|
+
}
|
|
608
|
+
this._pruneFinishedTasks();
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* 清理已结束的任务,只保留最新的 MAX_FINISHED_TASKS 个
|
|
612
|
+
*/
|
|
613
|
+
_pruneFinishedTasks() {
|
|
614
|
+
const finished = Array.from(this.tasks.values())
|
|
615
|
+
.filter(t => t.status !== 'running');
|
|
616
|
+
if (finished.length <= TaskManager.MAX_FINISHED_TASKS)
|
|
617
|
+
return;
|
|
618
|
+
const toRemove = finished.slice(0, finished.length - TaskManager.MAX_FINISHED_TASKS);
|
|
619
|
+
for (const t of toRemove) {
|
|
620
|
+
this.tasks.delete(t.taskId);
|
|
621
|
+
this.watchers.delete(t.taskId);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
_notify(record) {
|
|
625
|
+
if (!this.notifyCallback)
|
|
626
|
+
return;
|
|
627
|
+
let msg;
|
|
628
|
+
if (record.type === 'Agent') {
|
|
629
|
+
const usageTag = record.usage
|
|
630
|
+
? `\n<usage><total_tokens>${record.usage.totalTokens}</total_tokens><tool_uses>${record.usage.toolUses}</tool_uses><duration_ms>${record.usage.durationMs}</duration_ms></usage>`
|
|
631
|
+
: '';
|
|
632
|
+
msg = `<task-notification>
|
|
633
|
+
<task-id>${record.taskId}</task-id>
|
|
634
|
+
<tool-use-id>${record.toolUseId}</tool-use-id>
|
|
635
|
+
<status>${record.status}</status>
|
|
636
|
+
<summary>Agent "${record.command}" ${record.status}</summary>
|
|
637
|
+
<result>${record.output}</result>${usageTag}
|
|
638
|
+
</task-notification>`;
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
msg = `<task-notification>
|
|
642
|
+
<task-id>${record.taskId}</task-id>
|
|
643
|
+
<tool-use-id>${record.toolUseId}</tool-use-id>
|
|
644
|
+
<output-file>${record.filepath}</output-file>
|
|
645
|
+
<status>${record.status}</status>
|
|
646
|
+
<summary>Background bash command ${record.status} (exit code ${record.exitCode ?? 'N/A'})</summary>
|
|
647
|
+
</task-notification>
|
|
648
|
+
Read the output file to retrieve the result: ${record.filepath}`;
|
|
649
|
+
}
|
|
650
|
+
this.notifyCallback(msg);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
exports.TaskManager = TaskManager;
|
|
654
|
+
TaskManager.MAX_FINISHED_TASKS = 10;
|
|
655
|
+
TaskManager.MAX_RUNNING_TASKS = 5;
|
|
656
|
+
// 单例
|
|
657
|
+
let instance = null;
|
|
658
|
+
function getTaskManager() {
|
|
659
|
+
if (!instance) {
|
|
660
|
+
instance = new TaskManager();
|
|
661
|
+
}
|
|
662
|
+
return instance;
|
|
663
|
+
}
|
|
664
|
+
//# sourceMappingURL=TaskManager.js.map
|