@n8n/instance-ai 1.7.0 → 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,36 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.serializeModelIdForTrace =
|
|
3
|
+
exports.serializeModelIdForTrace = exports.redactLangSmithTelemetrySpan = exports.buildAgentTraceInputs = void 0;
|
|
4
4
|
exports.releaseTraceClient = releaseTraceClient;
|
|
5
5
|
exports.submitLangsmithUserFeedback = submitLangsmithUserFeedback;
|
|
6
|
-
exports.
|
|
7
|
-
exports.
|
|
6
|
+
exports.getCurrentOtelSpanContext = getCurrentOtelSpanContext;
|
|
7
|
+
exports.getCurrentTraceToolCallId = getCurrentTraceToolCallId;
|
|
8
8
|
exports.mergeCurrentTraceMetadata = mergeCurrentTraceMetadata;
|
|
9
9
|
exports.appendRootRunMetadata = appendRootRunMetadata;
|
|
10
10
|
exports.appendGeneratedWorkflowIdToRootMetadata = appendGeneratedWorkflowIdToRootMetadata;
|
|
11
11
|
exports.mergeTraceRunInputs = mergeTraceRunInputs;
|
|
12
|
-
exports.buildAgentTraceInputs = buildAgentTraceInputs;
|
|
13
|
-
exports.withTraceParentContext = withTraceParentContext;
|
|
14
12
|
exports.withCurrentTraceSpan = withCurrentTraceSpan;
|
|
15
13
|
exports.createTraceReplayOnlyContext = createTraceReplayOnlyContext;
|
|
16
14
|
exports.createInstanceAiTraceContext = createInstanceAiTraceContext;
|
|
17
15
|
exports.continueInstanceAiTraceContext = continueInstanceAiTraceContext;
|
|
18
16
|
exports.createDetachedSubAgentTraceContext = createDetachedSubAgentTraceContext;
|
|
19
|
-
|
|
17
|
+
exports.createInternalOperationTraceContext = createInternalOperationTraceContext;
|
|
18
|
+
const agents_1 = require("@n8n/agents");
|
|
19
|
+
const api_1 = require("@opentelemetry/api");
|
|
20
20
|
const langsmith_1 = require("langsmith");
|
|
21
|
-
const traceable_1 = require("langsmith/traceable");
|
|
22
21
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
22
|
+
const promises_1 = require("node:fs/promises");
|
|
23
|
+
const node_module_1 = require("node:module");
|
|
24
|
+
const node_path_1 = require("node:path");
|
|
25
|
+
const tool_registry_1 = require("../tool-registry");
|
|
26
|
+
const trace_labels_1 = require("./trace-labels");
|
|
27
|
+
const trace_payloads_1 = require("./trace-payloads");
|
|
23
28
|
const trace_replay_1 = require("./trace-replay");
|
|
24
29
|
const stream_helpers_1 = require("../utils/stream-helpers");
|
|
30
|
+
var trace_payloads_2 = require("./trace-payloads");
|
|
31
|
+
Object.defineProperty(exports, "buildAgentTraceInputs", { enumerable: true, get: function () { return trace_payloads_2.buildAgentTraceInputs; } });
|
|
32
|
+
Object.defineProperty(exports, "redactLangSmithTelemetrySpan", { enumerable: true, get: function () { return trace_payloads_2.redactLangSmithTelemetrySpan; } });
|
|
33
|
+
Object.defineProperty(exports, "serializeModelIdForTrace", { enumerable: true, get: function () { return trace_payloads_2.serializeModelIdForTrace; } });
|
|
25
34
|
const DEFAULT_PROJECT_NAME = 'instance-ai';
|
|
26
35
|
const DEFAULT_TAGS = ['instance-ai'];
|
|
27
|
-
const
|
|
28
|
-
const MAX_TRACE_STRING_LENGTH = 2_000;
|
|
29
|
-
const MAX_TRACE_ARRAY_ITEMS = 20;
|
|
30
|
-
const MAX_TRACE_OBJECT_KEYS = 30;
|
|
31
|
-
const traceParentOverrideStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
36
|
+
const productTraceStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
32
37
|
const proxyHeaderStore = new node_async_hooks_1.AsyncLocalStorage();
|
|
33
|
-
const
|
|
38
|
+
const otelTraceRuntimes = new Map();
|
|
39
|
+
const hostRequire = (0, node_module_1.createRequire)(__filename);
|
|
34
40
|
const gzipFetch = async (input, init) => {
|
|
35
41
|
const headers = new Headers(init?.headers);
|
|
36
42
|
if (!headers.has('Accept-Encoding')) {
|
|
@@ -44,6 +50,343 @@ const gzipFetch = async (input, init) => {
|
|
|
44
50
|
};
|
|
45
51
|
let cachedProxyClient = null;
|
|
46
52
|
let cachedDirectClient = null;
|
|
53
|
+
const OTEL_TRACE_VERSION = 'otel-v2';
|
|
54
|
+
const LANGSMITH_TRACEABLE = 'langsmith.traceable';
|
|
55
|
+
const LANGSMITH_TRACE_NAME = 'langsmith.trace.name';
|
|
56
|
+
const LANGSMITH_SPAN_KIND = 'langsmith.span.kind';
|
|
57
|
+
const LANGSMITH_SPAN_TAGS = 'langsmith.span.tags';
|
|
58
|
+
function isOtelTracer(value) {
|
|
59
|
+
return (value !== null &&
|
|
60
|
+
typeof value === 'object' &&
|
|
61
|
+
typeof Reflect.get(value, 'startSpan') === 'function');
|
|
62
|
+
}
|
|
63
|
+
function langsmithTraceIdFromOtelTraceId(traceId) {
|
|
64
|
+
return `${traceId.substring(0, 8)}-${traceId.substring(8, 12)}-${traceId.substring(12, 16)}-${traceId.substring(16, 20)}-${traceId.substring(20, 32)}`;
|
|
65
|
+
}
|
|
66
|
+
function langsmithRunIdFromOtelSpanId(spanId) {
|
|
67
|
+
const paddedHex = spanId.padStart(16, '0');
|
|
68
|
+
return `00000000-0000-0000-${paddedHex.substring(0, 4)}-${paddedHex.substring(4, 16)}`;
|
|
69
|
+
}
|
|
70
|
+
function stableDottedOrder(parentRun, runId) {
|
|
71
|
+
return parentRun?.dottedOrder ? `${parentRun.dottedOrder}.${runId}` : runId;
|
|
72
|
+
}
|
|
73
|
+
function inferDisplayKind(name) {
|
|
74
|
+
if (name === 'turn')
|
|
75
|
+
return 'turn';
|
|
76
|
+
if (name.startsWith('agent:'))
|
|
77
|
+
return 'agent';
|
|
78
|
+
if (name.startsWith('llm:'))
|
|
79
|
+
return 'llm';
|
|
80
|
+
if (name.startsWith('tool:'))
|
|
81
|
+
return 'tool';
|
|
82
|
+
if (name.startsWith('prepare:'))
|
|
83
|
+
return 'prepare';
|
|
84
|
+
if (name.startsWith('resume:'))
|
|
85
|
+
return 'resume';
|
|
86
|
+
if (name.startsWith('background task:'))
|
|
87
|
+
return 'background_task';
|
|
88
|
+
if (name.startsWith('hitl:'))
|
|
89
|
+
return 'hitl';
|
|
90
|
+
if (name.startsWith('internal:'))
|
|
91
|
+
return 'internal';
|
|
92
|
+
return 'operation';
|
|
93
|
+
}
|
|
94
|
+
function inferDisplayGroup(metadata, name) {
|
|
95
|
+
const role = typeof metadata?.agent_role === 'string'
|
|
96
|
+
? metadata.agent_role
|
|
97
|
+
: typeof metadata?.subagent_role === 'string'
|
|
98
|
+
? metadata.subagent_role
|
|
99
|
+
: undefined;
|
|
100
|
+
if (role) {
|
|
101
|
+
return (0, trace_labels_1.formatAgentRoleLabel)(role);
|
|
102
|
+
}
|
|
103
|
+
if (name.startsWith('prepare:'))
|
|
104
|
+
return 'preparation';
|
|
105
|
+
if (name.startsWith('hitl:'))
|
|
106
|
+
return 'human-in-the-loop';
|
|
107
|
+
if (name === 'turn')
|
|
108
|
+
return 'conversation';
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
function inferDisplayPhase(metadata) {
|
|
112
|
+
return typeof metadata?.execution_mode === 'string'
|
|
113
|
+
? (0, trace_labels_1.formatTraceLabel)(metadata.execution_mode)
|
|
114
|
+
: undefined;
|
|
115
|
+
}
|
|
116
|
+
function buildProductSpanMetadata(options) {
|
|
117
|
+
const canonicalName = options.canonicalName ?? options.name;
|
|
118
|
+
const displayGroup = inferDisplayGroup(options.metadata, options.name);
|
|
119
|
+
const displayPhase = inferDisplayPhase(options.metadata);
|
|
120
|
+
const displayDefaults = {
|
|
121
|
+
trace_version: OTEL_TRACE_VERSION,
|
|
122
|
+
'instance_ai.trace_version': OTEL_TRACE_VERSION,
|
|
123
|
+
display_kind: inferDisplayKind(options.name),
|
|
124
|
+
...(displayGroup ? { display_group: displayGroup } : {}),
|
|
125
|
+
...(displayPhase ? { display_phase: displayPhase } : {}),
|
|
126
|
+
};
|
|
127
|
+
return (mergeMetadata(displayDefaults, options.metadata, {
|
|
128
|
+
display_name: options.name,
|
|
129
|
+
'instance_ai.display_name': options.name,
|
|
130
|
+
'instance_ai.canonical_name': canonicalName,
|
|
131
|
+
'instance_ai.run_name': canonicalName,
|
|
132
|
+
}) ?? {});
|
|
133
|
+
}
|
|
134
|
+
function buildProductSpanAttributes(options) {
|
|
135
|
+
const attributes = {
|
|
136
|
+
[LANGSMITH_TRACEABLE]: 'true',
|
|
137
|
+
[LANGSMITH_TRACE_NAME]: options.name,
|
|
138
|
+
[LANGSMITH_SPAN_KIND]: options.runType ?? 'chain',
|
|
139
|
+
'instance_ai.trace_version': OTEL_TRACE_VERSION,
|
|
140
|
+
};
|
|
141
|
+
const tags = normalizeTags(DEFAULT_TAGS, options.tags);
|
|
142
|
+
if (tags?.length) {
|
|
143
|
+
attributes[LANGSMITH_SPAN_TAGS] = tags;
|
|
144
|
+
}
|
|
145
|
+
const metadata = buildProductSpanMetadata(options);
|
|
146
|
+
for (const [key, value] of Object.entries(metadata ?? {})) {
|
|
147
|
+
const attributeValue = (0, trace_payloads_1.toTelemetryAttributeValue)(value);
|
|
148
|
+
if (attributeValue === undefined)
|
|
149
|
+
continue;
|
|
150
|
+
attributes[key] = attributeValue;
|
|
151
|
+
if (!key.startsWith('langsmith.metadata.')) {
|
|
152
|
+
attributes[`langsmith.metadata.${key}`] = attributeValue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const inputs = options.inputs === undefined ? undefined : stringifyTracePayload(options.inputs);
|
|
156
|
+
if (inputs !== undefined) {
|
|
157
|
+
attributes[trace_payloads_1.GEN_AI_PROMPT] = inputs;
|
|
158
|
+
}
|
|
159
|
+
return attributes;
|
|
160
|
+
}
|
|
161
|
+
function stringifyTracePayload(value) {
|
|
162
|
+
try {
|
|
163
|
+
return JSON.stringify((0, trace_payloads_1.sanitizeTracePayload)(value));
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function startProductSpan(runtime, options) {
|
|
170
|
+
if (!isOtelTracer(runtime.telemetry.tracer)) {
|
|
171
|
+
throw new Error('Instance AI tracing requires an OpenTelemetry tracer');
|
|
172
|
+
}
|
|
173
|
+
const spanMetadata = buildProductSpanMetadata(options);
|
|
174
|
+
const parentContext = options.root
|
|
175
|
+
? api_1.ROOT_CONTEXT
|
|
176
|
+
: (options.parentContext ??
|
|
177
|
+
(options.parentRun ? runtime.contexts.get(options.parentRun.id) : undefined) ??
|
|
178
|
+
api_1.context.active());
|
|
179
|
+
const span = runtime.telemetry.tracer.startSpan(options.name, {
|
|
180
|
+
attributes: buildProductSpanAttributes(options),
|
|
181
|
+
}, parentContext);
|
|
182
|
+
const spanContext = span.spanContext();
|
|
183
|
+
const traceId = langsmithTraceIdFromOtelTraceId(spanContext.traceId);
|
|
184
|
+
const runId = langsmithRunIdFromOtelSpanId(spanContext.spanId);
|
|
185
|
+
const spanContextWithSpan = api_1.trace.setSpan(parentContext ?? api_1.context.active(), span);
|
|
186
|
+
const parentRun = options.parentRun;
|
|
187
|
+
const runMetadata = mergeMetadata(parentRun?.metadata, spanMetadata);
|
|
188
|
+
const run = {
|
|
189
|
+
id: runId,
|
|
190
|
+
name: options.name,
|
|
191
|
+
runType: options.runType ?? 'chain',
|
|
192
|
+
projectName: options.projectName,
|
|
193
|
+
startTime: Date.now(),
|
|
194
|
+
traceId,
|
|
195
|
+
otelTraceId: spanContext.traceId,
|
|
196
|
+
otelSpanId: spanContext.spanId,
|
|
197
|
+
dottedOrder: stableDottedOrder(parentRun, runId),
|
|
198
|
+
executionOrder: parentRun ? parentRun.childExecutionOrder + 1 : 0,
|
|
199
|
+
childExecutionOrder: 0,
|
|
200
|
+
...(parentRun ? { parentRunId: parentRun.id } : {}),
|
|
201
|
+
...(options.tags ? { tags: normalizeTags(DEFAULT_TAGS, parentRun?.tags, options.tags) } : {}),
|
|
202
|
+
...(runMetadata ? { metadata: runMetadata } : {}),
|
|
203
|
+
...(options.inputs !== undefined ? { inputs: (0, trace_payloads_1.sanitizeTracePayload)(options.inputs) } : {}),
|
|
204
|
+
};
|
|
205
|
+
if (parentRun) {
|
|
206
|
+
parentRun.childExecutionOrder += 1;
|
|
207
|
+
}
|
|
208
|
+
runtime.spans.set(run.id, span);
|
|
209
|
+
runtime.contexts.set(run.id, spanContextWithSpan);
|
|
210
|
+
return run;
|
|
211
|
+
}
|
|
212
|
+
async function finishProductSpan(runtime, run, options) {
|
|
213
|
+
const span = runtime.spans.get(run.id);
|
|
214
|
+
if (!span)
|
|
215
|
+
return;
|
|
216
|
+
const metadata = mergeMetadata(options?.metadata);
|
|
217
|
+
const attributes = {};
|
|
218
|
+
for (const [key, value] of Object.entries(metadata ?? {})) {
|
|
219
|
+
const attributeValue = (0, trace_payloads_1.toTelemetryAttributeValue)(value);
|
|
220
|
+
if (attributeValue === undefined)
|
|
221
|
+
continue;
|
|
222
|
+
attributes[key] = attributeValue;
|
|
223
|
+
attributes[`langsmith.metadata.${key}`] = attributeValue;
|
|
224
|
+
}
|
|
225
|
+
if (options?.outputs !== undefined) {
|
|
226
|
+
const completion = stringifyTracePayload(options.outputs);
|
|
227
|
+
if (completion !== undefined) {
|
|
228
|
+
attributes[trace_payloads_1.GEN_AI_COMPLETION] = completion;
|
|
229
|
+
}
|
|
230
|
+
run.outputs = (0, trace_payloads_1.sanitizeTracePayload)(options.outputs);
|
|
231
|
+
}
|
|
232
|
+
run.endTime = Date.now();
|
|
233
|
+
run.metadata = mergeMetadata(run.metadata, metadata);
|
|
234
|
+
try {
|
|
235
|
+
if (Object.keys(attributes).length > 0) {
|
|
236
|
+
span.setAttributes(attributes);
|
|
237
|
+
}
|
|
238
|
+
if (options?.error) {
|
|
239
|
+
span.recordException(new Error(options.error));
|
|
240
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: options.error });
|
|
241
|
+
run.error = options.error;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
245
|
+
}
|
|
246
|
+
span.end();
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
runtime.spans.delete(run.id);
|
|
250
|
+
runtime.contexts.delete(run.id);
|
|
251
|
+
}
|
|
252
|
+
if (options?.forceFlush === true) {
|
|
253
|
+
await agents_1.Telemetry.forceFlush(runtime.telemetry);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async function finishProductSpanBestEffort(runtime, run, options) {
|
|
257
|
+
try {
|
|
258
|
+
await finishProductSpan(runtime, run, options);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async function shutdownProductOtelRuntime(runtime, traceId) {
|
|
264
|
+
if (runtime.shutdown)
|
|
265
|
+
return;
|
|
266
|
+
runtime.shutdown = true;
|
|
267
|
+
runtime.spans.clear();
|
|
268
|
+
runtime.contexts.clear();
|
|
269
|
+
otelTraceRuntimes.delete(traceId);
|
|
270
|
+
try {
|
|
271
|
+
await agents_1.Telemetry.shutdown(runtime.telemetry);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async function withProxyHeaders(proxyConfig, fn) {
|
|
277
|
+
if (!proxyConfig)
|
|
278
|
+
return await fn();
|
|
279
|
+
const headers = await proxyConfig.getAuthHeaders();
|
|
280
|
+
return await proxyHeaderStore.run(headers, fn);
|
|
281
|
+
}
|
|
282
|
+
async function withProxyHeadersBestEffort(proxyConfig, fn) {
|
|
283
|
+
if (!proxyConfig)
|
|
284
|
+
return await fn();
|
|
285
|
+
let headers;
|
|
286
|
+
try {
|
|
287
|
+
headers = await proxyConfig.getAuthHeaders();
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return await fn();
|
|
291
|
+
}
|
|
292
|
+
return await proxyHeaderStore.run(headers, fn);
|
|
293
|
+
}
|
|
294
|
+
async function withProductSpanContext(runtime, run, fn) {
|
|
295
|
+
const spanContext = runtime.contexts.get(run.id);
|
|
296
|
+
if (!spanContext) {
|
|
297
|
+
return await fn();
|
|
298
|
+
}
|
|
299
|
+
return await productTraceStorage.run({ runtime, currentRun: run }, async () => await api_1.context.with(spanContext, fn));
|
|
300
|
+
}
|
|
301
|
+
async function withProductSpanContextBestEffort(runtime, run, fn) {
|
|
302
|
+
let callbackStarted = false;
|
|
303
|
+
try {
|
|
304
|
+
return await withProductSpanContext(runtime, run, async () => {
|
|
305
|
+
callbackStarted = true;
|
|
306
|
+
return await fn();
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
if (!callbackStarted) {
|
|
311
|
+
return await fn();
|
|
312
|
+
}
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function createFallbackChildRun(projectName, parentRun, init) {
|
|
317
|
+
const executionOrder = parentRun.childExecutionOrder + 1;
|
|
318
|
+
const id = `${parentRun.id}:child:${executionOrder}`;
|
|
319
|
+
const metadata = mergeMetadata(parentRun.metadata, init.metadata);
|
|
320
|
+
parentRun.childExecutionOrder = executionOrder;
|
|
321
|
+
return {
|
|
322
|
+
id,
|
|
323
|
+
name: init.name,
|
|
324
|
+
runType: init.runType ?? 'chain',
|
|
325
|
+
projectName,
|
|
326
|
+
startTime: Date.now(),
|
|
327
|
+
traceId: parentRun.traceId,
|
|
328
|
+
dottedOrder: stableDottedOrder(parentRun, id),
|
|
329
|
+
executionOrder,
|
|
330
|
+
childExecutionOrder: 0,
|
|
331
|
+
parentRunId: parentRun.id,
|
|
332
|
+
...(init.tags ? { tags: normalizeTags(DEFAULT_TAGS, parentRun.tags, init.tags) } : {}),
|
|
333
|
+
...(metadata ? { metadata } : {}),
|
|
334
|
+
...(init.inputs !== undefined ? { inputs: (0, trace_payloads_1.sanitizeTracePayload)(init.inputs) } : {}),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function getCurrentProductTrace() {
|
|
338
|
+
return productTraceStorage.getStore();
|
|
339
|
+
}
|
|
340
|
+
function getActiveOtelContextWithSpan(expectedTraceId) {
|
|
341
|
+
const activeContext = api_1.context.active();
|
|
342
|
+
const activeSpanContext = api_1.trace.getSpan(activeContext)?.spanContext();
|
|
343
|
+
if (!activeSpanContext) {
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
if (expectedTraceId && activeSpanContext.traceId !== expectedTraceId) {
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
return activeContext;
|
|
350
|
+
}
|
|
351
|
+
function spanMetadataAttributes(metadata) {
|
|
352
|
+
const attributes = {};
|
|
353
|
+
for (const [key, value] of Object.entries(metadata ?? {})) {
|
|
354
|
+
const attributeValue = (0, trace_payloads_1.toTelemetryAttributeValue)(value);
|
|
355
|
+
if (attributeValue === undefined)
|
|
356
|
+
continue;
|
|
357
|
+
attributes[key] = attributeValue;
|
|
358
|
+
if (!key.startsWith('langsmith.metadata.')) {
|
|
359
|
+
attributes[`langsmith.metadata.${key}`] = attributeValue;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return attributes;
|
|
363
|
+
}
|
|
364
|
+
function updateProductRunMetadata(runtime, run, metadata) {
|
|
365
|
+
const mergedMetadata = mergeMetadata(run.metadata, metadata);
|
|
366
|
+
if (!mergedMetadata)
|
|
367
|
+
return;
|
|
368
|
+
run.metadata = mergedMetadata;
|
|
369
|
+
const attributes = spanMetadataAttributes(metadata);
|
|
370
|
+
if (Object.keys(attributes).length > 0) {
|
|
371
|
+
try {
|
|
372
|
+
runtime.spans.get(run.id)?.setAttributes(attributes);
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function updateProductRunInputs(runtime, run, inputs) {
|
|
379
|
+
const mergedInputs = (0, trace_payloads_1.sanitizeTracePayload)((0, trace_payloads_1.mergeTraceInputs)(run.inputs, inputs));
|
|
380
|
+
run.inputs = mergedInputs;
|
|
381
|
+
const prompt = stringifyTracePayload(mergedInputs);
|
|
382
|
+
if (prompt !== undefined) {
|
|
383
|
+
try {
|
|
384
|
+
runtime.spans.get(run.id)?.setAttributes({ [trace_payloads_1.GEN_AI_PROMPT]: prompt });
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
47
390
|
function getOrCreateDirectClient() {
|
|
48
391
|
if (cachedDirectClient)
|
|
49
392
|
return cachedDirectClient;
|
|
@@ -75,9 +418,20 @@ function getOrCreateProxyClient(proxyConfig) {
|
|
|
75
418
|
cachedProxyClient = { client, apiUrl: proxyConfig.apiUrl };
|
|
76
419
|
return client;
|
|
77
420
|
}
|
|
78
|
-
function
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
421
|
+
function readBooleanEnvFlag(value) {
|
|
422
|
+
const normalized = value?.toLowerCase();
|
|
423
|
+
if (normalized === 'true')
|
|
424
|
+
return true;
|
|
425
|
+
if (normalized === 'false')
|
|
426
|
+
return false;
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
function isLangSmithTracingEnabled(proxyAvailable = false) {
|
|
430
|
+
if (readBooleanEnvFlag(process.env.N8N_DIAGNOSTICS_ENABLED) === false) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
const tracingFlag = readBooleanEnvFlag(process.env.LANGCHAIN_TRACING_V2 ?? process.env.LANGSMITH_TRACING);
|
|
434
|
+
if (tracingFlag === false) {
|
|
81
435
|
return false;
|
|
82
436
|
}
|
|
83
437
|
if (proxyAvailable) {
|
|
@@ -87,11 +441,11 @@ function isLangSmithTracingEnabled(proxyAvailable) {
|
|
|
87
441
|
process.env.LANGCHAIN_API_KEY ??
|
|
88
442
|
process.env.LANGSMITH_ENDPOINT ??
|
|
89
443
|
process.env.LANGCHAIN_ENDPOINT ??
|
|
90
|
-
tracingFlag
|
|
444
|
+
tracingFlag === true);
|
|
91
445
|
}
|
|
92
|
-
function
|
|
93
|
-
process.env.
|
|
94
|
-
|
|
446
|
+
function isInternalOperationTracingEnabled() {
|
|
447
|
+
return (process.env.N8N_INSTANCE_AI_TRACE_INTERNAL === 'true' ||
|
|
448
|
+
process.env.N8N_INSTANCE_AI_TRACE_INCLUDE_INTERNAL === 'true');
|
|
95
449
|
}
|
|
96
450
|
function normalizeErrorMessage(error) {
|
|
97
451
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -110,177 +464,18 @@ function mergeMetadata(...records) {
|
|
|
110
464
|
for (const [key, value] of Object.entries(record)) {
|
|
111
465
|
if (value !== undefined) {
|
|
112
466
|
merged[key] =
|
|
113
|
-
key === 'model_id' ? serializeModelIdForTrace(value) : sanitizeTraceValue(value);
|
|
467
|
+
key === 'model_id' ? (0, trace_payloads_1.serializeModelIdForTrace)(value) : (0, trace_payloads_1.sanitizeTraceValue)(value);
|
|
114
468
|
}
|
|
115
469
|
}
|
|
116
470
|
}
|
|
117
471
|
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
118
472
|
}
|
|
119
|
-
function truncateString(value) {
|
|
120
|
-
if (value.length <= MAX_TRACE_STRING_LENGTH) {
|
|
121
|
-
return value;
|
|
122
|
-
}
|
|
123
|
-
return `${value.slice(0, MAX_TRACE_STRING_LENGTH)}…`;
|
|
124
|
-
}
|
|
125
|
-
function splitTraceText(value) {
|
|
126
|
-
if (value.length <= MAX_TRACE_STRING_LENGTH) {
|
|
127
|
-
return [value];
|
|
128
|
-
}
|
|
129
|
-
const chunks = [];
|
|
130
|
-
let remaining = value;
|
|
131
|
-
while (remaining.length > MAX_TRACE_STRING_LENGTH) {
|
|
132
|
-
const candidate = remaining.slice(0, MAX_TRACE_STRING_LENGTH);
|
|
133
|
-
const splitIndex = candidate.lastIndexOf('\n');
|
|
134
|
-
const chunkEnd = splitIndex >= MAX_TRACE_STRING_LENGTH / 2 ? splitIndex + 1 : MAX_TRACE_STRING_LENGTH;
|
|
135
|
-
chunks.push(remaining.slice(0, chunkEnd));
|
|
136
|
-
remaining = remaining.slice(chunkEnd);
|
|
137
|
-
}
|
|
138
|
-
if (remaining.length > 0) {
|
|
139
|
-
chunks.push(remaining);
|
|
140
|
-
}
|
|
141
|
-
return chunks;
|
|
142
|
-
}
|
|
143
|
-
function serializeTraceText(value) {
|
|
144
|
-
const chunks = splitTraceText(value);
|
|
145
|
-
if (chunks.length === 1) {
|
|
146
|
-
return chunks[0];
|
|
147
|
-
}
|
|
148
|
-
return Object.fromEntries(chunks.map((chunk, index) => [`part_${String(index + 1).padStart(2, '0')}`, chunk]));
|
|
149
|
-
}
|
|
150
|
-
function summarizeToolDescription(tool) {
|
|
151
|
-
if (!(0, stream_helpers_1.isRecord)(tool)) {
|
|
152
|
-
return undefined;
|
|
153
|
-
}
|
|
154
|
-
return typeof tool.description === 'string' ? tool.description : undefined;
|
|
155
|
-
}
|
|
156
|
-
function summarizeToolSet(fieldPrefix, tools) {
|
|
157
|
-
if (!tools || Object.keys(tools).length === 0) {
|
|
158
|
-
return {};
|
|
159
|
-
}
|
|
160
|
-
const summaries = Object.entries(tools).map(([name, tool]) => ({
|
|
161
|
-
name,
|
|
162
|
-
...(summarizeToolDescription(tool) ? { description: summarizeToolDescription(tool) } : {}),
|
|
163
|
-
}));
|
|
164
|
-
const catalogText = summaries
|
|
165
|
-
.map((tool) => typeof tool.description === 'string' ? `${tool.name}: ${tool.description}` : tool.name)
|
|
166
|
-
.join('\n');
|
|
167
|
-
return {
|
|
168
|
-
[`${fieldPrefix}_tool_count`]: summaries.length,
|
|
169
|
-
[`${fieldPrefix}_tools`]: summaries,
|
|
170
|
-
[`${fieldPrefix}_tool_catalog`]: serializeTraceText(catalogText),
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
function summarizeMemoryBinding(memory) {
|
|
174
|
-
if (!(0, stream_helpers_1.isRecord)(memory)) {
|
|
175
|
-
return {};
|
|
176
|
-
}
|
|
177
|
-
return {
|
|
178
|
-
memory_enabled: true,
|
|
179
|
-
...(typeof memory.resource === 'string' ? { memory_resource_id: memory.resource } : {}),
|
|
180
|
-
...(typeof memory.thread === 'string' ? { memory_thread_id: memory.thread } : {}),
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
function sanitizeTraceValue(value, depth = 0) {
|
|
184
|
-
if (value === null || value === undefined) {
|
|
185
|
-
return value;
|
|
186
|
-
}
|
|
187
|
-
if (typeof value === 'string') {
|
|
188
|
-
return truncateString(value);
|
|
189
|
-
}
|
|
190
|
-
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
191
|
-
return value;
|
|
192
|
-
}
|
|
193
|
-
if (typeof value === 'bigint') {
|
|
194
|
-
return value.toString();
|
|
195
|
-
}
|
|
196
|
-
if (typeof value === 'function') {
|
|
197
|
-
return `[function ${value.name || 'anonymous'}]`;
|
|
198
|
-
}
|
|
199
|
-
if (value instanceof Date) {
|
|
200
|
-
return value.toISOString();
|
|
201
|
-
}
|
|
202
|
-
if (value instanceof Error) {
|
|
203
|
-
return {
|
|
204
|
-
name: value.name,
|
|
205
|
-
message: truncateString(value.message),
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
if (value instanceof Uint8Array) {
|
|
209
|
-
return `[binary ${value.byteLength} bytes]`;
|
|
210
|
-
}
|
|
211
|
-
if (Array.isArray(value)) {
|
|
212
|
-
if (depth >= MAX_TRACE_DEPTH) {
|
|
213
|
-
return `[array(${value.length})]`;
|
|
214
|
-
}
|
|
215
|
-
return value
|
|
216
|
-
.slice(0, MAX_TRACE_ARRAY_ITEMS)
|
|
217
|
-
.map((entry) => sanitizeTraceValue(entry, depth + 1));
|
|
218
|
-
}
|
|
219
|
-
if ((0, stream_helpers_1.isRecord)(value)) {
|
|
220
|
-
if (depth >= MAX_TRACE_DEPTH) {
|
|
221
|
-
return `[object ${Object.keys(value).length} keys]`;
|
|
222
|
-
}
|
|
223
|
-
const entries = Object.entries(value).slice(0, MAX_TRACE_OBJECT_KEYS);
|
|
224
|
-
const sanitized = {};
|
|
225
|
-
for (const [key, entryValue] of entries) {
|
|
226
|
-
sanitized[key] = sanitizeTraceValue(entryValue, depth + 1);
|
|
227
|
-
}
|
|
228
|
-
if (Object.keys(value).length > entries.length) {
|
|
229
|
-
sanitized.__truncatedKeys = Object.keys(value).length - entries.length;
|
|
230
|
-
}
|
|
231
|
-
return sanitized;
|
|
232
|
-
}
|
|
233
|
-
if (typeof value === 'symbol') {
|
|
234
|
-
return value.toString();
|
|
235
|
-
}
|
|
236
|
-
return truncateString(Object.prototype.toString.call(value));
|
|
237
|
-
}
|
|
238
|
-
function sanitizeTracePayload(value) {
|
|
239
|
-
if ((0, stream_helpers_1.isRecord)(value)) {
|
|
240
|
-
const sanitized = {};
|
|
241
|
-
for (const [key, entryValue] of Object.entries(value)) {
|
|
242
|
-
sanitized[key] = sanitizeTraceValue(entryValue);
|
|
243
|
-
}
|
|
244
|
-
return sanitized;
|
|
245
|
-
}
|
|
246
|
-
if (value === undefined) {
|
|
247
|
-
return {};
|
|
248
|
-
}
|
|
249
|
-
return { value: sanitizeTraceValue(value) };
|
|
250
|
-
}
|
|
251
|
-
function normalizeModelMetadata(modelId) {
|
|
252
|
-
if (typeof modelId === 'string' && modelId.length > 0) {
|
|
253
|
-
const [provider, ...modelParts] = modelId.split('/');
|
|
254
|
-
return modelParts.length > 0
|
|
255
|
-
? { provider, modelName: modelParts.join('/') }
|
|
256
|
-
: { modelName: modelId };
|
|
257
|
-
}
|
|
258
|
-
if ((0, stream_helpers_1.isRecord)(modelId) && typeof modelId.id === 'string') {
|
|
259
|
-
return normalizeModelMetadata(modelId.id);
|
|
260
|
-
}
|
|
261
|
-
return {};
|
|
262
|
-
}
|
|
263
|
-
function serializeModelIdForTrace(modelId) {
|
|
264
|
-
if (typeof modelId === 'string' && modelId.length > 0) {
|
|
265
|
-
return truncateString(modelId);
|
|
266
|
-
}
|
|
267
|
-
if ((0, stream_helpers_1.isRecord)(modelId) && typeof modelId.id === 'string') {
|
|
268
|
-
return truncateString(modelId.id);
|
|
269
|
-
}
|
|
270
|
-
return sanitizeTraceValue(modelId);
|
|
271
|
-
}
|
|
272
|
-
function mergeRunTreeMetadata(baseMetadata, metadata) {
|
|
273
|
-
return mergeMetadata(baseMetadata, metadata);
|
|
274
|
-
}
|
|
275
|
-
function mergeRunTreeInputs(baseInputs, inputs) {
|
|
276
|
-
const existingInputs = (0, stream_helpers_1.isRecord)(baseInputs) && !Array.isArray(baseInputs) ? { ...baseInputs } : {};
|
|
277
|
-
return {
|
|
278
|
-
...existingInputs,
|
|
279
|
-
...(inputs ?? {}),
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
473
|
function releaseTraceClient(traceId) {
|
|
283
|
-
|
|
474
|
+
const runtime = otelTraceRuntimes.get(traceId);
|
|
475
|
+
if (!runtime) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
void shutdownProductOtelRuntime(runtime, traceId);
|
|
284
479
|
}
|
|
285
480
|
async function submitLangsmithUserFeedback(options) {
|
|
286
481
|
if (!isLangSmithTracingEnabled(!!options.proxyConfig)) {
|
|
@@ -299,65 +494,49 @@ async function submitLangsmithUserFeedback(options) {
|
|
|
299
494
|
feedbackSourceType: 'api',
|
|
300
495
|
});
|
|
301
496
|
};
|
|
302
|
-
|
|
303
|
-
const headers = await options.proxyConfig.getAuthHeaders();
|
|
304
|
-
await proxyHeaderStore.run(headers, call);
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
await call();
|
|
308
|
-
}
|
|
497
|
+
await withProxyHeaders(options.proxyConfig, call);
|
|
309
498
|
return true;
|
|
310
499
|
}
|
|
311
|
-
function
|
|
312
|
-
const
|
|
313
|
-
if (
|
|
314
|
-
return
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
500
|
+
function getCurrentOtelSpanContext() {
|
|
501
|
+
const activeSpanContext = api_1.trace.getSpan(api_1.context.active())?.spanContext();
|
|
502
|
+
if (activeSpanContext) {
|
|
503
|
+
return {
|
|
504
|
+
traceId: activeSpanContext.traceId,
|
|
505
|
+
spanId: activeSpanContext.spanId,
|
|
506
|
+
};
|
|
318
507
|
}
|
|
319
|
-
|
|
320
|
-
|
|
508
|
+
const currentRun = getCurrentProductTrace()?.currentRun;
|
|
509
|
+
if (currentRun?.otelTraceId && currentRun.otelSpanId) {
|
|
510
|
+
return {
|
|
511
|
+
traceId: currentRun.otelTraceId,
|
|
512
|
+
spanId: currentRun.otelSpanId,
|
|
513
|
+
};
|
|
321
514
|
}
|
|
515
|
+
return undefined;
|
|
322
516
|
}
|
|
323
|
-
function
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
store.current = parentRun ?? null;
|
|
327
|
-
}
|
|
328
|
-
else if (parentRun) {
|
|
329
|
-
traceParentOverrideStorage.enterWith({ current: parentRun });
|
|
330
|
-
}
|
|
517
|
+
function getCurrentTraceToolCallId() {
|
|
518
|
+
const metadata = getCurrentProductTrace()?.currentRun.metadata;
|
|
519
|
+
return typeof metadata?.tool_call_id === 'string' ? metadata.tool_call_id : undefined;
|
|
331
520
|
}
|
|
332
521
|
function mergeCurrentTraceMetadata(metadata) {
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
const mergedMetadata = mergeRunTreeMetadata(currentRun.metadata, metadata);
|
|
338
|
-
if (mergedMetadata) {
|
|
339
|
-
currentRun.metadata = mergedMetadata;
|
|
522
|
+
const currentProductTrace = getCurrentProductTrace();
|
|
523
|
+
if (currentProductTrace) {
|
|
524
|
+
updateProductRunMetadata(currentProductTrace.runtime, currentProductTrace.currentRun, metadata);
|
|
340
525
|
}
|
|
341
526
|
}
|
|
342
527
|
function appendRootRunMetadata(root, patch) {
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
currentRun.metadata = merged;
|
|
352
|
-
}
|
|
528
|
+
const runtime = getCurrentProductTrace()?.runtime ?? otelTraceRuntimes.get(root.traceId);
|
|
529
|
+
if (runtime) {
|
|
530
|
+
updateProductRunMetadata(runtime, root, patch);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const mergedMetadata = mergeMetadata(root.metadata, patch);
|
|
534
|
+
if (mergedMetadata) {
|
|
535
|
+
root.metadata = mergedMetadata;
|
|
353
536
|
}
|
|
354
537
|
}
|
|
355
538
|
function appendGeneratedWorkflowIdToRootMetadata(root, workflowId) {
|
|
356
|
-
const
|
|
357
|
-
const metadata = currentRun?.id === root.id
|
|
358
|
-
? mergeRunTreeMetadata(root.metadata, currentRun.metadata)
|
|
359
|
-
: root.metadata;
|
|
360
|
-
const generatedWorkflowIds = metadata?.generated_workflow_ids;
|
|
539
|
+
const generatedWorkflowIds = root.metadata?.generated_workflow_ids;
|
|
361
540
|
const existing = Array.isArray(generatedWorkflowIds)
|
|
362
541
|
? generatedWorkflowIds.filter((value) => typeof value === 'string')
|
|
363
542
|
: [];
|
|
@@ -370,86 +549,52 @@ function mergeTraceRunInputs(run, inputs) {
|
|
|
370
549
|
if (!run) {
|
|
371
550
|
return;
|
|
372
551
|
}
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
currentRun.inputs = mergedInputs;
|
|
552
|
+
const runtime = getCurrentProductTrace()?.runtime ?? otelTraceRuntimes.get(run.traceId);
|
|
553
|
+
if (runtime) {
|
|
554
|
+
updateProductRunInputs(runtime, run, inputs);
|
|
555
|
+
return;
|
|
378
556
|
}
|
|
379
|
-
|
|
380
|
-
function buildAgentTraceInputs(options) {
|
|
381
|
-
return sanitizeTracePayload({
|
|
382
|
-
...(options.systemPrompt ? { system_prompt: serializeTraceText(options.systemPrompt) } : {}),
|
|
383
|
-
...(options.modelId !== undefined ? { model: serializeModelIdForTrace(options.modelId) } : {}),
|
|
384
|
-
...(options.toolSearchEnabled !== undefined
|
|
385
|
-
? { tool_search_enabled: options.toolSearchEnabled }
|
|
386
|
-
: {}),
|
|
387
|
-
...(options.inputProcessors?.length ? { input_processors: options.inputProcessors } : {}),
|
|
388
|
-
...summarizeMemoryBinding(options.memory),
|
|
389
|
-
...summarizeToolSet('loaded', options.tools),
|
|
390
|
-
...summarizeToolSet('deferred', options.deferredTools),
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
async function withTraceParentContext(parentRun, fn) {
|
|
394
|
-
return await traceParentOverrideStorage.run({ current: parentRun ?? null }, fn);
|
|
395
|
-
}
|
|
396
|
-
async function postChildRun(parentRun, options) {
|
|
397
|
-
const childRun = parentRun.createChild({
|
|
398
|
-
name: options.name,
|
|
399
|
-
run_type: options.runType ?? 'chain',
|
|
400
|
-
tags: normalizeTags(DEFAULT_TAGS, parentRun.tags, options.tags),
|
|
401
|
-
metadata: mergeRunTreeMetadata(parentRun.metadata, options.metadata),
|
|
402
|
-
inputs: sanitizeTracePayload(options.inputs),
|
|
403
|
-
});
|
|
404
|
-
childRun.parent_run_id ??= parentRun.id;
|
|
405
|
-
await childRun.postRun();
|
|
406
|
-
return childRun;
|
|
407
|
-
}
|
|
408
|
-
async function finishRunTree(runTree, options) {
|
|
409
|
-
await runTree.end(options?.outputs !== undefined ? sanitizeTracePayload(options.outputs) : undefined, options?.error, Date.now(), mergeMetadata(options?.metadata));
|
|
410
|
-
await runTree.patchRun();
|
|
557
|
+
run.inputs = (0, trace_payloads_1.sanitizeTracePayload)((0, trace_payloads_1.mergeTraceInputs)(run.inputs, inputs));
|
|
411
558
|
}
|
|
412
559
|
async function withCurrentTraceSpan(options, fn) {
|
|
413
|
-
const
|
|
414
|
-
if (!
|
|
560
|
+
const currentProductTrace = getCurrentProductTrace();
|
|
561
|
+
if (!currentProductTrace) {
|
|
562
|
+
return await fn();
|
|
563
|
+
}
|
|
564
|
+
const activeParentContext = getActiveOtelContextWithSpan(currentProductTrace.currentRun.otelTraceId);
|
|
565
|
+
let spanRun;
|
|
566
|
+
try {
|
|
567
|
+
spanRun = startProductSpan(currentProductTrace.runtime, {
|
|
568
|
+
projectName: currentProductTrace.currentRun.projectName,
|
|
569
|
+
name: options.name,
|
|
570
|
+
canonicalName: options.canonicalName,
|
|
571
|
+
runType: options.runType ?? 'chain',
|
|
572
|
+
tags: options.tags,
|
|
573
|
+
metadata: options.metadata,
|
|
574
|
+
inputs: options.inputs,
|
|
575
|
+
parentRun: currentProductTrace.currentRun,
|
|
576
|
+
...(activeParentContext ? { parentContext: activeParentContext } : {}),
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
catch {
|
|
415
580
|
return await fn();
|
|
416
581
|
}
|
|
417
|
-
const spanRun = await postChildRun(parentRun, {
|
|
418
|
-
name: options.name,
|
|
419
|
-
runType: options.runType ?? 'chain',
|
|
420
|
-
tags: options.tags,
|
|
421
|
-
metadata: options.metadata,
|
|
422
|
-
inputs: options.inputs,
|
|
423
|
-
});
|
|
424
582
|
try {
|
|
425
|
-
const result = await (
|
|
426
|
-
await
|
|
583
|
+
const result = await withProductSpanContextBestEffort(currentProductTrace.runtime, spanRun, fn);
|
|
584
|
+
await finishProductSpanBestEffort(currentProductTrace.runtime, spanRun, {
|
|
427
585
|
...(options.processOutputs ? { outputs: options.processOutputs(result) } : {}),
|
|
428
586
|
metadata: { final_status: 'completed' },
|
|
429
587
|
});
|
|
430
588
|
return result;
|
|
431
589
|
}
|
|
432
590
|
catch (error) {
|
|
433
|
-
await
|
|
591
|
+
await finishProductSpanBestEffort(currentProductTrace.runtime, spanRun, {
|
|
434
592
|
error: normalizeErrorMessage(error),
|
|
435
593
|
metadata: { final_status: 'error' },
|
|
436
594
|
});
|
|
437
595
|
throw error;
|
|
438
596
|
}
|
|
439
597
|
}
|
|
440
|
-
async function startHitlChildRun(parentRun, name, inputs, metadata) {
|
|
441
|
-
const hitlRun = await postChildRun(parentRun, {
|
|
442
|
-
name,
|
|
443
|
-
runType: 'chain',
|
|
444
|
-
tags: ['hitl'],
|
|
445
|
-
metadata,
|
|
446
|
-
inputs,
|
|
447
|
-
});
|
|
448
|
-
await finishRunTree(hitlRun, {
|
|
449
|
-
outputs: inputs,
|
|
450
|
-
metadata: { final_status: 'completed' },
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
598
|
function buildSuspendMetadata(toolName, suspendPayload) {
|
|
454
599
|
if (!(0, stream_helpers_1.isRecord)(suspendPayload)) {
|
|
455
600
|
return { tool_name: toolName };
|
|
@@ -465,145 +610,149 @@ function buildSuspendMetadata(toolName, suspendPayload) {
|
|
|
465
610
|
...(typeof suspendPayload.severity === 'string' ? { severity: suspendPayload.severity } : {}),
|
|
466
611
|
};
|
|
467
612
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
613
|
+
function isInterruptibleToolContext(context) {
|
|
614
|
+
return (0, stream_helpers_1.isRecord)(context) && typeof context.suspend === 'function';
|
|
615
|
+
}
|
|
616
|
+
function getToolCallId(context) {
|
|
617
|
+
return (0, stream_helpers_1.isRecord)(context) && typeof context.toolCallId === 'string'
|
|
618
|
+
? context.toolCallId
|
|
619
|
+
: undefined;
|
|
620
|
+
}
|
|
621
|
+
async function startAndFinishProductChildSpan(currentTrace, options) {
|
|
622
|
+
const activeParentContext = getActiveOtelContextWithSpan(currentTrace.currentRun.otelTraceId);
|
|
623
|
+
let childRun;
|
|
624
|
+
try {
|
|
625
|
+
childRun = startProductSpan(currentTrace.runtime, {
|
|
626
|
+
projectName: currentTrace.currentRun.projectName,
|
|
627
|
+
name: options.name,
|
|
628
|
+
canonicalName: options.canonicalName,
|
|
629
|
+
runType: options.runType ?? 'chain',
|
|
630
|
+
tags: options.tags,
|
|
631
|
+
metadata: mergeMetadata(currentTrace.currentRun.metadata, options.metadata),
|
|
632
|
+
inputs: options.inputs,
|
|
633
|
+
parentRun: currentTrace.currentRun,
|
|
634
|
+
...(activeParentContext ? { parentContext: activeParentContext } : {}),
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
catch {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
await finishProductSpanBestEffort(currentTrace.runtime, childRun, {
|
|
641
|
+
...(options.outputs !== undefined ? { outputs: options.outputs } : {}),
|
|
642
|
+
...(options.error ? { error: options.error } : {}),
|
|
643
|
+
metadata: {
|
|
644
|
+
final_status: options.error ? 'error' : 'completed',
|
|
645
|
+
},
|
|
646
|
+
forceFlush: options.forceFlush,
|
|
491
647
|
});
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
const originalSuspend = context.agent?.suspend;
|
|
500
|
-
const wrappedContext = context.agent && typeof originalSuspend === 'function'
|
|
648
|
+
}
|
|
649
|
+
async function traceProductSuspendableToolExecute(tool, input, context, currentTrace) {
|
|
650
|
+
const resumeData = isInterruptibleToolContext(context) ? context.resumeData : undefined;
|
|
651
|
+
const isResume = resumeData !== undefined && resumeData !== null;
|
|
652
|
+
const toolCallId = getToolCallId(context);
|
|
653
|
+
const originalSuspend = isInterruptibleToolContext(context) ? context.suspend : undefined;
|
|
654
|
+
const wrappedContext = typeof originalSuspend === 'function'
|
|
501
655
|
? {
|
|
502
656
|
...context,
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
},
|
|
657
|
+
suspend: async (suspendPayload) => {
|
|
658
|
+
await startAndFinishProductChildSpan(currentTrace, {
|
|
659
|
+
name: 'hitl: suspend',
|
|
660
|
+
canonicalName: 'instance-ai.hitl.suspend',
|
|
661
|
+
runType: 'chain',
|
|
662
|
+
tags: ['hitl'],
|
|
663
|
+
metadata: mergeMetadata(buildSuspendMetadata(tool.name, suspendPayload), {
|
|
664
|
+
...(toolCallId ? { tool_call_id: toolCallId } : {}),
|
|
665
|
+
}),
|
|
666
|
+
inputs: suspendPayload,
|
|
667
|
+
outputs: suspendPayload,
|
|
668
|
+
forceFlush: true,
|
|
669
|
+
});
|
|
670
|
+
return await originalSuspend(suspendPayload);
|
|
518
671
|
},
|
|
519
672
|
}
|
|
520
673
|
: context;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
metadata:
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
674
|
+
if (isResume) {
|
|
675
|
+
await startAndFinishProductChildSpan(currentTrace, {
|
|
676
|
+
name: 'hitl: resume',
|
|
677
|
+
canonicalName: 'instance-ai.hitl.resume',
|
|
678
|
+
runType: 'chain',
|
|
679
|
+
tags: ['hitl', 'resume'],
|
|
680
|
+
metadata: mergeMetadata(buildSuspendMetadata(tool.name, resumeData), {
|
|
681
|
+
approved: (0, stream_helpers_1.isRecord)(resumeData) ? resumeData.approved : undefined,
|
|
682
|
+
...(toolCallId ? { tool_call_id: toolCallId } : {}),
|
|
683
|
+
}),
|
|
684
|
+
inputs: resumeData,
|
|
685
|
+
outputs: {
|
|
686
|
+
status: 'resumed',
|
|
687
|
+
},
|
|
688
|
+
forceFlush: true,
|
|
535
689
|
});
|
|
536
|
-
throw error;
|
|
537
690
|
}
|
|
691
|
+
return await tool.handler(input, wrappedContext);
|
|
538
692
|
}
|
|
539
|
-
async function
|
|
540
|
-
const
|
|
541
|
-
if (!
|
|
542
|
-
return await tool.
|
|
543
|
-
}
|
|
544
|
-
const toolRun = await postChildRun(parentRun, {
|
|
545
|
-
name: `tool:${tool.id}`,
|
|
546
|
-
runType: 'tool',
|
|
547
|
-
tags: normalizeTags(['tool'], options?.tags),
|
|
548
|
-
metadata: mergeMetadata(options?.metadata, {
|
|
549
|
-
tool_name: tool.id,
|
|
550
|
-
...(options?.agentRole ? { agent_role: options.agentRole } : {}),
|
|
551
|
-
...normalizeModelMetadata(options?.metadata?.model_id),
|
|
552
|
-
}),
|
|
553
|
-
inputs: { input },
|
|
554
|
-
});
|
|
555
|
-
try {
|
|
556
|
-
const result = await (0, traceable_1.withRunTree)(toolRun, async () => await tool.execute(input, context));
|
|
557
|
-
await finishRunTree(toolRun, {
|
|
558
|
-
outputs: result,
|
|
559
|
-
metadata: { final_status: 'completed' },
|
|
560
|
-
});
|
|
561
|
-
return result;
|
|
562
|
-
}
|
|
563
|
-
catch (error) {
|
|
564
|
-
await finishRunTree(toolRun, {
|
|
565
|
-
error: normalizeErrorMessage(error),
|
|
566
|
-
metadata: { final_status: 'error' },
|
|
567
|
-
});
|
|
568
|
-
throw error;
|
|
693
|
+
async function traceSuspendableToolExecute(tool, input, context) {
|
|
694
|
+
const currentProductTrace = getCurrentProductTrace();
|
|
695
|
+
if (!currentProductTrace) {
|
|
696
|
+
return await tool.handler(input, context);
|
|
569
697
|
}
|
|
698
|
+
return await traceProductSuspendableToolExecute(tool, input, context, currentProductTrace);
|
|
570
699
|
}
|
|
571
|
-
function createTraceContext(projectName, traceKind, rootRun, actorRun,
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
700
|
+
function createTraceContext(projectName, traceKind, rootRun, actorRun, otelRuntime, proxyConfig, telemetryFactory) {
|
|
701
|
+
otelTraceRuntimes.set(rootRun.traceId, otelRuntime);
|
|
702
|
+
const startChildRun = async (parentRun, init) => await withProxyHeadersBestEffort(proxyConfig, () => {
|
|
703
|
+
const activeParentContext = getActiveOtelContextWithSpan(parentRun.otelTraceId);
|
|
704
|
+
try {
|
|
705
|
+
return startProductSpan(otelRuntime, {
|
|
706
|
+
projectName,
|
|
707
|
+
name: init.name,
|
|
708
|
+
canonicalName: init.canonicalName,
|
|
709
|
+
runType: init.runType,
|
|
710
|
+
tags: init.tags,
|
|
711
|
+
metadata: mergeMetadata(parentRun.metadata, init.metadata),
|
|
712
|
+
inputs: init.inputs,
|
|
713
|
+
parentRun,
|
|
714
|
+
...(activeParentContext ? { parentContext: activeParentContext } : {}),
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
catch {
|
|
718
|
+
return createFallbackChildRun(projectName, parentRun, init);
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
const withActiveSpan = async (run, fn) => await withProxyHeadersBestEffort(proxyConfig, async () => await withProductSpanContextBestEffort(otelRuntime, run, fn));
|
|
722
|
+
const withRunTree = withActiveSpan;
|
|
580
723
|
const finishRun = async (run, finishOptions) => {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
724
|
+
const isRootRun = !run.parentRunId;
|
|
725
|
+
await withProxyHeadersBestEffort(proxyConfig, async () => await finishProductSpanBestEffort(otelRuntime, run, isRootRun ? { ...finishOptions, forceFlush: true } : finishOptions));
|
|
726
|
+
if (isRootRun) {
|
|
727
|
+
await withProxyHeadersBestEffort(proxyConfig, async () => await shutdownProductOtelRuntime(otelRuntime, run.traceId));
|
|
584
728
|
}
|
|
585
729
|
};
|
|
586
730
|
const failRun = async (run, error, metadata) => {
|
|
587
|
-
|
|
731
|
+
const isRootRun = !run.parentRunId;
|
|
732
|
+
await withProxyHeadersBestEffort(proxyConfig, async () => await finishProductSpanBestEffort(otelRuntime, run, {
|
|
588
733
|
error: normalizeErrorMessage(error),
|
|
589
734
|
metadata,
|
|
735
|
+
forceFlush: isRootRun,
|
|
590
736
|
}));
|
|
591
|
-
if (
|
|
592
|
-
|
|
737
|
+
if (isRootRun) {
|
|
738
|
+
await withProxyHeadersBestEffort(proxyConfig, async () => await shutdownProductOtelRuntime(otelRuntime, run.traceId));
|
|
593
739
|
}
|
|
594
740
|
};
|
|
595
741
|
const ctx = {
|
|
596
742
|
projectName,
|
|
597
743
|
traceKind,
|
|
744
|
+
...(proxyConfig ? { proxyConfig } : {}),
|
|
598
745
|
rootRun,
|
|
599
746
|
actorRun,
|
|
600
747
|
messageRun: rootRun,
|
|
601
748
|
orchestratorRun: actorRun,
|
|
602
749
|
startChildRun,
|
|
603
750
|
withRunTree,
|
|
751
|
+
withActiveSpan,
|
|
752
|
+
toHeaders: () => ({}),
|
|
604
753
|
finishRun,
|
|
605
754
|
failRun,
|
|
606
|
-
|
|
755
|
+
...(telemetryFactory ? { getTelemetry: telemetryFactory } : {}),
|
|
607
756
|
wrapTools: (tools, traceOptions) => {
|
|
608
757
|
if (ctx.replayMode === 'replay' && ctx.traceIndex && ctx.idRemapper) {
|
|
609
758
|
return replayWrapTools(tools, ctx.traceIndex, ctx.idRemapper, traceOptions);
|
|
@@ -617,234 +766,113 @@ function createTraceContext(projectName, traceKind, rootRun, actorRun, getProxyH
|
|
|
617
766
|
};
|
|
618
767
|
return ctx;
|
|
619
768
|
}
|
|
620
|
-
function
|
|
621
|
-
|
|
769
|
+
function isTraceableNativeTool(value) {
|
|
770
|
+
return ((0, stream_helpers_1.isRecord)(value) &&
|
|
771
|
+
typeof value.name === 'string' &&
|
|
772
|
+
typeof value.description === 'string' &&
|
|
773
|
+
typeof value.handler === 'function');
|
|
774
|
+
}
|
|
775
|
+
function wrapToolHandler(tool) {
|
|
622
776
|
return {
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
runType: tree.run_type,
|
|
626
|
-
projectName: tree.project_name,
|
|
627
|
-
startTime: tree.start_time,
|
|
628
|
-
...(tree.end_time ? { endTime: tree.end_time } : {}),
|
|
629
|
-
traceId: tree.trace_id,
|
|
630
|
-
dottedOrder: tree.dotted_order,
|
|
631
|
-
executionOrder: tree.execution_order,
|
|
632
|
-
childExecutionOrder: tree.child_execution_order,
|
|
633
|
-
...(parentRunId ? { parentRunId } : {}),
|
|
634
|
-
...(tree.tags ? { tags: [...tree.tags] } : {}),
|
|
635
|
-
...(tree.metadata ? { metadata: { ...tree.metadata } } : {}),
|
|
636
|
-
...(tree.inputs ? { inputs: sanitizeTracePayload(tree.inputs) } : {}),
|
|
637
|
-
...(tree.outputs ? { outputs: sanitizeTracePayload(tree.outputs) } : {}),
|
|
638
|
-
...(tree.error ? { error: tree.error } : {}),
|
|
777
|
+
...tool,
|
|
778
|
+
handler: async (input, context) => await traceSuspendableToolExecute(tool, input, context),
|
|
639
779
|
};
|
|
640
780
|
}
|
|
641
|
-
function
|
|
642
|
-
|
|
643
|
-
}
|
|
644
|
-
function hydrateRunTree(state) {
|
|
645
|
-
const client = traceClients.get(state.traceId);
|
|
646
|
-
return new langsmith_1.RunTree({
|
|
647
|
-
id: state.id,
|
|
648
|
-
name: state.name,
|
|
649
|
-
run_type: state.runType,
|
|
650
|
-
project_name: state.projectName,
|
|
651
|
-
start_time: state.startTime,
|
|
652
|
-
end_time: state.endTime,
|
|
653
|
-
parent_run_id: state.parentRunId,
|
|
654
|
-
execution_order: state.executionOrder,
|
|
655
|
-
child_execution_order: state.childExecutionOrder,
|
|
656
|
-
trace_id: state.traceId,
|
|
657
|
-
dotted_order: state.dottedOrder,
|
|
658
|
-
tags: state.tags,
|
|
659
|
-
metadata: state.metadata,
|
|
660
|
-
inputs: state.inputs,
|
|
661
|
-
outputs: state.outputs,
|
|
662
|
-
error: state.error,
|
|
663
|
-
serialized: {},
|
|
664
|
-
client: client ?? getOrCreateDirectClient(),
|
|
665
|
-
});
|
|
666
|
-
}
|
|
667
|
-
function isTraceableMastraTool(value) {
|
|
668
|
-
return ((0, stream_helpers_1.isRecord)(value) &&
|
|
669
|
-
typeof value.id === 'string' &&
|
|
670
|
-
typeof value.description === 'string' &&
|
|
671
|
-
(!('execute' in value) || typeof value.execute === 'function'));
|
|
672
|
-
}
|
|
673
|
-
function wrapToolExecute(tool, options) {
|
|
674
|
-
if (typeof tool.execute !== 'function') {
|
|
675
|
-
return tool;
|
|
676
|
-
}
|
|
677
|
-
if (tool.suspendSchema !== undefined || tool.resumeSchema !== undefined) {
|
|
678
|
-
return (0, tools_1.createTool)({
|
|
679
|
-
id: tool.id,
|
|
680
|
-
description: tool.description,
|
|
681
|
-
inputSchema: tool.inputSchema,
|
|
682
|
-
outputSchema: tool.outputSchema,
|
|
683
|
-
suspendSchema: tool.suspendSchema,
|
|
684
|
-
resumeSchema: tool.resumeSchema,
|
|
685
|
-
requestContextSchema: tool.requestContextSchema,
|
|
686
|
-
execute: async (input, context) => await traceSuspendableToolExecute(tool, options, input, context),
|
|
687
|
-
mastra: tool.mastra,
|
|
688
|
-
requireApproval: tool.requireApproval,
|
|
689
|
-
providerOptions: tool.providerOptions,
|
|
690
|
-
toModelOutput: tool.toModelOutput,
|
|
691
|
-
mcp: tool.mcp,
|
|
692
|
-
onInputStart: tool.onInputStart,
|
|
693
|
-
onInputDelta: tool.onInputDelta,
|
|
694
|
-
onInputAvailable: tool.onInputAvailable,
|
|
695
|
-
onOutput: tool.onOutput,
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
return (0, tools_1.createTool)({
|
|
699
|
-
id: tool.id,
|
|
700
|
-
description: tool.description,
|
|
701
|
-
inputSchema: tool.inputSchema,
|
|
702
|
-
outputSchema: tool.outputSchema,
|
|
703
|
-
suspendSchema: tool.suspendSchema,
|
|
704
|
-
resumeSchema: tool.resumeSchema,
|
|
705
|
-
requestContextSchema: tool.requestContextSchema,
|
|
706
|
-
execute: async (input, context) => await traceToolExecute(tool, options, input, context),
|
|
707
|
-
mastra: tool.mastra,
|
|
708
|
-
requireApproval: tool.requireApproval,
|
|
709
|
-
providerOptions: tool.providerOptions,
|
|
710
|
-
toModelOutput: tool.toModelOutput,
|
|
711
|
-
mcp: tool.mcp,
|
|
712
|
-
onInputStart: tool.onInputStart,
|
|
713
|
-
onInputDelta: tool.onInputDelta,
|
|
714
|
-
onInputAvailable: tool.onInputAvailable,
|
|
715
|
-
onOutput: tool.onOutput,
|
|
716
|
-
});
|
|
781
|
+
function shouldTraceLocalToolExecution(tool) {
|
|
782
|
+
return tool.suspendSchema !== undefined || tool.resumeSchema !== undefined;
|
|
717
783
|
}
|
|
718
|
-
function wrapTools(tools,
|
|
719
|
-
const wrapped =
|
|
720
|
-
const
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
784
|
+
function wrapTools(tools, _options) {
|
|
785
|
+
const wrapped = (0, tool_registry_1.createToolRegistry)();
|
|
786
|
+
for (const [name, tool] of tools) {
|
|
787
|
+
wrapped.set(name, isTraceableNativeTool(tool) && shouldTraceLocalToolExecution(tool)
|
|
788
|
+
? wrapToolHandler(tool)
|
|
789
|
+
: tool);
|
|
724
790
|
}
|
|
725
791
|
return wrapped;
|
|
726
792
|
}
|
|
727
793
|
function replayWrapTool(tool, traceIndex, idRemapper, agentRole) {
|
|
728
|
-
return
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
outputSchema: tool.outputSchema,
|
|
733
|
-
suspendSchema: tool.suspendSchema,
|
|
734
|
-
resumeSchema: tool.resumeSchema,
|
|
735
|
-
requestContextSchema: tool.requestContextSchema,
|
|
736
|
-
execute: async (input, context) => {
|
|
737
|
-
const event = traceIndex.nextMatching(agentRole, tool.id);
|
|
794
|
+
return {
|
|
795
|
+
...tool,
|
|
796
|
+
handler: async (input, context) => {
|
|
797
|
+
const event = traceIndex.nextMatching(agentRole, tool.name);
|
|
738
798
|
const remappedInput = idRemapper.remapInput(input);
|
|
739
|
-
const realOutput = await tool.
|
|
799
|
+
const realOutput = await tool.handler(remappedInput, context);
|
|
740
800
|
if (event) {
|
|
741
801
|
idRemapper.learn(event.output, realOutput);
|
|
742
802
|
}
|
|
743
803
|
return realOutput;
|
|
744
804
|
},
|
|
745
|
-
|
|
746
|
-
requireApproval: tool.requireApproval,
|
|
747
|
-
providerOptions: tool.providerOptions,
|
|
748
|
-
toModelOutput: tool.toModelOutput,
|
|
749
|
-
mcp: tool.mcp,
|
|
750
|
-
onInputStart: tool.onInputStart,
|
|
751
|
-
onInputDelta: tool.onInputDelta,
|
|
752
|
-
onInputAvailable: tool.onInputAvailable,
|
|
753
|
-
onOutput: tool.onOutput,
|
|
754
|
-
});
|
|
805
|
+
};
|
|
755
806
|
}
|
|
756
807
|
function pureReplayWrapTool(tool, traceIndex, idRemapper, agentRole) {
|
|
757
|
-
return
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
outputSchema: tool.outputSchema,
|
|
762
|
-
suspendSchema: tool.suspendSchema,
|
|
763
|
-
resumeSchema: tool.resumeSchema,
|
|
764
|
-
requestContextSchema: tool.requestContextSchema,
|
|
765
|
-
execute: async (_input, _context) => {
|
|
766
|
-
const event = traceIndex.nextMatching(agentRole, tool.id);
|
|
808
|
+
return {
|
|
809
|
+
...tool,
|
|
810
|
+
handler: async (_input, _context) => {
|
|
811
|
+
const event = traceIndex.nextMatching(agentRole, tool.name);
|
|
767
812
|
if (!event) {
|
|
768
|
-
throw new Error(`No recorded output for pure-replay tool "${tool.
|
|
813
|
+
throw new Error(`No recorded output for pure-replay tool "${tool.name}" in role "${agentRole}"`);
|
|
769
814
|
}
|
|
770
815
|
return await Promise.resolve(idRemapper.remapOutput(event.output));
|
|
771
816
|
},
|
|
772
|
-
|
|
773
|
-
requireApproval: tool.requireApproval,
|
|
774
|
-
providerOptions: tool.providerOptions,
|
|
775
|
-
toModelOutput: tool.toModelOutput,
|
|
776
|
-
mcp: tool.mcp,
|
|
777
|
-
onInputStart: tool.onInputStart,
|
|
778
|
-
onInputDelta: tool.onInputDelta,
|
|
779
|
-
onInputAvailable: tool.onInputAvailable,
|
|
780
|
-
onOutput: tool.onOutput,
|
|
781
|
-
});
|
|
817
|
+
};
|
|
782
818
|
}
|
|
783
819
|
function replayWrapTools(tools, traceIndex, idRemapper, options) {
|
|
784
820
|
const agentRole = options?.agentRole ?? 'orchestrator';
|
|
785
|
-
const wrapped =
|
|
786
|
-
const
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
wrapped[name] = tools[name];
|
|
821
|
+
const wrapped = (0, tool_registry_1.createToolRegistry)();
|
|
822
|
+
for (const [name, tool] of tools) {
|
|
823
|
+
if (!isTraceableNativeTool(tool)) {
|
|
824
|
+
wrapped.set(name, tool);
|
|
790
825
|
continue;
|
|
791
826
|
}
|
|
792
|
-
if (trace_replay_1.PURE_REPLAY_TOOLS.has(tool.
|
|
793
|
-
wrapped
|
|
827
|
+
if (trace_replay_1.PURE_REPLAY_TOOLS.has(tool.name)) {
|
|
828
|
+
wrapped.set(name, pureReplayWrapTool(tool, traceIndex, idRemapper, agentRole));
|
|
794
829
|
}
|
|
795
830
|
else {
|
|
796
|
-
wrapped
|
|
831
|
+
wrapped.set(name, replayWrapTool(tool, traceIndex, idRemapper, agentRole));
|
|
797
832
|
}
|
|
798
833
|
}
|
|
799
834
|
return wrapped;
|
|
800
835
|
}
|
|
801
|
-
function recordWrapTool(tool, traceWriter, agentRole
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
inputSchema: traced.inputSchema,
|
|
807
|
-
outputSchema: traced.outputSchema,
|
|
808
|
-
suspendSchema: traced.suspendSchema,
|
|
809
|
-
resumeSchema: traced.resumeSchema,
|
|
810
|
-
requestContextSchema: traced.requestContextSchema,
|
|
811
|
-
execute: async (input, context) => {
|
|
812
|
-
const resumeData = context?.agent?.resumeData;
|
|
836
|
+
function recordWrapTool(tool, traceWriter, agentRole) {
|
|
837
|
+
return {
|
|
838
|
+
...tool,
|
|
839
|
+
handler: async (input, context) => {
|
|
840
|
+
const resumeData = isInterruptibleToolContext(context) ? context.resumeData : undefined;
|
|
813
841
|
const inputRecord = (input ?? {});
|
|
814
|
-
|
|
842
|
+
let capturedSuspendPayload;
|
|
843
|
+
const wrappedContext = isInterruptibleToolContext(context)
|
|
844
|
+
? {
|
|
845
|
+
...context,
|
|
846
|
+
suspend: async (suspendPayload) => {
|
|
847
|
+
capturedSuspendPayload = (0, stream_helpers_1.isRecord)(suspendPayload) ? suspendPayload : {};
|
|
848
|
+
return await context.suspend(suspendPayload);
|
|
849
|
+
},
|
|
850
|
+
}
|
|
851
|
+
: context;
|
|
852
|
+
const result = await tool.handler(input, wrappedContext);
|
|
815
853
|
const outputRecord = (result ?? {});
|
|
816
854
|
if (resumeData !== undefined && resumeData !== null) {
|
|
817
|
-
traceWriter.recordToolResume(agentRole, tool.
|
|
855
|
+
traceWriter.recordToolResume(agentRole, tool.name, inputRecord, outputRecord, resumeData);
|
|
818
856
|
}
|
|
819
|
-
else if (
|
|
820
|
-
traceWriter.recordToolSuspend(agentRole, tool.
|
|
857
|
+
else if (capturedSuspendPayload) {
|
|
858
|
+
traceWriter.recordToolSuspend(agentRole, tool.name, inputRecord, {}, capturedSuspendPayload);
|
|
821
859
|
}
|
|
822
860
|
else {
|
|
823
|
-
traceWriter.recordToolCall(agentRole, tool.
|
|
861
|
+
traceWriter.recordToolCall(agentRole, tool.name, inputRecord, outputRecord);
|
|
824
862
|
}
|
|
825
863
|
return result;
|
|
826
864
|
},
|
|
827
|
-
|
|
828
|
-
requireApproval: traced.requireApproval,
|
|
829
|
-
providerOptions: traced.providerOptions,
|
|
830
|
-
toModelOutput: traced.toModelOutput,
|
|
831
|
-
mcp: traced.mcp,
|
|
832
|
-
onInputStart: traced.onInputStart,
|
|
833
|
-
onInputDelta: traced.onInputDelta,
|
|
834
|
-
onInputAvailable: traced.onInputAvailable,
|
|
835
|
-
onOutput: traced.onOutput,
|
|
836
|
-
});
|
|
865
|
+
};
|
|
837
866
|
}
|
|
838
867
|
function recordWrapTools(tools, traceWriter, options) {
|
|
839
868
|
const agentRole = options?.agentRole ?? 'orchestrator';
|
|
840
|
-
const wrapped =
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
wrapped[name] = tools[name];
|
|
869
|
+
const wrapped = (0, tool_registry_1.createToolRegistry)();
|
|
870
|
+
for (const [name, tool] of tools) {
|
|
871
|
+
if (!isTraceableNativeTool(tool)) {
|
|
872
|
+
wrapped.set(name, tool);
|
|
845
873
|
continue;
|
|
846
874
|
}
|
|
847
|
-
wrapped
|
|
875
|
+
wrapped.set(name, recordWrapTool(tool, traceWriter, agentRole));
|
|
848
876
|
}
|
|
849
877
|
return wrapped;
|
|
850
878
|
}
|
|
@@ -869,9 +897,10 @@ function createTraceReplayOnlyContext() {
|
|
|
869
897
|
orchestratorRun: stubRun,
|
|
870
898
|
startChildRun: async () => await Promise.resolve(stubRun),
|
|
871
899
|
withRunTree: async (_run, fn) => await fn(),
|
|
900
|
+
withActiveSpan: async (_run, fn) => await fn(),
|
|
901
|
+
toHeaders: () => ({}),
|
|
872
902
|
finishRun: async () => { },
|
|
873
903
|
failRun: async () => { },
|
|
874
|
-
toHeaders: () => ({}),
|
|
875
904
|
wrapTools: (tools, traceOptions) => {
|
|
876
905
|
if (ctx.replayMode === 'replay' && ctx.traceIndex && ctx.idRemapper) {
|
|
877
906
|
return replayWrapTools(tools, ctx.traceIndex, ctx.idRemapper, traceOptions);
|
|
@@ -885,149 +914,384 @@ function createTraceReplayOnlyContext() {
|
|
|
885
914
|
};
|
|
886
915
|
return ctx;
|
|
887
916
|
}
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
client: options.client ?? getOrCreateDirectClient(),
|
|
897
|
-
});
|
|
898
|
-
await runTree.postRun();
|
|
899
|
-
if (options.client) {
|
|
900
|
-
traceClients.set(runTree.trace_id, options.client);
|
|
901
|
-
}
|
|
902
|
-
return createRunStateFromTree(runTree);
|
|
917
|
+
let traceRuntimeVersions;
|
|
918
|
+
function extractPackageVersion(packageJson) {
|
|
919
|
+
if (!packageJson || typeof packageJson !== 'object')
|
|
920
|
+
return undefined;
|
|
921
|
+
if (!('version' in packageJson))
|
|
922
|
+
return undefined;
|
|
923
|
+
const { version } = packageJson;
|
|
924
|
+
return typeof version === 'string' ? version : undefined;
|
|
903
925
|
}
|
|
904
|
-
async function
|
|
905
|
-
const parentRun = hydrateRunTree(parentState);
|
|
906
|
-
const childRun = parentRun.createChild({
|
|
907
|
-
name: options.name,
|
|
908
|
-
run_type: options.runType ?? 'chain',
|
|
909
|
-
tags: normalizeTags(DEFAULT_TAGS, parentState.tags, options.tags),
|
|
910
|
-
metadata: mergeMetadata(parentRun.metadata, options.metadata),
|
|
911
|
-
inputs: sanitizeTracePayload(options.inputs),
|
|
912
|
-
});
|
|
913
|
-
syncRunState(parentState, parentRun);
|
|
914
|
-
await childRun.postRun();
|
|
915
|
-
return createRunStateFromTree(childRun);
|
|
916
|
-
}
|
|
917
|
-
async function finishTraceRun(runState, options) {
|
|
918
|
-
const runTree = hydrateRunTree(runState);
|
|
919
|
-
await runTree.end(options?.outputs !== undefined ? sanitizeTracePayload(options.outputs) : undefined, options?.error, Date.now(), mergeMetadata(options?.metadata));
|
|
920
|
-
await runTree.patchRun();
|
|
921
|
-
syncRunState(runState, runTree);
|
|
922
|
-
}
|
|
923
|
-
async function withSerializedRunTree(runState, fn) {
|
|
924
|
-
const runTree = hydrateRunTree(runState);
|
|
926
|
+
async function readPackageVersion(packageName) {
|
|
925
927
|
try {
|
|
926
|
-
return
|
|
928
|
+
return extractPackageVersion(hostRequire(`${packageName}/package.json`));
|
|
927
929
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
+
catch {
|
|
931
|
+
}
|
|
932
|
+
try {
|
|
933
|
+
let current = (0, node_path_1.dirname)(hostRequire.resolve(packageName));
|
|
934
|
+
const { root } = (0, node_path_1.parse)(current);
|
|
935
|
+
while (current !== root) {
|
|
936
|
+
const packageJsonPath = (0, node_path_1.join)(current, 'package.json');
|
|
937
|
+
try {
|
|
938
|
+
return extractPackageVersion(JSON.parse(await (0, promises_1.readFile)(packageJsonPath, 'utf8')));
|
|
939
|
+
}
|
|
940
|
+
catch {
|
|
941
|
+
current = (0, node_path_1.dirname)(current);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
catch {
|
|
930
946
|
}
|
|
947
|
+
return undefined;
|
|
948
|
+
}
|
|
949
|
+
async function getTraceRuntimeVersions() {
|
|
950
|
+
traceRuntimeVersions ??= (async () => {
|
|
951
|
+
const [agentsVersion, workflowSdkVersion] = await Promise.all([
|
|
952
|
+
readPackageVersion('@n8n/agents'),
|
|
953
|
+
readPackageVersion('@n8n/workflow-sdk'),
|
|
954
|
+
]);
|
|
955
|
+
return {
|
|
956
|
+
...(agentsVersion ? { agents_version: agentsVersion } : {}),
|
|
957
|
+
...(workflowSdkVersion ? { workflow_sdk_version: workflowSdkVersion } : {}),
|
|
958
|
+
};
|
|
959
|
+
})();
|
|
960
|
+
return await traceRuntimeVersions;
|
|
931
961
|
}
|
|
932
|
-
function buildBaseMetadata(options) {
|
|
962
|
+
async function buildBaseMetadata(options) {
|
|
933
963
|
return {
|
|
934
964
|
thread_id: options.threadId,
|
|
965
|
+
'langsmith.metadata.thread_id': options.threadId,
|
|
935
966
|
conversation_id: options.conversationId ?? options.threadId,
|
|
936
967
|
message_group_id: options.messageGroupId,
|
|
937
968
|
message_id: options.messageId,
|
|
938
969
|
run_id: options.runId,
|
|
970
|
+
activation_id: options.runId,
|
|
939
971
|
user_id: options.userId,
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
: {}),
|
|
972
|
+
'instance_ai.trace_version': OTEL_TRACE_VERSION,
|
|
973
|
+
...(await getTraceRuntimeVersions()),
|
|
943
974
|
...(options.n8nVersion !== undefined ? { n8n_version: options.n8nVersion } : {}),
|
|
944
975
|
...(options.workflowSdkVersion !== undefined
|
|
945
976
|
? { workflow_sdk_version: options.workflowSdkVersion }
|
|
946
977
|
: {}),
|
|
978
|
+
...(options.modelId !== undefined
|
|
979
|
+
? { model_id: (0, trace_payloads_1.serializeModelIdForTrace)(options.modelId) }
|
|
980
|
+
: {}),
|
|
947
981
|
...options.metadata,
|
|
948
982
|
};
|
|
949
983
|
}
|
|
984
|
+
function buildDetachedSubAgentMetadata(options, includeSpawnMetadata) {
|
|
985
|
+
return {
|
|
986
|
+
agent_role: options.role,
|
|
987
|
+
agent_id: options.agentId,
|
|
988
|
+
execution_mode: 'background_subagent',
|
|
989
|
+
trace_kind: 'background_subagent',
|
|
990
|
+
task_kind: options.kind,
|
|
991
|
+
...(options.taskId ? { task_id: options.taskId } : {}),
|
|
992
|
+
...(options.plannedTaskId ? { planned_task_id: options.plannedTaskId } : {}),
|
|
993
|
+
...(options.workItemId ? { work_item_id: options.workItemId } : {}),
|
|
994
|
+
...(includeSpawnMetadata && options.spawnedByTraceId
|
|
995
|
+
? { spawned_by_trace_id: options.spawnedByTraceId }
|
|
996
|
+
: {}),
|
|
997
|
+
...(includeSpawnMetadata && options.spawnedBySpanId
|
|
998
|
+
? { spawned_by_span_id: options.spawnedBySpanId }
|
|
999
|
+
: {}),
|
|
1000
|
+
...(includeSpawnMetadata && options.spawnedByRunId
|
|
1001
|
+
? { spawned_by_run_id: options.spawnedByRunId }
|
|
1002
|
+
: {}),
|
|
1003
|
+
...(includeSpawnMetadata && options.spawnedByAgentId
|
|
1004
|
+
? { spawned_by_agent_id: options.spawnedByAgentId }
|
|
1005
|
+
: {}),
|
|
1006
|
+
...(includeSpawnMetadata && options.spawnedByAgentRole
|
|
1007
|
+
? { spawned_by_agent_role: options.spawnedByAgentRole }
|
|
1008
|
+
: {}),
|
|
1009
|
+
...(includeSpawnMetadata && options.spawnedByToolCallId
|
|
1010
|
+
? { spawned_by_tool_call_id: options.spawnedByToolCallId }
|
|
1011
|
+
: {}),
|
|
1012
|
+
subagent_role: options.role,
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
function buildInternalOperationMetadata(operationName) {
|
|
1016
|
+
return {
|
|
1017
|
+
agent_role: operationName,
|
|
1018
|
+
execution_mode: 'internal',
|
|
1019
|
+
trace_kind: 'internal_operation',
|
|
1020
|
+
operation_name: operationName,
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
function createTelemetryFactory(options) {
|
|
1024
|
+
return (telemetryOptions) => {
|
|
1025
|
+
const actorRun = options.getActorRun?.() ?? options.actorRun;
|
|
1026
|
+
const agentRole = telemetryOptions.agentRole;
|
|
1027
|
+
const executionMode = telemetryOptions.executionMode ??
|
|
1028
|
+
(options.traceKind === 'background_subagent' ? 'background_subagent' : 'foreground');
|
|
1029
|
+
const metadata = (0, trace_payloads_1.toTelemetryMetadata)(options.baseMetadata, telemetryOptions.metadata, {
|
|
1030
|
+
agent_role: agentRole,
|
|
1031
|
+
execution_mode: executionMode,
|
|
1032
|
+
trace_kind: options.traceKind,
|
|
1033
|
+
langsmith_trace_id: options.rootRun.traceId,
|
|
1034
|
+
langsmith_root_run_id: options.rootRun.id,
|
|
1035
|
+
langsmith_actor_run_id: actorRun.id,
|
|
1036
|
+
});
|
|
1037
|
+
const functionId = telemetryOptions.functionId ?? (0, trace_labels_1.formatTelemetryFunctionId)(agentRole);
|
|
1038
|
+
if (options.baseTelemetry) {
|
|
1039
|
+
return {
|
|
1040
|
+
...options.baseTelemetry,
|
|
1041
|
+
functionId,
|
|
1042
|
+
metadata,
|
|
1043
|
+
recordInputs: true,
|
|
1044
|
+
recordOutputs: true,
|
|
1045
|
+
runtimeRootSpanEnabled: false,
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
return createLangSmithTelemetryBuilder(options.projectName, options.proxyConfig)
|
|
1049
|
+
.functionId(functionId)
|
|
1050
|
+
.metadata(metadata)
|
|
1051
|
+
.recordInputs(true)
|
|
1052
|
+
.recordOutputs(true)
|
|
1053
|
+
.runtimeRootSpan(false);
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
function createLangSmithTelemetryBuilder(projectName, proxyConfig) {
|
|
1057
|
+
return new agents_1.LangSmithTelemetry({
|
|
1058
|
+
project: projectName,
|
|
1059
|
+
transformExportedSpan: trace_payloads_1.redactLangSmithTelemetrySpan,
|
|
1060
|
+
...(proxyConfig
|
|
1061
|
+
? {
|
|
1062
|
+
apiKey: '-',
|
|
1063
|
+
endpoint: proxyConfig.apiUrl,
|
|
1064
|
+
headers: proxyConfig.getAuthHeaders,
|
|
1065
|
+
}
|
|
1066
|
+
: {}),
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
async function createProductOtelRuntime(projectName, proxyConfig) {
|
|
1070
|
+
const telemetry = await createLangSmithTelemetryBuilder(projectName, proxyConfig)
|
|
1071
|
+
.functionId('instance-ai.product')
|
|
1072
|
+
.metadata({})
|
|
1073
|
+
.recordInputs(true)
|
|
1074
|
+
.recordOutputs(true)
|
|
1075
|
+
.build();
|
|
1076
|
+
return {
|
|
1077
|
+
telemetry,
|
|
1078
|
+
spans: new Map(),
|
|
1079
|
+
contexts: new Map(),
|
|
1080
|
+
shutdown: false,
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
function createProductTraceContext(options) {
|
|
1084
|
+
return createTraceContext(options.projectName, options.traceKind, options.rootRun, options.actorRun, options.otelRuntime, options.proxyConfig, createTelemetryFactory({
|
|
1085
|
+
projectName: options.projectName,
|
|
1086
|
+
traceKind: options.traceKind,
|
|
1087
|
+
rootRun: options.rootRun,
|
|
1088
|
+
actorRun: options.actorRun,
|
|
1089
|
+
...(options.getActorRun ? { getActorRun: options.getActorRun } : {}),
|
|
1090
|
+
baseMetadata: options.baseMetadata,
|
|
1091
|
+
baseTelemetry: options.otelRuntime.telemetry,
|
|
1092
|
+
...(options.proxyConfig ? { proxyConfig: options.proxyConfig } : {}),
|
|
1093
|
+
}));
|
|
1094
|
+
}
|
|
950
1095
|
async function createInstanceAiTraceContext(options) {
|
|
951
1096
|
if (!isLangSmithTracingEnabled(!!options.proxyConfig)) {
|
|
952
1097
|
return undefined;
|
|
953
1098
|
}
|
|
954
|
-
ensureLangSmithTracingEnv();
|
|
955
|
-
const client = options.proxyConfig ? getOrCreateProxyClient(options.proxyConfig) : undefined;
|
|
956
1099
|
const projectName = options.projectName ?? DEFAULT_PROJECT_NAME;
|
|
957
|
-
const baseMetadata = buildBaseMetadata(options);
|
|
1100
|
+
const baseMetadata = await buildBaseMetadata(options);
|
|
958
1101
|
const createTraceRuns = async () => {
|
|
959
|
-
const
|
|
1102
|
+
const otelRuntime = await createProductOtelRuntime(projectName, options.proxyConfig);
|
|
1103
|
+
const traceContextRef = {};
|
|
1104
|
+
const messageRun = startProductSpan(otelRuntime, {
|
|
960
1105
|
projectName,
|
|
961
|
-
name: '
|
|
1106
|
+
name: 'turn',
|
|
1107
|
+
canonicalName: 'instance-ai.message_turn',
|
|
1108
|
+
runType: 'chain',
|
|
962
1109
|
tags: ['message-turn'],
|
|
963
|
-
metadata: mergeMetadata(baseMetadata, {
|
|
1110
|
+
metadata: mergeMetadata(baseMetadata, {
|
|
1111
|
+
agent_role: 'message_turn',
|
|
1112
|
+
execution_mode: 'foreground',
|
|
1113
|
+
trace_kind: 'message_turn',
|
|
1114
|
+
}),
|
|
964
1115
|
inputs: options.input,
|
|
965
|
-
|
|
1116
|
+
root: true,
|
|
966
1117
|
});
|
|
967
|
-
const
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1118
|
+
const tracing = createProductTraceContext({
|
|
1119
|
+
projectName,
|
|
1120
|
+
traceKind: 'message_turn',
|
|
1121
|
+
rootRun: messageRun,
|
|
1122
|
+
actorRun: messageRun,
|
|
1123
|
+
otelRuntime,
|
|
1124
|
+
baseMetadata,
|
|
1125
|
+
getActorRun: () => traceContextRef.current?.actorRun ?? messageRun,
|
|
1126
|
+
...(options.proxyConfig ? { proxyConfig: options.proxyConfig } : {}),
|
|
972
1127
|
});
|
|
973
|
-
|
|
1128
|
+
traceContextRef.current = tracing;
|
|
1129
|
+
return tracing;
|
|
974
1130
|
};
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1131
|
+
try {
|
|
1132
|
+
return await withProxyHeaders(options.proxyConfig, createTraceRuns);
|
|
1133
|
+
}
|
|
1134
|
+
catch {
|
|
1135
|
+
return undefined;
|
|
978
1136
|
}
|
|
979
|
-
return await createTraceRuns();
|
|
980
1137
|
}
|
|
981
1138
|
async function continueInstanceAiTraceContext(existingContext, options) {
|
|
982
|
-
const
|
|
1139
|
+
const proxyConfig = options.proxyConfig ?? existingContext?.proxyConfig;
|
|
1140
|
+
if (!existingContext && !isLangSmithTracingEnabled(!!proxyConfig)) {
|
|
1141
|
+
return undefined;
|
|
1142
|
+
}
|
|
1143
|
+
if (existingContext?.rootRun.traceId === 'stub' && !isLangSmithTracingEnabled(!!proxyConfig)) {
|
|
1144
|
+
return existingContext;
|
|
1145
|
+
}
|
|
1146
|
+
const baseMetadata = await buildBaseMetadata(options);
|
|
1147
|
+
const projectName = existingContext?.projectName ?? options.projectName ?? DEFAULT_PROJECT_NAME;
|
|
1148
|
+
const continuedMetadata = existingContext && existingContext.rootRun.traceId !== 'stub'
|
|
1149
|
+
? {
|
|
1150
|
+
continued_from_trace_id: existingContext.rootRun.otelTraceId ?? existingContext.rootRun.traceId,
|
|
1151
|
+
continued_from_run_id: existingContext.rootRun.id,
|
|
1152
|
+
resumed_from_trace_id: existingContext.rootRun.otelTraceId ?? existingContext.rootRun.traceId,
|
|
1153
|
+
...(existingContext.actorRun.otelSpanId
|
|
1154
|
+
? { resumed_from_span_id: existingContext.actorRun.otelSpanId }
|
|
1155
|
+
: {}),
|
|
1156
|
+
resumed_from_activation_id: existingContext.actorRun.id,
|
|
1157
|
+
}
|
|
1158
|
+
: {};
|
|
983
1159
|
const createContinuation = async () => {
|
|
984
|
-
const
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1160
|
+
const otelRuntime = await createProductOtelRuntime(projectName, proxyConfig);
|
|
1161
|
+
const rootRun = startProductSpan(otelRuntime, {
|
|
1162
|
+
projectName,
|
|
1163
|
+
name: `resume: ${(0, trace_labels_1.formatResumeReasonLabel)(options.metadata?.resume_reason)}`,
|
|
1164
|
+
canonicalName: 'instance-ai.orchestrator_resume',
|
|
1165
|
+
runType: 'chain',
|
|
1166
|
+
tags: ['orchestrator-resume'],
|
|
1167
|
+
metadata: mergeMetadata(baseMetadata, {
|
|
1168
|
+
agent_role: 'orchestrator_resume',
|
|
1169
|
+
execution_mode: 'resume',
|
|
1170
|
+
trace_kind: 'orchestrator_resume',
|
|
1171
|
+
...continuedMetadata,
|
|
1172
|
+
}),
|
|
988
1173
|
inputs: options.input,
|
|
1174
|
+
root: true,
|
|
1175
|
+
});
|
|
1176
|
+
const orchestratorRun = startProductSpan(otelRuntime, {
|
|
1177
|
+
projectName,
|
|
1178
|
+
name: 'agent: orchestrator',
|
|
1179
|
+
canonicalName: 'instance-ai.agent.orchestrator',
|
|
1180
|
+
runType: 'chain',
|
|
1181
|
+
tags: ['orchestrator', 'resume'],
|
|
1182
|
+
metadata: mergeMetadata(baseMetadata, {
|
|
1183
|
+
agent_role: 'orchestrator',
|
|
1184
|
+
execution_mode: 'resume',
|
|
1185
|
+
trace_kind: 'orchestrator_resume',
|
|
1186
|
+
...continuedMetadata,
|
|
1187
|
+
}),
|
|
1188
|
+
inputs: options.input,
|
|
1189
|
+
parentRun: rootRun,
|
|
1190
|
+
});
|
|
1191
|
+
return createProductTraceContext({
|
|
1192
|
+
projectName,
|
|
1193
|
+
traceKind: 'orchestrator_resume',
|
|
1194
|
+
rootRun,
|
|
1195
|
+
actorRun: orchestratorRun,
|
|
1196
|
+
otelRuntime,
|
|
1197
|
+
baseMetadata,
|
|
1198
|
+
...(proxyConfig ? { proxyConfig } : {}),
|
|
989
1199
|
});
|
|
990
|
-
return createTraceContext(existingContext.projectName, 'message_turn', existingContext.rootRun, orchestratorRun, options.proxyConfig?.getAuthHeaders);
|
|
991
1200
|
};
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1201
|
+
try {
|
|
1202
|
+
return await withProxyHeaders(proxyConfig, createContinuation);
|
|
1203
|
+
}
|
|
1204
|
+
catch {
|
|
1205
|
+
return undefined;
|
|
995
1206
|
}
|
|
996
|
-
return await createContinuation();
|
|
997
1207
|
}
|
|
998
1208
|
async function createDetachedSubAgentTraceContext(options) {
|
|
999
1209
|
if (!isLangSmithTracingEnabled(!!options.proxyConfig)) {
|
|
1000
1210
|
return undefined;
|
|
1001
1211
|
}
|
|
1002
|
-
ensureLangSmithTracingEnv();
|
|
1003
|
-
const client = options.proxyConfig ? getOrCreateProxyClient(options.proxyConfig) : undefined;
|
|
1004
1212
|
const projectName = options.projectName ?? DEFAULT_PROJECT_NAME;
|
|
1005
|
-
const baseMetadata = buildBaseMetadata(options);
|
|
1213
|
+
const baseMetadata = await buildBaseMetadata(options);
|
|
1006
1214
|
const createDetachedRuns = async () => {
|
|
1007
|
-
const
|
|
1215
|
+
const otelRuntime = await createProductOtelRuntime(projectName, options.proxyConfig);
|
|
1216
|
+
const rootMetadata = buildDetachedSubAgentMetadata(options, true);
|
|
1217
|
+
const actorMetadata = buildDetachedSubAgentMetadata(options, false);
|
|
1218
|
+
const rootRun = startProductSpan(otelRuntime, {
|
|
1008
1219
|
projectName,
|
|
1009
|
-
name: `
|
|
1220
|
+
name: `background task: ${(0, trace_labels_1.formatAgentRoleLabel)(options.role)}`,
|
|
1221
|
+
canonicalName: 'instance-ai.background_subagent',
|
|
1222
|
+
runType: 'chain',
|
|
1010
1223
|
tags: normalizeTags(['sub-agent', 'background'], options.plannedTaskId ? ['planned'] : undefined),
|
|
1011
|
-
metadata: mergeMetadata(baseMetadata,
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1224
|
+
metadata: mergeMetadata(baseMetadata, rootMetadata),
|
|
1225
|
+
inputs: options.input,
|
|
1226
|
+
root: true,
|
|
1227
|
+
});
|
|
1228
|
+
const actorRun = startProductSpan(otelRuntime, {
|
|
1229
|
+
projectName,
|
|
1230
|
+
name: `agent: ${(0, trace_labels_1.formatAgentRoleLabel)(options.role)}`,
|
|
1231
|
+
canonicalName: `instance-ai.agent.${options.role}`,
|
|
1232
|
+
runType: 'chain',
|
|
1233
|
+
tags: normalizeTags(['sub-agent', 'background'], options.plannedTaskId ? ['planned'] : undefined),
|
|
1234
|
+
metadata: mergeMetadata(baseMetadata, actorMetadata),
|
|
1022
1235
|
inputs: options.input,
|
|
1023
|
-
|
|
1236
|
+
parentRun: rootRun,
|
|
1237
|
+
});
|
|
1238
|
+
return createProductTraceContext({
|
|
1239
|
+
projectName,
|
|
1240
|
+
traceKind: 'background_subagent',
|
|
1241
|
+
rootRun,
|
|
1242
|
+
actorRun,
|
|
1243
|
+
otelRuntime,
|
|
1244
|
+
baseMetadata: mergeMetadata(baseMetadata, rootMetadata) ?? baseMetadata,
|
|
1245
|
+
...(options.proxyConfig ? { proxyConfig: options.proxyConfig } : {}),
|
|
1024
1246
|
});
|
|
1025
|
-
return createTraceContext(projectName, 'detached_subagent', rootRun, rootRun, options.proxyConfig?.getAuthHeaders);
|
|
1026
1247
|
};
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1248
|
+
try {
|
|
1249
|
+
return await withProxyHeaders(options.proxyConfig, createDetachedRuns);
|
|
1250
|
+
}
|
|
1251
|
+
catch {
|
|
1252
|
+
return undefined;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
async function createInternalOperationTraceContext(options) {
|
|
1256
|
+
if (!isInternalOperationTracingEnabled() || !isLangSmithTracingEnabled(!!options.proxyConfig)) {
|
|
1257
|
+
return undefined;
|
|
1258
|
+
}
|
|
1259
|
+
const projectName = options.projectName ?? DEFAULT_PROJECT_NAME;
|
|
1260
|
+
const baseMetadata = await buildBaseMetadata({
|
|
1261
|
+
...options,
|
|
1262
|
+
messageId: options.messageId ?? `internal:${options.operationName}:${options.runId}`,
|
|
1263
|
+
metadata: mergeMetadata(options.metadata, {
|
|
1264
|
+
operation_name: options.operationName,
|
|
1265
|
+
}),
|
|
1266
|
+
});
|
|
1267
|
+
const createInternalRuns = async () => {
|
|
1268
|
+
const otelRuntime = await createProductOtelRuntime(projectName, options.proxyConfig);
|
|
1269
|
+
const internalMetadata = buildInternalOperationMetadata(options.operationName);
|
|
1270
|
+
const rootRun = startProductSpan(otelRuntime, {
|
|
1271
|
+
projectName,
|
|
1272
|
+
name: `internal: ${(0, trace_labels_1.formatInternalOperationLabel)(options.operationName)}`,
|
|
1273
|
+
canonicalName: `instance-ai.internal.${options.operationName}`,
|
|
1274
|
+
runType: 'chain',
|
|
1275
|
+
tags: ['internal-operation'],
|
|
1276
|
+
metadata: mergeMetadata(baseMetadata, internalMetadata),
|
|
1277
|
+
inputs: options.input,
|
|
1278
|
+
root: true,
|
|
1279
|
+
});
|
|
1280
|
+
return createProductTraceContext({
|
|
1281
|
+
projectName,
|
|
1282
|
+
traceKind: 'internal_operation',
|
|
1283
|
+
rootRun,
|
|
1284
|
+
actorRun: rootRun,
|
|
1285
|
+
otelRuntime,
|
|
1286
|
+
baseMetadata: mergeMetadata(baseMetadata, internalMetadata) ?? baseMetadata,
|
|
1287
|
+
...(options.proxyConfig ? { proxyConfig: options.proxyConfig } : {}),
|
|
1288
|
+
});
|
|
1289
|
+
};
|
|
1290
|
+
try {
|
|
1291
|
+
return await withProxyHeaders(options.proxyConfig, createInternalRuns);
|
|
1292
|
+
}
|
|
1293
|
+
catch {
|
|
1294
|
+
return undefined;
|
|
1030
1295
|
}
|
|
1031
|
-
return await createDetachedRuns();
|
|
1032
1296
|
}
|
|
1033
1297
|
//# sourceMappingURL=langsmith-tracing.js.map
|