@ai-setting/roy-agent-core 1.3.10 → 1.3.11
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/config/index.js +1647 -0
- package/dist/index.js +12579 -89691
- package/package.json +19 -56
- package/src/config/config-component.test.ts +0 -627
- package/src/config/config-component.ts +0 -906
- package/src/config/config-parser.test.ts +0 -319
- package/src/config/config-parser.ts +0 -203
- package/src/config/decentralized-config.test.ts +0 -740
- package/src/config/env-key.ts +0 -210
- package/src/config/env-source.test.ts +0 -252
- package/src/config/env-source.ts +0 -301
- package/src/config/file-source.test.ts +0 -357
- package/src/config/file-source.ts +0 -421
- package/src/config/index.ts +0 -24
- package/src/config/protocol-resolver.test.ts +0 -217
- package/src/config/protocol-resolver.ts +0 -228
- package/src/env/agent/agent-component.abort.test.ts +0 -511
- package/src/env/agent/agent-component.record-session.test.ts +0 -349
- package/src/env/agent/agent-component.test.ts +0 -1389
- package/src/env/agent/agent-component.tool-error.test.ts +0 -327
- package/src/env/agent/agent-component.ts +0 -1711
- package/src/env/agent/agent-config-registration.test.ts +0 -226
- package/src/env/agent/agent-config-registration.ts +0 -46
- package/src/env/agent/agent-reminder-plugin.integration.test.ts +0 -243
- package/src/env/agent/index.ts +0 -10
- package/src/env/agent/summary-agent.parse-hint.test.ts +0 -360
- package/src/env/agent/summary-agent.ts +0 -508
- package/src/env/agent/types.ts +0 -536
- package/src/env/commands/commands-component.test.ts +0 -364
- package/src/env/commands/commands-component.ts +0 -604
- package/src/env/commands/commands-config-registration.test.ts +0 -198
- package/src/env/commands/commands-config-registration.ts +0 -38
- package/src/env/commands/index.ts +0 -21
- package/src/env/commands/parser.test.ts +0 -203
- package/src/env/commands/parser.ts +0 -115
- package/src/env/commands/types.ts +0 -184
- package/src/env/commands-prompt-integration.test.ts +0 -243
- package/src/env/component-env.test.ts +0 -119
- package/src/env/component.ts +0 -335
- package/src/env/constants.test.ts +0 -72
- package/src/env/constants.ts +0 -123
- package/src/env/debug/debug-component.test.ts +0 -114
- package/src/env/debug/debug-component.ts +0 -547
- package/src/env/debug/formatters/index.ts +0 -9
- package/src/env/debug/formatters/repl-formatter.test.ts +0 -139
- package/src/env/debug/formatters/repl-formatter.ts +0 -358
- package/src/env/debug/formatters/trace-formatter.test.ts +0 -119
- package/src/env/debug/formatters/trace-formatter.ts +0 -191
- package/src/env/debug/formatters/tree-formatter.test.ts +0 -107
- package/src/env/debug/formatters/tree-formatter.ts +0 -325
- package/src/env/debug/index.ts +0 -38
- package/src/env/debug/parser/regex-parser.test.ts +0 -201
- package/src/env/debug/parser/regex-parser.ts +0 -196
- package/src/env/debug/parser/span-builder.test.ts +0 -241
- package/src/env/debug/parser/span-builder.ts +0 -386
- package/src/env/debug/reader/log-reader.test.ts +0 -170
- package/src/env/debug/reader/log-reader.ts +0 -186
- package/src/env/debug/reader/span-db-reader.test.ts +0 -118
- package/src/env/debug/reader/span-db-reader.ts +0 -201
- package/src/env/debug/types.test.ts +0 -187
- package/src/env/debug/types.ts +0 -171
- package/src/env/environment-init.test.ts +0 -183
- package/src/env/environment-lifecycle.test.ts +0 -516
- package/src/env/environment-service.test.ts +0 -332
- package/src/env/environment.handle-query.test.ts +0 -96
- package/src/env/environment.test.ts +0 -232
- package/src/env/environment.ts +0 -708
- package/src/env/errors.test.ts +0 -165
- package/src/env/errors.ts +0 -157
- package/src/env/event-source/event-source-agent-handler.test.ts +0 -193
- package/src/env/event-source/event-source-agent-handler.ts +0 -111
- package/src/env/event-source/event-source-component.process-cleanup.test.ts +0 -236
- package/src/env/event-source/event-source-component.stop.test.ts +0 -346
- package/src/env/event-source/event-source-component.test.ts +0 -1207
- package/src/env/event-source/event-source-component.ts +0 -1379
- package/src/env/event-source/event-source-config-registration.test.ts +0 -242
- package/src/env/event-source/event-source-config-registration.ts +0 -37
- package/src/env/event-source/event-source-integration.test.ts +0 -320
- package/src/env/event-source/event-source-platform.test.ts +0 -630
- package/src/env/event-source/types.ts +0 -298
- package/src/env/hook/global-hook-manager.ts +0 -162
- package/src/env/hook/hook-manager.test.ts +0 -374
- package/src/env/hook/hook-manager.ts +0 -309
- package/src/env/hook/index.ts +0 -38
- package/src/env/hook/types.ts +0 -138
- package/src/env/index.ts +0 -144
- package/src/env/interface.ts +0 -203
- package/src/env/llm/hooks.test.ts +0 -293
- package/src/env/llm/hooks.ts +0 -316
- package/src/env/llm/index.ts +0 -61
- package/src/env/llm/invoke-threshold-check.test.ts +0 -88
- package/src/env/llm/invoke-timeout.test.ts +0 -54
- package/src/env/llm/invoke.test.ts +0 -71
- package/src/env/llm/invoke.ts +0 -1039
- package/src/env/llm/llm-config.test.ts +0 -523
- package/src/env/llm/llm.test.ts +0 -233
- package/src/env/llm/llm.ts +0 -568
- package/src/env/llm/provider.test.ts +0 -182
- package/src/env/llm/provider.ts +0 -108
- package/src/env/llm/transform.test.ts +0 -251
- package/src/env/llm/transform.ts +0 -286
- package/src/env/llm/types.test.ts +0 -580
- package/src/env/llm/types.ts +0 -424
- package/src/env/log-trace/decorator-otel.test.ts +0 -182
- package/src/env/log-trace/decorator.ts +0 -230
- package/src/env/log-trace/index.ts +0 -79
- package/src/env/log-trace/log-trace-component.test.ts +0 -242
- package/src/env/log-trace/log-trace-component.ts +0 -497
- package/src/env/log-trace/log-trace-config-registration.test.ts +0 -348
- package/src/env/log-trace/log-trace-config-registration.ts +0 -45
- package/src/env/log-trace/logger.test.ts +0 -149
- package/src/env/log-trace/logger.ts +0 -522
- package/src/env/log-trace/opentelemetry/cli-propagation.test.ts +0 -147
- package/src/env/log-trace/opentelemetry/cli-propagation.ts +0 -194
- package/src/env/log-trace/opentelemetry/integration.test.ts +0 -668
- package/src/env/log-trace/opentelemetry/mod.ts +0 -25
- package/src/env/log-trace/opentelemetry/propagation-env.test.ts +0 -181
- package/src/env/log-trace/opentelemetry/propagation-env.ts +0 -136
- package/src/env/log-trace/opentelemetry/propagation.test.ts +0 -259
- package/src/env/log-trace/opentelemetry/propagation.ts +0 -215
- package/src/env/log-trace/opentelemetry/tracer-provider-context.test.ts +0 -166
- package/src/env/log-trace/opentelemetry/tracer-provider.test.ts +0 -379
- package/src/env/log-trace/opentelemetry/tracer-provider.ts +0 -612
- package/src/env/log-trace/span-storage.test.ts +0 -145
- package/src/env/log-trace/span-storage.ts +0 -230
- package/src/env/log-trace/trace-context.test.ts +0 -187
- package/src/env/log-trace/trace-context.ts +0 -162
- package/src/env/log-trace/types.test.ts +0 -63
- package/src/env/log-trace/types.ts +0 -172
- package/src/env/mcp/README.md +0 -244
- package/src/env/mcp/__integration__/mcp-component.integration.test.ts +0 -373
- package/src/env/mcp/config.test.ts +0 -74
- package/src/env/mcp/config.ts +0 -116
- package/src/env/mcp/index.ts +0 -41
- package/src/env/mcp/loader.test.ts +0 -161
- package/src/env/mcp/loader.ts +0 -209
- package/src/env/mcp/mcp-component.test.ts +0 -111
- package/src/env/mcp/mcp-component.ts +0 -358
- package/src/env/mcp/mcp-config-registration.test.ts +0 -304
- package/src/env/mcp/mcp-config-registration.ts +0 -50
- package/src/env/mcp/scanner.test.ts +0 -170
- package/src/env/mcp/scanner.ts +0 -246
- package/src/env/mcp/tool/adapter.test.ts +0 -520
- package/src/env/mcp/tool/adapter.ts +0 -521
- package/src/env/mcp/tool/index.ts +0 -5
- package/src/env/mcp/types.test.ts +0 -171
- package/src/env/mcp/types.ts +0 -79
- package/src/env/memory/README.md +0 -177
- package/src/env/memory/built-in/index.ts +0 -59
- package/src/env/memory/built-in/recall-memory.ts +0 -103
- package/src/env/memory/built-in/record-memory.ts +0 -148
- package/src/env/memory/index.ts +0 -20
- package/src/env/memory/memory-component.test.ts +0 -239
- package/src/env/memory/memory-component.ts +0 -503
- package/src/env/memory/memory-config-registration.test.ts +0 -67
- package/src/env/memory/memory-config-registration.ts +0 -48
- package/src/env/memory/memory-config.ts +0 -45
- package/src/env/memory/memory-file.test.ts +0 -268
- package/src/env/memory/plugin/index.ts +0 -48
- package/src/env/memory/plugin/memory-agent.test.ts +0 -249
- package/src/env/memory/plugin/memory-agent.ts +0 -365
- package/src/env/memory/plugin/memory-manager.ts +0 -198
- package/src/env/memory/plugin/memory-plugin-agent.test.ts +0 -145
- package/src/env/memory/plugin/memory-plugin.ts +0 -210
- package/src/env/memory/plugin/plugin-simplified.test.ts +0 -51
- package/src/env/memory/plugin/recall-memory.test.ts +0 -106
- package/src/env/memory/plugin/recall-memory.ts +0 -53
- package/src/env/memory/plugin/types.ts +0 -101
- package/src/env/memory/tools/memory-agent-tools.ts +0 -228
- package/src/env/memory/types.ts +0 -85
- package/src/env/paths.ts +0 -118
- package/src/env/prompt/index.ts +0 -18
- package/src/env/prompt/memory-prompts.test.ts +0 -91
- package/src/env/prompt/prompt-component.test.ts +0 -491
- package/src/env/prompt/prompt-component.ts +0 -619
- package/src/env/prompt/prompt-config-registration.test.ts +0 -213
- package/src/env/prompt/prompt-config-registration.ts +0 -39
- package/src/env/prompt/prompts-index.ts +0 -504
- package/src/env/prompt/renderer.ts +0 -67
- package/src/env/prompt/types.ts +0 -136
- package/src/env/session/hooks.ts +0 -18
- package/src/env/session/index.ts +0 -37
- package/src/env/session/search-query-parser.test.ts +0 -425
- package/src/env/session/search-query-parser.ts +0 -171
- package/src/env/session/session-checkpoint.test.ts +0 -523
- package/src/env/session/session-component.extract-recent-messages.test.ts +0 -209
- package/src/env/session/session-component.test.ts +0 -132
- package/src/env/session/session-component.ts +0 -1249
- package/src/env/session/session-config-registration.test.ts +0 -138
- package/src/env/session/session-config-registration.ts +0 -52
- package/src/env/session/session-message-converter.test.ts +0 -763
- package/src/env/session/session-message-converter.ts +0 -415
- package/src/env/session/session-message-e2e.test.ts +0 -448
- package/src/env/session/session-search.test.ts +0 -391
- package/src/env/session/session-store.test.ts +0 -362
- package/src/env/session/session-store.ts +0 -141
- package/src/env/session/storage/index.ts +0 -6
- package/src/env/session/storage/memory.ts +0 -502
- package/src/env/session/storage/sqlite.ts +0 -794
- package/src/env/session/types.ts +0 -742
- package/src/env/skill/config.ts +0 -39
- package/src/env/skill/index.ts +0 -6
- package/src/env/skill/parser.test.ts +0 -116
- package/src/env/skill/parser.ts +0 -77
- package/src/env/skill/scanner.test.ts +0 -211
- package/src/env/skill/scanner.ts +0 -119
- package/src/env/skill/skill-component.test.ts +0 -234
- package/src/env/skill/skill-component.ts +0 -352
- package/src/env/skill/skill-config-registration.test.ts +0 -60
- package/src/env/skill/skill-config-registration.ts +0 -43
- package/src/env/skill/tool/index.ts +0 -1
- package/src/env/skill/tool/skill-tool.test.ts +0 -100
- package/src/env/skill/tool/skill-tool.ts +0 -72
- package/src/env/skill/types.ts +0 -64
- package/src/env/task/delegate/delegate-tool.test.ts +0 -498
- package/src/env/task/delegate/delegate-tool.ts +0 -1014
- package/src/env/task/delegate/index.ts +0 -18
- package/src/env/task/delegate/stop-tool.test.ts +0 -140
- package/src/env/task/delegate/stop-tool.ts +0 -119
- package/src/env/task/delegate/task-events.test.ts +0 -178
- package/src/env/task/delegate/task-events.ts +0 -143
- package/src/env/task/hooks/contexts.test.ts +0 -92
- package/src/env/task/hooks/contexts.ts +0 -192
- package/src/env/task/hooks/index.ts +0 -23
- package/src/env/task/hooks/task-hook-points.test.ts +0 -32
- package/src/env/task/hooks/task-hook-points.ts +0 -54
- package/src/env/task/index.ts +0 -7
- package/src/env/task/plugins/index.ts +0 -13
- package/src/env/task/plugins/task-plugin.test.ts +0 -74
- package/src/env/task/plugins/task-plugin.ts +0 -89
- package/src/env/task/plugins/task-tag-plugin.test.ts +0 -377
- package/src/env/task/plugins/task-tag-plugin.ts +0 -319
- package/src/env/task/plugins/task-workflow-extractor.integration.test.ts +0 -226
- package/src/env/task/plugins/workflow-extractor-agent.test.ts +0 -107
- package/src/env/task/plugins/workflow-extractor-agent.ts +0 -225
- package/src/env/task/storage/index.ts +0 -6
- package/src/env/task/storage/sqlite-task-store.test.ts +0 -283
- package/src/env/task/storage/sqlite-task-store.ts +0 -903
- package/src/env/task/storage/task-search.test.ts +0 -291
- package/src/env/task/tag-service.test.ts +0 -198
- package/src/env/task/tag-service.ts +0 -264
- package/src/env/task/task-component.test.ts +0 -193
- package/src/env/task/task-component.ts +0 -658
- package/src/env/task/task-config-registration.test.ts +0 -57
- package/src/env/task/task-config-registration.ts +0 -37
- package/src/env/task/task-types.test.ts +0 -137
- package/src/env/task/tools/complete-tool.ts +0 -44
- package/src/env/task/tools/create-tool.ts +0 -49
- package/src/env/task/tools/delete-tool.ts +0 -43
- package/src/env/task/tools/get-tool.ts +0 -59
- package/src/env/task/tools/index.ts +0 -10
- package/src/env/task/tools/list-tool.ts +0 -40
- package/src/env/task/tools/operation/create-tool.ts +0 -48
- package/src/env/task/tools/operation/delete-tool.ts +0 -43
- package/src/env/task/tools/operation/get-tool.ts +0 -43
- package/src/env/task/tools/operation/index.ts +0 -9
- package/src/env/task/tools/operation/list-tool.ts +0 -40
- package/src/env/task/tools/operation/operation-tools.test.ts +0 -274
- package/src/env/task/tools/operation/operation-types.ts +0 -75
- package/src/env/task/tools/operation/update-tool.ts +0 -47
- package/src/env/task/tools/task-tools.test.ts +0 -203
- package/src/env/task/tools/task-types.test.ts +0 -75
- package/src/env/task/tools/task-types.ts +0 -68
- package/src/env/task/tools/update-tool.ts +0 -70
- package/src/env/task/types.ts +0 -160
- package/src/env/tool/built-in/bash.ts +0 -201
- package/src/env/tool/built-in/echo.ts +0 -29
- package/src/env/tool/built-in/edit-file.test.ts +0 -136
- package/src/env/tool/built-in/edit-file.ts +0 -92
- package/src/env/tool/built-in/glob.test.ts +0 -94
- package/src/env/tool/built-in/glob.ts +0 -65
- package/src/env/tool/built-in/grep.test.ts +0 -122
- package/src/env/tool/built-in/grep.ts +0 -108
- package/src/env/tool/built-in/index.ts +0 -44
- package/src/env/tool/built-in/read-file.test.ts +0 -84
- package/src/env/tool/built-in/read-file.ts +0 -75
- package/src/env/tool/built-in/write-file.test.ts +0 -119
- package/src/env/tool/built-in/write-file.ts +0 -68
- package/src/env/tool/index.ts +0 -24
- package/src/env/tool/registry.test.ts +0 -257
- package/src/env/tool/registry.ts +0 -167
- package/src/env/tool/tool-component.test.ts +0 -559
- package/src/env/tool/tool-component.ts +0 -563
- package/src/env/tool/tool-config-registration.test.ts +0 -249
- package/src/env/tool/tool-config-registration.ts +0 -46
- package/src/env/tool/types.ts +0 -267
- package/src/env/tool/validator.test.ts +0 -143
- package/src/env/tool/validator.ts +0 -44
- package/src/env/types.ts +0 -180
- package/src/env/workflow/ask-user-tool-registration.test.ts +0 -216
- package/src/env/workflow/complex-workflow.integration.test.ts +0 -1900
- package/src/env/workflow/decorators/decorator-node.ts +0 -229
- package/src/env/workflow/decorators/decorator.test.ts +0 -196
- package/src/env/workflow/decorators/edge.ts +0 -82
- package/src/env/workflow/decorators/index.ts +0 -31
- package/src/env/workflow/decorators/node-as.ts +0 -98
- package/src/env/workflow/decorators/workflow.ts +0 -54
- package/src/env/workflow/engine/dag-manager.test.ts +0 -570
- package/src/env/workflow/engine/dag-manager.ts +0 -594
- package/src/env/workflow/engine/engine.ts +0 -1422
- package/src/env/workflow/engine/event-bus.test.ts +0 -359
- package/src/env/workflow/engine/event-bus.ts +0 -156
- package/src/env/workflow/engine/executor-agent-session.test.ts +0 -84
- package/src/env/workflow/engine/executor.test.ts +0 -619
- package/src/env/workflow/engine/executor.ts +0 -593
- package/src/env/workflow/engine/index.ts +0 -24
- package/src/env/workflow/engine/node-registry.test.ts +0 -560
- package/src/env/workflow/engine/node-registry.ts +0 -289
- package/src/env/workflow/engine/resume-removed.test.ts +0 -22
- package/src/env/workflow/engine/scheduler.test.ts +0 -715
- package/src/env/workflow/engine/scheduler.ts +0 -318
- package/src/env/workflow/engine/workflow-engine.test.ts +0 -815
- package/src/env/workflow/extractor/workflow-converter.ts +0 -306
- package/src/env/workflow/fixtures.ts +0 -380
- package/src/env/workflow/index.ts +0 -38
- package/src/env/workflow/integration/run-resume-unified.test.ts +0 -186
- package/src/env/workflow/integration/service-integration.test.ts +0 -267
- package/src/env/workflow/metadata/keys.ts +0 -12
- package/src/env/workflow/nodes/agent-component-adapter.test.ts +0 -318
- package/src/env/workflow/nodes/agent-component-adapter.ts +0 -448
- package/src/env/workflow/nodes/agent-node.test.ts +0 -371
- package/src/env/workflow/nodes/agent-node.ts +0 -598
- package/src/env/workflow/nodes/ask-user-node.ts +0 -113
- package/src/env/workflow/nodes/condition-node.ts +0 -200
- package/src/env/workflow/nodes/index.ts +0 -9
- package/src/env/workflow/nodes/merge-node.ts +0 -141
- package/src/env/workflow/nodes/skill-node.test.ts +0 -253
- package/src/env/workflow/nodes/skill-node.ts +0 -393
- package/src/env/workflow/nodes/tool-node.test.ts +0 -251
- package/src/env/workflow/nodes/tool-node.ts +0 -493
- package/src/env/workflow/nodes/workflow-llm-history.test.ts +0 -455
- package/src/env/workflow/nodes/workflow-node.test.ts +0 -315
- package/src/env/workflow/nodes/workflow-node.ts +0 -311
- package/src/env/workflow/service/index.ts +0 -27
- package/src/env/workflow/service/registry.test.ts +0 -133
- package/src/env/workflow/service/registry.ts +0 -71
- package/src/env/workflow/service/workflow-service.test.ts +0 -310
- package/src/env/workflow/service/workflow-service.ts +0 -393
- package/src/env/workflow/storage/index.ts +0 -28
- package/src/env/workflow/storage/mock-repositories.ts +0 -385
- package/src/env/workflow/storage/sqlite.test.ts +0 -179
- package/src/env/workflow/storage/sqlite.ts +0 -163
- package/src/env/workflow/storage/workflow-repo.test.ts +0 -780
- package/src/env/workflow/storage/workflow-repo.ts +0 -342
- package/src/env/workflow/tools/ask-user-tool.ts +0 -82
- package/src/env/workflow/tools/index.ts +0 -26
- package/src/env/workflow/tools/run-workflow.test.ts +0 -352
- package/src/env/workflow/tools/run-workflow.ts +0 -214
- package/src/env/workflow/types/context.ts +0 -18
- package/src/env/workflow/types/decorators-types.ts +0 -198
- package/src/env/workflow/types/event.test.ts +0 -515
- package/src/env/workflow/types/event.ts +0 -193
- package/src/env/workflow/types/index.ts +0 -49
- package/src/env/workflow/types/run.test.ts +0 -437
- package/src/env/workflow/types/run.ts +0 -173
- package/src/env/workflow/types/workflow-hil.ts +0 -114
- package/src/env/workflow/types/workflow-message.test.ts +0 -138
- package/src/env/workflow/types/workflow-message.ts +0 -196
- package/src/env/workflow/types/workflow-session.test.ts +0 -95
- package/src/env/workflow/types/workflow-session.ts +0 -59
- package/src/env/workflow/types/workflow.test.ts +0 -495
- package/src/env/workflow/types/workflow.ts +0 -195
- package/src/env/workflow/types_compat.ts +0 -51
- package/src/env/workflow/utils/create-workflow.ts +0 -47
- package/src/env/workflow/utils/execution-state.ts +0 -245
- package/src/env/workflow/utils/index.ts +0 -18
- package/src/env/workflow/utils/node-registry-helper.ts +0 -58
- package/src/env/workflow/utils/recovery-validator.test.ts +0 -460
- package/src/env/workflow/utils/recovery-validator.ts +0 -377
- package/src/env/workflow/utils/session-parser.test.ts +0 -111
- package/src/env/workflow/utils/session-parser.ts +0 -94
- package/src/env/workflow/utils/session-recovery.test.ts +0 -334
- package/src/env/workflow/utils/session-recovery.ts +0 -188
- package/src/env/workflow/utils/template-resolver.test.ts +0 -258
- package/src/env/workflow/utils/template-resolver.ts +0 -436
- package/src/env/workflow/utils/validation-rules.ts +0 -149
- package/src/env/workflow/workflow-component.ts +0 -544
- package/src/index.ts +0 -422
- package/src/utils/id.ts +0 -21
|
@@ -1,522 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Logger - 统一日志系统
|
|
3
|
-
*
|
|
4
|
-
* 遵循 XDG Base Directory Specification:
|
|
5
|
-
* - 日志存储在 XDG_DATA_HOME/roy-agent/logs/ (默认 ~/.local/share/roy-agent/logs/)
|
|
6
|
-
* 无需配置,自动创建目录
|
|
7
|
-
*
|
|
8
|
-
* 支持 traceId 追踪:通过 trace-context 模块自动注入
|
|
9
|
-
*
|
|
10
|
-
* 配置优先级(从高到低):
|
|
11
|
-
* 1. ConfigComponent (log-trace.logging.level, log-trace.logging.dir)
|
|
12
|
-
* 2. 环境变量 (LOG_LEVEL, LOG_DIR)
|
|
13
|
-
* 3. setLogDirOverride() 手动设置
|
|
14
|
-
* 4. XDG 默认路径
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { appendFileSync, existsSync, mkdirSync } from "fs";
|
|
18
|
-
import { basename, dirname, join } from "path";
|
|
19
|
-
import { getTracerProvider } from "./opentelemetry/tracer-provider";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 简化 Bun file: 协议的编码路径
|
|
23
|
-
* 例如: packages+core/node_modules/.bun/@roy-agent+core@file+packages+core/node_modules/@roy-agent/core/src/env/agent/agent-component.ts
|
|
24
|
-
* 转换为: packages/core/src/env/agent/agent-component.ts
|
|
25
|
-
*/
|
|
26
|
-
export function simplifyFilePath(fullPath: string): string {
|
|
27
|
-
let path = fullPath.replace(/\\/g, "/");
|
|
28
|
-
|
|
29
|
-
// 处理 Bun 虚拟文件系统路径
|
|
30
|
-
// 例如: /$bunfs/root/cli/src/bin/roy.js -> cli/src/bin/roy.js
|
|
31
|
-
const bunfsMatch = path.match(/\/\$bunfs\/root\/(.+)$/);
|
|
32
|
-
if (bunfsMatch) {
|
|
33
|
-
// 从虚拟路径中提取相对路径
|
|
34
|
-
const virtualPath = bunfsMatch[1];
|
|
35
|
-
// 查找 packages/ 开头的路径
|
|
36
|
-
const packagesMatch = virtualPath.match(/(packages\/[^/]+\/src\/[^/]+\/.+)$/);
|
|
37
|
-
if (packagesMatch) {
|
|
38
|
-
return packagesMatch[1];
|
|
39
|
-
}
|
|
40
|
-
// 返回去掉 bunfs 前缀的路径
|
|
41
|
-
return virtualPath;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// 尝试匹配并简化 @roy-agent/core 的路径
|
|
45
|
-
const fileProtocolMatch = path.match(/@roy-agent\+core@file\+([^/]+)\/node_modules\/@roy-agent\/core\/(.+)$/);
|
|
46
|
-
if (fileProtocolMatch) {
|
|
47
|
-
const rootPkg = fileProtocolMatch[1].replace(/\+/g, "/");
|
|
48
|
-
const remaining = fileProtocolMatch[2];
|
|
49
|
-
const prefix = rootPkg;
|
|
50
|
-
const suffix = remaining.startsWith("src/") ? remaining : `src/${remaining}`;
|
|
51
|
-
return `${prefix}/${suffix}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// 回退:查找 packages/ 开头的路径
|
|
55
|
-
const packagesRootMatch = path.match(/(packages\/[^/]+\/src\/[^/]+\/.+)$/);
|
|
56
|
-
if (packagesRootMatch) {
|
|
57
|
-
return packagesRootMatch[1];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// 回退到简单查找
|
|
61
|
-
const rootMarkers = ["packages/core/src", "packages/core", "packages"];
|
|
62
|
-
for (const marker of rootMarkers) {
|
|
63
|
-
const idx = path.indexOf(marker);
|
|
64
|
-
if (idx !== -1) {
|
|
65
|
-
return path.substring(idx);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return path;
|
|
70
|
-
}
|
|
71
|
-
import type { LogLevel, ILogger } from "./types";
|
|
72
|
-
|
|
73
|
-
// ============================================================================
|
|
74
|
-
// Default Paths (based on XDG_DATA_HOME)
|
|
75
|
-
// ============================================================================
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 获取默认日志目录(基于 XDG 标准)
|
|
79
|
-
* 存储在 XDG_DATA_HOME/roy-agent/logs/
|
|
80
|
-
*/
|
|
81
|
-
function getDefaultLogDir(): string {
|
|
82
|
-
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
83
|
-
// 尝试使用 xdg-basedir
|
|
84
|
-
try {
|
|
85
|
-
const xdg = require("xdg-basedir");
|
|
86
|
-
if (xdg.xdgData) {
|
|
87
|
-
return join(xdg.xdgData, "roy-agent", "logs");
|
|
88
|
-
}
|
|
89
|
-
} catch {}
|
|
90
|
-
// 回退到 ~/.local/share/roy-agent/logs
|
|
91
|
-
return join(home, ".local", "share", "roy-agent", "logs");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 导入 ConfigComponent 类型(延迟导入避免循环依赖)
|
|
95
|
-
let ConfigComponent: any = null;
|
|
96
|
-
|
|
97
|
-
function getConfigComponentClass() {
|
|
98
|
-
if (!ConfigComponent) {
|
|
99
|
-
try {
|
|
100
|
-
ConfigComponent = require("../config/config-component").ConfigComponent;
|
|
101
|
-
} catch {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return ConfigComponent;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
let configComponentInstance: any = null;
|
|
109
|
-
|
|
110
|
-
// ============================================================================
|
|
111
|
-
// 安静模式
|
|
112
|
-
// ============================================================================
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 安静模式:启用后日志只写入文件,不输出到 stdout/stderr
|
|
116
|
-
*/
|
|
117
|
-
let quietMode = false;
|
|
118
|
-
|
|
119
|
-
export function isQuietMode(): boolean {
|
|
120
|
-
return quietMode;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function setQuietMode(enabled: boolean): void {
|
|
124
|
-
quietMode = enabled;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// ============================================================================
|
|
128
|
-
// ConfigComponent 配置
|
|
129
|
-
// ============================================================================
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 设置 ConfigComponent 实例(用于实时配置读取)
|
|
133
|
-
*/
|
|
134
|
-
export function setConfigComponent(component: any): void {
|
|
135
|
-
configComponentInstance = component;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* 获取日志级别(支持热更新)
|
|
140
|
-
*/
|
|
141
|
-
export function getLogLevel(): LogLevel {
|
|
142
|
-
// 1. 优先从 ConfigComponent 获取
|
|
143
|
-
if (configComponentInstance) {
|
|
144
|
-
const level = configComponentInstance.get("log_trace.logging.level");
|
|
145
|
-
if (level && ["debug", "info", "warn", "error"].includes(level)) {
|
|
146
|
-
return level as LogLevel;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// 2. 环境变量
|
|
151
|
-
const envLevel = process.env.LOG_LEVEL as LogLevel;
|
|
152
|
-
if (envLevel && ["debug", "info", "warn", "error"].includes(envLevel)) {
|
|
153
|
-
return envLevel;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// 3. 默认值
|
|
157
|
-
return "info";
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 获取指定类别的日志级别
|
|
162
|
-
*
|
|
163
|
-
* 支持按类别配置日志级别,配置路径:log-trace.logging.levels.<category>
|
|
164
|
-
*
|
|
165
|
-
* @param category 日志类别(如 "llm", "agent", "tool")
|
|
166
|
-
* @returns 该类别的日志级别,如果未配置则返回全局级别
|
|
167
|
-
*/
|
|
168
|
-
export function getCategoryLogLevel(category: string): LogLevel {
|
|
169
|
-
// 1. 从 ConfigComponent 获取按类别日志级别
|
|
170
|
-
if (configComponentInstance) {
|
|
171
|
-
// 支持嵌套配置:log-trace.logging.levels.llm
|
|
172
|
-
const levels = configComponentInstance.get("log_trace.logging.levels");
|
|
173
|
-
if (levels && typeof levels === "object") {
|
|
174
|
-
const categoryLevel = (levels as Record<string, string>)[category];
|
|
175
|
-
if (categoryLevel && ["debug", "info", "warn", "error"].includes(categoryLevel)) {
|
|
176
|
-
return categoryLevel as LogLevel;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
// 也支持直接配置:log-trace.logging.levels.llm = "debug"
|
|
180
|
-
const directLevel = configComponentInstance.get(`log_trace.logging.levels.${category}`);
|
|
181
|
-
if (directLevel && ["debug", "info", "warn", "error"].includes(directLevel as string)) {
|
|
182
|
-
return directLevel as LogLevel;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// 2. 环境变量(使用前缀,如 LOG_LEVEL_LLM)
|
|
187
|
-
const envKey = `LOG_LEVEL_${category.toUpperCase().replace(/:/g, "_")}`;
|
|
188
|
-
const envLevel = process.env[envKey] as LogLevel;
|
|
189
|
-
if (envLevel && ["debug", "info", "warn", "error"].includes(envLevel)) {
|
|
190
|
-
return envLevel;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// 3. 返回全局级别作为默认值
|
|
194
|
-
return getLogLevel();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// ============================================================================
|
|
198
|
-
// 日志目录
|
|
199
|
-
// ============================================================================
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 判断路径是否为绝对路径
|
|
203
|
-
* 支持:Unix (/开头)、Windows (盘符开头)、~ (home目录)
|
|
204
|
-
*/
|
|
205
|
-
function isAbsolutePath(path: string): boolean {
|
|
206
|
-
// Unix 绝对路径
|
|
207
|
-
if (path.startsWith("/")) {
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
// Windows 盘符路径 (如 C:\, D:\)
|
|
211
|
-
if (/^[a-zA-Z]:[/\\]/.test(path)) {
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* 解析路径中的 ~ 为 home 目录
|
|
219
|
-
*/
|
|
220
|
-
function expandHomeDir(path: string): string {
|
|
221
|
-
if (path.startsWith("~")) {
|
|
222
|
-
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
223
|
-
return join(home, path.slice(1));
|
|
224
|
-
}
|
|
225
|
-
return path;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* 获取日志文件名
|
|
230
|
-
* 实时从 ConfigComponent 读取(支持 env/file/memory 多 source)
|
|
231
|
-
*/
|
|
232
|
-
export function getLogFile(): string {
|
|
233
|
-
if (configComponentInstance) {
|
|
234
|
-
const file = configComponentInstance.get("log_trace.logging.file");
|
|
235
|
-
if (file) {
|
|
236
|
-
return file as string;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return "app.log";
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export function getLogDir(): string {
|
|
243
|
-
if (configComponentInstance) {
|
|
244
|
-
const dir = configComponentInstance.get("log_trace.logging.dir");
|
|
245
|
-
if (dir) {
|
|
246
|
-
return dir as string;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return getDefaultLogDir();
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* 获取日志输出最大长度配置
|
|
254
|
-
* 实时从 ConfigComponent 读取
|
|
255
|
-
*
|
|
256
|
-
* @returns 配置的值(0 表示不限制),如果未配置则返回 undefined
|
|
257
|
-
*/
|
|
258
|
-
export function getMaxOutput(): number | undefined {
|
|
259
|
-
if (configComponentInstance) {
|
|
260
|
-
const maxOutput = configComponentInstance.get("log-trace.logging.maxOutput");
|
|
261
|
-
if (typeof maxOutput === "number") {
|
|
262
|
-
return maxOutput;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return undefined; // 未配置
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// 日志目录覆盖(手动设置,优先级最高)
|
|
269
|
-
let logDirOverride: string | undefined;
|
|
270
|
-
|
|
271
|
-
export function setLogDirOverride(dir: string): void {
|
|
272
|
-
logDirOverride = dir;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
export function getLogDirOverride(): string | undefined {
|
|
276
|
-
return logDirOverride;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// 修改 getLogDir 使用 override
|
|
280
|
-
const originalGetLogDir = getLogDir;
|
|
281
|
-
// getLogDir 保持不变,override 由外部使用
|
|
282
|
-
|
|
283
|
-
// ============================================================================
|
|
284
|
-
// Logger 实现
|
|
285
|
-
// ============================================================================
|
|
286
|
-
|
|
287
|
-
const levelPriority: Record<LogLevel, number> = {
|
|
288
|
-
debug: 0,
|
|
289
|
-
info: 1,
|
|
290
|
-
warn: 2,
|
|
291
|
-
error: 3,
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
const levelEnvMap: Record<string, LogLevel> = {
|
|
295
|
-
"debug": "debug",
|
|
296
|
-
"info": "info",
|
|
297
|
-
"warn": "warn",
|
|
298
|
-
"error": "error",
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
function getEffectiveLevel(configured?: LogLevel): LogLevel {
|
|
302
|
-
// 优先级:构造函数配置 > 环境变量 > 默认 info
|
|
303
|
-
if (configured) return configured;
|
|
304
|
-
const envLevel = process.env.LOG_LEVEL as LogLevel;
|
|
305
|
-
return levelEnvMap[envLevel || ""] || "info";
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Log Data
|
|
310
|
-
*
|
|
311
|
-
* 用于日志记录的数据结构
|
|
312
|
-
*/
|
|
313
|
-
interface LogData {
|
|
314
|
-
level?: string;
|
|
315
|
-
category?: string;
|
|
316
|
-
message?: string;
|
|
317
|
-
timestamp?: number;
|
|
318
|
-
callerLocation?: string;
|
|
319
|
-
requestId?: string;
|
|
320
|
-
[key: string]: unknown;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
class Logger implements ILogger {
|
|
324
|
-
private prefix: string;
|
|
325
|
-
|
|
326
|
-
constructor(prefix: string) {
|
|
327
|
-
this.prefix = prefix;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
private shouldLog(level: LogLevel): boolean {
|
|
331
|
-
// 获取该类别的日志级别(支持按类别配置)
|
|
332
|
-
const categoryLevel = getCategoryLogLevel(this.prefix);
|
|
333
|
-
return levelPriority[level] >= levelPriority[categoryLevel];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
private ensureLogDirectory(dir: string): void {
|
|
337
|
-
if (!existsSync(dir)) {
|
|
338
|
-
try {
|
|
339
|
-
mkdirSync(dir, { recursive: true });
|
|
340
|
-
} catch (err) {
|
|
341
|
-
console.error("[Logger] Failed to create log directory:", dir, err);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
private getCallerLocation(): { file: string; line: number } | null {
|
|
347
|
-
const originalLimit = Error.stackTraceLimit;
|
|
348
|
-
Error.stackTraceLimit = 10;
|
|
349
|
-
|
|
350
|
-
const err = new Error();
|
|
351
|
-
Error.captureStackTrace(err, this.formatMessage);
|
|
352
|
-
|
|
353
|
-
const stack = err.stack?.split("\n") || [];
|
|
354
|
-
Error.stackTraceLimit = originalLimit;
|
|
355
|
-
|
|
356
|
-
for (let i = 1; i < stack.length; i++) {
|
|
357
|
-
const line = stack[i];
|
|
358
|
-
if (line.includes("at ") && !line.includes("logger.ts") && !line.includes("formatMessage")) {
|
|
359
|
-
const match = line.match(/at\s+.+\s+\((.+):(\d+):\d+\)/) || line.match(/at\s+(.+):(\d+):\d+/);
|
|
360
|
-
if (match) {
|
|
361
|
-
const filePath = match[1];
|
|
362
|
-
const relativePath = this.getRelativePath(filePath);
|
|
363
|
-
return {
|
|
364
|
-
file: relativePath,
|
|
365
|
-
line: parseInt(match[2], 10),
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return null;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
private getRelativePath(fullPath: string): string {
|
|
374
|
-
return simplifyFilePath(fullPath);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private formatMessage(level: LogLevel, message: string, data?: unknown): string {
|
|
378
|
-
const now = new Date();
|
|
379
|
-
const timestamp = now.toLocaleString("zh-CN", {
|
|
380
|
-
timeZone: "Asia/Shanghai",
|
|
381
|
-
year: "numeric",
|
|
382
|
-
month: "2-digit",
|
|
383
|
-
day: "2-digit",
|
|
384
|
-
hour: "2-digit",
|
|
385
|
-
minute: "2-digit",
|
|
386
|
-
second: "2-digit",
|
|
387
|
-
hour12: false,
|
|
388
|
-
}).replace(/\//g, "-") + "." + String(now.getMilliseconds()).padStart(3, "0");
|
|
389
|
-
const prefix = this.prefix ? `[${this.prefix}]` : "";
|
|
390
|
-
|
|
391
|
-
// 获取 OTel tracer 中的 traceId
|
|
392
|
-
let traceIdStr = "";
|
|
393
|
-
try {
|
|
394
|
-
const provider = getTracerProvider();
|
|
395
|
-
const tracer = provider.getTracer("roy-tracer");
|
|
396
|
-
const context = tracer.getCurrentContext();
|
|
397
|
-
if (context?.traceId) {
|
|
398
|
-
traceIdStr = `[traceId=${context.traceId}]`;
|
|
399
|
-
}
|
|
400
|
-
} catch {
|
|
401
|
-
// 忽略 tracer 获取失败的情况
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// 检查 data 中是否包含 callerLocation(用于 traced 装饰器传递原函数位置)
|
|
405
|
-
let locationStr = "";
|
|
406
|
-
if (data && typeof data === "object" && "callerLocation" in data) {
|
|
407
|
-
const logData = data as LogData;
|
|
408
|
-
locationStr = logData.callerLocation ? ` [${logData.callerLocation}]` : "";
|
|
409
|
-
// 从 data 中移除 callerLocation,避免它在日志中显示
|
|
410
|
-
const { callerLocation: _callerLocation, ...rest } = logData;
|
|
411
|
-
data = Object.keys(rest).length > 0 ? rest : undefined;
|
|
412
|
-
} else {
|
|
413
|
-
const location = this.getCallerLocation();
|
|
414
|
-
if (location) {
|
|
415
|
-
locationStr = ` [${location.file}:${location.line}]`;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
let formatted = `${timestamp} [${level.toUpperCase()}]${traceIdStr}${locationStr}${prefix} ${message}`;
|
|
420
|
-
|
|
421
|
-
if (data !== undefined) {
|
|
422
|
-
if (typeof data === "object") {
|
|
423
|
-
// 使用单行 JSON 格式,避免换行
|
|
424
|
-
formatted += " " + JSON.stringify(data).replace(/\n/g, "");
|
|
425
|
-
} else {
|
|
426
|
-
formatted += " " + String(data);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
// 应用 maxOutput 截断
|
|
431
|
-
const maxOutput = getMaxOutput();
|
|
432
|
-
if (maxOutput && maxOutput > 0 && formatted.length > maxOutput) {
|
|
433
|
-
formatted = formatted.substring(0, maxOutput) + " [TRUNCATED]";
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return formatted;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
private writeToFile(message: string): void {
|
|
440
|
-
try {
|
|
441
|
-
// 实时获取配置,支持热更新
|
|
442
|
-
const dir = getLogDir();
|
|
443
|
-
const filename = getLogFile();
|
|
444
|
-
|
|
445
|
-
// 解析路径:展开 ~,判断绝对/相对路径
|
|
446
|
-
const expandedDir = expandHomeDir(dir);
|
|
447
|
-
const resolvedDir = isAbsolutePath(expandedDir) ? expandedDir : join(process.cwd(), expandedDir);
|
|
448
|
-
this.ensureLogDirectory(resolvedDir);
|
|
449
|
-
const logFile = join(resolvedDir, filename);
|
|
450
|
-
appendFileSync(logFile, message + "\n", "utf-8");
|
|
451
|
-
} catch (err) {
|
|
452
|
-
console.error("[Logger] Failed to write to log file:", err);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
private log(level: LogLevel, message: string, data?: unknown): void {
|
|
457
|
-
if (!this.shouldLog(level)) return;
|
|
458
|
-
|
|
459
|
-
// formatMessage 会处理 callerLocation 并将其从 data 中移除
|
|
460
|
-
// 返回的 formatted 只包含 message 和剩余的 data
|
|
461
|
-
const formatted = this.formatMessage(level, message, data);
|
|
462
|
-
|
|
463
|
-
// 总是写入文件
|
|
464
|
-
this.writeToFile(formatted);
|
|
465
|
-
|
|
466
|
-
// 根据安静模式和级别决定是否输出到控制台
|
|
467
|
-
// 注意:不需要额外传递 data,因为 formatted 已包含所有需要的信息
|
|
468
|
-
if (!isQuietMode()) {
|
|
469
|
-
const consoleMethod = level === "error" ? console.error :
|
|
470
|
-
level === "warn" ? console.warn :
|
|
471
|
-
level === "info" ? console.log :
|
|
472
|
-
console.debug;
|
|
473
|
-
consoleMethod(formatted);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
debug(message: string, data?: unknown): void {
|
|
478
|
-
this.log("debug", message, data);
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
info(message: string, data?: unknown): void {
|
|
482
|
-
this.log("info", message, data);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
warn(message: string, data?: unknown): void {
|
|
486
|
-
this.log("warn", message, data);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
error(message: string, data?: unknown): void {
|
|
490
|
-
this.log("error", message, data);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// ============================================================================
|
|
495
|
-
// 导出
|
|
496
|
-
// ============================================================================
|
|
497
|
-
|
|
498
|
-
const loggerCache = new Map<string, Logger>();
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* 创建或获取 Logger 实例
|
|
502
|
-
* @param prefix Logger 前缀
|
|
503
|
-
*
|
|
504
|
-
* 日志文件名通过 ConfigComponent 实时读取(log-trace.logging.file)
|
|
505
|
-
*/
|
|
506
|
-
export function createLogger(prefix: string): Logger {
|
|
507
|
-
// 缓存 key 只用 prefix,因为 filename 通过配置实时读取
|
|
508
|
-
if (!loggerCache.has(prefix)) {
|
|
509
|
-
loggerCache.set(prefix, new Logger(prefix));
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
return loggerCache.get(prefix)!;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/**
|
|
516
|
-
* 获取所有日志级别
|
|
517
|
-
*/
|
|
518
|
-
export function getLogLevels(): LogLevel[] {
|
|
519
|
-
return ["debug", "info", "warn", "error"];
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
export type { LogLevel, ILogger } from "./types";
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview CLI Propagation 测试
|
|
3
|
-
*
|
|
4
|
-
* 测试 CLI 启动时从环境变量提取 trace context 的功能:
|
|
5
|
-
* - 从 TRACEPARENT 环境变量提取父 span context
|
|
6
|
-
* - 提供便捷函数用于 CLI 初始化
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { describe, test, expect, beforeEach, afterEach, vi } from 'bun:test';
|
|
10
|
-
import { propagation, type ExtractedContext } from './propagation';
|
|
11
|
-
|
|
12
|
-
// 测试目标模块
|
|
13
|
-
// 注意:这是 TDD,测试先于实现
|
|
14
|
-
// import { initTracerFromEnv, extractFromEnv } from './cli-propagation';
|
|
15
|
-
|
|
16
|
-
describe('cli-propagation', () => {
|
|
17
|
-
describe('extractFromEnv', () => {
|
|
18
|
-
// 模拟环境变量
|
|
19
|
-
let originalEnv: NodeJS.ProcessEnv;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
originalEnv = { ...process.env };
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
// 恢复环境变量
|
|
27
|
-
process.env = originalEnv;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('should return undefined when TRACEPARENT is not set', () => {
|
|
31
|
-
delete process.env.TRACEPARENT;
|
|
32
|
-
|
|
33
|
-
// 模拟从环境变量提取
|
|
34
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
35
|
-
const result = propagation.extract(carrier);
|
|
36
|
-
|
|
37
|
-
expect(result).toBeUndefined();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('should extract valid TRACEPARENT from env', () => {
|
|
41
|
-
const traceparent = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
|
|
42
|
-
process.env.TRACEPARENT = traceparent;
|
|
43
|
-
|
|
44
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
45
|
-
const result = propagation.extract(carrier);
|
|
46
|
-
|
|
47
|
-
expect(result).toBeDefined();
|
|
48
|
-
expect(result!.traceId).toBe('0af7651916cd43dd8448eb211c80319c');
|
|
49
|
-
expect(result!.spanId).toBe('b7ad6b7169203331');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('should handle invalid TRACEPARENT gracefully', () => {
|
|
53
|
-
process.env.TRACEPARENT = 'invalid-traceparent';
|
|
54
|
-
|
|
55
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
56
|
-
const result = propagation.extract(carrier);
|
|
57
|
-
|
|
58
|
-
expect(result).toBeUndefined();
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe('initTracerFromEnv', () => {
|
|
63
|
-
// 模拟 OTelTracerProvider
|
|
64
|
-
const mockProvider = {
|
|
65
|
-
initialized: false,
|
|
66
|
-
tracers: new Map(),
|
|
67
|
-
|
|
68
|
-
async initialize(): Promise<void> {
|
|
69
|
-
this.initialized = true;
|
|
70
|
-
},
|
|
71
|
-
|
|
72
|
-
isInitialized(): boolean {
|
|
73
|
-
return this.initialized;
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
getTracer(name: string): any {
|
|
77
|
-
return {
|
|
78
|
-
name,
|
|
79
|
-
setCurrentContext: vi.fn(),
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
test('should initialize with extracted context', async () => {
|
|
85
|
-
const traceparent = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
|
|
86
|
-
const originalEnv = process.env.TRACEPARENT;
|
|
87
|
-
process.env.TRACEPARENT = traceparent;
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
// 模拟从环境变量提取并初始化
|
|
91
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
92
|
-
const extracted = propagation.extract(carrier);
|
|
93
|
-
|
|
94
|
-
expect(extracted).toBeDefined();
|
|
95
|
-
expect(extracted!.traceId).toBe('0af7651916cd43dd8448eb211c80319c');
|
|
96
|
-
expect(extracted!.spanId).toBe('b7ad6b7169203331');
|
|
97
|
-
} finally {
|
|
98
|
-
process.env.TRACEPARENT = originalEnv;
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test('should not initialize when TRACEPARENT is missing', async () => {
|
|
103
|
-
const originalEnv = process.env.TRACEPARENT;
|
|
104
|
-
delete process.env.TRACEPARENT;
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
108
|
-
const extracted = propagation.extract(carrier);
|
|
109
|
-
|
|
110
|
-
expect(extracted).toBeUndefined();
|
|
111
|
-
} finally {
|
|
112
|
-
process.env.TRACEPARENT = originalEnv;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('integration with OTelTracerProvider', () => {
|
|
118
|
-
test('should create SpanContext from extracted env context', async () => {
|
|
119
|
-
const traceparent = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01';
|
|
120
|
-
const originalEnv = process.env.TRACEPARENT;
|
|
121
|
-
process.env.TRACEPARENT = traceparent;
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
// 模拟从环境变量提取
|
|
125
|
-
const carrier = { TRACEPARENT: process.env.TRACEPARENT };
|
|
126
|
-
const extracted = propagation.extract(carrier);
|
|
127
|
-
|
|
128
|
-
// 验证提取的内容
|
|
129
|
-
expect(extracted).toBeDefined();
|
|
130
|
-
|
|
131
|
-
// 从 ExtractedContext 构建 SpanContext
|
|
132
|
-
// 注意:spanId 由接收方生成,parentSpanId 是发送方的 spanId
|
|
133
|
-
const spanContext = {
|
|
134
|
-
traceId: extracted!.traceId,
|
|
135
|
-
// 生成新的 spanId(这里用 extracted 的 parentSpanId 作为占位)
|
|
136
|
-
spanId: '0000000000000000', // placeholder - new spanId will be generated
|
|
137
|
-
parentSpanId: extracted!.spanId,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
expect(spanContext.traceId).toBe('0af7651916cd43dd8448eb211c80319c');
|
|
141
|
-
expect(spanContext.parentSpanId).toBe('b7ad6b7169203331');
|
|
142
|
-
} finally {
|
|
143
|
-
process.env.TRACEPARENT = originalEnv;
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
});
|