@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.
- package/README.md +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +42 -9
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +80 -50
- package/plugins/task/plugin.json +6 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -418
- package/plugins-dist/approval/policy.js +391 -508
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -253
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +86 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -117
- package/plugins-dist/task/plugin.json +5 -2
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -355
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- 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
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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})]
|
|
105
|
-
|
|
94
|
+
const messageText = (t) => `[Scheduled task "${t.title}" (${t.id})]
|
|
95
|
+
${t.prompt}`;
|
|
106
96
|
export function delivery(t) {
|
|
107
|
-
|
|
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) =>
|
|
106
|
+
const err = (e) => e instanceof Error ? e.message : String(e);
|
|
117
107
|
function tools(store, cwd) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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
|
});
|