@loopman/langchain-sdk 1.7.0 → 1.12.0
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/README.md +78 -5
- package/dist/agents/loopman-agent.d.ts.map +1 -1
- package/dist/agents/loopman-agent.js +51 -92
- package/dist/agents/loopman-agent.js.map +1 -1
- package/dist/client/loopman-api.d.ts +19 -64
- package/dist/client/loopman-api.d.ts.map +1 -1
- package/dist/client/loopman-api.js +85 -248
- package/dist/client/loopman-api.js.map +1 -1
- package/dist/helpers/prompt-orchestrator.js +17 -17
- package/dist/helpers/prompt-orchestrator.js.map +1 -1
- package/dist/helpers/template-generator-static.d.ts +27 -0
- package/dist/helpers/template-generator-static.d.ts.map +1 -0
- package/dist/helpers/template-generator-static.js +52 -0
- package/dist/helpers/template-generator-static.js.map +1 -0
- package/dist/helpers/template-generator.d.ts +50 -0
- package/dist/helpers/template-generator.d.ts.map +1 -0
- package/dist/helpers/template-generator.js +85 -0
- package/dist/helpers/template-generator.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/langgraph/helpers.d.ts +171 -0
- package/dist/langgraph/helpers.d.ts.map +1 -0
- package/dist/langgraph/helpers.js +216 -0
- package/dist/langgraph/helpers.js.map +1 -0
- package/dist/langgraph/index.d.ts +16 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +17 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/langgraph/loopman-conditional-edge.d.ts +58 -0
- package/dist/langgraph/loopman-conditional-edge.d.ts.map +1 -0
- package/dist/langgraph/loopman-conditional-edge.js +77 -0
- package/dist/langgraph/loopman-conditional-edge.js.map +1 -0
- package/dist/langgraph/loopman-context-node.d.ts +74 -0
- package/dist/langgraph/loopman-context-node.d.ts.map +1 -0
- package/dist/langgraph/loopman-context-node.js +143 -0
- package/dist/langgraph/loopman-context-node.js.map +1 -0
- package/dist/langgraph/loopman-validation-node.d.ts +119 -0
- package/dist/langgraph/loopman-validation-node.d.ts.map +1 -0
- package/dist/langgraph/loopman-validation-node.js +420 -0
- package/dist/langgraph/loopman-validation-node.js.map +1 -0
- package/dist/langgraph/types.d.ts +75 -0
- package/dist/langgraph/types.d.ts.map +1 -0
- package/dist/langgraph/types.js +74 -0
- package/dist/langgraph/types.js.map +1 -0
- package/dist/loopman-agent-wrapper.d.ts +18 -0
- package/dist/loopman-agent-wrapper.d.ts.map +1 -1
- package/dist/loopman-agent-wrapper.js +31 -21
- package/dist/loopman-agent-wrapper.js.map +1 -1
- package/dist/loopman-middleware.d.ts +0 -5
- package/dist/loopman-middleware.d.ts.map +1 -1
- package/dist/loopman-middleware.js +22 -23
- package/dist/loopman-middleware.js.map +1 -1
- package/dist/mcp/loopman-mcp-client.d.ts.map +1 -1
- package/dist/mcp/loopman-mcp-client.js +5 -0
- package/dist/mcp/loopman-mcp-client.js.map +1 -1
- package/dist/mcp/tool-registry.d.ts +7 -1
- package/dist/mcp/tool-registry.d.ts.map +1 -1
- package/dist/mcp/tool-registry.js +23 -16
- package/dist/mcp/tool-registry.js.map +1 -1
- package/dist/services/logger.service.d.ts.map +1 -1
- package/dist/services/logger.service.js +4 -12
- package/dist/services/logger.service.js.map +1 -1
- package/dist/services/loopman.service.d.ts +25 -11
- package/dist/services/loopman.service.d.ts.map +1 -1
- package/dist/services/loopman.service.js +74 -45
- package/dist/services/loopman.service.js.map +1 -1
- package/dist/services/polling.service.d.ts +7 -3
- package/dist/services/polling.service.d.ts.map +1 -1
- package/dist/services/polling.service.js +61 -47
- package/dist/services/polling.service.js.map +1 -1
- package/dist/templates.json +20 -0
- package/dist/types.d.ts +9 -35
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/examples/README.md +346 -0
- package/examples/templates/README.md +285 -0
- package/examples/templates/langchain-full-review/.env.example +3 -0
- package/examples/templates/langchain-full-review/README.md +165 -0
- package/examples/templates/langchain-full-review/index.ts +54 -0
- package/examples/templates/langchain-full-review/package.json +29 -0
- package/examples/templates/langchain-full-review/tsconfig.json +22 -0
- package/examples/templates/langchain-tool-validation/.env.example +3 -0
- package/examples/templates/langchain-tool-validation/README.md +137 -0
- package/examples/templates/langchain-tool-validation/index.ts +65 -0
- package/examples/templates/langchain-tool-validation/package.json +29 -0
- package/examples/templates/langchain-tool-validation/tsconfig.json +22 -0
- package/examples/templates/langgraph-hello-world/.env.example +3 -0
- package/examples/templates/langgraph-hello-world/README.md +71 -0
- package/examples/templates/langgraph-hello-world/index.ts +147 -0
- package/examples/templates/langgraph-hello-world/package.json +27 -0
- package/examples/templates/langgraph-hello-world/tsconfig.json +22 -0
- package/package.json +12 -7
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# LangChain SDK Templates
|
|
2
|
+
|
|
3
|
+
This directory contains working example projects that serve as templates for users integrating Loopman with their LangChain applications.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
These are **not** template files - they are **real, functional examples** that:
|
|
8
|
+
- Can be run independently (`npm install && npm start`)
|
|
9
|
+
- Are tested to ensure they work correctly
|
|
10
|
+
- Are exported during build for use by the web application
|
|
11
|
+
- Serve as a single source of truth for starter projects
|
|
12
|
+
|
|
13
|
+
## Available Templates
|
|
14
|
+
|
|
15
|
+
### `langgraph-hello-world/`
|
|
16
|
+
|
|
17
|
+
A complete **LangGraph** integration example demonstrating:
|
|
18
|
+
- Context Enrichment mode (loading guidelines and decision history)
|
|
19
|
+
- Validation with human-in-the-loop
|
|
20
|
+
- Tool execution after approval
|
|
21
|
+
- Retry mechanism for rejected actions
|
|
22
|
+
|
|
23
|
+
**Best for:** Complex stateful workflows with conditional routing
|
|
24
|
+
|
|
25
|
+
**How to run:**
|
|
26
|
+
```bash
|
|
27
|
+
cd langgraph-hello-world
|
|
28
|
+
cp .env.example .env
|
|
29
|
+
# Edit .env with your API keys
|
|
30
|
+
npm install
|
|
31
|
+
npm start
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### `langchain-tool-validation/`
|
|
35
|
+
|
|
36
|
+
A **LangChain middleware** integration demonstrating:
|
|
37
|
+
- Selective tool validation (only specific tools require approval)
|
|
38
|
+
- Automatic interception of tool calls
|
|
39
|
+
- Lightweight integration with existing agents
|
|
40
|
+
- Stateful conversation with checkpointer
|
|
41
|
+
|
|
42
|
+
**Best for:** Adding validation to existing LangChain agents
|
|
43
|
+
|
|
44
|
+
**How to run:**
|
|
45
|
+
```bash
|
|
46
|
+
cd langchain-tool-validation
|
|
47
|
+
cp .env.example .env
|
|
48
|
+
# Edit .env with your API keys
|
|
49
|
+
npm install
|
|
50
|
+
npm start
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `langchain-full-review/`
|
|
54
|
+
|
|
55
|
+
A **Loopman Agent** integration demonstrating:
|
|
56
|
+
- Complete HITL (Human-In-The-Loop) workflow
|
|
57
|
+
- Automatic polling for human decisions
|
|
58
|
+
- Feedback handling and retry mechanism
|
|
59
|
+
- Full lifecycle management
|
|
60
|
+
|
|
61
|
+
**Best for:** Complete turnkey HITL solution
|
|
62
|
+
|
|
63
|
+
**How to run:**
|
|
64
|
+
```bash
|
|
65
|
+
cd langchain-full-review
|
|
66
|
+
cp .env.example .env
|
|
67
|
+
# Edit .env with your API keys
|
|
68
|
+
npm install
|
|
69
|
+
npm start
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## How Templates Work
|
|
73
|
+
|
|
74
|
+
### 1. Development & Testing
|
|
75
|
+
|
|
76
|
+
Templates are maintained as working examples in this directory. When you update a template:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
cd langgraph-hello-world
|
|
80
|
+
# Make changes to index.ts, README.md, etc.
|
|
81
|
+
npm start # Test your changes
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 2. Export to JSON
|
|
85
|
+
|
|
86
|
+
During SDK build, templates are exported to `dist/templates.json`:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
npm run build
|
|
90
|
+
# Runs: tsc && node scripts/export-templates.js
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
This creates a JSON file with all template contents:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"langgraph-hello-world": {
|
|
98
|
+
"index.ts": "... full file contents ...",
|
|
99
|
+
"package.json": "... full file contents ...",
|
|
100
|
+
...
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 3. Web Application Usage
|
|
106
|
+
|
|
107
|
+
The web application imports this JSON and generates customized starter projects:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import sdkTemplates from './sdk-templates.json';
|
|
111
|
+
|
|
112
|
+
// Customize template with user's workflow ID and API key
|
|
113
|
+
const files = processTemplate(sdkTemplates['langgraph-hello-world'], {
|
|
114
|
+
workflowId: 'user-workflow-123',
|
|
115
|
+
apiKey: 'user-api-key-xyz',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Generate downloadable zip
|
|
119
|
+
createZipFromFiles(files);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Adding a New Template
|
|
123
|
+
|
|
124
|
+
1. **Create the template directory:**
|
|
125
|
+
```bash
|
|
126
|
+
mkdir -p examples/templates/my-new-template
|
|
127
|
+
cd examples/templates/my-new-template
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
2. **Add required files:**
|
|
131
|
+
- `index.ts` - Main code example
|
|
132
|
+
- `package.json` - Dependencies and scripts
|
|
133
|
+
- `tsconfig.json` - TypeScript configuration
|
|
134
|
+
- `README.md` - Usage instructions
|
|
135
|
+
- `.env.example` - Environment variable template
|
|
136
|
+
|
|
137
|
+
3. **Test it works:**
|
|
138
|
+
```bash
|
|
139
|
+
npm install
|
|
140
|
+
npm start
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
4. **Update export script:**
|
|
144
|
+
Edit `scripts/export-templates.js` to include your new template.
|
|
145
|
+
|
|
146
|
+
5. **Build and commit:**
|
|
147
|
+
```bash
|
|
148
|
+
cd ../../../
|
|
149
|
+
npm run build
|
|
150
|
+
git add examples/templates/my-new-template
|
|
151
|
+
git add dist/templates.json
|
|
152
|
+
git commit -m "feat(templates): add my-new-template example"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Design Principles
|
|
156
|
+
|
|
157
|
+
### ✅ DO
|
|
158
|
+
|
|
159
|
+
- **Use environment variables** for dynamic values
|
|
160
|
+
```typescript
|
|
161
|
+
workflowId: process.env.LOOPMAN_WORKFLOW_ID!
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
- **Keep examples simple** and focused on one concept
|
|
165
|
+
|
|
166
|
+
- **Include detailed comments** explaining key concepts
|
|
167
|
+
|
|
168
|
+
- **Test before committing** - ensure it runs successfully
|
|
169
|
+
|
|
170
|
+
- **Document prerequisites** in the template's README
|
|
171
|
+
|
|
172
|
+
### ❌ DON'T
|
|
173
|
+
|
|
174
|
+
- **Use template placeholders** in the code
|
|
175
|
+
```typescript
|
|
176
|
+
// ❌ Bad
|
|
177
|
+
workflowId: "{{WORKFLOW_ID}}"
|
|
178
|
+
|
|
179
|
+
// ✅ Good
|
|
180
|
+
workflowId: process.env.LOOPMAN_WORKFLOW_ID!
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
- **Add non-working code** - everything should be executable
|
|
184
|
+
|
|
185
|
+
- **Hardcode credentials** - always use .env files
|
|
186
|
+
|
|
187
|
+
- **Include node_modules** or build artifacts
|
|
188
|
+
|
|
189
|
+
## Template Structure
|
|
190
|
+
|
|
191
|
+
Each template should follow this structure:
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
template-name/
|
|
195
|
+
├── index.ts # Main entry point
|
|
196
|
+
├── package.json # NPM dependencies and scripts
|
|
197
|
+
├── tsconfig.json # TypeScript configuration
|
|
198
|
+
├── README.md # Template-specific documentation
|
|
199
|
+
└── .env.example # Environment variables (no secrets!)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Required Scripts in package.json
|
|
203
|
+
|
|
204
|
+
```json
|
|
205
|
+
{
|
|
206
|
+
"scripts": {
|
|
207
|
+
"start": "tsx index.ts",
|
|
208
|
+
"dev": "tsx index.ts",
|
|
209
|
+
"build": "tsc",
|
|
210
|
+
"run": "node dist/index.js"
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Required Environment Variables
|
|
216
|
+
|
|
217
|
+
At minimum, include:
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
OPENAI_API_KEY=your-openai-api-key-here
|
|
221
|
+
LOOPMAN_API_KEY=your-loopman-api-key-here
|
|
222
|
+
LOOPMAN_WORKFLOW_ID=your-workflow-id-here
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Updating Templates
|
|
226
|
+
|
|
227
|
+
When you update a template:
|
|
228
|
+
|
|
229
|
+
1. **Make changes** in the template directory
|
|
230
|
+
2. **Test locally** to ensure it still works
|
|
231
|
+
3. **Build SDK** to regenerate `templates.json`
|
|
232
|
+
4. **Commit both** the template files and `dist/templates.json`
|
|
233
|
+
5. **Release SDK** to make updates available to web app
|
|
234
|
+
|
|
235
|
+
## Testing
|
|
236
|
+
|
|
237
|
+
### Manual Testing
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
cd examples/templates/langgraph-hello-world
|
|
241
|
+
npm install
|
|
242
|
+
npm start
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Automated Testing (Future)
|
|
246
|
+
|
|
247
|
+
We plan to add:
|
|
248
|
+
- E2E tests for each template
|
|
249
|
+
- Validation of template structure
|
|
250
|
+
- Dependency version checks
|
|
251
|
+
- README completeness checks
|
|
252
|
+
|
|
253
|
+
## Publishing
|
|
254
|
+
|
|
255
|
+
Templates are included in the npm package:
|
|
256
|
+
|
|
257
|
+
```json
|
|
258
|
+
{
|
|
259
|
+
"files": [
|
|
260
|
+
"dist",
|
|
261
|
+
"examples/templates",
|
|
262
|
+
"README.md",
|
|
263
|
+
"LICENSE"
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
This ensures users can:
|
|
269
|
+
- Reference the source code
|
|
270
|
+
- Copy templates directly if needed
|
|
271
|
+
- See the latest working examples
|
|
272
|
+
|
|
273
|
+
## Related Documentation
|
|
274
|
+
|
|
275
|
+
- [Templates System Guide](../../docs/TEMPLATES.md) - Detailed architecture
|
|
276
|
+
- [LangGraph Integration](../../docs/LANGGRAPH_INTEGRATION.md) - Integration patterns
|
|
277
|
+
- [SDK README](../../README.md) - Main documentation
|
|
278
|
+
|
|
279
|
+
## Questions?
|
|
280
|
+
|
|
281
|
+
If you have questions about templates:
|
|
282
|
+
- Check [Templates System Guide](../../docs/TEMPLATES.md)
|
|
283
|
+
- Open an issue on GitHub
|
|
284
|
+
- Contact the Loopman team
|
|
285
|
+
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Loopman LangChain - Full Human Review Mode
|
|
2
|
+
|
|
3
|
+
This project demonstrates how to integrate Loopman with LangChain using **Full Human Review Process** mode.
|
|
4
|
+
|
|
5
|
+
## About Full Human Review Mode
|
|
6
|
+
|
|
7
|
+
In this mode, the Loopman Agent handles the entire workflow including:
|
|
8
|
+
- Creating validation tasks
|
|
9
|
+
- Waiting for human approval
|
|
10
|
+
- Handling feedback and retries
|
|
11
|
+
- Managing the complete lifecycle
|
|
12
|
+
|
|
13
|
+
This is useful when:
|
|
14
|
+
- You want a complete HITL (Human-In-The-Loop) solution
|
|
15
|
+
- The agent should wait for human decisions before proceeding
|
|
16
|
+
- You need full control over the validation workflow
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
1. Install dependencies:
|
|
21
|
+
```bash
|
|
22
|
+
npm install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
2. Copy .env.example to .env and configure your keys:
|
|
26
|
+
```bash
|
|
27
|
+
cp .env.example .env
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Edit the .env file and replace the placeholders with your actual values:
|
|
31
|
+
- `OPENAI_API_KEY`: Your OpenAI API key
|
|
32
|
+
- `LOOPMAN_API_KEY`: Your Loopman API key
|
|
33
|
+
- `LOOPMAN_WORKFLOW_ID`: Your workflow ID from Loopman
|
|
34
|
+
|
|
35
|
+
3. Run the example:
|
|
36
|
+
```bash
|
|
37
|
+
npm start
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## How It Works
|
|
41
|
+
|
|
42
|
+
### 1. Define Tools
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const sayHello = tool(
|
|
46
|
+
({ name }) => {
|
|
47
|
+
console.log(`Hello, ${name}!`);
|
|
48
|
+
return `Hello, ${name}! Welcome to Loopman.`;
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "say_hello",
|
|
52
|
+
description: "Say hello to someone",
|
|
53
|
+
schema: z.object({
|
|
54
|
+
name: z.string().describe("The name of the person to greet"),
|
|
55
|
+
}),
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Create Loopman Agent
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const agent = createLoopmanAgent({
|
|
64
|
+
apiKey: process.env.LOOPMAN_API_KEY!,
|
|
65
|
+
workflowId: process.env.LOOPMAN_WORKFLOW_ID!,
|
|
66
|
+
model: "openai:gpt-4o-mini",
|
|
67
|
+
systemPrompt: "You are a helpful assistant that greets people.",
|
|
68
|
+
category: "hello-world",
|
|
69
|
+
additionalTools: [sayHello],
|
|
70
|
+
requireApprovalForTools: ["say_hello"],
|
|
71
|
+
debug: true,
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Process with Human Validation
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const result = await agent.processWithHumanValidation({
|
|
79
|
+
input: "Say hello to Alice",
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
console.log("Agent response:", result.response);
|
|
83
|
+
if (result.decision) {
|
|
84
|
+
console.log("Decision status:", result.decision.status);
|
|
85
|
+
console.log("Human feedback:", result.decision.feedback);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Key Features
|
|
90
|
+
|
|
91
|
+
- **Complete HITL Workflow**: Agent manages the entire validation lifecycle
|
|
92
|
+
- **Automatic Polling**: Waits for human decisions
|
|
93
|
+
- **Feedback Handling**: Processes human feedback and retries if needed
|
|
94
|
+
- **Category Support**: Organize validations by category
|
|
95
|
+
- **Debug Mode**: Verbose logging for development
|
|
96
|
+
|
|
97
|
+
## Workflow
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
1. User sends message
|
|
101
|
+
↓
|
|
102
|
+
2. Agent processes with LLM
|
|
103
|
+
↓
|
|
104
|
+
3. Agent generates tool call
|
|
105
|
+
↓
|
|
106
|
+
4. Creates Loopman validation task
|
|
107
|
+
↓
|
|
108
|
+
5. Polls for human decision
|
|
109
|
+
↓
|
|
110
|
+
6. If approved → Execute and return result
|
|
111
|
+
If rejected → Process feedback and retry
|
|
112
|
+
If timeout → Handle timeout
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Configuration Options
|
|
116
|
+
|
|
117
|
+
### requireApprovalForTools
|
|
118
|
+
|
|
119
|
+
Specify which tools require validation:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
requireApprovalForTools: ["say_hello", "send_email"]
|
|
123
|
+
// Only these tools will require human approval
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### category
|
|
127
|
+
|
|
128
|
+
Organize validations by category:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
category: "hello-world"
|
|
132
|
+
// Helps filter and organize validation tasks
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### debug
|
|
136
|
+
|
|
137
|
+
Enable/disable verbose logging:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
debug: true // Show detailed logs
|
|
141
|
+
debug: false // Production mode
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Response Structure
|
|
145
|
+
|
|
146
|
+
The `processWithHumanValidation` method returns:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
{
|
|
150
|
+
response: string, // Final agent response
|
|
151
|
+
decision?: {
|
|
152
|
+
status: "APPROVED" | "REJECTED" | "NEEDS_CHANGES",
|
|
153
|
+
feedback?: string, // Human feedback if provided
|
|
154
|
+
createdAt: Date,
|
|
155
|
+
updatedAt: Date,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Learn More
|
|
161
|
+
|
|
162
|
+
- [Loopman Agent Documentation](https://github.com/loopman/loopman-langchain-sdk/blob/main/docs/LOOPMAN_AGENT.md)
|
|
163
|
+
- [LangChain Documentation](https://js.langchain.com/docs)
|
|
164
|
+
- [Loopman Documentation](https://loopman.dev/docs)
|
|
165
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { config } from "dotenv";
|
|
2
|
+
import { tool } from "langchain";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
import { createLoopmanAgent } from "@loopman/langchain-sdk";
|
|
5
|
+
|
|
6
|
+
// Load environment variables
|
|
7
|
+
config();
|
|
8
|
+
|
|
9
|
+
// Simple hello world tool
|
|
10
|
+
const sayHello = tool(
|
|
11
|
+
({ name }) => {
|
|
12
|
+
console.log(`Hello, ${name}!`);
|
|
13
|
+
return `Hello, ${name}! Welcome to Loopman.`;
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
name: "say_hello",
|
|
17
|
+
description: "Say hello to someone",
|
|
18
|
+
schema: z.object({
|
|
19
|
+
name: z.string().describe("The name of the person to greet"),
|
|
20
|
+
}),
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
// Create Loopman agent with full human review process
|
|
25
|
+
const agent = createLoopmanAgent({
|
|
26
|
+
apiKey: process.env.LOOPMAN_API_KEY!,
|
|
27
|
+
workflowId: process.env.LOOPMAN_WORKFLOW_ID!,
|
|
28
|
+
model: "openai:gpt-4o-mini",
|
|
29
|
+
systemPrompt: "You are a helpful assistant that greets people.",
|
|
30
|
+
category: "hello-world",
|
|
31
|
+
additionalTools: [sayHello],
|
|
32
|
+
requireApprovalForTools: ["say_hello"], // Requires validation
|
|
33
|
+
debug: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Run the agent
|
|
37
|
+
async function main() {
|
|
38
|
+
const result = await agent.processWithHumanValidation({
|
|
39
|
+
input: "Say hello to Alice",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log("Agent response:", result.response);
|
|
43
|
+
if (result.decision) {
|
|
44
|
+
console.log("Decision status:", result.decision.status);
|
|
45
|
+
if (result.decision.feedback) {
|
|
46
|
+
console.log("Human feedback:", result.decision.feedback);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await agent.disconnect();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
main().catch(console.error);
|
|
54
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "loopman-langchain-full-review",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Loopman LangChain Full Human Review Example",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "tsx index.ts",
|
|
9
|
+
"dev": "tsx index.ts",
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"run": "node dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@langchain/core": "^1.0.2",
|
|
15
|
+
"@langchain/mcp-adapters": "^1.0.0",
|
|
16
|
+
"@langchain/langgraph": "^1.0.1",
|
|
17
|
+
"@langchain/openai": "^1.0.0",
|
|
18
|
+
"@loopman/langchain-sdk": "^1.12.0",
|
|
19
|
+
"dotenv": "^17.2.3",
|
|
20
|
+
"langchain": "^1.0.2",
|
|
21
|
+
"tsx": "^4.20.6",
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"zod": "^3.25.76"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^24.9.2"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"lib": ["ES2022"],
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"resolveJsonModule": true,
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"declaration": true,
|
|
16
|
+
"declarationMap": true,
|
|
17
|
+
"sourceMap": true
|
|
18
|
+
},
|
|
19
|
+
"include": ["**/*.ts"],
|
|
20
|
+
"exclude": ["node_modules", "dist"]
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Loopman LangChain - Tool Validation Mode
|
|
2
|
+
|
|
3
|
+
This project demonstrates how to integrate Loopman with LangChain using **Tool Validation** mode.
|
|
4
|
+
|
|
5
|
+
## About Tool Validation Mode
|
|
6
|
+
|
|
7
|
+
In this mode, Loopman middleware intercepts specific tool calls and requires human validation before execution. This is useful when:
|
|
8
|
+
- Certain actions need human approval (e.g., sending emails, making payments)
|
|
9
|
+
- You want to validate AI decisions before they affect real systems
|
|
10
|
+
- Compliance requires human oversight for specific operations
|
|
11
|
+
|
|
12
|
+
## Setup
|
|
13
|
+
|
|
14
|
+
1. Install dependencies:
|
|
15
|
+
```bash
|
|
16
|
+
npm install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
2. Copy .env.example to .env and configure your keys:
|
|
20
|
+
```bash
|
|
21
|
+
cp .env.example .env
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Edit the .env file and replace the placeholders with your actual values:
|
|
25
|
+
- `OPENAI_API_KEY`: Your OpenAI API key
|
|
26
|
+
- `LOOPMAN_API_KEY`: Your Loopman API key
|
|
27
|
+
- `LOOPMAN_WORKFLOW_ID`: Your workflow ID from Loopman
|
|
28
|
+
|
|
29
|
+
3. Run the example:
|
|
30
|
+
```bash
|
|
31
|
+
npm start
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## How It Works
|
|
35
|
+
|
|
36
|
+
### 1. Define Tools
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
const sayHello = tool(
|
|
40
|
+
({ name }) => {
|
|
41
|
+
console.log(`Hello, ${name}!`);
|
|
42
|
+
return `Hello, ${name}! Welcome to Loopman.`;
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "say_hello",
|
|
46
|
+
description: "Say hello to someone",
|
|
47
|
+
schema: z.object({
|
|
48
|
+
name: z.string().describe("The name of the person to greet"),
|
|
49
|
+
}),
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. Add Loopman Middleware
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
const agent = createAgent({
|
|
58
|
+
model: "openai:gpt-4o-mini",
|
|
59
|
+
tools: [sayHello],
|
|
60
|
+
middleware: [
|
|
61
|
+
loopmanMiddleware({
|
|
62
|
+
apiKey: process.env.LOOPMAN_API_KEY!,
|
|
63
|
+
workflowId: process.env.LOOPMAN_WORKFLOW_ID!,
|
|
64
|
+
interruptOn: {
|
|
65
|
+
say_hello: true, // Requires human validation
|
|
66
|
+
},
|
|
67
|
+
debug: true,
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
70
|
+
checkpointer,
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 3. Run the Agent
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const result = await agent.invoke(
|
|
78
|
+
{
|
|
79
|
+
messages: [{ role: "user", content: "Say hello to Alice" }],
|
|
80
|
+
},
|
|
81
|
+
config
|
|
82
|
+
);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Key Features
|
|
86
|
+
|
|
87
|
+
- **Selective Validation**: Only specified tools require approval
|
|
88
|
+
- **Automatic Interception**: Middleware handles validation workflow
|
|
89
|
+
- **Stateful**: Uses checkpointer to maintain conversation state
|
|
90
|
+
- **Debug Mode**: Verbose logging for development
|
|
91
|
+
|
|
92
|
+
## Workflow
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
1. User sends message
|
|
96
|
+
↓
|
|
97
|
+
2. Agent generates tool call (say_hello)
|
|
98
|
+
↓
|
|
99
|
+
3. Loopman middleware intercepts
|
|
100
|
+
↓
|
|
101
|
+
4. Creates validation task
|
|
102
|
+
↓
|
|
103
|
+
5. Waits for human decision
|
|
104
|
+
↓
|
|
105
|
+
6. If approved → Execute tool
|
|
106
|
+
If rejected → Return to agent with feedback
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Configuration Options
|
|
110
|
+
|
|
111
|
+
### interruptOn
|
|
112
|
+
|
|
113
|
+
Specify which tools require validation:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
interruptOn: {
|
|
117
|
+
say_hello: true, // Always validate
|
|
118
|
+
send_email: true, // Always validate
|
|
119
|
+
get_weather: false, // Never validate (auto-execute)
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### debug
|
|
124
|
+
|
|
125
|
+
Enable/disable verbose logging:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
debug: true // Show detailed logs
|
|
129
|
+
debug: false // Production mode
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Learn More
|
|
133
|
+
|
|
134
|
+
- [Loopman Middleware Documentation](https://github.com/loopman/loopman-langchain-sdk/blob/main/docs/TOOL_VALIDATION_MODE.md)
|
|
135
|
+
- [LangChain Agent Documentation](https://js.langchain.com/docs/how_to/agent_executor)
|
|
136
|
+
- [Loopman Documentation](https://loopman.dev/docs)
|
|
137
|
+
|