@uncarved/jade 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) 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 +490 -29
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +36 -14
  14. package/plugins/approval/policy.ts +15 -10
  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 +42 -9
  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 +47 -12
  50. package/plugins/models/index.ts +27 -1
  51. package/plugins/models/provider.ts +24 -0
  52. package/plugins/models/saved.ts +1 -1
  53. package/plugins/modes/index.ts +13 -2
  54. package/plugins/print/index.ts +7 -4
  55. package/plugins/prompts/index.ts +0 -0
  56. package/plugins/renderers/index.ts +10 -5
  57. package/plugins/repeats/index.ts +8 -8
  58. package/plugins/schedule/index.ts +5 -1
  59. package/plugins/screenshot/browser.ts +10 -0
  60. package/plugins/sessions/index.ts +48 -2
  61. package/plugins/shell/index.ts +27 -28
  62. package/plugins/shell/plugin.json +1 -1
  63. package/plugins/shell/renderers.ts +9 -7
  64. package/plugins/shell/shell.ts +3 -1
  65. package/plugins/skills/index.ts +28 -19
  66. package/plugins/skills/project.ts +12 -7
  67. package/plugins/steer/index.ts +5 -4
  68. package/plugins/task/index.ts +80 -50
  69. package/plugins/task/plugin.json +6 -2
  70. package/plugins/task/style.ts +13 -14
  71. package/plugins/task/subagent.ts +67 -184
  72. package/plugins/todo/index.ts +39 -9
  73. package/plugins/tools/index.ts +9 -2
  74. package/plugins/tui/frontend.ts +139 -89
  75. package/plugins/tui/index.ts +2 -3
  76. package/plugins/tui/mentions.ts +11 -80
  77. package/plugins/tui/plugin.json +1 -1
  78. package/plugins/tui/screen.ts +55 -5
  79. package/plugins/tui/transcript.ts +17 -7
  80. package/plugins/tui/tui.ts +337 -101
  81. package/plugins/tui/vendor.ts +1 -1
  82. package/plugins/undo/checkpoint.ts +8 -4
  83. package/plugins/undo/index.ts +170 -31
  84. package/plugins/undo/plugin.json +1 -0
  85. package/plugins/undo/snapshot.ts +87 -14
  86. package/plugins/undo/style.ts +13 -14
  87. package/plugins/web/index.ts +7 -18
  88. package/plugins/web/plugin.json +1 -1
  89. package/plugins-dist/approval/index.js +363 -418
  90. package/plugins-dist/approval/policy.js +391 -508
  91. package/plugins-dist/approval/review.js +132 -158
  92. package/plugins-dist/approval/schema.js +50 -55
  93. package/plugins-dist/ask/index.js +115 -129
  94. package/plugins-dist/budget/index.js +27 -31
  95. package/plugins-dist/checks/checks.js +293 -329
  96. package/plugins-dist/checks/index.js +119 -139
  97. package/plugins-dist/checks/plugin.json +2 -1
  98. package/plugins-dist/checks/project.js +57 -65
  99. package/plugins-dist/commands/index.js +47 -29
  100. package/plugins-dist/compact/compact.js +233 -249
  101. package/plugins-dist/compact/index.js +98 -95
  102. package/plugins-dist/compact/style.js +9 -14
  103. package/plugins-dist/completions/index.js +30 -0
  104. package/plugins-dist/completions/plugin.json +9 -0
  105. package/plugins-dist/continue/index.js +40 -52
  106. package/plugins-dist/copy/index.js +40 -0
  107. package/plugins-dist/copy/plugin.json +9 -0
  108. package/plugins-dist/cost/index.js +98 -115
  109. package/plugins-dist/custom-commands/index.js +55 -72
  110. package/plugins-dist/editor/index.js +64 -72
  111. package/plugins-dist/fs/diff.js +111 -124
  112. package/plugins-dist/fs/fs.js +426 -469
  113. package/plugins-dist/fs/grep.js +262 -253
  114. package/plugins-dist/fs/index.js +20 -34
  115. package/plugins-dist/fs/plugin.json +1 -2
  116. package/plugins-dist/fs/renderers.js +100 -112
  117. package/plugins-dist/fs/style.js +11 -15
  118. package/plugins-dist/fs/sys.js +31 -43
  119. package/plugins-dist/hooks/index.js +107 -127
  120. package/plugins-dist/keys/index.js +27 -31
  121. package/plugins-dist/mcp/index.js +271 -199
  122. package/plugins-dist/mcp/install.js +123 -137
  123. package/plugins-dist/mcp/mcp.js +638 -577
  124. package/plugins-dist/mcp/plugin.json +3 -1
  125. package/plugins-dist/mcp/project.js +98 -115
  126. package/plugins-dist/memory/index.js +83 -88
  127. package/plugins-dist/memory/memory.js +111 -128
  128. package/plugins-dist/memory/style.js +9 -14
  129. package/plugins-dist/mentions/attach.js +91 -99
  130. package/plugins-dist/mentions/candidates.js +49 -0
  131. package/plugins-dist/mentions/index.js +71 -81
  132. package/plugins-dist/mentions/plugin.json +2 -1
  133. package/plugins-dist/mentions/style.js +9 -14
  134. package/plugins-dist/models/config.js +29 -43
  135. package/plugins-dist/models/configure.js +421 -474
  136. package/plugins-dist/models/context.js +14 -22
  137. package/plugins-dist/models/index.js +182 -189
  138. package/plugins-dist/models/provider.js +263 -307
  139. package/plugins-dist/models/saved.js +60 -72
  140. package/plugins-dist/modes/index.js +138 -165
  141. package/plugins-dist/plugins/index.js +34 -34
  142. package/plugins-dist/print/index.js +192 -175
  143. package/plugins-dist/prompts/index.js +86 -89
  144. package/plugins-dist/prompts/notes.js +31 -37
  145. package/plugins-dist/prompts/prompts.js +155 -193
  146. package/plugins-dist/prompts/results.js +40 -53
  147. package/plugins-dist/prompts/sys.js +39 -50
  148. package/plugins-dist/renderers/index.js +32 -32
  149. package/plugins-dist/repeats/index.js +131 -174
  150. package/plugins-dist/schedule/cron.js +49 -56
  151. package/plugins-dist/schedule/index.js +230 -247
  152. package/plugins-dist/schedule/store.js +93 -109
  153. package/plugins-dist/scratch/index.js +12 -18
  154. package/plugins-dist/screenshot/browser.js +136 -154
  155. package/plugins-dist/screenshot/index.js +37 -46
  156. package/plugins-dist/screenshot/sys.js +53 -65
  157. package/plugins-dist/screenshot/tool.js +98 -126
  158. package/plugins-dist/sessions/index.js +149 -117
  159. package/plugins-dist/shell/index.js +117 -135
  160. package/plugins-dist/shell/plugin.json +1 -1
  161. package/plugins-dist/shell/renderers.js +28 -33
  162. package/plugins-dist/shell/service.js +17 -23
  163. package/plugins-dist/shell/shell.js +307 -362
  164. package/plugins-dist/shell/sys.js +64 -79
  165. package/plugins-dist/skills/index.js +193 -211
  166. package/plugins-dist/skills/install.js +122 -149
  167. package/plugins-dist/skills/project.js +65 -73
  168. package/plugins-dist/skills/skills.js +135 -171
  169. package/plugins-dist/steer/index.js +108 -125
  170. package/plugins-dist/suggest/index.js +235 -259
  171. package/plugins-dist/task/index.js +130 -117
  172. package/plugins-dist/task/plugin.json +5 -2
  173. package/plugins-dist/task/style.js +9 -14
  174. package/plugins-dist/task/subagent.js +156 -279
  175. package/plugins-dist/todo/index.js +79 -63
  176. package/plugins-dist/tools/index.js +36 -42
  177. package/plugins-dist/tui/editor.js +145 -160
  178. package/plugins-dist/tui/frontend.js +639 -678
  179. package/plugins-dist/tui/history.js +34 -37
  180. package/plugins-dist/tui/index.js +16 -18
  181. package/plugins-dist/tui/markdown.js +203 -247
  182. package/plugins-dist/tui/mascot.js +266 -355
  183. package/plugins-dist/tui/mentions.js +67 -144
  184. package/plugins-dist/tui/plugin.json +1 -2
  185. package/plugins-dist/tui/render.js +35 -45
  186. package/plugins-dist/tui/screen.js +197 -180
  187. package/plugins-dist/tui/transcript.js +436 -468
  188. package/plugins-dist/tui/tui.js +1554 -1656
  189. package/plugins-dist/tui/vendor.js +1088 -1388
  190. package/plugins-dist/undo/checkpoint.js +97 -118
  191. package/plugins-dist/undo/index.js +281 -192
  192. package/plugins-dist/undo/plugin.json +3 -0
  193. package/plugins-dist/undo/snapshot.js +134 -99
  194. package/plugins-dist/undo/style.js +9 -14
  195. package/plugins-dist/update/index.js +26 -28
  196. package/plugins-dist/web/env.js +55 -68
  197. package/plugins-dist/web/index.js +80 -110
  198. package/plugins-dist/web/plugin.json +1 -2
  199. package/plugins-dist/web/renderers.js +4 -5
  200. package/plugins-dist/web/web.js +514 -593
  201. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  202. package/sdk/python/jade_plugin.py +9 -2
  203. package/src/api.ts +1 -1
  204. package/src/host.ts +39 -4
  205. package/src/remote.ts +6 -1
  206. package/src/session.ts +496 -36
@@ -1,7 +1,3 @@
1
- // jade/schedule:定时任务。任务存在 $JADE_HOME/state/schedules.json,重启后还在;
2
- // 到点时投到创建它的目录里正在运行的 jade 的当前会话(正在跑就排在本轮之后),没有 jade 开着就等着,
3
- // 下次打开时错过的几次只补一次。不能提问的前端(jade -p)不投递。
4
- // 管理入口:/schedule 命令和给模型的 schedule_create、schedule_list、schedule_edit 工具,共用同一份存储。
5
1
  import path from "node:path";
6
2
  import { definePlugin } from "@uncarved/jade-api";
7
3
  import { parseCron } from "./cron.js";
@@ -9,102 +5,96 @@ import { claimDue, nextAt, sameDir, ScheduleStore } from "./store.js";
9
5
  const ID = "jade/schedule";
10
6
  const TITLE_CHARS = 120;
11
7
  function titleFrom(prompt) {
12
- const one = prompt.replace(/\s+/g, " ").trim();
13
- return one.length > 50 ? `${one.slice(0, 50)}…` : one;
8
+ const one = prompt.replace(/\s+/g, " ").trim();
9
+ return one.length > 50 ? `${one.slice(0, 50)}…` : one;
14
10
  }
15
- /** 检查 cron 并算出下一次;没有下一次时抛错 */
16
11
  function firstRun(cron, now) {
17
- parseCron(cron);
18
- const next = nextAt(cron, now);
19
- if (!next)
20
- throw new Error(`cron "${cron}" never fires`);
21
- return next;
12
+ parseCron(cron);
13
+ const next = nextAt(cron, now);
14
+ if (!next)
15
+ throw new Error(`cron "${cron}" never fires`);
16
+ return next;
22
17
  }
23
- export async function createTask(store, opts, now = new Date()) {
24
- const prompt = opts.prompt.trim();
25
- if (!prompt)
26
- throw new Error("prompt is empty");
27
- const cron = opts.cron.trim().split(/\s+/).join(" ");
28
- const next = firstRun(cron, now);
29
- const title = (opts.title?.trim() || titleFrom(prompt)).slice(0, TITLE_CHARS);
30
- const at = now.toISOString();
31
- const task = {
32
- id: crypto.randomUUID().slice(0, 8),
33
- title,
34
- prompt,
35
- cron,
36
- recurring: opts.recurring ?? true,
37
- cwd: path.resolve(opts.cwd),
38
- target: { kind: "cwd" },
39
- status: "active",
40
- next,
41
- createdAt: at,
42
- updatedAt: at,
43
- };
44
- await store.update((tasks) => void tasks.push(task));
45
- return task;
18
+ export async function createTask(store, opts, now = new Date) {
19
+ const prompt = opts.prompt.trim();
20
+ if (!prompt)
21
+ throw new Error("prompt is empty");
22
+ const cron = opts.cron.trim().split(/\s+/).join(" ");
23
+ const next = firstRun(cron, now);
24
+ const title = (opts.title?.trim() || titleFrom(prompt)).slice(0, TITLE_CHARS);
25
+ const at = now.toISOString();
26
+ const task = {
27
+ id: crypto.randomUUID().slice(0, 8),
28
+ title,
29
+ prompt,
30
+ cron,
31
+ recurring: opts.recurring ?? true,
32
+ cwd: path.resolve(opts.cwd),
33
+ target: { kind: "cwd" },
34
+ status: "active",
35
+ next,
36
+ createdAt: at,
37
+ updatedAt: at
38
+ };
39
+ await store.update((tasks) => void tasks.push(task));
40
+ return task;
46
41
  }
47
- /** 改一个任务;删除时返回 undefined。恢复和改 cron 从现在起重新算下一次 */
48
- export function editTask(store, id, action, patch = {}, now = new Date()) {
49
- return store.update((tasks) => {
50
- const i = tasks.findIndex((t) => t.id === id);
51
- if (i < 0)
52
- throw new Error(`no scheduled task ${id}`);
53
- if (action === "delete") {
54
- tasks.splice(i, 1);
55
- return undefined;
56
- }
57
- const t = tasks[i];
58
- if (action === "pause") {
59
- t.status = "paused";
60
- delete t.next;
61
- }
62
- else if (action === "resume") {
63
- t.status = "active";
64
- t.next = firstRun(t.cron, now);
65
- }
66
- else if (action === "update") {
67
- if (patch.prompt !== undefined) {
68
- if (!patch.prompt.trim())
69
- throw new Error("prompt is empty");
70
- t.prompt = patch.prompt.trim();
71
- }
72
- if (patch.title !== undefined)
73
- t.title = (patch.title.trim() || titleFrom(t.prompt)).slice(0, TITLE_CHARS);
74
- if (patch.recurring !== undefined)
75
- t.recurring = patch.recurring;
76
- if (patch.cron !== undefined) {
77
- t.cron = patch.cron.trim().split(/\s+/).join(" ");
78
- const next = firstRun(t.cron, now);
79
- if (t.status === "active")
80
- t.next = next;
81
- }
82
- }
83
- else {
84
- throw new Error(`unknown action ${String(action)}`);
85
- }
86
- t.updatedAt = now.toISOString();
87
- return { ...t };
88
- });
42
+ export function editTask(store, id, action, patch = {}, now = new Date) {
43
+ return store.update((tasks) => {
44
+ const i = tasks.findIndex((t) => t.id === id);
45
+ if (i < 0)
46
+ throw new Error(`no scheduled task ${id}`);
47
+ if (action === "delete") {
48
+ tasks.splice(i, 1);
49
+ return;
50
+ }
51
+ const t = tasks[i];
52
+ if (action === "pause") {
53
+ t.status = "paused";
54
+ delete t.next;
55
+ } else if (action === "resume") {
56
+ t.status = "active";
57
+ t.next = firstRun(t.cron, now);
58
+ } else if (action === "update") {
59
+ if (patch.prompt !== undefined) {
60
+ if (!patch.prompt.trim())
61
+ throw new Error("prompt is empty");
62
+ t.prompt = patch.prompt.trim();
63
+ }
64
+ if (patch.title !== undefined)
65
+ t.title = (patch.title.trim() || titleFrom(t.prompt)).slice(0, TITLE_CHARS);
66
+ if (patch.recurring !== undefined)
67
+ t.recurring = patch.recurring;
68
+ if (patch.cron !== undefined) {
69
+ t.cron = patch.cron.trim().split(/\s+/).join(" ");
70
+ const next = firstRun(t.cron, now);
71
+ if (t.status === "active")
72
+ t.next = next;
73
+ }
74
+ } else {
75
+ throw new Error(`unknown action ${String(action)}`);
76
+ }
77
+ t.updatedAt = now.toISOString();
78
+ return { ...t };
79
+ });
89
80
  }
90
- /** 一行说明;任务不在 cwd 时写上它的目录 */
91
81
  export function describe(t, cwd) {
92
- const when = t.status === "active" && t.next ? `next ${new Date(t.next).toLocaleString()}` : t.status;
93
- const once = t.recurring ? "" : " (once)";
94
- const where = sameDir(t.cwd, cwd) ? "" : ` [in ${t.cwd}]`;
95
- return `${t.id} ${t.cron}${once} ${when} ${t.title}${where}`;
82
+ const when = t.status === "active" && t.next ? `next ${new Date(t.next).toLocaleString()}` : t.status;
83
+ const once = t.recurring ? "" : " (once)";
84
+ const where = sameDir(t.cwd, cwd) ? "" : ` [in ${t.cwd}]`;
85
+ return `${t.id} ${t.cron}${once} ${when} ${t.title}${where}`;
96
86
  }
97
87
  function listing(tasks, cwd) {
98
- if (tasks.length === 0)
99
- return "No scheduled tasks.";
100
- // 本目录的在前
101
- const sorted = [...tasks].sort((a, b) => Number(sameDir(b.cwd, cwd)) - Number(sameDir(a.cwd, cwd)));
102
- return sorted.map((t) => describe(t, cwd)).join("\n");
88
+ if (tasks.length === 0)
89
+ return "No scheduled tasks.";
90
+ const sorted = [...tasks].sort((a, b) => Number(sameDir(b.cwd, cwd)) - Number(sameDir(a.cwd, cwd)));
91
+ return sorted.map((t) => describe(t, cwd)).join(`
92
+ `);
103
93
  }
104
- const messageText = (t) => `[Scheduled task "${t.title}" (${t.id})]\n${t.prompt}`;
105
- /** 到点时发给模型的消息 */
94
+ const messageText = (t) => `[Scheduled task "${t.title}" (${t.id})]
95
+ ${t.prompt}`;
106
96
  export function delivery(t) {
107
- return { kind: "message", content: [{ kind: "text", text: messageText(t), origin: { by: "plugin", id: ID }, trust: "trusted" }] };
97
+ return { kind: "message", content: [{ kind: "text", text: messageText(t), origin: { by: "plugin", id: ID }, trust: "trusted" }] };
108
98
  }
109
99
  const USAGE = `usage:
110
100
  /schedule list tasks
@@ -113,169 +103,162 @@ const USAGE = `usage:
113
103
  /schedule pause|resume|rm <id>
114
104
  /schedule run <id> send a task's prompt now (does not move its next run)
115
105
  Tasks run in the current session of a jade that is open in the directory they were created in.`;
116
- const err = (e) => (e instanceof Error ? e.message : String(e));
106
+ const err = (e) => e instanceof Error ? e.message : String(e);
117
107
  function tools(store, cwd) {
118
- const result = async (fn) => {
119
- try {
120
- return { content: await fn() };
121
- }
122
- catch (e) {
123
- return { content: err(e), isError: true };
124
- }
125
- };
126
- const where = "A task runs only while jade is open in the directory it was created in: its prompt is sent as a message to that jade's current session (after the current turn if one is running). " +
127
- "Occurrences missed while jade was closed are run once when it opens again.";
128
- return [
129
- {
130
- name: "schedule_create",
131
- description: `Create a scheduled task that sends a prompt to the session at times given by a 5-field cron expression in the user's local time zone (minute hour day-of-month month day-of-week). ${where} ` +
132
- "Write the prompt as a self-contained instruction: it is read later, possibly in a different session. Use recurring: false for a one-time reminder.",
133
- inputSchema: {
134
- type: "object",
135
- properties: {
136
- prompt: { type: "string", description: "the instruction to send when the task fires" },
137
- cron: { type: "string", description: 'e.g. "0 9 * * 1-5" for weekdays at 09:00' },
138
- title: { type: "string", description: "short name; defaults to the start of the prompt" },
139
- recurring: { type: "boolean", description: "false: fire once, then end (default true)" },
140
- },
141
- required: ["prompt", "cron"],
142
- additionalProperties: false,
143
- },
144
- // 任务在本会话之后、以用户的身份发出指令,所以和改工作区之外的东西一样要问
145
- effect: "external",
146
- concurrency: "exclusive",
147
- idempotent: false,
148
- execute: (input) => result(async () => {
149
- const i = input;
150
- const t = await createTask(store, { ...i, cwd });
151
- return `Created scheduled task:\n${describe(t, cwd)}`;
152
- }),
153
- },
154
- {
155
- name: "schedule_list",
156
- description: `List all scheduled tasks with their id, cron, next run time and title. ${where}`,
157
- inputSchema: { type: "object", properties: {}, additionalProperties: false },
158
- effect: "read",
159
- concurrency: "safe",
160
- idempotent: true,
161
- execute: () => result(async () => listing(await store.list(), cwd)),
108
+ const result = async (fn) => {
109
+ try {
110
+ return { content: await fn() };
111
+ } catch (e) {
112
+ return { content: err(e), isError: true };
113
+ }
114
+ };
115
+ const where = "A task runs only while jade is open in the directory it was created in: its prompt is sent as a message to that jade's current session (after the current turn if one is running). " + "Occurrences missed while jade was closed are run once when it opens again.";
116
+ return [
117
+ {
118
+ name: "schedule_create",
119
+ description: `Create a scheduled task that sends a prompt to the session at times given by a 5-field cron expression in the user's local time zone (minute hour day-of-month month day-of-week). ${where} ` + "Write the prompt as a self-contained instruction: it is read later, possibly in a different session. Use recurring: false for a one-time reminder.",
120
+ inputSchema: {
121
+ type: "object",
122
+ properties: {
123
+ prompt: { type: "string", description: "the instruction to send when the task fires" },
124
+ cron: { type: "string", description: 'e.g. "0 9 * * 1-5" for weekdays at 09:00' },
125
+ title: { type: "string", description: "short name; defaults to the start of the prompt" },
126
+ recurring: { type: "boolean", description: "false: fire once, then end (default true)" }
162
127
  },
163
- {
164
- name: "schedule_edit",
165
- description: "Change a scheduled task (update its prompt, cron, title or recurring), pause it, resume it, or delete it. Get ids from schedule_list.",
166
- inputSchema: {
167
- type: "object",
168
- properties: {
169
- id: { type: "string" },
170
- action: { type: "string", enum: ["update", "pause", "resume", "delete"] },
171
- prompt: { type: "string" },
172
- cron: { type: "string" },
173
- title: { type: "string" },
174
- recurring: { type: "boolean" },
175
- },
176
- required: ["id", "action"],
177
- additionalProperties: false,
178
- },
179
- effect: "external",
180
- concurrency: "exclusive",
181
- idempotent: false,
182
- execute: (input) => result(async () => {
183
- const { id, action, ...patch } = input;
184
- const t = await editTask(store, id, action, patch);
185
- return t ? `Updated:\n${describe(t, cwd)}` : `Deleted scheduled task ${id}.`;
186
- }),
128
+ required: ["prompt", "cron"],
129
+ additionalProperties: false
130
+ },
131
+ effect: "external",
132
+ concurrency: "exclusive",
133
+ idempotent: false,
134
+ execute: (input) => result(async () => {
135
+ const i = input;
136
+ const t = await createTask(store, { ...i, cwd });
137
+ return `Created scheduled task:
138
+ ${describe(t, cwd)}`;
139
+ })
140
+ },
141
+ {
142
+ name: "schedule_list",
143
+ description: `List all scheduled tasks with their id, cron, next run time and title. ${where}`,
144
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
145
+ effect: "read",
146
+ concurrency: "safe",
147
+ idempotent: true,
148
+ execute: () => result(async () => listing(await store.list(), cwd))
149
+ },
150
+ {
151
+ name: "schedule_edit",
152
+ description: "Change a scheduled task (update its prompt, cron, title or recurring), pause it, resume it, or delete it. Get ids from schedule_list.",
153
+ inputSchema: {
154
+ type: "object",
155
+ properties: {
156
+ id: { type: "string" },
157
+ action: { type: "string", enum: ["update", "pause", "resume", "delete"] },
158
+ prompt: { type: "string" },
159
+ cron: { type: "string" },
160
+ title: { type: "string" },
161
+ recurring: { type: "boolean" }
187
162
  },
188
- ];
163
+ required: ["id", "action"],
164
+ additionalProperties: false
165
+ },
166
+ effect: "external",
167
+ concurrency: "exclusive",
168
+ idempotent: false,
169
+ execute: (input) => result(async () => {
170
+ const { id, action, ...patch } = input;
171
+ const t = await editTask(store, id, action, patch);
172
+ return t ? `Updated:
173
+ ${describe(t, cwd)}` : `Deleted scheduled task ${id}.`;
174
+ })
175
+ }
176
+ ];
189
177
  }
190
178
  export default definePlugin({
191
- process(ctx) {
192
- const store = new ScheduleStore(ctx.home);
193
- ctx.service("commands@1")?.register({
194
- name: "schedule",
195
- args: "[add|once|pause|resume|rm|run] …",
196
- description: "scheduled tasks: prompts sent on a cron schedule while jade is open here",
197
- async run(args) {
198
- const [sub = "list", ...rest] = args.trim().split(/\s+/).filter(Boolean);
199
- try {
200
- switch (sub) {
201
- case "list":
202
- return listing(await store.list(), ctx.cwd);
203
- case "add":
204
- case "once": {
205
- if (rest.length < 6)
206
- return USAGE;
207
- const t = await createTask(store, { cron: rest.slice(0, 5).join(" "), prompt: rest.slice(5).join(" "), recurring: sub === "add", cwd: ctx.cwd });
208
- return `Scheduled:\n${describe(t, ctx.cwd)}`;
209
- }
210
- case "pause":
211
- case "resume":
212
- case "rm": {
213
- if (rest.length !== 1)
214
- return USAGE;
215
- const t = await editTask(store, rest[0], sub === "rm" ? "delete" : sub);
216
- return t ? describe(t, ctx.cwd) : `Deleted ${rest[0]}.`;
217
- }
218
- case "run": {
219
- const t = (await store.list()).find((x) => x.id === rest[0]);
220
- if (!t)
221
- return rest.length ? `No scheduled task ${rest[0]}.` : USAGE;
222
- return { prompt: messageText(t) };
223
- }
224
- default:
225
- return USAGE;
226
- }
227
- }
228
- catch (e) {
229
- return `schedule: ${err(e)}`;
230
- }
231
- },
232
- });
233
- },
234
- async session(ctx) {
235
- const store = new ScheduleStore(ctx.home);
236
- for (const t of tools(store, ctx.cwd))
237
- ctx.kernel.tool(t);
238
- const ui = ctx.service("ui@1");
239
- // jade -p 这类不能提问的运行只做用户要它做的那一件事
240
- if (!ui?.interactive)
241
- return;
242
- const seconds = Number(ctx.config.checkSeconds);
243
- const every = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 30_000;
244
- /** 已经发出、还没跑完的任务:跑完之前不再认领 */
245
- const running = new Set();
246
- let warned = false;
247
- const check = async () => {
248
- if (ctx.signal.aborted)
249
- return;
250
- let due;
251
- try {
252
- due = await claimDue(store, ctx.cwd, ctx.session.id, new Date(), running);
179
+ process(ctx) {
180
+ const store = new ScheduleStore(ctx.home);
181
+ ctx.service("commands@1")?.register({
182
+ name: "schedule",
183
+ args: "[add|once|pause|resume|rm|run] …",
184
+ description: "scheduled tasks: prompts sent on a cron schedule while jade is open here",
185
+ async run(args) {
186
+ const [sub = "list", ...rest] = args.trim().split(/\s+/).filter(Boolean);
187
+ try {
188
+ switch (sub) {
189
+ case "list":
190
+ return listing(await store.list(), ctx.cwd);
191
+ case "add":
192
+ case "once": {
193
+ if (rest.length < 6)
194
+ return USAGE;
195
+ const t = await createTask(store, { cron: rest.slice(0, 5).join(" "), prompt: rest.slice(5).join(" "), recurring: sub === "add", cwd: ctx.cwd });
196
+ return `Scheduled:
197
+ ${describe(t, ctx.cwd)}`;
253
198
  }
254
- catch (e) {
255
- if (ctx.signal.aborted)
256
- return;
257
- if (!warned)
258
- ui.notify(`schedule: ${err(e)}`, "warning");
259
- warned = true;
260
- return;
199
+ case "pause":
200
+ case "resume":
201
+ case "rm": {
202
+ if (rest.length !== 1)
203
+ return USAGE;
204
+ const t = await editTask(store, rest[0], sub === "rm" ? "delete" : sub);
205
+ return t ? describe(t, ctx.cwd) : `Deleted ${rest[0]}.`;
261
206
  }
262
- warned = false;
263
- for (const t of due) {
264
- if (ctx.signal.aborted)
265
- return;
266
- running.add(t.id);
267
- ui.notify(`[scheduled task "${t.title}" (${t.id})]`);
268
- ctx.session
269
- .send(delivery(t))
270
- // 会话已经离开(换会话、退出)时 ui 已经不能用了
271
- .catch((e) => ctx.signal.aborted || ui.notify(`scheduled task ${t.id} failed: ${err(e)}`, "error"))
272
- .finally(() => running.delete(t.id));
207
+ case "run": {
208
+ const t = (await store.list()).find((x) => x.id === rest[0]);
209
+ if (!t)
210
+ return rest.length ? `No scheduled task ${rest[0]}.` : USAGE;
211
+ return { prompt: messageText(t) };
273
212
  }
274
- };
275
- // 不在这里立即检查:session() 执行时会话还没建好,这时 send 会失败,认领了的任务就丢了
276
- await ctx.effect(() => {
277
- const timer = setInterval(() => void check(), every);
278
- return () => clearInterval(timer);
279
- });
280
- },
213
+ default:
214
+ return USAGE;
215
+ }
216
+ } catch (e) {
217
+ return `schedule: ${err(e)}`;
218
+ }
219
+ }
220
+ });
221
+ },
222
+ async session(ctx) {
223
+ if (ctx.session.parent)
224
+ return;
225
+ const store = new ScheduleStore(ctx.home);
226
+ for (const t of tools(store, ctx.cwd))
227
+ ctx.kernel.tool(t);
228
+ const ui = ctx.service("ui@1");
229
+ if (!ui?.interactive)
230
+ return;
231
+ const seconds = Number(ctx.config.checkSeconds);
232
+ const every = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 30000;
233
+ const running = new Set;
234
+ let warned = false;
235
+ const check = async () => {
236
+ if (ctx.signal.aborted)
237
+ return;
238
+ let due;
239
+ try {
240
+ due = await claimDue(store, ctx.cwd, ctx.session.id, new Date, running);
241
+ } catch (e) {
242
+ if (ctx.signal.aborted)
243
+ return;
244
+ if (!warned)
245
+ ui.notify(`schedule: ${err(e)}`, "warning");
246
+ warned = true;
247
+ return;
248
+ }
249
+ warned = false;
250
+ for (const t of due) {
251
+ if (ctx.signal.aborted)
252
+ return;
253
+ running.add(t.id);
254
+ ui.notify(`[scheduled task "${t.title}" (${t.id})]`);
255
+ ctx.session.send(delivery(t)).catch((e) => ctx.signal.aborted || ui.notify(`scheduled task ${t.id} failed: ${err(e)}`, "error")).finally(() => running.delete(t.id));
256
+ }
257
+ };
258
+ ctx.session.afterTurn(() => void check());
259
+ await ctx.effect(() => {
260
+ const timer = setInterval(() => void check(), every);
261
+ return () => clearInterval(timer);
262
+ });
263
+ }
281
264
  });