@uncarved/jade 0.3.1 → 0.4.1
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 +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +86 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +496 -36
package/dist/session.js
CHANGED
|
@@ -12,6 +12,12 @@ export const NO_APPROVAL_REASON = "No permission plugin is active in this sessio
|
|
|
12
12
|
/** 有插件要人工审批、但没有审批插件来问时,这一轮结束的原因 */
|
|
13
13
|
export const NO_APPROVER_ERROR = "A plugin asked for the user's approval, but no approval plugin is active in this session to ask.";
|
|
14
14
|
const KERNEL_LAYER = { bundled: "distro", user: "user", project: "project" };
|
|
15
|
+
/** 核心放在子会话系统提示词最前面的委派说明(设计稿 live-tools-and-child-sessions 7.3) */
|
|
16
|
+
export const DELEGATION = "You are a sub-agent: another agent delegated a task to you. You cannot see its conversation, so the task you are given is all you know about it. " +
|
|
17
|
+
"Nobody reads your intermediate messages; when you are done, reply with a self-contained report of what you did and found, including anything you could not do.";
|
|
18
|
+
/** 子会话的层数、同时数量的默认上限(7.2) */
|
|
19
|
+
export const DEFAULT_MAX_DEPTH = 1;
|
|
20
|
+
export const DEFAULT_MAX_ACTIVE = 8;
|
|
15
21
|
class LiveSession {
|
|
16
22
|
id;
|
|
17
23
|
parts = [];
|
|
@@ -31,16 +37,71 @@ class LiveSession {
|
|
|
31
37
|
#attended;
|
|
32
38
|
/** 插件订阅的运行时通知(ctx.session.subscribe) */
|
|
33
39
|
#watchers = new Set();
|
|
40
|
+
/** 前端和核心订阅的运行时通知(Session.subscribe)。由会话自己持有、从当前的 Runtime 转发,重挂时不丢 */
|
|
41
|
+
#listeners = new Set();
|
|
42
|
+
#unsubscribe;
|
|
43
|
+
/** Runtime 建好了(attach 之后):session() 里登记的 afterTurn 等到这时才执行,那时 state() 才有 */
|
|
44
|
+
#attached = false;
|
|
45
|
+
/** 工具表变了,下一项工作开始前要重挂 Runtime */
|
|
46
|
+
#stale = false;
|
|
47
|
+
/** 重挂时旧实例已经 detach、新实例没建成:下次重挂不再 detach,离开时也不用 */
|
|
48
|
+
#loose = false;
|
|
49
|
+
#provider;
|
|
50
|
+
#plugins;
|
|
34
51
|
#store;
|
|
35
|
-
|
|
52
|
+
/** 子会话的信息;顶层会话没有 */
|
|
53
|
+
child;
|
|
54
|
+
/** 第几层:顶层会话是 0 */
|
|
55
|
+
depth = 0;
|
|
56
|
+
/** 本会话开的、还没结束的子会话 */
|
|
57
|
+
children = new Set();
|
|
58
|
+
/** 子会话结束(Sessions.#end)的结果;结束时调用 onEnd(让出同时数量的名额) */
|
|
59
|
+
ended;
|
|
60
|
+
onEnd;
|
|
61
|
+
constructor(id, store, report, attended, child) {
|
|
36
62
|
this.id = id;
|
|
37
63
|
this.#store = store;
|
|
38
64
|
this.#report = report;
|
|
39
65
|
this.#attended = attended;
|
|
66
|
+
this.child = child;
|
|
67
|
+
if (child)
|
|
68
|
+
this.depth = child.session.depth + 1;
|
|
69
|
+
}
|
|
70
|
+
get store() {
|
|
71
|
+
return this.#store;
|
|
72
|
+
}
|
|
73
|
+
get provider() {
|
|
74
|
+
return this.#provider;
|
|
75
|
+
}
|
|
76
|
+
get left() {
|
|
77
|
+
return this.#left !== undefined;
|
|
78
|
+
}
|
|
79
|
+
/** 子会话只保留开它的插件要的工具(SpawnOptions.tools) */
|
|
80
|
+
keeps(t) {
|
|
81
|
+
const keep = this.child?.tools;
|
|
82
|
+
if (!keep)
|
|
83
|
+
return true;
|
|
84
|
+
try {
|
|
85
|
+
return keep(sessionTool(t)) === true;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
40
90
|
}
|
|
41
91
|
history() {
|
|
42
92
|
return readAll(this.#store, this.id);
|
|
43
93
|
}
|
|
94
|
+
tool(name) {
|
|
95
|
+
for (const p of this.parts) {
|
|
96
|
+
if (p.owner.scope.closed)
|
|
97
|
+
continue;
|
|
98
|
+
const t = p.tools.find((x) => x.name === name);
|
|
99
|
+
if (!t || !this.keeps(t))
|
|
100
|
+
continue;
|
|
101
|
+
return sessionTool(t);
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
44
105
|
get state() {
|
|
45
106
|
return this.rt.state;
|
|
46
107
|
}
|
|
@@ -109,14 +170,17 @@ class LiveSession {
|
|
|
109
170
|
return Promise.reject(this.#left);
|
|
110
171
|
return new Promise((resolve, reject) => {
|
|
111
172
|
this.#queue.push({ run, resolve, reject });
|
|
112
|
-
|
|
173
|
+
// session() 执行期间(Runtime 还没建)排进来的等 attach 再开始
|
|
174
|
+
if (this.#attached)
|
|
175
|
+
this.#kick();
|
|
113
176
|
});
|
|
114
177
|
}
|
|
115
178
|
afterTurn(fn, owner) {
|
|
116
179
|
if (this.#left)
|
|
117
180
|
return;
|
|
118
181
|
this.#after.push({ fn, owner });
|
|
119
|
-
this.#
|
|
182
|
+
if (this.#attached)
|
|
183
|
+
this.#kick();
|
|
120
184
|
}
|
|
121
185
|
/** 离开会话:还没开始的输入全部拒绝、afterTurn 不再执行,再中止正在跑的一轮。之后的 send 直接拒绝 */
|
|
122
186
|
leave() {
|
|
@@ -144,9 +208,28 @@ class LiveSession {
|
|
|
144
208
|
this.#watchers.add(w);
|
|
145
209
|
return () => void this.#watchers.delete(w);
|
|
146
210
|
}
|
|
147
|
-
/** Runtime
|
|
148
|
-
attach() {
|
|
149
|
-
this
|
|
211
|
+
/** Runtime 建好之后调用一次。provider、plugins 留着给重挂用:plugins 每次按现在的工具表生成内核插件 */
|
|
212
|
+
attach(rt, provider, plugins) {
|
|
213
|
+
this.#provider = provider;
|
|
214
|
+
this.#plugins = plugins;
|
|
215
|
+
this.#use(rt);
|
|
216
|
+
this.#attached = true;
|
|
217
|
+
if (this.#queue.length > 0 || this.#after.length > 0)
|
|
218
|
+
this.#kick();
|
|
219
|
+
}
|
|
220
|
+
/** 换上一个 Runtime:通知从它转给订阅者。插件的回调以插件身份执行;出错只报告 */
|
|
221
|
+
#use(rt) {
|
|
222
|
+
this.#unsubscribe?.();
|
|
223
|
+
this.rt = rt;
|
|
224
|
+
this.#unsubscribe = rt.subscribe((n) => {
|
|
225
|
+
for (const fn of this.#listeners) {
|
|
226
|
+
try {
|
|
227
|
+
fn(n);
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
// 和内核一样:订阅者的错误不影响运行
|
|
231
|
+
}
|
|
232
|
+
}
|
|
150
233
|
for (const w of this.#watchers) {
|
|
151
234
|
if (w.owner.scope.closed)
|
|
152
235
|
continue;
|
|
@@ -159,8 +242,51 @@ class LiveSession {
|
|
|
159
242
|
}
|
|
160
243
|
});
|
|
161
244
|
}
|
|
245
|
+
/** 工具表变了(会话中途注册、撤销工具,插件被隔离):下一项工作开始前重挂。几次改动合并成一次 */
|
|
246
|
+
invalidate() {
|
|
247
|
+
if (this.#attached && !this.#left)
|
|
248
|
+
this.#stale = true;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* 重挂:detach 旧实例,按现在的工具表 resume 同一个会话(设计稿 live-tools-and-child-sessions 3.2)。
|
|
252
|
+
* 只在两项工作之间执行,内核这时不在跑。插件清单(id、版本)不变,内核不写 plugins.changed。
|
|
253
|
+
* 失败时这项工作失败,下一项工作再试
|
|
254
|
+
*/
|
|
255
|
+
async #remount() {
|
|
256
|
+
this.#stale = false;
|
|
257
|
+
if (this.rt.phase === "ended")
|
|
258
|
+
return;
|
|
259
|
+
try {
|
|
260
|
+
if (!this.#loose) {
|
|
261
|
+
await this.rt.detach();
|
|
262
|
+
this.#loose = true;
|
|
263
|
+
}
|
|
264
|
+
const rt = await Runtime.resume({ store: this.#store, provider: this.#provider, plugins: this.#plugins(), sessionId: this.id });
|
|
265
|
+
this.#loose = false;
|
|
266
|
+
this.#use(rt);
|
|
267
|
+
}
|
|
268
|
+
catch (e) {
|
|
269
|
+
this.#stale = true;
|
|
270
|
+
throw new Error(`session ${this.id}: could not update its tools: ${e instanceof Error ? e.message : String(e)}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** 结束会话(session.ended),子会话用完时。重挂到一半(旧实例已经 detach)时先恢复一个实例来写 */
|
|
274
|
+
async close() {
|
|
275
|
+
if (this.#loose) {
|
|
276
|
+
this.#use(await Runtime.resume({ store: this.#store, provider: this.#provider, plugins: this.#plugins(), sessionId: this.id }));
|
|
277
|
+
this.#loose = false;
|
|
278
|
+
}
|
|
279
|
+
if (this.rt.phase !== "ended")
|
|
280
|
+
await this.rt.close();
|
|
281
|
+
}
|
|
282
|
+
/** 宿主离开会话时 detach;重挂到一半(旧实例已经 detach)时不用再写 */
|
|
283
|
+
async detach() {
|
|
284
|
+
if (!this.#loose)
|
|
285
|
+
await this.rt.detach();
|
|
286
|
+
}
|
|
162
287
|
abort(reason) {
|
|
163
|
-
|
|
288
|
+
// Runtime 还没建(session() 执行期间、打开失败时):没有在跑的
|
|
289
|
+
this.rt?.abort(reason);
|
|
164
290
|
// 停在等待人工审批时内核没有在跑,信号结束不了这一轮
|
|
165
291
|
if (this.#turn) {
|
|
166
292
|
this.#turn.stop = { reason };
|
|
@@ -168,7 +294,8 @@ class LiveSession {
|
|
|
168
294
|
}
|
|
169
295
|
}
|
|
170
296
|
subscribe(fn) {
|
|
171
|
-
|
|
297
|
+
this.#listeners.add(fn);
|
|
298
|
+
return () => void this.#listeners.delete(fn);
|
|
172
299
|
}
|
|
173
300
|
#kick() {
|
|
174
301
|
this.#pump ??= this.#run().finally(() => {
|
|
@@ -185,6 +312,12 @@ class LiveSession {
|
|
|
185
312
|
const job = this.#queue.shift();
|
|
186
313
|
if (job) {
|
|
187
314
|
try {
|
|
315
|
+
if (this.#stale) {
|
|
316
|
+
await this.#remount();
|
|
317
|
+
// 重挂期间离开了会话:中止落在旧实例上,这项工作不再开始
|
|
318
|
+
if (this.#left)
|
|
319
|
+
throw this.#left;
|
|
320
|
+
}
|
|
188
321
|
await job.run();
|
|
189
322
|
job.resolve();
|
|
190
323
|
}
|
|
@@ -218,6 +351,11 @@ export class Sessions {
|
|
|
218
351
|
#store;
|
|
219
352
|
/** 换会话串行进行 */
|
|
220
353
|
#switching = Promise.resolve();
|
|
354
|
+
/** 正在运行的子会话数,和等着开的 */
|
|
355
|
+
#running = 0;
|
|
356
|
+
#waiting = [];
|
|
357
|
+
/** 子会话的对话框排成一队,一次一个(7.4) */
|
|
358
|
+
#dialogs = Promise.resolve();
|
|
221
359
|
constructor(host) {
|
|
222
360
|
this.#host = host;
|
|
223
361
|
}
|
|
@@ -248,22 +386,35 @@ export class Sessions {
|
|
|
248
386
|
if (!provider)
|
|
249
387
|
throw new ServiceUnavailable("provider@1", "(none): no active plugin provides a model");
|
|
250
388
|
const store = host.service("store@1", host.core) ?? (this.#store ??= new JsonlLogStore(path.join(host.home, "sessions")));
|
|
389
|
+
if (opts.fork && opts.resume !== undefined)
|
|
390
|
+
throw new Error("sessions.open: resume and fork cannot be used together");
|
|
251
391
|
const s = new LiveSession(opts.resume ?? crypto.randomUUID(), store, host.report, () => this.#guarded(s));
|
|
392
|
+
// 复制出的会话:先写好日志,再和恢复一样打开。打开失败时留下的日志没人引用,不影响原会话
|
|
393
|
+
if (opts.fork)
|
|
394
|
+
await copyLog(store, opts.fork.session, opts.fork.before, s.id);
|
|
395
|
+
// 直接恢复一个子会话(例如按 id):层数从日志里的 parent 往上数
|
|
396
|
+
if (opts.resume !== undefined || opts.fork)
|
|
397
|
+
s.depth = await depthOf(store, s.id);
|
|
398
|
+
let rt;
|
|
399
|
+
const core = this.#corePlugin(s);
|
|
400
|
+
const kernelPlugins = () => [core, ...s.parts.map((p) => p.kernel())];
|
|
252
401
|
try {
|
|
253
402
|
for (const p of host.active())
|
|
254
403
|
if (p.module.session)
|
|
255
404
|
await this.#join(s, p);
|
|
256
|
-
const plugins =
|
|
257
|
-
|
|
405
|
+
const plugins = kernelPlugins();
|
|
406
|
+
rt = opts.resume !== undefined || opts.fork
|
|
258
407
|
? await Runtime.resume({ store, provider, plugins, sessionId: s.id })
|
|
259
408
|
: await Runtime.create({ store, provider, plugins, sessionId: s.id });
|
|
260
409
|
}
|
|
261
410
|
catch (e) {
|
|
411
|
+
// session() 里排进来的输入不会再执行了
|
|
412
|
+
s.leave();
|
|
262
413
|
await this.#closeParts(s);
|
|
263
414
|
throw e;
|
|
264
415
|
}
|
|
265
|
-
s.attach();
|
|
266
|
-
s.
|
|
416
|
+
s.attach(rt, provider, kernelPlugins);
|
|
417
|
+
s.subscribe((n) => {
|
|
267
418
|
if (n.type === "hook-error")
|
|
268
419
|
host.report(`plugin ${n.info.plugin} failed at ${n.info.point}: ${n.info.error instanceof Error ? n.info.error.message : String(n.info.error)}`);
|
|
269
420
|
});
|
|
@@ -274,6 +425,163 @@ export class Sessions {
|
|
|
274
425
|
await this.#leave(old);
|
|
275
426
|
return s;
|
|
276
427
|
}
|
|
428
|
+
/** 子会话的上限(7.2):用户 config.json 的 "jade/core" */
|
|
429
|
+
#limits() {
|
|
430
|
+
const c = this.#host.coreConfig();
|
|
431
|
+
const n = (v, d, min) => (typeof v === "number" && Number.isInteger(v) && v >= min ? v : d);
|
|
432
|
+
return { maxDepth: n(c.maxDepth, DEFAULT_MAX_DEPTH, 0), maxActive: n(c.maxActive, DEFAULT_MAX_ACTIVE, 1) };
|
|
433
|
+
}
|
|
434
|
+
/** 开子会话(ctx.session.spawn):所有插件照常加入,Runtime.create 带 parent。不会成为当前会话 */
|
|
435
|
+
async #spawn(parent, opts, by) {
|
|
436
|
+
if (!opts || typeof opts !== "object" || opts.parent?.sessionId !== parent.id || typeof opts.parent.eventId !== "string") {
|
|
437
|
+
throw new Error(`plugin ${by}: spawn needs parent: the ToolContext.parent of a tool call in this session`);
|
|
438
|
+
}
|
|
439
|
+
if (parent.left)
|
|
440
|
+
throw new Error(`session ${parent.id} was closed`);
|
|
441
|
+
const { maxDepth, maxActive } = this.#limits();
|
|
442
|
+
if (parent.depth + 1 > maxDepth) {
|
|
443
|
+
throw new Error(maxDepth === 0
|
|
444
|
+
? `sub-agents are turned off ("jade/core": { "maxDepth": 0 } in config.json)`
|
|
445
|
+
: `sub-agents can be nested at most ${maxDepth} level${maxDepth > 1 ? "s" : ""} deep; this session is already at level ${parent.depth} (raise "jade/core": { "maxDepth" } in config.json)`);
|
|
446
|
+
}
|
|
447
|
+
const signal = opts.signal instanceof AbortSignal ? opts.signal : undefined;
|
|
448
|
+
// 同时数量的上限:顶层会话开的等前面的结束;子会话开的直接报错,它自己占着名额,等下去可能谁都等不到
|
|
449
|
+
while (this.#running >= maxActive) {
|
|
450
|
+
if (parent.depth > 0)
|
|
451
|
+
throw new Error(`${maxActive} sub-agents are already running; a sub-agent cannot wait for a slot (raise "jade/core": { "maxActive" } in config.json)`);
|
|
452
|
+
await new Promise((resolve, reject) => {
|
|
453
|
+
const wake = () => {
|
|
454
|
+
signal?.removeEventListener("abort", stop);
|
|
455
|
+
resolve();
|
|
456
|
+
};
|
|
457
|
+
const stop = () => {
|
|
458
|
+
const i = this.#waiting.indexOf(wake);
|
|
459
|
+
if (i >= 0)
|
|
460
|
+
this.#waiting.splice(i, 1);
|
|
461
|
+
reject(signal.reason);
|
|
462
|
+
};
|
|
463
|
+
if (signal?.aborted)
|
|
464
|
+
return stop();
|
|
465
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
466
|
+
this.#waiting.push(wake);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
if (signal?.aborted) {
|
|
470
|
+
// 被叫醒时已经中止:名额让给下一个等着的
|
|
471
|
+
this.#waiting.shift()?.();
|
|
472
|
+
throw signal.reason;
|
|
473
|
+
}
|
|
474
|
+
this.#running++;
|
|
475
|
+
let released = false;
|
|
476
|
+
const release = () => {
|
|
477
|
+
if (released)
|
|
478
|
+
return;
|
|
479
|
+
released = true;
|
|
480
|
+
this.#running--;
|
|
481
|
+
this.#waiting.shift()?.();
|
|
482
|
+
};
|
|
483
|
+
if (parent.left) {
|
|
484
|
+
release();
|
|
485
|
+
throw new Error(`session ${parent.id} was closed`);
|
|
486
|
+
}
|
|
487
|
+
const host = this.#host;
|
|
488
|
+
const provider = opts.provider ?? parent.provider;
|
|
489
|
+
const child = {
|
|
490
|
+
session: parent,
|
|
491
|
+
ref: { sessionId: opts.parent.sessionId, eventId: opts.parent.eventId },
|
|
492
|
+
by,
|
|
493
|
+
...(typeof opts.label === "string" && opts.label ? { label: opts.label } : {}),
|
|
494
|
+
...(typeof opts.tools === "function" ? { tools: opts.tools } : {}),
|
|
495
|
+
...(typeof opts.system === "string" && opts.system ? { system: opts.system } : {}),
|
|
496
|
+
};
|
|
497
|
+
const s = new LiveSession(crypto.randomUUID(), parent.store, host.report, () => this.#guarded(s), child);
|
|
498
|
+
const core = this.#corePlugin(s);
|
|
499
|
+
const delegation = this.#delegation(child);
|
|
500
|
+
const kernelPlugins = () => [core, ...s.parts.map((p) => p.kernel()), delegation];
|
|
501
|
+
let rt;
|
|
502
|
+
try {
|
|
503
|
+
for (const p of host.active())
|
|
504
|
+
if (p.module.session)
|
|
505
|
+
await this.#join(s, p);
|
|
506
|
+
rt = await Runtime.create({ store: s.store, provider, plugins: kernelPlugins(), sessionId: s.id, parent: child.ref });
|
|
507
|
+
}
|
|
508
|
+
catch (e) {
|
|
509
|
+
s.leave();
|
|
510
|
+
await this.#closeParts(s);
|
|
511
|
+
release();
|
|
512
|
+
throw e;
|
|
513
|
+
}
|
|
514
|
+
s.attach(rt, provider, kernelPlugins);
|
|
515
|
+
s.subscribe((n) => {
|
|
516
|
+
if (n.type === "hook-error")
|
|
517
|
+
host.report(`plugin ${n.info.plugin} failed at ${n.info.point} in a sub-agent: ${n.info.error instanceof Error ? n.info.error.message : String(n.info.error)}`);
|
|
518
|
+
});
|
|
519
|
+
s.onEnd = release;
|
|
520
|
+
// Runtime 建好了才算父会话的子会话:父会话离开时结束它要用 Runtime。开的期间父会话离开了:马上结束
|
|
521
|
+
parent.children.add(s);
|
|
522
|
+
if (parent.left) {
|
|
523
|
+
await this.#end(s);
|
|
524
|
+
throw new Error(`session ${parent.id} was closed`);
|
|
525
|
+
}
|
|
526
|
+
let closing;
|
|
527
|
+
return {
|
|
528
|
+
id: s.id,
|
|
529
|
+
send: (input) => s.send(input),
|
|
530
|
+
get state() {
|
|
531
|
+
return s.state;
|
|
532
|
+
},
|
|
533
|
+
subscribe: (fn) => s.subscribe(fn),
|
|
534
|
+
abort: (reason) => s.abort(new Error(reason ?? `stopped by plugin ${by}`)),
|
|
535
|
+
close: () => (closing ??= this.#end(s)),
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/** 结束子会话:先结束它的子会话,中止正在跑的工作,写 session.ended,关闭插件的作用域。返回 ChildRef */
|
|
539
|
+
async #end(s) {
|
|
540
|
+
if (s.ended)
|
|
541
|
+
return s.ended;
|
|
542
|
+
s.ended = (async () => {
|
|
543
|
+
s.leave();
|
|
544
|
+
for (const c of [...s.children])
|
|
545
|
+
await this.#end(c);
|
|
546
|
+
await s.idle();
|
|
547
|
+
try {
|
|
548
|
+
await s.close();
|
|
549
|
+
}
|
|
550
|
+
catch (e) {
|
|
551
|
+
this.#host.report(`sub-agent session ${s.id}: could not end it: ${e instanceof Error ? e.message : String(e)}`);
|
|
552
|
+
}
|
|
553
|
+
await this.#closeParts(s);
|
|
554
|
+
s.child?.session.children.delete(s);
|
|
555
|
+
s.onEnd?.();
|
|
556
|
+
return { sessionId: s.id, seq: s.rt.state.lastSeq };
|
|
557
|
+
})();
|
|
558
|
+
return s.ended;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* 子会话里的 ui@1:没有会话界面(状态栏、输入框);对话框在整个进程里排成一队,一次一个,标题前标上是哪个子 Agent(7.4)
|
|
562
|
+
*/
|
|
563
|
+
#childUI(ui, s) {
|
|
564
|
+
const name = s.child?.label ?? "sub-agent";
|
|
565
|
+
const queued = (fn) => {
|
|
566
|
+
const run = this.#dialogs.then(fn);
|
|
567
|
+
this.#dialogs = run.catch(() => { });
|
|
568
|
+
return run;
|
|
569
|
+
};
|
|
570
|
+
return new Proxy(ui, {
|
|
571
|
+
get: (target, prop) => {
|
|
572
|
+
if (prop === "session")
|
|
573
|
+
return undefined;
|
|
574
|
+
if (prop === "select") {
|
|
575
|
+
return (req, signal) => queued(() => (signal?.aborted ? Promise.resolve(undefined) : target.select({ ...req, title: `[${name}] ${req.title}` }, signal)));
|
|
576
|
+
}
|
|
577
|
+
if (prop === "input") {
|
|
578
|
+
return (req, signal) => queued(() => (signal?.aborted ? Promise.resolve(undefined) : target.input({ ...req, title: `[${name}] ${req.title}` }, signal)));
|
|
579
|
+
}
|
|
580
|
+
const v = Reflect.get(target, prop);
|
|
581
|
+
return typeof v === "function" ? v.bind(target) : v;
|
|
582
|
+
},
|
|
583
|
+
});
|
|
584
|
+
}
|
|
277
585
|
/** 一个插件加入会话:执行它的 session(),失败就撤销它这次的注册 */
|
|
278
586
|
async #join(s, p) {
|
|
279
587
|
const host = this.#host;
|
|
@@ -281,9 +589,13 @@ export class Sessions {
|
|
|
281
589
|
const id = manifest.id;
|
|
282
590
|
const scope = new Scope(id);
|
|
283
591
|
const owner = { plugin: id, scope };
|
|
592
|
+
/** 这个插件的工具,已包装;加入成功后就是 part.tools */
|
|
284
593
|
const tools = [];
|
|
285
594
|
const hooks = [];
|
|
286
595
|
let open = true;
|
|
596
|
+
let part;
|
|
597
|
+
/** 现在交给内核的工具里叫这个名字的是谁的(被隔离的插件的工具已经不交给内核,不算) */
|
|
598
|
+
const holder = (name) => s.parts.find((x) => !x.owner.scope.closed && x.tools.some((t) => t.name === name));
|
|
287
599
|
const declared = (name, lists) => {
|
|
288
600
|
if (scope.closed)
|
|
289
601
|
throw new StaleContext(id);
|
|
@@ -300,6 +612,8 @@ export class Sessions {
|
|
|
300
612
|
// 作用域已经关了:没有要清理的
|
|
301
613
|
}
|
|
302
614
|
}
|
|
615
|
+
/** 子会话里的 ui@1 换成排队、标名字的那份 */
|
|
616
|
+
const forSession = (name, h) => h && s.child && name === "ui@1" ? this.#childUI(h, s) : h;
|
|
303
617
|
const uiCall = (fn) => {
|
|
304
618
|
try {
|
|
305
619
|
fn();
|
|
@@ -317,20 +631,23 @@ export class Sessions {
|
|
|
317
631
|
secrets: scopedSecrets(host.secrets(id), () => scope.closed, id),
|
|
318
632
|
...(sessionUI
|
|
319
633
|
? {
|
|
320
|
-
status: (text) => uiCall(() => sessionUI.status(
|
|
321
|
-
|
|
634
|
+
status: (text) => uiCall(() => sessionUI.status(statusSegment(text))),
|
|
635
|
+
// 带样式的文字(Text)原样交给前端,由它决定怎么画
|
|
636
|
+
...(sessionUI.panel ? { panel: (text) => uiCall(() => sessionUI.panel(text)) } : {}),
|
|
637
|
+
// 项目层插件拿不到输入框(第 7 节):克隆的仓库不能替用户写字
|
|
638
|
+
...(sessionUI.draft && layer !== "project" ? { input: sessionUI.draft } : {}),
|
|
322
639
|
}
|
|
323
640
|
: {}),
|
|
324
641
|
require: (name) => {
|
|
325
642
|
declared(name, [manifest.requires]);
|
|
326
|
-
const h = host.service(name, owner);
|
|
643
|
+
const h = forSession(name, host.service(name, owner));
|
|
327
644
|
if (!h)
|
|
328
645
|
throw new ServiceUnavailable(name, "(none)");
|
|
329
646
|
return h;
|
|
330
647
|
},
|
|
331
648
|
service: (name) => {
|
|
332
649
|
declared(name, [manifest.requires, manifest.optional]);
|
|
333
|
-
return host.service(name, owner);
|
|
650
|
+
return forSession(name, host.service(name, owner));
|
|
334
651
|
},
|
|
335
652
|
effect: async (start) => {
|
|
336
653
|
if (scope.closed)
|
|
@@ -339,6 +656,11 @@ export class Sessions {
|
|
|
339
656
|
if (typeof d === "function")
|
|
340
657
|
scope.add(d);
|
|
341
658
|
},
|
|
659
|
+
report: (message) => {
|
|
660
|
+
if (scope.closed)
|
|
661
|
+
throw new StaleContext(id);
|
|
662
|
+
host.report(`${id}: ${String(message)}`);
|
|
663
|
+
},
|
|
342
664
|
session: {
|
|
343
665
|
id: s.id,
|
|
344
666
|
afterTurn: (fn) => {
|
|
@@ -358,7 +680,14 @@ export class Sessions {
|
|
|
358
680
|
throw new StaleContext(id);
|
|
359
681
|
if (layer === "project")
|
|
360
682
|
throw new Error(`plugin ${id}: project plugins cannot stop turns on the user's behalf`);
|
|
361
|
-
|
|
683
|
+
const why = new Error(reason ?? `stopped by plugin ${id}`);
|
|
684
|
+
s.abort(why);
|
|
685
|
+
// 子会话里替用户喊停(例如审批时按 Esc):用户要停的是整件事,顶层会话这一轮也停,不让父会话拿着结果接着干
|
|
686
|
+
let top = s;
|
|
687
|
+
while (top.child)
|
|
688
|
+
top = top.child.session;
|
|
689
|
+
if (top !== s)
|
|
690
|
+
top.abort(why);
|
|
362
691
|
},
|
|
363
692
|
state: () => {
|
|
364
693
|
if (scope.closed)
|
|
@@ -375,12 +704,50 @@ export class Sessions {
|
|
|
375
704
|
scope.add(off);
|
|
376
705
|
return off;
|
|
377
706
|
},
|
|
707
|
+
tool: (name) => {
|
|
708
|
+
if (scope.closed)
|
|
709
|
+
throw new StaleContext(id);
|
|
710
|
+
return s.tool(name);
|
|
711
|
+
},
|
|
712
|
+
...(s.child ? { parent: { ...s.child.ref } } : {}),
|
|
713
|
+
depth: s.depth,
|
|
714
|
+
canSpawn: layer !== "project" && s.depth < this.#limits().maxDepth,
|
|
715
|
+
spawn: (opts) => {
|
|
716
|
+
if (scope.closed)
|
|
717
|
+
return Promise.reject(new StaleContext(id));
|
|
718
|
+
if (layer === "project")
|
|
719
|
+
return Promise.reject(new Error(`plugin ${id}: project plugins cannot open sub-agent sessions`));
|
|
720
|
+
return this.#spawn(s, opts, id);
|
|
721
|
+
},
|
|
378
722
|
},
|
|
379
723
|
kernel: {
|
|
380
724
|
tool: (t) => {
|
|
725
|
+
if (scope.closed)
|
|
726
|
+
throw new StaleContext(id);
|
|
727
|
+
const tool = liveTool({ ...t, ...toolMeta(id, layer, t) }, owner);
|
|
728
|
+
if (!open) {
|
|
729
|
+
// session() 返回之后注册的从下一项工作起生效(重挂 Runtime)。重名的在这里拒绝,不能让重挂失败
|
|
730
|
+
if (!part)
|
|
731
|
+
throw new Error(`plugin ${id}: did not join session ${s.id}`);
|
|
732
|
+
const other = holder(tool.name);
|
|
733
|
+
if (other)
|
|
734
|
+
throw new Error(`plugin ${id}: tool ${tool.name} is already registered by ${other.source.manifest.id}`);
|
|
735
|
+
}
|
|
736
|
+
tools.push(tool);
|
|
381
737
|
if (!open)
|
|
382
|
-
|
|
383
|
-
|
|
738
|
+
s.invalidate();
|
|
739
|
+
let done = false;
|
|
740
|
+
return () => {
|
|
741
|
+
if (done)
|
|
742
|
+
return;
|
|
743
|
+
done = true;
|
|
744
|
+
const i = tools.indexOf(tool);
|
|
745
|
+
if (i < 0)
|
|
746
|
+
return;
|
|
747
|
+
tools.splice(i, 1);
|
|
748
|
+
if (part)
|
|
749
|
+
s.invalidate();
|
|
750
|
+
};
|
|
384
751
|
},
|
|
385
752
|
hook: (h) => {
|
|
386
753
|
if (!open)
|
|
@@ -404,32 +771,33 @@ export class Sessions {
|
|
|
404
771
|
}
|
|
405
772
|
open = false;
|
|
406
773
|
// 内核构造 Runtime 时会因为重名工具、不合法的 hook 拒绝整个会话:提前检查,只排除这一个插件
|
|
407
|
-
const
|
|
408
|
-
const
|
|
409
|
-
const problem = clash
|
|
410
|
-
? `tool ${clash.name} is already registered by ${s.parts.find((x) => x.kernel.tools?.some((t) => t.name === clash.name)).source.manifest.id}`
|
|
411
|
-
: compatProblem(id, manifest.version, hooks);
|
|
774
|
+
const clash = tools.find((t) => holder(t.name));
|
|
775
|
+
const problem = clash ? `tool ${clash.name} is already registered by ${holder(clash.name).source.manifest.id}` : compatProblem(id, manifest.version, hooks);
|
|
412
776
|
if (problem) {
|
|
413
777
|
host.untrack(id, scope);
|
|
414
778
|
await scope.close();
|
|
415
779
|
host.report(`plugin ${id} did not join session ${s.id}: ${problem}`);
|
|
416
780
|
return;
|
|
417
781
|
}
|
|
418
|
-
const
|
|
782
|
+
const live = hooks.map((h) => liveHook(h, owner));
|
|
783
|
+
part = {
|
|
419
784
|
source: p.source,
|
|
420
785
|
owner,
|
|
421
786
|
gates: hooks.filter((h) => h.kind === "gate" && h.point === "tool.gate").length,
|
|
422
|
-
|
|
787
|
+
tools,
|
|
788
|
+
kernel: () => this.#confirmed(p, kernelPlugin({
|
|
423
789
|
id,
|
|
424
790
|
version: manifest.version,
|
|
425
791
|
layer: KERNEL_LAYER[layer],
|
|
426
792
|
// 写日志的能力调用时才检查,推不出来(definePlugin 只补 hook 用到的);jade 的插件都可以写
|
|
427
793
|
needs: ["log:record", "log:emit"],
|
|
428
|
-
tools: tools.
|
|
429
|
-
hooks:
|
|
794
|
+
tools: scope.closed ? [] : tools.filter((t) => s.keeps(t)),
|
|
795
|
+
hooks: live,
|
|
430
796
|
})),
|
|
431
797
|
};
|
|
432
798
|
s.parts.push(part);
|
|
799
|
+
// 插件被隔离(作用域关闭):它的工具从下一项工作起不再给模型看
|
|
800
|
+
scope.add(() => s.invalidate());
|
|
433
801
|
}
|
|
434
802
|
/** 项目层插件交给内核的插件要用确认时拿到的 mark 标记,否则内核拒绝整个会话 */
|
|
435
803
|
#confirmed(p, plugin) {
|
|
@@ -444,6 +812,9 @@ export class Sessions {
|
|
|
444
812
|
* 获取失败也只当作没有:界面上的东西坏了不该让插件加入不了会话
|
|
445
813
|
*/
|
|
446
814
|
#sessionUI(s, plugin, owner) {
|
|
815
|
+
// 子会话不是当前会话,没有自己的界面(4.2)
|
|
816
|
+
if (s.child)
|
|
817
|
+
return undefined;
|
|
447
818
|
try {
|
|
448
819
|
const ui = this.#host.service("ui@1", owner);
|
|
449
820
|
return ui?.session?.({ session: s.id, plugin });
|
|
@@ -461,6 +832,21 @@ export class Sessions {
|
|
|
461
832
|
const gate = (call) => this.#guarded(s) || call.tool.effect === "read" ? { decision: "abstain" } : { decision: "deny", reason: NO_APPROVAL_REASON };
|
|
462
833
|
return kernelPlugin({ id: CORE_PLUGIN_ID, version: API_VERSION, layer: "distro", hooks: [{ kind: "gate", point: "tool.gate", fn: gate }] });
|
|
463
834
|
}
|
|
835
|
+
/**
|
|
836
|
+
* 子会话的委派说明和开它的插件给的系统提示词(7.3)。context.build 按顺序执行,别的插件(jade/prompts)
|
|
837
|
+
* 把自己的段落放在已有段落之前,最后执行的才能放在最前面:所以单独一个插件,hook 的 order 设得很大
|
|
838
|
+
*/
|
|
839
|
+
#delegation(child) {
|
|
840
|
+
const seg = (text, by) => ({ text, stability: "stable", origin: { by: "plugin", id: by }, trust: "trusted" });
|
|
841
|
+
const head = [seg(DELEGATION, CORE_PLUGIN_ID), ...(child.system ? [seg(child.system, child.by)] : [])];
|
|
842
|
+
return kernelPlugin({
|
|
843
|
+
id: `${CORE_PLUGIN_ID}/delegation`,
|
|
844
|
+
version: API_VERSION,
|
|
845
|
+
layer: "distro",
|
|
846
|
+
// order 大:任何层的插件之后执行
|
|
847
|
+
hooks: [{ kind: "transform", point: "context.build", order: 1_000_000, fn: (req) => ({ ...req, system: [...head, ...req.system] }) }],
|
|
848
|
+
});
|
|
849
|
+
}
|
|
464
850
|
/** approval@1 的提供者(不能是项目层插件)在这个会话里有仍然有效的 tool.gate */
|
|
465
851
|
#guarded(s) {
|
|
466
852
|
const approval = this.#host.provider("approval@1");
|
|
@@ -469,9 +855,12 @@ export class Sessions {
|
|
|
469
855
|
/** 离开会话:中止正在跑的工作,等它停下,detach,关闭插件的会话作用域 */
|
|
470
856
|
async #leave(s) {
|
|
471
857
|
s.leave();
|
|
858
|
+
// 它开的子会话一并结束:正在等子会话的工具调用随之结束
|
|
859
|
+
for (const c of [...s.children])
|
|
860
|
+
await this.#end(c);
|
|
472
861
|
await s.idle();
|
|
473
862
|
try {
|
|
474
|
-
await s.
|
|
863
|
+
await s.detach();
|
|
475
864
|
}
|
|
476
865
|
catch (e) {
|
|
477
866
|
this.#host.report(`session ${s.id}: detach failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
@@ -495,6 +884,46 @@ function compatProblem(id, version, hooks) {
|
|
|
495
884
|
return e instanceof Error ? e.message : String(e);
|
|
496
885
|
}
|
|
497
886
|
}
|
|
887
|
+
/** 状态栏的一段只留认得的字段:文字都换成字符串,不认得的 tone 去掉 */
|
|
888
|
+
function statusSegment(s) {
|
|
889
|
+
if (s === undefined || typeof s !== "object" || s === null)
|
|
890
|
+
return s === undefined ? undefined : String(s);
|
|
891
|
+
const tones = ["ok", "warn", "error", "accent"];
|
|
892
|
+
return {
|
|
893
|
+
text: String(s.text ?? ""),
|
|
894
|
+
...(s.short !== undefined ? { short: String(s.short) } : {}),
|
|
895
|
+
...(tones.includes(s.tone) ? { tone: s.tone } : {}),
|
|
896
|
+
...(s.keep === true ? { keep: true } : {}),
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* 工具的 meta 只留认得的字段(字段名都是字符串),写错了抛错:插件加入会话失败,比别的插件悄悄认不出这个工具好查。
|
|
901
|
+
* 项目层插件的 shell 不算:审批按它把只读命令直接放行、只读的子 Agent 也留着这个工具,克隆的仓库不能自己声明这个。
|
|
902
|
+
* 去掉之后它就是普通的工具,照常经过审批
|
|
903
|
+
*/
|
|
904
|
+
function toolMeta(plugin, layer, t) {
|
|
905
|
+
if (t.meta === undefined)
|
|
906
|
+
return {};
|
|
907
|
+
const m = t.meta;
|
|
908
|
+
if (typeof m !== "object" || m === null)
|
|
909
|
+
throw new Error(`plugin ${plugin}: tool ${t.name}: meta must be an object`);
|
|
910
|
+
const meta = {};
|
|
911
|
+
for (const k of ["writes", "shell"]) {
|
|
912
|
+
if (m[k] === undefined)
|
|
913
|
+
continue;
|
|
914
|
+
if (typeof m[k] !== "string" || !m[k])
|
|
915
|
+
throw new Error(`plugin ${plugin}: tool ${t.name}: meta.${k} must be the name of an input field`);
|
|
916
|
+
if (k === "shell" && layer === "project")
|
|
917
|
+
continue;
|
|
918
|
+
meta[k] = m[k];
|
|
919
|
+
}
|
|
920
|
+
return { meta };
|
|
921
|
+
}
|
|
922
|
+
/** 工具的声明部分(给插件看的 SessionTool),meta 是拷贝 */
|
|
923
|
+
function sessionTool(t) {
|
|
924
|
+
const { name, description, inputSchema, effect, concurrency, idempotent, meta } = t;
|
|
925
|
+
return { name, description, inputSchema, effect, concurrency, idempotent, ...(meta ? { meta: { ...meta } } : {}) };
|
|
926
|
+
}
|
|
498
927
|
/** 插件的工具:以插件身份执行;插件被隔离后返回错误结果(由内核转成 tool_result) */
|
|
499
928
|
function liveTool(t, owner) {
|
|
500
929
|
return { ...t, execute: (input, ctx) => invoke(owner, `tool ${t.name}`, () => t.execute(input, ctx)) };
|
|
@@ -518,4 +947,36 @@ function liveHook(h, owner) {
|
|
|
518
947
|
};
|
|
519
948
|
return { ...h, fn: wrapped };
|
|
520
949
|
}
|
|
950
|
+
/** 会话是第几层子会话:沿着 session.started 里的 parent 往上数(内核保证引用能解析到,宪法 3.4) */
|
|
951
|
+
async function depthOf(store, sessionId) {
|
|
952
|
+
let depth = 0;
|
|
953
|
+
let id = sessionId;
|
|
954
|
+
const seen = new Set();
|
|
955
|
+
while (id && !seen.has(id) && depth < 100) {
|
|
956
|
+
seen.add(id);
|
|
957
|
+
let parent;
|
|
958
|
+
for await (const e of store.read(id)) {
|
|
959
|
+
if (e.type === "session.started")
|
|
960
|
+
parent = e.data.parent?.sessionId;
|
|
961
|
+
break;
|
|
962
|
+
}
|
|
963
|
+
if (!parent)
|
|
964
|
+
break;
|
|
965
|
+
depth++;
|
|
966
|
+
id = parent;
|
|
967
|
+
}
|
|
968
|
+
return depth;
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* 把 from 的日志里 seq 小于 before 的事件写成新会话 to 的日志(OpenOptions.fork)。before 要是一条消息输入的 seq:
|
|
972
|
+
* 这样复制出的会话停在一轮结束之后、下一条消息之前,恢复时没有没做完的一轮
|
|
973
|
+
*/
|
|
974
|
+
async function copyLog(store, from, before, to) {
|
|
975
|
+
const events = await readAll(store, from);
|
|
976
|
+
const at = events.find((e) => e.seq === before);
|
|
977
|
+
if (!at || at.type !== "input.received" || at.data.kind !== "message") {
|
|
978
|
+
throw new Error(`sessions.open: seq ${before} of session ${from} is not a message from the user`);
|
|
979
|
+
}
|
|
980
|
+
await store.append(to, events.filter((e) => e.seq < before));
|
|
981
|
+
}
|
|
521
982
|
//# sourceMappingURL=session.js.map
|