opencode-session-spawn 2.1.1 → 2.3.0

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.
Files changed (3) hide show
  1. package/README.md +28 -10
  2. package/index.js +78 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # opencode-session-spawn
2
2
 
3
- opencode 插件:**会话生成原语**。只做一件事——**创建一个新的顶层会话,并把起始语句注入为该会话的首条消息**。
3
+ opencode 插件:**会话生成原语**。做两件事——**创建一个新的顶层会话,并把起始语句注入为该会话首条消息**(新会话保持顶层,会出现在会话列表中,便于人工查看);以及**子会话反向通知父会话**(`notify_parent`:注入一条 `[relay]` 消息并唤醒父会话)。
4
4
 
5
- 它的用途是**无人值守长任务编排**:当一条任务超出单会话上下文时,当前会话的模型可以在需要时自行生成一段交接语句、调用工具开出一个新会话继续,新会话拿到语句立即开跑。插件不落文书、不读文书、不追踪链、无状态文件——需要交接文书时,把「读取某份文书路径」等引导写进起始语句即可,与其他工具天然兼容。
5
+ 它的用途是**无人值守长任务编排**:当一条任务超出单会话上下文时,当前会话的模型可以在需要时自行生成一段交接语句、调用工具开出一个新会话继续,新会话拿到语句立即开跑;子会话完成 / 受阻时可用 `notify_parent` 回调 spawn 它的会话。插件不落文书、不读文书、不追踪链、无状态文件——需要交接文书时,把「读取某份文书路径」等引导写进起始语句即可,与其他工具天然兼容。
6
6
 
7
- ## 两个入口(共用同一实现)
7
+ ## 入口与工具
8
8
 
9
- | 入口 | 用法 | 谁触发 |
9
+ | 入口 / 工具 | 用法 | 谁触发 |
10
10
  |------|------|--------|
11
- | 工具 `spawn_session` | `spawn_session({ prompt, title?, agent?, directory? })` | **模型自主调用**(打通无人值守) |
11
+ | 工具 `spawn_session` | `spawn_session({ prompt, title?, agent?, directory?, model? })` | **模型自主调用**(打通无人值守) |
12
+ | 工具 `notify_parent` | `notify_parent({ text })` | **子会话模型自主调用**(完成 / 受阻时回调父会话) |
12
13
  | 命令 | `@spawn [--agent <名称>] [--title <标题>] <起始语句>`(兼容 `@relay spawn`) | 人工触发 |
13
14
 
14
15
  参数 / 行为:
@@ -17,14 +18,17 @@ opencode 插件:**会话生成原语**。只做一件事——**创建一个
17
18
  - `title`(可选):新会话标题。缺省取 `prompt` 首行(清洗非法字符、截断 60)。
18
19
  - `agent`(可选,工具参数):新会话以某 agent/模式启动(如 `leader`)。仅在传了非空白值时透传,交服务端校验。
19
20
  - `directory`(可选,工具参数):新会话绑定的项目目录(如 `D:\dev\bbcare`)。仅在传了非空白值时透传,交服务端校验。
21
+ - `model`(可选,工具参数):新会话使用的模型,格式 `provider/model`(如 `deepseek/deepseek-v4-flash`、`opencode/big-pickle`),透传为 `{ providerID, id }`;空 / 无 `/` 视为未传,缺省由服务端默认。
22
+ - 父链接(自动,非参数):`spawn_session` 把调用者会话 id 写入新会话 **`metadata.spawnParentID`**(`context.sessionID` 非空白时)。**不写 `parentID`**——桌面端会话列表只拉取顶层会话,写 `parentID` 会让新会话变成「子会话」而从列表消失(v2.2.0 的可见性回归);metadata 不影响列表。命令 `@spawn` 无会话上下文,不带父链接。
20
23
  - 命令 flag 说明:`--agent` / `--title` 值均为**单个 token**(不处理引号/空格);仅解析命令后**前导** flag,起始语句内部再出现的 `--xxx` 原样保留;flag 缺值或起始语句为空 → warning toast 且不建会话。`--title` 优先于起始语句首行。
21
- - 内部调用:`client.session.create({ body: { title }, query: { directory } })` → `client.session.promptAsync({ path: { id }, body: { agent, parts: [{ type: "text", text: prompt }] } })`;`agent` / `directory` 未传时不出现对应键。
24
+ - 内部调用(spawn):`client.session.create({ body: { title, metadata?, model? }, query: { directory } })` → `client.session.promptAsync({ path: { id }, body: { agent, parts: [{ type: "text", text: prompt }] } })`;`agent` / `directory` / `model` / `metadata` 未传时不出现对应键。
25
+ - `notify_parent` 行为:仅能通知**父会话**(不提供任意 session id 参数);`client.session.get` 解析自身 `metadata.spawnParentID`(旧会话回退 `parentID`)后,向父会话注入 `[relay] <text>` 并唤醒它;父会话以服务端默认模式(`build`)被唤醒;空文本 / 无父会话 / 读取或注入失败 → 返回错误文本(不抛);无重试 / 队列 / 追踪(保持无状态原语)。
22
26
  - 优先 `promptAsync`(异步 fire-and-forget,**不阻塞父会话**);缺失则回退同步 `prompt`。
23
27
  - create 失败 → 不建会话;create 成功但注入失败 → **保留已建会话**并提示 id,不产生重复会话。
24
28
 
25
- ## 安装
29
+ ## 安装(部署到桌面版)
26
30
 
27
- 在全局或项目 `opencode.json(c)` 的 `plugin` 数组注册包名,重启桌面版生效(opencode 自动拉取最新发布并装入包缓存):
31
+ 现有部署形态:公共层配置 `share/opencode.json`(挂载到项目内即 `.opencode/opencode.json`)的 `plugin` 数组注册 **npm 包名**:
28
32
 
29
33
  ```jsonc
30
34
  {
@@ -32,7 +36,9 @@ opencode 插件:**会话生成原语**。只做一件事——**创建一个
32
36
  }
33
37
  ```
34
38
 
35
- 本地开发也可注册本地目录(opencode 按路径加载):把 `index.js` + `package.json` 复制到自选目录,在 `plugin` 中写该目录相对路径(如 `./plugins/session-spawn`)。
39
+ opencode 启动时自动把最新发布安装到包缓存 `~/.cache/opencode/packages/opencode-session-spawn/`(活实例为其中的 `node_modules/opencode-session-spawn/index.js`),**重启桌面版生效**。发布新版本后重启即自动更新;若缓存未刷新,删除 `~/.cache/opencode/packages/opencode-session-spawn@latest` 后重启强制重装。
40
+
41
+ > 旧的本地目录注册写法(如 `./plugins/session-relay/`)已作废;开发与生产验证统一走 `test/test.mjs` + `RELAY_MAIN`(见下节)。
36
42
 
37
43
  ## 编排示例
38
44
 
@@ -55,11 +61,19 @@ E:\\proj\\handoff\\migration-3.md
55
61
  @spawn --agent leader --title "[P1-10] sprint" 读取 handoff/migration-3.md 后继续迁移任务
56
62
  ```
57
63
 
64
+ 父子回复约定(防环路):父会话在起始语句里要求「子会话收尾 / 受阻时调用 `notify_parent`」;子会话执行:
65
+
66
+ ```
67
+ notify_parent({ text: "P1-10 已完成,验收报告见 runs/P1-10-acceptance.md" })
68
+ ```
69
+
70
+ 父会话会收到一条 `[relay] …` 消息并以服务端默认模式被唤醒;收到通知后按需行动,**不要自动回发通知**(`notify_parent` 只向父会话发送,避免环路)。
71
+
58
72
  ## 开发与测试
59
73
 
60
74
  ```bash
61
75
  npm install # 安装 devDependency @opencode-ai/plugin(唯一依赖,仅测试用)
62
- node test/test.mjs # 23 用例 / 62 断言
76
+ node test/test.mjs # 39 用例 / 94 断言
63
77
  ```
64
78
 
65
79
  - `test/test.mjs` 默认经相对路径 `../index.js` 测项目版;环境变量 `RELAY_MAIN` 覆盖为其他路径(tarball 解包版 / 包缓存活实例)。`RELAY_MAIN` 目标须能向上解析到 `@opencode-ai/plugin`(解包放项目内即可)。
@@ -72,3 +86,7 @@ bump 版本号(`package.json` 与 `index.js` 头注释同步)→ `node test/
72
86
  ## 历史
73
87
 
74
88
  前身 `opencode-session-relay` 的接力链、两阶段交接文书、空闲事件驱动、哨兵兜底、`@relay status/leave/refresh/verify` 等机制已全部移除,仅保留「建会话 + 注入语句」这一核心原语。
89
+
90
+ - **v2.3.0(2026-09-15)**:父链接由 `parentID` 改为 **`metadata.spawnParentID`**(修复 v2.2.0 可见性回归:写 parentID 会让新会话变成子会话、从桌面端会话列表消失;且子会话默认不能再开 `task` 子代理,会破坏编排);`notify_parent` 兼容旧会话的 `parentID` 回退;新增可选 `model` 参数(`provider/model`)。
91
+ - **v2.2.0**:新增 `notify_parent` 反向通知;`spawn_session` 自动挂 `parentID`。
92
+ - **v2.1.0**:`spawn_session` 支持 `agent` / `directory`;命令 `@spawn` 支持 `--agent` / `--title`。
package/index.js CHANGED
@@ -1,19 +1,24 @@
1
1
  import { tool } from "@opencode-ai/plugin/tool";
2
2
 
3
3
  /**
4
- * session-spawn —— 会话生成原语(v2.1.1,2026-09-12 起支持 agent/directory)
4
+ * session-spawn —— 会话生成原语(v2.3.0,2026-09-15 起:父链接走 metadata + 可选 model)
5
5
  *
6
- * 只做一件事:创建一个新的顶层会话,并把起始语句注入为该会话首条消息。
7
- * create: client.session.create({ body: { title }, query: { directory } })
8
- * inject: client.session.promptAsync({ path: { id }, body: { agent, parts: [{ type: "text", text }] } })
9
- * (agent / directory 均可选,仅在显式传入且非空白时透传;不做校验,交服务端)
6
+ * 只做两件事:
7
+ * 1. 创建一个新的顶层会话,并把起始语句注入为该会话首条消息。
8
+ * create: client.session.create({ body: { title, metadata?, model? }, query: { directory } })
9
+ * inject: client.session.promptAsync({ path: { id }, body: { agent, parts: [{ type: "text", text }] } })
10
+ * (agent / directory / model 均可选,仅在显式传入且非空白时透传;不做校验,交服务端)
11
+ * 2. 子会话向父会话注入一条 [relay] 消息并唤醒它(工具 notify_parent;仅父会话,无任意会话注入)。
10
12
  *
11
13
  * 设计约束(用户定稿):
12
- * - 插件只负责「建会话 + 注入语句」,不落文书、不读文书、不追踪链、无状态文件。
14
+ * - 插件只负责「建会话 + 注入语句 + 父会话通知」,不落文书、不读文书、不追踪链、无状态文件。
13
15
  * - 编排中若需交接文书,由调用方把「读取文书路径」等引导写进起始语句即可(与其他工具天然兼容)。
14
16
  * - 优先 promptAsync(异步 fire-and-forget,不阻塞父会话);缺失则回退同步 prompt。
15
17
  * - 两个入口共用同一 spawn():① 工具 spawn_session(模型自主触发,打通无人值守)
16
18
  * ② 命令 @spawn / @relay spawn(人工触发)。
19
+ * - **父链接写入会话 metadata(`spawnParentID`),不写 `parentID`**:桌面端会话列表只拉取顶层会话,
20
+ * 写 parentID 会让派发会话变成「子会话」而从列表消失(v2.2.0 的可见性回归);metadata 不影响列表,
21
+ * notify_parent 沿 `metadata.spawnParentID` 上行,并兼容旧会话的 `parentID` 回退。
17
22
  *
18
23
  * 历史:本仓库前身 session-relay 的接力链、两阶段文书、空闲事件驱动、哨兵等机制已全部移除。
19
24
  */
@@ -42,6 +47,15 @@ function nonEmpty(v) {
42
47
  return typeof v === "string" && v.trim() !== "" ? v : undefined;
43
48
  }
44
49
 
50
+ // 解析 "provider/model" → { providerID, id };空 / 无 "/" / 缺段 → undefined(视为未传)。
51
+ function parseModel(v) {
52
+ const s = nonEmpty(v);
53
+ if (s === undefined) return undefined;
54
+ const i = s.indexOf("/");
55
+ if (i <= 0 || i >= s.length - 1) return undefined;
56
+ return { providerID: s.slice(0, i), id: s.slice(i + 1) };
57
+ }
58
+
45
59
  const SPAWN_USAGE = "用法:@spawn [--agent <名称>] [--title <标题>] <起始语句>";
46
60
 
47
61
  // 解析人工命令:@spawn / @relay spawn,支持前导 --agent / --title。
@@ -87,7 +101,8 @@ export const SessionSpawn = async (ctx) => {
87
101
  };
88
102
 
89
103
  // 核心:建新会话 + 注入起始语句。
90
- // opts.agent / opts.directory 可选;仅在非空白时透传(不做校验,交服务端)。
104
+ // opts.agent / opts.directory / opts.model / opts.parentSessionID 可选;仅非空白时透传(不做校验)。
105
+ // 父链接写 metadata.spawnParentID(不写 parentID:保持顶层 → 会话列表可见)。
91
106
  // 返回 { id, title, injected, agent?, directory?, error? };create 失败则抛出(未建会话)。
92
107
  async function spawn(prompt, title, opts = {}) {
93
108
  const text = typeof prompt === "string" ? prompt : "";
@@ -98,8 +113,12 @@ export const SessionSpawn = async (ctx) => {
98
113
  const t = (title && String(title).trim()) || defaultTitle(text);
99
114
  const agent = nonEmpty(opts.agent);
100
115
  const directory = nonEmpty(opts.directory);
116
+ const parentSessionID = nonEmpty(opts.parentSessionID);
117
+ const model = parseModel(opts.model);
101
118
 
102
119
  const createReq = { body: { title: t } };
120
+ if (parentSessionID !== undefined) createReq.body.metadata = { spawnParentID: parentSessionID };
121
+ if (model !== undefined) createReq.body.model = model;
103
122
  if (directory !== undefined) createReq.query = { directory };
104
123
  const created = await client.session.create(createReq);
105
124
  const id = created && (created.data?.id || created.id);
@@ -131,16 +150,22 @@ export const SessionSpawn = async (ctx) => {
131
150
  tool: {
132
151
  spawn_session: tool({
133
152
  description:
134
- "创建一个新的顶层会话并向其注入起始语句,用于无人值守长任务编排:当前会话模型在需要时自行决定把工作交给一个新会话继续。新会话会立即开始执行该起始语句。若需让其读取某份交接文书,请把文书路径与读取要求写进 prompt。agent 可指定新会话以某 agent/模式启动(如 leader);directory 可绑定项目目录。",
153
+ "创建一个新的顶层会话并向其注入起始语句,用于无人值守长任务编排:当前会话模型在需要时自行决定把工作交给一个新会话继续。新会话会立即开始执行该起始语句;它保持顶层,会出现在会话列表中(便于人工查看)。若需让其读取某份交接文书,请把文书路径与读取要求写进 prompt。agent 可指定新会话以某 agent/模式启动(如 leader);directory 可绑定项目目录;model 可指定模型(provider/model)。新会话可用 notify_parent 回通知本会话。",
135
154
  args: {
136
155
  prompt: tool.schema.string().describe("注入新会话的起始语句(可内含读取交接文书路径等引导)"),
137
156
  title: tool.schema.string().optional().describe("新会话标题;缺省取起始语句首行"),
138
157
  agent: tool.schema.string().optional().describe("新会话以某 agent/模式启动(如 leader);缺省由服务端默认"),
139
158
  directory: tool.schema.string().optional().describe("新会话绑定的项目目录(绝对路径,如 D:\\dev\\bbcare);缺省由服务端默认"),
159
+ model: tool.schema.string().optional().describe("新会话使用的模型(provider/model,如 deepseek/deepseek-v4-flash 或 opencode/big-pickle);缺省由服务端默认"),
140
160
  },
141
- async execute(args) {
161
+ async execute(args, context) {
142
162
  try {
143
- const r = await spawn(args.prompt, args.title, { agent: args.agent, directory: args.directory });
163
+ const r = await spawn(args.prompt, args.title, {
164
+ agent: args.agent,
165
+ directory: args.directory,
166
+ model: args.model,
167
+ parentSessionID: context && context.sessionID,
168
+ });
144
169
  if (!r.injected) {
145
170
  return `会话「${r.title}」(id=${r.id}) 已创建,但起始语句注入失败:${r.error}。请手动打开该会话补发指令。`;
146
171
  }
@@ -150,6 +175,49 @@ export const SessionSpawn = async (ctx) => {
150
175
  }
151
176
  },
152
177
  }),
178
+
179
+ // 工具:子会话反向通知父会话(沿 metadata.spawnParentID 上行,兼容 parentID;失败一律返回文本,不抛)。
180
+ notify_parent: tool({
181
+ description:
182
+ "向本会话的父会话(spawn 本会话的会话)注入一条消息并唤醒它:用于子会话完成 / 受阻时通知父会话。仅能通知父会话,不支持指定任意会话;注入文本自动加 [relay] 前缀;父会话以服务端默认模式(build)被唤醒。收到通知后按需行动即可,不要自动回发通知(防环路)。",
183
+ args: {
184
+ text: tool.schema.string().describe("通知内容(注入父会话,自动加 [relay] 前缀)"),
185
+ },
186
+ async execute(args, context) {
187
+ const text = typeof args.text === "string" ? args.text : "";
188
+ if (!text.trim()) return "通知失败:内容为空";
189
+ if (!client || !client.session || typeof client.session.get !== "function") {
190
+ return "通知失败:client.session.get 不可用";
191
+ }
192
+
193
+ let parentID;
194
+ try {
195
+ const got = await client.session.get({ path: { id: context && context.sessionID } });
196
+ const info = (got && (got.data || got)) || {};
197
+ const meta = info.metadata || {};
198
+ parentID = nonEmpty(meta.spawnParentID) || nonEmpty(info.parentID);
199
+ } catch (e) {
200
+ return `通知失败:读取会话失败(${e.message})`;
201
+ }
202
+ if (!parentID) return "当前会话没有父会话,无法通知";
203
+
204
+ const body = { parts: [{ type: "text", text: "[relay] " + text }] };
205
+ const injectFn =
206
+ typeof client.session.promptAsync === "function"
207
+ ? client.session.promptAsync.bind(client.session)
208
+ : typeof client.session.prompt === "function"
209
+ ? client.session.prompt.bind(client.session)
210
+ : null;
211
+ if (!injectFn) return "通知失败:client.session.promptAsync/prompt 均不可用";
212
+
213
+ try {
214
+ await injectFn({ path: { id: parentID }, body });
215
+ } catch (e) {
216
+ return `通知失败:注入父会话失败(${e.message})`;
217
+ }
218
+ return `已通知父会话 (id=${parentID})。`;
219
+ },
220
+ }),
153
221
  },
154
222
 
155
223
  // 人工命令:@spawn [--agent <名称>] [--title <标题>] <起始语句>(兼容 @relay spawn)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-session-spawn",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "type": "module",
5
5
  "description": "opencode 会话生成原语:创建新顶层会话并注入起始语句,用于无人值守长任务编排(工具 spawn_session + 命令 @spawn)。",
6
6
  "main": "index.js",