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

Implementation:23blocks OS Ai maestro Streaming Runtime

From Leeroopedia
Knowledge Sources
Domains Streaming, Agent_SDK
Last Updated 2026-10-26 18:00 GMT

Overview

Concrete tool for per-agent Claude Agent SDK streaming sessions (stream-json mode) with persistent context, permission callbacks, and buffered replay, provided by AI Maestro's `lib/streaming-runtime.mjs`.

Description

The streaming-runtime module manages one persistent SDK-driven `claude` session per agent, kept alive across client (re)connects. It drives the Agent SDK's streaming-input `query()` for persistent context across turns, `--resume` of the agent's latest conversation (continuity with terminal mode), and `canUseTool` permission callbacks (rendered as cards in the browser). The WebSocket layer (`server.mjs /stream-chat`) attaches clients, pushes user turns, relays permission decisions, and receives buffered + live events. Sessions are backed by `globalThis._streamSessions` so the raw-ESM copy and the Next-bundled copy share one map. Sessions auto-kill 15 minutes after the last client leaves. Buffer cap is 5000 events; permission auto-denies after 5 minutes.

Usage

Call `getOrCreateSession(agentKey, opts)` to get or create a session. Call `session.addClient(ws)` / `session.removeClient(ws)` to manage subscribers. Call `session.pushUserTurn(text)` to send a turn. The SDK `query()` runs in the background.

Code Reference

Source Location

  • Repository: ai-maestro
  • File: lib/streaming-runtime.mjs
  • Lines: L1-L289

Signature

export class Session {
  constructor(agentKey, opts)
  addClient(ws)
  removeClient(ws)
  pushUserTurn(text)
  resolvePermission(permissionId, decision)
}

export function getOrCreateSession(agentKey, opts)
export function getSession(agentKey)

Import

import { getOrCreateSession, getSession } from '@/lib/streaming-runtime.mjs'

I/O Contract

Name Type Description
agentKey string Unique key for the agent's streaming session
Session.clients Set<WebSocket> Connected subscribers
Session.buffer event[] Buffered events (max 5000)

Usage Examples

import { getOrCreateSession } from '@/lib/streaming-runtime.mjs'

const session = getOrCreateSession(agentKey, { cwd, agentId, agentName })
session.addClient(ws)
session.pushUserTurn('Fix the login bug')

Related Pages

Implements Principle

Requires Environment

Page Connections

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