@ai-setting/roy-agent-core 1.5.16-test → 1.5.22
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/env/index.js +20 -8
- package/dist/env/log-trace/index.js +1 -1
- package/dist/env/prompt/index.js +1 -1
- package/dist/env/workflow/engine/index.js +1 -1
- package/dist/env/workflow/index.js +2 -2
- package/dist/index.js +6 -7
- package/dist/shared/@ai-setting/{roy-agent-core-xq8hhqb8.js → roy-agent-core-4wjywp3c.js} +4 -2
- package/dist/shared/@ai-setting/roy-agent-core-8jxva565.js +19 -0
- package/dist/shared/@ai-setting/roy-agent-core-avq1x4t7.js +84 -0
- package/dist/shared/@ai-setting/{roy-agent-core-gq20wsgv.js → roy-agent-core-ffb9fq4v.js} +23 -2
- package/dist/shared/@ai-setting/{roy-agent-core-93zfb3r1.js → roy-agent-core-mrcxzpbg.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-rhmtwnw1.js → roy-agent-core-pw7cv1px.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-gbqcyegm.js → roy-agent-core-rccptwv0.js} +512 -673
- package/dist/shared/@ai-setting/{roy-agent-core-wrcy0h6z.js → roy-agent-core-ty94k28r.js} +1 -1
- package/package.json +1 -1
- package/dist/config/index.d.ts +0 -1250
- package/dist/env/agent/index.d.ts +0 -2279
- package/dist/env/commands/index.d.ts +0 -1131
- package/dist/env/debug/formatters/index.d.ts +0 -236
- package/dist/env/debug/index.d.ts +0 -1652
- package/dist/env/hook/index.d.ts +0 -279
- package/dist/env/index.d.ts +0 -3481
- package/dist/env/llm/index.d.ts +0 -1760
- package/dist/env/log-trace/index.d.ts +0 -1574
- package/dist/env/mcp/index.d.ts +0 -1331
- package/dist/env/mcp/tool/index.d.ts +0 -183
- package/dist/env/memory/built-in/index.d.ts +0 -232
- package/dist/env/memory/index.d.ts +0 -1799
- package/dist/env/memory/plugin/index.d.ts +0 -747
- package/dist/env/prompt/index.d.ts +0 -1164
- package/dist/env/session/index.d.ts +0 -1908
- package/dist/env/session/storage/index.d.ts +0 -564
- package/dist/env/skill/index.d.ts +0 -1266
- package/dist/env/skill/tool/index.d.ts +0 -193
- package/dist/env/task/delegate/index.d.ts +0 -1612
- package/dist/env/task/events/index.d.ts +0 -171
- package/dist/env/task/hooks/index.d.ts +0 -624
- package/dist/env/task/index.d.ts +0 -1553
- package/dist/env/task/plugins/index.d.ts +0 -466
- package/dist/env/task/storage/index.d.ts +0 -241
- package/dist/env/task/tools/index.d.ts +0 -1485
- package/dist/env/task/tools/operation/index.d.ts +0 -1484
- package/dist/env/tool/built-in/index.d.ts +0 -218
- package/dist/env/tool/index.d.ts +0 -1396
- package/dist/env/workflow/decorators/index.d.ts +0 -2161
- package/dist/env/workflow/engine/index.d.ts +0 -3453
- package/dist/env/workflow/index.d.ts +0 -3546
- package/dist/env/workflow/nodes/index.d.ts +0 -2092
- package/dist/env/workflow/service/index.d.ts +0 -227
- package/dist/env/workflow/storage/index.d.ts +0 -165
- package/dist/env/workflow/tools/index.d.ts +0 -416
- package/dist/env/workflow/types/index.d.ts +0 -2255
- package/dist/env/workflow/utils/index.d.ts +0 -2031
- package/dist/index.d.ts +0 -7858
|
@@ -1,1574 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Hook 系统统一类型定义
|
|
3
|
-
*
|
|
4
|
-
* 提供所有 Component 使用的统一 Hook 接口
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Hook 执行阶段
|
|
8
|
-
*/
|
|
9
|
-
type HookPhase = "before" | "after" | "error";
|
|
10
|
-
/**
|
|
11
|
-
* Hook 元信息
|
|
12
|
-
*/
|
|
13
|
-
interface HookMeta {
|
|
14
|
-
/** Hook 唯一名称 */
|
|
15
|
-
name: string;
|
|
16
|
-
/** 执行优先级,默认 0,数值越小越先执行 */
|
|
17
|
-
priority?: number;
|
|
18
|
-
/** 描述 */
|
|
19
|
-
description?: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* 统一 Hook 上下文
|
|
23
|
-
*
|
|
24
|
-
* 泛型参数 T 表示该 Component 的上下文数据类型
|
|
25
|
-
*/
|
|
26
|
-
interface HookContext<T = unknown> {
|
|
27
|
-
/** 当前组件信息 */
|
|
28
|
-
component: {
|
|
29
|
-
/** 组件名称 */
|
|
30
|
-
name: string;
|
|
31
|
-
/** 组件版本 */
|
|
32
|
-
version: string;
|
|
33
|
-
};
|
|
34
|
-
/** 上下文数据(Component 特定) */
|
|
35
|
-
data: T;
|
|
36
|
-
/** 元数据(可用于传递额外信息) */
|
|
37
|
-
metadata: Record<string, unknown>;
|
|
38
|
-
/** 执行阶段 */
|
|
39
|
-
phase: HookPhase;
|
|
40
|
-
/** 关联的 hook 点名称 */
|
|
41
|
-
hookPoint: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* 统一 Hook 接口
|
|
45
|
-
*
|
|
46
|
-
* 所有 Component 的 Hook 都应实现此接口
|
|
47
|
-
*/
|
|
48
|
-
interface Hook<T = unknown> extends HookMeta {
|
|
49
|
-
/** 执行 Hook
|
|
50
|
-
* @param ctx 上下文
|
|
51
|
-
*/
|
|
52
|
-
execute(ctx: HookContext<T>): void | Promise<void>;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Hook 函数类型(便捷别名)
|
|
56
|
-
*/
|
|
57
|
-
type HookFn<T = unknown> = (ctx: HookContext<T>) => void | Promise<void>;
|
|
58
|
-
/**
|
|
59
|
-
* Hook 干预动作类型
|
|
60
|
-
*/
|
|
61
|
-
type HookActionType = "stop" | "retry" | "compress" | "extract_memory" | "inject_message" | "skip_tool" | "custom";
|
|
62
|
-
/**
|
|
63
|
-
* Hook 干预动作
|
|
64
|
-
*/
|
|
65
|
-
interface HookAction {
|
|
66
|
-
/** 动作类型 */
|
|
67
|
-
type: HookActionType;
|
|
68
|
-
/** 动作参数 */
|
|
69
|
-
params?: Record<string, unknown>;
|
|
70
|
-
/** 动作描述 */
|
|
71
|
-
description?: string;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Hook 执行结果
|
|
75
|
-
*/
|
|
76
|
-
interface HookResult {
|
|
77
|
-
/** 是否被停止 */
|
|
78
|
-
stopped?: boolean;
|
|
79
|
-
/** 干预动作(如果有) */
|
|
80
|
-
action?: HookAction;
|
|
81
|
-
/** 执行结果数据 */
|
|
82
|
-
results?: unknown[];
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* HookManager 配置
|
|
86
|
-
*/
|
|
87
|
-
interface HookManagerOptions {
|
|
88
|
-
/** 组件名称(用于上下文) */
|
|
89
|
-
componentName?: string;
|
|
90
|
-
/** 组件版本(用于上下文) */
|
|
91
|
-
componentVersion?: string;
|
|
92
|
-
/** 默认优先级 */
|
|
93
|
-
defaultPriority?: number;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* HookManager
|
|
97
|
-
*
|
|
98
|
-
* 统一管理 Component 的 Hook
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* const manager = new HookManager<MyContext>();
|
|
103
|
-
*
|
|
104
|
-
* // 注册 Hook
|
|
105
|
-
* manager.register("before-action", {
|
|
106
|
-
* name: "my-hook",
|
|
107
|
-
* priority: 10,
|
|
108
|
-
* execute: async (ctx) => { /* ... *\/ }
|
|
109
|
-
* });
|
|
110
|
-
*
|
|
111
|
-
* // 执行 Hooks
|
|
112
|
-
* await manager.execute("before-action", data);
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
declare class HookManager<T = unknown> {
|
|
116
|
-
private _hooks;
|
|
117
|
-
private componentName;
|
|
118
|
-
private componentVersion;
|
|
119
|
-
private defaultPriority;
|
|
120
|
-
constructor(options?: HookManagerOptions);
|
|
121
|
-
/**
|
|
122
|
-
* 注册 Hook 到指定 hook 点
|
|
123
|
-
*/
|
|
124
|
-
register(hookPoint: string, hook: Hook<T>): void;
|
|
125
|
-
/**
|
|
126
|
-
* 注册多个 Hook
|
|
127
|
-
*/
|
|
128
|
-
registerMany(hookPoint: string, hooks: Hook<T>[]): void;
|
|
129
|
-
/**
|
|
130
|
-
* 按名称取消注册 Hook
|
|
131
|
-
* @returns 是否成功取消注册
|
|
132
|
-
*/
|
|
133
|
-
unregister(hookPoint: string, name: string): boolean;
|
|
134
|
-
/**
|
|
135
|
-
* 取消注册所有指定 hook 点的 Hook
|
|
136
|
-
*/
|
|
137
|
-
unregisterAll(hookPoint: string): void;
|
|
138
|
-
/**
|
|
139
|
-
* 执行指定 hook 点的所有 Hook
|
|
140
|
-
*/
|
|
141
|
-
execute(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
142
|
-
/**
|
|
143
|
-
* 执行指定 hook 点的所有 Hook 并收集返回值
|
|
144
|
-
*/
|
|
145
|
-
executeAndCollect<R>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<R[]>;
|
|
146
|
-
/**
|
|
147
|
-
* 获取指定 hook 点的 Hook 数量
|
|
148
|
-
*/
|
|
149
|
-
count(hookPoint: string): number;
|
|
150
|
-
/**
|
|
151
|
-
* 清空所有 Hook
|
|
152
|
-
*/
|
|
153
|
-
clear(): void;
|
|
154
|
-
/**
|
|
155
|
-
* 获取所有已注册的 hook 点
|
|
156
|
-
*/
|
|
157
|
-
getHookPoints(): string[];
|
|
158
|
-
/**
|
|
159
|
-
* 检查 hook 点是否有任何 Hook
|
|
160
|
-
*/
|
|
161
|
-
hasHooks(hookPoint: string): boolean;
|
|
162
|
-
/**
|
|
163
|
-
* 设置组件信息
|
|
164
|
-
*/
|
|
165
|
-
setComponentInfo(name: string, version: string): void;
|
|
166
|
-
/**
|
|
167
|
-
* 获取所有注册的 hooks(只读副本)
|
|
168
|
-
*/
|
|
169
|
-
get hooks(): ReadonlyMap<string, Hook<T>[]>;
|
|
170
|
-
/**
|
|
171
|
-
* 执行 Hook 并支持干预机制
|
|
172
|
-
*
|
|
173
|
-
* 如果任何一个 Hook 返回 { stop: true },执行会停止
|
|
174
|
-
* Hook 可以通过返回 HookResult 来执行干预动作
|
|
175
|
-
*
|
|
176
|
-
* @param hookPoint Hook 点
|
|
177
|
-
* @param data 数据
|
|
178
|
-
* @param metadata 元数据
|
|
179
|
-
* @returns 执行结果
|
|
180
|
-
*/
|
|
181
|
-
executeWithIntervention(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<HookResult>;
|
|
182
|
-
/**
|
|
183
|
-
* 获取或创建 hook 列表
|
|
184
|
-
*/
|
|
185
|
-
private getOrCreateHooks;
|
|
186
|
-
/**
|
|
187
|
-
* 按优先级排序 Hook
|
|
188
|
-
*
|
|
189
|
-
* 优先级相同时保持原顺序(稳定排序)
|
|
190
|
-
*/
|
|
191
|
-
private sortHooks;
|
|
192
|
-
/**
|
|
193
|
-
* 创建 Hook 上下文
|
|
194
|
-
*/
|
|
195
|
-
private createContext;
|
|
196
|
-
/**
|
|
197
|
-
* 安全执行 Hook(错误隔离)
|
|
198
|
-
*/
|
|
199
|
-
private safeExecute;
|
|
200
|
-
/**
|
|
201
|
-
* 安全执行 Hook 并返回结果
|
|
202
|
-
*/
|
|
203
|
-
private safeExecuteAndReturn;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* @fileoverview Log & Trace Component 类型定义
|
|
207
|
-
*/
|
|
208
|
-
/**
|
|
209
|
-
* Span 上下文
|
|
210
|
-
*/
|
|
211
|
-
interface SpanContext {
|
|
212
|
-
traceId: string;
|
|
213
|
-
spanId: string;
|
|
214
|
-
parentSpanId?: string;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Span 属性
|
|
218
|
-
*/
|
|
219
|
-
interface SpanAttributes {
|
|
220
|
-
[key: string]: string | number | boolean | undefined | Record<string, unknown>;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Span
|
|
224
|
-
*/
|
|
225
|
-
interface Span {
|
|
226
|
-
traceId: string;
|
|
227
|
-
spanId: string;
|
|
228
|
-
parentSpanId?: string;
|
|
229
|
-
name: string;
|
|
230
|
-
kind: SpanKind;
|
|
231
|
-
status: SpanStatus;
|
|
232
|
-
startTime: number;
|
|
233
|
-
endTime?: number;
|
|
234
|
-
attributes: SpanAttributes;
|
|
235
|
-
result?: unknown;
|
|
236
|
-
error?: string;
|
|
237
|
-
children?: Span[];
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Span 类型
|
|
241
|
-
*/
|
|
242
|
-
declare enum SpanKind {
|
|
243
|
-
CLIENT = "client",
|
|
244
|
-
SERVER = "server",
|
|
245
|
-
INTERNAL = "internal"
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Span 状态
|
|
249
|
-
*/
|
|
250
|
-
declare enum SpanStatus {
|
|
251
|
-
OK = "ok",
|
|
252
|
-
ERROR = "error"
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Trace 信息
|
|
256
|
-
*/
|
|
257
|
-
interface TraceInfo {
|
|
258
|
-
traceId: string;
|
|
259
|
-
rootSpanName: string;
|
|
260
|
-
startTime: number;
|
|
261
|
-
endTime?: number;
|
|
262
|
-
duration?: number;
|
|
263
|
-
spanCount: number;
|
|
264
|
-
status: "ok" | "error" | "mixed";
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* SpanStorage 接口
|
|
268
|
-
*/
|
|
269
|
-
interface SpanStorage {
|
|
270
|
-
initialize(): Promise<void>;
|
|
271
|
-
save(span: Span): void;
|
|
272
|
-
saveBatch(spans: Span[]): void;
|
|
273
|
-
findByTraceId(traceId: string): Span[];
|
|
274
|
-
findBySpanId(spanId: string): Span | undefined;
|
|
275
|
-
listTraces(limit?: number): TraceInfo[];
|
|
276
|
-
deleteByTraceId(traceId: string): void;
|
|
277
|
-
close(): void;
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* TraceContext 选项
|
|
281
|
-
*/
|
|
282
|
-
interface TraceContextOptions {
|
|
283
|
-
/** 追踪器实现类型 */
|
|
284
|
-
impl?: "lightweight" | "opentelemetry";
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* ITraceContext 接口
|
|
288
|
-
*/
|
|
289
|
-
interface ITraceContext {
|
|
290
|
-
/** 获取当前请求的 requestId */
|
|
291
|
-
getRequestId(): string | undefined;
|
|
292
|
-
/** 获取当前 Span 上下文 */
|
|
293
|
-
getSpanContext(): SpanContext | undefined;
|
|
294
|
-
/** 更新当前 Span 上下文(用于设置子 span 的 parentSpanId) */
|
|
295
|
-
setSpanContext(spanContext: SpanContext): void;
|
|
296
|
-
/** 在已存在的追踪上下文中执行函数 */
|
|
297
|
-
runWithContext<T>(fn: () => T): T;
|
|
298
|
-
/** 在新追踪上下文中执行函数(同步) */
|
|
299
|
-
runWithNewContext<T>(requestId: string, sessionId: string | undefined, fn: () => T): T;
|
|
300
|
-
/** 在已存在的追踪上下文中执行异步函数 */
|
|
301
|
-
runWithContextAsync<T>(fn: () => Promise<T>): Promise<T>;
|
|
302
|
-
/** 在新追踪上下文中执行函数(异步) */
|
|
303
|
-
runWithNewContextAsync<T>(requestId: string, sessionId: string | undefined, fn: () => Promise<T>): Promise<T>;
|
|
304
|
-
/** 生成新的 requestId */
|
|
305
|
-
generateRequestId(): string;
|
|
306
|
-
/** 初始化追踪上下文(请求入口调用) */
|
|
307
|
-
initContext(requestId: string, sessionId?: string): void;
|
|
308
|
-
/** 创建并进入新的追踪上下文(请求入口使用,返回清理函数) */
|
|
309
|
-
startNewContext(requestId: string, sessionId?: string): () => void;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* 日志级别
|
|
313
|
-
*/
|
|
314
|
-
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
315
|
-
/**
|
|
316
|
-
* Logger 配置
|
|
317
|
-
*/
|
|
318
|
-
interface LoggerConfig {
|
|
319
|
-
level?: LogLevel;
|
|
320
|
-
prefix?: string;
|
|
321
|
-
filename?: string;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Logger 接口
|
|
325
|
-
*/
|
|
326
|
-
interface ILogger {
|
|
327
|
-
debug(message: string, data?: unknown): void;
|
|
328
|
-
info(message: string, data?: unknown): void;
|
|
329
|
-
warn(message: string, data?: unknown): void;
|
|
330
|
-
error(message: string, data?: unknown): void;
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* SQLite 存储实现
|
|
334
|
-
*
|
|
335
|
-
* 根据运行时环境使用 bun:sqlite 或 better-sqlite3
|
|
336
|
-
*
|
|
337
|
-
* 注意:此实现仅支持 SQLite 文件存储,不支持 :memory: 模式
|
|
338
|
-
* WAL 模式默认启用以支持跨进程并发访问
|
|
339
|
-
*/
|
|
340
|
-
declare class SQLiteSpanStorage implements SpanStorage {
|
|
341
|
-
private db;
|
|
342
|
-
private dbPath;
|
|
343
|
-
private initialized;
|
|
344
|
-
/**
|
|
345
|
-
* 创建 SQLiteSpanStorage 实例
|
|
346
|
-
*
|
|
347
|
-
* @param dbPath SQLite 数据库文件路径(必填)
|
|
348
|
-
* @throws Error 如果 dbPath 为空或为 :memory:
|
|
349
|
-
*/
|
|
350
|
-
constructor(dbPath: string);
|
|
351
|
-
initialize(): Promise<void>;
|
|
352
|
-
save(span: Span): void;
|
|
353
|
-
saveBatch(spans: Span[]): void;
|
|
354
|
-
findByTraceId(traceId: string): Span[];
|
|
355
|
-
findBySpanId(spanId: string): Span | undefined;
|
|
356
|
-
listTraces(limit?: number): TraceInfo[];
|
|
357
|
-
deleteByTraceId(traceId: string): void;
|
|
358
|
-
close(): void;
|
|
359
|
-
private rowToSpan;
|
|
360
|
-
private buildTree;
|
|
361
|
-
}
|
|
362
|
-
import { ZodType as ZodType3, ZodTypeDef as ZodTypeDef2 } from "zod";
|
|
363
|
-
/**
|
|
364
|
-
* @fileoverview Environment types and interfaces
|
|
365
|
-
*
|
|
366
|
-
* 定义 Environment 的核心类型,包括:
|
|
367
|
-
* - Environment 配置
|
|
368
|
-
* - EnvEvent 事件类型
|
|
369
|
-
*/
|
|
370
|
-
/**
|
|
371
|
-
* Environment Config
|
|
372
|
-
*/
|
|
373
|
-
interface EnvironmentConfig {
|
|
374
|
-
/** 环境名称 */
|
|
375
|
-
name: string;
|
|
376
|
-
/** 环境版本 */
|
|
377
|
-
version: string;
|
|
378
|
-
/** 是否启用 */
|
|
379
|
-
enabled: boolean;
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* EnvEvent 元信息
|
|
383
|
-
*/
|
|
384
|
-
interface EnvEventMetadata {
|
|
385
|
-
/** 触发事件的 session ID */
|
|
386
|
-
trigger_session_id?: string;
|
|
387
|
-
/** 触发事件的 agent ID */
|
|
388
|
-
trigger_agent_id?: string;
|
|
389
|
-
/** Agent 名称 */
|
|
390
|
-
trigger_agent_name?: string;
|
|
391
|
-
/** 环境名称 */
|
|
392
|
-
env_name?: string;
|
|
393
|
-
/** 事件来源 */
|
|
394
|
-
source?: string;
|
|
395
|
-
/** 其他元数据 */
|
|
396
|
-
[key: string]: unknown;
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* EnvEvent
|
|
400
|
-
*
|
|
401
|
-
* 统一的事件机制,用于:
|
|
402
|
-
* - Stream 事件(stream.start, stream.text, stream.completed 等)
|
|
403
|
-
* - 工具调用事件(tool.call, tool.result 等)
|
|
404
|
-
* - 会话事件(session.created, session.updated 等)
|
|
405
|
-
* - Agent 事件(agent.thinking, agent.acting 等)
|
|
406
|
-
*/
|
|
407
|
-
interface EnvEvent<T = unknown> {
|
|
408
|
-
/** 事件 ID */
|
|
409
|
-
id: string;
|
|
410
|
-
/** 事件类型 */
|
|
411
|
-
type: string;
|
|
412
|
-
/** 时间戳 */
|
|
413
|
-
timestamp: number;
|
|
414
|
-
/** 元信息 */
|
|
415
|
-
metadata: EnvEventMetadata;
|
|
416
|
-
/** 事件负载 */
|
|
417
|
-
payload: T;
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* EnvEvent Handler
|
|
421
|
-
*/
|
|
422
|
-
type EnvEventHandler = (event: EnvEvent) => void | Promise<void>;
|
|
423
|
-
/**
|
|
424
|
-
* EnvEvent 创建参数(部分属性,可选)
|
|
425
|
-
*
|
|
426
|
-
* 用于 pushEnvEvent 方法,允许传入部分属性,自动补全必填字段
|
|
427
|
-
*/
|
|
428
|
-
interface EnvEventInput {
|
|
429
|
-
/** 事件类型(必填) */
|
|
430
|
-
type: string;
|
|
431
|
-
/** 事件 ID(可选,自动生成) */
|
|
432
|
-
id?: string;
|
|
433
|
-
/** 时间戳(可选,自动生成) */
|
|
434
|
-
timestamp?: number;
|
|
435
|
-
/** 元信息(可选,自动创建) */
|
|
436
|
-
metadata?: EnvEventMetadata;
|
|
437
|
-
/** 事件负载(可选) */
|
|
438
|
-
payload?: unknown;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Action 类型
|
|
442
|
-
*/
|
|
443
|
-
interface Action {
|
|
444
|
-
/** Action 类型 */
|
|
445
|
-
type: string;
|
|
446
|
-
/** Action 参数 */
|
|
447
|
-
params?: Record<string, unknown>;
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* Tool Result
|
|
451
|
-
*/
|
|
452
|
-
interface ToolResult {
|
|
453
|
-
/** 是否成功 */
|
|
454
|
-
success: boolean;
|
|
455
|
-
/** 输出内容 */
|
|
456
|
-
output: string | Record<string, unknown>;
|
|
457
|
-
/** 错误信息 */
|
|
458
|
-
error?: string;
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Context
|
|
462
|
-
*/
|
|
463
|
-
interface Context {
|
|
464
|
-
/** 会话 ID */
|
|
465
|
-
sessionId?: string;
|
|
466
|
-
/** 用户 ID */
|
|
467
|
-
userId?: string;
|
|
468
|
-
/** 工作目录 */
|
|
469
|
-
workdir?: string;
|
|
470
|
-
/** 中断信号 */
|
|
471
|
-
abort?: AbortSignal;
|
|
472
|
-
/** 元数据 */
|
|
473
|
-
metadata?: Record<string, unknown>;
|
|
474
|
-
}
|
|
475
|
-
type AbortSignalType = AbortSignal;
|
|
476
|
-
/**
|
|
477
|
-
* Agent 执行上下文
|
|
478
|
-
*/
|
|
479
|
-
interface AgentContext {
|
|
480
|
-
/** 会话 ID */
|
|
481
|
-
sessionId?: string;
|
|
482
|
-
/** 消息 ID */
|
|
483
|
-
messageId?: string;
|
|
484
|
-
/** 中断信号 */
|
|
485
|
-
abort?: AbortSignalType;
|
|
486
|
-
/** 额外的上下文信息(会添加到 system prompt) */
|
|
487
|
-
additionInfo?: string;
|
|
488
|
-
/** 元数据 */
|
|
489
|
-
metadata?: Record<string, unknown>;
|
|
490
|
-
/** Agent 类型(用于选择 Behavior Spec) */
|
|
491
|
-
agentType?: string;
|
|
492
|
-
/** 使用的模型 */
|
|
493
|
-
model?: string;
|
|
494
|
-
/** 允许的工具列表(上下文级别,会覆盖 agent 配置) */
|
|
495
|
-
allowedTools?: string[];
|
|
496
|
-
/** 拒绝的工具列表(上下文级别,会覆盖 agent 配置) */
|
|
497
|
-
deniedTools?: string[];
|
|
498
|
-
/** 是否过滤 history 中的 tool 消息(上下文级别,会覆盖 agent 配置) */
|
|
499
|
-
filterHistory?: boolean;
|
|
500
|
-
}
|
|
501
|
-
import { ZodType as ZodType2, ZodTypeDef } from "zod";
|
|
502
|
-
/**
|
|
503
|
-
* ConfigSource 类型
|
|
504
|
-
*/
|
|
505
|
-
type ConfigSourceType = "memory" | "file" | "env" | "remote";
|
|
506
|
-
/**
|
|
507
|
-
* SourceRegistration - Source 注册配置
|
|
508
|
-
*/
|
|
509
|
-
interface SourceRegistration {
|
|
510
|
-
/** Source 类型 */
|
|
511
|
-
type: ConfigSourceType;
|
|
512
|
-
/** 相对路径(file 类型使用,基于 XDG_DATA_HOME) */
|
|
513
|
-
relativePath?: string;
|
|
514
|
-
/** 环境变量前缀(env 类型使用) */
|
|
515
|
-
envPrefix?: string;
|
|
516
|
-
/** 优先级,默认值:memory=0, file=10, env=20 */
|
|
517
|
-
priority?: number;
|
|
518
|
-
/** 是否可选(file 类型使用,文件不存在时不报错) */
|
|
519
|
-
optional?: boolean;
|
|
520
|
-
/** 是否启用文件监听(file 类型使用,默认 true) */
|
|
521
|
-
watch?: boolean;
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* KeyRegistration - 配置 Key 注册
|
|
525
|
-
*/
|
|
526
|
-
interface KeyRegistration {
|
|
527
|
-
/** 配置 Key(支持点号嵌套,如 "llm.provider") */
|
|
528
|
-
key: string;
|
|
529
|
-
/** 优先使用的 Source 列表(按优先级从低到高) */
|
|
530
|
-
sources: ConfigSourceType[];
|
|
531
|
-
/** 自定义优先级(覆盖默认优先级) */
|
|
532
|
-
priority?: number;
|
|
533
|
-
/** 默认值 */
|
|
534
|
-
default?: unknown;
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* ComponentRegistration - Component 注册配置
|
|
538
|
-
*/
|
|
539
|
-
interface ComponentRegistration {
|
|
540
|
-
/** Component 名称 */
|
|
541
|
-
name: string;
|
|
542
|
-
/** 声明需要的 Source */
|
|
543
|
-
sources: SourceRegistration[];
|
|
544
|
-
/** 声明需要的配置 Key */
|
|
545
|
-
keys: KeyRegistration[];
|
|
546
|
-
/** 配置 Schema(用于验证,可选) */
|
|
547
|
-
schema?: ZodType2<unknown, ZodTypeDef, unknown>;
|
|
548
|
-
/** 默认值 */
|
|
549
|
-
defaults?: Record<string, unknown>;
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* ConfigSource 接口
|
|
553
|
-
*/
|
|
554
|
-
interface ConfigSource {
|
|
555
|
-
readonly name: ConfigSourceType;
|
|
556
|
-
readonly priority: number;
|
|
557
|
-
read(key: string): unknown | undefined;
|
|
558
|
-
write?(key: string, value: unknown): boolean;
|
|
559
|
-
delete?(key: string): boolean;
|
|
560
|
-
list(): Array<{
|
|
561
|
-
key: string;
|
|
562
|
-
value: unknown;
|
|
563
|
-
}>;
|
|
564
|
-
watch?(callback: (event: ConfigChangeEvent) => void): () => void;
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Config 变更事件
|
|
568
|
-
*/
|
|
569
|
-
interface ConfigChangeEvent {
|
|
570
|
-
type: "change" | "add" | "delete";
|
|
571
|
-
key: string;
|
|
572
|
-
oldValue: unknown;
|
|
573
|
-
newValue: unknown;
|
|
574
|
-
source: ConfigSourceType;
|
|
575
|
-
timestamp: number;
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* ConfigComponent 配置
|
|
579
|
-
*/
|
|
580
|
-
interface ConfigComponentOptions {
|
|
581
|
-
/** 持久化文件路径 */
|
|
582
|
-
persistFile?: string;
|
|
583
|
-
/** 防抖延迟(ms) */
|
|
584
|
-
debounceMs?: number;
|
|
585
|
-
/** 最大批量大小 */
|
|
586
|
-
maxBatchSize?: number;
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* ConfigComponent
|
|
590
|
-
*/
|
|
591
|
-
declare class ConfigComponent extends BaseComponent {
|
|
592
|
-
readonly name = "config";
|
|
593
|
-
readonly version = "1.0.0";
|
|
594
|
-
private memorySource;
|
|
595
|
-
private sources;
|
|
596
|
-
private componentSchemas;
|
|
597
|
-
private componentPaths;
|
|
598
|
-
private watchers;
|
|
599
|
-
/** Source watch unsubscribe 函数 */
|
|
600
|
-
private sourceUnwatchFns;
|
|
601
|
-
private persistQueue;
|
|
602
|
-
private persistFile?;
|
|
603
|
-
/** Key 注册表:key -> KeyRegistration */
|
|
604
|
-
private keyRegistry;
|
|
605
|
-
/** Component 注册表:name -> ComponentRegistration */
|
|
606
|
-
private componentRegistrations;
|
|
607
|
-
/** 协议解析器 */
|
|
608
|
-
private protocolResolver?;
|
|
609
|
-
/** XDG_DATA_HOME 缓存(用于测试注入) */
|
|
610
|
-
private __xdgDataHome?;
|
|
611
|
-
/** Source 注册配置(延迟创建) */
|
|
612
|
-
private pendingSources;
|
|
613
|
-
/** Source 唯一标识缓存:type:relativePath -> true */
|
|
614
|
-
private sourceKeys;
|
|
615
|
-
/**
|
|
616
|
-
* 获取 XDG_DATA_HOME(动态获取,支持运行时覆盖)
|
|
617
|
-
*/
|
|
618
|
-
getXdgDataHome(): string;
|
|
619
|
-
/**
|
|
620
|
-
* 设置 XDG_DATA_HOME(用于测试)
|
|
621
|
-
*/
|
|
622
|
-
setXdgDataHome(path: string): void;
|
|
623
|
-
constructor(options?: ConfigComponentOptions);
|
|
624
|
-
/**
|
|
625
|
-
* 注册 Component Schema
|
|
626
|
-
*/
|
|
627
|
-
register(schema: ComponentSchema): void;
|
|
628
|
-
/**
|
|
629
|
-
* 注销 Component
|
|
630
|
-
*/
|
|
631
|
-
unregister(name: string): boolean;
|
|
632
|
-
/**
|
|
633
|
-
* 检查是否已注册
|
|
634
|
-
*/
|
|
635
|
-
isRegistered(name: string): boolean;
|
|
636
|
-
/**
|
|
637
|
-
* 列出所有注册的 Component
|
|
638
|
-
*/
|
|
639
|
-
listComponents(): string[];
|
|
640
|
-
/**
|
|
641
|
-
* 获取配置值(支持 xxx.yyy.zzz 嵌套访问)
|
|
642
|
-
* 支持两种模式:
|
|
643
|
-
* 1. 完整 key:直接查找,如 "component.key" -> 读取 "component.key"
|
|
644
|
-
* 2. 嵌套对象:查找 component 对象后从中获取属性,如 "component.nested.deep"
|
|
645
|
-
* -> 先找 "component.nested",再获取其 "deep" 属性
|
|
646
|
-
*/
|
|
647
|
-
get(key: string): unknown;
|
|
648
|
-
/**
|
|
649
|
-
* 批量获取
|
|
650
|
-
*/
|
|
651
|
-
getMany(keys: string[]): Record<string, unknown>;
|
|
652
|
-
/**
|
|
653
|
-
* 设置配置值(内存级别 + 异步持久化)
|
|
654
|
-
*/
|
|
655
|
-
set(key: string, value: unknown): Promise<void>;
|
|
656
|
-
/**
|
|
657
|
-
* 批量设置
|
|
658
|
-
*/
|
|
659
|
-
setMany(config: Record<string, unknown>): Promise<void>;
|
|
660
|
-
/**
|
|
661
|
-
* 获取 Component 的路径
|
|
662
|
-
*/
|
|
663
|
-
getPath(component: string, subPath?: string): string;
|
|
664
|
-
/**
|
|
665
|
-
* 手动持久化
|
|
666
|
-
*/
|
|
667
|
-
save(key?: string): Promise<void>;
|
|
668
|
-
/**
|
|
669
|
-
* 重置到默认值
|
|
670
|
-
*/
|
|
671
|
-
reset(key: string): Promise<void>;
|
|
672
|
-
/**
|
|
673
|
-
* 监听配置变更
|
|
674
|
-
*/
|
|
675
|
-
watch(pattern: string, callback: ComponentWatchCallback): () => void;
|
|
676
|
-
/**
|
|
677
|
-
* 添加配置源
|
|
678
|
-
*/
|
|
679
|
-
addSource(source: ConfigSource): void;
|
|
680
|
-
/**
|
|
681
|
-
* 获取 Source 唯一标识
|
|
682
|
-
*/
|
|
683
|
-
private getSourceKey;
|
|
684
|
-
/**
|
|
685
|
-
* 移除配置源
|
|
686
|
-
*/
|
|
687
|
-
removeSource(name: ConfigSourceType): boolean;
|
|
688
|
-
/**
|
|
689
|
-
* 关闭所有配置源的 watcher(用于优雅退出)
|
|
690
|
-
*/
|
|
691
|
-
unwatchAll(): void;
|
|
692
|
-
/**
|
|
693
|
-
* 获取配置源列表
|
|
694
|
-
*/
|
|
695
|
-
getSources(): ConfigSource[];
|
|
696
|
-
private getNestedValue;
|
|
697
|
-
private notifyWatchers;
|
|
698
|
-
private persistToFile;
|
|
699
|
-
/**
|
|
700
|
-
* 注册配置 Source
|
|
701
|
-
*
|
|
702
|
-
* Source 会在首次 load() 时被创建
|
|
703
|
-
*
|
|
704
|
-
* @example
|
|
705
|
-
* config.registerSource({ type: "file", relativePath: "llm-config.jsonc" });
|
|
706
|
-
* config.registerSource({ type: "env", envPrefix: "LLM" });
|
|
707
|
-
*/
|
|
708
|
-
registerSource(registration: SourceRegistration): void;
|
|
709
|
-
/**
|
|
710
|
-
* 创建 Source 实例
|
|
711
|
-
*/
|
|
712
|
-
private createSource;
|
|
713
|
-
/**
|
|
714
|
-
* 确保所有待创建的 Source 已被创建
|
|
715
|
-
*/
|
|
716
|
-
private ensureSourcesCreated;
|
|
717
|
-
/**
|
|
718
|
-
* 注册配置 Key
|
|
719
|
-
*
|
|
720
|
-
* @example
|
|
721
|
-
* config.registerKeys([
|
|
722
|
-
* { key: "llm.provider", sources: ["file", "memory"], default: "openai" },
|
|
723
|
-
* { key: "llm.model", sources: ["file"] },
|
|
724
|
-
* ]);
|
|
725
|
-
*/
|
|
726
|
-
registerKeys(keys: KeyRegistration[]): void;
|
|
727
|
-
/**
|
|
728
|
-
* 注册 Component(一次性注册 Source 和 Keys)
|
|
729
|
-
*
|
|
730
|
-
* @example
|
|
731
|
-
* config.registerComponent({
|
|
732
|
-
* name: "llm",
|
|
733
|
-
* sources: [
|
|
734
|
-
* { type: "file", relativePath: "llm-config.jsonc" },
|
|
735
|
-
* { type: "env", envPrefix: "LLM" },
|
|
736
|
-
* ],
|
|
737
|
-
* keys: [
|
|
738
|
-
* { key: "llm.provider", sources: ["file", "env", "memory"] },
|
|
739
|
-
* ],
|
|
740
|
-
* });
|
|
741
|
-
*/
|
|
742
|
-
registerComponent(registration: ComponentRegistration): void;
|
|
743
|
-
/**
|
|
744
|
-
* 按需加载配置
|
|
745
|
-
*
|
|
746
|
-
* 从注册的 Source 加载已注册 Keys 的配置值到内存中
|
|
747
|
-
*
|
|
748
|
-
* @param componentName - 可选,指定要加载的 Component 名称
|
|
749
|
-
*
|
|
750
|
-
* @example
|
|
751
|
-
* // 加载所有已注册的 Keys
|
|
752
|
-
* await config.load();
|
|
753
|
-
*
|
|
754
|
-
* // 只加载指定 Component 的 Keys
|
|
755
|
-
* await config.load("llm");
|
|
756
|
-
*/
|
|
757
|
-
load(componentName?: string): Promise<void>;
|
|
758
|
-
/**
|
|
759
|
-
* 获取已注册的 Source 列表
|
|
760
|
-
*/
|
|
761
|
-
getRegisteredSources(): ConfigSource[];
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* 服务配置接口
|
|
765
|
-
*/
|
|
766
|
-
interface ServiceConfig {
|
|
767
|
-
/** 环境配置 */
|
|
768
|
-
environment?: {
|
|
769
|
-
name?: string;
|
|
770
|
-
version?: string;
|
|
771
|
-
};
|
|
772
|
-
/** 组件配置映射 */
|
|
773
|
-
components?: Record<string, ComponentConfigEntry>;
|
|
774
|
-
}
|
|
775
|
-
/**
|
|
776
|
-
* 组件配置项
|
|
777
|
-
*/
|
|
778
|
-
interface ComponentConfigEntry {
|
|
779
|
-
/** 配置文件路径(可选) */
|
|
780
|
-
configPath?: string;
|
|
781
|
-
/** 环境变量前缀(可选) */
|
|
782
|
-
envPrefix?: string;
|
|
783
|
-
/** 直接配置对象(可选) */
|
|
784
|
-
config?: Record<string, unknown>;
|
|
785
|
-
/** 是否启用(可选,默认 true) */
|
|
786
|
-
enabled?: boolean;
|
|
787
|
-
}
|
|
788
|
-
/**
|
|
789
|
-
* 组件初始化选项(由 generateComponentOptions 生成)
|
|
790
|
-
*/
|
|
791
|
-
interface GeneratedComponentOptions {
|
|
792
|
-
/** ConfigComponent 实例 */
|
|
793
|
-
configComponent: ConfigComponent;
|
|
794
|
-
/** 配置文件路径 */
|
|
795
|
-
configPath?: string;
|
|
796
|
-
/** 环境变量前缀 */
|
|
797
|
-
envPrefix?: string;
|
|
798
|
-
/** 直接配置对象 */
|
|
799
|
-
config?: Record<string, unknown>;
|
|
800
|
-
/** 允许的其他属性 */
|
|
801
|
-
[key: string]: unknown;
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* Environment 接口
|
|
805
|
-
*
|
|
806
|
-
* 定义 Agent 运行时的核心能力接口。
|
|
807
|
-
*/
|
|
808
|
-
interface Environment {
|
|
809
|
-
/** 环境名称 */
|
|
810
|
-
readonly name: string;
|
|
811
|
-
/** 环境版本 */
|
|
812
|
-
readonly version: string;
|
|
813
|
-
/** 获取环境配置 */
|
|
814
|
-
getConfig(): EnvironmentConfig;
|
|
815
|
-
/**
|
|
816
|
-
* 注册组件
|
|
817
|
-
*/
|
|
818
|
-
registerComponent(component: Component): void;
|
|
819
|
-
/**
|
|
820
|
-
* 注销组件
|
|
821
|
-
*/
|
|
822
|
-
unregisterComponent(name: string): void;
|
|
823
|
-
/**
|
|
824
|
-
* 获取组件
|
|
825
|
-
*/
|
|
826
|
-
getComponent<T extends Component>(name: string): T | undefined;
|
|
827
|
-
/**
|
|
828
|
-
* 列出所有组件
|
|
829
|
-
*/
|
|
830
|
-
listComponents(): Component[];
|
|
831
|
-
/**
|
|
832
|
-
* 处理自然语言查询(核心入口)
|
|
833
|
-
*
|
|
834
|
-
* @param query 用户查询文本
|
|
835
|
-
* @param context 执行上下文(可选)
|
|
836
|
-
* @returns 完整的文本响应
|
|
837
|
-
*/
|
|
838
|
-
handle_query(query: string, context?: AgentContext): Promise<string>;
|
|
839
|
-
/**
|
|
840
|
-
* 处理动作
|
|
841
|
-
*/
|
|
842
|
-
handle_action(action: Action, context: Context): Promise<ToolResult>;
|
|
843
|
-
/**
|
|
844
|
-
* 订阅 EnvEvent(返回取消订阅函数)
|
|
845
|
-
*/
|
|
846
|
-
subscribe(handler: EnvEventHandler): () => void;
|
|
847
|
-
/**
|
|
848
|
-
* 订阅指定类型事件
|
|
849
|
-
*/
|
|
850
|
-
subscribeTo(eventType: string | string[], handler: EnvEventHandler): () => void;
|
|
851
|
-
/**
|
|
852
|
-
* 订阅所有事件(通配符)
|
|
853
|
-
*/
|
|
854
|
-
subscribeAll(handler: EnvEventHandler): () => void;
|
|
855
|
-
/**
|
|
856
|
-
* 推送事件
|
|
857
|
-
*
|
|
858
|
-
* @param event EnvEvent 或 EnvEventInput(部分属性)
|
|
859
|
-
*/
|
|
860
|
-
pushEnvEvent(event: EnvEvent | EnvEventInput): void;
|
|
861
|
-
/**
|
|
862
|
-
* 从配置文件加载服务配置
|
|
863
|
-
*
|
|
864
|
-
* @param configPath 配置文件路径(基于 XDG_DATA_HOME)
|
|
865
|
-
* @returns 解析后的服务配置对象
|
|
866
|
-
*/
|
|
867
|
-
loadServiceConfig(configPath: string): Promise<ServiceConfig>;
|
|
868
|
-
/**
|
|
869
|
-
* 生成组件初始化选项
|
|
870
|
-
*
|
|
871
|
-
* 根据组件配置项生成可用于初始化的选项对象。
|
|
872
|
-
*
|
|
873
|
-
* @param componentName 组件名称
|
|
874
|
-
* @param configEntry 组件配置项
|
|
875
|
-
* @returns 生成的组件初始化选项
|
|
876
|
-
*/
|
|
877
|
-
generateComponentOptions(componentName: string, configEntry: ComponentConfigEntry): GeneratedComponentOptions;
|
|
878
|
-
/**
|
|
879
|
-
* 注册组件并用配置初始化
|
|
880
|
-
*
|
|
881
|
-
* 便捷方法:注册组件并通过 ConfigComponent 初始化。
|
|
882
|
-
*
|
|
883
|
-
* @param component 要注册的组件实例
|
|
884
|
-
* @param configEntry 组件配置项
|
|
885
|
-
*/
|
|
886
|
-
registerComponentWithConfig(component: Component, configEntry: ComponentConfigEntry): Promise<void>;
|
|
887
|
-
/**
|
|
888
|
-
* 从配置文件初始化整个环境
|
|
889
|
-
*
|
|
890
|
-
* 加载服务配置,注册并初始化所有配置的组件。
|
|
891
|
-
*
|
|
892
|
-
* @param configPath 配置文件路径(基于 XDG_DATA_HOME)
|
|
893
|
-
*/
|
|
894
|
-
initFromConfig(configPath: string): Promise<void>;
|
|
895
|
-
}
|
|
896
|
-
/**
|
|
897
|
-
* Component 状态
|
|
898
|
-
*/
|
|
899
|
-
type ComponentStatus = "created" | "initializing" | "running" | "stopping" | "stopped" | "error";
|
|
900
|
-
/**
|
|
901
|
-
* Component 配置
|
|
902
|
-
*/
|
|
903
|
-
interface ComponentConfig {
|
|
904
|
-
name: string;
|
|
905
|
-
version: string;
|
|
906
|
-
enabled: boolean;
|
|
907
|
-
options?: Record<string, unknown>;
|
|
908
|
-
/**
|
|
909
|
-
* Environment 实例(可选)
|
|
910
|
-
*
|
|
911
|
-
* 推荐在 init 时接收 env 实例,以便:
|
|
912
|
-
* - 通过 env.getComponent() 获取其他组件
|
|
913
|
-
* - 通过 env.pushEnvEvent() 发布事件
|
|
914
|
-
* - 通过 env.handle_query() 处理查询
|
|
915
|
-
*
|
|
916
|
-
* 如果未提供,部分 Component 仍可正常工作(如测试场景)
|
|
917
|
-
*
|
|
918
|
-
* @example
|
|
919
|
-
* ```typescript
|
|
920
|
-
* async onInit(): Promise<void> {
|
|
921
|
-
* if (this.env) {
|
|
922
|
-
* const configComp = this.env.getComponent("config");
|
|
923
|
-
* }
|
|
924
|
-
* }
|
|
925
|
-
* ```
|
|
926
|
-
*/
|
|
927
|
-
env?: Environment;
|
|
928
|
-
}
|
|
929
|
-
/**
|
|
930
|
-
* Component 路径声明
|
|
931
|
-
*/
|
|
932
|
-
interface ComponentPaths {
|
|
933
|
-
base: "config" | "state" | "data" | "cache";
|
|
934
|
-
subPath: string;
|
|
935
|
-
isDirectory: boolean;
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
* Component Schema(注册配置)
|
|
939
|
-
*/
|
|
940
|
-
interface ComponentSchema {
|
|
941
|
-
/** Component 名称 */
|
|
942
|
-
name: string;
|
|
943
|
-
/** 配置 Schema(Zod) */
|
|
944
|
-
schema: ZodType3<unknown, ZodTypeDef2, unknown>;
|
|
945
|
-
/** 默认值(从 Schema 提取) */
|
|
946
|
-
defaults: Record<string, unknown>;
|
|
947
|
-
/** 路径声明 */
|
|
948
|
-
paths?: Record<string, ComponentPaths>;
|
|
949
|
-
/** 元信息 */
|
|
950
|
-
metadata?: {
|
|
951
|
-
description?: string;
|
|
952
|
-
version?: string;
|
|
953
|
-
author?: string;
|
|
954
|
-
};
|
|
955
|
-
}
|
|
956
|
-
/**
|
|
957
|
-
* Component 配置变更事件
|
|
958
|
-
*/
|
|
959
|
-
interface ComponentConfigChangeEvent {
|
|
960
|
-
/** Component 名称 */
|
|
961
|
-
component: string;
|
|
962
|
-
/** 变更的配置键 */
|
|
963
|
-
key: string;
|
|
964
|
-
/** 旧值 */
|
|
965
|
-
oldValue: unknown;
|
|
966
|
-
/** 新值 */
|
|
967
|
-
newValue: unknown;
|
|
968
|
-
/** 是否持久化 */
|
|
969
|
-
persisted: boolean;
|
|
970
|
-
/** 时间戳 */
|
|
971
|
-
timestamp: number;
|
|
972
|
-
}
|
|
973
|
-
/**
|
|
974
|
-
* Component 配置监听回调
|
|
975
|
-
*/
|
|
976
|
-
type ComponentWatchCallback = (event: ComponentConfigChangeEvent) => void | Promise<void>;
|
|
977
|
-
/**
|
|
978
|
-
* Component 基类接口
|
|
979
|
-
*/
|
|
980
|
-
interface Component {
|
|
981
|
-
/** Component 名称 */
|
|
982
|
-
readonly name: string;
|
|
983
|
-
/** Component 版本 */
|
|
984
|
-
readonly version: string;
|
|
985
|
-
/** 获取状态 */
|
|
986
|
-
getStatus(): ComponentStatus;
|
|
987
|
-
/** 获取配置 */
|
|
988
|
-
getConfig(): ComponentConfig;
|
|
989
|
-
/** 初始化 */
|
|
990
|
-
init(config: ComponentConfig): Promise<void>;
|
|
991
|
-
/** 启动 */
|
|
992
|
-
start(): Promise<void>;
|
|
993
|
-
/** 停止 */
|
|
994
|
-
stop(): Promise<void>;
|
|
995
|
-
}
|
|
996
|
-
/**
|
|
997
|
-
* Component 基类实现
|
|
998
|
-
*/
|
|
999
|
-
declare abstract class BaseComponent implements Component {
|
|
1000
|
-
abstract readonly name: string;
|
|
1001
|
-
abstract readonly version: string;
|
|
1002
|
-
protected _status: ComponentStatus;
|
|
1003
|
-
protected _enabled: boolean;
|
|
1004
|
-
protected env: Environment;
|
|
1005
|
-
/** Hook 管理器 - 子类初始化时由构造函数设置 */
|
|
1006
|
-
readonly hookManager: HookManager;
|
|
1007
|
-
constructor();
|
|
1008
|
-
getStatus(): ComponentStatus;
|
|
1009
|
-
getConfig(): ComponentConfig;
|
|
1010
|
-
/**
|
|
1011
|
-
* 检查 Environment 是否已初始化
|
|
1012
|
-
*/
|
|
1013
|
-
protected isEnvInitialized(): boolean;
|
|
1014
|
-
protected setStatus(status: ComponentStatus): void;
|
|
1015
|
-
/**
|
|
1016
|
-
* 获取 Environment 实例
|
|
1017
|
-
*/
|
|
1018
|
-
protected getEnv(): Environment;
|
|
1019
|
-
init(config?: ComponentConfig): Promise<void>;
|
|
1020
|
-
start(): Promise<void>;
|
|
1021
|
-
stop(): Promise<void>;
|
|
1022
|
-
/**
|
|
1023
|
-
* 子类可覆盖的钩子方法
|
|
1024
|
-
*/
|
|
1025
|
-
protected onInit(): Promise<void>;
|
|
1026
|
-
protected onStart(): Promise<void>;
|
|
1027
|
-
protected onStop(): Promise<void>;
|
|
1028
|
-
/**
|
|
1029
|
-
* 注册 Hook
|
|
1030
|
-
*
|
|
1031
|
-
* @param hookPoint Hook 点名称
|
|
1032
|
-
* @param hook Hook 实例
|
|
1033
|
-
*/
|
|
1034
|
-
protected registerHook<T = unknown>(hookPoint: string, hook: Hook<T>): void;
|
|
1035
|
-
/**
|
|
1036
|
-
* 便捷方法:快速注册 Hook
|
|
1037
|
-
*/
|
|
1038
|
-
protected addHook<T = unknown>(hookPoint: string, name: string, fn: HookFn<T>, priority?: number): void;
|
|
1039
|
-
/**
|
|
1040
|
-
* 便捷方法:按名称取消注册 Hook
|
|
1041
|
-
*/
|
|
1042
|
-
protected removeHook(hookPoint: string, name: string): boolean;
|
|
1043
|
-
/**
|
|
1044
|
-
* 执行 Hook
|
|
1045
|
-
*/
|
|
1046
|
-
protected executeHooks<T = unknown>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
1047
|
-
/**
|
|
1048
|
-
* 获取 ConfigComponent 实例
|
|
1049
|
-
*
|
|
1050
|
-
* 子类可使用此方法获取配置组件,支持热更新
|
|
1051
|
-
*/
|
|
1052
|
-
protected getConfigComponent(): {
|
|
1053
|
-
get(key: string): unknown;
|
|
1054
|
-
} | undefined;
|
|
1055
|
-
/**
|
|
1056
|
-
* 动态获取运行时配置值
|
|
1057
|
-
*
|
|
1058
|
-
* 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
|
|
1059
|
-
* 适用于运行时可能变化的配置(如 API Keys、模型参数)
|
|
1060
|
-
*
|
|
1061
|
-
* @param key 配置键(支持点号分隔的路径,如 "llm.temperature")
|
|
1062
|
-
* @param defaultValue 默认值
|
|
1063
|
-
* @returns 配置值或默认值
|
|
1064
|
-
*
|
|
1065
|
-
* @example
|
|
1066
|
-
* ```typescript
|
|
1067
|
-
* // 获取 LLM 温度配置(可能支持热更新)
|
|
1068
|
-
* const temperature = this.getRuntimeConfig("llm.temperature", 0.7);
|
|
1069
|
-
*
|
|
1070
|
-
* // 获取 API Key
|
|
1071
|
-
* const apiKey = this.getRuntimeConfig<string>("llm.providers.openai.apiKey");
|
|
1072
|
-
* ```
|
|
1073
|
-
*/
|
|
1074
|
-
protected getRuntimeConfig<T>(key: string, defaultValue: T): T;
|
|
1075
|
-
}
|
|
1076
|
-
import { z as z2 } from "zod";
|
|
1077
|
-
/**
|
|
1078
|
-
* 简化 Bun file: 协议的编码路径
|
|
1079
|
-
* 例如: packages+core/node_modules/.bun/@roy-agent+core@file+packages+core/node_modules/@roy-agent/core/src/env/agent/agent-component.ts
|
|
1080
|
-
* 转换为: packages/core/src/env/agent/agent-component.ts
|
|
1081
|
-
*/
|
|
1082
|
-
declare function simplifyFilePath(fullPath: string): string;
|
|
1083
|
-
declare function isQuietMode(): boolean;
|
|
1084
|
-
declare function setQuietMode(enabled: boolean): void;
|
|
1085
|
-
/**
|
|
1086
|
-
* 设置 ConfigComponent 实例(用于实时配置读取)
|
|
1087
|
-
*/
|
|
1088
|
-
declare function setConfigComponent(component: any): void;
|
|
1089
|
-
/**
|
|
1090
|
-
* 获取日志级别(支持热更新)
|
|
1091
|
-
*/
|
|
1092
|
-
declare function getLogLevel(): LogLevel;
|
|
1093
|
-
/**
|
|
1094
|
-
* 获取日志文件名
|
|
1095
|
-
* 实时从 ConfigComponent 读取(支持 env/file/memory 多 source)
|
|
1096
|
-
*/
|
|
1097
|
-
declare function getLogFile(): string;
|
|
1098
|
-
declare function getLogDir(): string;
|
|
1099
|
-
/**
|
|
1100
|
-
* 获取日志输出最大长度配置
|
|
1101
|
-
* 实时从 ConfigComponent 读取
|
|
1102
|
-
*
|
|
1103
|
-
* @returns 配置的值(0 表示不限制),如果未配置则返回 undefined
|
|
1104
|
-
*/
|
|
1105
|
-
declare function getMaxOutput(): number | undefined;
|
|
1106
|
-
declare function setLogDirOverride(dir: string): void;
|
|
1107
|
-
declare function getLogDirOverride(): string | undefined;
|
|
1108
|
-
declare class Logger implements ILogger {
|
|
1109
|
-
private prefix;
|
|
1110
|
-
constructor(prefix: string);
|
|
1111
|
-
private shouldLog;
|
|
1112
|
-
private ensureLogDirectory;
|
|
1113
|
-
private getCallerLocation;
|
|
1114
|
-
private getRelativePath;
|
|
1115
|
-
private formatMessage;
|
|
1116
|
-
private writeToFile;
|
|
1117
|
-
private log;
|
|
1118
|
-
debug(message: string, data?: unknown): void;
|
|
1119
|
-
info(message: string, data?: unknown): void;
|
|
1120
|
-
warn(message: string, data?: unknown): void;
|
|
1121
|
-
error(message: string, data?: unknown): void;
|
|
1122
|
-
}
|
|
1123
|
-
/**
|
|
1124
|
-
* 创建或获取 Logger 实例
|
|
1125
|
-
* @param prefix Logger 前缀
|
|
1126
|
-
*
|
|
1127
|
-
* 日志文件名通过 ConfigComponent 实时读取(log-trace.logging.file)
|
|
1128
|
-
*/
|
|
1129
|
-
declare function createLogger(prefix: string): Logger;
|
|
1130
|
-
/**
|
|
1131
|
-
* 获取所有日志级别
|
|
1132
|
-
*/
|
|
1133
|
-
declare function getLogLevels(): LogLevel[];
|
|
1134
|
-
/**
|
|
1135
|
-
* Span 选项
|
|
1136
|
-
*/
|
|
1137
|
-
interface SpanOptions {
|
|
1138
|
-
/** 父 Span 上下文(可选) */
|
|
1139
|
-
parent?: SpanContext;
|
|
1140
|
-
/** Span 属性 */
|
|
1141
|
-
attributes?: SpanAttributes;
|
|
1142
|
-
/** Span 类型 */
|
|
1143
|
-
kind?: string;
|
|
1144
|
-
/** 指定 traceId(可选,如果不提供则自动生成) */
|
|
1145
|
-
traceId?: string;
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* OTel Span 接口
|
|
1149
|
-
*/
|
|
1150
|
-
interface OTelSpan {
|
|
1151
|
-
readonly name: string;
|
|
1152
|
-
readonly kind: string;
|
|
1153
|
-
readonly spanContext: SpanContext;
|
|
1154
|
-
readonly attributes: SpanAttributes;
|
|
1155
|
-
startTime?: number;
|
|
1156
|
-
endTime?: number;
|
|
1157
|
-
error?: string;
|
|
1158
|
-
setAttribute(key: string, value: unknown): void;
|
|
1159
|
-
addEvent(name: string, attributes?: Record<string, unknown>): void;
|
|
1160
|
-
end(result?: unknown, error?: Error): void;
|
|
1161
|
-
}
|
|
1162
|
-
/**
|
|
1163
|
-
* OTel Tracer 接口
|
|
1164
|
-
*/
|
|
1165
|
-
interface OTelTracer {
|
|
1166
|
-
readonly name: string;
|
|
1167
|
-
readonly version?: string;
|
|
1168
|
-
startSpan(name: string, options?: SpanOptions): OTelSpan;
|
|
1169
|
-
/**
|
|
1170
|
-
* 结束指定 span 并恢复父上下文
|
|
1171
|
-
* @param span 要结束的 span
|
|
1172
|
-
* @returns 结束后的父上下文(用于恢复之前的上下文)
|
|
1173
|
-
*/
|
|
1174
|
-
endSpan(span: OTelSpan): SpanContext | undefined;
|
|
1175
|
-
injectToEnv(env: Record<string, string | undefined>): void;
|
|
1176
|
-
getCurrentContext(): SpanContext | undefined;
|
|
1177
|
-
setCurrentContext(context: SpanContext | undefined): void;
|
|
1178
|
-
/**
|
|
1179
|
-
* 生成符合 OTel 格式的 traceId(32 字符 16 进制)
|
|
1180
|
-
*/
|
|
1181
|
-
generateTraceId(): string;
|
|
1182
|
-
/**
|
|
1183
|
-
* 生成符合 OTel 格式的 spanId(16 字符 16 进制)
|
|
1184
|
-
*/
|
|
1185
|
-
generateSpanId(): string;
|
|
1186
|
-
}
|
|
1187
|
-
/**
|
|
1188
|
-
* OpenTelemetry TracerProvider
|
|
1189
|
-
*
|
|
1190
|
-
* 全局 Tracer 管理器,负责:
|
|
1191
|
-
* - 维护 Tracer 缓存
|
|
1192
|
-
* - 管理存储
|
|
1193
|
-
* - 处理上下文初始化
|
|
1194
|
-
* - 提供全局 context 供所有 tracer 共享
|
|
1195
|
-
*/
|
|
1196
|
-
declare class OTelTracerProvider {
|
|
1197
|
-
private tracers;
|
|
1198
|
-
private storage;
|
|
1199
|
-
private initialized;
|
|
1200
|
-
private globalContext;
|
|
1201
|
-
constructor(storage?: SpanStorage, dbPath?: string);
|
|
1202
|
-
/**
|
|
1203
|
-
* 初始化 Provider
|
|
1204
|
-
*
|
|
1205
|
-
* 从环境变量恢复上下文(如 TRACEPARENT)
|
|
1206
|
-
*/
|
|
1207
|
-
initialize(): Promise<void>;
|
|
1208
|
-
/**
|
|
1209
|
-
* 检查是否已初始化
|
|
1210
|
-
*/
|
|
1211
|
-
isInitialized(): boolean;
|
|
1212
|
-
/**
|
|
1213
|
-
* 获取全局 context(所有 tracer 共享)
|
|
1214
|
-
*/
|
|
1215
|
-
getGlobalContext(): SpanContext | undefined;
|
|
1216
|
-
/**
|
|
1217
|
-
* 设置全局 context(所有 tracer 共享)
|
|
1218
|
-
*/
|
|
1219
|
-
setGlobalContext(context: SpanContext | undefined): void;
|
|
1220
|
-
/**
|
|
1221
|
-
* 获取 Tracer
|
|
1222
|
-
*/
|
|
1223
|
-
getTracer(name: string, version?: string): OTelTracer;
|
|
1224
|
-
/**
|
|
1225
|
-
* 从环境变量恢复上下文
|
|
1226
|
-
*/
|
|
1227
|
-
private restoreFromEnv;
|
|
1228
|
-
/**
|
|
1229
|
-
* 关闭 Provider
|
|
1230
|
-
*/
|
|
1231
|
-
shutdown(): void;
|
|
1232
|
-
/**
|
|
1233
|
-
* 获取存储
|
|
1234
|
-
*/
|
|
1235
|
-
getStorage(): SpanStorage;
|
|
1236
|
-
}
|
|
1237
|
-
/**
|
|
1238
|
-
* 获取 TracerProvider 单例
|
|
1239
|
-
*/
|
|
1240
|
-
declare function getTracerProvider(): OTelTracerProvider;
|
|
1241
|
-
/**
|
|
1242
|
-
* 重置 TracerProvider(用于测试)
|
|
1243
|
-
*/
|
|
1244
|
-
declare function resetTracerProvider(): void;
|
|
1245
|
-
/**
|
|
1246
|
-
* 获取默认日志目录(基于 XDG 标准)
|
|
1247
|
-
* 存储在 XDG_DATA_HOME/roy-agent/logs/
|
|
1248
|
-
*/
|
|
1249
|
-
declare function getDefaultLogDir(): string;
|
|
1250
|
-
/**
|
|
1251
|
-
* 获取默认的 span 数据库路径(基于 XDG 标准)
|
|
1252
|
-
* 存储在 XDG_DATA_HOME/roy-agent/traces.db
|
|
1253
|
-
*/
|
|
1254
|
-
declare function getDefaultSpanDbPath(): string;
|
|
1255
|
-
/**
|
|
1256
|
-
* Log & Trace Component 配置 Schema
|
|
1257
|
-
*/
|
|
1258
|
-
declare const LogTraceConfigSchema: unknown;
|
|
1259
|
-
type LogTraceConfig = z2.infer<typeof LogTraceConfigSchema>;
|
|
1260
|
-
/**
|
|
1261
|
-
* LogTraceComponent 配置选项(通过 options 传递)
|
|
1262
|
-
*
|
|
1263
|
-
* 配置加载顺序(优先级从低到高):
|
|
1264
|
-
* 1. File - 配置文件(通过 configPath 指定)
|
|
1265
|
-
* 2. Env - 环境变量(通过 envPrefix 配置前缀)
|
|
1266
|
-
* 3. Object - 直接传入的 config 对象(最高优先级)
|
|
1267
|
-
*/
|
|
1268
|
-
interface LogTraceComponentOptions {
|
|
1269
|
-
/** ConfigComponent 实例(必填) */
|
|
1270
|
-
configComponent: ConfigComponent;
|
|
1271
|
-
/** 配置文件相对路径(可选,基于 XDG_DATA_HOME) */
|
|
1272
|
-
configPath?: string;
|
|
1273
|
-
/** 环境变量前缀(可选,默认 "LOG") */
|
|
1274
|
-
envPrefix?: string;
|
|
1275
|
-
/** 回调函数 */
|
|
1276
|
-
callbacks?: {
|
|
1277
|
-
onLogLevelChange?: (level: LogLevel) => void;
|
|
1278
|
-
};
|
|
1279
|
-
/** 直接传入的配置对象(可选,优先级最高) */
|
|
1280
|
-
config?: Partial<LogTraceConfig>;
|
|
1281
|
-
}
|
|
1282
|
-
/**
|
|
1283
|
-
* Log & Trace Component
|
|
1284
|
-
*
|
|
1285
|
-
* 提供统一的日志和追踪能力
|
|
1286
|
-
* 配置通过 ConfigComponent 实时获取,支持配置热更新
|
|
1287
|
-
*
|
|
1288
|
-
* @example
|
|
1289
|
-
* // 通过 ConfigComponent 初始化
|
|
1290
|
-
* const configComponent = new ConfigComponent();
|
|
1291
|
-
* const component = new LogTraceComponent();
|
|
1292
|
-
* await component.init({
|
|
1293
|
-
* env, // Environment 实例(必填)
|
|
1294
|
-
* options: { configComponent }
|
|
1295
|
-
* });
|
|
1296
|
-
*
|
|
1297
|
-
* // 配置更新后,日志级别会自动热更新
|
|
1298
|
-
* configComponent.set("log-trace.logging.level", "debug");
|
|
1299
|
-
*
|
|
1300
|
-
* // 初始化追踪上下文
|
|
1301
|
-
* component.initContext("request-123", "session-456");
|
|
1302
|
-
*
|
|
1303
|
-
* // 使用日志
|
|
1304
|
-
* component.getLogger().info("Hello, world!");
|
|
1305
|
-
*
|
|
1306
|
-
* // 使用追踪
|
|
1307
|
-
* const span = component.getTracer().startSpan("my-span");
|
|
1308
|
-
* component.getTracer().endSpan(span);
|
|
1309
|
-
*/
|
|
1310
|
-
declare class LogTraceComponent extends BaseComponent {
|
|
1311
|
-
readonly name = "log-trace";
|
|
1312
|
-
readonly version = "1.0.0";
|
|
1313
|
-
private config?;
|
|
1314
|
-
private configComponent?;
|
|
1315
|
-
private tracer?;
|
|
1316
|
-
private traceContext?;
|
|
1317
|
-
/** 配置变更 watcher 清理函数 */
|
|
1318
|
-
private configWatcher?;
|
|
1319
|
-
constructor();
|
|
1320
|
-
/**
|
|
1321
|
-
* 初始化组件
|
|
1322
|
-
*
|
|
1323
|
-
* 配置加载优先级(从高到低):
|
|
1324
|
-
* 1. Object - 直接传入的 config 对象
|
|
1325
|
-
* 2. Env - 环境变量(通过 envPrefix 配置前缀)
|
|
1326
|
-
* 3. File - 配置文件(通过 configPath 指定)
|
|
1327
|
-
*/
|
|
1328
|
-
init(config: ComponentConfig): Promise<void>;
|
|
1329
|
-
/**
|
|
1330
|
-
* 注册配置到 ConfigComponent
|
|
1331
|
-
*
|
|
1332
|
-
* 遵循 SessionComponent 模式:
|
|
1333
|
-
* 1. 使用 registerComponent 注册配置结构
|
|
1334
|
-
* 2. 使用 load 加载配置
|
|
1335
|
-
* 3. 设置默认值
|
|
1336
|
-
* 4. 处理环境变量后备方案
|
|
1337
|
-
* 5. 处理 config 对象
|
|
1338
|
-
*
|
|
1339
|
-
* 配置加载顺序(优先级从低到高):
|
|
1340
|
-
* 1. FileSource - 从配置文件加载(最低)
|
|
1341
|
-
* 2. EnvSource - 从环境变量加载(中等)
|
|
1342
|
-
* 3. MemorySource - 直接配置对象(最高)
|
|
1343
|
-
*/
|
|
1344
|
-
private registerConfig;
|
|
1345
|
-
/**
|
|
1346
|
-
* 将配置对象展平为点号路径
|
|
1347
|
-
*/
|
|
1348
|
-
private flattenConfig;
|
|
1349
|
-
/**
|
|
1350
|
-
* 注册配置热更新监听
|
|
1351
|
-
*/
|
|
1352
|
-
private registerConfigWatcher;
|
|
1353
|
-
/**
|
|
1354
|
-
* 处理配置变更
|
|
1355
|
-
*/
|
|
1356
|
-
protected onConfigChange(event: {
|
|
1357
|
-
key: string;
|
|
1358
|
-
oldValue?: unknown;
|
|
1359
|
-
newValue?: unknown;
|
|
1360
|
-
}): void;
|
|
1361
|
-
/**
|
|
1362
|
-
* 创建存储实例
|
|
1363
|
-
*
|
|
1364
|
-
* @throws Error 如果未配置 dbPath
|
|
1365
|
-
*/
|
|
1366
|
-
private createStorage;
|
|
1367
|
-
/**
|
|
1368
|
-
* 配置日志系统
|
|
1369
|
-
*/
|
|
1370
|
-
private configureLogging;
|
|
1371
|
-
/**
|
|
1372
|
-
* 初始化追踪上下文
|
|
1373
|
-
*
|
|
1374
|
-
* @param requestId 请求 ID
|
|
1375
|
-
* @param sessionId 会话 ID(可选)
|
|
1376
|
-
*/
|
|
1377
|
-
initContext(requestId: string, sessionId?: string): void;
|
|
1378
|
-
/**
|
|
1379
|
-
* 创建新的追踪上下文(返回清理函数)
|
|
1380
|
-
*
|
|
1381
|
-
* @param requestId 请求 ID
|
|
1382
|
-
* @param sessionId 会话 ID(可选)
|
|
1383
|
-
*/
|
|
1384
|
-
startContext(requestId: string, sessionId?: string): () => void;
|
|
1385
|
-
/**
|
|
1386
|
-
* 获取 Logger
|
|
1387
|
-
*
|
|
1388
|
-
* @param prefix 日志前缀
|
|
1389
|
-
*/
|
|
1390
|
-
getLogger(prefix?: string): ReturnType<typeof createLogger>;
|
|
1391
|
-
/**
|
|
1392
|
-
* 获取 Tracer
|
|
1393
|
-
*/
|
|
1394
|
-
getTracer(): OTelTracer;
|
|
1395
|
-
/**
|
|
1396
|
-
* 获取 Trace Context
|
|
1397
|
-
*/
|
|
1398
|
-
getTraceContext(): ITraceContext | undefined;
|
|
1399
|
-
/**
|
|
1400
|
-
* 获取当前 requestId
|
|
1401
|
-
*/
|
|
1402
|
-
getRequestId(): string | undefined;
|
|
1403
|
-
/**
|
|
1404
|
-
* 停止组件
|
|
1405
|
-
*/
|
|
1406
|
-
onStop(): Promise<void>;
|
|
1407
|
-
}
|
|
1408
|
-
/**
|
|
1409
|
-
* 将 SpanContext 序列化为 traceparent 字符串
|
|
1410
|
-
*
|
|
1411
|
-
* W3C TraceContext 语义:
|
|
1412
|
-
* - traceparent 中的 spanId 是"发送方 span"的 ID
|
|
1413
|
-
* - 接收方会将其作为新 span 的 parentSpanId
|
|
1414
|
-
* - 所以应该使用 context.spanId,而非 parentSpanId
|
|
1415
|
-
*
|
|
1416
|
-
* @param context SpanContext
|
|
1417
|
-
* @returns traceparent 格式字符串
|
|
1418
|
-
*/
|
|
1419
|
-
declare function serialize(context: SpanContext): string;
|
|
1420
|
-
/**
|
|
1421
|
-
* 提取的 Trace Context
|
|
1422
|
-
*
|
|
1423
|
-
* W3C TraceContext 格式: 00-{traceId}-{spanId}-{flags}
|
|
1424
|
-
* - {spanId} 是发送方 span 的 ID
|
|
1425
|
-
* - 接收方应将其作为新 span 的 parentSpanId
|
|
1426
|
-
*/
|
|
1427
|
-
interface ExtractedContext {
|
|
1428
|
-
traceId: string;
|
|
1429
|
-
/** 发送方 span 的 ID,接收方应作为新 span 的 parentSpanId */
|
|
1430
|
-
spanId: string;
|
|
1431
|
-
}
|
|
1432
|
-
/**
|
|
1433
|
-
* 将 traceparent 字符串反序列化为 ExtractedContext
|
|
1434
|
-
*
|
|
1435
|
-
* W3C TraceContext 语义:
|
|
1436
|
-
* - traceparent 中的 "span-id" 是发送方 span 的 ID
|
|
1437
|
-
* - 接收方应该:
|
|
1438
|
-
* - 使用提取的 traceId
|
|
1439
|
-
* - 将提取的 spanId 作为 parentSpanId
|
|
1440
|
-
* - 生成自己的新 spanId
|
|
1441
|
-
*
|
|
1442
|
-
* @param traceparent traceparent 格式字符串
|
|
1443
|
-
* @returns 接收方 ExtractedContext 或 undefined(格式无效)
|
|
1444
|
-
*/
|
|
1445
|
-
declare function parse(traceparent: string): ExtractedContext | undefined;
|
|
1446
|
-
/**
|
|
1447
|
-
* W3C TraceContext Propagation 实现
|
|
1448
|
-
*
|
|
1449
|
-
* 用于在进程间传递 trace 上下文:
|
|
1450
|
-
* - inject: 将 SpanContext 注入到 carrier(环境变量)
|
|
1451
|
-
* - extract: 从 carrier(环境变量)提取 SpanContext
|
|
1452
|
-
*/
|
|
1453
|
-
declare const propagation: {
|
|
1454
|
-
/**
|
|
1455
|
-
* 将 SpanContext 注入到 carrier
|
|
1456
|
-
*
|
|
1457
|
-
* @param carrier 载体对象(通常是环境变量对象)
|
|
1458
|
-
* @param context Span 上下文
|
|
1459
|
-
*/
|
|
1460
|
-
inject(carrier: Record<string, string | undefined>, context: SpanContext): void;
|
|
1461
|
-
/**
|
|
1462
|
-
* 从 carrier 提取 SpanContext
|
|
1463
|
-
*
|
|
1464
|
-
* @param carrier 载体对象(通常是环境变量对象)
|
|
1465
|
-
* @returns ExtractedContext 或 undefined
|
|
1466
|
-
*/
|
|
1467
|
-
extract(carrier: Record<string, string | undefined>): ExtractedContext | undefined;
|
|
1468
|
-
/**
|
|
1469
|
-
* 获取 traceparent 字段名
|
|
1470
|
-
*/
|
|
1471
|
-
getTraceparentHeader(): string;
|
|
1472
|
-
};
|
|
1473
|
-
/**
|
|
1474
|
-
* 环境变量对象类型
|
|
1475
|
-
*/
|
|
1476
|
-
type EnvCarrier = Record<string, string | undefined>;
|
|
1477
|
-
/**
|
|
1478
|
-
* 从当前 OTelTracerProvider 获取上下文并注入到环境变量
|
|
1479
|
-
*
|
|
1480
|
-
* 这是一个便捷函数,封装了常见的注入模式:
|
|
1481
|
-
* 1. 从全局 TracerProvider 获取当前 Tracer
|
|
1482
|
-
* 2. 获取当前 span context
|
|
1483
|
-
* 3. 注入到目标环境变量
|
|
1484
|
-
*
|
|
1485
|
-
* @param env 目标环境变量对象(默认:{})
|
|
1486
|
-
* @returns 注入了 TRACEPARENT 的环境变量对象
|
|
1487
|
-
*
|
|
1488
|
-
* @example
|
|
1489
|
-
* ```typescript
|
|
1490
|
-
* const env = injectToEnv({ PATH: '/usr/bin' });
|
|
1491
|
-
* // env.TRACEPARENT = '00-{traceId}-{spanId}-01'
|
|
1492
|
-
* // env.PATH = '/usr/bin'
|
|
1493
|
-
* ```
|
|
1494
|
-
*/
|
|
1495
|
-
declare function injectToEnv2(env?: EnvCarrier): EnvCarrier;
|
|
1496
|
-
/**
|
|
1497
|
-
* 将指定的 SpanContext 直接注入到环境变量
|
|
1498
|
-
*
|
|
1499
|
-
* 与 injectToEnv 不同,这个函数不需要访问全局 TracerProvider,
|
|
1500
|
-
* 直接使用传入的 SpanContext 进行注入。
|
|
1501
|
-
*
|
|
1502
|
-
* @param env 目标环境变量对象
|
|
1503
|
-
* @param context 要注入的 SpanContext
|
|
1504
|
-
* @returns 注入了 TRACEPARENT 的环境变量对象
|
|
1505
|
-
*
|
|
1506
|
-
* @example
|
|
1507
|
-
* ```typescript
|
|
1508
|
-
* const span = tracer.startSpan('my-span');
|
|
1509
|
-
* const env = injectToEnvWithContext({}, span.spanContext);
|
|
1510
|
-
* ```
|
|
1511
|
-
*/
|
|
1512
|
-
declare function injectToEnvWithContext(env: EnvCarrier, context: SpanContext): EnvCarrier;
|
|
1513
|
-
/**
|
|
1514
|
-
* 从源环境变量 carrier 复制 TRACEPARENT 到目标 carrier
|
|
1515
|
-
*
|
|
1516
|
-
* 用于将一个环境变量对象中的 trace context 传递到另一个:
|
|
1517
|
-
* - 从 process.env 复制到子进程的环境变量
|
|
1518
|
-
* - 从父进程环境变量复制到子进程环境变量
|
|
1519
|
-
*
|
|
1520
|
-
* @param target 目标 carrier(将被修改)
|
|
1521
|
-
* @param source 源 carrier
|
|
1522
|
-
* @returns 目标 carrier
|
|
1523
|
-
*
|
|
1524
|
-
* @example
|
|
1525
|
-
* ```typescript
|
|
1526
|
-
* // 在 fork 子进程时继承父进程的 trace context
|
|
1527
|
-
* const childEnv = injectToEnvFromCarrier({}, process.env);
|
|
1528
|
-
* ```
|
|
1529
|
-
*/
|
|
1530
|
-
declare function injectToEnvFromCarrier(target: EnvCarrier, source: EnvCarrier): EnvCarrier;
|
|
1531
|
-
/**
|
|
1532
|
-
* 获取 TRACEPARENT 字段名
|
|
1533
|
-
*/
|
|
1534
|
-
declare function getTraceparentHeader2(): string;
|
|
1535
|
-
interface TracedOptions {
|
|
1536
|
-
name?: string;
|
|
1537
|
-
recordParams?: boolean;
|
|
1538
|
-
recordResult?: boolean;
|
|
1539
|
-
recordError?: boolean;
|
|
1540
|
-
log?: boolean;
|
|
1541
|
-
maxLogSize?: number;
|
|
1542
|
-
paramFilter?: (args: any[], argNames?: string[]) => Record<string, any>;
|
|
1543
|
-
}
|
|
1544
|
-
/**
|
|
1545
|
-
* @Traced 装饰器工厂
|
|
1546
|
-
*/
|
|
1547
|
-
declare function Traced(options?: TracedOptions): unknown;
|
|
1548
|
-
/**
|
|
1549
|
-
* @TracedAs 装饰器工厂 - 使用自定义名称
|
|
1550
|
-
*/
|
|
1551
|
-
declare function TracedAs(name: string, options?: Omit<TracedOptions, "name">);
|
|
1552
|
-
/**
|
|
1553
|
-
* @TracedLightweight 装饰器工厂 - 轻量级追踪
|
|
1554
|
-
*/
|
|
1555
|
-
declare function TracedLightweight(options?: {
|
|
1556
|
-
log?: boolean;
|
|
1557
|
-
maxLogSize?: number;
|
|
1558
|
-
});
|
|
1559
|
-
/**
|
|
1560
|
-
* 包装函数以自动创建 Span
|
|
1561
|
-
*
|
|
1562
|
-
* 同时支持 async 函数和非 async 函数:
|
|
1563
|
-
* - async 函数:自动检测 Promise 并正确处理
|
|
1564
|
-
* - 非 async 函数:同步执行,直接返回结果
|
|
1565
|
-
*/
|
|
1566
|
-
declare function wrapFunction<T extends (...args: any[]) => any>(fn: T, name: string, options?: {
|
|
1567
|
-
recordParams?: boolean;
|
|
1568
|
-
recordResult?: boolean;
|
|
1569
|
-
recordError?: boolean;
|
|
1570
|
-
log?: boolean;
|
|
1571
|
-
maxLogSize?: number;
|
|
1572
|
-
paramFilter?: (args: any[], argNames?: string[]) => Record<string, any>;
|
|
1573
|
-
}): T;
|
|
1574
|
-
export { wrapFunction, simplifyFilePath, setQuietMode, setConfigComponent as setLoggerConfigComponent, setLogDirOverride, serialize, resetTracerProvider, propagation, parse, isQuietMode, injectToEnvWithContext, injectToEnvFromCarrier, injectToEnv2 as injectToEnv, getTracerProvider, getTraceparentHeader2 as getTraceparentHeader, getMaxOutput, getLogLevels, getLogLevel, getLogFile, getLogDirOverride, getLogDir, getDefaultSpanDbPath, getDefaultLogDir, createLogger, TracedOptions, TracedLightweight, TracedAs, Traced, TraceInfo, TraceContextOptions, SpanStorage, SpanStatus, SpanOptions, SpanKind, SpanContext, SpanAttributes, Span, SQLiteSpanStorage, OTelTracerProvider, OTelTracer, OTelSpan, LoggerConfig, LogTraceConfigSchema, LogTraceConfig, LogTraceComponentOptions, LogTraceComponent, LogLevel, ITraceContext, ILogger, ExtractedContext, EnvCarrier };
|