@uncarved/jade 0.3.0 → 0.4.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 (207) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +482 -27
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +49 -24
  14. package/plugins/approval/policy.ts +48 -25
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +55 -12
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +10 -9
  50. package/plugins/models/index.ts +31 -4
  51. package/plugins/models/provider.ts +12 -1
  52. package/plugins/modes/index.ts +13 -2
  53. package/plugins/print/index.ts +7 -4
  54. package/plugins/prompts/index.ts +0 -0
  55. package/plugins/renderers/index.ts +10 -5
  56. package/plugins/repeats/index.ts +39 -9
  57. package/plugins/schedule/index.ts +2 -0
  58. package/plugins/screenshot/browser.ts +231 -0
  59. package/plugins/screenshot/index.ts +3 -2
  60. package/plugins/screenshot/tool.ts +27 -51
  61. package/plugins/sessions/index.ts +48 -2
  62. package/plugins/shell/index.ts +75 -22
  63. package/plugins/shell/plugin.json +1 -2
  64. package/plugins/shell/renderers.ts +9 -7
  65. package/plugins/shell/shell.ts +93 -34
  66. package/plugins/skills/index.ts +28 -19
  67. package/plugins/skills/project.ts +12 -7
  68. package/plugins/steer/index.ts +5 -4
  69. package/plugins/task/index.ts +81 -81
  70. package/plugins/task/plugin.json +5 -2
  71. package/plugins/task/style.ts +13 -14
  72. package/plugins/task/subagent.ts +67 -184
  73. package/plugins/todo/index.ts +39 -9
  74. package/plugins/tools/index.ts +57 -0
  75. package/plugins/tools/plugin.json +7 -0
  76. package/plugins/tui/frontend.ts +161 -87
  77. package/plugins/tui/index.ts +2 -3
  78. package/plugins/tui/mascot.ts +8 -7
  79. package/plugins/tui/mentions.ts +11 -80
  80. package/plugins/tui/plugin.json +1 -1
  81. package/plugins/tui/screen.ts +55 -5
  82. package/plugins/tui/transcript.ts +17 -7
  83. package/plugins/tui/tui.ts +356 -104
  84. package/plugins/tui/vendor.ts +1 -1
  85. package/plugins/undo/checkpoint.ts +5 -3
  86. package/plugins/undo/index.ts +43 -14
  87. package/plugins/undo/style.ts +13 -14
  88. package/plugins/web/index.ts +7 -18
  89. package/plugins/web/plugin.json +1 -1
  90. package/plugins-dist/approval/index.js +363 -414
  91. package/plugins-dist/approval/policy.js +395 -497
  92. package/plugins-dist/approval/review.js +132 -158
  93. package/plugins-dist/approval/schema.js +50 -55
  94. package/plugins-dist/ask/index.js +115 -129
  95. package/plugins-dist/budget/index.js +27 -31
  96. package/plugins-dist/checks/checks.js +293 -329
  97. package/plugins-dist/checks/index.js +119 -139
  98. package/plugins-dist/checks/plugin.json +2 -1
  99. package/plugins-dist/checks/project.js +57 -65
  100. package/plugins-dist/commands/index.js +47 -29
  101. package/plugins-dist/compact/compact.js +233 -249
  102. package/plugins-dist/compact/index.js +98 -95
  103. package/plugins-dist/compact/style.js +9 -14
  104. package/plugins-dist/completions/index.js +30 -0
  105. package/plugins-dist/completions/plugin.json +9 -0
  106. package/plugins-dist/continue/index.js +40 -52
  107. package/plugins-dist/copy/index.js +40 -0
  108. package/plugins-dist/copy/plugin.json +9 -0
  109. package/plugins-dist/cost/index.js +98 -115
  110. package/plugins-dist/custom-commands/index.js +55 -72
  111. package/plugins-dist/editor/index.js +64 -72
  112. package/plugins-dist/fs/diff.js +111 -124
  113. package/plugins-dist/fs/fs.js +426 -469
  114. package/plugins-dist/fs/grep.js +262 -244
  115. package/plugins-dist/fs/index.js +20 -34
  116. package/plugins-dist/fs/plugin.json +1 -2
  117. package/plugins-dist/fs/renderers.js +100 -112
  118. package/plugins-dist/fs/style.js +11 -15
  119. package/plugins-dist/fs/sys.js +31 -43
  120. package/plugins-dist/hooks/index.js +107 -127
  121. package/plugins-dist/keys/index.js +27 -31
  122. package/plugins-dist/mcp/index.js +271 -199
  123. package/plugins-dist/mcp/install.js +123 -137
  124. package/plugins-dist/mcp/mcp.js +638 -577
  125. package/plugins-dist/mcp/plugin.json +3 -1
  126. package/plugins-dist/mcp/project.js +98 -115
  127. package/plugins-dist/memory/index.js +83 -88
  128. package/plugins-dist/memory/memory.js +111 -128
  129. package/plugins-dist/memory/style.js +9 -14
  130. package/plugins-dist/mentions/attach.js +91 -99
  131. package/plugins-dist/mentions/candidates.js +49 -0
  132. package/plugins-dist/mentions/index.js +71 -81
  133. package/plugins-dist/mentions/plugin.json +2 -1
  134. package/plugins-dist/mentions/style.js +9 -14
  135. package/plugins-dist/models/config.js +29 -43
  136. package/plugins-dist/models/configure.js +392 -473
  137. package/plugins-dist/models/context.js +14 -22
  138. package/plugins-dist/models/index.js +182 -188
  139. package/plugins-dist/models/provider.js +243 -295
  140. package/plugins-dist/models/saved.js +59 -71
  141. package/plugins-dist/modes/index.js +138 -165
  142. package/plugins-dist/plugins/index.js +34 -34
  143. package/plugins-dist/print/index.js +192 -175
  144. package/plugins-dist/prompts/index.js +81 -89
  145. package/plugins-dist/prompts/notes.js +31 -37
  146. package/plugins-dist/prompts/prompts.js +155 -193
  147. package/plugins-dist/prompts/results.js +40 -53
  148. package/plugins-dist/prompts/sys.js +39 -50
  149. package/plugins-dist/renderers/index.js +32 -32
  150. package/plugins-dist/repeats/index.js +134 -151
  151. package/plugins-dist/schedule/cron.js +49 -56
  152. package/plugins-dist/schedule/index.js +229 -247
  153. package/plugins-dist/schedule/store.js +93 -109
  154. package/plugins-dist/scratch/index.js +12 -18
  155. package/plugins-dist/screenshot/browser.js +169 -0
  156. package/plugins-dist/screenshot/index.js +37 -45
  157. package/plugins-dist/screenshot/sys.js +53 -65
  158. package/plugins-dist/screenshot/tool.js +101 -151
  159. package/plugins-dist/sessions/index.js +149 -117
  160. package/plugins-dist/shell/index.js +117 -74
  161. package/plugins-dist/shell/plugin.json +1 -2
  162. package/plugins-dist/shell/renderers.js +28 -33
  163. package/plugins-dist/shell/service.js +17 -23
  164. package/plugins-dist/shell/shell.js +317 -320
  165. package/plugins-dist/shell/sys.js +64 -79
  166. package/plugins-dist/skills/index.js +193 -211
  167. package/plugins-dist/skills/install.js +122 -149
  168. package/plugins-dist/skills/project.js +65 -73
  169. package/plugins-dist/skills/skills.js +135 -171
  170. package/plugins-dist/steer/index.js +108 -125
  171. package/plugins-dist/suggest/index.js +235 -259
  172. package/plugins-dist/task/index.js +130 -148
  173. package/plugins-dist/task/plugin.json +5 -4
  174. package/plugins-dist/task/style.js +9 -14
  175. package/plugins-dist/task/subagent.js +156 -279
  176. package/plugins-dist/todo/index.js +79 -63
  177. package/plugins-dist/tools/index.js +43 -0
  178. package/plugins-dist/tools/plugin.json +9 -0
  179. package/plugins-dist/tui/editor.js +145 -160
  180. package/plugins-dist/tui/frontend.js +641 -655
  181. package/plugins-dist/tui/history.js +34 -37
  182. package/plugins-dist/tui/index.js +16 -18
  183. package/plugins-dist/tui/markdown.js +203 -247
  184. package/plugins-dist/tui/mascot.js +266 -353
  185. package/plugins-dist/tui/mentions.js +67 -144
  186. package/plugins-dist/tui/plugin.json +1 -2
  187. package/plugins-dist/tui/render.js +35 -45
  188. package/plugins-dist/tui/screen.js +197 -180
  189. package/plugins-dist/tui/transcript.js +436 -468
  190. package/plugins-dist/tui/tui.js +1554 -1645
  191. package/plugins-dist/tui/vendor.js +1388 -1088
  192. package/plugins-dist/undo/checkpoint.js +95 -118
  193. package/plugins-dist/undo/index.js +185 -192
  194. package/plugins-dist/undo/snapshot.js +83 -99
  195. package/plugins-dist/undo/style.js +9 -14
  196. package/plugins-dist/update/index.js +26 -28
  197. package/plugins-dist/web/env.js +55 -68
  198. package/plugins-dist/web/index.js +80 -110
  199. package/plugins-dist/web/plugin.json +1 -2
  200. package/plugins-dist/web/renderers.js +4 -5
  201. package/plugins-dist/web/web.js +514 -593
  202. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  203. package/sdk/python/jade_plugin.py +9 -2
  204. package/src/api.ts +1 -1
  205. package/src/host.ts +39 -4
  206. package/src/remote.ts +6 -1
  207. package/src/session.ts +489 -34
@@ -1,104 +1,107 @@
1
- // jade/compact:历史接近上下文窗口时压缩投影(见 compact.ts);/compact 让下一次请求前总结一次;
2
- // 请求因为超出上下文窗口失败时,压缩后用 { kind: "retry" } 重试一次(session.afterTurn,设计 4.3)。
3
1
  import { definePlugin } from "@uncarved/jade-api";
4
2
  import { compactor, compactThreshold, isContextOverflow, parseTokens } from "./compact.js";
5
3
  import { dim } from "./style.js";
6
- /** 认不出窗口时的阈值 */
7
- const FALLBACK_THRESHOLD = 120_000;
8
- /** Claude 默认的回复上限,阈值要给它留出位置 */
9
- const ANTHROPIC_REPLY_TOKENS = 64_000;
10
- /** 总结要调用模型,比 hook 默认的 30 秒长 */
11
- const SUMMARY_TIMEOUT_MS = 300_000;
12
- /** 自动压缩的阈值:配置的 thresholdTokens,否则按当前模型的上下文窗口算 */
4
+ const FALLBACK_THRESHOLD = 120000;
5
+ const ANTHROPIC_REPLY_TOKENS = 64000;
6
+ const SUMMARY_TIMEOUT_MS = 300000;
13
7
  function thresholdFor(config, models) {
14
- const configured = parseTokens(config.thresholdTokens);
15
- return () => {
16
- if (configured)
17
- return configured;
18
- const m = models.current();
19
- // 别的模型插件可能不给窗口(或给错)
20
- if (!m || !(typeof m.contextWindow === "number" && m.contextWindow > 0))
21
- return FALLBACK_THRESHOLD;
22
- return compactThreshold(m.contextWindow, m.kind === "anthropic" ? ANTHROPIC_REPLY_TOKENS : 0);
23
- };
8
+ const configured = parseTokens(config.thresholdTokens);
9
+ return () => {
10
+ if (configured)
11
+ return configured;
12
+ const m = models.current();
13
+ if (!m || !(typeof m.contextWindow === "number" && m.contextWindow > 0))
14
+ return FALLBACK_THRESHOLD;
15
+ return compactThreshold(m.contextWindow, m.kind === "anthropic" ? ANTHROPIC_REPLY_TOKENS : 0);
16
+ };
24
17
  }
25
- /** /compact 请求过的会话:下一次投影时无论是否超过阈值都总结一次。值是命令后面写的、要总结特别留意的事 */
26
- const requested = new Map();
18
+ const requested = new Map;
27
19
  export default definePlugin({
28
- process(ctx) {
29
- ctx.provide("compact@1", { threshold: thresholdFor(ctx.config, ctx.require("models@1")) });
30
- const commands = ctx.service("commands@1");
31
- commands?.register({
32
- name: "compact",
33
- args: "[what to keep]",
34
- description: "compact the history before the next request; text after the command tells the summary what to pay attention to",
35
- run(args, c) {
36
- const s = c.sessions.current;
37
- if (!s)
38
- return "No session is open.";
39
- requested.set(s.id, args.trim());
40
- return dim("History will be compacted before the next request.");
41
- },
42
- });
43
- const renderers = ctx.service("renderers@1");
44
- if (renderers) {
45
- renderers.event({
46
- event: "compacted",
47
- render: (data) => {
48
- const d = data;
49
- return { line: dim(`[compacted ${d.upTo - d.from} messages, ~${d.before} tokens before]`) };
50
- },
51
- });
52
- renderers.event({ event: "trimmed", render: (data) => ({ line: dim(`[cleared old tool output, ~${data.saved} tokens]`) }) });
20
+ process(ctx) {
21
+ ctx.provide("compact@1", { threshold: thresholdFor(ctx.config, ctx.require("models@1")) });
22
+ const commands = ctx.service("commands@1");
23
+ commands?.register({
24
+ name: "compact",
25
+ args: "[what to keep]",
26
+ description: "compact the history before the next request; text after the command tells the summary what to pay attention to",
27
+ run(args, c) {
28
+ const s = c.sessions.current;
29
+ if (!s)
30
+ return "No session is open.";
31
+ requested.set(s.id, args.trim());
32
+ return dim("History will be compacted before the next request.");
33
+ }
34
+ });
35
+ const renderers = ctx.service("renderers@1");
36
+ if (renderers) {
37
+ renderers.event({
38
+ event: "compacted",
39
+ render: (data) => {
40
+ const d = data;
41
+ return { line: dim(`[compacted ${d.upTo - d.from} messages, ~${d.before} tokens before]`) };
53
42
  }
54
- },
55
- session(ctx) {
56
- const models = ctx.require("models@1");
57
- const configured = parseTokens(ctx.config.thresholdTokens);
58
- const keepTokens = parseTokens(ctx.config.keepTokens);
59
- const threshold = thresholdFor(ctx.config, models);
60
- const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin });
61
- const id = ctx.session.id;
62
- ctx.kernel.hook({
63
- kind: "transform",
64
- point: "context.project",
65
- // 排在其他改投影的插件(例如 jade/mentions 附上引用的文件)之后:估算的是最终的投影
66
- order: 100,
67
- timeoutMs: SUMMARY_TIMEOUT_MS,
68
- fn: (messages, hook) => {
69
- const focus = requested.get(id);
70
- if (focus !== undefined) {
71
- requested.delete(id);
72
- c.request(focus || undefined);
73
- }
74
- return c.project(messages, hook);
75
- },
76
- });
77
- // 上下文溢出后压缩重试,每次用户输入只重试一次
78
- let retried = false;
79
- ctx.kernel.hook({
80
- kind: "observe",
81
- point: "event",
82
- fn: (e) => {
83
- c.observe(e);
84
- if (e.type === "input.received" && e.data.kind === "message")
85
- retried = false;
86
- if (e.type !== "turn.ended")
87
- return;
88
- const d = e.data;
89
- if (d.reason !== "error" || retried || !isContextOverflow(d.error))
90
- return;
91
- retried = true;
92
- c.request();
93
- const ui = ctx.service("ui@1");
94
- const window = models.current()?.contextWindow;
95
- ui?.notify("the request exceeded the model's context window; compacting the history and retrying" +
96
- (configured || !window ? "" : ` (jade assumed a ${window}-token window; set the real one with JADE_CONTEXT_WINDOW)`), "warning");
97
- // 不能在这里等 send:它要等这一轮结束,而这一轮在等这个 hook
98
- ctx.session.afterTurn(() => {
99
- ctx.session.send({ kind: "retry" }).catch((err) => ctx.signal.aborted || ui?.notify(`retry after compaction failed: ${err instanceof Error ? err.message : String(err)}`, "error"));
100
- });
101
- },
43
+ });
44
+ renderers.event({ event: "trimmed", render: (data) => ({ line: dim(`[cleared old tool output, ~${data.saved} tokens]`) }) });
45
+ }
46
+ },
47
+ session(ctx) {
48
+ if (ctx.session.parent)
49
+ return;
50
+ const models = ctx.require("models@1");
51
+ const configured = parseTokens(ctx.config.thresholdTokens);
52
+ const keepTokens = parseTokens(ctx.config.keepTokens);
53
+ const threshold = thresholdFor(ctx.config, models);
54
+ const c = compactor({ complete: (req, o) => models.complete(req, o), threshold, keepTokens, id: ctx.plugin, writes: (t) => ctx.session.tool(t)?.meta?.writes });
55
+ const id = ctx.session.id;
56
+ const show = (projected) => {
57
+ const at = threshold();
58
+ if (!at)
59
+ return ctx.status?.(undefined);
60
+ ctx.status?.({ text: `compacts at ${tokens(at)}`, ...projected !== undefined && projected >= at * 0.85 ? { tone: "warn" } : {} });
61
+ };
62
+ show();
63
+ const offModel = models.subscribe?.(() => show());
64
+ if (offModel)
65
+ ctx.effect(() => offModel);
66
+ ctx.kernel.hook({
67
+ kind: "transform",
68
+ point: "context.project",
69
+ order: 100,
70
+ timeoutMs: SUMMARY_TIMEOUT_MS,
71
+ fn: async (messages, hook) => {
72
+ const focus = requested.get(id);
73
+ if (focus !== undefined) {
74
+ requested.delete(id);
75
+ c.request(focus || undefined);
76
+ }
77
+ const out = await c.project(messages, hook);
78
+ show(c.projected());
79
+ return out;
80
+ }
81
+ });
82
+ let retried = false;
83
+ ctx.kernel.hook({
84
+ kind: "observe",
85
+ point: "event",
86
+ fn: (e) => {
87
+ c.observe(e);
88
+ if (e.type === "input.received" && e.data.kind === "message")
89
+ retried = false;
90
+ if (e.type !== "turn.ended")
91
+ return;
92
+ const d = e.data;
93
+ if (d.reason !== "error" || retried || !isContextOverflow(d.error))
94
+ return;
95
+ retried = true;
96
+ c.request();
97
+ const ui = ctx.service("ui@1");
98
+ const window = models.current()?.contextWindow;
99
+ ui?.notify("the request exceeded the model's context window; compacting the history and retrying" + (configured || !window ? "" : ` (jade assumed a ${window}-token window; set the real one with JADE_CONTEXT_WINDOW)`), "warning");
100
+ ctx.session.afterTurn(() => {
101
+ ctx.session.send({ kind: "retry" }).catch((err) => ctx.signal.aborted || ui?.notify(`retry after compaction failed: ${err instanceof Error ? err.message : String(err)}`, "error"));
102
102
  });
103
- },
103
+ }
104
+ });
105
+ }
104
106
  });
107
+ const tokens = (n) => n >= 1e6 ? `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M` : n >= 1000 ? `${(n / 1000).toFixed(n >= 1e4 ? 0 : 1)}k` : String(n);
@@ -1,14 +1,9 @@
1
- // 显示用的小工具:截断和 ANSI 颜色。插件之间不共享代码(只能共享类型),所以这里也有一份(同 jade/fs 的 style.ts)。
2
- export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
3
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
4
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
5
- /**
6
- * 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
7
- * 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
8
- */
9
- const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
10
- export const dim = paint("2", "22");
11
- export const red = paint("31", "39");
12
- export const green = paint("32", "39");
13
- export const yellow = paint("33", "39");
14
- export const bold = paint("1", "22");
1
+ export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
2
+ const styled = (style) => (text) => ({ text, style });
3
+ export const dim = styled("dim");
4
+ export const bold = styled("bold");
5
+ export const ok = styled("ok");
6
+ export const warn = styled("warn");
7
+ export const error = styled("error");
8
+ export const lines = (rows) => rows.flatMap((r, i) => i ? [`
9
+ `, r] : [r]);
@@ -0,0 +1,30 @@
1
+ import { definePlugin } from "@uncarved/jade-api";
2
+ const registry = new Map;
3
+ export default definePlugin({
4
+ process(ctx) {
5
+ ctx.provide("completions@1", (caller) => ({
6
+ register(completion) {
7
+ const trigger = completion?.trigger;
8
+ if (typeof trigger !== "string" || [...trigger].length !== 1 || /[\p{L}\p{N}\s/]/u.test(trigger)) {
9
+ throw new Error(`invalid completion trigger ${JSON.stringify(trigger)}: use one character that is not a letter, digit, space or "/"`);
10
+ }
11
+ if (typeof completion.candidates !== "function")
12
+ throw new Error(`completion ${trigger} has no candidates function`);
13
+ const taken = registry.get(trigger);
14
+ if (taken) {
15
+ ctx.report(`completion ${trigger} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
16
+ return () => {};
17
+ }
18
+ const entry = { completion, plugin: caller.plugin };
19
+ registry.set(trigger, entry);
20
+ const revoke = () => {
21
+ if (registry.get(trigger) === entry)
22
+ registry.delete(trigger);
23
+ };
24
+ caller.onDispose(revoke);
25
+ return revoke;
26
+ },
27
+ list: () => [...registry.values()]
28
+ }));
29
+ }
30
+ });
@@ -0,0 +1,9 @@
1
+ {
2
+ "id": "jade/completions",
3
+ "version": "0.1.0",
4
+ "api": "^1",
5
+ "entry": "./index.js",
6
+ "provides": [
7
+ "completions@1"
8
+ ]
9
+ }
@@ -1,57 +1,45 @@
1
- // jade/continue:模型的回复被输出上限截断(这一轮以 max_tokens 结束)时,让模型接着做,而不是把半截任务丢给用户。
2
- //
3
- // 从 turn.ended 事件知道这一轮怎么结束的(observe event),在 session.afterTurn 里用 session.send 发一条 [Note from jade]。
4
- // 每次用户输入之后最多续写 maxContinues 次。从旧 jade 的 plugins/turns.ts 的 continueTurns 移植。
5
- //
6
- // 旧 jade 的"上下文溢出后压缩重试"不在这里:不压缩就重试只会再溢出一次,整件事归 jade/compact。
7
1
  import { definePlugin } from "@uncarved/jade-api";
8
2
  export const DEFAULT_CONTINUES = 3;
9
- export const CONTINUE_NOTE = "Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " +
10
- "If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
3
+ export const CONTINUE_NOTE = "Your previous reply was cut off because it reached the output token limit. Continue from where you stopped. " + "If you were writing a large file or edit, split it into several smaller write_file/edit_file calls.";
11
4
  export default definePlugin({
12
- session(ctx) {
13
- const ui = ctx.service("ui@1");
14
- const raw = ctx.config.maxContinues;
15
- let max = DEFAULT_CONTINUES;
16
- if (typeof raw === "number" && Number.isInteger(raw) && raw >= 0)
17
- max = raw;
18
- else if (raw !== undefined)
19
- ui?.notify(`jade/continue: maxContinues must be a non-negative integer, got ${JSON.stringify(raw)}; using ${DEFAULT_CONTINUES}`, "warning");
20
- /** 这次用户输入之后已经续写了几次 */
21
- let count = 0;
22
- /** 我们发出、还没收到的续写;收到它不算用户的新输入 */
23
- let pending;
24
- const note = () => ({ kind: "text", text: `[Note from jade] ${CONTINUE_NOTE}`, origin: { by: "plugin", id: ctx.plugin }, trust: "trusted" });
25
- ctx.kernel.hook({
26
- kind: "observe",
27
- point: "event",
28
- fn: (e) => {
29
- if (e.type === "input.received") {
30
- const input = e.data;
31
- // 用户(或别的插件替用户)发的新消息:重新计数。/retry、审批不算新输入
32
- if (input.kind !== "message")
33
- return;
34
- const ours = pending !== undefined && input.content.length === 1 && input.content[0].kind === "text" && input.content[0].origin.by === "plugin" && input.content[0].origin.id === ctx.plugin;
35
- if (ours)
36
- pending = undefined;
37
- else
38
- count = 0;
39
- return;
40
- }
41
- if (e.type !== "turn.ended")
42
- return;
43
- if (e.data.reason !== "max_tokens" || count >= max)
44
- return;
45
- count++;
46
- const n = count;
47
- // 本轮完全结束之后再发:现在发会排在本轮之后,但 send 要等本轮结束,不能在 hook 里等
48
- ctx.session.afterTurn(() => {
49
- ui?.notify(`[asking the model to continue (${n}/${max})]`);
50
- pending = note();
51
- // 不能 await:afterTurn 跑完之后会话才处理排队的输入。出错时前端从 turn.ended 显示原因
52
- ctx.session.send({ kind: "message", content: [pending] }).catch(() => { });
53
- });
54
- },
5
+ session(ctx) {
6
+ const ui = ctx.service("ui@1");
7
+ const raw = ctx.config.maxContinues;
8
+ let max = DEFAULT_CONTINUES;
9
+ if (typeof raw === "number" && Number.isInteger(raw) && raw >= 0)
10
+ max = raw;
11
+ else if (raw !== undefined)
12
+ ui?.notify(`jade/continue: maxContinues must be a non-negative integer, got ${JSON.stringify(raw)}; using ${DEFAULT_CONTINUES}`, "warning");
13
+ let count = 0;
14
+ let pending;
15
+ const note = () => ({ kind: "text", text: `[Note from jade] ${CONTINUE_NOTE}`, origin: { by: "plugin", id: ctx.plugin }, trust: "trusted" });
16
+ ctx.kernel.hook({
17
+ kind: "observe",
18
+ point: "event",
19
+ fn: (e) => {
20
+ if (e.type === "input.received") {
21
+ const input = e.data;
22
+ if (input.kind !== "message")
23
+ return;
24
+ const ours = pending !== undefined && input.content.length === 1 && input.content[0].kind === "text" && input.content[0].origin.by === "plugin" && input.content[0].origin.id === ctx.plugin;
25
+ if (ours)
26
+ pending = undefined;
27
+ else
28
+ count = 0;
29
+ return;
30
+ }
31
+ if (e.type !== "turn.ended")
32
+ return;
33
+ if (e.data.reason !== "max_tokens" || count >= max)
34
+ return;
35
+ count++;
36
+ const n = count;
37
+ ctx.session.afterTurn(() => {
38
+ ui?.notify(`[asking the model to continue (${n}/${max})]`);
39
+ pending = note();
40
+ ctx.session.send({ kind: "message", content: [pending] }).catch(() => {});
55
41
  });
56
- },
42
+ }
43
+ });
44
+ }
57
45
  });
@@ -0,0 +1,40 @@
1
+ import { definePlugin } from "@uncarved/jade-api";
2
+ const styled = (style) => (text) => ({ text, style });
3
+ const dim = styled("dim");
4
+ const error = styled("error");
5
+ export function lastReply(messages) {
6
+ for (let i = messages.length - 1;i >= 0; i--) {
7
+ const m = messages[i];
8
+ if (m.role !== "assistant")
9
+ continue;
10
+ const text = m.content.flatMap((b) => b.kind === "text" ? [b.text] : []).join(`
11
+ `).trim();
12
+ if (text)
13
+ return text;
14
+ }
15
+ return;
16
+ }
17
+ export function lastCodeBlock(markdown) {
18
+ const blocks = [...markdown.matchAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[`~]*[ \t]*$/gm)];
19
+ return blocks.at(-1)?.[2]?.replace(/\n$/, "");
20
+ }
21
+ export default definePlugin({
22
+ process(ctx) {
23
+ ctx.require("commands@1").register({
24
+ name: "copy",
25
+ args: "[code]",
26
+ description: "copy the last reply to the clipboard as it was written (code: only its last code block)",
27
+ instant: true,
28
+ run(args, c) {
29
+ const code = args.trim() === "code";
30
+ const reply = lastReply(c.sessions.current?.state.messages ?? []);
31
+ const text = code ? reply && lastCodeBlock(reply) : reply;
32
+ if (!text)
33
+ return dim(code ? "The last reply has no code block." : "There is no reply to copy yet.");
34
+ if (!c.ui.copy?.(text))
35
+ return error("[error] this front end cannot copy to the clipboard");
36
+ return dim(`[copied ${[...text].length} characters]`);
37
+ }
38
+ });
39
+ }
40
+ });
@@ -0,0 +1,9 @@
1
+ {
2
+ "id": "jade/copy",
3
+ "version": "0.1.0",
4
+ "api": "^1",
5
+ "entry": "./index.js",
6
+ "requires": [
7
+ "commands@1"
8
+ ]
9
+ }