Documentation
¶
Overview ¶
Package main is a reference HITL (Human-In-The-Loop) server built on GopherAgent. It bridges the synchronous ConfirmHITL callback with an async HTTP approval workflow so a web client can approve / deny risky tool calls in real time.
The flow:
- Client POSTs a prompt to /chat. The server starts an AgentLoop and streams StreamEvents back as SSE.
- When the agent wants to call a tool that declares RequiresConfirmation() == true, the loop invokes the ConfirmHITL hook. The server enqueues a pending approval (keyed by a generated ID), emits an "approval_required" SSE event containing that ID, and blocks the tool call until a decision arrives.
- The client inspects the pending call and POSTs a decision to /approve with {"id": "...", "approved": true|false}. The server delivers the boolean to the waiting hook and the agent resumes.
This is a single-process reference — the pending map lives in memory, so restarting the server cancels in-flight approvals. For production, persist pending approvals to a store that survives restarts and fan them out to every replica (Redis pub/sub, a DB row with a TTL, etc.).
Run:
OPENAI_API_KEY=sk-... go run ./examples/hitl_server
Demo with curl (two terminals):
# terminal 1 — start the chat
curl -N -X POST http://localhost:8080/chat \
-H "Content-Type: application/json" \
-d '{"session_id":"demo","message":"Delete /tmp/foo using the shell tool."}'
# watch for: event: approval_required
# data: {"id":"appr-...","tool":"shell","args":{...}}
# terminal 2 — approve the call
curl -X POST http://localhost:8080/approve \
-H "Content-Type: application/json" \
-d '{"id":"appr-...","approved":true}'