@memgrafter/flatagents 0.8.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 +221 -0
- package/dist/index.d.mts +673 -0
- package/dist/index.d.ts +673 -0
- package/dist/index.js +1700 -0
- package/dist/index.mjs +1642 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# FlatAgents TypeScript SDK
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for FlatAgents - Declarative LLM orchestration with YAML.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install flatagents
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### Single Agent Call
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { FlatAgent } from 'flatagents';
|
|
17
|
+
|
|
18
|
+
const agent = new FlatAgent('agent.yml');
|
|
19
|
+
const result = await agent.call({ query: "Hello World" });
|
|
20
|
+
console.log(result.output);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### State Machine Execution
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { FlatMachine } from 'flatagents';
|
|
27
|
+
|
|
28
|
+
const machine = new FlatMachine({
|
|
29
|
+
config: 'machine.yml',
|
|
30
|
+
hooks: customHooks,
|
|
31
|
+
persistence: new MemoryBackend()
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const result = await machine.execute({ input: "Hello" });
|
|
35
|
+
console.log(result);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Core Concepts
|
|
39
|
+
|
|
40
|
+
### FlatAgent
|
|
41
|
+
A single LLM call configured in YAML:
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
spec: flatagent
|
|
45
|
+
spec_version: "1.0"
|
|
46
|
+
data:
|
|
47
|
+
name: my_agent
|
|
48
|
+
model:
|
|
49
|
+
name: gpt-4o-mini
|
|
50
|
+
provider: openai
|
|
51
|
+
system: "You are a helpful assistant."
|
|
52
|
+
user: "{{ input.query }}"
|
|
53
|
+
output:
|
|
54
|
+
response:
|
|
55
|
+
type: str
|
|
56
|
+
description: "The response"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### FlatMachine
|
|
60
|
+
A state machine that orchestrates agents:
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
spec: flatmachine
|
|
64
|
+
spec_version: "1.0"
|
|
65
|
+
data:
|
|
66
|
+
name: my_workflow
|
|
67
|
+
context:
|
|
68
|
+
result: ""
|
|
69
|
+
states:
|
|
70
|
+
initial:
|
|
71
|
+
type: initial
|
|
72
|
+
agent: agent.yml
|
|
73
|
+
transitions:
|
|
74
|
+
- to: final
|
|
75
|
+
final:
|
|
76
|
+
type: final
|
|
77
|
+
output:
|
|
78
|
+
result: "{{ context.result }}"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Key Features
|
|
82
|
+
|
|
83
|
+
### Parallel Execution
|
|
84
|
+
```yaml
|
|
85
|
+
states:
|
|
86
|
+
parallel_review:
|
|
87
|
+
machine: [legal_review, tech_review, finance_review]
|
|
88
|
+
transitions:
|
|
89
|
+
- to: synthesize
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Dynamic Parallelism (Foreach)
|
|
93
|
+
```yaml
|
|
94
|
+
states:
|
|
95
|
+
process_all:
|
|
96
|
+
foreach: "{{ context.documents }}"
|
|
97
|
+
as: doc
|
|
98
|
+
machine: processor.yml
|
|
99
|
+
transitions:
|
|
100
|
+
- to: aggregate
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Retry with Backoff
|
|
104
|
+
```yaml
|
|
105
|
+
states:
|
|
106
|
+
robust_call:
|
|
107
|
+
agent: agent.yml
|
|
108
|
+
execution:
|
|
109
|
+
type: retry
|
|
110
|
+
backoffs: [2, 8, 16, 35]
|
|
111
|
+
jitter: 0.1
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Conditional Transitions
|
|
115
|
+
```yaml
|
|
116
|
+
states:
|
|
117
|
+
check_result:
|
|
118
|
+
agent: evaluator.yml
|
|
119
|
+
transitions:
|
|
120
|
+
- condition: "context.score >= 8"
|
|
121
|
+
to: success
|
|
122
|
+
- to: retry
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Error Handling
|
|
126
|
+
```yaml
|
|
127
|
+
states:
|
|
128
|
+
risky_state:
|
|
129
|
+
agent: agent.yml
|
|
130
|
+
on_error: error_handler
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Persistence & Checkpointing
|
|
134
|
+
```yaml
|
|
135
|
+
persistence:
|
|
136
|
+
enabled: true
|
|
137
|
+
backend: local # or memory
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Hooks
|
|
141
|
+
|
|
142
|
+
Extend with custom logic:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
class CustomHooks implements MachineHooks {
|
|
146
|
+
async onStateEnter(state: string, context: any) {
|
|
147
|
+
console.log(`Entering ${state}`);
|
|
148
|
+
return context;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async onError(state: string, error: Error, context: any) {
|
|
152
|
+
console.error(`Error in ${state}:`, error);
|
|
153
|
+
return "recovery_state";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## MCP Integration
|
|
159
|
+
|
|
160
|
+
```yaml
|
|
161
|
+
data:
|
|
162
|
+
mcp:
|
|
163
|
+
servers:
|
|
164
|
+
filesystem:
|
|
165
|
+
command: "npx"
|
|
166
|
+
args: ["@modelcontextprotocol/server-filesystem", "/path/to/files"]
|
|
167
|
+
tool_filter:
|
|
168
|
+
allow: ["filesystem:*"]
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Examples
|
|
172
|
+
|
|
173
|
+
- **Helloworld**: Simple agent that builds "Hello World" one character at a time
|
|
174
|
+
- **Parallelism**: Machine arrays, foreach loops, and fire-and-forget patterns
|
|
175
|
+
- **Human-in-the-loop**: Custom hooks for interactive approval flows
|
|
176
|
+
- **Peering**: Parent-child machine communication with result backends
|
|
177
|
+
|
|
178
|
+
## API Reference
|
|
179
|
+
|
|
180
|
+
### FlatAgent
|
|
181
|
+
```typescript
|
|
182
|
+
class FlatAgent {
|
|
183
|
+
constructor(config: AgentConfig | string);
|
|
184
|
+
async call(input: Record<string, any>): Promise<{content: string, output: any}>;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### FlatMachine
|
|
189
|
+
```typescript
|
|
190
|
+
class FlatMachine {
|
|
191
|
+
constructor(options: MachineOptions);
|
|
192
|
+
async execute(input?: Record<string, any>): Promise<any>;
|
|
193
|
+
async resume(executionId: string): Promise<any>;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Execution Types
|
|
198
|
+
- `DefaultExecution`: Simple single execution
|
|
199
|
+
- `RetryExecution`: Retry with exponential backoff
|
|
200
|
+
|
|
201
|
+
### Persistence Backends
|
|
202
|
+
- `MemoryBackend`: In-memory storage
|
|
203
|
+
- `LocalFileBackend`: File-based with atomic writes
|
|
204
|
+
|
|
205
|
+
## Testing
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
npm test
|
|
209
|
+
npm run typecheck
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Building
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
npm run build
|
|
216
|
+
npm run dev # watch mode
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## License
|
|
220
|
+
|
|
221
|
+
MIT
|