@keyou007/agent-bot 0.1.25-alpha.0 → 0.1.25-alpha.2

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 (103) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +60 -5
  3. package/README.zh.md +60 -5
  4. package/config.example.yaml +1 -3
  5. package/dist/cli/controlProtocol.js.map +1 -1
  6. package/dist/cli/help.js +14 -0
  7. package/dist/cli/help.js.map +1 -1
  8. package/dist/cli.js +32 -0
  9. package/dist/cli.js.map +1 -1
  10. package/dist/codex/CodexRuntime.js +15 -5
  11. package/dist/codex/CodexRuntime.js.map +1 -1
  12. package/dist/commands/CommandRouter.js +7 -0
  13. package/dist/commands/CommandRouter.js.map +1 -1
  14. package/dist/config/SettingsStore.js +341 -0
  15. package/dist/config/SettingsStore.js.map +1 -0
  16. package/dist/config/loadConfig.js +1 -2
  17. package/dist/config/loadConfig.js.map +1 -1
  18. package/dist/config/schema.js +0 -1
  19. package/dist/config/schema.js.map +1 -1
  20. package/dist/console/ConsoleTurnPresenter.js +3 -0
  21. package/dist/console/ConsoleTurnPresenter.js.map +1 -1
  22. package/dist/feishu/CardRenderer.js +32 -96
  23. package/dist/feishu/CardRenderer.js.map +1 -1
  24. package/dist/feishu/FeishuConnector.js +9 -0
  25. package/dist/feishu/FeishuConnector.js.map +1 -1
  26. package/dist/feishu/FeishuMessageClient.js +35 -14
  27. package/dist/feishu/FeishuMessageClient.js.map +1 -1
  28. package/dist/feishu/FeishuTurnPresenter.js +36 -23
  29. package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
  30. package/dist/feishu/LocalCardImage.js.map +1 -1
  31. package/dist/feishu/LocalImageMarkdown.js +59 -3
  32. package/dist/feishu/LocalImageMarkdown.js.map +1 -1
  33. package/dist/feishu/SettingsCard.js +199 -0
  34. package/dist/feishu/SettingsCard.js.map +1 -0
  35. package/dist/index.js +17 -6
  36. package/dist/index.js.map +1 -1
  37. package/dist/local-files/TurnPreviewPage.js +8 -4
  38. package/dist/local-files/TurnPreviewPage.js.map +1 -1
  39. package/dist/presentation/TurnStateReducer.js +21 -2
  40. package/dist/presentation/TurnStateReducer.js.map +1 -1
  41. package/dist/presentation/turnStatus.js +34 -0
  42. package/dist/presentation/turnStatus.js.map +1 -0
  43. package/dist/proxy/ProxySessionController.js +688 -153
  44. package/dist/proxy/ProxySessionController.js.map +1 -1
  45. package/dist/runtime/submissionErrors.js +15 -0
  46. package/dist/runtime/submissionErrors.js.map +1 -0
  47. package/dist/scheduling/ScheduleCards.js +240 -0
  48. package/dist/scheduling/ScheduleCards.js.map +1 -0
  49. package/dist/scheduling/ScheduleCommand.js +67 -0
  50. package/dist/scheduling/ScheduleCommand.js.map +1 -0
  51. package/dist/scheduling/ScheduledTaskStore.js +226 -0
  52. package/dist/scheduling/ScheduledTaskStore.js.map +1 -0
  53. package/dist/scheduling/TaskScheduler.js +132 -0
  54. package/dist/scheduling/TaskScheduler.js.map +1 -0
  55. package/dist/scheduling/types.js +89 -0
  56. package/dist/scheduling/types.js.map +1 -0
  57. package/dist/state/ExecutionProvenanceStore.js +58 -0
  58. package/dist/state/ExecutionProvenanceStore.js.map +1 -0
  59. package/dist/state/StateStore.js +145 -45
  60. package/dist/state/StateStore.js.map +1 -1
  61. package/dist/state/migrations.js +37 -0
  62. package/dist/state/migrations.js.map +1 -1
  63. package/dist/supervision/SafeRestartNotifier.js +1 -1
  64. package/dist/supervision/SafeRestartNotifier.js.map +1 -1
  65. package/docs/technical-reference.md +11 -8
  66. package/docs/technical-reference.zh.md +11 -8
  67. package/npm-shrinkwrap.json +24 -2
  68. package/package.json +2 -1
  69. package/skills/agent-bot/SKILL.md +33 -3
  70. package/src/cli/controlProtocol.ts +1 -0
  71. package/src/cli/help.ts +14 -0
  72. package/src/cli.ts +25 -0
  73. package/src/codex/CodexRuntime.ts +14 -5
  74. package/src/commands/CommandRouter.ts +7 -0
  75. package/src/commands/commandTypes.ts +2 -0
  76. package/src/config/SettingsStore.ts +588 -0
  77. package/src/config/loadConfig.ts +1 -2
  78. package/src/config/schema.ts +0 -1
  79. package/src/console/ConsoleTurnPresenter.ts +2 -0
  80. package/src/feishu/CardRenderer.ts +33 -110
  81. package/src/feishu/FeishuConnector.ts +9 -0
  82. package/src/feishu/FeishuMessageClient.ts +39 -17
  83. package/src/feishu/FeishuTurnPresenter.ts +33 -22
  84. package/src/feishu/LocalCardImage.ts +0 -1
  85. package/src/feishu/LocalImageMarkdown.ts +54 -3
  86. package/src/feishu/SettingsCard.ts +231 -0
  87. package/src/index.ts +17 -6
  88. package/src/local-files/TurnPreviewPage.ts +8 -4
  89. package/src/presentation/TurnStateReducer.ts +21 -2
  90. package/src/presentation/turnStatus.ts +38 -0
  91. package/src/presentation/turnViewTypes.ts +3 -0
  92. package/src/proxy/ProxySessionController.ts +628 -152
  93. package/src/runtime/submissionErrors.ts +15 -0
  94. package/src/runtime/types.ts +1 -0
  95. package/src/scheduling/ScheduleCards.ts +317 -0
  96. package/src/scheduling/ScheduleCommand.ts +76 -0
  97. package/src/scheduling/ScheduledTaskStore.ts +332 -0
  98. package/src/scheduling/TaskScheduler.ts +171 -0
  99. package/src/scheduling/types.ts +151 -0
  100. package/src/state/ExecutionProvenanceStore.ts +100 -0
  101. package/src/state/StateStore.ts +167 -44
  102. package/src/state/migrations.ts +37 -0
  103. package/src/supervision/SafeRestartNotifier.ts +1 -1
@@ -0,0 +1,15 @@
1
+ // Only raise this before dispatching a prompt-bearing request. A timeout,
2
+ // disconnect, or generic provider rejection is not evidence of non-submission.
3
+ export class TurnNotSubmittedError extends Error {
4
+ constructor(cause: unknown) {
5
+ super(cause instanceof Error ? cause.message : String(cause), { cause });
6
+ this.name = "TurnNotSubmittedError";
7
+ }
8
+ }
9
+
10
+ export class TurnStoppedBeforeSubmissionError extends TurnNotSubmittedError {
11
+ constructor() {
12
+ super(new Error("本次执行已停止,未提交请求。"));
13
+ this.name = "TurnStoppedBeforeSubmissionError";
14
+ }
15
+ }
@@ -113,6 +113,7 @@ export type AgentEvent =
113
113
  | { type: "approval_resolved"; sessionId: string; turnId: string; requestId: string; decision: ApprovalDecision; automatic?: boolean }
114
114
  | { type: "turn_completed"; sessionId: string; turnId: string; finalResponse: string; durationMs?: number; approvedPlan?: boolean }
115
115
  | { type: "turn_cancelled"; sessionId: string; turnId: string }
116
+ | { type: "turn_status_unknown"; sessionId: string; turnId: string; message: string }
116
117
  | { type: "turn_failed"; sessionId: string; turnId: string; message: string };
117
118
 
118
119
  export interface SessionMetadataUpdatedEvent {
@@ -0,0 +1,317 @@
1
+ import type { ScheduledJob, ScheduledRun } from "./types.js";
2
+ import { scheduleTime, nextScheduleTime } from "./types.js";
3
+
4
+ const labels: Record<string, string> = {
5
+ draft: "待确认",
6
+ enabled: "已启用",
7
+ paused: "已暂停",
8
+ completed: "已完成",
9
+ deleted: "已删除",
10
+ queued: "排队中",
11
+ dispatching: "准备中",
12
+ submitting: "提交中",
13
+ running: "运行中",
14
+ failed: "失败",
15
+ cancelled: "已取消",
16
+ expired: "已过期",
17
+ skipped: "已跳过",
18
+ unknown: "待核实",
19
+ };
20
+ const md = (content: string) => ({ tag: "markdown", content });
21
+ const planStatus = (job: ScheduledJob) =>
22
+ job.status === "completed" ? "已触发" : labels[job.status];
23
+ const escape = (value: string) =>
24
+ value
25
+ .replaceAll("&", "&amp;")
26
+ .replaceAll("<", "&lt;")
27
+ .replaceAll(">", "&gt;");
28
+ const card = (title: string, elements: unknown[]) => ({
29
+ schema: "2.0",
30
+ config: { update_multi: true, width_mode: "fill" },
31
+ header: { template: "blue", title: { tag: "plain_text", content: title } },
32
+ body: { elements },
33
+ });
34
+ const action = (
35
+ text: string,
36
+ value: Record<string, string>,
37
+ type = "default",
38
+ ) => ({
39
+ tag: "button",
40
+ type,
41
+ text: { tag: "plain_text", content: text },
42
+ value,
43
+ });
44
+ const row = (elements: unknown[]) => ({
45
+ tag: "column_set",
46
+ flex_mode: "flow",
47
+ columns: elements.map((element) => ({
48
+ tag: "column",
49
+ width: "auto",
50
+ elements: [element],
51
+ })),
52
+ });
53
+ const navigationLink = (text: string, value: Record<string, string>) => ({
54
+ tag: "interactive_container",
55
+ margin: "0px",
56
+ padding: "4px 0px",
57
+ has_border: false,
58
+ elements: [
59
+ {
60
+ ...md(`<font color='blue'>${text}</font>`),
61
+ text_size: "notation",
62
+ },
63
+ ],
64
+ behaviors: [{ type: "callback", value }],
65
+ });
66
+
67
+ export function renderScheduleList(
68
+ jobs: ScheduledJob[],
69
+ contextKey: string,
70
+ page: number,
71
+ ): Record<string, unknown> {
72
+ const elements: unknown[] = jobs.length ? [] : [md("暂无定时任务")];
73
+ for (const job of jobs.slice(0, 10)) {
74
+ elements.push(
75
+ md(
76
+ `**${escape(job.spec.name)}** · ${planStatus(job)}\n${job.id}\n${escape(job.spec.cron ?? job.spec.at!)} · ${escape(job.spec.timeZone)}\n下次:${scheduleTime(job.nextRunAt, job.spec.timeZone)}`,
77
+ ),
78
+ row([
79
+ action("查看", {
80
+ action: "schedule_show",
81
+ scheduleId: job.id,
82
+ contextKey,
83
+ }),
84
+ ]),
85
+ { tag: "hr" },
86
+ );
87
+ }
88
+ elements.push(
89
+ row([
90
+ action("新建", { action: "schedule_new", contextKey }),
91
+ ...(page > 0
92
+ ? [
93
+ action("上一页", {
94
+ action: "schedule_list",
95
+ contextKey,
96
+ page: String(page - 1),
97
+ }),
98
+ ]
99
+ : []),
100
+ ...(jobs.length > 10
101
+ ? [
102
+ action("下一页", {
103
+ action: "schedule_list",
104
+ contextKey,
105
+ page: String(page + 1),
106
+ }),
107
+ ]
108
+ : []),
109
+ action("刷新", {
110
+ action: "schedule_list",
111
+ contextKey,
112
+ page: String(page),
113
+ }),
114
+ ]),
115
+ );
116
+ return card("定时任务", elements);
117
+ }
118
+
119
+ export function renderSchedule(
120
+ job: ScheduledJob,
121
+ title: string,
122
+ runs: ScheduledRun[],
123
+ history = false,
124
+ ): Record<string, unknown> {
125
+ const value = {
126
+ scheduleId: job.id,
127
+ revision: String(job.revision),
128
+ contextKey: job.contextKey,
129
+ };
130
+ const button = (text: string, operation: string, type = "default") =>
131
+ action(text, { ...value, action: `schedule_${operation}` }, type);
132
+ const tab = (text: string, operation: string, active: boolean) =>
133
+ active
134
+ ? { ...md(`**${text}**`), text_size: "notation" }
135
+ : navigationLink(text, { ...value, action: `schedule_${operation}` });
136
+ const elements: unknown[] = [
137
+ md(
138
+ `**${escape(job.spec.name)}** · ${planStatus(job)}\n任务:${escape(title)}`,
139
+ ),
140
+ {
141
+ ...row([
142
+ navigationLink("返回列表", {
143
+ action: "schedule_list",
144
+ contextKey: job.contextKey,
145
+ }),
146
+ tab("计划详情", "show", !history),
147
+ tab("执行记录", "history", history),
148
+ navigationLink("刷新", {
149
+ ...value,
150
+ action: history ? "schedule_history" : "schedule_show",
151
+ }),
152
+ ]),
153
+ horizontal_spacing: "12px",
154
+ vertical_align: "center",
155
+ },
156
+ { tag: "hr" },
157
+ ];
158
+ if (!history) {
159
+ const time = job.spec.at
160
+ ? `执行时间:${scheduleTime(Date.parse(job.spec.at), job.spec.timeZone)}`
161
+ : `周期:${escape(job.spec.cron!)}\n下次执行:${scheduleTime(job.nextRunAt ?? (job.status === "draft" ? nextScheduleTime(job.spec, Date.now()) : undefined), job.spec.timeZone)}`;
162
+ elements.push(
163
+ md(`${time}\n时区:${escape(job.spec.timeZone)}`),
164
+ {
165
+ ...md(
166
+ `最晚开始:计划时间后 ${job.spec.maxDelayMinutes} 分钟 · ${job.spec.catchUpMinutes ? `错过 ${job.spec.catchUpMinutes} 分钟内补跑最近一次` : "错过不补跑"}`,
167
+ ),
168
+ text_size: "notation",
169
+ },
170
+ md(`**Prompt**\n${escape(job.spec.prompt)}`),
171
+ );
172
+ }
173
+ if (job.notice) elements.push(md(escape(job.notice)));
174
+ if (!history && runs[0])
175
+ elements.push(
176
+ md(
177
+ `最近运行:${labels[runs[0].status]} · ${scheduleTime(runs[0].scheduledFor, job.spec.timeZone)}`,
178
+ ),
179
+ );
180
+ if (history) {
181
+ elements.push({ ...md("最近 10 次运行"), text_size: "notation" });
182
+ for (const [index, run] of runs.entries()) {
183
+ if (index > 0) elements.push({ tag: "hr" });
184
+ elements.push(
185
+ md(
186
+ `${run.manual ? "手动" : "计划"} ${scheduleTime(run.scheduledFor, job.spec.timeZone)} · ${labels[run.status]}\n实际开始:${scheduleTime(run.startedAt, job.spec.timeZone)}${run.turnId ? `\nTurn:${run.turnId}` : ""}${run.detail ? `\n${escape(run.detail)}` : ""}`,
187
+ ),
188
+ );
189
+ const controls: unknown[] = [];
190
+ if (run.turnId)
191
+ controls.push(
192
+ action("查看运行详情", {
193
+ action: "turn_details",
194
+ turnId: run.turnId,
195
+ contextKey: job.contextKey,
196
+ }),
197
+ );
198
+ if (run.status === "unknown")
199
+ controls.push({
200
+ ...action("已核实结束", {
201
+ ...value,
202
+ action: "schedule_resolve",
203
+ runId: run.id,
204
+ }),
205
+ confirm: {
206
+ title: { tag: "plain_text", content: "确认本次运行已结束?" },
207
+ text: {
208
+ tag: "plain_text",
209
+ content:
210
+ "仅在核实 Agent 已停止或完成后确认。这不会停止 Agent,只会解除后续调度阻塞。",
211
+ },
212
+ },
213
+ });
214
+ if (controls.length) elements.push(row(controls));
215
+ }
216
+ if (!runs.length) elements.push(md("暂无执行记录"));
217
+ }
218
+ if (!history && job.status !== "deleted") {
219
+ elements.push(
220
+ { tag: "hr" },
221
+ row([
222
+ ...(job.status === "draft"
223
+ ? [button("确认启用", "confirm", "primary")]
224
+ : []),
225
+ ...(job.status === "enabled" ? [button("暂停", "pause")] : []),
226
+ ...(job.status === "paused" ? [button("恢复", "resume")] : []),
227
+ ...(["enabled", "paused", "completed"].includes(job.status)
228
+ ? [button("立即执行", "run")]
229
+ : []),
230
+ button("编辑", "edit"),
231
+ {
232
+ ...button("删除", "delete", "danger"),
233
+ confirm: {
234
+ title: { tag: "plain_text", content: "删除定时计划?" },
235
+ text: {
236
+ tag: "plain_text",
237
+ content:
238
+ "取消尚未开始的运行,保留执行记录;不会停止正在执行的任务。",
239
+ },
240
+ },
241
+ },
242
+ ]),
243
+ );
244
+ }
245
+ elements.push({ ...md(`计划 ID:${job.id}`), text_size: "notation" });
246
+ return card("定时任务", elements);
247
+ }
248
+
249
+ export function renderScheduleForm(
250
+ contextKey: string,
251
+ job?: ScheduledJob,
252
+ localSessionId?: string,
253
+ ): Record<string, unknown> {
254
+ const field = (
255
+ name: string,
256
+ label: string,
257
+ value: string,
258
+ required = false,
259
+ maxLength = 1000,
260
+ ) => ({
261
+ tag: "input",
262
+ name,
263
+ required,
264
+ input_type: "text",
265
+ width: "fill",
266
+ max_length: maxLength,
267
+ label: { tag: "plain_text", content: label },
268
+ default_value: value,
269
+ });
270
+ return card(job ? "编辑定时任务" : "新建定时任务", [
271
+ {
272
+ tag: "form",
273
+ name: "schedule_form",
274
+ elements: [
275
+ field("name", "名称", job?.spec.name ?? "定时任务", true, 120),
276
+ field("prompt", "Prompt", job?.spec.prompt ?? "", true),
277
+ field("cron", "Cron(五段,与执行时间二选一)", job?.spec.cron ?? ""),
278
+ field("at", "执行时间(ISO 格式,含时区)", job?.spec.at ?? ""),
279
+ field(
280
+ "timeZone",
281
+ "时区",
282
+ job?.spec.timeZone ??
283
+ Intl.DateTimeFormat().resolvedOptions().timeZone,
284
+ true,
285
+ ),
286
+ field(
287
+ "maxDelayMinutes",
288
+ "最长等待(分钟)",
289
+ String(job?.spec.maxDelayMinutes ?? 60),
290
+ true,
291
+ ),
292
+ field(
293
+ "catchUpMinutes",
294
+ "错过补跑窗口(分钟,0 为不补跑)",
295
+ String(job?.spec.catchUpMinutes ?? 0),
296
+ true,
297
+ ),
298
+ {
299
+ ...action(
300
+ "保存草稿",
301
+ {
302
+ action: "schedule_submit",
303
+ contextKey,
304
+ ...(job
305
+ ? { scheduleId: job.id, revision: String(job.revision) }
306
+ : { sessionId: localSessionId! }),
307
+ },
308
+ "primary",
309
+ ),
310
+ name: "schedule_submit",
311
+ action_type: "form_submit",
312
+ },
313
+ ],
314
+ },
315
+ row([action("取消", { action: "schedule_list", contextKey })]),
316
+ ]);
317
+ }
@@ -0,0 +1,76 @@
1
+ import type {
2
+ ScheduleCommand,
3
+ ScheduleInput,
4
+ ScheduleOperation,
5
+ } from "./types.js";
6
+
7
+ const operations = new Set<ScheduleOperation>([
8
+ "list",
9
+ "add",
10
+ "edit",
11
+ "show",
12
+ "history",
13
+ "pause",
14
+ "resume",
15
+ "run",
16
+ "delete",
17
+ ]);
18
+
19
+ export function parseScheduleCommand(args: string[]): ScheduleCommand {
20
+ const [operation = "list", ...rest] = args;
21
+ if (!operations.has(operation as ScheduleOperation))
22
+ throw new Error(
23
+ "定时任务操作:list、add、edit、show、history、pause、resume、run、delete。",
24
+ );
25
+ if (operation === "add" || operation === "edit") {
26
+ const id = operation === "edit" ? rest.shift() : undefined;
27
+ if (operation === "edit" && (!id || id.startsWith("--")))
28
+ throw new Error("请指定计划 ID。");
29
+ const values: Record<string, string> = {};
30
+ const allowed = new Set([
31
+ "--name",
32
+ "--prompt",
33
+ "--cron",
34
+ "--at",
35
+ "--timezone",
36
+ "--max-delay",
37
+ "--catch-up",
38
+ ]);
39
+ for (let index = 0; index < rest.length; index += 2) {
40
+ const key = rest[index]!;
41
+ const value = rest[index + 1];
42
+ if (
43
+ !allowed.has(key) ||
44
+ value === undefined ||
45
+ value.startsWith("--") ||
46
+ values[key] !== undefined
47
+ ) {
48
+ throw new Error(`无效或重复的定时任务参数:${key}`);
49
+ }
50
+ values[key] = value;
51
+ }
52
+ const input: ScheduleInput = {
53
+ prompt: values["--prompt"] ?? "",
54
+ ...(values["--name"] !== undefined ? { name: values["--name"] } : {}),
55
+ ...(values["--cron"] !== undefined ? { cron: values["--cron"] } : {}),
56
+ ...(values["--at"] !== undefined ? { at: values["--at"] } : {}),
57
+ ...(values["--timezone"] !== undefined
58
+ ? { timeZone: values["--timezone"] }
59
+ : {}),
60
+ ...(values["--max-delay"] !== undefined
61
+ ? { maxDelayMinutes: Number(values["--max-delay"]) }
62
+ : {}),
63
+ ...(values["--catch-up"] !== undefined
64
+ ? { catchUpMinutes: Number(values["--catch-up"]) }
65
+ : {}),
66
+ };
67
+ return { operation, id, input };
68
+ }
69
+ if (operation === "list") {
70
+ if (rest.length) throw new Error("schedule list 不接受位置参数。");
71
+ return { operation };
72
+ }
73
+ if (rest.length !== 1 || !rest[0] || rest[0].startsWith("--"))
74
+ throw new Error("请指定一个计划 ID。");
75
+ return { operation: operation as ScheduleOperation, id: rest[0] };
76
+ }