@faapi/faapi 6.3.0 → 6.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.
- package/dist/cli/index.js +301 -155
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +68 -28
- package/dist/index.js +292 -152
- package/dist/index.js.map +1 -1
- package/dist/{routeTypes-CCveqSnY.d.ts → routeTypes-_17rXzal.d.ts} +152 -11
- package/dist/testing.d.ts +1 -1
- package/package.json +1 -1
|
@@ -22,14 +22,14 @@ declare function createTaskRegistry(): TaskRegistry;
|
|
|
22
22
|
* run 执行包装、任务记录(list);驱动层负责:入队存储、worker 消费、
|
|
23
23
|
* 失败重试、停机 drain。换驱动 = 换存储,业务方写法不变。
|
|
24
24
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
25
|
+
* 框架不内置任何驱动实现:外部驱动由独立子包提供——`@faapi/task-pgboss`(Postgres)、
|
|
26
|
+
* `@faapi/task-bullmq`(Redis),主包不依赖它们——按 config.task.driver 动态加载
|
|
27
|
+
* (loadTaskDriver.ts);无任务清单时用 idleTaskDriver 占位(enqueue 显式报错)。
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
30
|
/** 驱动层交付给语义层执行的单个任务 */
|
|
31
31
|
interface TaskDriverJob {
|
|
32
|
-
/** 队列系统侧任务 id(
|
|
32
|
+
/** 队列系统侧任务 id(pg-boss/bullmq 为其自身 id;自定义驱动自行生成) */
|
|
33
33
|
id: string;
|
|
34
34
|
name: string;
|
|
35
35
|
payload: unknown;
|
|
@@ -40,6 +40,28 @@ interface TaskDriverJob {
|
|
|
40
40
|
}
|
|
41
41
|
/** 语义层交给驱动层的执行函数(抛错 = 失败,由驱动按入队时的 retries 重试) */
|
|
42
42
|
type TaskDriverProcess = (job: TaskDriverJob) => Promise<unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* 驱动侧任务查询记录(TaskDriver.list 的返回项)
|
|
45
|
+
*
|
|
46
|
+
* status 由子包从队列系统原生状态映射为 faapi 语义
|
|
47
|
+
* (pgboss:created→pending、active→running、completed→done、cancelled→cancelled;
|
|
48
|
+
* bullmq:waiting/delayed→pending、active→running、completed→done、failed→failed),
|
|
49
|
+
* 语义层零映射直接转 TaskJob。
|
|
50
|
+
*/
|
|
51
|
+
interface TaskDriverRecord {
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
payload: unknown;
|
|
55
|
+
status: TaskJobStatus;
|
|
56
|
+
attempts: number;
|
|
57
|
+
/** 执行返回值(已完成时) */
|
|
58
|
+
result?: unknown;
|
|
59
|
+
/** 失败/取消原因 */
|
|
60
|
+
error?: string;
|
|
61
|
+
createdAt: number;
|
|
62
|
+
/** 计划执行时间戳(延迟任务) */
|
|
63
|
+
runAt?: number;
|
|
64
|
+
}
|
|
43
65
|
/**
|
|
44
66
|
* 任务队列驱动接口
|
|
45
67
|
*/
|
|
@@ -48,11 +70,15 @@ interface TaskDriver {
|
|
|
48
70
|
* 入队一个任务,返回驱动侧任务 id
|
|
49
71
|
*
|
|
50
72
|
* @param opts.retries 失败重试次数(语义层从任务 meta 取,驱动负责执行重试策略)
|
|
73
|
+
* @param opts.dedupId 幂等键(可选)——同键任务在队列系统保留期内不重复入队。
|
|
74
|
+
* pgboss 映射 send 自定义 id(驱动内做任意字符串 → 确定性 UUID 映射,冲突跳过);
|
|
75
|
+
* bullmq 映射 jobId。重复投递时返回已存在任务的 id。
|
|
51
76
|
* @throws 驱动已停止 / 连接失败等
|
|
52
77
|
*/
|
|
53
78
|
enqueue(name: string, payload: unknown, opts?: {
|
|
54
79
|
delayMs?: number;
|
|
55
80
|
retries?: number;
|
|
81
|
+
dedupId?: string;
|
|
56
82
|
}): Promise<string>;
|
|
57
83
|
/**
|
|
58
84
|
* 注册某任务的消费 worker(幂等覆盖)。process 抛错 = 本次失败,驱动决定重试。
|
|
@@ -66,8 +92,59 @@ interface TaskDriver {
|
|
|
66
92
|
stop(timeoutMs?: number): Promise<void>;
|
|
67
93
|
/** 仅停止 worker 消费(不断开驱动连接),供 dev reloadTasks 重注册用;可选 */
|
|
68
94
|
stopWorkers?(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* 查询持久化队列任务(可选能力)
|
|
97
|
+
*
|
|
98
|
+
* 未实现时 TaskClient.listQueued 显式抛错。pg-boss v10 无批量列出 jobs 的公开 API,
|
|
99
|
+
* task-pgboss 未实现;BullMQ API 完整,task-bullmq 全支持。
|
|
100
|
+
* 不传 name 时列出本进程已创建队列/已知任务的范围由驱动自行定义。
|
|
101
|
+
*/
|
|
102
|
+
list?(opts?: {
|
|
103
|
+
name?: string;
|
|
104
|
+
/** 按 faapi 语义状态过滤 */
|
|
105
|
+
state?: TaskJobStatus;
|
|
106
|
+
/** 返回条数上限(默认由驱动决定,建议 50) */
|
|
107
|
+
limit?: number;
|
|
108
|
+
}): Promise<TaskDriverRecord[]>;
|
|
109
|
+
/**
|
|
110
|
+
* 取消队列侧任务(可选能力):等待/延迟中的不再执行。
|
|
111
|
+
* pgboss → boss.cancel(保留 cancelled 记录);bullmq → job.remove()(记录随之移除)。
|
|
112
|
+
*/
|
|
113
|
+
cancel?(name: string, id: string): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* 重试失败/取消的任务(可选能力)。
|
|
116
|
+
* pgboss → boss.resume(仅 cancelled);bullmq → job.retry()(仅 failed)。
|
|
117
|
+
* 任务不存在 / 状态不允许时由驱动抛错。
|
|
118
|
+
*/
|
|
119
|
+
retry?(name: string, id: string): Promise<void>;
|
|
69
120
|
}
|
|
70
121
|
|
|
122
|
+
/**
|
|
123
|
+
* 隔离执行器签名(taskQueue 按任务 meta.timeoutMs 调用;测试可注入 spy)
|
|
124
|
+
*/
|
|
125
|
+
type TaskWorkerRunner = typeof runTaskInWorker;
|
|
126
|
+
interface TaskWorkerOptions {
|
|
127
|
+
/** 任务产物模块绝对路径(`<dist>/tasks/<dir>/task.js`) */
|
|
128
|
+
taskModulePath: string;
|
|
129
|
+
payload: unknown;
|
|
130
|
+
/** signal 由执行器构造(abort/terminate 时触发),宿主只传 config 与 job 信息 */
|
|
131
|
+
taskCtx: {
|
|
132
|
+
config: unknown;
|
|
133
|
+
job: {
|
|
134
|
+
id: string;
|
|
135
|
+
name: string;
|
|
136
|
+
attempt: number;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
/** 单次执行超时(毫秒) */
|
|
140
|
+
timeoutMs: number;
|
|
141
|
+
/** 外部取消信号(驱动停机超时 abort)——abort 同样触发两段式取消 */
|
|
142
|
+
externalSignal?: AbortSignal;
|
|
143
|
+
/** 宽限期覆盖(默认 KILL_GRACE_MS)——测试注入短值用,业务不配置 */
|
|
144
|
+
killGraceMs?: number;
|
|
145
|
+
}
|
|
146
|
+
declare function runTaskInWorker(options: TaskWorkerOptions): Promise<unknown>;
|
|
147
|
+
|
|
71
148
|
/**
|
|
72
149
|
* 任务元信息(业务方在 task.ts 中 `export const task = {...}` 声明)
|
|
73
150
|
*
|
|
@@ -78,6 +155,12 @@ interface FaapiTaskMeta {
|
|
|
78
155
|
concurrency?: number;
|
|
79
156
|
/** 失败重试次数(默认 0——失败即 failed,不重试) */
|
|
80
157
|
retries?: number;
|
|
158
|
+
/**
|
|
159
|
+
* 单次执行超时(毫秒)。声明后该任务在独立 worker 线程执行,超时两段式取消
|
|
160
|
+
* (先 abort 信号宽限 5s,未退出 terminate 硬杀)——判定超时即执行真正终止。
|
|
161
|
+
* 未声明走进程内执行(零开销,但卡住时框架只能不再等待)。
|
|
162
|
+
*/
|
|
163
|
+
timeoutMs?: number;
|
|
81
164
|
/** cron 表达式(croner 语法,支持秒级)——到点自动入队空 payload */
|
|
82
165
|
cron?: string;
|
|
83
166
|
}
|
|
@@ -92,6 +175,7 @@ interface TaskManifest {
|
|
|
92
175
|
cron?: string;
|
|
93
176
|
concurrency?: number;
|
|
94
177
|
retries?: number;
|
|
178
|
+
timeoutMs?: number;
|
|
95
179
|
}
|
|
96
180
|
/**
|
|
97
181
|
* 运行时任务元数据(faapi-tasks.js 水合到 TaskRegistry 后的形态,产物路径形式)
|
|
@@ -103,9 +187,15 @@ interface TaskMetadata {
|
|
|
103
187
|
cron?: string;
|
|
104
188
|
concurrency?: number;
|
|
105
189
|
retries?: number;
|
|
190
|
+
timeoutMs?: number;
|
|
106
191
|
}
|
|
107
|
-
/**
|
|
108
|
-
|
|
192
|
+
/**
|
|
193
|
+
* 任务记录状态
|
|
194
|
+
*
|
|
195
|
+
* cancelled = 执行被框架终止(超时两段式取消 / 停机取消),区别于 run 自身抛错的 failed;
|
|
196
|
+
* 驱动重试时记录回到 running 继续流转。
|
|
197
|
+
*/
|
|
198
|
+
type TaskJobStatus = 'pending' | 'running' | 'retry' | 'done' | 'failed' | 'cancelled';
|
|
109
199
|
/**
|
|
110
200
|
* 任务执行记录(内存快照)
|
|
111
201
|
*/
|
|
@@ -151,16 +241,42 @@ interface TaskClient {
|
|
|
151
241
|
/**
|
|
152
242
|
* 入队一个任务
|
|
153
243
|
*
|
|
244
|
+
* @param opts.dedupId 幂等键(可选)——同键任务在队列系统保留期内不重复入队,
|
|
245
|
+
* 重复投递返回已存在任务 id(驱动语义见 driverTypes.md);cron 投递自动携带
|
|
154
246
|
* @returns 任务 id
|
|
155
247
|
* @throws 任务不存在 / 队列已停止 / payload 校验失败(ValidationError)
|
|
156
248
|
*/
|
|
157
249
|
enqueue(name: string, payload?: unknown, opts?: {
|
|
158
250
|
delayMs?: number;
|
|
251
|
+
dedupId?: string;
|
|
159
252
|
}): Promise<{
|
|
160
253
|
id: string;
|
|
161
254
|
}>;
|
|
162
|
-
/**
|
|
255
|
+
/** 任务记录快照(可按任务名过滤)——本进程内存活记录,不含其他实例/重启前历史 */
|
|
163
256
|
list(name?: string): TaskJob[];
|
|
257
|
+
/**
|
|
258
|
+
* 持久化队列视图:驱动实现 `TaskDriver.list` 时返回队列侧任务(含其他实例的
|
|
259
|
+
* 与历史执行),并与本进程记录按 id 合并(本进程观测优先:status/attempts/
|
|
260
|
+
* result/error 以本进程为准)
|
|
261
|
+
*
|
|
262
|
+
* @throws 驱动未实现 TaskDriver.list(能力边界见 driverTypes.md——pgboss 未实现,
|
|
263
|
+
* 用 pg-boss 自身 API/SQL 旁路;BullMQ 全支持)
|
|
264
|
+
*/
|
|
265
|
+
listQueued(name?: string): Promise<TaskJob[]>;
|
|
266
|
+
/**
|
|
267
|
+
* 取消队列侧任务:等待/延迟中的不再执行(语义随驱动——pgboss 保留 cancelled
|
|
268
|
+
* 记录,bullmq 为 job.remove 即移除)
|
|
269
|
+
*
|
|
270
|
+
* @throws 驱动未实现 TaskDriver.cancel
|
|
271
|
+
*/
|
|
272
|
+
cancel(name: string, id: string): Promise<void>;
|
|
273
|
+
/**
|
|
274
|
+
* 重试队列侧失败/取消的任务(pgboss 仅 cancelled 可 resume;bullmq 仅 failed
|
|
275
|
+
* 可 retry;任务不存在/状态不允许由驱动抛错)
|
|
276
|
+
*
|
|
277
|
+
* @throws 驱动未实现 TaskDriver.retry
|
|
278
|
+
*/
|
|
279
|
+
retry(name: string, id: string): Promise<void>;
|
|
164
280
|
}
|
|
165
281
|
/**
|
|
166
282
|
* 任务队列(TaskClient + 生命周期控制,createAppBase 内部使用)
|
|
@@ -184,15 +300,40 @@ interface TaskQueueDeps {
|
|
|
184
300
|
/** faapi.config.ts 全量配置,透传给 run 的 TaskContext.config */
|
|
185
301
|
config?: unknown;
|
|
186
302
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
303
|
+
* 队列驱动(必填):`loadTaskDriver` 解析结果(pgboss/bullmq 子包驱动)
|
|
304
|
+
* 或自定义 TaskDriver 实例;无任务清单时由 createAppBase 传入 idleTaskDriver
|
|
189
305
|
*/
|
|
190
|
-
driver
|
|
306
|
+
driver: TaskDriver;
|
|
191
307
|
/** 任务模块加载器(默认:import 产物路径) */
|
|
192
308
|
loadTaskModule?: (filePath: string) => Promise<TaskModule>;
|
|
193
309
|
/** payload schema 加载器(默认:import 任务目录 zod.js,取首个 `*Schema` 导出) */
|
|
194
310
|
loadPayloadSchema?: (filePath: string) => Promise<unknown>;
|
|
311
|
+
/**
|
|
312
|
+
* 隔离执行器(默认 runTaskInWorker)——任务声明 timeoutMs 时由语义层调用,
|
|
313
|
+
* 独立 worker 线程执行 + 超时两段式取消;测试注入 spy 验证执行路径路由
|
|
314
|
+
*/
|
|
315
|
+
runIsolated?: TaskWorkerRunner;
|
|
316
|
+
/**
|
|
317
|
+
* 执行失败/取消钩子(config.task.onFailed)——每次 process 抛错后触发
|
|
318
|
+
* (含将重试的失败);用于告警/死信上报等副作用,自身抛错被忽略
|
|
319
|
+
*/
|
|
320
|
+
onFailed?: TaskFailedHandler;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* 任务失败信息(onFailed 钩子参数)
|
|
324
|
+
*
|
|
325
|
+
* willRetry 按任务 meta.retries 推算(attempt <= retries 时驱动会重试);
|
|
326
|
+
* cancelled = 执行被框架终止(超时终止/停机取消),与 run 自身失败区分。
|
|
327
|
+
*/
|
|
328
|
+
interface TaskFailedInfo {
|
|
329
|
+
task: string;
|
|
330
|
+
jobId: string;
|
|
331
|
+
attempt: number;
|
|
332
|
+
willRetry: boolean;
|
|
333
|
+
cancelled: boolean;
|
|
334
|
+
error: string;
|
|
195
335
|
}
|
|
336
|
+
type TaskFailedHandler = (info: TaskFailedInfo) => Promise<void> | void;
|
|
196
337
|
|
|
197
338
|
/**
|
|
198
339
|
* SSE(Server-Sent Events)支持
|
|
@@ -919,4 +1060,4 @@ interface RouteInfo {
|
|
|
919
1060
|
output: RouteOutputSchema | null;
|
|
920
1061
|
}
|
|
921
1062
|
|
|
922
|
-
export { type AppRegistries as A, type
|
|
1063
|
+
export { type AppRegistries as A, type TaskDriverJob as B, type CorsOptions as C, type TaskDriverProcess as D, type TaskDriverRecord as E, type FaapiContext as F, type TaskFailedInfo as G, type HelmetOptions as H, type InjectorMap as I, type TaskJob as J, type TaskJobStatus as K, type LoggerOptions as L, type TaskMetadata as M, type TaskModule as N, type ToolCore as O, type ToolPathMeta as P, type ToolRegistry as Q, type RouteManifest as R, type SkillRegistry as S, type TaskClient as T, cors as U, createAppRegistries as V, type WsRouteManifest as W, createTaskRegistry as X, helmet as Y, logger as Z, type FaapiMiddleware as a, type TaskFailedHandler as b, type TaskQueueDeps as c, type TaskQueue as d, type TaskDriver as e, type TaskRegistry as f, type TaskManifest as g, type ToolMetadata as h, type AgentCore as i, type AgentMetadata as j, type AgentHandleFactory as k, type AgentHandleStore as l, type AgentPathMeta as m, type AgentRegistry as n, type AgentToolDescriptor as o, type FaapiContextConfig as p, type FaapiTaskMeta as q, type FailOptions as r, type Injector as s, type RouteInfo as t, type RouteInputSchema as u, type RouteOutputSchema as v, type RouteParamSchema as w, type SseEvent as x, type SseWriter as y, type TaskContext as z };
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { F as FaapiContext, a as FaapiMiddleware, I as InjectorMap, R as RouteManifest, W as WsRouteManifest, C as CorsOptions, H as HelmetOptions, L as LoggerOptions } from './routeTypes-
|
|
1
|
+
import { F as FaapiContext, a as FaapiMiddleware, I as InjectorMap, R as RouteManifest, W as WsRouteManifest, C as CorsOptions, H as HelmetOptions, L as LoggerOptions } from './routeTypes-_17rXzal.js';
|
|
2
2
|
import { Server } from 'node:http';
|
|
3
3
|
import { WebSocket } from 'ws';
|
|
4
4
|
|