Spaces:
Paused
Download docs/ARCHITECTURE.md from manelmoura/donkey: direct link, hf CLI and curl.
- Browser
- Download file 9.69 kB
-
https://huggingface.co/spaces/manelmoura/donkey/resolve/main/docs/ARCHITECTURE.md
- Command line
-
hf download hf://spaces/manelmoura/donkey/docs/ARCHITECTURE.md
-
curl -L -o ARCHITECTURE.md https://huggingface.co/spaces/manelmoura/donkey/resolve/main/docs/ARCHITECTURE.md
Donkey β Architecture
Overview
Donkey follows hexagonal architecture (ports & adapters). The domain core is
pure Nim func β zero side effects, zero I/O, fully unit-testable. The effectful
shell lives in adapters, wired together by the application layer via Chronos async.
For outbound UI specifically, see docs/architecture.md. That document defines the ownership boundary for presentation surfaces, replaceable menus, non-editable fallbacks, and streaming/status state.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β INBOUND ADAPTERS β
β TelegramAdapter ββ β
β DiscordAdapter ββΌβ IdentityResolver β SessionManager β
β [future] ββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β APPLICATION LAYER (Chronos, effectful) β
β startDaemon β OrchestratorAgent (one per user session) β
β βββ MemoryAgent β MemoryPort β MdFileAdapter β
β βββ ToolAgent β ToolPort β McpClientAdapter β
β βββ [workers] β ProviderPort β ZhipuAdapter / β¦ β
β HttpPort β StdHttpAdapter β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β DOMAIN CORE (Nim `func`, zero side effects) β
β buildPrompt Β· parseOnboardingResponse Β· needsOnboarding β
β addUser Β· addAssistant Β· addToolResult Β· trimTo β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β OUTBOUND ADAPTERS β
β ProviderPort β Zhipu / OpenRouter / Anthropic / Gemini β
β ChannelPort β TelegramAdapter β
β MemoryPort β MdFileAdapter (F1) / SQLite MAGMA / Postgres MAGMA β
β HttpPort β StdHttpAdapter (VPS) / ChronosHttp (IoT) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Ports
Ports are Nim concept β structural typing verified at compile time.
Adapters do not declare conformance; the compiler verifies it.
| Port | Key operations | Adapters |
|---|---|---|
ProviderPort |
complete(CompletionRequest) β Result[CompletionResponse, string], caps() |
Zhipu, OpenRouter, Anthropic, Gemini, MiniMax |
ChannelPort |
send(OutboundMessage) β Result[void, string], receive() β Result[seq[InboundMessage], string], ack(string) |
TelegramAdapter |
MemoryPort |
hasProfile, loadContext, saveUserMd, appendMemory, savePreferences |
MdFileAdapter, SQLite MAGMA runtime, Postgres MAGMA runtime |
ToolPort |
listTools() β Result[seq[ToolSchema], string], invoke(ToolCall) β Result[ToolResult, string] |
McpClientAdapter |
HttpPort |
get(url, headers) β Result[HttpResponse, string], post(url, body, headers) β Result[HttpResponse, string] |
StdHttpAdapter, ChronosHttpAdapter (IoT, planned) |
HttpPort is internal to adapters β not exposed to the application layer.
All I/O is Result[T, string]; exceptions never cross port boundaries.
Multi-agent model
One OrchestratorAgent is spawned per active user session. Workers
(MemoryAgent, ToolAgent) run as persistent Chronos coroutines for the
lifetime of the session.
startDaemon (sync loop)
β
ββ channel.receive() β blocking HTTP long-poll (acceptable F4)
β
ββ waitFor dispatchMsg(...) β runs Chronos event loop
β
ββ OrchestratorAgent β asyncSpawn'd on first message
ββ MemoryAgent β asyncSpawn'd by orchestrator
ββ ToolAgent β asyncSpawn'd by orchestrator
Agents communicate exclusively through AsyncQueue[AgentMessage]. No shared
state. Cancel cascades: amkCancel to orchestrator β forwarded to workers.
type AgentMessageKind* = enum amkTask, amkResult, amkError, amkCancel
type AgentMessage* = object
id*: string # correlation id
kind*: AgentMessageKind
taskKind*: string # "userMsg" | "loadCtx" | "saveMem" | "invokeTool"
userId*: string
payload*: string # task input
text*: string # result text
error*: string # error description
Provider tool-call loop
For each incoming user message the orchestrator runs up to MaxToolRoundsOrch
(5) rounds:
1. Load context from MemoryAgent (loadCtx)
2. Build prompt (history + context + tools)
3. Call provider (safeComplete)
4a. No tool calls β record assistant text β done
4b. Tool calls β dispatch each to ToolAgent (invokeTool) β add results to history β goto 2
4c. Empty response β inject re-prompt β goto 2
5. If rounds exhausted β send amkError
HttpPort and IoT target
All provider and channel adapters hold an http: StdHttpAdapter field.
StdHttpAdapter is a zero-field stateless value type wrapping std/httpclient.
To target Raspberry Pi Zero (or any musl/static build):
- Implement
ChronosHttpAdapterinsrc/donkey/adapters/http/chronos_http.nimusingchronos/apps/http/httpclient+nim-bearssl(no OpenSSL dependency):type ChronosHttpAdapter* = object proc get*(a: ChronosHttpAdapter; url: string; headers: seq[HttpHeader]): Result[HttpResponse, string] proc post*(a: ChronosHttpAdapter; url, body: string; headers: seq[HttpHeader]): Result[HttpResponse, string] - Pass
http: newChronosHttpAdapter()to adapter constructors β no other code changes. - Build with musl for a fully static binary:
CC=musl-gcc nim c -d:release --opt:size --passL:"-static" src/donkey.nim strip donkey # Result: ~1MB static binary, zero runtime dependencies
Memory β two cooperating layers
F1: Markdown files in the user paddock.
~/.donkey/paddocks/<uuid>/
USER.md # identity + profile (OpenClaw format, YAML frontmatter)
PREFERENCES.md # TOML: provider, model, language, timezone
MEMORY.md # append-only episodic notes
sessions/<id>.md # per-session summary
Files remain human-editable and useful for inspection/debugging.
F2 (current default): MAGMA graph state backed by SQLite.
- default database path:
~/.donkey/state/magma.sqlite - SQLite base and
sqlite-vecare embedded in the binary - Markdown and MAGMA are kept in sync by the runtime
F2 optional: PostgreSQL-backed MAGMA for larger installs.
- enabled only when
DONKEY_PG_DSNis set - no longer the default installation path
Provider quirks
Each adapter absorbs its own API peculiarities:
| Adapter | Quirk |
|---|---|
OpenRouterAdapter |
Sends HTTP-Referer and X-Title headers |
ZhipuAdapter |
GLM tool-call format; native tools (web_search, code_interpreter) via caps() |
GeminiAdapter |
Role "model" not "assistant"; systemInstruction top-level; auth via ?key= query param; history must alternate user/model turns |
AnthropicAdapter |
system top-level; x-api-key + anthropic-version headers; tool_use/tool_result content blocks |
MiniMaxAdapter |
OpenAI-compatible (M2 series) |
Error handling
All I/O returns Result[T, string]. The ? operator propagates errors up.
try/except is only used inside adapters to convert exceptions to Result.
# Inside adapter β exceptions caught here, never escape
proc complete*(a: ZhipuAdapter; req: CompletionRequest): Result[CompletionResponse, string] =
let resp = ? a.http.post(url, body, headers) # ? propagates HttpPort errors
if resp.status < 200 or resp.status >= 300:
return err("Zhipu API error " & $resp.status)
try:
ok(parseZhipuResponse(parseJson(resp.body))) # JSON parse can throw
except CatchableError as e:
err(e.msg)
# Caller uses valueOr / isOkOr / ? β never catches exceptions
let reply = runner.complete(req).valueOr:
log.error(error)
return
Testing
nimble test # full suite
testament r tests/app/t_orchestrator.nim # single file
testament cat app # category
Chronos async test pattern β avoid GC-safety errors:
# β Wrong: outer var captured by async proc
test "example":
var got: AgentMessage # GC-managed var in test scope
proc doTest() {.async.} =
got = await queue.get() # ERROR: not GC-safe
waitFor doTest()
# β Correct: return value through Future, pass data as parameters
test "example":
proc doTest(dir: string): Future[AgentMessage] {.async.} =
let mem = MdFileAdapter(paddocksDir: dir) # created inside async proc
return await queue.get()
let got = waitFor doTest(tmpDir) # assertions outside async
check got.kind == amkResult
See CLAUDE.md for the full list of Chronos async macro restrictions.