Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions go-opencode/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Go-OpenCode Architecture

This document describes the package structure and dependencies of the Go OpenCode implementation.

## Package Dependency Graph

```
┌─────────────┐
│ server │ ◄── HTTP API Layer
└──────┬──────┘
┌────────────────────────────────┼────────────────────────────────┐
│ │ │
▼ ▼ ▼
┌───────────┐ ┌───────────┐ ┌───────────┐
│ session │ │ mcp │ │ executor │
└─────┬─────┘ └─────┬─────┘ └─────┬─────┘
│ │ │
│ ┌─────────────────────┴────────────────────────────────┤
│ │ │
▼ ▼ ▼
┌───────────────────┐ ┌───────────┐
│ tool │ ◄────────────────────────────────────── │ provider │
└─────────┬─────────┘ └───────────┘
┌─────────┴─────────┬────────────────┐
│ │ │
▼ ▼ ▼
┌─────────┐ ┌───────────┐ ┌───────────┐
│ agent │ │ storage │ │ event │
└────┬────┘ └───────────┘ └───────────┘
┌────────────┐
│ permission │
└────────────┘
```

## Package Descriptions

### Command Layer (`cmd/`)

| Package | Description |
|---------|-------------|
| `cmd/opencode` | Main CLI application entry point. Provides `run` and `serve` commands. |
| `cmd/calculator-mcp` | Example MCP server implementation for testing. |

### Public API (`pkg/`)

| Package | Description |
|---------|-------------|
| `pkg/types` | Shared type definitions used across the codebase (Session, Message, Part, etc.). |
| `pkg/mcpserver` | MCP Server implementation utilities for building MCP servers. |

### Internal Packages (`internal/`)

#### Foundation Layer (No internal dependencies)

| Package | Description |
|---------|-------------|
| `event` | Type-safe pub/sub event system. Enables decoupled communication between components for session events, message updates, and tool execution notifications. |
| `logging` | Structured logging using zerolog. Provides consistent logging across all packages. |
| `storage` | File-based JSON storage matching TypeScript implementation. Provides persistent storage for sessions, messages, and parts. |
| `formatter` | Code formatting integration. Supports automatic code formatting via external tools. |
| `lsp` | Language Server Protocol client. Provides code intelligence and symbol search capabilities. |
| `sharing` | Session sharing and collaboration features. |
| `command` | Flexible command execution system. Supports templated commands with variable substitution from configuration or markdown files. |

#### Configuration Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `config` | Configuration loading, merging, and path management. Handles hierarchical loading from multiple sources (global, project, environment) with TypeScript compatibility. | `logging` |

#### Permission & Safety Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `permission` | Comprehensive permission control system. Manages user consent for file editing, bash commands, web fetching, and external directory access. | `event` |
| `agent` | Multi-agent configuration and management. Implements flexible agent system with different operation modes (primary/subagent) and tool access controls. | `permission` |

#### Tool & Execution Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `tool` | Tool registry and execution framework. Manages tool registration, lookup, and execution. Includes built-in tools: Read, Write, Edit, Bash, Glob, Grep, List, WebFetch, TodoRead, TodoWrite, Task. | `agent`, `event`, `permission`, `storage` |
| `mcp` | Model Context Protocol (MCP) client. Connects to external MCP servers and exposes their tools, resources, and prompts to the LLM. | `tool` |
| `clienttool` | Registry for client-side tools. Enables external clients to register and execute tools via HTTP API. | `event` |
| `executor` | Task execution implementations. Runs subagent tasks by creating child sessions and managing their lifecycle. | `agent`, `event`, `permission`, `provider`, `session`, `storage`, `tool` |

#### Provider Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `provider` | LLM provider abstraction layer using Eino framework. Supports Anthropic (Claude), OpenAI (GPT), and Volcengine ARK models. Handles streaming, tool calls, and message formatting. | (none) |

#### Session Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `session` | Core agentic loop and session management. Manages conversations, message processing, tool execution, and session state. Implements the main LLM interaction loop with tool calling. | `event`, `logging`, `permission`, `provider`, `storage`, `tool` |

#### Integration Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `vcs` | Version control system (Git) integration. Provides repository status, diff tracking, and file change monitoring. | `event` |

#### API Layer

| Package | Description | Dependencies |
|---------|-------------|--------------|
| `server` | HTTP server implementation for OpenCode API. Provides RESTful endpoints for sessions, messages, files, config, events (SSE), MCP management, and client tools. | `clienttool`, `command`, `event`, `formatter`, `logging`, `lsp`, `mcp`, `provider`, `session`, `storage`, `tool`, `vcs` |

## Dependency Statistics

### Most Depended Upon (Foundation Packages)

1. **event** - 6 dependents (core pub/sub infrastructure)
2. **tool** - 5 dependents (tool execution framework)
3. **storage** - 4 dependents (persistence layer)
4. **provider** - 4 dependents (LLM abstraction)
5. **permission** - 4 dependents (safety controls)

### Hub Packages (High Fan-out)

1. **server** - imports 12 internal packages (API orchestration)
2. **executor** - imports 7 internal packages (subagent coordination)
3. **session** - imports 6 internal packages (agentic loop)

## Key Design Principles

1. **Layered Architecture**: Foundation packages (event, storage, logging) support higher-level abstractions (session, server).

2. **No Circular Dependencies**: The import cycle between `tool` and `session` was resolved by extracting `executor` as a separate package.

3. **Event-Driven Communication**: Components communicate via the `event` package for loose coupling.

4. **Permission-First Design**: All potentially dangerous operations (file writes, bash commands) go through the `permission` package.

5. **Provider Abstraction**: LLM providers are abstracted behind a common interface, enabling easy addition of new providers.

## External Dependencies

Key external packages used:

| Package | Purpose |
|---------|---------|
| `github.com/cloudwego/eino` | LLM framework for provider abstraction |
| `github.com/go-chi/chi/v5` | HTTP router |
| `github.com/rs/zerolog` | Structured logging |
| `github.com/spf13/cobra` | CLI framework |
| `github.com/mark3labs/mcp-go` | MCP protocol implementation |
| `github.com/JohannesKaufmann/html-to-markdown` | HTML to Markdown conversion (WebFetch tool) |
| `github.com/PuerkitoBio/goquery` | HTML parsing and text extraction (WebFetch tool) |
| `github.com/sergi/go-diff` | Diff computation for file changes |
3 changes: 2 additions & 1 deletion go-opencode/cmd/opencode/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/opencode-ai/opencode/internal/agent"
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/executor"
"github.com/opencode-ai/opencode/internal/mcp"
"github.com/opencode-ai/opencode/internal/permission"
"github.com/opencode-ai/opencode/internal/provider"
Expand Down Expand Up @@ -198,7 +199,7 @@ func runInteractive(cmd *cobra.Command, args []string) error {
}

// Create and configure SubagentExecutor for task tool
subagentExecutor := tool.NewSubagentExecutor(tool.SubagentExecutorConfig{
subagentExecutor := executor.NewSubagentExecutor(executor.SubagentExecutorConfig{
Storage: store,
ProviderRegistry: providerReg,
ToolRegistry: toolReg,
Expand Down
3 changes: 2 additions & 1 deletion go-opencode/cmd/opencode/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/opencode-ai/opencode/internal/agent"
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/executor"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/opencode-ai/opencode/internal/mcp"
"github.com/opencode-ai/opencode/internal/provider"
Expand Down Expand Up @@ -108,7 +109,7 @@ func runServe(cmd *cobra.Command, args []string) error {
}

// Create and configure SubagentExecutor for task tool
subagentExecutor := tool.NewSubagentExecutor(tool.SubagentExecutorConfig{
subagentExecutor := executor.NewSubagentExecutor(executor.SubagentExecutorConfig{
Storage: store,
ProviderRegistry: providerReg,
ToolRegistry: toolReg,
Expand Down
9 changes: 6 additions & 3 deletions go-opencode/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/opencode-ai/opencode

go 1.25
go 1.24

require (
// Eino LLM Framework
Expand All @@ -21,11 +21,14 @@ require (
)

require (
github.com/JohannesKaufmann/html-to-markdown v1.6.0
github.com/PuerkitoBio/goquery v1.10.0
github.com/ThreeDotsLabs/watermill v1.5.1
github.com/agnivade/levenshtein v1.2.1
github.com/bmatcuk/doublestar/v4 v4.9.1
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cloudwego/eino-ext/components/model/ark v0.1.50
github.com/fsnotify/fsnotify v1.9.0
github.com/joho/godotenv v1.5.1
github.com/mark3labs/mcp-go v0.43.1
github.com/modelcontextprotocol/go-sdk v1.1.0
Expand All @@ -41,6 +44,7 @@ require (

require (
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/anthropics/anthropic-sdk-go v1.19.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.33.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect
Expand All @@ -67,7 +71,6 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/eino-contrib/jsonschema v1.0.2 // indirect
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
Expand Down Expand Up @@ -122,4 +125,4 @@ require (

replace github.com/sst/opencode-sdk-go => ../packages/sdk/go

replace github.com/cloudwego/eino-ext/components/model/claude => /Users/joohwi.lee/repos/cloudwego/eino-ext/components/model/claude
// replace github.com/cloudwego/eino-ext/components/model/claude => /Users/joohwi.lee/repos/cloudwego/eino-ext/components/model/claude
Loading
Loading