@minded-ai/mindedjs 1.0.151-beta-1 → 2.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +2 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +33 -55
- package/dist/agent.js.map +1 -1
- package/dist/cli/index.js +0 -0
- package/dist/edges/createLogicalRouter.d.ts.map +1 -1
- package/dist/edges/createLogicalRouter.js +4 -18
- package/dist/edges/createLogicalRouter.js.map +1 -1
- package/dist/events/AgentEvents.d.ts +5 -14
- package/dist/events/AgentEvents.d.ts.map +1 -1
- package/dist/guidelines/guidelinesManager.d.ts +37 -0
- package/dist/guidelines/guidelinesManager.d.ts.map +1 -0
- package/dist/guidelines/guidelinesManager.js +172 -0
- package/dist/guidelines/guidelinesManager.js.map +1 -0
- package/dist/internalTools/documentExtraction.d.ts +69 -0
- package/dist/internalTools/documentExtraction.d.ts.map +1 -0
- package/dist/internalTools/documentExtraction.js +65 -0
- package/dist/internalTools/documentExtraction.js.map +1 -0
- package/dist/internalTools/libraryActionRunnerTool.d.ts.map +1 -1
- package/dist/internalTools/libraryActionRunnerTool.js +0 -1
- package/dist/internalTools/libraryActionRunnerTool.js.map +1 -1
- package/dist/internalTools/managedDocumentParser/managedDocumentParser.d.ts +10 -0
- package/dist/internalTools/managedDocumentParser/managedDocumentParser.d.ts.map +1 -0
- package/dist/internalTools/managedDocumentParser/managedDocumentParser.js +80 -0
- package/dist/internalTools/managedDocumentParser/managedDocumentParser.js.map +1 -0
- package/dist/internalTools/retell.js +7 -17
- package/dist/internalTools/retell.js.map +1 -1
- package/dist/internalTools/sendPlaceholderMessage.js +7 -17
- package/dist/internalTools/sendPlaceholderMessage.js.map +1 -1
- package/dist/interrupts/BaseInterruptSessionManager.d.ts +2 -2
- package/dist/interrupts/BaseInterruptSessionManager.d.ts.map +1 -1
- package/dist/interrupts/BaseInterruptSessionManager.js +1 -7
- package/dist/interrupts/BaseInterruptSessionManager.js.map +1 -1
- package/dist/nodes/addAppToolNode.d.ts.map +1 -1
- package/dist/nodes/addAppToolNode.js +24 -18
- package/dist/nodes/addAppToolNode.js.map +1 -1
- package/dist/nodes/addBrowserTaskNode.d.ts.map +1 -1
- package/dist/nodes/addBrowserTaskNode.js +30 -27
- package/dist/nodes/addBrowserTaskNode.js.map +1 -1
- package/dist/nodes/addBrowserTaskRunNode.d.ts.map +1 -1
- package/dist/nodes/addBrowserTaskRunNode.js +18 -20
- package/dist/nodes/addBrowserTaskRunNode.js.map +1 -1
- package/dist/nodes/addJumpToNode.d.ts.map +1 -1
- package/dist/nodes/addJumpToNode.js +8 -9
- package/dist/nodes/addJumpToNode.js.map +1 -1
- package/dist/nodes/addJunctionNode.d.ts.map +1 -1
- package/dist/nodes/addJunctionNode.js +8 -9
- package/dist/nodes/addJunctionNode.js.map +1 -1
- package/dist/nodes/addPromptNode.d.ts.map +1 -1
- package/dist/nodes/addPromptNode.js +107 -270
- package/dist/nodes/addPromptNode.js.map +1 -1
- package/dist/nodes/addToolNode.d.ts.map +1 -1
- package/dist/nodes/addToolNode.js +33 -20
- package/dist/nodes/addToolNode.js.map +1 -1
- package/dist/nodes/addToolRunNode.d.ts.map +1 -1
- package/dist/nodes/addToolRunNode.js +15 -21
- package/dist/nodes/addToolRunNode.js.map +1 -1
- package/dist/nodes/addTriggerNode.js +9 -11
- package/dist/nodes/addTriggerNode.js.map +1 -1
- package/dist/nodes/compilePrompt.d.ts.map +1 -1
- package/dist/nodes/compilePrompt.js +2 -0
- package/dist/nodes/compilePrompt.js.map +1 -1
- package/dist/pii-gateway/gateway.d.ts +17 -0
- package/dist/pii-gateway/gateway.d.ts.map +1 -0
- package/dist/pii-gateway/gateway.js +77 -0
- package/dist/pii-gateway/gateway.js.map +1 -0
- package/dist/pii-gateway/index.d.ts +4 -0
- package/dist/pii-gateway/index.d.ts.map +1 -0
- package/dist/pii-gateway/index.js +14 -0
- package/dist/pii-gateway/index.js.map +1 -0
- package/dist/pii-gateway/proxy.d.ts +80 -0
- package/dist/pii-gateway/proxy.d.ts.map +1 -0
- package/dist/pii-gateway/proxy.js +106 -0
- package/dist/pii-gateway/proxy.js.map +1 -0
- package/dist/pii-gateway/types.d.ts +27 -0
- package/dist/pii-gateway/types.d.ts.map +1 -0
- package/dist/pii-gateway/types.js +3 -0
- package/dist/pii-gateway/types.js.map +1 -0
- package/dist/platform/mindedChatOpenAI.d.ts +5 -0
- package/dist/platform/mindedChatOpenAI.d.ts.map +1 -0
- package/dist/platform/mindedChatOpenAI.js +23 -0
- package/dist/platform/mindedChatOpenAI.js.map +1 -0
- package/dist/platform/mindedCheckpointSaver.js.map +1 -1
- package/dist/platform/mindedUtils.d.ts +1 -0
- package/dist/platform/mindedUtils.d.ts.map +1 -0
- package/dist/platform/mindedUtils.js +2 -0
- package/dist/platform/mindedUtils.js.map +1 -0
- package/dist/platform/parallelAzureChatOpenAI.d.ts +39 -0
- package/dist/platform/parallelAzureChatOpenAI.d.ts.map +1 -0
- package/dist/platform/parallelAzureChatOpenAI.js +114 -0
- package/dist/platform/parallelAzureChatOpenAI.js.map +1 -0
- package/dist/platform/parallelChatOpenAI.d.ts +38 -0
- package/dist/platform/parallelChatOpenAI.d.ts.map +1 -0
- package/dist/platform/parallelChatOpenAI.js +111 -0
- package/dist/platform/parallelChatOpenAI.js.map +1 -0
- package/dist/platform/parallelMindedChatOpenAI.d.ts +39 -0
- package/dist/platform/parallelMindedChatOpenAI.d.ts.map +1 -0
- package/dist/platform/parallelMindedChatOpenAI.js +111 -0
- package/dist/platform/parallelMindedChatOpenAI.js.map +1 -0
- package/dist/platform/piiGateway/proxy.d.ts +20 -0
- package/dist/platform/piiGateway/proxy.d.ts.map +1 -0
- package/dist/platform/piiGateway/proxy.js +24 -0
- package/dist/platform/piiGateway/proxy.js.map +1 -0
- package/dist/platform/toolExecutor.d.ts.map +1 -1
- package/dist/platform/toolExecutor.js +4 -20
- package/dist/platform/toolExecutor.js.map +1 -1
- package/dist/playbooks/playbooks.d.ts +1 -1
- package/dist/playbooks/playbooks.d.ts.map +1 -1
- package/dist/playbooks/playbooks.js +1 -1
- package/dist/playbooks/playbooks.js.map +1 -1
- package/dist/toolsLibrary/classifier.d.ts.map +1 -1
- package/dist/toolsLibrary/classifier.js +5 -9
- package/dist/toolsLibrary/classifier.js.map +1 -1
- package/dist/toolsLibrary/extraction.d.ts.map +1 -1
- package/dist/toolsLibrary/extraction.js +5 -9
- package/dist/toolsLibrary/extraction.js.map +1 -1
- package/dist/toolsLibrary/parseDocument.d.ts.map +1 -1
- package/dist/toolsLibrary/parseDocument.js +6 -14
- package/dist/toolsLibrary/parseDocument.js.map +1 -1
- package/dist/types/Agent.d.ts +2 -0
- package/dist/types/Agent.d.ts.map +1 -0
- package/dist/types/Agent.js +18 -0
- package/dist/types/Agent.js.map +1 -0
- package/dist/types/Agent.types.d.ts +1 -1
- package/dist/types/Agent.types.d.ts.map +1 -1
- package/dist/types/Flows.d.ts +2 -0
- package/dist/types/Flows.d.ts.map +1 -0
- package/dist/types/Flows.js +18 -0
- package/dist/types/Flows.js.map +1 -0
- package/dist/types/LLM.d.ts +2 -0
- package/dist/types/LLM.d.ts.map +1 -0
- package/dist/types/LLM.js +18 -0
- package/dist/types/LLM.js.map +1 -0
- package/dist/types/LangGraph.d.ts +2 -0
- package/dist/types/LangGraph.d.ts.map +1 -0
- package/dist/types/LangGraph.js +18 -0
- package/dist/types/LangGraph.js.map +1 -0
- package/dist/types/LangGraph.types.d.ts.map +1 -1
- package/dist/types/LangGraph.types.js +30 -42
- package/dist/types/LangGraph.types.js.map +1 -1
- package/dist/types/Platform.d.ts +2 -0
- package/dist/types/Platform.d.ts.map +1 -0
- package/dist/types/Platform.js +18 -0
- package/dist/types/Platform.js.map +1 -0
- package/dist/types/Tools.d.ts +2 -0
- package/dist/types/Tools.d.ts.map +1 -0
- package/dist/types/Tools.js +18 -0
- package/dist/types/Tools.js.map +1 -0
- package/dist/types/Tools.types.d.ts +2 -7
- package/dist/types/Tools.types.d.ts.map +1 -1
- package/dist/types/Triggers.d.ts +2 -0
- package/dist/types/Triggers.d.ts.map +1 -0
- package/dist/types/Triggers.js +18 -0
- package/dist/types/Triggers.js.map +1 -0
- package/dist/types/Triggers.types.d.ts +25 -0
- package/dist/types/Triggers.types.d.ts.map +1 -0
- package/dist/types/Triggers.types.js +3 -0
- package/dist/types/Triggers.types.js.map +1 -0
- package/dist/types/Voice.d.ts +2 -0
- package/dist/types/Voice.d.ts.map +1 -0
- package/dist/types/Voice.js +18 -0
- package/dist/types/Voice.js.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +25 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/LogicalConditionDebugger.d.ts +57 -0
- package/dist/utils/LogicalConditionDebugger.d.ts.map +1 -0
- package/dist/utils/LogicalConditionDebugger.js +143 -0
- package/dist/utils/LogicalConditionDebugger.js.map +1 -0
- package/dist/utils/documentProcessor.d.ts +87 -0
- package/dist/utils/documentProcessor.d.ts.map +1 -0
- package/dist/utils/documentProcessor.js +726 -0
- package/dist/utils/documentProcessor.js.map +1 -0
- package/dist/utils/extractStateMemoryResponse.d.ts +0 -4
- package/dist/utils/extractStateMemoryResponse.d.ts.map +1 -1
- package/dist/utils/extractStateMemoryResponse.js +87 -89
- package/dist/utils/extractStateMemoryResponse.js.map +1 -1
- package/dist/utils/extractToolGuidelineParams.d.ts +4 -0
- package/dist/utils/extractToolGuidelineParams.d.ts.map +1 -0
- package/dist/utils/extractToolGuidelineParams.js +20 -0
- package/dist/utils/extractToolGuidelineParams.js.map +1 -0
- package/dist/utils/extractToolMemoryResponse.d.ts +4 -0
- package/dist/utils/extractToolMemoryResponse.d.ts.map +1 -0
- package/dist/utils/extractToolMemoryResponse.js +16 -0
- package/dist/utils/extractToolMemoryResponse.js.map +1 -0
- package/dist/utils/parseAttachments.d.ts +14 -0
- package/dist/utils/parseAttachments.d.ts.map +1 -0
- package/dist/utils/parseAttachments.js +54 -0
- package/dist/utils/parseAttachments.js.map +1 -0
- package/docs/sdk/agent-api.md +1 -1
- package/package.json +1 -1
- package/src/agent.ts +37 -63
- package/src/edges/createLogicalRouter.ts +4 -19
- package/src/events/AgentEvents.ts +5 -14
- package/src/internalTools/libraryActionRunnerTool.ts +0 -1
- package/src/interrupts/BaseInterruptSessionManager.ts +3 -9
- package/src/nodes/addAppToolNode.ts +19 -11
- package/src/nodes/addBrowserTaskNode.ts +22 -16
- package/src/nodes/addBrowserTaskRunNode.ts +10 -9
- package/src/nodes/addJumpToNode.ts +5 -4
- package/src/nodes/addJunctionNode.ts +5 -4
- package/src/nodes/addPromptNode.ts +107 -299
- package/src/nodes/addToolNode.ts +34 -20
- package/src/nodes/addToolRunNode.ts +15 -15
- package/src/nodes/addTriggerNode.ts +5 -5
- package/src/nodes/compilePrompt.ts +3 -0
- package/src/platform/mindedCheckpointSaver.ts +13 -13
- package/src/platform/toolExecutor.ts +4 -20
- package/src/playbooks/playbooks.ts +2 -2
- package/src/toolsLibrary/classifier.ts +6 -9
- package/src/toolsLibrary/extraction.ts +6 -9
- package/src/toolsLibrary/parseDocument.ts +7 -14
- package/src/types/Agent.types.ts +1 -1
- package/src/types/LangGraph.types.ts +30 -41
- package/src/types/Tools.types.ts +2 -7
- package/dist/browserTask/README.md +0 -419
- package/dist/browserTask/browserAgent.py +0 -632
- package/dist/browserTask/captcha_isolated.png +0 -0
- package/dist/browserTask/executeBrowserTask.ts +0 -79
- package/dist/browserTask/requirements.txt +0 -8
- package/dist/browserTask/setup.sh +0 -144
- package/src/utils/extractStateMemoryResponse.ts +0 -94
|
@@ -9,7 +9,6 @@ import { tool as langchainTool } from '@langchain/core/tools';
|
|
|
9
9
|
import { AgentEventRequestPayloads, AgentEvents } from '../events/AgentEvents';
|
|
10
10
|
import { EmitSignature, HistoryStep } from '../types/Agent.types';
|
|
11
11
|
import { createLlmInstance } from '../llm/createLlmInstance';
|
|
12
|
-
import extractToolStateResponse from '../utils/extractStateMemoryResponse';
|
|
13
12
|
import { Agent } from '../agent';
|
|
14
13
|
import { logger } from '../utils/logger';
|
|
15
14
|
import { combinePlaybooks } from '../playbooks/playbooks';
|
|
@@ -41,343 +40,152 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
41
40
|
}),
|
|
42
41
|
);
|
|
43
42
|
|
|
44
|
-
const combinedPlaybooks = combinePlaybooks(agent.playbooks) || '';
|
|
45
|
-
|
|
46
43
|
// Get edges for the current node and format them
|
|
47
44
|
const edges = agent.flows?.flatMap((flow: any) => flow.edges) || [];
|
|
48
45
|
const nodeEdges = edges.filter((edge: any) => edge.source === node.name);
|
|
49
46
|
const currentPromptNode = getCurrentPromptNode(node, nodeEdges);
|
|
50
|
-
const systemMessage = combinedPlaybooks + '\n\n' + currentPromptNode;
|
|
51
|
-
const compiledPrompt = compilePrompt(systemMessage, { memory: state.memory, system: { currentTime: new Date().toISOString() } });
|
|
52
|
-
|
|
53
|
-
// Check if we should use the agentic loop (only when humanInTheLoop is true)
|
|
54
|
-
const useAgenticLoop = node.humanInTheLoop === true;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* When humanInTheLoop is true: Run an agentic loop that continues invoking the LLM
|
|
58
|
-
* after tool calls until we get a final AI message without tool calls.
|
|
59
|
-
*
|
|
60
|
-
* When humanInTheLoop is false: Return immediately after tool execution without
|
|
61
|
-
* additional LLM invocations.
|
|
62
|
-
*/
|
|
63
|
-
let allMessages: any[] = [];
|
|
64
|
-
let allHistory: any[] = [];
|
|
65
|
-
let accumulatedStateUpdates = {};
|
|
66
|
-
let currentMessages = [...state.messages];
|
|
67
|
-
let finalAIMessage: AIMessage | null = null;
|
|
68
|
-
let loopCount = 0;
|
|
69
|
-
const MAX_LOOP_ITERATIONS = 10;
|
|
70
47
|
|
|
71
|
-
|
|
72
|
-
|
|
48
|
+
let finalMessage = currentPromptNode;
|
|
49
|
+
const combinedPlaybooks = combinePlaybooks(agent.playbooks);
|
|
50
|
+
if (combinedPlaybooks) {
|
|
51
|
+
finalMessage = combinedPlaybooks + '\n\n' + currentPromptNode;
|
|
52
|
+
}
|
|
53
|
+
const compiledPrompt = compilePrompt(finalMessage, { memory: state.memory });
|
|
54
|
+
const systemMessage = new SystemMessage(compiledPrompt);
|
|
55
|
+
if (state.messages.length === 0 || state.messages[0].getType() === 'system') {
|
|
56
|
+
state.messages[0] = systemMessage;
|
|
57
|
+
} else {
|
|
58
|
+
state.messages.unshift(systemMessage);
|
|
59
|
+
}
|
|
73
60
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
61
|
+
const startTime = Date.now();
|
|
62
|
+
const result: AIMessage = await llmToUse.bindTools(scopedTools).invoke(state.messages);
|
|
63
|
+
const endTime = Date.now();
|
|
64
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
65
|
+
logger.debug({
|
|
66
|
+
msg: '[Model] Model execution time',
|
|
67
|
+
executionTimeMs: endTime - startTime,
|
|
68
|
+
sessionId: state.sessionId,
|
|
69
|
+
node: node.displayName,
|
|
70
|
+
});
|
|
71
|
+
// Check if the result contains tool calls
|
|
72
|
+
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
73
|
+
// Execute the tools
|
|
74
|
+
const toolResults = [];
|
|
75
|
+
|
|
76
|
+
for (const toolCall of result.tool_calls) {
|
|
77
|
+
const matchedTool = scopedTools.find((t) => t.name === toolCall.name);
|
|
78
|
+
logger.info({
|
|
79
|
+
msg: `[Model] Calling tool inside prompt node`,
|
|
80
|
+
tool: matchedTool?.name,
|
|
79
81
|
sessionId: state.sessionId,
|
|
80
82
|
node: node.displayName,
|
|
81
83
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// Check if the result contains tool calls
|
|
110
|
-
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
111
|
-
// Add the tool call message
|
|
112
|
-
allMessages.push(result);
|
|
113
|
-
allHistory.push(
|
|
114
|
-
createHistoryStep<HistoryStep>(state.history, {
|
|
115
|
-
type: NodeType.TOOL,
|
|
116
|
-
nodeId: node.name,
|
|
117
|
-
nodeDisplayName: node.displayName,
|
|
118
|
-
raw: result,
|
|
119
|
-
messageIds: [result.id!],
|
|
120
|
-
}),
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
// Execute the tools
|
|
124
|
-
// Track how many tool responses we've added for validation
|
|
125
|
-
const toolResponseStartIndex = allMessages.length;
|
|
126
|
-
|
|
127
|
-
// Ensure we process ALL tool calls, even if some fail
|
|
128
|
-
for (const toolCall of result.tool_calls) {
|
|
129
|
-
const matchedTool = scopedTools.find((t) => t.name === toolCall.name);
|
|
130
|
-
logger.info({
|
|
131
|
-
msg: `[Model] Calling tool inside prompt node`,
|
|
132
|
-
tool: matchedTool?.name || toolCall.name,
|
|
133
|
-
toolCallId: toolCall.id,
|
|
134
|
-
sessionId: state.sessionId,
|
|
135
|
-
node: node.displayName,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
if (matchedTool) {
|
|
139
|
-
try {
|
|
140
|
-
// Invoke the LangChain tool directly
|
|
141
|
-
const toolStartTime = Date.now();
|
|
142
|
-
const toolResult = await matchedTool.invoke(toolCall);
|
|
143
|
-
const toolEndTime = Date.now();
|
|
144
|
-
|
|
145
|
-
logger.debug({
|
|
146
|
-
msg: `[Tool] Tool result inside prompt node`,
|
|
147
|
-
tool: matchedTool?.name,
|
|
148
|
-
result: toolResult,
|
|
149
|
-
executionTimeMs: toolEndTime - toolStartTime,
|
|
150
|
-
sessionId: state.sessionId,
|
|
151
|
-
node: node.displayName,
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// Add the tool result directly to allMessages
|
|
155
|
-
allMessages.push(toolResult);
|
|
156
|
-
|
|
157
|
-
// Check for queue after tool call
|
|
158
|
-
const systemMessageId = uuidv4();
|
|
159
|
-
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId, {
|
|
160
|
-
messages: [
|
|
161
|
-
...allMessages,
|
|
162
|
-
new SystemMessage({
|
|
163
|
-
id: systemMessageId,
|
|
164
|
-
content:
|
|
165
|
-
'you called tool when the user send a new message, Consider calling the function again after user message is processed',
|
|
166
|
-
}),
|
|
167
|
-
],
|
|
168
|
-
history: [
|
|
169
|
-
...allHistory,
|
|
170
|
-
createHistoryStep<HistoryStep>(state.history, {
|
|
171
|
-
type: NodeType.TOOL,
|
|
172
|
-
nodeId: node.name,
|
|
173
|
-
nodeDisplayName: node.displayName,
|
|
174
|
-
raw: toolResult,
|
|
175
|
-
messageIds: [toolResult.id!, systemMessageId],
|
|
176
|
-
}),
|
|
177
|
-
],
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
const toolStateUpdate = extractToolStateResponse(toolResult);
|
|
181
|
-
// Properly merge memory and other state updates
|
|
182
|
-
accumulatedStateUpdates = {
|
|
183
|
-
...accumulatedStateUpdates,
|
|
184
|
-
...toolStateUpdate,
|
|
185
|
-
memory: { ...(accumulatedStateUpdates as any).memory, ...(toolStateUpdate as any).memory },
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
allHistory.push(
|
|
189
|
-
createHistoryStep<HistoryStep>(state.history, {
|
|
190
|
-
type: NodeType.TOOL,
|
|
191
|
-
nodeId: node.name,
|
|
192
|
-
nodeDisplayName: node.displayName,
|
|
193
|
-
raw: toolResult,
|
|
194
|
-
messageIds: [toolResult.id!],
|
|
84
|
+
if (matchedTool) {
|
|
85
|
+
try {
|
|
86
|
+
// Invoke the LangChain tool directly
|
|
87
|
+
const startTime = Date.now();
|
|
88
|
+
const toolResult = await matchedTool.invoke(toolCall);
|
|
89
|
+
const endTime = Date.now();
|
|
90
|
+
logger.debug({
|
|
91
|
+
msg: `[Tool] Tool result inside prompt node`,
|
|
92
|
+
tool: matchedTool?.name,
|
|
93
|
+
result: toolResult,
|
|
94
|
+
executionTimeMs: endTime - startTime,
|
|
95
|
+
sessionId: state.sessionId,
|
|
96
|
+
node: node.displayName,
|
|
97
|
+
});
|
|
98
|
+
//check for queue after tool call
|
|
99
|
+
const systemMessageId = uuidv4();
|
|
100
|
+
|
|
101
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId, {
|
|
102
|
+
messages: [
|
|
103
|
+
result,
|
|
104
|
+
toolResult,
|
|
105
|
+
new SystemMessage({
|
|
106
|
+
id: systemMessageId,
|
|
107
|
+
content:
|
|
108
|
+
'you called tool when the user send a new message, Consider calling the function again after user message is processed',
|
|
195
109
|
}),
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (err?.name === 'GraphInterrupt') throw err;
|
|
199
|
-
logger.error({
|
|
200
|
-
msg: `[Tool] Error executing tool inside prompt node`,
|
|
201
|
-
tool: toolCall.name,
|
|
202
|
-
err,
|
|
203
|
-
sessionId: state.sessionId,
|
|
204
|
-
node: node.displayName,
|
|
205
|
-
});
|
|
206
|
-
const errorMessage = new ToolMessage({
|
|
207
|
-
content: JSON.stringify({ error: err instanceof Error ? err.message : String(err) }),
|
|
208
|
-
tool_call_id: toolCall.id!,
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
// Add the error message directly to allMessages
|
|
212
|
-
allMessages.push(errorMessage);
|
|
213
|
-
|
|
214
|
-
// Check for queue after tool error
|
|
215
|
-
const errorSystemMessageId = uuidv4();
|
|
216
|
-
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId, {
|
|
217
|
-
messages: [
|
|
218
|
-
...allMessages,
|
|
219
|
-
new SystemMessage({
|
|
220
|
-
id: errorSystemMessageId,
|
|
221
|
-
content: 'Tool execution failed. Consider handling the error or calling another function if needed.',
|
|
222
|
-
}),
|
|
223
|
-
],
|
|
224
|
-
history: [
|
|
225
|
-
...allHistory,
|
|
226
|
-
createHistoryStep<HistoryStep>(state.history, {
|
|
227
|
-
type: NodeType.TOOL,
|
|
228
|
-
nodeId: node.name,
|
|
229
|
-
nodeDisplayName: node.displayName,
|
|
230
|
-
raw: errorMessage,
|
|
231
|
-
messageIds: [errorMessage.id!, errorSystemMessageId],
|
|
232
|
-
}),
|
|
233
|
-
],
|
|
234
|
-
});
|
|
235
|
-
allHistory.push(
|
|
110
|
+
],
|
|
111
|
+
history: [
|
|
236
112
|
createHistoryStep<HistoryStep>(state.history, {
|
|
237
113
|
type: NodeType.TOOL,
|
|
238
114
|
nodeId: node.name,
|
|
239
115
|
nodeDisplayName: node.displayName,
|
|
240
|
-
raw:
|
|
241
|
-
messageIds: [
|
|
116
|
+
raw: toolResult,
|
|
117
|
+
messageIds: [toolResult.id!, systemMessageId],
|
|
242
118
|
}),
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
119
|
+
],
|
|
120
|
+
});
|
|
121
|
+
toolResults.push(toolResult);
|
|
122
|
+
} catch (err: any) {
|
|
123
|
+
if (err?.name === 'GraphInterrupt') throw err;
|
|
246
124
|
logger.error({
|
|
247
|
-
msg: `[Tool]
|
|
125
|
+
msg: `[Tool] Error executing tool inside prompt node`,
|
|
248
126
|
tool: toolCall.name,
|
|
249
|
-
|
|
127
|
+
err,
|
|
250
128
|
sessionId: state.sessionId,
|
|
251
129
|
node: node.displayName,
|
|
252
130
|
});
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
content: JSON.stringify({ error: `Tool '${toolCall.name}' not found` }),
|
|
257
|
-
tool_call_id: toolCall.id!, // This MUST match the tool_call_id from the AI message
|
|
258
|
-
name: toolCall.name,
|
|
131
|
+
const errorMessage = new ToolMessage({
|
|
132
|
+
content: JSON.stringify({ error: err instanceof Error ? err.message : String(err) }),
|
|
133
|
+
tool_call_id: toolCall.id!,
|
|
259
134
|
});
|
|
260
|
-
|
|
261
|
-
allMessages.push(missingToolMessage);
|
|
262
|
-
allHistory.push(
|
|
263
|
-
createHistoryStep<HistoryStep>(state.history, {
|
|
264
|
-
type: NodeType.TOOL,
|
|
265
|
-
nodeId: node.name,
|
|
266
|
-
nodeDisplayName: node.displayName,
|
|
267
|
-
raw: missingToolMessage,
|
|
268
|
-
messageIds: [missingToolMessage.id!],
|
|
269
|
-
}),
|
|
270
|
-
);
|
|
135
|
+
toolResults.push(errorMessage);
|
|
271
136
|
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// Validate that we have responses for ALL tool calls
|
|
275
|
-
const toolResponseCount = allMessages.length - toolResponseStartIndex;
|
|
276
|
-
if (toolResponseCount !== result.tool_calls.length) {
|
|
137
|
+
} else {
|
|
277
138
|
logger.error({
|
|
278
|
-
msg:
|
|
279
|
-
|
|
280
|
-
actualCount: toolResponseCount,
|
|
281
|
-
toolCallIds: result.tool_calls.map((tc) => tc.id),
|
|
139
|
+
msg: `[Tool] Model called tool but it was not found inside prompt node`,
|
|
140
|
+
tool: toolCall.name,
|
|
282
141
|
sessionId: state.sessionId,
|
|
283
142
|
node: node.displayName,
|
|
284
143
|
});
|
|
285
144
|
}
|
|
286
|
-
|
|
287
|
-
// If humanInTheLoop is false, return immediately after tool execution
|
|
288
|
-
if (!useAgenticLoop) {
|
|
289
|
-
return {
|
|
290
|
-
...accumulatedStateUpdates,
|
|
291
|
-
messages: allMessages,
|
|
292
|
-
history: allHistory,
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Otherwise, prepare for next iteration
|
|
297
|
-
// allMessages now contains all the new messages from this iteration
|
|
298
|
-
currentMessages = [...state.messages, ...allMessages];
|
|
299
|
-
|
|
300
|
-
// Continue the loop to see what the model wants to do next
|
|
301
|
-
continue;
|
|
302
|
-
} else {
|
|
303
|
-
// We got an AI message without tool calls - this is our final response
|
|
304
|
-
finalAIMessage = result;
|
|
305
|
-
break;
|
|
306
145
|
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Process the final AI message if we have one
|
|
310
|
-
if (finalAIMessage) {
|
|
311
|
-
// Update state so AI_MESSAGE will get the latest state
|
|
312
|
-
const stateForAIMessage = structuredClone(state);
|
|
313
|
-
stateForAIMessage.goto = null;
|
|
314
|
-
stateForAIMessage.messages.push(...allMessages, finalAIMessage);
|
|
315
146
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (finalAIMessage.getType() === 'ai') {
|
|
320
|
-
logger.info({
|
|
321
|
-
msg: `[Model] Response`,
|
|
322
|
-
content: finalAIMessage.content,
|
|
323
|
-
sessionId: state.sessionId,
|
|
324
|
-
node: node.displayName,
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
const results = await emit(AgentEvents.AI_MESSAGE, {
|
|
328
|
-
message: finalAIMessage.content as string,
|
|
329
|
-
state: stateForAIMessage,
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
const handlerResult = results.find((r) => r !== undefined);
|
|
333
|
-
if (handlerResult && handlerResult.state) {
|
|
334
|
-
aiMessageStateUpdate = { ...aiMessageStateUpdate, ...handlerResult.state };
|
|
335
|
-
|
|
336
|
-
// If the handler returned modified messages, use those instead
|
|
337
|
-
if (handlerResult.state.messages) {
|
|
338
|
-
messagesToReturn = [...allMessages, ...handlerResult.state.messages];
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
allHistory.push(
|
|
147
|
+
state.messages.push(result, ...toolResults);
|
|
148
|
+
state.history.push(
|
|
344
149
|
createHistoryStep<HistoryStep>(state.history, {
|
|
345
|
-
type: NodeType.
|
|
150
|
+
type: NodeType.TOOL,
|
|
346
151
|
nodeId: node.name,
|
|
347
152
|
nodeDisplayName: node.displayName,
|
|
348
|
-
raw:
|
|
349
|
-
messageIds: [
|
|
153
|
+
raw: result,
|
|
154
|
+
messageIds: [result.id!],
|
|
350
155
|
}),
|
|
156
|
+
...toolResults.map((toolResult) =>
|
|
157
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
158
|
+
type: NodeType.TOOL,
|
|
159
|
+
nodeId: node.name,
|
|
160
|
+
nodeDisplayName: node.displayName,
|
|
161
|
+
raw: toolResult,
|
|
162
|
+
messageIds: [toolResult.id!],
|
|
163
|
+
}),
|
|
164
|
+
),
|
|
351
165
|
);
|
|
352
|
-
|
|
353
|
-
// Return all accumulated state updates and messages
|
|
354
|
-
return {
|
|
355
|
-
...accumulatedStateUpdates,
|
|
356
|
-
...aiMessageStateUpdate,
|
|
357
|
-
messages: messagesToReturn,
|
|
358
|
-
history: allHistory,
|
|
359
|
-
};
|
|
166
|
+
return state;
|
|
360
167
|
}
|
|
361
168
|
|
|
362
|
-
//
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
msg: '[Model] Returning accumulated messages after hitting loop limit',
|
|
366
|
-
messageCount: allMessages.length,
|
|
367
|
-
sessionId: state.sessionId,
|
|
368
|
-
node: node.displayName,
|
|
369
|
-
});
|
|
169
|
+
// Model text response
|
|
170
|
+
state.goto = null;
|
|
171
|
+
state.messages.push(result);
|
|
370
172
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
messages: allMessages,
|
|
375
|
-
history: allHistory,
|
|
376
|
-
};
|
|
173
|
+
if (result.getType() === 'ai') {
|
|
174
|
+
logger.info({ msg: `[Model] Response`, content: result.content, sessionId: state.sessionId, node: node.displayName });
|
|
175
|
+
await emit(AgentEvents.AI_MESSAGE, { message: result.content as string, state });
|
|
377
176
|
}
|
|
378
177
|
|
|
379
|
-
|
|
380
|
-
|
|
178
|
+
state.history.push(
|
|
179
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
180
|
+
type: NodeType.PROMPT_NODE,
|
|
181
|
+
nodeId: node.name,
|
|
182
|
+
nodeDisplayName: node.displayName,
|
|
183
|
+
raw: result.content,
|
|
184
|
+
messageIds: [result.id!],
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
return state;
|
|
381
189
|
};
|
|
382
190
|
graph.addNode(node.name, callback);
|
|
383
191
|
};
|
package/src/nodes/addToolNode.ts
CHANGED
|
@@ -40,31 +40,45 @@ export const addToolNode = async ({
|
|
|
40
40
|
schema: matchedTool.input,
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
const combinedPlaybooks = combinePlaybooks(agent.playbooks)
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
const combinedPlaybooks = combinePlaybooks(agent.playbooks);
|
|
44
|
+
const toolPrompt = node.prompt;
|
|
45
|
+
|
|
46
|
+
let finalMessage = toolPrompt;
|
|
47
|
+
if (combinedPlaybooks) {
|
|
48
|
+
if (toolPrompt) {
|
|
49
|
+
finalMessage = combinedPlaybooks + '\n\n' + toolPrompt;
|
|
50
|
+
} else {
|
|
51
|
+
finalMessage = combinedPlaybooks;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (finalMessage) {
|
|
55
|
+
const compiledPrompt = compilePrompt(finalMessage, { memory: state.memory });
|
|
56
|
+
const systemMessage = new SystemMessage(compiledPrompt);
|
|
57
|
+
if (state.messages.length === 0 || state.messages[0].getType() === 'system') {
|
|
58
|
+
state.messages[0] = systemMessage;
|
|
59
|
+
} else {
|
|
60
|
+
state.messages.unshift(systemMessage);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
47
63
|
|
|
48
64
|
const startTime = Date.now();
|
|
49
|
-
const AIToolCallMessage: AIMessage = await llm
|
|
50
|
-
.bindTools([tool], {
|
|
51
|
-
tool_choice: tool.name,
|
|
52
|
-
})
|
|
53
|
-
.invoke([...state.messages, new SystemMessage(systemPrompt)]);
|
|
65
|
+
const AIToolCallMessage: AIMessage = await llm.bindTools([tool], { tool_choice: tool.name }).invoke(state.messages);
|
|
54
66
|
const endTime = Date.now();
|
|
55
67
|
logger.debug({ msg: '[Tool] Model execution time', tool: matchedTool.name, executionTimeMs: endTime - startTime });
|
|
56
68
|
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
state.goto = null;
|
|
70
|
+
state.messages.push(AIToolCallMessage);
|
|
71
|
+
|
|
72
|
+
const historyMsg = createHistoryStep<HistoryStep>(state.history, {
|
|
73
|
+
type: NodeType.TOOL,
|
|
74
|
+
nodeId: node.name,
|
|
75
|
+
nodeDisplayName: node.displayName,
|
|
76
|
+
raw: AIToolCallMessage.tool_calls?.[0] || '',
|
|
77
|
+
messageIds: [AIToolCallMessage.id!],
|
|
78
|
+
});
|
|
79
|
+
state.history.push(historyMsg);
|
|
80
|
+
|
|
81
|
+
return state;
|
|
68
82
|
};
|
|
69
83
|
graph.addNode(node.name, callback);
|
|
70
84
|
};
|
|
@@ -4,13 +4,12 @@ import { Tool } from '../types/Tools.types';
|
|
|
4
4
|
import { LLMProviders } from '../types/LLM.types';
|
|
5
5
|
import { internalNodesSuffix, NodeType, ToolNode } from '../types/Flows.types';
|
|
6
6
|
import { tool as langchainTool } from '@langchain/core/tools';
|
|
7
|
-
import { ToolMessage } from '@langchain/core/messages';
|
|
8
7
|
import { z } from 'zod';
|
|
9
|
-
import extractToolStateResponse from '../utils/extractStateMemoryResponse';
|
|
10
8
|
import { Agent } from '../agent';
|
|
11
9
|
import { logger } from '../utils/logger';
|
|
12
|
-
import { createHistoryStep } from '../utils/history';
|
|
13
10
|
import { HistoryStep } from '../types/Agent.types';
|
|
11
|
+
import { createHistoryStep } from '../utils/history';
|
|
12
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
14
13
|
|
|
15
14
|
type AddToolRunNodeParams = {
|
|
16
15
|
graph: PreCompiledGraph;
|
|
@@ -37,7 +36,7 @@ export const addToolRunNode = async ({ graph, tools, toolNode, attachedToNodeNam
|
|
|
37
36
|
const response = await matchedTool.execute({ input, state, agent });
|
|
38
37
|
const endTime = Date.now();
|
|
39
38
|
logger.debug({ msg: '[Tool] Tool execution time', tool: matchedTool.name, executionTimeMs: endTime - startTime });
|
|
40
|
-
return response
|
|
39
|
+
return response as ToolMessage;
|
|
41
40
|
} catch (err) {
|
|
42
41
|
logger.error({ message: '[Tool] Error executing tool', err, node: toolNode.displayName });
|
|
43
42
|
throw err;
|
|
@@ -54,25 +53,26 @@ export const addToolRunNode = async ({ graph, tools, toolNode, attachedToNodeNam
|
|
|
54
53
|
throw new Error('Tool call not found');
|
|
55
54
|
}
|
|
56
55
|
const toolCallMessage = await tool.invoke(toolCallObj.tool_calls[0]);
|
|
57
|
-
const toolStateUpdate = extractToolStateResponse(toolCallMessage as ToolMessage);
|
|
58
56
|
|
|
59
|
-
//
|
|
60
|
-
|
|
57
|
+
// Add the tool message to the state
|
|
58
|
+
state.messages.push(toolCallMessage);
|
|
61
59
|
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
goto: null,
|
|
66
|
-
...toolStateUpdate,
|
|
67
|
-
messages: updatedMessages,
|
|
68
|
-
history: createHistoryStep<HistoryStep>(state.history, {
|
|
60
|
+
// Add history step
|
|
61
|
+
state.history.push(
|
|
62
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
69
63
|
type: NodeType.TOOL,
|
|
70
64
|
nodeId: toolNode.name,
|
|
71
65
|
nodeDisplayName: toolNode.displayName,
|
|
72
66
|
raw: toolCallMessage,
|
|
73
67
|
messageIds: [toolCallObj.tool_calls[0].id],
|
|
74
68
|
}),
|
|
75
|
-
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// Clear goto if it was set
|
|
72
|
+
state.goto = null;
|
|
73
|
+
|
|
74
|
+
// Return the entire modified state
|
|
75
|
+
return state;
|
|
76
76
|
};
|
|
77
77
|
|
|
78
78
|
graph.addNode(buildToolRunNodeName(attachedToNodeName), callback);
|
|
@@ -9,18 +9,18 @@ export const addTriggerNode = async ({ graph, node }: { graph: PreCompiledGraph;
|
|
|
9
9
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
10
10
|
logger.debug({ msg: `[Trigger] Executing trigger node`, node: node.displayName });
|
|
11
11
|
if (node.triggerType === TriggerType.MANUAL) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
state.goto = null;
|
|
13
|
+
state.history.push(
|
|
14
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
15
15
|
type: NodeType.TRIGGER,
|
|
16
16
|
nodeId: node.name,
|
|
17
17
|
nodeDisplayName: node.displayName,
|
|
18
18
|
raw: '',
|
|
19
19
|
messageIds: [],
|
|
20
20
|
}),
|
|
21
|
-
|
|
21
|
+
);
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return state;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
graph.addNode(node.name, callback);
|
|
@@ -6,6 +6,9 @@ import { logger } from '../utils/logger';
|
|
|
6
6
|
*/
|
|
7
7
|
export function compilePrompt(prompt: string, params: Record<string, any> = {}): string {
|
|
8
8
|
try {
|
|
9
|
+
// Define system parameters
|
|
10
|
+
params.system = { currentTime: new Date().toISOString() };
|
|
11
|
+
|
|
9
12
|
// First, render with EJS
|
|
10
13
|
let compiledPrompt = ejs.render(prompt, params);
|
|
11
14
|
|
|
@@ -49,21 +49,21 @@ export class MindedCheckpointSaver extends BaseCheckpointSaver<number> {
|
|
|
49
49
|
// Re-serialize and deserialize metadata to restore class information
|
|
50
50
|
const metadata = tuple.metadata
|
|
51
51
|
? (() => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
const [metadataType, serializedMetadata] = this.serde.dumpsTyped(tuple.metadata);
|
|
53
|
+
return this.serde.loadsTyped(metadataType, serializedMetadata) as CheckpointMetadata;
|
|
54
|
+
})()
|
|
55
55
|
: undefined;
|
|
56
56
|
|
|
57
57
|
// Re-serialize and deserialize pending writes to restore class information
|
|
58
58
|
const pendingWrites: CheckpointPendingWrite[] = tuple.pendingWrites
|
|
59
59
|
? await Promise.all(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
tuple.pendingWrites.map(async (write) => {
|
|
61
|
+
const [taskId, channel, value] = write;
|
|
62
|
+
const [valueType, serializedValue] = this.serde.dumpsTyped(value);
|
|
63
|
+
const deserializedValue = await this.serde.loadsTyped(valueType, serializedValue);
|
|
64
|
+
return [taskId, channel, deserializedValue] as CheckpointPendingWrite;
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
67
|
: [];
|
|
68
68
|
|
|
69
69
|
return {
|
|
@@ -93,9 +93,9 @@ export class MindedCheckpointSaver extends BaseCheckpointSaver<number> {
|
|
|
93
93
|
// Re-serialize and deserialize metadata to restore class information
|
|
94
94
|
const metadata = tuple.metadata
|
|
95
95
|
? (() => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
const [metadataType, serializedMetadata] = this.serde.dumpsTyped(tuple.metadata);
|
|
97
|
+
return this.serde.loadsTyped(metadataType, serializedMetadata) as CheckpointMetadata;
|
|
98
|
+
})()
|
|
99
99
|
: undefined;
|
|
100
100
|
|
|
101
101
|
yield {
|