@uncarved/jade 0.3.1 → 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 (199) hide show
  1. package/dist/api.d.ts +1 -1
  2. package/dist/api.js +1 -1
  3. package/dist/host.d.ts +2 -0
  4. package/dist/host.js +39 -5
  5. package/dist/host.js.map +1 -1
  6. package/dist/remote.js +5 -0
  7. package/dist/remote.js.map +1 -1
  8. package/dist/session.d.ts +7 -0
  9. package/dist/session.js +482 -27
  10. package/dist/session.js.map +1 -1
  11. package/package.json +4 -4
  12. package/plugins/approval/index.ts +36 -14
  13. package/plugins/approval/policy.ts +15 -10
  14. package/plugins/approval/review.ts +9 -5
  15. package/plugins/checks/checks.ts +10 -7
  16. package/plugins/checks/index.ts +13 -13
  17. package/plugins/checks/plugin.json +1 -1
  18. package/plugins/checks/project.ts +37 -38
  19. package/plugins/commands/index.ts +27 -3
  20. package/plugins/compact/compact.ts +13 -5
  21. package/plugins/compact/index.ts +20 -3
  22. package/plugins/compact/style.ts +13 -14
  23. package/plugins/completions/index.ts +35 -0
  24. package/plugins/completions/plugin.json +7 -0
  25. package/plugins/copy/index.ts +46 -0
  26. package/plugins/copy/plugin.json +7 -0
  27. package/plugins/editor/index.ts +10 -4
  28. package/plugins/fs/diff.ts +7 -6
  29. package/plugins/fs/fs.ts +8 -4
  30. package/plugins/fs/grep.ts +42 -9
  31. package/plugins/fs/index.ts +2 -7
  32. package/plugins/fs/plugin.json +1 -1
  33. package/plugins/fs/renderers.ts +16 -15
  34. package/plugins/fs/style.ts +16 -15
  35. package/plugins/hooks/index.ts +3 -2
  36. package/plugins/keys/index.ts +5 -1
  37. package/plugins/mcp/index.ts +197 -78
  38. package/plugins/mcp/install.ts +5 -3
  39. package/plugins/mcp/mcp.ts +163 -11
  40. package/plugins/mcp/plugin.json +2 -1
  41. package/plugins/mcp/project.ts +47 -50
  42. package/plugins/memory/index.ts +4 -4
  43. package/plugins/memory/style.ts +13 -14
  44. package/plugins/mentions/candidates.ts +67 -0
  45. package/plugins/mentions/index.ts +12 -5
  46. package/plugins/mentions/plugin.json +1 -1
  47. package/plugins/mentions/style.ts +13 -14
  48. package/plugins/models/configure.ts +10 -9
  49. package/plugins/models/index.ts +27 -1
  50. package/plugins/modes/index.ts +13 -2
  51. package/plugins/print/index.ts +7 -4
  52. package/plugins/prompts/index.ts +0 -0
  53. package/plugins/renderers/index.ts +10 -5
  54. package/plugins/repeats/index.ts +8 -8
  55. package/plugins/schedule/index.ts +2 -0
  56. package/plugins/screenshot/browser.ts +10 -0
  57. package/plugins/sessions/index.ts +48 -2
  58. package/plugins/shell/index.ts +20 -25
  59. package/plugins/shell/plugin.json +1 -2
  60. package/plugins/shell/renderers.ts +9 -7
  61. package/plugins/shell/shell.ts +3 -1
  62. package/plugins/skills/index.ts +28 -19
  63. package/plugins/skills/project.ts +12 -7
  64. package/plugins/steer/index.ts +5 -4
  65. package/plugins/task/index.ts +80 -50
  66. package/plugins/task/plugin.json +6 -2
  67. package/plugins/task/style.ts +13 -14
  68. package/plugins/task/subagent.ts +67 -184
  69. package/plugins/todo/index.ts +39 -9
  70. package/plugins/tools/index.ts +9 -2
  71. package/plugins/tui/frontend.ts +139 -89
  72. package/plugins/tui/index.ts +2 -3
  73. package/plugins/tui/mentions.ts +11 -80
  74. package/plugins/tui/plugin.json +1 -1
  75. package/plugins/tui/screen.ts +55 -5
  76. package/plugins/tui/transcript.ts +17 -7
  77. package/plugins/tui/tui.ts +337 -101
  78. package/plugins/tui/vendor.ts +1 -1
  79. package/plugins/undo/checkpoint.ts +5 -3
  80. package/plugins/undo/index.ts +43 -14
  81. package/plugins/undo/style.ts +13 -14
  82. package/plugins/web/index.ts +7 -18
  83. package/plugins/web/plugin.json +1 -1
  84. package/plugins-dist/approval/index.js +363 -418
  85. package/plugins-dist/approval/policy.js +391 -508
  86. package/plugins-dist/approval/review.js +132 -158
  87. package/plugins-dist/approval/schema.js +50 -55
  88. package/plugins-dist/ask/index.js +115 -129
  89. package/plugins-dist/budget/index.js +27 -31
  90. package/plugins-dist/checks/checks.js +293 -329
  91. package/plugins-dist/checks/index.js +119 -139
  92. package/plugins-dist/checks/plugin.json +2 -1
  93. package/plugins-dist/checks/project.js +57 -65
  94. package/plugins-dist/commands/index.js +47 -29
  95. package/plugins-dist/compact/compact.js +233 -249
  96. package/plugins-dist/compact/index.js +98 -95
  97. package/plugins-dist/compact/style.js +9 -14
  98. package/plugins-dist/completions/index.js +30 -0
  99. package/plugins-dist/completions/plugin.json +9 -0
  100. package/plugins-dist/continue/index.js +40 -52
  101. package/plugins-dist/copy/index.js +40 -0
  102. package/plugins-dist/copy/plugin.json +9 -0
  103. package/plugins-dist/cost/index.js +98 -115
  104. package/plugins-dist/custom-commands/index.js +55 -72
  105. package/plugins-dist/editor/index.js +64 -72
  106. package/plugins-dist/fs/diff.js +111 -124
  107. package/plugins-dist/fs/fs.js +426 -469
  108. package/plugins-dist/fs/grep.js +262 -253
  109. package/plugins-dist/fs/index.js +20 -34
  110. package/plugins-dist/fs/plugin.json +1 -2
  111. package/plugins-dist/fs/renderers.js +100 -112
  112. package/plugins-dist/fs/style.js +11 -15
  113. package/plugins-dist/fs/sys.js +31 -43
  114. package/plugins-dist/hooks/index.js +107 -127
  115. package/plugins-dist/keys/index.js +27 -31
  116. package/plugins-dist/mcp/index.js +271 -199
  117. package/plugins-dist/mcp/install.js +123 -137
  118. package/plugins-dist/mcp/mcp.js +638 -577
  119. package/plugins-dist/mcp/plugin.json +3 -1
  120. package/plugins-dist/mcp/project.js +98 -115
  121. package/plugins-dist/memory/index.js +83 -88
  122. package/plugins-dist/memory/memory.js +111 -128
  123. package/plugins-dist/memory/style.js +9 -14
  124. package/plugins-dist/mentions/attach.js +91 -99
  125. package/plugins-dist/mentions/candidates.js +49 -0
  126. package/plugins-dist/mentions/index.js +71 -81
  127. package/plugins-dist/mentions/plugin.json +2 -1
  128. package/plugins-dist/mentions/style.js +9 -14
  129. package/plugins-dist/models/config.js +29 -43
  130. package/plugins-dist/models/configure.js +392 -473
  131. package/plugins-dist/models/context.js +14 -22
  132. package/plugins-dist/models/index.js +182 -189
  133. package/plugins-dist/models/provider.js +243 -307
  134. package/plugins-dist/models/saved.js +59 -71
  135. package/plugins-dist/modes/index.js +138 -165
  136. package/plugins-dist/plugins/index.js +34 -34
  137. package/plugins-dist/print/index.js +192 -175
  138. package/plugins-dist/prompts/index.js +81 -89
  139. package/plugins-dist/prompts/notes.js +31 -37
  140. package/plugins-dist/prompts/prompts.js +155 -193
  141. package/plugins-dist/prompts/results.js +40 -53
  142. package/plugins-dist/prompts/sys.js +39 -50
  143. package/plugins-dist/renderers/index.js +32 -32
  144. package/plugins-dist/repeats/index.js +131 -174
  145. package/plugins-dist/schedule/cron.js +49 -56
  146. package/plugins-dist/schedule/index.js +229 -247
  147. package/plugins-dist/schedule/store.js +93 -109
  148. package/plugins-dist/scratch/index.js +12 -18
  149. package/plugins-dist/screenshot/browser.js +136 -154
  150. package/plugins-dist/screenshot/index.js +37 -46
  151. package/plugins-dist/screenshot/sys.js +53 -65
  152. package/plugins-dist/screenshot/tool.js +98 -126
  153. package/plugins-dist/sessions/index.js +149 -117
  154. package/plugins-dist/shell/index.js +115 -135
  155. package/plugins-dist/shell/plugin.json +1 -2
  156. package/plugins-dist/shell/renderers.js +28 -33
  157. package/plugins-dist/shell/service.js +17 -23
  158. package/plugins-dist/shell/shell.js +307 -362
  159. package/plugins-dist/shell/sys.js +64 -79
  160. package/plugins-dist/skills/index.js +193 -211
  161. package/plugins-dist/skills/install.js +122 -149
  162. package/plugins-dist/skills/project.js +65 -73
  163. package/plugins-dist/skills/skills.js +135 -171
  164. package/plugins-dist/steer/index.js +108 -125
  165. package/plugins-dist/suggest/index.js +235 -259
  166. package/plugins-dist/task/index.js +130 -117
  167. package/plugins-dist/task/plugin.json +5 -2
  168. package/plugins-dist/task/style.js +9 -14
  169. package/plugins-dist/task/subagent.js +156 -279
  170. package/plugins-dist/todo/index.js +79 -63
  171. package/plugins-dist/tools/index.js +36 -42
  172. package/plugins-dist/tui/editor.js +145 -160
  173. package/plugins-dist/tui/frontend.js +639 -678
  174. package/plugins-dist/tui/history.js +34 -37
  175. package/plugins-dist/tui/index.js +16 -18
  176. package/plugins-dist/tui/markdown.js +203 -247
  177. package/plugins-dist/tui/mascot.js +266 -355
  178. package/plugins-dist/tui/mentions.js +67 -144
  179. package/plugins-dist/tui/plugin.json +1 -2
  180. package/plugins-dist/tui/render.js +35 -45
  181. package/plugins-dist/tui/screen.js +197 -180
  182. package/plugins-dist/tui/transcript.js +436 -468
  183. package/plugins-dist/tui/tui.js +1554 -1656
  184. package/plugins-dist/undo/checkpoint.js +95 -118
  185. package/plugins-dist/undo/index.js +185 -192
  186. package/plugins-dist/undo/snapshot.js +83 -99
  187. package/plugins-dist/undo/style.js +9 -14
  188. package/plugins-dist/update/index.js +26 -28
  189. package/plugins-dist/web/env.js +55 -68
  190. package/plugins-dist/web/index.js +80 -110
  191. package/plugins-dist/web/plugin.json +1 -2
  192. package/plugins-dist/web/renderers.js +4 -5
  193. package/plugins-dist/web/web.js +514 -593
  194. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  195. package/sdk/python/jade_plugin.py +9 -2
  196. package/src/api.ts +1 -1
  197. package/src/host.ts +39 -4
  198. package/src/remote.ts +6 -1
  199. package/src/session.ts +489 -34
@@ -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,161 @@ 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
+ await ctx.effect(() => {
259
+ const timer = setInterval(() => void check(), every);
260
+ return () => clearInterval(timer);
261
+ });
262
+ }
281
263
  });