@minded-ai/mindedjs 1.0.19

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.
Files changed (183) hide show
  1. package/.github/workflows/CI.yml +34 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +6 -0
  4. package/dist/agent.d.ts +36 -0
  5. package/dist/agent.js +199 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/analytics.d.ts +6 -0
  8. package/dist/analytics.js +19 -0
  9. package/dist/analytics.js.map +1 -0
  10. package/dist/edges/createDirectEdge.d.ts +4 -0
  11. package/dist/edges/createDirectEdge.js +14 -0
  12. package/dist/edges/createDirectEdge.js.map +1 -0
  13. package/dist/edges/createLogicalRouter.d.ts +5 -0
  14. package/dist/edges/createLogicalRouter.js +18 -0
  15. package/dist/edges/createLogicalRouter.js.map +1 -0
  16. package/dist/edges/createPromptRouter.d.ts +7 -0
  17. package/dist/edges/createPromptRouter.js +54 -0
  18. package/dist/edges/createPromptRouter.js.map +1 -0
  19. package/dist/edges/edgeFactory.d.ts +9 -0
  20. package/dist/edges/edgeFactory.js +65 -0
  21. package/dist/edges/edgeFactory.js.map +1 -0
  22. package/dist/events/AgentEvents.d.ts +22 -0
  23. package/dist/events/AgentEvents.js +9 -0
  24. package/dist/events/AgentEvents.js.map +1 -0
  25. package/dist/events/index.d.ts +2 -0
  26. package/dist/events/index.js +5 -0
  27. package/dist/events/index.js.map +1 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +15 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/infrastructure.ts/mindedRequest.d.ts +8 -0
  32. package/dist/infrastructure.ts/mindedRequest.js +22 -0
  33. package/dist/infrastructure.ts/mindedRequest.js.map +1 -0
  34. package/dist/llm/createLlmInstance.d.ts +2 -0
  35. package/dist/llm/createLlmInstance.js +14 -0
  36. package/dist/llm/createLlmInstance.js.map +1 -0
  37. package/dist/nodes/addHumanInTheLoopNode.d.ts +8 -0
  38. package/dist/nodes/addHumanInTheLoopNode.js +17 -0
  39. package/dist/nodes/addHumanInTheLoopNode.js.map +1 -0
  40. package/dist/nodes/addPromptNode.d.ts +15 -0
  41. package/dist/nodes/addPromptNode.js +52 -0
  42. package/dist/nodes/addPromptNode.js.map +1 -0
  43. package/dist/nodes/addToolNode.d.ts +10 -0
  44. package/dist/nodes/addToolNode.js +82 -0
  45. package/dist/nodes/addToolNode.js.map +1 -0
  46. package/dist/nodes/addTriggerNode.d.ts +6 -0
  47. package/dist/nodes/addTriggerNode.js +12 -0
  48. package/dist/nodes/addTriggerNode.js.map +1 -0
  49. package/dist/nodes/nodeFactory.d.ts +13 -0
  50. package/dist/nodes/nodeFactory.js +41 -0
  51. package/dist/nodes/nodeFactory.js.map +1 -0
  52. package/dist/platform/analytics.d.ts +6 -0
  53. package/dist/platform/analytics.js +19 -0
  54. package/dist/platform/analytics.js.map +1 -0
  55. package/dist/platform/mindedCheckpointSaver.d.ts +10 -0
  56. package/dist/platform/mindedCheckpointSaver.js +49 -0
  57. package/dist/platform/mindedCheckpointSaver.js.map +1 -0
  58. package/dist/platform/mindedConnection.d.ts +13 -0
  59. package/dist/platform/mindedConnection.js +117 -0
  60. package/dist/platform/mindedConnection.js.map +1 -0
  61. package/dist/platform/mindedConnectionTypes.d.ts +10 -0
  62. package/dist/platform/mindedConnectionTypes.js +8 -0
  63. package/dist/platform/mindedConnectionTypes.js.map +1 -0
  64. package/dist/platform/mindedRequest.d.ts +8 -0
  65. package/dist/platform/mindedRequest.js +22 -0
  66. package/dist/platform/mindedRequest.js.map +1 -0
  67. package/dist/types/Agent.types.d.ts +8 -0
  68. package/dist/types/Agent.types.js +3 -0
  69. package/dist/types/Agent.types.js.map +1 -0
  70. package/dist/types/Flows.types.d.ts +83 -0
  71. package/dist/types/Flows.types.js +24 -0
  72. package/dist/types/Flows.types.js.map +1 -0
  73. package/dist/types/LLM.types.d.ts +10 -0
  74. package/dist/types/LLM.types.js +9 -0
  75. package/dist/types/LLM.types.js.map +1 -0
  76. package/dist/types/LangGraph.types.d.ts +29 -0
  77. package/dist/types/LangGraph.types.js +20 -0
  78. package/dist/types/LangGraph.types.js.map +1 -0
  79. package/dist/types/Tools.types.d.ts +14 -0
  80. package/dist/types/Tools.types.js +3 -0
  81. package/dist/types/Tools.types.js.map +1 -0
  82. package/dist/types/Triggers.types.d.ts +1 -0
  83. package/dist/types/Triggers.types.js +3 -0
  84. package/dist/types/Triggers.types.js.map +1 -0
  85. package/docs/.gitbook/assets/image.png +0 -0
  86. package/docs/README.md +51 -0
  87. package/docs/SUMMARY.md +21 -0
  88. package/docs/api-reference/.nojekyll +1 -0
  89. package/docs/api-reference/assets/hierarchy.js +1 -0
  90. package/docs/api-reference/assets/highlight.css +120 -0
  91. package/docs/api-reference/assets/icons.js +18 -0
  92. package/docs/api-reference/assets/icons.svg +1 -0
  93. package/docs/api-reference/assets/main.js +60 -0
  94. package/docs/api-reference/assets/navigation.js +1 -0
  95. package/docs/api-reference/assets/search.js +1 -0
  96. package/docs/api-reference/assets/style.css +1640 -0
  97. package/docs/api-reference/classes/index.Agent.html +4 -0
  98. package/docs/api-reference/enums/index.EdgeType.html +4 -0
  99. package/docs/api-reference/enums/index.NodeType.html +6 -0
  100. package/docs/api-reference/enums/index.TriggerType.html +4 -0
  101. package/docs/api-reference/enums/index.events.html +3 -0
  102. package/docs/api-reference/hierarchy.html +1 -0
  103. package/docs/api-reference/index.html +310 -0
  104. package/docs/api-reference/interfaces/index.AppToolNode.html +5 -0
  105. package/docs/api-reference/interfaces/index.AppTriggerNode.html +6 -0
  106. package/docs/api-reference/interfaces/index.Flow.html +4 -0
  107. package/docs/api-reference/interfaces/index.JunctionNode.html +4 -0
  108. package/docs/api-reference/interfaces/index.LogicalConditionEdge.html +5 -0
  109. package/docs/api-reference/interfaces/index.ManualTriggerNode.html +5 -0
  110. package/docs/api-reference/interfaces/index.PromptConditionEdge.html +5 -0
  111. package/docs/api-reference/interfaces/index.PromptNode.html +6 -0
  112. package/docs/api-reference/interfaces/index.StepForwardEdge.html +4 -0
  113. package/docs/api-reference/interfaces/index.Tool.html +6 -0
  114. package/docs/api-reference/interfaces/index.ToolNode.html +5 -0
  115. package/docs/api-reference/modules/index-1.html +1 -0
  116. package/docs/api-reference/modules/index.html +1 -0
  117. package/docs/api-reference/modules.html +1 -0
  118. package/docs/api-reference/types/index.Edge.html +1 -0
  119. package/docs/api-reference/types/index.Node.html +1 -0
  120. package/docs/api-reference/types/index.TriggerNode.html +1 -0
  121. package/docs/core-concepts/edges.md +242 -0
  122. package/docs/core-concepts/events.md +161 -0
  123. package/docs/core-concepts/flows.md +74 -0
  124. package/docs/core-concepts/memory-types.md +208 -0
  125. package/docs/core-concepts/nodes.md +239 -0
  126. package/docs/core-concepts/tools.md +205 -0
  127. package/docs/examples/order-refund-flow.md +560 -0
  128. package/docs/getting-started/installation.md +34 -0
  129. package/docs/getting-started/quick-start.md +264 -0
  130. package/docs-structure.md +144 -0
  131. package/eslint.config.js +68 -0
  132. package/examples/orderRefundAgent/flows/orderRefundFlow.yaml +32 -0
  133. package/examples/orderRefundAgent/minded.json +14 -0
  134. package/examples/orderRefundAgent/orderRefundAgent.ts +58 -0
  135. package/examples/orderRefundAgent/schema.ts +7 -0
  136. package/examples/orderRefundAgent/tools/escalateConversation.ts +28 -0
  137. package/examples/orderRefundAgent/tools/index.ts +4 -0
  138. package/examples/orderRefundAgent/tools/refundOrder.ts +27 -0
  139. package/package.json +46 -0
  140. package/src/agent.ts +216 -0
  141. package/src/edges/createDirectEdge.ts +11 -0
  142. package/src/edges/createLogicalRouter.ts +16 -0
  143. package/src/edges/createPromptRouter.ts +52 -0
  144. package/src/edges/edgeFactory.ts +85 -0
  145. package/src/events/AgentEvents.ts +22 -0
  146. package/src/events/index.ts +3 -0
  147. package/src/index.ts +22 -0
  148. package/src/llm/createLlmInstance.ts +10 -0
  149. package/src/nodes/addHumanInTheLoopNode.ts +20 -0
  150. package/src/nodes/addPromptNode.ts +66 -0
  151. package/src/nodes/addToolNode.ts +95 -0
  152. package/src/nodes/addTriggerNode.ts +12 -0
  153. package/src/nodes/nodeFactory.ts +65 -0
  154. package/src/platform/analytics.ts +16 -0
  155. package/src/platform/mindedCheckpointSaver.ts +74 -0
  156. package/src/platform/mindedConnection.ts +106 -0
  157. package/src/platform/mindedConnectionTypes.ts +15 -0
  158. package/src/platform/mindedRequest.ts +28 -0
  159. package/src/types/Agent.types.ts +10 -0
  160. package/src/types/Flows.types.ts +103 -0
  161. package/src/types/LLM.types.ts +13 -0
  162. package/src/types/LangGraph.types.ts +25 -0
  163. package/src/types/Tools.types.ts +9 -0
  164. package/test/can-stay-on-node/can-stay-on-node.test.ts +148 -0
  165. package/test/can-stay-on-node/flows/test-flow.yaml +25 -0
  166. package/test/cannot-stay-on-node/cannot-stay-on-node.test.ts +201 -0
  167. package/test/cannot-stay-on-node/flows/test-flow.yaml +34 -0
  168. package/test/human-in-the-loop-node/flows/test-flow.yaml +23 -0
  169. package/test/human-in-the-loop-node/human-in-the-loop-node.test.ts +92 -0
  170. package/test/logical-edges/flows/logical-edge-test-flow.yaml +24 -0
  171. package/test/logical-edges/logical-edges.test.ts +66 -0
  172. package/test/no-human-in-the-loop-node/flows/test-flow.yaml +23 -0
  173. package/test/no-human-in-the-loop-node/no-human-in-the-loop-node.test.ts +80 -0
  174. package/test/prompt-edges/flows/test-flow.yaml +24 -0
  175. package/test/prompt-edges/prompt-edges.test.ts +90 -0
  176. package/test/prompt-node/flows/test-flow.yaml +24 -0
  177. package/test/prompt-node/prompt-node.test.ts +86 -0
  178. package/test/setup.ts +5 -0
  179. package/test/tool-node/flows/test-flow.yaml +14 -0
  180. package/test/tool-node/tool-node.test.ts +67 -0
  181. package/test/trigger/flows/test-flow.yaml +7 -0
  182. package/test/trigger/trigger.test.ts +57 -0
  183. package/tsconfig.json +17 -0
@@ -0,0 +1,161 @@
1
+ # Events
2
+
3
+ Events are messages that flow through your agent during execution, providing visibility into what's happening and enabling reactive behavior.
4
+
5
+ MindedJS currently supports two main event types: `AI_MESSAGE` and `TRIGGER_EVENT`. Each event type has its own specific input structure, output requirements, and use cases.
6
+
7
+ ## AI\_MESSAGE
8
+
9
+ The `AI_MESSAGE` event is emitted when an AI generates a message that should be sent to the user.
10
+
11
+ ### Input Structure
12
+
13
+ ```typescript
14
+ {
15
+ message: string; // The AI-generated message content
16
+ memory: Memory; // Current memory state (your defined memory schema)
17
+ }
18
+ ```
19
+
20
+ ### Handler Return Value
21
+
22
+ * **Return type**: `void`
23
+ * **Purpose**: Handlers are used for side effects like sending messages to UI
24
+ * **Note**: Return values are ignored
25
+
26
+ ### Usage Example
27
+
28
+ ```typescript
29
+ import { Agent, events } from 'mindedjs';
30
+
31
+ const agent = new Agent({
32
+ memorySchema,
33
+ config,
34
+ tools,
35
+ });
36
+
37
+ // Listen to AI messages
38
+ agent.on(events.AI_MESSAGE, async ({ message, memory }) => {
39
+ console.log('AI said:', message);
40
+ console.log('Current memory:', memory);
41
+
42
+ // Send message to user interface
43
+ await sendMessageToUser(message);
44
+
45
+ // Send via WebSocket
46
+ await websocket.send(JSON.stringify({
47
+ type: 'ai_message',
48
+ content: message
49
+ }));
50
+ });
51
+ ```
52
+
53
+ ### Common Use Cases
54
+
55
+ * **Real-time Chat UI**: Send AI responses to chat interfaces
56
+ * **Logging**: Record AI responses for analytics or debugging
57
+ * **Message Formatting**: Transform AI messages before displaying to users
58
+ * **Notifications**: Trigger alerts or notifications based on AI responses
59
+
60
+ ## TRIGGER\_EVENT
61
+
62
+ The `TRIGGER_EVENT` event is emitted when a trigger node is executed. This event allows you to qualify, transform, and provide initial state for trigger inputs before they're processed by the agent.
63
+
64
+ ### Input Structure
65
+
66
+ ```typescript
67
+ {
68
+ triggerName: string; // Name of the trigger being executed
69
+ triggerBody: any; // The trigger input data (type varies by trigger)
70
+ }
71
+ ```
72
+
73
+ ### Handler Return Values
74
+
75
+ TRIGGER\_EVENT handlers have three possible return types:
76
+
77
+ #### 1. Provide Initial State
78
+
79
+ ```typescript
80
+ {
81
+ messages?: BaseMessage[]; // Initial messages for the conversation
82
+ memory?: Memory; // Initial memory state
83
+ }
84
+ ```
85
+
86
+ #### 2. Disqualify the Trigger
87
+
88
+ ```typescript
89
+ false // Rejects/disqualifies the trigger
90
+ ```
91
+
92
+ #### 3. Qualify without providing initial state
93
+
94
+ ```typescript
95
+ void // If no initial state is needed
96
+ ```
97
+
98
+ ### Usage Examples
99
+
100
+ #### Processing User Input
101
+
102
+ ```typescript
103
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
104
+ if (triggerName === 'userMessage') {
105
+ return {
106
+ memory: {
107
+ conversationStarted: true,
108
+ },
109
+ messages: [new HumanMessage(triggerBody)],
110
+ };
111
+ }
112
+ });
113
+ ```
114
+
115
+ #### Trigger Qualification
116
+
117
+ ```typescript
118
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
119
+ // Validate the trigger input
120
+ if (!isValidInput(triggerBody)) {
121
+ return false; // Disqualify the trigger
122
+ }
123
+
124
+ // Only process business hours triggers
125
+ if (triggerName === 'supportRequest' && !isBusinessHours()) {
126
+ return false;
127
+ }
128
+
129
+ return {
130
+ memory: { validatedInput: triggerBody },
131
+ messages: [],
132
+ };
133
+ });
134
+ ```
135
+
136
+ #### Input Transformation
137
+
138
+ ```typescript
139
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
140
+ if (triggerName === 'emailTrigger') {
141
+ // Transform email data into structured format
142
+ const parsedEmail = parseEmailContent(triggerBody);
143
+
144
+ return {
145
+ memory: {
146
+ emailSubject: parsedEmail.subject,
147
+ senderEmail: parsedEmail.from
148
+ },
149
+ messages: [new HumanMessage(parsedEmail.content)],
150
+ };
151
+ }
152
+ });
153
+ ```
154
+
155
+ ### Common Use Cases
156
+
157
+ * **Input Validation**: Ensure trigger data meets requirements before processing
158
+ * **Data Transformation**: Convert trigger inputs into standardized formats
159
+ * **Context Setting**: Provide initial memory state based on trigger context
160
+ * **Access Control**: Disqualify triggers based on permissions or business rules
161
+ * **Routing Logic**: Handle different trigger types with specific logic
@@ -0,0 +1,74 @@
1
+ # Flows
2
+
3
+ Flows are the **core orchestration mechanism** in MindedJS that define how your AI agent processes conversations through a series of connected nodes. A flow represents a complete run of an AI agent that handles user interactions from trigger to completion.
4
+
5
+ ## What are Flows?
6
+
7
+ Flows define the **conversation logic** and **decision-making pathways** for your AI agent. Each flow consists of:
8
+
9
+ * **Nodes** - Processing units that handle specific tasks
10
+ * **Edges** - Routing logic that connects nodes
11
+ * **Memory** - Shared state that persists throughout the flow
12
+ * **Configuration** - Settings that control flow behavior
13
+
14
+ ```yaml
15
+ name: Customer Support Flow
16
+ nodes:
17
+ - type: trigger
18
+ triggerType: manual
19
+ name: Customer Request
20
+
21
+ - type: promptNode
22
+ name: Support Agent
23
+ prompt: |
24
+ You are a helpful customer support agent.
25
+ Assist the customer with their inquiry.
26
+ llmConfig:
27
+ name: ChatOpenAI
28
+ properties:
29
+ model: gpt-4o
30
+
31
+ edges:
32
+ - source: Customer Request
33
+ target: Support Agent
34
+ type: stepForward
35
+ ```
36
+
37
+ ## Flow Components Overview
38
+
39
+ | Component | Purpose | Description |
40
+ | ------------------------------------------------ | ---------- | ---------------------------------------------------- |
41
+ | [**Nodes**](nodes.md) | Processing | Handle specific tasks like LLM calls, tool execution |
42
+ | [**Edges**](edges.md) | Routing | Define transitions and decision logic between nodes |
43
+ | [**Memory**](memory-types.md) | State | Shared context that persists throughout the flow |
44
+ | [**Configuration**](flows.md#flow-configuration) | Settings | Flow-level settings and metadata |
45
+
46
+ ## Basic Flow Structure
47
+
48
+ Every flow follows this YAML structure:
49
+
50
+ ```yaml
51
+ name: Flow Name # Required: Human-readable flow identifier
52
+ nodes: # Required: Array of processing nodes
53
+ - type: trigger # Node type
54
+ name: Node Name # Unique node identifier
55
+ # ... node-specific config
56
+
57
+ edges: # Required: Array of routing connections
58
+ - source: Source Node # Source node name
59
+ target: Target Node # Target node name
60
+ type: stepForward # Edge type
61
+ # ... edge-specific config
62
+ ```
63
+
64
+ ## Nodes in Flows
65
+
66
+ Flows orchestrate different types of nodes to handle various aspects of conversation processing. For detailed information about each node type, see the [**Nodes documentation**](nodes.md).
67
+
68
+ ## Edges in Flows
69
+
70
+ Edges define how your flow moves between nodes based on different conditions. For comprehensive information about edge types and routing logic, see the [**Edges documentation**](edges.md).
71
+
72
+ ## Memory in Flows
73
+
74
+ Memory provides shared state that persists throughout flow execution, allowing nodes to access and modify data from previous steps.
@@ -0,0 +1,208 @@
1
+ # Memory Types
2
+
3
+ Memory in MindedJS allows agents to persist and share data across conversation turns and flow executions. It acts as the agent's working memory, storing context, user information, conversation state, and any other data your agent needs to remember.
4
+
5
+ ## What is Memory?
6
+
7
+ Memory is a structured data store that:
8
+ - Persists throughout an agent session
9
+ - Is accessible to all nodes in your flows
10
+ - Gets automatically merged when updated
11
+ - Is validated against a schema you define
12
+ - Can store any JSON-serializable data
13
+
14
+ ## Defining Memory Schema
15
+
16
+ Memory schemas are defined using [Zod](https://zod.dev/) for type safety and runtime validation.
17
+
18
+ ```typescript
19
+ import { z } from 'zod';
20
+
21
+ const memorySchema = z.object({
22
+ user: z.object({
23
+ id: z.string(),
24
+ name: z.string(),
25
+ preferences: z.object({
26
+ language: z.string().default('en'),
27
+ timezone: z.string().default('UTC'),
28
+ }),
29
+ }),
30
+ conversation: z.object({
31
+ topic: z.string().optional(),
32
+ urgency: z.enum(['low', 'medium', 'high']).default('medium'),
33
+ }),
34
+ order: z.object({
35
+ id: z.string(),
36
+ status: z.string(),
37
+ total: z.number(),
38
+ }).optional(),
39
+ });
40
+
41
+ type Memory = z.infer<typeof memorySchema>;
42
+ ```
43
+
44
+ ## Using Memory in Your Agent
45
+
46
+ ### 1. Configure Memory Schema
47
+
48
+ ```typescript
49
+ import { Agent } from 'mindedjs';
50
+
51
+ const agent = new Agent({
52
+ memorySchema,
53
+ config,
54
+ tools,
55
+ });
56
+ ```
57
+
58
+ ### 2. Initialize Memory
59
+
60
+ Memory is typically initialized when a trigger event occurs:
61
+
62
+ ```typescript
63
+ import { events } from 'mindedjs';
64
+
65
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
66
+ if (triggerName === 'new_order_issue') {
67
+ return {
68
+ memory: {
69
+ user: { id: triggerBody.userId, name: triggerBody.userName },
70
+ order: { id: triggerBody.orderId, status: 'pending' },
71
+ },
72
+ messages: [],
73
+ };
74
+ }
75
+ });
76
+ ```
77
+
78
+ ### 3. Access Memory in Tools
79
+
80
+ Tools can read from and write to memory:
81
+
82
+ ```typescript
83
+ const updateOrderStatus = {
84
+ name: 'updateOrderStatus',
85
+ description: 'Updates the order status in memory',
86
+ inputSchema: z.object({
87
+ newStatus: z.string(),
88
+ }),
89
+ execute: async ({ input, memory }) => {
90
+ // Read from memory
91
+ const currentOrder = memory.order;
92
+
93
+ // Return updated memory (merges with existing)
94
+ return {
95
+ result: `Order status updated to ${input.newStatus}`,
96
+ memory: {
97
+ order: {
98
+ ...currentOrder,
99
+ status: input.newStatus,
100
+ lastUpdated: new Date().toISOString(),
101
+ }
102
+ }
103
+ };
104
+ },
105
+ };
106
+ ```
107
+
108
+ ## Memory Lifecycle
109
+
110
+ 1. **Initialization**: Memory starts empty `{}` and gets populated during trigger events
111
+ 2. **Persistence**: Automatically persisted between conversation turns
112
+ 3. **Updates**: Memory updates are merged when tools return a memory object
113
+ 4. **Validation**: All updates are validated against your schema
114
+
115
+ ```typescript
116
+ // Current memory: { userId: "123", userName: "John" }
117
+ // Tool returns: { memory: { userName: "John Doe", email: "john@example.com" } }
118
+ // Result: { userId: "123", userName: "John Doe", email: "john@example.com" }
119
+ ```
120
+
121
+ ## Best Practices
122
+
123
+ ### 1. Keep It Focused
124
+ Only store data relevant to your agent's functionality:
125
+
126
+ ```typescript
127
+ // Good: Focused schema
128
+ const memorySchema = z.object({
129
+ customerId: z.string(),
130
+ currentIssue: z.string(),
131
+ resolutionSteps: z.array(z.string()),
132
+ });
133
+ ```
134
+
135
+ ### 2. Use Optional Fields and Defaults
136
+ ```typescript
137
+ const memorySchema = z.object({
138
+ userId: z.string(),
139
+ orderId: z.string().optional(), // Not all conversations involve orders
140
+ conversationState: z.enum(['started', 'in_progress', 'resolved']).default('started'),
141
+ attemptCount: z.number().default(0),
142
+ });
143
+ ```
144
+
145
+ ### 3. Structure Related Data
146
+ Group related fields into objects:
147
+
148
+ ```typescript
149
+ const memorySchema = z.object({
150
+ customer: z.object({
151
+ id: z.string(),
152
+ name: z.string(),
153
+ tier: z.enum(['bronze', 'silver', 'gold']),
154
+ }),
155
+ session: z.object({
156
+ startTime: z.string(),
157
+ channel: z.enum(['chat', 'email', 'phone']),
158
+ }),
159
+ });
160
+ ```
161
+
162
+ ## Common Patterns
163
+
164
+ ### User Context Pattern
165
+ ```typescript
166
+ const memorySchema = z.object({
167
+ user: z.object({
168
+ id: z.string(),
169
+ profile: z.object({
170
+ name: z.string(),
171
+ email: z.string(),
172
+ preferences: z.record(z.unknown()),
173
+ }),
174
+ }),
175
+ });
176
+ ```
177
+
178
+ ### Conversation State Pattern
179
+ ```typescript
180
+ const memorySchema = z.object({
181
+ conversation: z.object({
182
+ phase: z.enum(['greeting', 'information_gathering', 'processing', 'resolution']),
183
+ data: z.record(z.unknown()),
184
+ }),
185
+ });
186
+ ```
187
+
188
+ ## Memory vs Messages
189
+
190
+ - **Memory**: Structured data representing current state and context
191
+ - **Messages**: Message history (AI, Human, Tool call, System etc.)
192
+
193
+ ```typescript
194
+ // Memory: Current state
195
+ {
196
+ orderId: "ORD-123",
197
+ status: "processing",
198
+ customer: { name: "John", tier: "gold" }
199
+ }
200
+
201
+ // Messages: Conversation history
202
+ [
203
+ { role: "user", content: "I need help with my order" },
204
+ { role: "assistant", content: "I'd be happy to help!" },
205
+ ]
206
+ ```
207
+
208
+ Both work together to provide complete context to your agent.
@@ -0,0 +1,239 @@
1
+ # Nodes
2
+
3
+ Nodes are the building blocks of MindedJS flows - discrete processing units that handle different aspects of your agent's workflow.
4
+
5
+ ## Node Types Overview
6
+
7
+ | Node Type | Purpose | Description |
8
+ |-----------|---------|-------------|
9
+ | **[Trigger](#trigger-nodes)** | Entry Points | Start flows from various sources |
10
+ | **[Prompt](#prompt-nodes)** | LLM Processing | Generate responses using language models |
11
+ | **[Tool](#tool-nodes)** | Actions | Execute external functions and APIs |
12
+ | **[Junction](#junction-nodes)** | Flow Control | Route and organize flow logic |
13
+
14
+ ## Trigger Nodes
15
+
16
+ Trigger nodes are entry points that start your flows and initialize them with memory and messages.
17
+
18
+ ### Trigger Types
19
+
20
+ #### Manual Trigger
21
+ ```yaml
22
+ - type: trigger
23
+ triggerType: manual
24
+ name: Customer Support Request
25
+ ```
26
+
27
+ #### App Trigger
28
+ ```yaml
29
+ - type: trigger
30
+ triggerType: app
31
+ name: Zendesk Ticket Created
32
+ appTriggerId: zendesk-new-ticket
33
+ ```
34
+
35
+ #### Webhook Trigger
36
+ ```yaml
37
+ - type: trigger
38
+ triggerType: webhook
39
+ name: Payment Failed Notification
40
+ ```
41
+
42
+ ### Trigger Implementation
43
+
44
+ ```ts
45
+ import { events } from 'mindedjs/src/index';
46
+ import { HumanMessage } from '@langchain/core/messages';
47
+
48
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
49
+ if (triggerName === 'Customer Support Request') {
50
+ return {
51
+ memory: {
52
+ customerQuery: triggerBody,
53
+ timestamp: new Date().toISOString(),
54
+ },
55
+ messages: [new HumanMessage(triggerBody)],
56
+ };
57
+ }
58
+ });
59
+ ```
60
+
61
+ #### Disqualifying Triggers
62
+
63
+ Return `false` to prevent flow execution:
64
+
65
+ ```ts
66
+ agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
67
+ // Only process during business hours
68
+ const hour = new Date().getHours();
69
+ if (hour < 9 || hour > 17) {
70
+ return false;
71
+ }
72
+
73
+ return {
74
+ memory: { businessHours: true },
75
+ messages: [new HumanMessage(triggerBody)],
76
+ };
77
+ });
78
+ ```
79
+
80
+ ## Prompt Nodes
81
+
82
+ Prompt nodes process input through LLM to generate intelligent responses or invoke tools.
83
+
84
+ ### Basic Configuration
85
+
86
+ ```yaml
87
+ - type: promptNode
88
+ name: Customer Service Agent
89
+ prompt: |
90
+ You are a helpful customer service representative.
91
+ Respond to customer inquiries professionally.
92
+
93
+ Customer: {{memory.customerName}}
94
+ Issue: {{memory.issueCategory}}
95
+ llmConfig:
96
+ name: ChatOpenAI
97
+ properties:
98
+ model: gpt-4o
99
+ temperature: 0.7
100
+ max_tokens: 500
101
+ ```
102
+
103
+ ### LLM Configuration Options
104
+
105
+ ```yaml
106
+ # OpenAI
107
+ llmConfig:
108
+ name: ChatOpenAI
109
+ properties:
110
+ model: gpt-4o # or gpt-4o-mini, gpt-3.5-turbo
111
+ temperature: 0.7 # 0.0 (deterministic) to 1.0 (creative)
112
+ max_tokens: 500
113
+
114
+ # Anthropic Claude
115
+ llmConfig:
116
+ name: ChatAnthropic
117
+ properties:
118
+ model: claude-3-sonnet-20240229
119
+ temperature: 0.5
120
+ max_tokens: 1000
121
+ ```
122
+
123
+ ## Tool Nodes
124
+
125
+ Tool nodes execute functions to perform actions like API calls or database queries.
126
+
127
+ ### Basic Tool Node
128
+
129
+ ```yaml
130
+ - type: tool
131
+ name: Lookup Customer Order
132
+ toolName: lookupOrder
133
+ ```
134
+
135
+ ### Tool Implementation
136
+
137
+ ```ts
138
+ import { z } from 'zod';
139
+ import { Tool } from 'mindedjs/src/types/Tools.types';
140
+
141
+ const schema = z.object({
142
+ orderId: z.string(),
143
+ customerEmail: z.string().optional(),
144
+ });
145
+
146
+ const lookupOrderTool: Tool<typeof schema, Memory> = {
147
+ name: 'lookupOrder',
148
+ description: 'Look up order details by order ID',
149
+ input: schema,
150
+ execute: async ({ input, memory }) => {
151
+ const order = await fetchOrderById(input.orderId);
152
+
153
+ if (!order) {
154
+ throw new Error(`Order ${input.orderId} not found`);
155
+ }
156
+
157
+ return {
158
+ memory: {
159
+ orderInfo: order,
160
+ orderStatus: order.status,
161
+ },
162
+ result: `Found order ${order.id} - Status: ${order.status}`,
163
+ };
164
+ },
165
+ };
166
+
167
+ export default lookupOrderTool;
168
+ ```
169
+
170
+ ## Junction Nodes
171
+
172
+ Junction nodes provide flow control without processing, useful for organizing routing logic.
173
+
174
+ ### Basic Junction
175
+
176
+ ```yaml
177
+ - type: junction
178
+ name: Customer Routing Hub
179
+ ```
180
+
181
+ ### Routing Example
182
+
183
+ ```yaml
184
+ nodes:
185
+ - type: junction
186
+ name: Route Customer Request
187
+ - type: promptNode
188
+ name: VIP Support
189
+ - type: promptNode
190
+ name: Standard Support
191
+
192
+ edges:
193
+ - source: Route Customer Request
194
+ target: VIP Support
195
+ type: logicalCondition
196
+ condition: "({ memory }) => memory.customerTier === 'premium'"
197
+
198
+ - source: Route Customer Request
199
+ target: Standard Support
200
+ type: logicalCondition
201
+ condition: "({ memory }) => memory.customerTier === 'standard'"
202
+ ```
203
+
204
+ ## Best Practices
205
+
206
+ ### Use Descriptive Names
207
+ ```yaml
208
+ # ✅ Good
209
+ - type: promptNode
210
+ name: Technical Support Specialist
211
+
212
+ # ❌ Avoid
213
+ - type: promptNode
214
+ name: Agent 1
215
+ ```
216
+
217
+ ### Keep Prompts Focused
218
+ ```yaml
219
+ # ✅ Good - Specific role and context
220
+ - type: promptNode
221
+ name: Order Refund Processor
222
+ prompt: |
223
+ You process customer refund requests for e-commerce orders.
224
+ Order ID: {{memory.orderId}}
225
+ Determine if refund should be approved based on order status and timing.
226
+
227
+ # ❌ Avoid - Too broad
228
+ - type: promptNode
229
+ name: General Assistant
230
+ prompt: "Help the customer with anything they need"
231
+ ```
232
+
233
+ ## Next Steps
234
+
235
+ - **[Edges](edges.md)** - Connect nodes with intelligent routing
236
+ - **[Tools](tools.md)** - Build powerful tool functions
237
+ - **[Memory Types](memory-types.md)** - Design effective state management
238
+
239
+ Nodes are your building blocks - combine them strategically to create powerful AI workflows! 🔧