openloops 0.1.31 → 0.1.33
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/clients/llm-client.d.ts +5 -0
- package/dist/clients/llm-client.js +73 -15
- package/dist/clients/llm-client.mjs +73 -15
- package/dist/clients/llm-error.d.ts +9 -0
- package/dist/clients/llm-error.js +69 -0
- package/dist/clients/llm-error.mjs +45 -0
- package/dist/clients/llm-types.d.ts +61 -0
- package/dist/clients/llm-types.js +16 -0
- package/dist/clients/llm-types.mjs +0 -0
- package/dist/clients/pi-ai-adapter.d.ts +7 -0
- package/dist/clients/pi-ai-adapter.js +137 -0
- package/dist/clients/pi-ai-adapter.mjs +113 -0
- package/dist/{agent → harness}/agent-loop.d.ts +11 -1
- package/dist/{agent → harness}/agent-state.js +1 -1
- package/dist/{agent → harness}/agent-state.mjs +1 -1
- package/dist/{agent → harness}/agent.d.ts +34 -3
- package/dist/harness/agent.js +546 -0
- package/dist/harness/agent.mjs +530 -0
- package/dist/{agent → harness}/index.js +5 -5
- package/dist/harness/native-tools.d.ts +23 -0
- package/dist/harness/native-tools.js +251 -0
- package/dist/harness/native-tools.mjs +227 -0
- package/dist/harness/tool-catalog.d.ts +16 -0
- package/dist/harness/tool-catalog.js +85 -0
- package/dist/harness/tool-catalog.mjs +59 -0
- package/dist/harness/tool-pipeline.d.ts +42 -0
- package/dist/harness/tool-pipeline.js +200 -0
- package/dist/harness/tool-pipeline.mjs +177 -0
- package/dist/harness/utils/describe-shape.d.ts +2 -0
- package/dist/harness/utils/describe-shape.js +56 -0
- package/dist/harness/utils/describe-shape.mjs +32 -0
- package/dist/harness/utils/grounding.d.ts +6 -0
- package/dist/harness/utils/grounding.js +79 -0
- package/dist/harness/utils/grounding.mjs +53 -0
- package/dist/harness/workspace.d.ts +44 -0
- package/dist/harness/workspace.js +95 -0
- package/dist/harness/workspace.mjs +78 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.mjs +1 -1
- package/dist/loops/agi/index.d.ts +1 -0
- package/dist/loops/agi/index.js +22 -0
- package/dist/loops/agi/index.mjs +1 -0
- package/dist/loops/agi/loop.d.ts +24 -0
- package/dist/loops/agi/loop.js +220 -0
- package/dist/loops/agi/loop.mjs +197 -0
- package/dist/loops/agi/skills/reasoning-engine.d.ts +14 -0
- package/dist/loops/agi/skills/reasoning-engine.js +94 -0
- package/dist/loops/agi/skills/reasoning-engine.mjs +71 -0
- package/dist/loops/index.d.ts +1 -0
- package/dist/loops/index.js +3 -1
- package/dist/loops/index.mjs +1 -0
- package/dist/loops/plan-execute/plan-execute-loop.d.ts +2 -1
- package/dist/loops/plan-execute/plan-execute-loop.js +90 -35
- package/dist/loops/plan-execute/plan-execute-loop.mjs +80 -25
- package/dist/loops/shared/skills/action-planner.js +7 -4
- package/dist/loops/shared/skills/action-planner.mjs +7 -4
- package/dist/loops/shared/skills/final-responder.js +4 -3
- package/dist/loops/shared/skills/final-responder.mjs +4 -3
- package/dist/loops/shared/skills/state-observer.js +6 -2
- package/dist/loops/shared/skills/state-observer.mjs +6 -2
- package/dist/services/chats/entities/chat-message.entity.d.ts +4 -1
- package/dist/services/chats/entities/chat-message.entity.js +7 -3
- package/dist/services/chats/entities/chat-message.entity.mjs +7 -3
- package/dist/services/chats/entities/chat.entity.d.ts +10 -11
- package/dist/services/chats/entities/chat.entity.js +17 -21
- package/dist/services/chats/entities/chat.entity.mjs +17 -21
- package/dist/services/chats/lambdas/get-chat-by-id/get-chat-by-id.use-case.d.ts +2 -0
- package/dist/services/chats/lambdas/get-chat-by-id/get-chat-by-id.use-case.js +4 -4
- package/dist/services/chats/lambdas/get-chat-by-id/get-chat-by-id.use-case.mjs +2 -2
- package/dist/services/tasks/entities/agent-action.entity.d.ts +111 -0
- package/dist/services/tasks/entities/agent-action.entity.js +271 -0
- package/dist/services/tasks/entities/agent-action.entity.mjs +247 -0
- package/dist/services/tasks/entities/agent-task.entity.d.ts +176 -0
- package/dist/services/tasks/entities/agent-task.entity.js +695 -0
- package/dist/services/tasks/entities/agent-task.entity.mjs +662 -0
- package/dist/services/tasks/entities/index.d.ts +2 -0
- package/dist/services/tasks/entities/index.js +24 -0
- package/dist/services/tasks/entities/index.mjs +2 -0
- package/dist/services/tasks/index.d.ts +3 -0
- package/dist/services/tasks/index.js +26 -0
- package/dist/services/tasks/index.mjs +3 -0
- package/dist/services/tasks/lambdas/create-agent-action/create-agent-action.use-case.d.ts +14 -0
- package/dist/services/tasks/lambdas/create-agent-action/create-agent-action.use-case.js +60 -0
- package/dist/services/tasks/lambdas/create-agent-action/create-agent-action.use-case.mjs +31 -0
- package/dist/services/tasks/lambdas/create-agent-action/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/create-agent-action/index.js +22 -0
- package/dist/services/tasks/lambdas/create-agent-action/index.mjs +1 -0
- package/dist/services/tasks/lambdas/create-agent-task/create-agent-task.use-case.d.ts +14 -0
- package/dist/services/tasks/lambdas/create-agent-task/create-agent-task.use-case.js +60 -0
- package/dist/services/tasks/lambdas/create-agent-task/create-agent-task.use-case.mjs +31 -0
- package/dist/services/tasks/lambdas/create-agent-task/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/create-agent-task/index.js +22 -0
- package/dist/services/tasks/lambdas/create-agent-task/index.mjs +1 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/get-agent-task-by-id.use-case.d.ts +14 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/get-agent-task-by-id.use-case.js +55 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/get-agent-task-by-id.use-case.mjs +26 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/index.js +22 -0
- package/dist/services/tasks/lambdas/get-agent-task-by-id/index.mjs +1 -0
- package/dist/services/tasks/lambdas/index.d.ts +7 -0
- package/dist/services/tasks/lambdas/index.js +46 -0
- package/dist/services/tasks/lambdas/index.mjs +17 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/index.js +22 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/index.mjs +1 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/list-agent-actions-by-task.use-case.d.ts +16 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/list-agent-actions-by-task.use-case.js +62 -0
- package/dist/services/tasks/lambdas/list-agent-actions-by-task/list-agent-actions-by-task.use-case.mjs +33 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/index.js +22 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/index.mjs +1 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/list-agent-tasks-by-chat.use-case.d.ts +16 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/list-agent-tasks-by-chat.use-case.js +62 -0
- package/dist/services/tasks/lambdas/list-agent-tasks-by-chat/list-agent-tasks-by-chat.use-case.mjs +33 -0
- package/dist/services/tasks/lambdas/update-agent-action/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/update-agent-action/index.js +22 -0
- package/dist/services/tasks/lambdas/update-agent-action/index.mjs +1 -0
- package/dist/services/tasks/lambdas/update-agent-action/update-agent-action.use-case.d.ts +15 -0
- package/dist/services/tasks/lambdas/update-agent-action/update-agent-action.use-case.js +67 -0
- package/dist/services/tasks/lambdas/update-agent-action/update-agent-action.use-case.mjs +38 -0
- package/dist/services/tasks/lambdas/update-agent-task/index.d.ts +1 -0
- package/dist/services/tasks/lambdas/update-agent-task/index.js +22 -0
- package/dist/services/tasks/lambdas/update-agent-task/index.mjs +1 -0
- package/dist/services/tasks/lambdas/update-agent-task/update-agent-task.use-case.d.ts +15 -0
- package/dist/services/tasks/lambdas/update-agent-task/update-agent-task.use-case.js +67 -0
- package/dist/services/tasks/lambdas/update-agent-task/update-agent-task.use-case.mjs +38 -0
- package/dist/services/tasks/module.d.ts +15 -0
- package/dist/services/tasks/module.js +76 -0
- package/dist/services/tasks/module.mjs +47 -0
- package/dist/services/tasks/repositories/agent-action.repository.d.ts +8 -0
- package/dist/services/tasks/repositories/agent-action.repository.js +62 -0
- package/dist/services/tasks/repositories/agent-action.repository.mjs +33 -0
- package/dist/services/tasks/repositories/agent-task.repository.d.ts +9 -0
- package/dist/services/tasks/repositories/agent-task.repository.js +65 -0
- package/dist/services/tasks/repositories/agent-task.repository.mjs +36 -0
- package/dist/services/tasks/repositories/index.d.ts +2 -0
- package/dist/services/tasks/repositories/index.js +24 -0
- package/dist/services/tasks/repositories/index.mjs +2 -0
- package/dist/services/traces/entities/agent-trace-entry.entity.d.ts +30 -0
- package/dist/services/traces/entities/agent-trace-entry.entity.js +85 -0
- package/dist/services/traces/entities/agent-trace-entry.entity.mjs +62 -0
- package/dist/services/traces/entities/index.d.ts +1 -0
- package/dist/services/traces/entities/index.js +22 -0
- package/dist/services/traces/entities/index.mjs +1 -0
- package/dist/services/traces/index.d.ts +3 -0
- package/dist/services/traces/index.js +26 -0
- package/dist/services/traces/index.mjs +3 -0
- package/dist/services/traces/lambdas/create-agent-traces/create-agent-traces.use-case.d.ts +16 -0
- package/dist/services/traces/lambdas/create-agent-traces/create-agent-traces.use-case.js +62 -0
- package/dist/services/traces/lambdas/create-agent-traces/create-agent-traces.use-case.mjs +33 -0
- package/dist/services/traces/lambdas/create-agent-traces/index.d.ts +1 -0
- package/dist/services/traces/lambdas/create-agent-traces/index.js +22 -0
- package/dist/services/traces/lambdas/create-agent-traces/index.mjs +1 -0
- package/dist/services/traces/lambdas/index.d.ts +2 -0
- package/dist/services/traces/lambdas/index.js +31 -0
- package/dist/services/traces/lambdas/index.mjs +7 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/index.d.ts +1 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/index.js +22 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/index.mjs +1 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/list-agent-traces-by-chat.use-case.d.ts +18 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/list-agent-traces-by-chat.use-case.js +67 -0
- package/dist/services/traces/lambdas/list-agent-traces-by-chat/list-agent-traces-by-chat.use-case.mjs +38 -0
- package/dist/services/traces/module.d.ts +5 -0
- package/dist/services/traces/module.js +41 -0
- package/dist/services/traces/module.mjs +17 -0
- package/dist/services/traces/repositories/agent-trace.repository.d.ts +7 -0
- package/dist/services/traces/repositories/agent-trace.repository.js +55 -0
- package/dist/services/traces/repositories/agent-trace.repository.mjs +26 -0
- package/dist/services/traces/repositories/index.d.ts +1 -0
- package/dist/services/traces/repositories/index.js +22 -0
- package/dist/services/traces/repositories/index.mjs +1 -0
- package/dist/services/workspaces/entities/index.d.ts +1 -0
- package/dist/services/workspaces/entities/index.js +22 -0
- package/dist/services/workspaces/entities/index.mjs +1 -0
- package/dist/services/workspaces/entities/workspace-item.entity.d.ts +33 -0
- package/dist/services/workspaces/entities/workspace-item.entity.js +95 -0
- package/dist/services/workspaces/entities/workspace-item.entity.mjs +72 -0
- package/dist/services/workspaces/index.d.ts +3 -0
- package/dist/services/workspaces/index.js +26 -0
- package/dist/services/workspaces/index.mjs +3 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/delete-workspace-item.use-case.d.ts +17 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/delete-workspace-item.use-case.js +68 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/delete-workspace-item.use-case.mjs +39 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/index.d.ts +1 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/index.js +22 -0
- package/dist/services/workspaces/lambdas/delete-workspace-item/index.mjs +1 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/get-workspace-item.use-case.d.ts +17 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/get-workspace-item.use-case.js +67 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/get-workspace-item.use-case.mjs +38 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/index.d.ts +1 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/index.js +22 -0
- package/dist/services/workspaces/lambdas/get-workspace-item/index.mjs +1 -0
- package/dist/services/workspaces/lambdas/index.d.ts +5 -0
- package/dist/services/workspaces/lambdas/index.js +40 -0
- package/dist/services/workspaces/lambdas/index.mjs +13 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/index.d.ts +1 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/index.js +22 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/index.mjs +1 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/list-workspace-items.use-case.d.ts +18 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/list-workspace-items.use-case.js +69 -0
- package/dist/services/workspaces/lambdas/list-workspace-items/list-workspace-items.use-case.mjs +40 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/index.d.ts +1 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/index.js +22 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/index.mjs +1 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/save-workspace-item.use-case.d.ts +14 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/save-workspace-item.use-case.js +76 -0
- package/dist/services/workspaces/lambdas/save-workspace-item/save-workspace-item.use-case.mjs +47 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/index.d.ts +1 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/index.js +22 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/index.mjs +1 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/search-workspace-items.use-case.d.ts +19 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/search-workspace-items.use-case.js +73 -0
- package/dist/services/workspaces/lambdas/search-workspace-items/search-workspace-items.use-case.mjs +44 -0
- package/dist/services/workspaces/module.d.ts +11 -0
- package/dist/services/workspaces/module.js +62 -0
- package/dist/services/workspaces/module.mjs +35 -0
- package/dist/services/workspaces/repositories/index.d.ts +1 -0
- package/dist/services/workspaces/repositories/index.js +22 -0
- package/dist/services/workspaces/repositories/index.mjs +1 -0
- package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +18 -0
- package/dist/services/workspaces/repositories/workspace-item.repository.js +74 -0
- package/dist/services/workspaces/repositories/workspace-item.repository.mjs +45 -0
- package/dist/skills/runtime-context.d.ts +8 -0
- package/dist/skills/runtime-context.js +65 -0
- package/dist/skills/runtime-context.mjs +42 -0
- package/dist/skills/skill.d.ts +7 -0
- package/dist/skills/skill.js +26 -60
- package/dist/skills/skill.mjs +26 -60
- package/dist/tools/tool.d.ts +6 -0
- package/dist/tools/tool.js +56 -0
- package/dist/tools/tool.mjs +56 -0
- package/package.json +3 -3
- package/dist/agent/agent.js +0 -327
- package/dist/agent/agent.mjs +0 -303
- package/dist/services/chats/entities/agent-action.entity.d.ts +0 -58
- package/dist/services/chats/entities/agent-action.entity.js +0 -151
- package/dist/services/chats/entities/agent-action.entity.mjs +0 -128
- package/dist/services/chats/entities/agent-task.entity.d.ts +0 -59
- package/dist/services/chats/entities/agent-task.entity.js +0 -231
- package/dist/services/chats/entities/agent-task.entity.mjs +0 -207
- package/dist/services/chats/entities/chat-context.entity.d.ts +0 -39
- package/dist/services/chats/entities/chat-context.entity.js +0 -70
- package/dist/services/chats/entities/chat-context.entity.mjs +0 -47
- /package/dist/{agent → harness}/agent-loop.js +0 -0
- /package/dist/{agent → harness}/agent-loop.mjs +0 -0
- /package/dist/{agent → harness}/agent-state.d.ts +0 -0
- /package/dist/{agent → harness}/index.d.ts +0 -0
- /package/dist/{agent → harness}/index.mjs +0 -0
- /package/dist/{agent → harness}/utils/rank-tools-by-keywords.d.ts +0 -0
- /package/dist/{agent → harness}/utils/rank-tools-by-keywords.js +0 -0
- /package/dist/{agent → harness}/utils/rank-tools-by-keywords.mjs +0 -0
package/dist/agent/agent.js
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var agent_exports = {};
|
|
20
|
-
__export(agent_exports, {
|
|
21
|
-
AGENT_ERROR_CODES: () => AGENT_ERROR_CODES,
|
|
22
|
-
Agent: () => Agent
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(agent_exports);
|
|
25
|
-
var import_reflect_metadata = require("reflect-metadata");
|
|
26
|
-
var import_tsyringe = require("tsyringe");
|
|
27
|
-
var import_logger = require("../common/logger/index.js");
|
|
28
|
-
var import_create_chat = require("../services/chats/lambdas/create-chat/index.js");
|
|
29
|
-
var import_update_chat = require("../services/chats/lambdas/update-chat/index.js");
|
|
30
|
-
var import_entities = require("../services/mcp-servers/entities/index.js");
|
|
31
|
-
var import_repositories = require("../services/chats/repositories/index.js");
|
|
32
|
-
var import_chat = require("../services/chats/entities/chat.entity.js");
|
|
33
|
-
var import_tool = require("../tools/tool.js");
|
|
34
|
-
var import_core = require("../tools/core/index.js");
|
|
35
|
-
var import_mcp_servers = require("../services/mcp-servers/index.js");
|
|
36
|
-
var import_rank_tools_by_keywords = require("./utils/rank-tools-by-keywords");
|
|
37
|
-
var import_agent_state = require("./agent-state");
|
|
38
|
-
const AGENT_ERROR_CODES = {
|
|
39
|
-
NODE_NOT_FOUND: "AGENT_NODE_NOT_FOUND"
|
|
40
|
-
};
|
|
41
|
-
const MAX_ITERATIONS = 50;
|
|
42
|
-
class Agent {
|
|
43
|
-
updateChatUseCase = import_tsyringe.container.resolve(import_update_chat.UpdateChatUseCase);
|
|
44
|
-
chatRepository = import_tsyringe.container.resolve(import_repositories.ChatRepository);
|
|
45
|
-
logger = new import_logger.Logger();
|
|
46
|
-
_loop;
|
|
47
|
-
_identity;
|
|
48
|
-
_tools;
|
|
49
|
-
_mcps = [];
|
|
50
|
-
_preHooks = [];
|
|
51
|
-
_postHooks = [];
|
|
52
|
-
currentChat;
|
|
53
|
-
chatOptions;
|
|
54
|
-
currentUser;
|
|
55
|
-
currentSession;
|
|
56
|
-
currentMessage = "";
|
|
57
|
-
answerId = "";
|
|
58
|
-
stopLoop = false;
|
|
59
|
-
stopRequested = false;
|
|
60
|
-
pendingNextNode;
|
|
61
|
-
static async interruptExecution(chatId, currentUser) {
|
|
62
|
-
const updateChatUseCase = import_tsyringe.container.resolve(import_update_chat.UpdateChatUseCase);
|
|
63
|
-
const logger = new import_logger.Logger();
|
|
64
|
-
logger.debug("[AGENT] Interrupt execution triggered");
|
|
65
|
-
await updateChatUseCase.execute({
|
|
66
|
-
id: chatId,
|
|
67
|
-
payload: { state: { stopRequested: true } },
|
|
68
|
-
currentUser
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
static async createChatSession(currentUser, loop) {
|
|
72
|
-
if (!currentUser) throw new Error("[AGENT] CurrentUser is required to create a new chat");
|
|
73
|
-
const createChatUseCase = import_tsyringe.container.resolve(import_create_chat.CreateChatUseCase);
|
|
74
|
-
const newChat = import_chat.Chat.withDefaults(loop?.name);
|
|
75
|
-
const { data } = await createChatUseCase.execute({
|
|
76
|
-
payload: newChat.toJSON(),
|
|
77
|
-
currentUser
|
|
78
|
-
});
|
|
79
|
-
if (!data) throw new Error("[AGENT] There was an error creating the chat");
|
|
80
|
-
return import_chat.Chat.fromJSON(data);
|
|
81
|
-
}
|
|
82
|
-
constructor(agentOptions) {
|
|
83
|
-
if (!agentOptions?.loop) throw new Error("[AGENT] An AgentLoop instance is required");
|
|
84
|
-
this._loop = agentOptions.loop;
|
|
85
|
-
this._tools = agentOptions.tools ?? [];
|
|
86
|
-
this._identity = agentOptions.identity;
|
|
87
|
-
this.currentChat = import_chat.Chat.withDefaults(this._loop.name);
|
|
88
|
-
}
|
|
89
|
-
get tools() {
|
|
90
|
-
return this._tools;
|
|
91
|
-
}
|
|
92
|
-
get mcps() {
|
|
93
|
-
return this._mcps;
|
|
94
|
-
}
|
|
95
|
-
get notifyOnCompletion() {
|
|
96
|
-
return this.currentChat?.settings?.notifyOnCompletion;
|
|
97
|
-
}
|
|
98
|
-
get lastAssistantAnswer() {
|
|
99
|
-
return this.currentChat.lastAnswer;
|
|
100
|
-
}
|
|
101
|
-
async run(props) {
|
|
102
|
-
try {
|
|
103
|
-
const { input, options, currentUser, currentSession } = props || {};
|
|
104
|
-
const { chatId, message } = input || {};
|
|
105
|
-
this.logger.debug(input, "[AGENT] Agent started");
|
|
106
|
-
if (!this._loop?.nodes || !this._loop?.initialNode) throw new Error("[AGENT] Loop is missing");
|
|
107
|
-
if (!message) throw new Error("[AGENT] Message is required");
|
|
108
|
-
if (!currentUser) throw new Error("[AGENT] CurrentUser is required");
|
|
109
|
-
this.stopLoop = false;
|
|
110
|
-
this.stopRequested = false;
|
|
111
|
-
this.pendingNextNode = void 0;
|
|
112
|
-
this.currentUser = currentUser;
|
|
113
|
-
this.currentSession = currentSession;
|
|
114
|
-
this.currentMessage = message;
|
|
115
|
-
this.chatOptions = options;
|
|
116
|
-
this.answerId = new (require("mongodb")).ObjectId().toString();
|
|
117
|
-
await this.preRunner(chatId);
|
|
118
|
-
await this.runLoop();
|
|
119
|
-
await this.postRunner();
|
|
120
|
-
return { data: this.currentChat };
|
|
121
|
-
} catch (error) {
|
|
122
|
-
this.currentChat.state?.setStatus(import_agent_state.AgentStatus.FAILED);
|
|
123
|
-
this.currentChat.state?.setCurrentActivity("stopped");
|
|
124
|
-
await this.saveChat();
|
|
125
|
-
await this.disconnectMcpServers();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
stopAgent() {
|
|
129
|
-
this.stopLoop = true;
|
|
130
|
-
}
|
|
131
|
-
addTool(tool) {
|
|
132
|
-
this._tools.push(tool);
|
|
133
|
-
}
|
|
134
|
-
addHook(type, fn) {
|
|
135
|
-
if (type === "pre_execution") this._preHooks.push(fn);
|
|
136
|
-
if (type === "post_execution") this._postHooks.push(fn);
|
|
137
|
-
}
|
|
138
|
-
async preRunner(chatId) {
|
|
139
|
-
let chatData;
|
|
140
|
-
if (chatId) {
|
|
141
|
-
chatData = await this.chatRepository?.findById(chatId);
|
|
142
|
-
}
|
|
143
|
-
if (!chatData) {
|
|
144
|
-
this.logger.debug("[AGENT] Not existing chat found");
|
|
145
|
-
if (!this.currentUser) throw new Error("[AGENT] CurrentUser params is required");
|
|
146
|
-
this.currentChat = await Agent.createChatSession(this.currentUser, this._loop);
|
|
147
|
-
} else {
|
|
148
|
-
this.logger.debug("[AGENT] Existing chat found");
|
|
149
|
-
this.currentChat = import_chat.Chat.fromJSON(chatData);
|
|
150
|
-
this.currentChat.state?.setStopRequested(false);
|
|
151
|
-
this.currentChat.switchLoop(this._loop.name);
|
|
152
|
-
}
|
|
153
|
-
this.currentChat.pushMessage({
|
|
154
|
-
role: "user",
|
|
155
|
-
content: this.currentMessage
|
|
156
|
-
});
|
|
157
|
-
if (this.chatOptions?.notifyOnCompletion) this.currentChat.settings?.setNotifyOnCompletion(true);
|
|
158
|
-
if (this.chatOptions?.isPrivateSession) this.currentChat.settings?.setIsPrivateSession(true);
|
|
159
|
-
if (this.chatOptions?.modelName) this.currentChat.settings?.setModelName(this.chatOptions.modelName);
|
|
160
|
-
if (this.chatOptions?.loopName) this.currentChat.settings?.setLoopName(this.chatOptions.loopName);
|
|
161
|
-
this.currentChat.state?.setStatus(import_agent_state.AgentStatus.PROCESSING);
|
|
162
|
-
this.currentChat.state?.setCurrentActivity("Starting...");
|
|
163
|
-
await this.saveChat({ resetStop: true });
|
|
164
|
-
await this.loadMcpServers();
|
|
165
|
-
for (const hook of this._preHooks) {
|
|
166
|
-
await hook();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
async postRunner() {
|
|
170
|
-
for (const hook of this._postHooks) {
|
|
171
|
-
await hook();
|
|
172
|
-
}
|
|
173
|
-
this.currentChat.state?.setCurrentActivity("stopped");
|
|
174
|
-
await this.saveChat();
|
|
175
|
-
await this.disconnectMcpServers();
|
|
176
|
-
}
|
|
177
|
-
async loadMcpServers() {
|
|
178
|
-
if (!this.currentUser) return;
|
|
179
|
-
const { data: servers } = await (0, import_mcp_servers.listMcpServersByUser)({
|
|
180
|
-
currentUser: this.currentUser
|
|
181
|
-
});
|
|
182
|
-
for (const props of servers ?? []) {
|
|
183
|
-
const mcpServer = import_entities.McpServer.fromJSON(props);
|
|
184
|
-
try {
|
|
185
|
-
const mcpClient = await mcpServer.connect();
|
|
186
|
-
const { tools: mcpTools } = await mcpClient.listTools();
|
|
187
|
-
const tools = mcpTools.map((mcpTool) => import_tool.Tool.fromMcp(mcpTool, mcpClient, { namespace: mcpServer.name ?? "mcp" }));
|
|
188
|
-
this._tools.push(...tools);
|
|
189
|
-
this._mcps.push(mcpServer);
|
|
190
|
-
} catch (error) {
|
|
191
|
-
this.logger.error({ error, mcpServer: mcpServer.name }, "[AGENT] Failed to connect MCP server");
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
async disconnectMcpServers() {
|
|
196
|
-
for (const mcpServer of this._mcps) {
|
|
197
|
-
await mcpServer.disconnect().catch((error) => {
|
|
198
|
-
this.logger.error({ error, mcpServer: mcpServer.name }, "[AGENT] Failed to disconnect MCP server");
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
this._mcps = [];
|
|
202
|
-
}
|
|
203
|
-
async saveChat(props) {
|
|
204
|
-
if (this.currentChat?._id) {
|
|
205
|
-
this.logger.debug("[AGENT] Updating chat...");
|
|
206
|
-
if (!props?.resetStop) {
|
|
207
|
-
this.logger.debug("[Agent] Removing stopRequested");
|
|
208
|
-
delete this.currentChat.state?.stopRequested;
|
|
209
|
-
}
|
|
210
|
-
await this.updateChatUseCase?.execute({
|
|
211
|
-
id: this.currentChat?._id.toString(),
|
|
212
|
-
payload: this.currentChat.toJSON(),
|
|
213
|
-
currentUser: this.currentUser
|
|
214
|
-
}) ?? {};
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
async checkInterruptionRequest() {
|
|
218
|
-
const chatId = this.currentChat?._id?.toString();
|
|
219
|
-
const chatData = chatId && await this.chatRepository.findById(chatId, { state: 1 });
|
|
220
|
-
if (chatData && chatData.state?.stopRequested) this.stopRequested = true;
|
|
221
|
-
}
|
|
222
|
-
async runLoop() {
|
|
223
|
-
this.logger.info("[AGENT] Agent Loop started");
|
|
224
|
-
let iterations = 0;
|
|
225
|
-
while (!this.stopLoop && iterations < MAX_ITERATIONS) {
|
|
226
|
-
iterations++;
|
|
227
|
-
await this.checkInterruptionRequest();
|
|
228
|
-
if (this.stopRequested) {
|
|
229
|
-
this.stopLoop = true;
|
|
230
|
-
this.currentChat.state?.setStatus(import_agent_state.AgentStatus.IDLE);
|
|
231
|
-
this.resetNextNode();
|
|
232
|
-
this.currentChat.state?.setStopRequested(false);
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
const activeTask = this.currentChat.activeTask;
|
|
236
|
-
const nodeName = activeTask?.nextNode ?? this.pendingNextNode ?? this.resolveNodeName(this._loop.initialNode);
|
|
237
|
-
const nodeFn = nodeName ? this._loop.nodes[nodeName] : void 0;
|
|
238
|
-
if (!nodeFn) {
|
|
239
|
-
this.logger.error({ nodeName, loop: this._loop.name }, "[AGENT] Node not found in current loop");
|
|
240
|
-
this.currentChat.state?.setStatus(import_agent_state.AgentStatus.FAILED);
|
|
241
|
-
this.currentChat.state?.setLastError({
|
|
242
|
-
code: AGENT_ERROR_CODES.NODE_NOT_FOUND,
|
|
243
|
-
message: `Node "${nodeName}" is not implemented by loop "${this._loop.name}"`,
|
|
244
|
-
isRetryable: false,
|
|
245
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
246
|
-
});
|
|
247
|
-
this.currentChat.addTrace({ node: nodeName ?? "", reasoning: "Node not found" });
|
|
248
|
-
this.stopLoop = true;
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
await nodeFn.call(this._loop, this.buildRunContext());
|
|
252
|
-
await this.saveChat();
|
|
253
|
-
if (!this.currentChat.state || this.currentChat.state?.status === import_agent_state.AgentStatus.IDLE || this.currentChat.state?.status === import_agent_state.AgentStatus.FAILED || this.currentChat.state?.status === import_agent_state.AgentStatus.AWAITING_USER_INPUT || this.currentChat.state?.status === import_agent_state.AgentStatus.AWAITING_USER_CONFIRMATION) {
|
|
254
|
-
this.stopLoop = true;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
this.logger.info("[AGENT] Agent Loop stopped");
|
|
259
|
-
}
|
|
260
|
-
resetNextNode() {
|
|
261
|
-
const resolved = this.resolveNodeName(this._loop.initialNode);
|
|
262
|
-
const active = this.currentChat.activeTask;
|
|
263
|
-
if (active) active.setNextNode(resolved);
|
|
264
|
-
else this.pendingNextNode = resolved;
|
|
265
|
-
}
|
|
266
|
-
resolveNodeName(node) {
|
|
267
|
-
if (typeof node !== "function") return node;
|
|
268
|
-
return Object.entries(this._loop.nodes).find(([, fn]) => fn === node)?.[0];
|
|
269
|
-
}
|
|
270
|
-
searchTools(keywords, opts) {
|
|
271
|
-
this.logger.debug({ opts }, "[AGENT] Searching tools");
|
|
272
|
-
const page = opts?.page ?? 1;
|
|
273
|
-
const limit = opts?.limit ?? 5;
|
|
274
|
-
const matchedTools = (0, import_rank_tools_by_keywords.rankToolsByKeywords)(keywords, this._tools);
|
|
275
|
-
if (matchedTools.length <= 5 && !matchedTools.some((t) => t.toolName === import_core.webSearchTool.name)) {
|
|
276
|
-
matchedTools.push({ toolName: import_core.webSearchTool.name, score: 0.1, matchedKeywords: [], tool: import_core.webSearchTool });
|
|
277
|
-
}
|
|
278
|
-
if (matchedTools.length <= 5 && !matchedTools.some((t) => t.toolName === import_core.callAiTool.name)) {
|
|
279
|
-
matchedTools.push({ toolName: import_core.callAiTool.name, score: 0.1, matchedKeywords: [], tool: import_core.callAiTool });
|
|
280
|
-
}
|
|
281
|
-
const startIndex = (page - 1) * limit;
|
|
282
|
-
return matchedTools.slice(startIndex, startIndex + limit);
|
|
283
|
-
}
|
|
284
|
-
buildRunContext() {
|
|
285
|
-
const sessionId = this.currentChat._id?.toString() ?? "";
|
|
286
|
-
return {
|
|
287
|
-
identity: this._identity,
|
|
288
|
-
task: this.currentChat.activeTask,
|
|
289
|
-
chat: this.currentChat,
|
|
290
|
-
chatOptions: this.chatOptions,
|
|
291
|
-
currentUser: this.currentUser,
|
|
292
|
-
currentSession: this.currentSession,
|
|
293
|
-
currentMessage: this.currentMessage,
|
|
294
|
-
answerId: this.answerId,
|
|
295
|
-
tools: this._tools,
|
|
296
|
-
searchTools: this.searchTools.bind(this),
|
|
297
|
-
baseParams: {
|
|
298
|
-
currentUser: this.currentUser,
|
|
299
|
-
sessionId,
|
|
300
|
-
answerId: this.answerId
|
|
301
|
-
},
|
|
302
|
-
skillParams: {
|
|
303
|
-
provider: this.chatOptions?.modelProvider,
|
|
304
|
-
modelName: this.chatOptions?.modelName,
|
|
305
|
-
sessionId,
|
|
306
|
-
answerId: this.answerId,
|
|
307
|
-
userMessage: this.currentMessage,
|
|
308
|
-
currentUser: this.currentUser,
|
|
309
|
-
currentSession: this.currentSession,
|
|
310
|
-
chat: this.currentChat
|
|
311
|
-
},
|
|
312
|
-
reply: (content) => this.currentChat.pushMessage({ _id: this.answerId, role: "assistant", content }),
|
|
313
|
-
setNextNode: (node) => {
|
|
314
|
-
const resolved = this.resolveNodeName(node);
|
|
315
|
-
const active = this.currentChat.activeTask;
|
|
316
|
-
if (active) active.setNextNode(resolved);
|
|
317
|
-
else this.pendingNextNode = resolved;
|
|
318
|
-
},
|
|
319
|
-
logger: this.logger
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
324
|
-
0 && (module.exports = {
|
|
325
|
-
AGENT_ERROR_CODES,
|
|
326
|
-
Agent
|
|
327
|
-
});
|
package/dist/agent/agent.mjs
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import "../chunk-CF3WOMB3.mjs";
|
|
2
|
-
import "reflect-metadata";
|
|
3
|
-
import { container } from "tsyringe";
|
|
4
|
-
import { Logger } from "../common/logger/index.js";
|
|
5
|
-
import { CreateChatUseCase } from "../services/chats/lambdas/create-chat/index.js";
|
|
6
|
-
import { UpdateChatUseCase } from "../services/chats/lambdas/update-chat/index.js";
|
|
7
|
-
import { McpServer } from "../services/mcp-servers/entities/index.js";
|
|
8
|
-
import { ChatRepository } from "../services/chats/repositories/index.js";
|
|
9
|
-
import { Chat } from "../services/chats/entities/chat.entity.js";
|
|
10
|
-
import { Tool } from "../tools/tool.js";
|
|
11
|
-
import { webSearchTool, callAiTool } from "../tools/core/index.js";
|
|
12
|
-
import { listMcpServersByUser } from "../services/mcp-servers/index.js";
|
|
13
|
-
import { rankToolsByKeywords } from "./utils/rank-tools-by-keywords";
|
|
14
|
-
import { AgentStatus } from "./agent-state";
|
|
15
|
-
const AGENT_ERROR_CODES = {
|
|
16
|
-
NODE_NOT_FOUND: "AGENT_NODE_NOT_FOUND"
|
|
17
|
-
};
|
|
18
|
-
const MAX_ITERATIONS = 50;
|
|
19
|
-
class Agent {
|
|
20
|
-
updateChatUseCase = container.resolve(UpdateChatUseCase);
|
|
21
|
-
chatRepository = container.resolve(ChatRepository);
|
|
22
|
-
logger = new Logger();
|
|
23
|
-
_loop;
|
|
24
|
-
_identity;
|
|
25
|
-
_tools;
|
|
26
|
-
_mcps = [];
|
|
27
|
-
_preHooks = [];
|
|
28
|
-
_postHooks = [];
|
|
29
|
-
currentChat;
|
|
30
|
-
chatOptions;
|
|
31
|
-
currentUser;
|
|
32
|
-
currentSession;
|
|
33
|
-
currentMessage = "";
|
|
34
|
-
answerId = "";
|
|
35
|
-
stopLoop = false;
|
|
36
|
-
stopRequested = false;
|
|
37
|
-
pendingNextNode;
|
|
38
|
-
static async interruptExecution(chatId, currentUser) {
|
|
39
|
-
const updateChatUseCase = container.resolve(UpdateChatUseCase);
|
|
40
|
-
const logger = new Logger();
|
|
41
|
-
logger.debug("[AGENT] Interrupt execution triggered");
|
|
42
|
-
await updateChatUseCase.execute({
|
|
43
|
-
id: chatId,
|
|
44
|
-
payload: { state: { stopRequested: true } },
|
|
45
|
-
currentUser
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
static async createChatSession(currentUser, loop) {
|
|
49
|
-
if (!currentUser) throw new Error("[AGENT] CurrentUser is required to create a new chat");
|
|
50
|
-
const createChatUseCase = container.resolve(CreateChatUseCase);
|
|
51
|
-
const newChat = Chat.withDefaults(loop?.name);
|
|
52
|
-
const { data } = await createChatUseCase.execute({
|
|
53
|
-
payload: newChat.toJSON(),
|
|
54
|
-
currentUser
|
|
55
|
-
});
|
|
56
|
-
if (!data) throw new Error("[AGENT] There was an error creating the chat");
|
|
57
|
-
return Chat.fromJSON(data);
|
|
58
|
-
}
|
|
59
|
-
constructor(agentOptions) {
|
|
60
|
-
if (!agentOptions?.loop) throw new Error("[AGENT] An AgentLoop instance is required");
|
|
61
|
-
this._loop = agentOptions.loop;
|
|
62
|
-
this._tools = agentOptions.tools ?? [];
|
|
63
|
-
this._identity = agentOptions.identity;
|
|
64
|
-
this.currentChat = Chat.withDefaults(this._loop.name);
|
|
65
|
-
}
|
|
66
|
-
get tools() {
|
|
67
|
-
return this._tools;
|
|
68
|
-
}
|
|
69
|
-
get mcps() {
|
|
70
|
-
return this._mcps;
|
|
71
|
-
}
|
|
72
|
-
get notifyOnCompletion() {
|
|
73
|
-
return this.currentChat?.settings?.notifyOnCompletion;
|
|
74
|
-
}
|
|
75
|
-
get lastAssistantAnswer() {
|
|
76
|
-
return this.currentChat.lastAnswer;
|
|
77
|
-
}
|
|
78
|
-
async run(props) {
|
|
79
|
-
try {
|
|
80
|
-
const { input, options, currentUser, currentSession } = props || {};
|
|
81
|
-
const { chatId, message } = input || {};
|
|
82
|
-
this.logger.debug(input, "[AGENT] Agent started");
|
|
83
|
-
if (!this._loop?.nodes || !this._loop?.initialNode) throw new Error("[AGENT] Loop is missing");
|
|
84
|
-
if (!message) throw new Error("[AGENT] Message is required");
|
|
85
|
-
if (!currentUser) throw new Error("[AGENT] CurrentUser is required");
|
|
86
|
-
this.stopLoop = false;
|
|
87
|
-
this.stopRequested = false;
|
|
88
|
-
this.pendingNextNode = void 0;
|
|
89
|
-
this.currentUser = currentUser;
|
|
90
|
-
this.currentSession = currentSession;
|
|
91
|
-
this.currentMessage = message;
|
|
92
|
-
this.chatOptions = options;
|
|
93
|
-
this.answerId = new (require("mongodb")).ObjectId().toString();
|
|
94
|
-
await this.preRunner(chatId);
|
|
95
|
-
await this.runLoop();
|
|
96
|
-
await this.postRunner();
|
|
97
|
-
return { data: this.currentChat };
|
|
98
|
-
} catch (error) {
|
|
99
|
-
this.currentChat.state?.setStatus(AgentStatus.FAILED);
|
|
100
|
-
this.currentChat.state?.setCurrentActivity("stopped");
|
|
101
|
-
await this.saveChat();
|
|
102
|
-
await this.disconnectMcpServers();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
stopAgent() {
|
|
106
|
-
this.stopLoop = true;
|
|
107
|
-
}
|
|
108
|
-
addTool(tool) {
|
|
109
|
-
this._tools.push(tool);
|
|
110
|
-
}
|
|
111
|
-
addHook(type, fn) {
|
|
112
|
-
if (type === "pre_execution") this._preHooks.push(fn);
|
|
113
|
-
if (type === "post_execution") this._postHooks.push(fn);
|
|
114
|
-
}
|
|
115
|
-
async preRunner(chatId) {
|
|
116
|
-
let chatData;
|
|
117
|
-
if (chatId) {
|
|
118
|
-
chatData = await this.chatRepository?.findById(chatId);
|
|
119
|
-
}
|
|
120
|
-
if (!chatData) {
|
|
121
|
-
this.logger.debug("[AGENT] Not existing chat found");
|
|
122
|
-
if (!this.currentUser) throw new Error("[AGENT] CurrentUser params is required");
|
|
123
|
-
this.currentChat = await Agent.createChatSession(this.currentUser, this._loop);
|
|
124
|
-
} else {
|
|
125
|
-
this.logger.debug("[AGENT] Existing chat found");
|
|
126
|
-
this.currentChat = Chat.fromJSON(chatData);
|
|
127
|
-
this.currentChat.state?.setStopRequested(false);
|
|
128
|
-
this.currentChat.switchLoop(this._loop.name);
|
|
129
|
-
}
|
|
130
|
-
this.currentChat.pushMessage({
|
|
131
|
-
role: "user",
|
|
132
|
-
content: this.currentMessage
|
|
133
|
-
});
|
|
134
|
-
if (this.chatOptions?.notifyOnCompletion) this.currentChat.settings?.setNotifyOnCompletion(true);
|
|
135
|
-
if (this.chatOptions?.isPrivateSession) this.currentChat.settings?.setIsPrivateSession(true);
|
|
136
|
-
if (this.chatOptions?.modelName) this.currentChat.settings?.setModelName(this.chatOptions.modelName);
|
|
137
|
-
if (this.chatOptions?.loopName) this.currentChat.settings?.setLoopName(this.chatOptions.loopName);
|
|
138
|
-
this.currentChat.state?.setStatus(AgentStatus.PROCESSING);
|
|
139
|
-
this.currentChat.state?.setCurrentActivity("Starting...");
|
|
140
|
-
await this.saveChat({ resetStop: true });
|
|
141
|
-
await this.loadMcpServers();
|
|
142
|
-
for (const hook of this._preHooks) {
|
|
143
|
-
await hook();
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
async postRunner() {
|
|
147
|
-
for (const hook of this._postHooks) {
|
|
148
|
-
await hook();
|
|
149
|
-
}
|
|
150
|
-
this.currentChat.state?.setCurrentActivity("stopped");
|
|
151
|
-
await this.saveChat();
|
|
152
|
-
await this.disconnectMcpServers();
|
|
153
|
-
}
|
|
154
|
-
async loadMcpServers() {
|
|
155
|
-
if (!this.currentUser) return;
|
|
156
|
-
const { data: servers } = await listMcpServersByUser({
|
|
157
|
-
currentUser: this.currentUser
|
|
158
|
-
});
|
|
159
|
-
for (const props of servers ?? []) {
|
|
160
|
-
const mcpServer = McpServer.fromJSON(props);
|
|
161
|
-
try {
|
|
162
|
-
const mcpClient = await mcpServer.connect();
|
|
163
|
-
const { tools: mcpTools } = await mcpClient.listTools();
|
|
164
|
-
const tools = mcpTools.map((mcpTool) => Tool.fromMcp(mcpTool, mcpClient, { namespace: mcpServer.name ?? "mcp" }));
|
|
165
|
-
this._tools.push(...tools);
|
|
166
|
-
this._mcps.push(mcpServer);
|
|
167
|
-
} catch (error) {
|
|
168
|
-
this.logger.error({ error, mcpServer: mcpServer.name }, "[AGENT] Failed to connect MCP server");
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async disconnectMcpServers() {
|
|
173
|
-
for (const mcpServer of this._mcps) {
|
|
174
|
-
await mcpServer.disconnect().catch((error) => {
|
|
175
|
-
this.logger.error({ error, mcpServer: mcpServer.name }, "[AGENT] Failed to disconnect MCP server");
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
this._mcps = [];
|
|
179
|
-
}
|
|
180
|
-
async saveChat(props) {
|
|
181
|
-
if (this.currentChat?._id) {
|
|
182
|
-
this.logger.debug("[AGENT] Updating chat...");
|
|
183
|
-
if (!props?.resetStop) {
|
|
184
|
-
this.logger.debug("[Agent] Removing stopRequested");
|
|
185
|
-
delete this.currentChat.state?.stopRequested;
|
|
186
|
-
}
|
|
187
|
-
await this.updateChatUseCase?.execute({
|
|
188
|
-
id: this.currentChat?._id.toString(),
|
|
189
|
-
payload: this.currentChat.toJSON(),
|
|
190
|
-
currentUser: this.currentUser
|
|
191
|
-
}) ?? {};
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
async checkInterruptionRequest() {
|
|
195
|
-
const chatId = this.currentChat?._id?.toString();
|
|
196
|
-
const chatData = chatId && await this.chatRepository.findById(chatId, { state: 1 });
|
|
197
|
-
if (chatData && chatData.state?.stopRequested) this.stopRequested = true;
|
|
198
|
-
}
|
|
199
|
-
async runLoop() {
|
|
200
|
-
this.logger.info("[AGENT] Agent Loop started");
|
|
201
|
-
let iterations = 0;
|
|
202
|
-
while (!this.stopLoop && iterations < MAX_ITERATIONS) {
|
|
203
|
-
iterations++;
|
|
204
|
-
await this.checkInterruptionRequest();
|
|
205
|
-
if (this.stopRequested) {
|
|
206
|
-
this.stopLoop = true;
|
|
207
|
-
this.currentChat.state?.setStatus(AgentStatus.IDLE);
|
|
208
|
-
this.resetNextNode();
|
|
209
|
-
this.currentChat.state?.setStopRequested(false);
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
const activeTask = this.currentChat.activeTask;
|
|
213
|
-
const nodeName = activeTask?.nextNode ?? this.pendingNextNode ?? this.resolveNodeName(this._loop.initialNode);
|
|
214
|
-
const nodeFn = nodeName ? this._loop.nodes[nodeName] : void 0;
|
|
215
|
-
if (!nodeFn) {
|
|
216
|
-
this.logger.error({ nodeName, loop: this._loop.name }, "[AGENT] Node not found in current loop");
|
|
217
|
-
this.currentChat.state?.setStatus(AgentStatus.FAILED);
|
|
218
|
-
this.currentChat.state?.setLastError({
|
|
219
|
-
code: AGENT_ERROR_CODES.NODE_NOT_FOUND,
|
|
220
|
-
message: `Node "${nodeName}" is not implemented by loop "${this._loop.name}"`,
|
|
221
|
-
isRetryable: false,
|
|
222
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
223
|
-
});
|
|
224
|
-
this.currentChat.addTrace({ node: nodeName ?? "", reasoning: "Node not found" });
|
|
225
|
-
this.stopLoop = true;
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
await nodeFn.call(this._loop, this.buildRunContext());
|
|
229
|
-
await this.saveChat();
|
|
230
|
-
if (!this.currentChat.state || this.currentChat.state?.status === AgentStatus.IDLE || this.currentChat.state?.status === AgentStatus.FAILED || this.currentChat.state?.status === AgentStatus.AWAITING_USER_INPUT || this.currentChat.state?.status === AgentStatus.AWAITING_USER_CONFIRMATION) {
|
|
231
|
-
this.stopLoop = true;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
this.logger.info("[AGENT] Agent Loop stopped");
|
|
236
|
-
}
|
|
237
|
-
resetNextNode() {
|
|
238
|
-
const resolved = this.resolveNodeName(this._loop.initialNode);
|
|
239
|
-
const active = this.currentChat.activeTask;
|
|
240
|
-
if (active) active.setNextNode(resolved);
|
|
241
|
-
else this.pendingNextNode = resolved;
|
|
242
|
-
}
|
|
243
|
-
resolveNodeName(node) {
|
|
244
|
-
if (typeof node !== "function") return node;
|
|
245
|
-
return Object.entries(this._loop.nodes).find(([, fn]) => fn === node)?.[0];
|
|
246
|
-
}
|
|
247
|
-
searchTools(keywords, opts) {
|
|
248
|
-
this.logger.debug({ opts }, "[AGENT] Searching tools");
|
|
249
|
-
const page = opts?.page ?? 1;
|
|
250
|
-
const limit = opts?.limit ?? 5;
|
|
251
|
-
const matchedTools = rankToolsByKeywords(keywords, this._tools);
|
|
252
|
-
if (matchedTools.length <= 5 && !matchedTools.some((t) => t.toolName === webSearchTool.name)) {
|
|
253
|
-
matchedTools.push({ toolName: webSearchTool.name, score: 0.1, matchedKeywords: [], tool: webSearchTool });
|
|
254
|
-
}
|
|
255
|
-
if (matchedTools.length <= 5 && !matchedTools.some((t) => t.toolName === callAiTool.name)) {
|
|
256
|
-
matchedTools.push({ toolName: callAiTool.name, score: 0.1, matchedKeywords: [], tool: callAiTool });
|
|
257
|
-
}
|
|
258
|
-
const startIndex = (page - 1) * limit;
|
|
259
|
-
return matchedTools.slice(startIndex, startIndex + limit);
|
|
260
|
-
}
|
|
261
|
-
buildRunContext() {
|
|
262
|
-
const sessionId = this.currentChat._id?.toString() ?? "";
|
|
263
|
-
return {
|
|
264
|
-
identity: this._identity,
|
|
265
|
-
task: this.currentChat.activeTask,
|
|
266
|
-
chat: this.currentChat,
|
|
267
|
-
chatOptions: this.chatOptions,
|
|
268
|
-
currentUser: this.currentUser,
|
|
269
|
-
currentSession: this.currentSession,
|
|
270
|
-
currentMessage: this.currentMessage,
|
|
271
|
-
answerId: this.answerId,
|
|
272
|
-
tools: this._tools,
|
|
273
|
-
searchTools: this.searchTools.bind(this),
|
|
274
|
-
baseParams: {
|
|
275
|
-
currentUser: this.currentUser,
|
|
276
|
-
sessionId,
|
|
277
|
-
answerId: this.answerId
|
|
278
|
-
},
|
|
279
|
-
skillParams: {
|
|
280
|
-
provider: this.chatOptions?.modelProvider,
|
|
281
|
-
modelName: this.chatOptions?.modelName,
|
|
282
|
-
sessionId,
|
|
283
|
-
answerId: this.answerId,
|
|
284
|
-
userMessage: this.currentMessage,
|
|
285
|
-
currentUser: this.currentUser,
|
|
286
|
-
currentSession: this.currentSession,
|
|
287
|
-
chat: this.currentChat
|
|
288
|
-
},
|
|
289
|
-
reply: (content) => this.currentChat.pushMessage({ _id: this.answerId, role: "assistant", content }),
|
|
290
|
-
setNextNode: (node) => {
|
|
291
|
-
const resolved = this.resolveNodeName(node);
|
|
292
|
-
const active = this.currentChat.activeTask;
|
|
293
|
-
if (active) active.setNextNode(resolved);
|
|
294
|
-
else this.pendingNextNode = resolved;
|
|
295
|
-
},
|
|
296
|
-
logger: this.logger
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
export {
|
|
301
|
-
AGENT_ERROR_CODES,
|
|
302
|
-
Agent
|
|
303
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { BaseEntity, BaseEntityProps } from '../../../common/base/base.entity.js';
|
|
2
|
-
import { Tool, BaseParams } from '../../../tools/tool.js';
|
|
3
|
-
export type ActionStatus = 'pending' | 'running' | 'completed' | 'failed';
|
|
4
|
-
export interface ActionErrorProps {
|
|
5
|
-
message: string;
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
}
|
|
8
|
-
export interface AgentActionProps extends BaseEntityProps {
|
|
9
|
-
name?: string;
|
|
10
|
-
stepId?: string;
|
|
11
|
-
args?: Record<string, any>;
|
|
12
|
-
dependsOn?: string[];
|
|
13
|
-
status?: ActionStatus;
|
|
14
|
-
output?: any;
|
|
15
|
-
error?: ActionErrorProps;
|
|
16
|
-
reasoning?: string;
|
|
17
|
-
retries?: number;
|
|
18
|
-
author?: string;
|
|
19
|
-
startedAt?: Date;
|
|
20
|
-
completedAt?: Date;
|
|
21
|
-
}
|
|
22
|
-
export declare class AgentAction extends BaseEntity {
|
|
23
|
-
private readonly props;
|
|
24
|
-
name?: string | undefined;
|
|
25
|
-
stepId?: string | undefined;
|
|
26
|
-
args?: Record<string, any> | undefined;
|
|
27
|
-
dependsOn?: string[] | undefined;
|
|
28
|
-
status?: ActionStatus | undefined;
|
|
29
|
-
output?: any | undefined;
|
|
30
|
-
error?: ActionErrorProps | undefined;
|
|
31
|
-
reasoning?: string | undefined;
|
|
32
|
-
retries?: number | undefined;
|
|
33
|
-
author?: string | undefined;
|
|
34
|
-
startedAt?: Date | undefined;
|
|
35
|
-
completedAt?: Date | undefined;
|
|
36
|
-
static factory(action: AgentActionProps): AgentAction;
|
|
37
|
-
static fromJSON(props: AgentActionProps): AgentAction;
|
|
38
|
-
constructor(props: AgentActionProps, name?: string | undefined, stepId?: string | undefined, args?: Record<string, any> | undefined, dependsOn?: string[] | undefined, status?: ActionStatus | undefined, output?: any | undefined, error?: ActionErrorProps | undefined, reasoning?: string | undefined, retries?: number | undefined, author?: string | undefined, startedAt?: Date | undefined, completedAt?: Date | undefined);
|
|
39
|
-
toJSON(): AgentActionProps;
|
|
40
|
-
get id(): string;
|
|
41
|
-
get isReady(): boolean;
|
|
42
|
-
get isTerminal(): boolean;
|
|
43
|
-
dependenciesSatisfiedBy(completedRefs: Set<string>): boolean;
|
|
44
|
-
markRunning(): void;
|
|
45
|
-
markCompleted(output: any): void;
|
|
46
|
-
markFailed(error: ActionErrorProps): void;
|
|
47
|
-
incrementRetries(): number;
|
|
48
|
-
resetForRetry(): void;
|
|
49
|
-
runTool(tool: Tool | undefined, args: Record<string, any>, baseParams: BaseParams): Promise<this>;
|
|
50
|
-
}
|
|
51
|
-
export interface ActionDescriptor {
|
|
52
|
-
name: string;
|
|
53
|
-
stepId?: string;
|
|
54
|
-
args?: Record<string, any>;
|
|
55
|
-
dependsOn?: string[];
|
|
56
|
-
reasoning?: string;
|
|
57
|
-
author?: string;
|
|
58
|
-
}
|