How mybricks/plugin-ai works
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: agent.ts Agent runtime · src/index.ts Request adapter · index.tsx Plugin entry · code-agent/index.ts CodeAgent
Components 23
Agent SDK
Isomorphic orchestration and capability layer.
- SDK exports
packages/agent/src/index.ts - Agent runtime · Streams model turns, executes tools, and completes requests.
packages/agent/src/agent.ts - CodeAgent · Adds coding prompts, filesystem tools, skills, and sandbox support.
packages/agent/src/code-agent/index.ts - Agent events
packages/agent/src/events.ts - Code tools · File, search, edit, shell, and skill capabilities.
packages/agent/src/code-agent/tools/index.ts - IndexedDB sandbox · Browser sandbox implementation for code operations.
packages/agent/src/code-agent/idb-sandbox.ts - Web fetch tool
packages/agent/src/tools/web-fetch/index.ts - History manager · Manages durable conversation history and context.
packages/agent/src/history/manager.ts - Mode manager · Selects build and plan-style execution modes.
packages/agent/src/mode-manager.ts - Sub-agents · Runs delegated work in isolated contexts.
packages/agent/src/sub-agent.ts
Model Transport
Provider-agnostic streaming request boundary.
- Request adapter · Configures direct, custom, and infrastructure-backed model requests.
packages/request/src/index.ts - Provider config
packages/request/src/providers.ts - SSE parser
packages/request/src/sse-parser.ts
MyBricks Plugin
Host-integrated agent application and chat UI.
- Plugin entry
packages/plugin/src/index.tsx - Agent presets · Defines MyBricks product-specific agent roles.
packages/plugin/src/preset/index.ts - Plugin sandbox setup · Connects host and project state to agent abstractions.
packages/plugin/src/sandbox/setup.ts - Chat UI · Renders sessions, messages, plans, attachments, and settings.
packages/plugin/src/ui/chat/index.ts - Session hook
packages/plugin/src/ui/chat/use-session.ts - Tool renderers · 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
packages/playground/src/main.tsx - Playground app
packages/playground/src/App.tsx - Playground agent · Builds agents with in-memory filesystem, mock history, and scripted requests.
packages/playground/src/lib/use-playground-agent.ts
Other components
- LLM endpoints · Compatible OpenAI, Anthropic, OpenRouter, private, or local gateways.
Connections 26
- 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