# mybricks/plugin-ai architecture

> Architecture diagram of the GitHub repository mybricks/plugin-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mybricks/plugin-ai
- Repository: https://github.com/mybricks/plugin-ai

## Overview

#### Purpose and Package Boundaries
This monorepo provides an isomorphic TypeScript AI-agent SDK plus an embeddable MyBricks-oriented chat/plugin layer. The core package runs a streaming LLM/tool loop in browser or server environments; consumers supply model providers, prompts, tools, and storage/filesystem implementations. Primary exports are rooted at `packages/agent/src/index.ts`; the plugin entry is `packages/plugin/src/index.tsx`; `index.tsx` is the repository-level entry.

#### Core Agent Execution Loop
`Agent` is the low-level orchestration runtime, responsible for iterative model requests, tool calls, observations, retries, event emission, and turn completion. `CodeAgent` specializes it with coding prompts, filesystem-aware tools, skills, and sandbox integration.
- `packages/agent/src/agent.ts`
- `packages/agent/src/code-agent/index.ts`
- `packages/agent/src/events.ts`

Control flow: caller invokes an agent request → the selected LLM streams content/tool calls → tools execute → results are appended to agent context → the loop continues until completion or an error/limit boundary.

#### Model Transport and Streaming Boundary
The request package is the model-provider adapter boundary. It supports provider/environment configuration, direct and custom requests, infrastructure integration, and SSE parsing. The agent remains model-agnostic; callers configure compatible OpenAI/Anthropic endpoints or provide a custom streaming request function.
- `packages/request/src/index.ts`
- `packages/request/src/providers.ts`
- `packages/request/src/sse-parser.ts`

External services are arbitrary LLM endpoints, including OpenRouter-compatible APIs, OpenAI/Anthropic-compatible gateways, and private/local gateways.

#### Tools, Code Operations, and Sandboxing
Tools are injected capabilities with validation and execution contracts. CodeAgent bundles file reads/writes/edits/deletes, grep/glob, multi-file operations, shell execution, skills, and optional web fetch. Filesystem access is abstracted through a sandbox; IndexedDB supplies the browser implementation, while server consumers provide their own sandbox backed by Node or other storage.
- `packages/agent/src/code-agent/tools/index.ts`
- `packages/agent/src/code-agent/idb-sandbox.ts`
- `packages/agent/src/tools/web-fetch/index.ts`

This is the principal security and runtime boundary: model output does not access files, commands, or the network except through configured tools and sandbox implementations.

#### Context, History, and Execution Policy
The runtime manages durable conversation history, context/content limits, selectable modes, handoffs, sub-agents, masking, and retry behavior. History has HTTP and IndexedDB implementations; mode management supports build versus plan-style execution. Sub-agents isolate delegated work into separate contexts.
- `packages/agent/src/history/manager.ts`
- `packages/agent/src/mode-manager.ts`
- `packages/agent/src/sub-agent.ts`

#### MyBricks Plugin Composition
The plugin assembles the SDK into a host-integrated agent application: preset prompts/workflows, context KV/queue state, sandbox setup, project initialization/status tools, default skills, and a React chat UI. Presets define product-specific agent roles such as copilot, full-stack app, and agent app.
- `packages/plugin/src/index.tsx`
- `packages/plugin/src/preset/index.ts`
- `packages/plugin/src/sandbox/setup.ts`

The plugin is intended for embedding into a MyBricks host and bridges host/project state into the generic agent abstractions.

#### UI and Interaction Rendering
The React UI renders sessions, messages, plans, attachments, model selection, settings, and structured tool execution views. Tool-render registration maps emitted tool events/results to human-readable components, keeping agent execution separate from presentation.
- `packages/plugin/src/ui/chat/index.ts`
- `packages/plugin/src/ui/chat/use-session.ts`
- `packages/plugin/src/ui/chat/messages/tool-renders/register.tsx`

#### Development Harness
The playground is a Vite/React integration harness for exercising agent behaviors with in-memory filesystem, mock history, scripted requests, and UI cases. It is the practical browser entry point for validating SDK and plugin-facing behavior.
- `packages/playground/src/main.tsx`
- `packages/playground/src/App.tsx`
- `packages/playground/src/lib/use-playground-agent.ts`

Main entry points: [`packages/agent/src/agent.ts`](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/agent.ts) (Agent runtime), [`packages/request/src/index.ts`](https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/index.ts) (Request adapter), [`packages/plugin/src/index.tsx`](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/index.tsx) (Plugin entry), [`packages/agent/src/code-agent/index.ts`](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/index.ts) (CodeAgent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agent["Agent SDK"]
  node_sdk_exports["SDK exports<br/>package entry<br/>[index.ts]"]
  node_agent_runtime{{"Agent runtime<br/>orchestration loop<br/>[agent.ts]"}}
  node_code_agent["CodeAgent<br/>specialized agent<br/>[index.ts]"]
  node_agent_events["Agent events<br/>event contract<br/>[events.ts]"]
  node_code_tools["Code tools<br/>tool bundle<br/>[index.ts]"]
  node_sandbox[("IndexedDB sandbox<br/>browser filesystem<br/>[idb-sandbox.ts]")]
  node_web_fetch["Web fetch tool<br/>optional tool<br/>[index.ts]"]
  node_history[("History manager<br/>context persistence<br/>[manager.ts]")]
  node_mode_policy["Mode manager<br/>execution policy<br/>[mode-manager.ts]"]
  node_sub_agents["Sub-agents<br/>delegation runtime<br/>[sub-agent.ts]"]
end

subgraph group_transport["Model Transport"]
  node_request_api{{"Request adapter<br/>model transport API<br/>[index.ts]"}}
  node_providers["Provider config<br/>provider adapters<br/>[providers.ts]"]
  node_sse_parser["SSE parser<br/>stream parser<br/>[sse-parser.ts]"]
end

subgraph group_plugin["MyBricks Plugin"]
  node_plugin_entry["Plugin entry<br/>package entry<br/>[index.tsx]"]
  node_presets["Agent presets<br/>workflow presets<br/>[index.ts]"]
  node_plugin_sandbox["Plugin sandbox setup<br/>host bridge<br/>[setup.ts]"]
  node_chat_ui["Chat UI<br/>React interface<br/>[index.ts]"]
  node_session_hook["Session hook<br/>UI state<br/>[use-session.ts]"]
  node_tool_renderers["Tool renderers<br/>event presentation<br/>[register.tsx]"]
end

subgraph group_playground["Development Harness"]
  node_playground_entry["Playground entry<br/>Vite entry<br/>[main.tsx]"]
  node_playground_app["Playground app<br/>integration UI<br/>[App.tsx]"]
  node_playground_agent["Playground agent<br/>test integration"]
end

node_llm_endpoints(("LLM endpoints<br/>external service"))

node_sdk_exports -->|"exports"| node_agent_runtime
node_agent_runtime -->|"streams requests through"| node_request_api
node_request_api -->|"uses"| node_providers
node_request_api -->|"parses streams with"| node_sse_parser
node_request_api -->|"calls"| node_llm_endpoints
node_agent_runtime -->|"executes"| node_code_tools
node_code_agent -->|"specializes"| node_agent_runtime
node_code_agent -->|"bundles"| node_code_tools
node_code_tools -->|"accesses files through"| node_sandbox
node_code_tools -.->|"optionally includes"| node_web_fetch
node_agent_runtime -->|"persists context in"| node_history
node_agent_runtime -->|"applies"| node_mode_policy
node_agent_runtime -->|"delegates to"| node_sub_agents
node_agent_runtime -->|"emits"| node_agent_events
node_plugin_entry -->|"assembles"| node_presets
node_plugin_entry -->|"configures"| node_plugin_sandbox
node_plugin_entry -->|"integrates"| node_code_agent
node_plugin_entry -->|"mounts"| node_chat_ui
node_chat_ui -->|"uses"| node_session_hook
node_chat_ui -->|"renders tool events with"| node_tool_renderers
node_tool_renderers -->|"consumes"| node_agent_events
node_plugin_sandbox -->|"sets up"| node_sandbox
node_playground_entry -->|"mounts"| node_playground_app
node_playground_app -->|"exercises"| node_playground_agent
node_playground_agent -->|"constructs"| node_code_agent
node_playground_agent -.->|"uses scripted requests"| node_request_api

click node_sdk_exports "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/index.ts"
click node_agent_runtime "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/agent.ts"
click node_code_agent "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/index.ts"
click node_agent_events "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/events.ts"
click node_code_tools "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/tools/index.ts"
click node_sandbox "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/idb-sandbox.ts"
click node_web_fetch "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/tools/web-fetch/index.ts"
click node_history "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/history/manager.ts"
click node_mode_policy "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/mode-manager.ts"
click node_sub_agents "https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/sub-agent.ts"
click node_request_api "https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/index.ts"
click node_providers "https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/providers.ts"
click node_sse_parser "https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/sse-parser.ts"
click node_plugin_entry "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/index.tsx"
click node_presets "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/preset/index.ts"
click node_plugin_sandbox "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/sandbox/setup.ts"
click node_chat_ui "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/index.ts"
click node_session_hook "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/use-session.ts"
click node_tool_renderers "https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/messages/tool-renders/register.tsx"
click node_playground_entry "https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/main.tsx"
click node_playground_app "https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/App.tsx"
click node_playground_agent "https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/lib/use-playground-agent.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sdk_exports,node_agent_runtime,node_code_agent,node_agent_events,node_code_tools,node_sandbox,node_web_fetch,node_history,node_mode_policy,node_sub_agents toneBlue
class node_request_api,node_providers,node_sse_parser toneAmber
class node_plugin_entry,node_presets,node_plugin_sandbox,node_chat_ui,node_session_hook,node_tool_renderers toneMint
class node_playground_entry,node_playground_app,node_playground_agent toneRose
class node_llm_endpoints toneNeutral
```

## Components

### Agent SDK

Isomorphic orchestration and capability layer.

- [SDK exports](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/index.ts): package entry · `packages/agent/src/index.ts`
- [Agent runtime](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/agent.ts): orchestration loop · Streams model turns, executes tools, and completes requests. · `packages/agent/src/agent.ts`
- [CodeAgent](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/index.ts): specialized agent · Adds coding prompts, filesystem tools, skills, and sandbox support. · `packages/agent/src/code-agent/index.ts`
- [Agent events](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/events.ts): event contract · `packages/agent/src/events.ts`
- [Code tools](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/tools/index.ts): tool bundle · File, search, edit, shell, and skill capabilities. · `packages/agent/src/code-agent/tools/index.ts`
- [IndexedDB sandbox](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/code-agent/idb-sandbox.ts): browser filesystem · Browser sandbox implementation for code operations. · `packages/agent/src/code-agent/idb-sandbox.ts`
- [Web fetch tool](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/tools/web-fetch/index.ts): optional tool · `packages/agent/src/tools/web-fetch/index.ts`
- [History manager](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/history/manager.ts): context persistence · Manages durable conversation history and context. · `packages/agent/src/history/manager.ts`
- [Mode manager](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/mode-manager.ts): execution policy · Selects build and plan-style execution modes. · `packages/agent/src/mode-manager.ts`
- [Sub-agents](https://github.com/mybricks/plugin-ai/blob/main/packages/agent/src/sub-agent.ts): delegation runtime · Runs delegated work in isolated contexts. · `packages/agent/src/sub-agent.ts`

### Model Transport

Provider-agnostic streaming request boundary.

- [Request adapter](https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/index.ts): model transport API · Configures direct, custom, and infrastructure-backed model requests. · `packages/request/src/index.ts`
- [Provider config](https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/providers.ts): provider adapters · `packages/request/src/providers.ts`
- [SSE parser](https://github.com/mybricks/plugin-ai/blob/main/packages/request/src/sse-parser.ts): stream parser · `packages/request/src/sse-parser.ts`

### MyBricks Plugin

Host-integrated agent application and chat UI.

- [Plugin entry](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/index.tsx): package entry · `packages/plugin/src/index.tsx`
- [Agent presets](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/preset/index.ts): workflow presets · Defines MyBricks product-specific agent roles. · `packages/plugin/src/preset/index.ts`
- [Plugin sandbox setup](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/sandbox/setup.ts): host bridge · Connects host and project state to agent abstractions. · `packages/plugin/src/sandbox/setup.ts`
- [Chat UI](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/index.ts): React interface · Renders sessions, messages, plans, attachments, and settings. · `packages/plugin/src/ui/chat/index.ts`
- [Session hook](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/use-session.ts): UI state · `packages/plugin/src/ui/chat/use-session.ts`
- [Tool renderers](https://github.com/mybricks/plugin-ai/blob/main/packages/plugin/src/ui/chat/messages/tool-renders/register.tsx): event presentation · Maps tool events and results to structured UI components. · `packages/plugin/src/ui/chat/messages/tool-renders/register.tsx`

### Development Harness

Browser integration environment.

- [Playground entry](https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/main.tsx): Vite entry · `packages/playground/src/main.tsx`
- [Playground app](https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/App.tsx): integration UI · `packages/playground/src/App.tsx`
- [Playground agent](https://github.com/mybricks/plugin-ai/blob/main/packages/playground/src/lib/use-playground-agent.ts): test integration · Builds agents with in-memory filesystem, mock history, and scripted requests. · `packages/playground/src/lib/use-playground-agent.ts`

### Other components

- **LLM endpoints**: external service · Compatible OpenAI, Anthropic, OpenRouter, private, or local gateways.

## Connections

- SDK exports → Agent runtime: exports
- Agent runtime → Request adapter: streams requests through
- Request adapter → Provider config: uses
- Request adapter → SSE parser: parses streams with
- Request adapter → LLM endpoints: calls
- Agent runtime → Code tools: executes
- CodeAgent → Agent runtime: specializes
- CodeAgent → Code tools: bundles
- Code tools → IndexedDB sandbox: accesses files through
- Code tools → Web fetch tool: optionally includes
- Agent runtime → History manager: persists context in
- Agent runtime → Mode manager: applies
- Agent runtime → Sub-agents: delegates to
- Agent runtime → Agent events: emits
- Plugin entry → Agent presets: assembles
- Plugin entry → Plugin sandbox setup: configures
- Plugin entry → CodeAgent: integrates
- Plugin entry → Chat UI: mounts
- Chat UI → Session hook: uses
- Chat UI → Tool renderers: renders tool events with
- Tool renderers → Agent events: consumes
- Plugin sandbox setup → IndexedDB sandbox: sets up
- Playground entry → Playground app: mounts
- Playground app → Playground agent: exercises
- Playground agent → CodeAgent: constructs
- Playground agent → Request adapter: uses scripted requests
