How all-hands-ai/openhands works
Project Type & Purpose
• OpenHands is a full‑stack AI‑driven software development platform. It includes:
• A Python core (CLI, controller, agents, runtime, server, storage, events, etc.)
• A TypeScript/React frontend served by the Python server
• Dockerized runtimes for sandboxed code execution
Focus: openhands/events Subsystem
The openhands/events package implements a domain‑specific event system for:
• Capturing every “action” an AI agent takes (e.g., run bash, browse web, file edits)
• Recording every “observation” the agent receives (e.g., command output, file contents)
• Serializing/deserializing events for storage, replay, and streaming
• Feeding events into the memory and analysis layers
Main Components (to include
A. Event Model
• events/event.py – base Event class (timestamp, metadata)
• events/action/*.py – Action subclasses (agent, browse, commands, files, mcp, message)
• events/observation/*.py – Observation subclasses (agent, browse, commands, empty, error, files, mcp, reject, success)
B. Event Storage & Streaming
• events/event_store.py – synchronous in‐memory event store API (append, query)
• events/async_event_store_wrapper.py – async façade for non‐blocking writes/reads
• events/stream.py – optional real‐time stream interface for subscribers
C. Serialization Layer
• events/serialization/event.py – JSON (de)serialization of the base event envelope
• events/serialization/action.py – (de)serialize Action objects
• events/serialization/observation.py – (de)serialize Observation objects
• events/serialization/utils.py – shared helpers (e.g., mapping class ↔ type string)
D. Tooling & Utilities
• events/tool.py – definitions of external tooling invoked by events
• events/utils.py – common utilities (e.g., event validation, type guards)
Relationships & Data Flow
1) Agent Controller (in openhands/controller/agent_controller.py)
– invokes an Action (e.g., “run bash”)
– constructs Action event object
2) Event Dispatcher
– passes the Action to event_store.append(action)
– async_event_store_wrapper may enqueue this call
– subscribers (memory condenser, replay engine) receive the action via stream
3) Runtime or external tool executes the action
– captures output, errors → constructs corresponding Observation
– again, event_store.append(observation)
4) Serializer
– when persisting to disk or sending over HTTP, each event goes through serialization/*
– JSON payload stored in conversation log or a database
5) Consumers
– Memory condenser pipeline (openhands/memory) reads event stream to build context
– Replayer / testing harness uses stream to replay events for debugging
– Monitoring / logging layers subscribe to event stream for metrics & audits
Architectural Patterns & Principles
• Event Sourcing: every state change (action/observation) is an immutable event
• Publish‑Subscribe: real-time stream allows loose coupling between producers & consumers
• Serialization Abstraction: separate modules for action vs. observation keep (de)serialization clean
• Async Wrapping: non‑blocking I/O via async_event_store_wrapper for high throughput