hitl_server

command
v0.39.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

HITL Server Example

Minimal reference for Human-In-The-Loop tool approvals over HTTP.

The AgentLoop's ConfirmHITL callback is synchronous — it blocks the tool call until a boolean decision arrives. This example shows how to bridge that synchronous signature to an asynchronous HTTP approval endpoint so a web UI can intervene in real time.

Run

OPENAI_API_KEY=sk-... go run ./examples/hitl_server

Server listens on :8080:

  • POST /chat — start a chat turn, stream SSE events back
  • POST /approve — deliver an approval decision
  • GET /healthz

Demo

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."}'

When the agent wants to call the shell tool you will see:

event: approval_required
data: {"id":"appr-1713379200123456789","tool":"shell","args":"{\"command\":\"rm -rf /tmp/foo\"}"}

Terminal 2 — approve (or deny):

curl -X POST http://localhost:8080/approve \
  -H "Content-Type: application/json" \
  -d '{"id":"appr-1713379200123456789","approved":true}'

The agent resumes and streams the rest of the response.

How it works

approvalBroker.Await is called from inside ConfirmHITL. It:

  1. Generates a stable ID and stores a pendingApproval (with a buffered decision channel) keyed by that ID.
  2. Fires the approval_required SSE event so the client learns the ID.
  3. Blocks on the decision channel until /approve arrives (or the request context is cancelled, in which case the call is auto-denied).

/approve does a map lookup and sends the boolean down the channel — releasing the agent goroutine.

Production notes

This is a single-process reference. For real deployments:

  • Persist pending approvals (DB row with a TTL, Redis hash, etc.) so a restart doesn't strand the agent.
  • Route decisions across replicas — if /chat lands on replica A and /approve lands on replica B, use pub/sub (Redis / NATS) to deliver the verdict. Keep the broker stateful only at the node that owns the pending call.
  • Add authn/authz on /approve — otherwise anyone who guesses an ID can approve tool calls.
  • Add a timeout — deny automatically after N minutes rather than blocking indefinitely.
  • Pass sessionKey into ConfirmHITL — this example broadcasts the approval_required event to every open SSE channel because ConfirmFunc's current signature does not carry sessionKey. A multi-tenant server must either extend the signature or route pending approvals through a session-scoped channel before entering the loop.

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:

  1. Client POSTs a prompt to /chat. The server starts an AgentLoop and streams StreamEvents back as SSE.
  2. 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.
  3. 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}'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL