# OpenCodeMgr 版本修正對照（Portable Patch Notes）


> - 基準版本：**v1.8.1**（v1.8.0 亦適用）
> - 觸發條件：**OpenCode ≥ v2.2.x**（DB schema 改版）
> - 本地修改檔案：`src/core/node-transport.mjs`、`db_helper.py`

---

## 修正一：非 Windows 主機的 local 節點路徑錯誤

**檔案**：`src/core/node-transport.mjs` → `getNodePaths()`

| | |
|---|---|
| **症狀** | Linux/macOS 上執行時，GUI 設定快照全空（`config: {}`、`rawJsonc: ""`），Skills / Agents / Plugins 全部讀不到 |
| **原因** | 函式假設 `type === 'local'` 的節點必然是 Windows，路徑分隔符固定使用 `\`，在非 Windows 上組出如 `<configDir>\opencode.jsonc` 的無效路徑 |
| **upstream 狀態** | v1.8.1 尚未修復 |

**修正（1 行）**：

```diff
- const isWindows = node.type === 'local' || String(node.configDir || '').includes('\\');
+ const isWindows = String(node.configDir || '').includes('\\') || (node.type === 'local' && os.platform() === 'win32');
```

> 判斷依據改為「configDir 是否含反斜線」或「local 且執行平台為 win32」，Windows 行為不變。

---

## 修正二：OpenCode v2 資料庫結構支援

**檔案**：`db_helper.py`

| | |
|---|---|
| **症狀** | 工作區清單只剩少數舊目錄、多數對話「消失」、會話統計明顯偏少；對話詳情與瘦身功能對新會話無效 |
| **原因** | OpenCode v2.2.x 起會話改寫入 `session_v2` / `session_message`（parts 嵌在訊息 JSON 的 `content` 欄位，`session_part` 表為空），舊表 `session` / `message` / `part` 停寫。`db_helper.py` 全部查詢都寫死舊表 |
| **upstream 狀態** | 完全沒有 v2 支援 |

**設計原則**：執行時以 `sqlite_master` 自動偵測 `session_v2` 是否存在，**新舊 DB 皆可操作，向後相容**。

### 新舊版行為對照

| 函式 | 舊版行為 | 新版行為（v2 偵測啟用時） |
|------|----------|--------------------------|
| `detect_schema()`（新增） | ― | 偵測 DB 佈局，回傳新/舊表名對應 |
| `v2_flatten_content_blocks()`（新增） | ― | 將 v2 tool 的 `state.content` 文字區塊攤平為顯示字串 |
| `v2_normalize_part()`（新增） | ― | 將 v2 content item 正規化為 GUI 期望的舊 part 格式（`name`→`tool`、`state.content`→`state.output`） |
| `get_stats()` | `COUNT(*) FROM session/message/part`、依舊表 `directory` 分組 | 改查 `session_v2` / `session_message`；parts 數以 SQLite JSON 函數計算：`json_array_length(data,'$.content')`（含 `json_valid` 防護） |
| `get_sessions()` | 子查詢舊 `message`/`part` 表取訊息數、parts 數、體積 | 子查詢 `session_message`；體積為 `SUM(length(data))`；`PRAGMA table_info` 改對偵測到的表執行 |
| `get_session_detail()` | 讀 `message` + 逐筆讀 `part` | 讀 `session_message ORDER BY seq`，從 `data.content` 合成 parts；`user.text` / `compaction.summary` 轉為 text part；跳過 `idle` 型別訊息；`model` 字串包成 `{"modelID": ...}` |
| `delete_session()` | 只刪 `part`/`message`/`session` | 新舊兩組表（`session_v2`/`session_message`/`session_part` + `session`/`message`/`part`）全部刪除，各以 try/except 容錯 |
| `clean_session()` | 逐筆處理 `part.data`（5 種瘦身規則） | v2 分支：逐筆解析 `session_message.data`，對 `content` 陣列套用相同 5 規則後**寫回訊息 JSON**；tool 輸出截斷同時處理 `state.output` 與 `state.content` 文字區塊 |

### v2 欄位差異對照（合成 parts 時需正規化）

| 用途 | 舊版 part | v2 content item |
|------|-----------|-----------------|
| 工具名稱 | `data.tool` | `data.name` |
| 工具輸出 | `state.output`（字串） | `state.content`（`[{type:'text', text}]` 區塊陣列） |
| 訊息角色 | `message.data.role` | `session_message.type` 欄位（`user`/`assistant`/`system`/`synthetic`/`compaction`/`idle`） |
| 排序 | `time_created` | `seq`（同 session 內單調遞增） |


---

## 修正三（建議）：本機 Python 指令寫死 `python`

**檔案**：`src/core/node-transport.mjs`（本機 DB helper 呼叫，約 194 行）

| | |
|---|---|
| **症狀** | 所有走 Python helper 的 API（providers 目錄、會話統計/列表/詳情/瘦身）報錯 `python: not found`，GUI 顯示空 |
| **原因** | 本機呼叫寫死 `python "…db_helper.py…"`，僅遠端 SSH 分支用 `python3`；多數 Linux 發行版已不提供 `python` 指令 |
| **upstream 狀態** | 尚未修復 |

**建議修正**：

```diff
- const cmd = `python "${LOCAL_DB_HELPER}" ${quotedArgs.join(' ')}`;
+ const cmd = `python3 "${LOCAL_DB_HELPER}" ${quotedArgs.join(' ')}`;
```

> 注意：macOS 與部分環境可能只有 `python`。更完整的做法是啟動時以 `where python3 || where python` 偵測一次並快取結果。

---

