Spaces:
Paused
Paused
File size: 6,140 Bytes
74c37a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | # Basic Concepts
Understanding Donkey's architecture
## The Big Picture
Donkey is an AI agent daemon that connects messaging channels to LLM providers. It handles the full lifecycle of a conversation: receiving messages, processing them through AI, executing tools, and sending responses.
## Hexagonal Architecture
Donkey uses hexagonal (ports & adapters) architecture to keep the core business logic isolated from infrastructure:
```
βββββββββββββββββββββββββββββββββββββββββββ
β Channels β
β Telegram, Discord, Slack, ... β
ββββββββββββββββββββ¬βββββββββββββββββββββββ
β InboundMessage
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β Ports β
β ChannelPort, ProviderPort, HookPort β
ββββββββββββββββββββ¬βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β Core β
β Session, Orchestrator, Tool Loop β
ββββββββββββββββββββ¬βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β Adapters β
β LLM Providers, Memory, Tools β
βββββββββββββββββββββββββββββββββββββββββββ
```
### Why Hexagonal?
- **Testable**: Core logic has no external dependencies
- **Flexible**: Swap adapters without changing core
- **Maintainable**: Clear separation of concerns
## Key Concepts
### Session
A session represents a conversation with a user. Each user can have multiple sessions, one per channel.
```nim
type
SessionId* = distinct string
Session* = object
id*: SessionId
userId*: UserId
channel*: ChannelKind
messages*: seq[ChatMessage]
createdAt*: DateTime
```
### Orchestrator
The orchestrator is the heart of message processing:
1. Receives inbound message
2. Routes to session
3. Executes pre-LLM hooks
4. Calls LLM with context
5. Executes tool calls if needed
6. Executes post-LLM hooks
7. Sends response
### Ports (Interfaces)
Ports define how the core interacts with the outside world:
| Port | Purpose |
|------|---------|
| `ChannelPort` | Send/receive messages |
| `ProviderPort` | Call LLM APIs |
| `HookPort` | Intercept message flow |
| `MemoryPort` | Persist data |
| `ToolPort` | Execute tools |
### Adapters
Adapters implement ports for specific technologies:
| Adapter | Implements | Technology |
|---------|------------|------------|
| `TelegramAdapter` | ChannelPort | Telegram Bot API |
| `DiscordAdapter` | ChannelPort | Discord WebSocket |
| `AnthropicAdapter` | ProviderPort | Anthropic API |
| `MdFileAdapter` | MemoryPort | Markdown files |
## Message Flow
When a user sends a message:
```
1. Channel receives message (Telegram, Discord, etc.)
β
βΌ
2. Session ingress - create new session or reuse existing
β
βΌ
3. Orchestrator receives message
β
βΌ
4. Execute hpPreLlm hooks (context injection, validation...)
β
βΌ
5. Build prompt with history and context
β
βΌ
6. Call LLM (Anthropic, OpenAI, etc.)
β
βΌ
7. Execute hpPostLlm hooks (response modification...)
β
βΌ
8. Execute tool calls if needed (hpPreTool β tool β hpPostTool)
β
βΌ
9. Execute hpPostResponse hooks
β
βΌ
10. Send response via channel
```
## Hooks System
Hooks provide middleware-style interception:
```nim
type
HookPhase* = enum
hpPreLlm # Before LLM call
hpPostLlm # After LLM response
hpPreTool # Before tool execution
hpPostTool # After tool result
hpPostResponse # Before sending response
```
### Example: Spam Filter
```nim
proc spamFilter*(ctx: HookContext): HookResult =
if "buy now" in ctx.message.toLower():
return newHookResultRejected(ctx, "Spam detected")
return newHookResultContinue(ctx)
```
## Memory System
Donkey uses two cooperating memory layers:
### F1: Workspace Memory (Markdown)
Each user has a paddock directory:
```
~/.donkey/paddocks/<user_id>/
βββ USER.md # Identity
βββ MEMORY.md # Memories
βββ PREFERENCES.md # Settings
βββ NOTES.md # Notes
```
### F2: Structured Memory (MAGMA)
By default this is SQLite-backed MAGMA stored under `~/.donkey/state/`.
PostgreSQL remains optional for larger installs, but it is no longer the
default path.
## Commands
Slash commands are parsed and routed:
| Route | Description | Example |
|-------|-------------|---------|
| `crLocal` | Handled locally | `/help`, `/start` |
| `crSessionTask` | Session operation | `/reset`, `/compact` |
| `crUi` | Interactive UI | `/models`, `/think` |
| `crStatus` | Status query | `/status` |
| `crPing` | Ping | `/ping` |
## Error Handling
Donkey uses `Result[T, E]` instead of exceptions:
```nim
type
MyError* = object
kind*: ErrorKind
message*: string
proc fetchData*(url: string): FetchResult =
if url.len == 0:
return err(MyError(kind: ErrEmptyUrl, message: "URL required"))
# ... success path
return ok(data)
```
## Next Steps
- [Commands System](topics/commands.md)
- [Hooks System](topics/hooks.md)
- [Memory System](topics/memory.md)
- [Configuration Reference](reference/configuration.md)
|