@faapi/faapi 6.2.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 +1397 -499
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +156 -3
- package/dist/index.js +1090 -259
- package/dist/index.js.map +1 -1
- package/dist/{routeTypes-Bm--uTbm.d.ts → routeTypes-_17rXzal.d.ts} +355 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +48 -2
- package/dist/testing.js.map +1 -1
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as AppRegistries, R as RouteManifest, F as FaapiContext, C as CorsOptions, H as HelmetOptions, L as LoggerOptions, a as FaapiMiddleware, I as InjectorMap,
|
|
2
|
-
export {
|
|
1
|
+
import { A as AppRegistries, R as RouteManifest, F as FaapiContext, C as CorsOptions, T as TaskClient, H as HelmetOptions, L as LoggerOptions, a as FaapiMiddleware, I as InjectorMap, b as TaskFailedHandler, c as TaskQueueDeps, d as TaskQueue, e as TaskDriver, f as TaskRegistry, g as TaskManifest, h as ToolMetadata, i as AgentCore, j as AgentMetadata, W as WsRouteManifest } from './routeTypes-_17rXzal.js';
|
|
2
|
+
export { k as AgentHandleFactory, l as AgentHandleStore, m as AgentPathMeta, n as AgentRegistry, o as AgentToolDescriptor, p as FaapiContextConfig, q as FaapiTaskMeta, r as FailOptions, s as Injector, t as RouteInfo, u as RouteInputSchema, v as RouteOutputSchema, w as RouteParamSchema, S as SkillRegistry, x as SseEvent, y as SseWriter, z as TaskContext, B as TaskDriverJob, D as TaskDriverProcess, E as TaskDriverRecord, G as TaskFailedInfo, J as TaskJob, K as TaskJobStatus, M as TaskMetadata, N as TaskModule, O as ToolCore, P as ToolPathMeta, Q as ToolRegistry, U as cors, V as createAppRegistries, X as createTaskRegistry, Y as helmet, Z as logger } from './routeTypes-_17rXzal.js';
|
|
3
3
|
import * as node_http from 'node:http';
|
|
4
4
|
import { Server, IncomingMessage, ServerResponse } from 'node:http';
|
|
5
5
|
import { Socket } from 'node:net';
|
|
@@ -156,6 +156,67 @@ interface LifecycleHooks {
|
|
|
156
156
|
*/
|
|
157
157
|
onError?: (error: unknown, ctx: FaapiContext) => Promise<void> | void;
|
|
158
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* pg-boss 驱动选项(透传给 @faapi/task-pgboss)
|
|
161
|
+
*
|
|
162
|
+
* 主包不依赖 pg-boss,这里只声明常用连接字段的透传形状;完整项见 pg-boss 文档
|
|
163
|
+
* (ConstructorOptions)。
|
|
164
|
+
*/
|
|
165
|
+
interface TaskPgBossOptions {
|
|
166
|
+
/** PostgreSQL 连接串(如 `postgres://localhost:5432/app`) */
|
|
167
|
+
connectionString?: string;
|
|
168
|
+
/** 其余 pg-boss ConstructorOptions 字段原样透传 */
|
|
169
|
+
[key: string]: unknown;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* BullMQ 驱动选项(透传给 @faapi/task-bullmq)
|
|
173
|
+
*/
|
|
174
|
+
interface TaskBullMqOptions {
|
|
175
|
+
/** Redis 连接配置(透传给 Queue / Worker 的 connection) */
|
|
176
|
+
connection?: {
|
|
177
|
+
host?: string;
|
|
178
|
+
port?: number;
|
|
179
|
+
password?: string;
|
|
180
|
+
db?: number;
|
|
181
|
+
[key: string]: unknown;
|
|
182
|
+
};
|
|
183
|
+
/** 队列名前缀(默认 `faapi`——同一 Redis 下多个 faapi 应用隔离用) */
|
|
184
|
+
prefix?: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* 任务子系统配置
|
|
188
|
+
*
|
|
189
|
+
* 任务队列由持久化驱动承载(内置 memory 驱动已移除):存在任务清单
|
|
190
|
+
* (`src/tasks/` 下的 task.ts)时必须显式配置 `driver`,否则启动报错;
|
|
191
|
+
* 无任务清单的项目不加载驱动(零任务项目无需安装驱动子包)。
|
|
192
|
+
*/
|
|
193
|
+
interface TaskConfig {
|
|
194
|
+
/**
|
|
195
|
+
* 队列驱动:'pgboss'(@faapi/task-pgboss,Postgres)| 'bullmq'(@faapi/task-bullmq,Redis)
|
|
196
|
+
*
|
|
197
|
+
* 有任务清单时必填;也可在编程式场景传入自定义 TaskDriver 实例
|
|
198
|
+
*/
|
|
199
|
+
driver?: 'pgboss' | 'bullmq';
|
|
200
|
+
/** `driver: 'pgboss'` 时的选项(透传给 @faapi/task-pgboss) */
|
|
201
|
+
pgboss?: TaskPgBossOptions;
|
|
202
|
+
/** `driver: 'bullmq'` 时的选项(透传给 @faapi/task-bullmq) */
|
|
203
|
+
bullmq?: TaskBullMqOptions;
|
|
204
|
+
/**
|
|
205
|
+
* 是否启动任务队列(默认 true)
|
|
206
|
+
*
|
|
207
|
+
* 多实例部署时可在非 worker 节点设 `FAAPI_TASKS_DISABLED=1` 或 `task.enabled: false`,
|
|
208
|
+
* API 节点照常入队(入队能力保留),但不消费执行。
|
|
209
|
+
*/
|
|
210
|
+
enabled?: boolean;
|
|
211
|
+
/** 优雅停机时等待在跑任务的最长时间(毫秒,默认 10000;超时 abort) */
|
|
212
|
+
shutdownTimeoutMs?: number;
|
|
213
|
+
/**
|
|
214
|
+
* 任务执行失败/取消钩子——每次 process 抛错后触发(含将重试的失败),
|
|
215
|
+
* `info.willRetry` 按任务 meta.retries 推算、`info.cancelled` 标记框架终止;
|
|
216
|
+
* 用于告警/死信上报等副作用,自身抛错被忽略
|
|
217
|
+
*/
|
|
218
|
+
onFailed?: TaskFailedHandler;
|
|
219
|
+
}
|
|
159
220
|
/**
|
|
160
221
|
* 生命周期上下文
|
|
161
222
|
*/
|
|
@@ -168,6 +229,8 @@ interface LifecycleContext {
|
|
|
168
229
|
server: node_http.Server;
|
|
169
230
|
/** app 级注册表——skill 等运行时动态注册路径(`registries.skill.upsert(...)`) */
|
|
170
231
|
registries: AppRegistries;
|
|
232
|
+
/** 任务客户端——onBoot/onReady 中投递异步任务(`tasks.enqueue(name, payload)`) */
|
|
233
|
+
tasks: TaskClient;
|
|
171
234
|
}
|
|
172
235
|
/**
|
|
173
236
|
* 统一响应包装配置
|
|
@@ -617,6 +680,19 @@ interface FaapiConfig {
|
|
|
617
680
|
* 详见 `src/config/configTypes.md` agent 配置块章节。
|
|
618
681
|
*/
|
|
619
682
|
agent?: AgentConfig;
|
|
683
|
+
/**
|
|
684
|
+
* 任务子系统全局配置(队列驱动、启停、停机超时)
|
|
685
|
+
*
|
|
686
|
+
* ```ts
|
|
687
|
+
* import type { FaapiConfig } from '@faapi/faapi';
|
|
688
|
+
* export default {
|
|
689
|
+
* task: { enabled: true, shutdownTimeoutMs: 10_000 },
|
|
690
|
+
* } satisfies FaapiConfig;
|
|
691
|
+
* ```
|
|
692
|
+
*
|
|
693
|
+
* 详见 `src/task/README.md`。
|
|
694
|
+
*/
|
|
695
|
+
task?: TaskConfig;
|
|
620
696
|
/**
|
|
621
697
|
* 扩展 ctx:在每次请求创建上下文后调用,可挂载自定义方法(如 ctx.xml、ctx.stream)
|
|
622
698
|
*
|
|
@@ -1048,6 +1124,79 @@ declare function collectRouteSchemaSources(routes: RouteManifest, rootDir?: stri
|
|
|
1048
1124
|
resolversByFile: Map<string, LazyTypeResolver>;
|
|
1049
1125
|
};
|
|
1050
1126
|
|
|
1127
|
+
/**
|
|
1128
|
+
* 任务队列语义层
|
|
1129
|
+
*
|
|
1130
|
+
* 职责边界(driverTypes.md):
|
|
1131
|
+
* - 本层:任务存在性检查 → payload zod 校验 → 驱动入队;worker 执行包装
|
|
1132
|
+
* (模块加载 + run 调用 + 任务记录更新);start/stop/reload 生命周期编排
|
|
1133
|
+
* - 驱动层(deps.driver,必填——持久化驱动子包或自定义 TaskDriver):
|
|
1134
|
+
* 入队存储、worker 消费、重试策略、停机 drain
|
|
1135
|
+
*
|
|
1136
|
+
* 任务记录(list)由本层维护:enqueue 写 pending,每次 process 写 attempts/running,
|
|
1137
|
+
* 返回写 done,抛错写 failed——对任何驱动语义一致。
|
|
1138
|
+
*/
|
|
1139
|
+
declare function createTaskQueue(deps: TaskQueueDeps): TaskQueue;
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* 按 config.task.driver 解析并加载任务队列驱动
|
|
1143
|
+
*
|
|
1144
|
+
* - `'pgboss'` → 动态加载子包 `@faapi/task-pgboss` 的 `createPgBossDriver(options)`
|
|
1145
|
+
* - `'bullmq'` → 动态加载子包 `@faapi/task-bullmq` 的 `createBullMQDriver(options)`
|
|
1146
|
+
* - TaskDriver 对象 → 直接使用(编程式自定义驱动)
|
|
1147
|
+
* - `undefined` / `'memory'` / 其他值 → 显式抛错,不静默降级
|
|
1148
|
+
*
|
|
1149
|
+
* 内置 memory 驱动已移除——任务队列由持久化/外部驱动承载;未配置 driver 时
|
|
1150
|
+
* createAppBase 仅在存在任务清单时报错(无任务项目用 idleTaskDriver,不调用本函数)。
|
|
1151
|
+
*
|
|
1152
|
+
* 子包 specifier 用变量拼接(`'@faapi/task-' + name`)而非字面量——主包不依赖
|
|
1153
|
+
* 驱动子包,运行时从业务方 node_modules 解析(与 zod peerDependency 同策略)。
|
|
1154
|
+
* 未安装时抛出带安装指引的错误。
|
|
1155
|
+
*/
|
|
1156
|
+
declare function loadTaskDriver(driver: string | TaskDriver | undefined, driverOptions: unknown): Promise<TaskDriver>;
|
|
1157
|
+
|
|
1158
|
+
/**
|
|
1159
|
+
* cron 定时入队调度器
|
|
1160
|
+
*
|
|
1161
|
+
* 为注册表中声明了 `cron` 的任务建立 croner 定时器,到点调用
|
|
1162
|
+
* `enqueue(name, { dedupId })` 投递空 payload——定时只是"自动投递者",
|
|
1163
|
+
* 复用队列的重试/并发/停机语义。详见 cronScheduler.md。
|
|
1164
|
+
*/
|
|
1165
|
+
interface CronScheduler {
|
|
1166
|
+
/** 建立全部 cron 定时器(幂等——重复调用先清理旧定时器) */
|
|
1167
|
+
start(): void;
|
|
1168
|
+
/** 停止全部定时器(幂等) */
|
|
1169
|
+
stop(): void;
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* cron 投递回调:opts.dedupId 为多实例防重幂等键
|
|
1173
|
+
* (`cron:<任务名>:<计划触发时刻>`——各实例同一触发窗算出同键,驱动按键去重)
|
|
1174
|
+
*/
|
|
1175
|
+
type CronEnqueue = (name: string, opts: {
|
|
1176
|
+
dedupId?: string;
|
|
1177
|
+
}) => Promise<unknown>;
|
|
1178
|
+
declare function createCronScheduler(registry: TaskRegistry, enqueue: CronEnqueue): CronScheduler;
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* 任务扫描 patterns(框架约定,非用户可配置项)
|
|
1182
|
+
*
|
|
1183
|
+
* 与路由的 src/api、tool 的 src/tools 对称——任务文件约定放在
|
|
1184
|
+
* `src/tasks` 下任意层级子目录的 task.ts(与 handler.ts 同构的一文件一任务约定)。
|
|
1185
|
+
*/
|
|
1186
|
+
declare const TASK_PATTERNS: string[];
|
|
1187
|
+
/**
|
|
1188
|
+
* 扫描 tasks 目录,生成任务清单
|
|
1189
|
+
*
|
|
1190
|
+
* Vite 风格:只读源码 + 正则提取 meta 字面量,不 import 任务模块。
|
|
1191
|
+
* 任务模块的编译与加载延后到运行时(队列派发时 import 产物路径)。
|
|
1192
|
+
*
|
|
1193
|
+
* 重名检测:不同文件推导出同名任务时抛错。
|
|
1194
|
+
*
|
|
1195
|
+
* @param rootDir 项目根目录
|
|
1196
|
+
* @param patterns glob patterns(匹配 src/tasks 下的 task.ts)
|
|
1197
|
+
*/
|
|
1198
|
+
declare function scanTasks(rootDir: string, patterns: string[]): Promise<TaskManifest[]>;
|
|
1199
|
+
|
|
1051
1200
|
/**
|
|
1052
1201
|
* 加载后的 agent 模块
|
|
1053
1202
|
*
|
|
@@ -1443,6 +1592,8 @@ interface AppBase {
|
|
|
1443
1592
|
wsRoutes: WsRouteManifest;
|
|
1444
1593
|
/** 项目根目录 */
|
|
1445
1594
|
rootDir: string;
|
|
1595
|
+
/** 任务队列客户端(入队/查询;app 实例级,close 时随队列停机) */
|
|
1596
|
+
tasks: TaskClient;
|
|
1446
1597
|
/** 启动 HTTP server,打印路由表,执行 onReady 钩子 */
|
|
1447
1598
|
listen(port?: number): Promise<Server>;
|
|
1448
1599
|
/** 关闭 server,执行 onClose 钩子 */
|
|
@@ -1467,6 +1618,8 @@ interface DevApp extends AppBase {
|
|
|
1467
1618
|
reloadTools(): Promise<void>;
|
|
1468
1619
|
/** 重新扫描 agents + 重生成 faapi-agents.js + 清缓存(dev 热替换用) */
|
|
1469
1620
|
reloadAgents(): Promise<void>;
|
|
1621
|
+
/** 重新扫描 tasks + 重编译任务源码 + 重生成 faapi-tasks.js + 清队列缓存(dev 热替换用) */
|
|
1622
|
+
reloadTasks(): Promise<void>;
|
|
1470
1623
|
}
|
|
1471
1624
|
/**
|
|
1472
1625
|
* dev 模式应用启动 API
|
|
@@ -1513,4 +1666,4 @@ type ProdApp = AppBase;
|
|
|
1513
1666
|
*/
|
|
1514
1667
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1515
1668
|
|
|
1516
|
-
export { type AgentConfig, AgentCore, AgentMetadata, type AgentModule, type ProdApp as App, AppRegistries, CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, FaapiContext, FaapiError, FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, HelmetOptions, type InjectOptions, type InjectResponse, InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LlmConfig, type LlmModelConfig, LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseConfig, RouteManifest, RouteNotFoundError, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, ToolMetadata, type ToolModule, type ToolSchemaModule, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, clearAgentHandleFactory, collectRouteSchemaSources, createProdApp as createApp, createDevApp, createProdApp, createProgram, createPrograms, extractTypeInfo, getAgent, getAgentEntry, getApp, getInputTypeForMethod, getSkill, getTool, getToolSchemaPath, hydrateSkillRegistry, invalidateProgramCache, listSkills, loadAgentModule, loadConfig, loadEnv, loadToolModule, loadToolSchema, registerAgentHandleFactory, removeSkill, resolveAgentTools, resolveSubAgents, resolveTypeNode, upsertSkill };
|
|
1669
|
+
export { type AgentConfig, AgentCore, AgentMetadata, type AgentModule, type ProdApp as App, AppRegistries, CorsOptions, type CreateAppOptions, type CronScheduler, type DevApp, type FaapiConfig, FaapiContext, FaapiError, FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, HelmetOptions, type InjectOptions, type InjectResponse, InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LlmConfig, type LlmModelConfig, LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseConfig, RouteManifest, RouteNotFoundError, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, TASK_PATTERNS, type TaskBullMqOptions, TaskClient, type TaskConfig, TaskDriver, TaskFailedHandler, TaskManifest, type TaskPgBossOptions, TaskQueue, TaskRegistry, ToolMetadata, type ToolModule, type ToolSchemaModule, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, clearAgentHandleFactory, collectRouteSchemaSources, createProdApp as createApp, createCronScheduler, createDevApp, createProdApp, createProgram, createPrograms, createTaskQueue, extractTypeInfo, getAgent, getAgentEntry, getApp, getInputTypeForMethod, getSkill, getTool, getToolSchemaPath, hydrateSkillRegistry, invalidateProgramCache, listSkills, loadAgentModule, loadConfig, loadEnv, loadTaskDriver, loadToolModule, loadToolSchema, registerAgentHandleFactory, removeSkill, resolveAgentTools, resolveSubAgents, resolveTypeNode, scanTasks, upsertSkill };
|