@od-oneapp/ai-platform 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +235 -0
- package/dist/agents-base.d.mts +12 -0
- package/dist/agents-base.d.mts.map +1 -0
- package/dist/agents-base.mjs +20 -0
- package/dist/agents-base.mjs.map +1 -0
- package/dist/agents-control-flow.d.mts +115 -0
- package/dist/agents-control-flow.d.mts.map +1 -0
- package/dist/agents-control-flow.mjs +514 -0
- package/dist/agents-control-flow.mjs.map +1 -0
- package/dist/agents-coordinator.d.mts +11 -0
- package/dist/agents-coordinator.d.mts.map +1 -0
- package/dist/agents-coordinator.mjs +12 -0
- package/dist/agents-coordinator.mjs.map +1 -0
- package/dist/agents-experimental.d.mts +153 -0
- package/dist/agents-experimental.d.mts.map +1 -0
- package/dist/agents-experimental.mjs +580 -0
- package/dist/agents-experimental.mjs.map +1 -0
- package/dist/agents-fallback.d.mts +10 -0
- package/dist/agents-fallback.d.mts.map +1 -0
- package/dist/agents-fallback.mjs +15 -0
- package/dist/agents-fallback.mjs.map +1 -0
- package/dist/agents-multi-swarm.d.mts +9 -0
- package/dist/agents-multi-swarm.d.mts.map +1 -0
- package/dist/agents-multi-swarm.mjs +44 -0
- package/dist/agents-multi-swarm.mjs.map +1 -0
- package/dist/agents-multi.d.mts +10 -0
- package/dist/agents-multi.d.mts.map +1 -0
- package/dist/agents-multi.mjs +44 -0
- package/dist/agents-multi.mjs.map +1 -0
- package/dist/agents-observability.d.mts +161 -0
- package/dist/agents-observability.d.mts.map +1 -0
- package/dist/agents-observability.mjs +550 -0
- package/dist/agents-observability.mjs.map +1 -0
- package/dist/agents-patterns.d.mts +9 -0
- package/dist/agents-patterns.d.mts.map +1 -0
- package/dist/agents-patterns.mjs +18 -0
- package/dist/agents-patterns.mjs.map +1 -0
- package/dist/agents-persistence.d.mts +234 -0
- package/dist/agents-persistence.d.mts.map +1 -0
- package/dist/agents-persistence.mjs +447 -0
- package/dist/agents-persistence.mjs.map +1 -0
- package/dist/agents-triage.d.mts +11 -0
- package/dist/agents-triage.d.mts.map +1 -0
- package/dist/agents-triage.mjs +13 -0
- package/dist/agents-triage.mjs.map +1 -0
- package/dist/agents-workflows.d.mts +9 -0
- package/dist/agents-workflows.d.mts.map +1 -0
- package/dist/agents-workflows.mjs +9 -0
- package/dist/agents-workflows.mjs.map +1 -0
- package/dist/agents.d.mts +30 -0
- package/dist/agents.d.mts.map +1 -0
- package/dist/agents.mjs +50 -0
- package/dist/agents.mjs.map +1 -0
- package/dist/aggregation-8KJF1uzp.d.mts +276 -0
- package/dist/aggregation-8KJF1uzp.d.mts.map +1 -0
- package/dist/aggregation-BDop87kL.mjs +1180 -0
- package/dist/aggregation-BDop87kL.mjs.map +1 -0
- package/dist/ai-runtime-CDzQztTt.mjs +85 -0
- package/dist/ai-runtime-CDzQztTt.mjs.map +1 -0
- package/dist/ai-runtime-DIwOEc6g.d.mts +56 -0
- package/dist/ai-runtime-DIwOEc6g.d.mts.map +1 -0
- package/dist/ai-sdk-error-integration-D0GDqrM0.d.mts +553 -0
- package/dist/ai-sdk-error-integration-D0GDqrM0.d.mts.map +1 -0
- package/dist/approval-queue-BcDDQ4oQ.mjs +104 -0
- package/dist/approval-queue-BcDDQ4oQ.mjs.map +1 -0
- package/dist/approval-queue-CiKiFT9z.d.mts +21 -0
- package/dist/approval-queue-CiKiFT9z.d.mts.map +1 -0
- package/dist/audio-BzvN7r10.d.mts +79 -0
- package/dist/audio-BzvN7r10.d.mts.map +1 -0
- package/dist/audio-vBG_62ME.mjs +226 -0
- package/dist/audio-vBG_62ME.mjs.map +1 -0
- package/dist/audit-logger-Bb2JIcIk.d.mts +12 -0
- package/dist/audit-logger-Bb2JIcIk.d.mts.map +1 -0
- package/dist/audit-logger-CHIP8bRO.mjs +596 -0
- package/dist/audit-logger-CHIP8bRO.mjs.map +1 -0
- package/dist/auto-resume-BpUNbPtp.d.mts +160 -0
- package/dist/auto-resume-BpUNbPtp.d.mts.map +1 -0
- package/dist/auto-resume-BuFRNvAX.mjs +638 -0
- package/dist/auto-resume-BuFRNvAX.mjs.map +1 -0
- package/dist/budget-guard-C83KCH9V.d.mts +52 -0
- package/dist/budget-guard-C83KCH9V.d.mts.map +1 -0
- package/dist/budget-guard-d_b5rq4u.mjs +158 -0
- package/dist/budget-guard-d_b5rq4u.mjs.map +1 -0
- package/dist/budget-guard-gyhJS00s.mjs +234 -0
- package/dist/budget-guard-gyhJS00s.mjs.map +1 -0
- package/dist/buffer-BC8mvXHE.d.mts +98 -0
- package/dist/buffer-BC8mvXHE.d.mts.map +1 -0
- package/dist/buffer-CefJGbRy.mjs +289 -0
- package/dist/buffer-CefJGbRy.mjs.map +1 -0
- package/dist/caching-adapters.d.mts +5 -0
- package/dist/caching-adapters.mjs +3 -0
- package/dist/caching-strategies.d.mts +52 -0
- package/dist/caching-strategies.d.mts.map +1 -0
- package/dist/caching-strategies.mjs +703 -0
- package/dist/caching-strategies.mjs.map +1 -0
- package/dist/caching.d.mts +14 -0
- package/dist/caching.d.mts.map +1 -0
- package/dist/caching.mjs +15 -0
- package/dist/caching.mjs.map +1 -0
- package/dist/catalog.d.mts +19 -0
- package/dist/catalog.d.mts.map +1 -0
- package/dist/catalog.mjs +1114 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/chunk-CkzbjWQW.mjs +20 -0
- package/dist/circuit-breaker-DoKWPORd.mjs +262 -0
- package/dist/circuit-breaker-DoKWPORd.mjs.map +1 -0
- package/dist/circuit-breaker-Mey3E7tW.d.mts +64 -0
- package/dist/circuit-breaker-Mey3E7tW.d.mts.map +1 -0
- package/dist/citation-generator-C-9RpbHq.mjs +103 -0
- package/dist/citation-generator-C-9RpbHq.mjs.map +1 -0
- package/dist/citation-generator-CDSymDs_.d.mts +18 -0
- package/dist/citation-generator-CDSymDs_.d.mts.map +1 -0
- package/dist/client-CpacYDIE.mjs +882 -0
- package/dist/client-CpacYDIE.mjs.map +1 -0
- package/dist/client.d.mts +103 -0
- package/dist/client.d.mts.map +1 -0
- package/dist/client.mjs +470 -0
- package/dist/client.mjs.map +1 -0
- package/dist/compliance-approval-queue-DQGLojAm.mjs +172 -0
- package/dist/compliance-approval-queue-DQGLojAm.mjs.map +1 -0
- package/dist/compliance-approval-queue-IrMxFfSJ.d.mts +99 -0
- package/dist/compliance-approval-queue-IrMxFfSJ.d.mts.map +1 -0
- package/dist/compliance-wrapper-CrOMHhHN.mjs +528 -0
- package/dist/compliance-wrapper-CrOMHhHN.mjs.map +1 -0
- package/dist/conditions-DmQ6Y1Wt.mjs +179 -0
- package/dist/conditions-DmQ6Y1Wt.mjs.map +1 -0
- package/dist/conditions-zDrKfrc3.d.mts +42 -0
- package/dist/conditions-zDrKfrc3.d.mts.map +1 -0
- package/dist/console-BGMxxPZN.mjs +181 -0
- package/dist/console-BGMxxPZN.mjs.map +1 -0
- package/dist/console-DqEqZd4A.d.mts +76 -0
- package/dist/console-DqEqZd4A.d.mts.map +1 -0
- package/dist/controller-BOy3-xbC.mjs +501 -0
- package/dist/controller-BOy3-xbC.mjs.map +1 -0
- package/dist/controller-Y0NGosbJ.d.mts +104 -0
- package/dist/controller-Y0NGosbJ.d.mts.map +1 -0
- package/dist/coordinator-agent-BglqZLwo.d.mts +54 -0
- package/dist/coordinator-agent-BglqZLwo.d.mts.map +1 -0
- package/dist/coordinator-agent-WFWBRL-G.mjs +236 -0
- package/dist/coordinator-agent-WFWBRL-G.mjs.map +1 -0
- package/dist/crypto-8ABhc3TD.mjs +40 -0
- package/dist/crypto-8ABhc3TD.mjs.map +1 -0
- package/dist/environment-CSoJb0SW.mjs +255 -0
- package/dist/environment-CSoJb0SW.mjs.map +1 -0
- package/dist/error-handling-DNVkm6RY.mjs +1334 -0
- package/dist/error-handling-DNVkm6RY.mjs.map +1 -0
- package/dist/errors-CQ8tF4dP.mjs +985 -0
- package/dist/errors-CQ8tF4dP.mjs.map +1 -0
- package/dist/errors-CfYdVeum.d.mts +212 -0
- package/dist/errors-CfYdVeum.d.mts.map +1 -0
- package/dist/errors-Dtn-UeRi.mjs +61 -0
- package/dist/errors-Dtn-UeRi.mjs.map +1 -0
- package/dist/evaluator-Cs84qkr8.mjs +91 -0
- package/dist/evaluator-Cs84qkr8.mjs.map +1 -0
- package/dist/evaluator-optimizer-De67_mJC.mjs +1086 -0
- package/dist/evaluator-optimizer-De67_mJC.mjs.map +1 -0
- package/dist/evaluator-optimizer-pattern-B5939s2Z.mjs +367 -0
- package/dist/evaluator-optimizer-pattern-B5939s2Z.mjs.map +1 -0
- package/dist/evaluator-optimizer-pattern-D1AJrzBD.d.mts +72 -0
- package/dist/evaluator-optimizer-pattern-D1AJrzBD.d.mts.map +1 -0
- package/dist/factory-DP6VSl8C.mjs +307 -0
- package/dist/factory-DP6VSl8C.mjs.map +1 -0
- package/dist/generative-ui-catalog.d.mts +8 -0
- package/dist/generative-ui-catalog.d.mts.map +1 -0
- package/dist/generative-ui-catalog.mjs +679 -0
- package/dist/generative-ui-catalog.mjs.map +1 -0
- package/dist/generative-ui-registry.d.mts +195 -0
- package/dist/generative-ui-registry.d.mts.map +1 -0
- package/dist/generative-ui-registry.mjs +250 -0
- package/dist/generative-ui-registry.mjs.map +1 -0
- package/dist/generative-ui-stream.d.mts +23 -0
- package/dist/generative-ui-stream.d.mts.map +1 -0
- package/dist/generative-ui-stream.mjs +219 -0
- package/dist/generative-ui-stream.mjs.map +1 -0
- package/dist/generative-ui-types.d.mts +123 -0
- package/dist/generative-ui-types.d.mts.map +1 -0
- package/dist/generative-ui-types.mjs +1 -0
- package/dist/generative-ui.d.mts +13 -0
- package/dist/generative-ui.d.mts.map +1 -0
- package/dist/generative-ui.mjs +21 -0
- package/dist/generative-ui.mjs.map +1 -0
- package/dist/governance-audit.d.mts +3 -0
- package/dist/governance-audit.mjs +3 -0
- package/dist/governance-compliance.d.mts +5 -0
- package/dist/governance-compliance.mjs +4 -0
- package/dist/governance-policies.d.mts +4 -0
- package/dist/governance-policies.mjs +4 -0
- package/dist/governance-tenancy.d.mts +3 -0
- package/dist/governance-tenancy.mjs +3 -0
- package/dist/governance.d.mts +88 -0
- package/dist/governance.d.mts.map +1 -0
- package/dist/governance.mjs +432 -0
- package/dist/governance.mjs.map +1 -0
- package/dist/grounding-attribution.d.mts +63 -0
- package/dist/grounding-attribution.d.mts.map +1 -0
- package/dist/grounding-attribution.mjs +259 -0
- package/dist/grounding-attribution.mjs.map +1 -0
- package/dist/grounding-citation.d.mts +2 -0
- package/dist/grounding-citation.mjs +3 -0
- package/dist/grounding-context.d.mts +9 -0
- package/dist/grounding-context.d.mts.map +1 -0
- package/dist/grounding-context.mjs +19 -0
- package/dist/grounding-context.mjs.map +1 -0
- package/dist/grounding-embed.d.mts +102 -0
- package/dist/grounding-embed.d.mts.map +1 -0
- package/dist/grounding-embed.mjs +417 -0
- package/dist/grounding-embed.mjs.map +1 -0
- package/dist/grounding-hallucination.d.mts +44 -0
- package/dist/grounding-hallucination.d.mts.map +1 -0
- package/dist/grounding-hallucination.mjs +115 -0
- package/dist/grounding-hallucination.mjs.map +1 -0
- package/dist/grounding-proof-map.d.mts +9 -0
- package/dist/grounding-proof-map.d.mts.map +1 -0
- package/dist/grounding-proof-map.mjs +26 -0
- package/dist/grounding-proof-map.mjs.map +1 -0
- package/dist/grounding-rag.d.mts +10 -0
- package/dist/grounding-rag.d.mts.map +1 -0
- package/dist/grounding-rag.mjs +27 -0
- package/dist/grounding-rag.mjs.map +1 -0
- package/dist/grounding-verification.d.mts +48 -0
- package/dist/grounding-verification.d.mts.map +1 -0
- package/dist/grounding-verification.mjs +224 -0
- package/dist/grounding-verification.mjs.map +1 -0
- package/dist/grounding.d.mts +24 -0
- package/dist/grounding.d.mts.map +1 -0
- package/dist/grounding.mjs +77 -0
- package/dist/grounding.mjs.map +1 -0
- package/dist/hitl-active-learning.d.mts +41 -0
- package/dist/hitl-active-learning.d.mts.map +1 -0
- package/dist/hitl-active-learning.mjs +178 -0
- package/dist/hitl-active-learning.mjs.map +1 -0
- package/dist/hitl-annotation.d.mts +74 -0
- package/dist/hitl-annotation.d.mts.map +1 -0
- package/dist/hitl-annotation.mjs +170 -0
- package/dist/hitl-annotation.mjs.map +1 -0
- package/dist/hitl-approval.d.mts +2 -0
- package/dist/hitl-approval.mjs +3 -0
- package/dist/hitl-feedback.d.mts +59 -0
- package/dist/hitl-feedback.d.mts.map +1 -0
- package/dist/hitl-feedback.mjs +137 -0
- package/dist/hitl-feedback.mjs.map +1 -0
- package/dist/hitl-review.d.mts +2 -0
- package/dist/hitl-review.mjs +3 -0
- package/dist/hitl.d.mts +14 -0
- package/dist/hitl.d.mts.map +1 -0
- package/dist/hitl.mjs +22 -0
- package/dist/hitl.mjs.map +1 -0
- package/dist/index-B17HT-VL.d.mts +285 -0
- package/dist/index-B17HT-VL.d.mts.map +1 -0
- package/dist/index-BDwgsK9B.d.mts +101 -0
- package/dist/index-BDwgsK9B.d.mts.map +1 -0
- package/dist/index-BGgMn_Ev.d.mts +2615 -0
- package/dist/index-BGgMn_Ev.d.mts.map +1 -0
- package/dist/index-DOqe5r9G.d.mts +318 -0
- package/dist/index-DOqe5r9G.d.mts.map +1 -0
- package/dist/index-DotINT7o.d.mts +1004 -0
- package/dist/index-DotINT7o.d.mts.map +1 -0
- package/dist/index-URlW7aD1.d.mts +67 -0
- package/dist/index-URlW7aD1.d.mts.map +1 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +46 -0
- package/dist/index.mjs.map +1 -0
- package/dist/integrations-blob-storage.d.mts +25 -0
- package/dist/integrations-blob-storage.d.mts.map +1 -0
- package/dist/integrations-blob-storage.mjs +3 -0
- package/dist/integrations-notifications.d.mts +2 -0
- package/dist/integrations-notifications.mjs +3 -0
- package/dist/integrations-rate-limit.d.mts +27 -0
- package/dist/integrations-rate-limit.d.mts.map +1 -0
- package/dist/integrations-rate-limit.mjs +30 -0
- package/dist/integrations-rate-limit.mjs.map +1 -0
- package/dist/integrations-redis.d.mts +3 -0
- package/dist/integrations-redis.mjs +3 -0
- package/dist/integrations-stream.d.mts +14 -0
- package/dist/integrations-stream.d.mts.map +1 -0
- package/dist/integrations-stream.mjs +3 -0
- package/dist/integrations.d.mts +7 -0
- package/dist/integrations.mjs +7 -0
- package/dist/log-adapter-BLegSZtz.d.mts +16 -0
- package/dist/log-adapter-BLegSZtz.d.mts.map +1 -0
- package/dist/log-adapter-PPe_2Pwv.mjs +28 -0
- package/dist/log-adapter-PPe_2Pwv.mjs.map +1 -0
- package/dist/loop-BOYEtr2g.mjs +520 -0
- package/dist/loop-BOYEtr2g.mjs.map +1 -0
- package/dist/loop-C-tBBOqi.d.mts +219 -0
- package/dist/loop-C-tBBOqi.d.mts.map +1 -0
- package/dist/middleware-5wQ9bANW.mjs +306 -0
- package/dist/middleware-5wQ9bANW.mjs.map +1 -0
- package/dist/middleware-C1apSrj0.mjs +613 -0
- package/dist/middleware-C1apSrj0.mjs.map +1 -0
- package/dist/middleware-C7k0uItW.d.mts +9 -0
- package/dist/middleware-C7k0uItW.d.mts.map +1 -0
- package/dist/middleware-CZQCTHfl.mjs +366 -0
- package/dist/middleware-CZQCTHfl.mjs.map +1 -0
- package/dist/models.d.mts +11 -0
- package/dist/models.d.mts.map +1 -0
- package/dist/models.mjs +32 -0
- package/dist/models.mjs.map +1 -0
- package/dist/observability-analytics.d.mts +107 -0
- package/dist/observability-analytics.d.mts.map +1 -0
- package/dist/observability-analytics.mjs +409 -0
- package/dist/observability-analytics.mjs.map +1 -0
- package/dist/observability-cost.d.mts +10 -0
- package/dist/observability-cost.d.mts.map +1 -0
- package/dist/observability-cost.mjs +15 -0
- package/dist/observability-cost.mjs.map +1 -0
- package/dist/observability-telemetry.d.mts +111 -0
- package/dist/observability-telemetry.d.mts.map +1 -0
- package/dist/observability-telemetry.mjs +343 -0
- package/dist/observability-telemetry.mjs.map +1 -0
- package/dist/observability-tracing.d.mts +10 -0
- package/dist/observability-tracing.d.mts.map +1 -0
- package/dist/observability-tracing.mjs +17 -0
- package/dist/observability-tracing.mjs.map +1 -0
- package/dist/observability.d.mts +15 -0
- package/dist/observability.d.mts.map +1 -0
- package/dist/observability.mjs +17 -0
- package/dist/observability.mjs.map +1 -0
- package/dist/optimizer-DhXXpci6.mjs +97 -0
- package/dist/optimizer-DhXXpci6.mjs.map +1 -0
- package/dist/output-multimodal.d.mts +9 -0
- package/dist/output-multimodal.d.mts.map +1 -0
- package/dist/output-multimodal.mjs +18 -0
- package/dist/output-multimodal.mjs.map +1 -0
- package/dist/output.d.mts +11 -0
- package/dist/output.d.mts.map +1 -0
- package/dist/output.mjs +40 -0
- package/dist/output.mjs.map +1 -0
- package/dist/pii-filter-3AxmYSiu.d.mts +23 -0
- package/dist/pii-filter-3AxmYSiu.d.mts.map +1 -0
- package/dist/pipelines.d.mts +448 -0
- package/dist/pipelines.d.mts.map +1 -0
- package/dist/pipelines.mjs +1534 -0
- package/dist/pipelines.mjs.map +1 -0
- package/dist/prompt-injection-DQXchzsV.d.mts +8 -0
- package/dist/prompt-injection-DQXchzsV.d.mts.map +1 -0
- package/dist/prompt-injection-RpoLPwSa.mjs +52 -0
- package/dist/prompt-injection-RpoLPwSa.mjs.map +1 -0
- package/dist/prompts.d.mts +192 -0
- package/dist/prompts.d.mts.map +1 -0
- package/dist/prompts.mjs +732 -0
- package/dist/prompts.mjs.map +1 -0
- package/dist/protocol-DfBiEsnl.d.mts +112 -0
- package/dist/protocol-DfBiEsnl.d.mts.map +1 -0
- package/dist/quota-manager-0iPMkQWN.d.mts +62 -0
- package/dist/quota-manager-0iPMkQWN.d.mts.map +1 -0
- package/dist/quota-manager-D_N7FuQ2.mjs +180 -0
- package/dist/quota-manager-D_N7FuQ2.mjs.map +1 -0
- package/dist/redaction-utils-DcQwsiNh.mjs +438 -0
- package/dist/redaction-utils-DcQwsiNh.mjs.map +1 -0
- package/dist/redis-CpsSrF8K.mjs +102 -0
- package/dist/redis-CpsSrF8K.mjs.map +1 -0
- package/dist/redis-CwguYFGh.d.mts +33 -0
- package/dist/redis-CwguYFGh.d.mts.map +1 -0
- package/dist/registry-CsD3iTIx.mjs +190 -0
- package/dist/registry-CsD3iTIx.mjs.map +1 -0
- package/dist/registry-DVPWzkXR.d.mts +36 -0
- package/dist/registry-DVPWzkXR.d.mts.map +1 -0
- package/dist/reranking-BpWYhYzl.d.mts +72 -0
- package/dist/reranking-BpWYhYzl.d.mts.map +1 -0
- package/dist/reranking-Ck8aKZW7.mjs +331 -0
- package/dist/reranking-Ck8aKZW7.mjs.map +1 -0
- package/dist/resumable-adapter-CO1HtsgJ.mjs +21 -0
- package/dist/resumable-adapter-CO1HtsgJ.mjs.map +1 -0
- package/dist/review-trigger-DmAsiQlM.d.mts +24 -0
- package/dist/review-trigger-DmAsiQlM.d.mts.map +1 -0
- package/dist/review-trigger-DwXfpww9.mjs +112 -0
- package/dist/review-trigger-DwXfpww9.mjs.map +1 -0
- package/dist/safe-context-BynhkTKR.d.mts +54 -0
- package/dist/safe-context-BynhkTKR.d.mts.map +1 -0
- package/dist/safe-context-C5A3Wv3b.mjs +143 -0
- package/dist/safe-context-C5A3Wv3b.mjs.map +1 -0
- package/dist/schema-Bu2noOZ4.mjs +27 -0
- package/dist/schema-Bu2noOZ4.mjs.map +1 -0
- package/dist/schema-CwFvuCnA.mjs +97 -0
- package/dist/schema-CwFvuCnA.mjs.map +1 -0
- package/dist/schema-Wz-1-ro9.d.mts +37 -0
- package/dist/schema-Wz-1-ro9.d.mts.map +1 -0
- package/dist/schemas-CxQtxIga.mjs +62 -0
- package/dist/schemas-CxQtxIga.mjs.map +1 -0
- package/dist/schemas-DBOhxgW7.d.mts +32 -0
- package/dist/schemas-DBOhxgW7.d.mts.map +1 -0
- package/dist/schemas-Dp_OCqBt.d.mts +63 -0
- package/dist/schemas-Dp_OCqBt.d.mts.map +1 -0
- package/dist/schemas-SwCsnT0z.mjs +83 -0
- package/dist/schemas-SwCsnT0z.mjs.map +1 -0
- package/dist/sdk-errors.d.mts +2 -0
- package/dist/sdk-errors.mjs +3 -0
- package/dist/sdk-experimental.d.mts +59 -0
- package/dist/sdk-experimental.d.mts.map +1 -0
- package/dist/sdk-experimental.mjs +193 -0
- package/dist/sdk-experimental.mjs.map +1 -0
- package/dist/sdk-stop-conditions.d.mts +3 -0
- package/dist/sdk-stop-conditions.mjs +3 -0
- package/dist/sdk.d.mts +15 -0
- package/dist/sdk.d.mts.map +1 -0
- package/dist/sdk.mjs +50 -0
- package/dist/sdk.mjs.map +1 -0
- package/dist/security-guardrails.d.mts +3 -0
- package/dist/security-guardrails.mjs +3 -0
- package/dist/security-injection.d.mts +2 -0
- package/dist/security-injection.mjs +3 -0
- package/dist/security.d.mts +12 -0
- package/dist/security.d.mts.map +1 -0
- package/dist/security.mjs +18 -0
- package/dist/security.mjs.map +1 -0
- package/dist/server.d.mts +420 -0
- package/dist/server.d.mts.map +1 -0
- package/dist/server.mjs +2225 -0
- package/dist/server.mjs.map +1 -0
- package/dist/shared.d.mts +2 -0
- package/dist/shared.mjs +3 -0
- package/dist/streaming-control.d.mts +2 -0
- package/dist/streaming-control.mjs +4 -0
- package/dist/streaming-core.d.mts +4 -0
- package/dist/streaming-core.mjs +3 -0
- package/dist/streaming-infra-resilience.d.mts +120 -0
- package/dist/streaming-infra-resilience.d.mts.map +1 -0
- package/dist/streaming-infra-resilience.mjs +358 -0
- package/dist/streaming-infra-resilience.mjs.map +1 -0
- package/dist/streaming-infra-transport.d.mts +57 -0
- package/dist/streaming-infra-transport.d.mts.map +1 -0
- package/dist/streaming-infra-transport.mjs +488 -0
- package/dist/streaming-infra-transport.mjs.map +1 -0
- package/dist/streaming-infra.d.mts +5 -0
- package/dist/streaming-infra.mjs +5 -0
- package/dist/streaming.d.mts +17 -0
- package/dist/streaming.d.mts.map +1 -0
- package/dist/streaming.mjs +71 -0
- package/dist/streaming.mjs.map +1 -0
- package/dist/telemetry-2eKMojIb.mjs +1046 -0
- package/dist/telemetry-2eKMojIb.mjs.map +1 -0
- package/dist/telemetry-C2t03dwD.d.mts +59 -0
- package/dist/telemetry-C2t03dwD.d.mts.map +1 -0
- package/dist/tool-Btbththq.d.mts +253 -0
- package/dist/tool-Btbththq.d.mts.map +1 -0
- package/dist/tool-JSf8JXZ8.mjs +1150 -0
- package/dist/tool-JSf8JXZ8.mjs.map +1 -0
- package/dist/tool-safety-CZO8a4D4.d.mts +60 -0
- package/dist/tool-safety-CZO8a4D4.d.mts.map +1 -0
- package/dist/tool-safety-DXtYDXod.mjs +319 -0
- package/dist/tool-safety-DXtYDXod.mjs.map +1 -0
- package/dist/tools-BuS2Uv0q.mjs +1708 -0
- package/dist/tools-BuS2Uv0q.mjs.map +1 -0
- package/dist/tools-approval.d.mts +99 -0
- package/dist/tools-approval.d.mts.map +1 -0
- package/dist/tools-approval.mjs +395 -0
- package/dist/tools-approval.mjs.map +1 -0
- package/dist/tools-compliance.d.mts +67 -0
- package/dist/tools-compliance.d.mts.map +1 -0
- package/dist/tools-compliance.mjs +330 -0
- package/dist/tools-compliance.mjs.map +1 -0
- package/dist/tools-computer.d.mts +25 -0
- package/dist/tools-computer.d.mts.map +1 -0
- package/dist/tools-computer.mjs +64 -0
- package/dist/tools-computer.mjs.map +1 -0
- package/dist/tools-core.d.mts +3 -0
- package/dist/tools-core.mjs +3 -0
- package/dist/tools-mcp.d.mts +3 -0
- package/dist/tools-mcp.mjs +5 -0
- package/dist/tools-superpowers.d.mts +2 -0
- package/dist/tools-superpowers.mjs +3 -0
- package/dist/tools.d.mts +401 -0
- package/dist/tools.d.mts.map +1 -0
- package/dist/tools.mjs +1921 -0
- package/dist/tools.mjs.map +1 -0
- package/dist/transport-selector-D-Ib05X1.mjs +1936 -0
- package/dist/transport-selector-D-Ib05X1.mjs.map +1 -0
- package/dist/triage-agent-BEsXg5sw.d.mts +63 -0
- package/dist/triage-agent-BEsXg5sw.d.mts.map +1 -0
- package/dist/triage-agent-CBsfX-HW.mjs +167 -0
- package/dist/triage-agent-CBsfX-HW.mjs.map +1 -0
- package/dist/types-BPnq3GQo.d.mts +23 -0
- package/dist/types-BPnq3GQo.d.mts.map +1 -0
- package/dist/types-BjWgimpY.d.mts +16 -0
- package/dist/types-BjWgimpY.d.mts.map +1 -0
- package/dist/types-BxD-5btB.d.mts +41 -0
- package/dist/types-BxD-5btB.d.mts.map +1 -0
- package/dist/types-By-r93bE.d.mts +36 -0
- package/dist/types-By-r93bE.d.mts.map +1 -0
- package/dist/types-CLBWFRZN.d.mts +69 -0
- package/dist/types-CLBWFRZN.d.mts.map +1 -0
- package/dist/types-CQ0HFd0u.d.mts +62 -0
- package/dist/types-CQ0HFd0u.d.mts.map +1 -0
- package/dist/types-D3zJb59_.d.mts +47 -0
- package/dist/types-D3zJb59_.d.mts.map +1 -0
- package/dist/types-DJnugQX0.d.mts +80 -0
- package/dist/types-DJnugQX0.d.mts.map +1 -0
- package/dist/types-DbUfMCnT.d.mts +70 -0
- package/dist/types-DbUfMCnT.d.mts.map +1 -0
- package/dist/upstash-adapter-D96Caq2O.mjs +22 -0
- package/dist/upstash-adapter-D96Caq2O.mjs.map +1 -0
- package/dist/upstash-adapter-DD4433dx.d.mts +8 -0
- package/dist/upstash-adapter-DD4433dx.d.mts.map +1 -0
- package/dist/utils-BlYhcD6M.mjs +319 -0
- package/dist/utils-BlYhcD6M.mjs.map +1 -0
- package/dist/utils-DpJGOb3y.d.mts +120 -0
- package/dist/utils-DpJGOb3y.d.mts.map +1 -0
- package/dist/vercel-blob-adapter-CkOXLT2D.mjs +25 -0
- package/dist/vercel-blob-adapter-CkOXLT2D.mjs.map +1 -0
- package/dist/xai-CbV_dCnP.mjs +1600 -0
- package/dist/xai-CbV_dCnP.mjs.map +1 -0
- package/package.json +479 -0
- package/src/agents/base/factory.ts +382 -0
- package/src/agents/base/index.ts +8 -0
- package/src/agents/base/schemas.ts +117 -0
- package/src/agents/base/types.ts +192 -0
- package/src/agents/control-flow/index.ts +683 -0
- package/src/agents/coordinator/coordinator-agent.ts +381 -0
- package/src/agents/coordinator/index.ts +6 -0
- package/src/agents/default-agent.ts +211 -0
- package/src/agents/evaluator-optimizer/README.md +612 -0
- package/src/agents/evaluator-optimizer/evaluator-optimizer.example.ts +437 -0
- package/src/agents/evaluator-optimizer/evaluator.ts +282 -0
- package/src/agents/evaluator-optimizer/index.test.ts +416 -0
- package/src/agents/evaluator-optimizer/index.ts +519 -0
- package/src/agents/evaluator-optimizer/optimizer.ts +322 -0
- package/src/agents/evaluator-optimizer/schema.ts +302 -0
- package/src/agents/evaluator-optimizer/utils.ts +42 -0
- package/src/agents/experimental/index.ts +1095 -0
- package/src/agents/experimental/types.ts +212 -0
- package/src/agents/fallback/index.ts +18 -0
- package/src/agents/fallback/recovery/circuit-breaker.ts +166 -0
- package/src/agents/fallback/strategies/model-fallback.ts +192 -0
- package/src/agents/fallback/types.ts +87 -0
- package/src/agents/governance-agent.ts +446 -0
- package/src/agents/index.ts +79 -0
- package/src/agents/multi/coordination/index.ts +6 -0
- package/src/agents/multi/coordination/message-bus.ts +144 -0
- package/src/agents/multi/index.ts +6 -0
- package/src/agents/multi/state/index.ts +162 -0
- package/src/agents/multi/supervisor/index.ts +7 -0
- package/src/agents/multi/supervisor/supervisor.ts +254 -0
- package/src/agents/multi/swarm/aggregation.ts +466 -0
- package/src/agents/multi/swarm/communication.ts +388 -0
- package/src/agents/multi/swarm/coordination.ts +380 -0
- package/src/agents/multi/swarm/index.ts +73 -0
- package/src/agents/multi/swarm/swarm-executor.ts +479 -0
- package/src/agents/multi/types.ts +181 -0
- package/src/agents/observability/index.ts +914 -0
- package/src/agents/orchestrator.ts +218 -0
- package/src/agents/patterns/README.md +512 -0
- package/src/agents/patterns/evaluator-optimizer-pattern.example.ts +455 -0
- package/src/agents/patterns/evaluator-optimizer-pattern.ts +653 -0
- package/src/agents/patterns/index.ts +26 -0
- package/src/agents/persistence/index.ts +726 -0
- package/src/agents/tools/index.ts +291 -0
- package/src/agents/tools/mcp.ts +188 -0
- package/src/agents/triage/index.ts +6 -0
- package/src/agents/triage/triage-agent.ts +280 -0
- package/src/agents/workflows/index.ts +6 -0
- package/src/agents/workflows/interfaces.ts +36 -0
- package/src/agents/workflows/schema.ts +20 -0
- package/src/caching/adapters/index.ts +7 -0
- package/src/caching/adapters/memory.ts +77 -0
- package/src/caching/adapters/redis.ts +60 -0
- package/src/caching/index.ts +17 -0
- package/src/caching/middleware.ts +452 -0
- package/src/caching/strategies/index.ts +1008 -0
- package/src/caching/types.ts +47 -0
- package/src/catalog.ts +921 -0
- package/src/client/chat-usage.ts +53 -0
- package/src/client/hooks.ts +343 -0
- package/src/client/index.ts +36 -0
- package/src/client/message-utils.ts +29 -0
- package/src/client/use-generative-ui.ts +174 -0
- package/src/client/utils.ts +66 -0
- package/src/generative-ui/catalog.ts +653 -0
- package/src/generative-ui/index.ts +82 -0
- package/src/generative-ui/registry.ts +273 -0
- package/src/generative-ui/stream.ts +324 -0
- package/src/generative-ui/types.ts +376 -0
- package/src/governance/audit/audit-logger.ts +239 -0
- package/src/governance/audit/audit-schema.ts +82 -0
- package/src/governance/audit/index.ts +6 -0
- package/src/governance/compliance/abac/policy-engine.ts +175 -0
- package/src/governance/compliance/abac/types.ts +40 -0
- package/src/governance/compliance/approval/compliance-approval-queue.ts +217 -0
- package/src/governance/compliance/index.ts +16 -0
- package/src/governance/compliance/schemas.ts +68 -0
- package/src/governance/compliance/types.ts +143 -0
- package/src/governance/compliance/validators/phi-detector.ts +145 -0
- package/src/governance/compliance/validators/redaction-utils.ts +176 -0
- package/src/governance/compliance/validators/safe-harbor.ts +135 -0
- package/src/governance/entitlements/index.ts +585 -0
- package/src/governance/entitlements/middleware.ts +651 -0
- package/src/governance/entitlements/rate-limiter.ts +711 -0
- package/src/governance/index.ts +32 -0
- package/src/governance/policies/guardrails.ts +1121 -0
- package/src/governance/policies/index.ts +42 -0
- package/src/governance/policies/loop-controls.ts +136 -0
- package/src/governance/policies/telemetry.ts +63 -0
- package/src/governance/tenancy/index.ts +30 -0
- package/src/governance/tenancy/isolation/context.ts +92 -0
- package/src/governance/tenancy/isolation/index.ts +13 -0
- package/src/governance/tenancy/quotas/index.ts +11 -0
- package/src/governance/tenancy/quotas/quota-manager.ts +180 -0
- package/src/governance/tenancy/types.ts +66 -0
- package/src/governance/types.ts +16 -0
- package/src/governance/versioning/index.ts +573 -0
- package/src/grounding/attribution/index.ts +424 -0
- package/src/grounding/citation/citation-generator.ts +174 -0
- package/src/grounding/citation/index.ts +12 -0
- package/src/grounding/context/index.ts +32 -0
- package/src/grounding/context/safe-context.ts +116 -0
- package/src/grounding/context/types.ts +62 -0
- package/src/grounding/context-engineering/error-handling.ts +359 -0
- package/src/grounding/context-engineering/index.ts +23 -0
- package/src/grounding/context-engineering/memory.ts +559 -0
- package/src/grounding/context-engineering/tool-masking.ts +338 -0
- package/src/grounding/embed/index.ts +704 -0
- package/src/grounding/embed/reranking.ts +604 -0
- package/src/grounding/hallucination/index.ts +223 -0
- package/src/grounding/index.ts +82 -0
- package/src/grounding/proof-map/applyPatch.ts +172 -0
- package/src/grounding/proof-map/index.ts +41 -0
- package/src/grounding/proof-map/loop.ts +275 -0
- package/src/grounding/proof-map/schema.ts +217 -0
- package/src/grounding/rag/__tests__/pipeline.test.ts +274 -0
- package/src/grounding/rag/__tests__/tool.test.ts +202 -0
- package/src/grounding/rag/__tests__/trace.test.ts +229 -0
- package/src/grounding/rag/circuit-breaker.ts +152 -0
- package/src/grounding/rag/index.ts +64 -0
- package/src/grounding/rag/pipeline.ts +602 -0
- package/src/grounding/rag/tool.ts +281 -0
- package/src/grounding/rag/trace.ts +503 -0
- package/src/grounding/rag/types.ts +284 -0
- package/src/grounding/retrieval/in-memory-store.ts +107 -0
- package/src/grounding/sources/index.ts +943 -0
- package/src/grounding/tests/applyPatch.test.ts +194 -0
- package/src/grounding/tests/loop.test.ts +141 -0
- package/src/grounding/tests/schema.test.ts +160 -0
- package/src/grounding/types.ts +100 -0
- package/src/grounding/verification/index.ts +419 -0
- package/src/hitl/active-learning/index.ts +332 -0
- package/src/hitl/annotation/index.ts +362 -0
- package/src/hitl/approval/approval-queue.ts +132 -0
- package/src/hitl/approval/index.ts +5 -0
- package/src/hitl/feedback/index.ts +284 -0
- package/src/hitl/index.ts +69 -0
- package/src/hitl/review/index.ts +6 -0
- package/src/hitl/review/review-trigger.ts +162 -0
- package/src/hitl/types.ts +126 -0
- package/src/index.ts +125 -0
- package/src/integrations/blob-storage/index.ts +7 -0
- package/src/integrations/blob-storage/types.ts +28 -0
- package/src/integrations/blob-storage/vercel-blob-adapter.ts +35 -0
- package/src/integrations/index.ts +15 -0
- package/src/integrations/notifications/index.ts +7 -0
- package/src/integrations/notifications/log-adapter.ts +30 -0
- package/src/integrations/notifications/types.ts +27 -0
- package/src/integrations/rate-limit/index.ts +7 -0
- package/src/integrations/rate-limit/types.ts +26 -0
- package/src/integrations/rate-limit/upstash-adapter.ts +45 -0
- package/src/integrations/redis/index.ts +7 -0
- package/src/integrations/redis/types.ts +67 -0
- package/src/integrations/redis/upstash-adapter.ts +18 -0
- package/src/integrations/stream/index.ts +7 -0
- package/src/integrations/stream/resumable-adapter.ts +20 -0
- package/src/integrations/stream/types.ts +21 -0
- package/src/internal/__tests__/hallucination.test.ts +162 -0
- package/src/internal/__tests__/models.test.ts +104 -0
- package/src/internal/__tests__/sdk-errors.test.ts +201 -0
- package/src/internal/__tests__/stop-conditions.test.ts +210 -0
- package/src/internal/shared/ai-types.ts +942 -0
- package/src/internal/testing/evaluators.ts +575 -0
- package/src/internal/testing/index.ts +960 -0
- package/src/internal/ui/data-parts.ts +511 -0
- package/src/internal/ui/type-guards.ts +344 -0
- package/src/internal/ui-factories/__tests__/ui-factories.test.ts +548 -0
- package/src/internal/ui-factories/artifact-factory.ts +667 -0
- package/src/internal/ui-factories/index.ts +82 -0
- package/src/internal/ui-factories/shimmer-manager.ts +220 -0
- package/src/internal/ui-factories/status-helpers.ts +149 -0
- package/src/internal/ui-factories/tool-renderer.ts +167 -0
- package/src/internal/ui-factories/types.ts +235 -0
- package/src/models/capabilities.ts +88 -0
- package/src/models/index.ts +16 -0
- package/src/models/provider-factory.ts +229 -0
- package/src/models/providers/anthropic.ts +539 -0
- package/src/models/providers/google.ts +354 -0
- package/src/models/providers/index.ts +21 -0
- package/src/models/providers/openai.ts +346 -0
- package/src/models/providers/perplexity.ts +276 -0
- package/src/models/providers/shared.ts +90 -0
- package/src/models/providers/xai.ts +269 -0
- package/src/models/registry.ts +208 -0
- package/src/models/routing/index.ts +45 -0
- package/src/models/routing/intent-router.ts +143 -0
- package/src/models/routing/model-router.ts +300 -0
- package/src/models/routing/types.ts +106 -0
- package/src/models/types.ts +23 -0
- package/src/observability/analytics/index.ts +593 -0
- package/src/observability/cost/index.ts +16 -0
- package/src/observability/cost/tracking/budget-guard.ts +110 -0
- package/src/observability/cost/tracking/usage-tracker.ts +120 -0
- package/src/observability/cost/types.ts +85 -0
- package/src/observability/index.ts +17 -0
- package/src/observability/telemetry/index.ts +508 -0
- package/src/observability/tracing/index.ts +30 -0
- package/src/observability/tracing/otel/ai-instrumentation.ts +193 -0
- package/src/observability/tracing/otel/exporters/console.ts +58 -0
- package/src/observability/tracing/otel/exporters/index.ts +6 -0
- package/src/observability/tracing/provenance.ts +769 -0
- package/src/observability/tracing/types.ts +92 -0
- package/src/output/__tests__/output.test.ts +737 -0
- package/src/output/element-stream.ts +678 -0
- package/src/output/errors.ts +108 -0
- package/src/output/factories.ts +392 -0
- package/src/output/index.ts +98 -0
- package/src/output/multimodal/EXPORTS.md +306 -0
- package/src/output/multimodal/IMPLEMENTATION_SUMMARY.md +421 -0
- package/src/output/multimodal/README.md +349 -0
- package/src/output/multimodal/SETUP_GUIDE.md +472 -0
- package/src/output/multimodal/audio.ts +650 -0
- package/src/output/multimodal/image.ts +22 -0
- package/src/output/multimodal/index.ts +32 -0
- package/src/output/multimodal/providers.example.ts +375 -0
- package/src/output/validator.ts +495 -0
- package/src/pipelines/adapters/trace-storage-blob.ts +458 -0
- package/src/pipelines/adapters/trace-storage-memory.ts +319 -0
- package/src/pipelines/defaults.ts +109 -0
- package/src/pipelines/index.ts +24 -0
- package/src/pipelines/message-transforms.ts +107 -0
- package/src/pipelines/multi-step-wrapper.ts +433 -0
- package/src/pipelines/pipeline-presets.ts +339 -0
- package/src/pipelines/step-executor.ts +257 -0
- package/src/pipelines/storage-factory.ts +85 -0
- package/src/pipelines/trace-storage-interface.ts +216 -0
- package/src/pipelines/types.ts +255 -0
- package/src/pipelines/validation.ts +323 -0
- package/src/prompts/index.ts +271 -0
- package/src/prompts/model-variants.ts +410 -0
- package/src/prompts/templates.ts +327 -0
- package/src/sdk/errors/base.ts +296 -0
- package/src/sdk/errors/index.ts +31 -0
- package/src/sdk/errors/utils.ts +148 -0
- package/src/sdk/experimental/index.ts +286 -0
- package/src/sdk/index.ts +25 -0
- package/src/sdk/middleware/ai-middleware.ts +95 -0
- package/src/sdk/middleware/cache.ts +154 -0
- package/src/sdk/middleware/circuit-breaker.ts +388 -0
- package/src/sdk/middleware/compose.ts +81 -0
- package/src/sdk/middleware/deduplication.ts +307 -0
- package/src/sdk/middleware/index.ts +660 -0
- package/src/sdk/middleware/model-middleware.ts +200 -0
- package/src/sdk/stop-conditions/conditions.ts +209 -0
- package/src/sdk/stop-conditions/index.ts +35 -0
- package/src/sdk/stop-conditions/types.ts +59 -0
- package/src/security/guardrails/index.ts +6 -0
- package/src/security/guardrails/middleware.ts +465 -0
- package/src/security/guardrails/pii-filter.ts +396 -0
- package/src/security/index.ts +33 -0
- package/src/security/injection/index.ts +5 -0
- package/src/security/injection/prompt-injection.ts +64 -0
- package/src/security/types.ts +85 -0
- package/src/server/cache/crypto.ts +47 -0
- package/src/server/cache/performance.ts +79 -0
- package/src/server/error-handler.ts +93 -0
- package/src/server/errors.ts +73 -0
- package/src/server/helpers.ts +944 -0
- package/src/server/http.ts +156 -0
- package/src/server/index.ts +12 -0
- package/src/server/messages/__tests__/messages.test.ts +720 -0
- package/src/server/messages/converter.ts +245 -0
- package/src/server/messages/data-parts.ts +338 -0
- package/src/server/messages/extraction.ts +328 -0
- package/src/server/messages/index.ts +126 -0
- package/src/server/messages/types.ts +355 -0
- package/src/server/messages/window.ts +450 -0
- package/src/server/rate-limit/env.ts +8 -0
- package/src/server/rate-limit/rate-limit.ts +165 -0
- package/src/server/routes/HEALTH_CHECK.md +502 -0
- package/src/server/routes/IMPLEMENTATION_SUMMARY.md +432 -0
- package/src/server/routes/QUICK_START.md +327 -0
- package/src/server/routes/README.md +357 -0
- package/src/server/routes/__tests__/routes.test.ts +628 -0
- package/src/server/routes/agent-route.ts +224 -0
- package/src/server/routes/agent-routes.ts +191 -0
- package/src/server/routes/embed-config.ts +181 -0
- package/src/server/routes/health-check.example.ts +507 -0
- package/src/server/routes/health-check.test.ts +533 -0
- package/src/server/routes/health-check.ts +639 -0
- package/src/server/routes/health-check.types.ts +217 -0
- package/src/server/routes/index.ts +32 -0
- package/src/server/routes/types.ts +274 -0
- package/src/shared/__tests__/schemas.test.ts +317 -0
- package/src/shared/ai-runtime.ts +139 -0
- package/src/shared/ai-types.ts +133 -0
- package/src/shared/index.ts +30 -0
- package/src/shared/sdk-runtime.ts +198 -0
- package/src/shared/sdk-types.ts +301 -0
- package/src/streaming/control/__tests__/streaming-control.test.ts +708 -0
- package/src/streaming/control/budget-guard.ts +264 -0
- package/src/streaming/control/controller.ts +255 -0
- package/src/streaming/control/index.ts +105 -0
- package/src/streaming/control/smoothing.ts +201 -0
- package/src/streaming/control/step-limit.ts +215 -0
- package/src/streaming/control/types.ts +234 -0
- package/src/streaming/core/auto-resume.ts +276 -0
- package/src/streaming/core/index.ts +85 -0
- package/src/streaming/core/multi-step.ts +471 -0
- package/src/streaming/core/protocol.ts +194 -0
- package/src/streaming/core/types.ts +182 -0
- package/src/streaming/index.ts +97 -0
- package/src/streaming/infra/backpressure/buffer.ts +210 -0
- package/src/streaming/infra/backpressure/index.ts +6 -0
- package/src/streaming/infra/index.ts +75 -0
- package/src/streaming/infra/multiplexing/index.ts +311 -0
- package/src/streaming/infra/resilience/index.ts +684 -0
- package/src/streaming/infra/transform/index.ts +15 -0
- package/src/streaming/infra/transform/stream-transforms.ts +166 -0
- package/src/streaming/infra/transport/index.ts +774 -0
- package/src/streaming/infra/types.ts +118 -0
- package/src/streaming/infra-extra/types.ts +118 -0
- package/src/tools/advanced/caching.ts +299 -0
- package/src/tools/advanced/generator.ts +267 -0
- package/src/tools/advanced/hitl.ts +251 -0
- package/src/tools/advanced/index.ts +9 -0
- package/src/tools/advanced/llm-tool.ts +208 -0
- package/src/tools/approval/FILES.md +449 -0
- package/src/tools/approval/IMPLEMENTATION_SUMMARY.md +567 -0
- package/src/tools/approval/QUICK_START.md +362 -0
- package/src/tools/approval/README.md +514 -0
- package/src/tools/approval/advanced-approval-queue.ts +7 -0
- package/src/tools/approval/approval.example.ts +571 -0
- package/src/tools/approval/in-memory-queue.ts +405 -0
- package/src/tools/approval/index.ts +737 -0
- package/src/tools/approval/middleware.ts +590 -0
- package/src/tools/approval/queue-factory.ts +162 -0
- package/src/tools/approval/redis-queue.ts +327 -0
- package/src/tools/approval/testing.ts +493 -0
- package/src/tools/approval/types.ts +221 -0
- package/src/tools/approval/with-approval.ts +366 -0
- package/src/tools/artifacts/artifact-tools.ts +273 -0
- package/src/tools/artifacts/index.ts +6 -0
- package/src/tools/compliance/compliance-wrapper.ts +789 -0
- package/src/tools/compliance/create-compliant-stream.ts +226 -0
- package/src/tools/compliance/index.ts +8 -0
- package/src/tools/compliance/phi-redaction.ts +406 -0
- package/src/tools/compliance/tool-wrapper.ts +306 -0
- package/src/tools/computer/index.ts +99 -0
- package/src/tools/computer/types.ts +41 -0
- package/src/tools/core/abort.ts +202 -0
- package/src/tools/core/factory.ts +197 -0
- package/src/tools/core/index.ts +8 -0
- package/src/tools/core/tool-safety.ts +112 -0
- package/src/tools/generic/index.ts +9 -0
- package/src/tools/generic/json-schema-tool.ts +301 -0
- package/src/tools/generic/tiptap-context.ts +619 -0
- package/src/tools/generic/web-search-tool.ts +82 -0
- package/src/tools/generic/web-search.ts +142 -0
- package/src/tools/index.ts +36 -0
- package/src/tools/mcp/ai-sdk-error-integration.ts +401 -0
- package/src/tools/mcp/client.ts +988 -0
- package/src/tools/mcp/connection-manager.ts +380 -0
- package/src/tools/mcp/connection-pool.ts +408 -0
- package/src/tools/mcp/edge-runtime.ts +318 -0
- package/src/tools/mcp/environment.ts +310 -0
- package/src/tools/mcp/index.ts +20 -0
- package/src/tools/mcp/next-pattern.ts +401 -0
- package/src/tools/mcp/stream-lifecycle-integration.ts +617 -0
- package/src/tools/mcp/tool-cache.ts +359 -0
- package/src/tools/mcp/transport-selector.ts +492 -0
- package/src/tools/mcp/transports.ts +99 -0
- package/src/tools/simple-factory.ts +55 -0
- package/src/tools/superpowers/index.ts +122 -0
- package/src/tools/superpowers/prompts/index.ts +533 -0
- package/src/tools/superpowers/schemas/index.ts +701 -0
- package/src/tools/superpowers/tools/index.ts +721 -0
- package/src/tools/validation-wrapper.ts +97 -0
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview api-helpers.ts
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* API Route Helpers
|
|
6
|
+
* DRY utilities for Next.js API routes with database persistence
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
createAIConversationOrm,
|
|
11
|
+
createAIMessageOrm,
|
|
12
|
+
findAIConversationOrm,
|
|
13
|
+
updateAIConversationOrm,
|
|
14
|
+
} from '@od-oneapp/db-prisma/orm';
|
|
15
|
+
import { AIMessageRole, AISessionType } from '@od-oneapp/db-prisma/types';
|
|
16
|
+
import { type Logger, logError, logInfo, logWarn } from '@repo/shared/logs';
|
|
17
|
+
|
|
18
|
+
import { createResumableStream } from '../integrations/stream';
|
|
19
|
+
import { UI_MESSAGE_STREAM_HEADERS , generateId } from '../shared';
|
|
20
|
+
|
|
21
|
+
import { generateSignature, verifySignature } from './cache/crypto';
|
|
22
|
+
|
|
23
|
+
import type {
|
|
24
|
+
SDKLanguageModelUsage,
|
|
25
|
+
SDKStreamTextResult,
|
|
26
|
+
SDKUIMessage,
|
|
27
|
+
} from '../shared';
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Type for AI SDK stream finish event
|
|
32
|
+
* Matches the onFinish callback shape from streamText/generateText
|
|
33
|
+
*/
|
|
34
|
+
interface StreamFinishEvent {
|
|
35
|
+
usage?: SDKLanguageModelUsage;
|
|
36
|
+
totalUsage?: SDKLanguageModelUsage;
|
|
37
|
+
finishReason?: string;
|
|
38
|
+
text?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type WaitUntil = (promise: Promise<unknown>) => void;
|
|
42
|
+
|
|
43
|
+
const defaultWaitUntil: WaitUntil = promise => {
|
|
44
|
+
void promise;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export interface StreamResumptionAuth {
|
|
48
|
+
getUserContext: (req: Request) => Promise<{ userId: string } | null>;
|
|
49
|
+
verifyOwnership?: (userId: string, chatId: string) => Promise<boolean>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type StreamResumptionOptions = {
|
|
53
|
+
waitUntil?: WaitUntil;
|
|
54
|
+
auth?: StreamResumptionAuth;
|
|
55
|
+
tokenTtlSeconds?: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type SetupResumableStreamOptions = {
|
|
59
|
+
waitUntil?: WaitUntil;
|
|
60
|
+
userId?: string;
|
|
61
|
+
tokenTtlSeconds?: number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type ResumptionTokenPayload = {
|
|
65
|
+
streamId: string;
|
|
66
|
+
chatId: string;
|
|
67
|
+
userId: string;
|
|
68
|
+
issuedAt: number;
|
|
69
|
+
expiresAt: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const defaultTokenTtlSeconds = () => {
|
|
73
|
+
const raw = process.env.STREAM_RESUME_TOKEN_TTL_SEC ?? '3600';
|
|
74
|
+
const parsed = Number.parseInt(raw, 10);
|
|
75
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : 3600;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const getResumptionSigningKey = (): string | undefined =>
|
|
79
|
+
process.env.STREAM_RESUME_SIGNING_KEY ?? process.env.AUDIT_SIGNING_KEY;
|
|
80
|
+
|
|
81
|
+
const encodeBase64Url = (value: string): string => {
|
|
82
|
+
if (typeof Buffer !== 'undefined') {
|
|
83
|
+
return Buffer.from(value, 'utf8').toString('base64url');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof btoa !== 'undefined') {
|
|
87
|
+
const encoded = btoa(value);
|
|
88
|
+
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new Error('Base64 encoding unavailable');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const decodeBase64Url = (value: string): string => {
|
|
95
|
+
if (typeof Buffer !== 'undefined') {
|
|
96
|
+
return Buffer.from(value, 'base64url').toString('utf8');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (typeof atob !== 'undefined') {
|
|
100
|
+
const normalized = value.replace(/-/g, '+').replace(/_/g, '/');
|
|
101
|
+
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '=');
|
|
102
|
+
return atob(padded);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
throw new Error('Base64 decoding unavailable');
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const createResumptionToken = async (payload: ResumptionTokenPayload): Promise<string> => {
|
|
109
|
+
const signingKey = getResumptionSigningKey();
|
|
110
|
+
if (!signingKey) {
|
|
111
|
+
throw new Error('STREAM_RESUME_SIGNING_KEY or AUDIT_SIGNING_KEY is required for resumption');
|
|
112
|
+
}
|
|
113
|
+
const encodedPayload = encodeBase64Url(JSON.stringify(payload));
|
|
114
|
+
const signature = await generateSignature({ payload: encodedPayload }, signingKey);
|
|
115
|
+
return `${encodedPayload}.${signature}`;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const verifyResumptionToken = async (token: string): Promise<ResumptionTokenPayload | null> => {
|
|
119
|
+
const signingKey = getResumptionSigningKey();
|
|
120
|
+
if (!signingKey) {
|
|
121
|
+
throw new Error('STREAM_RESUME_SIGNING_KEY or AUDIT_SIGNING_KEY is required for resumption');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const parts = token.split('.');
|
|
125
|
+
if (parts.length !== 2) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const encodedPayload = parts[0];
|
|
130
|
+
const signature = parts[1];
|
|
131
|
+
if (!encodedPayload || !signature) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const valid = await verifySignature({ payload: encodedPayload }, signature, signingKey);
|
|
136
|
+
if (!valid) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const payload = JSON.parse(decodeBase64Url(encodedPayload)) as ResumptionTokenPayload;
|
|
141
|
+
if (!payload || typeof payload !== 'object') {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (Date.now() > payload.expiresAt) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return payload;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const resolveStreamResumptionOptions = (
|
|
153
|
+
options?: StreamResumptionOptions | WaitUntil,
|
|
154
|
+
): StreamResumptionOptions => {
|
|
155
|
+
if (typeof options === 'function') {
|
|
156
|
+
return { waitUntil: options };
|
|
157
|
+
}
|
|
158
|
+
return options ?? {};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const resolveSetupResumableStreamOptions = (
|
|
162
|
+
options?: SetupResumableStreamOptions | WaitUntil,
|
|
163
|
+
): SetupResumableStreamOptions => {
|
|
164
|
+
if (typeof options === 'function') {
|
|
165
|
+
return { waitUntil: options };
|
|
166
|
+
}
|
|
167
|
+
return options ?? {};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => {
|
|
171
|
+
return typeof value === 'object' && value !== null;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const normalizeError = (error: unknown): { message: string } => {
|
|
175
|
+
if (error instanceof Error) {
|
|
176
|
+
return { message: error.message };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (typeof error === 'string') {
|
|
180
|
+
return { message: error };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (isRecord(error) && typeof error.message === 'string') {
|
|
184
|
+
return { message: error.message };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { message: 'Unknown error' };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const toByteStream = (stream: ReadableStream<Uint8Array | string>): ReadableStream<Uint8Array> => {
|
|
191
|
+
const encoder = new TextEncoder();
|
|
192
|
+
return stream.pipeThrough(
|
|
193
|
+
new TransformStream({
|
|
194
|
+
transform(chunk, controller) {
|
|
195
|
+
if (typeof chunk === 'string') {
|
|
196
|
+
controller.enqueue(encoder.encode(chunk));
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (chunk instanceof Uint8Array) {
|
|
200
|
+
controller.enqueue(chunk);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
controller.enqueue(encoder.encode(String(chunk)));
|
|
204
|
+
},
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Extracts error message from unknown error type.
|
|
211
|
+
* Provides consistent error message extraction across all routes.
|
|
212
|
+
*
|
|
213
|
+
* @param {unknown} error - The error to extract message from
|
|
214
|
+
* @returns {string} The error message
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* // Extract error message
|
|
219
|
+
* const message = getErrorMessage(error);
|
|
220
|
+
* return jsonError(message, 500);
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export const getErrorMessage = (error: unknown): string => {
|
|
224
|
+
return normalizeError(error).message;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Creates a JSON error response with consistent format.
|
|
229
|
+
*
|
|
230
|
+
* @param {string | Error} error - The error message or Error object
|
|
231
|
+
* @param {number} [status] - HTTP status code
|
|
232
|
+
* @returns {Response} JSON error response
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* // Create error response
|
|
237
|
+
* return jsonError('Invalid request', 400);
|
|
238
|
+
* // or
|
|
239
|
+
* return jsonError(error, 500);
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export const jsonError = (error: string | Error, status: number = 500): Response => {
|
|
243
|
+
const message = typeof error === 'string' ? error : error.message;
|
|
244
|
+
|
|
245
|
+
return new Response(JSON.stringify({ error: message }), {
|
|
246
|
+
status,
|
|
247
|
+
headers: { 'Content-Type': 'application/json' },
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Creates a JSON success response.
|
|
253
|
+
*
|
|
254
|
+
* @template T
|
|
255
|
+
* @param {T} data - The data to return
|
|
256
|
+
* @param {number} [status] - HTTP status code
|
|
257
|
+
* @returns {Response} JSON success response
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* // Create success response
|
|
262
|
+
* return jsonSuccess({ userId: '123', name: 'John' }, 200);
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export const jsonSuccess = <T>(data: T, status: number = 200): Response => {
|
|
266
|
+
return new Response(JSON.stringify(data), {
|
|
267
|
+
status,
|
|
268
|
+
headers: { 'Content-Type': 'application/json' },
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Parses and validates request body.
|
|
274
|
+
* Returns typed data or error response.
|
|
275
|
+
*
|
|
276
|
+
* @template T
|
|
277
|
+
* @param {Request} req - The request object
|
|
278
|
+
* @returns {Promise<{ data: T } | { error: Response }>} Parsed data or error response
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* // Parse request body
|
|
283
|
+
* const result = await parseRequestBody<{ message: string }>(req);
|
|
284
|
+
* if ('error' in result) {
|
|
285
|
+
* return result.error;
|
|
286
|
+
* }
|
|
287
|
+
* const { message } = result.data;
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export const parseRequestBody = async <T = unknown>(
|
|
291
|
+
req: Request,
|
|
292
|
+
): Promise<{ data: T } | { error: Response }> => {
|
|
293
|
+
try {
|
|
294
|
+
const data = (await req.json()) as T;
|
|
295
|
+
return { data };
|
|
296
|
+
} catch {
|
|
297
|
+
return {
|
|
298
|
+
error: jsonError('Invalid JSON in request body', 400),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Validates that messages is a non-empty array of UIMessage.
|
|
305
|
+
*
|
|
306
|
+
* @param {unknown} messages - The value to validate
|
|
307
|
+
* @returns {messages is UIMessage[]} True if messages is a valid array
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* // Validate messages
|
|
312
|
+
* if (!validateMessages(messages)) {
|
|
313
|
+
* return jsonError('Invalid messages format', 400);
|
|
314
|
+
* }
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
export const validateMessages = (messages: unknown): messages is SDKUIMessage[] => {
|
|
318
|
+
return Array.isArray(messages) && messages.length > 0;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Common response options for streaming
|
|
323
|
+
*/
|
|
324
|
+
export const STREAM_RESPONSE_OPTIONS = {
|
|
325
|
+
/** Standard options: sources + reasoning */
|
|
326
|
+
standard: {
|
|
327
|
+
sendSources: true,
|
|
328
|
+
sendReasoning: true,
|
|
329
|
+
},
|
|
330
|
+
/** Sources only */
|
|
331
|
+
sources: {
|
|
332
|
+
sendSources: true,
|
|
333
|
+
},
|
|
334
|
+
/** Minimal (no extras) */
|
|
335
|
+
minimal: {},
|
|
336
|
+
} as const;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Creates experimental_telemetry config for AI SDK v6.
|
|
340
|
+
* Provides observability for AI operations.
|
|
341
|
+
*
|
|
342
|
+
* @param {object} options - Telemetry configuration options
|
|
343
|
+
* @param {string} options.functionId - Unique identifier for the function
|
|
344
|
+
* @param {string} options.component - Component name
|
|
345
|
+
* @param {string} [options.version] - Version string
|
|
346
|
+
* @param {Record<string, unknown>} [options.metadata] - Additional metadata
|
|
347
|
+
* @returns {object} Telemetry configuration object
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* // Create telemetry config
|
|
352
|
+
* const telemetry = createTelemetryConfig({
|
|
353
|
+
* functionId: 'chat-handler',
|
|
354
|
+
* component: 'chat-api',
|
|
355
|
+
* version: '1.0.0',
|
|
356
|
+
* metadata: { userId: '123' }
|
|
357
|
+
* });
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
export const createTelemetryConfig = (options: {
|
|
361
|
+
functionId: string;
|
|
362
|
+
component: string;
|
|
363
|
+
version?: string;
|
|
364
|
+
metadata?: Record<string, unknown>;
|
|
365
|
+
}) => {
|
|
366
|
+
return {
|
|
367
|
+
isEnabled: true,
|
|
368
|
+
functionId: options.functionId,
|
|
369
|
+
metadata: {
|
|
370
|
+
component: options.component,
|
|
371
|
+
version: options.version || '1.0.0',
|
|
372
|
+
...options.metadata,
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Handle stream resumption for GET /stream routes
|
|
379
|
+
* Queries the database for activeStreamId and resumes if found.
|
|
380
|
+
*
|
|
381
|
+
* Note: Requires Redis to be configured for resumable streams.
|
|
382
|
+
*/
|
|
383
|
+
export async function handleStreamResumption(
|
|
384
|
+
req: Request,
|
|
385
|
+
options?: StreamResumptionOptions | WaitUntil,
|
|
386
|
+
): Promise<Response> {
|
|
387
|
+
const { waitUntil, auth } = resolveStreamResumptionOptions(options);
|
|
388
|
+
const effectiveWaitUntil = waitUntil ?? defaultWaitUntil;
|
|
389
|
+
const chatId = req.headers.get('X-Chat-ID');
|
|
390
|
+
|
|
391
|
+
if (!chatId) {
|
|
392
|
+
return jsonError('Missing X-Chat-ID header', 400);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (!auth) {
|
|
396
|
+
return jsonError('Unauthorized', 401);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const userContext = await auth.getUserContext(req);
|
|
400
|
+
if (!userContext) {
|
|
401
|
+
return jsonError('Unauthorized', 401);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (auth.verifyOwnership) {
|
|
405
|
+
const allowed = await auth.verifyOwnership(userContext.userId, chatId);
|
|
406
|
+
if (!allowed) {
|
|
407
|
+
return jsonError('Forbidden', 403);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Query database for active stream ID
|
|
412
|
+
const conversation = await findAIConversationOrm({ id: chatId });
|
|
413
|
+
const resumptionToken = conversation?.activeStreamId;
|
|
414
|
+
|
|
415
|
+
if (!resumptionToken) {
|
|
416
|
+
return new Response(null, { status: 204 });
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
const payload = await verifyResumptionToken(resumptionToken);
|
|
421
|
+
if (payload?.chatId !== chatId || payload.userId !== userContext.userId) {
|
|
422
|
+
await updateAIConversationOrm({ id: chatId }, { activeStreamId: null }).catch(() => {});
|
|
423
|
+
return new Response(null, { status: 204 });
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const streamContext = createResumableStream({ waitUntil: effectiveWaitUntil });
|
|
427
|
+
const stream = await streamContext.resumeExistingStream(payload.streamId);
|
|
428
|
+
|
|
429
|
+
if (!stream) {
|
|
430
|
+
// Stream expired or not found - clear the reference
|
|
431
|
+
await updateAIConversationOrm({ id: chatId }, { activeStreamId: null }).catch(() => {});
|
|
432
|
+
return new Response(null, { status: 204 });
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const responseStream = toByteStream(stream as ReadableStream<Uint8Array | string>);
|
|
436
|
+
|
|
437
|
+
return new Response(responseStream, {
|
|
438
|
+
headers: { ...UI_MESSAGE_STREAM_HEADERS, 'X-Stream-Resumed': 'true' },
|
|
439
|
+
});
|
|
440
|
+
} catch (error) {
|
|
441
|
+
logWarn('[handleStreamResumption] Failed to resume stream:', { error, chatId });
|
|
442
|
+
return new Response(null, { status: 204 });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Sets up resumable stream for agent responses.
|
|
448
|
+
* Wraps a response stream with resumption capabilities and stores
|
|
449
|
+
* the stream ID in the database for client-side resume.
|
|
450
|
+
*
|
|
451
|
+
* Note: Fails gracefully if Redis is not available - stream will still work,
|
|
452
|
+
* just without resumption support across page refreshes.
|
|
453
|
+
*
|
|
454
|
+
* @param {Response} response - The response stream to wrap
|
|
455
|
+
* @param {string} chatId - The chat/conversation ID for stream tracking
|
|
456
|
+
* @returns {Promise<void>}
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```ts
|
|
460
|
+
* // Setup resumable stream
|
|
461
|
+
* const response = streamText(...);
|
|
462
|
+
* await setupResumableStream(response, chatId);
|
|
463
|
+
* return response;
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
export const setupResumableStream = async (
|
|
467
|
+
response: Response,
|
|
468
|
+
chatId: string,
|
|
469
|
+
options?: SetupResumableStreamOptions | WaitUntil,
|
|
470
|
+
): Promise<void> => {
|
|
471
|
+
const { waitUntil, userId, tokenTtlSeconds } = resolveSetupResumableStreamOptions(options);
|
|
472
|
+
const effectiveWaitUntil = waitUntil ?? defaultWaitUntil;
|
|
473
|
+
try {
|
|
474
|
+
const streamId = generateId();
|
|
475
|
+
const streamContext = createResumableStream({ waitUntil: effectiveWaitUntil });
|
|
476
|
+
|
|
477
|
+
if (response.body) {
|
|
478
|
+
await streamContext.createNewResumableStream(streamId, () => response.body as ReadableStream);
|
|
479
|
+
if (!userId) {
|
|
480
|
+
logWarn('[setupResumableStream] Missing userId, resumption disabled', { chatId });
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const ttlSeconds = tokenTtlSeconds ?? defaultTokenTtlSeconds();
|
|
485
|
+
const resumptionToken = await createResumptionToken({
|
|
486
|
+
streamId,
|
|
487
|
+
chatId,
|
|
488
|
+
userId,
|
|
489
|
+
issuedAt: Date.now(),
|
|
490
|
+
expiresAt: Date.now() + ttlSeconds * 1000,
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
// Store signed resumption token for client resume
|
|
494
|
+
await updateAIConversationOrm({ id: chatId }, { activeStreamId: resumptionToken });
|
|
495
|
+
}
|
|
496
|
+
} catch (error) {
|
|
497
|
+
// Fail gracefully - stream will work without resumption support
|
|
498
|
+
// This typically happens when Redis is not configured/available
|
|
499
|
+
const isRedisError =
|
|
500
|
+
error instanceof Error &&
|
|
501
|
+
'code' in error &&
|
|
502
|
+
(error as NodeJS.ErrnoException).code === 'ECONNREFUSED';
|
|
503
|
+
|
|
504
|
+
if (isRedisError) {
|
|
505
|
+
// Silent fail for Redis connection issues - already warned in createResumableStream
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Log unexpected errors but don't throw
|
|
510
|
+
logWarn('[setupResumableStream] Failed to setup resumable stream:', { error });
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Gets or generates a chat ID for non-persistent sessions.
|
|
516
|
+
*
|
|
517
|
+
* Note: For persistent sessions with database storage, use initializePersistentSession instead.
|
|
518
|
+
*
|
|
519
|
+
* @param {string} [id] - Optional existing chat ID
|
|
520
|
+
* @returns {string} The chat ID
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* // Initialize chat session
|
|
525
|
+
* const chatId = initializeChatSession();
|
|
526
|
+
* // or with existing ID
|
|
527
|
+
* const chatId = initializeChatSession('existing-id');
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
export const initializeChatSession = (id?: string): string => {
|
|
531
|
+
return id ?? generateId();
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Initializes a persistent chat session with database storage.
|
|
536
|
+
* Use this instead of initializeChatSession for demos that need persistence.
|
|
537
|
+
*
|
|
538
|
+
* @param {object} [options] - Session initialization options
|
|
539
|
+
* @param {string} [options.sessionId] - Existing session ID
|
|
540
|
+
* @param {string} [options.userId] - User ID for the session
|
|
541
|
+
* @param {string} [options.model] - Model name for the session
|
|
542
|
+
* @param {string} [options.demo] - Demo identifier
|
|
543
|
+
* @returns {Promise<{ sessionId: string; isNew: boolean }>} Session ID and whether it's new
|
|
544
|
+
*
|
|
545
|
+
* @example
|
|
546
|
+
* ```ts
|
|
547
|
+
* // Initialize persistent session
|
|
548
|
+
* const { sessionId, isNew } = await initializePersistentSession({
|
|
549
|
+
* userId: 'user-123',
|
|
550
|
+
* model: 'gpt-4'
|
|
551
|
+
* });
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
export const initializePersistentSession = async (options?: {
|
|
555
|
+
sessionId?: string;
|
|
556
|
+
userId?: string;
|
|
557
|
+
model?: string;
|
|
558
|
+
demo?: string;
|
|
559
|
+
}): Promise<{ sessionId: string; isNew: boolean }> => {
|
|
560
|
+
let session;
|
|
561
|
+
let isNew = false;
|
|
562
|
+
|
|
563
|
+
// Try to get existing session if ID provided
|
|
564
|
+
if (options?.sessionId) {
|
|
565
|
+
session = await findAIConversationOrm({ id: options.sessionId });
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// Create new session if not found
|
|
569
|
+
if (!session) {
|
|
570
|
+
session = await createAIConversationOrm({
|
|
571
|
+
...(options?.userId ? { user: { connect: { id: options.userId } } } : {}),
|
|
572
|
+
primaryModel: options?.model,
|
|
573
|
+
sessionType: AISessionType.CONVERSATION,
|
|
574
|
+
});
|
|
575
|
+
isNew = true;
|
|
576
|
+
} else if (session.activeStreamId) {
|
|
577
|
+
// Clear any stale active stream from previous session
|
|
578
|
+
await updateAIConversationOrm({ id: session.id }, { activeStreamId: null }).catch(() => {});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
return {
|
|
582
|
+
sessionId: session.id,
|
|
583
|
+
isNew,
|
|
584
|
+
};
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Wrapper for initializePersistentSession that matches InitializeSessionFn signature
|
|
589
|
+
* Use this with createAgentRouteHandler for consistent session initialization
|
|
590
|
+
*/
|
|
591
|
+
export const initializePersistentSessionFn: (
|
|
592
|
+
sessionId?: string,
|
|
593
|
+
) => Promise<{ sessionId: string; isNew: boolean }> = async sessionId => {
|
|
594
|
+
logInfo('[initializePersistentSessionFn] Starting with sessionId:', { sessionId });
|
|
595
|
+
try {
|
|
596
|
+
const result = await initializePersistentSession({ sessionId });
|
|
597
|
+
logInfo('[initializePersistentSessionFn] Success:', { result });
|
|
598
|
+
return result;
|
|
599
|
+
} catch (error) {
|
|
600
|
+
logError(error instanceof Error ? error : new Error(String(error)), {
|
|
601
|
+
message: '[initializePersistentSessionFn] Error:',
|
|
602
|
+
});
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Save a user message to the database
|
|
609
|
+
*/
|
|
610
|
+
export async function persistUserMessage(
|
|
611
|
+
sessionId: string,
|
|
612
|
+
content: string,
|
|
613
|
+
options?: { autoTitle?: boolean },
|
|
614
|
+
): Promise<void> {
|
|
615
|
+
await createAIMessageOrm({
|
|
616
|
+
conversation: { connect: { id: sessionId } },
|
|
617
|
+
role: AIMessageRole.USER,
|
|
618
|
+
content,
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
// Auto-generate title from first message if requested
|
|
622
|
+
if (options?.autoTitle) {
|
|
623
|
+
const title = content.slice(0, 50) + (content.length > 50 ? '...' : '');
|
|
624
|
+
await updateAIConversationOrm({ id: sessionId }, { title }).catch(() => {});
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Saves an assistant message to the database.
|
|
630
|
+
*
|
|
631
|
+
* @param {string} sessionId - The session ID
|
|
632
|
+
* @param {string} content - The message content
|
|
633
|
+
* @returns {Promise<void>}
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```ts
|
|
637
|
+
* // Persist assistant message
|
|
638
|
+
* await persistAssistantMessage(sessionId, 'Hello! How can I help you?');
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
export const persistAssistantMessage = async (
|
|
642
|
+
sessionId: string,
|
|
643
|
+
content: string,
|
|
644
|
+
): Promise<void> => {
|
|
645
|
+
await createAIMessageOrm({
|
|
646
|
+
conversation: { connect: { id: sessionId } },
|
|
647
|
+
role: AIMessageRole.ASSISTANT,
|
|
648
|
+
content,
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Creates a standard onFinish callback for AI SDK streams.
|
|
654
|
+
* Combines stream cleanup with optional usage logging and persistence.
|
|
655
|
+
*
|
|
656
|
+
* AI SDK v6 native feature - leverages built-in onFinish callback
|
|
657
|
+
* instead of manually handling result.usage Promise.
|
|
658
|
+
*
|
|
659
|
+
* @param {string} chatId - The chat ID for stream tracking
|
|
660
|
+
* @param {object} [options] - Callback options
|
|
661
|
+
* @param {boolean} [options.logUsage] - Whether to log usage statistics
|
|
662
|
+
* @param {string} [options.demo] - Demo identifier for logging
|
|
663
|
+
* @param {Logger} [options.logger] - Logger instance for usage logging
|
|
664
|
+
* @param {string} [options.persistSessionId] - Session ID for database persistence
|
|
665
|
+
* @returns {(event: StreamFinishEvent) => Promise<void>} onFinish callback function
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```ts
|
|
669
|
+
* // Create onFinish callback
|
|
670
|
+
* const onFinish = createOnFinishCallback(chatId, {
|
|
671
|
+
* logUsage: true,
|
|
672
|
+
* demo: 'chat-demo',
|
|
673
|
+
* persistSessionId: sessionId
|
|
674
|
+
* });
|
|
675
|
+
* ```
|
|
676
|
+
*/
|
|
677
|
+
export const createOnFinishCallback = (
|
|
678
|
+
chatId: string,
|
|
679
|
+
options?: {
|
|
680
|
+
logUsage?: boolean;
|
|
681
|
+
demo?: string;
|
|
682
|
+
logger?: Logger;
|
|
683
|
+
/** Session ID for database persistence */
|
|
684
|
+
persistSessionId?: string;
|
|
685
|
+
},
|
|
686
|
+
) => {
|
|
687
|
+
return async (event: StreamFinishEvent) => {
|
|
688
|
+
// Clear active stream in database
|
|
689
|
+
await updateAIConversationOrm({ id: chatId }, { activeStreamId: null }).catch(() => {});
|
|
690
|
+
|
|
691
|
+
// Persist assistant response if session ID provided
|
|
692
|
+
if (options?.persistSessionId && event?.text) {
|
|
693
|
+
await persistAssistantMessage(options.persistSessionId, event.text).catch(err => {
|
|
694
|
+
options?.logger?.info?.('Failed to persist assistant message', { error: String(err) });
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Optional usage logging using AI SDK's native onFinish instead of manual Promise handling
|
|
699
|
+
if (options?.logUsage && options?.logger && event?.usage) {
|
|
700
|
+
options.logger.info(`${options.demo || 'stream'} usage`, {
|
|
701
|
+
...event.usage,
|
|
702
|
+
totalUsage: event.totalUsage,
|
|
703
|
+
demo: options.demo,
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// ---------------------------------------------------------------------------
|
|
710
|
+
// Cost & telemetry utilities (migrated from @repo/ai)
|
|
711
|
+
// ---------------------------------------------------------------------------
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Calculate model cost with extended pricing data including Vercel AI Gateway models.
|
|
715
|
+
*
|
|
716
|
+
* Supports provider-prefixed model IDs (e.g., 'openai/gpt-4o', 'anthropic/claude-sonnet-4-5')
|
|
717
|
+
* and includes pricing for newer models and gateway-specific models.
|
|
718
|
+
*
|
|
719
|
+
* @param modelId - Model identifier (e.g., 'openai/gpt-4o', 'anthropic/claude-sonnet-4-5')
|
|
720
|
+
* @param usage - Token usage object
|
|
721
|
+
* @returns Estimated cost in USD
|
|
722
|
+
*
|
|
723
|
+
* @example
|
|
724
|
+
* ```typescript
|
|
725
|
+
* const cost = calculateModelCost('openai/gpt-4o', {
|
|
726
|
+
* inputTokens: 1000,
|
|
727
|
+
* outputTokens: 500
|
|
728
|
+
* });
|
|
729
|
+
* // Returns: 0.005 (estimated cost in USD)
|
|
730
|
+
* ```
|
|
731
|
+
*/
|
|
732
|
+
export const calculateModelCost = (
|
|
733
|
+
modelId: string,
|
|
734
|
+
usage: { inputTokens?: number; outputTokens?: number },
|
|
735
|
+
): number => {
|
|
736
|
+
const MODEL_COSTS: Record<string, { input: number; output: number }> = {
|
|
737
|
+
'openai/gpt-4o': { input: 0.0025, output: 0.01 },
|
|
738
|
+
'openai/gpt-4o-mini': { input: 0.00015, output: 0.0006 },
|
|
739
|
+
'openai/gpt-5.1-instant': { input: 0.0001, output: 0.0004 },
|
|
740
|
+
'anthropic/claude-sonnet-4-20250514': { input: 0.003, output: 0.015 },
|
|
741
|
+
'anthropic/claude-opus-4-5-20251101': { input: 0.015, output: 0.075 },
|
|
742
|
+
'perplexity/sonar': { input: 0.001, output: 0.001 },
|
|
743
|
+
// Legacy model names (without provider prefix)
|
|
744
|
+
'gpt-4o': { input: 0.0025, output: 0.01 },
|
|
745
|
+
'gpt-4o-mini': { input: 0.00015, output: 0.0006 },
|
|
746
|
+
'gpt-5.1-instant': { input: 0.0001, output: 0.0004 },
|
|
747
|
+
'claude-sonnet-4': { input: 0.003, output: 0.015 },
|
|
748
|
+
'claude-opus-4': { input: 0.015, output: 0.075 },
|
|
749
|
+
// Default fallback
|
|
750
|
+
default: { input: 0.001, output: 0.002 },
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const pricing = MODEL_COSTS[modelId] ?? MODEL_COSTS.default ?? { input: 0.001, output: 0.002 };
|
|
754
|
+
const inputTokens = usage.inputTokens ?? 0;
|
|
755
|
+
const outputTokens = usage.outputTokens ?? 0;
|
|
756
|
+
|
|
757
|
+
return (inputTokens / 1000) * pricing.input + (outputTokens / 1000) * pricing.output;
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Estimate input tokens from text.
|
|
762
|
+
*
|
|
763
|
+
* Uses rough approximation: ~4 characters per token for English text.
|
|
764
|
+
* This is a simple heuristic and may vary by language and content type.
|
|
765
|
+
*
|
|
766
|
+
* @param text - Text to estimate tokens for
|
|
767
|
+
* @returns Estimated number of input tokens
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```typescript
|
|
771
|
+
* estimateInputTokens('Hello world'); // Returns: 3 (12 chars / 4)
|
|
772
|
+
* ```
|
|
773
|
+
*/
|
|
774
|
+
export const estimateInputTokens = (text: string): number => Math.ceil(text.length / 4);
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Extract model name from provider-prefixed model ID.
|
|
778
|
+
*
|
|
779
|
+
* Removes provider prefix from model IDs like "openai/gpt-4o" or "anthropic/claude-3-5-sonnet".
|
|
780
|
+
* Handles both "provider/model" and "provider:model" formats.
|
|
781
|
+
*
|
|
782
|
+
* @param modelId - Model identifier (e.g., "openai/gpt-4o", "openai:gpt-4o", "gpt-4o")
|
|
783
|
+
* @returns Model name without provider prefix
|
|
784
|
+
*
|
|
785
|
+
* @example
|
|
786
|
+
* ```typescript
|
|
787
|
+
* extractModelName('openai/gpt-4o'); // Returns: 'gpt-4o'
|
|
788
|
+
* extractModelName('openai:gpt-4o'); // Returns: 'gpt-4o'
|
|
789
|
+
* extractModelName('gpt-4o'); // Returns: 'gpt-4o'
|
|
790
|
+
* ```
|
|
791
|
+
*/
|
|
792
|
+
export const extractModelName = (modelId: string): string => {
|
|
793
|
+
if (modelId.includes('/')) {
|
|
794
|
+
const parts = modelId.split('/');
|
|
795
|
+
return parts[parts.length - 1] ?? modelId;
|
|
796
|
+
}
|
|
797
|
+
if (modelId.includes(':')) {
|
|
798
|
+
const parts = modelId.split(':');
|
|
799
|
+
return parts[parts.length - 1] ?? modelId;
|
|
800
|
+
}
|
|
801
|
+
return modelId;
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Round cost to specified decimal places.
|
|
806
|
+
*
|
|
807
|
+
* @param cost - Cost value to round
|
|
808
|
+
* @param decimals - Number of decimal places (default: 6)
|
|
809
|
+
* @returns Rounded cost value
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* ```typescript
|
|
813
|
+
* roundCost(0.123456789); // Returns: 0.123457
|
|
814
|
+
* roundCost(0.123456789, 2); // Returns: 0.12
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
export const roundCost = (cost: number, decimals: number = 6): number =>
|
|
818
|
+
Math.round(cost * Math.pow(10, decimals)) / Math.pow(10, decimals);
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Truncate text to specified length with ellipsis.
|
|
822
|
+
*
|
|
823
|
+
* @param text - Text to truncate
|
|
824
|
+
* @param maxLength - Maximum length before truncation
|
|
825
|
+
* @param ellipsis - Ellipsis string (default: '...')
|
|
826
|
+
* @returns Truncated text
|
|
827
|
+
*
|
|
828
|
+
* @example
|
|
829
|
+
* ```typescript
|
|
830
|
+
* truncateText('Hello world', 8); // Returns: 'Hello...'
|
|
831
|
+
* truncateText('Short', 10); // Returns: 'Short'
|
|
832
|
+
* ```
|
|
833
|
+
*/
|
|
834
|
+
export const truncateText = (text: string, maxLength: number, ellipsis: string = '...'): string => {
|
|
835
|
+
if (text.length <= maxLength) return text;
|
|
836
|
+
return `${text.slice(0, maxLength - ellipsis.length)}${ellipsis}`;
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* Calculate elapsed duration in milliseconds from a start timestamp.
|
|
841
|
+
*
|
|
842
|
+
* @param startTime - Epoch milliseconds captured at the start of an operation
|
|
843
|
+
* @returns Milliseconds elapsed since `startTime`
|
|
844
|
+
*
|
|
845
|
+
* @example
|
|
846
|
+
* ```typescript
|
|
847
|
+
* const startedAt = Date.now();
|
|
848
|
+
* // ... work ...
|
|
849
|
+
* const durationMs = calculateDuration(startedAt);
|
|
850
|
+
* ```
|
|
851
|
+
*/
|
|
852
|
+
export const calculateDuration = (startTime: number): number => Date.now() - startTime;
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Extract telemetry data from an AI SDK stream result.
|
|
856
|
+
* Awaits text, usage, and reasoningText in parallel for efficiency.
|
|
857
|
+
*
|
|
858
|
+
* @param result - AI SDK stream result to extract from
|
|
859
|
+
* @returns Parsed text, usage, and optional reasoning text
|
|
860
|
+
*
|
|
861
|
+
* @example
|
|
862
|
+
* ```typescript
|
|
863
|
+
* const telemetry = await extractTelemetry(streamResult);
|
|
864
|
+
* console.log(telemetry.usage.totalTokens);
|
|
865
|
+
* ```
|
|
866
|
+
*/
|
|
867
|
+
export const extractTelemetry = async <TUsage extends SDKLanguageModelUsage>(
|
|
868
|
+
|
|
869
|
+
result: SDKStreamTextResult<any, any>,
|
|
870
|
+
): Promise<{ text: string; usage: TUsage; reasoningText: string | undefined }> => {
|
|
871
|
+
const [text, usage, reasoningText] = await Promise.all([
|
|
872
|
+
result.text,
|
|
873
|
+
result.usage,
|
|
874
|
+
result.reasoningText,
|
|
875
|
+
]);
|
|
876
|
+
|
|
877
|
+
return { text, usage: usage as TUsage, reasoningText };
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* Aggregate token usage from multiple messages or operations.
|
|
882
|
+
*
|
|
883
|
+
* Combines usage statistics from multiple sources, handling undefined/null values safely.
|
|
884
|
+
* Useful for tracking cumulative usage across a conversation or session.
|
|
885
|
+
*
|
|
886
|
+
* @param current - Current usage statistics
|
|
887
|
+
* @param addition - Usage to add to current
|
|
888
|
+
* @returns Aggregated usage statistics
|
|
889
|
+
*
|
|
890
|
+
* @example
|
|
891
|
+
* ```typescript
|
|
892
|
+
* const totalUsage = aggregateUsage(
|
|
893
|
+
* { inputTokens: 100, outputTokens: 50, totalTokens: 150 },
|
|
894
|
+
* { inputTokens: 200, outputTokens: 100, totalTokens: 300 }
|
|
895
|
+
* );
|
|
896
|
+
* // Returns: { inputTokens: 300, outputTokens: 150, totalTokens: 450, ... }
|
|
897
|
+
* ```
|
|
898
|
+
*/
|
|
899
|
+
export const aggregateUsage = (
|
|
900
|
+
current: Partial<SDKLanguageModelUsage>,
|
|
901
|
+
addition: Partial<SDKLanguageModelUsage>,
|
|
902
|
+
): SDKLanguageModelUsage => {
|
|
903
|
+
// Aggregate reasoning tokens (prefer outputTokenDetails, fallback to deprecated field)
|
|
904
|
+
const reasoningTokens =
|
|
905
|
+
(current.outputTokenDetails?.reasoningTokens ?? current.reasoningTokens ?? 0) +
|
|
906
|
+
(addition.outputTokenDetails?.reasoningTokens ?? addition.reasoningTokens ?? 0);
|
|
907
|
+
|
|
908
|
+
// Aggregate cache read tokens (prefer inputTokenDetails, fallback to deprecated field)
|
|
909
|
+
const cacheReadTokens =
|
|
910
|
+
(current.inputTokenDetails?.cacheReadTokens ?? current.cachedInputTokens ?? 0) +
|
|
911
|
+
(addition.inputTokenDetails?.cacheReadTokens ?? addition.cachedInputTokens ?? 0);
|
|
912
|
+
|
|
913
|
+
// Aggregate other input token details
|
|
914
|
+
const noCacheTokens =
|
|
915
|
+
(current.inputTokenDetails?.noCacheTokens ?? 0) +
|
|
916
|
+
(addition.inputTokenDetails?.noCacheTokens ?? 0);
|
|
917
|
+
|
|
918
|
+
const cacheWriteTokens =
|
|
919
|
+
(current.inputTokenDetails?.cacheWriteTokens ?? 0) +
|
|
920
|
+
(addition.inputTokenDetails?.cacheWriteTokens ?? 0);
|
|
921
|
+
|
|
922
|
+
// Aggregate text tokens from output details
|
|
923
|
+
const textTokens =
|
|
924
|
+
(current.outputTokenDetails?.textTokens ?? 0) + (addition.outputTokenDetails?.textTokens ?? 0);
|
|
925
|
+
|
|
926
|
+
return {
|
|
927
|
+
inputTokens: (current.inputTokens ?? 0) + (addition.inputTokens ?? 0),
|
|
928
|
+
outputTokens: (current.outputTokens ?? 0) + (addition.outputTokens ?? 0),
|
|
929
|
+
totalTokens: (current.totalTokens ?? 0) + (addition.totalTokens ?? 0),
|
|
930
|
+
// Deprecated fields for backwards compatibility
|
|
931
|
+
reasoningTokens,
|
|
932
|
+
cachedInputTokens: cacheReadTokens,
|
|
933
|
+
// AI SDK v6 detailed token info
|
|
934
|
+
inputTokenDetails: {
|
|
935
|
+
noCacheTokens: noCacheTokens || undefined,
|
|
936
|
+
cacheReadTokens: cacheReadTokens || undefined,
|
|
937
|
+
cacheWriteTokens: cacheWriteTokens || undefined,
|
|
938
|
+
},
|
|
939
|
+
outputTokenDetails: {
|
|
940
|
+
textTokens: textTokens || undefined,
|
|
941
|
+
reasoningTokens: reasoningTokens || undefined,
|
|
942
|
+
},
|
|
943
|
+
};
|
|
944
|
+
};
|