@minded-ai/mindedjs 1.0.90 β 1.0.91
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/docs/.gitbook/assets/ADLC.png +0 -0
- package/docs/.gitbook/assets/PII-masking.png +0 -0
- package/docs/.gitbook/assets/image.png +0 -0
- package/docs/README.md +54 -0
- package/docs/SUMMARY.md +39 -0
- package/docs/examples/order-refund-flow.md +566 -0
- package/docs/getting-started/environment-configuration.md +98 -0
- package/docs/getting-started/installation.md +44 -0
- package/docs/getting-started/project-configuration.md +206 -0
- package/docs/getting-started/quick-start.md +262 -0
- package/docs/integrations/zendesk.md +554 -0
- package/docs/low-code-editor/edges.md +392 -0
- package/docs/low-code-editor/flows.md +74 -0
- package/docs/low-code-editor/nodes.md +331 -0
- package/docs/low-code-editor/playbooks.md +262 -0
- package/docs/low-code-editor/tools.md +303 -0
- package/docs/low-code-editor/triggers.md +156 -0
- package/docs/platform/events.md +374 -0
- package/docs/platform/logging.md +72 -0
- package/docs/platform/memory.md +219 -0
- package/docs/platform/pii-masking.md +220 -0
- package/docs/platform/secrets.md +99 -0
- package/docs/resources/your-first-eval.md +108 -0
- package/docs-structure.md +141 -0
- package/package.json +4 -2
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Introduction
|
|
2
|
+
|
|
3
|
+
Welcome to Minded Docs π
|
|
4
|
+
|
|
5
|
+
Minded is an AI agent development platform designed for rapidly building, deploying, and scaling production-grade agents.
|
|
6
|
+
|
|
7
|
+
It brings developers, product teams, and business users together to accelerate AI agent creation by up to 10x.
|
|
8
|
+
|
|
9
|
+
- **Generate with AI**: Prompt Copilot, our AI assistant, to quickly generate sophisticated AI agents. Copilot seamlessly integrates with your existing systems, APIs, and workflows, ensuring every agent is production-ready from the start.
|
|
10
|
+
- **Build Visually**: Easily refine agents and workflows with our intuitive visual editor. Drag-and-drop to create, configure, and test your agents without writing code.
|
|
11
|
+
- **Extend with Code**: Customize and extend agents by editing code directly. Enjoy two-way synchronization with GitHub, so your changes stay seamlessly integrated across visual and code-based workflows.
|
|
12
|
+
- **Manage Centrally**: Control and manage your agents with built-in governance, including granular access controls, version control, audit logs, and comprehensive monitoringβall accessible from a single unified platform.
|
|
13
|
+
|
|
14
|
+
Let's dive in and start building powerful AI agents with Minded!
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## β¨ Why MindedJS?
|
|
19
|
+
|
|
20
|
+
- **SDK with no-code platform** β Define Agent as Code or use the visual no-code editor on top.
|
|
21
|
+
|
|
22
|
+
<figure><img src=".gitbook/assets/image.png" alt=""><figcaption></figcaption></figure>
|
|
23
|
+
|
|
24
|
+
## π Get Started
|
|
25
|
+
|
|
26
|
+
Ready to build your first AI agent? Start with our [Quick Start Guide](getting-started/quick-start.md) to create a working agent in minutes.
|
|
27
|
+
|
|
28
|
+
### What You'll Learn
|
|
29
|
+
|
|
30
|
+
1. [**Installation**](getting-started/installation.md) - Set up MindedJS in your project
|
|
31
|
+
2. [**Quick Start**](getting-started/quick-start.md) - Build your first refund processing agent
|
|
32
|
+
3. [**Core Concepts**](low-code-editor/flows.md) - Understand flows, nodes, edges, and memory
|
|
33
|
+
4. [**Your First Eval**](resources/your-first-eval.md) - Solve your first eval
|
|
34
|
+
|
|
35
|
+
### Key Concepts
|
|
36
|
+
|
|
37
|
+
- [**Flows**](low-code-editor/flows.md) - Directed graphs of nodes connected by edges
|
|
38
|
+
- [**Nodes**](low-code-editor/nodes.md) - Processing units (triggers, prompts, tools, junctions)
|
|
39
|
+
- [**Edges**](low-code-editor/edges.md) - Routing logic between nodes
|
|
40
|
+
- [**Tools**](low-code-editor/tools.md) - Strongly-typed functions for LLMs to call
|
|
41
|
+
- [**Memory**](platform/memory.md) - Type-safe state management across your flow
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## π Documentation
|
|
46
|
+
|
|
47
|
+
This documentation is organized to help you learn progressively:
|
|
48
|
+
|
|
49
|
+
- **New to MindedJS?** Start with [Installation](getting-started/installation.md) and [Quick Start](getting-started/quick-start.md)
|
|
50
|
+
- **Understanding concepts?** Explore [Core Concepts](low-code-editor/flows.md)
|
|
51
|
+
- **Ready to build?** Check out [Implementation Examples](implementation-examples/node-examples.md)
|
|
52
|
+
- **Need reference?** Browse the [API Reference](api-reference/agent-class.md)
|
|
53
|
+
|
|
54
|
+
Let's build something amazing! π
|
package/docs/SUMMARY.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Table of contents
|
|
2
|
+
|
|
3
|
+
- [Introduction](README.md)
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
- [Installation](getting-started/installation.md)
|
|
8
|
+
- [Environment Configuration](getting-started/environment-configuration.md)
|
|
9
|
+
- [Project Configuration](getting-started/project-configuration.md)
|
|
10
|
+
- [Quick Start](getting-started/quick-start.md)
|
|
11
|
+
|
|
12
|
+
## Low-Code Editor
|
|
13
|
+
|
|
14
|
+
- [Flows](low-code-editor/flows.md)
|
|
15
|
+
- [Nodes](low-code-editor/nodes.md)
|
|
16
|
+
- [Edges](low-code-editor/edges.md)
|
|
17
|
+
- [Tools](low-code-editor/tools.md)
|
|
18
|
+
- [Triggers](low-code-editor/triggers.md)
|
|
19
|
+
- [Playbooks](low-code-editor/playbooks.md)
|
|
20
|
+
|
|
21
|
+
## Platform
|
|
22
|
+
|
|
23
|
+
- [Memory](platform/memory.md)
|
|
24
|
+
- [Events](platform/events.md)
|
|
25
|
+
- [Secrets](platform/secrets.md)
|
|
26
|
+
- [PII Masking](platform/pii-masking.md)
|
|
27
|
+
- [Logging](platform/logging.md)
|
|
28
|
+
|
|
29
|
+
## Integrations
|
|
30
|
+
|
|
31
|
+
- [Zendesk](integrations/zendesk.md)
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
- [Order Refund Flow](examples/order-refund-flow.md)
|
|
36
|
+
|
|
37
|
+
## Resources
|
|
38
|
+
|
|
39
|
+
- [Your First Eval](resources/your-first-eval.md)
|
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
# Order Refund Flow Example
|
|
2
|
+
|
|
3
|
+
A complete example demonstrating how to build an intelligent customer support agent that can handle order refunds with sophisticated routing and business logic.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This example showcases:
|
|
8
|
+
|
|
9
|
+
- **Trigger handling** with qualification logic
|
|
10
|
+
- **Prompt-based routing** for intelligent conversation flow
|
|
11
|
+
- **Tool integration** for business logic execution
|
|
12
|
+
- **Memory management** for maintaining conversation state
|
|
13
|
+
- **Error handling** and edge cases
|
|
14
|
+
|
|
15
|
+
## Complete Implementation
|
|
16
|
+
|
|
17
|
+
### Project Structure
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
order-refund-agent/
|
|
21
|
+
βββ minded.json # Agent configuration
|
|
22
|
+
βββ schema.ts # Memory type definition
|
|
23
|
+
βββ agent.ts # Main agent implementation
|
|
24
|
+
βββ flows/
|
|
25
|
+
β βββ refundFlow.yaml # Flow definition
|
|
26
|
+
βββ tools/
|
|
27
|
+
βββ lookupOrder.ts # Order lookup tool
|
|
28
|
+
βββ processRefund.ts # Refund processing tool
|
|
29
|
+
βββ index.ts # Tool exports
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 1. Agent Configuration
|
|
33
|
+
|
|
34
|
+
Create your `minded.json` configuration file (see [Project Configuration](../getting-started/project-configuration.md) for detailed documentation):
|
|
35
|
+
|
|
36
|
+
**`minded.json`:**
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"flows": ["./flows"],
|
|
41
|
+
"agent": "./agent.ts",
|
|
42
|
+
"llm": {
|
|
43
|
+
"name": "ChatOpenAI",
|
|
44
|
+
"properties": {
|
|
45
|
+
"model": "gpt-4o",
|
|
46
|
+
"temperature": 0.7
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Memory Schema
|
|
53
|
+
|
|
54
|
+
**`schema.ts`:**
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { z } from 'zod';
|
|
58
|
+
|
|
59
|
+
const memorySchema = z.object({
|
|
60
|
+
// Customer information
|
|
61
|
+
customerName: z.string().optional(),
|
|
62
|
+
customerEmail: z.string().optional(),
|
|
63
|
+
customerId: z.string().optional(),
|
|
64
|
+
|
|
65
|
+
// Order information
|
|
66
|
+
orderId: z.string().optional(),
|
|
67
|
+
orderInfo: z
|
|
68
|
+
.object({
|
|
69
|
+
id: z.string(),
|
|
70
|
+
status: z.string(),
|
|
71
|
+
total: z.number(),
|
|
72
|
+
items: z.array(z.string()),
|
|
73
|
+
purchaseDate: z.string(),
|
|
74
|
+
canRefund: z.boolean(),
|
|
75
|
+
})
|
|
76
|
+
.optional(),
|
|
77
|
+
|
|
78
|
+
// Conversation state
|
|
79
|
+
conversationStage: z.enum(['greeting', 'gathering_info', 'processing_refund', 'complete']).optional(),
|
|
80
|
+
|
|
81
|
+
// Refund information
|
|
82
|
+
refundReason: z.string().optional(),
|
|
83
|
+
refundAmount: z.number().optional(),
|
|
84
|
+
refundProcessed: z.boolean().optional(),
|
|
85
|
+
|
|
86
|
+
// Metadata
|
|
87
|
+
timestamp: z.string().optional(),
|
|
88
|
+
customerQuery: z.string().optional(),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export type Memory = z.infer<typeof memorySchema>;
|
|
92
|
+
export default memorySchema;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 3. Flow Definition
|
|
96
|
+
|
|
97
|
+
**`flows/refundFlow.yaml`:**
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
name: 'Advanced Order Refund Flow'
|
|
101
|
+
nodes:
|
|
102
|
+
# Entry point
|
|
103
|
+
- type: trigger
|
|
104
|
+
triggerType: manual
|
|
105
|
+
name: Customer Support Trigger
|
|
106
|
+
|
|
107
|
+
# Initial customer interaction
|
|
108
|
+
- type: promptNode
|
|
109
|
+
name: Support Agent
|
|
110
|
+
prompt: |
|
|
111
|
+
You are a helpful customer support agent specializing in order issues and refunds.
|
|
112
|
+
|
|
113
|
+
Current customer context:
|
|
114
|
+
- Query: {{memory.customerQuery}}
|
|
115
|
+
- Stage: {{memory.conversationStage}}
|
|
116
|
+
|
|
117
|
+
If this is the first interaction:
|
|
118
|
+
1. Greet the customer warmly
|
|
119
|
+
2. Ask for their order ID if they mention order issues
|
|
120
|
+
3. Ask for their name and email for verification
|
|
121
|
+
|
|
122
|
+
If you have order information:
|
|
123
|
+
- Customer: {{memory.customerName}}
|
|
124
|
+
- Order ID: {{memory.orderId}}
|
|
125
|
+
- Order Status: {{memory.orderInfo.status}}
|
|
126
|
+
- Order Total: ${{memory.orderInfo.total}}
|
|
127
|
+
|
|
128
|
+
Be empathetic, professional, and focused on resolving their issue.
|
|
129
|
+
llmConfig:
|
|
130
|
+
name: ChatOpenAI
|
|
131
|
+
properties:
|
|
132
|
+
model: gpt-4o
|
|
133
|
+
temperature: 0.7
|
|
134
|
+
|
|
135
|
+
# Order lookup functionality
|
|
136
|
+
- type: tool
|
|
137
|
+
name: Lookup Order
|
|
138
|
+
toolName: lookupOrder
|
|
139
|
+
|
|
140
|
+
# Junction for decision making
|
|
141
|
+
- type: junction
|
|
142
|
+
name: Refund Decision Point
|
|
143
|
+
|
|
144
|
+
# Refund processing
|
|
145
|
+
- type: tool
|
|
146
|
+
name: Process Refund
|
|
147
|
+
toolName: processRefund
|
|
148
|
+
|
|
149
|
+
# Final confirmation
|
|
150
|
+
- type: promptNode
|
|
151
|
+
name: Refund Confirmation
|
|
152
|
+
prompt: |
|
|
153
|
+
Great news! Your refund has been processed successfully.
|
|
154
|
+
|
|
155
|
+
Refund Details:
|
|
156
|
+
- Order ID: {{memory.orderId}}
|
|
157
|
+
- Refund Amount: ${{memory.refundAmount}}
|
|
158
|
+
- Reason: {{memory.refundReason}}
|
|
159
|
+
|
|
160
|
+
You can expect to see the refund in your original payment method within 3-5 business days.
|
|
161
|
+
|
|
162
|
+
Is there anything else I can help you with today?
|
|
163
|
+
|
|
164
|
+
edges:
|
|
165
|
+
# Initial flow
|
|
166
|
+
- source: Customer Support Trigger
|
|
167
|
+
target: Support Agent
|
|
168
|
+
type: stepForward
|
|
169
|
+
|
|
170
|
+
# Route to order lookup when customer provides order ID
|
|
171
|
+
- source: Support Agent
|
|
172
|
+
target: Lookup Order
|
|
173
|
+
type: promptCondition
|
|
174
|
+
prompt: 'Customer has provided an order ID and wants to look up order information'
|
|
175
|
+
|
|
176
|
+
# After order lookup, go to decision point
|
|
177
|
+
- source: Lookup Order
|
|
178
|
+
target: Refund Decision Point
|
|
179
|
+
type: stepForward
|
|
180
|
+
|
|
181
|
+
# Decision point routing
|
|
182
|
+
- source: Refund Decision Point
|
|
183
|
+
target: Process Refund
|
|
184
|
+
type: logicalCondition
|
|
185
|
+
condition: "({ memory }) => memory.orderInfo?.canRefund && memory.customerQuery?.toLowerCase().includes('refund')"
|
|
186
|
+
|
|
187
|
+
- source: Refund Decision Point
|
|
188
|
+
target: Support Agent
|
|
189
|
+
type: logicalCondition
|
|
190
|
+
condition: '({ memory }) => !memory.orderInfo?.canRefund'
|
|
191
|
+
|
|
192
|
+
# After refund processing
|
|
193
|
+
- source: Process Refund
|
|
194
|
+
target: Refund Confirmation
|
|
195
|
+
type: stepForward
|
|
196
|
+
|
|
197
|
+
# Allow continued conversation
|
|
198
|
+
- source: Refund Confirmation
|
|
199
|
+
target: Support Agent
|
|
200
|
+
type: promptCondition
|
|
201
|
+
prompt: 'Customer has additional questions or needs more help'
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 4. Tools Implementation
|
|
205
|
+
|
|
206
|
+
**`tools/lookupOrder.ts`:**
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import { z } from 'zod';
|
|
210
|
+
import { Tool } from 'mindedjs/src/types/Tools.types';
|
|
211
|
+
import { Memory } from '../schema';
|
|
212
|
+
|
|
213
|
+
const schema = z.object({
|
|
214
|
+
orderId: z.string(),
|
|
215
|
+
customerEmail: z.string().optional(),
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const lookupOrderTool: Tool<typeof schema, Memory> = {
|
|
219
|
+
name: 'lookupOrder',
|
|
220
|
+
description: 'Look up order details by order ID and optionally verify with customer email',
|
|
221
|
+
input: schema,
|
|
222
|
+
execute: async ({ input, memory }) => {
|
|
223
|
+
try {
|
|
224
|
+
// Simulate API call to order system
|
|
225
|
+
const orderInfo = await fetchOrderFromAPI(input.orderId, input.customerEmail);
|
|
226
|
+
|
|
227
|
+
if (!orderInfo) {
|
|
228
|
+
throw new Error(`Order ${input.orderId} not found or email doesn't match`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Determine if refund is allowed based on business rules
|
|
232
|
+
const canRefund = determineRefundEligibility(orderInfo);
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
memory: {
|
|
236
|
+
orderId: input.orderId,
|
|
237
|
+
orderInfo: {
|
|
238
|
+
...orderInfo,
|
|
239
|
+
canRefund,
|
|
240
|
+
},
|
|
241
|
+
conversationStage: 'gathering_info',
|
|
242
|
+
customerId: orderInfo.customerId,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error('Order lookup failed:', error);
|
|
247
|
+
return {
|
|
248
|
+
memory: {
|
|
249
|
+
orderId: input.orderId,
|
|
250
|
+
conversationStage: 'gathering_info',
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// Simulate order API
|
|
258
|
+
async function fetchOrderFromAPI(orderId: string, email?: string) {
|
|
259
|
+
// Simulate database lookup
|
|
260
|
+
const orders = {
|
|
261
|
+
'12345': {
|
|
262
|
+
id: '12345',
|
|
263
|
+
customerId: 'cust_123',
|
|
264
|
+
customerEmail: 'john@example.com',
|
|
265
|
+
status: 'delivered',
|
|
266
|
+
total: 89.99,
|
|
267
|
+
items: ['Wireless Headphones', 'Phone Case'],
|
|
268
|
+
purchaseDate: '2024-01-15T10:30:00Z',
|
|
269
|
+
},
|
|
270
|
+
'67890': {
|
|
271
|
+
id: '67890',
|
|
272
|
+
customerId: 'cust_456',
|
|
273
|
+
customerEmail: 'jane@example.com',
|
|
274
|
+
status: 'shipped',
|
|
275
|
+
total: 199.99,
|
|
276
|
+
items: ['Laptop Stand', 'Wireless Mouse'],
|
|
277
|
+
purchaseDate: '2024-01-20T14:20:00Z',
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const order = orders[orderId];
|
|
282
|
+
if (!order) return null;
|
|
283
|
+
|
|
284
|
+
// Verify email if provided
|
|
285
|
+
if (email && order.customerEmail !== email) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return order;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Business rules for refund eligibility
|
|
293
|
+
function determineRefundEligibility(orderInfo: any): boolean {
|
|
294
|
+
const purchaseDate = new Date(orderInfo.purchaseDate);
|
|
295
|
+
const now = new Date();
|
|
296
|
+
const daysSincePurchase = Math.floor((now.getTime() - purchaseDate.getTime()) / (1000 * 60 * 60 * 24));
|
|
297
|
+
|
|
298
|
+
// Refund rules:
|
|
299
|
+
// - Must be within 30 days
|
|
300
|
+
// - Order must be delivered
|
|
301
|
+
// - Order total must be less than $500 (higher amounts need manager approval)
|
|
302
|
+
return daysSincePurchase <= 30 && orderInfo.status === 'delivered' && orderInfo.total < 500;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export default lookupOrderTool;
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
**`tools/processRefund.ts`:**
|
|
309
|
+
|
|
310
|
+
```ts
|
|
311
|
+
import { z } from 'zod';
|
|
312
|
+
import { Tool } from 'mindedjs/src/types/Tools.types';
|
|
313
|
+
import { Memory } from '../schema';
|
|
314
|
+
|
|
315
|
+
const schema = z.object({
|
|
316
|
+
orderId: z.string(),
|
|
317
|
+
customerName: z.string(),
|
|
318
|
+
reason: z.string(),
|
|
319
|
+
amount: z.number().optional(), // Optional partial refund amount
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const processRefundTool: Tool<typeof schema, Memory> = {
|
|
323
|
+
name: 'processRefund',
|
|
324
|
+
description: 'Process a refund for the customer order',
|
|
325
|
+
input: schema,
|
|
326
|
+
execute: async ({ input, memory }) => {
|
|
327
|
+
try {
|
|
328
|
+
// Determine refund amount
|
|
329
|
+
const refundAmount = input.amount || memory.orderInfo?.total || 0;
|
|
330
|
+
|
|
331
|
+
// Validate refund amount
|
|
332
|
+
if (refundAmount > (memory.orderInfo?.total || 0)) {
|
|
333
|
+
throw new Error('Refund amount cannot exceed order total');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Process the refund (simulate payment processor API)
|
|
337
|
+
const refundResult = await processRefundWithPaymentProcessor({
|
|
338
|
+
orderId: input.orderId,
|
|
339
|
+
amount: refundAmount,
|
|
340
|
+
reason: input.reason,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Log the refund for auditing
|
|
344
|
+
await logRefundTransaction({
|
|
345
|
+
orderId: input.orderId,
|
|
346
|
+
customerId: memory.customerId,
|
|
347
|
+
amount: refundAmount,
|
|
348
|
+
reason: input.reason,
|
|
349
|
+
processedBy: 'ai-agent',
|
|
350
|
+
timestamp: new Date().toISOString(),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
console.log(`β
Refund processed: Order ${input.orderId}, Amount: $${refundAmount}`);
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
memory: {
|
|
357
|
+
refundAmount,
|
|
358
|
+
refundReason: input.reason,
|
|
359
|
+
refundProcessed: true,
|
|
360
|
+
conversationStage: 'complete',
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
} catch (error) {
|
|
364
|
+
console.error('Refund processing failed:', error);
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
memory: {
|
|
368
|
+
refundAmount: 0,
|
|
369
|
+
refundReason: input.reason,
|
|
370
|
+
refundProcessed: false,
|
|
371
|
+
conversationStage: 'gathering_info', // Go back to gathering info
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// Simulate payment processor API
|
|
379
|
+
async function processRefundWithPaymentProcessor(params: any) {
|
|
380
|
+
// Simulate API call delay
|
|
381
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
382
|
+
|
|
383
|
+
// Simulate occasional failures for testing
|
|
384
|
+
if (Math.random() < 0.1) {
|
|
385
|
+
throw new Error('Payment processor temporarily unavailable');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return {
|
|
389
|
+
refundId: `ref_${Date.now()}`,
|
|
390
|
+
status: 'processed',
|
|
391
|
+
amount: params.amount,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Simulate audit logging
|
|
396
|
+
async function logRefundTransaction(transaction: any) {
|
|
397
|
+
console.log('π Logging refund transaction:', transaction);
|
|
398
|
+
// In real implementation, this would write to a database or audit system
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export default processRefundTool;
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
**`tools/index.ts`:**
|
|
405
|
+
|
|
406
|
+
```ts
|
|
407
|
+
import lookupOrderTool from './lookupOrder';
|
|
408
|
+
import processRefundTool from './processRefund';
|
|
409
|
+
|
|
410
|
+
export default [lookupOrderTool, processRefundTool];
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### 5. Main Agent Implementation
|
|
414
|
+
|
|
415
|
+
**`agent.ts`:**
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
import { Agent } from 'mindedjs/src/agent';
|
|
419
|
+
import { HumanMessage } from '@langchain/core/messages';
|
|
420
|
+
import { events } from 'mindedjs/src/index';
|
|
421
|
+
import memorySchema, { Memory } from './schema';
|
|
422
|
+
import tools from './tools';
|
|
423
|
+
import config from './minded.json';
|
|
424
|
+
|
|
425
|
+
// Create the agent
|
|
426
|
+
const agent = new Agent<Memory>({
|
|
427
|
+
memorySchema,
|
|
428
|
+
config,
|
|
429
|
+
tools,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// Handle AI messages (responses to user)
|
|
433
|
+
agent.on(events.AI_MESSAGE, async ({ message, memory }) => {
|
|
434
|
+
console.log('π€ Agent:', message);
|
|
435
|
+
if (memory.conversationStage) {
|
|
436
|
+
console.log(` Stage: ${memory.conversationStage}`);
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// Handle trigger events with qualification logic
|
|
441
|
+
agent.on(events.TRIGGER_EVENT, async ({ triggerName, triggerBody }) => {
|
|
442
|
+
if (triggerName === 'Customer Support Trigger') {
|
|
443
|
+
// Qualify the trigger - only handle customer service related queries
|
|
444
|
+
const lowerQuery = triggerBody.toLowerCase();
|
|
445
|
+
const serviceKeywords = ['order', 'refund', 'help', 'support', 'issue', 'problem'];
|
|
446
|
+
|
|
447
|
+
const isServiceQuery = serviceKeywords.some((keyword) => lowerQuery.includes(keyword));
|
|
448
|
+
|
|
449
|
+
if (!isServiceQuery) {
|
|
450
|
+
console.log('β Query not qualified for customer service');
|
|
451
|
+
return { isQualified: false }; // Disqualify this trigger
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
console.log('β
Customer service query qualified');
|
|
455
|
+
|
|
456
|
+
// Transform the request and initialize memory
|
|
457
|
+
return {
|
|
458
|
+
isQualified: true,
|
|
459
|
+
memory: {
|
|
460
|
+
customerQuery: triggerBody,
|
|
461
|
+
conversationStage: 'greeting',
|
|
462
|
+
timestamp: new Date().toISOString(),
|
|
463
|
+
},
|
|
464
|
+
messages: [new HumanMessage(triggerBody)],
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return { isQualified: false }; // Unknown trigger
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// Example usage function
|
|
472
|
+
async function runRefundExample() {
|
|
473
|
+
console.log('π Starting Order Refund Flow Example\n');
|
|
474
|
+
|
|
475
|
+
const examples = [
|
|
476
|
+
{
|
|
477
|
+
description: 'Happy path - valid refund request',
|
|
478
|
+
query: "Hi, I need to return my order #12345. The headphones don't work properly.",
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
description: 'Order lookup required',
|
|
482
|
+
query: 'I want to check the status of my recent order and possibly get a refund.',
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
description: 'Non-service query (should be disqualified)',
|
|
486
|
+
query: "What's the weather like today?",
|
|
487
|
+
},
|
|
488
|
+
];
|
|
489
|
+
|
|
490
|
+
for (const example of examples) {
|
|
491
|
+
console.log(`\nπ Example: ${example.description}`);
|
|
492
|
+
console.log(`User: ${example.query}\n`);
|
|
493
|
+
|
|
494
|
+
try {
|
|
495
|
+
const result = await agent.invoke({
|
|
496
|
+
triggerBody: example.query,
|
|
497
|
+
triggerName: 'Customer Support Trigger',
|
|
498
|
+
sessionId: `session_${Date.now()}`,
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
console.log('β
Flow completed successfully');
|
|
502
|
+
console.log('Final memory state:', JSON.stringify(result.memory, null, 2));
|
|
503
|
+
} catch (error) {
|
|
504
|
+
console.error('β Flow execution failed:', error);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
console.log('\n' + 'β'.repeat(50));
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// Run the example if this file is executed directly
|
|
512
|
+
if (require.main === module) {
|
|
513
|
+
runRefundExample().catch(console.error);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export { agent, runRefundExample };
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
## Running the Example
|
|
520
|
+
|
|
521
|
+
1. **Install dependencies:**
|
|
522
|
+
|
|
523
|
+
```bash
|
|
524
|
+
npm install @minded-ai/mindedjs langchain
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
2. **Set up environment:**
|
|
528
|
+
|
|
529
|
+
```bash
|
|
530
|
+
export OPENAI_API_KEY="your-openai-api-key"
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
3. **Run the agent:**
|
|
534
|
+
|
|
535
|
+
```bash
|
|
536
|
+
npx ts-node agent.ts
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
## Expected Output
|
|
540
|
+
|
|
541
|
+
```
|
|
542
|
+
π Starting Order Refund Flow Example
|
|
543
|
+
|
|
544
|
+
π Example: Happy path - valid refund request
|
|
545
|
+
User: Hi, I need to return my order #12345. The headphones don't work properly.
|
|
546
|
+
|
|
547
|
+
β
Customer service query qualified
|
|
548
|
+
π€ Agent: Hello! I'm sorry to hear you're having trouble with your headphones. I'd be happy to help you with a return. I see you mentioned order #12345. Could you please provide your email address so I can look up your order details?
|
|
549
|
+
|
|
550
|
+
β
Refund processed: Order 12345, Amount: $89.99
|
|
551
|
+
π Logging refund transaction: {...}
|
|
552
|
+
π€ Agent: Great news! Your refund has been processed successfully...
|
|
553
|
+
|
|
554
|
+
β
Flow completed successfully
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
## Key Features Demonstrated
|
|
558
|
+
|
|
559
|
+
1. **Intelligent Qualification**: The trigger only accepts customer service related queries
|
|
560
|
+
2. **Conversation State Management**: Memory tracks the conversation stage and relevant information
|
|
561
|
+
3. **Business Logic Integration**: Tools implement real business rules for refund eligibility
|
|
562
|
+
4. **Error Handling**: Graceful handling of failed API calls and invalid requests
|
|
563
|
+
5. **Audit Trail**: All refund transactions are logged for compliance
|
|
564
|
+
6. **Type Safety**: Full TypeScript type safety throughout the flow
|
|
565
|
+
|
|
566
|
+
This example provides a solid foundation for building production-ready customer service agents with MindedJS!
|