@ai-setting/roy-agent-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +99145 -0
- package/package.json +114 -0
- package/src/config/config-component.test.ts +627 -0
- package/src/config/config-component.ts +906 -0
- package/src/config/config-parser.test.ts +319 -0
- package/src/config/config-parser.ts +203 -0
- package/src/config/decentralized-config.test.ts +740 -0
- package/src/config/env-key.ts +210 -0
- package/src/config/env-source.test.ts +252 -0
- package/src/config/env-source.ts +301 -0
- package/src/config/file-source.test.ts +357 -0
- package/src/config/file-source.ts +421 -0
- package/src/config/index.ts +24 -0
- package/src/config/protocol-resolver.test.ts +217 -0
- package/src/config/protocol-resolver.ts +228 -0
- package/src/env/agent/agent-component.abort.test.ts +511 -0
- package/src/env/agent/agent-component.record-session.test.ts +349 -0
- package/src/env/agent/agent-component.test.ts +1389 -0
- package/src/env/agent/agent-component.tool-error.test.ts +327 -0
- package/src/env/agent/agent-component.ts +1711 -0
- package/src/env/agent/agent-config-registration.test.ts +226 -0
- package/src/env/agent/agent-config-registration.ts +46 -0
- package/src/env/agent/agent-reminder-plugin.integration.test.ts +243 -0
- package/src/env/agent/index.ts +10 -0
- package/src/env/agent/summary-agent.parse-hint.test.ts +360 -0
- package/src/env/agent/summary-agent.ts +508 -0
- package/src/env/agent/types.ts +536 -0
- package/src/env/commands/commands-component.test.ts +364 -0
- package/src/env/commands/commands-component.ts +604 -0
- package/src/env/commands/commands-config-registration.test.ts +198 -0
- package/src/env/commands/commands-config-registration.ts +38 -0
- package/src/env/commands/index.ts +21 -0
- package/src/env/commands/parser.test.ts +203 -0
- package/src/env/commands/parser.ts +115 -0
- package/src/env/commands/types.ts +184 -0
- package/src/env/commands-prompt-integration.test.ts +243 -0
- package/src/env/component-env.test.ts +119 -0
- package/src/env/component.ts +335 -0
- package/src/env/constants.test.ts +72 -0
- package/src/env/constants.ts +123 -0
- package/src/env/debug/debug-component.test.ts +114 -0
- package/src/env/debug/debug-component.ts +547 -0
- package/src/env/debug/formatters/index.ts +9 -0
- package/src/env/debug/formatters/repl-formatter.test.ts +139 -0
- package/src/env/debug/formatters/repl-formatter.ts +358 -0
- package/src/env/debug/formatters/trace-formatter.test.ts +119 -0
- package/src/env/debug/formatters/trace-formatter.ts +191 -0
- package/src/env/debug/formatters/tree-formatter.test.ts +107 -0
- package/src/env/debug/formatters/tree-formatter.ts +325 -0
- package/src/env/debug/index.ts +38 -0
- package/src/env/debug/parser/regex-parser.test.ts +201 -0
- package/src/env/debug/parser/regex-parser.ts +196 -0
- package/src/env/debug/parser/span-builder.test.ts +241 -0
- package/src/env/debug/parser/span-builder.ts +386 -0
- package/src/env/debug/reader/log-reader.test.ts +170 -0
- package/src/env/debug/reader/log-reader.ts +186 -0
- package/src/env/debug/reader/span-db-reader.test.ts +118 -0
- package/src/env/debug/reader/span-db-reader.ts +201 -0
- package/src/env/debug/types.test.ts +187 -0
- package/src/env/debug/types.ts +171 -0
- package/src/env/environment-init.test.ts +183 -0
- package/src/env/environment-lifecycle.test.ts +516 -0
- package/src/env/environment-service.test.ts +332 -0
- package/src/env/environment.handle-query.test.ts +96 -0
- package/src/env/environment.test.ts +232 -0
- package/src/env/environment.ts +708 -0
- package/src/env/errors.test.ts +165 -0
- package/src/env/errors.ts +157 -0
- package/src/env/event-source/event-source-agent-handler.test.ts +193 -0
- package/src/env/event-source/event-source-agent-handler.ts +111 -0
- package/src/env/event-source/event-source-component.process-cleanup.test.ts +236 -0
- package/src/env/event-source/event-source-component.stop.test.ts +346 -0
- package/src/env/event-source/event-source-component.test.ts +1207 -0
- package/src/env/event-source/event-source-component.ts +1379 -0
- package/src/env/event-source/event-source-config-registration.test.ts +242 -0
- package/src/env/event-source/event-source-config-registration.ts +37 -0
- package/src/env/event-source/event-source-integration.test.ts +320 -0
- package/src/env/event-source/event-source-platform.test.ts +630 -0
- package/src/env/event-source/types.ts +298 -0
- package/src/env/hook/global-hook-manager.ts +162 -0
- package/src/env/hook/hook-manager.test.ts +374 -0
- package/src/env/hook/hook-manager.ts +309 -0
- package/src/env/hook/index.ts +38 -0
- package/src/env/hook/types.ts +138 -0
- package/src/env/index.ts +144 -0
- package/src/env/interface.ts +203 -0
- package/src/env/llm/hooks.test.ts +293 -0
- package/src/env/llm/hooks.ts +316 -0
- package/src/env/llm/index.ts +61 -0
- package/src/env/llm/invoke-threshold-check.test.ts +88 -0
- package/src/env/llm/invoke-timeout.test.ts +54 -0
- package/src/env/llm/invoke.test.ts +71 -0
- package/src/env/llm/invoke.ts +1039 -0
- package/src/env/llm/llm-config.test.ts +523 -0
- package/src/env/llm/llm.test.ts +233 -0
- package/src/env/llm/llm.ts +568 -0
- package/src/env/llm/provider.test.ts +182 -0
- package/src/env/llm/provider.ts +108 -0
- package/src/env/llm/transform.test.ts +251 -0
- package/src/env/llm/transform.ts +286 -0
- package/src/env/llm/types.test.ts +580 -0
- package/src/env/llm/types.ts +424 -0
- package/src/env/log-trace/decorator-otel.test.ts +182 -0
- package/src/env/log-trace/decorator.ts +230 -0
- package/src/env/log-trace/index.ts +79 -0
- package/src/env/log-trace/log-trace-component.test.ts +242 -0
- package/src/env/log-trace/log-trace-component.ts +497 -0
- package/src/env/log-trace/log-trace-config-registration.test.ts +348 -0
- package/src/env/log-trace/log-trace-config-registration.ts +45 -0
- package/src/env/log-trace/logger.test.ts +149 -0
- package/src/env/log-trace/logger.ts +522 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.test.ts +147 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.ts +194 -0
- package/src/env/log-trace/opentelemetry/integration.test.ts +668 -0
- package/src/env/log-trace/opentelemetry/mod.ts +25 -0
- package/src/env/log-trace/opentelemetry/propagation-env.test.ts +181 -0
- package/src/env/log-trace/opentelemetry/propagation-env.ts +136 -0
- package/src/env/log-trace/opentelemetry/propagation.test.ts +259 -0
- package/src/env/log-trace/opentelemetry/propagation.ts +215 -0
- package/src/env/log-trace/opentelemetry/tracer-provider-context.test.ts +166 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.test.ts +379 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.ts +612 -0
- package/src/env/log-trace/span-storage.test.ts +145 -0
- package/src/env/log-trace/span-storage.ts +230 -0
- package/src/env/log-trace/trace-context.test.ts +187 -0
- package/src/env/log-trace/trace-context.ts +162 -0
- package/src/env/log-trace/types.test.ts +63 -0
- package/src/env/log-trace/types.ts +172 -0
- package/src/env/mcp/README.md +244 -0
- package/src/env/mcp/__integration__/mcp-component.integration.test.ts +373 -0
- package/src/env/mcp/config.test.ts +74 -0
- package/src/env/mcp/config.ts +116 -0
- package/src/env/mcp/index.ts +41 -0
- package/src/env/mcp/loader.test.ts +161 -0
- package/src/env/mcp/loader.ts +209 -0
- package/src/env/mcp/mcp-component.test.ts +111 -0
- package/src/env/mcp/mcp-component.ts +358 -0
- package/src/env/mcp/mcp-config-registration.test.ts +304 -0
- package/src/env/mcp/mcp-config-registration.ts +50 -0
- package/src/env/mcp/scanner.test.ts +170 -0
- package/src/env/mcp/scanner.ts +246 -0
- package/src/env/mcp/tool/adapter.test.ts +520 -0
- package/src/env/mcp/tool/adapter.ts +521 -0
- package/src/env/mcp/tool/index.ts +5 -0
- package/src/env/mcp/types.test.ts +171 -0
- package/src/env/mcp/types.ts +79 -0
- package/src/env/memory/README.md +177 -0
- package/src/env/memory/built-in/index.ts +59 -0
- package/src/env/memory/built-in/recall-memory.ts +103 -0
- package/src/env/memory/built-in/record-memory.ts +148 -0
- package/src/env/memory/index.ts +20 -0
- package/src/env/memory/memory-component.test.ts +239 -0
- package/src/env/memory/memory-component.ts +503 -0
- package/src/env/memory/memory-config-registration.test.ts +67 -0
- package/src/env/memory/memory-config-registration.ts +48 -0
- package/src/env/memory/memory-config.ts +45 -0
- package/src/env/memory/memory-file.test.ts +268 -0
- package/src/env/memory/plugin/index.ts +48 -0
- package/src/env/memory/plugin/memory-agent.test.ts +249 -0
- package/src/env/memory/plugin/memory-agent.ts +365 -0
- package/src/env/memory/plugin/memory-manager.ts +198 -0
- package/src/env/memory/plugin/memory-plugin-agent.test.ts +145 -0
- package/src/env/memory/plugin/memory-plugin.ts +210 -0
- package/src/env/memory/plugin/plugin-simplified.test.ts +51 -0
- package/src/env/memory/plugin/recall-memory.test.ts +106 -0
- package/src/env/memory/plugin/recall-memory.ts +53 -0
- package/src/env/memory/plugin/types.ts +101 -0
- package/src/env/memory/tools/memory-agent-tools.ts +228 -0
- package/src/env/memory/types.ts +85 -0
- package/src/env/paths.ts +118 -0
- package/src/env/prompt/index.ts +18 -0
- package/src/env/prompt/memory-prompts.test.ts +91 -0
- package/src/env/prompt/prompt-component.test.ts +491 -0
- package/src/env/prompt/prompt-component.ts +619 -0
- package/src/env/prompt/prompt-config-registration.test.ts +213 -0
- package/src/env/prompt/prompt-config-registration.ts +39 -0
- package/src/env/prompt/prompts-index.ts +504 -0
- package/src/env/prompt/renderer.ts +67 -0
- package/src/env/prompt/types.ts +136 -0
- package/src/env/session/hooks.ts +18 -0
- package/src/env/session/index.ts +37 -0
- package/src/env/session/search-query-parser.test.ts +425 -0
- package/src/env/session/search-query-parser.ts +171 -0
- package/src/env/session/session-checkpoint.test.ts +523 -0
- package/src/env/session/session-component.extract-recent-messages.test.ts +209 -0
- package/src/env/session/session-component.test.ts +132 -0
- package/src/env/session/session-component.ts +1249 -0
- package/src/env/session/session-config-registration.test.ts +138 -0
- package/src/env/session/session-config-registration.ts +52 -0
- package/src/env/session/session-message-converter.test.ts +763 -0
- package/src/env/session/session-message-converter.ts +415 -0
- package/src/env/session/session-message-e2e.test.ts +448 -0
- package/src/env/session/session-search.test.ts +391 -0
- package/src/env/session/session-store.test.ts +362 -0
- package/src/env/session/session-store.ts +141 -0
- package/src/env/session/storage/index.ts +6 -0
- package/src/env/session/storage/memory.ts +502 -0
- package/src/env/session/storage/sqlite.ts +794 -0
- package/src/env/session/types.ts +742 -0
- package/src/env/skill/config.ts +39 -0
- package/src/env/skill/index.ts +6 -0
- package/src/env/skill/parser.test.ts +116 -0
- package/src/env/skill/parser.ts +77 -0
- package/src/env/skill/scanner.test.ts +211 -0
- package/src/env/skill/scanner.ts +119 -0
- package/src/env/skill/skill-component.test.ts +234 -0
- package/src/env/skill/skill-component.ts +352 -0
- package/src/env/skill/skill-config-registration.test.ts +60 -0
- package/src/env/skill/skill-config-registration.ts +43 -0
- package/src/env/skill/tool/index.ts +1 -0
- package/src/env/skill/tool/skill-tool.test.ts +100 -0
- package/src/env/skill/tool/skill-tool.ts +72 -0
- package/src/env/skill/types.ts +64 -0
- package/src/env/task/delegate/delegate-tool.test.ts +498 -0
- package/src/env/task/delegate/delegate-tool.ts +1014 -0
- package/src/env/task/delegate/index.ts +18 -0
- package/src/env/task/delegate/stop-tool.test.ts +140 -0
- package/src/env/task/delegate/stop-tool.ts +119 -0
- package/src/env/task/delegate/task-events.test.ts +178 -0
- package/src/env/task/delegate/task-events.ts +143 -0
- package/src/env/task/hooks/contexts.test.ts +92 -0
- package/src/env/task/hooks/contexts.ts +192 -0
- package/src/env/task/hooks/index.ts +23 -0
- package/src/env/task/hooks/task-hook-points.test.ts +32 -0
- package/src/env/task/hooks/task-hook-points.ts +54 -0
- package/src/env/task/index.ts +7 -0
- package/src/env/task/plugins/index.ts +13 -0
- package/src/env/task/plugins/task-plugin.test.ts +74 -0
- package/src/env/task/plugins/task-plugin.ts +89 -0
- package/src/env/task/plugins/task-tag-plugin.test.ts +377 -0
- package/src/env/task/plugins/task-tag-plugin.ts +319 -0
- package/src/env/task/plugins/task-workflow-extractor.integration.test.ts +226 -0
- package/src/env/task/plugins/workflow-extractor-agent.test.ts +107 -0
- package/src/env/task/plugins/workflow-extractor-agent.ts +225 -0
- package/src/env/task/storage/index.ts +6 -0
- package/src/env/task/storage/sqlite-task-store.test.ts +283 -0
- package/src/env/task/storage/sqlite-task-store.ts +903 -0
- package/src/env/task/storage/task-search.test.ts +291 -0
- package/src/env/task/tag-service.test.ts +198 -0
- package/src/env/task/tag-service.ts +264 -0
- package/src/env/task/task-component.test.ts +193 -0
- package/src/env/task/task-component.ts +658 -0
- package/src/env/task/task-config-registration.test.ts +57 -0
- package/src/env/task/task-config-registration.ts +37 -0
- package/src/env/task/task-types.test.ts +137 -0
- package/src/env/task/tools/complete-tool.ts +44 -0
- package/src/env/task/tools/create-tool.ts +49 -0
- package/src/env/task/tools/delete-tool.ts +43 -0
- package/src/env/task/tools/get-tool.ts +59 -0
- package/src/env/task/tools/index.ts +10 -0
- package/src/env/task/tools/list-tool.ts +40 -0
- package/src/env/task/tools/operation/create-tool.ts +48 -0
- package/src/env/task/tools/operation/delete-tool.ts +43 -0
- package/src/env/task/tools/operation/get-tool.ts +43 -0
- package/src/env/task/tools/operation/index.ts +9 -0
- package/src/env/task/tools/operation/list-tool.ts +40 -0
- package/src/env/task/tools/operation/operation-tools.test.ts +274 -0
- package/src/env/task/tools/operation/operation-types.ts +75 -0
- package/src/env/task/tools/operation/update-tool.ts +47 -0
- package/src/env/task/tools/task-tools.test.ts +203 -0
- package/src/env/task/tools/task-types.test.ts +75 -0
- package/src/env/task/tools/task-types.ts +68 -0
- package/src/env/task/tools/update-tool.ts +70 -0
- package/src/env/task/types.ts +160 -0
- package/src/env/tool/built-in/bash.ts +201 -0
- package/src/env/tool/built-in/echo.ts +29 -0
- package/src/env/tool/built-in/edit-file.test.ts +136 -0
- package/src/env/tool/built-in/edit-file.ts +92 -0
- package/src/env/tool/built-in/glob.test.ts +94 -0
- package/src/env/tool/built-in/glob.ts +65 -0
- package/src/env/tool/built-in/grep.test.ts +122 -0
- package/src/env/tool/built-in/grep.ts +108 -0
- package/src/env/tool/built-in/index.ts +44 -0
- package/src/env/tool/built-in/read-file.test.ts +84 -0
- package/src/env/tool/built-in/read-file.ts +75 -0
- package/src/env/tool/built-in/write-file.test.ts +119 -0
- package/src/env/tool/built-in/write-file.ts +68 -0
- package/src/env/tool/index.ts +24 -0
- package/src/env/tool/registry.test.ts +257 -0
- package/src/env/tool/registry.ts +167 -0
- package/src/env/tool/tool-component.test.ts +559 -0
- package/src/env/tool/tool-component.ts +563 -0
- package/src/env/tool/tool-config-registration.test.ts +249 -0
- package/src/env/tool/tool-config-registration.ts +46 -0
- package/src/env/tool/types.ts +267 -0
- package/src/env/tool/validator.test.ts +143 -0
- package/src/env/tool/validator.ts +44 -0
- package/src/env/types.ts +180 -0
- package/src/env/workflow/ask-user-tool-registration.test.ts +216 -0
- package/src/env/workflow/complex-workflow.integration.test.ts +1900 -0
- package/src/env/workflow/decorators/decorator-node.ts +229 -0
- package/src/env/workflow/decorators/decorator.test.ts +196 -0
- package/src/env/workflow/decorators/edge.ts +82 -0
- package/src/env/workflow/decorators/index.ts +31 -0
- package/src/env/workflow/decorators/node-as.ts +98 -0
- package/src/env/workflow/decorators/workflow.ts +54 -0
- package/src/env/workflow/engine/dag-manager.test.ts +570 -0
- package/src/env/workflow/engine/dag-manager.ts +594 -0
- package/src/env/workflow/engine/engine.ts +1422 -0
- package/src/env/workflow/engine/event-bus.test.ts +359 -0
- package/src/env/workflow/engine/event-bus.ts +156 -0
- package/src/env/workflow/engine/executor-agent-session.test.ts +84 -0
- package/src/env/workflow/engine/executor.test.ts +619 -0
- package/src/env/workflow/engine/executor.ts +593 -0
- package/src/env/workflow/engine/index.ts +24 -0
- package/src/env/workflow/engine/node-registry.test.ts +560 -0
- package/src/env/workflow/engine/node-registry.ts +289 -0
- package/src/env/workflow/engine/resume-removed.test.ts +22 -0
- package/src/env/workflow/engine/scheduler.test.ts +715 -0
- package/src/env/workflow/engine/scheduler.ts +318 -0
- package/src/env/workflow/engine/workflow-engine.test.ts +815 -0
- package/src/env/workflow/extractor/workflow-converter.ts +306 -0
- package/src/env/workflow/fixtures.ts +380 -0
- package/src/env/workflow/index.ts +38 -0
- package/src/env/workflow/integration/run-resume-unified.test.ts +186 -0
- package/src/env/workflow/integration/service-integration.test.ts +267 -0
- package/src/env/workflow/metadata/keys.ts +12 -0
- package/src/env/workflow/nodes/agent-component-adapter.test.ts +318 -0
- package/src/env/workflow/nodes/agent-component-adapter.ts +448 -0
- package/src/env/workflow/nodes/agent-node.test.ts +371 -0
- package/src/env/workflow/nodes/agent-node.ts +598 -0
- package/src/env/workflow/nodes/ask-user-node.ts +113 -0
- package/src/env/workflow/nodes/condition-node.ts +200 -0
- package/src/env/workflow/nodes/index.ts +9 -0
- package/src/env/workflow/nodes/merge-node.ts +141 -0
- package/src/env/workflow/nodes/skill-node.test.ts +253 -0
- package/src/env/workflow/nodes/skill-node.ts +393 -0
- package/src/env/workflow/nodes/tool-node.test.ts +251 -0
- package/src/env/workflow/nodes/tool-node.ts +493 -0
- package/src/env/workflow/nodes/workflow-llm-history.test.ts +455 -0
- package/src/env/workflow/nodes/workflow-node.test.ts +315 -0
- package/src/env/workflow/nodes/workflow-node.ts +311 -0
- package/src/env/workflow/service/index.ts +27 -0
- package/src/env/workflow/service/registry.test.ts +133 -0
- package/src/env/workflow/service/registry.ts +71 -0
- package/src/env/workflow/service/workflow-service.test.ts +310 -0
- package/src/env/workflow/service/workflow-service.ts +393 -0
- package/src/env/workflow/storage/index.ts +28 -0
- package/src/env/workflow/storage/mock-repositories.ts +385 -0
- package/src/env/workflow/storage/sqlite.test.ts +179 -0
- package/src/env/workflow/storage/sqlite.ts +163 -0
- package/src/env/workflow/storage/workflow-repo.test.ts +780 -0
- package/src/env/workflow/storage/workflow-repo.ts +342 -0
- package/src/env/workflow/tools/ask-user-tool.ts +82 -0
- package/src/env/workflow/tools/index.ts +26 -0
- package/src/env/workflow/tools/run-workflow.test.ts +352 -0
- package/src/env/workflow/tools/run-workflow.ts +214 -0
- package/src/env/workflow/types/context.ts +18 -0
- package/src/env/workflow/types/decorators-types.ts +198 -0
- package/src/env/workflow/types/event.test.ts +515 -0
- package/src/env/workflow/types/event.ts +193 -0
- package/src/env/workflow/types/index.ts +49 -0
- package/src/env/workflow/types/run.test.ts +437 -0
- package/src/env/workflow/types/run.ts +173 -0
- package/src/env/workflow/types/workflow-hil.ts +114 -0
- package/src/env/workflow/types/workflow-message.test.ts +138 -0
- package/src/env/workflow/types/workflow-message.ts +196 -0
- package/src/env/workflow/types/workflow-session.test.ts +95 -0
- package/src/env/workflow/types/workflow-session.ts +59 -0
- package/src/env/workflow/types/workflow.test.ts +495 -0
- package/src/env/workflow/types/workflow.ts +195 -0
- package/src/env/workflow/types_compat.ts +51 -0
- package/src/env/workflow/utils/create-workflow.ts +47 -0
- package/src/env/workflow/utils/execution-state.ts +245 -0
- package/src/env/workflow/utils/index.ts +18 -0
- package/src/env/workflow/utils/node-registry-helper.ts +58 -0
- package/src/env/workflow/utils/recovery-validator.test.ts +460 -0
- package/src/env/workflow/utils/recovery-validator.ts +377 -0
- package/src/env/workflow/utils/session-parser.test.ts +111 -0
- package/src/env/workflow/utils/session-parser.ts +94 -0
- package/src/env/workflow/utils/session-recovery.test.ts +334 -0
- package/src/env/workflow/utils/session-recovery.ts +188 -0
- package/src/env/workflow/utils/template-resolver.test.ts +258 -0
- package/src/env/workflow/utils/template-resolver.ts +436 -0
- package/src/env/workflow/utils/validation-rules.ts +149 -0
- package/src/env/workflow/workflow-component.ts +544 -0
- package/src/index.ts +422 -0
- package/src/utils/id.ts +21 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview REPL Formatter for Interactive Debugging
|
|
3
|
+
*
|
|
4
|
+
* Provides an interactive REPL interface for debugging with:
|
|
5
|
+
* - Command parsing
|
|
6
|
+
* - Output formatting
|
|
7
|
+
* - Help text generation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { TraceEntry, TraceTreeNode, TraceIdInfo } from '../types';
|
|
11
|
+
|
|
12
|
+
export interface ReplContext {
|
|
13
|
+
traceId?: string;
|
|
14
|
+
function?: string;
|
|
15
|
+
limit?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ParsedCommand {
|
|
19
|
+
command: string;
|
|
20
|
+
args: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Formats output for REPL interface and provides command parsing
|
|
25
|
+
*/
|
|
26
|
+
export class ReplFormatter {
|
|
27
|
+
private context: ReplContext = {};
|
|
28
|
+
private history: string[] = [];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get welcome message for REPL
|
|
32
|
+
*/
|
|
33
|
+
getWelcomeMessage(): string {
|
|
34
|
+
return `========================================
|
|
35
|
+
Debug REPL - Interactive Debugger
|
|
36
|
+
========================================
|
|
37
|
+
|
|
38
|
+
Commands:
|
|
39
|
+
list [limit] - List trace IDs
|
|
40
|
+
trace [options] - Show trace details
|
|
41
|
+
tree [options] - Show call tree
|
|
42
|
+
help - Show this help
|
|
43
|
+
exit - Exit REPL
|
|
44
|
+
|
|
45
|
+
Examples:
|
|
46
|
+
list 10
|
|
47
|
+
trace --func llm.invoke
|
|
48
|
+
tree --trace trace-001
|
|
49
|
+
|
|
50
|
+
========================================
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Format prompt with current context
|
|
56
|
+
*/
|
|
57
|
+
formatPrompt(context?: Partial<ReplContext>): string {
|
|
58
|
+
const ctx = { ...this.context, ...context };
|
|
59
|
+
let prompt = 'debug';
|
|
60
|
+
|
|
61
|
+
if (ctx.traceId) {
|
|
62
|
+
prompt += `:${ctx.traceId}`;
|
|
63
|
+
} else if (ctx.function) {
|
|
64
|
+
prompt += `:${ctx.function}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return `${prompt}> `;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get help text
|
|
72
|
+
*/
|
|
73
|
+
getHelpText(): string {
|
|
74
|
+
return `
|
|
75
|
+
Available Commands:
|
|
76
|
+
==================
|
|
77
|
+
list [n] - List recent trace IDs (default: 10)
|
|
78
|
+
trace [options] - Show trace details
|
|
79
|
+
--func <name> - Filter by function name
|
|
80
|
+
--trace <id> - Show specific trace
|
|
81
|
+
--pretty - Pretty print output
|
|
82
|
+
--limit <n> - Limit results
|
|
83
|
+
tree [options] - Show call tree
|
|
84
|
+
--func <name> - Show tree for function
|
|
85
|
+
--trace <id> - Show tree for trace
|
|
86
|
+
clear - Clear screen
|
|
87
|
+
help - Show this help
|
|
88
|
+
exit - Exit REPL
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Format command result
|
|
94
|
+
*/
|
|
95
|
+
formatCommandResult(command: string, data: unknown): string {
|
|
96
|
+
switch (command) {
|
|
97
|
+
case 'list':
|
|
98
|
+
return this.formatListResult(data as { traceIds: TraceIdInfo[]; count: number });
|
|
99
|
+
case 'trace':
|
|
100
|
+
return this.formatTraceResult(data as TraceEntry[]);
|
|
101
|
+
case 'tree':
|
|
102
|
+
return this.formatTreeResult(data as TraceTreeNode[]);
|
|
103
|
+
default:
|
|
104
|
+
return `Unknown command: ${command}`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Format list command result
|
|
110
|
+
*/
|
|
111
|
+
private formatListResult(data: { traceIds: TraceIdInfo[]; count: number }): string {
|
|
112
|
+
const { traceIds, count } = data;
|
|
113
|
+
|
|
114
|
+
if (traceIds.length === 0) {
|
|
115
|
+
return 'No traces found.';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let output = '\n';
|
|
119
|
+
output += ' TraceId First Time Count\n';
|
|
120
|
+
output += ' ' + '-'.repeat(70) + '\n';
|
|
121
|
+
|
|
122
|
+
for (const trace of traceIds) {
|
|
123
|
+
const time = trace.firstTime.replace('T', ' ').substring(0, 19);
|
|
124
|
+
output += ` ${trace.traceId.padEnd(18)} ${time.padEnd(22)} ${trace.count}\n`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
output += `\n Total: ${count} traces\n`;
|
|
128
|
+
return output;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Format trace command result
|
|
133
|
+
*/
|
|
134
|
+
private formatTraceResult(entries: TraceEntry[]): string {
|
|
135
|
+
if (entries.length === 0) {
|
|
136
|
+
return 'No traces found.';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let output = '';
|
|
140
|
+
let currentTraceId: string | null = null;
|
|
141
|
+
|
|
142
|
+
for (const entry of entries) {
|
|
143
|
+
if (entry.traceId !== currentTraceId) {
|
|
144
|
+
currentTraceId = entry.traceId;
|
|
145
|
+
output += `\n=== Trace: ${entry.traceId} ===\n\n`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (entry.action === 'enter') {
|
|
149
|
+
output += `>>> ${entry.function} enter:`;
|
|
150
|
+
if (entry.params) {
|
|
151
|
+
output += '\n' + this.formatJsonCompact(entry.params);
|
|
152
|
+
}
|
|
153
|
+
output += '\n';
|
|
154
|
+
} else if (entry.action === 'quit') {
|
|
155
|
+
output += `<<< ${entry.function} quit`;
|
|
156
|
+
if (entry.durationMs !== undefined) {
|
|
157
|
+
output += ` (${entry.durationMs}ms)`;
|
|
158
|
+
}
|
|
159
|
+
if (entry.result) {
|
|
160
|
+
output += ':\n' + this.formatJsonCompact(entry.result);
|
|
161
|
+
}
|
|
162
|
+
output += '\n';
|
|
163
|
+
} else if (entry.action === 'error') {
|
|
164
|
+
output += `!!! ${entry.function} error: ${entry.error}\n`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return output;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Format tree command result
|
|
173
|
+
*/
|
|
174
|
+
private formatTreeResult(trees: TraceTreeNode[]): string {
|
|
175
|
+
if (trees.length === 0) {
|
|
176
|
+
return 'No trees found.';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let output = '';
|
|
180
|
+
|
|
181
|
+
for (const tree of trees) {
|
|
182
|
+
output += this.formatTreeNode(tree, '');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return output;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Format a single tree node recursively
|
|
190
|
+
*/
|
|
191
|
+
private formatTreeNode(node: TraceTreeNode, prefix: string): string {
|
|
192
|
+
const duration = node.durationMs !== undefined ? ` (${node.durationMs}ms)` : '';
|
|
193
|
+
let output = `${prefix}${node.function}${duration}\n`;
|
|
194
|
+
|
|
195
|
+
const childPrefix = prefix + ' ';
|
|
196
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
197
|
+
const isLast = i === node.children.length - 1;
|
|
198
|
+
const connector = isLast ? '└── ' : '├── ';
|
|
199
|
+
output += this.formatTreeNode(node.children[i], childPrefix + connector);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return output;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Format JSON in compact form
|
|
207
|
+
*/
|
|
208
|
+
private formatJsonCompact(data: unknown, indent: number = 2): string {
|
|
209
|
+
const spaces = ' '.repeat(indent);
|
|
210
|
+
|
|
211
|
+
if (data === null || data === undefined) {
|
|
212
|
+
return `${spaces}null\n`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (typeof data === 'string') {
|
|
216
|
+
// Handle long strings
|
|
217
|
+
if (data.length > 200) {
|
|
218
|
+
return `${spaces}"${data.substring(0, 200)}..."\n`;
|
|
219
|
+
}
|
|
220
|
+
return `${spaces}"${data}"\n`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (typeof data === 'number' || typeof data === 'boolean') {
|
|
224
|
+
return `${spaces}${data}\n`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (Array.isArray(data)) {
|
|
228
|
+
if (data.length === 0) return `${spaces}[]\n`;
|
|
229
|
+
|
|
230
|
+
let output = `${spaces}[\n`;
|
|
231
|
+
for (const item of data) {
|
|
232
|
+
output += this.formatJsonCompact(item, indent + 1);
|
|
233
|
+
}
|
|
234
|
+
output += `${spaces}]\n`;
|
|
235
|
+
return output;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (typeof data === 'object') {
|
|
239
|
+
const entries = Object.entries(data as Record<string, unknown>);
|
|
240
|
+
if (entries.length === 0) return `${spaces}{}\n`;
|
|
241
|
+
|
|
242
|
+
let output = `${spaces}{\n`;
|
|
243
|
+
for (const [key, val] of entries) {
|
|
244
|
+
output += `${spaces} ${key}: ${JSON.stringify(val).substring(0, 50)}`;
|
|
245
|
+
if (JSON.stringify(val).length > 50) {
|
|
246
|
+
output += '...';
|
|
247
|
+
}
|
|
248
|
+
output += '\n';
|
|
249
|
+
}
|
|
250
|
+
output += `${spaces}}\n`;
|
|
251
|
+
return output;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return `${spaces}${String(data)}\n`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Parse command string
|
|
259
|
+
*/
|
|
260
|
+
parseCommand(input: string): ParsedCommand {
|
|
261
|
+
const trimmed = input.trim();
|
|
262
|
+
|
|
263
|
+
if (!trimmed) {
|
|
264
|
+
return { command: 'help', args: {} };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Handle exit commands
|
|
268
|
+
if (['exit', 'quit', 'q'].includes(trimmed.toLowerCase())) {
|
|
269
|
+
return { command: 'exit', args: {} };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Handle clear
|
|
273
|
+
if (trimmed.toLowerCase() === 'clear') {
|
|
274
|
+
return { command: 'clear', args: {} };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Handle help
|
|
278
|
+
if (['help', '?', 'h'].includes(trimmed.toLowerCase())) {
|
|
279
|
+
return { command: 'help', args: {} };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Parse command and args
|
|
283
|
+
const parts = trimmed.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
|
|
284
|
+
|
|
285
|
+
if (parts.length === 0) {
|
|
286
|
+
return { command: 'help', args: {} };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const command = parts[0]?.toLowerCase() ?? '';
|
|
290
|
+
const args: Record<string, string> = {};
|
|
291
|
+
|
|
292
|
+
// Parse named arguments
|
|
293
|
+
for (let i = 1; i < parts.length; i++) {
|
|
294
|
+
const part = parts[i];
|
|
295
|
+
|
|
296
|
+
if (part.startsWith('--')) {
|
|
297
|
+
const key = part.substring(2);
|
|
298
|
+
const next = parts[i + 1];
|
|
299
|
+
if (next && !next.startsWith('--')) {
|
|
300
|
+
args[key] = next;
|
|
301
|
+
i++;
|
|
302
|
+
} else {
|
|
303
|
+
args[key] = 'true';
|
|
304
|
+
}
|
|
305
|
+
} else if (part.startsWith('-')) {
|
|
306
|
+
const key = part.substring(1);
|
|
307
|
+
const next = parts[i + 1];
|
|
308
|
+
if (next && !next.startsWith('-')) {
|
|
309
|
+
args[key] = next;
|
|
310
|
+
i++;
|
|
311
|
+
} else {
|
|
312
|
+
args[key] = 'true';
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
// Positional arguments
|
|
316
|
+
if (command === 'list' && !args.limit) {
|
|
317
|
+
args.limit = part;
|
|
318
|
+
} else if (command === 'trace' && !args.func) {
|
|
319
|
+
args.func = part;
|
|
320
|
+
} else if (command === 'tree' && !args.func) {
|
|
321
|
+
args.func = part;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return { command, args };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Update context
|
|
331
|
+
*/
|
|
332
|
+
setContext(context: Partial<ReplContext>): void {
|
|
333
|
+
this.context = { ...this.context, ...context };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Get current context
|
|
338
|
+
*/
|
|
339
|
+
getContext(): ReplContext {
|
|
340
|
+
return { ...this.context };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Add to history
|
|
345
|
+
*/
|
|
346
|
+
addToHistory(command: string): void {
|
|
347
|
+
if (command.trim() && this.history[this.history.length - 1] !== command) {
|
|
348
|
+
this.history.push(command);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Get history
|
|
354
|
+
*/
|
|
355
|
+
getHistory(): string[] {
|
|
356
|
+
return [...this.history];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test';
|
|
2
|
+
import { TraceFormatter } from './trace-formatter';
|
|
3
|
+
import type { TraceEntry, FormatOptions } from '../types';
|
|
4
|
+
|
|
5
|
+
describe('TraceFormatter', () => {
|
|
6
|
+
const formatter = new TraceFormatter();
|
|
7
|
+
|
|
8
|
+
const mockEnterEntry: TraceEntry = {
|
|
9
|
+
traceId: 'trace-001',
|
|
10
|
+
timestamp: '2026-04-08T10:00:00.000',
|
|
11
|
+
function: 'llm.component.invoke',
|
|
12
|
+
action: 'enter',
|
|
13
|
+
params: [
|
|
14
|
+
[{ role: 'user', content: 'Hello' }],
|
|
15
|
+
{ providerId: 'openai', model: 'gpt-4' }
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const mockQuitEntry: TraceEntry = {
|
|
20
|
+
traceId: 'trace-001',
|
|
21
|
+
timestamp: '2026-04-08T10:00:01.000',
|
|
22
|
+
function: 'llm.component.invoke',
|
|
23
|
+
action: 'quit',
|
|
24
|
+
result: { content: 'Hi there!', finishReason: 'stop' },
|
|
25
|
+
durationMs: 1000,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
test('formatEntry - should format enter entry', () => {
|
|
29
|
+
const result = formatter.formatEntry(mockEnterEntry);
|
|
30
|
+
expect(result).toContain('>>>');
|
|
31
|
+
expect(result).toContain('llm.component.invoke');
|
|
32
|
+
expect(result).toContain('enter');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('formatEntry - should format quit entry with duration', () => {
|
|
36
|
+
const result = formatter.formatEntry(mockQuitEntry);
|
|
37
|
+
expect(result).toContain('<<<');
|
|
38
|
+
expect(result).toContain('llm.component.invoke');
|
|
39
|
+
expect(result).toContain('1000ms');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('formatEntry - should handle multi-line content without breaking layout', () => {
|
|
43
|
+
const entryWithMultiline: TraceEntry = {
|
|
44
|
+
...mockQuitEntry,
|
|
45
|
+
result: {
|
|
46
|
+
content: 'This is a\nmulti-line\nresponse',
|
|
47
|
+
finishReason: 'stop'
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const result = formatter.formatEntry(entryWithMultiline, { pretty: true });
|
|
52
|
+
// Should handle newlines gracefully - indent continuation lines
|
|
53
|
+
const lines = result.split('\n');
|
|
54
|
+
expect(lines.length).toBeGreaterThan(1);
|
|
55
|
+
// Second line onwards should be indented
|
|
56
|
+
for (let i = 1; i < lines.length; i++) {
|
|
57
|
+
expect(lines[i].startsWith(' ')).toBe(true);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('formatEntry - should handle deep nesting', () => {
|
|
62
|
+
const deepEntry: TraceEntry = {
|
|
63
|
+
...mockQuitEntry,
|
|
64
|
+
result: {
|
|
65
|
+
level1: {
|
|
66
|
+
level2: {
|
|
67
|
+
level3: {
|
|
68
|
+
level4: 'deep value'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const result = formatter.formatEntry(deepEntry, { pretty: true, maxDepth: 2 });
|
|
76
|
+
// Should truncate at maxDepth
|
|
77
|
+
expect(result).toContain('level1');
|
|
78
|
+
expect(result).toContain('...');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('formatEntries - should format multiple entries', () => {
|
|
82
|
+
const entries = [mockEnterEntry, mockQuitEntry];
|
|
83
|
+
const result = formatter.formatEntries(entries);
|
|
84
|
+
expect(result).toContain('>>>');
|
|
85
|
+
expect(result).toContain('<<<');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('formatEntries - should respect pretty option', () => {
|
|
89
|
+
const entries = [mockEnterEntry, mockQuitEntry];
|
|
90
|
+
|
|
91
|
+
const prettyResult = formatter.formatEntries(entries, { pretty: true });
|
|
92
|
+
const jsonResult = formatter.formatEntries(entries, { pretty: false });
|
|
93
|
+
|
|
94
|
+
// Pretty should have newlines and indentation
|
|
95
|
+
expect(prettyResult).toContain('\n');
|
|
96
|
+
// Pretty should have separator line
|
|
97
|
+
expect(prettyResult).toContain('-'.repeat(40));
|
|
98
|
+
// JSON should be compact (no separator line between entries)
|
|
99
|
+
expect(jsonResult).not.toContain('-'.repeat(40));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('toJSON - should output valid JSON', () => {
|
|
103
|
+
const entries = [mockEnterEntry, mockQuitEntry];
|
|
104
|
+
const result = formatter.toJSON(entries);
|
|
105
|
+
|
|
106
|
+
const parsed = JSON.parse(result);
|
|
107
|
+
expect(parsed).toHaveLength(2);
|
|
108
|
+
expect(parsed[0].action).toBe('enter');
|
|
109
|
+
expect(parsed[1].action).toBe('quit');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('toSummary - should return trace summary', () => {
|
|
113
|
+
const entries = [mockEnterEntry, mockQuitEntry];
|
|
114
|
+
const result = formatter.toSummary(entries);
|
|
115
|
+
|
|
116
|
+
expect(result).toContain('llm.component.invoke');
|
|
117
|
+
expect(result).toContain('2 entries');
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Trace Formatter
|
|
3
|
+
*
|
|
4
|
+
* Formats trace entries for console output with support for:
|
|
5
|
+
* - Pretty printing with indentation
|
|
6
|
+
* - Multi-line content handling
|
|
7
|
+
* - JSON output
|
|
8
|
+
* - Depth-limited nested object display
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { TraceEntry, FormatOptions } from '../types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Formats trace entries for human-readable output
|
|
15
|
+
*/
|
|
16
|
+
export class TraceFormatter {
|
|
17
|
+
/**
|
|
18
|
+
* Format a single trace entry
|
|
19
|
+
*/
|
|
20
|
+
formatEntry(entry: TraceEntry, options: FormatOptions = {}): string {
|
|
21
|
+
const { pretty = false } = options;
|
|
22
|
+
const actionSymbol = entry.action === 'enter' ? '>>>' :
|
|
23
|
+
entry.action === 'error' ? '!!!' : '<<<';
|
|
24
|
+
|
|
25
|
+
let output = `${actionSymbol} ${entry.function} ${entry.action}`;
|
|
26
|
+
|
|
27
|
+
if (entry.action === 'enter' && entry.params) {
|
|
28
|
+
output += '\n' + this.formatData(entry.params, options);
|
|
29
|
+
} else if (entry.action === 'quit') {
|
|
30
|
+
if (entry.durationMs !== undefined) {
|
|
31
|
+
output += ` (${entry.durationMs}ms)`;
|
|
32
|
+
}
|
|
33
|
+
if (entry.result) {
|
|
34
|
+
output += '\n' + this.formatData(entry.result, options);
|
|
35
|
+
}
|
|
36
|
+
} else if (entry.action === 'error' && entry.error) {
|
|
37
|
+
output += `: ${entry.error}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Handle multi-line content by indenting continuation lines
|
|
41
|
+
if (pretty) {
|
|
42
|
+
output = this.indentMultilineContent(output);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return output;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Indent all lines after the first one
|
|
50
|
+
*/
|
|
51
|
+
private indentMultilineContent(text: string, indent: string = ' '): string {
|
|
52
|
+
const lines = text.split('\n');
|
|
53
|
+
if (lines.length <= 1) return text;
|
|
54
|
+
|
|
55
|
+
return lines.map((line, i) => i === 0 ? line : `${indent}${line}`).join('\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Format multiple trace entries
|
|
60
|
+
*/
|
|
61
|
+
formatEntries(entries: TraceEntry[], options: FormatOptions = {}): string {
|
|
62
|
+
if (entries.length === 0) {
|
|
63
|
+
return 'No trace entries';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const formatted = entries.map(entry => this.formatEntry(entry, options));
|
|
67
|
+
|
|
68
|
+
// Use separator only for pretty mode, otherwise join without separator
|
|
69
|
+
if (options.pretty) {
|
|
70
|
+
return formatted.join('\n' + '-'.repeat(80) + '\n');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return formatted.join('\n');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Format data with proper indentation for multi-line content
|
|
78
|
+
*/
|
|
79
|
+
private formatData(data: unknown, options: FormatOptions = {}): string {
|
|
80
|
+
const { pretty = false, maxDepth = 10 } = options;
|
|
81
|
+
|
|
82
|
+
if (pretty) {
|
|
83
|
+
return this.formatPretty(data, 0, maxDepth);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Compact JSON - no newlines
|
|
87
|
+
return JSON.stringify(data);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Format data with pretty printing and indentation
|
|
92
|
+
*/
|
|
93
|
+
private formatPretty(data: unknown, indent: number = 0, maxDepth: number): string {
|
|
94
|
+
const baseIndent = ' '.repeat(indent);
|
|
95
|
+
const nextIndent = ' '.repeat(indent + 1);
|
|
96
|
+
|
|
97
|
+
if (data === null || data === undefined) {
|
|
98
|
+
return String(data);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (typeof data === 'string') {
|
|
102
|
+
// Handle multi-line strings - indent each line
|
|
103
|
+
if (data.includes('\n')) {
|
|
104
|
+
return data.split('\n').map((line, i) =>
|
|
105
|
+
i === 0 ? line : `${nextIndent}${line}`
|
|
106
|
+
).join('\n');
|
|
107
|
+
}
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (typeof data === 'number' || typeof data === 'boolean') {
|
|
112
|
+
return String(data);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (Array.isArray(data)) {
|
|
116
|
+
if (data.length === 0) return '[]';
|
|
117
|
+
|
|
118
|
+
const items = data.map(item => {
|
|
119
|
+
const formatted = this.formatPretty(item, indent + 1, maxDepth);
|
|
120
|
+
// Indent each line of the formatted item
|
|
121
|
+
return formatted.split('\n').map(line => ` ${line}`).join('\n');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return '[\n' + items.join(',\n') + '\n' + baseIndent + ']';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof data === 'object') {
|
|
128
|
+
const obj = data as Record<string, unknown>;
|
|
129
|
+
const keys = Object.keys(obj);
|
|
130
|
+
|
|
131
|
+
if (keys.length === 0) return '{}';
|
|
132
|
+
|
|
133
|
+
if (indent >= maxDepth) {
|
|
134
|
+
return '{...}';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const entries = keys.map(key => {
|
|
138
|
+
const value = obj[key];
|
|
139
|
+
let formatted: string;
|
|
140
|
+
|
|
141
|
+
if (typeof value === 'object' && value !== null) {
|
|
142
|
+
formatted = this.formatPretty(value, indent + 1, maxDepth);
|
|
143
|
+
} else if (typeof value === 'string' && value.includes('\n')) {
|
|
144
|
+
// Handle multi-line string values - indent each line
|
|
145
|
+
formatted = value.split('\n').map((line, i) =>
|
|
146
|
+
i === 0 ? line : `${nextIndent}${line}`
|
|
147
|
+
).join('\n');
|
|
148
|
+
} else {
|
|
149
|
+
formatted = JSON.stringify(value);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Indent all lines of the formatted value
|
|
153
|
+
return ` ${key}: ` + formatted.split('\n').map((line, i) =>
|
|
154
|
+
i === 0 ? line : nextIndent + line
|
|
155
|
+
).join('\n');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return '{\n' + entries.join(',\n') + '\n' + baseIndent + '}';
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return JSON.stringify(data);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Output as JSON
|
|
166
|
+
*/
|
|
167
|
+
toJSON(entries: TraceEntry[]): string {
|
|
168
|
+
return JSON.stringify(entries, null, 2);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Generate a summary of trace entries
|
|
173
|
+
*/
|
|
174
|
+
toSummary(entries: TraceEntry[]): string {
|
|
175
|
+
if (entries.length === 0) {
|
|
176
|
+
return 'No trace entries';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const functions = new Set(entries.map(e => e.function));
|
|
180
|
+
const actions = entries.reduce((acc, e) => {
|
|
181
|
+
acc[e.action] = (acc[e.action] || 0) + 1;
|
|
182
|
+
return acc;
|
|
183
|
+
}, {} as Record<string, number>);
|
|
184
|
+
|
|
185
|
+
let summary = `Trace Summary (${entries.length} entries)\n`;
|
|
186
|
+
summary += `Functions: ${[...functions].join(', ')}\n`;
|
|
187
|
+
summary += `Actions: ${Object.entries(actions).map(([k, v]) => `${k}=${v}`).join(', ')}`;
|
|
188
|
+
|
|
189
|
+
return summary;
|
|
190
|
+
}
|
|
191
|
+
}
|