opencode-bridge

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Overview

概述

OpenWork communicates with OpenCode via three mechanisms:
  1. CLI invocation: Spawn
    opencode
    with prompts and get JSON responses.
  2. Database access: Read OpenCode's SQLite database for sessions and messages.
  3. MCP bridge: Real-time bidirectional communication for streaming and permissions.
OpenWork通过三种机制与OpenCode进行通信:
  1. CLI调用:启动
    opencode
    并传入提示词,获取JSON格式的响应。
  2. 数据库访问:读取OpenCode的SQLite数据库以获取会话和消息数据。
  3. MCP桥接:用于流式传输和权限管理的实时双向通信。

CLI Invocation

CLI调用

Non-interactive mode

非交互模式

bash
opencode -p "your prompt" -f json -q
Returns JSON with the response content.
bash
opencode -p "your prompt" -f json -q
返回包含响应内容的JSON数据。

Flags

标志参数

FlagDescription
-p
Prompt to execute
-f
Output format (
text
,
json
)
-q
Quiet mode (no spinner)
-c
Working directory
-d
Debug mode
标志描述
-p
要执行的提示词
-f
输出格式(
text
,
json
-q
静默模式(不显示加载动画)
-c
工作目录
-d
调试模式

Example response

响应示例

json
{
  "content": "Here is the result...",
  "session_id": "abc123"
}
json
{
  "content": "Here is the result...",
  "session_id": "abc123"
}

Database Access

数据库访问

Location

位置

~/.opencode/opencode.db
Or project-local:
.opencode/opencode.db
~/.opencode/opencode.db
或项目本地路径:
.opencode/opencode.db

Schema (key tables)

数据库架构(核心表)

sessions

sessions表

sql
CREATE TABLE sessions (
  id TEXT PRIMARY KEY,
  parent_session_id TEXT,
  title TEXT,
  message_count INTEGER,
  prompt_tokens INTEGER,
  completion_tokens INTEGER,
  summary_message_id TEXT,
  cost REAL,
  created_at INTEGER,
  updated_at INTEGER
);
sql
CREATE TABLE sessions (
  id TEXT PRIMARY KEY,
  parent_session_id TEXT,
  title TEXT,
  message_count INTEGER,
  prompt_tokens INTEGER,
  completion_tokens INTEGER,
  summary_message_id TEXT,
  cost REAL,
  created_at INTEGER,
  updated_at INTEGER
);

messages

messages表

sql
CREATE TABLE messages (
  id TEXT PRIMARY KEY,
  session_id TEXT,
  role TEXT,  -- 'user', 'assistant', 'tool'
  parts TEXT, -- JSON array of content parts
  model TEXT,
  created_at INTEGER,
  updated_at INTEGER
);
sql
CREATE TABLE messages (
  id TEXT PRIMARY KEY,
  session_id TEXT,
  role TEXT,  -- 'user', 'assistant', 'tool'
  parts TEXT, -- 内容片段的JSON数组
  model TEXT,
  created_at INTEGER,
  updated_at INTEGER
);

Querying from Rust (Tauri)

从Rust(Tauri)中查询

rust
use tauri_plugin_sql::{Migration, MigrationKind};

#[tauri::command]
async fn list_sessions(db: tauri::State<'_, Database>) -> Result<Vec<Session>, String> {
    let sessions = sqlx::query_as::<_, Session>(
        "SELECT * FROM sessions ORDER BY updated_at DESC"
    )
    .fetch_all(&db.pool)
    .await
    .map_err(|e| e.to_string())?;
    
    Ok(sessions)
}
rust
use tauri_plugin_sql::{Migration, MigrationKind};

#[tauri::command]
async fn list_sessions(db: tauri::State<'_, Database>) -> Result<Vec<Session>, String> {
    let sessions = sqlx::query_as::<_, Session>(
        "SELECT * FROM sessions ORDER BY updated_at DESC"
    )
    .fetch_all(&db.pool)
    .await
    .map_err(|e| e.to_string())?;
    
    Ok(sessions)
}

Querying from SolidJS

从SolidJS中查询

tsx
import Database from "@tauri-apps/plugin-sql";

const db = await Database.load("sqlite:~/.opencode/opencode.db");
const sessions = await db.select<Session[]>(
  "SELECT * FROM sessions ORDER BY updated_at DESC"
);
tsx
import Database from "@tauri-apps/plugin-sql";

const db = await Database.load("sqlite:~/.opencode/opencode.db");
const sessions = await db.select<Session[]>(
  "SELECT * FROM sessions ORDER BY updated_at DESC"
);

MCP Bridge (Advanced)

MCP桥接(高级功能)

OpenWork can register as an MCP server that OpenCode connects to.
OpenWork可以注册为OpenCode连接的MCP服务器。

Configuration (opencode.json)

配置(opencode.json)

json
{
  "mcpServers": {
    "openwork": {
      "type": "stdio",
      "command": "openwork-mcp-bridge"
    }
  }
}
json
{
  "mcpServers": {
    "openwork": {
      "type": "stdio",
      "command": "openwork-mcp-bridge"
    }
  }
}

Use cases

使用场景

  • Real-time permission prompts surfaced in OpenWork UI.
  • Streaming progress updates.
  • Custom tools exposed from OpenWork (e.g., native file picker).
  • 在OpenWork UI中实时显示权限提示。
  • 流式传输进度更新。
  • 从OpenWork暴露自定义工具(例如原生文件选择器)。

Message Content Parts

消息内容片段

Messages contain a
parts
JSON array with different content types:
消息包含一个
parts
JSON数组,其中包含不同类型的内容:

TextContent

TextContent

json
{ "type": "text", "text": "Hello world" }
json
{ "type": "text", "text": "Hello world" }

ToolCall

ToolCall

json
{
  "type": "tool_call",
  "id": "call_123",
  "name": "bash",
  "input": "{\"command\": \"ls\"}"
}
json
{
  "type": "tool_call",
  "id": "call_123",
  "name": "bash",
  "input": "{\"command\": \"ls\"}"
}

ToolResult

ToolResult

json
{
  "type": "tool_result",
  "tool_call_id": "call_123",
  "content": "file1.txt\nfile2.txt",
  "is_error": false
}
json
{
  "type": "tool_result",
  "tool_call_id": "call_123",
  "content": "file1.txt\nfile2.txt",
  "is_error": false
}

Finish

Finish

json
{
  "type": "finish",
  "reason": "end_turn",
  "time": 1704067200
}
json
{
  "type": "finish",
  "reason": "end_turn",
  "time": 1704067200
}

Common Gotchas

常见注意事项

  • Database is SQLite; use read-only access to avoid conflicts with running OpenCode.
  • Message parts are JSON-encoded strings; parse them in the UI.
  • Session IDs are UUIDs; tool call IDs are also UUIDs.
  • Cost is in USD; tokens are raw counts.
  • 数据库为SQLite;使用只读访问以避免与运行中的OpenCode产生冲突。
  • 消息片段是JSON编码的字符串;需在UI中进行解析。
  • 会话ID为UUID格式;工具调用ID同样为UUID格式。
  • 成本以美元计算;令牌数为原始统计数量。

First-Time Setup

首次设置

Verify OpenCode is installed

验证OpenCode已安装

bash
which opencode
opencode --version
bash
which opencode
opencode --version

Verify database exists

验证数据库存在

bash
ls ~/.opencode/opencode.db
bash
ls ~/.opencode/opencode.db

Test CLI invocation

测试CLI调用

bash
opencode -p "Hello" -f json -q
bash
opencode -p "Hello" -f json -q