opencode-idea 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,8 +9,9 @@ OpenCode 插件,面向 JetBrains 项目 (IntelliJ IDEA / PyCharm / WebStorm 等)
9
9
  3. **注入 IDE 环境变量** —— 从 IDE 集成终端读取它实际运行的环境,注入每个 OpenCode shell。
10
10
 
11
11
  非 JetBrains 项目完全不受影响。 **手动接入**:插件启动不探测、不连接 IDE,只有跑 `/open-in-idea` 才接入。跑过一次后,该**项目**
12
- 被「标记」(持久化在 `ctx.storage`,重启 OpenCode 后仍在):之后每条用户消息都默认 IDE 仍在,一旦发现 IDE MCP 未连接,插件会在
13
- 构建本次请求前自动重跑一遍接入流程并按需补一条通知。`/close-in-idea` 删除该标记。没跑过 `/open-in-idea` 的项目保持全手动、零自主动作。
12
+ 被「标记」(持久化在 `ctx.storage`,重启 OpenCode 后仍在):之后插件在后台**每 5s 探活**该 IDE——**掉线就注销 `idea` MCP**(杀掉不会自愈的
13
+ stdio 僵尸桥 + 移除 `idea_*` 工具,让模型调不到、**不会挂起**),并锁存"待重建";**下一条用户消息**读到锁存后**静默重建**(用户无感)。
14
+ `/close-in-idea` 注销 IDE MCP + 删除标记。没跑过 `/open-in-idea` 的项目保持全手动、零自主动作。
14
15
 
15
16
  ## 工作原理
16
17
 
@@ -18,42 +19,51 @@ OpenCode 插件,面向 JetBrains 项目 (IntelliJ IDEA / PyCharm / WebStorm 等)
18
19
  setup(启动, 每项目一次):
19
20
  a. 读项目标记(ctx.storage: project/<项目路径>; OpenCode 重启后仍生效)
20
21
  b. 读 IDE 集成终端环境 → 通过 shell create.before 钩子注入每个 shell(null: 未连 IDE 时为空)
21
- c. 注册 /open-in-idea 与 /close-in-idea 命令 + session context/prompt 钩子
22
+ c. 注册 /open-in-idea 与 /close-in-idea 命令 + session context 钩子 + tool execute.after 钩子
22
23
  不探测、不注册 MCP
23
24
 
24
- /open-in-idea(手动触发):
25
- 0. 写项目标记 = true(持久化)+ 清内存缓存(下次读取者回源)
26
- 1. 探测本地 JetBrains IDE MCP 服务(/stream)
27
- 2. 已就绪 → 直接连接
28
- 3. 未就绪 → open -a "IntelliJ IDEA" <项目目录>(复用已运行实例),然后立即提示:
29
- 在 IDEA 中开启 MCP 服务 + Brave Mode
30
- 4. 本次命令 fast-fail 返回;用户手动开启后再次执行 `/open-in-idea`
31
- 5. MCP 就绪时:
32
- a. 注册 IDE MCP(url /stream,header IJ_MCP_SERVER_PROJECT_PATH 锁定当前项目)
33
- b. 读 IDE 终端环境 → 更新注入每个 shell 的环境变量
34
- c. 启用系统提示引导(直接调用 `idea_*` 工具)+ IDEA MCP 工具描述前缀
35
- 6. 发一条结果通知(用户看到干净气泡,模型只回"收到")
25
+ /open-in-idea(手动触发, 阻塞到就绪):
26
+ 1. 写项目标记 = true + 同步内存缓存;注册 `idea-run-config` skill
27
+ 2. 探测本地 JetBrains IDE MCP 服务(/stream)
28
+ 3. 未就绪 → open -a "IntelliJ IDEA" <项目目录>(复用已运行实例),然后轮询等到 MCP 就绪(冷启动约数十秒,上限 60s)
29
+ 4. **先注销旧的 idea MCP(杀掉旧的 stdio 桥子进程),再重新注册**
30
+ (`idea stdioMcpServer`,env 带项目路径 + 端口;每项目一个子进程)
31
+ —— 每次执行都重建,因为断掉的桥不会自愈、也不能靠 refresh 复活
32
+ 5. 等 `idea_*` 工具进入注册表;读 IDE 终端环境更新 shell 环境;启用引导
33
+ 6. 发结果通知(用户看到干净气泡,模型只回"收到")
36
34
 
37
35
  /close-in-idea(手动触发):
38
- 1. 删项目标记 + 清内存缓存
39
- 2. 发一条结果通知(不改动已建立的连接)
40
-
41
- 后续用户消息(仅在被标记的项目里):
42
- 1. 项目已标记 → 默认 IDE 仍在线
43
- 2. 查 OpenCode 里 `idea` MCP server 状态;不是 connected/pending(真断了)→ 在 prompt 钩子里同步重跑上面的接入流程
44
- 3. 成功 → 本条消息即可用 `idea_*` 工具,并补一条"已自动重新连接"通知(排在用户消息之后)
45
- 4. 失败 → 静默,沿用 fast-fail 恢复提示(不会每条消息都重复拉起 IDE)
36
+ 1. 删项目标记 + 同步内存缓存 + 注销 IDE MCP + 清空注入的环境
37
+ 2. 发结果通知
38
+
39
+ 工具失败触发(仅在被标记的项目里;需先 /open-in-idea 过一次):
40
+ 1. `idea_*` 工具调用失败 → tool execute.after 钩子**分类**:
41
+ - IDE MCP 不可用(掉了)→ 重跑接入流程
42
+ - IDE 在、但项目没打开("Unable to determine the target project" / "doesn't correspond to any open project")→ 让 IDEA 重新打开该项目
43
+ - 普通业务错误(File not found 等)→ 不动作
44
+ 2. 触发成功 → 把该错误改写为"已重连 / 已重新打开,请重试",模型同一回合重试;失败 → 保持原错误
45
+
46
+ 后台探活(心跳;仅"已标记 + 桥已注册"的项目):
47
+ 1. 每 5s 探测 IDE 端口(/stream);连续 2 次不通 →
48
+ 2. **注销 `idea` MCP**:杀掉僵尸桥子进程 + 从注册表移除 `idea_*` 工具
49
+ (→ 模型调不到,也就不会挂起;这一步是"从根上避免挂起"的关键)
50
+ 3. 锁存 `needRebuild = true`(不会因端口恢复而清,因为桥不会自愈)
51
+ 4. 下一条用户消息:纯内存读到锁存 → 自动重跑接入流程(IDE 不在则 `open -a` 拉起),成功后清锁存
52
+ —— 全程静默,用户无感
53
+ ※ 这是**每个 Location(项目)各跑一份**探活;IDE 只有一个,所以所有项目的桥几乎同时被注销、再各自重建
46
54
  ```
47
55
 
48
56
  ## 在 IDE 中打开项目 (`/open-in-idea`)
49
57
 
50
58
  在会话里输入 `/open-in-idea`:
51
59
 
52
- 1. 先探测 `/stream`;已就绪 → 直接加载能力;
53
- 2. 未就绪 → 用 `open -a "IntelliJ IDEA" <项目目录>` 拉起或激活 IDEA —— **复用已运行的实例**(不会重复开新实例);
54
- 3. 如果 MCP 仍未就绪,命令会 **立即 fast-fail**,提示在 IDEA 的 `Settings → MCP Server` 开启 MCP 服务并启用 **Brave
55
- Mode**;
56
- 4. 用户手动完成设置后,再次执行 `/open-in-idea`;探测成功后才注册 IDE MCP、重读 IDE 环境并启用系统引导与 live tool catalog。
60
+ 1. 先探测 `/stream`;已就绪 → 直接进入下一步;
61
+ 2. 未就绪 → 用 `open -a "IntelliJ IDEA" <项目目录>` 拉起或激活 IDEA —— **复用已运行的实例**(不会重复开新实例),然后
62
+ **轮询等到 MCP 就绪**(冷启动约数十秒,上限 60s);
63
+ 3. 等不到(例如 IDE 里 MCP 服务未开启)→ 提示在 IDEA 的 `Settings → MCP Server` 开启 MCP 服务并启用 **Brave Mode**,用户设置好后
64
+ 再次执行 `/open-in-idea`;
65
+ 4. 就绪后 **先注销旧的 `idea` MCP(杀掉旧的 stdio 桥子进程)→ 再重新注册**(每次执行都重建,因为断掉的桥不会自愈),然后重读
66
+ IDE 环境、启用系统引导,并**等到 `idea_*` 工具进入注册表**才返回。
57
67
 
58
68
  > **通知不触发 AI 干活**:默认 `feedback: "message"` 把结果作为一条 **标注清楚的通知**发到会话里 —— 用户看到干净的通知气泡
59
69
  > (经 `metadata.displayText`),模型只看到"这是通知,请只回复『收到』"的指令,因此不会执行任何操作、不会调用工具。
@@ -68,8 +78,8 @@ Brave Mode 后再次执行 `/open-in-idea` 即可接上。
68
78
 
69
79
  ## 关闭接入 (`/close-in-idea`)
70
80
 
71
- 输入 `/close-in-idea`:删除当前项目的接入标记(持久化 + 内存缓存一并清),之后 IDE MCP 即使断开也**不再自动重连**。命令只删标记,
72
- **不主动断开已建立的连接**——当前已注册的工具会保留到连接自然中断为止。想重新接入,再次执行 `/open-in-idea`。
81
+ 输入 `/close-in-idea`:**注销当前项目的 IDE MCP** + 删除项目标记(持久化 + 内存缓存一并清),并清空注入的环境变量。
82
+ 之后 `idea_*` 工具不再可用,也不会再因失败自动接入。想重新接入,再次执行 `/open-in-idea`。
73
83
 
74
84
  ## IDE 工具优先 (直接调用引导)
75
85
 
@@ -128,19 +138,39 @@ IDE MCP 只能 **查**(`get_run_configurations`)和 **执行**(`execute_run_conf
128
138
  - JetBrains IDE 已开启 MCP 服务 (IDE 设置里的 MCP Server / AI Assistant MCP),版本需 **2026.2+**(提供 Streamable HTTP 端点
129
139
  `/stream`);若关闭,`/open-in-idea` 会立即提示如何重新开启。
130
140
  - 要读 IDE 终端环境,需要在 IDE 里开启 **Brave Mode**(否则 `execute_terminal_command` 会等确认);MCP 未就绪时通知会同时提醒开启它。
131
- - 插件注册给 OpenCode 的 MCP 配置形如:
141
+ - 插件注册给 OpenCode 的 MCP 配置。默认用 **stdio 桥**:
132
142
 
133
143
  ```jsonc
134
144
  {
135
- "type": "remote",
136
- "url": "http://127.0.0.1:64342/stream",
137
- "headers": { "IJ_MCP_SERVER_PROJECT_PATH": "<项目路径>" },
145
+ "type": "local",
146
+ // OpenCode 的 `LocalConfig.command` 是【字符串数组】:可执行文件 + 参数
147
+ // (源码 `client.ts` 里 `const [command, ...args] = config.command`)。没有单独的 `args` 字段。
148
+ "command": ["/Applications/IntelliJ IDEA.app/Contents/MacOS/idea", "stdioMcpServer"],
149
+ "environment": {
150
+ "IJ_MCP_SERVER_PROJECT_PATH": "<项目路径>",
151
+ "IJ_MCP_SERVER_PORT": "64342",
152
+ },
138
153
  }
139
154
  ```
140
155
 
141
- > **为什么是 `/stream` 而不是 `/sse`**:OpenCode V2 的远程 MCP 客户端 **只支持 Streamable HTTP**,没有旧版 SSE 传输。IDE 的
142
- > `/sse` 是旧版 SSE (POST 会返回 405),V2 连不上 (报 `Error POSTing to endpoint`)。IDE 2026.2 起额外提供 `/stream` 这个
143
- > Streamable HTTP 端点。 **插件的探测也走 `/stream`**(一次 `initialize`),协议统一。
156
+ `idea stdioMcpServer` 是 IDE 自带的桥:OpenCode 通过 **stdin/stdout 子进程管道**跟它通信,它在后端连 IDE 的 MCP。
157
+ **客户端侧是持久管道,不存在 HTTP 空闲断流**——这正是默认选它的原因。每个项目一个插件实例、各带自己的
158
+ `IJ_MCP_SERVER_PROJECT_PATH`,所以每个项目会起一个独立的 stdio 客户端进程。
159
+
160
+ > **为什么不用远程 `/stream`**:Streamable HTTP 的客户端会挂一条用于 server→client 通知的 SSE 流;这条流**空闲一段时间会
161
+ > 被关**,而**远程传输在客户端侧不重连**(OpenCode 只为 legacy session 过期重连)。于是"闲置一会儿"就会掉。stdio 桥没有这个
162
+ > 问题。`transport: "http"` 可切回远程模式:
163
+ >
164
+ > ```jsonc
165
+ > {
166
+ > "type": "remote",
167
+ > "url": "http://127.0.0.1:64342/stream",
168
+ > "headers": { "IJ_MCP_SERVER_PROJECT_PATH": "<项目路径>" },
169
+ > }
170
+ > ```
171
+ >
172
+ > **为什么不用 `/sse`**:OpenCode V2 的远程 MCP 客户端**只支持 Streamable HTTP**,没有旧版 SSE 传输。IDE 的 `/sse` 是旧版
173
+ > SSE (POST 返回 405),V2 连不上。插件对 IDE 的**探测**仍走 `/stream`(一次 `initialize`),用来确认 IDE MCP 已就绪并拿到端口。
144
174
 
145
175
  插件按 `ports` 顺序选择第一个可用端口,但会并发探测以避免多个端口的超时叠加;默认 `[64342]`。
146
176
 
@@ -205,9 +235,14 @@ export { default } from "/Users/yutao/Projects/opencode-idea/src/index.js";
205
235
  "injectEnv": true,
206
236
  "injectGuidance": true, // IDE 可用时向系统提示注入「优先使用 IDEA MCP」引导
207
237
  "openInIde": "idea", // false 关闭;或任意 macOS 应用名(如 "IntelliJ IDEA")
238
+ "transport": "stdio", // "stdio"(默认,本地桥,不会空闲断流)| "http"(远程 Streamable HTTP)
239
+ "ideExecutable": undefined, // 可显式指定 IDE 可执行文件;默认从 .app 里推断
208
240
  "launchCooldownMs": 120000, // 启动保护冷却
209
241
  "launchMaxAttempts": 5,
210
242
  "mcpProbeTimeoutMs": 1000, // 单个 MCP 端口的短探测超时
243
+ "executionTimeoutMs": 600000, // 单次 idea_* 工具调用硬上限;默认 10 分钟(OpenCode 默认 12 小时)
244
+ "heartbeatIntervalMs": 5000, // 后台端口探活间隔
245
+ "heartbeatFailures": 2, // 连续失败几次后注销 idea MCP
211
246
  "feedback": "message", // "message"(默认,会话通知,AI 只回"收到")| false(静默)
212
247
  },
213
248
  },
@@ -217,36 +252,46 @@ export { default } from "/Users/yutao/Projects/opencode-idea/src/index.js";
217
252
 
218
253
  不传 options 时全部使用下表默认值。
219
254
 
220
- | 选项 | 类型 | 默认 | 说明 |
221
- | ------------------- | -------------------- | ----------- | ---------------------------------------------------------------- |
222
- | `ports` | `number[]` | `[64342]` | 依次探测的 IDE MCP 端口 |
223
- | `injectEnv` | `boolean` | `true` | 是否注入 IDE 终端环境变量 |
224
- | `injectGuidance` | `boolean` | `true` | IDE 可用时是否向系统提示注入「优先使用 IDEA MCP」引导 |
225
- | `openInIde` | `boolean \| string` | `"idea"` | 拉起哪个 IDE;`false` 关闭,`"idea"` 映射到 IntelliJ IDEA |
226
- | `launchCooldownMs` | `number` | `120000` | 启动保护冷却:冷却期内不重复 spawn |
227
- | `launchMaxAttempts` | `number` | `5` | 单次会话内最多 spawn 次数,超出后停止重试 |
228
- | `mcpProbeTimeoutMs` | `number` | `1000` | 单个 MCP 端口的短探测超时 |
229
- | `feedback` | `"message" \| false` | `"message"` | 结果反馈:`message` 会话通知(用户可见,AI 只回"收到");`false` 静默 |
255
+ | 选项 | 类型 | 默认 | 说明 |
256
+ | --------------------- | -------------------- | ----------- | --------------------------------------------------------------------------------------- |
257
+ | `ports` | `number[]` | `[64342]` | 依次探测的 IDE MCP 端口 |
258
+ | `injectEnv` | `boolean` | `true` | 是否注入 IDE 终端环境变量 |
259
+ | `injectGuidance` | `boolean` | `true` | IDE 可用时是否向系统提示注入「优先使用 IDEA MCP」引导 |
260
+ | `openInIde` | `boolean \| string` | `"idea"` | 拉起哪个 IDE;`false` 关闭,`"idea"` 映射到 IntelliJ IDEA |
261
+ | `transport` | `"stdio" \| "http"` | `"stdio"` | `stdio` 注册 IDE 自带的 stdio 桥(本地子进程,不会空闲断流);`http` 用远程 Streamable HTTP |
262
+ | `ideExecutable` | `string` | (推断) | IDE 可执行文件绝对路径;默认从 `<App>.app/Contents/MacOS/` 推断 |
263
+ | `launchCooldownMs` | `number` | `120000` | 启动保护冷却:冷却期内不重复 spawn |
264
+ | `launchMaxAttempts` | `number` | `5` | 单次会话内最多 spawn 次数,超出后停止重试 |
265
+ | `mcpProbeTimeoutMs` | `number` | `1000` | 单个 MCP 端口的短探测超时 |
266
+ | `executionTimeoutMs` | `number` | `600000` | 单次 `idea_*` 工具调用的硬上限(毫秒);防止桥挂起后无限等待(OpenCode 默认 12 小时) |
267
+ | `heartbeatIntervalMs` | `number` | `5000` | 后台 IDE 端口探活间隔(毫秒);仅"已标记 + 桥已注册"的项目有效 |
268
+ | `heartbeatFailures` | `number` | `2` | 连续探活失败几次后注销 `idea` MCP(杀僵尸桥 + 移除工具),下一条消息自动重建 |
269
+ | `feedback` | `"message" \| false` | `"message"` | 结果反馈:`message` 会话通知(用户可见,AI 只回"收到");`false` 静默 |
230
270
 
231
271
  ## 行为
232
272
 
233
- - **被标记的项目**:启动读回标记后挂上 shell 环境钩子 (环境值待接入后填充)、注册 `idea-run-config` skill;
234
- `/open-in-idea` 接入 IDE MCP + 启用引导。
273
+ - **被标记的项目**:启动读回标记后挂上 shell 环境钩子 (环境值待接入后填充)、注册 `idea-run-config` skill;IDE MCP 由
274
+ `/open-in-idea` 注册。
235
275
  - **未标记的项目**:不改环境、不注册、不注入任何 IDEA 提示 (每请求零开销;setup 时只做一次项目标记读取)。
236
- - **手动接入**:启动 **不探测、不连接 IDE MCP**;注册、**环境重读**、引导注入全部由 `/open-in-idea` 触发。
237
- `context` 钩子按「每次模型调用」触发 (含工具续跑),是**请求的纯函数**:它只看本次请求自带的工具快照 ——
238
- 有 `idea_*` 就剥离隐藏工具并注入引导;没有则只在项目被标记时给恢复提示。**不做任何 I/O(get 只看内存缓存),没有定时器,
239
- 没有事件订阅**。一次失败的调用不会触发探测或注销。
240
- - **项目标记自动重连**:`/open-in-idea` 会持久化一个**项目级**标记(插件名下 KV,key 为 `project/<项目根路径>`,重启 OpenCode 后
241
- 仍在),并在内存缓存。之后每条用户消息经 `prompt` 钩子处理时,先查 OpenCode 里 `idea` MCP server 的状态:不是
242
- `connected`/`pending`(真断了)才同步重跑一遍完整接入流程 (探测 → `open -a` → 等 MCP → 注册 + `tool.reload`),让**这条消息
243
- 本身**就能用上 IDEA 工具;恢复成功后补一条会话通知。`/close-in-idea` 删标记后即不再自愈。失败时沿用启动保护的冷却与次数上限
244
- (`launchCooldownMs` / `launchMaxAttempts`),不会每条消息都重复拉起 IDE。
245
- - **判定只看 MCP 状态,不看工具报错**:是否重连只取决于 `idea` MCP server 的连接状态(`ctx.mcp.list()`)。工具调用的业务报错
246
- (如 `File not found`)只可能发生在 server 仍 `connected` 时,所以**永远不会**被误判成断开;插件也不注册 `tool.execute.after`
247
- 钩子,一次失败的调用不会触发探测或注销。
248
- - **`/open-in-idea`**:手动打开当前项目并即时加载能力 (已在 IDE 打开时则只重新加载);MCP 未开启时只发一次提示并
249
- fast-fail,用户手动开启 MCP + Brave Mode 后重试。**`/close-in-idea`**:删项目标记,不改动已建立的连接。
276
+ - **手动接入 + 就绪即返回**:`/open-in-idea` 会 `open -a` 拉起 IDEA 并**等 IDE MCP 就绪**(冷启动数十秒,上限 60s);就绪后
277
+ **先注销旧的 `idea`(杀掉旧 stdio 桥)→ 再以 stdio 重新注册**,等 `idea_*` 工具就绪,最后才通知。**每次执行都重建**,所以断掉的
278
+ 僵尸桥只要重跑一次命令就能恢复。命令返回即可用;`/close-in-idea` 注销 `idea` + 删标记 + 清环境。
279
+ - **`context` 钩子是请求的纯函数**:按「每次模型调用」触发 (含工具续跑),只看本次请求自带的工具快照 —— 有 `idea_*` 就剥离隐藏
280
+ 工具并注入引导;没有则只在项目被标记时给恢复提示。该钩子本身**不做任何 I/O**(只看内存缓存),也不订阅任何事件。
281
+ - **后台心跳(仅"已标记 + 桥已注册"的项目)**:每 `heartbeatIntervalMs`(默认 5s)探一次 IDE 端口;连续 `heartbeatFailures`
282
+ (默认 2)次不通就**注销 `idea` MCP** —— 杀掉僵尸桥子进程 + 移除 `idea_*` 工具,于是模型**调不到、也就不会挂起** —— 并锁存
283
+ `needRebuild`。下一条用户消息在 `prompt` 钩子里读到锁存 → 自动重跑接入流程(IDE 不在则 `open -a` 拉起),成功后清锁存。**全程静默**。
284
+ 探活是**每个 Location(项目)各一份**;IDE 只有一个,所以 IDE 一挂、所有项目的桥几乎同时被注销,再由各自的消息触发重建。
285
+ - **失败触发的自动接入(仅被标记项目)**:`tool.execute.after` 钩子监听 `idea_*` 工具报错并**分类**:
286
+ - IDE MCP 不可用(掉了)→ 重跑 `/open-in-idea` 的接入流程;
287
+ - IDE 在、但项目没打开(`Unable to determine the target project` / `doesn't correspond to any open project`)→ 让 IDEA 重新打开该项目;
288
+ - 普通业务错误(`File not found` 等)→ **不动作**。判定只认**传输层专有标记**(`MCP server "idea" is not available` /
289
+ `MCP server is not connected` / `Error POSTing to endpoint` / `SseClientTransport is closed`),**不认**工具输出里出现的通用串
290
+ (例如 SQL 工具报的 `Connection refused`) —— MCP 工具错误的 message 就是工具自己的输出,认通用串会把正常业务失败误判成掉线并重建。
291
+ 触发成功会把该工具错误改写为"已重连 / 已重新打开,请重试",模型在同一回合内重试;失败则保持原错误。非 `idea_*` 工具、未标记
292
+ 项目都不受影响。启动保护 (`launchCooldownMs` / `launchMaxAttempts`) 限制重复拉起 IDE。
293
+ - **`/open-in-idea`**:手动打开当前项目;**等 IDE MCP 就绪 → 先注销旧的 `idea`(杀旧桥)→ 再重新注册 → 等 `idea_*` 工具就绪**才返回。
294
+ **`/close-in-idea`**:注销 IDE MCP + 删项目标记 + 清空注入的环境。
250
295
 
251
296
  ## 限制
252
297
 
@@ -263,14 +308,20 @@ export { default } from "/Users/yutao/Projects/opencode-idea/src/index.js";
263
308
  IDEA 里配置,AI 只读和查询)。`idea_execute_terminal_command` 也不暴露给模型 —— 它只由插件内部读取 IDE 环境,而那是插件直接调
264
309
  MCP,不经过模型工具表。
265
310
  - 探测只确认「MCP 服务是否在监听」,不校验「当前项目是否已在 IDE 打开」;MCP 关闭时会用一次短探测并立即给出设置提示。
266
- - OpenCode 的 MCP 自动重连**只覆盖 legacy Streamable HTTP 的 session 过期**(`SessionExpiredError`:有 session id + 404 /
267
- "Server not initialized" → `recover()` + 重试该次调用);modern 端点 (IDE 2026.2,protocol 2025-06-18,不带 session id)
268
- 的连接被关只会置 `failed` 并移除工具,**不会自动重连**。**被标记的项目**由插件在 `prompt` 钩子里查 MCP 状态并按需重跑接入
269
- 流程补这个缺口;未标记的项目仍走手动 `/open-in-idea`。
311
+ - IDE **2026.2+** 提供官方 stdio 桥 (`idea stdioMcpServer`),插件默认用它注册。stdio 是子进程管道,不像 Streamable HTTP 那样
312
+ 会空闲断流;但它**启动时要求 IDE MCP 已在监听**,所以 `/open-in-idea` 必须先等就绪再注册(冷启动约数十秒)。`transport: "http"`
313
+ 可切回远程 Streamable HTTP(OpenCode 会因连接关闭把 server 置 failed 并移除工具,不自动重连)。
314
+ - **stdio 的边界(已实测)**:IDE 中途退出后,桥进程会变成"僵尸"——进程还在、调用挂起、**不回任何错误**。两种情况都实测过:**调用之后**IDE 挂、
315
+ 以及**调用进行中**IDE 挂(在 IDE 里跑 `sleep 120` 时 `kill` 掉 IDEA),桥都只在 stderr 打一行错误(`SseClientTransport is closed!` /
316
+ `Request timeout has expired`),**stdout 一个字节都不回**。而且**重开 IDEA 也不会自愈**(实测:重开 IDEA 后连续探测 5 分钟仍全部 TIMEOUT);
317
+ OpenCode 依旧显示 connected,`refresh`/`reload` 只会 `reconcile`,配置没变就跳过,不会重启它。所以:
318
+ - **后台心跳(见上)自动兜底**:5s 探活、连续 2 次不通就注销 `idea`(杀僵尸桥 + 移除工具),下一条消息自动重建 —— 常态下**不需要用户手动做什么**;
319
+ - 手动重建仍可用:重开 IDEA 后再执行一次 `/open-in-idea`(先注销旧 `idea`、再重新注册);
320
+ - 插件给 `idea` server 配 `executionTimeoutMs`(默认 **10 分钟**)作为硬上限:兜住"心跳尚未触发、工具已在途"的那次挂起,不再沿用 OpenCode 默认的 12 小时;
321
+ - "IDE 被关掉 → 工具挂起"这类**不会**走工具失败分类(没有 error);能自动分类处理的是"IDE 在、项目没打开"和"server 掉了(启动即失败)"。
270
322
  - 项目标记存在插件名下 KV (`plugin:opencode-idea:project/<项目根路径>`)。插件实例按 **Location**(项目/打开目录)创建,标记按
271
- **项目根路径**共享;但两个不同 Location(例如项目根与某子目录)各有自己的内存缓存 —— 在其中一个执行 open/close 命令,另一个
272
- 实例要等它下次回源(目前只在 setup 读一次)才会看到。单目录打开项目的常见用法不受影响。
273
- - `/close-in-idea` **只删标记,不断开已建立的连接**:已注册的 IDEA 工具会保留到连接自然中断,期间标记与实际连接可能短暂不一致。
323
+ **项目根路径**共享;但两个不同 Location(例如项目根与某子目录)各有自己的内存缓存 —— 单目录打开项目的常见用法不受影响。
324
+ - `/close-in-idea` 会**注销 IDE MCP** 并删标记 + 清空注入的环境。
274
325
  - 插件**完全不再检查 `.idea` 目录**:是否是 IDEA 项目只由项目标记(`/open-in-idea` 设置、`/close-in-idea` 清除)决定。
275
326
  (`/open-in-idea` 会让 IDEA 打开并导入该项目,`.idea` 由 IDEA 自己生成,插件无需也不应据此判断。)
276
327
  - 打开 IDE 仅实现 macOS (`open -a`);首次打开若弹 Trust 对话框,需确认后重试。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-idea",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "OpenCode plugin that connects to JetBrains IDE MCP servers and prefers IDE tools when the current project is open in the IDE",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -82,6 +82,16 @@ export const MCP_RECOVERY_GUIDANCE = [
82
82
  'If an idea.* / idea_* tool is missing, MCP is disconnected, the connection is refused, or a tool times out: stop retrying immediately; do not run extra probes or wait in tool calls. Tell the user that IDEA MCP is unavailable and ask them to run /open-in-idea to reconnect.',
83
83
  ].join('\n');
84
84
 
85
+ /**
86
+ * Recovery instruction for a marked project, where the plugin reconnects the
87
+ * IDE MCP on its own. The model must not fall back to "ask the user" when a
88
+ * single connection error is all it saw — the reconnect is already happening.
89
+ */
90
+ export const MCP_RECOVERY_GUIDANCE_MARKED = [
91
+ '### IDEA MCP failure recovery',
92
+ 'This project is IDEA-connected and the plugin reconnects the IDE MCP automatically. When an idea_* call fails with a connection error (for example `MCP server "idea" is not available`), do not run extra probes or wait in tool calls: the reconnect is already under way, so retry the operation once. Only if it still fails, tell the user that IDEA MCP is unavailable and to run /open-in-idea.',
93
+ ].join('\n');
94
+
85
95
  /**
86
96
  * Push the guidance onto a session `system` array. Called for every primary
87
97
  * model request, so it stays idempotent per request.
@@ -97,9 +107,13 @@ export function appendIdeGuidance(system, projectPath) {
97
107
  system.push({ type: 'text', text });
98
108
  }
99
109
 
100
- /** @param {unknown} system */
101
- export function appendIdeRecoveryGuidance(system) {
110
+ /**
111
+ * @param {unknown} system
112
+ * @param {{ marked?: boolean }} [options] `marked` picks the auto-reconnect variant
113
+ */
114
+ export function appendIdeRecoveryGuidance(system, { marked = false } = {}) {
102
115
  if (!Array.isArray(system)) return;
103
- if (system.some((part) => part?.text === MCP_RECOVERY_GUIDANCE)) return;
104
- system.push({ type: 'text', text: MCP_RECOVERY_GUIDANCE });
116
+ const text = marked ? MCP_RECOVERY_GUIDANCE_MARKED : MCP_RECOVERY_GUIDANCE;
117
+ if (system.some((part) => part?.text === text)) return;
118
+ system.push({ type: 'text', text });
105
119
  }
@@ -12,6 +12,8 @@
12
12
  // unavailable; this module only requests opening/activating the project.
13
13
 
14
14
  import { spawn } from 'node:child_process';
15
+ import { readdirSync, statSync } from 'node:fs';
16
+ import path from 'node:path';
15
17
 
16
18
  /** Default macOS app name for IntelliJ IDEA. */
17
19
  export const DEFAULT_IDE_APP = 'IntelliJ IDEA';
@@ -35,6 +37,62 @@ export function resolveIdeApp(value) {
35
37
  return IDE_APPS[value.toLowerCase()] ?? value;
36
38
  }
37
39
 
40
+ /** Preferred launcher binary name per app, used when `Contents/MacOS` has several. */
41
+ const IDE_EXECUTABLES = {
42
+ 'IntelliJ IDEA': 'idea',
43
+ PyCharm: 'pycharm',
44
+ 'PyCharm Community': 'pycharm',
45
+ WebStorm: 'webstorm',
46
+ GoLand: 'goland',
47
+ CLion: 'clion',
48
+ PhpStorm: 'phpstorm',
49
+ RubyMine: 'rubymine',
50
+ Rider: 'rider',
51
+ DataGrip: 'datagrip',
52
+ 'Android Studio': 'studio',
53
+ };
54
+
55
+ /**
56
+ * Absolute path to the IDE launcher binary (`<App>.app/Contents/MacOS/<bin>`).
57
+ * The stdio bridge is started by running this binary with `stdioMcpServer`.
58
+ *
59
+ * Returns undefined when the app bundle cannot be found — the caller then falls
60
+ * back to the remote transport.
61
+ *
62
+ * @param {string | undefined} app macOS app name, e.g. "IntelliJ IDEA"
63
+ * @param {{ dirs?: string[] }} [options] search roots (defaults to the two
64
+ * standard Applications folders); tests pass their own.
65
+ * @returns {string | undefined}
66
+ */
67
+ export function resolveIdeExecutable(app, options = {}) {
68
+ if (typeof app !== 'string' || app.length === 0) return undefined;
69
+ const home = process.env.HOME;
70
+ const dirs = options.dirs ?? [home ? path.join(home, 'Applications') : undefined, '/Applications'].filter(
71
+ (value) => typeof value === 'string',
72
+ );
73
+ const preferred = IDE_EXECUTABLES[app];
74
+ for (const dir of dirs) {
75
+ const macos = path.join(dir, `${app}.app`, 'Contents', 'MacOS');
76
+ let entries;
77
+ try {
78
+ entries = readdirSync(macos);
79
+ } catch {
80
+ continue;
81
+ }
82
+ const executable = (name) => {
83
+ try {
84
+ const stats = statSync(path.join(macos, name));
85
+ return stats.isFile() && (stats.mode & 0o111) !== 0;
86
+ } catch {
87
+ return false;
88
+ }
89
+ };
90
+ const chosen = (preferred && entries.includes(preferred) && preferred) || entries.find(executable);
91
+ if (chosen) return path.join(macos, chosen);
92
+ }
93
+ return undefined;
94
+ }
95
+
38
96
  /**
39
97
  * Open `directory` in the IDE, reusing a running instance when possible.
40
98
  * Never throws; resolves `true` when `open` reported success.
package/src/mcp/idea.js CHANGED
@@ -6,11 +6,10 @@
6
6
  // * The IDE exposes a local MCP server. IntelliJ 2026.2+ exposes the modern
7
7
  // Streamable-HTTP endpoint at `http://127.0.0.1:<port>/stream`; older builds
8
8
  // only had the legacy SSE endpoint at `/sse`.
9
- // * OpenCode V2's remote MCP client speaks ONLY Streamable HTTP, so the plugin
10
- // registers `/stream` (see `serverConfig`). The legacy `/sse` endpoint is
11
- // unusable from OpenCode (POST returns 405 → "Error POSTing to endpoint").
12
- // * The project a call targets is selected by the HTTP header
13
- // `IJ_MCP_SERVER_PROJECT_PATH`.
9
+ // * The project a call targets is selected by the project path: the HTTP
10
+ // header `IJ_MCP_SERVER_PROJECT_PATH` for the remote transport, or the
11
+ // `IJ_MCP_SERVER_PROJECT_PATH` environment variable for the stdio bridge
12
+ // (see `serverConfig` / `stdioServerConfig`).
14
13
  // * Availability probe: a single `initialize` POST to `/stream` succeeds when
15
14
  // the MCP server is up. We do NOT check whether a specific project is open —
16
15
  // the user triggers `/open-in-idea` for the project they want, so "server is
@@ -18,6 +17,15 @@
18
17
 
19
18
  export const PROJECT_HEADER = 'IJ_MCP_SERVER_PROJECT_PATH';
20
19
 
20
+ /** Environment variable the IDE's stdio bridge uses to select the project. */
21
+ export const PROJECT_ENV = 'IJ_MCP_SERVER_PROJECT_PATH';
22
+
23
+ /** Environment variable the IDE's stdio bridge uses to reach the running server. */
24
+ export const PORT_ENV = 'IJ_MCP_SERVER_PORT';
25
+
26
+ /** Argument that puts the IDE launcher into its stdio MCP bridge mode. */
27
+ export const STDIO_ARG = 'stdioMcpServer';
28
+
21
29
  /** OpenCode MCP server name registered for the IDE. */
22
30
  export const IDEA_SERVER_NAME = 'idea';
23
31
 
@@ -44,16 +52,73 @@ const CALL_TIMEOUT_MS = 15000;
44
52
  *
45
53
  * @param {number} port
46
54
  * @param {string} projectPath
55
+ * @param {{ executionTimeoutMs?: number }} [options]
47
56
  */
48
- export function serverConfig(port, projectPath) {
49
- return {
57
+ export function serverConfig(port, projectPath, options = {}) {
58
+ /** @type {Record<string, unknown>} */
59
+ const config = {
50
60
  type: 'remote',
51
61
  url: `http://127.0.0.1:${port}${MCP_STREAM_PATH}`,
52
62
  headers: { [PROJECT_HEADER]: projectPath },
53
63
  codemode: false,
54
64
  };
65
+ return withExecutionTimeout(config, options);
55
66
  }
56
67
 
68
+ /**
69
+ * Build the OpenCode MCP server config for the IDE's **stdio bridge**.
70
+ *
71
+ * The IDE launcher (`<App>.app/Contents/MacOS/<binary>`) with the
72
+ * `stdioMcpServer` argument runs a small proxy that speaks MCP over
73
+ * stdin/stdout and connects to the running IDE MCP server selected by the
74
+ * environment. That makes the client side a child-process pipe instead of a
75
+ * Streamable-HTTP stream, so it does not suffer the idle-close that the remote
76
+ * transport does. The project and port are carried in the environment, matching
77
+ * what the remote config carries in a header and URL.
78
+ *
79
+ * @param {string} executable absolute path to the IDE launcher
80
+ * @param {number} port IDE MCP port to bridge to
81
+ * @param {string} projectPath project root the bridge should target
82
+ * @param {{ executionTimeoutMs?: number }} [options]
83
+ */
84
+ export function stdioServerConfig(executable, port, projectPath, options = {}) {
85
+ /** @type {Record<string, unknown>} */
86
+ const config = {
87
+ type: 'local',
88
+ // OpenCode's `LocalConfig.command` is a STRING ARRAY holding the executable
89
+ // followed by its arguments (`client.ts` does `const [command, ...args] =
90
+ // config.command`). There is no separate `args` field — passing one, or a
91
+ // bare string command, fails schema validation and the server never spawns.
92
+ command: [executable, STDIO_ARG],
93
+ environment: {
94
+ [PROJECT_ENV]: projectPath,
95
+ [PORT_ENV]: String(port),
96
+ },
97
+ codemode: false,
98
+ };
99
+ return withExecutionTimeout(config, options);
100
+ }
101
+
102
+ /**
103
+ * Attach OpenCode's per-server execution timeout to an MCP config.
104
+ *
105
+ * OpenCode defaults this to **12 hours**. The IDE's stdio bridge never answers a
106
+ * call whose upstream IDE connection has dropped (it logs on stderr but sends no
107
+ * JSON-RPC response), so without a cap a call made after the IDE died would sit
108
+ * unresolved for the whole default. A finite cap turns that permanent hang into
109
+ * an ordinary timeout error.
110
+ *
111
+ * @param {Record<string, unknown>} config
112
+ * @param {{ executionTimeoutMs?: number }} options
113
+ */
114
+ function withExecutionTimeout(config, options) {
115
+ if (typeof options.executionTimeoutMs === 'number') {
116
+ return { ...config, timeout: { execution: options.executionTimeoutMs } };
117
+ }
118
+ return config;
119
+ }
120
+
121
+
57
122
  /**
58
123
  * Is an IDE MCP server listening on `port`? One Streamable-HTTP `initialize`
59
124
  * POST decides it: any 2xx JSON reply means the server is up.
@@ -81,7 +146,7 @@ export async function probePort(port, options = {}) {
81
146
  params: {
82
147
  protocolVersion: PROTOCOL_VERSION,
83
148
  capabilities: {},
84
- clientInfo: { name: 'opencode-idea', version: '0.0.10' },
149
+ clientInfo: { name: 'opencode-idea', version: '0.0.11' },
85
150
  },
86
151
  }),
87
152
  signal: controller.signal,
@@ -151,7 +216,7 @@ export async function callTool(port, projectPath, name, args = {}, options = {})
151
216
  params: {
152
217
  protocolVersion: PROTOCOL_VERSION,
153
218
  capabilities: {},
154
- clientInfo: { name: 'opencode-idea', version: '0.0.10' },
219
+ clientInfo: { name: 'opencode-idea', version: '0.0.11' },
155
220
  },
156
221
  });
157
222
  const response = await post({
package/src/plugin.js CHANGED
@@ -3,9 +3,10 @@
3
3
  // For a project the user opted into IDEA this plugin:
4
4
  // 1. injects the IDE integrated-terminal environment (JAVA_HOME / GOROOT /
5
5
  // Node / Maven / ...) into every OpenCode shell;
6
- // 2. registers the IDE MCP server with OpenCode (scoped to the project via the
7
- // IJ_MCP_SERVER_PROJECT_PATH header), injects direct-call guidance for
8
- // IDEA MCP tools, and updates only IDEA MCP tool descriptions;
6
+ // 2. registers the IDE MCP server with OpenCode — by default as the IDE's
7
+ // stdio bridge (`idea stdioMcpServer`), scoped to the project through
8
+ // `IJ_MCP_SERVER_PROJECT_PATH` — injects direct-call guidance for IDEA MCP
9
+ // tools, and updates only IDEA MCP tool descriptions;
9
10
  // 3. registers the `/open-in-idea` command, which opens the current project
10
11
  // in IntelliJ IDEA (reusing a running instance), loads the IDE
11
12
  // capabilities immediately, and marks the project as an IDEA project;
@@ -19,13 +20,17 @@
19
20
  // when a probe is requested). `/open-in-idea` probes, opens the IDE, and loads
20
21
  // the IDE MCP + guidance on demand.
21
22
  //
22
- // Once the user runs `/open-in-idea` in a project, that project is marked as an
23
- // IDEA project. The mark is persisted through `ctx.storage` (durable across
24
- // restarts) and cleared by `/close-in-idea`. While a project is marked, every
25
- // user message assumes the IDE is still up: the plugin asks OpenCode for the
26
- // `idea` MCP server's status and, when it is not connected, re-runs the open
27
- // flow before the request is built. Detection is by MCP status, never by tool
28
- // errors, so a normal business error can never look like a dropped connection.
23
+ // `/open-in-idea` opens the project in the IDE, waits until its MCP endpoint is
24
+ // ready, registers `idea` (stdio, project-scoped) and marks the project in
25
+ // `ctx.storage`; `/close-in-idea` unregisters the server and clears the mark.
26
+ // The mark gates the IDEA skills and the failure-driven recovery. After a manual
27
+ // open, an `idea_*` call that fails because the IDE MCP is not usable (the server
28
+ // dropped, or the project is not open in the IDE) re-runs the open flow; a plain
29
+ // business error is classified apart and left alone. Because a stdio bridge whose
30
+ // IDE connection drops never reports it (it hangs), a background heartbeat probes
31
+ // the IDE endpoint and, once it is clearly gone, deactivates the server — killing
32
+ // the zombie bridge and removing the tools so a call cannot hang — and the next
33
+ // user message rebuilds it.
29
34
  //
30
35
  // `Plugin.define` from `@opencode/plugin` is an identity function, so a plain
31
36
  // `{ id, setup }` object is the whole plugin contract. Exporting it directly
@@ -36,14 +41,17 @@ import {
36
41
  IDEA_SERVER_NAME,
37
42
  findIdePort,
38
43
  serverConfig,
44
+ stdioServerConfig,
39
45
  } from './mcp/idea.js';
40
46
  import { readIdeTerminalEnv } from './mcp/ide-env.js';
41
47
  import { currentProjectPath } from './project.js';
42
48
  import { applyEnv, mergeEnv } from './env.js';
43
49
  import {
44
50
  createLaunchGuard,
51
+ DEFAULT_IDE_APP,
45
52
  openInIde,
46
53
  resolveIdeApp,
54
+ resolveIdeExecutable,
47
55
  } from './ide-launcher.js';
48
56
  import {
49
57
  appendIdeGuidance,
@@ -62,6 +70,20 @@ const DEFAULT_MCP_START_TIMEOUT_MS = 60000;
62
70
  const MCP_START_POLL_MS = 1000;
63
71
  const DEFAULT_TOOL_REFRESH_TIMEOUT_MS = 5000;
64
72
  const TOOL_REFRESH_POLL_MS = 25;
73
+ // Hard cap on ONE IDE MCP tool call. OpenCode's own default is 12 hours, and the
74
+ // IDE's stdio bridge NEVER answers a call once its upstream IDE connection has
75
+ // dropped (it logs on stderr but sends no JSON-RPC response) — so a call made
76
+ // after the IDE died would otherwise sit unresolved for 12h. 10 minutes is far
77
+ // beyond any normal IDE tool and turns that permanent hang into a normal error.
78
+ const DEFAULT_EXECUTION_TIMEOUT_MS = 600000;
79
+ // Background IDE liveness probe. The stdio bridge NEVER reports a dropped IDE
80
+ // connection (it just hangs), so the plugin watches the IDE endpoint itself.
81
+ // After HEARTBEAT_FAILURES consecutive misses it deactivates the `idea` server
82
+ // — which kills the zombie bridge child AND removes the `idea_*` tools, so the
83
+ // model can never call into a dead pipe — and latches a rebuild for the next
84
+ // user message (the bridge never heals on its own).
85
+ const DEFAULT_HEARTBEAT_INTERVAL_MS = 5000;
86
+ const DEFAULT_HEARTBEAT_FAILURES = 2;
65
87
  const IDEA_READY_TOOL_IDS = new Set(['idea_read_file', 'idea_apply_patch']);
66
88
 
67
89
  /**
@@ -156,13 +178,97 @@ function toolIds(tools) {
156
178
  return (tools ?? []).map((tool) => tool.id).filter((id) => typeof id === 'string').sort();
157
179
  }
158
180
 
181
+ /**
182
+ * Classify an `idea_*` tool failure for the reconnect decision.
183
+ *
184
+ * It must never mistake a plain business error (a bad path, a missing file) for
185
+ * a broken connection, so only two shapes trigger action:
186
+ *
187
+ * - `project`: the IDE is up but the requested project is not open in it, e.g.
188
+ * "`projectPath`=... doesn't correspond to any open project." Re-opening the
189
+ * project fixes it.
190
+ * - `mcp`: the IDE MCP server itself is gone (dropped / transport closed).
191
+ *
192
+ * Everything else is `none`.
193
+ *
194
+ * @param {{ message?: unknown } | undefined} error
195
+ * @returns {'project' | 'mcp' | 'none'}
196
+ */
197
+ function classifyIdeaFailure(error) {
198
+ const message = typeof error?.message === 'string' ? error.message : '';
199
+ // "Project not open in the IDE" comes in two shapes, both meaning the same
200
+ // thing and both fixed by opening the project again:
201
+ // - the called tool carried no `projectPath`, so the server could not pick
202
+ // one: "Unable to determine the target project for the current MCP tool
203
+ // call." (this is what the plugin's stdio registration produces, since it
204
+ // sets the project through env, not through the call)
205
+ // - an explicit `projectPath` was passed but is not open: "`projectPath`=...
206
+ // doesn't correspond to any open project."
207
+ if (
208
+ message.includes('Unable to determine the target project') ||
209
+ message.includes('correspond to any open project')
210
+ ) {
211
+ return 'project';
212
+ }
213
+ // Only transport-level markers, which OpenCode / the IDE bridge emit
214
+ // themselves. The message of an MCP *tool error* is the tool's own content
215
+ // (`tool/mcp.ts` turns `result.isError` into `ToolFailure({ message: <text> })`),
216
+ // so a generic string such as "Connection refused" from a SQL or run-config
217
+ // tool would otherwise be misread as a dropped connection and rebuild a
218
+ // healthy bridge.
219
+ if (
220
+ message.includes(`MCP server "${IDEA_SERVER_NAME}" is not available`) ||
221
+ message.includes('MCP server is not connected') ||
222
+ message.includes('Error POSTing to endpoint') ||
223
+ message.includes('SseClientTransport is closed')
224
+ ) {
225
+ return 'mcp';
226
+ }
227
+ return 'none';
228
+ }
229
+
230
+ /**
231
+ * Replace a tool error's message, so the model sees the replacement. OpenCode
232
+ * returns `event.error` after the `execute.after` hook resolves, so rewriting it
233
+ * is how the plugin steers the model after an automatic reconnect.
234
+ *
235
+ * The error is a `Tool.Error` instance (`ToolFailure` extends it). It is rebuilt
236
+ * through its own constructor — no runtime dependency on `@opencode/ai` — with a
237
+ * direct field mutation as a fallback.
238
+ *
239
+ * @param {{ error?: unknown }} event
240
+ * @param {string} message
241
+ * @returns {boolean}
242
+ */
243
+ function rewriteToolError(event, message) {
244
+ const current = event?.error;
245
+ if (!current || typeof current !== 'object') return false;
246
+ try {
247
+ const Ctor = current.constructor;
248
+ if (typeof Ctor === 'function') {
249
+ const next = { message };
250
+ if (current.error !== undefined) next.error = current.error;
251
+ if (current.metadata !== undefined) next.metadata = current.metadata;
252
+ event.error = new Ctor(next);
253
+ return true;
254
+ }
255
+ } catch {
256
+ // fall through to mutation
257
+ }
258
+ try {
259
+ current.message = message;
260
+ return true;
261
+ } catch {
262
+ return false;
263
+ }
264
+ }
265
+
159
266
  /**
160
267
  * @typedef {{
161
268
  * status: 'connected' | 'tools-loading' | 'mcp-unavailable' | 'launch-failed' | 'disabled' | 'closed',
162
269
  * port?: number,
163
270
  * opened?: boolean,
164
- * unavailableNoticeSent?: boolean,
165
- * autoRecovered?: boolean
271
+ * unavailableNoticeSent?: boolean
166
272
  * }} IdeCommandResult
167
273
  */
168
274
 
@@ -182,9 +288,6 @@ const asPositiveNumber = (value, fallback) =>
182
288
  /** User-facing status text for a `/open-in-idea` result. */
183
289
  /** @param {IdeCommandResult} result */
184
290
  export function ideFeedback(result) {
185
- if (result?.autoRecovered) {
186
- return '检测到 IDE MCP 连接已断开,已自动重新连接并恢复 IDEA 原生工具。';
187
- }
188
291
  switch (result?.status) {
189
292
  case 'connected':
190
293
  return 'IDE MCP 已连接;IDEA 原生工具已注册并可用于后续请求。';
@@ -199,7 +302,7 @@ export function ideFeedback(result) {
199
302
  case 'disabled':
200
303
  return '插件已通过 openInIde: false 关闭 IDE 启动。请手动打开 IntelliJ IDEA,在 Settings → MCP Server 开启 MCP 服务并启用 Brave Mode,然后重试 /open-in-idea。';
201
304
  case 'closed':
202
- return '已关闭当前项目的 IDEA 接入标记;IDE MCP 断开后将不再自动重连。如需重新接入,请再次执行 /open-in-idea。';
305
+ return '已关闭当前项目的 IDEA 接入:已注销 IDE MCP 并清除项目标记。如需重新接入,请再次执行 /open-in-idea。';
203
306
  default:
204
307
  return '已处理 /open-in-idea。';
205
308
  }
@@ -210,8 +313,11 @@ export function ideFeedback(result) {
210
313
  * notification, not a task: the model must only acknowledge it. The user sees
211
314
  * the clean notice instead, via `metadata.displayText`.
212
315
  */
316
+ /** Marks the plugin's own session notices, so recovery never reacts to them. */
317
+ export const NOTICE_PREFIX = '【插件通知】';
318
+
213
319
  export function idePromptText(result) {
214
- return [`【插件通知】${ideFeedback(result)}`, '这是发给用户的通知,你无需处理。请只回复"收到"。'].join('\n\n');
320
+ return [`${NOTICE_PREFIX}${ideFeedback(result)}`, '这是发给用户的通知,你无需处理。请只回复"收到"。'].join('\n\n');
215
321
  }
216
322
 
217
323
  /** `metadata` so the UI shows the clean notice while the model sees the instruction. */
@@ -234,6 +340,11 @@ export default {
234
340
  * mcpProbeTimeoutMs?: number,
235
341
  * mcpStartTimeoutMs?: number,
236
342
  * toolRefreshTimeoutMs?: number,
343
+ * executionTimeoutMs?: number,
344
+ * heartbeatIntervalMs?: number,
345
+ * heartbeatFailures?: number,
346
+ * transport?: 'stdio' | 'http',
347
+ * ideExecutable?: string,
237
348
  * feedback?: false | 'message',
238
349
  * },
239
350
  * command?: {
@@ -260,13 +371,17 @@ export default {
260
371
  * agent?: string,
261
372
  * system?: unknown[],
262
373
  * tools?: Record<string, unknown>,
263
- * prompt?: { text?: string },
264
- * metadata?: Record<string, unknown>,
265
374
  * }) => void | Promise<void>) => Promise<{ dispose: () => Promise<void> | void }>,
266
375
  * },
267
376
  * tool?: {
268
377
  * list?: () => Promise<ReadonlyArray<{ id?: string }>>,
269
378
  * reload?: () => Promise<void>,
379
+ * hook?: (name: string, cb: (event: {
380
+ * tool?: string,
381
+ * sessionID?: string,
382
+ * status?: string,
383
+ * error?: unknown,
384
+ * }) => void | Promise<void>) => Promise<{ dispose: () => Promise<void> | void }>,
270
385
  * },
271
386
  * mcp: {
272
387
  * transform: (cb: (editor: { set: (name: string, config: unknown) => void }) => void) => Promise<{ dispose: () => Promise<void> | void }>,
@@ -290,9 +405,28 @@ export default {
290
405
  const injectEnv = options.injectEnv !== false;
291
406
  const injectGuidance = options.injectGuidance !== false;
292
407
  const ideApp = resolveIdeApp(options.openInIde);
408
+ // Transport for the IDE MCP server. `stdio` (default) registers the IDE's
409
+ // stdio bridge as a local server: the client side is a child-process pipe,
410
+ // which — unlike the remote Streamable-HTTP stream — does not get closed
411
+ // while idle and never reconnected. `http` keeps the older remote config.
412
+ const transport = options.transport === 'http' ? 'http' : 'stdio';
413
+ const ideExecutable = typeof options.ideExecutable === 'string'
414
+ ? options.ideExecutable
415
+ // Resolve independently of `openInIde`: that option only controls whether
416
+ // the plugin may LAUNCH the IDE, not which transport the MCP server uses.
417
+ // Falling back to the default app when launching is disabled keeps stdio
418
+ // the default there too (otherwise `openInIde: false` silently registered
419
+ // the remote HTTP transport).
420
+ : resolveIdeExecutable(ideApp ?? DEFAULT_IDE_APP);
421
+ const useStdio = transport === 'stdio' && typeof ideExecutable === 'string';
293
422
  const mcpProbeTimeoutMs = asPositiveNumber(options.mcpProbeTimeoutMs, DEFAULT_MCP_PROBE_TIMEOUT_MS);
294
423
  const mcpStartTimeoutMs = asPositiveNumber(options.mcpStartTimeoutMs, DEFAULT_MCP_START_TIMEOUT_MS);
295
424
  const toolRefreshTimeoutMs = asPositiveNumber(options.toolRefreshTimeoutMs, DEFAULT_TOOL_REFRESH_TIMEOUT_MS);
425
+ // Cap on a single IDE tool call, so a bridge that hangs (IDE died) fails in
426
+ // finite time instead of OpenCode's 12h default. See DEFAULT_EXECUTION_TIMEOUT_MS.
427
+ const executionTimeoutMs = asPositiveNumber(options.executionTimeoutMs, DEFAULT_EXECUTION_TIMEOUT_MS);
428
+ const heartbeatIntervalMs = asPositiveNumber(options.heartbeatIntervalMs, DEFAULT_HEARTBEAT_INTERVAL_MS);
429
+ const heartbeatFailures = asPositiveNumber(options.heartbeatFailures, DEFAULT_HEARTBEAT_FAILURES);
296
430
  // "message" (default) injects a clearly-labelled notification that the
297
431
  // model only acknowledges ("收到"); the user sees the clean notice via
298
432
  // metadata.displayText. false is silent.
@@ -310,6 +444,8 @@ export default {
310
444
  /** @type {{ dispose: () => Promise<void> | void } | undefined} */
311
445
  let sessionRegistration;
312
446
  /** @type {{ dispose: () => Promise<void> | void } | undefined} */
447
+ let toolRegistration;
448
+ /** @type {{ dispose: () => Promise<void> | void } | undefined} */
313
449
  let promptRegistration;
314
450
  /** @type {{ dispose: () => Promise<void> | void } | undefined} */
315
451
  let commandRegistration;
@@ -318,6 +454,15 @@ export default {
318
454
  let skillsRegistered = false;
319
455
  let disposed = false;
320
456
 
457
+ // Background heartbeat state (see DEFAULT_HEARTBEAT_INTERVAL_MS).
458
+ /** @type {ReturnType<typeof setInterval> | undefined} */
459
+ let heartbeatTimer;
460
+ let heartbeatMisses = 0;
461
+ // Latches when the IDE was seen down and the server was deactivated. It is
462
+ // cleared ONLY by a successful rebuild — never by the endpoint coming back —
463
+ // because the bridge does not heal on its own.
464
+ let needsRebuild = false;
465
+
321
466
  // Project-level mark: has the user opted this project into IDEA via
322
467
  // `/open-in-idea`? One plugin instance serves one project (OpenCode
323
468
  // activates plugins per Location), so a single boolean is the whole cache.
@@ -328,9 +473,6 @@ export default {
328
473
  const markKey = `project/${projectPath}`;
329
474
  /** @type {boolean | undefined} undefined = a storage read is due */
330
475
  let markCache;
331
- // One auto-recovery run per project at a time; overlapping prompts share it.
332
- /** @type {Promise<void> | undefined} */
333
- let autoRecovering;
334
476
 
335
477
  /** Read the durable mark into the cache; at most one storage read per invalidation. */
336
478
  const readMark = async () => {
@@ -361,7 +503,9 @@ export default {
361
503
  } catch {
362
504
  // best effort
363
505
  }
364
- markCache = undefined;
506
+ // Keep the in-memory cache in sync with the write (write-through), so any
507
+ // later synchronous reader sees the new value immediately.
508
+ markCache = marked;
365
509
  // The skill lifecycle follows the mark: marking the project registers the
366
510
  // IDEA skills, clearing the mark removes them again. Same semantics as the
367
511
  // reconnect behavior, so the two can never disagree.
@@ -411,29 +555,16 @@ export default {
411
555
  };
412
556
 
413
557
  const activateIde = async (port) => {
558
+ const config = useStdio
559
+ ? stdioServerConfig(ideExecutable, port, projectPath, { executionTimeoutMs })
560
+ : serverConfig(port, projectPath, { executionTimeoutMs });
414
561
  const mcpRegistration = await ctx.mcp.transform((editor) => {
415
- editor.set(IDEA_SERVER_NAME, serverConfig(port, projectPath));
562
+ editor.set(IDEA_SERVER_NAME, config);
416
563
  });
417
564
  registrations = [mcpRegistration];
418
565
  return refreshIdeTools();
419
566
  };
420
567
 
421
- /**
422
- * Is the IDE MCP server contributing tools right now? OpenCode's registry is
423
- * the source of truth: it adds the tools when a server connects and drops
424
- * them when the connection stops (`stopServer` publishes `mcp.tools.changed`
425
- * and the registry reloads). No probe of our own is involved, and only
426
- * `/open-in-idea` asks, so the read is user-initiated.
427
- */
428
- const ideaToolsRegistered = async () => {
429
- if (typeof ctx.tool?.list !== 'function') return false;
430
- try {
431
- return toolIds(await ctx.tool.list()).some((id) => id.startsWith('idea_'));
432
- } catch {
433
- return false;
434
- }
435
- };
436
-
437
568
  const reconcileOnce = async ({ probeIde = true } = {}) => {
438
569
  if (disposed) return false;
439
570
 
@@ -458,10 +589,15 @@ export default {
458
589
  shellRegistration = await ctx.shell.hook('create.before', (input) => applyEnv(input, currentEnv));
459
590
  }
460
591
 
461
- // A live server is only refreshed; a dead one must be replaced, because
462
- // `reconcile()` skips an unchanged config and would never reconnect it.
463
- // The registry says which case this is.
464
- if (port !== undefined && (await ideaToolsRegistered())) return refreshIdeTools();
592
+ // Always tear the server down and register it again. A stdio bridge whose
593
+ // IDE connection dropped becomes a permanent zombie: the child process is
594
+ // still alive, so OpenCode keeps reporting it as connected, never restarts
595
+ // it, and SKIPS a re-`set` whose config is unchanged (`reconcile()` compares
596
+ // the config). Disposing the registration removes the server from the
597
+ // reconciled config, which is what actually kills the child process — so a
598
+ // full rebuild is the only reliable way back to a working bridge.
599
+ // `/open-in-idea` is manual and rare; rebuilding an already-healthy bridge
600
+ // costs one restart, which is the right trade for never leaving a zombie.
465
601
  await deactivateIde();
466
602
  if (port === undefined) return false;
467
603
  return activateIde(port);
@@ -587,6 +723,48 @@ export default {
587
723
  // Setup never probes or connects the IDE — everything is on `/open-in-idea`.
588
724
  await reconcile({ probeIde: false });
589
725
 
726
+ /**
727
+ * One heartbeat tick. Only marked projects are watched, and it only acts
728
+ * when an `idea` server is actually registered: the point is to protect an
729
+ * ESTABLISHED connection. A project that never connected has no zombie to
730
+ * kill, and must not have IDEA launched on its behalf. The deactivation
731
+ * runs under the reconcile chain, so a tick cannot race `/open-in-idea`.
732
+ */
733
+ let heartbeatBusy = false;
734
+ const heartbeatTick = async () => {
735
+ if (disposed || heartbeatBusy) return;
736
+ if (markCache !== true || registrations.length === 0) {
737
+ heartbeatMisses = 0;
738
+ return;
739
+ }
740
+ heartbeatBusy = true;
741
+ try {
742
+ const port = await findIdePort(ports, { timeoutMs: mcpProbeTimeoutMs }).catch(() => undefined);
743
+ if (port !== undefined) {
744
+ heartbeatMisses = 0;
745
+ return;
746
+ }
747
+ heartbeatMisses += 1;
748
+ if (heartbeatMisses < heartbeatFailures) return;
749
+ // The IDE is gone. Deactivating kills the zombie bridge child and
750
+ // removes the `idea_*` tools, so the model cannot call into a dead
751
+ // pipe. Latch a rebuild for the next user message.
752
+ const kicked = await enqueue(async () => {
753
+ if (disposed || registrations.length === 0) return false;
754
+ await deactivateIde();
755
+ return true;
756
+ });
757
+ if (kicked) needsRebuild = true;
758
+ } finally {
759
+ heartbeatBusy = false;
760
+ }
761
+ };
762
+ heartbeatTimer = setInterval(() => {
763
+ heartbeatTick().catch(() => {});
764
+ }, heartbeatIntervalMs);
765
+ // Never keep the host process alive just to probe (also keeps tests from hanging).
766
+ if (typeof heartbeatTimer?.unref === 'function') heartbeatTimer.unref();
767
+
590
768
  const sendFeedback = async (input, result) => {
591
769
  if (feedback !== 'message' || typeof ctx.session?.prompt !== 'function') return false;
592
770
  try {
@@ -604,71 +782,6 @@ export default {
604
782
  }
605
783
  };
606
784
 
607
- /**
608
- * Is the plugin's `idea` MCP server connected right now? OpenCode tracks
609
- * every server's status in memory, and this is the authoritative "the IDE
610
- * MCP is down" signal: it reflects the transport, not the result of any
611
- * single tool call, so a business error can never look like a disconnect.
612
- * `pending` means "still connecting" and is treated as healthy, so a server
613
- * that is merely starting up is left alone.
614
- *
615
- * @returns {Promise<boolean>}
616
- */
617
- const isIdeaConnected = async () => {
618
- if (typeof ctx.mcp?.list !== 'function') return false;
619
- try {
620
- const result = await ctx.mcp.list();
621
- const servers = Array.isArray(result) ? result : (result?.data ?? []);
622
- return servers.some((server) => {
623
- const status = server?.status?.status;
624
- return server?.name === IDEA_SERVER_NAME && (status === 'connected' || status === 'pending');
625
- });
626
- } catch {
627
- return false;
628
- }
629
- };
630
-
631
- /**
632
- * Marked-project recovery: while the project is marked (the user ran
633
- * `/open-in-idea`), treat the IDE as expected and rebuild the connection
634
- * whenever its MCP server is not connected. Runs before the prompt's request
635
- * is built, so a successful recovery makes its `idea_*` tools available to
636
- * that very message.
637
- *
638
- * Only a confirmed reconnect is announced. A failed recovery stays silent:
639
- * the per-request recovery guidance already tells the model what to say, and
640
- * a notice per failed message would be noise (the launch guard, shared with
641
- * the command, keeps it from re-spawning on every message).
642
- *
643
- * @param {string} sessionID the notice target; the mark itself is per-project
644
- */
645
- const autoReconnect = (sessionID) => {
646
- if (autoRecovering) return autoRecovering;
647
- const run = (async () => {
648
- try {
649
- if (disposed) return;
650
- if (await isIdeaConnected()) return;
651
- const result = await runOpenInIdea();
652
- // A found endpoint is what counts as recovered, even if its tool
653
- // catalog is still registering (`tools-loading`).
654
- if (result.port === undefined) return;
655
- // Queue the notice so it is delivered after the user's own message,
656
- // never mixed into the same model turn as it.
657
- await sendFeedback(
658
- { sessionID, prompt: { text: '' }, delivery: 'queue' },
659
- { status: 'connected', autoRecovered: true },
660
- );
661
- } catch {
662
- // auto-recovery is best effort
663
- }
664
- })();
665
- autoRecovering = run;
666
- void run.finally(() => {
667
- if (autoRecovering === run) autoRecovering = undefined;
668
- });
669
- return run;
670
- };
671
-
672
785
  // The shipped skills are static knowledge (`skills/<id>/SKILL.md`), e.g. the
673
786
  // run-configuration workflow: where `.run/*.run.xml` live, how to bootstrap a
674
787
  // schema from a real example, and how to verify a write. Registered only for
@@ -738,9 +851,14 @@ export default {
738
851
  });
739
852
  editor.add({
740
853
  name: 'close-in-idea',
741
- description: '取消当前项目的 IDEA 接入标记,断开后不再自动重连',
854
+ description: '取消当前项目的 IDEA 接入:注销 IDE MCP 并清除项目标记',
742
855
  execute: async (input) => {
743
856
  await setMark(false);
857
+ // Unregister the IDE MCP server and clear the injected env. Reuses
858
+ // the serialized reconcile, so a concurrent rebuild (an in-flight
859
+ // `/open-in-idea`, or a heartbeat tick) cannot re-register the
860
+ // server after the mark is gone.
861
+ await reconcile({ probeIde: false });
744
862
  await sendFeedback(input, { status: 'closed' });
745
863
  },
746
864
  });
@@ -762,7 +880,7 @@ export default {
762
880
  const tools = event?.tools;
763
881
  const serving = tools !== undefined && Object.keys(tools).some((name) => name.startsWith('idea_'));
764
882
  if (serving) removeIdeaToolDefinitions(tools, HIDDEN_IDEA_TOOLS);
765
- if (markCache === true || serving) appendIdeRecoveryGuidance(event?.system);
883
+ if (markCache === true || serving) appendIdeRecoveryGuidance(event?.system, { marked: markCache === true });
766
884
  if (!injectGuidance) return;
767
885
  // Guidance promises tools the model can call, so it only goes out when
768
886
  // they are actually in this request.
@@ -770,32 +888,55 @@ export default {
770
888
  appendIdeGuidance(event?.system, projectPath);
771
889
  });
772
890
 
773
- // Project-level auto-recovery. The only self-initiated action in the
774
- // plugin, and only for a marked project. It belongs to the `prompt` hook
775
- // (not `context`) because the tool catalog for a request is already fixed
776
- // by the time `context` runs: a `prompt`-time reconnect is what lets the
777
- // message that triggered it use `idea_*` tools.
778
- //
779
- // The returned promise is awaited by OpenCode before the request is built,
780
- // so a slow cold start delays that message — the accepted cost of treating
781
- // a marked project's IDE as always up.
891
+ // Rebuild after a heartbeat-detected drop. It reads only the in-memory
892
+ // `needsRebuild` latch, so it is a cheap no-op on every other message.
893
+ // OpenCode awaits `prompt` before the request is built, so when the IDE is
894
+ // back, the message that triggers this already carries working tools.
782
895
  promptRegistration = await ctx.session.hook('prompt', async (event) => {
783
- const sessionID = event?.sessionID;
784
- if (typeof sessionID !== 'string') return;
785
- // Never react to the plugin's own notices, or auto-recovery would feed
786
- // on itself. Notices carry `metadata.displayText`; the prefix guards
787
- // against an older notice shape.
788
- const displayText = event?.metadata?.displayText;
789
- if (typeof displayText === 'string') return;
896
+ if (disposed || !needsRebuild || markCache !== true) return;
897
+ // Never react to the plugin's own notices, or recovery would feed on
898
+ // itself. Match the notice TEXT, never `metadata.displayText`: OpenCode
899
+ // also sets `displayText` on ordinary user messages (it is the message
900
+ // text itself), so a displayText guard silently skips every real message
901
+ // — verified live, and it made this rebuild never run.
790
902
  const text = event?.prompt?.text;
791
- if (typeof text === 'string' && text.startsWith('【插件通知】')) return;
792
- if (!(await readMark())) return;
793
- return autoReconnect(sessionID);
903
+ if (typeof text === 'string' && text.startsWith(NOTICE_PREFIX)) return;
904
+ const result = await runOpenInIdea();
905
+ if (result.port !== undefined) {
906
+ needsRebuild = false;
907
+ heartbeatMisses = 0;
908
+ }
794
909
  });
910
+
911
+ // The only self-initiated action: when an `idea_*` call fails in a way
912
+ // that means the IDE MCP is not usable, run the open flow again. The
913
+ // failure is classified — a plain business error (a bad path, a missing
914
+ // file) never triggers a reconnect, only "the MCP is not available" or
915
+ // "the project is not open in the IDE" do.
916
+ if (typeof ctx.tool?.hook === 'function') {
917
+ toolRegistration = await ctx.tool.hook('execute.after', async (event) => {
918
+ if (event?.status !== 'error') return;
919
+ if (typeof event?.tool !== 'string' || !event.tool.startsWith('idea_')) return;
920
+ if (!(await readMark())) return;
921
+ const kind = classifyIdeaFailure(event.error);
922
+ if (kind === 'none') return;
923
+ if (kind === 'project' && ideApp) {
924
+ // The IDE is up; the project simply is not open in it. Ask the IDE
925
+ // to open the project again, and tell the model to retry.
926
+ await openInIde(projectPath, { app: ideApp });
927
+ rewriteToolError(event, '该项目已在 IDEA 中重新打开,请重试刚才的操作。');
928
+ return;
929
+ }
930
+ const result = await runOpenInIdea();
931
+ if (result.port === undefined) return;
932
+ rewriteToolError(event, 'IDE MCP 已重新连接,请重试刚才的操作。');
933
+ });
934
+ }
795
935
  }
796
936
 
797
937
  return async () => {
798
938
  disposed = true;
939
+ if (heartbeatTimer) clearInterval(heartbeatTimer);
799
940
  if (shellRegistration) {
800
941
  try {
801
942
  await shellRegistration.dispose();
@@ -810,6 +951,13 @@ export default {
810
951
  // best effort
811
952
  }
812
953
  }
954
+ if (toolRegistration) {
955
+ try {
956
+ await toolRegistration.dispose();
957
+ } catch {
958
+ // best effort
959
+ }
960
+ }
813
961
  if (promptRegistration) {
814
962
  try {
815
963
  await promptRegistration.dispose();