@uncarved/jade 0.2.1 → 0.3.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.
Files changed (227) hide show
  1. package/README.md +65 -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 +236 -85
  30. package/plugins/approval/plugin.json +1 -1
  31. package/plugins/approval/policy.ts +36 -17
  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 +19 -7
  49. package/plugins/fs/index.ts +6 -7
  50. package/plugins/fs/plugin.json +1 -1
  51. package/plugins/fs/renderers.ts +1 -1
  52. package/plugins/hooks/index.ts +152 -0
  53. package/plugins/hooks/plugin.json +14 -0
  54. package/plugins/keys/index.ts +2 -18
  55. package/plugins/mcp/index.ts +43 -15
  56. package/plugins/mcp/install.ts +139 -0
  57. package/plugins/mcp/mcp.ts +2 -1
  58. package/plugins/mcp/plugin.json +1 -1
  59. package/plugins/mcp/project.ts +37 -1
  60. package/plugins/memory/index.ts +4 -9
  61. package/plugins/memory/plugin.json +1 -1
  62. package/plugins/mentions/index.ts +2 -2
  63. package/plugins/models/configure.ts +258 -150
  64. package/plugins/models/context.ts +2 -2
  65. package/plugins/models/index.ts +39 -45
  66. package/plugins/models/plugin.json +1 -1
  67. package/plugins/models/provider.ts +34 -5
  68. package/plugins/models/saved.ts +8 -4
  69. package/plugins/modes/index.ts +2 -17
  70. package/plugins/plugins/index.ts +2 -2
  71. package/plugins/print/index.ts +36 -6
  72. package/plugins/print/plugin.json +1 -0
  73. package/plugins/prompts/index.ts +22 -14
  74. package/plugins/{memory → prompts}/notes.ts +21 -9
  75. package/plugins/prompts/plugin.json +8 -2
  76. package/plugins/prompts/prompts.ts +5 -4
  77. package/plugins/renderers/index.ts +2 -43
  78. package/plugins/repeats/index.ts +32 -2
  79. package/plugins/schedule/index.ts +2 -2
  80. package/plugins/scratch/index.ts +2 -3
  81. package/plugins/screenshot/browser.ts +221 -0
  82. package/plugins/screenshot/index.ts +29 -15
  83. package/plugins/screenshot/plugin.json +1 -1
  84. package/plugins/screenshot/tool.ts +27 -51
  85. package/plugins/sessions/index.ts +2 -17
  86. package/plugins/shell/index.ts +115 -7
  87. package/plugins/shell/plugin.json +3 -1
  88. package/plugins/shell/renderers.ts +1 -1
  89. package/plugins/shell/service.ts +2 -13
  90. package/plugins/shell/shell.ts +119 -47
  91. package/plugins/skills/index.ts +172 -37
  92. package/plugins/skills/install.ts +212 -0
  93. package/plugins/skills/plugin.json +3 -2
  94. package/plugins/skills/project.ts +38 -1
  95. package/plugins/skills/skills.ts +28 -9
  96. package/plugins/steer/index.ts +142 -0
  97. package/plugins/steer/plugin.json +8 -0
  98. package/plugins/suggest/index.ts +16 -11
  99. package/plugins/suggest/plugin.json +1 -1
  100. package/plugins/task/index.ts +18 -57
  101. package/plugins/task/plugin.json +9 -3
  102. package/plugins/task/subagent.ts +25 -7
  103. package/plugins/todo/index.ts +2 -2
  104. package/plugins/tools/index.ts +50 -0
  105. package/plugins/tools/plugin.json +7 -0
  106. package/plugins/tui/editor.ts +10 -2
  107. package/plugins/tui/frontend.ts +129 -23
  108. package/plugins/tui/index.ts +3 -8
  109. package/plugins/tui/markdown.ts +51 -6
  110. package/plugins/tui/mascot.ts +506 -0
  111. package/plugins/tui/plugin.json +2 -2
  112. package/plugins/tui/screen.ts +6 -0
  113. package/plugins/tui/transcript.ts +17 -2
  114. package/plugins/tui/tui.ts +344 -97
  115. package/plugins/undo/checkpoint.ts +2 -2
  116. package/plugins/undo/index.ts +84 -26
  117. package/plugins/undo/plugin.json +8 -2
  118. package/plugins/undo/snapshot.ts +129 -0
  119. package/plugins/update/index.ts +11 -15
  120. package/plugins/update/plugin.json +2 -1
  121. package/plugins/web/index.ts +8 -11
  122. package/plugins/web/plugin.json +1 -1
  123. package/plugins/web/renderers.ts +1 -1
  124. package/plugins-dist/approval/index.js +221 -51
  125. package/plugins-dist/approval/plugin.json +1 -1
  126. package/plugins-dist/approval/policy.js +30 -15
  127. package/plugins-dist/ask/index.js +1 -1
  128. package/plugins-dist/budget/index.js +1 -1
  129. package/plugins-dist/checks/checks.js +2 -1
  130. package/plugins-dist/checks/index.js +1 -1
  131. package/plugins-dist/commands/index.js +1 -1
  132. package/plugins-dist/compact/compact.js +6 -1
  133. package/plugins-dist/compact/index.js +12 -8
  134. package/plugins-dist/continue/index.js +1 -1
  135. package/plugins-dist/cost/index.js +135 -0
  136. package/plugins-dist/cost/plugin.json +16 -0
  137. package/plugins-dist/custom-commands/index.js +86 -0
  138. package/plugins-dist/custom-commands/plugin.json +12 -0
  139. package/plugins-dist/editor/index.js +89 -0
  140. package/plugins-dist/editor/plugin.json +21 -0
  141. package/plugins-dist/fs/fs.js +34 -6
  142. package/plugins-dist/fs/grep.js +17 -6
  143. package/plugins-dist/fs/index.js +6 -6
  144. package/plugins-dist/fs/plugin.json +1 -1
  145. package/plugins-dist/hooks/index.js +145 -0
  146. package/plugins-dist/hooks/plugin.json +19 -0
  147. package/plugins-dist/keys/index.js +1 -1
  148. package/plugins-dist/mcp/index.js +45 -13
  149. package/plugins-dist/mcp/install.js +160 -0
  150. package/plugins-dist/mcp/mcp.js +2 -1
  151. package/plugins-dist/mcp/plugin.json +2 -1
  152. package/plugins-dist/mcp/project.js +42 -1
  153. package/plugins-dist/memory/index.js +3 -5
  154. package/plugins-dist/memory/plugin.json +1 -1
  155. package/plugins-dist/mentions/index.js +1 -1
  156. package/plugins-dist/models/configure.js +252 -151
  157. package/plugins-dist/models/context.js +2 -2
  158. package/plugins-dist/models/index.js +41 -17
  159. package/plugins-dist/models/plugin.json +1 -1
  160. package/plugins-dist/models/provider.js +17 -3
  161. package/plugins-dist/models/saved.js +9 -4
  162. package/plugins-dist/modes/index.js +1 -1
  163. package/plugins-dist/plugins/index.js +1 -1
  164. package/plugins-dist/print/index.js +37 -3
  165. package/plugins-dist/print/plugin.json +3 -0
  166. package/plugins-dist/prompts/index.js +22 -3
  167. package/plugins-dist/{memory → prompts}/notes.js +10 -7
  168. package/plugins-dist/prompts/plugin.json +8 -2
  169. package/plugins-dist/prompts/prompts.js +5 -4
  170. package/plugins-dist/renderers/index.js +1 -1
  171. package/plugins-dist/repeats/index.js +28 -2
  172. package/plugins-dist/schedule/index.js +1 -1
  173. package/plugins-dist/scratch/index.js +1 -1
  174. package/plugins-dist/screenshot/browser.js +187 -0
  175. package/plugins-dist/screenshot/index.js +28 -13
  176. package/plugins-dist/screenshot/plugin.json +1 -1
  177. package/plugins-dist/screenshot/tool.js +27 -49
  178. package/plugins-dist/sessions/index.js +1 -1
  179. package/plugins-dist/shell/index.js +119 -4
  180. package/plugins-dist/shell/plugin.json +3 -1
  181. package/plugins-dist/shell/shell.js +106 -36
  182. package/plugins-dist/skills/index.js +184 -35
  183. package/plugins-dist/skills/install.js +178 -0
  184. package/plugins-dist/skills/plugin.json +6 -2
  185. package/plugins-dist/skills/project.js +43 -1
  186. package/plugins-dist/skills/skills.js +27 -10
  187. package/plugins-dist/steer/index.js +135 -0
  188. package/plugins-dist/steer/plugin.json +12 -0
  189. package/plugins-dist/suggest/index.js +15 -9
  190. package/plugins-dist/suggest/plugin.json +1 -1
  191. package/plugins-dist/task/index.js +17 -46
  192. package/plugins-dist/task/plugin.json +11 -5
  193. package/plugins-dist/task/subagent.js +21 -6
  194. package/plugins-dist/todo/index.js +1 -1
  195. package/plugins-dist/tools/index.js +49 -0
  196. package/plugins-dist/tools/plugin.json +9 -0
  197. package/plugins-dist/tui/editor.js +7 -2
  198. package/plugins-dist/tui/frontend.js +134 -15
  199. package/plugins-dist/tui/index.js +2 -1
  200. package/plugins-dist/tui/markdown.js +48 -6
  201. package/plugins-dist/tui/mascot.js +441 -0
  202. package/plugins-dist/tui/plugin.json +3 -2
  203. package/plugins-dist/tui/screen.js +2 -0
  204. package/plugins-dist/tui/transcript.js +16 -1
  205. package/plugins-dist/tui/tui.js +320 -88
  206. package/plugins-dist/tui/vendor.js +1388 -1088
  207. package/plugins-dist/undo/checkpoint.js +1 -1
  208. package/plugins-dist/undo/index.js +91 -24
  209. package/plugins-dist/undo/plugin.json +10 -3
  210. package/plugins-dist/undo/snapshot.js +111 -0
  211. package/plugins-dist/update/index.js +9 -10
  212. package/plugins-dist/update/plugin.json +4 -1
  213. package/plugins-dist/web/index.js +7 -7
  214. package/plugins-dist/web/plugin.json +1 -1
  215. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  216. package/sdk/python/jade_plugin.py +8 -0
  217. package/src/api.ts +7 -262
  218. package/src/fetch.ts +18 -16
  219. package/src/host.ts +49 -1
  220. package/src/index.ts +1 -1
  221. package/src/main.ts +23 -5
  222. package/src/remote.ts +2 -0
  223. package/src/session.ts +134 -15
  224. package/src/source.ts +2 -11
  225. package/src/update.ts +16 -10
  226. package/plugins/undo/notes.ts +0 -43
  227. package/plugins-dist/undo/notes.js +0 -36
package/src/api.ts CHANGED
@@ -1,269 +1,14 @@
1
- // jade 插件 API:插件能看到的全部类型。设计见 docs/design/jade.md。
2
- // 插件只从这里 import 类型和 definePlugin,运行时用到的一切都从 ctx 拿(3.5)。
1
+ // 核心这一侧的插件 API:类型和 definePlugin 在 @uncarved/jade-api(插件只 import 那个包,3.5),这里原样转出,
2
+ // 再加上只有核心用得到值的东西:API 版本、层级顺序、错误类。
3
3
 
4
- import type { LocalHookSpec, LogEvent, RemoteHookSpec, RuntimeNotice, State, Tool, UserInput } from "uncarved";
5
- export type { LocalHookSpec, LogEvent, RemoteHookSpec, RuntimeNotice, State, Tool, UserInput } from "uncarved";
4
+ import type { PluginLayer } from "@uncarved/jade-api";
5
+ export * from "@uncarved/jade-api";
6
6
 
7
- /** 插件 API 的版本,插件在 plugin.json 的 api 字段里用 ^ 范围声明 */
8
- export const API_VERSION = "1.0.0";
7
+ /** 插件 API 的版本,插件在 plugin.json 的 api 字段里用 ^ 范围声明。和 @uncarved/jade-api 的版本一致 */
8
+ export const API_VERSION = "1.2.0";
9
9
 
10
10
  /** 来源层级,后面的层级更高(3.3) */
11
- export const LAYERS = ["bundled", "user", "project"] as const;
12
- export type PluginLayer = (typeof LAYERS)[number];
13
-
14
- /** plugin.json:不执行插件代码就能读到的一切(原则 4) */
15
- export interface Manifest {
16
- id: string;
17
- version: string;
18
- /** 兼容的插件 API 版本,只接受 ^ 范围,如 "^1" */
19
- api: string;
20
- /** 进程内插件的入口模块,相对插件目录 */
21
- entry?: string;
22
- /** 进程外插件的启动方式(第 6 节),和 entry 二选一 */
23
- runtime?: { command: string; args?: string[] };
24
- /** 提供的服务,形如 "commands@1" */
25
- provides?: string[];
26
- /** 必需的服务:缺任何一项就不激活 */
27
- requires?: string[];
28
- /** 可选的服务:缺了照样激活,ctx.service 返回 undefined */
29
- optional?: string[];
30
- /** 代替的插件 id(3.3) */
31
- replaces?: string[];
32
- /** 这是一个前端(3.4):default 为默认前端,flag 为选中它的命令行开关 */
33
- frontend?: { name: string; default?: boolean; flag?: string };
34
- /** 配置项(第 8 节):键 → 默认值、说明;project 为 true 的键项目层的 .jade/config.json 也能设 */
35
- config?: Record<string, ConfigKey>;
36
- }
37
-
38
- export interface ConfigKey {
39
- default?: unknown;
40
- description?: string;
41
- /** 项目层可以设置这个键。项目层插件自己的键不用标 */
42
- project?: boolean;
43
- }
44
-
45
- /** 发现的一个插件 */
46
- export interface PluginSource {
47
- manifest: Manifest;
48
- /** 插件目录的绝对路径 */
49
- dir: string;
50
- layer: PluginLayer;
51
- }
52
-
53
- /** 清理函数,作用域关闭时逆序执行 */
54
- export type Disposer = () => void | Promise<void>;
55
-
56
- /** 一个插件的密钥(第 8 节):按插件 id 隔离,插件只能读写自己的那一份 */
57
- export interface Secrets {
58
- /** 读一个密钥;没有时 undefined */
59
- get(key: string): Promise<string | undefined>;
60
- /** 写一个密钥;value 为 undefined 时删除它 */
61
- set(key: string, value: string | undefined): Promise<void>;
62
- }
63
-
64
- /** 进程级和会话级 ctx 共有的部分 */
65
- export interface ScopeContext {
66
- /** 本插件的 id */
67
- readonly plugin: string;
68
- /** 作用域关闭时中止 */
69
- readonly signal: AbortSignal;
70
- /** 工作目录 */
71
- readonly cwd: string;
72
- /** $JADE_HOME */
73
- readonly home: string;
74
- /** 本插件的配置:清单里的默认值 < 用户 config.json < 项目 .jade/config.json,按键合并(第 8 节) */
75
- readonly config: Readonly<Record<string, unknown>>;
76
- /** 本插件的密钥:按插件 id 分隔存储,和作用域同生共死(第 8 节) */
77
- readonly secrets: Secrets;
78
- /** 取一个声明在 requires 里的服务,没有声明或没有提供者时抛错 */
79
- require<T extends object>(service: string): T;
80
- /** 取一个声明在 requires 或 optional 里的服务,没有提供者时返回 undefined */
81
- service<T extends object>(service: string): T | undefined;
82
- /** 启动一项后台资源,返回的清理函数在作用域关闭时执行 */
83
- effect(start: () => Disposer | void | Promise<Disposer | void>): Promise<void>;
84
- }
85
-
86
- /** 服务的调用方,交给按调用方创建的服务实例(ProcessContext.provide) */
87
- export interface Caller {
88
- /** 调用方插件的 id;核心自己调用时为 "jade" */
89
- readonly plugin: string;
90
- /** 调用方插件的层级;核心自己调用时为 "core"。项目层插件给的东西(例如提示词)应当当作不可信 */
91
- readonly layer: PluginLayer | "core";
92
- /** 调用方的作用域关闭时中止 */
93
- readonly signal: AbortSignal;
94
- /** 调用方的作用域关闭时执行,以提供者的身份执行。注册表用它撤销调用方注册的东西 */
95
- onDispose(d: Disposer): void;
96
- }
97
-
98
- export interface ProcessContext extends ScopeContext {
99
- /**
100
- * 提供一个声明在 provides 里的服务。
101
- * 传对象:所有调用方共用;传函数:每个调用方作用域调用一次,得到这个调用方专用的实例。
102
- * 注册表应当用函数形式,在 caller.onDispose 里撤销调用方注册的东西,这样调用方关闭、
103
- * 激活失败或被隔离时,它的注册自动撤销(4.1)
104
- */
105
- provide(service: string, impl: object | ((caller: Caller) => object)): void;
106
- }
107
-
108
- /** 插件在会话里能做的事(4.3) */
109
- export interface SessionControl {
110
- readonly id: string;
111
- /** 本轮完全结束(turn.ended 已落盘、没有在途工具)后执行一次;会话空闲时尽快执行 */
112
- afterTurn(fn: () => void | Promise<void>): void;
113
- /**
114
- * 发起一轮。正在跑时排到当前这一轮之后,这一轮跑完时 resolve。
115
- * 不要在 hook 或工具里 await 它:它要等当前这一轮结束,而当前这一轮在等你。项目层插件不能用(第 7 节)
116
- */
117
- send(input: UserInput): Promise<void>;
118
- /** 中止正在跑的这一轮(例如用户在别处喊停);空闲时什么都不做。项目层插件不能用 */
119
- abort(reason?: string): void;
120
- }
121
-
122
- export interface SessionContext extends ScopeContext {
123
- readonly session: SessionControl;
124
- /**
125
- * 状态栏上这个插件的一段(一行);undefined 去掉。前端没有状态栏时是 undefined。
126
- * 状态栏同时显示几个插件的段落时按调用先后排列(4.3)
127
- */
128
- readonly status?: (text: string | undefined) => void;
129
- /** 输入框里的草稿:读、追加、整段替换、灰色提示;只有终端界面有 */
130
- readonly input?: Draft;
131
- /** 交给内核的部分,只能在 session() 执行期间注册:会话的内核插件清单在创建时确定 */
132
- readonly kernel: {
133
- tool(tool: Tool): void;
134
- /** 进程内的 hook,或进程外插件的 RemoteHookSpec(字段裁剪、超时由内核保证) */
135
- hook(hook: LocalHookSpec | RemoteHookSpec): void;
136
- };
137
- }
138
-
139
- /** 输入框(4.3):只在前端有输入框时给插件 */
140
- export interface Draft {
141
- /** 现在输入框里的文字 */
142
- text(): string;
143
- /** 在光标处插入一段文字 */
144
- insert(text: string): void;
145
- /** 换掉整段文字,光标移到末尾 */
146
- set(text: string): void;
147
- /** 输入框空着时显示的灰色提示,Tab 或 → 接受;undefined 去掉 */
148
- hint(text: string | undefined): void;
149
- }
150
-
151
- // ---------- 核心消费的服务契约(3.4) ----------
152
-
153
- /** 前端拿到的会话 */
154
- export interface Session {
155
- readonly id: string;
156
- readonly state: State;
157
- /** 发一条输入;正在跑时排队。这一轮跑完时 resolve(之后的 afterTurn 不等) */
158
- send(input: UserInput): Promise<void>;
159
- /** 排队的输入和 afterTurn 全部处理完时 resolve */
160
- idle(): Promise<void>;
161
- /** 继续日志里没做完的工作(崩溃、重启后) */
162
- recover(): Promise<void>;
163
- abort(reason?: unknown): void;
164
- subscribe(fn: (n: RuntimeNotice) => void): () => void;
165
- /** 日志里的全部事件(恢复会话时前端用它重新显示对话) */
166
- history(): Promise<LogEvent[]>;
167
- }
168
-
169
- export interface OpenOptions {
170
- /** 恢复这个会话;不写就新建 */
171
- resume?: string;
172
- }
173
-
174
- /** 会话管理器:新建、恢复、切换(4.2) */
175
- export interface SessionManager {
176
- readonly current: Session | undefined;
177
- /** 新建或恢复一个会话,成功后它成为当前会话,旧会话离开(仍可恢复)。失败时旧会话不受影响 */
178
- open(opts?: OpenOptions): Promise<Session>;
179
- /** 当前会话离开(仍可恢复) */
180
- close(): Promise<void>;
181
- }
182
-
183
- /** frontend@1:核心启动后把控制权交给它,返回进程的退出码 */
184
- export interface Frontend {
185
- run(opts: { argv: readonly string[]; sessions: SessionManager; signal: AbortSignal }): Promise<number | void>;
186
- }
187
-
188
- /**
189
- * jade@1:核心自己的诊断信息,由核心提供(不是插件,见 3.4)。只读;`/plugins` 用它,
190
- * 和救援 CLI `jade plugins list` 看的是同一份状态
191
- */
192
- export interface Jade {
193
- /** 这次启动里每个插件的状态和原因,包括读不出清单的目录 */
194
- plugins(): PluginStatus[];
195
- /** 服务名 → 提供者 id(含核心自己的 jade@1) */
196
- services(): { service: string; provider: string }[];
197
- }
198
-
199
- /** 一个插件在这次启动里的状态(和 PluginState 一致,这里不 import 宿主) */
200
- export interface PluginStatus {
201
- id?: string;
202
- version?: string;
203
- layer: PluginLayer;
204
- dir: string;
205
- state: "active" | "disabled" | "duplicate" | "incompatible" | "replaced" | "conflict" | "skipped" | "broken" | "failed" | "untrusted" | "quarantined";
206
- reason?: string;
207
- }
208
-
209
- /** ui@1:前端提供的和用户打交道的方式。每个前端都提供它,形状由 jade 定义 */
210
- export interface UI {
211
- /** 能不能向用户提问。不能时 select 总是返回 undefined */
212
- readonly interactive: boolean;
213
- /** 告诉用户一件事 */
214
- notify(message: string, level?: "info" | "warning" | "error"): void;
215
- /** 请用户从选项里选一个,返回选中项的 value;不能提问、用户取消或 signal 中止时返回 undefined */
216
- select<T extends string>(request: SelectRequest<T>, signal?: AbortSignal): Promise<T | undefined>;
217
- /** 请用户输入一行;不能提问、用户取消或 signal 中止时返回 undefined */
218
- input(request: InputRequest, signal?: AbortSignal): Promise<string | undefined>;
219
- /**
220
- * 某个插件在一个会话里的界面(4.3):状态栏上它的一段、输入框。
221
- * 核心为每个加入会话的插件调用一次;不能显示时返回 undefined。插件的作用域关闭时核心调用 dispose
222
- */
223
- session?(request: SessionRequest): SessionUI | undefined;
224
- }
225
-
226
- /** UI.session 的参数:会话 id 和调用它的插件 */
227
- export interface SessionRequest {
228
- session: string;
229
- plugin: string;
230
- }
231
-
232
- /** 一个插件在一个会话里的界面;每个插件一份,所以状态栏的段落不用 key */
233
- export interface SessionUI {
234
- /** 状态栏上这个插件的一段(一行);undefined 去掉 */
235
- status(text: string | undefined): void;
236
- /** 输入框;没有输入框的前端(例如 -p)没有 */
237
- readonly draft?: Draft;
238
- /** 去掉这个插件在这个会话里留下的一切(状态栏的段落、输入框的提示) */
239
- dispose(): void;
240
- }
241
-
242
- export interface InputRequest {
243
- title: string;
244
- initial?: string;
245
- placeholder?: string;
246
- /** 输入的内容不显示(密钥) */
247
- mask?: boolean;
248
- }
249
-
250
- export interface SelectRequest<T extends string> {
251
- title: string;
252
- /** 选项上方的详细内容,例如要执行的命令、要改的文件 */
253
- detail?: string;
254
- options: { label: string; value: T; /** 选项后面的灰色说明 */ hint?: string }[];
255
- }
256
-
257
- export interface PluginModule {
258
- /** 进程级:提供服务、向其他插件的注册服务注册东西。只做注册,不在这里直接启动后台资源 */
259
- process?(ctx: ProcessContext): void | Promise<void>;
260
- /** 会话级:每个会话调用一次,注册工具和内核 hook。会话关闭(换会话、退出)时作用域关闭 */
261
- session?(ctx: SessionContext): void | Promise<void>;
262
- }
263
-
264
- export function definePlugin<T extends PluginModule>(module: T): T {
265
- return module;
266
- }
11
+ export const LAYERS: readonly PluginLayer[] = ["bundled", "user", "project"];
267
12
 
268
13
  // ---------- 错误 ----------
269
14
 
package/src/fetch.ts CHANGED
@@ -31,22 +31,7 @@ export async function fetchPlugin(
31
31
  subdir?: string,
32
32
  log?: (text: string) => void,
33
33
  ): Promise<Fetched> {
34
- await mkdir(staging, { recursive: true });
35
- let root: string;
36
- let commit: string | undefined;
37
- if (source.kind === "git") {
38
- ({ root, commit } = await fetchGit(source, staging, log));
39
- } else if (source.kind === "npm") {
40
- root = await fetchNpm(source.location, staging, log);
41
- } else {
42
- root = path.join(staging, "local");
43
- log?.(`copying ${source.location}`);
44
- await cp(source.location, root, {
45
- recursive: true,
46
- filter: (src) => !/[\\/](node_modules|\.git)$/.test(src),
47
- });
48
- }
49
-
34
+ const { root, commit } = await fetchSource(source, staging, log);
50
35
  const dir = subdir ? inSubdir(root, subdir) : root;
51
36
  const manifest = await readPluginManifest(dir);
52
37
  const hash = await treeHash(dir);
@@ -54,6 +39,23 @@ export async function fetchPlugin(
54
39
  return { dir, commit, manifest, hash };
55
40
  }
56
41
 
42
+ /**
43
+ * 只下载来源,不检查 plugin.json、不装依赖:jade/skills 安装 skill 也用它。
44
+ * 返回 staging 里来源的根目录;git 来源同时返回提交
45
+ */
46
+ export async function fetchSource(source: PluginSourceSpec, staging: string, log?: (text: string) => void): Promise<{ root: string; commit?: string }> {
47
+ await mkdir(staging, { recursive: true });
48
+ if (source.kind === "git") return fetchGit(source, staging, log);
49
+ if (source.kind === "npm") return { root: await fetchNpm(source.location, staging, log) };
50
+ const root = path.join(staging, "local");
51
+ log?.(`copying ${source.location}`);
52
+ await cp(source.location, root, {
53
+ recursive: true,
54
+ filter: (src) => !/[\\/](node_modules|\.git)$/.test(src),
55
+ });
56
+ return { root };
57
+ }
58
+
57
59
  /** 读一个插件目录的 plugin.json;不是插件时给出能看懂的错误 */
58
60
  export async function readPluginManifest(dir: string): Promise<Manifest> {
59
61
  let raw: unknown;
package/src/host.ts CHANGED
@@ -9,6 +9,8 @@ import {
9
9
  ServiceUnavailable,
10
10
  StaleContext,
11
11
  type Caller,
12
+ type Cli,
13
+ type CliCommand,
12
14
  type Disposer,
13
15
  type Jade,
14
16
  type PluginLayer,
@@ -16,15 +18,20 @@ import {
16
18
  type PluginSource,
17
19
  type ProcessContext,
18
20
  type Secrets,
21
+ type Sources,
19
22
  } from "./api.ts";
20
23
  import { pluginConfig, readConfigFile, type ConfigFile } from "./config.ts";
24
+ import { fetchSource } from "./fetch.ts";
25
+ import { treeHash } from "./hash.ts";
21
26
  import { discover, type BrokenSource, type LayerRoot } from "./manifest.ts";
22
27
  import { ambient, invoke, Scope, serviceHandle, type Owner } from "./membrane.ts";
23
28
  import { startRemote } from "./remote.ts";
24
29
  import { resolve, type Plan, type PlanState } from "./resolve.ts";
25
30
  import { scopedSecrets, SecretsStore } from "./secrets.ts";
26
31
  import { Sessions } from "./session.ts";
32
+ import { parseSource } from "./source.ts";
27
33
  import { countFault, readFaults, readPluginsConfig, writeFaults, type Faults } from "./state.ts";
34
+ import { updater } from "./update.ts";
28
35
 
29
36
  /** untrusted:项目层插件没有得到用户确认,没有执行它的任何代码 */
30
37
  export type PluginState = PlanState | "broken" | "failed" | "untrusted" | "quarantined";
@@ -59,6 +66,17 @@ const CORE_ID = "jade";
59
66
 
60
67
  /** 核心自己提供的诊断服务(3.4),名字要和 api.ts 的 Jade 一致 */
61
68
  export const JADE_SERVICE = "jade@1";
69
+ /** 核心自己提供的 jade <子命令> 注册表(3.4),名字要和 api.ts 的 Cli 一致 */
70
+ export const CLI_SERVICE = "cli@1";
71
+ /** 核心自己的下载器(3.4),名字要和 api.ts 的 Sources 一致 */
72
+ export const SOURCES_SERVICE = "sources@1";
73
+ /** jade 本身的版本和升级(3.4),名字要和 api.ts 的 Updater 一致 */
74
+ export const UPDATE_SERVICE = "update@1";
75
+ const CORE_SERVICES = [JADE_SERVICE, CLI_SERVICE, SOURCES_SERVICE, UPDATE_SERVICE];
76
+
77
+ /** main.ts 在启动宿主之前自己处理的第一个参数:插件注册了也轮不到,所以不让注册 */
78
+ const CORE_WORDS: ReadonlySet<string> = new Set(["plugins", "update", "upgrade", "help", "list", "ls", "add", "install", "remove", "rm", "uninstall", "enable", "disable"]);
79
+ const CLI_NAME = /^[a-z0-9][a-z0-9-]*$/;
62
80
 
63
81
  interface Active {
64
82
  source: PluginSource;
@@ -75,7 +93,7 @@ interface Active {
75
93
  export async function planPlugins(opts: Pick<HostOptions, "roots" | "home" | "flags" | "safe">): Promise<{ plan: Plan; broken: BrokenSource[]; faults: Faults }> {
76
94
  const [{ sources, broken }, config, faults] = await Promise.all([discover(opts.roots), readPluginsConfig(opts.home), readFaults(opts.home)]);
77
95
  const disabled = new Set([...config.disabled, ...faults.autoDisabled]);
78
- const plan = resolve(sources, { apiVersion: API_VERSION, disabled, safe: opts.safe, flags: opts.flags, coreServices: [JADE_SERVICE] });
96
+ const plan = resolve(sources, { apiVersion: API_VERSION, disabled, safe: opts.safe, flags: opts.flags, coreServices: CORE_SERVICES });
79
97
  // 区分"用户停用"和"连续崩溃后自动停用"
80
98
  for (const e of plan.entries) {
81
99
  if (e.state === "disabled" && e.reason !== "safe mode" && !config.disabled.includes(e.source.manifest.id)) {
@@ -102,6 +120,7 @@ export class PluginHost {
102
120
  readonly #secrets: SecretsStore;
103
121
  #config: { user: ConfigFile; project: ConfigFile } = { user: {}, project: {} };
104
122
  readonly #pluginConfigs = new Map<string, Readonly<Record<string, unknown>>>();
123
+ readonly #cli = new Map<string, { command: CliCommand; plugin: string }>();
105
124
  #sessions: Sessions | undefined;
106
125
  #writing: Promise<void> = Promise.resolve();
107
126
 
@@ -120,6 +139,35 @@ export class PluginHost {
120
139
  } satisfies Jade,
121
140
  provider: this.#core,
122
141
  });
142
+ // 核心自己装插件、升级自己用的代码,插件经服务用同一份,不 import 核心的模块(3.5)
143
+ this.#services.set(SOURCES_SERVICE, { impl: { parse: parseSource, fetch: fetchSource, hash: treeHash } satisfies Sources, provider: this.#core });
144
+ this.#services.set(UPDATE_SERVICE, { impl: updater(opts.home), provider: this.#core });
145
+ // jade <子命令> 的注册表:每个调用方一个实例,调用方关闭(停用、被隔离)时撤销它注册的
146
+ this.#services.set(CLI_SERVICE, {
147
+ impl: (caller: Caller): Cli => ({
148
+ register: (command) => {
149
+ const name = command?.name;
150
+ if (typeof name !== "string" || !CLI_NAME.test(name)) throw new Error(`invalid jade subcommand name ${JSON.stringify(name)}`);
151
+ if (CORE_WORDS.has(name)) throw new Error(`"jade ${name}" belongs to jade itself`);
152
+ if (typeof command.run !== "function") throw new Error(`jade ${name} has no run function`);
153
+ const taken = this.#cli.get(name);
154
+ if (taken) throw new Error(`"jade ${name}" is already registered by ${taken.plugin}`);
155
+ const entry = { command, plugin: caller.plugin };
156
+ this.#cli.set(name, entry);
157
+ const revoke = () => {
158
+ if (this.#cli.get(name) === entry) this.#cli.delete(name);
159
+ };
160
+ caller.onDispose(revoke);
161
+ return revoke;
162
+ },
163
+ }),
164
+ provider: this.#core,
165
+ });
166
+ }
167
+
168
+ /** 插件注册的 jade <子命令>;main.ts 在交给前端之前查它 */
169
+ cliCommand(name: string): { command: CliCommand; plugin: string } | undefined {
170
+ return this.#cli.get(name);
123
171
  }
124
172
 
125
173
  /** 解析并按顺序激活全部插件。单个插件失败不影响其他插件,不抛出 */
package/src/index.ts CHANGED
@@ -18,6 +18,6 @@ export {
18
18
  } from "./install.ts";
19
19
  export { parseSource, type PluginSourceSpec, type SourceKind } from "./source.ts";
20
20
  export { treeHash } from "./hash.ts";
21
- export { CORE_PLUGIN_ID, NO_APPROVAL_REASON, Sessions } from "./session.ts";
21
+ export { CORE_PLUGIN_ID, NO_APPROVAL_REASON, NO_APPROVER_ERROR, Sessions } from "./session.ts";
22
22
  export { pluginConfig, readConfigFile, type ConfigFile } from "./config.ts";
23
23
  export { FAULT_LIMIT } from "./state.ts";
package/src/main.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // jade 命令行入口:解析核心自己的参数,启动插件宿主,把控制权交给前端插件(3.4)。
2
- // 核心自己只处理 jade plugins …、jade update、--safe、--version(以及把 jade remove x 这类漏了 plugins 的写法挡下来);其余参数原样交给前端。
2
+ // 核心自己只处理 jade plugins …、jade update、--safe、--version(以及把 jade remove x 这类漏了 plugins 的写法挡下来);
3
+ // 第一个参数是插件注册的子命令(cli@1)时交给那个插件;其余参数原样交给前端。
3
4
 
4
5
  import { existsSync, readFileSync } from "node:fs";
5
6
  import os from "node:os";
@@ -7,7 +8,7 @@ import path from "node:path";
7
8
  import { createInterface } from "node:readline/promises";
8
9
  import { fileURLToPath } from "node:url";
9
10
  import type { TrustOptions } from "uncarved";
10
- import type { Frontend, UI } from "./api.ts";
11
+ import { PluginError, type Frontend, type UI } from "./api.ts";
11
12
  import { PluginHost, type HostOptions } from "./host.ts";
12
13
  import type { LayerRoot } from "./manifest.ts";
13
14
  import { pluginsCommand } from "./rescue.ts";
@@ -20,6 +21,7 @@ const DOUBLE_SIGINT_MS = 2000;
20
21
  const USAGE = `usage: jade [--safe] [frontend options…]
21
22
  jade plugins [list | add <source> | update [id…] | remove <id> | enable <id> | disable <id>] [options]
22
23
  jade update [--check] upgrade jade itself to the latest version on npm
24
+ jade <subcommand> … a subcommand added by a plugin; "jade <subcommand> --help" shows its options
23
25
 
24
26
  --safe load only the plugins that come with jade
25
27
  -v, --version print the version
@@ -97,9 +99,11 @@ export async function main(argv: readonly string[]): Promise<number> {
97
99
 
98
100
  // 前端起来之前,消息打到 stderr;之后交给 ui@1
99
101
  let host: PluginHost | undefined;
102
+ /** 跑的是插件的子命令:前端不会起来,消息一直打到 stderr */
103
+ let cliMode = false;
100
104
  const report = (message: string) => {
101
105
  try {
102
- const ui = host?.service<UI>("ui@1");
106
+ const ui = cliMode ? undefined : host?.service<UI>("ui@1");
103
107
  if (ui) return ui.notify(message, "warning");
104
108
  } catch {
105
109
  // ui@1 的提供者坏了:退回 stderr
@@ -141,8 +145,22 @@ export async function main(argv: readonly string[]): Promise<number> {
141
145
 
142
146
  let code = 1;
143
147
  try {
144
- const frontend = host.service<Frontend>("frontend@1");
145
- if (!frontend) {
148
+ // 插件注册的 jade <子命令>(cli@1):交给它,不进前端
149
+ const sub = rest[0] !== undefined && !rest[0].startsWith("-") ? host.cliCommand(rest[0]) : undefined;
150
+ const frontend = sub ? undefined : host.service<Frontend>("frontend@1");
151
+ if (sub) {
152
+ cliMode = true;
153
+ try {
154
+ const out = await sub.command.run(rest.slice(1), { log: (text) => console.error(text), signal: ac.signal });
155
+ if (out) console.log(out);
156
+ code = 0;
157
+ } catch (e) {
158
+ // 插件抛出的错误经服务层包成 PluginError(带 "plugin <id> (…)" 前缀);用法错误只给用户看插件自己的消息
159
+ const cause = e instanceof PluginError ? e.cause : e;
160
+ console.error(cause instanceof Error ? cause.message : String(cause));
161
+ code = 2;
162
+ }
163
+ } else if (!frontend) {
146
164
  console.error(noFrontend(host, safe));
147
165
  } else {
148
166
  code = (await frontend.run({ argv: rest, sessions: host.sessions, signal: ac.signal })) ?? 0;
package/src/remote.ts CHANGED
@@ -242,6 +242,8 @@ export async function startRemote(source: PluginSource, owner: Owner, opts: Remo
242
242
  if (p.method === "send") result = await session.send(a[0] as Parameters<typeof session.send>[0]);
243
243
  else if (p.method === "afterTurn") session.afterTurn(a[0] as () => void);
244
244
  else if (p.method === "abort") session.abort(a[0] === undefined || a[0] === null ? undefined : String(a[0]));
245
+ else if (p.method === "state") result = session.state();
246
+ else if (p.method === "subscribe") result = session.subscribe(a[0] as Parameters<typeof session.subscribe>[0]);
245
247
  else throw new Error(`$session has no method ${String(p.method)}`);
246
248
  } else if (typeof target === "string") {
247
249
  const service = ctx.service<Record<string, unknown>>(target);