Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xranx OpenContext Agent Streaming

From Leeroopedia
Knowledge Sources
Domains AI_Agents, Real_Time_Systems
Last Updated 2026-10-24 12:00 GMT

Overview

A pattern for spawning external coding agent processes (Codex, Claude, OpenCode), streaming their responses to a frontend UI via Tauri events, handling tool call permissions interactively, and persisting session state across restarts.

Description

Agent streaming addresses the challenge of integrating external CLI-based coding agents into a desktop application. These agents (Codex via MCP, Claude via ACP, OpenCode via ACP) run as separate processes that communicate via JSON-RPC, produce streaming output, and require interactive permission decisions for tool calls.

The pattern involves:

  • Process spawning: Each agent type is spawned as a child process with its own communication protocol (MCP for Codex, ACP for Claude/OpenCode). A background thread reads the process stdout line by line.
  • Event streaming: The backend emits Tauri events (`agent-stream-{request_id}`) carrying structured `AgentStreamEvent` payloads. The frontend subscribes to these events and updates React state incrementally.
  • Stream event types: Events carry content (text tokens), reasoning (thought process), tool calls, permission requests, status updates, model lists, and completion signals.
  • Permission handling: When an agent requests permission for a tool call (e.g., applying a patch), the backend emits a permission event; the frontend shows a dialog; the user's decision is sent back via a Tauri command (`codex_permission_response` or `acp_permission_response`).
  • Session persistence: Session state (sessions array, active session ID, next index) is persisted to `agent-sessions.json` in the app data directory, with localStorage as a fallback.
  • Session termination: Killing a session stops the RPC stream and cleans up the agent process.

Usage

Use this principle when building a desktop application that embeds external AI coding agents. The streaming pattern ensures responsive UI updates while the agent works, and the permission system ensures user control over agent actions.

Theoretical Basis

The streaming architecture follows a producer-consumer with interactive backpressure model:

# Abstract algorithm (NOT real implementation)
def agent_stream_loop(process, app_handle, request_id):
    for line in process.stdout:
        event = parse_json_rpc(line)

        if event is content:
            emit_event(app_handle, request_id, AgentStreamEvent(
                content=event.text
            ))
        elif event is reasoning:
            emit_event(app_handle, request_id, AgentStreamEvent(
                reasoning=event.text
            ))
        elif event is tool_call:
            emit_event(app_handle, request_id, AgentStreamEvent(
                tool=event.tool_info
            ))
        elif event is permission_request:
            emit_event(app_handle, request_id, AgentStreamEvent(
                permission=event.permission_info
            ))
            # Wait for user response via separate command
        elif event is done:
            emit_event(app_handle, request_id, AgentStreamEvent(
                done=True
            ))
            break

The AgentStreamEvent structure:

struct AgentStreamEvent {
    content: Option<String>,      // Text content tokens
    done: Option<bool>,            // Completion signal
    error: Option<String>,         // Error messages
    status: Option<String>,        // Status updates
    reasoning: Option<String>,     // Reasoning/thinking tokens
    permission: Option<Value>,    // Permission request data
    tool: Option<Value>,           // Tool call data
    models: Option<Value>,         // Available model list
}

Practical Guide

  • Event naming: Events use the pattern `agent-stream-{request_id}` to allow concurrent agent sessions.
  • Permission flow: The backend emits a permission event, the frontend renders a dialog, and the user's decision is sent via `codex_permission_response` or `acp_permission_response`.
  • Session state: The frontend uses `useAgentSessions` hook which persists to both Tauri backend (`agent-sessions.json`) and localStorage (fallback).
  • Multi-agent support: Codex uses MCP protocol, Claude and OpenCode use ACP protocol; the streaming abstraction handles both.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment