@uncarved/jade 0.2.1 → 0.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 (213) hide show
  1. package/README.md +64 -2
  2. package/assets/logo.svg +1 -0
  3. package/bin/jade.js +4 -0
  4. package/dist/api.d.ts +5 -250
  5. package/dist/api.js +5 -7
  6. package/dist/api.js.map +1 -1
  7. package/dist/fetch.d.ts +8 -0
  8. package/dist/fetch.js +19 -17
  9. package/dist/fetch.js.map +1 -1
  10. package/dist/host.d.ts +12 -1
  11. package/dist/host.js +49 -1
  12. package/dist/host.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/main.js +26 -4
  17. package/dist/main.js.map +1 -1
  18. package/dist/remote.js +4 -0
  19. package/dist/remote.js.map +1 -1
  20. package/dist/session.d.ts +2 -0
  21. package/dist/session.js +133 -13
  22. package/dist/session.js.map +1 -1
  23. package/dist/source.d.ts +2 -10
  24. package/dist/source.js.map +1 -1
  25. package/dist/update.d.ts +8 -20
  26. package/dist/update.js +12 -1
  27. package/dist/update.js.map +1 -1
  28. package/package.json +4 -2
  29. package/plugins/approval/index.ts +233 -85
  30. package/plugins/approval/plugin.json +1 -1
  31. package/plugins/approval/policy.ts +2 -1
  32. package/plugins/approval/review.ts +1 -1
  33. package/plugins/ask/index.ts +2 -2
  34. package/plugins/budget/index.ts +1 -1
  35. package/plugins/checks/checks.ts +3 -2
  36. package/plugins/checks/index.ts +2 -5
  37. package/plugins/commands/index.ts +2 -44
  38. package/plugins/compact/compact.ts +8 -3
  39. package/plugins/compact/index.ts +13 -16
  40. package/plugins/continue/index.ts +1 -1
  41. package/plugins/cost/index.ts +143 -0
  42. package/plugins/cost/plugin.json +13 -0
  43. package/plugins/custom-commands/index.ts +85 -0
  44. package/plugins/custom-commands/plugin.json +8 -0
  45. package/plugins/editor/index.ts +88 -0
  46. package/plugins/editor/plugin.json +17 -0
  47. package/plugins/fs/fs.ts +33 -6
  48. package/plugins/fs/grep.ts +6 -4
  49. package/plugins/fs/index.ts +2 -3
  50. package/plugins/fs/renderers.ts +1 -1
  51. package/plugins/hooks/index.ts +152 -0
  52. package/plugins/hooks/plugin.json +14 -0
  53. package/plugins/keys/index.ts +2 -18
  54. package/plugins/mcp/index.ts +40 -12
  55. package/plugins/mcp/install.ts +139 -0
  56. package/plugins/mcp/mcp.ts +2 -1
  57. package/plugins/mcp/plugin.json +1 -1
  58. package/plugins/mcp/project.ts +37 -1
  59. package/plugins/memory/index.ts +4 -9
  60. package/plugins/memory/plugin.json +1 -1
  61. package/plugins/mentions/index.ts +2 -2
  62. package/plugins/models/configure.ts +258 -150
  63. package/plugins/models/context.ts +2 -2
  64. package/plugins/models/index.ts +35 -42
  65. package/plugins/models/plugin.json +1 -1
  66. package/plugins/models/provider.ts +22 -4
  67. package/plugins/models/saved.ts +8 -4
  68. package/plugins/modes/index.ts +2 -17
  69. package/plugins/plugins/index.ts +2 -2
  70. package/plugins/print/index.ts +36 -6
  71. package/plugins/print/plugin.json +1 -0
  72. package/plugins/prompts/index.ts +22 -14
  73. package/plugins/{memory → prompts}/notes.ts +21 -9
  74. package/plugins/prompts/plugin.json +8 -2
  75. package/plugins/prompts/prompts.ts +5 -4
  76. package/plugins/renderers/index.ts +2 -43
  77. package/plugins/repeats/index.ts +1 -1
  78. package/plugins/schedule/index.ts +2 -2
  79. package/plugins/scratch/index.ts +2 -3
  80. package/plugins/screenshot/index.ts +26 -13
  81. package/plugins/screenshot/plugin.json +1 -1
  82. package/plugins/sessions/index.ts +2 -17
  83. package/plugins/shell/index.ts +56 -6
  84. package/plugins/shell/plugin.json +3 -1
  85. package/plugins/shell/renderers.ts +1 -1
  86. package/plugins/shell/service.ts +2 -13
  87. package/plugins/shell/shell.ts +31 -16
  88. package/plugins/skills/index.ts +172 -37
  89. package/plugins/skills/install.ts +212 -0
  90. package/plugins/skills/plugin.json +3 -2
  91. package/plugins/skills/project.ts +38 -1
  92. package/plugins/skills/skills.ts +28 -9
  93. package/plugins/steer/index.ts +142 -0
  94. package/plugins/steer/plugin.json +8 -0
  95. package/plugins/suggest/index.ts +16 -11
  96. package/plugins/suggest/plugin.json +1 -1
  97. package/plugins/task/index.ts +7 -16
  98. package/plugins/task/plugin.json +9 -2
  99. package/plugins/task/subagent.ts +23 -5
  100. package/plugins/todo/index.ts +2 -2
  101. package/plugins/tui/editor.ts +10 -2
  102. package/plugins/tui/frontend.ts +103 -21
  103. package/plugins/tui/index.ts +3 -8
  104. package/plugins/tui/markdown.ts +51 -6
  105. package/plugins/tui/mascot.ts +505 -0
  106. package/plugins/tui/plugin.json +2 -2
  107. package/plugins/tui/screen.ts +6 -0
  108. package/plugins/tui/transcript.ts +17 -2
  109. package/plugins/tui/tui.ts +321 -90
  110. package/plugins/undo/checkpoint.ts +2 -2
  111. package/plugins/undo/index.ts +84 -26
  112. package/plugins/undo/plugin.json +8 -2
  113. package/plugins/undo/snapshot.ts +129 -0
  114. package/plugins/update/index.ts +11 -15
  115. package/plugins/update/plugin.json +2 -1
  116. package/plugins/web/index.ts +2 -5
  117. package/plugins/web/renderers.ts +1 -1
  118. package/plugins-dist/approval/index.js +217 -51
  119. package/plugins-dist/approval/plugin.json +1 -1
  120. package/plugins-dist/ask/index.js +1 -1
  121. package/plugins-dist/budget/index.js +1 -1
  122. package/plugins-dist/checks/checks.js +2 -1
  123. package/plugins-dist/checks/index.js +1 -1
  124. package/plugins-dist/commands/index.js +1 -1
  125. package/plugins-dist/compact/compact.js +6 -1
  126. package/plugins-dist/compact/index.js +12 -8
  127. package/plugins-dist/continue/index.js +1 -1
  128. package/plugins-dist/cost/index.js +135 -0
  129. package/plugins-dist/cost/plugin.json +16 -0
  130. package/plugins-dist/custom-commands/index.js +86 -0
  131. package/plugins-dist/custom-commands/plugin.json +12 -0
  132. package/plugins-dist/editor/index.js +89 -0
  133. package/plugins-dist/editor/plugin.json +21 -0
  134. package/plugins-dist/fs/fs.js +34 -6
  135. package/plugins-dist/fs/grep.js +5 -3
  136. package/plugins-dist/fs/index.js +1 -1
  137. package/plugins-dist/hooks/index.js +145 -0
  138. package/plugins-dist/hooks/plugin.json +19 -0
  139. package/plugins-dist/keys/index.js +1 -1
  140. package/plugins-dist/mcp/index.js +41 -9
  141. package/plugins-dist/mcp/install.js +160 -0
  142. package/plugins-dist/mcp/mcp.js +2 -1
  143. package/plugins-dist/mcp/plugin.json +2 -1
  144. package/plugins-dist/mcp/project.js +42 -1
  145. package/plugins-dist/memory/index.js +3 -5
  146. package/plugins-dist/memory/plugin.json +1 -1
  147. package/plugins-dist/mentions/index.js +1 -1
  148. package/plugins-dist/models/configure.js +252 -151
  149. package/plugins-dist/models/context.js +2 -2
  150. package/plugins-dist/models/index.js +37 -14
  151. package/plugins-dist/models/plugin.json +1 -1
  152. package/plugins-dist/models/provider.js +4 -2
  153. package/plugins-dist/models/saved.js +9 -4
  154. package/plugins-dist/modes/index.js +1 -1
  155. package/plugins-dist/plugins/index.js +1 -1
  156. package/plugins-dist/print/index.js +37 -3
  157. package/plugins-dist/print/plugin.json +3 -0
  158. package/plugins-dist/prompts/index.js +22 -3
  159. package/plugins-dist/{memory → prompts}/notes.js +10 -7
  160. package/plugins-dist/prompts/plugin.json +8 -2
  161. package/plugins-dist/prompts/prompts.js +5 -4
  162. package/plugins-dist/renderers/index.js +1 -1
  163. package/plugins-dist/repeats/index.js +1 -1
  164. package/plugins-dist/schedule/index.js +1 -1
  165. package/plugins-dist/scratch/index.js +1 -1
  166. package/plugins-dist/screenshot/index.js +25 -11
  167. package/plugins-dist/screenshot/plugin.json +1 -1
  168. package/plugins-dist/sessions/index.js +1 -1
  169. package/plugins-dist/shell/index.js +55 -3
  170. package/plugins-dist/shell/plugin.json +3 -1
  171. package/plugins-dist/shell/shell.js +26 -8
  172. package/plugins-dist/skills/index.js +184 -35
  173. package/plugins-dist/skills/install.js +178 -0
  174. package/plugins-dist/skills/plugin.json +6 -2
  175. package/plugins-dist/skills/project.js +43 -1
  176. package/plugins-dist/skills/skills.js +27 -10
  177. package/plugins-dist/steer/index.js +135 -0
  178. package/plugins-dist/steer/plugin.json +12 -0
  179. package/plugins-dist/suggest/index.js +15 -9
  180. package/plugins-dist/suggest/plugin.json +1 -1
  181. package/plugins-dist/task/index.js +6 -4
  182. package/plugins-dist/task/plugin.json +10 -2
  183. package/plugins-dist/task/subagent.js +20 -5
  184. package/plugins-dist/todo/index.js +1 -1
  185. package/plugins-dist/tui/editor.js +7 -2
  186. package/plugins-dist/tui/frontend.js +107 -13
  187. package/plugins-dist/tui/index.js +2 -1
  188. package/plugins-dist/tui/markdown.js +48 -6
  189. package/plugins-dist/tui/mascot.js +439 -0
  190. package/plugins-dist/tui/plugin.json +3 -2
  191. package/plugins-dist/tui/screen.js +2 -0
  192. package/plugins-dist/tui/transcript.js +16 -1
  193. package/plugins-dist/tui/tui.js +302 -81
  194. package/plugins-dist/undo/checkpoint.js +1 -1
  195. package/plugins-dist/undo/index.js +91 -24
  196. package/plugins-dist/undo/plugin.json +10 -3
  197. package/plugins-dist/undo/snapshot.js +111 -0
  198. package/plugins-dist/update/index.js +9 -10
  199. package/plugins-dist/update/plugin.json +4 -1
  200. package/plugins-dist/web/index.js +1 -1
  201. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  202. package/sdk/python/jade_plugin.py +8 -0
  203. package/src/api.ts +7 -262
  204. package/src/fetch.ts +18 -16
  205. package/src/host.ts +49 -1
  206. package/src/index.ts +1 -1
  207. package/src/main.ts +23 -5
  208. package/src/remote.ts +2 -0
  209. package/src/session.ts +134 -15
  210. package/src/source.ts +2 -11
  211. package/src/update.ts +16 -10
  212. package/plugins/undo/notes.ts +0 -43
  213. package/plugins-dist/undo/notes.js +0 -36
package/src/session.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  checkCompat,
7
7
  readAll,
8
8
  definePlugin as kernelPlugin,
9
+ InvalidTransition,
9
10
  Runtime,
10
11
  type GateResult,
11
12
  type Layer,
@@ -45,6 +46,9 @@ export const CORE_PLUGIN_ID = "jade/core";
45
46
  export const NO_APPROVAL_REASON =
46
47
  "No permission plugin is active in this session, so only read-only tools may run. Ask the user to enable an approval plugin.";
47
48
 
49
+ /** 有插件要人工审批、但没有审批插件来问时,这一轮结束的原因 */
50
+ export const NO_APPROVER_ERROR = "A plugin asked for the user's approval, but no approval plugin is active in this session to ask.";
51
+
48
52
  /** 会话管理器向宿主要的东西 */
49
53
  export interface SessionHost {
50
54
  home: string;
@@ -75,23 +79,38 @@ interface Part {
75
79
  kernel: Plugin;
76
80
  }
77
81
 
82
+ interface Turn {
83
+ stop?: { reason: unknown };
84
+ wake?: () => void;
85
+ }
86
+
78
87
  class LiveSession implements Session {
79
88
  readonly id: string;
80
89
  readonly parts: Part[] = [];
81
90
  rt!: Runtime;
82
- #queue: { input: UserInput; resolve: () => void; reject: (e: unknown) => void }[] = [];
91
+ /** 排队的工作:一条输入,或者恢复(recover)。一次只跑一项,Runtime 同一时间只接受一个调用 */
92
+ #queue: { run: () => Promise<void>; resolve: () => void; reject: (e: unknown) => void }[] = [];
83
93
  #after: { fn: () => void | Promise<void>; owner: Owner }[] = [];
84
94
  #pump: Promise<void> | undefined;
85
95
  /** 离开时的原因;有了以后不再接受输入 */
86
96
  #left: Error | undefined;
87
97
  #report: (m: string) => void;
98
+ /** 正在跑的这一轮(包括停下来等人工审批的时候):stop 是要它结束的原因,wake 叫醒等审批的循环 */
99
+ #turn: Turn | undefined;
100
+ /** 交进来、还没交给内核的审批回答 */
101
+ #answers: { input: UserInput; resolve: () => void; reject: (e: unknown) => void }[] = [];
102
+ /** 审批插件是不是守着这个会话(第 7 节):不是的话没有人会回答审批 */
103
+ #attended: () => boolean;
104
+ /** 插件订阅的运行时通知(ctx.session.subscribe) */
105
+ #watchers = new Set<{ fn: (n: RuntimeNotice) => void; owner: Owner }>();
88
106
 
89
107
  readonly #store: LogStore;
90
108
 
91
- constructor(id: string, store: LogStore, report: (m: string) => void) {
109
+ constructor(id: string, store: LogStore, report: (m: string) => void, attended: () => boolean) {
92
110
  this.id = id;
93
111
  this.#store = store;
94
112
  this.#report = report;
113
+ this.#attended = attended;
95
114
  }
96
115
 
97
116
  history(): Promise<LogEvent[]> {
@@ -103,9 +122,64 @@ class LiveSession implements Session {
103
122
  }
104
123
 
105
124
  send(input: UserInput): Promise<void> {
125
+ // 对插件变化的确认不开始一轮(内核只记录结果)
126
+ if (input.kind === "plugins") return this.#enqueue(() => this.rt.send(input));
127
+ if (input.kind !== "approval") return this.#enqueue(() => this.#drive(() => this.rt.send(input)));
128
+ // 审批的回答属于正在跑的这一轮,不排到它后面
129
+ if (this.#left) return Promise.reject(this.#left);
130
+ const turn = this.#turn;
131
+ if (!turn) return Promise.reject(new Error(`session ${this.id}: no turn is waiting for approval`));
132
+ return new Promise((resolve, reject) => {
133
+ this.#answers.push({ input, resolve, reject });
134
+ turn.wake?.();
135
+ });
136
+ }
137
+
138
+ /**
139
+ * 跑完一轮。内核停在等待人工审批(awaiting)时这一轮还没完:在这里等审批的回答交给内核,直到这一轮结束。
140
+ * 等的时候被中止,或者没有审批插件来问,就结束这一轮(内核的 endTurn)
141
+ */
142
+ async #drive(start: () => Promise<void>): Promise<void> {
143
+ const turn: Turn = {};
144
+ this.#turn = turn;
145
+ try {
146
+ await start();
147
+ while (this.rt.phase === "awaiting") {
148
+ const stop = turn.stop ?? (this.#attended() ? undefined : { reason: new Error(NO_APPROVER_ERROR) });
149
+ if (stop) {
150
+ const reason = stop.reason ?? new Error("aborted");
151
+ await this.rt.endTurn(reason);
152
+ throw reason;
153
+ }
154
+ const next = this.#answers.shift();
155
+ if (!next) {
156
+ await new Promise<void>((wake) => (turn.wake = wake));
157
+ turn.wake = undefined;
158
+ continue;
159
+ }
160
+ try {
161
+ await this.rt.send(next.input);
162
+ next.resolve();
163
+ } catch (e) {
164
+ // 内核不认的回答(没有这个待批的调用):只是这个回答作废,这一轮照常等
165
+ if (e instanceof InvalidTransition && this.rt.phase === "awaiting") {
166
+ next.reject(e);
167
+ continue;
168
+ }
169
+ next.resolve();
170
+ throw e;
171
+ }
172
+ }
173
+ } finally {
174
+ this.#turn = undefined;
175
+ for (const a of this.#answers.splice(0)) a.reject(new Error(`session ${this.id}: the turn ended before this approval was used`));
176
+ }
177
+ }
178
+
179
+ #enqueue(run: () => Promise<void>): Promise<void> {
106
180
  if (this.#left) return Promise.reject(this.#left);
107
181
  return new Promise((resolve, reject) => {
108
- this.#queue.push({ input, resolve, reject });
182
+ this.#queue.push({ run, resolve, reject });
109
183
  this.#kick();
110
184
  });
111
185
  }
@@ -121,19 +195,50 @@ class LiveSession implements Session {
121
195
  this.#left ??= new Error(`session ${this.id} was closed before this input ran`);
122
196
  for (const job of this.#queue.splice(0)) job.reject(this.#left);
123
197
  this.#after.length = 0;
124
- this.rt.abort(this.#left);
198
+ this.abort(this.#left);
125
199
  }
126
200
 
127
201
  async idle(): Promise<void> {
128
202
  while (this.#pump) await this.#pump;
129
203
  }
130
204
 
205
+ /**
206
+ * 和输入排同一个队:恢复可能要跑完一整轮,这期间插件发来的输入排在它后面,而不是撞上 RuntimeBusy。
207
+ * 上次停在等待人工审批的(进程被杀、崩溃):内核再发一次 awaiting 通知,审批插件据此重新问(ctx.session.subscribe),
208
+ * 这一轮接着等回答;没有审批插件守着时和平时一样直接结束
209
+ */
131
210
  recover(): Promise<void> {
132
- return this.rt.recover();
211
+ return this.#enqueue(() => this.#drive(() => this.rt.recover()));
212
+ }
213
+
214
+ /** 插件订阅运行时通知,返回取消订阅。Runtime 建好之前就可以订阅(session() 里) */
215
+ watch(fn: (n: RuntimeNotice) => void, owner: Owner): () => void {
216
+ const w = { fn, owner };
217
+ this.#watchers.add(w);
218
+ return () => void this.#watchers.delete(w);
219
+ }
220
+
221
+ /** Runtime 建好之后调用一次:把通知转给订阅的插件,以插件身份执行,出错只报告 */
222
+ attach(): void {
223
+ this.rt.subscribe((n) => {
224
+ for (const w of this.#watchers) {
225
+ if (w.owner.scope.closed) continue;
226
+ try {
227
+ invoke(w.owner, "session.subscribe", () => w.fn(n));
228
+ } catch (e) {
229
+ this.#report(e instanceof Error ? e.message : String(e));
230
+ }
231
+ }
232
+ });
133
233
  }
134
234
 
135
235
  abort(reason?: unknown): void {
136
236
  this.rt.abort(reason);
237
+ // 停在等待人工审批时内核没有在跑,信号结束不了这一轮
238
+ if (this.#turn) {
239
+ this.#turn.stop = { reason };
240
+ this.#turn.wake?.();
241
+ }
137
242
  }
138
243
 
139
244
  subscribe(fn: (n: RuntimeNotice) => void): () => void {
@@ -148,14 +253,14 @@ class LiveSession implements Session {
148
253
  });
149
254
  }
150
255
 
151
- /** 一次处理一条输入;每轮之后执行 afterTurn,它们可能又排进新的输入 */
256
+ /** 一次处理一项排队的工作;每轮之后执行 afterTurn,它们可能又排进新的输入 */
152
257
  async #run(): Promise<void> {
153
258
  await null; // 让调用方先拿到 send 的 Promise
154
259
  for (;;) {
155
260
  const job = this.#queue.shift();
156
261
  if (job) {
157
262
  try {
158
- await this.rt.send(job.input);
263
+ await job.run();
159
264
  job.resolve();
160
265
  } catch (e) {
161
266
  job.reject(e);
@@ -219,7 +324,7 @@ export class Sessions implements SessionManager {
219
324
  if (!provider) throw new ServiceUnavailable("provider@1", "(none): no active plugin provides a model");
220
325
  const store = host.service<LogStore>("store@1", host.core) ?? (this.#store ??= new JsonlLogStore(path.join(host.home, "sessions")));
221
326
 
222
- const s = new LiveSession(opts.resume ?? crypto.randomUUID(), store, host.report);
327
+ const s: LiveSession = new LiveSession(opts.resume ?? crypto.randomUUID(), store, host.report, () => this.#guarded(s));
223
328
  try {
224
329
  for (const p of host.active()) if (p.module.session) await this.#join(s, p);
225
330
  const plugins = [this.#corePlugin(s), ...s.parts.map((p) => p.kernel)];
@@ -230,6 +335,7 @@ export class Sessions implements SessionManager {
230
335
  await this.#closeParts(s);
231
336
  throw e;
232
337
  }
338
+ s.attach();
233
339
  s.rt.subscribe((n) => {
234
340
  if (n.type === "hook-error") host.report(`plugin ${n.info.plugin} failed at ${n.info.point}: ${n.info.error instanceof Error ? n.info.error.message : String(n.info.error)}`);
235
341
  });
@@ -314,6 +420,18 @@ export class Sessions implements SessionManager {
314
420
  if (layer === "project") throw new Error(`plugin ${id}: project plugins cannot stop turns on the user's behalf`);
315
421
  s.abort(new Error(reason ?? `stopped by plugin ${id}`));
316
422
  },
423
+ state: () => {
424
+ if (scope.closed) throw new StaleContext(id);
425
+ // session() 执行期间 Runtime 还没建(全部插件加入之后才建)
426
+ if (!s.rt) throw new Error(`plugin ${id}: the session's state is not available while session() runs`);
427
+ return s.rt.state;
428
+ },
429
+ subscribe: (fn) => {
430
+ if (scope.closed) throw new StaleContext(id);
431
+ const off = s.watch(fn, owner);
432
+ scope.add(off);
433
+ return off;
434
+ },
317
435
  },
318
436
  kernel: {
319
437
  tool: (t) => {
@@ -394,16 +512,17 @@ export class Sessions implements SessionManager {
394
512
  * 项目层的提供者也不算:克隆的仓库不能放宽审批
395
513
  */
396
514
  #corePlugin(s: LiveSession): Plugin {
397
- const gate = (call: { tool: { effect: string } }): GateResult => {
398
- const approval = this.#host.provider("approval@1");
399
- const guarded = s.parts.some(
400
- (p) => p.gates > 0 && !p.owner.scope.closed && p.source.manifest.id === approval && p.source.layer !== "project",
401
- );
402
- return guarded || call.tool.effect === "read" ? { decision: "abstain" } : { decision: "deny", reason: NO_APPROVAL_REASON };
403
- };
515
+ const gate = (call: { tool: { effect: string } }): GateResult =>
516
+ this.#guarded(s) || call.tool.effect === "read" ? { decision: "abstain" } : { decision: "deny", reason: NO_APPROVAL_REASON };
404
517
  return kernelPlugin({ id: CORE_PLUGIN_ID, version: API_VERSION, layer: "distro", hooks: [{ kind: "gate", point: "tool.gate", fn: gate }] });
405
518
  }
406
519
 
520
+ /** approval@1 的提供者(不能是项目层插件)在这个会话里有仍然有效的 tool.gate */
521
+ #guarded(s: LiveSession): boolean {
522
+ const approval = this.#host.provider("approval@1");
523
+ return s.parts.some((p) => p.gates > 0 && !p.owner.scope.closed && p.source.manifest.id === approval && p.source.layer !== "project");
524
+ }
525
+
407
526
  /** 离开会话:中止正在跑的工作,等它停下,detach,关闭插件的会话作用域 */
408
527
  async #leave(s: LiveSession): Promise<void> {
409
528
  s.leave();
package/src/source.ts CHANGED
@@ -10,17 +10,8 @@ import { stat } from "node:fs/promises";
10
10
  import os from "node:os";
11
11
  import path from "node:path";
12
12
 
13
- export type SourceKind = "git" | "npm" | "local";
14
-
15
- export interface PluginSourceSpec {
16
- kind: SourceKind;
17
- /** git:仓库地址;npm:交给包管理器的写法;local:绝对路径 */
18
- location: string;
19
- /** git 的分支、tag 或提交 */
20
- ref?: string;
21
- /** 用户写的原文,记进安装记录 */
22
- spec: string;
23
- }
13
+ import type { PluginSourceSpec } from "./api.ts";
14
+ export type { PluginSourceSpec, SourceKind } from "./api.ts";
24
15
 
25
16
  /** 识别一个来源。本地路径在这里就已经解析成绝对路径;不检查插件的 plugin.json */
26
17
  export async function parseSource(text: string, cwd: string): Promise<PluginSourceSpec> {
package/src/update.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // jade 本身的升级:查 npm 上的最新版本,按 jade 的安装方式(npm、bun、pnpm、yarn 全局安装)升级。
2
- // jade update 直接用这里(核心命令,插件坏了也能升级);启动时的检查和后台升级在 jade/update 插件里,也用这里。
2
+ // jade update 直接用这里(核心命令,插件坏了也能升级);启动时的检查和后台升级在 jade/update 插件里,经核心的 update@1 服务(updater)用这里。
3
3
  // 启动时的检查每天最多一次,结果缓存在 $JADE_HOME/update-check.json,查不到时什么也不说
4
4
 
5
5
  import { spawn } from "node:child_process";
@@ -7,8 +7,11 @@ import { closeSync, openSync, readFileSync, realpathSync } from "node:fs";
7
7
  import { mkdir, open, readFile, rm, stat, writeFile } from "node:fs/promises";
8
8
  import path from "node:path";
9
9
  import { fileURLToPath } from "node:url";
10
+ import type { AutoUpdateResult, Install, Updater } from "./api.ts";
10
11
  import { npmCommand, which } from "./fetch.ts";
11
12
 
13
+ export type { AutoUpdateResult, Install } from "./api.ts";
14
+
12
15
  export const PACKAGE = "@uncarved/jade";
13
16
  /** 正在运行的 jade 的版本 */
14
17
  export const VERSION: string = JSON.parse(readFileSync(path.join(packageDir(), "package.json"), "utf8")).version;
@@ -79,8 +82,6 @@ export async function newerVersion(home: string, opts: { now?: number; current?:
79
82
  return isNewer(cache.latest, current) ? cache.latest : undefined;
80
83
  }
81
84
 
82
- export type Install = { kind: "npm" | "bun" | "pnpm" | "yarn"; dir: string } | { kind: "dev"; dir: string };
83
-
84
85
  /**
85
86
  * 看 jade 的包目录(解析符号链接后)在哪:不在 node_modules 里就是仓库里的开发版或 npm link;
86
87
  * 在 bun、pnpm、yarn 的全局目录里按对应工具算,其余按 npm 算
@@ -184,13 +185,6 @@ export function autoUpdateEnabled(): boolean {
184
185
  return !off(process.env.JADE_AUTO_UPDATE) && !off(process.env.JADE_UPDATE_CHECK);
185
186
  }
186
187
 
187
- export type AutoUpdateResult =
188
- /** 装好了,下次启动生效 */
189
- | { status: "installed" }
190
- /** 别的 jade 正在升级,或者这个版本不久前刚失败过:这次不试 */
191
- | { status: "skipped"; why: "busy" | "failed-recently" | "unsupported" }
192
- | { status: "failed"; log: string };
193
-
194
188
  interface Attempt {
195
189
  version: string;
196
190
  at: number;
@@ -246,6 +240,18 @@ export async function autoUpdate(home: string, latest: string, opts: { install?:
246
240
  return ok ? { status: "installed" } : { status: "failed", log };
247
241
  }
248
242
 
243
+ /** update@1(3.4):$JADE_HOME 固定下来的这一套。opts 是测试用的:假装的安装方式、最新版本和升级命令 */
244
+ export function updater(home: string, opts: { install?: Install; latest?: () => Promise<string>; command?: string[] } = {}): Updater {
245
+ const install = opts.install ?? detectInstall();
246
+ return {
247
+ version: VERSION,
248
+ install: install.kind,
249
+ newer: () => newerVersion(home, opts.latest ? { latest: opts.latest } : {}),
250
+ autoEnabled: autoUpdateEnabled,
251
+ upgrade: (latest) => autoUpdate(home, latest, { install, ...(opts.command ? { command: opts.command } : {}) }),
252
+ };
253
+ }
254
+
249
255
  /** 新建锁文件;已经有了且不算太旧时返回 false,太旧的(上次的进程不在了)删掉重来 */
250
256
  async function takeLock(lock: string): Promise<boolean> {
251
257
  for (let i = 0; i < 2; i++) {
@@ -1,43 +0,0 @@
1
- // 给模型的说明([Note from jade]):命令里做的事(例如用户撤销了模型改的文件)要让模型知道,
2
- // 但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
3
- // 附在最后一条消息后面,并用 ctx.record 写进日志(derived):之后的投影、恢复的会话里它都在同一个位置。
4
- // 插件之间不共享代码,jade/memory 里也有一份。
5
-
6
- import type { Block, LocalHookSpec, Message } from "uncarved";
7
-
8
- /** derived 里:消息的下标 → 附在它后面的说明 */
9
- type NotesState = Record<string, string[]>;
10
-
11
- export class Notes {
12
- /** 会话 → 还没交给模型的说明 */
13
- readonly #pending = new Map<string, string[]>();
14
-
15
- add(sessionId: string, text: string): void {
16
- this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), text]);
17
- }
18
-
19
- /** 会话里的 context.project hook;key 是 derived 里的键 */
20
- hook(sessionId: string, plugin: string, key: string): LocalHookSpec {
21
- return {
22
- kind: "transform",
23
- point: "context.project",
24
- order: -40,
25
- fn: async (messages, ctx) => {
26
- let state = (ctx.derived[key] as NotesState | undefined) ?? {};
27
- const fresh = this.#pending.get(sessionId);
28
- if (fresh?.length && messages.length > 0) {
29
- const at = String(messages.length - 1);
30
- state = { ...state, [at]: [...(state[at] ?? []), ...fresh] };
31
- await ctx.record(key, state);
32
- this.#pending.delete(sessionId);
33
- }
34
- return messages.map((m, i): Message => {
35
- const notes = state[i];
36
- if (!notes?.length || m.role !== "user") return m;
37
- const blocks: Block[] = notes.map((text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id: plugin }, trust: "trusted" }));
38
- return { ...m, content: [...m.content, ...blocks] };
39
- });
40
- },
41
- };
42
- }
43
- }
@@ -1,36 +0,0 @@
1
- // 给模型的说明([Note from jade]):命令里做的事(例如用户撤销了模型改的文件)要让模型知道,
2
- // 但命令拿不到会话日志,也不该替用户发一条消息。所以先记下来,下一次请求模型时在 context.project 里
3
- // 附在最后一条消息后面,并用 ctx.record 写进日志(derived):之后的投影、恢复的会话里它都在同一个位置。
4
- // 插件之间不共享代码,jade/memory 里也有一份。
5
- export class Notes {
6
- /** 会话 → 还没交给模型的说明 */
7
- #pending = new Map();
8
- add(sessionId, text) {
9
- this.#pending.set(sessionId, [...(this.#pending.get(sessionId) ?? []), text]);
10
- }
11
- /** 会话里的 context.project hook;key 是 derived 里的键 */
12
- hook(sessionId, plugin, key) {
13
- return {
14
- kind: "transform",
15
- point: "context.project",
16
- order: -40,
17
- fn: async (messages, ctx) => {
18
- let state = ctx.derived[key] ?? {};
19
- const fresh = this.#pending.get(sessionId);
20
- if (fresh?.length && messages.length > 0) {
21
- const at = String(messages.length - 1);
22
- state = { ...state, [at]: [...(state[at] ?? []), ...fresh] };
23
- await ctx.record(key, state);
24
- this.#pending.delete(sessionId);
25
- }
26
- return messages.map((m, i) => {
27
- const notes = state[i];
28
- if (!notes?.length || m.role !== "user")
29
- return m;
30
- const blocks = notes.map((text) => ({ kind: "text", text: `[Note from jade] ${text}`, origin: { by: "plugin", id: plugin }, trust: "trusted" }));
31
- return { ...m, content: [...m.content, ...blocks] };
32
- });
33
- },
34
- };
35
- }
36
- }