@faapi/faapi 6.9.1 → 6.10.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/dist/cli/index.js +73 -48
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +47 -21
- package/dist/index.js +74 -48
- package/dist/index.js.map +1 -1
- package/dist/{routeTypes-CuGMDCGB.d.ts → routeTypes-m9wZHWyo.d.ts} +9 -34
- package/dist/testing.d.ts +2 -4
- package/dist/testing.js +53 -37
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,38 @@
|
|
|
1
|
-
import { A as AppRegistries, R as RouteManifest,
|
|
2
|
-
export {
|
|
1
|
+
import { F as FaapiMiddleware, A as AppRegistries, R as RouteManifest, a as FaapiContext, C as CorsOptions, T as TaskClient, H as HelmetOptions, L as LogConfig, 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, k as CreateLoggerOptions, l as Logger, W as WsRouteManifest } from './routeTypes-m9wZHWyo.js';
|
|
2
|
+
export { m as AgentHandleFactory, n as AgentHandleStore, o as AgentPathMeta, p as AgentRegistry, q as AgentToolDescriptor, r as FaapiContextConfig, s as FaapiTaskMeta, t as FailOptions, u as Injector, v as LogEntry, w as LogLevel, x as LogSink, y as RouteInfo, z as RouteInputSchema, B as RouteOutputSchema, D as RouteParamSchema, S as SkillRegistry, E as SseEvent, G as SseWriter, J as TaskContext, K as TaskDriverJob, M as TaskDriverProcess, N as TaskDriverRecord, O as TaskFailedInfo, P as TaskJob, Q as TaskJobStatus, U as TaskMetadata, V as TaskModule, X as TaskRegistriesSnapshot, Y as TaskRegistriesView, Z as ToolCore, _ as ToolPathMeta, $ as ToolRegistry, a0 as cors, a1 as createAppRegistries, a2 as createTaskRegistriesView, a3 as createTaskRegistry, a4 as helmet } from './routeTypes-m9wZHWyo.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';
|
|
6
6
|
import ts from 'typescript';
|
|
7
7
|
|
|
8
|
+
type LoggerFn = (messageOrObj: string | Record<string, unknown>, message?: string) => void;
|
|
9
|
+
interface LoggerOptions {
|
|
10
|
+
/**
|
|
11
|
+
* 自定义日志函数(完全接管输出,不再走统一管道——高级用法,一般无需配置)
|
|
12
|
+
*
|
|
13
|
+
* - 传入 `console.log`:纯文本格式 `GET /api/users 200 12ms`
|
|
14
|
+
* - 传入 pino logger:结构化日志 `logger.info({ method, path, status, durationMs }, 'request completed')`
|
|
15
|
+
*/
|
|
16
|
+
log?: LoggerFn;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 创建请求日志中间件(洋葱模型)
|
|
20
|
+
*
|
|
21
|
+
* 默认并入统一日志管道(egg 模型:访问日志与业务日志同管道,一份 config.log
|
|
22
|
+
* 管全部输出),`config.log.accessLog: false` 关闭。
|
|
23
|
+
*
|
|
24
|
+
* 日志格式(文本 message):GET /api/users 200 12ms
|
|
25
|
+
* 错误格式:POST /api/users 400 45ms - Error: ...
|
|
26
|
+
*
|
|
27
|
+
* before/after 一体,闭包变量共享开始时间,无需污染 ctx。
|
|
28
|
+
* 错误用 try/catch 捕获,记录后重新抛出(让上层处理)。
|
|
29
|
+
* 成功时从 next() 返回的 Response 读取状态码。
|
|
30
|
+
*
|
|
31
|
+
* log 函数每次请求时读取(options.log ?? defaultLog),运行时切换全局日志配置
|
|
32
|
+
* 同样生效。编程式自定义输出:`middlewares: [logger({ log: myFn })]`。
|
|
33
|
+
*/
|
|
34
|
+
declare function logger(options?: LoggerOptions): FaapiMiddleware;
|
|
35
|
+
|
|
8
36
|
/** HTTP 请求 handler 类型 */
|
|
9
37
|
type RequestHandler = (req: IncomingMessage, res: ServerResponse) => void;
|
|
10
38
|
/** WebSocket 升级 handler 类型 */
|
|
@@ -550,21 +578,19 @@ interface FaapiConfig {
|
|
|
550
578
|
etag?: EtagOptions | boolean;
|
|
551
579
|
/** 请求体大小限制(字节),默认 10MB(10 * 1024 * 1024) */
|
|
552
580
|
bodyLimit?: number;
|
|
553
|
-
/** 日志中间件配置 */
|
|
554
|
-
logger?: LoggerOptions | boolean;
|
|
555
581
|
/**
|
|
556
|
-
*
|
|
582
|
+
* 日志全局配置——唯一日志配置入口,业务日志与请求日志同管道(egg 模型,
|
|
583
|
+
* 详见 logger/logger.md;`config.logger` 独立配置已废除)
|
|
557
584
|
*
|
|
558
|
-
* - `false
|
|
559
|
-
* - `true` /
|
|
560
|
-
* - `LogConfig
|
|
561
|
-
*
|
|
562
|
-
* 两者互斥;`
|
|
563
|
-
*
|
|
585
|
+
* - `false`:全部静默(含 error 与请求日志,测试降噪)
|
|
586
|
+
* - `true` / 缺省:console 输出(consoleLevel 默认 'info'),管道不过滤
|
|
587
|
+
* - `LogConfig`:`dir` egg 风格文件输出(app.log 全量 + error.log 仅 error,
|
|
588
|
+
* splitByLevel 按级别四文件)或 `sink` 自定义管道接管(接 pino/winston),
|
|
589
|
+
* 两者互斥;`level` 管道阈值(不配不过滤)、`consoleLevel` console 出口阈值
|
|
590
|
+
* (不配 'info',false 关 console);请求日志无条件并入(`accessLog: false` 关闭)
|
|
564
591
|
*
|
|
565
|
-
* 生效范围:`ctx.log` / 参数注入 `log` / `createLogger` / `taskCtx.log
|
|
566
|
-
*
|
|
567
|
-
* 日志全局配置是进程级资源,多 app 同进程时后启动覆盖先启动。
|
|
592
|
+
* 生效范围:`ctx.log` / 参数注入 `log` / `createLogger` / `taskCtx.log` /
|
|
593
|
+
* 请求日志中间件。日志全局配置是进程级资源,多 app 同进程时后启动覆盖先启动。
|
|
568
594
|
*
|
|
569
595
|
* ```ts
|
|
570
596
|
* export default {
|
|
@@ -1495,14 +1521,14 @@ declare function loadEnv(rootDir: string): void;
|
|
|
1495
1521
|
/**
|
|
1496
1522
|
* 设置全局日志配置(进程级,`createAppBase` 启动时以 `config.log` 调用)
|
|
1497
1523
|
*
|
|
1498
|
-
* - `false
|
|
1499
|
-
* - `true` / `{}
|
|
1500
|
-
* - `{ level, sink }
|
|
1501
|
-
*
|
|
1524
|
+
* - `false`:全部静默(含 error 与请求日志,测试降噪场景)
|
|
1525
|
+
* - `true` / `{}`:console 出口默认 info,管道出口不过滤,无文件输出
|
|
1526
|
+
* - `{ level, consoleLevel, sink, dir, stdout, accessLog }`:egg 模型精细配置
|
|
1527
|
+
* (level 管管道出口、consoleLevel 管 console 出口;sink 与 dir 互斥)
|
|
1502
1528
|
* - `undefined`:重置为默认(编程式多 app 切换全局配置用)
|
|
1503
1529
|
*
|
|
1504
|
-
* LOG_LEVEL env
|
|
1505
|
-
*
|
|
1530
|
+
* LOG_LEVEL env 仅作为管道 level 的环境来源(consoleLevel 无 env)——本函数是
|
|
1531
|
+
* env 读取点,非法值启动期报错(fail fast)。
|
|
1506
1532
|
*/
|
|
1507
1533
|
declare function configureLogging(config?: LogConfig | boolean | undefined): void;
|
|
1508
1534
|
/**
|
|
@@ -1720,4 +1746,4 @@ type ProdApp = AppBase;
|
|
|
1720
1746
|
*/
|
|
1721
1747
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1722
1748
|
|
|
1723
|
-
export { type AgentConfig, AgentCore, AgentMetadata, type AgentModule, type ProdApp as App, AppRegistries, CorsOptions, type CreateAppOptions, CreateLoggerOptions, 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, LogConfig, Logger, 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, configureLogging, createProdApp as createApp, createCronScheduler, createDevApp, createLogger, createProdApp, createProgram, createPrograms, createTaskQueue, extractTypeInfo, flushLogging, getAgent, getAgentEntry, getApp, getInputTypeForMethod, getSkill, getTool, getToolSchemaPath, hydrateSkillRegistry, invalidateProgramCache, listSkills, loadAgentModule, loadConfig, loadEnv, loadTaskDriver, loadToolModule, loadToolSchema, registerAgentHandleFactory, removeSkill, resolveAgentTools, resolveSubAgents, resolveTypeNode, scanTasks, upsertSkill };
|
|
1749
|
+
export { type AgentConfig, AgentCore, AgentMetadata, type AgentModule, type ProdApp as App, AppRegistries, CorsOptions, type CreateAppOptions, CreateLoggerOptions, 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, LogConfig, Logger, type 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, configureLogging, createProdApp as createApp, createCronScheduler, createDevApp, createLogger, createProdApp, createProgram, createPrograms, createTaskQueue, extractTypeInfo, flushLogging, getAgent, getAgentEntry, getApp, getInputTypeForMethod, getSkill, getTool, getToolSchemaPath, hydrateSkillRegistry, invalidateProgramCache, listSkills, loadAgentModule, loadConfig, loadEnv, loadTaskDriver, loadToolModule, loadToolSchema, logger, registerAgentHandleFactory, removeSkill, resolveAgentTools, resolveSubAgents, resolveTypeNode, scanTasks, upsertSkill };
|
package/dist/index.js
CHANGED
|
@@ -2374,79 +2374,113 @@ function assertLogLevel(value, source) {
|
|
|
2374
2374
|
);
|
|
2375
2375
|
}
|
|
2376
2376
|
}
|
|
2377
|
-
var
|
|
2377
|
+
var LOGGING_STATE_KEY = /* @__PURE__ */ Symbol.for("faapi.logger.state");
|
|
2378
|
+
function createDefaultLoggingState() {
|
|
2379
|
+
return {
|
|
2380
|
+
level: null,
|
|
2381
|
+
consoleLevel: null,
|
|
2382
|
+
sink: null,
|
|
2383
|
+
fileSink: null,
|
|
2384
|
+
stdout: true,
|
|
2385
|
+
accessLog: true,
|
|
2386
|
+
disabled: false
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2389
|
+
function getLoggingState() {
|
|
2390
|
+
const g = globalThis;
|
|
2391
|
+
let state2 = g[LOGGING_STATE_KEY];
|
|
2392
|
+
if (!state2) {
|
|
2393
|
+
state2 = createDefaultLoggingState();
|
|
2394
|
+
g[LOGGING_STATE_KEY] = state2;
|
|
2395
|
+
}
|
|
2396
|
+
return state2;
|
|
2397
|
+
}
|
|
2378
2398
|
function configureLogging(config) {
|
|
2399
|
+
const state2 = getLoggingState();
|
|
2379
2400
|
const objectConfig = typeof config === "object" ? config : {};
|
|
2380
2401
|
if (objectConfig.sink && objectConfig.dir) {
|
|
2381
2402
|
throw new Error(
|
|
2382
2403
|
"[faapi] config.log.sink and config.log.dir are mutually exclusive: pick one output pipeline (custom sink OR built-in file logs)."
|
|
2383
2404
|
);
|
|
2384
2405
|
}
|
|
2385
|
-
void
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2406
|
+
void state2.fileSink?.close();
|
|
2407
|
+
state2.disabled = config === false;
|
|
2408
|
+
state2.sink = objectConfig.sink ?? null;
|
|
2409
|
+
state2.accessLog = objectConfig.accessLog ?? true;
|
|
2410
|
+
state2.stdout = objectConfig.stdout ?? true;
|
|
2411
|
+
state2.fileSink = objectConfig.dir ? createFileLogSink({ dir: objectConfig.dir, splitByLevel: objectConfig.splitByLevel }) : null;
|
|
2412
|
+
if (objectConfig.consoleLevel !== void 0) {
|
|
2413
|
+
if (objectConfig.consoleLevel !== false) {
|
|
2414
|
+
assertLogLevel(objectConfig.consoleLevel, "config.log.consoleLevel");
|
|
2415
|
+
}
|
|
2416
|
+
state2.consoleLevel = objectConfig.consoleLevel;
|
|
2417
|
+
} else {
|
|
2418
|
+
state2.consoleLevel = null;
|
|
2419
|
+
}
|
|
2391
2420
|
if (objectConfig.level !== void 0) {
|
|
2392
2421
|
assertLogLevel(objectConfig.level, "config.log.level");
|
|
2393
|
-
|
|
2422
|
+
state2.level = objectConfig.level;
|
|
2394
2423
|
} else if (process.env.LOG_LEVEL !== void 0) {
|
|
2395
2424
|
assertLogLevel(process.env.LOG_LEVEL, "LOG_LEVEL env");
|
|
2396
|
-
|
|
2425
|
+
state2.level = process.env.LOG_LEVEL;
|
|
2397
2426
|
} else {
|
|
2398
|
-
|
|
2427
|
+
state2.level = null;
|
|
2399
2428
|
}
|
|
2400
2429
|
}
|
|
2401
|
-
function
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2430
|
+
function pipelineThreshold() {
|
|
2431
|
+
return getLoggingState().level ?? void 0;
|
|
2432
|
+
}
|
|
2433
|
+
function consoleThreshold() {
|
|
2434
|
+
return getLoggingState().consoleLevel ?? "info";
|
|
2405
2435
|
}
|
|
2406
2436
|
function belowThreshold(level, threshold) {
|
|
2407
2437
|
return threshold !== void 0 && LEVEL_RANK[level] < LEVEL_RANK[threshold];
|
|
2408
2438
|
}
|
|
2409
2439
|
function getEffectiveLogLevel() {
|
|
2410
|
-
return
|
|
2440
|
+
return pipelineThreshold();
|
|
2411
2441
|
}
|
|
2412
|
-
var consoleSink = (entry) => {
|
|
2413
|
-
console[CONSOLE_METHOD[entry.level]](formatEntry(entry));
|
|
2414
|
-
};
|
|
2415
2442
|
var CONSOLE_METHOD = {
|
|
2416
2443
|
debug: "debug",
|
|
2417
2444
|
info: "info",
|
|
2418
2445
|
warn: "warn",
|
|
2419
2446
|
error: "error"
|
|
2420
2447
|
};
|
|
2448
|
+
function consoleDispatch(entry) {
|
|
2449
|
+
const threshold = consoleThreshold();
|
|
2450
|
+
if (threshold === false || belowThreshold(entry.level, threshold)) return;
|
|
2451
|
+
console[CONSOLE_METHOD[entry.level]](formatEntry(entry));
|
|
2452
|
+
}
|
|
2421
2453
|
function globalDispatch(entry) {
|
|
2422
|
-
|
|
2423
|
-
if (
|
|
2424
|
-
|
|
2454
|
+
const state2 = getLoggingState();
|
|
2455
|
+
if (state2.disabled) return;
|
|
2456
|
+
const pipeThreshold = pipelineThreshold();
|
|
2457
|
+
if (state2.sink) {
|
|
2458
|
+
if (!belowThreshold(entry.level, pipeThreshold)) state2.sink(entry);
|
|
2425
2459
|
return;
|
|
2426
2460
|
}
|
|
2427
|
-
if (
|
|
2428
|
-
|
|
2429
|
-
if (
|
|
2461
|
+
if (state2.fileSink) {
|
|
2462
|
+
if (!belowThreshold(entry.level, pipeThreshold)) state2.fileSink.write(entry);
|
|
2463
|
+
if (state2.stdout) consoleDispatch(entry);
|
|
2430
2464
|
return;
|
|
2431
2465
|
}
|
|
2432
|
-
|
|
2466
|
+
consoleDispatch(entry);
|
|
2433
2467
|
}
|
|
2434
2468
|
function writeLogEntry(entry) {
|
|
2435
|
-
if (belowThreshold(entry.level, globalThreshold())) return;
|
|
2436
2469
|
globalDispatch(entry);
|
|
2437
2470
|
}
|
|
2438
2471
|
function flushLogging() {
|
|
2439
|
-
|
|
2472
|
+
const state2 = getLoggingState();
|
|
2473
|
+
return state2.fileSink ? state2.fileSink.flush() : Promise.resolve();
|
|
2440
2474
|
}
|
|
2441
|
-
function
|
|
2442
|
-
|
|
2475
|
+
function isAccessLogEnabled() {
|
|
2476
|
+
const state2 = getLoggingState();
|
|
2477
|
+
return !state2.disabled && state2.accessLog;
|
|
2443
2478
|
}
|
|
2444
2479
|
function createLogger(scope, options) {
|
|
2445
2480
|
const baseScope = scope;
|
|
2446
2481
|
const baseFields = options?.fields;
|
|
2447
2482
|
const write = (level, message, callFields, scopeSuffix) => {
|
|
2448
|
-
|
|
2449
|
-
if (belowThreshold(level, threshold)) return;
|
|
2483
|
+
if (belowThreshold(level, options?.level)) return;
|
|
2450
2484
|
const entry = {
|
|
2451
2485
|
level,
|
|
2452
2486
|
message,
|
|
@@ -4017,18 +4051,14 @@ function levelForStatus(status) {
|
|
|
4017
4051
|
return "info";
|
|
4018
4052
|
}
|
|
4019
4053
|
function defaultLog(entry, text) {
|
|
4020
|
-
if (
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
writeLogEntry(logEntry);
|
|
4029
|
-
return;
|
|
4030
|
-
}
|
|
4031
|
-
console.log(entry);
|
|
4054
|
+
if (!isAccessLogEnabled()) return;
|
|
4055
|
+
writeLogEntry({
|
|
4056
|
+
level: levelForStatus(entry.status),
|
|
4057
|
+
message: text,
|
|
4058
|
+
time: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4059
|
+
scope: "access",
|
|
4060
|
+
fields: entry
|
|
4061
|
+
});
|
|
4032
4062
|
}
|
|
4033
4063
|
function logger(options = {}) {
|
|
4034
4064
|
return async (ctx, next) => {
|
|
@@ -5941,7 +5971,6 @@ function createServer(options) {
|
|
|
5941
5971
|
compression: compressionOption,
|
|
5942
5972
|
etag: etagOption,
|
|
5943
5973
|
registries,
|
|
5944
|
-
logger: loggerOption,
|
|
5945
5974
|
bodyLimit = DEFAULT_BODY_LIMIT,
|
|
5946
5975
|
http2: http2Option,
|
|
5947
5976
|
trustedProxy = false
|
|
@@ -5958,8 +5987,7 @@ function createServer(options) {
|
|
|
5958
5987
|
const helmOpts = typeof helmetOption === "object" ? helmetOption : {};
|
|
5959
5988
|
configMiddlewares.push(helmet(helmOpts));
|
|
5960
5989
|
}
|
|
5961
|
-
|
|
5962
|
-
if (loggerMiddlewareInst) configMiddlewares.push(loggerMiddlewareInst);
|
|
5990
|
+
configMiddlewares.push(logger());
|
|
5963
5991
|
if (etagOption) {
|
|
5964
5992
|
const etagOpts = typeof etagOption === "object" ? etagOption : {};
|
|
5965
5993
|
configMiddlewares.push(etag(etagOpts));
|
|
@@ -6941,7 +6969,6 @@ var FAAPI_CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
|
6941
6969
|
"compression",
|
|
6942
6970
|
"etag",
|
|
6943
6971
|
"bodyLimit",
|
|
6944
|
-
"logger",
|
|
6945
6972
|
"log",
|
|
6946
6973
|
"http2",
|
|
6947
6974
|
"trustedProxy",
|
|
@@ -7016,7 +7043,6 @@ async function createAppBase(options) {
|
|
|
7016
7043
|
helmet: config?.helmet,
|
|
7017
7044
|
compression: config?.compression,
|
|
7018
7045
|
etag: config?.etag,
|
|
7019
|
-
logger: config?.logger,
|
|
7020
7046
|
bodyLimit: config?.bodyLimit,
|
|
7021
7047
|
http2: config?.http2,
|
|
7022
7048
|
trustedProxy: config?.trustedProxy,
|