@uncarved/jade 0.1.2 → 0.2.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.
- package/bin/jade.js +5 -4
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -28
- package/dist/index.js +11 -28
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +156 -1223
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +55 -0
- package/dist/update.js +258 -0
- package/dist/update.js.map +1 -0
- package/package.json +12 -9
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -84
- package/plugins/approval/review.ts +192 -0
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +68 -39
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +112 -38
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +3 -4
- package/{src/tools → plugins/fs}/fs.ts +64 -46
- package/{src/tools → plugins/fs}/grep.ts +4 -3
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/plugins/fs/renderers.ts +138 -0
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +79 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +198 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +23 -27
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +142 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +83 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +253 -0
- package/plugins/prompts/sys.ts +72 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +36 -21
- package/{src → plugins/shell}/sys.ts +19 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +167 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +257 -0
- package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +53 -1
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/{src/ui → plugins/tui}/markdown.ts +258 -219
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/plugins/tui/render.ts +67 -0
- package/plugins/tui/screen.ts +268 -0
- package/{src/ui → plugins/tui}/transcript.ts +179 -111
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +684 -418
- package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
- package/plugins/undo/index.ts +139 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/plugins/web/web.ts +713 -0
- package/plugins-dist/approval/index.js +271 -0
- package/plugins-dist/approval/plugin.json +24 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +15 -65
- package/plugins-dist/approval/review.js +176 -0
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +65 -34
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +95 -33
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +3 -5
- package/{dist/tools → plugins-dist/fs}/fs.js +62 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +4 -4
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/plugins-dist/fs/renderers.js +132 -0
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +69 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +167 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +22 -19
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +153 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +57 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +227 -0
- package/plugins-dist/prompts/sys.js +46 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +36 -23
- package/{dist → plugins-dist/shell}/sys.js +20 -44
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +151 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +208 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +53 -2
- package/plugins-dist/tui/frontend.js +586 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +57 -20
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/plugins-dist/tui/render.js +61 -0
- package/plugins-dist/tui/screen.js +201 -0
- package/{dist/ui → plugins-dist/tui}/transcript.js +179 -111
- package/{dist/ui → plugins-dist/tui}/tui.js +484 -101
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
- package/plugins-dist/undo/index.js +140 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/plugins-dist/web/web.js +660 -0
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -28
- package/src/install.ts +355 -0
- package/src/main.ts +152 -1263
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -0
- package/LICENSE +0 -21
- package/README.md +0 -198
- package/dist/commands.d.ts +0 -30
- package/dist/commands.js +0 -106
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -104
- package/dist/distro.js +0 -312
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -251
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -22
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -70
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -42
- package/dist/plugins/core.js +0 -190
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -182
- package/dist/plugins/loader.js +0 -253
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -96
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -38
- package/dist/plugins/prompts.js +0 -196
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -9
- package/dist/plugins/repeats.js +0 -90
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -16
- package/dist/plugins/review.js +0 -92
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/provider.d.ts +0 -111
- package/dist/provider.js.map +0 -1
- package/dist/sessions.d.ts +0 -22
- package/dist/sessions.js +0 -53
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -48
- package/dist/sys.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -35
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -35
- package/dist/tools/task.js +0 -89
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -23
- package/dist/tools/web.js +0 -426
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -378
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -33
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -17
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -15
- package/dist/ui/render.js +0 -94
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -98
- package/dist/ui/screen.js +0 -108
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -33
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -45
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -120
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -400
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -239
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -31
- package/src/plugins/core.ts +0 -217
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -386
- package/src/plugins/prompts.ts +0 -234
- package/src/plugins/repeats.ts +0 -96
- package/src/plugins/review.ts +0 -109
- package/src/sessions.ts +0 -61
- package/src/tools/task.ts +0 -109
- package/src/tools/web.ts +0 -451
- package/src/ui/configure.ts +0 -408
- package/src/ui/render.ts +0 -83
- package/src/ui/screen.ts +0 -169
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
"""jade 进程外插件的 Python SDK(设计见 docs/design/jade.md 第 6 节)。单文件,只用标准库。
|
|
2
|
+
|
|
3
|
+
jade 启动插件进程时把这个文件所在的目录加进 PYTHONPATH,插件直接 ``import jade_plugin``。
|
|
4
|
+
|
|
5
|
+
from jade_plugin import Plugin
|
|
6
|
+
|
|
7
|
+
plugin = Plugin()
|
|
8
|
+
|
|
9
|
+
@plugin.process
|
|
10
|
+
def process(ctx):
|
|
11
|
+
ctx.provide("greeter@1", Greeter()) # 公开方法都能被其他插件调用
|
|
12
|
+
ctx.service("commands@1").register({"name": "hello", "run": hello})
|
|
13
|
+
ctx.secrets.set("API_TOKEN", "...") # 只有这个插件自己的密钥(第 8 节)
|
|
14
|
+
|
|
15
|
+
@plugin.session
|
|
16
|
+
def session(ctx):
|
|
17
|
+
ctx.tool("shout", "Upper-cases text", {"type": "object"}, effect="read", execute=shout)
|
|
18
|
+
|
|
19
|
+
plugin.run()
|
|
20
|
+
|
|
21
|
+
stdout 只用来传协议消息,日志写到 stderr(jade 会转告用户)。每个请求在自己的线程里处理,
|
|
22
|
+
插件的处理函数可能同时被调用;处理函数里可以调用宿主(ctx.service(...).method(...)),调用会阻塞到宿主回复。
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import json
|
|
28
|
+
import sys
|
|
29
|
+
import threading
|
|
30
|
+
import traceback
|
|
31
|
+
from typing import Any, Callable
|
|
32
|
+
|
|
33
|
+
__all__ = ["Plugin", "Context", "Caller", "JadeError", "cancelled"]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class JadeError(Exception):
|
|
37
|
+
"""宿主返回的错误"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
_local = threading.local()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def cancelled() -> bool:
|
|
44
|
+
"""当前处理的请求是否已被宿主取消(例如用户中止了正在执行的工具)"""
|
|
45
|
+
ev = getattr(_local, "cancel", None)
|
|
46
|
+
return bool(ev and ev.is_set())
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Plugin:
|
|
50
|
+
def __init__(self, id: str | None = None):
|
|
51
|
+
self.id = id
|
|
52
|
+
self._process: Callable[[Context], Any] | None = None
|
|
53
|
+
self._session: Callable[[Context], Any] | None = None
|
|
54
|
+
|
|
55
|
+
def process(self, fn: Callable[["Context"], Any]):
|
|
56
|
+
"""进程级:提供服务、向其他插件的注册服务注册东西"""
|
|
57
|
+
self._process = fn
|
|
58
|
+
return fn
|
|
59
|
+
|
|
60
|
+
def session(self, fn: Callable[["Context"], Any]):
|
|
61
|
+
"""会话级:每个会话调用一次,注册工具和 hook"""
|
|
62
|
+
self._session = fn
|
|
63
|
+
return fn
|
|
64
|
+
|
|
65
|
+
def run(self) -> None:
|
|
66
|
+
_Connection(self).serve()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class Caller:
|
|
70
|
+
"""按调用方创建的服务实例拿到的调用方信息"""
|
|
71
|
+
|
|
72
|
+
def __init__(self, id: str, plugin: str, layer: str):
|
|
73
|
+
self.id = id
|
|
74
|
+
self.plugin = plugin
|
|
75
|
+
self.layer = layer
|
|
76
|
+
self._disposers: list[Callable[[], Any]] = []
|
|
77
|
+
|
|
78
|
+
def on_dispose(self, fn: Callable[[], Any]) -> None:
|
|
79
|
+
"""调用方关闭时执行:注册表在这里撤销调用方注册的东西"""
|
|
80
|
+
self._disposers.append(fn)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class _Secrets:
|
|
84
|
+
"""一个作用域里的 ctx.secrets:按插件 id 隔离,value 为 None 时删除"""
|
|
85
|
+
|
|
86
|
+
def __init__(self, conn: "_Connection", scope: str):
|
|
87
|
+
self._conn = conn
|
|
88
|
+
self._scope = scope
|
|
89
|
+
|
|
90
|
+
def get(self, key: str) -> str | None:
|
|
91
|
+
return self._conn.request("secrets/get", {"scope": self._scope, "key": key})
|
|
92
|
+
|
|
93
|
+
def set(self, key: str, value: str | None) -> None:
|
|
94
|
+
self._conn.request("secrets/set", {"scope": self._scope, "key": key, "value": value})
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class _Service:
|
|
98
|
+
"""宿主那边一个服务的代理:任何属性都是方法,调用时发给宿主"""
|
|
99
|
+
|
|
100
|
+
def __init__(self, conn: "_Connection", scope: str, name: str):
|
|
101
|
+
self._conn = conn
|
|
102
|
+
self._scope = scope
|
|
103
|
+
self._name = name
|
|
104
|
+
|
|
105
|
+
def __getattr__(self, method: str):
|
|
106
|
+
if method.startswith("__"):
|
|
107
|
+
raise AttributeError(method)
|
|
108
|
+
return lambda *args: self._conn.call(self._scope, self._name, method, list(args))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class _RemoteFunction:
|
|
112
|
+
"""宿主交给插件的函数"""
|
|
113
|
+
|
|
114
|
+
def __init__(self, conn: "_Connection", scope: str, fn: str):
|
|
115
|
+
self._conn = conn
|
|
116
|
+
self._scope = scope
|
|
117
|
+
self._fn = fn
|
|
118
|
+
|
|
119
|
+
def __call__(self, *args):
|
|
120
|
+
return self._conn.call(self._scope, {"$fn": self._fn}, None, list(args))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class Context:
|
|
124
|
+
"""一个作用域(进程级或某个会话)的 ctx"""
|
|
125
|
+
|
|
126
|
+
def __init__(self, conn: "_Connection", scope: str, params: dict):
|
|
127
|
+
self._conn = conn
|
|
128
|
+
self.scope = scope
|
|
129
|
+
self.kind: str = params["kind"]
|
|
130
|
+
self.cwd: str = params.get("cwd", "")
|
|
131
|
+
self.home: str = params.get("home", "")
|
|
132
|
+
self.config: dict = params.get("config") or {}
|
|
133
|
+
self.secrets = _Secrets(conn, scope)
|
|
134
|
+
self.session_id: str | None = (params.get("session") or {}).get("id")
|
|
135
|
+
self._on_close: list[Callable[[], Any]] = []
|
|
136
|
+
|
|
137
|
+
def service(self, name: str) -> Any:
|
|
138
|
+
"""取一个声明在 plugin.json 的 requires 或 optional 里的服务"""
|
|
139
|
+
return _Service(self._conn, self.scope, name)
|
|
140
|
+
|
|
141
|
+
require = service
|
|
142
|
+
|
|
143
|
+
def provide(self, name: str, impl: Any = None, *, factory: Callable[[Caller], Any] | None = None, methods: list[str] | None = None) -> None:
|
|
144
|
+
"""
|
|
145
|
+
提供一个声明在 provides 里的服务,只能在 process 里调用。
|
|
146
|
+
impl:所有调用方共用的对象,公开方法都可以被调用。
|
|
147
|
+
factory:每个调用方调用一次,返回这个调用方专用的实例;factory 不是类时用 methods 列出方法名
|
|
148
|
+
"""
|
|
149
|
+
if (impl is None) == (factory is None):
|
|
150
|
+
raise ValueError("provide needs exactly one of impl and factory")
|
|
151
|
+
source = impl if impl is not None else factory
|
|
152
|
+
if methods is None:
|
|
153
|
+
if factory is not None and not isinstance(factory, type):
|
|
154
|
+
raise ValueError("provide(factory=...) needs methods=[...] unless the factory is a class")
|
|
155
|
+
methods = [n for n in dir(source) if not n.startswith("_") and callable(getattr(source, n))]
|
|
156
|
+
if factory is not None:
|
|
157
|
+
self._conn.factories[name] = factory
|
|
158
|
+
else:
|
|
159
|
+
self._conn.services[name] = impl
|
|
160
|
+
self._conn.request("provide", {"scope": self.scope, "service": name, "methods": methods, "perCaller": factory is not None})
|
|
161
|
+
|
|
162
|
+
def tool(self, name: str, description: str, input_schema: dict, *, effect: str, execute: Callable[[Any, dict], Any], concurrency: str = "exclusive", idempotent: bool = False) -> None:
|
|
163
|
+
"""
|
|
164
|
+
注册一个工具,只能在 session 里调用。effect:"read"、"write" 或 "external"。
|
|
165
|
+
execute(input, ctx) 返回文字,或 {"content": 文字或 block 列表, "isError": bool}
|
|
166
|
+
"""
|
|
167
|
+
tool = {"name": name, "description": description, "inputSchema": input_schema, "effect": effect, "concurrency": concurrency, "idempotent": idempotent}
|
|
168
|
+
self._conn.request("kernel/tool", {"scope": self.scope, "tool": tool, "execute": self._conn.handle(execute, self.scope)})
|
|
169
|
+
|
|
170
|
+
def hook(self, kind: str, point: str, fn: Callable[[dict], Any], *, reads: list[str], writes: list[str] | None = None, timeout_ms: int = 30_000, order: int | None = None) -> None:
|
|
171
|
+
"""
|
|
172
|
+
注册一个内核 hook,只能在 session 里调用。fn 只收到 reads 列出的字段;
|
|
173
|
+
transform 返回要改的字段(只能是 writes 里的),gate 返回 {"decision": ..., "reason": ...}
|
|
174
|
+
"""
|
|
175
|
+
hook = {"kind": kind, "point": point, "reads": reads, "writes": writes or [], "timeoutMs": timeout_ms}
|
|
176
|
+
if order is not None:
|
|
177
|
+
hook["order"] = order
|
|
178
|
+
self._conn.request("kernel/hook", {"scope": self.scope, "hook": hook, "fn": self._conn.handle(fn, self.scope)})
|
|
179
|
+
|
|
180
|
+
def send(self, input: dict) -> None:
|
|
181
|
+
"""发起一轮(会话级)。不要在工具或 hook 里调用:它要等当前这一轮结束"""
|
|
182
|
+
self._conn.call(self.scope, "$session", "send", [input])
|
|
183
|
+
|
|
184
|
+
def abort(self, reason: str | None = None) -> None:
|
|
185
|
+
"""中止正在跑的这一轮(会话级);空闲时什么都不做"""
|
|
186
|
+
self._conn.call(self.scope, "$session", "abort", [reason])
|
|
187
|
+
|
|
188
|
+
def after_turn(self, fn: Callable[[], Any]) -> None:
|
|
189
|
+
"""本轮完全结束后执行一次(会话级)"""
|
|
190
|
+
self._conn.call(self.scope, "$session", "afterTurn", [fn])
|
|
191
|
+
|
|
192
|
+
def on_close(self, fn: Callable[[], Any]) -> None:
|
|
193
|
+
"""作用域关闭时执行,逆序"""
|
|
194
|
+
self._on_close.append(fn)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class _Connection:
|
|
198
|
+
def __init__(self, plugin: Plugin):
|
|
199
|
+
self.plugin = plugin
|
|
200
|
+
self.services: dict[str, Any] = {}
|
|
201
|
+
self.factories: dict[str, Callable[[Caller], Any]] = {}
|
|
202
|
+
self.callers: dict[str, tuple[Caller, Any]] = {}
|
|
203
|
+
self.scopes: dict[str, Context] = {}
|
|
204
|
+
self._fns: dict[str, Callable] = {}
|
|
205
|
+
self._buckets: dict[str, set[str]] = {}
|
|
206
|
+
self._pending: dict[int, list] = {}
|
|
207
|
+
self._incoming: dict[Any, threading.Event] = {}
|
|
208
|
+
self._lock = threading.Lock()
|
|
209
|
+
self._next = 0
|
|
210
|
+
self._out = sys.stdout.buffer
|
|
211
|
+
self._in = sys.stdin.buffer
|
|
212
|
+
# 插件代码误用 print 时不要弄坏协议:stdout 改指向 stderr
|
|
213
|
+
sys.stdout = sys.stderr
|
|
214
|
+
|
|
215
|
+
# ---------- 值 ----------
|
|
216
|
+
|
|
217
|
+
def handle(self, fn: Callable, scope: str) -> dict:
|
|
218
|
+
with self._lock:
|
|
219
|
+
self._next += 1
|
|
220
|
+
h = f"p{self._next}"
|
|
221
|
+
self._fns[h] = fn
|
|
222
|
+
self._buckets.setdefault(scope, set()).add(h)
|
|
223
|
+
return {"$fn": h}
|
|
224
|
+
|
|
225
|
+
def encode(self, v: Any, scope: str) -> Any:
|
|
226
|
+
if callable(v) and not isinstance(v, (type, _Service)):
|
|
227
|
+
return self.handle(v, scope)
|
|
228
|
+
if isinstance(v, (list, tuple)):
|
|
229
|
+
return [self.encode(x, scope) for x in v]
|
|
230
|
+
if isinstance(v, dict):
|
|
231
|
+
return {k: self.encode(x, scope) for k, x in v.items()}
|
|
232
|
+
return v
|
|
233
|
+
|
|
234
|
+
def decode(self, v: Any, scope: str) -> Any:
|
|
235
|
+
if isinstance(v, dict):
|
|
236
|
+
if len(v) == 1 and isinstance(v.get("$fn"), str):
|
|
237
|
+
return _RemoteFunction(self, scope, v["$fn"])
|
|
238
|
+
return {k: self.decode(x, scope) for k, x in v.items()}
|
|
239
|
+
if isinstance(v, list):
|
|
240
|
+
return [self.decode(x, scope) for x in v]
|
|
241
|
+
return v
|
|
242
|
+
|
|
243
|
+
# ---------- 传输 ----------
|
|
244
|
+
|
|
245
|
+
def _write(self, msg: dict) -> None:
|
|
246
|
+
data = json.dumps(msg).encode("utf-8") + b"\n"
|
|
247
|
+
with self._lock:
|
|
248
|
+
self._out.write(data)
|
|
249
|
+
self._out.flush()
|
|
250
|
+
|
|
251
|
+
def request(self, method: str, params: dict) -> Any:
|
|
252
|
+
with self._lock:
|
|
253
|
+
self._next += 1
|
|
254
|
+
id = self._next
|
|
255
|
+
slot = [threading.Event(), None, None]
|
|
256
|
+
self._pending[id] = slot
|
|
257
|
+
self._write({"jsonrpc": "2.0", "id": id, "method": method, "params": params})
|
|
258
|
+
slot[0].wait()
|
|
259
|
+
if slot[2] is not None:
|
|
260
|
+
raise JadeError(slot[2].get("message", "error"))
|
|
261
|
+
return slot[1]
|
|
262
|
+
|
|
263
|
+
def call(self, scope: str, target: Any, method: str | None, args: list) -> Any:
|
|
264
|
+
params = {"scope": scope, "target": target, "args": self.encode(args, scope)}
|
|
265
|
+
if method is not None:
|
|
266
|
+
params["method"] = method
|
|
267
|
+
return self.decode(self.request("call", params), scope)
|
|
268
|
+
|
|
269
|
+
def serve(self) -> None:
|
|
270
|
+
while True:
|
|
271
|
+
line = self._in.readline()
|
|
272
|
+
if not line:
|
|
273
|
+
break
|
|
274
|
+
line = line.strip()
|
|
275
|
+
if not line:
|
|
276
|
+
continue
|
|
277
|
+
try:
|
|
278
|
+
msg = json.loads(line)
|
|
279
|
+
except ValueError:
|
|
280
|
+
print(f"jade_plugin: ignored a malformed line: {line[:200]!r}", file=sys.stderr)
|
|
281
|
+
continue
|
|
282
|
+
method = msg.get("method")
|
|
283
|
+
if method is None:
|
|
284
|
+
slot = self._pending.pop(msg.get("id"), None)
|
|
285
|
+
if slot is not None:
|
|
286
|
+
slot[1] = msg.get("result")
|
|
287
|
+
slot[2] = msg.get("error")
|
|
288
|
+
slot[0].set()
|
|
289
|
+
elif method == "$/cancel":
|
|
290
|
+
ev = self._incoming.get((msg.get("params") or {}).get("id"))
|
|
291
|
+
if ev is not None:
|
|
292
|
+
ev.set()
|
|
293
|
+
else:
|
|
294
|
+
id = msg.get("id")
|
|
295
|
+
cancel = threading.Event()
|
|
296
|
+
if id is not None:
|
|
297
|
+
self._incoming[id] = cancel
|
|
298
|
+
threading.Thread(target=self._serve_one, args=(id, method, msg.get("params") or {}, cancel), daemon=True).start()
|
|
299
|
+
# 宿主关闭了管道:清理全部作用域后退出
|
|
300
|
+
for scope in list(self.scopes):
|
|
301
|
+
self._close(scope)
|
|
302
|
+
|
|
303
|
+
def _serve_one(self, id: Any, method: str, params: dict, cancel: threading.Event) -> None:
|
|
304
|
+
_local.cancel = cancel
|
|
305
|
+
try:
|
|
306
|
+
result = self._dispatch(method, params)
|
|
307
|
+
if id is not None:
|
|
308
|
+
self._write({"jsonrpc": "2.0", "id": id, "result": result})
|
|
309
|
+
except Exception as e:
|
|
310
|
+
if not isinstance(e, JadeError):
|
|
311
|
+
traceback.print_exc(file=sys.stderr)
|
|
312
|
+
if id is not None:
|
|
313
|
+
self._write({"jsonrpc": "2.0", "id": id, "error": {"code": -32000, "message": str(e) or type(e).__name__}})
|
|
314
|
+
finally:
|
|
315
|
+
self._incoming.pop(id, None)
|
|
316
|
+
|
|
317
|
+
# ---------- 宿主发来的请求 ----------
|
|
318
|
+
|
|
319
|
+
def _dispatch(self, method: str, p: dict) -> Any:
|
|
320
|
+
if method == "initialize":
|
|
321
|
+
return {**({"id": self.plugin.id} if self.plugin.id else {}), "session": self.plugin._session is not None}
|
|
322
|
+
if method == "scope/open":
|
|
323
|
+
ctx = Context(self, p["scope"], p)
|
|
324
|
+
self.scopes[ctx.scope] = ctx
|
|
325
|
+
fn = self.plugin._process if ctx.kind == "process" else self.plugin._session
|
|
326
|
+
if fn is not None:
|
|
327
|
+
fn(ctx)
|
|
328
|
+
return None
|
|
329
|
+
if method == "scope/close":
|
|
330
|
+
self._close(p["scope"])
|
|
331
|
+
return None
|
|
332
|
+
if method == "caller/open":
|
|
333
|
+
c = p["caller"]
|
|
334
|
+
caller = Caller(c["id"], c["plugin"], c["layer"])
|
|
335
|
+
self.callers[caller.id] = (caller, self.factories[p["service"]](caller))
|
|
336
|
+
return None
|
|
337
|
+
if method == "caller/close":
|
|
338
|
+
entry = self.callers.pop(p["caller"], None)
|
|
339
|
+
if entry:
|
|
340
|
+
for d in reversed(entry[0]._disposers):
|
|
341
|
+
_quietly(d)
|
|
342
|
+
return None
|
|
343
|
+
if method == "call":
|
|
344
|
+
scope = p.get("scope", "process")
|
|
345
|
+
args = self.decode(p.get("args") or [], scope)
|
|
346
|
+
target = p.get("target")
|
|
347
|
+
if isinstance(target, dict):
|
|
348
|
+
fn = self._fns.get(target.get("$fn"))
|
|
349
|
+
if fn is None:
|
|
350
|
+
raise JadeError(f"function {target.get('$fn')} is no longer valid")
|
|
351
|
+
result = fn(*args)
|
|
352
|
+
else:
|
|
353
|
+
if "caller" in p:
|
|
354
|
+
entry = self.callers.get(p["caller"])
|
|
355
|
+
if entry is None:
|
|
356
|
+
raise JadeError(f"caller {p['caller']} is closed")
|
|
357
|
+
impl = entry[1]
|
|
358
|
+
else:
|
|
359
|
+
impl = self.services.get(target)
|
|
360
|
+
if impl is None:
|
|
361
|
+
raise JadeError(f"service {target} is not provided by this plugin")
|
|
362
|
+
result = getattr(impl, p["method"])(*args)
|
|
363
|
+
return self.encode(result, scope)
|
|
364
|
+
raise JadeError(f"unknown method {method}")
|
|
365
|
+
|
|
366
|
+
def _close(self, scope: str) -> None:
|
|
367
|
+
ctx = self.scopes.pop(scope, None)
|
|
368
|
+
if ctx is not None:
|
|
369
|
+
for fn in reversed(ctx._on_close):
|
|
370
|
+
_quietly(fn)
|
|
371
|
+
with self._lock:
|
|
372
|
+
for h in self._buckets.pop(scope, set()):
|
|
373
|
+
self._fns.pop(h, None)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _quietly(fn: Callable[[], Any]) -> None:
|
|
377
|
+
try:
|
|
378
|
+
fn()
|
|
379
|
+
except Exception:
|
|
380
|
+
traceback.print_exc(file=sys.stderr)
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// jade 插件 API:插件能看到的全部类型。设计见 docs/design/jade.md。
|
|
2
|
+
// 插件只从这里 import 类型和 definePlugin,运行时用到的一切都从 ctx 拿(3.5)。
|
|
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";
|
|
6
|
+
|
|
7
|
+
/** 插件 API 的版本,插件在 plugin.json 的 api 字段里用 ^ 范围声明 */
|
|
8
|
+
export const API_VERSION = "1.0.0";
|
|
9
|
+
|
|
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
|
+
}
|
|
267
|
+
|
|
268
|
+
// ---------- 错误 ----------
|
|
269
|
+
|
|
270
|
+
/** 插件代码抛出的错误,标明归属 */
|
|
271
|
+
export class PluginError extends Error {
|
|
272
|
+
readonly plugin: string;
|
|
273
|
+
constructor(plugin: string, where: string, cause: unknown) {
|
|
274
|
+
super(`plugin ${plugin} (${where}): ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
275
|
+
this.name = "PluginError";
|
|
276
|
+
this.plugin = plugin;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** 作用域已关闭之后还在用它的 ctx 或从它拿到的东西(4.1) */
|
|
281
|
+
export class StaleContext extends Error {
|
|
282
|
+
readonly plugin: string;
|
|
283
|
+
constructor(plugin: string) {
|
|
284
|
+
super(`plugin ${plugin}: this context belongs to a closed scope; do not keep ctx or services across a session switch`);
|
|
285
|
+
this.name = "StaleContext";
|
|
286
|
+
this.plugin = plugin;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** 服务的提供者已被隔离或已关闭(3.3) */
|
|
291
|
+
export class ServiceUnavailable extends Error {
|
|
292
|
+
readonly service: string;
|
|
293
|
+
constructor(service: string, provider: string) {
|
|
294
|
+
super(`service ${service}: provider ${provider} is no longer active`);
|
|
295
|
+
this.name = "ServiceUnavailable";
|
|
296
|
+
this.service = service;
|
|
297
|
+
}
|
|
298
|
+
}
|