@n8n/instance-ai 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/instance-agent.d.ts +1 -1
- package/dist/agent/instance-agent.js +88 -75
- package/dist/agent/instance-agent.js.map +1 -1
- package/dist/agent/mcp-tool-name-validation.d.ts +4 -2
- package/dist/agent/mcp-tool-name-validation.js +2 -2
- package/dist/agent/mcp-tool-name-validation.js.map +1 -1
- package/dist/agent/runtime-workspace.d.ts +6 -0
- package/dist/agent/runtime-workspace.js +14 -0
- package/dist/agent/runtime-workspace.js.map +1 -0
- package/dist/agent/sanitize-mcp-schemas.d.ts +4 -3
- package/dist/agent/sanitize-mcp-schemas.js +53 -24
- package/dist/agent/sanitize-mcp-schemas.js.map +1 -1
- package/dist/agent/sub-agent-debriefing.d.ts +2 -2
- package/dist/agent/sub-agent-factory.d.ts +7 -4
- package/dist/agent/sub-agent-factory.js +24 -13
- package/dist/agent/sub-agent-factory.js.map +1 -1
- package/dist/agent/system-prompt.js +18 -15
- package/dist/agent/system-prompt.js.map +1 -1
- package/dist/build.tsbuildinfo +1 -1
- package/dist/constants/max-steps.d.ts +1 -1
- package/dist/constants/max-steps.js +1 -1
- package/dist/constants/max-steps.js.map +1 -1
- package/dist/domain-access/domain-gating.d.ts +2 -2
- package/dist/error-formatting.d.ts +1 -0
- package/dist/error-formatting.js +159 -0
- package/dist/error-formatting.js.map +1 -0
- package/dist/index.d.ts +65 -24
- package/dist/index.js +96 -52
- package/dist/index.js.map +1 -1
- package/dist/mcp/mcp-client-manager.d.ts +5 -4
- package/dist/mcp/mcp-client-manager.js +42 -17
- package/dist/mcp/mcp-client-manager.js.map +1 -1
- package/dist/memory/title-utils.d.ts +4 -1
- package/dist/memory/title-utils.js +10 -2
- package/dist/memory/title-utils.js.map +1 -1
- package/dist/parsers/structured-file-parser.d.ts +5 -2
- package/dist/parsers/structured-file-parser.js +9 -2
- package/dist/parsers/structured-file-parser.js.map +1 -1
- package/dist/planned-tasks/planned-task-permissions.js +3 -5
- package/dist/planned-tasks/planned-task-permissions.js.map +1 -1
- package/dist/planned-tasks/planned-task-service.d.ts +1 -0
- package/dist/planned-tasks/planned-task-service.js +8 -0
- package/dist/planned-tasks/planned-task-service.js.map +1 -1
- package/dist/runtime/background-task-manager.d.ts +9 -2
- package/dist/runtime/background-task-manager.js +7 -2
- package/dist/runtime/background-task-manager.js.map +1 -1
- package/dist/runtime/liveness-policy.js +4 -3
- package/dist/runtime/liveness-policy.js.map +1 -1
- package/dist/runtime/resumable-stream-executor.d.ts +4 -24
- package/dist/runtime/resumable-stream-executor.js +103 -1318
- package/dist/runtime/resumable-stream-executor.js.map +1 -1
- package/dist/runtime/run-state-registry.d.ts +8 -2
- package/dist/runtime/run-state-registry.js +19 -1
- package/dist/runtime/run-state-registry.js.map +1 -1
- package/dist/runtime/stream-runner.d.ts +4 -4
- package/dist/runtime/stream-runner.js +11 -25
- package/dist/runtime/stream-runner.js.map +1 -1
- package/dist/skills/materialize-runtime-skills.d.ts +62 -0
- package/dist/skills/materialize-runtime-skills.js +465 -0
- package/dist/skills/materialize-runtime-skills.js.map +1 -0
- package/dist/skills/runtime-skills.d.ts +4 -0
- package/dist/skills/runtime-skills.js +17 -0
- package/dist/skills/runtime-skills.js.map +1 -0
- package/dist/source-map-filter.d.ts +17 -0
- package/dist/source-map-filter.js +39 -0
- package/dist/source-map-filter.js.map +1 -0
- package/dist/storage/agent-tree-snapshot.d.ts +2 -0
- package/dist/storage/index.d.ts +3 -3
- package/dist/storage/index.js +8 -7
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/planned-task-storage.d.ts +3 -3
- package/dist/storage/planned-task-storage.js +3 -8
- package/dist/storage/planned-task-storage.js.map +1 -1
- package/dist/storage/terminal-outcome-storage.d.ts +4 -4
- package/dist/storage/terminal-outcome-storage.js +1 -1
- package/dist/storage/terminal-outcome-storage.js.map +1 -1
- package/dist/storage/{mastra-iteration-log-storage.d.ts → thread-iteration-log-storage.d.ts} +3 -3
- package/dist/storage/{mastra-iteration-log-storage.js → thread-iteration-log-storage.js} +5 -5
- package/dist/storage/thread-iteration-log-storage.js.map +1 -0
- package/dist/storage/thread-patch.d.ts +16 -10
- package/dist/storage/thread-patch.js +37 -36
- package/dist/storage/thread-patch.js.map +1 -1
- package/dist/storage/{mastra-task-storage.d.ts → thread-task-storage.d.ts} +3 -3
- package/dist/storage/{mastra-task-storage.js → thread-task-storage.js} +5 -5
- package/dist/storage/thread-task-storage.js.map +1 -0
- package/dist/storage/workflow-loop-storage.d.ts +28 -28
- package/dist/storage/workflow-loop-storage.js +1 -1
- package/dist/storage/workflow-loop-storage.js.map +1 -1
- package/dist/stream/consume-with-hitl.d.ts +11 -11
- package/dist/stream/consume-with-hitl.js +27 -15
- package/dist/stream/consume-with-hitl.js.map +1 -1
- package/dist/stream/map-chunk.d.ts +1 -1
- package/dist/stream/map-chunk.js +153 -36
- package/dist/stream/map-chunk.js.map +1 -1
- package/dist/tool-registry.d.ts +8 -0
- package/dist/tool-registry.js +45 -0
- package/dist/tool-registry.js.map +1 -0
- package/dist/tools/attachments/parse-file.tool.d.ts +42 -45
- package/dist/tools/attachments/parse-file.tool.js +140 -97
- package/dist/tools/attachments/parse-file.tool.js.map +1 -1
- package/dist/tools/credentials.tool.d.ts +5 -5
- package/dist/tools/credentials.tool.js +31 -34
- package/dist/tools/credentials.tool.js.map +1 -1
- package/dist/tools/data-tables.tool.d.ts +3 -2
- package/dist/tools/data-tables.tool.js +99 -100
- package/dist/tools/data-tables.tool.js.map +1 -1
- package/dist/tools/evals/apply-pin-data.service.d.ts +3 -0
- package/dist/tools/evals/apply-pin-data.service.js +19 -0
- package/dist/tools/evals/apply-pin-data.service.js.map +1 -0
- package/dist/tools/evals/column-ref-utils.d.ts +3 -0
- package/dist/tools/evals/column-ref-utils.js +205 -48
- package/dist/tools/evals/column-ref-utils.js.map +1 -1
- package/dist/tools/evals/detect-agent-named-refs.service.js +20 -4
- package/dist/tools/evals/detect-agent-named-refs.service.js.map +1 -1
- package/dist/tools/evals/ensure-eval-data-table.service.d.ts +15 -0
- package/dist/tools/evals/ensure-eval-data-table.service.js +83 -0
- package/dist/tools/evals/ensure-eval-data-table.service.js.map +1 -0
- package/dist/tools/evals/eval-data-requirements.service.d.ts +1 -0
- package/dist/tools/evals/eval-data-requirements.service.js +23 -4
- package/dist/tools/evals/eval-data-requirements.service.js.map +1 -1
- package/dist/tools/evals/evals.tool.d.ts +2 -0
- package/dist/tools/evals/evals.tool.js +452 -0
- package/dist/tools/evals/evals.tool.js.map +1 -0
- package/dist/tools/evals/extract-rows-from-history.service.d.ts +17 -0
- package/dist/tools/evals/extract-rows-from-history.service.js +103 -0
- package/dist/tools/evals/extract-rows-from-history.service.js.map +1 -0
- package/dist/tools/evals/format-eval-setup-task.d.ts +16 -0
- package/dist/tools/evals/format-eval-setup-task.js +167 -0
- package/dist/tools/evals/format-eval-setup-task.js.map +1 -0
- package/dist/tools/evals/generate-sample-rows.service.d.ts +35 -0
- package/dist/tools/evals/generate-sample-rows.service.js +278 -0
- package/dist/tools/evals/generate-sample-rows.service.js.map +1 -0
- package/dist/tools/evals/generate-tool-ref-pin-data.service.d.ts +11 -0
- package/dist/tools/evals/generate-tool-ref-pin-data.service.js +125 -0
- package/dist/tools/evals/generate-tool-ref-pin-data.service.js.map +1 -0
- package/dist/tools/evals/metric-catalog.d.ts +10 -4
- package/dist/tools/evals/metric-catalog.js.map +1 -1
- package/dist/tools/executions.tool.d.ts +1 -1
- package/dist/tools/executions.tool.js +25 -34
- package/dist/tools/executions.tool.js.map +1 -1
- package/dist/tools/filesystem/create-tools-from-mcp-server.d.ts +2 -3
- package/dist/tools/filesystem/create-tools-from-mcp-server.js +129 -67
- package/dist/tools/filesystem/create-tools-from-mcp-server.js.map +1 -1
- package/dist/tools/index.d.ts +4 -16
- package/dist/tools/index.js +102 -92
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/nodes.tool.d.ts +1 -1
- package/dist/tools/nodes.tool.js +31 -33
- package/dist/tools/nodes.tool.js.map +1 -1
- package/dist/tools/orchestration/add-plan-item.tool.d.ts +2 -2
- package/dist/tools/orchestration/add-plan-item.tool.js +45 -49
- package/dist/tools/orchestration/add-plan-item.tool.js.map +1 -1
- package/dist/tools/orchestration/agent-persistence.d.ts +14 -0
- package/dist/tools/orchestration/agent-persistence.js +43 -0
- package/dist/tools/orchestration/agent-persistence.js.map +1 -0
- package/dist/tools/orchestration/blueprint-accumulator.d.ts +4 -8
- package/dist/tools/orchestration/blueprint-accumulator.js +11 -78
- package/dist/tools/orchestration/blueprint-accumulator.js.map +1 -1
- package/dist/tools/orchestration/blueprint.schema.d.ts +13 -147
- package/dist/tools/orchestration/blueprint.schema.js +4 -23
- package/dist/tools/orchestration/blueprint.schema.js.map +1 -1
- package/dist/tools/orchestration/browser-credential-setup.nudge.d.ts +2 -4
- package/dist/tools/orchestration/browser-credential-setup.nudge.js +1 -1
- package/dist/tools/orchestration/browser-credential-setup.nudge.js.map +1 -1
- package/dist/tools/orchestration/browser-credential-setup.tool.d.ts +10 -2
- package/dist/tools/orchestration/browser-credential-setup.tool.js +299 -270
- package/dist/tools/orchestration/browser-credential-setup.tool.js.map +1 -1
- package/dist/tools/orchestration/build-workflow-agent.prompt.d.ts +7 -1
- package/dist/tools/orchestration/build-workflow-agent.prompt.js +46 -20
- package/dist/tools/orchestration/build-workflow-agent.prompt.js.map +1 -1
- package/dist/tools/orchestration/build-workflow-agent.tool.d.ts +50 -5
- package/dist/tools/orchestration/build-workflow-agent.tool.js +748 -605
- package/dist/tools/orchestration/build-workflow-agent.tool.js.map +1 -1
- package/dist/tools/orchestration/builder-memory-compaction.d.ts +6 -2
- package/dist/tools/orchestration/builder-memory-compaction.js +32 -46
- package/dist/tools/orchestration/builder-memory-compaction.js.map +1 -1
- package/dist/tools/orchestration/complete-checkpoint.tool.d.ts +1 -1
- package/dist/tools/orchestration/complete-checkpoint.tool.js +46 -47
- package/dist/tools/orchestration/complete-checkpoint.tool.js.map +1 -1
- package/dist/tools/orchestration/delegate.tool.d.ts +1 -1
- package/dist/tools/orchestration/delegate.tool.js +170 -166
- package/dist/tools/orchestration/delegate.tool.js.map +1 -1
- package/dist/tools/orchestration/eval-data-agent.tool.d.ts +2 -0
- package/dist/tools/orchestration/eval-data-agent.tool.js +183 -0
- package/dist/tools/orchestration/eval-data-agent.tool.js.map +1 -0
- package/dist/tools/orchestration/eval-setup-agent.prompt.d.ts +1 -0
- package/dist/tools/orchestration/eval-setup-agent.prompt.js +238 -0
- package/dist/tools/orchestration/eval-setup-agent.prompt.js.map +1 -0
- package/dist/tools/orchestration/eval-setup-agent.tool.d.ts +31 -0
- package/dist/tools/orchestration/eval-setup-agent.tool.js +204 -0
- package/dist/tools/orchestration/eval-setup-agent.tool.js.map +1 -0
- package/dist/tools/orchestration/plan-agent-prompt.d.ts +1 -1
- package/dist/tools/orchestration/plan-agent-prompt.js +9 -9
- package/dist/tools/orchestration/plan-with-agent.tool.d.ts +1 -1
- package/dist/tools/orchestration/plan-with-agent.tool.js +210 -179
- package/dist/tools/orchestration/plan-with-agent.tool.js.map +1 -1
- package/dist/tools/orchestration/plan.tool.d.ts +5 -2
- package/dist/tools/orchestration/plan.tool.js +125 -105
- package/dist/tools/orchestration/plan.tool.js.map +1 -1
- package/dist/tools/orchestration/report-verification-verdict.tool.d.ts +3 -3
- package/dist/tools/orchestration/report-verification-verdict.tool.js +65 -66
- package/dist/tools/orchestration/report-verification-verdict.tool.js.map +1 -1
- package/dist/tools/orchestration/submit-plan.tool.d.ts +1 -1
- package/dist/tools/orchestration/submit-plan.tool.js +83 -66
- package/dist/tools/orchestration/submit-plan.tool.js.map +1 -1
- package/dist/tools/orchestration/tracing-utils.d.ts +3 -2
- package/dist/tools/orchestration/tracing-utils.js +58 -39
- package/dist/tools/orchestration/tracing-utils.js.map +1 -1
- package/dist/tools/orchestration/verify-built-workflow.tool.d.ts +5 -5
- package/dist/tools/orchestration/verify-built-workflow.tool.js +166 -166
- package/dist/tools/orchestration/verify-built-workflow.tool.js.map +1 -1
- package/dist/tools/research.tool.d.ts +1 -1
- package/dist/tools/research.tool.js +59 -27
- package/dist/tools/research.tool.js.map +1 -1
- package/dist/tools/shared/ask-user.tool.d.ts +3 -2
- package/dist/tools/shared/ask-user.tool.js +61 -63
- package/dist/tools/shared/ask-user.tool.js.map +1 -1
- package/dist/tools/task-control.tool.d.ts +1 -1
- package/dist/tools/task-control.tool.js +15 -16
- package/dist/tools/task-control.tool.js.map +1 -1
- package/dist/tools/templates.tool.d.ts +1 -1
- package/dist/tools/templates.tool.js +6 -7
- package/dist/tools/templates.tool.js.map +1 -1
- package/dist/tools/tool-ids.d.ts +40 -0
- package/dist/tools/tool-ids.js +62 -0
- package/dist/tools/tool-ids.js.map +1 -0
- package/dist/tools/workflows/apply-workflow-credentials.tool.d.ts +3 -3
- package/dist/tools/workflows/apply-workflow-credentials.tool.js +72 -73
- package/dist/tools/workflows/apply-workflow-credentials.tool.js.map +1 -1
- package/dist/tools/workflows/build-workflow.tool.d.ts +3 -3
- package/dist/tools/workflows/build-workflow.tool.js +113 -114
- package/dist/tools/workflows/build-workflow.tool.js.map +1 -1
- package/dist/tools/workflows/materialize-node-type.tool.d.ts +6 -6
- package/dist/tools/workflows/materialize-node-type.tool.js +64 -65
- package/dist/tools/workflows/materialize-node-type.tool.js.map +1 -1
- package/dist/tools/workflows/resolve-credentials.d.ts +3 -5
- package/dist/tools/workflows/resolve-credentials.js +15 -6
- package/dist/tools/workflows/resolve-credentials.js.map +1 -1
- package/dist/tools/workflows/setup-workflow.schema.d.ts +2 -2
- package/dist/tools/workflows/submit-workflow-identity.d.ts +8 -9
- package/dist/tools/workflows/submit-workflow-identity.js +41 -27
- package/dist/tools/workflows/submit-workflow-identity.js.map +1 -1
- package/dist/tools/workflows/submit-workflow.tool.d.ts +10 -7
- package/dist/tools/workflows/submit-workflow.tool.js +224 -191
- package/dist/tools/workflows/submit-workflow.tool.js.map +1 -1
- package/dist/tools/workflows/validate-workflow.service.d.ts +15 -0
- package/dist/tools/workflows/validate-workflow.service.js +289 -0
- package/dist/tools/workflows/validate-workflow.service.js.map +1 -0
- package/dist/tools/workflows/write-sandbox-file.tool.d.ts +2 -2
- package/dist/tools/workflows/write-sandbox-file.tool.js +29 -30
- package/dist/tools/workflows/write-sandbox-file.tool.js.map +1 -1
- package/dist/tools/workflows.tool.d.ts +5 -2
- package/dist/tools/workflows.tool.js +184 -85
- package/dist/tools/workflows.tool.js.map +1 -1
- package/dist/tools/workspace.tool.d.ts +1 -1
- package/dist/tools/workspace.tool.js +49 -65
- package/dist/tools/workspace.tool.js.map +1 -1
- package/dist/tracing/langsmith-tracing.d.ts +17 -18
- package/dist/tracing/langsmith-tracing.js +941 -677
- package/dist/tracing/langsmith-tracing.js.map +1 -1
- package/dist/tracing/trace-labels.d.ts +5 -0
- package/dist/tracing/trace-labels.js +36 -0
- package/dist/tracing/trace-labels.js.map +1 -0
- package/dist/tracing/trace-payloads.d.ts +23 -0
- package/dist/tracing/trace-payloads.js +1008 -0
- package/dist/tracing/trace-payloads.js.map +1 -0
- package/dist/types.d.ts +57 -25
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -1
- package/dist/utils/eval-agents.js +1 -1
- package/dist/utils/eval-agents.js.map +1 -1
- package/dist/utils/scrub-secrets.js +1 -1
- package/dist/utils/scrub-secrets.js.map +1 -1
- package/dist/utils/stream-helpers.d.ts +2 -5
- package/dist/utils/stream-helpers.js +12 -1
- package/dist/utils/stream-helpers.js.map +1 -1
- package/dist/workflow-loop/workflow-loop-state.d.ts +18 -18
- package/dist/workspace/builder-templates-service.d.ts +54 -0
- package/dist/workspace/builder-templates-service.js +442 -0
- package/dist/workspace/builder-templates-service.js.map +1 -0
- package/dist/workspace/create-workspace.d.ts +16 -3
- package/dist/workspace/create-workspace.js +45 -21
- package/dist/workspace/create-workspace.js.map +1 -1
- package/dist/workspace/daytona-auth-manager.d.ts +26 -0
- package/dist/workspace/daytona-auth-manager.js +90 -0
- package/dist/workspace/daytona-auth-manager.js.map +1 -0
- package/dist/workspace/daytona-filesystem.d.ts +5 -4
- package/dist/workspace/daytona-filesystem.js +25 -15
- package/dist/workspace/daytona-filesystem.js.map +1 -1
- package/dist/workspace/daytona-sandbox.d.ts +65 -0
- package/dist/workspace/daytona-sandbox.js +314 -0
- package/dist/workspace/daytona-sandbox.js.map +1 -0
- package/dist/workspace/lazy-daytona.d.ts +2 -0
- package/dist/workspace/lazy-daytona.js +12 -0
- package/dist/workspace/lazy-daytona.js.map +1 -0
- package/dist/workspace/lazy-runtime-workspace.d.ts +8 -0
- package/dist/workspace/lazy-runtime-workspace.js +246 -0
- package/dist/workspace/lazy-runtime-workspace.js.map +1 -0
- package/dist/workspace/local-filesystem.d.ts +48 -0
- package/dist/workspace/local-filesystem.js +250 -0
- package/dist/workspace/local-filesystem.js.map +1 -0
- package/dist/workspace/local-sandbox.d.ts +28 -0
- package/dist/workspace/local-sandbox.js +121 -0
- package/dist/workspace/local-sandbox.js.map +1 -0
- package/dist/workspace/n8n-sandbox-filesystem.d.ts +3 -3
- package/dist/workspace/n8n-sandbox-filesystem.js +3 -3
- package/dist/workspace/n8n-sandbox-filesystem.js.map +1 -1
- package/dist/workspace/n8n-sandbox-sandbox.d.ts +12 -3
- package/dist/workspace/n8n-sandbox-sandbox.js +54 -10
- package/dist/workspace/n8n-sandbox-sandbox.js.map +1 -1
- package/dist/workspace/sandbox-fs.d.ts +35 -4
- package/dist/workspace/sandbox-fs.js +1 -1
- package/dist/workspace/sandbox-fs.js.map +1 -1
- package/dist/workspace/sandbox-setup.d.ts +13 -4
- package/dist/workspace/sandbox-setup.js +250 -36
- package/dist/workspace/sandbox-setup.js.map +1 -1
- package/dist/workspace/scoped-workspace.d.ts +2 -0
- package/dist/workspace/scoped-workspace.js +141 -0
- package/dist/workspace/scoped-workspace.js.map +1 -0
- package/dist/workspace/snapshot-manager.d.ts +11 -5
- package/dist/workspace/snapshot-manager.js +74 -17
- package/dist/workspace/snapshot-manager.js.map +1 -1
- package/dist/workspace/template-telemetry.d.ts +4 -4
- package/dist/workspace/template-telemetry.js +18 -6
- package/dist/workspace/template-telemetry.js.map +1 -1
- package/package.json +13 -16
- package/skills/data-table-manager/SKILL.md +119 -0
- package/skills/data-table-manager/references/data-table-playbook.md +212 -0
- package/dist/agent/register-with-mastra.d.ts +0 -3
- package/dist/agent/register-with-mastra.js +0 -15
- package/dist/agent/register-with-mastra.js.map +0 -1
- package/dist/compaction/compaction-helper.d.ts +0 -9
- package/dist/compaction/compaction-helper.js +0 -58
- package/dist/compaction/compaction-helper.js.map +0 -1
- package/dist/compaction/index.d.ts +0 -2
- package/dist/compaction/index.js +0 -6
- package/dist/compaction/index.js.map +0 -1
- package/dist/memory/memory-config.d.ts +0 -3
- package/dist/memory/memory-config.js +0 -27
- package/dist/memory/memory-config.js.map +0 -1
- package/dist/runtime/builder-sandbox-session-registry.d.ts +0 -47
- package/dist/runtime/builder-sandbox-session-registry.js +0 -166
- package/dist/runtime/builder-sandbox-session-registry.js.map +0 -1
- package/dist/storage/mastra-iteration-log-storage.js.map +0 -1
- package/dist/storage/mastra-task-storage.js.map +0 -1
- package/dist/tools/orchestration/data-table-agent.prompt.d.ts +0 -1
- package/dist/tools/orchestration/data-table-agent.prompt.js +0 -47
- package/dist/tools/orchestration/data-table-agent.prompt.js.map +0 -1
- package/dist/tools/orchestration/data-table-agent.tool.d.ts +0 -26
- package/dist/tools/orchestration/data-table-agent.tool.js +0 -174
- package/dist/tools/orchestration/data-table-agent.tool.js.map +0 -1
- package/dist/tools/orchestration/research-agent-prompt.d.ts +0 -1
- package/dist/tools/orchestration/research-agent-prompt.js +0 -26
- package/dist/tools/orchestration/research-agent-prompt.js.map +0 -1
- package/dist/tools/orchestration/research-with-agent.tool.d.ts +0 -30
- package/dist/tools/orchestration/research-with-agent.tool.js +0 -179
- package/dist/tools/orchestration/research-with-agent.tool.js.map +0 -1
- package/dist/workspace/builder-sandbox-factory.d.ts +0 -30
- package/dist/workspace/builder-sandbox-factory.js +0 -289
- package/dist/workspace/builder-sandbox-factory.js.map +0 -1
- package/dist/workspace/guarded-filesystem.d.ts +0 -10
- package/dist/workspace/guarded-filesystem.js +0 -136
- package/dist/workspace/guarded-filesystem.js.map +0 -1
|
@@ -1,1331 +1,129 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.normalizeStreamSource = normalizeStreamSource;
|
|
4
4
|
exports.executeResumableStream = executeResumableStream;
|
|
5
5
|
const map_chunk_1 = require("../stream/map-chunk");
|
|
6
6
|
const work_summary_accumulator_1 = require("../stream/work-summary-accumulator");
|
|
7
|
-
const langsmith_tracing_1 = require("../tracing/langsmith-tracing");
|
|
8
7
|
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();
|
|
13
8
|
function isRecord(value) {
|
|
14
9
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
15
10
|
}
|
|
16
|
-
function
|
|
17
|
-
return
|
|
11
|
+
function isAsyncIterable(value) {
|
|
12
|
+
return (value !== null &&
|
|
13
|
+
typeof value === 'object' &&
|
|
14
|
+
typeof Reflect.get(value, Symbol.asyncIterator) === 'function');
|
|
18
15
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
function isReadableStream(value) {
|
|
17
|
+
return (value !== null &&
|
|
18
|
+
typeof value === 'object' &&
|
|
19
|
+
typeof Reflect.get(value, 'getReader') === 'function');
|
|
22
20
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
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;
|
|
21
|
+
function isResumableStreamSource(value) {
|
|
22
|
+
return isRecord(value) && isAsyncIterable(value.fullStream);
|
|
355
23
|
}
|
|
356
|
-
function
|
|
357
|
-
|
|
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 };
|
|
24
|
+
function isNativeStreamResult(value) {
|
|
25
|
+
return isRecord(value) && isReadableStream(value.stream);
|
|
404
26
|
}
|
|
405
|
-
function
|
|
406
|
-
if (
|
|
407
|
-
|
|
27
|
+
function extractContentText(content) {
|
|
28
|
+
if (typeof content === 'string') {
|
|
29
|
+
return content;
|
|
408
30
|
}
|
|
409
|
-
if (
|
|
410
|
-
|
|
31
|
+
if (!Array.isArray(content)) {
|
|
32
|
+
return '';
|
|
411
33
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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];
|
|
34
|
+
return content
|
|
35
|
+
.map((part) => {
|
|
36
|
+
if (!isRecord(part) || part.type !== 'text') {
|
|
37
|
+
return '';
|
|
454
38
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
if (Array.isArray(value.parts)) {
|
|
460
|
-
return value.parts.flatMap((entry) => extractTextParts(entry));
|
|
461
|
-
}
|
|
462
|
-
return [];
|
|
39
|
+
return typeof part.text === 'string' ? part.text : '';
|
|
40
|
+
})
|
|
41
|
+
.join('');
|
|
463
42
|
}
|
|
464
|
-
function
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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)) {
|
|
43
|
+
async function collectNativeStreamText(stream) {
|
|
44
|
+
const reader = stream.getReader();
|
|
45
|
+
let deltaText = '';
|
|
46
|
+
let messageText = '';
|
|
47
|
+
try {
|
|
48
|
+
while (true) {
|
|
49
|
+
const { done, value } = await reader.read();
|
|
50
|
+
if (done)
|
|
51
|
+
return deltaText || messageText;
|
|
52
|
+
if (!isRecord(value)) {
|
|
487
53
|
continue;
|
|
488
54
|
}
|
|
489
|
-
if (
|
|
490
|
-
|
|
55
|
+
if (value.type === 'text-delta' && typeof value.delta === 'string') {
|
|
56
|
+
deltaText += value.delta;
|
|
57
|
+
continue;
|
|
491
58
|
}
|
|
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);
|
|
59
|
+
if (value.type !== 'message' || !isRecord(value.message)) {
|
|
60
|
+
continue;
|
|
498
61
|
}
|
|
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 buildSyntheticToolInputs(toolCallId, _toolName, args) {
|
|
643
|
-
return sanitizeTracePayload({
|
|
644
|
-
toolCallId,
|
|
645
|
-
args,
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
function shouldCreateSyntheticToolTrace(payload) {
|
|
649
|
-
const toolName = typeof payload.toolName === 'string' ? payload.toolName : '';
|
|
650
|
-
return (toolName.startsWith('mastra_') ||
|
|
651
|
-
SYNTHETIC_TOOL_TRACE_NAMES.has(toolName) ||
|
|
652
|
-
payload.providerExecuted === true ||
|
|
653
|
-
payload.dynamic === true);
|
|
654
|
-
}
|
|
655
|
-
async function startSyntheticToolTrace(chunk, records) {
|
|
656
|
-
if (!isRecord(chunk) || chunk.type !== 'tool-call') {
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
const payload = getChunkPayload(chunk);
|
|
660
|
-
if (!payload || !shouldCreateSyntheticToolTrace(payload)) {
|
|
661
|
-
return;
|
|
662
|
-
}
|
|
663
|
-
const toolCallId = typeof payload.toolCallId === 'string' ? payload.toolCallId : '';
|
|
664
|
-
const toolName = typeof payload.toolName === 'string' ? payload.toolName : '';
|
|
665
|
-
if (!toolCallId || !toolName || records.has(toolCallId)) {
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
const parentRun = (0, langsmith_tracing_1.getTraceParentRun)();
|
|
669
|
-
if (!parentRun) {
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
const runTree = parentRun.createChild({
|
|
673
|
-
name: `tool:${toolName}`,
|
|
674
|
-
run_type: 'tool',
|
|
675
|
-
tags: dedupeTags([
|
|
676
|
-
...(parentRun.tags ?? []),
|
|
677
|
-
'tool',
|
|
678
|
-
...(toolName.startsWith('mastra_') ? ['native-tool'] : []),
|
|
679
|
-
]),
|
|
680
|
-
metadata: {
|
|
681
|
-
...(parentRun.metadata ?? {}),
|
|
682
|
-
tool_name: toolName,
|
|
683
|
-
synthetic_tool_trace: true,
|
|
684
|
-
...(payload.providerExecuted === true ? { provider_executed: true } : {}),
|
|
685
|
-
...(payload.dynamic === true ? { dynamic_tool: true } : {}),
|
|
686
|
-
},
|
|
687
|
-
inputs: buildSyntheticToolInputs(toolCallId, toolName, payload.args),
|
|
688
|
-
});
|
|
689
|
-
await runTree.postRun();
|
|
690
|
-
records.set(toolCallId, {
|
|
691
|
-
toolCallId,
|
|
692
|
-
toolName,
|
|
693
|
-
runTree,
|
|
694
|
-
finished: false,
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
async function finishSyntheticToolTrace(chunk, records) {
|
|
698
|
-
if (!isRecord(chunk) || (chunk.type !== 'tool-result' && chunk.type !== 'tool-error')) {
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
|
-
const payload = getChunkPayload(chunk);
|
|
702
|
-
if (!payload) {
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
const toolCallId = typeof payload.toolCallId === 'string' ? payload.toolCallId : '';
|
|
706
|
-
if (!toolCallId) {
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
if (!records.has(toolCallId)) {
|
|
710
|
-
if (!shouldCreateSyntheticToolTrace(payload)) {
|
|
711
|
-
return;
|
|
712
|
-
}
|
|
713
|
-
await startSyntheticToolTrace({
|
|
714
|
-
type: 'tool-call',
|
|
715
|
-
payload,
|
|
716
|
-
}, records);
|
|
717
|
-
}
|
|
718
|
-
const record = records.get(toolCallId);
|
|
719
|
-
if (!record || record.finished) {
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
record.finished = true;
|
|
723
|
-
await finishRunTree(record.runTree, {
|
|
724
|
-
outputs: sanitizeTracePayload({
|
|
725
|
-
result: payload.result,
|
|
726
|
-
}),
|
|
727
|
-
...(payload.isError === true
|
|
728
|
-
? {
|
|
729
|
-
error: typeof payload.result === 'string'
|
|
730
|
-
? payload.result
|
|
731
|
-
: typeof payload.error === 'string'
|
|
732
|
-
? payload.error
|
|
733
|
-
: 'Tool execution failed',
|
|
62
|
+
if (value.message.role === 'assistant') {
|
|
63
|
+
messageText += extractContentText(value.message.content);
|
|
734
64
|
}
|
|
735
|
-
: {}),
|
|
736
|
-
metadata: {
|
|
737
|
-
final_status: payload.isError === true ? 'error' : 'completed',
|
|
738
|
-
},
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
async function finalizeSyntheticToolTraces(records, options) {
|
|
742
|
-
for (const record of records.values()) {
|
|
743
|
-
if (record.finished) {
|
|
744
|
-
continue;
|
|
745
65
|
}
|
|
746
|
-
record.finished = true;
|
|
747
|
-
await finishRunTree(record.runTree, {
|
|
748
|
-
outputs: sanitizeTracePayload({
|
|
749
|
-
status: options?.status ?? 'completed',
|
|
750
|
-
}),
|
|
751
|
-
...(options?.error ? { error: options.error } : {}),
|
|
752
|
-
metadata: {
|
|
753
|
-
final_status: options?.status ?? 'completed',
|
|
754
|
-
},
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
async function startLlmStepTrace(parentRun, messageId, request, stepNumber) {
|
|
759
|
-
const resolvedParentRun = parentRun ?? (0, langsmith_tracing_1.getTraceParentRun)();
|
|
760
|
-
if (!resolvedParentRun) {
|
|
761
|
-
return undefined;
|
|
762
|
-
}
|
|
763
|
-
const inputs = buildLlmInputPayload(request);
|
|
764
|
-
const model = normalizeModelMetadata(resolvedParentRun.metadata?.model_id);
|
|
765
|
-
const runTree = resolvedParentRun.createChild({
|
|
766
|
-
name: formatLlmRunName(model),
|
|
767
|
-
run_type: 'llm',
|
|
768
|
-
tags: dedupeTags([...(resolvedParentRun.tags ?? []), 'llm']),
|
|
769
|
-
metadata: {
|
|
770
|
-
...(resolvedParentRun.metadata ?? {}),
|
|
771
|
-
step_message_id: messageId,
|
|
772
|
-
...(typeof stepNumber === 'number' ? { step_number: stepNumber + 1 } : {}),
|
|
773
|
-
...(model.provider ? { ls_provider: model.provider } : {}),
|
|
774
|
-
...(model.modelName ? { ls_model_name: model.modelName } : {}),
|
|
775
|
-
},
|
|
776
|
-
inputs,
|
|
777
|
-
});
|
|
778
|
-
await runTree.postRun();
|
|
779
|
-
return {
|
|
780
|
-
messageId,
|
|
781
|
-
stepNumber,
|
|
782
|
-
runTree,
|
|
783
|
-
model,
|
|
784
|
-
inputs,
|
|
785
|
-
textParts: [],
|
|
786
|
-
reasoningParts: [],
|
|
787
|
-
toolCalls: [],
|
|
788
|
-
toolResults: [],
|
|
789
|
-
request,
|
|
790
|
-
recordedFirstToken: false,
|
|
791
|
-
finished: false,
|
|
792
|
-
};
|
|
793
|
-
}
|
|
794
|
-
function findActiveStepRecord(records, messageId) {
|
|
795
|
-
if (messageId) {
|
|
796
|
-
return records.find((record) => record.messageId === messageId && !record.finished);
|
|
797
|
-
}
|
|
798
|
-
for (let index = records.length - 1; index >= 0; index--) {
|
|
799
|
-
if (!records[index].finished) {
|
|
800
|
-
return records[index];
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
return undefined;
|
|
804
|
-
}
|
|
805
|
-
function recordFirstTokenEvent(record) {
|
|
806
|
-
if (record.recordedFirstToken) {
|
|
807
|
-
return;
|
|
808
|
-
}
|
|
809
|
-
record.runTree.addEvent({ name: 'new_token', time: new Date().toISOString() });
|
|
810
|
-
record.recordedFirstToken = true;
|
|
811
|
-
}
|
|
812
|
-
function updateStepRecordFromChunk(chunk, records) {
|
|
813
|
-
if (!isRecord(chunk) || typeof chunk.type !== 'string') {
|
|
814
|
-
return undefined;
|
|
815
|
-
}
|
|
816
|
-
if (chunk.type === 'step-start' && typeof chunk.messageId === 'string') {
|
|
817
|
-
return undefined;
|
|
818
|
-
}
|
|
819
|
-
const record = findActiveStepRecord(records);
|
|
820
|
-
if (!record) {
|
|
821
|
-
return undefined;
|
|
822
|
-
}
|
|
823
|
-
const payload = isRecord(chunk.payload) ? chunk.payload : chunk;
|
|
824
|
-
if ((chunk.type === 'text-delta' || chunk.type === 'text') && typeof payload.text === 'string') {
|
|
825
|
-
record.textParts.push(payload.text);
|
|
826
|
-
recordFirstTokenEvent(record);
|
|
827
|
-
}
|
|
828
|
-
if ((chunk.type === 'reasoning-delta' || chunk.type === 'reasoning') &&
|
|
829
|
-
typeof payload.text === 'string') {
|
|
830
|
-
record.reasoningParts.push(payload.text);
|
|
831
|
-
}
|
|
832
|
-
if (chunk.type === 'tool-call' && isRecord(payload)) {
|
|
833
|
-
record.toolCalls.push(toTraceObject(payload));
|
|
834
|
-
}
|
|
835
|
-
if ((chunk.type === 'tool-result' || chunk.type === 'tool-error') && isRecord(payload)) {
|
|
836
|
-
record.toolResults.push(toTraceObject(payload));
|
|
837
|
-
}
|
|
838
|
-
return record;
|
|
839
|
-
}
|
|
840
|
-
function applyStepFinishChunk(chunk, records) {
|
|
841
|
-
if (!isRecord(chunk) || chunk.type !== 'step-finish') {
|
|
842
|
-
return undefined;
|
|
843
|
-
}
|
|
844
|
-
const payload = getChunkPayload(chunk);
|
|
845
|
-
const messageId = typeof payload?.messageId === 'string' ? payload.messageId : undefined;
|
|
846
|
-
const record = findActiveStepRecord(records, messageId);
|
|
847
|
-
if (!record) {
|
|
848
|
-
return undefined;
|
|
849
|
-
}
|
|
850
|
-
const output = isRecord(payload?.output) ? payload.output : undefined;
|
|
851
|
-
const stepResult = isRecord(payload?.stepResult) ? payload.stepResult : undefined;
|
|
852
|
-
const metadata = isRecord(payload?.metadata) ? payload.metadata : undefined;
|
|
853
|
-
record.finishReason =
|
|
854
|
-
(stepResult && typeof stepResult.reason === 'string' ? stepResult.reason : undefined) ??
|
|
855
|
-
(payload && typeof payload.finishReason === 'string' ? payload.finishReason : undefined) ??
|
|
856
|
-
record.finishReason;
|
|
857
|
-
const usage = output?.usage ?? payload?.usage;
|
|
858
|
-
if (usage !== undefined) {
|
|
859
|
-
record.usage = usage;
|
|
860
|
-
}
|
|
861
|
-
if (payload?.response !== undefined) {
|
|
862
|
-
record.response = payload.response;
|
|
863
|
-
}
|
|
864
|
-
const request = metadata?.request ?? payload?.request;
|
|
865
|
-
if (request !== undefined) {
|
|
866
|
-
record.request = request;
|
|
867
|
-
}
|
|
868
|
-
const providerMetadata = metadata?.providerMetadata ?? payload?.providerMetadata;
|
|
869
|
-
if (providerMetadata !== undefined) {
|
|
870
|
-
record.providerMetadata = providerMetadata;
|
|
871
|
-
}
|
|
872
|
-
record.warnings =
|
|
873
|
-
(stepResult && Array.isArray(stepResult.warnings) ? stepResult.warnings : undefined) ??
|
|
874
|
-
payload?.warnings ??
|
|
875
|
-
record.warnings;
|
|
876
|
-
record.isContinued =
|
|
877
|
-
stepResult?.isContinued === true ? true : payload?.isContinued === true || record.isContinued;
|
|
878
|
-
if (output && typeof output.text === 'string') {
|
|
879
|
-
record.textParts = output.text.length > 0 ? [output.text] : [];
|
|
880
|
-
}
|
|
881
|
-
if (output && Array.isArray(output.toolCalls)) {
|
|
882
|
-
record.toolCalls = output.toolCalls.map((entry) => toTraceObject(entry));
|
|
883
|
-
}
|
|
884
|
-
if (output && Array.isArray(output.toolResults)) {
|
|
885
|
-
record.toolResults = output.toolResults.map((entry) => toTraceObject(entry));
|
|
886
|
-
}
|
|
887
|
-
const responseModelId = payload && isRecord(payload.response) && typeof payload.response.modelId === 'string'
|
|
888
|
-
? payload.response.modelId
|
|
889
|
-
: undefined;
|
|
890
|
-
if (responseModelId) {
|
|
891
|
-
const model = normalizeModelMetadata(responseModelId);
|
|
892
|
-
record.model = {
|
|
893
|
-
provider: model.provider ?? record.model.provider,
|
|
894
|
-
modelName: model.modelName ?? responseModelId,
|
|
895
|
-
};
|
|
896
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
897
|
-
record.runTree.metadata = {
|
|
898
|
-
...(record.runTree.metadata ?? {}),
|
|
899
|
-
...(record.model.provider ? { ls_provider: record.model.provider } : {}),
|
|
900
|
-
...(record.model.modelName ? { ls_model_name: record.model.modelName } : {}),
|
|
901
|
-
};
|
|
902
|
-
}
|
|
903
|
-
return record;
|
|
904
|
-
}
|
|
905
|
-
function isStepResultLike(value) {
|
|
906
|
-
return isRecord(value);
|
|
907
|
-
}
|
|
908
|
-
function toStepResultLike(value) {
|
|
909
|
-
return isStepResultLike(value) ? value : undefined;
|
|
910
|
-
}
|
|
911
|
-
function toStepStartLike(value) {
|
|
912
|
-
return isRecord(value) ? value : undefined;
|
|
913
|
-
}
|
|
914
|
-
function getSyntheticStepMessageId(stepResult, index) {
|
|
915
|
-
const messages = extractResponseMessages(stepResult.response);
|
|
916
|
-
const messageId = messages?.find((message) => isRecord(message) && typeof message.id === 'string')?.id;
|
|
917
|
-
return typeof messageId === 'string' ? messageId : `step-${index + 1}`;
|
|
918
|
-
}
|
|
919
|
-
async function createFallbackStepTrace(parentRun, stepResult, index) {
|
|
920
|
-
const record = await startLlmStepTrace(parentRun, getSyntheticStepMessageId(stepResult, index), stepResult.request, stepResult.stepNumber ?? index);
|
|
921
|
-
if (!record) {
|
|
922
|
-
return undefined;
|
|
923
|
-
}
|
|
924
|
-
if (typeof stepResult.text === 'string' && stepResult.text.length > 0) {
|
|
925
|
-
record.textParts.push(stepResult.text);
|
|
926
|
-
recordFirstTokenEvent(record);
|
|
927
|
-
}
|
|
928
|
-
if (typeof stepResult.reasoning === 'string' && stepResult.reasoning.length > 0) {
|
|
929
|
-
record.reasoningParts.push(stepResult.reasoning);
|
|
930
|
-
}
|
|
931
|
-
else if (Array.isArray(stepResult.reasoning)) {
|
|
932
|
-
record.reasoningParts.push(...stepResult.reasoning
|
|
933
|
-
.map((entry) => isRecord(entry) && typeof entry.text === 'string' ? entry.text : undefined)
|
|
934
|
-
.filter((entry) => entry !== undefined));
|
|
935
|
-
}
|
|
936
|
-
if (Array.isArray(stepResult.toolCalls)) {
|
|
937
|
-
record.toolCalls.push(...stepResult.toolCalls.map((entry) => toTraceObject(entry)));
|
|
938
|
-
}
|
|
939
|
-
if (Array.isArray(stepResult.toolResults)) {
|
|
940
|
-
record.toolResults.push(...stepResult.toolResults.map((entry) => toTraceObject(entry)));
|
|
941
|
-
}
|
|
942
|
-
record.finishReason = stepResult.finishReason;
|
|
943
|
-
if (stepResult.usage !== undefined) {
|
|
944
|
-
record.usage = stepResult.usage;
|
|
945
|
-
}
|
|
946
|
-
if (stepResult.request !== undefined) {
|
|
947
|
-
record.request = stepResult.request;
|
|
948
|
-
}
|
|
949
|
-
if (stepResult.response !== undefined) {
|
|
950
|
-
record.response = stepResult.response;
|
|
951
|
-
}
|
|
952
|
-
if (stepResult.providerMetadata !== undefined) {
|
|
953
|
-
record.providerMetadata = stepResult.providerMetadata;
|
|
954
|
-
}
|
|
955
|
-
const responseModelId = isRecord(stepResult.response) && typeof stepResult.response.modelId === 'string'
|
|
956
|
-
? stepResult.response.modelId
|
|
957
|
-
: undefined;
|
|
958
|
-
if (responseModelId) {
|
|
959
|
-
const model = normalizeModelMetadata(responseModelId);
|
|
960
|
-
record.model = {
|
|
961
|
-
provider: model.provider ?? record.model.provider,
|
|
962
|
-
modelName: model.modelName ?? responseModelId,
|
|
963
|
-
};
|
|
964
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
965
|
-
}
|
|
966
|
-
return record;
|
|
967
|
-
}
|
|
968
|
-
function applyStepResultToRecord(record, stepResult) {
|
|
969
|
-
if (typeof stepResult.text === 'string') {
|
|
970
|
-
record.textParts = stepResult.text.length > 0 ? [stepResult.text] : [];
|
|
971
|
-
}
|
|
972
|
-
if (typeof stepResult.reasoning === 'string') {
|
|
973
|
-
record.reasoningParts = stepResult.reasoning.length > 0 ? [stepResult.reasoning] : [];
|
|
974
|
-
}
|
|
975
|
-
else if (Array.isArray(stepResult.reasoning)) {
|
|
976
|
-
record.reasoningParts = stepResult.reasoning
|
|
977
|
-
.map((entry) => (isRecord(entry) && typeof entry.text === 'string' ? entry.text : undefined))
|
|
978
|
-
.filter((entry) => entry !== undefined);
|
|
979
|
-
}
|
|
980
|
-
if (Array.isArray(stepResult.toolCalls)) {
|
|
981
|
-
record.toolCalls = stepResult.toolCalls.map((entry) => toTraceObject(entry));
|
|
982
|
-
}
|
|
983
|
-
if (Array.isArray(stepResult.toolResults)) {
|
|
984
|
-
record.toolResults = stepResult.toolResults.map((entry) => toTraceObject(entry));
|
|
985
|
-
}
|
|
986
|
-
if (typeof stepResult.finishReason === 'string') {
|
|
987
|
-
record.finishReason = stepResult.finishReason;
|
|
988
|
-
}
|
|
989
|
-
if (stepResult.usage !== undefined) {
|
|
990
|
-
record.usage = stepResult.usage;
|
|
991
66
|
}
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
}
|
|
995
|
-
if (stepResult.response !== undefined) {
|
|
996
|
-
record.response = stepResult.response;
|
|
997
|
-
}
|
|
998
|
-
if (stepResult.providerMetadata !== undefined) {
|
|
999
|
-
record.providerMetadata = stepResult.providerMetadata;
|
|
1000
|
-
}
|
|
1001
|
-
const responseModelId = isRecord(stepResult.response) && typeof stepResult.response.modelId === 'string'
|
|
1002
|
-
? stepResult.response.modelId
|
|
1003
|
-
: undefined;
|
|
1004
|
-
if (responseModelId) {
|
|
1005
|
-
const model = normalizeModelMetadata(responseModelId);
|
|
1006
|
-
record.model = {
|
|
1007
|
-
provider: model.provider ?? record.model.provider,
|
|
1008
|
-
modelName: model.modelName ?? responseModelId,
|
|
1009
|
-
};
|
|
1010
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
67
|
+
finally {
|
|
68
|
+
reader.releaseLock();
|
|
1011
69
|
}
|
|
1012
70
|
}
|
|
1013
|
-
function
|
|
1014
|
-
const
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
const recordsByStepNumber = new Map();
|
|
1020
|
-
const getActiveRecord = () => {
|
|
1021
|
-
for (let index = records.length - 1; index >= 0; index--) {
|
|
1022
|
-
const record = records[index];
|
|
1023
|
-
if (!record.finished) {
|
|
1024
|
-
return record;
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
return undefined;
|
|
1028
|
-
};
|
|
1029
|
-
const restoreTraceParent = () => {
|
|
1030
|
-
(0, langsmith_tracing_1.setTraceParentOverride)(activeParentRun);
|
|
1031
|
-
};
|
|
1032
|
-
restoreTraceParent();
|
|
1033
|
-
const patchFinishedRecordIfNeeded = async (record, stepResult, options) => {
|
|
1034
|
-
const metadata = {
|
|
1035
|
-
...(record.runTree.metadata ?? {}),
|
|
1036
|
-
...buildLlmMetadata(record, stepResult),
|
|
1037
|
-
...(options?.status && options.status !== 'completed'
|
|
1038
|
-
? { final_status: options.status }
|
|
1039
|
-
: {}),
|
|
1040
|
-
};
|
|
1041
|
-
record.runTree.inputs = record.inputs;
|
|
1042
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
1043
|
-
record.runTree.metadata = metadata;
|
|
1044
|
-
const endTimeValue = Reflect.get(record.runTree, 'end_time');
|
|
1045
|
-
const endTime = typeof endTimeValue === 'number' ? endTimeValue : undefined;
|
|
1046
|
-
await finishRunTree(record.runTree, {
|
|
1047
|
-
outputs: buildLlmOutputs(record, stepResult),
|
|
1048
|
-
metadata,
|
|
1049
|
-
...(options?.error ? { error: options.error } : {}),
|
|
1050
|
-
...(endTime !== undefined ? { endTime } : {}),
|
|
1051
|
-
});
|
|
1052
|
-
};
|
|
1053
|
-
const startStepTrace = async (options) => {
|
|
1054
|
-
const stepStart = toStepStartLike(options);
|
|
1055
|
-
if (typeof stepStart?.stepNumber !== 'number') {
|
|
1056
|
-
return undefined;
|
|
1057
|
-
}
|
|
1058
|
-
const existingRecord = recordsByStepNumber.get(stepStart.stepNumber);
|
|
1059
|
-
if (existingRecord && !existingRecord.finished) {
|
|
1060
|
-
(0, langsmith_tracing_1.setTraceParentOverride)(existingRecord.runTree);
|
|
1061
|
-
return existingRecord;
|
|
1062
|
-
}
|
|
1063
|
-
const record = await startLlmStepTrace(activeParentRun, `step-${stepStart.stepNumber + 1}`, {
|
|
1064
|
-
messages: Array.isArray(stepStart.messages) ? stepStart.messages : [],
|
|
1065
|
-
}, stepStart.stepNumber);
|
|
1066
|
-
if (!record) {
|
|
1067
|
-
return undefined;
|
|
1068
|
-
}
|
|
1069
|
-
const stepModelId = stepStart.model?.modelId;
|
|
1070
|
-
if (typeof stepModelId === 'string' && stepModelId.length > 0) {
|
|
1071
|
-
record.model = {
|
|
1072
|
-
provider: stepStart.model?.provider ?? record.model.provider,
|
|
1073
|
-
modelName: normalizeModelMetadata(stepModelId).modelName ?? stepModelId,
|
|
1074
|
-
};
|
|
1075
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
1076
|
-
record.runTree.metadata = {
|
|
1077
|
-
...(record.runTree.metadata ?? {}),
|
|
1078
|
-
...(record.model.provider ? { ls_provider: record.model.provider } : {}),
|
|
1079
|
-
...(record.model.modelName ? { ls_model_name: record.model.modelName } : {}),
|
|
1080
|
-
};
|
|
1081
|
-
}
|
|
1082
|
-
recordsByStepNumber.set(stepStart.stepNumber, record);
|
|
1083
|
-
records.push(record);
|
|
1084
|
-
(0, langsmith_tracing_1.setTraceParentOverride)(record.runTree);
|
|
1085
|
-
return record;
|
|
1086
|
-
};
|
|
1087
|
-
const prepareStep = async (options) => {
|
|
1088
|
-
await startStepTrace(options);
|
|
1089
|
-
return undefined;
|
|
1090
|
-
};
|
|
1091
|
-
const onStepStart = async (options) => {
|
|
1092
|
-
await startStepTrace(options);
|
|
1093
|
-
};
|
|
1094
|
-
const onStepFinish = async (stepResultValue) => {
|
|
1095
|
-
const stepResult = toStepResultLike(stepResultValue);
|
|
1096
|
-
if (!stepResult) {
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
const stepNumber = typeof stepResult.stepNumber === 'number' ? stepResult.stepNumber : undefined;
|
|
1100
|
-
const record = stepNumber !== undefined ? recordsByStepNumber.get(stepNumber) : undefined;
|
|
1101
|
-
if (!record || record.finished) {
|
|
1102
|
-
return;
|
|
1103
|
-
}
|
|
1104
|
-
applyStepResultToRecord(record, stepResult);
|
|
1105
|
-
record.runTree.inputs = record.inputs;
|
|
1106
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
1107
|
-
record.runTree.metadata = {
|
|
1108
|
-
...(record.runTree.metadata ?? {}),
|
|
1109
|
-
...buildLlmMetadata(record, stepResult),
|
|
1110
|
-
};
|
|
1111
|
-
await finishRunTree(record.runTree, {
|
|
1112
|
-
outputs: buildLlmOutputs(record, stepResult),
|
|
1113
|
-
metadata: record.runTree.metadata,
|
|
1114
|
-
});
|
|
1115
|
-
record.finished = true;
|
|
1116
|
-
if (stepNumber !== undefined) {
|
|
1117
|
-
recordsByStepNumber.delete(stepNumber);
|
|
1118
|
-
}
|
|
1119
|
-
restoreTraceParent();
|
|
1120
|
-
};
|
|
1121
|
-
return {
|
|
1122
|
-
executionOptions: {
|
|
1123
|
-
prepareStep,
|
|
1124
|
-
experimental_prepareStep: prepareStep,
|
|
1125
|
-
experimental_onStepStart: onStepStart,
|
|
1126
|
-
onStepFinish,
|
|
1127
|
-
experimental_telemetry: { isEnabled: false },
|
|
1128
|
-
},
|
|
1129
|
-
onStreamChunk: (chunk) => {
|
|
1130
|
-
updateStepRecordFromChunk(chunk, records);
|
|
1131
|
-
applyStepFinishChunk(chunk, records);
|
|
1132
|
-
},
|
|
1133
|
-
startSegment: () => {
|
|
1134
|
-
for (const [stepNumber, record] of recordsByStepNumber.entries()) {
|
|
1135
|
-
if (record.finished) {
|
|
1136
|
-
recordsByStepNumber.delete(stepNumber);
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
const activeRecord = getActiveRecord();
|
|
1140
|
-
if (activeRecord) {
|
|
1141
|
-
(0, langsmith_tracing_1.setTraceParentOverride)(activeRecord.runTree);
|
|
71
|
+
async function* readableStreamToAsyncIterable(stream) {
|
|
72
|
+
const reader = stream.getReader();
|
|
73
|
+
try {
|
|
74
|
+
while (true) {
|
|
75
|
+
const { done, value } = await reader.read();
|
|
76
|
+
if (done)
|
|
1142
77
|
return;
|
|
1143
|
-
|
|
1144
|
-
restoreTraceParent();
|
|
1145
|
-
},
|
|
1146
|
-
finalize: async (source, options) => {
|
|
1147
|
-
const resolvedSteps = await source.steps?.then((steps) => steps, () => undefined);
|
|
1148
|
-
const segmentUsage = await resolveSegmentUsage(source);
|
|
1149
|
-
const stepResults = Array.isArray(resolvedSteps)
|
|
1150
|
-
? resolvedSteps
|
|
1151
|
-
.map((stepValue) => toStepResultLike(stepValue))
|
|
1152
|
-
.filter((stepResult) => stepResult !== undefined)
|
|
1153
|
-
: [];
|
|
1154
|
-
const stepResultsByStepNumber = new Map();
|
|
1155
|
-
for (const stepResult of stepResults) {
|
|
1156
|
-
if (typeof stepResult.stepNumber === 'number') {
|
|
1157
|
-
stepResultsByStepNumber.set(stepResult.stepNumber, stepResult);
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
for (const [index, record] of records.entries()) {
|
|
1161
|
-
const stepResult = (typeof record.stepNumber === 'number'
|
|
1162
|
-
? stepResultsByStepNumber.get(record.stepNumber)
|
|
1163
|
-
: undefined) ?? stepResults[index];
|
|
1164
|
-
const hadUsageMetadata = buildUsageMetadata(record.usage, record.providerMetadata);
|
|
1165
|
-
const hadUsageMetadataJson = hadUsageMetadata
|
|
1166
|
-
? JSON.stringify(hadUsageMetadata)
|
|
1167
|
-
: undefined;
|
|
1168
|
-
const hadResponse = record.response !== undefined;
|
|
1169
|
-
const hadFinishReason = record.finishReason !== undefined;
|
|
1170
|
-
if (stepResult) {
|
|
1171
|
-
applyStepResultToRecord(record, stepResult);
|
|
1172
|
-
}
|
|
1173
|
-
maybeBackfillRecordUsageFromSegment(record, records, segmentUsage);
|
|
1174
|
-
if (record.finished) {
|
|
1175
|
-
const hasUsageMetadata = buildUsageMetadata(record.usage, record.providerMetadata);
|
|
1176
|
-
const hasUsageMetadataJson = hasUsageMetadata
|
|
1177
|
-
? JSON.stringify(hasUsageMetadata)
|
|
1178
|
-
: undefined;
|
|
1179
|
-
const hasResponse = record.response !== undefined;
|
|
1180
|
-
const hasFinishReason = record.finishReason !== undefined;
|
|
1181
|
-
if (hadUsageMetadataJson !== hasUsageMetadataJson ||
|
|
1182
|
-
(!hadResponse && hasResponse) ||
|
|
1183
|
-
(!hadFinishReason && hasFinishReason)) {
|
|
1184
|
-
await patchFinishedRecordIfNeeded(record, stepResult, options);
|
|
1185
|
-
}
|
|
1186
|
-
continue;
|
|
1187
|
-
}
|
|
1188
|
-
record.runTree.inputs = record.inputs;
|
|
1189
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
1190
|
-
record.runTree.metadata = {
|
|
1191
|
-
...(record.runTree.metadata ?? {}),
|
|
1192
|
-
...buildLlmMetadata(record, stepResult),
|
|
1193
|
-
...(options?.status && options.status !== 'completed'
|
|
1194
|
-
? { final_status: options.status }
|
|
1195
|
-
: {}),
|
|
1196
|
-
};
|
|
1197
|
-
await finishRunTree(record.runTree, {
|
|
1198
|
-
outputs: buildLlmOutputs(record, stepResult),
|
|
1199
|
-
metadata: record.runTree.metadata,
|
|
1200
|
-
...(options?.error ? { error: options.error } : {}),
|
|
1201
|
-
});
|
|
1202
|
-
record.finished = true;
|
|
1203
|
-
}
|
|
1204
|
-
restoreTraceParent();
|
|
1205
|
-
recordsByStepNumber.clear();
|
|
1206
|
-
},
|
|
1207
|
-
};
|
|
1208
|
-
}
|
|
1209
|
-
async function finalizeLlmStepTraces(source, records, options) {
|
|
1210
|
-
const parentRun = (0, langsmith_tracing_1.getTraceParentRun)();
|
|
1211
|
-
const resolvedSteps = await source.steps?.then((steps) => steps, () => undefined);
|
|
1212
|
-
const segmentUsage = await resolveSegmentUsage(source);
|
|
1213
|
-
const stepResults = Array.isArray(resolvedSteps)
|
|
1214
|
-
? resolvedSteps
|
|
1215
|
-
.map((stepValue) => toStepResultLike(stepValue))
|
|
1216
|
-
.filter((stepResult) => stepResult !== undefined)
|
|
1217
|
-
: [];
|
|
1218
|
-
const stepResultsByStepNumber = new Map();
|
|
1219
|
-
for (const stepResult of stepResults) {
|
|
1220
|
-
if (typeof stepResult.stepNumber === 'number') {
|
|
1221
|
-
stepResultsByStepNumber.set(stepResult.stepNumber, stepResult);
|
|
78
|
+
yield value;
|
|
1222
79
|
}
|
|
1223
80
|
}
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
const fallbackRecord = await createFallbackStepTrace(parentRun, stepResult, index);
|
|
1227
|
-
if (fallbackRecord) {
|
|
1228
|
-
records.push(fallbackRecord);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
81
|
+
finally {
|
|
82
|
+
reader.releaseLock();
|
|
1231
83
|
}
|
|
1232
|
-
|
|
1233
|
-
|
|
84
|
+
}
|
|
85
|
+
function normalizeStreamSource(result) {
|
|
86
|
+
if (isResumableStreamSource(result)) {
|
|
87
|
+
return result;
|
|
1234
88
|
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
: undefined) ?? stepResults[index];
|
|
1242
|
-
if (stepResult) {
|
|
1243
|
-
applyStepResultToRecord(record, stepResult);
|
|
1244
|
-
}
|
|
1245
|
-
maybeBackfillRecordUsageFromSegment(record, records, segmentUsage);
|
|
1246
|
-
record.runTree.inputs = record.inputs;
|
|
1247
|
-
record.runTree.name = formatLlmRunName(record.model);
|
|
1248
|
-
record.runTree.metadata = {
|
|
1249
|
-
...(record.runTree.metadata ?? {}),
|
|
1250
|
-
...buildLlmMetadata(record, stepResult),
|
|
1251
|
-
...(options?.status && options.status !== 'completed'
|
|
1252
|
-
? { final_status: options.status }
|
|
1253
|
-
: {}),
|
|
89
|
+
if (isNativeStreamResult(result)) {
|
|
90
|
+
const [eventStream, textStream] = result.stream.tee();
|
|
91
|
+
return {
|
|
92
|
+
runId: result.runId,
|
|
93
|
+
fullStream: readableStreamToAsyncIterable(eventStream),
|
|
94
|
+
text: collectNativeStreamText(textStream),
|
|
1254
95
|
};
|
|
1255
|
-
await finishRunTree(record.runTree, {
|
|
1256
|
-
outputs: buildLlmOutputs(record, stepResult),
|
|
1257
|
-
metadata: record.runTree.metadata,
|
|
1258
|
-
...(options?.error ? { error: options.error } : {}),
|
|
1259
|
-
});
|
|
1260
|
-
record.finished = true;
|
|
1261
96
|
}
|
|
97
|
+
throw new Error('Unsupported agent stream result');
|
|
1262
98
|
}
|
|
1263
99
|
async function executeResumableStream(options) {
|
|
1264
|
-
let activeSource = options.stream;
|
|
1265
100
|
let activeStream = options.stream.fullStream;
|
|
1266
|
-
let
|
|
101
|
+
let activeAgentRunId = options.stream.runId ?? options.initialAgentRunId ?? '';
|
|
1267
102
|
let text = options.stream.text;
|
|
1268
103
|
const workSummaryAccumulator = new work_summary_accumulator_1.WorkSummaryAccumulator();
|
|
1269
104
|
let currentResponseId;
|
|
105
|
+
let nativeStepIndex = 0;
|
|
1270
106
|
while (true) {
|
|
1271
107
|
let suspension;
|
|
1272
108
|
let hasError = false;
|
|
1273
109
|
let pendingConfirmation;
|
|
1274
110
|
let confirmationEvent;
|
|
1275
111
|
let confirmationEventPublished = false;
|
|
1276
|
-
const
|
|
1277
|
-
const syntheticToolRecords = new Map();
|
|
1278
|
-
options.llmStepTraceHooks?.startSegment();
|
|
112
|
+
const drainedCorrectionsForResume = [];
|
|
1279
113
|
for await (const chunk of activeStream) {
|
|
1280
|
-
options.context.onActivity?.();
|
|
1281
114
|
if (options.context.signal.aborted) {
|
|
1282
|
-
if (options.llmStepTraceHooks) {
|
|
1283
|
-
await options.llmStepTraceHooks.finalize(activeSource, {
|
|
1284
|
-
status: 'cancelled',
|
|
1285
|
-
error: 'Run cancelled while streaming',
|
|
1286
|
-
});
|
|
1287
|
-
}
|
|
1288
|
-
else {
|
|
1289
|
-
await finalizeLlmStepTraces(activeSource, llmStepRecords, {
|
|
1290
|
-
status: 'cancelled',
|
|
1291
|
-
error: 'Run cancelled while streaming',
|
|
1292
|
-
});
|
|
1293
|
-
}
|
|
1294
|
-
await finalizeSyntheticToolTraces(syntheticToolRecords, {
|
|
1295
|
-
status: 'cancelled',
|
|
1296
|
-
error: 'Run cancelled while streaming',
|
|
1297
|
-
});
|
|
1298
115
|
return {
|
|
1299
116
|
status: 'cancelled',
|
|
1300
|
-
|
|
117
|
+
agentRunId: activeAgentRunId,
|
|
1301
118
|
text,
|
|
1302
119
|
workSummary: workSummaryAccumulator.toSummary(),
|
|
1303
120
|
};
|
|
1304
121
|
}
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
const stepMessageId = typeof stepPayload?.messageId === 'string' ? stepPayload.messageId : undefined;
|
|
1311
|
-
currentResponseId = stepMessageId;
|
|
1312
|
-
}
|
|
1313
|
-
if (options.llmStepTraceHooks) {
|
|
1314
|
-
}
|
|
1315
|
-
else if (isRecord(chunk) && chunk.type === 'step-start') {
|
|
1316
|
-
const payload = getChunkPayload(chunk);
|
|
1317
|
-
const messageId = typeof payload?.messageId === 'string' ? payload.messageId : undefined;
|
|
1318
|
-
const request = payload?.request;
|
|
1319
|
-
const stepTrace = typeof messageId === 'string'
|
|
1320
|
-
? await startLlmStepTrace(undefined, messageId, request)
|
|
1321
|
-
: undefined;
|
|
1322
|
-
if (stepTrace) {
|
|
1323
|
-
llmStepRecords.push(stepTrace);
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
else {
|
|
1327
|
-
updateStepRecordFromChunk(chunk, llmStepRecords);
|
|
1328
|
-
applyStepFinishChunk(chunk, llmStepRecords);
|
|
122
|
+
options.context.onActivity?.();
|
|
123
|
+
if (isRecord(chunk) && chunk.type === 'start-step') {
|
|
124
|
+
nativeStepIndex += 1;
|
|
125
|
+
const responseRunId = activeAgentRunId || options.context.runId;
|
|
126
|
+
currentResponseId = `${responseRunId}:step:${nativeStepIndex}`;
|
|
1329
127
|
}
|
|
1330
128
|
const parsedSuspension = (0, stream_helpers_1.parseSuspension)(chunk);
|
|
1331
129
|
if (parsedSuspension) {
|
|
@@ -1350,16 +148,9 @@ async function executeResumableStream(options) {
|
|
|
1350
148
|
if (isErrorChunk(chunk)) {
|
|
1351
149
|
hasError = true;
|
|
1352
150
|
}
|
|
1353
|
-
const event = (0, map_chunk_1.
|
|
151
|
+
const event = (0, map_chunk_1.mapAgentChunkToEvent)(options.context.runId, options.context.agentId, chunk, currentResponseId);
|
|
1354
152
|
if (event) {
|
|
1355
153
|
workSummaryAccumulator.observe(event);
|
|
1356
|
-
if (options.onStreamEvent) {
|
|
1357
|
-
try {
|
|
1358
|
-
options.onStreamEvent(event);
|
|
1359
|
-
}
|
|
1360
|
-
catch {
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
154
|
let shouldPublishEvent = true;
|
|
1364
155
|
if (event.type === 'confirmation-request') {
|
|
1365
156
|
const isPrimarySuspension = suspension !== undefined &&
|
|
@@ -1381,27 +172,15 @@ async function executeResumableStream(options) {
|
|
|
1381
172
|
}
|
|
1382
173
|
}
|
|
1383
174
|
if (options.control.mode === 'auto' && options.control.drainCorrections) {
|
|
1384
|
-
|
|
175
|
+
const corrections = options.control.drainCorrections();
|
|
176
|
+
publishCorrections(options.context, corrections);
|
|
177
|
+
drainedCorrectionsForResume.push(...corrections);
|
|
1385
178
|
}
|
|
1386
179
|
}
|
|
1387
|
-
const traceStatus = suspension ? 'suspended' : hasError ? 'errored' : 'completed';
|
|
1388
|
-
if (options.llmStepTraceHooks) {
|
|
1389
|
-
await options.llmStepTraceHooks.finalize(activeSource, {
|
|
1390
|
-
status: traceStatus,
|
|
1391
|
-
});
|
|
1392
|
-
}
|
|
1393
|
-
else {
|
|
1394
|
-
await finalizeLlmStepTraces(activeSource, llmStepRecords, {
|
|
1395
|
-
status: traceStatus,
|
|
1396
|
-
});
|
|
1397
|
-
}
|
|
1398
|
-
await finalizeSyntheticToolTraces(syntheticToolRecords, {
|
|
1399
|
-
status: traceStatus,
|
|
1400
|
-
});
|
|
1401
180
|
if (options.context.signal.aborted) {
|
|
1402
181
|
return {
|
|
1403
182
|
status: 'cancelled',
|
|
1404
|
-
|
|
183
|
+
agentRunId: activeAgentRunId,
|
|
1405
184
|
text,
|
|
1406
185
|
workSummary: workSummaryAccumulator.toSummary(),
|
|
1407
186
|
};
|
|
@@ -1409,7 +188,7 @@ async function executeResumableStream(options) {
|
|
|
1409
188
|
if (!suspension) {
|
|
1410
189
|
return {
|
|
1411
190
|
status: hasError ? 'errored' : 'completed',
|
|
1412
|
-
|
|
191
|
+
agentRunId: activeAgentRunId,
|
|
1413
192
|
text,
|
|
1414
193
|
workSummary: workSummaryAccumulator.toSummary(),
|
|
1415
194
|
};
|
|
@@ -1417,7 +196,7 @@ async function executeResumableStream(options) {
|
|
|
1417
196
|
if (options.control.mode === 'manual') {
|
|
1418
197
|
return {
|
|
1419
198
|
status: 'suspended',
|
|
1420
|
-
|
|
199
|
+
agentRunId: activeAgentRunId,
|
|
1421
200
|
text,
|
|
1422
201
|
suspension,
|
|
1423
202
|
workSummary: workSummaryAccumulator.toSummary(),
|
|
@@ -1425,23 +204,22 @@ async function executeResumableStream(options) {
|
|
|
1425
204
|
};
|
|
1426
205
|
}
|
|
1427
206
|
const confirmationPromise = pendingConfirmation ?? options.control.waitForConfirmation(suspension.requestId);
|
|
1428
|
-
const resumeData = await waitForConfirmationOrCorrection(options.context.signal, confirmationPromise, options.control, options.context);
|
|
207
|
+
const resumeData = await waitForConfirmationOrCorrection(options.context.signal, confirmationPromise, options.control, options.context, drainedCorrectionsForResume);
|
|
1429
208
|
const resumeOptions = options.control.buildResumeOptions?.({
|
|
1430
|
-
|
|
209
|
+
agentRunId: activeAgentRunId,
|
|
1431
210
|
suspension,
|
|
1432
211
|
}) ?? {
|
|
1433
|
-
runId:
|
|
212
|
+
runId: activeAgentRunId,
|
|
1434
213
|
toolCallId: suspension.toolCallId,
|
|
1435
214
|
};
|
|
1436
|
-
const resumed = await (0, stream_helpers_1.
|
|
215
|
+
const resumed = await (0, stream_helpers_1.resumeAgentStream)(options.agent, resumeData, {
|
|
1437
216
|
...resumeOptions,
|
|
1438
|
-
...(options.llmStepTraceHooks?.executionOptions ?? {}),
|
|
1439
217
|
});
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
activeStream =
|
|
1444
|
-
text =
|
|
218
|
+
const resumedSource = normalizeStreamSource(resumed);
|
|
219
|
+
activeAgentRunId =
|
|
220
|
+
(typeof resumedSource.runId === 'string' ? resumedSource.runId : '') || activeAgentRunId;
|
|
221
|
+
activeStream = resumedSource.fullStream;
|
|
222
|
+
text = resumedSource.text;
|
|
1445
223
|
}
|
|
1446
224
|
}
|
|
1447
225
|
function publishCorrections(context, corrections) {
|
|
@@ -1454,6 +232,14 @@ function publishCorrections(context, corrections) {
|
|
|
1454
232
|
});
|
|
1455
233
|
}
|
|
1456
234
|
}
|
|
235
|
+
function buildCorrectionResumeData(corrections) {
|
|
236
|
+
return {
|
|
237
|
+
__correctionOverride: true,
|
|
238
|
+
message: 'The user sent a correction while this run was active. ' +
|
|
239
|
+
'The confirmation has been skipped. Apply the correction and continue.',
|
|
240
|
+
corrections,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
1457
243
|
function isErrorChunk(chunk) {
|
|
1458
244
|
return (chunk !== null &&
|
|
1459
245
|
typeof chunk === 'object' &&
|
|
@@ -1479,7 +265,11 @@ async function waitForConfirmation(signal, confirmationPromise) {
|
|
|
1479
265
|
}
|
|
1480
266
|
}
|
|
1481
267
|
}
|
|
1482
|
-
async function waitForConfirmationOrCorrection(signal, confirmationPromise, control, context) {
|
|
268
|
+
async function waitForConfirmationOrCorrection(signal, confirmationPromise, control, context, drainedCorrectionsForResume) {
|
|
269
|
+
if (drainedCorrectionsForResume.length > 0) {
|
|
270
|
+
void confirmationPromise.catch(() => undefined);
|
|
271
|
+
return buildCorrectionResumeData(drainedCorrectionsForResume);
|
|
272
|
+
}
|
|
1483
273
|
if (!control.waitForCorrection) {
|
|
1484
274
|
return await waitForConfirmation(signal, confirmationPromise);
|
|
1485
275
|
}
|
|
@@ -1492,12 +282,7 @@ async function waitForConfirmationOrCorrection(signal, confirmationPromise, cont
|
|
|
1492
282
|
if (result === correctionSentinel) {
|
|
1493
283
|
const corrections = control.drainCorrections?.() ?? [];
|
|
1494
284
|
publishCorrections(context, corrections);
|
|
1495
|
-
return
|
|
1496
|
-
__correctionOverride: true,
|
|
1497
|
-
message: 'The user sent a correction while this tool was waiting for confirmation. ' +
|
|
1498
|
-
'The confirmation has been skipped. Apply the correction and continue.',
|
|
1499
|
-
corrections,
|
|
1500
|
-
};
|
|
285
|
+
return buildCorrectionResumeData(corrections);
|
|
1501
286
|
}
|
|
1502
287
|
return result;
|
|
1503
288
|
}
|