@zhin.js/core 1.0.36 → 1.0.38

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 (196) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +57 -3
  3. package/lib/adapter.d.ts +11 -0
  4. package/lib/adapter.d.ts.map +1 -1
  5. package/lib/adapter.js +61 -0
  6. package/lib/adapter.js.map +1 -1
  7. package/lib/ai/index.d.ts +3 -39
  8. package/lib/ai/index.d.ts.map +1 -1
  9. package/lib/ai/index.js +2 -44
  10. package/lib/ai/index.js.map +1 -1
  11. package/lib/ai/types.d.ts +4 -3
  12. package/lib/ai/types.d.ts.map +1 -1
  13. package/lib/built/ai-trigger.js.map +1 -1
  14. package/lib/built/common-adapter-tools.d.ts +55 -0
  15. package/lib/built/common-adapter-tools.d.ts.map +1 -0
  16. package/lib/built/common-adapter-tools.js +158 -0
  17. package/lib/built/common-adapter-tools.js.map +1 -0
  18. package/lib/built/dispatcher.d.ts.map +1 -1
  19. package/lib/built/dispatcher.js +50 -46
  20. package/lib/built/dispatcher.js.map +1 -1
  21. package/lib/built/skill.d.ts.map +1 -1
  22. package/lib/built/skill.js +0 -1
  23. package/lib/built/skill.js.map +1 -1
  24. package/lib/built/tool.d.ts +3 -3
  25. package/lib/built/tool.d.ts.map +1 -1
  26. package/lib/built/tool.js.map +1 -1
  27. package/lib/feature.d.ts +16 -1
  28. package/lib/feature.d.ts.map +1 -1
  29. package/lib/feature.js +41 -2
  30. package/lib/feature.js.map +1 -1
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.d.ts.map +1 -1
  33. package/lib/index.js +2 -0
  34. package/lib/index.js.map +1 -1
  35. package/lib/plugin.d.ts +38 -1
  36. package/lib/plugin.d.ts.map +1 -1
  37. package/lib/plugin.js +73 -22
  38. package/lib/plugin.js.map +1 -1
  39. package/lib/scheduler/scheduler.js +1 -1
  40. package/lib/scheduler/scheduler.js.map +1 -1
  41. package/lib/types.d.ts +43 -28
  42. package/lib/types.d.ts.map +1 -1
  43. package/lib/utils.d.ts +12 -3
  44. package/lib/utils.d.ts.map +1 -1
  45. package/lib/utils.js +64 -54
  46. package/lib/utils.js.map +1 -1
  47. package/package.json +5 -5
  48. package/src/adapter.ts +85 -5
  49. package/src/ai/index.ts +8 -186
  50. package/src/ai/types.ts +5 -4
  51. package/src/built/ai-trigger.ts +2 -2
  52. package/src/built/common-adapter-tools.ts +207 -0
  53. package/src/built/dispatcher.ts +51 -52
  54. package/src/built/skill.ts +3 -4
  55. package/src/built/tool.ts +3 -3
  56. package/src/feature.ts +45 -2
  57. package/src/index.ts +2 -0
  58. package/src/plugin.ts +92 -31
  59. package/src/scheduler/scheduler.ts +1 -1
  60. package/src/types.ts +39 -28
  61. package/src/utils.ts +63 -52
  62. package/tests/ai/setup.ts +2 -2
  63. package/tests/utils.test.ts +1 -3
  64. package/lib/ai/agent.d.ts +0 -130
  65. package/lib/ai/agent.d.ts.map +0 -1
  66. package/lib/ai/agent.js +0 -684
  67. package/lib/ai/agent.js.map +0 -1
  68. package/lib/ai/bootstrap.d.ts +0 -91
  69. package/lib/ai/bootstrap.d.ts.map +0 -1
  70. package/lib/ai/bootstrap.js +0 -243
  71. package/lib/ai/bootstrap.js.map +0 -1
  72. package/lib/ai/builtin-tools.d.ts +0 -59
  73. package/lib/ai/builtin-tools.d.ts.map +0 -1
  74. package/lib/ai/builtin-tools.js +0 -777
  75. package/lib/ai/builtin-tools.js.map +0 -1
  76. package/lib/ai/compaction.d.ts +0 -132
  77. package/lib/ai/compaction.d.ts.map +0 -1
  78. package/lib/ai/compaction.js +0 -370
  79. package/lib/ai/compaction.js.map +0 -1
  80. package/lib/ai/context-manager.d.ts +0 -213
  81. package/lib/ai/context-manager.d.ts.map +0 -1
  82. package/lib/ai/context-manager.js +0 -313
  83. package/lib/ai/context-manager.js.map +0 -1
  84. package/lib/ai/conversation-memory.d.ts +0 -181
  85. package/lib/ai/conversation-memory.d.ts.map +0 -1
  86. package/lib/ai/conversation-memory.js +0 -581
  87. package/lib/ai/conversation-memory.js.map +0 -1
  88. package/lib/ai/cron-engine.d.ts +0 -92
  89. package/lib/ai/cron-engine.d.ts.map +0 -1
  90. package/lib/ai/cron-engine.js +0 -278
  91. package/lib/ai/cron-engine.js.map +0 -1
  92. package/lib/ai/follow-up.d.ts +0 -131
  93. package/lib/ai/follow-up.d.ts.map +0 -1
  94. package/lib/ai/follow-up.js +0 -265
  95. package/lib/ai/follow-up.js.map +0 -1
  96. package/lib/ai/hooks.d.ts +0 -143
  97. package/lib/ai/hooks.d.ts.map +0 -1
  98. package/lib/ai/hooks.js +0 -108
  99. package/lib/ai/hooks.js.map +0 -1
  100. package/lib/ai/init.d.ts +0 -30
  101. package/lib/ai/init.d.ts.map +0 -1
  102. package/lib/ai/init.js +0 -686
  103. package/lib/ai/init.js.map +0 -1
  104. package/lib/ai/output.d.ts +0 -93
  105. package/lib/ai/output.d.ts.map +0 -1
  106. package/lib/ai/output.js +0 -176
  107. package/lib/ai/output.js.map +0 -1
  108. package/lib/ai/rate-limiter.d.ts +0 -38
  109. package/lib/ai/rate-limiter.d.ts.map +0 -1
  110. package/lib/ai/rate-limiter.js +0 -86
  111. package/lib/ai/rate-limiter.js.map +0 -1
  112. package/lib/ai/service.d.ts +0 -88
  113. package/lib/ai/service.d.ts.map +0 -1
  114. package/lib/ai/service.js +0 -285
  115. package/lib/ai/service.js.map +0 -1
  116. package/lib/ai/session.d.ts +0 -186
  117. package/lib/ai/session.d.ts.map +0 -1
  118. package/lib/ai/session.js +0 -443
  119. package/lib/ai/session.js.map +0 -1
  120. package/lib/ai/subagent.d.ts +0 -50
  121. package/lib/ai/subagent.d.ts.map +0 -1
  122. package/lib/ai/subagent.js +0 -144
  123. package/lib/ai/subagent.js.map +0 -1
  124. package/lib/ai/tone-detector.d.ts +0 -19
  125. package/lib/ai/tone-detector.d.ts.map +0 -1
  126. package/lib/ai/tone-detector.js +0 -72
  127. package/lib/ai/tone-detector.js.map +0 -1
  128. package/lib/ai/tools.d.ts +0 -45
  129. package/lib/ai/tools.d.ts.map +0 -1
  130. package/lib/ai/tools.js +0 -206
  131. package/lib/ai/tools.js.map +0 -1
  132. package/lib/ai/user-profile.d.ts +0 -56
  133. package/lib/ai/user-profile.d.ts.map +0 -1
  134. package/lib/ai/user-profile.js +0 -130
  135. package/lib/ai/user-profile.js.map +0 -1
  136. package/lib/ai/zhin-agent/builtin-tools.d.ts +0 -17
  137. package/lib/ai/zhin-agent/builtin-tools.d.ts.map +0 -1
  138. package/lib/ai/zhin-agent/builtin-tools.js +0 -220
  139. package/lib/ai/zhin-agent/builtin-tools.js.map +0 -1
  140. package/lib/ai/zhin-agent/config.d.ts +0 -54
  141. package/lib/ai/zhin-agent/config.d.ts.map +0 -1
  142. package/lib/ai/zhin-agent/config.js +0 -76
  143. package/lib/ai/zhin-agent/config.js.map +0 -1
  144. package/lib/ai/zhin-agent/exec-policy.d.ts +0 -20
  145. package/lib/ai/zhin-agent/exec-policy.d.ts.map +0 -1
  146. package/lib/ai/zhin-agent/exec-policy.js +0 -71
  147. package/lib/ai/zhin-agent/exec-policy.js.map +0 -1
  148. package/lib/ai/zhin-agent/index.d.ts +0 -70
  149. package/lib/ai/zhin-agent/index.d.ts.map +0 -1
  150. package/lib/ai/zhin-agent/index.js +0 -404
  151. package/lib/ai/zhin-agent/index.js.map +0 -1
  152. package/lib/ai/zhin-agent/prompt.d.ts +0 -21
  153. package/lib/ai/zhin-agent/prompt.d.ts.map +0 -1
  154. package/lib/ai/zhin-agent/prompt.js +0 -111
  155. package/lib/ai/zhin-agent/prompt.js.map +0 -1
  156. package/lib/ai/zhin-agent/tool-collector.d.ts +0 -22
  157. package/lib/ai/zhin-agent/tool-collector.d.ts.map +0 -1
  158. package/lib/ai/zhin-agent/tool-collector.js +0 -218
  159. package/lib/ai/zhin-agent/tool-collector.js.map +0 -1
  160. package/src/ai/agent.ts +0 -812
  161. package/src/ai/bootstrap.ts +0 -309
  162. package/src/ai/builtin-tools.ts +0 -849
  163. package/src/ai/compaction.ts +0 -529
  164. package/src/ai/context-manager.ts +0 -440
  165. package/src/ai/conversation-memory.ts +0 -774
  166. package/src/ai/cron-engine.ts +0 -337
  167. package/src/ai/follow-up.ts +0 -357
  168. package/src/ai/hooks.ts +0 -223
  169. package/src/ai/init.ts +0 -762
  170. package/src/ai/output.ts +0 -261
  171. package/src/ai/rate-limiter.ts +0 -129
  172. package/src/ai/service.ts +0 -331
  173. package/src/ai/session.ts +0 -544
  174. package/src/ai/subagent.ts +0 -209
  175. package/src/ai/tone-detector.ts +0 -89
  176. package/src/ai/tools.ts +0 -218
  177. package/src/ai/user-profile.ts +0 -181
  178. package/src/ai/zhin-agent/builtin-tools.ts +0 -247
  179. package/src/ai/zhin-agent/config.ts +0 -113
  180. package/src/ai/zhin-agent/exec-policy.ts +0 -78
  181. package/src/ai/zhin-agent/index.ts +0 -512
  182. package/src/ai/zhin-agent/prompt.ts +0 -131
  183. package/src/ai/zhin-agent/tool-collector.ts +0 -243
  184. package/tests/ai/agent.test.ts +0 -614
  185. package/tests/ai/context-manager.test.ts +0 -413
  186. package/tests/ai/conversation-memory.test.ts +0 -128
  187. package/tests/ai/follow-up.test.ts +0 -175
  188. package/tests/ai/integration.test.ts +0 -584
  189. package/tests/ai/output.test.ts +0 -128
  190. package/tests/ai/rate-limiter.test.ts +0 -108
  191. package/tests/ai/session.test.ts +0 -375
  192. package/tests/ai/subagent.test.ts +0 -270
  193. package/tests/ai/tone-detector.test.ts +0 -80
  194. package/tests/ai/tools-builtin.test.ts +0 -346
  195. package/tests/ai/user-profile.test.ts +0 -73
  196. package/tests/ai/zhin-agent.test.ts +0 -177
@@ -1,337 +0,0 @@
1
- /**
2
- * 持久化定时任务
3
- *
4
- * 将定时任务持久化到 data/cron-jobs.json,进程重启后自动加载;
5
- * 触发时以 prompt 调用 ZhinAgent,实现「到点执行 AI 任务」。
6
- *
7
- * - 存储:id, cronExpression, prompt, label?, enabled, createdAt
8
- * - 启动时:读取文件 → 为每条启用的任务创建 Cron → 注册到 CronFeature
9
- * - CLI / AI 工具:可对持久化任务做 list / add / remove / pause / resume(AI 侧立即生效)
10
- */
11
-
12
- import * as fs from 'fs';
13
- import * as path from 'path';
14
- import { Cron } from '../cron.js';
15
- import { Logger } from '@zhin.js/logger';
16
- import { ZhinTool } from '../built/tool.js';
17
-
18
- const logger = new Logger(null, 'cron-engine');
19
-
20
- // ─────────────────────────────────────────────────────────────────────────────
21
- // 类型与存储路径
22
- // ─────────────────────────────────────────────────────────────────────────────
23
-
24
- export const CRON_JOBS_FILENAME = 'cron-jobs.json';
25
-
26
- export interface CronJobRecord {
27
- id: string;
28
- /** Cron 表达式,5 字段:分 时 日 月 周 */
29
- cronExpression: string;
30
- /** 触发时发给 AI 的 prompt */
31
- prompt: string;
32
- /** 可选标签,便于识别 */
33
- label?: string;
34
- /** 是否启用(暂停的任务不加载) */
35
- enabled: boolean;
36
- createdAt: number;
37
- }
38
-
39
- export function getCronJobsFilePath(dataDir: string): string {
40
- return path.join(dataDir, CRON_JOBS_FILENAME);
41
- }
42
-
43
- export async function readCronJobsFile(dataDir: string): Promise<CronJobRecord[]> {
44
- const filePath = getCronJobsFilePath(dataDir);
45
- try {
46
- const raw = await fs.promises.readFile(filePath, 'utf-8');
47
- const data = JSON.parse(raw);
48
- if (!Array.isArray(data)) return [];
49
- return data;
50
- } catch (e: any) {
51
- if (e?.code === 'ENOENT') return [];
52
- logger.warn('读取定时任务文件失败: ' + (e?.message || String(e)));
53
- return [];
54
- }
55
- }
56
-
57
- export async function writeCronJobsFile(dataDir: string, jobs: CronJobRecord[]): Promise<void> {
58
- const filePath = getCronJobsFilePath(dataDir);
59
- await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
60
- await fs.promises.writeFile(filePath, JSON.stringify(jobs, null, 2), 'utf-8');
61
- }
62
-
63
- // ─────────────────────────────────────────────────────────────────────────────
64
- // 持久化引擎:加载文件并注册到 CronFeature
65
- // ─────────────────────────────────────────────────────────────────────────────
66
-
67
- export type CronRunner = (prompt: string, jobId: string) => void | Promise<void>;
68
-
69
- export type AddCronFn = (cron: Cron) => () => void;
70
-
71
- export interface PersistentCronEngineOptions {
72
- dataDir: string;
73
- addCron: AddCronFn;
74
- runner: CronRunner;
75
- }
76
-
77
- export class PersistentCronEngine {
78
- private options: PersistentCronEngineOptions;
79
- /** jobId -> dispose */
80
- private disposes = new Map<string, () => void>();
81
-
82
- constructor(options: PersistentCronEngineOptions) {
83
- this.options = options;
84
- }
85
-
86
- getDataDir(): string {
87
- return this.options.dataDir;
88
- }
89
-
90
- /**
91
- * 从文件加载任务并注册到 CronFeature;仅加载 enabled 的任务。
92
- */
93
- load(): void {
94
- const { dataDir, addCron, runner } = this.options;
95
- readCronJobsFile(dataDir).then((jobs) => {
96
- for (const job of jobs) {
97
- if (!job.enabled) continue;
98
- this.registerOne(job, addCron, runner);
99
- }
100
- if (jobs.filter((j) => j.enabled).length > 0) {
101
- logger.info(`已加载 ${this.disposes.size} 个持久化定时任务`);
102
- }
103
- }).catch((e) => {
104
- logger.warn('加载持久化定时任务失败: ' + (e?.message || String(e)));
105
- });
106
- }
107
-
108
- private registerOne(
109
- job: CronJobRecord,
110
- addCron: AddCronFn,
111
- runner: CronRunner,
112
- ): void {
113
- const { prompt, id: jobId, cronExpression } = job;
114
- try {
115
- const cron = new Cron(cronExpression, async () => {
116
- await runner(prompt, jobId);
117
- });
118
- cron.id = jobId;
119
- const dispose = addCron(cron);
120
- this.disposes.set(jobId, dispose);
121
- } catch (e: any) {
122
- logger.warn(`定时任务加载失败 [${jobId}]: ${e?.message || String(e)}`);
123
- }
124
- }
125
-
126
- /**
127
- * 列出所有持久化任务(从文件读取)
128
- */
129
- async listJobs(): Promise<CronJobRecord[]> {
130
- return readCronJobsFile(this.options.dataDir);
131
- }
132
-
133
- /**
134
- * 添加持久化任务并立即生效
135
- */
136
- async addJob(record: Omit<CronJobRecord, 'createdAt'> & { createdAt?: number }): Promise<CronJobRecord> {
137
- const jobs = await readCronJobsFile(this.options.dataDir);
138
- const full: CronJobRecord = {
139
- ...record,
140
- createdAt: record.createdAt ?? Date.now(),
141
- enabled: record.enabled ?? true,
142
- };
143
- jobs.push(full);
144
- await writeCronJobsFile(this.options.dataDir, jobs);
145
- if (full.enabled) {
146
- this.registerOne(full, this.options.addCron, this.options.runner);
147
- }
148
- return full;
149
- }
150
-
151
- /**
152
- * 删除持久化任务并立即生效
153
- */
154
- async removeJob(id: string): Promise<boolean> {
155
- const jobs = await readCronJobsFile(this.options.dataDir);
156
- const next = jobs.filter((j) => j.id !== id);
157
- if (next.length === jobs.length) return false;
158
- await writeCronJobsFile(this.options.dataDir, next);
159
- const dispose = this.disposes.get(id);
160
- if (dispose) {
161
- dispose();
162
- this.disposes.delete(id);
163
- }
164
- return true;
165
- }
166
-
167
- /**
168
- * 暂停任务(不删除,停止调度)
169
- */
170
- async pauseJob(id: string): Promise<boolean> {
171
- const jobs = await readCronJobsFile(this.options.dataDir);
172
- const j = jobs.find((x) => x.id === id);
173
- if (!j) return false;
174
- j.enabled = false;
175
- await writeCronJobsFile(this.options.dataDir, jobs);
176
- const dispose = this.disposes.get(id);
177
- if (dispose) {
178
- dispose();
179
- this.disposes.delete(id);
180
- }
181
- return true;
182
- }
183
-
184
- /**
185
- * 恢复已暂停的任务
186
- */
187
- async resumeJob(id: string): Promise<boolean> {
188
- const jobs = await readCronJobsFile(this.options.dataDir);
189
- const j = jobs.find((x) => x.id === id);
190
- if (!j) return false;
191
- j.enabled = true;
192
- await writeCronJobsFile(this.options.dataDir, jobs);
193
- this.registerOne(j, this.options.addCron, this.options.runner);
194
- return true;
195
- }
196
-
197
- /**
198
- * 卸载所有由本引擎注册的定时任务(用于 dispose)
199
- */
200
- unload(): void {
201
- for (const dispose of this.disposes.values()) {
202
- try {
203
- dispose();
204
- } catch (_) {}
205
- }
206
- this.disposes.clear();
207
- }
208
- }
209
-
210
- /**
211
- * 生成唯一 ID(用于 CLI / AI 添加时)
212
- */
213
- export function generateCronJobId(): string {
214
- return `cron_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
215
- }
216
-
217
- // ─────────────────────────────────────────────────────────────────────────────
218
- // 供 AI 工具使用的 Cron 管理器引用(init 中设置)
219
- // ─────────────────────────────────────────────────────────────────────────────
220
-
221
- export interface CronManager {
222
- cronFeature: { getStatus(): Array<{ expression: string; running: boolean; nextExecution: Date | null; plugin: string }> };
223
- engine: PersistentCronEngine | null;
224
- }
225
-
226
- let cronManager: CronManager | null = null;
227
-
228
- export function setCronManager(m: CronManager | null): void {
229
- cronManager = m;
230
- }
231
-
232
- export function getCronManager(): CronManager | null {
233
- return cronManager;
234
- }
235
-
236
- // ─────────────────────────────────────────────────────────────────────────────
237
- // AI 可调用的定时任务管理工具
238
- // ─────────────────────────────────────────────────────────────────────────────
239
-
240
- export function createCronTools(): ZhinTool[] {
241
- const listTool = new ZhinTool('cron_list')
242
- .desc('列出所有定时任务:包括插件注册的内存任务与持久化任务(持久化任务有 id,可用于 cron_remove/cron_pause/cron_resume)')
243
- .keyword('定时任务', 'cron', '计划任务', '任务列表')
244
- .tag('cron', 'schedule')
245
- .execute(async () => {
246
- const m = getCronManager();
247
- if (!m) {
248
- return { error: '定时任务服务不可用' };
249
- }
250
- const memory = m.cronFeature.getStatus().map((s) => ({
251
- type: 'memory' as const,
252
- expression: s.expression,
253
- running: s.running,
254
- nextExecution: s.nextExecution?.toISOString() ?? null,
255
- plugin: s.plugin,
256
- }));
257
- const persistent = m.engine
258
- ? (await m.engine.listJobs()).map((j) => ({
259
- type: 'persistent' as const,
260
- id: j.id,
261
- cronExpression: j.cronExpression,
262
- prompt: j.prompt,
263
- label: j.label,
264
- enabled: j.enabled,
265
- createdAt: j.createdAt,
266
- }))
267
- : [];
268
- return { memory, persistent };
269
- });
270
-
271
- const addTool = new ZhinTool('cron_add')
272
- .desc('添加一条持久化定时任务:到点由 AI 执行指定 prompt,重启后仍保留')
273
- .keyword('添加定时', '新建定时任务', 'cron add')
274
- .tag('cron', 'schedule')
275
- .param('cron_expression', { type: 'string', description: 'Cron 表达式,如 "0 9 * * *" 表示每天 9:00' }, true)
276
- .param('prompt', { type: 'string', description: '到点触发时发给 AI 的提示词' }, true)
277
- .param('label', { type: 'string', description: '可选标签,便于识别' })
278
- .execute(async (args) => {
279
- const m = getCronManager();
280
- if (!m?.engine) {
281
- return { error: '持久化定时任务引擎不可用' };
282
- }
283
- const id = generateCronJobId();
284
- const job = await m.engine.addJob({
285
- id,
286
- cronExpression: args.cron_expression as string,
287
- prompt: args.prompt as string,
288
- label: args.label as string | undefined,
289
- enabled: true,
290
- });
291
- return { success: true, id: job.id, message: '已添加并立即生效' };
292
- });
293
-
294
- const removeTool = new ZhinTool('cron_remove')
295
- .desc('按 id 删除一条持久化定时任务')
296
- .keyword('删除定时', '取消定时', 'cron remove')
297
- .tag('cron', 'schedule')
298
- .param('id', { type: 'string', description: '任务 ID(cron_list 中 persistent 的 id)' }, true)
299
- .execute(async (args) => {
300
- const m = getCronManager();
301
- if (!m?.engine) {
302
- return { error: '持久化定时任务引擎不可用' };
303
- }
304
- const ok = await m.engine.removeJob(args.id as string);
305
- return ok ? { success: true, message: '已删除' } : { error: '未找到该任务' };
306
- });
307
-
308
- const pauseTool = new ZhinTool('cron_pause')
309
- .desc('暂停一条持久化定时任务(不删除,可 cron_resume 恢复)')
310
- .keyword('暂停定时', 'cron pause')
311
- .tag('cron', 'schedule')
312
- .param('id', { type: 'string', description: '任务 ID' }, true)
313
- .execute(async (args) => {
314
- const m = getCronManager();
315
- if (!m?.engine) {
316
- return { error: '持久化定时任务引擎不可用' };
317
- }
318
- const ok = await m.engine.pauseJob(args.id as string);
319
- return ok ? { success: true, message: '已暂停' } : { error: '未找到该任务' };
320
- });
321
-
322
- const resumeTool = new ZhinTool('cron_resume')
323
- .desc('恢复已暂停的持久化定时任务')
324
- .keyword('恢复定时', 'cron resume')
325
- .tag('cron', 'schedule')
326
- .param('id', { type: 'string', description: '任务 ID' }, true)
327
- .execute(async (args) => {
328
- const m = getCronManager();
329
- if (!m?.engine) {
330
- return { error: '持久化定时任务引擎不可用' };
331
- }
332
- const ok = await m.engine.resumeJob(args.id as string);
333
- return ok ? { success: true, message: '已恢复' } : { error: '未找到该任务' };
334
- });
335
-
336
- return [listTool, addTool, removeTool, pauseTool, resumeTool];
337
- }
@@ -1,357 +0,0 @@
1
- /**
2
- * FollowUpStore — 定时跟进任务的持久化存储
3
- *
4
- * 解决纯 setTimeout 重启即丢失的问题:
5
- * 1. 创建任务时同步写入数据库
6
- * 2. 执行完成 / 过期后从数据库中删除
7
- * 3. 机器人启动时从数据库加载所有未完成任务,重新挂定时器
8
- * 4. 同一会话创建新任务时,自动取消旧的 pending 任务(防止重复提醒)
9
- *
10
- * ai_followups 表:
11
- * ┌──────────────────────────────────────────────────────────────────┐
12
- * │ id (PK) | session_id | platform | sender_id | scene_id │
13
- * │ | message | fire_at | created_at | status │
14
- * └──────────────────────────────────────────────────────────────────┘
15
- */
16
-
17
- import { Logger } from '@zhin.js/logger';
18
-
19
- const logger = new Logger(null, 'FollowUp');
20
-
21
- // ============================================================================
22
- // 数据库模型
23
- // ============================================================================
24
-
25
- export const AI_FOLLOWUP_MODEL = {
26
- session_id: { type: 'text' as const, nullable: false },
27
- platform: { type: 'text' as const, nullable: false },
28
- bot_id: { type: 'text' as const, nullable: false },
29
- sender_id: { type: 'text' as const, nullable: false },
30
- scene_id: { type: 'text' as const, nullable: false },
31
- scene_type: { type: 'text' as const, nullable: false },
32
- message: { type: 'text' as const, nullable: false },
33
- /** 触发时间戳 (ms) */
34
- fire_at: { type: 'integer' as const, nullable: false },
35
- created_at: { type: 'integer' as const, default: 0 },
36
- /** pending | fired | cancelled */
37
- status: { type: 'text' as const, default: 'pending' },
38
- };
39
-
40
- // ============================================================================
41
- // 类型
42
- // ============================================================================
43
-
44
- export interface FollowUpRecord {
45
- id?: number;
46
- session_id: string;
47
- platform: string;
48
- bot_id: string;
49
- sender_id: string;
50
- scene_id: string;
51
- scene_type: string;
52
- message: string;
53
- fire_at: number;
54
- created_at: number;
55
- status: string;
56
- }
57
-
58
- /**
59
- * 数据库模型接口(与 RelatedModel 的链式查询 API 对齐)
60
- */
61
- interface DbModel {
62
- select(...fields: string[]): any; // 返回 Selection (thenable, 支持 .where())
63
- create(data: Record<string, any>): Promise<any>;
64
- update(data: Partial<any>): any; // 返回 Updation (thenable, 支持 .where())
65
- delete(condition: Record<string, any>): any; // 返回 Deletion (thenable, 支持 .where())
66
- }
67
-
68
- /**
69
- * 发送提醒的回调函数
70
- *
71
- * 由 ZhinAgent 在 init 阶段注入,负责把提醒消息发到正确的会话。
72
- */
73
- export type FollowUpSender = (record: FollowUpRecord) => Promise<void>;
74
-
75
- // ============================================================================
76
- // Store 接口
77
- // ============================================================================
78
-
79
- interface IFollowUpStore {
80
- create(record: Omit<FollowUpRecord, 'id'>): Promise<FollowUpRecord>;
81
- markFired(id: number): Promise<void>;
82
- cancel(id: number): Promise<void>;
83
- getPending(): Promise<FollowUpRecord[]>;
84
- getPendingBySession(sessionId: string): Promise<FollowUpRecord[]>;
85
- dispose(): void;
86
- }
87
-
88
- // ============================================================================
89
- // 内存实现
90
- // ============================================================================
91
-
92
- class MemoryFollowUpStore implements IFollowUpStore {
93
- private records: FollowUpRecord[] = [];
94
- private nextId = 1;
95
-
96
- async create(record: Omit<FollowUpRecord, 'id'>): Promise<FollowUpRecord> {
97
- const full: FollowUpRecord = { ...record, id: this.nextId++ };
98
- this.records.push(full);
99
- return full;
100
- }
101
-
102
- async markFired(id: number): Promise<void> {
103
- const r = this.records.find(r => r.id === id);
104
- if (r) r.status = 'fired';
105
- }
106
-
107
- async cancel(id: number): Promise<void> {
108
- const r = this.records.find(r => r.id === id);
109
- if (r) r.status = 'cancelled';
110
- }
111
-
112
- async getPending(): Promise<FollowUpRecord[]> {
113
- return this.records.filter(r => r.status === 'pending');
114
- }
115
-
116
- async getPendingBySession(sessionId: string): Promise<FollowUpRecord[]> {
117
- return this.records.filter(r => r.status === 'pending' && r.session_id === sessionId);
118
- }
119
-
120
- dispose(): void {
121
- this.records = [];
122
- }
123
- }
124
-
125
- // ============================================================================
126
- // 数据库实现
127
- // ============================================================================
128
-
129
- class DatabaseFollowUpStore implements IFollowUpStore {
130
- constructor(private model: DbModel) {}
131
-
132
- async create(record: Omit<FollowUpRecord, 'id'>): Promise<FollowUpRecord> {
133
- const created = await this.model.create(record);
134
- return { ...record, id: created.id ?? created };
135
- }
136
-
137
- async markFired(id: number): Promise<void> {
138
- await this.model.update({ status: 'fired' }).where({ id });
139
- }
140
-
141
- async cancel(id: number): Promise<void> {
142
- await this.model.update({ status: 'cancelled' }).where({ id });
143
- }
144
-
145
- async getPending(): Promise<FollowUpRecord[]> {
146
- return this.model.select().where({ status: 'pending' }) as Promise<FollowUpRecord[]>;
147
- }
148
-
149
- async getPendingBySession(sessionId: string): Promise<FollowUpRecord[]> {
150
- return this.model.select().where({ status: 'pending', session_id: sessionId }) as Promise<FollowUpRecord[]>;
151
- }
152
-
153
- dispose(): void {}
154
- }
155
-
156
- // ============================================================================
157
- // FollowUpManager
158
- // ============================================================================
159
-
160
- export class FollowUpManager {
161
- private store: IFollowUpStore;
162
- /** 内存中活跃的定时器: recordId → timer */
163
- private timers: Map<number, ReturnType<typeof setTimeout>> = new Map();
164
- private sender: FollowUpSender | null = null;
165
-
166
- constructor() {
167
- this.store = new MemoryFollowUpStore();
168
- }
169
-
170
- /** 注入消息发送回调 */
171
- setSender(sender: FollowUpSender): void {
172
- this.sender = sender;
173
- }
174
-
175
- /** 升级到数据库存储 */
176
- upgradeToDatabase(model: DbModel): void {
177
- const old = this.store;
178
- this.store = new DatabaseFollowUpStore(model);
179
- old.dispose();
180
- logger.debug('FollowUpManager: 已升级到数据库存储');
181
- }
182
-
183
- /**
184
- * 创建一个跟进任务
185
- *
186
- * 重要:同一会话的旧 pending 任务会被自动取消,防止重复提醒。
187
- *
188
- * @returns 人类可读的确认文本
189
- */
190
- async schedule(params: {
191
- sessionId: string;
192
- platform: string;
193
- botId: string;
194
- senderId: string;
195
- sceneId: string;
196
- sceneType: string;
197
- message: string;
198
- delayMinutes: number;
199
- }): Promise<string> {
200
- const { sessionId, platform, botId, senderId, sceneId, sceneType, message, delayMinutes } = params;
201
-
202
- // ── 详细参数日志(方便排查问题) ──
203
- logger.debug(`[跟进] 收到请求: delay_minutes=${delayMinutes}, message="${message}", session=${sessionId}`);
204
-
205
- // 限制最大延迟 7 天
206
- const maxDelay = 7 * 24 * 60;
207
- const actualDelay = Math.min(Math.max(delayMinutes, 1), maxDelay);
208
- const delayMs = actualDelay * 60 * 1000;
209
- const fireAt = Date.now() + delayMs;
210
-
211
- // ── 自动取消同一会话的旧 pending 任务 ──
212
- const existingPending = await this.store.getPendingBySession(sessionId);
213
- if (existingPending.length > 0) {
214
- for (const old of existingPending) {
215
- if (old.id != null) {
216
- // 取消数据库记录
217
- await this.store.cancel(old.id);
218
- // 清除内存中的定时器
219
- const timer = this.timers.get(old.id);
220
- if (timer) {
221
- clearTimeout(timer);
222
- this.timers.delete(old.id);
223
- }
224
- logger.debug(`[跟进] 自动取消旧任务: id=${old.id}, "${old.message}"`);
225
- }
226
- }
227
- }
228
-
229
- const record = await this.store.create({
230
- session_id: sessionId,
231
- platform,
232
- bot_id: botId,
233
- sender_id: senderId,
234
- scene_id: sceneId,
235
- scene_type: sceneType,
236
- message,
237
- fire_at: fireAt,
238
- created_at: Date.now(),
239
- status: 'pending',
240
- });
241
-
242
- // 挂定时器
243
- this.scheduleTimer(record);
244
-
245
- const readableTime = actualDelay >= 1440
246
- ? `${(actualDelay / 1440).toFixed(1)} 天后`
247
- : actualDelay >= 60
248
- ? `${(actualDelay / 60).toFixed(1)} 小时后`
249
- : `${actualDelay} 分钟后`;
250
-
251
- // 精确触发时间(方便日志对照)
252
- const fireDate = new Date(fireAt);
253
- const fireTimeStr = fireDate.toLocaleString('zh-CN', { hour12: false });
254
-
255
- logger.debug(`[跟进] 已创建: id=${record.id}, delay=${actualDelay}分钟(${delayMs}ms), 触发时间=${fireTimeStr}, "${message}"`);
256
- return `✅ 已安排提醒,将在 ${readableTime}(${fireTimeStr})提醒你:${message}`;
257
- }
258
-
259
- /**
260
- * 取消指定会话的所有 pending 任务
261
- */
262
- async cancelBySession(sessionId: string): Promise<number> {
263
- const pending = await this.store.getPendingBySession(sessionId);
264
- let count = 0;
265
- for (const record of pending) {
266
- if (record.id != null) {
267
- await this.store.cancel(record.id);
268
- const timer = this.timers.get(record.id);
269
- if (timer) {
270
- clearTimeout(timer);
271
- this.timers.delete(record.id);
272
- }
273
- count++;
274
- }
275
- }
276
- if (count > 0) {
277
- logger.debug(`[跟进] 已取消 ${count} 个待执行任务 (session=${sessionId})`);
278
- }
279
- return count;
280
- }
281
-
282
- /**
283
- * 启动时从数据库恢复所有未完成的跟进任务
284
- */
285
- async restore(): Promise<number> {
286
- const pending = await this.store.getPending();
287
- const now = Date.now();
288
- let restored = 0;
289
-
290
- for (const record of pending) {
291
- if (record.fire_at <= now) {
292
- // 已过期但未执行 → 立即触发(延迟 2 秒,等系统完全就绪)
293
- const overdueSec = Math.round((now - record.fire_at) / 1000);
294
- logger.debug(`[跟进恢复] id=${record.id} 已过期 ${overdueSec}s,立即触发`);
295
- this.scheduleTimerWithDelay(record, 2000);
296
- restored++;
297
- } else {
298
- // 还没到时间 → 重新挂定时器
299
- this.scheduleTimer(record);
300
- const remainMs = record.fire_at - now;
301
- const remainMin = (remainMs / 60_000).toFixed(1);
302
- logger.debug(`[跟进恢复] id=${record.id} 剩余 ${remainMin} 分钟, "${record.message}"`);
303
- restored++;
304
- }
305
- }
306
-
307
- if (restored > 0) {
308
- logger.info(`[跟进恢复] 共恢复 ${restored} 个待执行任务`);
309
- }
310
- return restored;
311
- }
312
-
313
- /**
314
- * 为一条记录挂定时器
315
- */
316
- private scheduleTimer(record: FollowUpRecord): void {
317
- const delay = Math.max(record.fire_at - Date.now(), 0);
318
- this.scheduleTimerWithDelay(record, delay);
319
- }
320
-
321
- private scheduleTimerWithDelay(record: FollowUpRecord, delayMs: number): void {
322
- if (!record.id) return;
323
-
324
- // 清除旧定时器(如果有)
325
- const existing = this.timers.get(record.id);
326
- if (existing) clearTimeout(existing);
327
-
328
- const timer = setTimeout(async () => {
329
- try {
330
- this.timers.delete(record.id!);
331
-
332
- // 发送提醒
333
- if (this.sender) {
334
- await this.sender(record);
335
- logger.info(`[跟进提醒] 已发送: id=${record.id}, "${record.message}"`);
336
- } else {
337
- logger.warn(`[跟进提醒] 无法发送 (sender 未注入): id=${record.id}`);
338
- }
339
-
340
- // 标记完成
341
- await this.store.markFired(record.id!);
342
- } catch (e) {
343
- logger.warn(`[跟进提醒] 发送失败: id=${record.id}`, e);
344
- }
345
- }, delayMs);
346
-
347
- this.timers.set(record.id, timer);
348
- }
349
-
350
- dispose(): void {
351
- for (const timer of this.timers.values()) {
352
- clearTimeout(timer);
353
- }
354
- this.timers.clear();
355
- this.store.dispose();
356
- }
357
- }