@minded-ai/mindedjs 1.0.94 → 1.0.95-beta-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +4 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +72 -15
- package/dist/agent.js.map +1 -1
- package/dist/cli/index.js +12 -12
- package/dist/cli/index.js.map +1 -1
- package/dist/edges/createDirectEdge.js +1 -1
- package/dist/edges/createDirectEdge.js.map +1 -1
- package/dist/edges/createLogicalRouter.d.ts.map +1 -1
- package/dist/edges/createLogicalRouter.js +7 -7
- package/dist/edges/createLogicalRouter.js.map +1 -1
- package/dist/edges/createPromptRouter.d.ts.map +1 -1
- package/dist/edges/createPromptRouter.js +4 -3
- package/dist/edges/createPromptRouter.js.map +1 -1
- package/dist/edges/edgeFactory.js +2 -2
- package/dist/edges/edgeFactory.js.map +1 -1
- package/dist/interrupts/BaseInterruptSessionManager.d.ts +36 -0
- package/dist/interrupts/BaseInterruptSessionManager.d.ts.map +1 -0
- package/dist/interrupts/BaseInterruptSessionManager.js +39 -0
- package/dist/interrupts/BaseInterruptSessionManager.js.map +1 -0
- package/dist/interrupts/InterruptSessionManager.types.d.ts +36 -0
- package/dist/interrupts/InterruptSessionManager.types.d.ts.map +1 -0
- package/dist/interrupts/InterruptSessionManager.types.js +40 -0
- package/dist/interrupts/InterruptSessionManager.types.js.map +1 -0
- package/dist/interrupts/MemoryInterruptSessionManager.d.ts +14 -0
- package/dist/interrupts/MemoryInterruptSessionManager.d.ts.map +1 -0
- package/dist/interrupts/MemoryInterruptSessionManager.js +56 -0
- package/dist/interrupts/MemoryInterruptSessionManager.js.map +1 -0
- package/dist/interrupts/MindedInterruptSessionManager.d.ts +14 -0
- package/dist/interrupts/MindedInterruptSessionManager.d.ts.map +1 -0
- package/dist/interrupts/MindedInterruptSessionManager.js +168 -0
- package/dist/interrupts/MindedInterruptSessionManager.js.map +1 -0
- package/dist/interrupts/interruptSessionManagerFactory.d.ts +3 -0
- package/dist/interrupts/interruptSessionManagerFactory.d.ts.map +1 -0
- package/dist/interrupts/interruptSessionManagerFactory.js +56 -0
- package/dist/interrupts/interruptSessionManagerFactory.js.map +1 -0
- package/dist/nodes/addAppToolNode.js +1 -1
- package/dist/nodes/addAppToolNode.js.map +1 -1
- package/dist/nodes/addHumanInTheLoopNode.d.ts.map +1 -1
- package/dist/nodes/addHumanInTheLoopNode.js +3 -2
- package/dist/nodes/addHumanInTheLoopNode.js.map +1 -1
- package/dist/nodes/addJumpToNode.js +1 -1
- package/dist/nodes/addJumpToNode.js.map +1 -1
- package/dist/nodes/addPromptNode.d.ts.map +1 -1
- package/dist/nodes/addPromptNode.js +45 -1
- package/dist/nodes/addPromptNode.js.map +1 -1
- package/dist/nodes/addToolNode.d.ts.map +1 -1
- package/dist/nodes/addToolNode.js +4 -1
- package/dist/nodes/addToolNode.js.map +1 -1
- package/dist/nodes/addToolRunNode.d.ts.map +1 -1
- package/dist/nodes/addToolRunNode.js +1 -0
- package/dist/nodes/addToolRunNode.js.map +1 -1
- package/dist/nodes/addTriggerNode.js +1 -1
- package/dist/nodes/addTriggerNode.js.map +1 -1
- package/dist/platform/mindedConnection.d.ts.map +1 -1
- package/dist/platform/mindedConnection.js +12 -12
- package/dist/platform/mindedConnection.js.map +1 -1
- package/dist/platform/mindedConnectionTypes.d.ts +162 -1
- package/dist/platform/mindedConnectionTypes.d.ts.map +1 -1
- package/dist/platform/mindedConnectionTypes.js +10 -0
- package/dist/platform/mindedConnectionTypes.js.map +1 -1
- package/dist/playbooks/playbooks.js +4 -4
- package/dist/playbooks/playbooks.js.map +1 -1
- package/dist/types/Agent.types.d.ts +2 -0
- package/dist/types/Agent.types.d.ts.map +1 -1
- package/dist/types/Agent.types.js.map +1 -1
- package/dist/types/LangGraph.types.d.ts +2 -2
- package/dist/types/LangGraph.types.d.ts.map +1 -1
- package/dist/types/LangGraph.types.js +3 -1
- package/dist/types/LangGraph.types.js.map +1 -1
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +1 -0
- package/dist/utils/logger.js.map +1 -1
- package/dist/voice/voiceSession.d.ts.map +1 -1
- package/dist/voice/voiceSession.js +10 -2
- package/dist/voice/voiceSession.js.map +1 -1
- package/package.json +2 -2
- package/src/agent.ts +106 -35
- package/src/cli/index.ts +12 -12
- package/src/edges/createDirectEdge.ts +1 -1
- package/src/edges/createLogicalRouter.ts +7 -8
- package/src/edges/createPromptRouter.ts +4 -3
- package/src/edges/edgeFactory.ts +2 -2
- package/src/interrupts/BaseInterruptSessionManager.ts +79 -0
- package/src/interrupts/MemoryInterruptSessionManager.ts +57 -0
- package/src/interrupts/MindedInterruptSessionManager.ts +172 -0
- package/src/interrupts/interruptSessionManagerFactory.ts +20 -0
- package/src/nodes/addAppToolNode.ts +1 -1
- package/src/nodes/addHumanInTheLoopNode.ts +3 -3
- package/src/nodes/addJumpToNode.ts +1 -1
- package/src/nodes/addPromptNode.ts +51 -6
- package/src/nodes/addToolNode.ts +4 -2
- package/src/nodes/addToolRunNode.ts +1 -1
- package/src/nodes/addTriggerNode.ts +1 -1
- package/src/platform/mindedConnection.ts +15 -25
- package/src/platform/mindedConnectionTypes.ts +202 -1
- package/src/playbooks/playbooks.ts +4 -4
- package/src/types/Agent.types.ts +2 -0
- package/src/types/LangGraph.types.ts +3 -1
- package/src/utils/logger.ts +1 -0
- package/src/voice/voiceSession.ts +14 -6
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { State } from '../types/LangGraph.types';
|
|
2
|
+
import { GraphInterrupt } from '@langchain/langgraph';
|
|
3
|
+
import { logger } from '../utils/logger';
|
|
4
|
+
|
|
5
|
+
export enum InterruptType {
|
|
6
|
+
NEW_TRIGGERS = 'NEW_TRIGGERS',
|
|
7
|
+
HUMAN_IN_THE_LOOP = 'HUMAN_IN_THE_LOOP',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface QueuedMessage {
|
|
11
|
+
triggerBody: any;
|
|
12
|
+
triggerName: string;
|
|
13
|
+
appName?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// export interface UpdateStateObject {
|
|
17
|
+
// memory: any;
|
|
18
|
+
// messages: any[];
|
|
19
|
+
// history: any[];
|
|
20
|
+
// sessionId: string;
|
|
21
|
+
// sessionType: SessionType;
|
|
22
|
+
// }
|
|
23
|
+
export interface InterruptPayload {
|
|
24
|
+
type: InterruptType.NEW_TRIGGERS;
|
|
25
|
+
updateStateObject?: Partial<State>;
|
|
26
|
+
id?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InterruptSessionManager {
|
|
30
|
+
lock(sessionId: string): void | Promise<void>;
|
|
31
|
+
release(sessionId: string): void | Promise<void>;
|
|
32
|
+
enqueueMessage(sessionId: string, message: QueuedMessage): void | Promise<void>;
|
|
33
|
+
dequeueAll(sessionId: string): QueuedMessage[] | Promise<QueuedMessage[]>;
|
|
34
|
+
dequeue(sessionId: string): QueuedMessage | null | Promise<QueuedMessage | null>;
|
|
35
|
+
checkQueueAndInterrupt(sessionId: string, updateStateObject?: Partial<State>): Promise<boolean>;
|
|
36
|
+
tryLock(sessionId: string): boolean | Promise<boolean>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export abstract class BaseInterruptSessionManager implements InterruptSessionManager {
|
|
40
|
+
// Abstract methods that subclasses must implement for queue management
|
|
41
|
+
abstract lock(sessionId: string): void | Promise<void>;
|
|
42
|
+
abstract release(sessionId: string): void | Promise<void>;
|
|
43
|
+
abstract enqueueMessage(sessionId: string, message: QueuedMessage): void | Promise<void>;
|
|
44
|
+
abstract dequeueAll(sessionId: string): QueuedMessage[] | Promise<QueuedMessage[]>;
|
|
45
|
+
abstract dequeue(sessionId: string): QueuedMessage | null | Promise<QueuedMessage | null>;
|
|
46
|
+
abstract tryLock(sessionId: string): boolean | Promise<boolean>;
|
|
47
|
+
|
|
48
|
+
// Abstract method to check if queue has messages - this is the only queue-specific part
|
|
49
|
+
protected abstract hasQueuedMessages(sessionId: string): boolean | Promise<boolean>;
|
|
50
|
+
protected abstract getQueuedMessages(sessionId: string): QueuedMessage[] | Promise<QueuedMessage[]>;
|
|
51
|
+
|
|
52
|
+
// Common implementation of checkQueueAndInterrupt
|
|
53
|
+
async checkQueueAndInterrupt(sessionId: string, updateStateObject?: Partial<State>): Promise<boolean> {
|
|
54
|
+
if (await this.hasQueuedMessages(sessionId)) {
|
|
55
|
+
logger.trace({ message: 'graph has queued messagess, interrupting graph', sessionId });
|
|
56
|
+
|
|
57
|
+
// Interrupt the graph with NEW_TRIGGERS flag and optional updateStateObject
|
|
58
|
+
const interruptPayload: InterruptPayload = { type: InterruptType.NEW_TRIGGERS };
|
|
59
|
+
if (updateStateObject) {
|
|
60
|
+
interruptPayload.updateStateObject = {
|
|
61
|
+
messages: updateStateObject.messages,
|
|
62
|
+
memory: updateStateObject.memory,
|
|
63
|
+
history: updateStateObject.history,
|
|
64
|
+
sessionId: updateStateObject.sessionId,
|
|
65
|
+
sessionType: updateStateObject.sessionType,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
throw new GraphInterrupt([
|
|
70
|
+
{
|
|
71
|
+
value: interruptPayload,
|
|
72
|
+
when: 'during',
|
|
73
|
+
resumable: true,
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { BaseInterruptSessionManager, QueuedMessage } from './BaseInterruptSessionManager';
|
|
2
|
+
|
|
3
|
+
export class MemoryInterruptSessionManager extends BaseInterruptSessionManager {
|
|
4
|
+
private sessionProcessing: Map<string, boolean> = new Map();
|
|
5
|
+
private sessionMessageQueues: Map<string, QueuedMessage[]> = new Map();
|
|
6
|
+
|
|
7
|
+
lock(sessionId: string): void {
|
|
8
|
+
this.sessionProcessing.set(sessionId, true);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
release(sessionId: string): void {
|
|
12
|
+
this.sessionProcessing.set(sessionId, false);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
enqueueMessage(sessionId: string, message: QueuedMessage): void {
|
|
16
|
+
if (!this.sessionMessageQueues.has(sessionId)) {
|
|
17
|
+
this.sessionMessageQueues.set(sessionId, []);
|
|
18
|
+
}
|
|
19
|
+
this.sessionMessageQueues.get(sessionId)!.push(message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
dequeueAll(sessionId: string): QueuedMessage[] {
|
|
23
|
+
const messages = this.sessionMessageQueues.get(sessionId) || [];
|
|
24
|
+
const result = [...messages];
|
|
25
|
+
this.sessionMessageQueues.set(sessionId, []);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
dequeue(sessionId: string): QueuedMessage | null {
|
|
30
|
+
const messages = this.sessionMessageQueues.get(sessionId) || [];
|
|
31
|
+
if (messages.length === 0) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const firstMessage = messages.shift()!;
|
|
35
|
+
this.sessionMessageQueues.set(sessionId, messages);
|
|
36
|
+
return firstMessage;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
tryLock(sessionId: string): boolean {
|
|
40
|
+
const isCurrentlyProcessed = this.sessionProcessing.get(sessionId) || false;
|
|
41
|
+
if (isCurrentlyProcessed) {
|
|
42
|
+
return false; // Could not acquire lock
|
|
43
|
+
}
|
|
44
|
+
this.sessionProcessing.set(sessionId, true);
|
|
45
|
+
return true; // Successfully acquired lock
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Implementation of abstract methods from BaseInterruptSessionManager
|
|
49
|
+
protected hasQueuedMessages(sessionId: string): boolean {
|
|
50
|
+
const queue = this.sessionMessageQueues.get(sessionId) || [];
|
|
51
|
+
return queue.length > 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected getQueuedMessages(sessionId: string): QueuedMessage[] {
|
|
55
|
+
return this.sessionMessageQueues.get(sessionId) || [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { BaseInterruptSessionManager, QueuedMessage } from './BaseInterruptSessionManager';
|
|
2
|
+
import * as mindedConnection from '../platform/mindedConnection';
|
|
3
|
+
import {
|
|
4
|
+
mindedConnectionSocketMessageType,
|
|
5
|
+
InterruptSessionIsProcessedRequest,
|
|
6
|
+
InterruptSessionIsProcessedResponse,
|
|
7
|
+
InterruptSessionLockRequest,
|
|
8
|
+
InterruptSessionLockResponse,
|
|
9
|
+
InterruptSessionTryLockRequest,
|
|
10
|
+
InterruptSessionTryLockResponse,
|
|
11
|
+
InterruptSessionReleaseRequest,
|
|
12
|
+
InterruptSessionReleaseResponse,
|
|
13
|
+
InterruptSessionEnqueueRequest,
|
|
14
|
+
InterruptSessionEnqueueResponse,
|
|
15
|
+
InterruptSessionDequeueAllRequest,
|
|
16
|
+
InterruptSessionDequeueAllResponse,
|
|
17
|
+
InterruptSessionDequeueRequest,
|
|
18
|
+
InterruptSessionDequeueResponse,
|
|
19
|
+
InterruptSessionHasMessagesRequest,
|
|
20
|
+
InterruptSessionHasMessagesResponse,
|
|
21
|
+
InterruptSessionGetMessagesRequest,
|
|
22
|
+
InterruptSessionGetMessagesResponse,
|
|
23
|
+
} from '../platform/mindedConnectionTypes';
|
|
24
|
+
|
|
25
|
+
export class MindedInterruptSessionManager extends BaseInterruptSessionManager {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async isProcessed(sessionId: string): Promise<boolean> {
|
|
31
|
+
try {
|
|
32
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionIsProcessedRequest, InterruptSessionIsProcessedResponse>(
|
|
33
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_IS_PROCESSED,
|
|
34
|
+
{
|
|
35
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_IS_PROCESSED,
|
|
36
|
+
sessionId,
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
return response.isProcessed ?? false;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error('Error checking if session is processed:', error);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async lock(sessionId: string): Promise<void> {
|
|
47
|
+
try {
|
|
48
|
+
await mindedConnection.awaitEmit<InterruptSessionLockRequest, InterruptSessionLockResponse>(
|
|
49
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_LOCK,
|
|
50
|
+
{
|
|
51
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_LOCK,
|
|
52
|
+
sessionId,
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error('Error locking session:', error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async release(sessionId: string): Promise<void> {
|
|
62
|
+
try {
|
|
63
|
+
await mindedConnection.awaitEmit<InterruptSessionReleaseRequest, InterruptSessionReleaseResponse>(
|
|
64
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_RELEASE,
|
|
65
|
+
{
|
|
66
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_RELEASE,
|
|
67
|
+
sessionId,
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error('Error releasing session:', error);
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async enqueueMessage(sessionId: string, message: QueuedMessage): Promise<void> {
|
|
77
|
+
try {
|
|
78
|
+
await mindedConnection.awaitEmit<InterruptSessionEnqueueRequest, InterruptSessionEnqueueResponse>(
|
|
79
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_ENQUEUE,
|
|
80
|
+
{
|
|
81
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_ENQUEUE,
|
|
82
|
+
sessionId,
|
|
83
|
+
message,
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('Error enqueuing message:', error);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async dequeueAll(sessionId: string): Promise<QueuedMessage[]> {
|
|
93
|
+
try {
|
|
94
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionDequeueAllRequest, InterruptSessionDequeueAllResponse>(
|
|
95
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_DEQUEUE_ALL,
|
|
96
|
+
{
|
|
97
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_DEQUEUE_ALL,
|
|
98
|
+
sessionId,
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
return response.messages ?? [];
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error('Error dequeuing all messages:', error);
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async dequeue(sessionId: string): Promise<QueuedMessage | null> {
|
|
109
|
+
try {
|
|
110
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionDequeueRequest, InterruptSessionDequeueResponse>(
|
|
111
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_DEQUEUE,
|
|
112
|
+
{
|
|
113
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_DEQUEUE,
|
|
114
|
+
sessionId,
|
|
115
|
+
},
|
|
116
|
+
);
|
|
117
|
+
return response.message ?? null;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error('Error dequeuing message:', error);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async tryLock(sessionId: string): Promise<boolean> {
|
|
125
|
+
try {
|
|
126
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionTryLockRequest, InterruptSessionTryLockResponse>(
|
|
127
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_TRY_LOCK,
|
|
128
|
+
{
|
|
129
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_TRY_LOCK,
|
|
130
|
+
sessionId,
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
return response.lockAcquired ?? false;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error('Error trying to lock session:', error);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Implementation of abstract methods from BaseInterruptSessionManager
|
|
141
|
+
protected async hasQueuedMessages(sessionId: string): Promise<boolean> {
|
|
142
|
+
try {
|
|
143
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionHasMessagesRequest, InterruptSessionHasMessagesResponse>(
|
|
144
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_HAS_MESSAGES,
|
|
145
|
+
{
|
|
146
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_HAS_MESSAGES,
|
|
147
|
+
sessionId,
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
return response.hasMessages ?? false;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.error('Error checking if session has messages:', error);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
protected async getQueuedMessages(sessionId: string): Promise<QueuedMessage[]> {
|
|
158
|
+
try {
|
|
159
|
+
const response = await mindedConnection.awaitEmit<InterruptSessionGetMessagesRequest, InterruptSessionGetMessagesResponse>(
|
|
160
|
+
mindedConnectionSocketMessageType.INTERRUPT_SESSION_GET_MESSAGES,
|
|
161
|
+
{
|
|
162
|
+
type: mindedConnectionSocketMessageType.INTERRUPT_SESSION_GET_MESSAGES,
|
|
163
|
+
sessionId,
|
|
164
|
+
},
|
|
165
|
+
);
|
|
166
|
+
return response.messages ?? [];
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error('Error getting queued messages:', error);
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { InterruptSessionManager } from './BaseInterruptSessionManager';
|
|
2
|
+
import { MemoryInterruptSessionManager } from './MemoryInterruptSessionManager';
|
|
3
|
+
import { MindedInterruptSessionManager } from './MindedInterruptSessionManager';
|
|
4
|
+
import * as mindedConnection from '../platform/mindedConnection';
|
|
5
|
+
import { getConfig } from '../platform/config';
|
|
6
|
+
import { logger } from '../utils/logger';
|
|
7
|
+
|
|
8
|
+
export function createInterruptSessionManager(): InterruptSessionManager {
|
|
9
|
+
const { runLocally } = getConfig();
|
|
10
|
+
if (runLocally) {
|
|
11
|
+
logger.info({ message: 'Using memory interrupt session manager' });
|
|
12
|
+
return new MemoryInterruptSessionManager();
|
|
13
|
+
} else {
|
|
14
|
+
if (!mindedConnection.isConnected()) {
|
|
15
|
+
throw new Error('MindedConnection is required for platform interrupt session manager');
|
|
16
|
+
}
|
|
17
|
+
logger.info({ message: 'Using Minded interrupt session manager' });
|
|
18
|
+
return new MindedInterruptSessionManager();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -26,7 +26,7 @@ export const addAppToolNode = async ({
|
|
|
26
26
|
const cleanedParameters = Object.fromEntries(Object.entries(node.parameters || {}).filter(([, value]) => value !== ''));
|
|
27
27
|
const appRunnerTool = getAppActionRunnerTool(node.displayName!);
|
|
28
28
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
29
|
-
logger.info(`Executing tool node ${appRunnerTool.name}`);
|
|
29
|
+
logger.info({ message: `Executing tool node ${appRunnerTool.name}` });
|
|
30
30
|
|
|
31
31
|
const executeWrapper = async (input: z.infer<typeof appRunnerTool.input>) => {
|
|
32
32
|
try {
|
|
@@ -3,6 +3,7 @@ import { PreCompiledGraph, stateAnnotation } from '../types/LangGraph.types';
|
|
|
3
3
|
import { RunnableLike } from '@langchain/core/runnables';
|
|
4
4
|
import { logger } from '../utils/logger';
|
|
5
5
|
import { internalNodesSuffix } from '../types/Flows.types';
|
|
6
|
+
import { InterruptType } from '../interrupts/BaseInterruptSessionManager';
|
|
6
7
|
|
|
7
8
|
type AddHumanInTheLoopNodeParams = {
|
|
8
9
|
graph: PreCompiledGraph;
|
|
@@ -13,10 +14,9 @@ export const buildHumanInTheLoopNodeName = (nodeName: string) => `${nodeName}${i
|
|
|
13
14
|
|
|
14
15
|
export const addHumanInTheLoopNode = async ({ graph, attachedToNodeName }: AddHumanInTheLoopNodeParams) => {
|
|
15
16
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
16
|
-
logger.info(`Executing "human in the loop" node for the attached node${attachedToNodeName}`);
|
|
17
|
-
|
|
17
|
+
logger.info({ message: `Executing "human in the loop" node for the attached node${attachedToNodeName}` });
|
|
18
18
|
if (state.messages[state.messages.length - 1].getType() === 'ai') {
|
|
19
|
-
const value = interrupt(
|
|
19
|
+
const value = interrupt({ type: InterruptType.HUMAN_IN_THE_LOOP });
|
|
20
20
|
return value;
|
|
21
21
|
}
|
|
22
22
|
};
|
|
@@ -7,7 +7,7 @@ import { createHistoryStep } from '../utils/history';
|
|
|
7
7
|
|
|
8
8
|
export const addJumpToNode = async ({ graph, node }: { graph: PreCompiledGraph; node: JumpToNode }) => {
|
|
9
9
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
10
|
-
logger.info(`Executing jump node ${node.displayName} – jumping to ${node.targetNodeId}`);
|
|
10
|
+
logger.info({ message: `Executing jump node ${node.displayName} – jumping to ${node.targetNodeId}` });
|
|
11
11
|
// No state modifications are necessary; control flow is handled via edges.
|
|
12
12
|
return {
|
|
13
13
|
history: createHistoryStep<HistoryStep>(state.history, {
|
|
@@ -14,7 +14,8 @@ import { Agent } from '../agent';
|
|
|
14
14
|
import { logger } from '../utils/logger';
|
|
15
15
|
import { compilePlaybooks } from '../playbooks/playbooks';
|
|
16
16
|
import { createHistoryStep } from '../utils/history';
|
|
17
|
-
|
|
17
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
18
|
+
import { wait } from '../utils/wait';
|
|
18
19
|
type AddPromptNodeParams = {
|
|
19
20
|
graph: PreCompiledGraph;
|
|
20
21
|
node: PromptNode;
|
|
@@ -26,7 +27,8 @@ type AddPromptNodeParams = {
|
|
|
26
27
|
|
|
27
28
|
export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: AddPromptNodeParams) => {
|
|
28
29
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
29
|
-
|
|
30
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
31
|
+
logger.info({ message: `Executing prompt node ${node.displayName}` });
|
|
30
32
|
const llmToUse = node.llmConfig ? createLlmInstance(node.llmConfig) : llm;
|
|
31
33
|
|
|
32
34
|
const globalTools = tools
|
|
@@ -50,9 +52,9 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
50
52
|
${state.memory ? `# Task context:\n${JSON.stringify(state.memory)}\n\n` : ''}
|
|
51
53
|
${compiledPlaybooks ? `# General guidelines:\n${compiledPlaybooks}\n\n` : ''}
|
|
52
54
|
`;
|
|
53
|
-
|
|
55
|
+
// await wait(5000);
|
|
54
56
|
const result: AIMessage = await llmToUse.bindTools(globalTools).invoke([...state.messages, new SystemMessage(message)]);
|
|
55
|
-
|
|
57
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
56
58
|
// Check if the result contains tool calls
|
|
57
59
|
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
58
60
|
// Execute the tools
|
|
@@ -66,6 +68,29 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
66
68
|
try {
|
|
67
69
|
// Invoke the LangChain tool directly
|
|
68
70
|
const toolResult = await matchedTool.invoke(toolCall);
|
|
71
|
+
//check for queue after tool call
|
|
72
|
+
const systemMessageId = uuidv4();
|
|
73
|
+
|
|
74
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId, {
|
|
75
|
+
messages: [
|
|
76
|
+
result,
|
|
77
|
+
toolResult,
|
|
78
|
+
new SystemMessage({
|
|
79
|
+
id: systemMessageId,
|
|
80
|
+
content:
|
|
81
|
+
'you called tool when the user send a new message, Consider calling the function again after user message is processed',
|
|
82
|
+
}),
|
|
83
|
+
],
|
|
84
|
+
history: [
|
|
85
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
86
|
+
type: NodeType.TOOL,
|
|
87
|
+
nodeId: node.name,
|
|
88
|
+
nodeDisplayName: node.displayName,
|
|
89
|
+
raw: toolResult,
|
|
90
|
+
messageIds: [toolResult.id!, systemMessageId],
|
|
91
|
+
}),
|
|
92
|
+
],
|
|
93
|
+
});
|
|
69
94
|
const toolStateUpdate = extractToolStateResponse(toolResult);
|
|
70
95
|
// Properly merge memory and other state updates
|
|
71
96
|
stateUpdates = {
|
|
@@ -74,7 +99,8 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
74
99
|
memory: { ...(stateUpdates as any).memory, ...(toolStateUpdate as any).memory },
|
|
75
100
|
};
|
|
76
101
|
toolResults.push(toolResult);
|
|
77
|
-
} catch (error) {
|
|
102
|
+
} catch (error: any) {
|
|
103
|
+
if (error?.name === 'GraphInterrupt') throw error;
|
|
78
104
|
logger.error({ msg: `Error executing tool ${toolCall.name}:`, error });
|
|
79
105
|
const errorMessage = new ToolMessage({
|
|
80
106
|
content: JSON.stringify({ error: error instanceof Error ? error.message : String(error) }),
|
|
@@ -87,10 +113,30 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
87
113
|
}
|
|
88
114
|
}
|
|
89
115
|
|
|
116
|
+
// await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
117
|
+
|
|
90
118
|
// Return the tool call message and tool results with state updates spread at top level
|
|
91
119
|
return {
|
|
92
120
|
...stateUpdates,
|
|
93
121
|
messages: [result, ...toolResults],
|
|
122
|
+
history: [
|
|
123
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
124
|
+
type: NodeType.TOOL,
|
|
125
|
+
nodeId: node.name,
|
|
126
|
+
nodeDisplayName: node.displayName,
|
|
127
|
+
raw: result,
|
|
128
|
+
messageIds: [result.id!],
|
|
129
|
+
}),
|
|
130
|
+
...toolResults.map((toolResult) =>
|
|
131
|
+
createHistoryStep<HistoryStep>(state.history, {
|
|
132
|
+
type: NodeType.TOOL,
|
|
133
|
+
nodeId: node.name,
|
|
134
|
+
nodeDisplayName: node.displayName,
|
|
135
|
+
raw: toolResult,
|
|
136
|
+
messageIds: [toolResult.id!],
|
|
137
|
+
}),
|
|
138
|
+
),
|
|
139
|
+
],
|
|
94
140
|
};
|
|
95
141
|
}
|
|
96
142
|
|
|
@@ -101,7 +147,6 @@ export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: Ad
|
|
|
101
147
|
});
|
|
102
148
|
console.log('AI Message', result.content);
|
|
103
149
|
}
|
|
104
|
-
|
|
105
150
|
return {
|
|
106
151
|
history: createHistoryStep<HistoryStep>(state.history, {
|
|
107
152
|
type: NodeType.PROMPT_NODE,
|
package/src/nodes/addToolNode.ts
CHANGED
|
@@ -30,8 +30,8 @@ export const addToolNode = async ({
|
|
|
30
30
|
throw new Error(`Tool not found: ${toolNode.toolName} in node ${node.name}`);
|
|
31
31
|
}
|
|
32
32
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
34
|
+
logger.info({ message: `Executing tool node ${toolNode.displayName}` });
|
|
35
35
|
const tool = langchainTool(() => {}, {
|
|
36
36
|
name: matchedTool.name,
|
|
37
37
|
description: matchedTool.description,
|
|
@@ -56,6 +56,8 @@ export const addToolNode = async ({
|
|
|
56
56
|
tool_choice: tool.name,
|
|
57
57
|
})
|
|
58
58
|
.invoke([...state.messages, new SystemMessage(prompt)]);
|
|
59
|
+
logger.info({ message: 'after llm tool invoke' });
|
|
60
|
+
await agent.interruptSessionManager.checkQueueAndInterrupt(state.sessionId);
|
|
59
61
|
return {
|
|
60
62
|
messages: [AIToolCallMessage],
|
|
61
63
|
history: createHistoryStep<HistoryStep>(state.history, {
|
|
@@ -23,6 +23,7 @@ type AddToolRunNodeParams = {
|
|
|
23
23
|
|
|
24
24
|
export const buildToolRunNodeName = (nodeName: string) => `${nodeName}${internalNodesSuffix.TOOL_RUN}`;
|
|
25
25
|
|
|
26
|
+
//you never want to interrupt here because of new triggers, as this node depends on the last message to be the tool call from the addToolNode, interrupting will add human message here
|
|
26
27
|
export const addToolRunNode = async ({ graph, tools, toolNode, attachedToNodeName, agent }: AddToolRunNodeParams) => {
|
|
27
28
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
28
29
|
const matchedTool = tools.find((tool) => tool.name === toolNode.toolName);
|
|
@@ -54,7 +55,6 @@ export const addToolRunNode = async ({ graph, tools, toolNode, attachedToNodeNam
|
|
|
54
55
|
|
|
55
56
|
// Push the toolCallMessage into the messages array from toolStateUpdate
|
|
56
57
|
const updatedMessages = [toolCallMessage, ...((toolStateUpdate as any).messages || [])];
|
|
57
|
-
|
|
58
58
|
// Return the full state update from the tool with the updated messages
|
|
59
59
|
return {
|
|
60
60
|
...toolStateUpdate,
|
|
@@ -7,7 +7,7 @@ import { createHistoryStep } from '../utils/history';
|
|
|
7
7
|
|
|
8
8
|
export const addTriggerNode = async ({ graph, node }: { graph: PreCompiledGraph; node: TriggerNode }) => {
|
|
9
9
|
const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
|
|
10
|
-
logger.info(`Executing trigger node ${node.displayName}`);
|
|
10
|
+
logger.info({ message: `Executing trigger node ${node.displayName}` });
|
|
11
11
|
if (node.triggerType === TriggerType.MANUAL) {
|
|
12
12
|
return {
|
|
13
13
|
history: createHistoryStep<HistoryStep>(state.history, {
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { io, Socket } from 'socket.io-client';
|
|
2
|
-
import {
|
|
3
|
-
mindedConnectionSocketMessageType,
|
|
4
|
-
mindedConnectionSocketMessageTypeMap,
|
|
5
|
-
} from './mindedConnectionTypes';
|
|
2
|
+
import { mindedConnectionSocketMessageType, mindedConnectionSocketMessageTypeMap } from './mindedConnectionTypes';
|
|
6
3
|
import { stringify } from 'flatted';
|
|
7
4
|
import { getConfig } from './config';
|
|
8
5
|
import { logger } from '../utils/logger';
|
|
@@ -28,20 +25,13 @@ export const on = <E extends keyof mindedConnectionSocketMessageTypeMap>(
|
|
|
28
25
|
listeners[event].push(callback);
|
|
29
26
|
};
|
|
30
27
|
|
|
31
|
-
export const emit = <E extends keyof mindedConnectionSocketMessageTypeMap>(
|
|
32
|
-
event: E,
|
|
33
|
-
message: mindedConnectionSocketMessageTypeMap[E]
|
|
34
|
-
) => {
|
|
28
|
+
export const emit = <E extends keyof mindedConnectionSocketMessageTypeMap>(event: E, message: mindedConnectionSocketMessageTypeMap[E]) => {
|
|
35
29
|
if (socket) {
|
|
36
30
|
socket.emit(event, message);
|
|
37
31
|
}
|
|
38
32
|
};
|
|
39
33
|
|
|
40
|
-
export const awaitEmit = async <T, R>(
|
|
41
|
-
event: mindedConnectionSocketMessageType,
|
|
42
|
-
message: T,
|
|
43
|
-
timeoutMs: number = 5000
|
|
44
|
-
): Promise<R> => {
|
|
34
|
+
export const awaitEmit = async <T, R>(event: mindedConnectionSocketMessageType, message: T, timeoutMs: number = 5000): Promise<R> => {
|
|
45
35
|
if (!socket) {
|
|
46
36
|
throw new Error('Socket is not connected');
|
|
47
37
|
}
|
|
@@ -97,43 +87,43 @@ const connect = async (token: string): Promise<void> => {
|
|
|
97
87
|
|
|
98
88
|
const checkReady = () => {
|
|
99
89
|
if (connected && ready) {
|
|
100
|
-
logger.info('\x1b[32mConnection with Minded platform is ready!\x1b[0m');
|
|
101
|
-
logger.info('\x1b[32mPress Ctrl+C to exit...');
|
|
90
|
+
logger.info({ message: '\x1b[32mConnection with Minded platform is ready!\x1b[0m' });
|
|
91
|
+
logger.info({ message: '\x1b[32mPress Ctrl+C to exit...' });
|
|
102
92
|
resolve();
|
|
103
93
|
}
|
|
104
94
|
};
|
|
105
95
|
|
|
106
96
|
// Connection event handlers
|
|
107
97
|
socket.on('connect', () => {
|
|
108
|
-
logger.info('Socket connected, waiting for server setup...');
|
|
98
|
+
logger.info({ message: 'Socket connected, waiting for server setup...' });
|
|
109
99
|
connected = true;
|
|
110
100
|
checkReady();
|
|
111
101
|
});
|
|
112
102
|
|
|
113
103
|
// Listen for ready event from server
|
|
114
104
|
socket.on('sdk-socket-ready', (data: { agentId: string; orgName: string }) => {
|
|
115
|
-
logger.info('Server ready signal received', data);
|
|
105
|
+
logger.info({ message: 'Server ready signal received', data });
|
|
116
106
|
ready = true;
|
|
117
107
|
checkReady();
|
|
118
108
|
});
|
|
119
109
|
|
|
120
110
|
socket.on('connect_error', () => {
|
|
121
|
-
logger.error('Failed to connect to minded platform');
|
|
111
|
+
logger.error({ message: 'Failed to connect to minded platform' });
|
|
122
112
|
reject(new Error('Failed to connect to minded platform'));
|
|
123
113
|
});
|
|
124
114
|
|
|
125
115
|
socket.on('disconnect', () => {
|
|
126
|
-
logger.info('Disconnected from local debugging socket');
|
|
116
|
+
logger.info({ message: 'Disconnected from local debugging socket' });
|
|
127
117
|
connected = false;
|
|
128
118
|
ready = false;
|
|
129
119
|
});
|
|
130
120
|
|
|
131
121
|
// Listen for error messages from the server
|
|
132
122
|
socket.on('error', async (error: { message: string }) => {
|
|
133
|
-
logger.error({
|
|
123
|
+
logger.error({ message: 'Server error:', error });
|
|
134
124
|
|
|
135
125
|
if (error.message.includes('Invalid token')) {
|
|
136
|
-
logger.info('Invalid token');
|
|
126
|
+
logger.info({ message: 'Invalid token' });
|
|
137
127
|
|
|
138
128
|
// Disconnect current socket
|
|
139
129
|
if (socket?.connected) {
|
|
@@ -159,7 +149,7 @@ const connect = async (token: string): Promise<void> => {
|
|
|
159
149
|
// Handle process termination
|
|
160
150
|
process.on('SIGINT', () => {
|
|
161
151
|
if (socket?.connected) {
|
|
162
|
-
logger.info('\nDisconnecting...');
|
|
152
|
+
logger.info({ message: '\nDisconnecting...' });
|
|
163
153
|
socket.disconnect();
|
|
164
154
|
}
|
|
165
155
|
process.exit(0);
|
|
@@ -177,17 +167,17 @@ export const start = async (): Promise<void> => {
|
|
|
177
167
|
|
|
178
168
|
export const disconnect = () => {
|
|
179
169
|
if (!socket) {
|
|
180
|
-
logger.warn('No socket connection to disconnect');
|
|
170
|
+
logger.warn({ message: 'No socket connection to disconnect' });
|
|
181
171
|
return;
|
|
182
172
|
}
|
|
183
173
|
|
|
184
174
|
if (socket.connected) {
|
|
185
|
-
logger.info('Disconnecting from Minded platform...');
|
|
175
|
+
logger.info({ message: 'Disconnecting from Minded platform...' });
|
|
186
176
|
socket.disconnect();
|
|
187
177
|
return;
|
|
188
178
|
}
|
|
189
179
|
|
|
190
|
-
logger.warn('Socket is already disconnected');
|
|
180
|
+
logger.warn({ message: 'Socket is already disconnected' });
|
|
191
181
|
};
|
|
192
182
|
|
|
193
183
|
export const mindedConnection = {
|