@mastra/react 1.7.3-alpha.3 → 1.8.0-alpha.4
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/accumulator-B1MuNX3j.js +1668 -0
- package/dist/accumulator-B1MuNX3j.js.map +1 -0
- package/dist/accumulator-CsUGDUHk.cjs +1709 -0
- package/dist/accumulator-CsUGDUHk.cjs.map +1 -0
- package/dist/agent/hooks.d.ts.map +1 -1
- package/dist/agent/user-message.d.ts +4 -0
- package/dist/agent/user-message.d.ts.map +1 -0
- package/dist/chat/index.cjs +3 -0
- package/dist/chat/index.d.ts +3 -0
- package/dist/chat/index.d.ts.map +1 -0
- package/dist/chat/index.js +2 -0
- package/dist/feedback-refetch-interval-DLHzQoVV.js +12 -0
- package/dist/feedback-refetch-interval-DLHzQoVV.js.map +1 -0
- package/dist/feedback-refetch-interval-XD5FVc2k.cjs +17 -0
- package/dist/feedback-refetch-interval-XD5FVc2k.cjs.map +1 -0
- package/dist/hooks/agents/index.cjs +988 -0
- package/dist/hooks/agents/index.cjs.map +1 -0
- package/dist/hooks/agents/index.d.ts +19 -0
- package/dist/hooks/agents/index.d.ts.map +1 -0
- package/dist/hooks/agents/index.js +952 -0
- package/dist/hooks/agents/index.js.map +1 -0
- package/dist/hooks/agents/is-model-not-allowed.d.ts +15 -0
- package/dist/hooks/agents/is-model-not-allowed.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-messages.d.ts +18 -0
- package/dist/hooks/agents/use-agent-messages.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-plan.d.ts +14 -0
- package/dist/hooks/agents/use-agent-plan.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-skills.d.ts +2021 -0
- package/dist/hooks/agents/use-agent-skills.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-versions.d.ts +72 -0
- package/dist/hooks/agents/use-agent-versions.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-working-memory.d.ts +11 -0
- package/dist/hooks/agents/use-agent-working-memory.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent-workspace.d.ts +2022 -0
- package/dist/hooks/agents/use-agent-workspace.d.ts.map +1 -0
- package/dist/hooks/agents/use-agent.d.ts +12 -0
- package/dist/hooks/agents/use-agent.d.ts.map +1 -0
- package/dist/hooks/agents/use-agents-model-providers.d.ts +9 -0
- package/dist/hooks/agents/use-agents-model-providers.d.ts.map +1 -0
- package/dist/hooks/agents/use-agents.d.ts +28 -0
- package/dist/hooks/agents/use-agents.d.ts.map +1 -0
- package/dist/hooks/agents/use-background-tasks.d.ts +51 -0
- package/dist/hooks/agents/use-background-tasks.d.ts.map +1 -0
- package/dist/hooks/agents/use-browser-session-probe.d.ts +34 -0
- package/dist/hooks/agents/use-browser-session-probe.d.ts.map +1 -0
- package/dist/hooks/agents/use-channels.d.ts +34 -0
- package/dist/hooks/agents/use-channels.d.ts.map +1 -0
- package/dist/hooks/agents/use-close-browser.d.ts +17 -0
- package/dist/hooks/agents/use-close-browser.d.ts.map +1 -0
- package/dist/hooks/agents/use-execute-agent-tool.d.ts +14 -0
- package/dist/hooks/agents/use-execute-agent-tool.d.ts.map +1 -0
- package/dist/hooks/agents/use-preview-instructions.d.ts +20 -0
- package/dist/hooks/agents/use-preview-instructions.d.ts.map +1 -0
- package/dist/hooks/agents/use-stored-agents.d.ts +8023 -0
- package/dist/hooks/agents/use-stored-agents.d.ts.map +1 -0
- package/dist/hooks/agents/use-stored-skills.d.ts +8 -0
- package/dist/hooks/agents/use-stored-skills.d.ts.map +1 -0
- package/dist/hooks/auth/fetch-with-refresh.d.ts +28 -0
- package/dist/hooks/auth/fetch-with-refresh.d.ts.map +1 -0
- package/dist/hooks/auth/get-client-query-key.d.ts +7 -0
- package/dist/hooks/auth/get-client-query-key.d.ts.map +1 -0
- package/dist/hooks/auth/index.cjs +492 -0
- package/dist/hooks/auth/index.cjs.map +1 -0
- package/dist/hooks/auth/index.d.ts +11 -0
- package/dist/hooks/auth/index.d.ts.map +1 -0
- package/dist/hooks/auth/index.js +474 -0
- package/dist/hooks/auth/index.js.map +1 -0
- package/dist/hooks/auth/sso-login.d.ts +8 -0
- package/dist/hooks/auth/sso-login.d.ts.map +1 -0
- package/dist/hooks/auth/types.d.ts +107 -0
- package/dist/hooks/auth/types.d.ts.map +1 -0
- package/dist/hooks/auth/use-auth-actions.d.ts +99 -0
- package/dist/hooks/auth/use-auth-actions.d.ts.map +1 -0
- package/dist/hooks/auth/use-auth-capabilities.d.ts +43 -0
- package/dist/hooks/auth/use-auth-capabilities.d.ts.map +1 -0
- package/dist/hooks/auth/use-credentials-login.d.ts +63 -0
- package/dist/hooks/auth/use-credentials-login.d.ts.map +1 -0
- package/dist/hooks/auth/use-credentials-signup.d.ts +66 -0
- package/dist/hooks/auth/use-credentials-signup.d.ts.map +1 -0
- package/dist/hooks/auth/use-current-user.d.ts +40 -0
- package/dist/hooks/auth/use-current-user.d.ts.map +1 -0
- package/dist/hooks/auth/use-permission-patterns.d.ts +24 -0
- package/dist/hooks/auth/use-permission-patterns.d.ts.map +1 -0
- package/dist/hooks/capabilities/index.cjs +47 -0
- package/dist/hooks/capabilities/index.cjs.map +1 -0
- package/dist/hooks/capabilities/index.d.ts +4 -0
- package/dist/hooks/capabilities/index.d.ts.map +1 -0
- package/dist/hooks/capabilities/index.js +44 -0
- package/dist/hooks/capabilities/index.js.map +1 -0
- package/dist/hooks/capabilities/use-feedback-available.d.ts +9 -0
- package/dist/hooks/capabilities/use-feedback-available.d.ts.map +1 -0
- package/dist/hooks/capabilities/use-observability-capabilities.d.ts +9 -0
- package/dist/hooks/capabilities/use-observability-capabilities.d.ts.map +1 -0
- package/dist/hooks/capabilities/use-trace-query-available.d.ts +9 -0
- package/dist/hooks/capabilities/use-trace-query-available.d.ts.map +1 -0
- package/dist/hooks/configuration/index.cjs +18 -0
- package/dist/hooks/configuration/index.cjs.map +1 -0
- package/dist/hooks/configuration/index.d.ts +2 -0
- package/dist/hooks/configuration/index.d.ts.map +1 -0
- package/dist/hooks/configuration/index.js +17 -0
- package/dist/hooks/configuration/index.js.map +1 -0
- package/dist/hooks/configuration/use-mastra-packages.d.ts +9 -0
- package/dist/hooks/configuration/use-mastra-packages.d.ts.map +1 -0
- package/dist/hooks/datasets/index.cjs +615 -0
- package/dist/hooks/datasets/index.cjs.map +1 -0
- package/dist/hooks/datasets/index.d.ts +10 -0
- package/dist/hooks/datasets/index.d.ts.map +1 -0
- package/dist/hooks/datasets/index.js +600 -0
- package/dist/hooks/datasets/index.js.map +1 -0
- package/dist/hooks/datasets/use-compare-experiments.d.ts +23 -0
- package/dist/hooks/datasets/use-compare-experiments.d.ts.map +1 -0
- package/dist/hooks/datasets/use-dataset-experiments.d.ts +49 -0
- package/dist/hooks/datasets/use-dataset-experiments.d.ts.map +1 -0
- package/dist/hooks/datasets/use-dataset-item-versions.d.ts +31 -0
- package/dist/hooks/datasets/use-dataset-item-versions.d.ts.map +1 -0
- package/dist/hooks/datasets/use-dataset-items.d.ts +38 -0
- package/dist/hooks/datasets/use-dataset-items.d.ts.map +1 -0
- package/dist/hooks/datasets/use-dataset-mutations.d.ts +362 -0
- package/dist/hooks/datasets/use-dataset-mutations.d.ts.map +1 -0
- package/dist/hooks/datasets/use-dataset-versions.d.ts +23 -0
- package/dist/hooks/datasets/use-dataset-versions.d.ts.map +1 -0
- package/dist/hooks/datasets/use-datasets.d.ts +44 -0
- package/dist/hooks/datasets/use-datasets.d.ts.map +1 -0
- package/dist/hooks/datasets/use-experiments.d.ts +16 -0
- package/dist/hooks/datasets/use-experiments.d.ts.map +1 -0
- package/dist/hooks/datasets/use-workflow-schema.d.ts +16 -0
- package/dist/hooks/datasets/use-workflow-schema.d.ts.map +1 -0
- package/dist/hooks/embedders/index.cjs +19 -0
- package/dist/hooks/embedders/index.cjs.map +1 -0
- package/dist/hooks/embedders/index.d.ts +2 -0
- package/dist/hooks/embedders/index.d.ts.map +1 -0
- package/dist/hooks/embedders/index.js +18 -0
- package/dist/hooks/embedders/index.js.map +1 -0
- package/dist/hooks/embedders/use-embedders.d.ts +17 -0
- package/dist/hooks/embedders/use-embedders.d.ts.map +1 -0
- package/dist/hooks/experiments/index.cjs +152 -0
- package/dist/hooks/experiments/index.cjs.map +1 -0
- package/dist/hooks/experiments/index.d.ts +5 -0
- package/dist/hooks/experiments/index.d.ts.map +1 -0
- package/dist/hooks/experiments/index.js +146 -0
- package/dist/hooks/experiments/index.js.map +1 -0
- package/dist/hooks/experiments/use-experiment-metrics.d.ts +30 -0
- package/dist/hooks/experiments/use-experiment-metrics.d.ts.map +1 -0
- package/dist/hooks/experiments/use-experiment-trace.d.ts +13 -0
- package/dist/hooks/experiments/use-experiment-trace.d.ts.map +1 -0
- package/dist/hooks/experiments/use-experiments-for-dataset-filter.d.ts +22 -0
- package/dist/hooks/experiments/use-experiments-for-dataset-filter.d.ts.map +1 -0
- package/dist/hooks/experiments/use-infinite-experiments.d.ts +22 -0
- package/dist/hooks/experiments/use-infinite-experiments.d.ts.map +1 -0
- package/dist/hooks/feedback/feedback-refetch-interval.d.ts +7 -0
- package/dist/hooks/feedback/feedback-refetch-interval.d.ts.map +1 -0
- package/dist/hooks/feedback/index.cjs +22 -0
- package/dist/hooks/feedback/index.cjs.map +1 -0
- package/dist/hooks/feedback/index.d.ts +3 -0
- package/dist/hooks/feedback/index.d.ts.map +1 -0
- package/dist/hooks/feedback/index.js +20 -0
- package/dist/hooks/feedback/index.js.map +1 -0
- package/dist/hooks/feedback/use-feedback.d.ts +56 -0
- package/dist/hooks/feedback/use-feedback.d.ts.map +1 -0
- package/dist/hooks/llm/index.cjs +17 -0
- package/dist/hooks/llm/index.cjs.map +1 -0
- package/dist/hooks/llm/index.d.ts +2 -0
- package/dist/hooks/llm/index.d.ts.map +1 -0
- package/dist/hooks/llm/index.js +16 -0
- package/dist/hooks/llm/index.js.map +1 -0
- package/dist/hooks/llm/use-llm-providers.d.ts +7 -0
- package/dist/hooks/llm/use-llm-providers.d.ts.map +1 -0
- package/dist/hooks/logs/index.cjs +84 -0
- package/dist/hooks/logs/index.cjs.map +1 -0
- package/dist/hooks/logs/index.d.ts +4 -0
- package/dist/hooks/logs/index.d.ts.map +1 -0
- package/dist/hooks/logs/index.js +82 -0
- package/dist/hooks/logs/index.js.map +1 -0
- package/dist/hooks/logs/types.d.ts +36 -0
- package/dist/hooks/logs/types.d.ts.map +1 -0
- package/dist/hooks/logs/use-logs.d.ts +79 -0
- package/dist/hooks/logs/use-logs.d.ts.map +1 -0
- package/dist/hooks/mcps/index.cjs +215 -0
- package/dist/hooks/mcps/index.cjs.map +1 -0
- package/dist/hooks/mcps/index.d.ts +7 -0
- package/dist/hooks/mcps/index.d.ts.map +1 -0
- package/dist/hooks/mcps/index.js +208 -0
- package/dist/hooks/mcps/index.js.map +1 -0
- package/dist/hooks/mcps/use-mcp-app-tools.d.ts +21 -0
- package/dist/hooks/mcps/use-mcp-app-tools.d.ts.map +1 -0
- package/dist/hooks/mcps/use-mcp-server-tool.d.ts +25 -0
- package/dist/hooks/mcps/use-mcp-server-tool.d.ts.map +1 -0
- package/dist/hooks/mcps/use-mcp-server-tools-by-id.d.ts +13 -0
- package/dist/hooks/mcps/use-mcp-server-tools-by-id.d.ts.map +1 -0
- package/dist/hooks/mcps/use-mcp-server-tools.d.ts +9 -0
- package/dist/hooks/mcps/use-mcp-server-tools.d.ts.map +1 -0
- package/dist/hooks/mcps/use-mcp-servers.d.ts +8 -0
- package/dist/hooks/mcps/use-mcp-servers.d.ts.map +1 -0
- package/dist/hooks/mcps/use-try-connect-mcp.d.ts +15 -0
- package/dist/hooks/mcps/use-try-connect-mcp.d.ts.map +1 -0
- package/dist/hooks/memory/index.cjs +328 -0
- package/dist/hooks/memory/index.cjs.map +1 -0
- package/dist/hooks/memory/index.d.ts +5 -0
- package/dist/hooks/memory/index.d.ts.map +1 -0
- package/dist/hooks/memory/index.js +312 -0
- package/dist/hooks/memory/index.js.map +1 -0
- package/dist/hooks/memory/use-memory-status.d.ts +12 -0
- package/dist/hooks/memory/use-memory-status.d.ts.map +1 -0
- package/dist/hooks/memory/use-memory-thread-messages.d.ts +13 -0
- package/dist/hooks/memory/use-memory-thread-messages.d.ts.map +1 -0
- package/dist/hooks/memory/use-memory.d.ts +159 -0
- package/dist/hooks/memory/use-memory.d.ts.map +1 -0
- package/dist/hooks/memory/use-observational-memory.d.ts +13 -0
- package/dist/hooks/memory/use-observational-memory.d.ts.map +1 -0
- package/dist/hooks/metrics/index.cjs +997 -0
- package/dist/hooks/metrics/index.cjs.map +1 -0
- package/dist/hooks/metrics/index.d.ts +16 -0
- package/dist/hooks/metrics/index.d.ts.map +1 -0
- package/dist/hooks/metrics/index.js +981 -0
- package/dist/hooks/metrics/index.js.map +1 -0
- package/dist/hooks/metrics/metrics-interval.d.ts +8 -0
- package/dist/hooks/metrics/metrics-interval.d.ts.map +1 -0
- package/dist/hooks/metrics/metrics-query-filters.d.ts +62 -0
- package/dist/hooks/metrics/metrics-query-filters.d.ts.map +1 -0
- package/dist/hooks/metrics/use-active-resources-kpi-metrics.d.ts +12 -0
- package/dist/hooks/metrics/use-active-resources-kpi-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-active-threads-kpi-metrics.d.ts +12 -0
- package/dist/hooks/metrics/use-active-threads-kpi-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-agent-runs-kpi-metrics.d.ts +9 -0
- package/dist/hooks/metrics/use-agent-runs-kpi-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-latency-metrics.d.ts +23 -0
- package/dist/hooks/metrics/use-latency-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-model-cost-kpi-metrics.d.ts +14 -0
- package/dist/hooks/metrics/use-model-cost-kpi-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-model-usage-cost-metrics.d.ts +16 -0
- package/dist/hooks/metrics/use-model-usage-cost-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-scores-metrics.d.ts +24 -0
- package/dist/hooks/metrics/use-scores-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-token-usage-by-agent-metrics.d.ts +15 -0
- package/dist/hooks/metrics/use-token-usage-by-agent-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-token-usage-timeseries.d.ts +22 -0
- package/dist/hooks/metrics/use-token-usage-timeseries.d.ts.map +1 -0
- package/dist/hooks/metrics/use-top-active-threads-metrics.d.ts +21 -0
- package/dist/hooks/metrics/use-top-active-threads-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-top-resources-by-threads-metrics.d.ts +20 -0
- package/dist/hooks/metrics/use-top-resources-by-threads-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-total-tokens-kpi-metrics.d.ts +13 -0
- package/dist/hooks/metrics/use-total-tokens-kpi-metrics.d.ts.map +1 -0
- package/dist/hooks/metrics/use-trace-volume-metrics.d.ts +17 -0
- package/dist/hooks/metrics/use-trace-volume-metrics.d.ts.map +1 -0
- package/dist/hooks/processors/index.cjs +43 -0
- package/dist/hooks/processors/index.cjs.map +1 -0
- package/dist/hooks/processors/index.d.ts +2 -0
- package/dist/hooks/processors/index.d.ts.map +1 -0
- package/dist/hooks/processors/index.js +40 -0
- package/dist/hooks/processors/index.js.map +1 -0
- package/dist/hooks/processors/use-processors.d.ts +64 -0
- package/dist/hooks/processors/use-processors.d.ts.map +1 -0
- package/dist/hooks/prompt-blocks/index.cjs +181 -0
- package/dist/hooks/prompt-blocks/index.cjs.map +1 -0
- package/dist/hooks/prompt-blocks/index.d.ts +3 -0
- package/dist/hooks/prompt-blocks/index.d.ts.map +1 -0
- package/dist/hooks/prompt-blocks/index.js +172 -0
- package/dist/hooks/prompt-blocks/index.js.map +1 -0
- package/dist/hooks/prompt-blocks/use-prompt-block-versions.d.ts +59 -0
- package/dist/hooks/prompt-blocks/use-prompt-block-versions.d.ts.map +1 -0
- package/dist/hooks/prompt-blocks/use-stored-prompt-blocks.d.ts +180 -0
- package/dist/hooks/prompt-blocks/use-stored-prompt-blocks.d.ts.map +1 -0
- package/dist/hooks/query/index.cjs +34 -0
- package/dist/hooks/query/index.cjs.map +1 -0
- package/dist/hooks/query/index.d.ts +4 -0
- package/dist/hooks/query/index.d.ts.map +1 -0
- package/dist/hooks/query/index.js +25 -0
- package/dist/hooks/query/index.js.map +1 -0
- package/dist/hooks/query/query-client-provider.d.ts +8 -0
- package/dist/hooks/query/query-client-provider.d.ts.map +1 -0
- package/dist/hooks/query/query-utils.d.ts +48 -0
- package/dist/hooks/query/query-utils.d.ts.map +1 -0
- package/dist/hooks/review/index.cjs +89 -0
- package/dist/hooks/review/index.cjs.map +1 -0
- package/dist/hooks/review/index.d.ts +4 -0
- package/dist/hooks/review/index.d.ts.map +1 -0
- package/dist/hooks/review/index.js +86 -0
- package/dist/hooks/review/index.js.map +1 -0
- package/dist/hooks/review/types.d.ts +21 -0
- package/dist/hooks/review/types.d.ts.map +1 -0
- package/dist/hooks/review/use-dataset-review-items.d.ts +19 -0
- package/dist/hooks/review/use-dataset-review-items.d.ts.map +1 -0
- package/dist/hooks/review/use-review-summary.d.ts +9 -0
- package/dist/hooks/review/use-review-summary.d.ts.map +1 -0
- package/dist/hooks/schedules/index.cjs +110 -0
- package/dist/hooks/schedules/index.cjs.map +1 -0
- package/dist/hooks/schedules/index.d.ts +5 -0
- package/dist/hooks/schedules/index.d.ts.map +1 -0
- package/dist/hooks/schedules/index.js +106 -0
- package/dist/hooks/schedules/index.js.map +1 -0
- package/dist/hooks/schedules/use-schedule-triggers.d.ts +18 -0
- package/dist/hooks/schedules/use-schedule-triggers.d.ts.map +1 -0
- package/dist/hooks/schedules/use-schedule.d.ts +11 -0
- package/dist/hooks/schedules/use-schedule.d.ts.map +1 -0
- package/dist/hooks/schedules/use-schedules.d.ts +7 -0
- package/dist/hooks/schedules/use-schedules.d.ts.map +1 -0
- package/dist/hooks/schedules/use-toggle-schedule.d.ts +13 -0
- package/dist/hooks/schedules/use-toggle-schedule.d.ts.map +1 -0
- package/dist/hooks/scores/index.cjs +339 -0
- package/dist/hooks/scores/index.cjs.map +1 -0
- package/dist/hooks/scores/index.d.ts +6 -0
- package/dist/hooks/scores/index.d.ts.map +1 -0
- package/dist/hooks/scores/index.js +323 -0
- package/dist/hooks/scores/index.js.map +1 -0
- package/dist/hooks/scores/use-scorer-versions.d.ts +71 -0
- package/dist/hooks/scores/use-scorer-versions.d.ts.map +1 -0
- package/dist/hooks/scores/use-scorers.d.ts +34 -0
- package/dist/hooks/scores/use-scorers.d.ts.map +1 -0
- package/dist/hooks/scores/use-stored-scorers.d.ts +151 -0
- package/dist/hooks/scores/use-stored-scorers.d.ts.map +1 -0
- package/dist/hooks/scores/use-trace-span-scores.d.ts +20 -0
- package/dist/hooks/scores/use-trace-span-scores.d.ts.map +1 -0
- package/dist/hooks/scores/use-trigger-scorer.d.ts +17 -0
- package/dist/hooks/scores/use-trigger-scorer.d.ts.map +1 -0
- package/dist/hooks/shared/cost.d.ts +7 -0
- package/dist/hooks/shared/cost.d.ts.map +1 -0
- package/dist/hooks/shared/date-format.d.ts +30 -0
- package/dist/hooks/shared/date-format.d.ts.map +1 -0
- package/dist/hooks/shared/map.d.ts +2 -0
- package/dist/hooks/shared/map.d.ts.map +1 -0
- package/dist/hooks/shared/query-options.d.ts +19 -0
- package/dist/hooks/shared/query-options.d.ts.map +1 -0
- package/dist/hooks/shared/use-in-view.d.ts +23 -0
- package/dist/hooks/shared/use-in-view.d.ts.map +1 -0
- package/dist/hooks/templates/index.cjs +464 -0
- package/dist/hooks/templates/index.cjs.map +1 -0
- package/dist/hooks/templates/index.d.ts +2 -0
- package/dist/hooks/templates/index.d.ts.map +1 -0
- package/dist/hooks/templates/index.js +456 -0
- package/dist/hooks/templates/index.js.map +1 -0
- package/dist/hooks/templates/use-templates.d.ts +139 -0
- package/dist/hooks/templates/use-templates.d.ts.map +1 -0
- package/dist/hooks/tool-providers/index.cjs +342 -0
- package/dist/hooks/tool-providers/index.cjs.map +1 -0
- package/dist/hooks/tool-providers/index.d.ts +10 -0
- package/dist/hooks/tool-providers/index.d.ts.map +1 -0
- package/dist/hooks/tool-providers/index.js +332 -0
- package/dist/hooks/tool-providers/index.js.map +1 -0
- package/dist/hooks/tool-providers/use-all-connections.d.ts +41 -0
- package/dist/hooks/tool-providers/use-all-connections.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-all-provider-tools.d.ts +25 -0
- package/dist/hooks/tool-providers/use-all-provider-tools.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-authorize.d.ts +45 -0
- package/dist/hooks/tool-providers/use-authorize.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-disconnect-connection.d.ts +24 -0
- package/dist/hooks/tool-providers/use-disconnect-connection.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-existing-connections.d.ts +33 -0
- package/dist/hooks/tool-providers/use-existing-connections.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-provider-tools.d.ts +17 -0
- package/dist/hooks/tool-providers/use-provider-tools.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-tool-providers.d.ts +23 -0
- package/dist/hooks/tool-providers/use-tool-providers.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-toolkits.d.ts +13 -0
- package/dist/hooks/tool-providers/use-toolkits.d.ts.map +1 -0
- package/dist/hooks/tool-providers/use-update-connection.d.ts +24 -0
- package/dist/hooks/tool-providers/use-update-connection.d.ts.map +1 -0
- package/dist/hooks/tools/index.cjs +49 -0
- package/dist/hooks/tools/index.cjs.map +1 -0
- package/dist/hooks/tools/index.d.ts +3 -0
- package/dist/hooks/tools/index.d.ts.map +1 -0
- package/dist/hooks/tools/index.js +46 -0
- package/dist/hooks/tools/index.js.map +1 -0
- package/dist/hooks/tools/use-all-tools.d.ts +16 -0
- package/dist/hooks/tools/use-all-tools.d.ts.map +1 -0
- package/dist/hooks/tools/use-execute-tool.d.ts +13 -0
- package/dist/hooks/tools/use-execute-tool.d.ts.map +1 -0
- package/dist/hooks/traces/discovery-cache.d.ts +12 -0
- package/dist/hooks/traces/discovery-cache.d.ts.map +1 -0
- package/dist/hooks/traces/index.cjs +1168 -0
- package/dist/hooks/traces/index.cjs.map +1 -0
- package/dist/hooks/traces/index.d.ts +22 -0
- package/dist/hooks/traces/index.d.ts.map +1 -0
- package/dist/hooks/traces/index.js +1129 -0
- package/dist/hooks/traces/index.js.map +1 -0
- package/dist/hooks/traces/types.d.ts +20 -0
- package/dist/hooks/traces/types.d.ts.map +1 -0
- package/dist/hooks/traces/use-branch.d.ts +18 -0
- package/dist/hooks/traces/use-branch.d.ts.map +1 -0
- package/dist/hooks/traces/use-create-feedback.d.ts +21 -0
- package/dist/hooks/traces/use-create-feedback.d.ts.map +1 -0
- package/dist/hooks/traces/use-delete-feedback.d.ts +21 -0
- package/dist/hooks/traces/use-delete-feedback.d.ts.map +1 -0
- package/dist/hooks/traces/use-entity-names.d.ts +14 -0
- package/dist/hooks/traces/use-entity-names.d.ts.map +1 -0
- package/dist/hooks/traces/use-environments.d.ts +10 -0
- package/dist/hooks/traces/use-environments.d.ts.map +1 -0
- package/dist/hooks/traces/use-service-names.d.ts +10 -0
- package/dist/hooks/traces/use-service-names.d.ts.map +1 -0
- package/dist/hooks/traces/use-span-detail.d.ts +14 -0
- package/dist/hooks/traces/use-span-detail.d.ts.map +1 -0
- package/dist/hooks/traces/use-span-feedback.d.ts +19 -0
- package/dist/hooks/traces/use-span-feedback.d.ts.map +1 -0
- package/dist/hooks/traces/use-tags.d.ts +10 -0
- package/dist/hooks/traces/use-tags.d.ts.map +1 -0
- package/dist/hooks/traces/use-thread-has-other-traces.d.ts +17 -0
- package/dist/hooks/traces/use-thread-has-other-traces.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-column-preferences-storage-key.d.ts +6 -0
- package/dist/hooks/traces/use-trace-column-preferences-storage-key.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-feedback.d.ts +16 -0
- package/dist/hooks/traces/use-trace-feedback.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-light-spans.d.ts +16 -0
- package/dist/hooks/traces/use-trace-light-spans.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-metadata-filter-fields.d.ts +36 -0
- package/dist/hooks/traces/use-trace-metadata-filter-fields.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-or-branch-spans.d.ts +39 -0
- package/dist/hooks/traces/use-trace-or-branch-spans.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-query.d.ts +57 -0
- package/dist/hooks/traces/use-trace-query.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-spans.d.ts +46 -0
- package/dist/hooks/traces/use-trace-spans.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-usage.d.ts +10 -0
- package/dist/hooks/traces/use-trace-usage.d.ts.map +1 -0
- package/dist/hooks/traces/use-traces.d.ts +292 -0
- package/dist/hooks/traces/use-traces.d.ts.map +1 -0
- package/dist/hooks/vectors/index.cjs +19 -0
- package/dist/hooks/vectors/index.cjs.map +1 -0
- package/dist/hooks/vectors/index.d.ts +2 -0
- package/dist/hooks/vectors/index.d.ts.map +1 -0
- package/dist/hooks/vectors/index.js +18 -0
- package/dist/hooks/vectors/index.js.map +1 -0
- package/dist/hooks/vectors/use-vectors.d.ts +14 -0
- package/dist/hooks/vectors/use-vectors.d.ts.map +1 -0
- package/dist/hooks/voice/index.cjs +87 -0
- package/dist/hooks/voice/index.cjs.map +1 -0
- package/dist/hooks/voice/index.d.ts +2 -0
- package/dist/hooks/voice/index.d.ts.map +1 -0
- package/dist/hooks/voice/index.js +86 -0
- package/dist/hooks/voice/index.js.map +1 -0
- package/dist/hooks/voice/use-read-aloud.d.ts +20 -0
- package/dist/hooks/voice/use-read-aloud.d.ts.map +1 -0
- package/dist/hooks/workflows/index.cjs +173 -0
- package/dist/hooks/workflows/index.cjs.map +1 -0
- package/dist/hooks/workflows/index.d.ts +7 -0
- package/dist/hooks/workflows/index.d.ts.map +1 -0
- package/dist/hooks/workflows/index.js +157 -0
- package/dist/hooks/workflows/index.js.map +1 -0
- package/dist/hooks/workflows/use-workflow-runs.d.ts +51 -0
- package/dist/hooks/workflows/use-workflow-runs.d.ts.map +1 -0
- package/dist/hooks/workflows/use-workflow.d.ts +14 -0
- package/dist/hooks/workflows/use-workflow.d.ts.map +1 -0
- package/dist/hooks/workflows/use-workflows-run-counts.d.ts +25 -0
- package/dist/hooks/workflows/use-workflows-run-counts.d.ts.map +1 -0
- package/dist/hooks/workflows/use-workflows.d.ts +10 -0
- package/dist/hooks/workflows/use-workflows.d.ts.map +1 -0
- package/dist/hooks/workspace/client-utils.d.ts +5 -0
- package/dist/hooks/workspace/client-utils.d.ts.map +1 -0
- package/dist/hooks/workspace/compatibility.d.ts +22 -0
- package/dist/hooks/workspace/compatibility.d.ts.map +1 -0
- package/dist/hooks/workspace/file-type.d.ts +7 -0
- package/dist/hooks/workspace/file-type.d.ts.map +1 -0
- package/dist/hooks/workspace/index.cjs +624 -0
- package/dist/hooks/workspace/index.cjs.map +1 -0
- package/dist/hooks/workspace/index.d.ts +11 -0
- package/dist/hooks/workspace/index.d.ts.map +1 -0
- package/dist/hooks/workspace/index.js +594 -0
- package/dist/hooks/workspace/index.js.map +1 -0
- package/dist/hooks/workspace/types.d.ts +199 -0
- package/dist/hooks/workspace/types.d.ts.map +1 -0
- package/dist/hooks/workspace/use-skills-sh.d.ts +104 -0
- package/dist/hooks/workspace/use-skills-sh.d.ts.map +1 -0
- package/dist/hooks/workspace/use-stored-workspaces.d.ts +12 -0
- package/dist/hooks/workspace/use-stored-workspaces.d.ts.map +1 -0
- package/dist/hooks/workspace/use-workspace-directory.d.ts +13 -0
- package/dist/hooks/workspace/use-workspace-directory.d.ts.map +1 -0
- package/dist/hooks/workspace/use-workspace-file-content.d.ts +15 -0
- package/dist/hooks/workspace/use-workspace-file-content.d.ts.map +1 -0
- package/dist/hooks/workspace/use-workspace-search.d.ts +20 -0
- package/dist/hooks/workspace/use-workspace-search.d.ts.map +1 -0
- package/dist/hooks/workspace/use-workspace-skills.d.ts +23 -0
- package/dist/hooks/workspace/use-workspace-skills.d.ts.map +1 -0
- package/dist/hooks/workspace/use-workspace.d.ts +56 -0
- package/dist/hooks/workspace/use-workspace.d.ts.map +1 -0
- package/dist/hooks-BFQK_s4g.js +1231 -0
- package/dist/hooks-BFQK_s4g.js.map +1 -0
- package/dist/hooks-CMjizg6y.cjs +1248 -0
- package/dist/hooks-CMjizg6y.cjs.map +1 -0
- package/dist/hooks-CjULNHpJ.js +252 -0
- package/dist/hooks-CjULNHpJ.js.map +1 -0
- package/dist/hooks-DsDYaJ3T.cjs +269 -0
- package/dist/hooks-DsDYaJ3T.cjs.map +1 -0
- package/dist/index.cjs +21 -3434
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3419
- package/dist/index.js.map +1 -1
- package/dist/lib/mastra-db/fromCoreUserMessage.d.ts +3 -4
- package/dist/lib/mastra-db/fromCoreUserMessage.d.ts.map +1 -1
- package/dist/map-CDv4fbu4.js +12 -0
- package/dist/map-CDv4fbu4.js.map +1 -0
- package/dist/map-wjmL1y5O.cjs +17 -0
- package/dist/map-wjmL1y5O.cjs.map +1 -0
- package/dist/mastra-client-context-Bx_g84YB.js +50 -0
- package/dist/mastra-client-context-Bx_g84YB.js.map +1 -0
- package/dist/mastra-client-context-DzX3f0RF.cjs +61 -0
- package/dist/mastra-client-context-DzX3f0RF.cjs.map +1 -0
- package/dist/query-utils-CcrChYgz.cjs +174 -0
- package/dist/query-utils-CcrChYgz.cjs.map +1 -0
- package/dist/query-utils-D1NgFnWU.js +127 -0
- package/dist/query-utils-D1NgFnWU.js.map +1 -0
- package/dist/use-in-view-B6eJO9Zi.cjs +37 -0
- package/dist/use-in-view-B6eJO9Zi.cjs.map +1 -0
- package/dist/use-in-view-C9sYPGRm.js +32 -0
- package/dist/use-in-view-C9sYPGRm.js.map +1 -0
- package/dist/voice-BOtUdMi8.cjs +281 -0
- package/dist/voice-BOtUdMi8.cjs.map +1 -0
- package/dist/voice-DgirjEyH.js +264 -0
- package/dist/voice-DgirjEyH.js.map +1 -0
- package/package.json +32 -3
|
@@ -0,0 +1,1668 @@
|
|
|
1
|
+
//#region src/agent/signal-data.ts
|
|
2
|
+
const uint8ArrayToBase64 = (bytes) => {
|
|
3
|
+
const chunkSize = 32768;
|
|
4
|
+
let binary = "";
|
|
5
|
+
for (let i = 0; i < bytes.length; i += chunkSize) binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
6
|
+
return btoa(binary);
|
|
7
|
+
};
|
|
8
|
+
function convertSignalDataToBase64String(content) {
|
|
9
|
+
if (typeof content === "string") return content;
|
|
10
|
+
const bytes = content instanceof ArrayBuffer ? new Uint8Array(content) : content;
|
|
11
|
+
return uint8ArrayToBase64(bytes);
|
|
12
|
+
}
|
|
13
|
+
/** Canonical DB `file` part `data` for optimistic UI and memory-shaped storage. */
|
|
14
|
+
function encodeFilePartDataForStorage(data, mimeType) {
|
|
15
|
+
if (typeof data === "string") return data;
|
|
16
|
+
if (data instanceof URL) return data.toString();
|
|
17
|
+
const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
18
|
+
const base64 = uint8ArrayToBase64(bytes);
|
|
19
|
+
if (base64.startsWith("data:")) return base64;
|
|
20
|
+
return `data:${mimeType};base64,${base64}`;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/lib/mastra-db/formatCompletionFeedback.ts
|
|
24
|
+
const formatBaseCompletionFeedback = (result, maxIterationReached, formatScorerHeading, incompleteMessage) => {
|
|
25
|
+
const lines = [];
|
|
26
|
+
lines.push("#### Completion Check Results");
|
|
27
|
+
lines.push("");
|
|
28
|
+
lines.push(`Overall: ${result.complete ? "✅ COMPLETE" : "❌ NOT COMPLETE"}`);
|
|
29
|
+
lines.push(`Duration: ${result.totalDuration}ms`);
|
|
30
|
+
if (result.timedOut) lines.push("⚠️ Scoring timed out");
|
|
31
|
+
lines.push("");
|
|
32
|
+
for (const scorer of result.scorers) {
|
|
33
|
+
lines.push(formatScorerHeading(scorer));
|
|
34
|
+
lines.push(`Score: ${scorer.score} ${scorer.passed ? "✅" : "❌"}`);
|
|
35
|
+
if (scorer.reason) lines.push(`Reason: ${scorer.reason}`);
|
|
36
|
+
lines.push("");
|
|
37
|
+
}
|
|
38
|
+
if (result.complete) lines.push("✅ The task is complete.");
|
|
39
|
+
else if (maxIterationReached) lines.push("⚠️ Max iterations reached.");
|
|
40
|
+
else lines.push(incompleteMessage);
|
|
41
|
+
return lines.join("\n");
|
|
42
|
+
};
|
|
43
|
+
const formatCompletionFeedback = (result, maxIterationReached) => {
|
|
44
|
+
return formatBaseCompletionFeedback(result, maxIterationReached, (scorer) => `###### ${scorer.scorerName} (${scorer.scorerId})`, "🔄 Will continue working on the task.");
|
|
45
|
+
};
|
|
46
|
+
const formatStreamCompletionFeedback = (result, maxIterationReached) => {
|
|
47
|
+
return formatBaseCompletionFeedback(result, maxIterationReached, (scorer) => `**${scorer.scorerName}** (${scorer.scorerId})`, "🔄 The task is not yet complete. Please continue working based on the feedback above.");
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/lib/mastra-db/types.ts
|
|
51
|
+
/**
|
|
52
|
+
* Canonical metadata block stored under `MastraDBMessage.content.metadata`.
|
|
53
|
+
*
|
|
54
|
+
* Every UX hint the React accumulator needs to surface lives here. Mode-specific
|
|
55
|
+
* fields are all optional so a single record can carry the union without forcing
|
|
56
|
+
* narrowing on the consumer side.
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Metadata key carrying a client-generated correlation id. The optimistic
|
|
60
|
+
* pending user bubble and the outgoing `sendMessage` request both stamp this
|
|
61
|
+
* id; the server echoes it back on the `data-user-message` data part so the
|
|
62
|
+
* accumulator can reconcile the pending bubble deterministically (decoupled
|
|
63
|
+
* from the server-assigned signal id). This is transient client state and is
|
|
64
|
+
* stripped once the echo is reconciled and on reload.
|
|
65
|
+
*/
|
|
66
|
+
const CLIENT_MESSAGE_ID_KEY = "clientMessageId";
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/lib/mastra-db/accumulator.ts
|
|
69
|
+
function toolErrorText(error) {
|
|
70
|
+
if (error && typeof error === "object") {
|
|
71
|
+
if ("message" in error && typeof error.message === "string") return error.message;
|
|
72
|
+
if ("cause" in error) {
|
|
73
|
+
const cause = error.cause;
|
|
74
|
+
if (cause && typeof cause === "object") {
|
|
75
|
+
if (("id" in cause ? cause.id : "code" in cause ? cause.code : void 0) === "AGENT_AGENT_TOOL_EXECUTION_FAILED" && "message" in cause && typeof cause.message === "string") return cause.message;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return String(error);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Agent-as-tool boundaries wrap progressive chunks in nested `tool-output`
|
|
83
|
+
* envelopes. Peel only agent boundaries; workflow/tool envelopes remain the
|
|
84
|
+
* leaf so existing child-message accumulation can process them normally.
|
|
85
|
+
*/
|
|
86
|
+
const normalizeNestedAgentOutput = (output) => {
|
|
87
|
+
const boundaries = [];
|
|
88
|
+
const seen = /* @__PURE__ */ new Set();
|
|
89
|
+
let current = output;
|
|
90
|
+
while (current !== null && typeof current === "object") {
|
|
91
|
+
if (seen.has(current)) return void 0;
|
|
92
|
+
seen.add(current);
|
|
93
|
+
const chunk = current;
|
|
94
|
+
if (chunk.type !== "tool-output") return {
|
|
95
|
+
leaf: current,
|
|
96
|
+
boundaries
|
|
97
|
+
};
|
|
98
|
+
const toolCallId = chunk.payload?.toolCallId;
|
|
99
|
+
const toolName = chunk.payload?.toolName;
|
|
100
|
+
if (typeof toolName !== "string" || !toolName.startsWith("agent-")) return {
|
|
101
|
+
leaf: current,
|
|
102
|
+
boundaries
|
|
103
|
+
};
|
|
104
|
+
if (typeof toolCallId !== "string" || !("output" in (chunk.payload ?? {}))) return void 0;
|
|
105
|
+
boundaries.push({
|
|
106
|
+
toolCallId,
|
|
107
|
+
toolName
|
|
108
|
+
});
|
|
109
|
+
current = chunk.payload?.output;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const cloneMetadata = (metadata) => metadata ? { ...metadata } : {};
|
|
113
|
+
const withParts = (message, parts) => ({
|
|
114
|
+
...message,
|
|
115
|
+
content: {
|
|
116
|
+
...message.content,
|
|
117
|
+
parts
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
const withMetadata = (message, metadata) => ({
|
|
121
|
+
...message,
|
|
122
|
+
content: {
|
|
123
|
+
...message.content,
|
|
124
|
+
metadata
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
const clearPendingStatus = (message) => {
|
|
128
|
+
const { status: _status, [CLIENT_MESSAGE_ID_KEY]: _clientMessageId, ...rest } = message.content.metadata ?? {};
|
|
129
|
+
return withMetadata(message, rest);
|
|
130
|
+
};
|
|
131
|
+
const clearPendingStatusKeepClientId = (message) => {
|
|
132
|
+
const { status: _status, ...rest } = message.content.metadata ?? {};
|
|
133
|
+
return withMetadata(message, rest);
|
|
134
|
+
};
|
|
135
|
+
const replaceLast = (conversation, message) => [...conversation.slice(0, -1), message];
|
|
136
|
+
const replaceAt = (conversation, index, message) => [
|
|
137
|
+
...conversation.slice(0, index),
|
|
138
|
+
message,
|
|
139
|
+
...conversation.slice(index + 1)
|
|
140
|
+
];
|
|
141
|
+
const newAssistantMessage = (id, parts, metadata) => ({
|
|
142
|
+
id,
|
|
143
|
+
role: "assistant",
|
|
144
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
145
|
+
content: {
|
|
146
|
+
format: 2,
|
|
147
|
+
parts,
|
|
148
|
+
metadata: cloneMetadata(metadata)
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
const appendAssistantMessage = (conversation, id, parts, metadata) => [...conversation, newAssistantMessage(id, parts, metadata)];
|
|
152
|
+
const isToolPart = (part) => part.type === "tool-invocation";
|
|
153
|
+
const partTextId = (part) => part.type === "text" ? part.textId : void 0;
|
|
154
|
+
const partState = (part) => part.state;
|
|
155
|
+
/**
|
|
156
|
+
* Set any streaming text/reasoning parts on the trailing assistant message to
|
|
157
|
+
* `state: 'done'`. Mirrors the previous `finishStreamingAssistantMessage` from
|
|
158
|
+
* the AI-SDK accumulator.
|
|
159
|
+
*/
|
|
160
|
+
const finishStreamingAssistantMessage = (conversation) => {
|
|
161
|
+
const lastMessage = conversation[conversation.length - 1];
|
|
162
|
+
if (!lastMessage || lastMessage.role !== "assistant") return conversation;
|
|
163
|
+
if (lastMessage.content.parts.length === 0) return conversation.slice(0, -1);
|
|
164
|
+
const nextParts = lastMessage.content.parts.map((part) => {
|
|
165
|
+
if ((part.type === "text" || part.type === "reasoning") && partState(part) === "streaming") return {
|
|
166
|
+
...part,
|
|
167
|
+
state: "done"
|
|
168
|
+
};
|
|
169
|
+
return part;
|
|
170
|
+
});
|
|
171
|
+
return replaceLast(conversation, withParts(lastMessage, nextParts));
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* Locate the assistant message + tool part owning `toolCallId`. Prefers the
|
|
175
|
+
* most recent assistant message and walks back up to 10 messages, matching the
|
|
176
|
+
* historical accumulator's lookup behavior.
|
|
177
|
+
*/
|
|
178
|
+
const locateToolPart = (messages, toolCallId, allowMetadataOnlyMatch) => {
|
|
179
|
+
const findIndex = (parts) => parts.findIndex((part) => isToolPart(part) && part.toolInvocation.toolCallId === toolCallId);
|
|
180
|
+
const lastMessage = messages[messages.length - 1];
|
|
181
|
+
if (lastMessage && lastMessage.role === "assistant") {
|
|
182
|
+
const idx = findIndex(lastMessage.content.parts);
|
|
183
|
+
if (idx !== -1) return {
|
|
184
|
+
messageIndex: messages.length - 1,
|
|
185
|
+
toolPartIndex: idx
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
let count = 0;
|
|
189
|
+
const maxMessagesBack = 10;
|
|
190
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
191
|
+
if (count > maxMessagesBack) break;
|
|
192
|
+
const message = messages[i];
|
|
193
|
+
if (message.role !== "assistant") continue;
|
|
194
|
+
const idx = findIndex(message.content.parts);
|
|
195
|
+
if (idx !== -1) return {
|
|
196
|
+
messageIndex: i,
|
|
197
|
+
toolPartIndex: idx
|
|
198
|
+
};
|
|
199
|
+
count++;
|
|
200
|
+
}
|
|
201
|
+
if (!allowMetadataOnlyMatch) return null;
|
|
202
|
+
for (let i = messages.length - 1; i >= 0; i--) if (messages[i].role === "assistant") return {
|
|
203
|
+
messageIndex: i,
|
|
204
|
+
toolPartIndex: -1
|
|
205
|
+
};
|
|
206
|
+
return null;
|
|
207
|
+
};
|
|
208
|
+
/**
|
|
209
|
+
* Merge per-toolCallId background-task bookkeeping onto message metadata.
|
|
210
|
+
*/
|
|
211
|
+
const mergeBgTaskMetadata = (existing, mode, args, otherMetadata) => {
|
|
212
|
+
const base = cloneMetadata(existing);
|
|
213
|
+
const existingBgTasks = base.backgroundTasks ?? {};
|
|
214
|
+
const nextBgTasks = { ...existingBgTasks };
|
|
215
|
+
if (args.perTaskEntry) {
|
|
216
|
+
const { toolCallId, startedAt, completedAt, taskId, suspendedAt } = args.perTaskEntry;
|
|
217
|
+
nextBgTasks[toolCallId] = {
|
|
218
|
+
...existingBgTasks[toolCallId] ?? { taskId },
|
|
219
|
+
taskId,
|
|
220
|
+
...startedAt !== void 0 ? { startedAt } : {},
|
|
221
|
+
...completedAt !== void 0 ? { completedAt } : {},
|
|
222
|
+
...suspendedAt !== void 0 ? { suspendedAt } : {}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const merged = {
|
|
226
|
+
...base,
|
|
227
|
+
...otherMetadata ?? {},
|
|
228
|
+
mode,
|
|
229
|
+
backgroundTasks: nextBgTasks
|
|
230
|
+
};
|
|
231
|
+
if (args.resetRunningCount) merged.runningBackgroundTasksCount = void 0;
|
|
232
|
+
return merged;
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Workflow chunk accumulation. Mirrors
|
|
236
|
+
* `mapWorkflowStreamChunkToWatchResult` from the previous accumulator.
|
|
237
|
+
*/
|
|
238
|
+
const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
|
|
239
|
+
const previous = prev && typeof prev === "object" && prev.steps && typeof prev.steps === "object" ? prev : {
|
|
240
|
+
status: "running",
|
|
241
|
+
input: void 0,
|
|
242
|
+
steps: {}
|
|
243
|
+
};
|
|
244
|
+
if (chunk.type === "workflow-start") return {
|
|
245
|
+
input: previous.input,
|
|
246
|
+
status: "running",
|
|
247
|
+
steps: previous.steps
|
|
248
|
+
};
|
|
249
|
+
if (chunk.type === "workflow-canceled") return {
|
|
250
|
+
...previous,
|
|
251
|
+
status: "canceled"
|
|
252
|
+
};
|
|
253
|
+
if (chunk.type === "workflow-paused") return {
|
|
254
|
+
...previous,
|
|
255
|
+
status: "paused"
|
|
256
|
+
};
|
|
257
|
+
if (chunk.type === "workflow-finish") {
|
|
258
|
+
const finalStatus = chunk.payload.workflowStatus;
|
|
259
|
+
const lastStep = Object.values(previous.steps).pop();
|
|
260
|
+
return {
|
|
261
|
+
...previous,
|
|
262
|
+
status: chunk.payload.workflowStatus,
|
|
263
|
+
...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : finalStatus === "tripwire" && chunk.payload.tripwire ? { tripwire: chunk.payload.tripwire } : {}
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const stepId = chunk.payload?.id;
|
|
267
|
+
if (stepId === void 0) return previous;
|
|
268
|
+
const { stepCallId: _stepCallId, stepName: _stepName, ...newPayload } = chunk.payload;
|
|
269
|
+
const newSteps = {
|
|
270
|
+
...previous.steps,
|
|
271
|
+
[stepId]: {
|
|
272
|
+
...previous.steps[stepId],
|
|
273
|
+
...newPayload
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
if (chunk.type === "workflow-step-start") return {
|
|
277
|
+
...previous,
|
|
278
|
+
steps: newSteps
|
|
279
|
+
};
|
|
280
|
+
if (chunk.type === "workflow-step-suspended") {
|
|
281
|
+
const suspended = Object.entries(newSteps).flatMap(([stepId, stepResult]) => {
|
|
282
|
+
if (stepResult?.status === "suspended") {
|
|
283
|
+
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
284
|
+
return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
|
|
285
|
+
}
|
|
286
|
+
return [];
|
|
287
|
+
});
|
|
288
|
+
return {
|
|
289
|
+
...previous,
|
|
290
|
+
status: "suspended",
|
|
291
|
+
steps: newSteps,
|
|
292
|
+
suspendPayload: chunk.payload.suspendPayload,
|
|
293
|
+
suspended
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
if (chunk.type === "workflow-step-waiting") return {
|
|
297
|
+
...previous,
|
|
298
|
+
status: "waiting",
|
|
299
|
+
steps: newSteps
|
|
300
|
+
};
|
|
301
|
+
if (chunk.type === "workflow-step-progress") return {
|
|
302
|
+
...previous,
|
|
303
|
+
steps: {
|
|
304
|
+
...previous.steps,
|
|
305
|
+
[stepId]: {
|
|
306
|
+
...previous.steps[stepId],
|
|
307
|
+
foreachProgress: {
|
|
308
|
+
completedCount: chunk.payload.completedCount,
|
|
309
|
+
totalCount: chunk.payload.totalCount,
|
|
310
|
+
currentIndex: chunk.payload.currentIndex,
|
|
311
|
+
iterationStatus: chunk.payload.iterationStatus,
|
|
312
|
+
iterationOutput: chunk.payload.iterationOutput
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
if (chunk.type === "workflow-step-result") return {
|
|
318
|
+
...previous,
|
|
319
|
+
steps: newSteps
|
|
320
|
+
};
|
|
321
|
+
return previous;
|
|
322
|
+
};
|
|
323
|
+
const signalContentsToUserMessages = (contents, metadata) => {
|
|
324
|
+
const makeUserMessage = (parts) => ({
|
|
325
|
+
id: `signal-${Date.now()}`,
|
|
326
|
+
role: "user",
|
|
327
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
328
|
+
content: {
|
|
329
|
+
format: 2,
|
|
330
|
+
parts,
|
|
331
|
+
metadata: cloneMetadata(metadata)
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
const toMessagePart = (part) => {
|
|
335
|
+
if (!part || typeof part !== "object") return [];
|
|
336
|
+
const typedPart = part;
|
|
337
|
+
if (typedPart.type === "text" && typeof typedPart.text === "string") return [{
|
|
338
|
+
type: "text",
|
|
339
|
+
text: typedPart.text
|
|
340
|
+
}];
|
|
341
|
+
if (typedPart.type === "image") {
|
|
342
|
+
const image = typedPart.image;
|
|
343
|
+
const mimeType = typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "image/*";
|
|
344
|
+
if (typeof image === "string" || image instanceof URL || image instanceof ArrayBuffer || image instanceof Uint8Array) return [{
|
|
345
|
+
type: "file",
|
|
346
|
+
mimeType,
|
|
347
|
+
data: encodeFilePartDataForStorage(image, mimeType)
|
|
348
|
+
}];
|
|
349
|
+
return [{
|
|
350
|
+
type: "file",
|
|
351
|
+
mimeType,
|
|
352
|
+
data: ""
|
|
353
|
+
}];
|
|
354
|
+
}
|
|
355
|
+
if (typedPart.type === "file") {
|
|
356
|
+
const data = typedPart.data;
|
|
357
|
+
const mimeType = typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "application/octet-stream";
|
|
358
|
+
if (typeof data === "string" || data instanceof URL || data instanceof ArrayBuffer || data instanceof Uint8Array) return [{
|
|
359
|
+
type: "file",
|
|
360
|
+
mimeType,
|
|
361
|
+
data: encodeFilePartDataForStorage(data, mimeType),
|
|
362
|
+
...typeof typedPart.filename === "string" ? { filename: typedPart.filename } : {}
|
|
363
|
+
}];
|
|
364
|
+
return [{
|
|
365
|
+
type: "file",
|
|
366
|
+
mimeType,
|
|
367
|
+
data: typeof typedPart.url === "string" ? typedPart.url : "",
|
|
368
|
+
...typeof typedPart.filename === "string" ? { filename: typedPart.filename } : {}
|
|
369
|
+
}];
|
|
370
|
+
}
|
|
371
|
+
return [];
|
|
372
|
+
};
|
|
373
|
+
if (typeof contents === "string") return [makeUserMessage([{
|
|
374
|
+
type: "text",
|
|
375
|
+
text: contents
|
|
376
|
+
}])];
|
|
377
|
+
if (Array.isArray(contents)) {
|
|
378
|
+
const parts = contents.flatMap(toMessagePart);
|
|
379
|
+
return parts.length ? [makeUserMessage(parts)] : contents.flatMap((content) => signalContentsToUserMessages(content, metadata));
|
|
380
|
+
}
|
|
381
|
+
if (!contents || typeof contents !== "object") return [];
|
|
382
|
+
const message = contents;
|
|
383
|
+
if (message.role && message.role !== "user") return [];
|
|
384
|
+
const content = message.content;
|
|
385
|
+
if (typeof content === "string") return [makeUserMessage([{
|
|
386
|
+
type: "text",
|
|
387
|
+
text: content
|
|
388
|
+
}])];
|
|
389
|
+
if (!Array.isArray(content)) return [];
|
|
390
|
+
const parts = content.flatMap(toMessagePart);
|
|
391
|
+
return parts.length ? [makeUserMessage(parts)] : [];
|
|
392
|
+
};
|
|
393
|
+
const reconcilePendingUserEcho = (message, echoedContents, metadata, options) => {
|
|
394
|
+
const echoedParts = signalContentsToUserMessages(echoedContents, metadata)[0]?.content.parts;
|
|
395
|
+
let reconciled = typeof options.signalId === "string" ? {
|
|
396
|
+
...message,
|
|
397
|
+
id: options.signalId
|
|
398
|
+
} : message;
|
|
399
|
+
if (echoedParts?.length) reconciled = {
|
|
400
|
+
...reconciled,
|
|
401
|
+
content: {
|
|
402
|
+
...reconciled.content,
|
|
403
|
+
parts: echoedParts
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
return options.keepClientMessageId ? clearPendingStatusKeepClientId(reconciled) : clearPendingStatus(reconciled);
|
|
407
|
+
};
|
|
408
|
+
const makeToolInvocationPart = (invocation) => ({
|
|
409
|
+
type: "tool-invocation",
|
|
410
|
+
toolInvocation: invocation
|
|
411
|
+
});
|
|
412
|
+
/**
|
|
413
|
+
* Narrow the chunk to the template-literal passthrough variants from
|
|
414
|
+
* `NetworkChunkType`. Encoded as a type guard so the surrounding `switch`
|
|
415
|
+
* statement can stay exhaustive over the remaining string-literal cases.
|
|
416
|
+
*/
|
|
417
|
+
const isTemplateLiteralPassthrough = (chunk) => chunk.type.startsWith("agent-execution-event-") || chunk.type.startsWith("workflow-execution-event-");
|
|
418
|
+
/**
|
|
419
|
+
* Narrow the chunk to the `data-${string}` family. Used so the trailing
|
|
420
|
+
* exhaustiveness check can prove the switch covers every remaining variant.
|
|
421
|
+
*/
|
|
422
|
+
const isDataChunk = (chunk) => chunk.type.startsWith("data-");
|
|
423
|
+
/**
|
|
424
|
+
* Reduce a single stream chunk into the running `MastraDBMessage[]`
|
|
425
|
+
* conversation. The accumulator owns the entire chunk→DB mapping for the
|
|
426
|
+
* React SDK; consumers downstream (e.g. `toAISdkV5Messages` in the playground)
|
|
427
|
+
* translate the result into AI SDK / assistant-ui shapes as needed.
|
|
428
|
+
*/
|
|
429
|
+
const accumulateChunk = ({ chunk, conversation, metadata }) => {
|
|
430
|
+
const result = [...conversation];
|
|
431
|
+
if (isTemplateLiteralPassthrough(chunk)) return result;
|
|
432
|
+
if (isDataChunk(chunk)) {
|
|
433
|
+
if (chunk.type === "data-user-message" && "data" in chunk && (chunk.data?.type === "user-message" || chunk.data?.type === "user")) {
|
|
434
|
+
const signalId = chunk.data.id;
|
|
435
|
+
const echoedClientMessageId = chunk.data?.metadata?.[CLIENT_MESSAGE_ID_KEY];
|
|
436
|
+
if (typeof echoedClientMessageId === "string" && result.some((message) => message.content.metadata?.status === "pending" && message.content.metadata["clientMessageId"] === echoedClientMessageId)) return finishStreamingAssistantMessage(result.map((message) => message.content.metadata?.status === "pending" && message.content.metadata["clientMessageId"] === echoedClientMessageId ? reconcilePendingUserEcho(message, chunk.data.contents, metadata, {
|
|
437
|
+
signalId: typeof signalId === "string" ? signalId : void 0,
|
|
438
|
+
keepClientMessageId: true
|
|
439
|
+
}) : message));
|
|
440
|
+
if (typeof signalId === "string" && result.some((message) => message.id === signalId)) return finishStreamingAssistantMessage(result.map((message) => message.id === signalId && message.content.metadata?.status === "pending" ? reconcilePendingUserEcho(message, chunk.data.contents, metadata, { keepClientMessageId: false }) : message));
|
|
441
|
+
const userMessages = signalContentsToUserMessages(chunk.data.contents, metadata);
|
|
442
|
+
if (!userMessages.length) return result;
|
|
443
|
+
const conversationWithFinishedAssistant = finishStreamingAssistantMessage(result);
|
|
444
|
+
const messageIdPrefix = typeof signalId === "string" ? signalId : `signal-${chunk.runId}-${Date.now()}`;
|
|
445
|
+
return [...conversationWithFinishedAssistant, ...userMessages.map((message, index) => ({
|
|
446
|
+
...message,
|
|
447
|
+
id: index === 0 ? messageIdPrefix : `${messageIdPrefix}-${index}`
|
|
448
|
+
}))];
|
|
449
|
+
}
|
|
450
|
+
const dataPart = {
|
|
451
|
+
type: chunk.type,
|
|
452
|
+
data: "data" in chunk ? chunk.data : void 0,
|
|
453
|
+
..."id" in chunk && typeof chunk.id === "string" ? { id: chunk.id } : {}
|
|
454
|
+
};
|
|
455
|
+
const lastMessage = result[result.length - 1];
|
|
456
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `data-${chunk.runId}-${Date.now()}`, [dataPart], metadata);
|
|
457
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, dataPart]));
|
|
458
|
+
}
|
|
459
|
+
switch (chunk.type) {
|
|
460
|
+
case "tripwire": {
|
|
461
|
+
const newMessage = newAssistantMessage(`tripwire-${chunk.runId + Date.now()}`, [{
|
|
462
|
+
type: "text",
|
|
463
|
+
text: chunk.payload.reason
|
|
464
|
+
}], {
|
|
465
|
+
...metadata,
|
|
466
|
+
status: "tripwire",
|
|
467
|
+
tripwire: {
|
|
468
|
+
reason: chunk.payload.reason,
|
|
469
|
+
retry: chunk.payload.retry,
|
|
470
|
+
metadata: chunk.payload.metadata,
|
|
471
|
+
processorId: chunk.payload.processorId
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
return [...result, newMessage];
|
|
475
|
+
}
|
|
476
|
+
case "start": {
|
|
477
|
+
const messageId = typeof chunk.payload?.messageId === "string" ? chunk.payload.messageId : void 0;
|
|
478
|
+
if (messageId && result.some((message) => message.id === messageId)) return result;
|
|
479
|
+
return [...result, newAssistantMessage(messageId ?? `start-${chunk.runId + Date.now()}`, [], metadata)];
|
|
480
|
+
}
|
|
481
|
+
case "text-start": {
|
|
482
|
+
const lastMessage = result[result.length - 1];
|
|
483
|
+
const textId = chunk.payload.id || `text-${Date.now()}`;
|
|
484
|
+
const tailPart = lastMessage?.content.parts[lastMessage.content.parts.length - 1];
|
|
485
|
+
if (chunk.payload.id && lastMessage?.role === "assistant" && tailPart?.type === "text" && partTextId(tailPart) === textId) return result;
|
|
486
|
+
const newTextPart = {
|
|
487
|
+
type: "text",
|
|
488
|
+
text: "",
|
|
489
|
+
state: "streaming",
|
|
490
|
+
textId,
|
|
491
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
492
|
+
};
|
|
493
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `start-${chunk.runId}-${Date.now()}`, [newTextPart], metadata);
|
|
494
|
+
if (lastMessage.content.metadata?.completionResult) return appendAssistantMessage(result, `start-${chunk.runId}-${Date.now()}`, [newTextPart], metadata);
|
|
495
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, newTextPart]));
|
|
496
|
+
}
|
|
497
|
+
case "background-task-progress": {
|
|
498
|
+
const lastMessage = result[result.length - 1];
|
|
499
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
500
|
+
return replaceLast(result, withMetadata(lastMessage, {
|
|
501
|
+
mode: metadata.mode,
|
|
502
|
+
...lastMessage.content.metadata,
|
|
503
|
+
runningBackgroundTasksCount: chunk.payload.runningCount
|
|
504
|
+
}));
|
|
505
|
+
}
|
|
506
|
+
case "text-delta": {
|
|
507
|
+
const lastMessage = result[result.length - 1];
|
|
508
|
+
const textId = chunk.payload.id;
|
|
509
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
510
|
+
const newTextPart = {
|
|
511
|
+
type: "text",
|
|
512
|
+
text: chunk.payload.text,
|
|
513
|
+
state: "streaming",
|
|
514
|
+
textId,
|
|
515
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
516
|
+
};
|
|
517
|
+
return appendAssistantMessage(result, `text-${chunk.runId}-${Date.now()}`, [newTextPart], metadata);
|
|
518
|
+
}
|
|
519
|
+
const parts = [...lastMessage.content.parts];
|
|
520
|
+
let textPartIndex = textId ? parts.findLastIndex((part) => part.type === "text" && partTextId(part) === textId) : -1;
|
|
521
|
+
if (textPartIndex === -1) textPartIndex = parts.findLastIndex((part) => part.type === "text" && partState(part) === "streaming");
|
|
522
|
+
if (textPartIndex !== -1 && parts.some((part, index) => index > textPartIndex && part.type !== "text")) textPartIndex = -1;
|
|
523
|
+
if (textPartIndex === -1) {
|
|
524
|
+
const newTextPart = {
|
|
525
|
+
type: "text",
|
|
526
|
+
text: chunk.payload.text,
|
|
527
|
+
state: "streaming",
|
|
528
|
+
textId,
|
|
529
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
530
|
+
};
|
|
531
|
+
parts.push(newTextPart);
|
|
532
|
+
} else {
|
|
533
|
+
const textPart = parts[textPartIndex];
|
|
534
|
+
parts[textPartIndex] = {
|
|
535
|
+
...textPart,
|
|
536
|
+
text: textPart.text + chunk.payload.text,
|
|
537
|
+
state: "streaming"
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
541
|
+
}
|
|
542
|
+
case "text-end": return result;
|
|
543
|
+
case "reasoning-start": {
|
|
544
|
+
const lastMessage = result[result.length - 1];
|
|
545
|
+
const newReasoningPart = {
|
|
546
|
+
type: "reasoning",
|
|
547
|
+
reasoning: "",
|
|
548
|
+
state: "streaming",
|
|
549
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
550
|
+
};
|
|
551
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `reasoning-${chunk.runId + Date.now()}`, [newReasoningPart], metadata);
|
|
552
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, newReasoningPart]));
|
|
553
|
+
}
|
|
554
|
+
case "reasoning-delta": {
|
|
555
|
+
const lastMessage = result[result.length - 1];
|
|
556
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
557
|
+
const newReasoningPart = {
|
|
558
|
+
type: "reasoning",
|
|
559
|
+
reasoning: chunk.payload.text,
|
|
560
|
+
state: "streaming",
|
|
561
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
562
|
+
};
|
|
563
|
+
return appendAssistantMessage(result, `reasoning-${chunk.runId + Date.now()}`, [newReasoningPart], metadata);
|
|
564
|
+
}
|
|
565
|
+
const parts = [...lastMessage.content.parts];
|
|
566
|
+
const lastIndex = parts.length - 1;
|
|
567
|
+
const lastPart = parts[lastIndex];
|
|
568
|
+
if (lastPart?.type === "reasoning") {
|
|
569
|
+
const reasoningPart = lastPart;
|
|
570
|
+
parts[lastIndex] = {
|
|
571
|
+
...reasoningPart,
|
|
572
|
+
reasoning: reasoningPart.reasoning + chunk.payload.text,
|
|
573
|
+
state: "streaming"
|
|
574
|
+
};
|
|
575
|
+
} else {
|
|
576
|
+
const newReasoningPart = {
|
|
577
|
+
type: "reasoning",
|
|
578
|
+
reasoning: chunk.payload.text,
|
|
579
|
+
state: "streaming",
|
|
580
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
581
|
+
};
|
|
582
|
+
parts.push(newReasoningPart);
|
|
583
|
+
}
|
|
584
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
585
|
+
}
|
|
586
|
+
case "reasoning-end": {
|
|
587
|
+
const lastMessage = result[result.length - 1];
|
|
588
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
589
|
+
const parts = [...lastMessage.content.parts];
|
|
590
|
+
const reasoningIndex = parts.findLastIndex((part) => part.type === "reasoning" && partState(part) === "streaming");
|
|
591
|
+
if (reasoningIndex === -1) return result;
|
|
592
|
+
const reasoningPart = parts[reasoningIndex];
|
|
593
|
+
const existingMeta = reasoningPart.providerMetadata;
|
|
594
|
+
const endMeta = chunk.payload.providerMetadata;
|
|
595
|
+
parts[reasoningIndex] = {
|
|
596
|
+
...reasoningPart,
|
|
597
|
+
state: "done",
|
|
598
|
+
...existingMeta || endMeta ? { providerMetadata: {
|
|
599
|
+
...existingMeta ?? {},
|
|
600
|
+
...endMeta ?? {}
|
|
601
|
+
} } : {}
|
|
602
|
+
};
|
|
603
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
604
|
+
}
|
|
605
|
+
case "reasoning-signature": {
|
|
606
|
+
const lastMessage = result[result.length - 1];
|
|
607
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
608
|
+
const parts = [...lastMessage.content.parts];
|
|
609
|
+
const reasoningIndex = parts.findLastIndex((part) => part.type === "reasoning");
|
|
610
|
+
if (reasoningIndex === -1) return result;
|
|
611
|
+
const reasoningPart = parts[reasoningIndex];
|
|
612
|
+
const existingMeta = reasoningPart.providerMetadata;
|
|
613
|
+
const sigMeta = chunk.payload.providerMetadata;
|
|
614
|
+
parts[reasoningIndex] = {
|
|
615
|
+
...reasoningPart,
|
|
616
|
+
...existingMeta || sigMeta ? { providerMetadata: {
|
|
617
|
+
...existingMeta ?? {},
|
|
618
|
+
...sigMeta ?? {}
|
|
619
|
+
} } : {}
|
|
620
|
+
};
|
|
621
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
622
|
+
}
|
|
623
|
+
case "redacted-reasoning": {
|
|
624
|
+
const lastMessage = result[result.length - 1];
|
|
625
|
+
const redactedData = chunk.payload.data;
|
|
626
|
+
const redactedPart = {
|
|
627
|
+
type: "reasoning",
|
|
628
|
+
reasoning: typeof redactedData === "string" ? redactedData : "",
|
|
629
|
+
state: "done",
|
|
630
|
+
redacted: true,
|
|
631
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
632
|
+
};
|
|
633
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `redacted-reasoning-${chunk.runId + Date.now()}`, [redactedPart], metadata);
|
|
634
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, redactedPart]));
|
|
635
|
+
}
|
|
636
|
+
case "tool-call": {
|
|
637
|
+
const invocation = {
|
|
638
|
+
state: "call",
|
|
639
|
+
toolCallId: chunk.payload.toolCallId,
|
|
640
|
+
toolName: chunk.payload.toolName,
|
|
641
|
+
args: chunk.payload.args
|
|
642
|
+
};
|
|
643
|
+
const newPart = {
|
|
644
|
+
...makeToolInvocationPart(invocation),
|
|
645
|
+
title: chunk.payload.title,
|
|
646
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
647
|
+
};
|
|
648
|
+
const existing = locateToolPart(result, chunk.payload.toolCallId, false);
|
|
649
|
+
if (existing && existing.toolPartIndex >= 0) {
|
|
650
|
+
const { messageIndex, toolPartIndex } = existing;
|
|
651
|
+
const targetMessage = result[messageIndex];
|
|
652
|
+
if (targetMessage && targetMessage.role === "assistant") {
|
|
653
|
+
const parts = [...targetMessage.content.parts];
|
|
654
|
+
const prev = parts[toolPartIndex];
|
|
655
|
+
if (isToolPart(prev)) {
|
|
656
|
+
const { argsText: _argsText, ...rest } = prev;
|
|
657
|
+
parts[toolPartIndex] = {
|
|
658
|
+
...rest,
|
|
659
|
+
toolInvocation: {
|
|
660
|
+
...prev.toolInvocation,
|
|
661
|
+
state: "call",
|
|
662
|
+
toolName: chunk.payload.toolName,
|
|
663
|
+
toolCallId: chunk.payload.toolCallId,
|
|
664
|
+
args: chunk.payload.args
|
|
665
|
+
},
|
|
666
|
+
title: chunk.payload.title ?? prev.title,
|
|
667
|
+
providerMetadata: chunk.payload.providerMetadata ?? prev.providerMetadata
|
|
668
|
+
};
|
|
669
|
+
return replaceAt(result, messageIndex, withParts(targetMessage, parts));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const lastMessage = result[result.length - 1];
|
|
674
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `tool-call-${chunk.runId + Date.now()}`, [newPart], metadata);
|
|
675
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, newPart]));
|
|
676
|
+
}
|
|
677
|
+
case "tool-call-input-streaming-start": {
|
|
678
|
+
const lastMessage = result[result.length - 1];
|
|
679
|
+
const invocation = {
|
|
680
|
+
state: "partial-call",
|
|
681
|
+
toolCallId: chunk.payload.toolCallId,
|
|
682
|
+
toolName: chunk.payload.toolName,
|
|
683
|
+
args: {}
|
|
684
|
+
};
|
|
685
|
+
const newPart = {
|
|
686
|
+
...makeToolInvocationPart(invocation),
|
|
687
|
+
title: chunk.payload.title,
|
|
688
|
+
argsText: ""
|
|
689
|
+
};
|
|
690
|
+
if (!lastMessage || lastMessage.role !== "assistant") return appendAssistantMessage(result, `tool-call-streaming-${chunk.runId + Date.now()}`, [newPart], metadata);
|
|
691
|
+
return replaceLast(result, withParts(lastMessage, [...lastMessage.content.parts, newPart]));
|
|
692
|
+
}
|
|
693
|
+
case "tool-call-delta": {
|
|
694
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, false);
|
|
695
|
+
if (!location || location.toolPartIndex < 0) return result;
|
|
696
|
+
const { messageIndex, toolPartIndex } = location;
|
|
697
|
+
const targetMessage = result[messageIndex];
|
|
698
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
699
|
+
const parts = [...targetMessage.content.parts];
|
|
700
|
+
const toolPart = parts[toolPartIndex];
|
|
701
|
+
if (!isToolPart(toolPart)) return result;
|
|
702
|
+
const nextArgsText = (toolPart.argsText ?? "") + (chunk.payload.argsTextDelta ?? "");
|
|
703
|
+
parts[toolPartIndex] = {
|
|
704
|
+
...toolPart,
|
|
705
|
+
argsText: nextArgsText,
|
|
706
|
+
toolInvocation: {
|
|
707
|
+
...toolPart.toolInvocation,
|
|
708
|
+
state: "partial-call"
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
return replaceAt(result, messageIndex, withParts(targetMessage, parts));
|
|
712
|
+
}
|
|
713
|
+
case "tool-call-input-streaming-end": {
|
|
714
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, false);
|
|
715
|
+
if (!location || location.toolPartIndex < 0) return result;
|
|
716
|
+
const { messageIndex, toolPartIndex } = location;
|
|
717
|
+
const targetMessage = result[messageIndex];
|
|
718
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
719
|
+
const parts = [...targetMessage.content.parts];
|
|
720
|
+
const toolPart = parts[toolPartIndex];
|
|
721
|
+
if (!isToolPart(toolPart)) return result;
|
|
722
|
+
let parsedArgs = {};
|
|
723
|
+
const argsText = toolPart.argsText;
|
|
724
|
+
if (typeof argsText === "string" && argsText.length > 0) try {
|
|
725
|
+
const maybe = JSON.parse(argsText);
|
|
726
|
+
if (maybe && typeof maybe === "object" && !Array.isArray(maybe)) parsedArgs = maybe;
|
|
727
|
+
} catch {
|
|
728
|
+
parsedArgs = {};
|
|
729
|
+
}
|
|
730
|
+
parts[toolPartIndex] = {
|
|
731
|
+
...toolPart,
|
|
732
|
+
toolInvocation: {
|
|
733
|
+
...toolPart.toolInvocation,
|
|
734
|
+
state: "call",
|
|
735
|
+
args: parsedArgs
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
return replaceAt(result, messageIndex, withParts(targetMessage, parts));
|
|
739
|
+
}
|
|
740
|
+
case "tool-output-denied": {
|
|
741
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, false);
|
|
742
|
+
if (!location || location.toolPartIndex < 0) return result;
|
|
743
|
+
const { messageIndex, toolPartIndex } = location;
|
|
744
|
+
const targetMessage = result[messageIndex];
|
|
745
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
746
|
+
const parts = [...targetMessage.content.parts];
|
|
747
|
+
const toolPart = parts[toolPartIndex];
|
|
748
|
+
if (!isToolPart(toolPart)) return result;
|
|
749
|
+
parts[toolPartIndex] = {
|
|
750
|
+
...toolPart,
|
|
751
|
+
toolInvocation: {
|
|
752
|
+
...toolPart.toolInvocation,
|
|
753
|
+
state: "output-denied",
|
|
754
|
+
toolName: chunk.payload.toolName,
|
|
755
|
+
args: chunk.payload.args ?? toolPart.toolInvocation.args,
|
|
756
|
+
approval: chunk.payload.approval
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
return replaceAt(result, messageIndex, withParts(targetMessage, parts));
|
|
760
|
+
}
|
|
761
|
+
case "tool-error":
|
|
762
|
+
case "tool-result":
|
|
763
|
+
case "background-task-completed":
|
|
764
|
+
case "background-task-failed": {
|
|
765
|
+
const isBgTaskEvent = chunk.type === "background-task-completed" || chunk.type === "background-task-failed";
|
|
766
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, isBgTaskEvent);
|
|
767
|
+
if (!location) return result;
|
|
768
|
+
const { messageIndex, toolPartIndex } = location;
|
|
769
|
+
const targetMessage = result[messageIndex];
|
|
770
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
771
|
+
const parts = [...targetMessage.content.parts];
|
|
772
|
+
const toolPart = toolPartIndex >= 0 ? parts[toolPartIndex] : void 0;
|
|
773
|
+
let payloadResult;
|
|
774
|
+
let payloadError;
|
|
775
|
+
let payloadIsError = false;
|
|
776
|
+
let payloadProviderMetadata;
|
|
777
|
+
let payloadCompletedAt;
|
|
778
|
+
let payloadTaskId;
|
|
779
|
+
switch (chunk.type) {
|
|
780
|
+
case "tool-result":
|
|
781
|
+
payloadResult = chunk.payload.result;
|
|
782
|
+
payloadIsError = Boolean(chunk.payload.isError);
|
|
783
|
+
payloadProviderMetadata = chunk.payload.providerMetadata;
|
|
784
|
+
break;
|
|
785
|
+
case "tool-error":
|
|
786
|
+
payloadError = chunk.payload.error;
|
|
787
|
+
payloadProviderMetadata = chunk.payload.providerMetadata;
|
|
788
|
+
break;
|
|
789
|
+
case "background-task-completed":
|
|
790
|
+
payloadResult = chunk.payload.result;
|
|
791
|
+
payloadCompletedAt = chunk.payload.completedAt;
|
|
792
|
+
payloadTaskId = chunk.payload.taskId;
|
|
793
|
+
break;
|
|
794
|
+
case "background-task-failed":
|
|
795
|
+
payloadError = chunk.payload.error;
|
|
796
|
+
payloadCompletedAt = chunk.payload.completedAt;
|
|
797
|
+
payloadTaskId = chunk.payload.taskId;
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
if (toolPart && isToolPart(toolPart)) {
|
|
801
|
+
const { toolName, toolCallId, args } = toolPart.toolInvocation;
|
|
802
|
+
const providerMeta = payloadProviderMetadata ?? toolPart.providerMetadata;
|
|
803
|
+
if (chunk.type === "tool-error" || chunk.type === "background-task-failed" || payloadIsError) {
|
|
804
|
+
const errorText = toolErrorText(chunk.type === "tool-error" || chunk.type === "background-task-failed" ? payloadError : payloadResult);
|
|
805
|
+
parts[toolPartIndex] = {
|
|
806
|
+
...toolPart,
|
|
807
|
+
providerMetadata: providerMeta,
|
|
808
|
+
toolInvocation: {
|
|
809
|
+
state: "output-error",
|
|
810
|
+
toolCallId,
|
|
811
|
+
toolName,
|
|
812
|
+
args,
|
|
813
|
+
errorText,
|
|
814
|
+
...chunk.from === "AGENT" ? { result: toolPart.toolInvocation.result } : {}
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
} else {
|
|
818
|
+
const resultObj = payloadResult;
|
|
819
|
+
const existingResult = toolPart.toolInvocation.state === "partial-call" || toolPart.toolInvocation.state === "result" ? toolPart.toolInvocation.result : void 0;
|
|
820
|
+
const existingLooksLikeWorkflow = Boolean(existingResult && typeof existingResult === "object" && "steps" in existingResult);
|
|
821
|
+
const isWorkflow = Boolean(resultObj?.result?.steps) || toolName?.startsWith("workflow-") || existingLooksLikeWorkflow;
|
|
822
|
+
const isAgent = chunk.from === "AGENT";
|
|
823
|
+
let output;
|
|
824
|
+
if (isWorkflow) {
|
|
825
|
+
const accumulated = existingLooksLikeWorkflow && existingResult && typeof existingResult === "object" ? existingResult : void 0;
|
|
826
|
+
const payloadWorkflow = resultObj?.result && typeof resultObj.result === "object" ? resultObj.result : void 0;
|
|
827
|
+
if (accumulated || payloadWorkflow) output = {
|
|
828
|
+
...accumulated ?? {},
|
|
829
|
+
...payloadWorkflow ?? {},
|
|
830
|
+
steps: payloadWorkflow?.steps ?? accumulated?.steps ?? [],
|
|
831
|
+
status: payloadWorkflow?.status ?? accumulated?.status ?? "success",
|
|
832
|
+
output: payloadResult
|
|
833
|
+
};
|
|
834
|
+
else output = payloadResult;
|
|
835
|
+
} else if (isAgent) {
|
|
836
|
+
const existingOutput = toolPart.toolInvocation.state === "partial-call" || toolPart.toolInvocation.state === "result" ? toolPart.toolInvocation.result : void 0;
|
|
837
|
+
const existingChild = existingOutput?.childMessages;
|
|
838
|
+
output = existingOutput ? {
|
|
839
|
+
...payloadResult,
|
|
840
|
+
childMessages: existingChild?.length ? existingChild : resultObj?.childMessages
|
|
841
|
+
} : payloadResult;
|
|
842
|
+
} else output = payloadResult;
|
|
843
|
+
parts[toolPartIndex] = {
|
|
844
|
+
...toolPart,
|
|
845
|
+
providerMetadata: providerMeta,
|
|
846
|
+
toolInvocation: {
|
|
847
|
+
state: "result",
|
|
848
|
+
toolCallId,
|
|
849
|
+
toolName,
|
|
850
|
+
args,
|
|
851
|
+
result: output
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
const nextMetadata = mergeBgTaskMetadata(targetMessage.content.metadata, metadata.mode, {
|
|
857
|
+
resetRunningCount: isBgTaskEvent,
|
|
858
|
+
perTaskEntry: isBgTaskEvent && payloadTaskId ? {
|
|
859
|
+
toolCallId: chunk.payload.toolCallId,
|
|
860
|
+
completedAt: payloadCompletedAt,
|
|
861
|
+
taskId: payloadTaskId
|
|
862
|
+
} : void 0
|
|
863
|
+
});
|
|
864
|
+
const nextMessage = {
|
|
865
|
+
...targetMessage,
|
|
866
|
+
content: {
|
|
867
|
+
...targetMessage.content,
|
|
868
|
+
parts,
|
|
869
|
+
metadata: nextMetadata
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
return replaceAt(result, messageIndex, nextMessage);
|
|
873
|
+
}
|
|
874
|
+
case "background-task-running": {
|
|
875
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, true);
|
|
876
|
+
if (!location) return result;
|
|
877
|
+
const { messageIndex } = location;
|
|
878
|
+
const targetMessage = result[messageIndex];
|
|
879
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
880
|
+
const nextMetadata = mergeBgTaskMetadata(targetMessage.content.metadata, metadata.mode, { perTaskEntry: {
|
|
881
|
+
toolCallId: chunk.payload.toolCallId,
|
|
882
|
+
startedAt: chunk.payload.startedAt,
|
|
883
|
+
taskId: chunk.payload.taskId
|
|
884
|
+
} });
|
|
885
|
+
return replaceAt(result, messageIndex, withMetadata(targetMessage, nextMetadata));
|
|
886
|
+
}
|
|
887
|
+
case "tool-output":
|
|
888
|
+
case "background-task-output": {
|
|
889
|
+
const isBgTaskOutput = chunk.type === "background-task-output";
|
|
890
|
+
const location = locateToolPart(result, chunk.payload.toolCallId, isBgTaskOutput);
|
|
891
|
+
if (!location || location.toolPartIndex < 0) return result;
|
|
892
|
+
const { messageIndex, toolPartIndex } = location;
|
|
893
|
+
const targetMessage = result[messageIndex];
|
|
894
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
895
|
+
const parts = [...targetMessage.content.parts];
|
|
896
|
+
const toolPart = parts[toolPartIndex];
|
|
897
|
+
if (!isToolPart(toolPart)) return result;
|
|
898
|
+
const { toolName, toolCallId, args } = toolPart.toolInvocation;
|
|
899
|
+
const payloadOutput = chunk.type === "background-task-output" ? chunk.payload.payload.payload.output : chunk.payload.output;
|
|
900
|
+
if (payloadOutput?.type?.startsWith("workflow-")) {
|
|
901
|
+
const existingWorkflowState = toolPart.toolInvocation.result;
|
|
902
|
+
const updated = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, payloadOutput);
|
|
903
|
+
parts[toolPartIndex] = {
|
|
904
|
+
...toolPart,
|
|
905
|
+
toolInvocation: {
|
|
906
|
+
state: "partial-call",
|
|
907
|
+
toolCallId,
|
|
908
|
+
toolName,
|
|
909
|
+
args,
|
|
910
|
+
result: updated
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
} else if (toolName.startsWith("agent-") || payloadOutput?.from === "AGENT") {
|
|
914
|
+
const normalized = normalizeNestedAgentOutput(payloadOutput);
|
|
915
|
+
if (!normalized) return result;
|
|
916
|
+
const { leaf, boundaries } = normalized;
|
|
917
|
+
if (leaf.from === "AGENT" || leaf.type === "tool-output" && leaf.payload?.output?.type?.startsWith("workflow-")) return accumulateAgentChunk(leaf, result, metadata, toolCallId, toolName, boundaries);
|
|
918
|
+
return result;
|
|
919
|
+
} else {
|
|
920
|
+
const currentResult = toolPart.toolInvocation.result;
|
|
921
|
+
const existing = Array.isArray(currentResult) ? currentResult : [];
|
|
922
|
+
parts[toolPartIndex] = {
|
|
923
|
+
...toolPart,
|
|
924
|
+
toolInvocation: {
|
|
925
|
+
state: "partial-call",
|
|
926
|
+
toolCallId,
|
|
927
|
+
toolName,
|
|
928
|
+
args,
|
|
929
|
+
result: [...existing, payloadOutput]
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
return replaceAt(result, messageIndex, withParts(targetMessage, parts));
|
|
934
|
+
}
|
|
935
|
+
case "is-task-complete": {
|
|
936
|
+
if (chunk.payload.suppressFeedback) return result;
|
|
937
|
+
const feedback = formatStreamCompletionFeedback({
|
|
938
|
+
complete: chunk.payload.passed,
|
|
939
|
+
scorers: chunk.payload.results,
|
|
940
|
+
totalDuration: chunk.payload.duration,
|
|
941
|
+
timedOut: chunk.payload.timedOut,
|
|
942
|
+
completionReason: chunk.payload.reason
|
|
943
|
+
}, chunk.payload.maxIterationReached);
|
|
944
|
+
const newMessage = newAssistantMessage(`is-task-complete-${chunk.runId + Date.now()}`, [{
|
|
945
|
+
type: "text",
|
|
946
|
+
text: feedback
|
|
947
|
+
}], {
|
|
948
|
+
...metadata,
|
|
949
|
+
completionResult: { passed: chunk.payload.passed }
|
|
950
|
+
});
|
|
951
|
+
return [...result, newMessage];
|
|
952
|
+
}
|
|
953
|
+
case "source": {
|
|
954
|
+
const lastMessage = result[result.length - 1];
|
|
955
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
956
|
+
const parts = [...lastMessage.content.parts];
|
|
957
|
+
if (chunk.payload.sourceType === "url") {
|
|
958
|
+
const sourceUrlPart = {
|
|
959
|
+
type: "source-url",
|
|
960
|
+
sourceId: chunk.payload.id,
|
|
961
|
+
url: chunk.payload.url || "",
|
|
962
|
+
title: chunk.payload.title,
|
|
963
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
964
|
+
};
|
|
965
|
+
parts.push(sourceUrlPart);
|
|
966
|
+
} else if (chunk.payload.sourceType === "document") parts.push({
|
|
967
|
+
type: "source-document",
|
|
968
|
+
sourceId: chunk.payload.id,
|
|
969
|
+
mediaType: chunk.payload.mimeType || "application/octet-stream",
|
|
970
|
+
title: chunk.payload.title,
|
|
971
|
+
filename: chunk.payload.filename,
|
|
972
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
973
|
+
});
|
|
974
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
975
|
+
}
|
|
976
|
+
case "file": {
|
|
977
|
+
const lastMessage = result[result.length - 1];
|
|
978
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
979
|
+
const parts = [...lastMessage.content.parts];
|
|
980
|
+
let data;
|
|
981
|
+
if (typeof chunk.payload.data === "string") data = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
|
|
982
|
+
else {
|
|
983
|
+
const base64 = uint8ArrayToBase64(chunk.payload.data);
|
|
984
|
+
data = `data:${chunk.payload.mimeType};base64,${base64}`;
|
|
985
|
+
}
|
|
986
|
+
parts.push({
|
|
987
|
+
type: "file",
|
|
988
|
+
mimeType: chunk.payload.mimeType,
|
|
989
|
+
data,
|
|
990
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
991
|
+
});
|
|
992
|
+
return replaceLast(result, withParts(lastMessage, parts));
|
|
993
|
+
}
|
|
994
|
+
case "tool-call-approval": {
|
|
995
|
+
const lastMessage = result[result.length - 1];
|
|
996
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
997
|
+
const existingMeta = lastMessage.content.metadata ?? {};
|
|
998
|
+
const lastRequireApproval = existingMeta.mode === "stream" ? existingMeta.requireApprovalMetadata ?? {} : {};
|
|
999
|
+
return replaceLast(result, {
|
|
1000
|
+
...lastMessage,
|
|
1001
|
+
content: {
|
|
1002
|
+
...lastMessage.content,
|
|
1003
|
+
metadata: {
|
|
1004
|
+
...existingMeta,
|
|
1005
|
+
mode: "stream",
|
|
1006
|
+
requireApprovalMetadata: {
|
|
1007
|
+
...lastRequireApproval,
|
|
1008
|
+
[chunk.payload.toolCallId]: {
|
|
1009
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1010
|
+
toolName: chunk.payload.toolName,
|
|
1011
|
+
args: chunk.payload.args
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
case "tool-call-suspended":
|
|
1019
|
+
case "background-task-suspended": {
|
|
1020
|
+
const isBgTaskEvent = chunk.type === "background-task-suspended";
|
|
1021
|
+
let suspToolCallId;
|
|
1022
|
+
let suspToolName;
|
|
1023
|
+
let suspArgs;
|
|
1024
|
+
let suspPayload;
|
|
1025
|
+
let suspSuspendedAt;
|
|
1026
|
+
let suspTaskId;
|
|
1027
|
+
if (chunk.type === "background-task-suspended") {
|
|
1028
|
+
suspToolCallId = chunk.payload.toolCallId;
|
|
1029
|
+
suspToolName = chunk.payload.toolName;
|
|
1030
|
+
suspArgs = chunk.payload.args;
|
|
1031
|
+
suspPayload = chunk.payload.suspendPayload;
|
|
1032
|
+
suspSuspendedAt = chunk.payload.suspendedAt;
|
|
1033
|
+
suspTaskId = chunk.payload.taskId;
|
|
1034
|
+
} else {
|
|
1035
|
+
suspToolCallId = chunk.payload.toolCallId;
|
|
1036
|
+
suspToolName = chunk.payload.toolName;
|
|
1037
|
+
suspArgs = chunk.payload.args;
|
|
1038
|
+
suspPayload = chunk.payload.suspendPayload;
|
|
1039
|
+
}
|
|
1040
|
+
const location = isBgTaskEvent ? locateToolPart(result, suspToolCallId, true) : { messageIndex: result.length - 1 };
|
|
1041
|
+
if (!location) return result;
|
|
1042
|
+
const { messageIndex } = location;
|
|
1043
|
+
const targetMessage = result[messageIndex];
|
|
1044
|
+
if (!targetMessage || targetMessage.role !== "assistant") return result;
|
|
1045
|
+
const existingMeta = targetMessage.content.metadata ?? {};
|
|
1046
|
+
const lastSuspendedTools = existingMeta.mode === "stream" ? existingMeta.suspendedTools ?? {} : {};
|
|
1047
|
+
const nextMetadata = mergeBgTaskMetadata(existingMeta, "stream", {
|
|
1048
|
+
resetRunningCount: isBgTaskEvent,
|
|
1049
|
+
perTaskEntry: isBgTaskEvent && suspTaskId ? {
|
|
1050
|
+
toolCallId: suspToolCallId,
|
|
1051
|
+
suspendedAt: suspSuspendedAt,
|
|
1052
|
+
taskId: suspTaskId
|
|
1053
|
+
} : void 0
|
|
1054
|
+
}, { suspendedTools: {
|
|
1055
|
+
...lastSuspendedTools,
|
|
1056
|
+
[suspToolName]: {
|
|
1057
|
+
toolCallId: suspToolCallId,
|
|
1058
|
+
toolName: suspToolName,
|
|
1059
|
+
args: suspArgs,
|
|
1060
|
+
suspendPayload: suspPayload,
|
|
1061
|
+
runId: chunk.runId
|
|
1062
|
+
}
|
|
1063
|
+
} });
|
|
1064
|
+
return replaceAt(result, messageIndex, withMetadata(targetMessage, nextMetadata));
|
|
1065
|
+
}
|
|
1066
|
+
case "tool-call-resumed": {
|
|
1067
|
+
const resumedToolCallId = chunk.payload.toolCallId;
|
|
1068
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
1069
|
+
const message = result[i];
|
|
1070
|
+
if (!message || message.role !== "assistant") continue;
|
|
1071
|
+
const meta = message.content.metadata;
|
|
1072
|
+
const metadataKeys = chunk.payload.kind === "approval" ? ["pendingToolApprovals", "requireApprovalMetadata"] : ["suspendedTools"];
|
|
1073
|
+
let nextMeta;
|
|
1074
|
+
for (const metadataKey of metadataKeys) {
|
|
1075
|
+
const entries = meta?.[metadataKey];
|
|
1076
|
+
if (!entries) continue;
|
|
1077
|
+
const key = Object.keys(entries).find((k) => (entries[k]?.toolCallId ?? k) === resumedToolCallId);
|
|
1078
|
+
if (!key) continue;
|
|
1079
|
+
const { [key]: _removed, ...remaining } = entries;
|
|
1080
|
+
nextMeta = {
|
|
1081
|
+
...nextMeta ?? meta,
|
|
1082
|
+
[metadataKey]: remaining
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
if (nextMeta) return replaceAt(result, i, withMetadata(message, nextMeta));
|
|
1086
|
+
}
|
|
1087
|
+
return result;
|
|
1088
|
+
}
|
|
1089
|
+
case "finish":
|
|
1090
|
+
case "abort": return finishStreamingAssistantMessage(result);
|
|
1091
|
+
case "error": {
|
|
1092
|
+
const newMessage = newAssistantMessage(`error-${chunk.runId + Date.now()}`, [{
|
|
1093
|
+
type: "text",
|
|
1094
|
+
text: typeof chunk.payload.error === "string" ? chunk.payload.error : JSON.stringify(chunk.payload.error)
|
|
1095
|
+
}], {
|
|
1096
|
+
...metadata,
|
|
1097
|
+
status: "error"
|
|
1098
|
+
});
|
|
1099
|
+
return [...result, newMessage];
|
|
1100
|
+
}
|
|
1101
|
+
case "step-start": {
|
|
1102
|
+
const stepMessageId = typeof chunk.payload?.messageId === "string" ? chunk.payload.messageId : void 0;
|
|
1103
|
+
if (!stepMessageId) return result;
|
|
1104
|
+
const lastMessage = result[result.length - 1];
|
|
1105
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
1106
|
+
if (result.some((message) => message.id === stepMessageId)) return result;
|
|
1107
|
+
if (!lastMessage.content.parts.some((part) => !String(part.type).startsWith("data-"))) return replaceLast(result, {
|
|
1108
|
+
...lastMessage,
|
|
1109
|
+
id: stepMessageId
|
|
1110
|
+
});
|
|
1111
|
+
return appendAssistantMessage(finishStreamingAssistantMessage(result), stepMessageId, [], metadata);
|
|
1112
|
+
}
|
|
1113
|
+
case "step-finish":
|
|
1114
|
+
case "step-output":
|
|
1115
|
+
case "raw":
|
|
1116
|
+
case "watch":
|
|
1117
|
+
case "response-metadata": return result;
|
|
1118
|
+
case "goal": return result;
|
|
1119
|
+
case "object":
|
|
1120
|
+
case "object-result": return result;
|
|
1121
|
+
case "background-task-started":
|
|
1122
|
+
case "background-task-cancelled":
|
|
1123
|
+
case "background-task-resumed": return result;
|
|
1124
|
+
case "workflow-start":
|
|
1125
|
+
case "workflow-finish":
|
|
1126
|
+
case "workflow-canceled":
|
|
1127
|
+
case "workflow-paused":
|
|
1128
|
+
case "workflow-step-start":
|
|
1129
|
+
case "workflow-step-finish":
|
|
1130
|
+
case "workflow-step-suspended":
|
|
1131
|
+
case "workflow-step-waiting":
|
|
1132
|
+
case "workflow-step-output":
|
|
1133
|
+
case "workflow-step-progress":
|
|
1134
|
+
case "workflow-step-result": return result;
|
|
1135
|
+
case "agent-execution-start":
|
|
1136
|
+
case "agent-execution-approval":
|
|
1137
|
+
case "agent-execution-suspended":
|
|
1138
|
+
case "agent-execution-end":
|
|
1139
|
+
case "agent-execution-abort":
|
|
1140
|
+
case "tool-execution-start":
|
|
1141
|
+
case "tool-execution-end":
|
|
1142
|
+
case "tool-execution-approval":
|
|
1143
|
+
case "tool-execution-suspended":
|
|
1144
|
+
case "tool-execution-abort":
|
|
1145
|
+
case "routing-agent-start":
|
|
1146
|
+
case "routing-agent-text-delta":
|
|
1147
|
+
case "routing-agent-text-start":
|
|
1148
|
+
case "routing-agent-end":
|
|
1149
|
+
case "routing-agent-abort":
|
|
1150
|
+
case "workflow-execution-start":
|
|
1151
|
+
case "workflow-execution-end":
|
|
1152
|
+
case "workflow-execution-suspended":
|
|
1153
|
+
case "workflow-execution-abort":
|
|
1154
|
+
case "network-execution-event-step-finish":
|
|
1155
|
+
case "network-execution-event-finish":
|
|
1156
|
+
case "network-validation-start":
|
|
1157
|
+
case "network-validation-end":
|
|
1158
|
+
case "network-object":
|
|
1159
|
+
case "network-object-result": return result;
|
|
1160
|
+
default: return assertExhaustive(chunk, result);
|
|
1161
|
+
}
|
|
1162
|
+
};
|
|
1163
|
+
/**
|
|
1164
|
+
* Compile-time exhaustiveness helper. At runtime, returns the conversation
|
|
1165
|
+
* unchanged so unexpected chunk variants never throw inside the React stream
|
|
1166
|
+
* pump; TypeScript will fail to compile if `ChunkType` ever grows a new branch
|
|
1167
|
+
* that isn't enumerated above.
|
|
1168
|
+
*/
|
|
1169
|
+
const assertExhaustive = (_chunk, fallback) => fallback;
|
|
1170
|
+
const findAgentChildToolIndex = (childMessages, toolCallId, toolName) => {
|
|
1171
|
+
for (let index = childMessages.length - 1; index >= 0; index--) {
|
|
1172
|
+
const message = childMessages[index];
|
|
1173
|
+
if (message?.type !== "tool") continue;
|
|
1174
|
+
if (toolCallId ? message.toolCallId === toolCallId : toolName && message.toolName === toolName) return index;
|
|
1175
|
+
}
|
|
1176
|
+
return -1;
|
|
1177
|
+
};
|
|
1178
|
+
const accumulateAgentChildMessages = (chunk, childMessages) => {
|
|
1179
|
+
if (chunk.type === "text-delta") {
|
|
1180
|
+
const text = chunk.payload.text;
|
|
1181
|
+
const lastChildMessage = childMessages[childMessages.length - 1];
|
|
1182
|
+
const textMessage = {
|
|
1183
|
+
type: "text",
|
|
1184
|
+
content: (lastChildMessage?.type === "text" ? lastChildMessage.content : "") + text
|
|
1185
|
+
};
|
|
1186
|
+
return lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
|
|
1187
|
+
}
|
|
1188
|
+
if (chunk.type === "tool-call") {
|
|
1189
|
+
const existingIndex = findAgentChildToolIndex(childMessages, chunk.payload.toolCallId);
|
|
1190
|
+
const toolMessage = {
|
|
1191
|
+
...existingIndex === -1 ? {} : childMessages[existingIndex],
|
|
1192
|
+
type: "tool",
|
|
1193
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1194
|
+
toolName: chunk.payload.toolName,
|
|
1195
|
+
args: chunk.payload.args
|
|
1196
|
+
};
|
|
1197
|
+
if (existingIndex === -1) return [...childMessages, toolMessage];
|
|
1198
|
+
const nextMessages = [...childMessages];
|
|
1199
|
+
nextMessages[existingIndex] = toolMessage;
|
|
1200
|
+
return nextMessages;
|
|
1201
|
+
}
|
|
1202
|
+
if (chunk.type === "tool-output" && chunk.payload.output?.type?.startsWith("workflow-")) {
|
|
1203
|
+
const toolIndex = findAgentChildToolIndex(childMessages, chunk.payload.toolCallId, chunk.payload.toolName);
|
|
1204
|
+
if (toolIndex === -1) return childMessages;
|
|
1205
|
+
const currentMessage = childMessages[toolIndex];
|
|
1206
|
+
if (currentMessage?.type !== "tool") return childMessages;
|
|
1207
|
+
const existingWorkflowState = currentMessage.toolOutput && typeof currentMessage.toolOutput === "object" ? currentMessage.toolOutput : {};
|
|
1208
|
+
const updated = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, chunk.payload.output);
|
|
1209
|
+
const nextMessages = [...childMessages];
|
|
1210
|
+
nextMessages[toolIndex] = {
|
|
1211
|
+
...currentMessage,
|
|
1212
|
+
toolOutput: {
|
|
1213
|
+
...updated,
|
|
1214
|
+
runId: chunk.payload.output.runId
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
return nextMessages;
|
|
1218
|
+
}
|
|
1219
|
+
if (chunk.type === "tool-result") {
|
|
1220
|
+
const toolIndex = findAgentChildToolIndex(childMessages, chunk.payload.toolCallId, chunk.payload.toolName);
|
|
1221
|
+
if (toolIndex === -1) return childMessages;
|
|
1222
|
+
const currentMessage = childMessages[toolIndex];
|
|
1223
|
+
if (currentMessage?.type !== "tool") return childMessages;
|
|
1224
|
+
const result = chunk.payload.result;
|
|
1225
|
+
const nextMessages = [...childMessages];
|
|
1226
|
+
nextMessages[toolIndex] = {
|
|
1227
|
+
...currentMessage,
|
|
1228
|
+
toolOutput: chunk.payload.toolName?.startsWith("workflow-") ? {
|
|
1229
|
+
...result?.result ?? {},
|
|
1230
|
+
runId: result?.runId
|
|
1231
|
+
} : chunk.payload.result
|
|
1232
|
+
};
|
|
1233
|
+
return nextMessages;
|
|
1234
|
+
}
|
|
1235
|
+
return childMessages;
|
|
1236
|
+
};
|
|
1237
|
+
const routeAgentChunk = (chunk, childMessages, boundaries) => {
|
|
1238
|
+
const [boundary, ...remaining] = boundaries;
|
|
1239
|
+
if (!boundary) return accumulateAgentChildMessages(chunk, childMessages);
|
|
1240
|
+
let toolIndex = findAgentChildToolIndex(childMessages, boundary.toolCallId, boundary.toolName);
|
|
1241
|
+
let nextMessages = childMessages;
|
|
1242
|
+
if (toolIndex === -1) {
|
|
1243
|
+
toolIndex = childMessages.length;
|
|
1244
|
+
nextMessages = [...childMessages, {
|
|
1245
|
+
type: "tool",
|
|
1246
|
+
toolCallId: boundary.toolCallId,
|
|
1247
|
+
toolName: boundary.toolName,
|
|
1248
|
+
args: {}
|
|
1249
|
+
}];
|
|
1250
|
+
}
|
|
1251
|
+
const currentMessage = nextMessages[toolIndex];
|
|
1252
|
+
if (currentMessage?.type !== "tool" || !currentMessage.toolName.startsWith("agent-")) return childMessages;
|
|
1253
|
+
const currentOutput = currentMessage.toolOutput !== null && typeof currentMessage.toolOutput === "object" ? currentMessage.toolOutput : {};
|
|
1254
|
+
const nestedMessages = Array.isArray(currentOutput.childMessages) ? currentOutput.childMessages : [];
|
|
1255
|
+
const updatedNestedMessages = routeAgentChunk(chunk, nestedMessages, remaining);
|
|
1256
|
+
if (updatedNestedMessages === nestedMessages) return childMessages;
|
|
1257
|
+
const updatedMessages = [...nextMessages];
|
|
1258
|
+
updatedMessages[toolIndex] = {
|
|
1259
|
+
...currentMessage,
|
|
1260
|
+
toolOutput: {
|
|
1261
|
+
...currentOutput,
|
|
1262
|
+
childMessages: updatedNestedMessages
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
return updatedMessages;
|
|
1266
|
+
};
|
|
1267
|
+
const accumulateAgentChunk = (chunk, conversation, _metadata, parentToolCallId, parentToolName, boundaries = []) => {
|
|
1268
|
+
if (!parentToolCallId) return conversation;
|
|
1269
|
+
const location = locateToolPart(conversation, parentToolCallId, false);
|
|
1270
|
+
if (!location) return conversation;
|
|
1271
|
+
const targetMessage = conversation[location.messageIndex];
|
|
1272
|
+
if (!targetMessage || targetMessage.role !== "assistant") return conversation;
|
|
1273
|
+
const parts = [...targetMessage.content.parts];
|
|
1274
|
+
const toolPart = parts[location.toolPartIndex];
|
|
1275
|
+
if (!toolPart || !isToolPart(toolPart)) return conversation;
|
|
1276
|
+
if (parentToolName && toolPart.toolInvocation.toolName !== parentToolName) return conversation;
|
|
1277
|
+
const invocationResult = "result" in toolPart.toolInvocation && toolPart.toolInvocation.result !== null ? toolPart.toolInvocation.result : void 0;
|
|
1278
|
+
const existingResult = invocationResult && typeof invocationResult === "object" ? invocationResult : {};
|
|
1279
|
+
const childMessages = Array.isArray(existingResult.childMessages) ? existingResult.childMessages : [];
|
|
1280
|
+
const updatedChildMessages = routeAgentChunk(chunk, childMessages, boundaries);
|
|
1281
|
+
if (updatedChildMessages === childMessages) return conversation;
|
|
1282
|
+
parts[location.toolPartIndex] = {
|
|
1283
|
+
...toolPart,
|
|
1284
|
+
toolInvocation: {
|
|
1285
|
+
...toolPart.toolInvocation,
|
|
1286
|
+
state: "partial-call",
|
|
1287
|
+
result: {
|
|
1288
|
+
...existingResult,
|
|
1289
|
+
childMessages: updatedChildMessages
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
return replaceAt(conversation, location.messageIndex, withParts(targetMessage, parts));
|
|
1294
|
+
};
|
|
1295
|
+
const networkMode = (metadata) => ({
|
|
1296
|
+
...metadata,
|
|
1297
|
+
mode: "network"
|
|
1298
|
+
});
|
|
1299
|
+
const findPartIndex = (parts, predicate) => parts.findIndex(predicate);
|
|
1300
|
+
const isDynamicToolPart = (part) => part.type === "dynamic-tool";
|
|
1301
|
+
const lastAssistant = (conversation) => {
|
|
1302
|
+
const last = conversation[conversation.length - 1];
|
|
1303
|
+
return last && last.role === "assistant" ? last : void 0;
|
|
1304
|
+
};
|
|
1305
|
+
/**
|
|
1306
|
+
* Try to parse the buffered routing-agent text as a JSON object. Returns the
|
|
1307
|
+
* parsed object on success, or `null` while the buffer is still incomplete or
|
|
1308
|
+
* not JSON at all.
|
|
1309
|
+
*/
|
|
1310
|
+
const tryParseRoutingDecision = (buffered) => {
|
|
1311
|
+
const trimmed = buffered.trim();
|
|
1312
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return null;
|
|
1313
|
+
try {
|
|
1314
|
+
const parsed = JSON.parse(trimmed);
|
|
1315
|
+
if (parsed && typeof parsed === "object") return parsed;
|
|
1316
|
+
return null;
|
|
1317
|
+
} catch {
|
|
1318
|
+
return null;
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
/**
|
|
1322
|
+
* Routing-agent text deltas describe the network's routing decision (often a
|
|
1323
|
+
* JSON object such as `{ "isNetwork": true, "agentId": "...", ... }`). The raw
|
|
1324
|
+
* payload is never useful in the rendered thread, so we buffer it in
|
|
1325
|
+
* message-level metadata, promote it to `routingDecision` when it parses as
|
|
1326
|
+
* JSON, and fall back to `routingDecisionText` for non-JSON routing models.
|
|
1327
|
+
* No visible text part is produced.
|
|
1328
|
+
*/
|
|
1329
|
+
const handleRoutingAgentDelta = (chunk, conversation, metadata) => {
|
|
1330
|
+
const delta = chunk.payload?.text ?? "";
|
|
1331
|
+
if (!delta) return conversation;
|
|
1332
|
+
const lastMessage = lastAssistant(conversation);
|
|
1333
|
+
const mergeRoutingMetadata = (existing) => {
|
|
1334
|
+
const buffered = (existing.routingDecisionBuffer ?? "") + delta;
|
|
1335
|
+
const next = {
|
|
1336
|
+
...cloneMetadata(existing),
|
|
1337
|
+
mode: "network"
|
|
1338
|
+
};
|
|
1339
|
+
const parsed = tryParseRoutingDecision(buffered);
|
|
1340
|
+
if (parsed) {
|
|
1341
|
+
next.routingDecision = parsed;
|
|
1342
|
+
delete next.routingDecisionBuffer;
|
|
1343
|
+
delete next.routingDecisionText;
|
|
1344
|
+
} else {
|
|
1345
|
+
next.routingDecisionBuffer = buffered;
|
|
1346
|
+
next.routingDecisionText = buffered;
|
|
1347
|
+
}
|
|
1348
|
+
return next;
|
|
1349
|
+
};
|
|
1350
|
+
if (!lastMessage) {
|
|
1351
|
+
const seed = mergeRoutingMetadata({});
|
|
1352
|
+
return appendAssistantMessage(conversation, `routing-agent-${chunk.payload?.runId ?? "unknown"}-${Date.now()}`, [], {
|
|
1353
|
+
...networkMode(metadata),
|
|
1354
|
+
...seed
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
return replaceLast(conversation, withMetadata(lastMessage, mergeRoutingMetadata(lastMessage.content.metadata ?? {})));
|
|
1358
|
+
};
|
|
1359
|
+
const handleAgentNetworkChunk = (chunk, conversation, metadata) => {
|
|
1360
|
+
if (chunk.type === "agent-execution-start") {
|
|
1361
|
+
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
1362
|
+
const runId = chunk.payload.runId;
|
|
1363
|
+
if (!primitiveId || !runId) return conversation;
|
|
1364
|
+
const toolPart = {
|
|
1365
|
+
type: "dynamic-tool",
|
|
1366
|
+
toolName: primitiveId,
|
|
1367
|
+
toolCallId: runId,
|
|
1368
|
+
state: "input-available",
|
|
1369
|
+
input: chunk.payload.args
|
|
1370
|
+
};
|
|
1371
|
+
return appendAssistantMessage(conversation, `agent-execution-start-${runId}-${Date.now()}`, [toolPart], {
|
|
1372
|
+
...networkMode(metadata),
|
|
1373
|
+
selectionReason: chunk.payload?.args?.selectionReason || "",
|
|
1374
|
+
agentInput: chunk.payload?.args?.task,
|
|
1375
|
+
from: "AGENT"
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
if (chunk.type === "agent-execution-end") {
|
|
1379
|
+
const lastMessage = lastAssistant(conversation);
|
|
1380
|
+
if (!lastMessage) return conversation;
|
|
1381
|
+
const parts = [...lastMessage.content.parts];
|
|
1382
|
+
const toolPartIndex = findPartIndex(parts, (part) => isDynamicToolPart(part));
|
|
1383
|
+
if (toolPartIndex !== -1) {
|
|
1384
|
+
const toolPart = parts[toolPartIndex];
|
|
1385
|
+
const currentOutput = toolPart.output;
|
|
1386
|
+
parts[toolPartIndex] = {
|
|
1387
|
+
type: "dynamic-tool",
|
|
1388
|
+
toolName: toolPart.toolName,
|
|
1389
|
+
toolCallId: toolPart.toolCallId,
|
|
1390
|
+
state: "output-available",
|
|
1391
|
+
input: toolPart.input,
|
|
1392
|
+
output: {
|
|
1393
|
+
...currentOutput,
|
|
1394
|
+
result: currentOutput?.result || chunk.payload?.result || ""
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
}
|
|
1398
|
+
return replaceLast(conversation, withParts(lastMessage, parts));
|
|
1399
|
+
}
|
|
1400
|
+
if (chunk.type.startsWith("agent-execution-event-")) {
|
|
1401
|
+
const lastMessage = lastAssistant(conversation);
|
|
1402
|
+
if (!lastMessage) return conversation;
|
|
1403
|
+
const agentChunk = chunk.payload;
|
|
1404
|
+
const parts = [...lastMessage.content.parts];
|
|
1405
|
+
const toolPartIndex = findPartIndex(parts, (part) => isDynamicToolPart(part));
|
|
1406
|
+
if (toolPartIndex === -1) return conversation;
|
|
1407
|
+
const toolPart = parts[toolPartIndex];
|
|
1408
|
+
if (agentChunk.type === "text-delta") {
|
|
1409
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1410
|
+
const lastChildMessage = childMessages[childMessages.length - 1];
|
|
1411
|
+
const textMessage = {
|
|
1412
|
+
type: "text",
|
|
1413
|
+
content: (lastChildMessage?.content || "") + agentChunk.payload.text
|
|
1414
|
+
};
|
|
1415
|
+
const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
|
|
1416
|
+
parts[toolPartIndex] = {
|
|
1417
|
+
...toolPart,
|
|
1418
|
+
output: { childMessages: nextMessages }
|
|
1419
|
+
};
|
|
1420
|
+
} else if (agentChunk.type === "tool-call") {
|
|
1421
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1422
|
+
parts[toolPartIndex] = {
|
|
1423
|
+
...toolPart,
|
|
1424
|
+
output: {
|
|
1425
|
+
...toolPart?.output,
|
|
1426
|
+
childMessages: [...childMessages, {
|
|
1427
|
+
type: "tool",
|
|
1428
|
+
toolCallId: agentChunk.payload.toolCallId,
|
|
1429
|
+
toolName: agentChunk.payload.toolName,
|
|
1430
|
+
args: agentChunk.payload.args
|
|
1431
|
+
}]
|
|
1432
|
+
}
|
|
1433
|
+
};
|
|
1434
|
+
} else if (agentChunk.type === "tool-output") {
|
|
1435
|
+
if (agentChunk.payload?.output?.type?.startsWith("workflow-")) {
|
|
1436
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1437
|
+
const lastToolIndex = childMessages.length - 1;
|
|
1438
|
+
const currentMessage = childMessages[lastToolIndex];
|
|
1439
|
+
const actualExistingWorkflowState = currentMessage?.toolOutput || {};
|
|
1440
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(actualExistingWorkflowState, agentChunk.payload.output);
|
|
1441
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") parts[toolPartIndex] = {
|
|
1442
|
+
...toolPart,
|
|
1443
|
+
output: {
|
|
1444
|
+
...toolPart?.output,
|
|
1445
|
+
childMessages: [...childMessages.slice(0, -1), {
|
|
1446
|
+
...currentMessage,
|
|
1447
|
+
toolOutput: updatedWorkflowState
|
|
1448
|
+
}]
|
|
1449
|
+
}
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
} else if (agentChunk.type === "tool-result") {
|
|
1453
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1454
|
+
const lastToolIndex = childMessages.length - 1;
|
|
1455
|
+
const isWorkflow = Boolean(agentChunk.payload?.result?.result?.steps);
|
|
1456
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") parts[toolPartIndex] = {
|
|
1457
|
+
...toolPart,
|
|
1458
|
+
output: {
|
|
1459
|
+
...toolPart?.output,
|
|
1460
|
+
childMessages: [...childMessages.slice(0, -1), {
|
|
1461
|
+
...childMessages[lastToolIndex],
|
|
1462
|
+
toolOutput: isWorkflow ? agentChunk.payload.result.result : agentChunk.payload.result
|
|
1463
|
+
}]
|
|
1464
|
+
}
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
return replaceLast(conversation, withParts(lastMessage, parts));
|
|
1468
|
+
}
|
|
1469
|
+
return conversation;
|
|
1470
|
+
};
|
|
1471
|
+
const handleWorkflowNetworkChunk = (chunk, conversation, metadata) => {
|
|
1472
|
+
if (chunk.type === "workflow-execution-start") {
|
|
1473
|
+
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
1474
|
+
const runId = chunk.payload.runId;
|
|
1475
|
+
if (!primitiveId || !runId) return conversation;
|
|
1476
|
+
let agentInput;
|
|
1477
|
+
try {
|
|
1478
|
+
agentInput = JSON.parse(chunk.payload?.args?.prompt);
|
|
1479
|
+
} catch {
|
|
1480
|
+
agentInput = chunk.payload?.args?.prompt;
|
|
1481
|
+
}
|
|
1482
|
+
const toolPart = {
|
|
1483
|
+
type: "dynamic-tool",
|
|
1484
|
+
toolName: primitiveId,
|
|
1485
|
+
toolCallId: runId,
|
|
1486
|
+
state: "input-available",
|
|
1487
|
+
input: chunk.payload.args
|
|
1488
|
+
};
|
|
1489
|
+
return appendAssistantMessage(conversation, `workflow-start-${runId}-${Date.now()}`, [toolPart], {
|
|
1490
|
+
...networkMode(metadata),
|
|
1491
|
+
selectionReason: chunk.payload?.args?.selectionReason || "",
|
|
1492
|
+
from: "WORKFLOW",
|
|
1493
|
+
agentInput
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
if (chunk.type === "workflow-execution-suspended") {
|
|
1497
|
+
const lastMessage = lastAssistant(conversation);
|
|
1498
|
+
if (!lastMessage) return conversation;
|
|
1499
|
+
const existing = lastMessage.content.metadata?.suspendedTools ?? {};
|
|
1500
|
+
return replaceLast(conversation, withMetadata(lastMessage, {
|
|
1501
|
+
...cloneMetadata(lastMessage.content.metadata),
|
|
1502
|
+
mode: "network",
|
|
1503
|
+
suspendedTools: {
|
|
1504
|
+
...existing,
|
|
1505
|
+
[chunk.payload.toolName]: {
|
|
1506
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1507
|
+
toolName: chunk.payload.toolName,
|
|
1508
|
+
args: chunk.payload.args,
|
|
1509
|
+
suspendPayload: chunk.payload.suspendPayload,
|
|
1510
|
+
runId: chunk.payload.runId
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
}));
|
|
1514
|
+
}
|
|
1515
|
+
if (chunk.type.startsWith("workflow-execution-event-")) {
|
|
1516
|
+
const lastMessage = lastAssistant(conversation);
|
|
1517
|
+
if (!lastMessage) return conversation;
|
|
1518
|
+
const parts = [...lastMessage.content.parts];
|
|
1519
|
+
const toolPartIndex = findPartIndex(parts, (part) => isDynamicToolPart(part));
|
|
1520
|
+
if (toolPartIndex === -1) return conversation;
|
|
1521
|
+
const toolPart = parts[toolPartIndex];
|
|
1522
|
+
const existingWorkflowState = toolPart.output || {};
|
|
1523
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, chunk.payload);
|
|
1524
|
+
parts[toolPartIndex] = {
|
|
1525
|
+
...toolPart,
|
|
1526
|
+
output: updatedWorkflowState
|
|
1527
|
+
};
|
|
1528
|
+
return replaceLast(conversation, withParts(lastMessage, parts));
|
|
1529
|
+
}
|
|
1530
|
+
return conversation;
|
|
1531
|
+
};
|
|
1532
|
+
const handleToolNetworkChunk = (chunk, conversation, metadata) => {
|
|
1533
|
+
if (chunk.type === "tool-execution-start") {
|
|
1534
|
+
const argsData = chunk.payload.args;
|
|
1535
|
+
const nestedArgs = argsData.args || {};
|
|
1536
|
+
const lastMessage = lastAssistant(conversation);
|
|
1537
|
+
const toolPart = {
|
|
1538
|
+
type: "dynamic-tool",
|
|
1539
|
+
toolName: argsData.toolName || "unknown",
|
|
1540
|
+
toolCallId: argsData.toolCallId || "unknown",
|
|
1541
|
+
state: "input-available",
|
|
1542
|
+
input: nestedArgs
|
|
1543
|
+
};
|
|
1544
|
+
if (!lastMessage) return appendAssistantMessage(conversation, `tool-start-${chunk.payload.runId}-${Date.now()}`, [toolPart], {
|
|
1545
|
+
...networkMode(metadata),
|
|
1546
|
+
selectionReason: metadata.mode === "network" ? metadata.selectionReason || argsData.selectionReason : "",
|
|
1547
|
+
agentInput: nestedArgs
|
|
1548
|
+
});
|
|
1549
|
+
const parts = [...lastMessage.content.parts, toolPart];
|
|
1550
|
+
return replaceLast(conversation, withParts(lastMessage, parts));
|
|
1551
|
+
}
|
|
1552
|
+
if (chunk.type === "tool-execution-approval") {
|
|
1553
|
+
const lastMessage = lastAssistant(conversation);
|
|
1554
|
+
if (!lastMessage) return conversation;
|
|
1555
|
+
const existing = lastMessage.content.metadata?.requireApprovalMetadata ?? {};
|
|
1556
|
+
return replaceLast(conversation, withMetadata(lastMessage, {
|
|
1557
|
+
...cloneMetadata(lastMessage.content.metadata),
|
|
1558
|
+
mode: "network",
|
|
1559
|
+
requireApprovalMetadata: {
|
|
1560
|
+
...existing,
|
|
1561
|
+
[chunk.payload.toolName]: {
|
|
1562
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1563
|
+
toolName: chunk.payload.toolName,
|
|
1564
|
+
args: chunk.payload.args,
|
|
1565
|
+
runId: chunk.payload.runId
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
}));
|
|
1569
|
+
}
|
|
1570
|
+
if (chunk.type === "tool-execution-suspended") {
|
|
1571
|
+
const lastMessage = lastAssistant(conversation);
|
|
1572
|
+
if (!lastMessage) return conversation;
|
|
1573
|
+
const existing = lastMessage.content.metadata?.suspendedTools ?? {};
|
|
1574
|
+
return replaceLast(conversation, withMetadata(lastMessage, {
|
|
1575
|
+
...cloneMetadata(lastMessage.content.metadata),
|
|
1576
|
+
mode: "network",
|
|
1577
|
+
suspendedTools: {
|
|
1578
|
+
...existing,
|
|
1579
|
+
[chunk.payload.toolName]: {
|
|
1580
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1581
|
+
toolName: chunk.payload.toolName,
|
|
1582
|
+
args: chunk.payload.args,
|
|
1583
|
+
suspendPayload: chunk.payload.suspendPayload,
|
|
1584
|
+
runId: chunk.payload.runId
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}));
|
|
1588
|
+
}
|
|
1589
|
+
if (chunk.type === "tool-execution-end") {
|
|
1590
|
+
const lastMessage = lastAssistant(conversation);
|
|
1591
|
+
if (!lastMessage) return conversation;
|
|
1592
|
+
const parts = [...lastMessage.content.parts];
|
|
1593
|
+
const toolPartIndex = findPartIndex(parts, (part) => isDynamicToolPart(part) && part.toolCallId === chunk.payload.toolCallId);
|
|
1594
|
+
if (toolPartIndex !== -1) {
|
|
1595
|
+
const toolPart = parts[toolPartIndex];
|
|
1596
|
+
const currentOutput = toolPart.output;
|
|
1597
|
+
parts[toolPartIndex] = {
|
|
1598
|
+
type: "dynamic-tool",
|
|
1599
|
+
toolName: toolPart.toolName,
|
|
1600
|
+
toolCallId: toolPart.toolCallId,
|
|
1601
|
+
state: "output-available",
|
|
1602
|
+
input: toolPart.input,
|
|
1603
|
+
output: currentOutput?.result || chunk.payload?.result || ""
|
|
1604
|
+
};
|
|
1605
|
+
}
|
|
1606
|
+
return replaceLast(conversation, withParts(lastMessage, parts));
|
|
1607
|
+
}
|
|
1608
|
+
return conversation;
|
|
1609
|
+
};
|
|
1610
|
+
/**
|
|
1611
|
+
* Reduce a single network-mode chunk into the running `MastraDBMessage[]`
|
|
1612
|
+
* conversation. Ports the historical `AISdkNetworkTransformer` so the playground
|
|
1613
|
+
* keeps rendering network badges (routing text, agent/workflow/tool execution,
|
|
1614
|
+
* suspensions, approvals, completion feedback) until the next major.
|
|
1615
|
+
*/
|
|
1616
|
+
const accumulateNetworkChunk = ({ chunk, conversation, metadata }) => {
|
|
1617
|
+
const newConversation = [...conversation];
|
|
1618
|
+
if (chunk.type === "routing-agent-text-delta") return handleRoutingAgentDelta(chunk, newConversation, metadata);
|
|
1619
|
+
if (chunk.type.startsWith("agent-execution-")) return handleAgentNetworkChunk(chunk, newConversation, metadata);
|
|
1620
|
+
if (chunk.type.startsWith("workflow-execution-")) return handleWorkflowNetworkChunk(chunk, newConversation, metadata);
|
|
1621
|
+
if (chunk.type.startsWith("tool-execution-")) return handleToolNetworkChunk(chunk, newConversation, metadata);
|
|
1622
|
+
if (chunk.type === "network-validation-end") {
|
|
1623
|
+
if (chunk.payload.suppressFeedback) return newConversation;
|
|
1624
|
+
const textPart = {
|
|
1625
|
+
type: "text",
|
|
1626
|
+
text: formatCompletionFeedback({
|
|
1627
|
+
complete: chunk.payload.passed,
|
|
1628
|
+
scorers: chunk.payload.results,
|
|
1629
|
+
totalDuration: chunk.payload.duration,
|
|
1630
|
+
timedOut: chunk.payload.timedOut,
|
|
1631
|
+
completionReason: chunk.payload.reason
|
|
1632
|
+
}, chunk.payload.maxIterationReached)
|
|
1633
|
+
};
|
|
1634
|
+
return appendAssistantMessage(newConversation, `network-validation-end-${chunk.payload.runId}-${Date.now()}`, [textPart], {
|
|
1635
|
+
...networkMode(metadata),
|
|
1636
|
+
completionResult: { passed: chunk.payload.passed }
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
1640
|
+
const lastMessage = lastAssistant(newConversation);
|
|
1641
|
+
if (!lastMessage) return newConversation;
|
|
1642
|
+
const agentChunk = chunk.payload;
|
|
1643
|
+
const parts = [...lastMessage.content.parts];
|
|
1644
|
+
const textPartIndex = findPartIndex(parts, (part) => part.type === "text");
|
|
1645
|
+
if (textPartIndex === -1) {
|
|
1646
|
+
parts.push({
|
|
1647
|
+
type: "text",
|
|
1648
|
+
text: agentChunk.result,
|
|
1649
|
+
state: "done"
|
|
1650
|
+
});
|
|
1651
|
+
return replaceLast(newConversation, withParts(lastMessage, parts));
|
|
1652
|
+
}
|
|
1653
|
+
const textPart = parts[textPartIndex];
|
|
1654
|
+
if (textPart.type === "text") {
|
|
1655
|
+
parts[textPartIndex] = {
|
|
1656
|
+
...textPart,
|
|
1657
|
+
state: "done"
|
|
1658
|
+
};
|
|
1659
|
+
return replaceLast(newConversation, withParts(lastMessage, parts));
|
|
1660
|
+
}
|
|
1661
|
+
return newConversation;
|
|
1662
|
+
}
|
|
1663
|
+
return newConversation;
|
|
1664
|
+
};
|
|
1665
|
+
//#endregion
|
|
1666
|
+
export { CLIENT_MESSAGE_ID_KEY as a, mapWorkflowStreamChunkToWatchResult as i, accumulateNetworkChunk as n, convertSignalDataToBase64String as o, finishStreamingAssistantMessage as r, encodeFilePartDataForStorage as s, accumulateChunk as t };
|
|
1667
|
+
|
|
1668
|
+
//# sourceMappingURL=accumulator-B1MuNX3j.js.map
|