@n8n/instance-ai 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/LICENSE.md +88 -0
- package/dist/agent/instance-agent.d.ts +3 -0
- package/dist/agent/instance-agent.js +215 -0
- package/dist/agent/instance-agent.js.map +1 -0
- package/dist/agent/register-with-mastra.d.ts +3 -0
- package/dist/agent/register-with-mastra.js +19 -0
- package/dist/agent/register-with-mastra.js.map +1 -0
- package/dist/agent/sanitize-mcp-schemas.d.ts +2 -0
- package/dist/agent/sanitize-mcp-schemas.js +91 -0
- package/dist/agent/sanitize-mcp-schemas.js.map +1 -0
- package/dist/agent/sub-agent-factory.d.ts +14 -0
- package/dist/agent/sub-agent-factory.js +48 -0
- package/dist/agent/sub-agent-factory.js.map +1 -0
- package/dist/agent/system-prompt.d.ts +13 -0
- package/dist/agent/system-prompt.js +226 -0
- package/dist/agent/system-prompt.js.map +1 -0
- package/dist/build.tsbuildinfo +1 -0
- package/dist/compaction/compaction-helper.d.ts +9 -0
- package/dist/compaction/compaction-helper.js +58 -0
- package/dist/compaction/compaction-helper.js.map +1 -0
- package/dist/compaction/index.d.ts +2 -0
- package/dist/compaction/index.js +6 -0
- package/dist/compaction/index.js.map +1 -0
- package/dist/domain-access/domain-access-tracker.d.ts +9 -0
- package/dist/domain-access/domain-access-tracker.js +46 -0
- package/dist/domain-access/domain-access-tracker.js.map +1 -0
- package/dist/domain-access/domain-gating.d.ts +65 -0
- package/dist/domain-access/domain-gating.js +68 -0
- package/dist/domain-access/domain-gating.js.map +1 -0
- package/dist/domain-access/index.d.ts +4 -0
- package/dist/domain-access/index.js +11 -0
- package/dist/domain-access/index.js.map +1 -0
- package/dist/event-bus/event-bus.interface.d.ts +15 -0
- package/dist/event-bus/event-bus.interface.js +3 -0
- package/dist/event-bus/event-bus.interface.js.map +1 -0
- package/dist/event-bus/index.d.ts +1 -0
- package/dist/event-bus/index.js +3 -0
- package/dist/event-bus/index.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/mcp-client-manager.d.ts +6 -0
- package/dist/mcp/mcp-client-manager.js +34 -0
- package/dist/mcp/mcp-client-manager.js.map +1 -0
- package/dist/memory/memory-config.d.ts +3 -0
- package/dist/memory/memory-config.js +29 -0
- package/dist/memory/memory-config.js.map +1 -0
- package/dist/memory/title-utils.d.ts +3 -0
- package/dist/memory/title-utils.js +42 -0
- package/dist/memory/title-utils.js.map +1 -0
- package/dist/memory/working-memory-template.d.ts +1 -0
- package/dist/memory/working-memory-template.js +25 -0
- package/dist/memory/working-memory-template.js.map +1 -0
- package/dist/planned-tasks/planned-task-service.d.ts +33 -0
- package/dist/planned-tasks/planned-task-service.js +151 -0
- package/dist/planned-tasks/planned-task-service.js.map +1 -0
- package/dist/runtime/background-task-manager.d.ts +53 -0
- package/dist/runtime/background-task-manager.js +127 -0
- package/dist/runtime/background-task-manager.js.map +1 -0
- package/dist/runtime/resumable-stream-executor.d.ts +75 -0
- package/dist/runtime/resumable-stream-executor.js +1552 -0
- package/dist/runtime/resumable-stream-executor.js.map +1 -0
- package/dist/runtime/run-state-registry.d.ts +109 -0
- package/dist/runtime/run-state-registry.js +210 -0
- package/dist/runtime/run-state-registry.js.map +1 -0
- package/dist/runtime/stream-runner.d.ts +27 -0
- package/dist/runtime/stream-runner.js +87 -0
- package/dist/runtime/stream-runner.js.map +1 -0
- package/dist/runtime/working-memory-tracing.d.ts +19 -0
- package/dist/runtime/working-memory-tracing.js +111 -0
- package/dist/runtime/working-memory-tracing.js.map +1 -0
- package/dist/storage/agent-tree-snapshot.d.ts +7 -0
- package/dist/storage/agent-tree-snapshot.js +3 -0
- package/dist/storage/agent-tree-snapshot.js.map +1 -0
- package/dist/storage/index.d.ts +10 -0
- package/dist/storage/index.js +17 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/iteration-log.d.ts +30 -0
- package/dist/storage/iteration-log.js +31 -0
- package/dist/storage/iteration-log.js.map +1 -0
- package/dist/storage/mastra-iteration-log-storage.d.ts +10 -0
- package/dist/storage/mastra-iteration-log-storage.js +57 -0
- package/dist/storage/mastra-iteration-log-storage.js.map +1 -0
- package/dist/storage/mastra-task-storage.d.ts +9 -0
- package/dist/storage/mastra-task-storage.js +35 -0
- package/dist/storage/mastra-task-storage.js.map +1 -0
- package/dist/storage/planned-task-storage.d.ts +10 -0
- package/dist/storage/planned-task-storage.js +97 -0
- package/dist/storage/planned-task-storage.js.map +1 -0
- package/dist/storage/thread-patch.d.ts +16 -0
- package/dist/storage/thread-patch.js +50 -0
- package/dist/storage/thread-patch.js.map +1 -0
- package/dist/storage/workflow-loop-storage.d.ts +217 -0
- package/dist/storage/workflow-loop-storage.js +59 -0
- package/dist/storage/workflow-loop-storage.js.map +1 -0
- package/dist/stream/consume-with-hitl.d.ts +22 -0
- package/dist/stream/consume-with-hitl.js +29 -0
- package/dist/stream/consume-with-hitl.js.map +1 -0
- package/dist/stream/map-chunk.d.ts +2 -0
- package/dist/stream/map-chunk.js +224 -0
- package/dist/stream/map-chunk.js.map +1 -0
- package/dist/tools/best-practices/get-best-practices.tool.d.ts +12 -0
- package/dist/tools/best-practices/get-best-practices.tool.js +64 -0
- package/dist/tools/best-practices/get-best-practices.tool.js.map +1 -0
- package/dist/tools/best-practices/guides/chatbot.d.ts +1 -0
- package/dist/tools/best-practices/guides/chatbot.js +111 -0
- package/dist/tools/best-practices/guides/chatbot.js.map +1 -0
- package/dist/tools/best-practices/guides/content-generation.d.ts +1 -0
- package/dist/tools/best-practices/guides/content-generation.js +108 -0
- package/dist/tools/best-practices/guides/content-generation.js.map +1 -0
- package/dist/tools/best-practices/guides/data-extraction.d.ts +1 -0
- package/dist/tools/best-practices/guides/data-extraction.js +115 -0
- package/dist/tools/best-practices/guides/data-extraction.js.map +1 -0
- package/dist/tools/best-practices/guides/data-persistence.d.ts +1 -0
- package/dist/tools/best-practices/guides/data-persistence.js +198 -0
- package/dist/tools/best-practices/guides/data-persistence.js.map +1 -0
- package/dist/tools/best-practices/guides/data-transformation.d.ts +1 -0
- package/dist/tools/best-practices/guides/data-transformation.js +139 -0
- package/dist/tools/best-practices/guides/data-transformation.js.map +1 -0
- package/dist/tools/best-practices/guides/document-processing.d.ts +1 -0
- package/dist/tools/best-practices/guides/document-processing.js +326 -0
- package/dist/tools/best-practices/guides/document-processing.js.map +1 -0
- package/dist/tools/best-practices/guides/form-input.d.ts +1 -0
- package/dist/tools/best-practices/guides/form-input.js +166 -0
- package/dist/tools/best-practices/guides/form-input.js.map +1 -0
- package/dist/tools/best-practices/guides/notification.d.ts +1 -0
- package/dist/tools/best-practices/guides/notification.js +128 -0
- package/dist/tools/best-practices/guides/notification.js.map +1 -0
- package/dist/tools/best-practices/guides/scheduling.d.ts +1 -0
- package/dist/tools/best-practices/guides/scheduling.js +145 -0
- package/dist/tools/best-practices/guides/scheduling.js.map +1 -0
- package/dist/tools/best-practices/guides/scraping-and-research.d.ts +1 -0
- package/dist/tools/best-practices/guides/scraping-and-research.js +151 -0
- package/dist/tools/best-practices/guides/scraping-and-research.js.map +1 -0
- package/dist/tools/best-practices/guides/triage.d.ts +1 -0
- package/dist/tools/best-practices/guides/triage.js +142 -0
- package/dist/tools/best-practices/guides/triage.js.map +1 -0
- package/dist/tools/best-practices/index.d.ts +2 -0
- package/dist/tools/best-practices/index.js +34 -0
- package/dist/tools/best-practices/index.js.map +1 -0
- package/dist/tools/best-practices/techniques.d.ts +20 -0
- package/dist/tools/best-practices/techniques.js +40 -0
- package/dist/tools/best-practices/techniques.js.map +1 -0
- package/dist/tools/credentials/delete-credential.tool.d.ts +21 -0
- package/dist/tools/credentials/delete-credential.tool.js +51 -0
- package/dist/tools/credentials/delete-credential.tool.js.map +1 -0
- package/dist/tools/credentials/get-credential.tool.d.ts +4 -0
- package/dist/tools/credentials/get-credential.tool.js +26 -0
- package/dist/tools/credentials/get-credential.tool.js.map +1 -0
- package/dist/tools/credentials/list-credentials.tool.d.ts +6 -0
- package/dist/tools/credentials/list-credentials.tool.js +30 -0
- package/dist/tools/credentials/list-credentials.tool.js.map +1 -0
- package/dist/tools/credentials/search-credential-types.tool.d.ts +6 -0
- package/dist/tools/credentials/search-credential-types.tool.js +44 -0
- package/dist/tools/credentials/search-credential-types.tool.js.map +1 -0
- package/dist/tools/credentials/setup-credentials.tool.d.ts +73 -0
- package/dist/tools/credentials/setup-credentials.tool.js +134 -0
- package/dist/tools/credentials/setup-credentials.tool.js.map +1 -0
- package/dist/tools/credentials/test-credential.tool.d.ts +7 -0
- package/dist/tools/credentials/test-credential.tool.js +30 -0
- package/dist/tools/credentials/test-credential.tool.js.map +1 -0
- package/dist/tools/data-tables/add-data-table-column.tool.d.ts +27 -0
- package/dist/tools/data-tables/add-data-table-column.tool.js +60 -0
- package/dist/tools/data-tables/add-data-table-column.tool.js.map +1 -0
- package/dist/tools/data-tables/create-data-table.tool.d.ts +36 -0
- package/dist/tools/data-tables/create-data-table.tool.js +79 -0
- package/dist/tools/data-tables/create-data-table.tool.js.map +1 -0
- package/dist/tools/data-tables/delete-data-table-column.tool.d.ts +21 -0
- package/dist/tools/data-tables/delete-data-table-column.tool.js +48 -0
- package/dist/tools/data-tables/delete-data-table-column.tool.js.map +1 -0
- package/dist/tools/data-tables/delete-data-table-rows.tool.d.ts +29 -0
- package/dist/tools/data-tables/delete-data-table-rows.tool.js +63 -0
- package/dist/tools/data-tables/delete-data-table-rows.tool.js.map +1 -0
- package/dist/tools/data-tables/delete-data-table.tool.d.ts +20 -0
- package/dist/tools/data-tables/delete-data-table.tool.js +46 -0
- package/dist/tools/data-tables/delete-data-table.tool.js.map +1 -0
- package/dist/tools/data-tables/get-data-table-schema.tool.d.ts +11 -0
- package/dist/tools/data-tables/get-data-table-schema.tool.js +27 -0
- package/dist/tools/data-tables/get-data-table-schema.tool.js.map +1 -0
- package/dist/tools/data-tables/insert-data-table-rows.tool.d.ts +21 -0
- package/dist/tools/data-tables/insert-data-table-rows.tool.js +52 -0
- package/dist/tools/data-tables/insert-data-table-rows.tool.js.map +1 -0
- package/dist/tools/data-tables/list-data-tables.tool.d.ts +6 -0
- package/dist/tools/data-tables/list-data-tables.tool.js +32 -0
- package/dist/tools/data-tables/list-data-tables.tool.js.map +1 -0
- package/dist/tools/data-tables/query-data-table-rows.tool.d.ts +21 -0
- package/dist/tools/data-tables/query-data-table-rows.tool.js +54 -0
- package/dist/tools/data-tables/query-data-table-rows.tool.js.map +1 -0
- package/dist/tools/data-tables/rename-data-table-column.tool.d.ts +22 -0
- package/dist/tools/data-tables/rename-data-table-column.tool.js +49 -0
- package/dist/tools/data-tables/rename-data-table-column.tool.js.map +1 -0
- package/dist/tools/data-tables/update-data-table-rows.tool.d.ts +29 -0
- package/dist/tools/data-tables/update-data-table-rows.tool.js +57 -0
- package/dist/tools/data-tables/update-data-table-rows.tool.js.map +1 -0
- package/dist/tools/executions/debug-execution.tool.d.ts +4 -0
- package/dist/tools/executions/debug-execution.tool.js +41 -0
- package/dist/tools/executions/debug-execution.tool.js.map +1 -0
- package/dist/tools/executions/get-execution.tool.d.ts +4 -0
- package/dist/tools/executions/get-execution.tool.js +26 -0
- package/dist/tools/executions/get-execution.tool.js.map +1 -0
- package/dist/tools/executions/get-node-output.tool.d.ts +7 -0
- package/dist/tools/executions/get-node-output.tool.js +44 -0
- package/dist/tools/executions/get-node-output.tool.js.map +1 -0
- package/dist/tools/executions/list-executions.tool.d.ts +8 -0
- package/dist/tools/executions/list-executions.tool.js +45 -0
- package/dist/tools/executions/list-executions.tool.js.map +1 -0
- package/dist/tools/executions/run-workflow.tool.d.ts +24 -0
- package/dist/tools/executions/run-workflow.tool.js +82 -0
- package/dist/tools/executions/run-workflow.tool.js.map +1 -0
- package/dist/tools/executions/stop-execution.tool.d.ts +7 -0
- package/dist/tools/executions/stop-execution.tool.js +22 -0
- package/dist/tools/executions/stop-execution.tool.js.map +1 -0
- package/dist/tools/filesystem/create-tools-from-mcp-server.d.ts +3 -0
- package/dist/tools/filesystem/create-tools-from-mcp-server.js +61 -0
- package/dist/tools/filesystem/create-tools-from-mcp-server.js.map +1 -0
- package/dist/tools/filesystem/get-file-tree.tool.d.ts +22 -0
- package/dist/tools/filesystem/get-file-tree.tool.js +64 -0
- package/dist/tools/filesystem/get-file-tree.tool.js.map +1 -0
- package/dist/tools/filesystem/list-files.tool.d.ts +30 -0
- package/dist/tools/filesystem/list-files.tool.js +100 -0
- package/dist/tools/filesystem/list-files.tool.js.map +1 -0
- package/dist/tools/filesystem/read-file.tool.d.ts +25 -0
- package/dist/tools/filesystem/read-file.tool.js +83 -0
- package/dist/tools/filesystem/read-file.tool.js.map +1 -0
- package/dist/tools/filesystem/search-files.tool.d.ts +31 -0
- package/dist/tools/filesystem/search-files.tool.js +96 -0
- package/dist/tools/filesystem/search-files.tool.js.map +1 -0
- package/dist/tools/index.d.ts +2385 -0
- package/dist/tools/index.js +195 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/nodes/explore-node-resources.tool.d.ts +21 -0
- package/dist/tools/nodes/explore-node-resources.tool.js +71 -0
- package/dist/tools/nodes/explore-node-resources.tool.js.map +1 -0
- package/dist/tools/nodes/get-node-description.tool.d.ts +23 -0
- package/dist/tools/nodes/get-node-description.tool.js +52 -0
- package/dist/tools/nodes/get-node-description.tool.js.map +1 -0
- package/dist/tools/nodes/get-node-type-definition.tool.d.ts +17 -0
- package/dist/tools/nodes/get-node-type-definition.tool.js +73 -0
- package/dist/tools/nodes/get-node-type-definition.tool.js.map +1 -0
- package/dist/tools/nodes/get-suggested-nodes.tool.d.ts +14 -0
- package/dist/tools/nodes/get-suggested-nodes.tool.js +54 -0
- package/dist/tools/nodes/get-suggested-nodes.tool.js.map +1 -0
- package/dist/tools/nodes/list-nodes.tool.d.ts +6 -0
- package/dist/tools/nodes/list-nodes.tool.js +33 -0
- package/dist/tools/nodes/list-nodes.tool.js.map +1 -0
- package/dist/tools/nodes/node-search-engine.d.ts +18 -0
- package/dist/tools/nodes/node-search-engine.js +266 -0
- package/dist/tools/nodes/node-search-engine.js.map +1 -0
- package/dist/tools/nodes/node-search-engine.types.d.ts +38 -0
- package/dist/tools/nodes/node-search-engine.types.js +18 -0
- package/dist/tools/nodes/node-search-engine.types.js.map +1 -0
- package/dist/tools/nodes/search-nodes.tool.d.ts +28 -0
- package/dist/tools/nodes/search-nodes.tool.js +87 -0
- package/dist/tools/nodes/search-nodes.tool.js.map +1 -0
- package/dist/tools/nodes/suggested-nodes-data.d.ts +11 -0
- package/dist/tools/nodes/suggested-nodes-data.js +326 -0
- package/dist/tools/nodes/suggested-nodes-data.js.map +1 -0
- package/dist/tools/orchestration/browser-credential-setup.tool.d.ts +14 -0
- package/dist/tools/orchestration/browser-credential-setup.tool.js +429 -0
- package/dist/tools/orchestration/browser-credential-setup.tool.js.map +1 -0
- package/dist/tools/orchestration/build-workflow-agent.prompt.d.ts +2 -0
- package/dist/tools/orchestration/build-workflow-agent.prompt.js +836 -0
- package/dist/tools/orchestration/build-workflow-agent.prompt.js.map +1 -0
- package/dist/tools/orchestration/build-workflow-agent.tool.d.ts +23 -0
- package/dist/tools/orchestration/build-workflow-agent.tool.js +503 -0
- package/dist/tools/orchestration/build-workflow-agent.tool.js.map +1 -0
- package/dist/tools/orchestration/cancel-background-task.tool.d.ts +6 -0
- package/dist/tools/orchestration/cancel-background-task.tool.js +24 -0
- package/dist/tools/orchestration/cancel-background-task.tool.js.map +1 -0
- package/dist/tools/orchestration/correct-background-task.tool.d.ts +7 -0
- package/dist/tools/orchestration/correct-background-task.tool.js +41 -0
- package/dist/tools/orchestration/correct-background-task.tool.js.map +1 -0
- package/dist/tools/orchestration/data-table-agent.prompt.d.ts +1 -0
- package/dist/tools/orchestration/data-table-agent.prompt.js +29 -0
- package/dist/tools/orchestration/data-table-agent.prompt.js.map +1 -0
- package/dist/tools/orchestration/data-table-agent.tool.d.ts +21 -0
- package/dist/tools/orchestration/data-table-agent.tool.js +171 -0
- package/dist/tools/orchestration/data-table-agent.tool.js.map +1 -0
- package/dist/tools/orchestration/delegate.schemas.d.ts +32 -0
- package/dist/tools/orchestration/delegate.schemas.js +29 -0
- package/dist/tools/orchestration/delegate.schemas.js.map +1 -0
- package/dist/tools/orchestration/delegate.tool.d.ts +27 -0
- package/dist/tools/orchestration/delegate.tool.js +307 -0
- package/dist/tools/orchestration/delegate.tool.js.map +1 -0
- package/dist/tools/orchestration/display-utils.d.ts +1 -0
- package/dist/tools/orchestration/display-utils.js +8 -0
- package/dist/tools/orchestration/display-utils.js.map +1 -0
- package/dist/tools/orchestration/plan.tool.d.ts +45 -0
- package/dist/tools/orchestration/plan.tool.js +99 -0
- package/dist/tools/orchestration/plan.tool.js.map +1 -0
- package/dist/tools/orchestration/report-verification-verdict.tool.d.ts +14 -0
- package/dist/tools/orchestration/report-verification-verdict.tool.js +66 -0
- package/dist/tools/orchestration/report-verification-verdict.tool.js.map +1 -0
- package/dist/tools/orchestration/research-agent-prompt.d.ts +1 -0
- package/dist/tools/orchestration/research-agent-prompt.js +28 -0
- package/dist/tools/orchestration/research-agent-prompt.js.map +1 -0
- package/dist/tools/orchestration/research-with-agent.tool.d.ts +23 -0
- package/dist/tools/orchestration/research-with-agent.tool.js +160 -0
- package/dist/tools/orchestration/research-with-agent.tool.js.map +1 -0
- package/dist/tools/orchestration/tracing-utils.d.ts +20 -0
- package/dist/tools/orchestration/tracing-utils.js +102 -0
- package/dist/tools/orchestration/tracing-utils.js.map +1 -0
- package/dist/tools/orchestration/update-tasks.tool.d.ts +10 -0
- package/dist/tools/orchestration/update-tasks.tool.js +26 -0
- package/dist/tools/orchestration/update-tasks.tool.js.map +1 -0
- package/dist/tools/orchestration/verify-built-workflow.tool.d.ts +13 -0
- package/dist/tools/orchestration/verify-built-workflow.tool.js +58 -0
- package/dist/tools/orchestration/verify-built-workflow.tool.js.map +1 -0
- package/dist/tools/shared/ask-user.tool.d.ts +61 -0
- package/dist/tools/shared/ask-user.tool.js +98 -0
- package/dist/tools/shared/ask-user.tool.js.map +1 -0
- package/dist/tools/templates/search-template-parameters.tool.d.ts +11 -0
- package/dist/tools/templates/search-template-parameters.tool.js +55 -0
- package/dist/tools/templates/search-template-parameters.tool.js.map +1 -0
- package/dist/tools/templates/search-template-structures.tool.d.ts +12 -0
- package/dist/tools/templates/search-template-structures.tool.js +45 -0
- package/dist/tools/templates/search-template-structures.tool.js.map +1 -0
- package/dist/tools/templates/template-api.d.ts +21 -0
- package/dist/tools/templates/template-api.js +104 -0
- package/dist/tools/templates/template-api.js.map +1 -0
- package/dist/tools/templates/types.d.ts +52 -0
- package/dist/tools/templates/types.js +37 -0
- package/dist/tools/templates/types.js.map +1 -0
- package/dist/tools/utils/mermaid.utils.d.ts +15 -0
- package/dist/tools/utils/mermaid.utils.js +622 -0
- package/dist/tools/utils/mermaid.utils.js.map +1 -0
- package/dist/tools/utils/node-configuration.utils.d.ts +6 -0
- package/dist/tools/utils/node-configuration.utils.js +77 -0
- package/dist/tools/utils/node-configuration.utils.js.map +1 -0
- package/dist/tools/web-research/fetch-url.tool.d.ts +27 -0
- package/dist/tools/web-research/fetch-url.tool.js +116 -0
- package/dist/tools/web-research/fetch-url.tool.js.map +1 -0
- package/dist/tools/web-research/index.d.ts +1 -0
- package/dist/tools/web-research/index.js +6 -0
- package/dist/tools/web-research/index.js.map +1 -0
- package/dist/tools/web-research/sanitize-web-content.d.ts +3 -0
- package/dist/tools/web-research/sanitize-web-content.js +36 -0
- package/dist/tools/web-research/sanitize-web-content.js.map +1 -0
- package/dist/tools/web-research/web-search.tool.d.ts +6 -0
- package/dist/tools/web-research/web-search.tool.js +57 -0
- package/dist/tools/web-research/web-search.tool.js.map +1 -0
- package/dist/tools/workflows/apply-workflow-credentials.tool.d.ts +10 -0
- package/dist/tools/workflows/apply-workflow-credentials.tool.js +87 -0
- package/dist/tools/workflows/apply-workflow-credentials.tool.js.map +1 -0
- package/dist/tools/workflows/build-workflow.tool.d.ts +16 -0
- package/dist/tools/workflows/build-workflow.tool.js +163 -0
- package/dist/tools/workflows/build-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/delete-workflow.tool.d.ts +21 -0
- package/dist/tools/workflows/delete-workflow.tool.js +51 -0
- package/dist/tools/workflows/delete-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/get-workflow-as-code.tool.d.ts +14 -0
- package/dist/tools/workflows/get-workflow-as-code.tool.js +37 -0
- package/dist/tools/workflows/get-workflow-as-code.tool.js.map +1 -0
- package/dist/tools/workflows/get-workflow-version.tool.d.ts +21 -0
- package/dist/tools/workflows/get-workflow-version.tool.js +38 -0
- package/dist/tools/workflows/get-workflow-version.tool.js.map +1 -0
- package/dist/tools/workflows/get-workflow.tool.d.ts +18 -0
- package/dist/tools/workflows/get-workflow.tool.js +36 -0
- package/dist/tools/workflows/get-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/list-workflow-versions.tool.d.ts +8 -0
- package/dist/tools/workflows/list-workflow-versions.tool.js +47 -0
- package/dist/tools/workflows/list-workflow-versions.tool.js.map +1 -0
- package/dist/tools/workflows/list-workflows.tool.d.ts +14 -0
- package/dist/tools/workflows/list-workflows.tool.js +42 -0
- package/dist/tools/workflows/list-workflows.tool.js.map +1 -0
- package/dist/tools/workflows/materialize-node-type.tool.d.ts +23 -0
- package/dist/tools/workflows/materialize-node-type.tool.js +105 -0
- package/dist/tools/workflows/materialize-node-type.tool.js.map +1 -0
- package/dist/tools/workflows/publish-workflow.tool.d.ts +24 -0
- package/dist/tools/workflows/publish-workflow.tool.js +92 -0
- package/dist/tools/workflows/publish-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/resolve-credentials.d.ts +14 -0
- package/dist/tools/workflows/resolve-credentials.js +89 -0
- package/dist/tools/workflows/resolve-credentials.js.map +1 -0
- package/dist/tools/workflows/restore-workflow-version.tool.d.ts +22 -0
- package/dist/tools/workflows/restore-workflow-version.tool.js +65 -0
- package/dist/tools/workflows/restore-workflow-version.tool.js.map +1 -0
- package/dist/tools/workflows/setup-workflow.schema.d.ts +331 -0
- package/dist/tools/workflows/setup-workflow.schema.js +21 -0
- package/dist/tools/workflows/setup-workflow.schema.js.map +1 -0
- package/dist/tools/workflows/setup-workflow.service.d.ts +40 -0
- package/dist/tools/workflows/setup-workflow.service.js +470 -0
- package/dist/tools/workflows/setup-workflow.service.js.map +1 -0
- package/dist/tools/workflows/setup-workflow.tool.d.ts +150 -0
- package/dist/tools/workflows/setup-workflow.tool.js +219 -0
- package/dist/tools/workflows/setup-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/submit-workflow.tool.d.ts +33 -0
- package/dist/tools/workflows/submit-workflow.tool.js +258 -0
- package/dist/tools/workflows/submit-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/unpublish-workflow.tool.d.ts +22 -0
- package/dist/tools/workflows/unpublish-workflow.tool.js +61 -0
- package/dist/tools/workflows/unpublish-workflow.tool.js.map +1 -0
- package/dist/tools/workflows/update-workflow-version.tool.d.ts +9 -0
- package/dist/tools/workflows/update-workflow-version.tool.js +35 -0
- package/dist/tools/workflows/update-workflow-version.tool.js.map +1 -0
- package/dist/tools/workflows/write-sandbox-file.tool.d.ts +13 -0
- package/dist/tools/workflows/write-sandbox-file.tool.js +53 -0
- package/dist/tools/workflows/write-sandbox-file.tool.js.map +1 -0
- package/dist/tools/workspace/cleanup-test-executions.tool.d.ts +22 -0
- package/dist/tools/workspace/cleanup-test-executions.tool.js +58 -0
- package/dist/tools/workspace/cleanup-test-executions.tool.js.map +1 -0
- package/dist/tools/workspace/create-folder.tool.d.ts +24 -0
- package/dist/tools/workspace/create-folder.tool.js +59 -0
- package/dist/tools/workspace/create-folder.tool.js.map +1 -0
- package/dist/tools/workspace/delete-folder.tool.d.ts +24 -0
- package/dist/tools/workspace/delete-folder.tool.js +60 -0
- package/dist/tools/workspace/delete-folder.tool.js.map +1 -0
- package/dist/tools/workspace/list-folders.tool.d.ts +6 -0
- package/dist/tools/workspace/list-folders.tool.js +26 -0
- package/dist/tools/workspace/list-folders.tool.js.map +1 -0
- package/dist/tools/workspace/list-projects.tool.d.ts +4 -0
- package/dist/tools/workspace/list-projects.tool.js +24 -0
- package/dist/tools/workspace/list-projects.tool.js.map +1 -0
- package/dist/tools/workspace/list-tags.tool.d.ts +7 -0
- package/dist/tools/workspace/list-tags.tool.js +23 -0
- package/dist/tools/workspace/list-tags.tool.js.map +1 -0
- package/dist/tools/workspace/move-workflow-to-folder.tool.d.ts +23 -0
- package/dist/tools/workspace/move-workflow-to-folder.tool.js +56 -0
- package/dist/tools/workspace/move-workflow-to-folder.tool.js.map +1 -0
- package/dist/tools/workspace/tag-workflow.tool.d.ts +22 -0
- package/dist/tools/workspace/tag-workflow.tool.js +52 -0
- package/dist/tools/workspace/tag-workflow.tool.js.map +1 -0
- package/dist/tracing/langsmith-tracing.d.ts +56 -0
- package/dist/tracing/langsmith-tracing.js +810 -0
- package/dist/tracing/langsmith-tracing.js.map +1 -0
- package/dist/types.d.ts +709 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/agent-tree.d.ts +3 -0
- package/dist/utils/agent-tree.js +23 -0
- package/dist/utils/agent-tree.js.map +1 -0
- package/dist/utils/format-timestamp.d.ts +1 -0
- package/dist/utils/format-timestamp.js +19 -0
- package/dist/utils/format-timestamp.js.map +1 -0
- package/dist/utils/stream-helpers.d.ts +15 -0
- package/dist/utils/stream-helpers.js +26 -0
- package/dist/utils/stream-helpers.js.map +1 -0
- package/dist/workflow-builder/extract-code.d.ts +5 -0
- package/dist/workflow-builder/extract-code.js +105 -0
- package/dist/workflow-builder/extract-code.js.map +1 -0
- package/dist/workflow-builder/index.d.ts +5 -0
- package/dist/workflow-builder/index.js +19 -0
- package/dist/workflow-builder/index.js.map +1 -0
- package/dist/workflow-builder/parse-validate.d.ts +6 -0
- package/dist/workflow-builder/parse-validate.js +49 -0
- package/dist/workflow-builder/parse-validate.js.map +1 -0
- package/dist/workflow-builder/patch-code.d.ts +14 -0
- package/dist/workflow-builder/patch-code.js +117 -0
- package/dist/workflow-builder/patch-code.js.map +1 -0
- package/dist/workflow-builder/sdk-prompt-sections.d.ts +4 -0
- package/dist/workflow-builder/sdk-prompt-sections.js +517 -0
- package/dist/workflow-builder/sdk-prompt-sections.js.map +1 -0
- package/dist/workflow-builder/types.d.ts +11 -0
- package/dist/workflow-builder/types.js +3 -0
- package/dist/workflow-builder/types.js.map +1 -0
- package/dist/workflow-loop/guidance.d.ts +5 -0
- package/dist/workflow-loop/guidance.js +37 -0
- package/dist/workflow-loop/guidance.js.map +1 -0
- package/dist/workflow-loop/index.d.ts +5 -0
- package/dist/workflow-loop/index.js +25 -0
- package/dist/workflow-loop/index.js.map +1 -0
- package/dist/workflow-loop/runtime.d.ts +8 -0
- package/dist/workflow-loop/runtime.js +40 -0
- package/dist/workflow-loop/runtime.js.map +1 -0
- package/dist/workflow-loop/workflow-loop-controller.d.ts +11 -0
- package/dist/workflow-loop/workflow-loop-controller.js +212 -0
- package/dist/workflow-loop/workflow-loop-controller.js.map +1 -0
- package/dist/workflow-loop/workflow-loop-state.d.ts +189 -0
- package/dist/workflow-loop/workflow-loop-state.js +77 -0
- package/dist/workflow-loop/workflow-loop-state.js.map +1 -0
- package/dist/workflow-loop/workflow-task-service.d.ts +13 -0
- package/dist/workflow-loop/workflow-task-service.js +35 -0
- package/dist/workflow-loop/workflow-task-service.js.map +1 -0
- package/dist/workspace/builder-sandbox-factory.d.ts +25 -0
- package/dist/workspace/builder-sandbox-factory.js +199 -0
- package/dist/workspace/builder-sandbox-factory.js.map +1 -0
- package/dist/workspace/create-workspace.d.ts +33 -0
- package/dist/workspace/create-workspace.js +56 -0
- package/dist/workspace/create-workspace.js.map +1 -0
- package/dist/workspace/daytona-filesystem.d.ts +25 -0
- package/dist/workspace/daytona-filesystem.js +109 -0
- package/dist/workspace/daytona-filesystem.js.map +1 -0
- package/dist/workspace/n8n-sandbox-client.d.ts +105 -0
- package/dist/workspace/n8n-sandbox-client.js +335 -0
- package/dist/workspace/n8n-sandbox-client.js.map +1 -0
- package/dist/workspace/n8n-sandbox-filesystem.d.ts +25 -0
- package/dist/workspace/n8n-sandbox-filesystem.js +128 -0
- package/dist/workspace/n8n-sandbox-filesystem.js.map +1 -0
- package/dist/workspace/n8n-sandbox-image-manager.d.ts +5 -0
- package/dist/workspace/n8n-sandbox-image-manager.js +25 -0
- package/dist/workspace/n8n-sandbox-image-manager.js.map +1 -0
- package/dist/workspace/n8n-sandbox-sandbox.d.ts +27 -0
- package/dist/workspace/n8n-sandbox-sandbox.js +103 -0
- package/dist/workspace/n8n-sandbox-sandbox.js.map +1 -0
- package/dist/workspace/sandbox-fs.d.ts +9 -0
- package/dist/workspace/sandbox-fs.js +43 -0
- package/dist/workspace/sandbox-fs.js.map +1 -0
- package/dist/workspace/sandbox-setup.d.ts +11 -0
- package/dist/workspace/sandbox-setup.js +139 -0
- package/dist/workspace/sandbox-setup.js.map +1 -0
- package/dist/workspace/snapshot-manager.d.ts +8 -0
- package/dist/workspace/snapshot-manager.js +31 -0
- package/dist/workspace/snapshot-manager.js.map +1 -0
- package/package.json +54 -0
|
@@ -0,0 +1,1552 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLlmStepTraceHooks = createLlmStepTraceHooks;
|
|
4
|
+
exports.executeResumableStream = executeResumableStream;
|
|
5
|
+
const working_memory_tracing_1 = require("./working-memory-tracing");
|
|
6
|
+
const map_chunk_1 = require("../stream/map-chunk");
|
|
7
|
+
const langsmith_tracing_1 = require("../tracing/langsmith-tracing");
|
|
8
|
+
const stream_helpers_1 = require("../utils/stream-helpers");
|
|
9
|
+
const MAX_TRACE_STRING_LENGTH = 2_000;
|
|
10
|
+
const MAX_TRACE_ARRAY_ITEMS = 20;
|
|
11
|
+
const MAX_TRACE_OBJECT_KEYS = 20;
|
|
12
|
+
const SYNTHETIC_TOOL_TRACE_NAMES = new Set(['updateWorkingMemory']);
|
|
13
|
+
function isRecord(value) {
|
|
14
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
function getFiniteNumber(value) {
|
|
17
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
18
|
+
}
|
|
19
|
+
function dedupeTags(tags) {
|
|
20
|
+
const values = tags.filter((tag) => Boolean(tag));
|
|
21
|
+
return values.length > 0 ? [...new Set(values)] : undefined;
|
|
22
|
+
}
|
|
23
|
+
function truncateTraceString(value) {
|
|
24
|
+
if (value.length <= MAX_TRACE_STRING_LENGTH) {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
return `${value.slice(0, MAX_TRACE_STRING_LENGTH)}…`;
|
|
28
|
+
}
|
|
29
|
+
function sanitizeTraceValue(value, depth = 0) {
|
|
30
|
+
if (value === null || value === undefined) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === 'string') {
|
|
34
|
+
return truncateTraceString(value);
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
if (typeof value === 'bigint') {
|
|
40
|
+
return value.toString();
|
|
41
|
+
}
|
|
42
|
+
if (value instanceof Date) {
|
|
43
|
+
return value.toISOString();
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
if (depth >= 3) {
|
|
47
|
+
return `[array(${value.length})]`;
|
|
48
|
+
}
|
|
49
|
+
return value
|
|
50
|
+
.slice(0, MAX_TRACE_ARRAY_ITEMS)
|
|
51
|
+
.map((entry) => sanitizeTraceValue(entry, depth + 1));
|
|
52
|
+
}
|
|
53
|
+
if (isRecord(value)) {
|
|
54
|
+
if (depth >= 3) {
|
|
55
|
+
return `[object ${Object.keys(value).length} keys]`;
|
|
56
|
+
}
|
|
57
|
+
const entries = Object.entries(value).slice(0, MAX_TRACE_OBJECT_KEYS);
|
|
58
|
+
const sanitized = {};
|
|
59
|
+
for (const [key, entryValue] of entries) {
|
|
60
|
+
sanitized[key] = sanitizeTraceValue(entryValue, depth + 1);
|
|
61
|
+
}
|
|
62
|
+
if (Object.keys(value).length > entries.length) {
|
|
63
|
+
sanitized.__truncatedKeys = Object.keys(value).length - entries.length;
|
|
64
|
+
}
|
|
65
|
+
return sanitized;
|
|
66
|
+
}
|
|
67
|
+
return truncateTraceString(Object.prototype.toString.call(value));
|
|
68
|
+
}
|
|
69
|
+
function sanitizeTracePayload(value) {
|
|
70
|
+
if (!isRecord(value)) {
|
|
71
|
+
return { value: sanitizeTraceValue(value) };
|
|
72
|
+
}
|
|
73
|
+
const sanitized = {};
|
|
74
|
+
for (const [key, entryValue] of Object.entries(value)) {
|
|
75
|
+
sanitized[key] = sanitizeTraceValue(entryValue);
|
|
76
|
+
}
|
|
77
|
+
return sanitized;
|
|
78
|
+
}
|
|
79
|
+
function normalizeModelMetadata(modelId) {
|
|
80
|
+
if (typeof modelId === 'string' && modelId.length > 0) {
|
|
81
|
+
const [provider, ...modelParts] = modelId.split('/');
|
|
82
|
+
return modelParts.length > 0
|
|
83
|
+
? { provider, modelName: modelParts.join('/') }
|
|
84
|
+
: { modelName: modelId };
|
|
85
|
+
}
|
|
86
|
+
if (isRecord(modelId) && typeof modelId.id === 'string') {
|
|
87
|
+
return normalizeModelMetadata(modelId.id);
|
|
88
|
+
}
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
function formatLlmRunName(model) {
|
|
92
|
+
if (model.provider && model.modelName) {
|
|
93
|
+
return `llm:${model.provider}/${model.modelName}`;
|
|
94
|
+
}
|
|
95
|
+
if (model.modelName) {
|
|
96
|
+
return `llm:${model.modelName}`;
|
|
97
|
+
}
|
|
98
|
+
return 'llm';
|
|
99
|
+
}
|
|
100
|
+
function parseMaybeJson(value) {
|
|
101
|
+
if (typeof value !== 'string') {
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(value);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function buildLlmInputPayload(request) {
|
|
112
|
+
const parsedRequest = isRecord(request) && 'body' in request ? parseMaybeJson(request.body) : request;
|
|
113
|
+
if (!isRecord(parsedRequest)) {
|
|
114
|
+
return { request: parsedRequest };
|
|
115
|
+
}
|
|
116
|
+
const selected = {};
|
|
117
|
+
for (const key of [
|
|
118
|
+
'messages',
|
|
119
|
+
'prompt',
|
|
120
|
+
'input',
|
|
121
|
+
'system',
|
|
122
|
+
'model',
|
|
123
|
+
'temperature',
|
|
124
|
+
'tools',
|
|
125
|
+
'tool_choice',
|
|
126
|
+
'toolChoice',
|
|
127
|
+
'stop',
|
|
128
|
+
'stop_sequences',
|
|
129
|
+
'stopSequences',
|
|
130
|
+
'max_tokens',
|
|
131
|
+
'max_output_tokens',
|
|
132
|
+
'max_completion_tokens',
|
|
133
|
+
]) {
|
|
134
|
+
if (parsedRequest[key] !== undefined) {
|
|
135
|
+
selected[key] =
|
|
136
|
+
key === 'messages' && Array.isArray(parsedRequest[key])
|
|
137
|
+
? (normalizeTraceMessages(parsedRequest[key]) ?? [])
|
|
138
|
+
: parsedRequest[key];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (Object.keys(selected).length > 0) {
|
|
142
|
+
return selected;
|
|
143
|
+
}
|
|
144
|
+
return { request: parsedRequest };
|
|
145
|
+
}
|
|
146
|
+
function extractInputTokenCount(usage) {
|
|
147
|
+
if (isRecord(usage.inputTokenDetails)) {
|
|
148
|
+
return (getFiniteNumber(usage.inputTokenDetails.noCacheTokens) ??
|
|
149
|
+
getFiniteNumber(usage.inputTokenDetails.no_cache_tokens) ??
|
|
150
|
+
undefined);
|
|
151
|
+
}
|
|
152
|
+
if (isRecord(usage.inputTokens)) {
|
|
153
|
+
return (getFiniteNumber(usage.inputTokens.noCache) ??
|
|
154
|
+
getFiniteNumber(usage.inputTokens.no_cache) ??
|
|
155
|
+
getFiniteNumber(usage.inputTokens.total) ??
|
|
156
|
+
undefined);
|
|
157
|
+
}
|
|
158
|
+
return getFiniteNumber(usage.inputTokens) ?? getFiniteNumber(usage.promptTokens) ?? undefined;
|
|
159
|
+
}
|
|
160
|
+
function extractOutputTokenCount(usage) {
|
|
161
|
+
if (isRecord(usage.outputTokens)) {
|
|
162
|
+
return (getFiniteNumber(usage.outputTokens.total) ??
|
|
163
|
+
getFiniteNumber(usage.outputTokens.text) ??
|
|
164
|
+
undefined);
|
|
165
|
+
}
|
|
166
|
+
return (getFiniteNumber(usage.outputTokens) ?? getFiniteNumber(usage.completionTokens) ?? undefined);
|
|
167
|
+
}
|
|
168
|
+
function extractReasoningTokenCount(usage) {
|
|
169
|
+
if (isRecord(usage.outputTokens)) {
|
|
170
|
+
return getFiniteNumber(usage.outputTokens.reasoning) ?? undefined;
|
|
171
|
+
}
|
|
172
|
+
return getFiniteNumber(usage.reasoningTokens) ?? undefined;
|
|
173
|
+
}
|
|
174
|
+
function extractCacheCreationTokens(raw) {
|
|
175
|
+
if (!isRecord(raw))
|
|
176
|
+
return undefined;
|
|
177
|
+
if (isRecord(raw.inputTokenDetails)) {
|
|
178
|
+
return (getFiniteNumber(raw.inputTokenDetails.cacheWriteTokens) ??
|
|
179
|
+
getFiniteNumber(raw.inputTokenDetails.cache_write_tokens) ??
|
|
180
|
+
undefined);
|
|
181
|
+
}
|
|
182
|
+
if (isRecord(raw.inputTokens)) {
|
|
183
|
+
return (getFiniteNumber(raw.inputTokens.cacheWrite) ??
|
|
184
|
+
getFiniteNumber(raw.inputTokens.cache_write) ??
|
|
185
|
+
getFiniteNumber(raw.inputTokens.cacheWriteTokens) ??
|
|
186
|
+
getFiniteNumber(raw.inputTokens.cache_write_tokens) ??
|
|
187
|
+
undefined);
|
|
188
|
+
}
|
|
189
|
+
return (getFiniteNumber(raw.cacheCreationInputTokens) ??
|
|
190
|
+
getFiniteNumber(raw.cache_creation_input_tokens) ??
|
|
191
|
+
getFiniteNumber(raw.input_cache_write) ??
|
|
192
|
+
undefined);
|
|
193
|
+
}
|
|
194
|
+
function extractCacheReadTokens(raw) {
|
|
195
|
+
if (!isRecord(raw))
|
|
196
|
+
return undefined;
|
|
197
|
+
if (isRecord(raw.inputTokenDetails)) {
|
|
198
|
+
return (getFiniteNumber(raw.inputTokenDetails.cacheReadTokens) ??
|
|
199
|
+
getFiniteNumber(raw.inputTokenDetails.cache_read_tokens) ??
|
|
200
|
+
undefined);
|
|
201
|
+
}
|
|
202
|
+
if (isRecord(raw.inputTokens)) {
|
|
203
|
+
return (getFiniteNumber(raw.inputTokens.cacheRead) ??
|
|
204
|
+
getFiniteNumber(raw.inputTokens.cache_read) ??
|
|
205
|
+
getFiniteNumber(raw.inputTokens.cacheReadTokens) ??
|
|
206
|
+
getFiniteNumber(raw.inputTokens.cache_read_tokens) ??
|
|
207
|
+
undefined);
|
|
208
|
+
}
|
|
209
|
+
return (getFiniteNumber(raw.cachedInputTokens) ??
|
|
210
|
+
getFiniteNumber(raw.cacheRead) ??
|
|
211
|
+
getFiniteNumber(raw.cache_read_input_tokens) ??
|
|
212
|
+
getFiniteNumber(raw.input_cache_read) ??
|
|
213
|
+
undefined);
|
|
214
|
+
}
|
|
215
|
+
function extractUsageFromProviderMetadata(providerMetadata) {
|
|
216
|
+
if (!isRecord(providerMetadata)) {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
if (isRecord(providerMetadata.usage)) {
|
|
220
|
+
return providerMetadata.usage;
|
|
221
|
+
}
|
|
222
|
+
for (const value of Object.values(providerMetadata)) {
|
|
223
|
+
if (!isRecord(value)) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (isRecord(value.usage)) {
|
|
227
|
+
return value.usage;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
function mergeUsageMetadata(primary, fallback) {
|
|
233
|
+
if (!primary) {
|
|
234
|
+
return fallback;
|
|
235
|
+
}
|
|
236
|
+
if (!fallback) {
|
|
237
|
+
return primary;
|
|
238
|
+
}
|
|
239
|
+
const merged = { ...primary };
|
|
240
|
+
for (const key of ['input_tokens', 'output_tokens', 'total_tokens']) {
|
|
241
|
+
if (merged[key] === undefined && fallback[key] !== undefined) {
|
|
242
|
+
merged[key] = fallback[key];
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const mergedInputTokenDetails = {};
|
|
246
|
+
if (isRecord(fallback.input_token_details)) {
|
|
247
|
+
Object.assign(mergedInputTokenDetails, fallback.input_token_details);
|
|
248
|
+
}
|
|
249
|
+
if (isRecord(primary.input_token_details)) {
|
|
250
|
+
Object.assign(mergedInputTokenDetails, primary.input_token_details);
|
|
251
|
+
}
|
|
252
|
+
if (Object.keys(mergedInputTokenDetails).length > 0) {
|
|
253
|
+
merged.input_token_details = mergedInputTokenDetails;
|
|
254
|
+
}
|
|
255
|
+
const mergedOutputTokenDetails = {};
|
|
256
|
+
if (isRecord(fallback.output_token_details)) {
|
|
257
|
+
Object.assign(mergedOutputTokenDetails, fallback.output_token_details);
|
|
258
|
+
}
|
|
259
|
+
if (isRecord(primary.output_token_details)) {
|
|
260
|
+
Object.assign(mergedOutputTokenDetails, primary.output_token_details);
|
|
261
|
+
}
|
|
262
|
+
if (Object.keys(mergedOutputTokenDetails).length > 0) {
|
|
263
|
+
merged.output_token_details = mergedOutputTokenDetails;
|
|
264
|
+
}
|
|
265
|
+
return merged;
|
|
266
|
+
}
|
|
267
|
+
function buildUsageMetadata(usage, providerMetadata) {
|
|
268
|
+
const usageRecord = isRecord(usage) ? usage : undefined;
|
|
269
|
+
const providerUsage = extractUsageFromProviderMetadata(providerMetadata);
|
|
270
|
+
if (!usageRecord && !providerUsage) {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
const inputTokens = (usageRecord ? extractInputTokenCount(usageRecord) : undefined) ??
|
|
274
|
+
(providerUsage ? extractInputTokenCount(providerUsage) : undefined);
|
|
275
|
+
const outputTokens = (usageRecord ? extractOutputTokenCount(usageRecord) : undefined) ??
|
|
276
|
+
(providerUsage ? extractOutputTokenCount(providerUsage) : undefined);
|
|
277
|
+
const totalTokens = inputTokens !== undefined || outputTokens !== undefined
|
|
278
|
+
? (inputTokens ?? 0) + (outputTokens ?? 0)
|
|
279
|
+
: ((usageRecord ? getFiniteNumber(usageRecord.totalTokens) : undefined) ??
|
|
280
|
+
(providerUsage ? getFiniteNumber(providerUsage.totalTokens) : undefined));
|
|
281
|
+
const cachedInputTokens = (usageRecord ? getFiniteNumber(usageRecord.cachedInputTokens) : undefined) ??
|
|
282
|
+
(usageRecord ? extractCacheReadTokens(usageRecord) : undefined) ??
|
|
283
|
+
(usageRecord ? extractCacheReadTokens(usageRecord.raw) : undefined) ??
|
|
284
|
+
(providerUsage ? extractCacheReadTokens(providerUsage) : undefined) ??
|
|
285
|
+
(providerUsage ? extractCacheReadTokens(providerUsage.raw) : undefined);
|
|
286
|
+
const cacheCreationTokens = (usageRecord ? extractCacheCreationTokens(usageRecord) : undefined) ??
|
|
287
|
+
(usageRecord ? extractCacheCreationTokens(usageRecord.raw) : undefined) ??
|
|
288
|
+
(providerUsage ? extractCacheCreationTokens(providerUsage) : undefined) ??
|
|
289
|
+
(providerUsage ? extractCacheCreationTokens(providerUsage.raw) : undefined);
|
|
290
|
+
const reasoningTokens = (usageRecord ? extractReasoningTokenCount(usageRecord) : undefined) ??
|
|
291
|
+
(providerUsage ? extractReasoningTokenCount(providerUsage) : undefined);
|
|
292
|
+
const usageMetadata = {};
|
|
293
|
+
if (inputTokens !== undefined) {
|
|
294
|
+
usageMetadata.input_tokens = inputTokens;
|
|
295
|
+
}
|
|
296
|
+
if (outputTokens !== undefined) {
|
|
297
|
+
usageMetadata.output_tokens = outputTokens;
|
|
298
|
+
}
|
|
299
|
+
if (totalTokens !== undefined) {
|
|
300
|
+
usageMetadata.total_tokens = totalTokens;
|
|
301
|
+
}
|
|
302
|
+
const inputTokenDetails = {};
|
|
303
|
+
if (cachedInputTokens !== undefined) {
|
|
304
|
+
inputTokenDetails.cache_read = cachedInputTokens;
|
|
305
|
+
}
|
|
306
|
+
if (cacheCreationTokens !== undefined) {
|
|
307
|
+
inputTokenDetails.cache_creation = cacheCreationTokens;
|
|
308
|
+
}
|
|
309
|
+
if (Object.keys(inputTokenDetails).length > 0) {
|
|
310
|
+
usageMetadata.input_token_details = inputTokenDetails;
|
|
311
|
+
}
|
|
312
|
+
if (reasoningTokens !== undefined) {
|
|
313
|
+
usageMetadata.output_token_details = { reasoning: reasoningTokens };
|
|
314
|
+
}
|
|
315
|
+
return Object.keys(usageMetadata).length > 0
|
|
316
|
+
? mergeUsageMetadata(usageMetadata, providerUsage ? buildUsageMetadata(providerUsage) : undefined)
|
|
317
|
+
: providerUsage
|
|
318
|
+
? buildUsageMetadata(providerUsage)
|
|
319
|
+
: undefined;
|
|
320
|
+
}
|
|
321
|
+
function summarizeUsageLikeValue(value) {
|
|
322
|
+
if (!isRecord(value)) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const summary = {};
|
|
326
|
+
for (const key of [
|
|
327
|
+
'promptTokens',
|
|
328
|
+
'completionTokens',
|
|
329
|
+
'totalTokens',
|
|
330
|
+
'cachedInputTokens',
|
|
331
|
+
'reasoningTokens',
|
|
332
|
+
'inputTokens',
|
|
333
|
+
'outputTokens',
|
|
334
|
+
'inputTokenDetails',
|
|
335
|
+
'outputTokenDetails',
|
|
336
|
+
'input_tokens',
|
|
337
|
+
'output_tokens',
|
|
338
|
+
'cache_creation_input_tokens',
|
|
339
|
+
'cache_read_input_tokens',
|
|
340
|
+
'cacheCreationInputTokens',
|
|
341
|
+
'cacheReadInputTokens',
|
|
342
|
+
'iterations',
|
|
343
|
+
]) {
|
|
344
|
+
if (value[key] !== undefined) {
|
|
345
|
+
summary[key] = sanitizeTraceValue(value[key]);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
for (const nestedKey of ['usage', 'raw', 'rawUsage', 'anthropic', 'openai']) {
|
|
349
|
+
const nestedSummary = summarizeUsageLikeValue(value[nestedKey]);
|
|
350
|
+
if (nestedSummary) {
|
|
351
|
+
summary[nestedKey] = nestedSummary;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return Object.keys(summary).length > 0 ? summary : undefined;
|
|
355
|
+
}
|
|
356
|
+
function buildLlmUsageDebug(record, stepResult) {
|
|
357
|
+
const usageDebug = {};
|
|
358
|
+
const stepUsage = summarizeUsageLikeValue(stepResult?.usage);
|
|
359
|
+
if (stepUsage) {
|
|
360
|
+
usageDebug.step_usage = stepUsage;
|
|
361
|
+
}
|
|
362
|
+
const recordUsage = summarizeUsageLikeValue(record.usage);
|
|
363
|
+
if (recordUsage) {
|
|
364
|
+
usageDebug.record_usage = recordUsage;
|
|
365
|
+
}
|
|
366
|
+
const streamUsage = summarizeUsageLikeValue(record.sourceUsage);
|
|
367
|
+
if (streamUsage) {
|
|
368
|
+
usageDebug.stream_usage = streamUsage;
|
|
369
|
+
}
|
|
370
|
+
const streamTotalUsage = summarizeUsageLikeValue(record.sourceTotalUsage);
|
|
371
|
+
if (streamTotalUsage) {
|
|
372
|
+
usageDebug.stream_total_usage = streamTotalUsage;
|
|
373
|
+
}
|
|
374
|
+
const stepProviderMetadata = summarizeUsageLikeValue(stepResult?.providerMetadata);
|
|
375
|
+
if (stepProviderMetadata) {
|
|
376
|
+
usageDebug.step_provider_metadata = stepProviderMetadata;
|
|
377
|
+
}
|
|
378
|
+
const providerMetadata = summarizeUsageLikeValue(record.providerMetadata);
|
|
379
|
+
if (providerMetadata) {
|
|
380
|
+
usageDebug.provider_metadata = providerMetadata;
|
|
381
|
+
}
|
|
382
|
+
const stepResponse = summarizeUsageLikeValue(stepResult?.response);
|
|
383
|
+
if (stepResponse) {
|
|
384
|
+
usageDebug.step_response = stepResponse;
|
|
385
|
+
}
|
|
386
|
+
const response = summarizeUsageLikeValue(record.response);
|
|
387
|
+
if (response) {
|
|
388
|
+
usageDebug.response = response;
|
|
389
|
+
}
|
|
390
|
+
return Object.keys(usageDebug).length > 0 ? sanitizeTracePayload(usageDebug) : undefined;
|
|
391
|
+
}
|
|
392
|
+
async function resolveUsagePromise(usage) {
|
|
393
|
+
if (!usage) {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
return await usage.then((value) => value, () => undefined);
|
|
397
|
+
}
|
|
398
|
+
async function resolveSegmentUsage(source) {
|
|
399
|
+
const [lastStepUsage, totalUsage] = await Promise.all([
|
|
400
|
+
resolveUsagePromise(source.usage),
|
|
401
|
+
resolveUsagePromise(source.totalUsage),
|
|
402
|
+
]);
|
|
403
|
+
return { lastStepUsage, totalUsage };
|
|
404
|
+
}
|
|
405
|
+
function maybeBackfillRecordUsageFromSegment(record, records, usage) {
|
|
406
|
+
if (usage.lastStepUsage !== undefined) {
|
|
407
|
+
record.sourceUsage = usage.lastStepUsage;
|
|
408
|
+
}
|
|
409
|
+
if (usage.totalUsage !== undefined) {
|
|
410
|
+
record.sourceTotalUsage = usage.totalUsage;
|
|
411
|
+
}
|
|
412
|
+
if (record.usage !== undefined) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const isLastRecord = records[records.length - 1] === record;
|
|
416
|
+
if (isLastRecord && usage.lastStepUsage !== undefined) {
|
|
417
|
+
record.usage = usage.lastStepUsage;
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (records.length === 1 && usage.totalUsage !== undefined) {
|
|
421
|
+
record.usage = usage.totalUsage;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function toTraceObject(value) {
|
|
425
|
+
if (isRecord(value)) {
|
|
426
|
+
return value;
|
|
427
|
+
}
|
|
428
|
+
return { value };
|
|
429
|
+
}
|
|
430
|
+
function extractResponseMessages(value) {
|
|
431
|
+
return isRecord(value) && Array.isArray(value.messages) ? value.messages : undefined;
|
|
432
|
+
}
|
|
433
|
+
function extractTextParts(value) {
|
|
434
|
+
if (typeof value === 'string') {
|
|
435
|
+
return [value];
|
|
436
|
+
}
|
|
437
|
+
if (Array.isArray(value)) {
|
|
438
|
+
return value.flatMap((entry) => extractTextParts(entry));
|
|
439
|
+
}
|
|
440
|
+
if (!isRecord(value)) {
|
|
441
|
+
return [];
|
|
442
|
+
}
|
|
443
|
+
if (value.type === 'text') {
|
|
444
|
+
if (typeof value.text === 'string') {
|
|
445
|
+
return [value.text];
|
|
446
|
+
}
|
|
447
|
+
if (isRecord(value.text) && typeof value.text.value === 'string') {
|
|
448
|
+
return [value.text.value];
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if ('content' in value) {
|
|
452
|
+
if (typeof value.content === 'string') {
|
|
453
|
+
return [value.content];
|
|
454
|
+
}
|
|
455
|
+
if (Array.isArray(value.content)) {
|
|
456
|
+
return value.content.flatMap((entry) => extractTextParts(entry));
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (Array.isArray(value.parts)) {
|
|
460
|
+
return value.parts.flatMap((entry) => extractTextParts(entry));
|
|
461
|
+
}
|
|
462
|
+
return [];
|
|
463
|
+
}
|
|
464
|
+
function extractTraceToolCallsFromMessage(message) {
|
|
465
|
+
if (!isRecord(message)) {
|
|
466
|
+
return [];
|
|
467
|
+
}
|
|
468
|
+
const toolCalls = [];
|
|
469
|
+
const pushToolCall = (value) => {
|
|
470
|
+
if (!isRecord(value)) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
toolCalls.push({
|
|
474
|
+
...(typeof value.toolCallId === 'string' ? { toolCallId: value.toolCallId } : {}),
|
|
475
|
+
...(typeof value.toolName === 'string' ? { toolName: value.toolName } : {}),
|
|
476
|
+
});
|
|
477
|
+
};
|
|
478
|
+
if (Array.isArray(message.tool_calls)) {
|
|
479
|
+
for (const toolCall of message.tool_calls) {
|
|
480
|
+
pushToolCall(toolCall);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
const content = message.content;
|
|
484
|
+
if (Array.isArray(content)) {
|
|
485
|
+
for (const part of content) {
|
|
486
|
+
if (!isRecord(part)) {
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
if (part.type === 'tool-call') {
|
|
490
|
+
pushToolCall(part);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (isRecord(content) && Array.isArray(content.parts)) {
|
|
495
|
+
for (const part of content.parts) {
|
|
496
|
+
if (isRecord(part) && part.type === 'tool-invocation' && isRecord(part.toolInvocation)) {
|
|
497
|
+
pushToolCall(part.toolInvocation);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return toolCalls;
|
|
502
|
+
}
|
|
503
|
+
function summarizeRequestedTools(toolCalls) {
|
|
504
|
+
const summaries = toolCalls
|
|
505
|
+
.map((toolCall) => ({
|
|
506
|
+
...(typeof toolCall.toolCallId === 'string' ? { toolCallId: toolCall.toolCallId } : {}),
|
|
507
|
+
...(typeof toolCall.toolName === 'string' ? { toolName: toolCall.toolName } : {}),
|
|
508
|
+
}))
|
|
509
|
+
.filter((toolCall) => Object.keys(toolCall).length > 0);
|
|
510
|
+
return summaries.length > 0 ? summaries : undefined;
|
|
511
|
+
}
|
|
512
|
+
function buildToolSummaryText(toolCalls) {
|
|
513
|
+
const toolNames = [
|
|
514
|
+
...new Set(toolCalls
|
|
515
|
+
.map((toolCall) => (typeof toolCall.toolName === 'string' ? toolCall.toolName : undefined))
|
|
516
|
+
.filter((toolName) => toolName !== undefined)),
|
|
517
|
+
];
|
|
518
|
+
if (toolNames.length === 0) {
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
return `Calling tools: ${toolNames.join(', ')}`;
|
|
522
|
+
}
|
|
523
|
+
function normalizeTraceMessage(message) {
|
|
524
|
+
if (!isRecord(message) || typeof message.role !== 'string') {
|
|
525
|
+
return undefined;
|
|
526
|
+
}
|
|
527
|
+
if (message.role === 'tool') {
|
|
528
|
+
return undefined;
|
|
529
|
+
}
|
|
530
|
+
const textContent = extractTextParts(message.content).join('').trim();
|
|
531
|
+
const toolSummaryText = buildToolSummaryText(extractTraceToolCallsFromMessage(message));
|
|
532
|
+
const content = textContent && toolSummaryText
|
|
533
|
+
? `${textContent}\n\n[${toolSummaryText}]`
|
|
534
|
+
: textContent || (toolSummaryText ? `[${toolSummaryText}]` : undefined);
|
|
535
|
+
if (!content) {
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
...(typeof message.id === 'string' ? { id: message.id } : {}),
|
|
540
|
+
role: message.role,
|
|
541
|
+
content,
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
function normalizeTraceMessages(messages) {
|
|
545
|
+
if (!Array.isArray(messages)) {
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
const normalized = messages
|
|
549
|
+
.map((message) => normalizeTraceMessage(message))
|
|
550
|
+
.filter((message) => message !== undefined);
|
|
551
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
552
|
+
}
|
|
553
|
+
function buildAssistantChoice(responseMessages, record) {
|
|
554
|
+
const assistantMessage = responseMessages?.find((message) => isRecord(message) && message.role === 'assistant');
|
|
555
|
+
if (assistantMessage) {
|
|
556
|
+
return { message: assistantMessage };
|
|
557
|
+
}
|
|
558
|
+
const toolSummaryText = buildToolSummaryText(record.toolCalls);
|
|
559
|
+
const content = record.textParts.join('');
|
|
560
|
+
if (!content && !toolSummaryText) {
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
const message = { role: 'assistant' };
|
|
564
|
+
if (content && toolSummaryText) {
|
|
565
|
+
message.content = `${content}\n\n[${toolSummaryText}]`;
|
|
566
|
+
}
|
|
567
|
+
else if (content) {
|
|
568
|
+
message.content = content;
|
|
569
|
+
}
|
|
570
|
+
else if (toolSummaryText) {
|
|
571
|
+
message.content = `[${toolSummaryText}]`;
|
|
572
|
+
}
|
|
573
|
+
return { message };
|
|
574
|
+
}
|
|
575
|
+
function buildLlmOutputs(record, stepResult) {
|
|
576
|
+
const rawResponseMessages = extractResponseMessages(stepResult?.response) ?? extractResponseMessages(record.response);
|
|
577
|
+
const responseMessages = normalizeTraceMessages(rawResponseMessages);
|
|
578
|
+
const usageMetadata = buildUsageMetadata(stepResult?.usage ?? record.usage, stepResult?.providerMetadata ?? record.providerMetadata);
|
|
579
|
+
const usageDebug = buildLlmUsageDebug(record, stepResult);
|
|
580
|
+
const outputs = {};
|
|
581
|
+
const choice = buildAssistantChoice(responseMessages, record);
|
|
582
|
+
const messages = responseMessages ?? (choice && isRecord(choice.message) ? [choice.message] : undefined);
|
|
583
|
+
if (choice) {
|
|
584
|
+
outputs.choices = [choice];
|
|
585
|
+
}
|
|
586
|
+
if (messages) {
|
|
587
|
+
outputs.messages = messages;
|
|
588
|
+
}
|
|
589
|
+
const requestedTools = summarizeRequestedTools(record.toolCalls);
|
|
590
|
+
if (requestedTools) {
|
|
591
|
+
outputs.requested_tools = requestedTools;
|
|
592
|
+
}
|
|
593
|
+
const reasoningText = record.reasoningParts.join('') ||
|
|
594
|
+
(typeof stepResult?.reasoning === 'string'
|
|
595
|
+
? stepResult.reasoning
|
|
596
|
+
: Array.isArray(stepResult?.reasoning)
|
|
597
|
+
? stepResult.reasoning
|
|
598
|
+
.map((entry) => isRecord(entry) && typeof entry.text === 'string' ? entry.text : undefined)
|
|
599
|
+
.filter((entry) => entry !== undefined)
|
|
600
|
+
.join('')
|
|
601
|
+
: undefined);
|
|
602
|
+
if (reasoningText) {
|
|
603
|
+
outputs.reasoning = reasoningText;
|
|
604
|
+
}
|
|
605
|
+
if (record.finishReason || stepResult?.finishReason) {
|
|
606
|
+
outputs.finish_reason = stepResult?.finishReason ?? record.finishReason;
|
|
607
|
+
}
|
|
608
|
+
if (usageMetadata) {
|
|
609
|
+
outputs.usage_metadata = usageMetadata;
|
|
610
|
+
}
|
|
611
|
+
if (usageDebug) {
|
|
612
|
+
outputs.usage_debug = usageDebug;
|
|
613
|
+
}
|
|
614
|
+
return outputs;
|
|
615
|
+
}
|
|
616
|
+
function buildLlmMetadata(record, stepResult) {
|
|
617
|
+
const metadata = {
|
|
618
|
+
step_message_id: record.messageId,
|
|
619
|
+
final_status: 'completed',
|
|
620
|
+
...(record.model.provider ? { ls_provider: record.model.provider } : {}),
|
|
621
|
+
...(record.model.modelName ? { ls_model_name: record.model.modelName } : {}),
|
|
622
|
+
...(record.finishReason || stepResult?.finishReason
|
|
623
|
+
? { finish_reason: stepResult?.finishReason ?? record.finishReason }
|
|
624
|
+
: {}),
|
|
625
|
+
};
|
|
626
|
+
const usageMetadata = buildUsageMetadata(stepResult?.usage ?? record.usage, stepResult?.providerMetadata ?? record.providerMetadata);
|
|
627
|
+
if (usageMetadata) {
|
|
628
|
+
metadata.usage_metadata = usageMetadata;
|
|
629
|
+
}
|
|
630
|
+
return metadata;
|
|
631
|
+
}
|
|
632
|
+
async function finishRunTree(runTree, options) {
|
|
633
|
+
await runTree.end(options.outputs, options.error, options.endTime ?? Date.now(), options.metadata);
|
|
634
|
+
await runTree.patchRun();
|
|
635
|
+
}
|
|
636
|
+
function getChunkPayload(chunk) {
|
|
637
|
+
if (!isRecord(chunk)) {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
return isRecord(chunk.payload) ? chunk.payload : chunk;
|
|
641
|
+
}
|
|
642
|
+
function isMemoryToolTrace(toolName) {
|
|
643
|
+
return toolName === 'updateWorkingMemory';
|
|
644
|
+
}
|
|
645
|
+
function summarizeWorkingMemoryInput(memory) {
|
|
646
|
+
return {
|
|
647
|
+
memory_chars: memory.length,
|
|
648
|
+
memory_lines: memory.length > 0 ? memory.split('\n').length : 0,
|
|
649
|
+
memory_preview: memory.length > 0 ? truncateTraceString(memory.slice(0, 400)) : '',
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function buildSyntheticToolInputs(toolCallId, toolName, args) {
|
|
653
|
+
if (isMemoryToolTrace(toolName) && isRecord(args) && typeof args.memory === 'string') {
|
|
654
|
+
return sanitizeTracePayload({
|
|
655
|
+
toolCallId,
|
|
656
|
+
args: summarizeWorkingMemoryInput(args.memory),
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
return sanitizeTracePayload({
|
|
660
|
+
toolCallId,
|
|
661
|
+
args,
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
function shouldCreateSyntheticToolTrace(payload) {
|
|
665
|
+
const toolName = typeof payload.toolName === 'string' ? payload.toolName : '';
|
|
666
|
+
return (toolName.startsWith('mastra_') ||
|
|
667
|
+
SYNTHETIC_TOOL_TRACE_NAMES.has(toolName) ||
|
|
668
|
+
payload.providerExecuted === true ||
|
|
669
|
+
payload.dynamic === true);
|
|
670
|
+
}
|
|
671
|
+
function resolveActorParentRun(parentRun) {
|
|
672
|
+
let current = parentRun;
|
|
673
|
+
while (current) {
|
|
674
|
+
if (current.run_type !== 'llm' && current.run_type !== 'tool') {
|
|
675
|
+
return current;
|
|
676
|
+
}
|
|
677
|
+
const next = Reflect.get(current, 'parent_run');
|
|
678
|
+
current = next instanceof Object ? next : undefined;
|
|
679
|
+
}
|
|
680
|
+
return parentRun;
|
|
681
|
+
}
|
|
682
|
+
async function startSyntheticToolGroupRun(parentRun, toolName) {
|
|
683
|
+
if (!isMemoryToolTrace(toolName)) {
|
|
684
|
+
return undefined;
|
|
685
|
+
}
|
|
686
|
+
const actorParentRun = resolveActorParentRun(parentRun);
|
|
687
|
+
const groupRunTree = actorParentRun.createChild({
|
|
688
|
+
name: 'internal_state',
|
|
689
|
+
run_type: 'chain',
|
|
690
|
+
tags: dedupeTags([...(actorParentRun.tags ?? []), 'internal', 'memory']),
|
|
691
|
+
metadata: {
|
|
692
|
+
...(actorParentRun.metadata ?? {}),
|
|
693
|
+
internal_state: true,
|
|
694
|
+
tool_name: toolName,
|
|
695
|
+
},
|
|
696
|
+
inputs: sanitizeTracePayload({
|
|
697
|
+
tool_name: toolName,
|
|
698
|
+
}),
|
|
699
|
+
});
|
|
700
|
+
await groupRunTree.postRun();
|
|
701
|
+
return groupRunTree;
|
|
702
|
+
}
|
|
703
|
+
async function startSyntheticToolTrace(chunk, records) {
|
|
704
|
+
if (!isRecord(chunk) || chunk.type !== 'tool-call') {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
const payload = getChunkPayload(chunk);
|
|
708
|
+
if (!payload || !shouldCreateSyntheticToolTrace(payload)) {
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const toolCallId = typeof payload.toolCallId === 'string' ? payload.toolCallId : '';
|
|
712
|
+
const toolName = typeof payload.toolName === 'string' ? payload.toolName : '';
|
|
713
|
+
if (!toolCallId || !toolName || records.has(toolCallId)) {
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const parentRun = (0, langsmith_tracing_1.getTraceParentRun)();
|
|
717
|
+
if (!parentRun) {
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
const groupRunTree = await startSyntheticToolGroupRun(parentRun, toolName);
|
|
721
|
+
const toolParentRun = groupRunTree ?? parentRun;
|
|
722
|
+
const runTree = toolParentRun.createChild({
|
|
723
|
+
name: `tool:${toolName}`,
|
|
724
|
+
run_type: 'tool',
|
|
725
|
+
tags: dedupeTags([
|
|
726
|
+
...(toolParentRun.tags ?? []),
|
|
727
|
+
'tool',
|
|
728
|
+
...(toolName.startsWith('mastra_') ? ['native-tool'] : []),
|
|
729
|
+
...(isMemoryToolTrace(toolName) ? ['memory', 'internal'] : []),
|
|
730
|
+
]),
|
|
731
|
+
metadata: {
|
|
732
|
+
...(toolParentRun.metadata ?? {}),
|
|
733
|
+
tool_name: toolName,
|
|
734
|
+
synthetic_tool_trace: true,
|
|
735
|
+
...(isMemoryToolTrace(toolName) ? { memory_tool: true } : {}),
|
|
736
|
+
...(payload.providerExecuted === true ? { provider_executed: true } : {}),
|
|
737
|
+
...(payload.dynamic === true ? { dynamic_tool: true } : {}),
|
|
738
|
+
},
|
|
739
|
+
inputs: buildSyntheticToolInputs(toolCallId, toolName, payload.args),
|
|
740
|
+
});
|
|
741
|
+
await runTree.postRun();
|
|
742
|
+
records.set(toolCallId, {
|
|
743
|
+
toolCallId,
|
|
744
|
+
toolName,
|
|
745
|
+
groupRunTree,
|
|
746
|
+
runTree,
|
|
747
|
+
finished: false,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
async function finishSyntheticToolTrace(chunk, records) {
|
|
751
|
+
if (!isRecord(chunk) || (chunk.type !== 'tool-result' && chunk.type !== 'tool-error')) {
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const payload = getChunkPayload(chunk);
|
|
755
|
+
if (!payload) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
const toolCallId = typeof payload.toolCallId === 'string' ? payload.toolCallId : '';
|
|
759
|
+
if (!toolCallId) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
if (!records.has(toolCallId)) {
|
|
763
|
+
if (!shouldCreateSyntheticToolTrace(payload)) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
await startSyntheticToolTrace({
|
|
767
|
+
type: 'tool-call',
|
|
768
|
+
payload,
|
|
769
|
+
}, records);
|
|
770
|
+
}
|
|
771
|
+
const record = records.get(toolCallId);
|
|
772
|
+
if (!record || record.finished) {
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
record.finished = true;
|
|
776
|
+
await finishRunTree(record.runTree, {
|
|
777
|
+
outputs: sanitizeTracePayload({
|
|
778
|
+
result: payload.result,
|
|
779
|
+
}),
|
|
780
|
+
...(payload.isError === true
|
|
781
|
+
? {
|
|
782
|
+
error: typeof payload.result === 'string'
|
|
783
|
+
? payload.result
|
|
784
|
+
: typeof payload.error === 'string'
|
|
785
|
+
? payload.error
|
|
786
|
+
: 'Tool execution failed',
|
|
787
|
+
}
|
|
788
|
+
: {}),
|
|
789
|
+
metadata: {
|
|
790
|
+
final_status: payload.isError === true ? 'error' : 'completed',
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
if (record.groupRunTree) {
|
|
794
|
+
await finishRunTree(record.groupRunTree, {
|
|
795
|
+
outputs: sanitizeTracePayload({
|
|
796
|
+
tool_name: record.toolName,
|
|
797
|
+
}),
|
|
798
|
+
...(payload.isError === true
|
|
799
|
+
? {
|
|
800
|
+
error: typeof payload.result === 'string'
|
|
801
|
+
? payload.result
|
|
802
|
+
: typeof payload.error === 'string'
|
|
803
|
+
? payload.error
|
|
804
|
+
: 'Tool execution failed',
|
|
805
|
+
}
|
|
806
|
+
: {}),
|
|
807
|
+
metadata: {
|
|
808
|
+
final_status: payload.isError === true ? 'error' : 'completed',
|
|
809
|
+
internal_state: true,
|
|
810
|
+
},
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
async function finalizeSyntheticToolTraces(records, options) {
|
|
815
|
+
for (const record of records.values()) {
|
|
816
|
+
if (record.finished) {
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
record.finished = true;
|
|
820
|
+
await finishRunTree(record.runTree, {
|
|
821
|
+
outputs: sanitizeTracePayload({
|
|
822
|
+
status: options?.status ?? 'completed',
|
|
823
|
+
}),
|
|
824
|
+
...(options?.error ? { error: options.error } : {}),
|
|
825
|
+
metadata: {
|
|
826
|
+
final_status: options?.status ?? 'completed',
|
|
827
|
+
},
|
|
828
|
+
});
|
|
829
|
+
if (record.groupRunTree) {
|
|
830
|
+
await finishRunTree(record.groupRunTree, {
|
|
831
|
+
outputs: sanitizeTracePayload({
|
|
832
|
+
tool_name: record.toolName,
|
|
833
|
+
status: options?.status ?? 'completed',
|
|
834
|
+
}),
|
|
835
|
+
...(options?.error ? { error: options.error } : {}),
|
|
836
|
+
metadata: {
|
|
837
|
+
final_status: options?.status ?? 'completed',
|
|
838
|
+
internal_state: true,
|
|
839
|
+
},
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
async function startLlmStepTrace(parentRun, messageId, request, stepNumber) {
|
|
845
|
+
const resolvedParentRun = parentRun ?? (0, langsmith_tracing_1.getTraceParentRun)();
|
|
846
|
+
if (!resolvedParentRun) {
|
|
847
|
+
return undefined;
|
|
848
|
+
}
|
|
849
|
+
const inputs = buildLlmInputPayload(request);
|
|
850
|
+
const model = normalizeModelMetadata(resolvedParentRun.metadata?.model_id);
|
|
851
|
+
const runTree = resolvedParentRun.createChild({
|
|
852
|
+
name: formatLlmRunName(model),
|
|
853
|
+
run_type: 'llm',
|
|
854
|
+
tags: dedupeTags([...(resolvedParentRun.tags ?? []), 'llm']),
|
|
855
|
+
metadata: {
|
|
856
|
+
...(resolvedParentRun.metadata ?? {}),
|
|
857
|
+
step_message_id: messageId,
|
|
858
|
+
...(typeof stepNumber === 'number' ? { step_number: stepNumber + 1 } : {}),
|
|
859
|
+
...(model.provider ? { ls_provider: model.provider } : {}),
|
|
860
|
+
...(model.modelName ? { ls_model_name: model.modelName } : {}),
|
|
861
|
+
},
|
|
862
|
+
inputs,
|
|
863
|
+
});
|
|
864
|
+
await runTree.postRun();
|
|
865
|
+
return {
|
|
866
|
+
messageId,
|
|
867
|
+
stepNumber,
|
|
868
|
+
runTree,
|
|
869
|
+
model,
|
|
870
|
+
inputs,
|
|
871
|
+
textParts: [],
|
|
872
|
+
reasoningParts: [],
|
|
873
|
+
toolCalls: [],
|
|
874
|
+
toolResults: [],
|
|
875
|
+
request,
|
|
876
|
+
recordedFirstToken: false,
|
|
877
|
+
finished: false,
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
function findActiveStepRecord(records, messageId) {
|
|
881
|
+
if (messageId) {
|
|
882
|
+
return records.find((record) => record.messageId === messageId && !record.finished);
|
|
883
|
+
}
|
|
884
|
+
for (let index = records.length - 1; index >= 0; index--) {
|
|
885
|
+
if (!records[index].finished) {
|
|
886
|
+
return records[index];
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return undefined;
|
|
890
|
+
}
|
|
891
|
+
function recordFirstTokenEvent(record) {
|
|
892
|
+
if (record.recordedFirstToken) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
record.runTree.addEvent({ name: 'new_token', time: new Date().toISOString() });
|
|
896
|
+
record.recordedFirstToken = true;
|
|
897
|
+
}
|
|
898
|
+
function updateStepRecordFromChunk(chunk, records) {
|
|
899
|
+
if (!isRecord(chunk) || typeof chunk.type !== 'string') {
|
|
900
|
+
return undefined;
|
|
901
|
+
}
|
|
902
|
+
if (chunk.type === 'step-start' && typeof chunk.messageId === 'string') {
|
|
903
|
+
return undefined;
|
|
904
|
+
}
|
|
905
|
+
const record = findActiveStepRecord(records);
|
|
906
|
+
if (!record) {
|
|
907
|
+
return undefined;
|
|
908
|
+
}
|
|
909
|
+
const payload = isRecord(chunk.payload) ? chunk.payload : chunk;
|
|
910
|
+
if ((chunk.type === 'text-delta' || chunk.type === 'text') && typeof payload.text === 'string') {
|
|
911
|
+
record.textParts.push(payload.text);
|
|
912
|
+
recordFirstTokenEvent(record);
|
|
913
|
+
}
|
|
914
|
+
if ((chunk.type === 'reasoning-delta' || chunk.type === 'reasoning') &&
|
|
915
|
+
typeof payload.text === 'string') {
|
|
916
|
+
record.reasoningParts.push(payload.text);
|
|
917
|
+
}
|
|
918
|
+
if (chunk.type === 'tool-call' && isRecord(payload)) {
|
|
919
|
+
record.toolCalls.push(toTraceObject(payload));
|
|
920
|
+
}
|
|
921
|
+
if ((chunk.type === 'tool-result' || chunk.type === 'tool-error') && isRecord(payload)) {
|
|
922
|
+
record.toolResults.push(toTraceObject(payload));
|
|
923
|
+
}
|
|
924
|
+
return record;
|
|
925
|
+
}
|
|
926
|
+
function applyStepFinishChunk(chunk, records) {
|
|
927
|
+
if (!isRecord(chunk) || chunk.type !== 'step-finish') {
|
|
928
|
+
return undefined;
|
|
929
|
+
}
|
|
930
|
+
const payload = getChunkPayload(chunk);
|
|
931
|
+
const messageId = typeof payload?.messageId === 'string' ? payload.messageId : undefined;
|
|
932
|
+
const record = findActiveStepRecord(records, messageId);
|
|
933
|
+
if (!record) {
|
|
934
|
+
return undefined;
|
|
935
|
+
}
|
|
936
|
+
const output = isRecord(payload?.output) ? payload.output : undefined;
|
|
937
|
+
const stepResult = isRecord(payload?.stepResult) ? payload.stepResult : undefined;
|
|
938
|
+
const metadata = isRecord(payload?.metadata) ? payload.metadata : undefined;
|
|
939
|
+
record.finishReason =
|
|
940
|
+
(stepResult && typeof stepResult.reason === 'string' ? stepResult.reason : undefined) ??
|
|
941
|
+
(payload && typeof payload.finishReason === 'string' ? payload.finishReason : undefined) ??
|
|
942
|
+
record.finishReason;
|
|
943
|
+
const usage = output?.usage ?? payload?.usage;
|
|
944
|
+
if (usage !== undefined) {
|
|
945
|
+
record.usage = usage;
|
|
946
|
+
}
|
|
947
|
+
if (payload?.response !== undefined) {
|
|
948
|
+
record.response = payload.response;
|
|
949
|
+
}
|
|
950
|
+
const request = metadata?.request ?? payload?.request;
|
|
951
|
+
if (request !== undefined) {
|
|
952
|
+
record.request = request;
|
|
953
|
+
}
|
|
954
|
+
const providerMetadata = metadata?.providerMetadata ?? payload?.providerMetadata;
|
|
955
|
+
if (providerMetadata !== undefined) {
|
|
956
|
+
record.providerMetadata = providerMetadata;
|
|
957
|
+
}
|
|
958
|
+
record.warnings =
|
|
959
|
+
(stepResult && Array.isArray(stepResult.warnings) ? stepResult.warnings : undefined) ??
|
|
960
|
+
payload?.warnings ??
|
|
961
|
+
record.warnings;
|
|
962
|
+
record.isContinued =
|
|
963
|
+
stepResult?.isContinued === true ? true : payload?.isContinued === true || record.isContinued;
|
|
964
|
+
if (output && typeof output.text === 'string') {
|
|
965
|
+
record.textParts = output.text.length > 0 ? [output.text] : [];
|
|
966
|
+
}
|
|
967
|
+
if (output && Array.isArray(output.toolCalls)) {
|
|
968
|
+
record.toolCalls = output.toolCalls.map((entry) => toTraceObject(entry));
|
|
969
|
+
}
|
|
970
|
+
if (output && Array.isArray(output.toolResults)) {
|
|
971
|
+
record.toolResults = output.toolResults.map((entry) => toTraceObject(entry));
|
|
972
|
+
}
|
|
973
|
+
const responseModelId = payload && isRecord(payload.response) && typeof payload.response.modelId === 'string'
|
|
974
|
+
? payload.response.modelId
|
|
975
|
+
: undefined;
|
|
976
|
+
if (responseModelId) {
|
|
977
|
+
const model = normalizeModelMetadata(responseModelId);
|
|
978
|
+
record.model = {
|
|
979
|
+
provider: model.provider ?? record.model.provider,
|
|
980
|
+
modelName: model.modelName ?? responseModelId,
|
|
981
|
+
};
|
|
982
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
983
|
+
record.runTree.metadata = {
|
|
984
|
+
...(record.runTree.metadata ?? {}),
|
|
985
|
+
...(record.model.provider ? { ls_provider: record.model.provider } : {}),
|
|
986
|
+
...(record.model.modelName ? { ls_model_name: record.model.modelName } : {}),
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
return record;
|
|
990
|
+
}
|
|
991
|
+
function isStepResultLike(value) {
|
|
992
|
+
return isRecord(value);
|
|
993
|
+
}
|
|
994
|
+
function toStepResultLike(value) {
|
|
995
|
+
return isStepResultLike(value) ? value : undefined;
|
|
996
|
+
}
|
|
997
|
+
function toStepStartLike(value) {
|
|
998
|
+
return isRecord(value) ? value : undefined;
|
|
999
|
+
}
|
|
1000
|
+
function getSyntheticStepMessageId(stepResult, index) {
|
|
1001
|
+
const messages = extractResponseMessages(stepResult.response);
|
|
1002
|
+
const messageId = messages?.find((message) => isRecord(message) && typeof message.id === 'string')?.id;
|
|
1003
|
+
return typeof messageId === 'string' ? messageId : `step-${index + 1}`;
|
|
1004
|
+
}
|
|
1005
|
+
async function createFallbackStepTrace(parentRun, stepResult, index) {
|
|
1006
|
+
const record = await startLlmStepTrace(parentRun, getSyntheticStepMessageId(stepResult, index), stepResult.request, stepResult.stepNumber ?? index);
|
|
1007
|
+
if (!record) {
|
|
1008
|
+
return undefined;
|
|
1009
|
+
}
|
|
1010
|
+
if (typeof stepResult.text === 'string' && stepResult.text.length > 0) {
|
|
1011
|
+
record.textParts.push(stepResult.text);
|
|
1012
|
+
recordFirstTokenEvent(record);
|
|
1013
|
+
}
|
|
1014
|
+
if (typeof stepResult.reasoning === 'string' && stepResult.reasoning.length > 0) {
|
|
1015
|
+
record.reasoningParts.push(stepResult.reasoning);
|
|
1016
|
+
}
|
|
1017
|
+
else if (Array.isArray(stepResult.reasoning)) {
|
|
1018
|
+
record.reasoningParts.push(...stepResult.reasoning
|
|
1019
|
+
.map((entry) => isRecord(entry) && typeof entry.text === 'string' ? entry.text : undefined)
|
|
1020
|
+
.filter((entry) => entry !== undefined));
|
|
1021
|
+
}
|
|
1022
|
+
if (Array.isArray(stepResult.toolCalls)) {
|
|
1023
|
+
record.toolCalls.push(...stepResult.toolCalls.map((entry) => toTraceObject(entry)));
|
|
1024
|
+
}
|
|
1025
|
+
if (Array.isArray(stepResult.toolResults)) {
|
|
1026
|
+
record.toolResults.push(...stepResult.toolResults.map((entry) => toTraceObject(entry)));
|
|
1027
|
+
}
|
|
1028
|
+
record.finishReason = stepResult.finishReason;
|
|
1029
|
+
if (stepResult.usage !== undefined) {
|
|
1030
|
+
record.usage = stepResult.usage;
|
|
1031
|
+
}
|
|
1032
|
+
if (stepResult.request !== undefined) {
|
|
1033
|
+
record.request = stepResult.request;
|
|
1034
|
+
}
|
|
1035
|
+
if (stepResult.response !== undefined) {
|
|
1036
|
+
record.response = stepResult.response;
|
|
1037
|
+
}
|
|
1038
|
+
if (stepResult.providerMetadata !== undefined) {
|
|
1039
|
+
record.providerMetadata = stepResult.providerMetadata;
|
|
1040
|
+
}
|
|
1041
|
+
const responseModelId = isRecord(stepResult.response) && typeof stepResult.response.modelId === 'string'
|
|
1042
|
+
? stepResult.response.modelId
|
|
1043
|
+
: undefined;
|
|
1044
|
+
if (responseModelId) {
|
|
1045
|
+
const model = normalizeModelMetadata(responseModelId);
|
|
1046
|
+
record.model = {
|
|
1047
|
+
provider: model.provider ?? record.model.provider,
|
|
1048
|
+
modelName: model.modelName ?? responseModelId,
|
|
1049
|
+
};
|
|
1050
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1051
|
+
}
|
|
1052
|
+
return record;
|
|
1053
|
+
}
|
|
1054
|
+
function applyStepResultToRecord(record, stepResult) {
|
|
1055
|
+
if (typeof stepResult.text === 'string') {
|
|
1056
|
+
record.textParts = stepResult.text.length > 0 ? [stepResult.text] : [];
|
|
1057
|
+
}
|
|
1058
|
+
if (typeof stepResult.reasoning === 'string') {
|
|
1059
|
+
record.reasoningParts = stepResult.reasoning.length > 0 ? [stepResult.reasoning] : [];
|
|
1060
|
+
}
|
|
1061
|
+
else if (Array.isArray(stepResult.reasoning)) {
|
|
1062
|
+
record.reasoningParts = stepResult.reasoning
|
|
1063
|
+
.map((entry) => (isRecord(entry) && typeof entry.text === 'string' ? entry.text : undefined))
|
|
1064
|
+
.filter((entry) => entry !== undefined);
|
|
1065
|
+
}
|
|
1066
|
+
if (Array.isArray(stepResult.toolCalls)) {
|
|
1067
|
+
record.toolCalls = stepResult.toolCalls.map((entry) => toTraceObject(entry));
|
|
1068
|
+
}
|
|
1069
|
+
if (Array.isArray(stepResult.toolResults)) {
|
|
1070
|
+
record.toolResults = stepResult.toolResults.map((entry) => toTraceObject(entry));
|
|
1071
|
+
}
|
|
1072
|
+
if (typeof stepResult.finishReason === 'string') {
|
|
1073
|
+
record.finishReason = stepResult.finishReason;
|
|
1074
|
+
}
|
|
1075
|
+
if (stepResult.usage !== undefined) {
|
|
1076
|
+
record.usage = stepResult.usage;
|
|
1077
|
+
}
|
|
1078
|
+
if (stepResult.request !== undefined) {
|
|
1079
|
+
record.request = stepResult.request;
|
|
1080
|
+
}
|
|
1081
|
+
if (stepResult.response !== undefined) {
|
|
1082
|
+
record.response = stepResult.response;
|
|
1083
|
+
}
|
|
1084
|
+
if (stepResult.providerMetadata !== undefined) {
|
|
1085
|
+
record.providerMetadata = stepResult.providerMetadata;
|
|
1086
|
+
}
|
|
1087
|
+
const responseModelId = isRecord(stepResult.response) && typeof stepResult.response.modelId === 'string'
|
|
1088
|
+
? stepResult.response.modelId
|
|
1089
|
+
: undefined;
|
|
1090
|
+
if (responseModelId) {
|
|
1091
|
+
const model = normalizeModelMetadata(responseModelId);
|
|
1092
|
+
record.model = {
|
|
1093
|
+
provider: model.provider ?? record.model.provider,
|
|
1094
|
+
modelName: model.modelName ?? responseModelId,
|
|
1095
|
+
};
|
|
1096
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
function createLlmStepTraceHooks(explicitParentRun) {
|
|
1100
|
+
const activeParentRun = explicitParentRun ?? (0, langsmith_tracing_1.getTraceParentRun)();
|
|
1101
|
+
if (!activeParentRun) {
|
|
1102
|
+
return undefined;
|
|
1103
|
+
}
|
|
1104
|
+
const records = [];
|
|
1105
|
+
const recordsByStepNumber = new Map();
|
|
1106
|
+
const getActiveRecord = () => {
|
|
1107
|
+
for (let index = records.length - 1; index >= 0; index--) {
|
|
1108
|
+
const record = records[index];
|
|
1109
|
+
if (!record.finished) {
|
|
1110
|
+
return record;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
return undefined;
|
|
1114
|
+
};
|
|
1115
|
+
const restoreTraceParent = () => {
|
|
1116
|
+
(0, langsmith_tracing_1.setTraceParentOverride)(activeParentRun);
|
|
1117
|
+
};
|
|
1118
|
+
restoreTraceParent();
|
|
1119
|
+
const patchFinishedRecordIfNeeded = async (record, stepResult, options) => {
|
|
1120
|
+
const metadata = {
|
|
1121
|
+
...(record.runTree.metadata ?? {}),
|
|
1122
|
+
...buildLlmMetadata(record, stepResult),
|
|
1123
|
+
...(options?.status && options.status !== 'completed'
|
|
1124
|
+
? { final_status: options.status }
|
|
1125
|
+
: {}),
|
|
1126
|
+
};
|
|
1127
|
+
record.runTree.inputs = record.inputs;
|
|
1128
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1129
|
+
record.runTree.metadata = metadata;
|
|
1130
|
+
const endTimeValue = Reflect.get(record.runTree, 'end_time');
|
|
1131
|
+
const endTime = typeof endTimeValue === 'number' ? endTimeValue : undefined;
|
|
1132
|
+
await finishRunTree(record.runTree, {
|
|
1133
|
+
outputs: buildLlmOutputs(record, stepResult),
|
|
1134
|
+
metadata,
|
|
1135
|
+
...(options?.error ? { error: options.error } : {}),
|
|
1136
|
+
...(endTime !== undefined ? { endTime } : {}),
|
|
1137
|
+
});
|
|
1138
|
+
};
|
|
1139
|
+
const startStepTrace = async (options) => {
|
|
1140
|
+
const stepStart = toStepStartLike(options);
|
|
1141
|
+
if (typeof stepStart?.stepNumber !== 'number') {
|
|
1142
|
+
return undefined;
|
|
1143
|
+
}
|
|
1144
|
+
const existingRecord = recordsByStepNumber.get(stepStart.stepNumber);
|
|
1145
|
+
if (existingRecord && !existingRecord.finished) {
|
|
1146
|
+
(0, langsmith_tracing_1.setTraceParentOverride)(existingRecord.runTree);
|
|
1147
|
+
return existingRecord;
|
|
1148
|
+
}
|
|
1149
|
+
const record = await startLlmStepTrace(activeParentRun, `step-${stepStart.stepNumber + 1}`, {
|
|
1150
|
+
messages: Array.isArray(stepStart.messages) ? stepStart.messages : [],
|
|
1151
|
+
}, stepStart.stepNumber);
|
|
1152
|
+
if (!record) {
|
|
1153
|
+
return undefined;
|
|
1154
|
+
}
|
|
1155
|
+
const stepModelId = stepStart.model?.modelId;
|
|
1156
|
+
if (typeof stepModelId === 'string' && stepModelId.length > 0) {
|
|
1157
|
+
record.model = {
|
|
1158
|
+
provider: stepStart.model?.provider ?? record.model.provider,
|
|
1159
|
+
modelName: normalizeModelMetadata(stepModelId).modelName ?? stepModelId,
|
|
1160
|
+
};
|
|
1161
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1162
|
+
record.runTree.metadata = {
|
|
1163
|
+
...(record.runTree.metadata ?? {}),
|
|
1164
|
+
...(record.model.provider ? { ls_provider: record.model.provider } : {}),
|
|
1165
|
+
...(record.model.modelName ? { ls_model_name: record.model.modelName } : {}),
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
recordsByStepNumber.set(stepStart.stepNumber, record);
|
|
1169
|
+
records.push(record);
|
|
1170
|
+
(0, langsmith_tracing_1.setTraceParentOverride)(record.runTree);
|
|
1171
|
+
return record;
|
|
1172
|
+
};
|
|
1173
|
+
const prepareStep = async (options) => {
|
|
1174
|
+
await startStepTrace(options);
|
|
1175
|
+
return undefined;
|
|
1176
|
+
};
|
|
1177
|
+
const onStepStart = async (options) => {
|
|
1178
|
+
await startStepTrace(options);
|
|
1179
|
+
};
|
|
1180
|
+
const onStepFinish = async (stepResultValue) => {
|
|
1181
|
+
const stepResult = toStepResultLike(stepResultValue);
|
|
1182
|
+
if (!stepResult) {
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
const stepNumber = typeof stepResult.stepNumber === 'number' ? stepResult.stepNumber : undefined;
|
|
1186
|
+
const record = stepNumber !== undefined ? recordsByStepNumber.get(stepNumber) : undefined;
|
|
1187
|
+
if (!record || record.finished) {
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
applyStepResultToRecord(record, stepResult);
|
|
1191
|
+
record.runTree.inputs = record.inputs;
|
|
1192
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1193
|
+
record.runTree.metadata = {
|
|
1194
|
+
...(record.runTree.metadata ?? {}),
|
|
1195
|
+
...buildLlmMetadata(record, stepResult),
|
|
1196
|
+
};
|
|
1197
|
+
await finishRunTree(record.runTree, {
|
|
1198
|
+
outputs: buildLlmOutputs(record, stepResult),
|
|
1199
|
+
metadata: record.runTree.metadata,
|
|
1200
|
+
});
|
|
1201
|
+
record.finished = true;
|
|
1202
|
+
if (stepNumber !== undefined) {
|
|
1203
|
+
recordsByStepNumber.delete(stepNumber);
|
|
1204
|
+
}
|
|
1205
|
+
restoreTraceParent();
|
|
1206
|
+
};
|
|
1207
|
+
return {
|
|
1208
|
+
executionOptions: {
|
|
1209
|
+
prepareStep,
|
|
1210
|
+
experimental_prepareStep: prepareStep,
|
|
1211
|
+
experimental_onStepStart: onStepStart,
|
|
1212
|
+
onStepFinish,
|
|
1213
|
+
experimental_telemetry: { isEnabled: false },
|
|
1214
|
+
},
|
|
1215
|
+
onStreamChunk: (chunk) => {
|
|
1216
|
+
updateStepRecordFromChunk(chunk, records);
|
|
1217
|
+
applyStepFinishChunk(chunk, records);
|
|
1218
|
+
},
|
|
1219
|
+
startSegment: () => {
|
|
1220
|
+
for (const [stepNumber, record] of recordsByStepNumber.entries()) {
|
|
1221
|
+
if (record.finished) {
|
|
1222
|
+
recordsByStepNumber.delete(stepNumber);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
const activeRecord = getActiveRecord();
|
|
1226
|
+
if (activeRecord) {
|
|
1227
|
+
(0, langsmith_tracing_1.setTraceParentOverride)(activeRecord.runTree);
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
restoreTraceParent();
|
|
1231
|
+
},
|
|
1232
|
+
finalize: async (source, options) => {
|
|
1233
|
+
const resolvedSteps = await source.steps?.then((steps) => steps, () => undefined);
|
|
1234
|
+
const segmentUsage = await resolveSegmentUsage(source);
|
|
1235
|
+
const stepResults = Array.isArray(resolvedSteps)
|
|
1236
|
+
? resolvedSteps
|
|
1237
|
+
.map((stepValue) => toStepResultLike(stepValue))
|
|
1238
|
+
.filter((stepResult) => stepResult !== undefined)
|
|
1239
|
+
: [];
|
|
1240
|
+
const stepResultsByStepNumber = new Map();
|
|
1241
|
+
for (const stepResult of stepResults) {
|
|
1242
|
+
if (typeof stepResult.stepNumber === 'number') {
|
|
1243
|
+
stepResultsByStepNumber.set(stepResult.stepNumber, stepResult);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
for (const [index, record] of records.entries()) {
|
|
1247
|
+
const stepResult = (typeof record.stepNumber === 'number'
|
|
1248
|
+
? stepResultsByStepNumber.get(record.stepNumber)
|
|
1249
|
+
: undefined) ?? stepResults[index];
|
|
1250
|
+
const hadUsageMetadata = buildUsageMetadata(record.usage, record.providerMetadata);
|
|
1251
|
+
const hadUsageMetadataJson = hadUsageMetadata
|
|
1252
|
+
? JSON.stringify(hadUsageMetadata)
|
|
1253
|
+
: undefined;
|
|
1254
|
+
const hadResponse = record.response !== undefined;
|
|
1255
|
+
const hadFinishReason = record.finishReason !== undefined;
|
|
1256
|
+
if (stepResult) {
|
|
1257
|
+
applyStepResultToRecord(record, stepResult);
|
|
1258
|
+
}
|
|
1259
|
+
maybeBackfillRecordUsageFromSegment(record, records, segmentUsage);
|
|
1260
|
+
if (record.finished) {
|
|
1261
|
+
const hasUsageMetadata = buildUsageMetadata(record.usage, record.providerMetadata);
|
|
1262
|
+
const hasUsageMetadataJson = hasUsageMetadata
|
|
1263
|
+
? JSON.stringify(hasUsageMetadata)
|
|
1264
|
+
: undefined;
|
|
1265
|
+
const hasResponse = record.response !== undefined;
|
|
1266
|
+
const hasFinishReason = record.finishReason !== undefined;
|
|
1267
|
+
if (hadUsageMetadataJson !== hasUsageMetadataJson ||
|
|
1268
|
+
(!hadResponse && hasResponse) ||
|
|
1269
|
+
(!hadFinishReason && hasFinishReason)) {
|
|
1270
|
+
await patchFinishedRecordIfNeeded(record, stepResult, options);
|
|
1271
|
+
}
|
|
1272
|
+
continue;
|
|
1273
|
+
}
|
|
1274
|
+
record.runTree.inputs = record.inputs;
|
|
1275
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1276
|
+
record.runTree.metadata = {
|
|
1277
|
+
...(record.runTree.metadata ?? {}),
|
|
1278
|
+
...buildLlmMetadata(record, stepResult),
|
|
1279
|
+
...(options?.status && options.status !== 'completed'
|
|
1280
|
+
? { final_status: options.status }
|
|
1281
|
+
: {}),
|
|
1282
|
+
};
|
|
1283
|
+
await finishRunTree(record.runTree, {
|
|
1284
|
+
outputs: buildLlmOutputs(record, stepResult),
|
|
1285
|
+
metadata: record.runTree.metadata,
|
|
1286
|
+
...(options?.error ? { error: options.error } : {}),
|
|
1287
|
+
});
|
|
1288
|
+
record.finished = true;
|
|
1289
|
+
}
|
|
1290
|
+
restoreTraceParent();
|
|
1291
|
+
recordsByStepNumber.clear();
|
|
1292
|
+
},
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
async function finalizeLlmStepTraces(source, records, options) {
|
|
1296
|
+
const parentRun = (0, langsmith_tracing_1.getTraceParentRun)();
|
|
1297
|
+
const resolvedSteps = await source.steps?.then((steps) => steps, () => undefined);
|
|
1298
|
+
const segmentUsage = await resolveSegmentUsage(source);
|
|
1299
|
+
const stepResults = Array.isArray(resolvedSteps)
|
|
1300
|
+
? resolvedSteps
|
|
1301
|
+
.map((stepValue) => toStepResultLike(stepValue))
|
|
1302
|
+
.filter((stepResult) => stepResult !== undefined)
|
|
1303
|
+
: [];
|
|
1304
|
+
const stepResultsByStepNumber = new Map();
|
|
1305
|
+
for (const stepResult of stepResults) {
|
|
1306
|
+
if (typeof stepResult.stepNumber === 'number') {
|
|
1307
|
+
stepResultsByStepNumber.set(stepResult.stepNumber, stepResult);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
if (records.length === 0 && stepResults.length > 0) {
|
|
1311
|
+
for (const [index, stepResult] of stepResults.entries()) {
|
|
1312
|
+
const fallbackRecord = await createFallbackStepTrace(parentRun, stepResult, index);
|
|
1313
|
+
if (fallbackRecord) {
|
|
1314
|
+
records.push(fallbackRecord);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
if (records.length === 0) {
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
for (const [index, record] of records.entries()) {
|
|
1322
|
+
if (record.finished) {
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
const stepResult = (typeof record.stepNumber === 'number'
|
|
1326
|
+
? stepResultsByStepNumber.get(record.stepNumber)
|
|
1327
|
+
: undefined) ?? stepResults[index];
|
|
1328
|
+
if (stepResult) {
|
|
1329
|
+
applyStepResultToRecord(record, stepResult);
|
|
1330
|
+
}
|
|
1331
|
+
maybeBackfillRecordUsageFromSegment(record, records, segmentUsage);
|
|
1332
|
+
record.runTree.inputs = record.inputs;
|
|
1333
|
+
record.runTree.name = formatLlmRunName(record.model);
|
|
1334
|
+
record.runTree.metadata = {
|
|
1335
|
+
...(record.runTree.metadata ?? {}),
|
|
1336
|
+
...buildLlmMetadata(record, stepResult),
|
|
1337
|
+
...(options?.status && options.status !== 'completed'
|
|
1338
|
+
? { final_status: options.status }
|
|
1339
|
+
: {}),
|
|
1340
|
+
};
|
|
1341
|
+
await finishRunTree(record.runTree, {
|
|
1342
|
+
outputs: buildLlmOutputs(record, stepResult),
|
|
1343
|
+
metadata: record.runTree.metadata,
|
|
1344
|
+
...(options?.error ? { error: options.error } : {}),
|
|
1345
|
+
});
|
|
1346
|
+
record.finished = true;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
async function executeResumableStream(options) {
|
|
1350
|
+
let activeSource = options.stream;
|
|
1351
|
+
let activeStream = options.stream.fullStream;
|
|
1352
|
+
let activeMastraRunId = options.stream.runId ?? options.initialMastraRunId ?? '';
|
|
1353
|
+
let text = options.stream.text;
|
|
1354
|
+
while (true) {
|
|
1355
|
+
let suspension;
|
|
1356
|
+
let hasError = false;
|
|
1357
|
+
let pendingConfirmation;
|
|
1358
|
+
let confirmationEvent;
|
|
1359
|
+
let confirmationEventPublished = false;
|
|
1360
|
+
const llmStepRecords = [];
|
|
1361
|
+
const syntheticToolRecords = new Map();
|
|
1362
|
+
options.llmStepTraceHooks?.startSegment();
|
|
1363
|
+
for await (const chunk of activeStream) {
|
|
1364
|
+
if (options.context.signal.aborted) {
|
|
1365
|
+
if (options.llmStepTraceHooks) {
|
|
1366
|
+
await options.llmStepTraceHooks.finalize(activeSource, {
|
|
1367
|
+
status: 'cancelled',
|
|
1368
|
+
error: 'Run cancelled while streaming',
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
else {
|
|
1372
|
+
await finalizeLlmStepTraces(activeSource, llmStepRecords, {
|
|
1373
|
+
status: 'cancelled',
|
|
1374
|
+
error: 'Run cancelled while streaming',
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
await finalizeSyntheticToolTraces(syntheticToolRecords, {
|
|
1378
|
+
status: 'cancelled',
|
|
1379
|
+
error: 'Run cancelled while streaming',
|
|
1380
|
+
});
|
|
1381
|
+
return { status: 'cancelled', mastraRunId: activeMastraRunId, text };
|
|
1382
|
+
}
|
|
1383
|
+
await startSyntheticToolTrace(chunk, syntheticToolRecords);
|
|
1384
|
+
await finishSyntheticToolTrace(chunk, syntheticToolRecords);
|
|
1385
|
+
options.llmStepTraceHooks?.onStreamChunk(chunk);
|
|
1386
|
+
if (options.llmStepTraceHooks) {
|
|
1387
|
+
}
|
|
1388
|
+
else if (isRecord(chunk) && chunk.type === 'step-start') {
|
|
1389
|
+
const payload = getChunkPayload(chunk);
|
|
1390
|
+
const messageId = typeof payload?.messageId === 'string' ? payload.messageId : undefined;
|
|
1391
|
+
const request = payload?.request;
|
|
1392
|
+
const stepTrace = typeof messageId === 'string'
|
|
1393
|
+
? await startLlmStepTrace(undefined, messageId, request)
|
|
1394
|
+
: undefined;
|
|
1395
|
+
if (stepTrace) {
|
|
1396
|
+
llmStepRecords.push(stepTrace);
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
else {
|
|
1400
|
+
updateStepRecordFromChunk(chunk, llmStepRecords);
|
|
1401
|
+
applyStepFinishChunk(chunk, llmStepRecords);
|
|
1402
|
+
}
|
|
1403
|
+
const parsedSuspension = (0, stream_helpers_1.parseSuspension)(chunk);
|
|
1404
|
+
if (parsedSuspension) {
|
|
1405
|
+
if (!suspension) {
|
|
1406
|
+
suspension = parsedSuspension;
|
|
1407
|
+
if (options.control.mode === 'auto') {
|
|
1408
|
+
options.control.onSuspension?.(parsedSuspension);
|
|
1409
|
+
pendingConfirmation = options.control.waitForConfirmation(parsedSuspension.requestId);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
else if (!isSameSuspension(parsedSuspension, suspension)) {
|
|
1413
|
+
console.warn('[HITL] additional suspension encountered before resume; deferring', {
|
|
1414
|
+
threadId: options.context.threadId,
|
|
1415
|
+
runId: options.context.runId,
|
|
1416
|
+
activeRequestId: suspension.requestId,
|
|
1417
|
+
deferredRequestId: parsedSuspension.requestId,
|
|
1418
|
+
activeToolCallId: suspension.toolCallId,
|
|
1419
|
+
deferredToolCallId: parsedSuspension.toolCallId,
|
|
1420
|
+
});
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
if (isErrorChunk(chunk)) {
|
|
1424
|
+
hasError = true;
|
|
1425
|
+
}
|
|
1426
|
+
const event = (0, map_chunk_1.mapMastraChunkToEvent)(options.context.runId, options.context.agentId, chunk);
|
|
1427
|
+
if (event) {
|
|
1428
|
+
let shouldPublishEvent = true;
|
|
1429
|
+
if (event.type === 'confirmation-request') {
|
|
1430
|
+
const isPrimarySuspension = suspension !== undefined &&
|
|
1431
|
+
event.payload.requestId === suspension.requestId &&
|
|
1432
|
+
event.payload.toolCallId === suspension.toolCallId;
|
|
1433
|
+
if (!isPrimarySuspension || confirmationEventPublished || confirmationEvent) {
|
|
1434
|
+
shouldPublishEvent = false;
|
|
1435
|
+
}
|
|
1436
|
+
if (shouldPublishEvent && options.control.mode === 'manual') {
|
|
1437
|
+
confirmationEvent = event;
|
|
1438
|
+
shouldPublishEvent = false;
|
|
1439
|
+
}
|
|
1440
|
+
if (shouldPublishEvent) {
|
|
1441
|
+
confirmationEventPublished = true;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
if (shouldPublishEvent) {
|
|
1445
|
+
options.context.eventBus.publish(options.context.threadId, event);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
if (options.control.mode === 'auto' && options.control.drainCorrections) {
|
|
1449
|
+
publishCorrections(options.context, options.control.drainCorrections());
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
if (options.llmStepTraceHooks) {
|
|
1453
|
+
await options.llmStepTraceHooks.finalize(activeSource, {
|
|
1454
|
+
status: suspension ? 'suspended' : 'completed',
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
else {
|
|
1458
|
+
await finalizeLlmStepTraces(activeSource, llmStepRecords, {
|
|
1459
|
+
status: suspension ? 'suspended' : 'completed',
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
await finalizeSyntheticToolTraces(syntheticToolRecords, {
|
|
1463
|
+
status: suspension ? 'suspended' : 'completed',
|
|
1464
|
+
});
|
|
1465
|
+
if (options.context.signal.aborted) {
|
|
1466
|
+
return { status: 'cancelled', mastraRunId: activeMastraRunId, text };
|
|
1467
|
+
}
|
|
1468
|
+
if (!suspension) {
|
|
1469
|
+
return { status: hasError ? 'errored' : 'completed', mastraRunId: activeMastraRunId, text };
|
|
1470
|
+
}
|
|
1471
|
+
if (options.control.mode === 'manual') {
|
|
1472
|
+
return {
|
|
1473
|
+
status: 'suspended',
|
|
1474
|
+
mastraRunId: activeMastraRunId,
|
|
1475
|
+
text,
|
|
1476
|
+
suspension,
|
|
1477
|
+
...(confirmationEvent ? { confirmationEvent } : {}),
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
const resumeData = await waitForConfirmation(options.context.signal, pendingConfirmation ?? options.control.waitForConfirmation(suspension.requestId));
|
|
1481
|
+
const resumeOptions = options.control.buildResumeOptions?.({
|
|
1482
|
+
mastraRunId: activeMastraRunId,
|
|
1483
|
+
suspension,
|
|
1484
|
+
}) ?? {
|
|
1485
|
+
runId: activeMastraRunId,
|
|
1486
|
+
toolCallId: suspension.toolCallId,
|
|
1487
|
+
};
|
|
1488
|
+
const resumed = options.workingMemoryEnabled
|
|
1489
|
+
? await (0, working_memory_tracing_1.traceWorkingMemoryContext)({
|
|
1490
|
+
phase: 'resume',
|
|
1491
|
+
agentId: options.context.agentId,
|
|
1492
|
+
threadId: options.context.threadId,
|
|
1493
|
+
resumeData: {
|
|
1494
|
+
requestId: suspension.requestId,
|
|
1495
|
+
toolCallId: suspension.toolCallId,
|
|
1496
|
+
...(typeof resumeOptions.runId === 'string' ? { runId: resumeOptions.runId } : {}),
|
|
1497
|
+
},
|
|
1498
|
+
enabled: true,
|
|
1499
|
+
}, async () => await (0, stream_helpers_1.asResumable)(options.agent).resumeStream(resumeData, {
|
|
1500
|
+
...resumeOptions,
|
|
1501
|
+
...(options.llmStepTraceHooks?.executionOptions ?? {}),
|
|
1502
|
+
}))
|
|
1503
|
+
: await (0, stream_helpers_1.asResumable)(options.agent).resumeStream(resumeData, {
|
|
1504
|
+
...resumeOptions,
|
|
1505
|
+
...(options.llmStepTraceHooks?.executionOptions ?? {}),
|
|
1506
|
+
});
|
|
1507
|
+
activeMastraRunId =
|
|
1508
|
+
(typeof resumed.runId === 'string' ? resumed.runId : '') || activeMastraRunId;
|
|
1509
|
+
activeSource = resumed;
|
|
1510
|
+
activeStream = resumed.fullStream;
|
|
1511
|
+
text = resumed.text;
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
function publishCorrections(context, corrections) {
|
|
1515
|
+
for (const correction of corrections) {
|
|
1516
|
+
context.eventBus.publish(context.threadId, {
|
|
1517
|
+
type: 'text-delta',
|
|
1518
|
+
runId: context.runId,
|
|
1519
|
+
agentId: context.agentId,
|
|
1520
|
+
payload: { text: `\n[USER CORRECTION]: ${correction}\n` },
|
|
1521
|
+
});
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
function isErrorChunk(chunk) {
|
|
1525
|
+
return (chunk !== null &&
|
|
1526
|
+
typeof chunk === 'object' &&
|
|
1527
|
+
chunk.type === 'error');
|
|
1528
|
+
}
|
|
1529
|
+
async function waitForConfirmation(signal, confirmationPromise) {
|
|
1530
|
+
if (signal.aborted) {
|
|
1531
|
+
throw new Error('Run cancelled while waiting for confirmation');
|
|
1532
|
+
}
|
|
1533
|
+
let abortHandler;
|
|
1534
|
+
try {
|
|
1535
|
+
return await Promise.race([
|
|
1536
|
+
confirmationPromise,
|
|
1537
|
+
new Promise((_, reject) => {
|
|
1538
|
+
abortHandler = () => reject(new Error('Run cancelled while waiting for confirmation'));
|
|
1539
|
+
signal.addEventListener('abort', abortHandler, { once: true });
|
|
1540
|
+
}),
|
|
1541
|
+
]);
|
|
1542
|
+
}
|
|
1543
|
+
finally {
|
|
1544
|
+
if (abortHandler) {
|
|
1545
|
+
signal.removeEventListener('abort', abortHandler);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
function isSameSuspension(left, right) {
|
|
1550
|
+
return left.requestId === right.requestId && left.toolCallId === right.toolCallId;
|
|
1551
|
+
}
|
|
1552
|
+
//# sourceMappingURL=resumable-stream-executor.js.map
|