@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
|
@@ -0,0 +1,1647 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
function __accessProp(key) {
|
|
7
|
+
return this[key];
|
|
8
|
+
}
|
|
9
|
+
var __toCommonJS = (from) => {
|
|
10
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
11
|
+
if (entry)
|
|
12
|
+
return entry;
|
|
13
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (var key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(entry, key))
|
|
17
|
+
__defProp(entry, key, {
|
|
18
|
+
get: __accessProp.bind(from, key),
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
__moduleCache.set(from, entry);
|
|
23
|
+
return entry;
|
|
24
|
+
};
|
|
25
|
+
var __moduleCache;
|
|
26
|
+
var __returnValue = (v) => v;
|
|
27
|
+
function __exportSetter(name, newValue) {
|
|
28
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
29
|
+
}
|
|
30
|
+
var __export = (target, all) => {
|
|
31
|
+
for (var name in all)
|
|
32
|
+
__defProp(target, name, {
|
|
33
|
+
get: all[name],
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
set: __exportSetter.bind(all, name)
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
40
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
41
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
42
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
43
|
+
else
|
|
44
|
+
for (var i = decorators.length - 1;i >= 0; i--)
|
|
45
|
+
if (d = decorators[i])
|
|
46
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
47
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
48
|
+
};
|
|
49
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
50
|
+
var __require = import.meta.require;
|
|
51
|
+
|
|
52
|
+
// packages/core/src/env/hook/types.ts
|
|
53
|
+
function createHook(meta, fn) {
|
|
54
|
+
return {
|
|
55
|
+
...meta,
|
|
56
|
+
execute: fn
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function createPriorityHook(name, fn, priority) {
|
|
60
|
+
return createHook({ name, priority }, fn);
|
|
61
|
+
}
|
|
62
|
+
// packages/core/src/env/hook/hook-manager.ts
|
|
63
|
+
class HookManager {
|
|
64
|
+
_hooks = new Map;
|
|
65
|
+
componentName;
|
|
66
|
+
componentVersion;
|
|
67
|
+
defaultPriority;
|
|
68
|
+
constructor(options = {}) {
|
|
69
|
+
this.componentName = options.componentName ?? "unknown";
|
|
70
|
+
this.componentVersion = options.componentVersion ?? "0.0.0";
|
|
71
|
+
this.defaultPriority = options.defaultPriority ?? 0;
|
|
72
|
+
}
|
|
73
|
+
register(hookPoint, hook) {
|
|
74
|
+
const hooks = this.getOrCreateHooks(hookPoint);
|
|
75
|
+
hooks.push(hook);
|
|
76
|
+
}
|
|
77
|
+
registerMany(hookPoint, hooks) {
|
|
78
|
+
const hookList = this.getOrCreateHooks(hookPoint);
|
|
79
|
+
hookList.push(...hooks);
|
|
80
|
+
}
|
|
81
|
+
unregister(hookPoint, name) {
|
|
82
|
+
const hooks = this._hooks.get(hookPoint);
|
|
83
|
+
if (!hooks)
|
|
84
|
+
return false;
|
|
85
|
+
const index = hooks.findIndex((h) => h.name === name);
|
|
86
|
+
if (index === -1)
|
|
87
|
+
return false;
|
|
88
|
+
hooks.splice(index, 1);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
unregisterAll(hookPoint) {
|
|
92
|
+
this._hooks.delete(hookPoint);
|
|
93
|
+
}
|
|
94
|
+
async execute(hookPoint, data, metadata = {}) {
|
|
95
|
+
const hooks = this._hooks.get(hookPoint);
|
|
96
|
+
if (!hooks || hooks.length === 0)
|
|
97
|
+
return;
|
|
98
|
+
const sortedHooks = this.sortHooks(hooks);
|
|
99
|
+
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
100
|
+
for (const hook of sortedHooks) {
|
|
101
|
+
await this.safeExecute(hook, ctx);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async executeAndCollect(hookPoint, data, metadata = {}) {
|
|
105
|
+
const hooks = this._hooks.get(hookPoint);
|
|
106
|
+
if (!hooks || hooks.length === 0)
|
|
107
|
+
return [];
|
|
108
|
+
const sortedHooks = this.sortHooks(hooks);
|
|
109
|
+
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
110
|
+
const results = [];
|
|
111
|
+
for (const hook of sortedHooks) {
|
|
112
|
+
const result = await this.safeExecuteAndReturn(hook, ctx);
|
|
113
|
+
if (result !== undefined) {
|
|
114
|
+
results.push(result);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return results;
|
|
118
|
+
}
|
|
119
|
+
count(hookPoint) {
|
|
120
|
+
return this._hooks.get(hookPoint)?.length ?? 0;
|
|
121
|
+
}
|
|
122
|
+
clear() {
|
|
123
|
+
this._hooks.clear();
|
|
124
|
+
}
|
|
125
|
+
getHookPoints() {
|
|
126
|
+
return Array.from(this._hooks.keys());
|
|
127
|
+
}
|
|
128
|
+
hasHooks(hookPoint) {
|
|
129
|
+
return this.count(hookPoint) > 0;
|
|
130
|
+
}
|
|
131
|
+
setComponentInfo(name, version) {
|
|
132
|
+
this.componentName = name;
|
|
133
|
+
this.componentVersion = version;
|
|
134
|
+
}
|
|
135
|
+
get hooks() {
|
|
136
|
+
return this._hooks;
|
|
137
|
+
}
|
|
138
|
+
async executeWithIntervention(hookPoint, data, metadata = {}) {
|
|
139
|
+
const hooks = this._hooks.get(hookPoint);
|
|
140
|
+
if (!hooks || hooks.length === 0) {
|
|
141
|
+
return { stopped: false, results: [] };
|
|
142
|
+
}
|
|
143
|
+
const sortedHooks = this.sortHooks(hooks);
|
|
144
|
+
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
145
|
+
const results = [];
|
|
146
|
+
let stopped = false;
|
|
147
|
+
let action;
|
|
148
|
+
for (const hook of sortedHooks) {
|
|
149
|
+
if (stopped)
|
|
150
|
+
break;
|
|
151
|
+
const result = await this.safeExecuteAndReturn(hook, ctx);
|
|
152
|
+
results.push(result);
|
|
153
|
+
if (result && typeof result === "object" && "stopped" in result) {
|
|
154
|
+
const hookResult = result;
|
|
155
|
+
if (hookResult.stopped) {
|
|
156
|
+
stopped = true;
|
|
157
|
+
action = hookResult.action;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return { stopped, action, results };
|
|
162
|
+
}
|
|
163
|
+
getOrCreateHooks(hookPoint) {
|
|
164
|
+
let hooks = this._hooks.get(hookPoint);
|
|
165
|
+
if (!hooks) {
|
|
166
|
+
hooks = [];
|
|
167
|
+
this._hooks.set(hookPoint, hooks);
|
|
168
|
+
}
|
|
169
|
+
return hooks;
|
|
170
|
+
}
|
|
171
|
+
sortHooks(hooks) {
|
|
172
|
+
return [...hooks].sort((a, b) => {
|
|
173
|
+
const priorityA = a.priority ?? this.defaultPriority;
|
|
174
|
+
const priorityB = b.priority ?? this.defaultPriority;
|
|
175
|
+
return priorityA - priorityB;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
createContext(hookPoint, data, metadata, phase) {
|
|
179
|
+
return {
|
|
180
|
+
component: {
|
|
181
|
+
name: this.componentName,
|
|
182
|
+
version: this.componentVersion
|
|
183
|
+
},
|
|
184
|
+
data,
|
|
185
|
+
metadata,
|
|
186
|
+
phase,
|
|
187
|
+
hookPoint
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
async safeExecute(hook, ctx) {
|
|
191
|
+
try {
|
|
192
|
+
await hook.execute(ctx);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error(`Hook "${hook.name}" failed:`, error);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async safeExecuteAndReturn(hook, ctx) {
|
|
198
|
+
try {
|
|
199
|
+
return await hook.execute(ctx);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error(`Hook "${hook.name}" failed:`, error);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// packages/core/src/env/hook/global-hook-manager.ts
|
|
207
|
+
var globalHookManager = new HookManager;
|
|
208
|
+
var LLMHookPoints = {
|
|
209
|
+
BEFORE_INVOKE: "llm:before.invoke",
|
|
210
|
+
AFTER_INVOKE: "llm:after.invoke",
|
|
211
|
+
ON_STREAM: "llm:on.stream"
|
|
212
|
+
};
|
|
213
|
+
var ToolHookPoints = {
|
|
214
|
+
BEFORE_EXECUTE: "tool:before.execute",
|
|
215
|
+
AFTER_EXECUTE: "tool:after.execute",
|
|
216
|
+
BEFORE_REGISTER: "tool:before.register",
|
|
217
|
+
AFTER_REGISTER: "tool:after.register",
|
|
218
|
+
ON_ERROR: "tool:on.error"
|
|
219
|
+
};
|
|
220
|
+
var hookPointAliases = {
|
|
221
|
+
"before.tool": "tool:before.execute",
|
|
222
|
+
"after.tool": "tool:after.execute",
|
|
223
|
+
"llm.before-invoke": "llm:before.invoke",
|
|
224
|
+
"llm.after-invoke": "llm:after.invoke",
|
|
225
|
+
"llm.stream": "llm:on.stream"
|
|
226
|
+
};
|
|
227
|
+
function setupAliasHooks() {
|
|
228
|
+
const originalRegister = globalHookManager.register.bind(globalHookManager);
|
|
229
|
+
globalHookManager.register = function(hookPoint, hook) {
|
|
230
|
+
originalRegister(hookPoint, hook);
|
|
231
|
+
const alias = hookPointAliases[hookPoint];
|
|
232
|
+
if (alias) {
|
|
233
|
+
originalRegister(alias, hook);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
setupAliasHooks();
|
|
238
|
+
// packages/core/src/env/component.ts
|
|
239
|
+
class BaseComponent {
|
|
240
|
+
_status = "created";
|
|
241
|
+
_enabled = true;
|
|
242
|
+
env;
|
|
243
|
+
hookManager;
|
|
244
|
+
constructor() {
|
|
245
|
+
this.hookManager = new HookManager;
|
|
246
|
+
}
|
|
247
|
+
getStatus() {
|
|
248
|
+
return this._status;
|
|
249
|
+
}
|
|
250
|
+
getConfig() {
|
|
251
|
+
return {
|
|
252
|
+
name: this.name,
|
|
253
|
+
version: this.version,
|
|
254
|
+
enabled: this._enabled,
|
|
255
|
+
env: this.env
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
isEnvInitialized() {
|
|
259
|
+
return this._status !== "created";
|
|
260
|
+
}
|
|
261
|
+
setStatus(status) {
|
|
262
|
+
this._status = status;
|
|
263
|
+
}
|
|
264
|
+
getEnv() {
|
|
265
|
+
return this.env;
|
|
266
|
+
}
|
|
267
|
+
async init(config) {
|
|
268
|
+
if (config?.env) {
|
|
269
|
+
this.env = config.env;
|
|
270
|
+
}
|
|
271
|
+
this.setStatus("initializing");
|
|
272
|
+
try {
|
|
273
|
+
if (config?.name)
|
|
274
|
+
Object.defineProperty(this, "name", { value: config.name, writable: false });
|
|
275
|
+
if (config?.version)
|
|
276
|
+
Object.defineProperty(this, "version", { value: config.version, writable: false });
|
|
277
|
+
} catch {}
|
|
278
|
+
if (config?.enabled !== undefined)
|
|
279
|
+
this._enabled = config.enabled;
|
|
280
|
+
this.hookManager.setComponentInfo(this.name, this.version);
|
|
281
|
+
await this.onInit();
|
|
282
|
+
this.setStatus("running");
|
|
283
|
+
}
|
|
284
|
+
async start() {
|
|
285
|
+
if (this._started)
|
|
286
|
+
return;
|
|
287
|
+
this._started = true;
|
|
288
|
+
await this.onStart();
|
|
289
|
+
this.setStatus("running");
|
|
290
|
+
}
|
|
291
|
+
async stop() {
|
|
292
|
+
this.setStatus("stopping");
|
|
293
|
+
this.hookManager.clear();
|
|
294
|
+
await this.onStop();
|
|
295
|
+
this.setStatus("stopped");
|
|
296
|
+
}
|
|
297
|
+
async onInit() {}
|
|
298
|
+
async onStart() {}
|
|
299
|
+
async onStop() {}
|
|
300
|
+
registerHook(hookPoint, hook) {
|
|
301
|
+
this.hookManager.register(hookPoint, hook);
|
|
302
|
+
}
|
|
303
|
+
addHook(hookPoint, name, fn, priority) {
|
|
304
|
+
this.hookManager.register(hookPoint, createHook({ name, priority }, fn));
|
|
305
|
+
}
|
|
306
|
+
removeHook(hookPoint, name) {
|
|
307
|
+
return this.hookManager.unregister(hookPoint, name);
|
|
308
|
+
}
|
|
309
|
+
async executeHooks(hookPoint, data, metadata) {
|
|
310
|
+
await this.hookManager.execute(hookPoint, data, metadata);
|
|
311
|
+
}
|
|
312
|
+
getConfigComponent() {
|
|
313
|
+
return this.env?.getComponent("config");
|
|
314
|
+
}
|
|
315
|
+
getRuntimeConfig(key, defaultValue) {
|
|
316
|
+
const configComponent = this.getConfigComponent();
|
|
317
|
+
if (configComponent) {
|
|
318
|
+
const value = configComponent.get(key);
|
|
319
|
+
if (value !== undefined) {
|
|
320
|
+
return value;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return defaultValue;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// packages/core/src/env/paths.ts
|
|
328
|
+
import os from "os";
|
|
329
|
+
import path from "path";
|
|
330
|
+
var APP_NAME = "roy-agent";
|
|
331
|
+
function getXdgBase() {
|
|
332
|
+
const home = os.homedir();
|
|
333
|
+
try {
|
|
334
|
+
const xdg = __require("xdg-basedir");
|
|
335
|
+
return {
|
|
336
|
+
config: xdg.xdgConfig || path.join(home, ".config"),
|
|
337
|
+
state: xdg.xdgState || path.join(home, ".local", "state"),
|
|
338
|
+
data: xdg.xdgData || path.join(home, ".local", "share"),
|
|
339
|
+
cache: xdg.xdgCache || path.join(home, ".cache")
|
|
340
|
+
};
|
|
341
|
+
} catch {
|
|
342
|
+
return {
|
|
343
|
+
config: path.join(home, ".config"),
|
|
344
|
+
state: path.join(home, ".local", "state"),
|
|
345
|
+
data: path.join(home, ".local", "share"),
|
|
346
|
+
cache: path.join(home, ".cache")
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function computeXDGPaths() {
|
|
351
|
+
const home = os.homedir();
|
|
352
|
+
const xdg = getXdgBase();
|
|
353
|
+
return {
|
|
354
|
+
home,
|
|
355
|
+
config: path.join(xdg.config, APP_NAME),
|
|
356
|
+
state: path.join(xdg.state, APP_NAME),
|
|
357
|
+
data: path.join(xdg.data, APP_NAME),
|
|
358
|
+
cache: path.join(xdg.cache, APP_NAME)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
class XDGPathsCalculator {
|
|
363
|
+
_paths;
|
|
364
|
+
get paths() {
|
|
365
|
+
if (!this._paths) {
|
|
366
|
+
this._paths = computeXDGPaths();
|
|
367
|
+
}
|
|
368
|
+
return this._paths;
|
|
369
|
+
}
|
|
370
|
+
getPath(type) {
|
|
371
|
+
return this.paths[type];
|
|
372
|
+
}
|
|
373
|
+
getComponentPath(base, subPath) {
|
|
374
|
+
return path.join(this.paths[base], subPath);
|
|
375
|
+
}
|
|
376
|
+
reset() {
|
|
377
|
+
this._paths = undefined;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
var XDG_PATHS = new XDGPathsCalculator;
|
|
381
|
+
function getXDGPaths() {
|
|
382
|
+
return XDG_PATHS.paths;
|
|
383
|
+
}
|
|
384
|
+
function getXDGPath(type) {
|
|
385
|
+
return XDG_PATHS.getPath(type);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// packages/core/src/config/protocol-resolver.ts
|
|
389
|
+
var PROTOCOL_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
390
|
+
|
|
391
|
+
class ProtocolResolver {
|
|
392
|
+
_xdgDataHome;
|
|
393
|
+
readFileFn;
|
|
394
|
+
constructor(options = {}) {
|
|
395
|
+
this._xdgDataHome = options.xdgDataHome ?? ProtocolResolver.getDefaultXDGDataHome();
|
|
396
|
+
if (options.readFileFn) {
|
|
397
|
+
this.readFileFn = options.readFileFn;
|
|
398
|
+
} else {
|
|
399
|
+
const fsSync = __require("fs");
|
|
400
|
+
this.readFileFn = (path2) => fsSync.readFileSync(path2, "utf-8");
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
get xdgDataHome() {
|
|
404
|
+
return this._xdgDataHome;
|
|
405
|
+
}
|
|
406
|
+
set xdgDataHome(value) {
|
|
407
|
+
this._xdgDataHome = value;
|
|
408
|
+
}
|
|
409
|
+
static getDefaultXDGDataHome() {
|
|
410
|
+
return process.env.XDG_DATA_HOME ?? `${process.env.HOME ?? "~"}/.local/share`;
|
|
411
|
+
}
|
|
412
|
+
parse(ref) {
|
|
413
|
+
const match = ref.match(PROTOCOL_PATTERN);
|
|
414
|
+
if (!match)
|
|
415
|
+
return null;
|
|
416
|
+
const [, protocol, path2, key, defaultValue] = match;
|
|
417
|
+
if (!["file", "env"].includes(protocol))
|
|
418
|
+
return null;
|
|
419
|
+
return {
|
|
420
|
+
protocol,
|
|
421
|
+
path: path2,
|
|
422
|
+
key: key ?? "",
|
|
423
|
+
defaultValue,
|
|
424
|
+
raw: ref
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
isProtocolRef(value) {
|
|
428
|
+
return PROTOCOL_PATTERN.test(value);
|
|
429
|
+
}
|
|
430
|
+
resolve(ref) {
|
|
431
|
+
const parsed = this.parse(ref);
|
|
432
|
+
if (!parsed)
|
|
433
|
+
return null;
|
|
434
|
+
const value = this.resolveInternal(parsed);
|
|
435
|
+
if (parsed.protocol === "file" && value === undefined) {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
if (value === undefined && parsed.protocol === "env" && parsed.defaultValue === undefined) {
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
if (value === undefined && parsed.defaultValue !== undefined) {
|
|
442
|
+
return {
|
|
443
|
+
value: parsed.defaultValue,
|
|
444
|
+
source: parsed.protocol
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
value,
|
|
449
|
+
source: parsed.protocol
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
resolveFileRef(filePath, key) {
|
|
453
|
+
const resolvedPath = this.resolvePath(filePath);
|
|
454
|
+
try {
|
|
455
|
+
const content = this.readFileFn(resolvedPath);
|
|
456
|
+
let data;
|
|
457
|
+
try {
|
|
458
|
+
data = JSON.parse(content);
|
|
459
|
+
} catch {
|
|
460
|
+
return content;
|
|
461
|
+
}
|
|
462
|
+
return this.getNestedValue(data, key);
|
|
463
|
+
} catch {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
resolveInternal(ref) {
|
|
468
|
+
switch (ref.protocol) {
|
|
469
|
+
case "env":
|
|
470
|
+
return process.env[ref.path];
|
|
471
|
+
case "file":
|
|
472
|
+
return this.resolveFileRef(ref.path, ref.key);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
resolvePath(filePath) {
|
|
476
|
+
if (filePath.startsWith("/")) {
|
|
477
|
+
return filePath;
|
|
478
|
+
}
|
|
479
|
+
if (filePath.startsWith("~")) {
|
|
480
|
+
return filePath.replace("~", process.env.HOME ?? "");
|
|
481
|
+
}
|
|
482
|
+
return `${this._xdgDataHome}/${filePath}`;
|
|
483
|
+
}
|
|
484
|
+
getNestedValue(obj, key) {
|
|
485
|
+
if (key === "$" || key === "")
|
|
486
|
+
return obj;
|
|
487
|
+
const keys = key.split(".");
|
|
488
|
+
let current = obj;
|
|
489
|
+
for (const k of keys) {
|
|
490
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
current = current[k];
|
|
494
|
+
}
|
|
495
|
+
return current;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// packages/core/src/config/file-source.ts
|
|
500
|
+
import * as fsSync from "fs";
|
|
501
|
+
import * as fsPromises from "fs/promises";
|
|
502
|
+
import path2 from "path";
|
|
503
|
+
|
|
504
|
+
// packages/core/src/config/config-parser.ts
|
|
505
|
+
import { parse as parseJSONCInternal, ParseErrorCode } from "jsonc-parser";
|
|
506
|
+
function parseJSONC(content) {
|
|
507
|
+
const errors = [];
|
|
508
|
+
const result = parseJSONCInternal(content, errors, {
|
|
509
|
+
allowTrailingComma: true,
|
|
510
|
+
disallowComments: false
|
|
511
|
+
});
|
|
512
|
+
if (errors.length > 0) {
|
|
513
|
+
const hasOnlyValueExpected = result === undefined && errors.length === 1 && errors[0].error === ParseErrorCode.ValueExpected;
|
|
514
|
+
if (!hasOnlyValueExpected) {
|
|
515
|
+
const error = errors[0];
|
|
516
|
+
const offset = error.offset;
|
|
517
|
+
const lines = content.substring(0, offset).split(`
|
|
518
|
+
`);
|
|
519
|
+
const line = lines.length;
|
|
520
|
+
const col = lines[lines.length - 1].length + 1;
|
|
521
|
+
const errorMessages = {
|
|
522
|
+
[ParseErrorCode.UnexpectedEndOfString]: "Unexpected end of string",
|
|
523
|
+
[ParseErrorCode.InvalidCharacter]: "Invalid character",
|
|
524
|
+
[ParseErrorCode.PropertyNameExpected]: "Property name expected",
|
|
525
|
+
[ParseErrorCode.ValueExpected]: "Value expected",
|
|
526
|
+
[ParseErrorCode.ColonExpected]: "Colon expected",
|
|
527
|
+
[ParseErrorCode.CommaExpected]: "Comma expected",
|
|
528
|
+
[ParseErrorCode.CloseBraceExpected]: "Close brace expected",
|
|
529
|
+
[ParseErrorCode.CloseBracketExpected]: "Close bracket expected",
|
|
530
|
+
[ParseErrorCode.EndOfFileExpected]: "Unexpected end of input",
|
|
531
|
+
[ParseErrorCode.InvalidSymbol]: "Invalid symbol"
|
|
532
|
+
};
|
|
533
|
+
throw new Error(`JSONC parse error at line ${line}, col ${col}: ${errorMessages[error.error] || "Unknown error"}`);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (result === undefined || result === null) {
|
|
537
|
+
return {};
|
|
538
|
+
}
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
function substituteEnvVars(value) {
|
|
542
|
+
if (typeof value === "string") {
|
|
543
|
+
return value.replace(/\$\{([^}]+)\}/g, (match, varExpr) => {
|
|
544
|
+
const [varName, defaultValue] = varExpr.split(":-");
|
|
545
|
+
const envValue = process.env[varName.trim()];
|
|
546
|
+
if (envValue !== undefined) {
|
|
547
|
+
return envValue;
|
|
548
|
+
}
|
|
549
|
+
if (defaultValue !== undefined) {
|
|
550
|
+
return defaultValue;
|
|
551
|
+
}
|
|
552
|
+
return "";
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
if (Array.isArray(value)) {
|
|
556
|
+
return value.map((item) => substituteEnvVars(item));
|
|
557
|
+
}
|
|
558
|
+
if (value !== null && typeof value === "object") {
|
|
559
|
+
const result = {};
|
|
560
|
+
for (const [key, val] of Object.entries(value)) {
|
|
561
|
+
result[key] = substituteEnvVars(val);
|
|
562
|
+
}
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
return value;
|
|
566
|
+
}
|
|
567
|
+
function parseJSONCWithEnv(content) {
|
|
568
|
+
const parsed = parseJSONC(content);
|
|
569
|
+
return substituteEnvVars(parsed);
|
|
570
|
+
}
|
|
571
|
+
var PROTOCOL_REF_PATTERN = /^\$\{([^:]+):\/\/[^}]+\}$/;
|
|
572
|
+
var PROTOCOL_REF_WITH_DEFAULT_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
573
|
+
function isProtocolRefString(value) {
|
|
574
|
+
return PROTOCOL_REF_PATTERN.test(value);
|
|
575
|
+
}
|
|
576
|
+
function extractDefaultValue(ref) {
|
|
577
|
+
const match = ref.match(PROTOCOL_REF_WITH_DEFAULT_PATTERN);
|
|
578
|
+
return match ? match[4] : undefined;
|
|
579
|
+
}
|
|
580
|
+
function substituteProtocolRefs(value, resolver) {
|
|
581
|
+
if (typeof value === "string") {
|
|
582
|
+
if (isProtocolRefString(value)) {
|
|
583
|
+
const result = resolver.resolve(value);
|
|
584
|
+
if (result !== null) {
|
|
585
|
+
return result.value;
|
|
586
|
+
}
|
|
587
|
+
const defaultValue = extractDefaultValue(value);
|
|
588
|
+
if (defaultValue !== undefined) {
|
|
589
|
+
return defaultValue;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return value;
|
|
593
|
+
}
|
|
594
|
+
if (Array.isArray(value)) {
|
|
595
|
+
return value.map((item) => substituteProtocolRefs(item, resolver));
|
|
596
|
+
}
|
|
597
|
+
if (value !== null && typeof value === "object") {
|
|
598
|
+
const result = {};
|
|
599
|
+
for (const [key, val] of Object.entries(value)) {
|
|
600
|
+
result[key] = substituteProtocolRefs(val, resolver);
|
|
601
|
+
}
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
return value;
|
|
605
|
+
}
|
|
606
|
+
function parseJSONCWithProtocols(content, resolver) {
|
|
607
|
+
const parsed = parseJSONC(content);
|
|
608
|
+
const withProtocols = substituteProtocolRefs(parsed, resolver);
|
|
609
|
+
return substituteEnvVars(withProtocols);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// packages/core/src/config/file-source.ts
|
|
613
|
+
class FileSource {
|
|
614
|
+
name = "file";
|
|
615
|
+
priority = 10;
|
|
616
|
+
filePath;
|
|
617
|
+
data = {};
|
|
618
|
+
watchers = new Set;
|
|
619
|
+
watcherHandle;
|
|
620
|
+
debounceMs;
|
|
621
|
+
pendingWrite;
|
|
622
|
+
lastFileContent = "";
|
|
623
|
+
resolver;
|
|
624
|
+
optional = false;
|
|
625
|
+
watchEnabled = true;
|
|
626
|
+
constructor(options, resolver) {
|
|
627
|
+
if (typeof options === "string") {
|
|
628
|
+
this.filePath = options;
|
|
629
|
+
this.debounceMs = 100;
|
|
630
|
+
this.resolver = resolver;
|
|
631
|
+
this.optional = true;
|
|
632
|
+
} else {
|
|
633
|
+
this.filePath = options.filePath;
|
|
634
|
+
this.debounceMs = options.debounceMs ?? 100;
|
|
635
|
+
this.resolver = options.resolver ?? resolver;
|
|
636
|
+
this.optional = options.optional ?? true;
|
|
637
|
+
this.watchEnabled = options.watch ?? true;
|
|
638
|
+
}
|
|
639
|
+
this.initSync();
|
|
640
|
+
}
|
|
641
|
+
initSync() {
|
|
642
|
+
try {
|
|
643
|
+
if (fsSync.existsSync(this.filePath)) {
|
|
644
|
+
const content = fsSync.readFileSync(this.filePath, "utf-8");
|
|
645
|
+
if (this.resolver) {
|
|
646
|
+
this.data = parseJSONCWithProtocols(content, this.resolver);
|
|
647
|
+
} else {
|
|
648
|
+
this.data = parseJSONCWithEnv(content);
|
|
649
|
+
}
|
|
650
|
+
this.lastFileContent = content;
|
|
651
|
+
} else {
|
|
652
|
+
if (this.optional) {
|
|
653
|
+
this.data = {};
|
|
654
|
+
this.lastFileContent = "{}";
|
|
655
|
+
} else {
|
|
656
|
+
throw new Error(`Config file not found: ${this.filePath}`);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
} catch (error) {
|
|
660
|
+
if (this.optional) {
|
|
661
|
+
this.data = {};
|
|
662
|
+
this.lastFileContent = "{}";
|
|
663
|
+
} else {
|
|
664
|
+
throw error;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
async ensureInit() {
|
|
669
|
+
if (this.lastFileContent === "" && !fsSync.existsSync(this.filePath)) {
|
|
670
|
+
this.initSync();
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
getNestedValue(obj, key) {
|
|
674
|
+
const keys = key.split(".");
|
|
675
|
+
let current = obj;
|
|
676
|
+
for (const k of keys) {
|
|
677
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
current = current[k];
|
|
681
|
+
}
|
|
682
|
+
return current;
|
|
683
|
+
}
|
|
684
|
+
setNestedValue(obj, key, value) {
|
|
685
|
+
const keys = key.split(".");
|
|
686
|
+
let current = obj;
|
|
687
|
+
for (let i = 0;i < keys.length - 1; i++) {
|
|
688
|
+
const k = keys[i];
|
|
689
|
+
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
690
|
+
current[k] = {};
|
|
691
|
+
}
|
|
692
|
+
current = current[k];
|
|
693
|
+
}
|
|
694
|
+
current[keys[keys.length - 1]] = value;
|
|
695
|
+
}
|
|
696
|
+
deleteNestedValue(obj, key) {
|
|
697
|
+
const keys = key.split(".");
|
|
698
|
+
let current = obj;
|
|
699
|
+
for (let i = 0;i < keys.length - 1; i++) {
|
|
700
|
+
const k = keys[i];
|
|
701
|
+
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
702
|
+
return false;
|
|
703
|
+
}
|
|
704
|
+
current = current[k];
|
|
705
|
+
}
|
|
706
|
+
const lastKey = keys[keys.length - 1];
|
|
707
|
+
if (lastKey in current) {
|
|
708
|
+
delete current[lastKey];
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
flattenObject(obj, prefix = "") {
|
|
714
|
+
if (obj === null || obj === undefined) {
|
|
715
|
+
return [];
|
|
716
|
+
}
|
|
717
|
+
if (typeof obj !== "object") {
|
|
718
|
+
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
719
|
+
}
|
|
720
|
+
if (Array.isArray(obj)) {
|
|
721
|
+
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
722
|
+
}
|
|
723
|
+
const result = [];
|
|
724
|
+
const entries = Object.entries(obj);
|
|
725
|
+
for (const [k, v] of entries) {
|
|
726
|
+
const newKey = prefix ? `${prefix}.${k}` : k;
|
|
727
|
+
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
728
|
+
result.push(...this.flattenObject(v, newKey));
|
|
729
|
+
} else {
|
|
730
|
+
result.push({ key: newKey, value: v });
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return result;
|
|
734
|
+
}
|
|
735
|
+
read(key) {
|
|
736
|
+
return this.getNestedValue(this.data, key);
|
|
737
|
+
}
|
|
738
|
+
async readAsync(key) {
|
|
739
|
+
await this.ensureInit();
|
|
740
|
+
return this.getNestedValue(this.data, key);
|
|
741
|
+
}
|
|
742
|
+
write(key, value) {
|
|
743
|
+
const oldValue = this.getNestedValue(this.data, key);
|
|
744
|
+
this.setNestedValue(this.data, key, value);
|
|
745
|
+
const event = {
|
|
746
|
+
type: oldValue === undefined ? "add" : "change",
|
|
747
|
+
key,
|
|
748
|
+
oldValue,
|
|
749
|
+
newValue: value,
|
|
750
|
+
source: this.name,
|
|
751
|
+
timestamp: Date.now()
|
|
752
|
+
};
|
|
753
|
+
this.notifyWatchers(event);
|
|
754
|
+
this.scheduleWrite();
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
scheduleWrite() {
|
|
758
|
+
if (this.pendingWrite) {
|
|
759
|
+
clearTimeout(this.pendingWrite);
|
|
760
|
+
}
|
|
761
|
+
this.pendingWrite = setTimeout(() => {
|
|
762
|
+
this.persistToFile();
|
|
763
|
+
}, this.debounceMs);
|
|
764
|
+
}
|
|
765
|
+
persistToFile() {
|
|
766
|
+
try {
|
|
767
|
+
const dir = path2.dirname(this.filePath);
|
|
768
|
+
if (!fsSync.existsSync(dir)) {
|
|
769
|
+
fsSync.mkdirSync(dir, { recursive: true });
|
|
770
|
+
}
|
|
771
|
+
const content = JSON.stringify(this.data, null, 2);
|
|
772
|
+
fsSync.writeFileSync(this.filePath, content, "utf-8");
|
|
773
|
+
this.lastFileContent = content;
|
|
774
|
+
} catch (error) {
|
|
775
|
+
console.error(`Failed to write config to ${this.filePath}:`, error);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
delete(key) {
|
|
779
|
+
const oldValue = this.getNestedValue(this.data, key);
|
|
780
|
+
if (oldValue === undefined) {
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
this.deleteNestedValue(this.data, key);
|
|
784
|
+
const event = {
|
|
785
|
+
type: "delete",
|
|
786
|
+
key,
|
|
787
|
+
oldValue,
|
|
788
|
+
newValue: undefined,
|
|
789
|
+
source: this.name,
|
|
790
|
+
timestamp: Date.now()
|
|
791
|
+
};
|
|
792
|
+
this.notifyWatchers(event);
|
|
793
|
+
this.scheduleWrite();
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
796
|
+
list() {
|
|
797
|
+
return this.flattenObject(this.data);
|
|
798
|
+
}
|
|
799
|
+
async listAsync() {
|
|
800
|
+
await this.ensureInit();
|
|
801
|
+
return this.flattenObject(this.data);
|
|
802
|
+
}
|
|
803
|
+
watch(callback) {
|
|
804
|
+
this.watchers.add(callback);
|
|
805
|
+
if (!this.watchEnabled) {
|
|
806
|
+
return () => {
|
|
807
|
+
this.watchers.delete(callback);
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
this.ensureWatch();
|
|
811
|
+
return () => {
|
|
812
|
+
this.watchers.delete(callback);
|
|
813
|
+
if (this.watchers.size === 0) {
|
|
814
|
+
this.stopWatch();
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
async ensureWatch() {
|
|
819
|
+
if (this.watcherHandle !== undefined) {
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
await this.ensureInit();
|
|
823
|
+
if (!fsSync.existsSync(this.filePath)) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
try {
|
|
827
|
+
this.watcherHandle = fsSync.watch(this.filePath, async (eventType) => {
|
|
828
|
+
if (eventType === "change") {
|
|
829
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
830
|
+
await this.reloadFromFile();
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
} catch (error) {
|
|
834
|
+
console.error(`Failed to watch file ${this.filePath}:`, error);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
stopWatch() {
|
|
838
|
+
if (this.watcherHandle) {
|
|
839
|
+
this.watcherHandle.close();
|
|
840
|
+
this.watcherHandle = undefined;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
async reloadFromFile() {
|
|
844
|
+
try {
|
|
845
|
+
const content = await fsPromises.readFile(this.filePath, "utf-8");
|
|
846
|
+
if (content === this.lastFileContent) {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
const newData = parseJSONCWithEnv(content);
|
|
850
|
+
const oldData = this.data;
|
|
851
|
+
this.data = newData;
|
|
852
|
+
this.lastFileContent = content;
|
|
853
|
+
const oldFlat = this.flattenObject(oldData);
|
|
854
|
+
const newFlat = this.flattenObject(newData);
|
|
855
|
+
const oldKeys = new Set(oldFlat.map((e) => e.key));
|
|
856
|
+
const newKeys = new Set(newFlat.map((e) => e.key));
|
|
857
|
+
for (const key of oldKeys) {
|
|
858
|
+
if (!newKeys.has(key)) {
|
|
859
|
+
this.notifyWatchers({
|
|
860
|
+
type: "delete",
|
|
861
|
+
key,
|
|
862
|
+
oldValue: oldFlat.find((e) => e.key === key)?.value,
|
|
863
|
+
newValue: undefined,
|
|
864
|
+
source: this.name,
|
|
865
|
+
timestamp: Date.now()
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
for (const newEntry of newFlat) {
|
|
870
|
+
const oldEntry = oldFlat.find((e) => e.key === newEntry.key);
|
|
871
|
+
if (!oldEntry) {
|
|
872
|
+
this.notifyWatchers({
|
|
873
|
+
type: "add",
|
|
874
|
+
key: newEntry.key,
|
|
875
|
+
oldValue: undefined,
|
|
876
|
+
newValue: newEntry.value,
|
|
877
|
+
source: this.name,
|
|
878
|
+
timestamp: Date.now()
|
|
879
|
+
});
|
|
880
|
+
} else if (oldEntry.value !== newEntry.value) {
|
|
881
|
+
this.notifyWatchers({
|
|
882
|
+
type: "change",
|
|
883
|
+
key: newEntry.key,
|
|
884
|
+
oldValue: oldEntry.value,
|
|
885
|
+
newValue: newEntry.value,
|
|
886
|
+
source: this.name,
|
|
887
|
+
timestamp: Date.now()
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
} catch (error) {
|
|
892
|
+
console.error(`Failed to reload config from ${this.filePath}:`, error);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
notifyWatchers(event) {
|
|
896
|
+
this.watchers.forEach((cb) => cb(event));
|
|
897
|
+
}
|
|
898
|
+
async refresh() {
|
|
899
|
+
await this.reloadFromFile();
|
|
900
|
+
}
|
|
901
|
+
async close() {
|
|
902
|
+
this.stopWatch();
|
|
903
|
+
if (this.pendingWrite) {
|
|
904
|
+
clearTimeout(this.pendingWrite);
|
|
905
|
+
this.pendingWrite = undefined;
|
|
906
|
+
}
|
|
907
|
+
this.persistToFile();
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// packages/core/src/config/env-key.ts
|
|
912
|
+
function toEnvKey(key, prefix) {
|
|
913
|
+
let keyNormalized = key.replace(/[.-]/g, "_");
|
|
914
|
+
keyNormalized = keyNormalized.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/_+/g, "_").toUpperCase();
|
|
915
|
+
if (prefix) {
|
|
916
|
+
const separator = prefix.endsWith("_") ? "" : "_";
|
|
917
|
+
return `${prefix}${separator}${keyNormalized}`;
|
|
918
|
+
}
|
|
919
|
+
return keyNormalized;
|
|
920
|
+
}
|
|
921
|
+
function fromEnvKey(envKey, prefix) {
|
|
922
|
+
if (!prefix) {
|
|
923
|
+
return envKey;
|
|
924
|
+
}
|
|
925
|
+
if (envKey.startsWith(prefix)) {
|
|
926
|
+
const afterPrefix = envKey.slice(prefix.length);
|
|
927
|
+
if (afterPrefix.startsWith("_")) {
|
|
928
|
+
return afterPrefix.slice(1);
|
|
929
|
+
}
|
|
930
|
+
return afterPrefix;
|
|
931
|
+
}
|
|
932
|
+
return envKey;
|
|
933
|
+
}
|
|
934
|
+
function envKeyToConfigKey(envKey, prefix, componentName) {
|
|
935
|
+
const prefixUpper = prefix.toUpperCase();
|
|
936
|
+
if (!envKey.startsWith(prefixUpper)) {
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
const componentUpperNormalized = componentName.replace(/-/g, "_").toUpperCase();
|
|
940
|
+
let keyPart = envKey.slice(prefixUpper.length);
|
|
941
|
+
if (keyPart.startsWith("_")) {
|
|
942
|
+
keyPart = keyPart.slice(1);
|
|
943
|
+
}
|
|
944
|
+
if (!keyPart) {
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
const firstUnderscore = keyPart.indexOf("_");
|
|
948
|
+
let restPart;
|
|
949
|
+
if (firstUnderscore === -1) {
|
|
950
|
+
restPart = keyPart;
|
|
951
|
+
} else {
|
|
952
|
+
const firstPart = keyPart.slice(0, firstUnderscore);
|
|
953
|
+
const remaining = keyPart.slice(firstUnderscore + 1);
|
|
954
|
+
if (firstPart === componentUpperNormalized) {
|
|
955
|
+
restPart = remaining;
|
|
956
|
+
} else {
|
|
957
|
+
restPart = keyPart;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
if (!restPart) {
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
const restPartConverted = restPart.replace(/_/g, ".");
|
|
964
|
+
return `${componentName}.${restPartConverted.toLowerCase()}`;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// packages/core/src/config/env-source.ts
|
|
968
|
+
class EnvSource {
|
|
969
|
+
name = "env";
|
|
970
|
+
priority = 20;
|
|
971
|
+
prefix;
|
|
972
|
+
transform;
|
|
973
|
+
pollInterval;
|
|
974
|
+
watchers = new Set;
|
|
975
|
+
pollTimer;
|
|
976
|
+
lastValues = new Map;
|
|
977
|
+
watchEnabled = true;
|
|
978
|
+
constructor(options = {}) {
|
|
979
|
+
this.prefix = options.prefix ?? "";
|
|
980
|
+
this.transform = options.transform;
|
|
981
|
+
this.pollInterval = options.pollInterval;
|
|
982
|
+
this.watchEnabled = options.watch ?? true;
|
|
983
|
+
}
|
|
984
|
+
getEnvKey(key) {
|
|
985
|
+
return toEnvKey(key, this.prefix);
|
|
986
|
+
}
|
|
987
|
+
getInternalKey(envKey) {
|
|
988
|
+
return fromEnvKey(envKey, this.prefix);
|
|
989
|
+
}
|
|
990
|
+
read(key) {
|
|
991
|
+
const envKey = this.getEnvKey(key);
|
|
992
|
+
const value = process.env[envKey];
|
|
993
|
+
if (value === undefined) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
if (this.transform) {
|
|
997
|
+
return this.transform(value, key);
|
|
998
|
+
}
|
|
999
|
+
return value;
|
|
1000
|
+
}
|
|
1001
|
+
write(key, value) {
|
|
1002
|
+
const envKey = this.getEnvKey(key);
|
|
1003
|
+
const oldValue = process.env[envKey];
|
|
1004
|
+
const stringValue = String(value);
|
|
1005
|
+
process.env[envKey] = stringValue;
|
|
1006
|
+
const event = {
|
|
1007
|
+
type: oldValue === undefined ? "add" : "change",
|
|
1008
|
+
key,
|
|
1009
|
+
oldValue: oldValue !== undefined ? this.transformValue(oldValue, key) : undefined,
|
|
1010
|
+
newValue: this.transformValue(stringValue, key),
|
|
1011
|
+
source: this.name,
|
|
1012
|
+
timestamp: Date.now()
|
|
1013
|
+
};
|
|
1014
|
+
this.notifyWatchers(event);
|
|
1015
|
+
return true;
|
|
1016
|
+
}
|
|
1017
|
+
delete(key) {
|
|
1018
|
+
const envKey = this.getEnvKey(key);
|
|
1019
|
+
const oldValue = process.env[envKey];
|
|
1020
|
+
if (oldValue === undefined) {
|
|
1021
|
+
return false;
|
|
1022
|
+
}
|
|
1023
|
+
delete process.env[envKey];
|
|
1024
|
+
const event = {
|
|
1025
|
+
type: "delete",
|
|
1026
|
+
key,
|
|
1027
|
+
oldValue: this.transformValue(oldValue, key),
|
|
1028
|
+
newValue: undefined,
|
|
1029
|
+
source: this.name,
|
|
1030
|
+
timestamp: Date.now()
|
|
1031
|
+
};
|
|
1032
|
+
this.notifyWatchers(event);
|
|
1033
|
+
return true;
|
|
1034
|
+
}
|
|
1035
|
+
list() {
|
|
1036
|
+
const result = [];
|
|
1037
|
+
const prefix = this.prefix.toUpperCase();
|
|
1038
|
+
for (const envKey of Object.keys(process.env)) {
|
|
1039
|
+
if (prefix && !envKey.startsWith(prefix)) {
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
const key = this.getInternalKey(envKey);
|
|
1043
|
+
const value = process.env[envKey];
|
|
1044
|
+
if (value !== undefined) {
|
|
1045
|
+
result.push({
|
|
1046
|
+
key,
|
|
1047
|
+
value: this.transformValue(value, key)
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
return result;
|
|
1052
|
+
}
|
|
1053
|
+
watch(callback) {
|
|
1054
|
+
this.watchers.add(callback);
|
|
1055
|
+
if (!this.watchEnabled) {
|
|
1056
|
+
return () => {
|
|
1057
|
+
this.watchers.delete(callback);
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
this.ensurePolling();
|
|
1061
|
+
this.recordCurrentValues();
|
|
1062
|
+
return () => {
|
|
1063
|
+
this.watchers.delete(callback);
|
|
1064
|
+
if (this.watchers.size === 0) {
|
|
1065
|
+
this.stopPolling();
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
ensurePolling() {
|
|
1070
|
+
if (this.pollTimer !== undefined) {
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
const interval = this.pollInterval ?? 1000;
|
|
1074
|
+
this.pollTimer = setInterval(() => {
|
|
1075
|
+
this.checkForChanges();
|
|
1076
|
+
}, interval);
|
|
1077
|
+
}
|
|
1078
|
+
stopPolling() {
|
|
1079
|
+
if (this.pollTimer) {
|
|
1080
|
+
clearInterval(this.pollTimer);
|
|
1081
|
+
this.pollTimer = undefined;
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
recordCurrentValues() {
|
|
1085
|
+
this.lastValues.clear();
|
|
1086
|
+
const entries = this.list();
|
|
1087
|
+
for (const entry of entries) {
|
|
1088
|
+
const envKey = this.getEnvKey(entry.key);
|
|
1089
|
+
const value = process.env[envKey];
|
|
1090
|
+
if (value !== undefined) {
|
|
1091
|
+
this.lastValues.set(envKey, value);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
checkForChanges() {
|
|
1096
|
+
const currentEntries = this.list();
|
|
1097
|
+
const currentValues = new Map;
|
|
1098
|
+
for (const entry of currentEntries) {
|
|
1099
|
+
const envKey = this.getEnvKey(entry.key);
|
|
1100
|
+
const value = process.env[envKey];
|
|
1101
|
+
if (value !== undefined) {
|
|
1102
|
+
currentValues.set(envKey, value);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
for (const [envKey, oldValue] of this.lastValues.entries()) {
|
|
1106
|
+
if (!currentValues.has(envKey)) {
|
|
1107
|
+
const key = this.getInternalKey(envKey);
|
|
1108
|
+
this.notifyWatchers({
|
|
1109
|
+
type: "delete",
|
|
1110
|
+
key,
|
|
1111
|
+
oldValue: this.transformValue(oldValue, key),
|
|
1112
|
+
newValue: undefined,
|
|
1113
|
+
source: this.name,
|
|
1114
|
+
timestamp: Date.now()
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
for (const [envKey, newValue] of currentValues.entries()) {
|
|
1119
|
+
const key = this.getInternalKey(envKey);
|
|
1120
|
+
const oldValue = this.lastValues.get(envKey);
|
|
1121
|
+
if (oldValue === undefined) {
|
|
1122
|
+
this.notifyWatchers({
|
|
1123
|
+
type: "add",
|
|
1124
|
+
key,
|
|
1125
|
+
oldValue: undefined,
|
|
1126
|
+
newValue: this.transformValue(newValue, key),
|
|
1127
|
+
source: this.name,
|
|
1128
|
+
timestamp: Date.now()
|
|
1129
|
+
});
|
|
1130
|
+
} else if (oldValue !== newValue) {
|
|
1131
|
+
this.notifyWatchers({
|
|
1132
|
+
type: "change",
|
|
1133
|
+
key,
|
|
1134
|
+
oldValue: this.transformValue(oldValue, key),
|
|
1135
|
+
newValue: this.transformValue(newValue, key),
|
|
1136
|
+
source: this.name,
|
|
1137
|
+
timestamp: Date.now()
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
this.lastValues = currentValues;
|
|
1142
|
+
}
|
|
1143
|
+
transformValue(value, key) {
|
|
1144
|
+
if (this.transform) {
|
|
1145
|
+
return this.transform(value, key);
|
|
1146
|
+
}
|
|
1147
|
+
return value;
|
|
1148
|
+
}
|
|
1149
|
+
notifyWatchers(event) {
|
|
1150
|
+
this.watchers.forEach((cb) => cb(event));
|
|
1151
|
+
}
|
|
1152
|
+
close() {
|
|
1153
|
+
this.stopPolling();
|
|
1154
|
+
this.watchers.clear();
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// packages/core/src/config/config-component.ts
|
|
1159
|
+
import * as os2 from "os";
|
|
1160
|
+
import * as path3 from "path";
|
|
1161
|
+
|
|
1162
|
+
class ConfigSourceNotFoundError extends Error {
|
|
1163
|
+
constructor(sourceType, path4) {
|
|
1164
|
+
const message = path4 ? `Config source not found: ${sourceType} at ${path4}` : `Config source not found: ${sourceType}`;
|
|
1165
|
+
super(message);
|
|
1166
|
+
this.name = "ConfigSourceNotFoundError";
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
class ConfigFileNotFoundError extends Error {
|
|
1171
|
+
constructor(filePath) {
|
|
1172
|
+
super(`Config file not found: ${filePath}`);
|
|
1173
|
+
this.name = "ConfigFileNotFoundError";
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
class MemorySource {
|
|
1178
|
+
name = "memory";
|
|
1179
|
+
priority = 0;
|
|
1180
|
+
store = new Map;
|
|
1181
|
+
watchers = new Set;
|
|
1182
|
+
read(key) {
|
|
1183
|
+
return this.store.get(key)?.value;
|
|
1184
|
+
}
|
|
1185
|
+
write(key, value) {
|
|
1186
|
+
const oldEntry = this.store.get(key);
|
|
1187
|
+
const entry = {
|
|
1188
|
+
value,
|
|
1189
|
+
timestamp: Date.now(),
|
|
1190
|
+
version: (oldEntry?.version ?? 0) + 1
|
|
1191
|
+
};
|
|
1192
|
+
this.store.set(key, entry);
|
|
1193
|
+
this.notifyWatchers({
|
|
1194
|
+
type: oldEntry ? "change" : "add",
|
|
1195
|
+
key,
|
|
1196
|
+
oldValue: oldEntry?.value,
|
|
1197
|
+
newValue: value,
|
|
1198
|
+
source: "memory",
|
|
1199
|
+
timestamp: entry.timestamp
|
|
1200
|
+
});
|
|
1201
|
+
return true;
|
|
1202
|
+
}
|
|
1203
|
+
delete(key) {
|
|
1204
|
+
const entry = this.store.get(key);
|
|
1205
|
+
if (!entry)
|
|
1206
|
+
return false;
|
|
1207
|
+
this.store.delete(key);
|
|
1208
|
+
this.notifyWatchers({
|
|
1209
|
+
type: "delete",
|
|
1210
|
+
key,
|
|
1211
|
+
oldValue: entry.value,
|
|
1212
|
+
newValue: undefined,
|
|
1213
|
+
source: "memory",
|
|
1214
|
+
timestamp: Date.now()
|
|
1215
|
+
});
|
|
1216
|
+
return true;
|
|
1217
|
+
}
|
|
1218
|
+
list() {
|
|
1219
|
+
return Array.from(this.store.entries()).map(([key, entry]) => ({
|
|
1220
|
+
key,
|
|
1221
|
+
value: entry.value
|
|
1222
|
+
}));
|
|
1223
|
+
}
|
|
1224
|
+
watch(callback) {
|
|
1225
|
+
this.watchers.add(callback);
|
|
1226
|
+
return () => this.watchers.delete(callback);
|
|
1227
|
+
}
|
|
1228
|
+
notifyWatchers(event) {
|
|
1229
|
+
this.watchers.forEach((cb) => cb(event));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
class AsyncPersistQueue {
|
|
1234
|
+
options;
|
|
1235
|
+
queue = new Map;
|
|
1236
|
+
timer;
|
|
1237
|
+
paused = false;
|
|
1238
|
+
constructor(options) {
|
|
1239
|
+
this.options = options;
|
|
1240
|
+
}
|
|
1241
|
+
add(key, value) {
|
|
1242
|
+
this.queue.set(key, { key, value, timestamp: Date.now() });
|
|
1243
|
+
this.scheduleFlush();
|
|
1244
|
+
}
|
|
1245
|
+
scheduleFlush() {
|
|
1246
|
+
if (this.paused || this.timer)
|
|
1247
|
+
return;
|
|
1248
|
+
this.timer = setTimeout(async () => {
|
|
1249
|
+
this.timer = undefined;
|
|
1250
|
+
await this.flush();
|
|
1251
|
+
}, this.options.debounceMs);
|
|
1252
|
+
}
|
|
1253
|
+
async flush() {
|
|
1254
|
+
if (this.queue.size === 0)
|
|
1255
|
+
return;
|
|
1256
|
+
const entries = Array.from(this.queue.values());
|
|
1257
|
+
this.queue.clear();
|
|
1258
|
+
try {
|
|
1259
|
+
await this.options.persistFn(entries);
|
|
1260
|
+
} catch (error) {
|
|
1261
|
+
console.error("Failed to persist config:", error);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
clear() {
|
|
1265
|
+
this.queue.clear();
|
|
1266
|
+
if (this.timer) {
|
|
1267
|
+
clearTimeout(this.timer);
|
|
1268
|
+
this.timer = undefined;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
size() {
|
|
1272
|
+
return this.queue.size;
|
|
1273
|
+
}
|
|
1274
|
+
pause() {
|
|
1275
|
+
this.paused = true;
|
|
1276
|
+
}
|
|
1277
|
+
resume() {
|
|
1278
|
+
this.paused = false;
|
|
1279
|
+
if (this.queue.size > 0) {
|
|
1280
|
+
this.scheduleFlush();
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
class ConfigComponent extends BaseComponent {
|
|
1286
|
+
name = "config";
|
|
1287
|
+
version = "1.0.0";
|
|
1288
|
+
memorySource = new MemorySource;
|
|
1289
|
+
sources = [this.memorySource];
|
|
1290
|
+
componentSchemas = new Map;
|
|
1291
|
+
componentPaths = new Map;
|
|
1292
|
+
watchers = new Map;
|
|
1293
|
+
sourceUnwatchFns = new Map;
|
|
1294
|
+
persistQueue;
|
|
1295
|
+
persistFile;
|
|
1296
|
+
keyRegistry = new Map;
|
|
1297
|
+
componentRegistrations = new Map;
|
|
1298
|
+
protocolResolver;
|
|
1299
|
+
__xdgDataHome;
|
|
1300
|
+
pendingSources = [];
|
|
1301
|
+
sourceKeys = new Map;
|
|
1302
|
+
getXdgDataHome() {
|
|
1303
|
+
return this.__xdgDataHome ?? process.env.XDG_DATA_HOME ?? path3.join(os2.homedir(), ".local", "share");
|
|
1304
|
+
}
|
|
1305
|
+
setXdgDataHome(path4) {
|
|
1306
|
+
this.__xdgDataHome = path4;
|
|
1307
|
+
if (this.protocolResolver) {
|
|
1308
|
+
this.protocolResolver.xdgDataHome = path4;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
constructor(options = {}) {
|
|
1312
|
+
super();
|
|
1313
|
+
this.persistFile = options.persistFile;
|
|
1314
|
+
this.persistQueue = new AsyncPersistQueue({
|
|
1315
|
+
debounceMs: options.debounceMs ?? 300,
|
|
1316
|
+
persistFn: this.persistToFile.bind(this)
|
|
1317
|
+
});
|
|
1318
|
+
this.protocolResolver = new ProtocolResolver;
|
|
1319
|
+
}
|
|
1320
|
+
register(schema) {
|
|
1321
|
+
this.componentSchemas.set(schema.name, schema);
|
|
1322
|
+
if (schema.paths) {
|
|
1323
|
+
this.componentPaths.set(schema.name, schema.paths);
|
|
1324
|
+
}
|
|
1325
|
+
for (const [key, value] of Object.entries(schema.defaults)) {
|
|
1326
|
+
const fullKey = `${schema.name}.${key}`;
|
|
1327
|
+
if (this.memorySource.read(fullKey) === undefined) {
|
|
1328
|
+
this.memorySource.write(fullKey, value);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
unregister(name) {
|
|
1333
|
+
this.componentSchemas.delete(name);
|
|
1334
|
+
this.componentPaths.delete(name);
|
|
1335
|
+
return true;
|
|
1336
|
+
}
|
|
1337
|
+
isRegistered(name) {
|
|
1338
|
+
return this.componentSchemas.has(name);
|
|
1339
|
+
}
|
|
1340
|
+
listComponents() {
|
|
1341
|
+
return Array.from(this.componentSchemas.keys());
|
|
1342
|
+
}
|
|
1343
|
+
get(key) {
|
|
1344
|
+
const memoryValue = this.memorySource.read(key);
|
|
1345
|
+
if (memoryValue !== undefined) {
|
|
1346
|
+
return memoryValue;
|
|
1347
|
+
}
|
|
1348
|
+
if (key.includes(".")) {
|
|
1349
|
+
const parts = key.split(".");
|
|
1350
|
+
for (let i = parts.length - 1;i >= 1; i--) {
|
|
1351
|
+
const componentParts = parts.slice(0, i);
|
|
1352
|
+
const component = componentParts.join(".");
|
|
1353
|
+
const restParts = parts.slice(i);
|
|
1354
|
+
const restKey = restParts.join(".");
|
|
1355
|
+
const componentValue = this.memorySource.read(component);
|
|
1356
|
+
if (typeof componentValue === "object" && componentValue !== null) {
|
|
1357
|
+
const nestedValue = this.getNestedValue(componentValue, restKey);
|
|
1358
|
+
if (nestedValue !== undefined) {
|
|
1359
|
+
return nestedValue;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
getMany(keys) {
|
|
1367
|
+
const result = {};
|
|
1368
|
+
for (const key of keys) {
|
|
1369
|
+
result[key] = this.get(key);
|
|
1370
|
+
}
|
|
1371
|
+
return result;
|
|
1372
|
+
}
|
|
1373
|
+
async set(key, value) {
|
|
1374
|
+
const oldValue = this.get(key);
|
|
1375
|
+
this.memorySource.write(key, value);
|
|
1376
|
+
const event = {
|
|
1377
|
+
component: key.split(".")[0],
|
|
1378
|
+
key,
|
|
1379
|
+
oldValue,
|
|
1380
|
+
newValue: value,
|
|
1381
|
+
persisted: false,
|
|
1382
|
+
timestamp: Date.now()
|
|
1383
|
+
};
|
|
1384
|
+
this.notifyWatchers(key, event);
|
|
1385
|
+
this.persistQueue.add(key, value);
|
|
1386
|
+
}
|
|
1387
|
+
async setMany(config) {
|
|
1388
|
+
for (const [key, value] of Object.entries(config)) {
|
|
1389
|
+
await this.set(key, value);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
getPath(component, subPath) {
|
|
1393
|
+
const paths = this.componentPaths.get(component);
|
|
1394
|
+
if (!paths) {
|
|
1395
|
+
throw new Error(`Component not registered: ${component}`);
|
|
1396
|
+
}
|
|
1397
|
+
if (subPath && paths[subPath]) {
|
|
1398
|
+
const { base: base2, subPath: relativePath2 } = paths[subPath];
|
|
1399
|
+
return XDG_PATHS.getComponentPath(base2, relativePath2);
|
|
1400
|
+
}
|
|
1401
|
+
const firstKey = Object.keys(paths)[0];
|
|
1402
|
+
if (!firstKey) {
|
|
1403
|
+
throw new Error(`No paths registered for component: ${component}`);
|
|
1404
|
+
}
|
|
1405
|
+
const { base, subPath: relativePath } = paths[firstKey];
|
|
1406
|
+
return XDG_PATHS.getComponentPath(base, relativePath);
|
|
1407
|
+
}
|
|
1408
|
+
async save(key) {
|
|
1409
|
+
await this.persistQueue.flush();
|
|
1410
|
+
}
|
|
1411
|
+
async reset(key) {
|
|
1412
|
+
const [component, ...rest] = key.split(".");
|
|
1413
|
+
const schema = this.componentSchemas.get(component);
|
|
1414
|
+
if (!schema)
|
|
1415
|
+
return;
|
|
1416
|
+
const partialKey = rest.join(".");
|
|
1417
|
+
const defaultValue = schema.defaults[partialKey] ?? schema.defaults[key];
|
|
1418
|
+
if (defaultValue !== undefined) {
|
|
1419
|
+
await this.set(key, defaultValue);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
watch(pattern, callback) {
|
|
1423
|
+
if (!this.watchers.has(pattern)) {
|
|
1424
|
+
this.watchers.set(pattern, new Set);
|
|
1425
|
+
}
|
|
1426
|
+
this.watchers.get(pattern).add(callback);
|
|
1427
|
+
return () => {
|
|
1428
|
+
this.watchers.get(pattern)?.delete(callback);
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
addSource(source) {
|
|
1432
|
+
this.sources.push(source);
|
|
1433
|
+
this.sources.sort((a, b) => a.priority - b.priority);
|
|
1434
|
+
if (source.watch) {
|
|
1435
|
+
const callback = (event) => {
|
|
1436
|
+
this.notifyWatchers(event.key, {
|
|
1437
|
+
component: event.key.split(".")[0],
|
|
1438
|
+
key: event.key,
|
|
1439
|
+
oldValue: event.oldValue,
|
|
1440
|
+
newValue: event.newValue,
|
|
1441
|
+
persisted: true,
|
|
1442
|
+
timestamp: event.timestamp
|
|
1443
|
+
});
|
|
1444
|
+
};
|
|
1445
|
+
try {
|
|
1446
|
+
const unwatch = source.watch(callback);
|
|
1447
|
+
const sourceKey = this.getSourceKey(source);
|
|
1448
|
+
this.sourceUnwatchFns.set(sourceKey, unwatch);
|
|
1449
|
+
} catch {}
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
getSourceKey(source) {
|
|
1453
|
+
if ("filePath" in source) {
|
|
1454
|
+
return `file:${source.filePath}`;
|
|
1455
|
+
}
|
|
1456
|
+
if ("prefix" in source) {
|
|
1457
|
+
return `env:${source.prefix}`;
|
|
1458
|
+
}
|
|
1459
|
+
return source.name;
|
|
1460
|
+
}
|
|
1461
|
+
removeSource(name) {
|
|
1462
|
+
const index = this.sources.findIndex((s) => s.name === name);
|
|
1463
|
+
if (index > 0) {
|
|
1464
|
+
const source = this.sources[index];
|
|
1465
|
+
const sourceKey = this.getSourceKey(source);
|
|
1466
|
+
const unwatch = this.sourceUnwatchFns.get(sourceKey);
|
|
1467
|
+
if (unwatch) {
|
|
1468
|
+
unwatch();
|
|
1469
|
+
this.sourceUnwatchFns.delete(sourceKey);
|
|
1470
|
+
}
|
|
1471
|
+
this.sources.splice(index, 1);
|
|
1472
|
+
return true;
|
|
1473
|
+
}
|
|
1474
|
+
return false;
|
|
1475
|
+
}
|
|
1476
|
+
unwatchAll() {
|
|
1477
|
+
for (const unwatch of this.sourceUnwatchFns.values()) {
|
|
1478
|
+
unwatch();
|
|
1479
|
+
}
|
|
1480
|
+
this.sourceUnwatchFns.clear();
|
|
1481
|
+
}
|
|
1482
|
+
getSources() {
|
|
1483
|
+
return [...this.sources];
|
|
1484
|
+
}
|
|
1485
|
+
getNestedValue(obj, key) {
|
|
1486
|
+
const keys = key.split(".");
|
|
1487
|
+
let current = obj;
|
|
1488
|
+
for (const k of keys) {
|
|
1489
|
+
if (typeof current !== "object" || current === null)
|
|
1490
|
+
return;
|
|
1491
|
+
current = current[k];
|
|
1492
|
+
}
|
|
1493
|
+
return current;
|
|
1494
|
+
}
|
|
1495
|
+
notifyWatchers(key, event) {
|
|
1496
|
+
const exactWatchers = this.watchers.get(key);
|
|
1497
|
+
exactWatchers?.forEach((cb) => cb(event));
|
|
1498
|
+
const [component] = key.split(".");
|
|
1499
|
+
const wildcardWatchers = this.watchers.get(`${component}.*`);
|
|
1500
|
+
wildcardWatchers?.forEach((cb) => cb(event));
|
|
1501
|
+
const globalWatchers = this.watchers.get("*");
|
|
1502
|
+
globalWatchers?.forEach((cb) => cb(event));
|
|
1503
|
+
}
|
|
1504
|
+
async persistToFile(entries) {
|
|
1505
|
+
if (!this.persistFile)
|
|
1506
|
+
return;
|
|
1507
|
+
console.log(`[ConfigComponent] Persisting ${entries.length} entries to ${this.persistFile}`);
|
|
1508
|
+
}
|
|
1509
|
+
registerSource(registration) {
|
|
1510
|
+
const { type, relativePath, envPrefix } = registration;
|
|
1511
|
+
const sourceKey = type === "file" && relativePath ? `file:${relativePath}` : `${type}:${envPrefix ?? ""}`;
|
|
1512
|
+
if (this.sourceKeys.has(sourceKey)) {
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
this.pendingSources.push(registration);
|
|
1516
|
+
this.sourceKeys.set(sourceKey, true);
|
|
1517
|
+
}
|
|
1518
|
+
createSource(registration) {
|
|
1519
|
+
const { type, relativePath, envPrefix, priority, optional, watch: watch2 } = registration;
|
|
1520
|
+
switch (type) {
|
|
1521
|
+
case "memory": {
|
|
1522
|
+
return this.memorySource;
|
|
1523
|
+
}
|
|
1524
|
+
case "file": {
|
|
1525
|
+
if (!relativePath) {
|
|
1526
|
+
throw new Error("File source requires relativePath");
|
|
1527
|
+
}
|
|
1528
|
+
const isAbsolutePath = path3.isAbsolute(relativePath) || relativePath.startsWith("file://");
|
|
1529
|
+
let filePath;
|
|
1530
|
+
if (isAbsolutePath) {
|
|
1531
|
+
filePath = relativePath.startsWith("file://") ? relativePath.replace(/^file:\/\//, "") : relativePath;
|
|
1532
|
+
} else {
|
|
1533
|
+
if (this.protocolResolver) {
|
|
1534
|
+
this.protocolResolver.xdgDataHome = this.getXdgDataHome();
|
|
1535
|
+
}
|
|
1536
|
+
filePath = path3.join(this.getXdgDataHome(), relativePath);
|
|
1537
|
+
}
|
|
1538
|
+
return new FileSource({
|
|
1539
|
+
filePath,
|
|
1540
|
+
optional: optional ?? false,
|
|
1541
|
+
resolver: this.protocolResolver,
|
|
1542
|
+
watch: watch2
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
case "env": {
|
|
1546
|
+
return new EnvSource({
|
|
1547
|
+
prefix: envPrefix ?? "",
|
|
1548
|
+
priority: priority ?? 20,
|
|
1549
|
+
watch: watch2
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
default:
|
|
1553
|
+
throw new Error(`Unknown source type: ${type}`);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
ensureSourcesCreated() {
|
|
1557
|
+
for (const pending of this.pendingSources) {
|
|
1558
|
+
const source = this.createSource(pending);
|
|
1559
|
+
this.addSource(source);
|
|
1560
|
+
}
|
|
1561
|
+
this.pendingSources = [];
|
|
1562
|
+
}
|
|
1563
|
+
registerKeys(keys) {
|
|
1564
|
+
for (const keyReg of keys) {
|
|
1565
|
+
if (this.keyRegistry.has(keyReg.key)) {
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
this.keyRegistry.set(keyReg.key, keyReg);
|
|
1569
|
+
if (keyReg.default !== undefined) {
|
|
1570
|
+
if (this.memorySource.read(keyReg.key) === undefined) {
|
|
1571
|
+
this.memorySource.write(keyReg.key, keyReg.default);
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
registerComponent(registration) {
|
|
1577
|
+
const { name, sources, keys, defaults } = registration;
|
|
1578
|
+
this.componentRegistrations.set(name, registration);
|
|
1579
|
+
for (const sourceReg of sources) {
|
|
1580
|
+
this.registerSource(sourceReg);
|
|
1581
|
+
}
|
|
1582
|
+
this.registerKeys(keys);
|
|
1583
|
+
if (defaults) {
|
|
1584
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
1585
|
+
const fullKey = `${name}.${key}`;
|
|
1586
|
+
if (this.memorySource.read(fullKey) === undefined) {
|
|
1587
|
+
this.memorySource.write(fullKey, value);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
async load(componentName) {
|
|
1593
|
+
this.ensureSourcesCreated();
|
|
1594
|
+
let keysToLoad;
|
|
1595
|
+
if (componentName) {
|
|
1596
|
+
const registration = this.componentRegistrations.get(componentName);
|
|
1597
|
+
if (!registration) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
keysToLoad = registration.keys.map((k) => ({
|
|
1601
|
+
key: k.key,
|
|
1602
|
+
sources: k.sources
|
|
1603
|
+
}));
|
|
1604
|
+
} else {
|
|
1605
|
+
keysToLoad = Array.from(this.keyRegistry.values()).map((k) => ({
|
|
1606
|
+
key: k.key,
|
|
1607
|
+
sources: k.sources
|
|
1608
|
+
}));
|
|
1609
|
+
}
|
|
1610
|
+
const sourcesByType = new Map;
|
|
1611
|
+
for (const source of this.sources) {
|
|
1612
|
+
sourcesByType.set(source.name, source);
|
|
1613
|
+
}
|
|
1614
|
+
for (const { key, sources: sourceTypes } of keysToLoad) {
|
|
1615
|
+
if (this.memorySource.read(key) !== undefined) {
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
for (const sourceType of sourceTypes) {
|
|
1619
|
+
const source = sourcesByType.get(sourceType);
|
|
1620
|
+
if (!source)
|
|
1621
|
+
continue;
|
|
1622
|
+
const value = source.read(key);
|
|
1623
|
+
if (value !== undefined) {
|
|
1624
|
+
this.memorySource.write(key, value);
|
|
1625
|
+
break;
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
getRegisteredSources() {
|
|
1631
|
+
this.ensureSourcesCreated();
|
|
1632
|
+
return [...this.sources];
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
export {
|
|
1636
|
+
substituteProtocolRefs,
|
|
1637
|
+
substituteEnvVars,
|
|
1638
|
+
parseJSONCWithProtocols,
|
|
1639
|
+
parseJSONCWithEnv,
|
|
1640
|
+
parseJSONC,
|
|
1641
|
+
MemorySource,
|
|
1642
|
+
FileSource,
|
|
1643
|
+
EnvSource,
|
|
1644
|
+
ConfigSourceNotFoundError,
|
|
1645
|
+
ConfigFileNotFoundError,
|
|
1646
|
+
ConfigComponent
|
|
1647
|
+
};
|