Skip to content

Commit edd3667

Browse files
arcaelaclaude
andcommitted
docs: complete Examples and Advanced sections with comprehensive content
- Add complete multi-provider example with failover patterns - Add custom tools example with simple and advanced tool creation - Add context inheritance example with enterprise patterns - Add advanced patterns example with agent chaining, parallelization, and more - Add architecture documentation with system design details - Add performance optimization techniques - Add troubleshooting guide for common issues - Add migration guide for version upgrades All examples include: - Complete working code - Step-by-step explanations - Best practices - Common pitfalls - Next steps 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bba2bea commit edd3667

8 files changed

Lines changed: 2082 additions & 16 deletions

File tree

docs/advanced/architecture.md

Lines changed: 294 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,295 @@
1-
# architecture.md
1+
# Architecture
22

3-
Documentation coming soon. Check back later or contribute at [GitHub](https://github.com/arcaelas/agent).
3+
Internal design and system architecture of @arcaelas/agent.
4+
5+
## System Overview
6+
7+
```
8+
┌─────────────────────────────────────────┐
9+
│ Agent Layer │
10+
│ ┌─────────────────────────────────┐ │
11+
│ │ Agent (Orchestrator) │ │
12+
│ │ - name, description │ │
13+
│ │ - providers (AI services) │ │
14+
│ │ - call() method │ │
15+
│ └───────────┬─────────────────────┘ │
16+
│ │ │
17+
└──────────────┼───────────────────────────┘
18+
19+
┌──────────────▼───────────────────────────┐
20+
│ Context Layer │
21+
│ ┌─────────────────────────────────┐ │
22+
│ │ Context (State Management) │ │
23+
│ │ - metadata (reactive KV store) │ │
24+
│ │ - rules (behavior guidelines) │ │
25+
│ │ - tools (executable functions) │ │
26+
│ │ - messages (conversation) │ │
27+
│ │ - contexts (parent inheritance) │ │
28+
│ └─────────────────────────────────┘ │
29+
└──────────────────────────────────────────┘
30+
```
31+
32+
## Core Components
33+
34+
### 1. Agent
35+
36+
**Responsibility**: Orchestration and execution
37+
38+
**Key Methods**:
39+
- `constructor(options)` - Initialize with configuration
40+
- `call(prompt)` - Execute conversation with providers and tools
41+
- `get/set metadata`, `rules`, `tools`, `messages` - Access context properties
42+
43+
**Execution Flow** (`call` method):
44+
1. Add user message to context
45+
2. Loop through providers with failover
46+
3. Call provider with current context
47+
4. If response includes tool_calls, execute them in parallel
48+
5. Add tool results to context and loop again
49+
6. Return when completion has no tool_calls
50+
51+
### 2. Context
52+
53+
**Responsibility**: Hierarchical state management with inheritance
54+
55+
**Key Features**:
56+
- Parent context inheritance via `contexts` parameter
57+
- Automatic merging of metadata, rules, tools, messages
58+
- Tool deduplication by name (child overrides parent)
59+
- Reactive metadata updates through broker pattern
60+
61+
**Inheritance Rules**:
62+
- **Metadata**: Child can override parent values (last wins)
63+
- **Rules**: Concatenated (parent rules + child rules)
64+
- **Tools**: Deduplicated by name (child tools replace parent)
65+
- **Messages**: Concatenated (parent messages + child messages)
66+
67+
### 3. Metadata
68+
69+
**Responsibility**: Reactive key-value storage with broker pattern
70+
71+
**Architecture**:
72+
```typescript
73+
Metadata {
74+
_brokers: Metadata[] // Parent metadatas
75+
_storage: Map // Local storage
76+
77+
get(key) {
78+
// Check local first
79+
// Then check brokers in order
80+
}
81+
82+
set(key, value) {
83+
// Always store locally
84+
return this // For chaining
85+
}
86+
}
87+
```
88+
89+
### 4. Tool
90+
91+
**Responsibility**: Encapsulate executable functions
92+
93+
**Two Constructor Modes**:
94+
1. **Simple**: `new Tool(name, func)` - String input only
95+
2. **Advanced**: `new Tool(name, options)` - Typed parameters
96+
97+
**Properties**:
98+
- `name` - Unique identifier
99+
- `description` - What the tool does
100+
- `parameters` - Schema of expected parameters
101+
- `func` - Executable function
102+
103+
### 5. Rule
104+
105+
**Responsibility**: Define behavioral guidelines
106+
107+
**Types**:
108+
- **Static**: `new Rule(content)` - Always applies
109+
- **Conditional**: `new Rule(content, { when })` - Applies when condition met
110+
111+
### 6. Message
112+
113+
**Responsibility**: Represent conversation messages
114+
115+
**Types** (discriminated union):
116+
- `UserMessage` - User input
117+
- `AssistantMessage` - AI response
118+
- `ToolMessage` - Tool result
119+
- `SystemMessage` - System instruction
120+
121+
### 7. Provider
122+
123+
**Responsibility**: Interface with AI services
124+
125+
**Signature**:
126+
```typescript
127+
type Provider = (ctx: Context) => ChatCompletionResponse | Promise<ChatCompletionResponse>
128+
```
129+
130+
**Contract**:
131+
- Input: Context with messages, tools, metadata
132+
- Output: ChatCompletionResponse (OpenAI-compatible format)
133+
134+
## Data Flow
135+
136+
### Standard Conversation
137+
138+
```
139+
User sends prompt
140+
141+
142+
┌──────────────────┐
143+
Agent.call() │
144+
│ - Add user msg
145+
└────────┬─────────┘
146+
147+
148+
┌──────────────────┐
149+
Provider Loop │──┐
150+
│ - Random select │ │ Failover
151+
│ - Try provider │◄─┘
152+
└────────┬─────────┘
153+
154+
155+
┌─────────────┐
156+
Tool calls?
157+
└──┬──────┬───┘
158+
NoYes
159+
│ ▼
160+
│ ┌────────────────┐
161+
│ │ Execute tools
162+
│ │ in parallel
163+
│ └───────┬────────┘
164+
│ │
165+
│ ▼
166+
│ ┌────────────────┐
167+
│ │ Add tool
168+
│ │ results to ctx
169+
│ └───────┬────────┘
170+
│ │
171+
│ └───┐
172+
│ │
173+
▼ ▼
174+
┌────────────────────┐
175+
Return messages
176+
and success flag
177+
└────────────────────┘
178+
```
179+
180+
### Provider Failover
181+
182+
```
183+
PROVIDERS = [P1, P2, P3]
184+
FALLBACK = []
185+
186+
┌──────────────────────┐
187+
Random select from
188+
PROVIDERS or FALLBACK
189+
└──────────┬───────────┘
190+
191+
192+
┌─────────┐
193+
Success?
194+
└─┬───┬───┘
195+
YesNo
196+
│ │
197+
▼ ▼
198+
┌────┐ ┌────────────────────────┐
199+
Done│ │Move to FALLBACK & retry
200+
└────┘ └────────────────────────┘
201+
```
202+
203+
## Design Patterns
204+
205+
### 1. Broker Pattern (Metadata)
206+
207+
Metadata uses broker pattern for hierarchical value resolution:
208+
209+
```typescript
210+
// Parent metadata
211+
const parent = new Metadata().set('theme', 'light');
212+
213+
// Child metadata with parent as broker
214+
const child = new Metadata(parent).set('color', 'blue');
215+
216+
child.get('theme'); // 'light' (from broker)
217+
child.get('color'); // 'blue' (local)
218+
```
219+
220+
### 2. Virtual Properties (Agent)
221+
222+
Agent virtualizes Context properties for cleaner API:
223+
224+
```typescript
225+
class Agent {
226+
private _context: Context;
227+
228+
get metadata() { return this._context.metadata; }
229+
get rules() { return this._context.rules; }
230+
// etc.
231+
}
232+
```
233+
234+
### 3. Discriminated Unions (Message)
235+
236+
TypeScript discriminated unions for type-safe message handling:
237+
238+
```typescript
239+
type Message =
240+
| { role: 'user'; content: string }
241+
| { role: 'assistant'; content: string }
242+
| { role: 'tool'; tool_id: string; content: string }
243+
| { role: 'system'; content: string }
244+
```
245+
246+
### 4. Promise.all for Tools
247+
248+
Parallel tool execution using Promise.all:
249+
250+
```typescript
251+
const tool_results = await Promise.all(
252+
tool_calls.map(async (call) => {
253+
const tool = tools.find(t => t.name === call.function.name);
254+
return await tool.func(JSON.parse(call.function.arguments));
255+
})
256+
);
257+
```
258+
259+
## Performance Characteristics
260+
261+
- **Context Creation**: O(1) - Shallow copying
262+
- **Metadata Lookup**: O(n) - Checks local then brokers
263+
- **Tool Deduplication**: O(n) - Single pass with Map
264+
- **Message Concatenation**: O(n) - Array concat
265+
- **Provider Failover**: O(p) worst case - p = number of providers
266+
267+
## Scalability
268+
269+
**Vertical Scaling**:
270+
- Single agent instance per conversation
271+
- In-memory state management
272+
- Stateless providers
273+
274+
**Horizontal Scaling**:
275+
- Multiple agent instances across processes
276+
- External state storage (database) if needed
277+
- Load balancing across provider instances
278+
279+
## Security Considerations
280+
281+
1. **API Keys**: Never hardcode, use environment variables
282+
2. **Input Validation**: Validate tool parameters
283+
3. **Rate Limiting**: Implement provider-level throttling
284+
4. **Error Handling**: Catch and sanitize error messages
285+
5. **Logging**: Avoid logging sensitive user data
286+
287+
## Next Steps
288+
289+
- **[Performance](performance.md)** - Optimization techniques
290+
- **[Troubleshooting](troubleshooting.md)** - Common issues
291+
- **[Migration Guide](migration.md)** - Version upgrades
292+
293+
---
294+
295+
**[← Back to Advanced](../index.md#advanced)**

docs/advanced/migration.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1-
# migration.md
1+
# Migration Guide
22

3-
Documentation coming soon. Check back later or contribute at [GitHub](https://github.com/arcaelas/agent).
3+
Guide for upgrading between major versions.
4+
5+
## v3.x to v4.x
6+
7+
### Breaking Changes
8+
9+
1. **Context API** - Simplified hierarchy
10+
2. **Message Types** - Now uses discriminated unions
11+
3. **Tool Constructor** - Supports overloads
12+
13+
### Migration Steps
14+
15+
#### 1. Update Context Creation
16+
17+
**Before (v3)**:
18+
```typescript
19+
const ctx = new Context();
20+
ctx.setMetadata("key", "value");
21+
```
22+
23+
**After (v4)**:
24+
```typescript
25+
const ctx = new Context({
26+
metadata: new Metadata().set("key", "value")
27+
});
28+
```
29+
30+
#### 2. Update Message Creation
31+
32+
**Before (v3)**:
33+
```typescript
34+
const msg = { role: "user", content: "Hello" };
35+
```
36+
37+
**After (v4)**:
38+
```typescript
39+
const msg = new Message({ role: "user", content: "Hello" });
40+
```
41+
42+
#### 3. Update Tool Creation
43+
44+
No changes required - v4 is backward compatible with v3 tools.
45+
46+
## Version History
47+
48+
- **v4.x** - Current, simplified API
49+
- **v3.x** - Previous stable
50+
- **v2.x** - Legacy (deprecated)
51+
- **v1.x** - Original (deprecated)
52+
53+
---
54+
55+
**[← Back to Advanced](../index.md#advanced)**

0 commit comments

Comments
 (0)