@mastra/core 0.13.2 → 0.14.0-alpha.1
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 +5 -44
- package/dist/agent/agent.types.d.ts +12 -2
- package/dist/agent/agent.types.d.ts.map +1 -1
- package/dist/agent/index.cjs +14 -14
- package/dist/agent/index.d.ts +17 -17
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +2 -2
- package/dist/agent/input-processor/index.cjs +28 -0
- package/dist/agent/input-processor/index.js +3 -0
- package/dist/agent/input-processor/processors/index.d.ts +4 -4
- package/dist/agent/input-processor/processors/index.d.ts.map +1 -1
- package/dist/agent/input-processor/processors/language-detector.d.ts +6 -139
- package/dist/agent/input-processor/processors/language-detector.d.ts.map +1 -1
- package/dist/agent/input-processor/processors/moderation.d.ts +5 -94
- package/dist/agent/input-processor/processors/moderation.d.ts.map +1 -1
- package/dist/agent/input-processor/processors/pii-detector.d.ts +6 -164
- package/dist/agent/input-processor/processors/pii-detector.d.ts.map +1 -1
- package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts +6 -96
- package/dist/agent/input-processor/processors/prompt-injection-detector.d.ts.map +1 -1
- package/dist/agent/input-processor/processors/unicode-normalizer.d.ts +9 -25
- package/dist/agent/input-processor/processors/unicode-normalizer.d.ts.map +1 -1
- package/dist/agent/message-list/index.d.ts +138 -39
- package/dist/agent/message-list/index.d.ts.map +1 -1
- package/dist/agent/message-list/types.d.ts +3 -0
- package/dist/agent/message-list/types.d.ts.map +1 -0
- package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts +3 -0
- package/dist/agent/message-list/utils/ai-v4-v5/core-model-message.d.ts.map +1 -0
- package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts +3 -0
- package/dist/agent/message-list/utils/ai-v4-v5/ui-message.d.ts.map +1 -0
- package/dist/agent/message-list/utils/ai-v5/tool.d.ts +16 -0
- package/dist/agent/message-list/utils/ai-v5/tool.d.ts.map +1 -0
- package/dist/agent/types.d.ts +34 -4
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/ai-tracing/base.d.ts +22 -7
- package/dist/ai-tracing/base.d.ts.map +1 -1
- package/dist/ai-tracing/default.d.ts +3 -3
- package/dist/ai-tracing/default.d.ts.map +1 -1
- package/dist/ai-tracing/index.cjs +253 -72
- package/dist/ai-tracing/index.cjs.map +1 -1
- package/dist/ai-tracing/index.d.ts +1 -0
- package/dist/ai-tracing/index.d.ts.map +1 -1
- package/dist/ai-tracing/index.js +243 -71
- package/dist/ai-tracing/index.js.map +1 -1
- package/dist/ai-tracing/no-op.d.ts +39 -7
- package/dist/ai-tracing/no-op.d.ts.map +1 -1
- package/dist/ai-tracing/registry.d.ts +29 -4
- package/dist/ai-tracing/registry.d.ts.map +1 -1
- package/dist/ai-tracing/types.d.ts +112 -53
- package/dist/ai-tracing/types.d.ts.map +1 -1
- package/dist/ai-tracing/utils.d.ts +24 -0
- package/dist/ai-tracing/utils.d.ts.map +1 -0
- package/dist/base.cjs +2 -2
- package/dist/base.js +1 -1
- package/dist/bundler/index.cjs +2 -2
- package/dist/bundler/index.js +1 -1
- package/dist/{chunk-N3VGOJZV.cjs → chunk-4BMFOQDC.cjs} +27 -47
- package/dist/chunk-4BMFOQDC.cjs.map +1 -0
- package/dist/{chunk-MTRRRTB4.js → chunk-4XDSHUFK.js} +7 -6
- package/dist/chunk-4XDSHUFK.js.map +1 -0
- package/dist/{chunk-SSJVFUBZ.js → chunk-67L5DRLO.js} +3 -3
- package/dist/{chunk-SSJVFUBZ.js.map → chunk-67L5DRLO.js.map} +1 -1
- package/dist/{chunk-FUERFM46.js → chunk-6GF5M4GX.js} +3 -3
- package/dist/{chunk-FUERFM46.js.map → chunk-6GF5M4GX.js.map} +1 -1
- package/dist/{chunk-7E2SNI5D.cjs → chunk-7AXX55C5.cjs} +5 -5
- package/dist/chunk-7AXX55C5.cjs.map +1 -0
- package/dist/{chunk-6W6LYR7M.js → chunk-7XGDFDZ2.js} +3 -3
- package/dist/{chunk-6W6LYR7M.js.map → chunk-7XGDFDZ2.js.map} +1 -1
- package/dist/{chunk-R4HT5XUH.cjs → chunk-ASJVWHIK.cjs} +993 -124
- package/dist/chunk-ASJVWHIK.cjs.map +1 -0
- package/dist/{chunk-ZDZ57MIU.js → chunk-AWILTHBM.js} +3 -3
- package/dist/{chunk-ZDZ57MIU.js.map → chunk-AWILTHBM.js.map} +1 -1
- package/dist/{chunk-DDYSOZ25.js → chunk-AYXRNQH3.js} +3 -3
- package/dist/{chunk-DDYSOZ25.js.map → chunk-AYXRNQH3.js.map} +1 -1
- package/dist/{chunk-U2CK6AN5.cjs → chunk-BCCEYWAV.cjs} +4 -4
- package/dist/{chunk-U2CK6AN5.cjs.map → chunk-BCCEYWAV.cjs.map} +1 -1
- package/dist/{chunk-MH64VYGF.cjs → chunk-CSTWQQ3C.cjs} +14 -13
- package/dist/chunk-CSTWQQ3C.cjs.map +1 -0
- package/dist/{chunk-FTPL7ILZ.js → chunk-CWZDKGJ6.js} +3 -3
- package/dist/{chunk-FTPL7ILZ.js.map → chunk-CWZDKGJ6.js.map} +1 -1
- package/dist/{chunk-MVSUZXER.cjs → chunk-D7P76YH6.cjs} +4 -4
- package/dist/{chunk-MVSUZXER.cjs.map → chunk-D7P76YH6.cjs.map} +1 -1
- package/dist/{chunk-IL5SHDY4.js → chunk-DAMIFZPW.js} +949 -103
- package/dist/chunk-DAMIFZPW.js.map +1 -0
- package/dist/{chunk-KLXRYVVQ.cjs → chunk-DVI6XHUC.cjs} +4 -4
- package/dist/{chunk-KLXRYVVQ.cjs.map → chunk-DVI6XHUC.cjs.map} +1 -1
- package/dist/chunk-ELKY3FEM.cjs +14 -0
- package/dist/{chunk-IP5NGA2S.cjs.map → chunk-ELKY3FEM.cjs.map} +1 -1
- package/dist/{chunk-TOODGJKM.js → chunk-GZVSHXPP.js} +3 -3
- package/dist/chunk-GZVSHXPP.js.map +1 -0
- package/dist/{chunk-WA3SWCXD.cjs → chunk-I4XP6RAN.cjs} +9 -9
- package/dist/{chunk-WA3SWCXD.cjs.map → chunk-I4XP6RAN.cjs.map} +1 -1
- package/dist/{chunk-PLVSXEQP.cjs → chunk-IAP4IWKM.cjs} +4 -4
- package/dist/{chunk-PLVSXEQP.cjs.map → chunk-IAP4IWKM.cjs.map} +1 -1
- package/dist/{chunk-YT4RGZYO.cjs → chunk-KXCUCBEI.cjs} +8 -8
- package/dist/{chunk-YT4RGZYO.cjs.map → chunk-KXCUCBEI.cjs.map} +1 -1
- package/dist/{chunk-Z74LG5VH.cjs → chunk-LFAAEOQC.cjs} +21 -4
- package/dist/chunk-LFAAEOQC.cjs.map +1 -0
- package/dist/{chunk-DK7AVDMI.cjs → chunk-LVVAUDVQ.cjs} +4 -4
- package/dist/{chunk-DK7AVDMI.cjs.map → chunk-LVVAUDVQ.cjs.map} +1 -1
- package/dist/{chunk-HBMZEZTO.js → chunk-ON4S33NP.js} +3 -3
- package/dist/{chunk-HBMZEZTO.js.map → chunk-ON4S33NP.js.map} +1 -1
- package/dist/{chunk-L6YBPFYF.js → chunk-OPT2L5AM.js} +1089 -81
- package/dist/chunk-OPT2L5AM.js.map +1 -0
- package/dist/{chunk-N2KMAW6T.js → chunk-P7WF6NQU.js} +20 -4
- package/dist/chunk-P7WF6NQU.js.map +1 -0
- package/dist/{chunk-KEMW2BER.js → chunk-PY4MHJTE.js} +3 -3
- package/dist/{chunk-KEMW2BER.js.map → chunk-PY4MHJTE.js.map} +1 -1
- package/dist/{chunk-TZVJV7EV.js → chunk-QSJZLHWA.js} +5 -5
- package/dist/{chunk-TZVJV7EV.js.map → chunk-QSJZLHWA.js.map} +1 -1
- package/dist/{chunk-DCOKWJ5G.cjs → chunk-RJCNC57P.cjs} +1110 -79
- package/dist/chunk-RJCNC57P.cjs.map +1 -0
- package/dist/{chunk-LMW44O4V.cjs → chunk-RPV7GQAX.cjs} +4 -4
- package/dist/{chunk-LMW44O4V.cjs.map → chunk-RPV7GQAX.cjs.map} +1 -1
- package/dist/{chunk-TZOR5M7H.js → chunk-RYCRCJCY.js} +25 -45
- package/dist/chunk-RYCRCJCY.js.map +1 -0
- package/dist/{chunk-IDDUQR6P.cjs → chunk-V5WKCX3G.cjs} +3 -3
- package/dist/chunk-V5WKCX3G.cjs.map +1 -0
- package/dist/{chunk-6AR2Z5ZG.js → chunk-X3GXU6TZ.js} +3 -3
- package/dist/chunk-X3GXU6TZ.js.map +1 -0
- package/dist/deployer/index.cjs +2 -2
- package/dist/deployer/index.js +1 -1
- package/dist/index.cjs +74 -70
- package/dist/index.js +14 -14
- package/dist/llm/index.d.ts +2 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/model/base.types.d.ts +2 -6
- package/dist/llm/model/base.types.d.ts.map +1 -1
- package/dist/llm/model/index.d.ts +0 -1
- package/dist/llm/model/index.d.ts.map +1 -1
- package/dist/llm/model/model.d.ts +3 -2
- package/dist/llm/model/model.d.ts.map +1 -1
- package/dist/llm/model/model.loop.d.ts +25 -0
- package/dist/llm/model/model.loop.d.ts.map +1 -0
- package/dist/llm/model/model.loop.types.d.ts +39 -0
- package/dist/llm/model/model.loop.types.d.ts.map +1 -0
- package/dist/llm/model/shared.types.d.ts +8 -0
- package/dist/llm/model/shared.types.d.ts.map +1 -0
- package/dist/logger/constants.d.ts +1 -1
- package/dist/logger/index.cjs +6 -6
- package/dist/logger/index.js +1 -1
- package/dist/loop/index.cjs +2848 -0
- package/dist/loop/index.cjs.map +1 -0
- package/dist/loop/index.d.ts +2 -0
- package/dist/loop/index.d.ts.map +1 -0
- package/dist/loop/index.js +2842 -0
- package/dist/loop/index.js.map +1 -0
- package/dist/loop/loop.d.ts +5 -0
- package/dist/loop/loop.d.ts.map +1 -0
- package/dist/loop/telemetry/index.d.ts +36 -0
- package/dist/loop/telemetry/index.d.ts.map +1 -0
- package/dist/loop/telemetry/noop.d.ts +3 -0
- package/dist/loop/telemetry/noop.d.ts.map +1 -0
- package/dist/loop/test-utils/fullStream.d.ts +6 -0
- package/dist/loop/test-utils/fullStream.d.ts.map +1 -0
- package/dist/loop/test-utils/generateText.d.ts +6 -0
- package/dist/loop/test-utils/generateText.d.ts.map +1 -0
- package/dist/loop/test-utils/mockTracer.d.ts +47 -0
- package/dist/loop/test-utils/mockTracer.d.ts.map +1 -0
- package/dist/loop/test-utils/options.d.ts +6 -0
- package/dist/loop/test-utils/options.d.ts.map +1 -0
- package/dist/loop/test-utils/resultObject.d.ts +6 -0
- package/dist/loop/test-utils/resultObject.d.ts.map +1 -0
- package/dist/loop/test-utils/streamObject.d.ts +6 -0
- package/dist/loop/test-utils/streamObject.d.ts.map +1 -0
- package/dist/loop/test-utils/telemetry.d.ts +6 -0
- package/dist/loop/test-utils/telemetry.d.ts.map +1 -0
- package/dist/loop/test-utils/textStream.d.ts +6 -0
- package/dist/loop/test-utils/textStream.d.ts.map +1 -0
- package/dist/loop/test-utils/toUIMessageStream.d.ts +6 -0
- package/dist/loop/test-utils/toUIMessageStream.d.ts.map +1 -0
- package/dist/loop/test-utils/tools.d.ts +6 -0
- package/dist/loop/test-utils/tools.d.ts.map +1 -0
- package/dist/loop/test-utils/utils.d.ts +40 -0
- package/dist/loop/test-utils/utils.d.ts.map +1 -0
- package/dist/loop/types.d.ts +68 -0
- package/dist/loop/types.d.ts.map +1 -0
- package/dist/loop/workflow/llm-execution.d.ts +80 -0
- package/dist/loop/workflow/llm-execution.d.ts.map +1 -0
- package/dist/loop/workflow/outer-llm-step.d.ts +43 -0
- package/dist/loop/workflow/outer-llm-step.d.ts.map +1 -0
- package/dist/loop/workflow/run-state.d.ts +24 -0
- package/dist/loop/workflow/run-state.d.ts.map +1 -0
- package/dist/loop/workflow/schema.d.ts +80 -0
- package/dist/loop/workflow/schema.d.ts.map +1 -0
- package/dist/loop/workflow/stream.d.ts +6 -0
- package/dist/loop/workflow/stream.d.ts.map +1 -0
- package/dist/loop/workflow/tool-call-step.d.ts +41 -0
- package/dist/loop/workflow/tool-call-step.d.ts.map +1 -0
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.d.ts +4 -5
- package/dist/mastra/index.d.ts.map +1 -1
- package/dist/mastra/index.js +1 -1
- package/dist/mcp/index.cjs +4 -4
- package/dist/mcp/index.js +2 -2
- package/dist/memory/index.cjs +4 -4
- package/dist/memory/index.js +1 -1
- package/dist/network/index.cjs +6 -6
- package/dist/network/index.cjs.map +1 -1
- package/dist/network/index.js +3 -3
- package/dist/network/index.js.map +1 -1
- package/dist/network/network.d.ts +8 -7
- package/dist/network/network.d.ts.map +1 -1
- package/dist/network/vNext/index.cjs +21 -21
- package/dist/network/vNext/index.cjs.map +1 -1
- package/dist/network/vNext/index.d.ts +7 -7
- package/dist/network/vNext/index.d.ts.map +1 -1
- package/dist/network/vNext/index.js +7 -7
- package/dist/network/vNext/index.js.map +1 -1
- package/dist/processors/index.cjs +512 -0
- package/dist/processors/index.cjs.map +1 -0
- package/dist/processors/index.d.ts +38 -0
- package/dist/processors/index.d.ts.map +1 -0
- package/dist/processors/index.js +481 -0
- package/dist/processors/index.js.map +1 -0
- package/dist/processors/processors/batch-parts.d.ts +42 -0
- package/dist/processors/processors/batch-parts.d.ts.map +1 -0
- package/dist/processors/processors/index.d.ts +10 -0
- package/dist/processors/processors/index.d.ts.map +1 -0
- package/dist/processors/processors/language-detector.d.ts +151 -0
- package/dist/processors/processors/language-detector.d.ts.map +1 -0
- package/dist/processors/processors/moderation.d.ts +130 -0
- package/dist/processors/processors/moderation.d.ts.map +1 -0
- package/dist/processors/processors/pii-detector.d.ts +197 -0
- package/dist/processors/processors/pii-detector.d.ts.map +1 -0
- package/dist/processors/processors/prompt-injection-detector.d.ts +108 -0
- package/dist/processors/processors/prompt-injection-detector.d.ts.map +1 -0
- package/dist/processors/processors/structured-output.d.ts +42 -0
- package/dist/processors/processors/structured-output.d.ts.map +1 -0
- package/dist/processors/processors/system-prompt-scrubber.d.ts +92 -0
- package/dist/processors/processors/system-prompt-scrubber.d.ts.map +1 -0
- package/dist/processors/processors/token-limiter.d.ts +66 -0
- package/dist/processors/processors/token-limiter.d.ts.map +1 -0
- package/dist/processors/processors/unicode-normalizer.d.ts +34 -0
- package/dist/processors/processors/unicode-normalizer.d.ts.map +1 -0
- package/dist/processors/runner.d.ts +41 -0
- package/dist/processors/runner.d.ts.map +1 -0
- package/dist/relevance/index.cjs +4 -4
- package/dist/relevance/index.js +1 -1
- package/dist/scores/index.cjs +5 -5
- package/dist/scores/index.js +2 -2
- package/dist/server/index.cjs +2 -2
- package/dist/server/index.js +1 -1
- package/dist/storage/domains/operations/base.d.ts.map +1 -1
- package/dist/storage/index.cjs +15 -17
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.js +4 -6
- package/dist/storage/index.js.map +1 -1
- package/dist/stream/aisdk/v4/transform.d.ts +26 -0
- package/dist/stream/aisdk/v4/transform.d.ts.map +1 -1
- package/dist/stream/aisdk/v5/compat.d.ts +159 -0
- package/dist/stream/aisdk/v5/compat.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/execute.d.ts +29 -0
- package/dist/stream/aisdk/v5/execute.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/file.d.ts +38 -0
- package/dist/stream/aisdk/v5/file.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/input.d.ts +15 -0
- package/dist/stream/aisdk/v5/input.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/object/schema.d.ts +4 -0
- package/dist/stream/aisdk/v5/object/schema.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/object/stream-object.d.ts +34 -0
- package/dist/stream/aisdk/v5/object/stream-object.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/output-helpers.d.ts +76 -0
- package/dist/stream/aisdk/v5/output-helpers.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/output.d.ts +119 -0
- package/dist/stream/aisdk/v5/output.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/test-utils.d.ts +19 -0
- package/dist/stream/aisdk/v5/test-utils.d.ts.map +1 -0
- package/dist/stream/aisdk/v5/transform.d.ts +31 -0
- package/dist/stream/aisdk/v5/transform.d.ts.map +1 -0
- package/dist/stream/base/input.d.ts +1 -1
- package/dist/stream/base/output.d.ts +89 -0
- package/dist/stream/base/output.d.ts.map +1 -0
- package/dist/stream/types.d.ts +30 -0
- package/dist/stream/types.d.ts.map +1 -1
- package/dist/test-utils/llm-mock.cjs +2 -2
- package/dist/test-utils/llm-mock.cjs.map +1 -1
- package/dist/test-utils/llm-mock.d.ts +2 -2
- package/dist/test-utils/llm-mock.d.ts.map +1 -1
- package/dist/test-utils/llm-mock.js +2 -2
- package/dist/test-utils/llm-mock.js.map +1 -1
- package/dist/tools/tool-builder/builder.d.ts +3 -2
- package/dist/tools/tool-builder/builder.d.ts.map +1 -1
- package/dist/tools/types.d.ts +9 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tts/index.cjs +2 -2
- package/dist/tts/index.js +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.cjs +19 -15
- package/dist/utils.d.ts +3 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/vector/index.cjs +2 -2
- package/dist/vector/index.js +1 -1
- package/dist/voice/index.cjs +4 -4
- package/dist/voice/index.js +1 -1
- package/dist/workflows/default.d.ts.map +1 -1
- package/dist/workflows/index.cjs +10 -10
- package/dist/workflows/index.js +1 -1
- package/dist/workflows/legacy/index.cjs +22 -22
- package/dist/workflows/legacy/index.js +1 -1
- package/loop.d.ts +1 -0
- package/package.json +16 -1
- package/processors.d.ts +1 -0
- package/dist/agent/input-processor/processors/index.cjs +0 -28
- package/dist/agent/input-processor/processors/index.js +0 -3
- package/dist/agent/input-processor/runner.d.ts +0 -4
- package/dist/agent/input-processor/runner.d.ts.map +0 -1
- package/dist/chunk-6AR2Z5ZG.js.map +0 -1
- package/dist/chunk-7E2SNI5D.cjs.map +0 -1
- package/dist/chunk-DCOKWJ5G.cjs.map +0 -1
- package/dist/chunk-IDDUQR6P.cjs.map +0 -1
- package/dist/chunk-IL5SHDY4.js.map +0 -1
- package/dist/chunk-IP5NGA2S.cjs +0 -14
- package/dist/chunk-L6YBPFYF.js.map +0 -1
- package/dist/chunk-MH64VYGF.cjs.map +0 -1
- package/dist/chunk-MTRRRTB4.js.map +0 -1
- package/dist/chunk-N2KMAW6T.js.map +0 -1
- package/dist/chunk-N3VGOJZV.cjs.map +0 -1
- package/dist/chunk-R4HT5XUH.cjs.map +0 -1
- package/dist/chunk-TOODGJKM.js.map +0 -1
- package/dist/chunk-TZOR5M7H.js.map +0 -1
- package/dist/chunk-Z74LG5VH.cjs.map +0 -1
- package/dist/llm/model/base.d.ts +0 -26
- package/dist/llm/model/base.d.ts.map +0 -1
- /package/dist/agent/input-processor/{processors/index.cjs.map → index.cjs.map} +0 -0
- /package/dist/agent/input-processor/{processors/index.js.map → index.js.map} +0 -0
|
@@ -1,10 +1,42 @@
|
|
|
1
|
-
import { isUiMessage, isCoreMessage } from './chunk-N2KMAW6T.js';
|
|
2
1
|
import { MastraError } from './chunk-4O4YNORN.js';
|
|
3
2
|
import { randomUUID } from 'crypto';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
3
|
+
import * as AIV4 from 'ai';
|
|
4
|
+
import * as AIV5 from 'ai-v5';
|
|
5
|
+
import { convertUint8ArrayToBase64, convertBase64ToUint8Array } from '@ai-sdk/provider-utils';
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
|
|
8
|
+
var DefaultGeneratedFile = class {
|
|
9
|
+
base64Data;
|
|
10
|
+
uint8ArrayData;
|
|
11
|
+
mediaType;
|
|
12
|
+
constructor({ data, mediaType }) {
|
|
13
|
+
const isUint8Array = data instanceof Uint8Array;
|
|
14
|
+
this.base64Data = isUint8Array ? void 0 : data;
|
|
15
|
+
this.uint8ArrayData = isUint8Array ? data : void 0;
|
|
16
|
+
this.mediaType = mediaType;
|
|
17
|
+
}
|
|
18
|
+
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
|
19
|
+
get base64() {
|
|
20
|
+
if (this.base64Data == null) {
|
|
21
|
+
this.base64Data = convertUint8ArrayToBase64(this.uint8ArrayData);
|
|
22
|
+
}
|
|
23
|
+
return this.base64Data;
|
|
24
|
+
}
|
|
25
|
+
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
|
26
|
+
get uint8Array() {
|
|
27
|
+
if (this.uint8ArrayData == null) {
|
|
28
|
+
this.uint8ArrayData = convertBase64ToUint8Array(this.base64Data);
|
|
29
|
+
}
|
|
30
|
+
return this.uint8ArrayData;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var DefaultGeneratedFileWithType = class extends DefaultGeneratedFile {
|
|
34
|
+
type = "file";
|
|
35
|
+
constructor(options) {
|
|
36
|
+
super(options);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
8
40
|
// src/agent/message-list/prompt/attachments-to-parts.ts
|
|
9
41
|
function attachmentsToParts(attachments) {
|
|
10
42
|
const parts = [];
|
|
@@ -383,6 +415,23 @@ function convertDataContentToBase64String(content) {
|
|
|
383
415
|
return convertUint8ArrayToBase64(content);
|
|
384
416
|
}
|
|
385
417
|
|
|
418
|
+
// src/agent/message-list/utils/ai-v5/tool.ts
|
|
419
|
+
function getToolName(type) {
|
|
420
|
+
if (typeof type === "object" && type && "type" in type) {
|
|
421
|
+
type = type.type;
|
|
422
|
+
}
|
|
423
|
+
if (typeof type !== "string") {
|
|
424
|
+
return "unknown";
|
|
425
|
+
}
|
|
426
|
+
if (type === "dynamic-tool") {
|
|
427
|
+
return "dynamic-tool";
|
|
428
|
+
}
|
|
429
|
+
if (type.startsWith("tool-")) {
|
|
430
|
+
return type.slice("tool-".length);
|
|
431
|
+
}
|
|
432
|
+
return type;
|
|
433
|
+
}
|
|
434
|
+
|
|
386
435
|
// src/agent/message-list/index.ts
|
|
387
436
|
var MessageList = class _MessageList {
|
|
388
437
|
messages = [];
|
|
@@ -416,6 +465,7 @@ var MessageList = class _MessageList {
|
|
|
416
465
|
this._agentNetworkAppend = _agentNetworkAppend || false;
|
|
417
466
|
}
|
|
418
467
|
add(messages, messageSource) {
|
|
468
|
+
if (messageSource === `user`) messageSource = `input`;
|
|
419
469
|
if (!messages) return this;
|
|
420
470
|
for (const message of Array.isArray(messages) ? messages : [messages]) {
|
|
421
471
|
this.addOne(
|
|
@@ -459,58 +509,216 @@ var MessageList = class _MessageList {
|
|
|
459
509
|
this.newUserMessages.clear();
|
|
460
510
|
return userMessages;
|
|
461
511
|
}
|
|
512
|
+
},
|
|
513
|
+
response: {
|
|
514
|
+
v2: () => {
|
|
515
|
+
const responseMessages = Array.from(this.newResponseMessages);
|
|
516
|
+
this.messages = this.messages.filter((m) => !this.newResponseMessages.has(m));
|
|
517
|
+
this.newResponseMessages.clear();
|
|
518
|
+
return responseMessages;
|
|
519
|
+
}
|
|
462
520
|
}
|
|
463
521
|
};
|
|
464
522
|
}
|
|
465
523
|
all = {
|
|
524
|
+
v3: () => this.cleanV3Metadata(this.messages.map(this.mastraMessageV2ToMastraMessageV3)),
|
|
466
525
|
v2: () => this.messages,
|
|
467
|
-
v1: () => convertToV1Messages(this.
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
526
|
+
v1: () => convertToV1Messages(this.all.v2()),
|
|
527
|
+
aiV5: {
|
|
528
|
+
model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.all.aiV5.ui()),
|
|
529
|
+
ui: () => this.all.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage),
|
|
530
|
+
// Used when calling AI SDK streamText/generateText
|
|
531
|
+
prompt: () => {
|
|
532
|
+
const messages = [
|
|
533
|
+
...this.aiV4CoreMessagesToAIV5ModelMessages(
|
|
534
|
+
[...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()],
|
|
535
|
+
`system`
|
|
536
|
+
),
|
|
537
|
+
...this.all.aiV5.model()
|
|
538
|
+
];
|
|
539
|
+
const needsDefaultUserMessage = !messages.length || messages[0]?.role === "assistant";
|
|
540
|
+
if (needsDefaultUserMessage) {
|
|
541
|
+
const defaultMessage = {
|
|
542
|
+
role: "user",
|
|
543
|
+
content: " "
|
|
544
|
+
};
|
|
545
|
+
messages.unshift(defaultMessage);
|
|
546
|
+
}
|
|
547
|
+
return messages;
|
|
548
|
+
},
|
|
549
|
+
// Used for creating LLM prompt messages without AI SDK streamText/generateText
|
|
550
|
+
llmPrompt: () => {
|
|
551
|
+
const modelMessages = this.all.aiV5.model();
|
|
552
|
+
const systemMessages = this.aiV4CoreMessagesToAIV5ModelMessages(
|
|
553
|
+
[...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()],
|
|
554
|
+
`system`
|
|
555
|
+
);
|
|
556
|
+
const messages = [...systemMessages, ...modelMessages];
|
|
557
|
+
const needsDefaultUserMessage = !messages.length || messages[0]?.role === "assistant";
|
|
558
|
+
if (needsDefaultUserMessage) {
|
|
559
|
+
const defaultMessage = {
|
|
560
|
+
role: "user",
|
|
561
|
+
content: " "
|
|
562
|
+
};
|
|
563
|
+
messages.unshift(defaultMessage);
|
|
564
|
+
}
|
|
565
|
+
return messages.map(_MessageList.aiV5ModelMessageToV2PromptMessage);
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
/* @deprecated use list.get.all.aiV4.prompt() instead */
|
|
569
|
+
prompt: () => this.all.aiV4.prompt(),
|
|
570
|
+
/* @deprecated use list.get.all.aiV4.ui() */
|
|
571
|
+
ui: () => this.all.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
572
|
+
/* @deprecated use list.get.all.aiV4.core() */
|
|
573
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
|
|
574
|
+
aiV4: {
|
|
575
|
+
ui: () => this.all.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
576
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
|
|
577
|
+
// Used when calling AI SDK streamText/generateText
|
|
578
|
+
prompt: () => {
|
|
579
|
+
const coreMessages = this.all.aiV4.core();
|
|
580
|
+
const messages = [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat(), ...coreMessages];
|
|
581
|
+
const needsDefaultUserMessage = !messages.length || messages[0]?.role === "assistant";
|
|
582
|
+
if (needsDefaultUserMessage) {
|
|
583
|
+
const defaultMessage = {
|
|
584
|
+
role: "user",
|
|
585
|
+
content: " "
|
|
586
|
+
};
|
|
587
|
+
messages.unshift(defaultMessage);
|
|
588
|
+
}
|
|
589
|
+
return messages;
|
|
590
|
+
},
|
|
591
|
+
// Used for creating LLM prompt messages without AI SDK streamText/generateText
|
|
592
|
+
llmPrompt: () => {
|
|
593
|
+
const coreMessages = this.all.aiV4.core();
|
|
594
|
+
const systemMessages = [...this.systemMessages, ...Object.values(this.taggedSystemMessages).flat()];
|
|
595
|
+
const messages = [...systemMessages, ...coreMessages];
|
|
596
|
+
const needsDefaultUserMessage = !messages.length || messages[0]?.role === "assistant";
|
|
597
|
+
if (needsDefaultUserMessage) {
|
|
598
|
+
const defaultMessage = {
|
|
599
|
+
role: "user",
|
|
600
|
+
content: " "
|
|
601
|
+
};
|
|
602
|
+
messages.unshift(defaultMessage);
|
|
603
|
+
}
|
|
604
|
+
return messages.map(_MessageList.aiV4CoreMessageToV1PromptMessage);
|
|
480
605
|
}
|
|
481
|
-
return messages;
|
|
482
606
|
}
|
|
483
607
|
};
|
|
484
608
|
remembered = {
|
|
609
|
+
v3: () => this.remembered.v2().map(this.mastraMessageV2ToMastraMessageV3),
|
|
485
610
|
v2: () => this.messages.filter((m) => this.memoryMessages.has(m)),
|
|
486
611
|
v1: () => convertToV1Messages(this.remembered.v2()),
|
|
487
|
-
|
|
488
|
-
|
|
612
|
+
aiV5: {
|
|
613
|
+
model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.remembered.aiV5.ui()),
|
|
614
|
+
ui: () => this.remembered.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage)
|
|
615
|
+
},
|
|
616
|
+
/* @deprecated use list.get.remembered.aiV4.ui() */
|
|
617
|
+
ui: () => this.remembered.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
618
|
+
/* @deprecated use list.get.remembered.aiV4.core() */
|
|
619
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui()),
|
|
620
|
+
aiV4: {
|
|
621
|
+
ui: () => this.remembered.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
622
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.all.aiV4.ui())
|
|
623
|
+
}
|
|
489
624
|
};
|
|
625
|
+
// TODO: need to update this for new .aiV4/5.x() pattern
|
|
490
626
|
rememberedPersisted = {
|
|
491
|
-
v2: () => this.
|
|
627
|
+
v2: () => this.all.v2().filter((m) => this.memoryMessagesPersisted.has(m)),
|
|
492
628
|
v1: () => convertToV1Messages(this.rememberedPersisted.v2()),
|
|
493
|
-
ui: () => this.rememberedPersisted.v2().map(_MessageList.
|
|
494
|
-
core: () => this.
|
|
629
|
+
ui: () => this.rememberedPersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
630
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.rememberedPersisted.ui())
|
|
495
631
|
};
|
|
496
632
|
input = {
|
|
633
|
+
v3: () => this.cleanV3Metadata(
|
|
634
|
+
this.messages.filter((m) => this.newUserMessages.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
|
|
635
|
+
),
|
|
497
636
|
v2: () => this.messages.filter((m) => this.newUserMessages.has(m)),
|
|
498
637
|
v1: () => convertToV1Messages(this.input.v2()),
|
|
499
|
-
|
|
500
|
-
|
|
638
|
+
aiV5: {
|
|
639
|
+
model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.input.aiV5.ui()),
|
|
640
|
+
ui: () => this.input.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage)
|
|
641
|
+
},
|
|
642
|
+
/* @deprecated use list.get.input.aiV4.ui() instead */
|
|
643
|
+
ui: () => this.input.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
644
|
+
/* @deprecated use list.get.core.aiV4.ui() instead */
|
|
645
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.input.ui()),
|
|
646
|
+
aiV4: {
|
|
647
|
+
ui: () => this.input.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
648
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.input.aiV4.ui())
|
|
649
|
+
}
|
|
501
650
|
};
|
|
651
|
+
// TODO: need to update this for new .aiV4/5.x() pattern
|
|
502
652
|
inputPersisted = {
|
|
653
|
+
v3: () => this.cleanV3Metadata(
|
|
654
|
+
this.messages.filter((m) => this.newUserMessagesPersisted.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
|
|
655
|
+
),
|
|
503
656
|
v2: () => this.messages.filter((m) => this.newUserMessagesPersisted.has(m)),
|
|
504
657
|
v1: () => convertToV1Messages(this.inputPersisted.v2()),
|
|
505
|
-
ui: () => this.inputPersisted.v2().map(_MessageList.
|
|
506
|
-
core: () => this.
|
|
658
|
+
ui: () => this.inputPersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
659
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.inputPersisted.ui())
|
|
507
660
|
};
|
|
508
661
|
response = {
|
|
509
|
-
|
|
662
|
+
v3: () => this.response.v2().map(this.mastraMessageV2ToMastraMessageV3),
|
|
663
|
+
v2: () => this.messages.filter((m) => this.newResponseMessages.has(m)),
|
|
664
|
+
v1: () => convertToV1Messages(this.response.v3().map(_MessageList.mastraMessageV3ToV2)),
|
|
665
|
+
aiV5: {
|
|
666
|
+
ui: () => this.response.v3().map(_MessageList.mastraMessageV3ToAIV5UIMessage),
|
|
667
|
+
model: () => this.aiV5UIMessagesToAIV5ModelMessages(this.response.aiV5.ui()).filter(
|
|
668
|
+
(m) => m.role === `tool` || m.role === `assistant`
|
|
669
|
+
),
|
|
670
|
+
modelContent: () => {
|
|
671
|
+
return this.response.aiV5.model().map(this.response.aiV5.stepContent).flat();
|
|
672
|
+
},
|
|
673
|
+
stepContent: (message) => {
|
|
674
|
+
const latest = message ? message : this.response.aiV5.model().at(-1);
|
|
675
|
+
if (!latest) return [];
|
|
676
|
+
if (typeof latest.content === `string`) {
|
|
677
|
+
return [{ type: "text", text: latest.content }];
|
|
678
|
+
}
|
|
679
|
+
return latest.content.map((c) => {
|
|
680
|
+
if (c.type === `tool-result`)
|
|
681
|
+
return {
|
|
682
|
+
type: "tool-result",
|
|
683
|
+
input: {},
|
|
684
|
+
// TODO: we need to find the tool call here and add the input from it
|
|
685
|
+
output: c.output,
|
|
686
|
+
toolCallId: c.toolCallId,
|
|
687
|
+
toolName: c.toolName
|
|
688
|
+
};
|
|
689
|
+
if (c.type === `file`)
|
|
690
|
+
return {
|
|
691
|
+
type: "file",
|
|
692
|
+
file: new DefaultGeneratedFileWithType({
|
|
693
|
+
data: typeof c.data === `string` ? c.data : c.data instanceof URL ? c.data.toString() : convertDataContentToBase64String(c.data),
|
|
694
|
+
mediaType: c.mediaType
|
|
695
|
+
})
|
|
696
|
+
};
|
|
697
|
+
if (c.type === `image`) {
|
|
698
|
+
return {
|
|
699
|
+
type: "file",
|
|
700
|
+
file: new DefaultGeneratedFileWithType({
|
|
701
|
+
data: typeof c.image === `string` ? c.image : c.image instanceof URL ? c.image.toString() : convertDataContentToBase64String(c.image),
|
|
702
|
+
mediaType: c.mediaType || "unknown"
|
|
703
|
+
})
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
return c;
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
},
|
|
710
|
+
aiV4: {
|
|
711
|
+
ui: () => this.response.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage),
|
|
712
|
+
core: () => this.aiV4UIMessagesToAIV4CoreMessages(this.response.aiV4.ui())
|
|
713
|
+
}
|
|
510
714
|
};
|
|
715
|
+
// TODO: need to update this for new .aiV4/5.x() pattern
|
|
511
716
|
responsePersisted = {
|
|
717
|
+
v3: () => this.cleanV3Metadata(
|
|
718
|
+
this.messages.filter((m) => this.newResponseMessagesPersisted.has(m)).map(this.mastraMessageV2ToMastraMessageV3)
|
|
719
|
+
),
|
|
512
720
|
v2: () => this.messages.filter((m) => this.newResponseMessagesPersisted.has(m)),
|
|
513
|
-
ui: () => this.responsePersisted.v2().map(_MessageList.
|
|
721
|
+
ui: () => this.responsePersisted.v2().map(_MessageList.mastraMessageV2ToAIV4UIMessage)
|
|
514
722
|
};
|
|
515
723
|
drainUnsavedMessages() {
|
|
516
724
|
const messages = this.messages.filter((m) => this.newUserMessages.has(m) || this.newResponseMessages.has(m));
|
|
@@ -536,10 +744,10 @@ var MessageList = class _MessageList {
|
|
|
536
744
|
}
|
|
537
745
|
return this;
|
|
538
746
|
}
|
|
539
|
-
|
|
540
|
-
return convertToCoreMessages(this.
|
|
747
|
+
aiV4UIMessagesToAIV4CoreMessages(messages) {
|
|
748
|
+
return AIV4.convertToCoreMessages(this.sanitizeAIV4UIMessages(messages));
|
|
541
749
|
}
|
|
542
|
-
|
|
750
|
+
sanitizeAIV4UIMessages(messages) {
|
|
543
751
|
const msgs = messages.map((m) => {
|
|
544
752
|
if (m.parts.length === 0) return false;
|
|
545
753
|
const safeParts = m.parts.filter(
|
|
@@ -561,25 +769,31 @@ var MessageList = class _MessageList {
|
|
|
561
769
|
}
|
|
562
770
|
addOneSystem(message, tag) {
|
|
563
771
|
if (typeof message === `string`) message = { role: "system", content: message };
|
|
564
|
-
|
|
772
|
+
const coreMessage = _MessageList.isAIV4CoreMessage(message) ? message : this.aiV5ModelMessagesToAIV4CoreMessages([message], `system`)[0];
|
|
773
|
+
if (coreMessage.role !== `system`) {
|
|
774
|
+
throw new Error(
|
|
775
|
+
`Expected role "system" but saw ${coreMessage.role} for message ${JSON.stringify(coreMessage, null, 2)}`
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
if (tag && !this.isDuplicateSystem(coreMessage, tag)) {
|
|
565
779
|
this.taggedSystemMessages[tag] ||= [];
|
|
566
|
-
this.taggedSystemMessages[tag].push(
|
|
567
|
-
} else if (!this.isDuplicateSystem(
|
|
568
|
-
this.systemMessages.push(
|
|
780
|
+
this.taggedSystemMessages[tag].push(coreMessage);
|
|
781
|
+
} else if (!this.isDuplicateSystem(coreMessage)) {
|
|
782
|
+
this.systemMessages.push(coreMessage);
|
|
569
783
|
}
|
|
570
784
|
}
|
|
571
785
|
isDuplicateSystem(message, tag) {
|
|
572
786
|
if (tag) {
|
|
573
787
|
if (!this.taggedSystemMessages[tag]) return false;
|
|
574
788
|
return this.taggedSystemMessages[tag].some(
|
|
575
|
-
(m) => _MessageList.
|
|
789
|
+
(m) => _MessageList.cacheKeyFromAIV4CoreMessageContent(m.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(message.content)
|
|
576
790
|
);
|
|
577
791
|
}
|
|
578
792
|
return this.systemMessages.some(
|
|
579
|
-
(m) => _MessageList.
|
|
793
|
+
(m) => _MessageList.cacheKeyFromAIV4CoreMessageContent(m.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(message.content)
|
|
580
794
|
);
|
|
581
795
|
}
|
|
582
|
-
static
|
|
796
|
+
static mastraMessageV2ToAIV4UIMessage(m) {
|
|
583
797
|
const experimentalAttachments = m.content.experimental_attachments ? [...m.content.experimental_attachments] : [];
|
|
584
798
|
const contentString = typeof m.content.content === `string` && m.content.content !== "" ? m.content.content : m.content.parts.reduce((prev, part) => {
|
|
585
799
|
if (part.type === `text`) {
|
|
@@ -597,6 +811,32 @@ var MessageList = class _MessageList {
|
|
|
597
811
|
});
|
|
598
812
|
} else if (part.type === "tool-invocation" && (part.toolInvocation.state === "call" || part.toolInvocation.state === "partial-call")) {
|
|
599
813
|
continue;
|
|
814
|
+
} else if (part.type === "tool-invocation") {
|
|
815
|
+
const toolInvocation = { ...part.toolInvocation };
|
|
816
|
+
let currentStep = -1;
|
|
817
|
+
let toolStep = -1;
|
|
818
|
+
for (const innerPart of m.content.parts) {
|
|
819
|
+
if (innerPart.type === `step-start`) currentStep++;
|
|
820
|
+
if (innerPart.type === `tool-invocation` && innerPart.toolInvocation.toolCallId === part.toolInvocation.toolCallId) {
|
|
821
|
+
toolStep = currentStep;
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
if (toolStep >= 0) {
|
|
826
|
+
const preparedInvocation = {
|
|
827
|
+
step: toolStep,
|
|
828
|
+
...toolInvocation
|
|
829
|
+
};
|
|
830
|
+
parts.push({
|
|
831
|
+
type: "tool-invocation",
|
|
832
|
+
toolInvocation: preparedInvocation
|
|
833
|
+
});
|
|
834
|
+
} else {
|
|
835
|
+
parts.push({
|
|
836
|
+
type: "tool-invocation",
|
|
837
|
+
toolInvocation
|
|
838
|
+
});
|
|
839
|
+
}
|
|
600
840
|
} else {
|
|
601
841
|
parts.push(part);
|
|
602
842
|
}
|
|
@@ -678,8 +918,10 @@ var MessageList = class _MessageList {
|
|
|
678
918
|
}
|
|
679
919
|
});
|
|
680
920
|
}
|
|
681
|
-
if (message.role === `system` && _MessageList.isVercelCoreMessage(message)) return this.addSystem(message);
|
|
682
921
|
if (message.role === `system`) {
|
|
922
|
+
if (messageSource === `memory`) return null;
|
|
923
|
+
if (_MessageList.isAIV4CoreMessage(message) || _MessageList.isAIV5CoreMessage(message))
|
|
924
|
+
return this.addSystem(message);
|
|
683
925
|
throw new MastraError({
|
|
684
926
|
id: "INVALID_SYSTEM_MESSAGE_FORMAT",
|
|
685
927
|
domain: "AGENT" /* AGENT */,
|
|
@@ -784,7 +1026,7 @@ var MessageList = class _MessageList {
|
|
|
784
1026
|
} else if (messageSource === `response`) {
|
|
785
1027
|
this.newResponseMessages.add(messageV2);
|
|
786
1028
|
this.newResponseMessagesPersisted.add(messageV2);
|
|
787
|
-
} else if (messageSource === `
|
|
1029
|
+
} else if (messageSource === `input`) {
|
|
788
1030
|
this.newUserMessages.add(messageV2);
|
|
789
1031
|
this.newUserMessagesPersisted.add(messageV2);
|
|
790
1032
|
} else if (messageSource === `context`) {
|
|
@@ -808,11 +1050,13 @@ var MessageList = class _MessageList {
|
|
|
808
1050
|
insertAt
|
|
809
1051
|
// optional
|
|
810
1052
|
}) {
|
|
811
|
-
const partKey = _MessageList.
|
|
1053
|
+
const partKey = _MessageList.cacheKeyFromAIV4Parts([part]);
|
|
812
1054
|
const latestPartCount = latestMessage.content.parts.filter(
|
|
813
|
-
(p) => _MessageList.
|
|
1055
|
+
(p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
|
|
1056
|
+
).length;
|
|
1057
|
+
const newPartCount = newMessage.content.parts.filter(
|
|
1058
|
+
(p) => _MessageList.cacheKeyFromAIV4Parts([p]) === partKey
|
|
814
1059
|
).length;
|
|
815
|
-
const newPartCount = newMessage.content.parts.filter((p) => _MessageList.cacheKeyFromParts([p]) === partKey).length;
|
|
816
1060
|
if (latestPartCount < newPartCount) {
|
|
817
1061
|
if (typeof insertAt === "number") {
|
|
818
1062
|
latestMessage.content.parts.splice(insertAt, 0, part);
|
|
@@ -837,7 +1081,7 @@ var MessageList = class _MessageList {
|
|
|
837
1081
|
for (let i = 0; i < messageV2.content.parts.length; ++i) {
|
|
838
1082
|
const part = messageV2.content.parts[i];
|
|
839
1083
|
if (!part) continue;
|
|
840
|
-
const key = _MessageList.
|
|
1084
|
+
const key = _MessageList.cacheKeyFromAIV4Parts([part]);
|
|
841
1085
|
const partToAdd = partsToAdd.get(i);
|
|
842
1086
|
if (!key || !partToAdd) continue;
|
|
843
1087
|
if (anchorMap.size > 0) {
|
|
@@ -848,7 +1092,7 @@ var MessageList = class _MessageList {
|
|
|
848
1092
|
const offset = leftAnchorV2 === -1 ? i : i - leftAnchorV2;
|
|
849
1093
|
const insertAt = leftAnchorLatest + offset;
|
|
850
1094
|
const rightAnchorLatest = rightAnchorV2 !== -1 ? anchorMap.get(rightAnchorV2) : latestMessage.content.parts.length;
|
|
851
|
-
if (insertAt >= 0 && insertAt <= rightAnchorLatest && !latestMessage.content.parts.slice(insertAt, rightAnchorLatest).some((p) => _MessageList.
|
|
1095
|
+
if (insertAt >= 0 && insertAt <= rightAnchorLatest && !latestMessage.content.parts.slice(insertAt, rightAnchorLatest).some((p) => _MessageList.cacheKeyFromAIV4Parts([p]) === _MessageList.cacheKeyFromAIV4Parts([part]))) {
|
|
852
1096
|
this.pushNewMessagePart({
|
|
853
1097
|
latestMessage,
|
|
854
1098
|
newMessage: messageV2,
|
|
@@ -891,11 +1135,20 @@ var MessageList = class _MessageList {
|
|
|
891
1135
|
if (_MessageList.isMastraMessageV2(message)) {
|
|
892
1136
|
return this.hydrateMastraMessageV2Fields(message);
|
|
893
1137
|
}
|
|
894
|
-
if (_MessageList.
|
|
895
|
-
return this.
|
|
1138
|
+
if (_MessageList.isAIV4CoreMessage(message)) {
|
|
1139
|
+
return this.aiV4CoreMessageToMastraMessageV2(message, messageSource);
|
|
1140
|
+
}
|
|
1141
|
+
if (_MessageList.isAIV4UIMessage(message)) {
|
|
1142
|
+
return this.aiV4UIMessageToMastraMessageV2(message, messageSource);
|
|
1143
|
+
}
|
|
1144
|
+
if (_MessageList.isAIV5CoreMessage(message)) {
|
|
1145
|
+
return _MessageList.mastraMessageV3ToV2(this.aiV5ModelMessageToMastraMessageV3(message, messageSource));
|
|
896
1146
|
}
|
|
897
|
-
if (_MessageList.
|
|
898
|
-
return this.
|
|
1147
|
+
if (_MessageList.isAIV5UIMessage(message)) {
|
|
1148
|
+
return _MessageList.mastraMessageV3ToV2(this.aiV5UIMessageToMastraMessageV3(message, messageSource));
|
|
1149
|
+
}
|
|
1150
|
+
if (_MessageList.isMastraMessageV3(message)) {
|
|
1151
|
+
return _MessageList.mastraMessageV3ToV2(this.hydrateMastraMessageV3Fields(message));
|
|
899
1152
|
}
|
|
900
1153
|
throw new Error(`Found unhandled message ${JSON.stringify(message)}`);
|
|
901
1154
|
}
|
|
@@ -931,7 +1184,7 @@ var MessageList = class _MessageList {
|
|
|
931
1184
|
return randomUUID();
|
|
932
1185
|
}
|
|
933
1186
|
mastraMessageV1ToMastraMessageV2(message, messageSource) {
|
|
934
|
-
const coreV2 = this.
|
|
1187
|
+
const coreV2 = this.aiV4CoreMessageToMastraMessageV2(
|
|
935
1188
|
{
|
|
936
1189
|
content: message.content,
|
|
937
1190
|
role: message.role
|
|
@@ -947,6 +1200,10 @@ var MessageList = class _MessageList {
|
|
|
947
1200
|
content: coreV2.content
|
|
948
1201
|
};
|
|
949
1202
|
}
|
|
1203
|
+
hydrateMastraMessageV3Fields(message) {
|
|
1204
|
+
if (!(message.createdAt instanceof Date)) message.createdAt = new Date(message.createdAt);
|
|
1205
|
+
return message;
|
|
1206
|
+
}
|
|
950
1207
|
hydrateMastraMessageV2Fields(message) {
|
|
951
1208
|
if (!(message.createdAt instanceof Date)) message.createdAt = new Date(message.createdAt);
|
|
952
1209
|
if (message.content.toolInvocations && message.content.parts) {
|
|
@@ -964,7 +1221,7 @@ var MessageList = class _MessageList {
|
|
|
964
1221
|
}
|
|
965
1222
|
return message;
|
|
966
1223
|
}
|
|
967
|
-
|
|
1224
|
+
aiV4UIMessageToMastraMessageV2(message, messageSource) {
|
|
968
1225
|
const content = {
|
|
969
1226
|
format: 2,
|
|
970
1227
|
parts: message.parts
|
|
@@ -987,7 +1244,7 @@ var MessageList = class _MessageList {
|
|
|
987
1244
|
content
|
|
988
1245
|
};
|
|
989
1246
|
}
|
|
990
|
-
|
|
1247
|
+
aiV4CoreMessageToMastraMessageV2(coreMessage, messageSource) {
|
|
991
1248
|
const id = `id` in coreMessage ? coreMessage.id : this.newMessageId();
|
|
992
1249
|
const parts = [];
|
|
993
1250
|
const experimentalAttachments = [];
|
|
@@ -1112,44 +1369,52 @@ var MessageList = class _MessageList {
|
|
|
1112
1369
|
content
|
|
1113
1370
|
};
|
|
1114
1371
|
}
|
|
1115
|
-
static
|
|
1116
|
-
return !_MessageList.isMastraMessage(msg) &&
|
|
1372
|
+
static isAIV4UIMessage(msg) {
|
|
1373
|
+
return !_MessageList.isMastraMessage(msg) && !_MessageList.isAIV4CoreMessage(msg) && `parts` in msg && !_MessageList.hasAIV5UIMessageCharacteristics(msg);
|
|
1374
|
+
}
|
|
1375
|
+
static isAIV5CoreMessage(msg) {
|
|
1376
|
+
return !_MessageList.isMastraMessage(msg) && !(`parts` in msg) && `content` in msg && _MessageList.hasAIV5CoreMessageCharacteristics(msg);
|
|
1117
1377
|
}
|
|
1118
|
-
static
|
|
1119
|
-
return !_MessageList.isMastraMessage(msg) &&
|
|
1378
|
+
static isAIV4CoreMessage(msg) {
|
|
1379
|
+
return !_MessageList.isMastraMessage(msg) && !(`parts` in msg) && `content` in msg && !_MessageList.hasAIV5CoreMessageCharacteristics(msg);
|
|
1120
1380
|
}
|
|
1121
1381
|
static isMastraMessage(msg) {
|
|
1122
|
-
return _MessageList.isMastraMessageV2(msg) || _MessageList.isMastraMessageV1(msg);
|
|
1382
|
+
return _MessageList.isMastraMessageV3(msg) || _MessageList.isMastraMessageV2(msg) || _MessageList.isMastraMessageV1(msg);
|
|
1123
1383
|
}
|
|
1124
1384
|
static isMastraMessageV1(msg) {
|
|
1125
|
-
return !_MessageList.isMastraMessageV2(msg) && (`threadId` in msg || `resourceId` in msg);
|
|
1385
|
+
return !_MessageList.isMastraMessageV2(msg) && !_MessageList.isMastraMessageV3(msg) && (`threadId` in msg || `resourceId` in msg);
|
|
1126
1386
|
}
|
|
1127
1387
|
static isMastraMessageV2(msg) {
|
|
1128
1388
|
return Boolean(
|
|
1129
|
-
msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` &&
|
|
1130
|
-
|
|
1389
|
+
`content` in msg && msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` && `format` in msg.content && msg.content.format === 2
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
1392
|
+
static isMastraMessageV3(msg) {
|
|
1393
|
+
return Boolean(
|
|
1394
|
+
`content` in msg && msg.content && !Array.isArray(msg.content) && typeof msg.content !== `string` && `format` in msg.content && msg.content.format === 3
|
|
1131
1395
|
);
|
|
1132
1396
|
}
|
|
1133
1397
|
static getRole(message) {
|
|
1134
1398
|
if (message.role === `assistant` || message.role === `tool`) return `assistant`;
|
|
1135
1399
|
if (message.role === `user`) return `user`;
|
|
1400
|
+
if (message.role === `system`) return `system`;
|
|
1136
1401
|
throw new Error(
|
|
1137
1402
|
`BUG: add handling for message role ${message.role} in message ${JSON.stringify(message, null, 2)}`
|
|
1138
1403
|
);
|
|
1139
1404
|
}
|
|
1140
|
-
static
|
|
1405
|
+
static cacheKeyFromAIV4Parts(parts) {
|
|
1141
1406
|
let key = ``;
|
|
1142
1407
|
for (const part of parts) {
|
|
1143
1408
|
key += part.type;
|
|
1144
1409
|
if (part.type === `text`) {
|
|
1145
|
-
key +=
|
|
1410
|
+
key += part.text;
|
|
1146
1411
|
}
|
|
1147
1412
|
if (part.type === `tool-invocation`) {
|
|
1148
1413
|
key += part.toolInvocation.toolCallId;
|
|
1149
1414
|
key += part.toolInvocation.state;
|
|
1150
1415
|
}
|
|
1151
1416
|
if (part.type === `reasoning`) {
|
|
1152
|
-
key += part.reasoning
|
|
1417
|
+
key += part.reasoning;
|
|
1153
1418
|
key += part.details.reduce((prev, current) => {
|
|
1154
1419
|
if (current.type === `text`) {
|
|
1155
1420
|
return prev + current.text.length + (current.signature?.length || 0);
|
|
@@ -1158,7 +1423,7 @@ var MessageList = class _MessageList {
|
|
|
1158
1423
|
}, 0);
|
|
1159
1424
|
}
|
|
1160
1425
|
if (part.type === `file`) {
|
|
1161
|
-
key += part.data
|
|
1426
|
+
key += part.data;
|
|
1162
1427
|
key += part.mimeType;
|
|
1163
1428
|
}
|
|
1164
1429
|
}
|
|
@@ -1173,7 +1438,7 @@ var MessageList = class _MessageList {
|
|
|
1173
1438
|
return p;
|
|
1174
1439
|
}, "");
|
|
1175
1440
|
}
|
|
1176
|
-
static
|
|
1441
|
+
static cacheKeyFromAIV4CoreMessageContent(content) {
|
|
1177
1442
|
if (typeof content === `string`) return content;
|
|
1178
1443
|
let key = ``;
|
|
1179
1444
|
for (const part of content) {
|
|
@@ -1207,34 +1472,777 @@ var MessageList = class _MessageList {
|
|
|
1207
1472
|
return key;
|
|
1208
1473
|
}
|
|
1209
1474
|
static messagesAreEqual(one, two) {
|
|
1210
|
-
const
|
|
1211
|
-
const
|
|
1212
|
-
if (
|
|
1213
|
-
if (
|
|
1214
|
-
return _MessageList.
|
|
1215
|
-
}
|
|
1216
|
-
const
|
|
1217
|
-
const
|
|
1218
|
-
if (
|
|
1219
|
-
if (
|
|
1220
|
-
return _MessageList.
|
|
1475
|
+
const oneUIV4 = _MessageList.isAIV4UIMessage(one) && one;
|
|
1476
|
+
const twoUIV4 = _MessageList.isAIV4UIMessage(two) && two;
|
|
1477
|
+
if (oneUIV4 && !twoUIV4) return false;
|
|
1478
|
+
if (oneUIV4 && twoUIV4) {
|
|
1479
|
+
return _MessageList.cacheKeyFromAIV4Parts(one.parts) === _MessageList.cacheKeyFromAIV4Parts(two.parts);
|
|
1480
|
+
}
|
|
1481
|
+
const oneCMV4 = _MessageList.isAIV4CoreMessage(one) && one;
|
|
1482
|
+
const twoCMV4 = _MessageList.isAIV4CoreMessage(two) && two;
|
|
1483
|
+
if (oneCMV4 && !twoCMV4) return false;
|
|
1484
|
+
if (oneCMV4 && twoCMV4) {
|
|
1485
|
+
return _MessageList.cacheKeyFromAIV4CoreMessageContent(oneCMV4.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(twoCMV4.content);
|
|
1221
1486
|
}
|
|
1222
1487
|
const oneMM1 = _MessageList.isMastraMessageV1(one) && one;
|
|
1223
1488
|
const twoMM1 = _MessageList.isMastraMessageV1(two) && two;
|
|
1224
1489
|
if (oneMM1 && !twoMM1) return false;
|
|
1225
1490
|
if (oneMM1 && twoMM1) {
|
|
1226
|
-
return oneMM1.id === twoMM1.id && _MessageList.
|
|
1491
|
+
return oneMM1.id === twoMM1.id && _MessageList.cacheKeyFromAIV4CoreMessageContent(oneMM1.content) === _MessageList.cacheKeyFromAIV4CoreMessageContent(twoMM1.content);
|
|
1227
1492
|
}
|
|
1228
1493
|
const oneMM2 = _MessageList.isMastraMessageV2(one) && one;
|
|
1229
1494
|
const twoMM2 = _MessageList.isMastraMessageV2(two) && two;
|
|
1230
1495
|
if (oneMM2 && !twoMM2) return false;
|
|
1231
1496
|
if (oneMM2 && twoMM2) {
|
|
1232
|
-
return oneMM2.id === twoMM2.id && _MessageList.
|
|
1497
|
+
return oneMM2.id === twoMM2.id && _MessageList.cacheKeyFromAIV4Parts(oneMM2.content.parts) === _MessageList.cacheKeyFromAIV4Parts(twoMM2.content.parts);
|
|
1498
|
+
}
|
|
1499
|
+
const oneMM3 = _MessageList.isMastraMessageV3(one) && one;
|
|
1500
|
+
const twoMM3 = _MessageList.isMastraMessageV3(two) && two;
|
|
1501
|
+
if (oneMM3 && !twoMM3) return false;
|
|
1502
|
+
if (oneMM3 && twoMM3) {
|
|
1503
|
+
return oneMM3.id === twoMM3.id && _MessageList.cacheKeyFromAIV5Parts(oneMM3.content.parts) === _MessageList.cacheKeyFromAIV5Parts(twoMM3.content.parts);
|
|
1504
|
+
}
|
|
1505
|
+
const oneUIV5 = _MessageList.isAIV5UIMessage(one) && one;
|
|
1506
|
+
const twoUIV5 = _MessageList.isAIV5UIMessage(two) && two;
|
|
1507
|
+
if (oneUIV5 && !twoUIV5) return false;
|
|
1508
|
+
if (oneUIV5 && twoUIV5) {
|
|
1509
|
+
return _MessageList.cacheKeyFromAIV5Parts(one.parts) === _MessageList.cacheKeyFromAIV5Parts(two.parts);
|
|
1510
|
+
}
|
|
1511
|
+
const oneCMV5 = _MessageList.isAIV5CoreMessage(one) && one;
|
|
1512
|
+
const twoCMV5 = _MessageList.isAIV5CoreMessage(two) && two;
|
|
1513
|
+
if (oneCMV5 && !twoCMV5) return false;
|
|
1514
|
+
if (oneCMV5 && twoCMV5) {
|
|
1515
|
+
return _MessageList.cacheKeyFromAIV5ModelMessageContent(oneCMV5.content) === _MessageList.cacheKeyFromAIV5ModelMessageContent(twoCMV5.content);
|
|
1233
1516
|
}
|
|
1234
1517
|
return true;
|
|
1235
1518
|
}
|
|
1519
|
+
cleanV3Metadata(messages) {
|
|
1520
|
+
return messages.map((msg) => {
|
|
1521
|
+
if (!msg.content.metadata || typeof msg.content.metadata !== "object") {
|
|
1522
|
+
return msg;
|
|
1523
|
+
}
|
|
1524
|
+
const metadata = { ...msg.content.metadata };
|
|
1525
|
+
const hasOriginalContent = "__originalContent" in metadata;
|
|
1526
|
+
const hasOriginalAttachments = "__originalExperimentalAttachments" in metadata;
|
|
1527
|
+
if (!hasOriginalContent && !hasOriginalAttachments) {
|
|
1528
|
+
return msg;
|
|
1529
|
+
}
|
|
1530
|
+
const { __originalContent, __originalExperimentalAttachments, ...cleanMetadata } = metadata;
|
|
1531
|
+
if (Object.keys(cleanMetadata).length === 0) {
|
|
1532
|
+
const { metadata: metadata2, ...contentWithoutMetadata } = msg.content;
|
|
1533
|
+
return { ...msg, content: contentWithoutMetadata };
|
|
1534
|
+
}
|
|
1535
|
+
return { ...msg, content: { ...msg.content, metadata: cleanMetadata } };
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
static aiV4CoreMessageToV1PromptMessage(coreMessage) {
|
|
1539
|
+
if (coreMessage.role === `system`) {
|
|
1540
|
+
return coreMessage;
|
|
1541
|
+
}
|
|
1542
|
+
if (typeof coreMessage.content === `string` && (coreMessage.role === `assistant` || coreMessage.role === `user`)) {
|
|
1543
|
+
return {
|
|
1544
|
+
...coreMessage,
|
|
1545
|
+
content: [{ type: "text", text: coreMessage.content }]
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
if (typeof coreMessage.content === `string`) {
|
|
1549
|
+
throw new Error(
|
|
1550
|
+
`Saw text content for input CoreMessage, but the role is ${coreMessage.role}. This is only allowed for "system", "assistant", and "user" roles.`
|
|
1551
|
+
);
|
|
1552
|
+
}
|
|
1553
|
+
const roleContent = {
|
|
1554
|
+
user: [],
|
|
1555
|
+
assistant: [],
|
|
1556
|
+
tool: []
|
|
1557
|
+
};
|
|
1558
|
+
const role = coreMessage.role;
|
|
1559
|
+
for (const part of coreMessage.content) {
|
|
1560
|
+
const incompatibleMessage = `Saw incompatible message content part type ${part.type} for message role ${role}`;
|
|
1561
|
+
switch (part.type) {
|
|
1562
|
+
case "text": {
|
|
1563
|
+
if (role === `tool`) {
|
|
1564
|
+
throw new Error(incompatibleMessage);
|
|
1565
|
+
}
|
|
1566
|
+
roleContent[role].push(part);
|
|
1567
|
+
break;
|
|
1568
|
+
}
|
|
1569
|
+
case "redacted-reasoning":
|
|
1570
|
+
case "reasoning": {
|
|
1571
|
+
if (role !== `assistant`) {
|
|
1572
|
+
throw new Error(incompatibleMessage);
|
|
1573
|
+
}
|
|
1574
|
+
roleContent[role].push(part);
|
|
1575
|
+
break;
|
|
1576
|
+
}
|
|
1577
|
+
case "tool-call": {
|
|
1578
|
+
if (role === `tool` || role === `user`) {
|
|
1579
|
+
throw new Error(incompatibleMessage);
|
|
1580
|
+
}
|
|
1581
|
+
roleContent[role].push(part);
|
|
1582
|
+
break;
|
|
1583
|
+
}
|
|
1584
|
+
case "tool-result": {
|
|
1585
|
+
if (role === `assistant` || role === `user`) {
|
|
1586
|
+
throw new Error(incompatibleMessage);
|
|
1587
|
+
}
|
|
1588
|
+
roleContent[role].push(part);
|
|
1589
|
+
break;
|
|
1590
|
+
}
|
|
1591
|
+
case "image": {
|
|
1592
|
+
if (role === `tool` || role === `assistant`) {
|
|
1593
|
+
throw new Error(incompatibleMessage);
|
|
1594
|
+
}
|
|
1595
|
+
roleContent[role].push({
|
|
1596
|
+
...part,
|
|
1597
|
+
image: part.image instanceof URL || part.image instanceof Uint8Array ? part.image : Buffer.isBuffer(part.image) || part.image instanceof ArrayBuffer ? new Uint8Array(part.image) : new URL(part.image)
|
|
1598
|
+
});
|
|
1599
|
+
break;
|
|
1600
|
+
}
|
|
1601
|
+
case "file": {
|
|
1602
|
+
if (role === `tool`) {
|
|
1603
|
+
throw new Error(incompatibleMessage);
|
|
1604
|
+
}
|
|
1605
|
+
roleContent[role].push({
|
|
1606
|
+
...part,
|
|
1607
|
+
data: part.data instanceof URL ? part.data : typeof part.data === "string" ? part.data : convertDataContentToBase64String(part.data)
|
|
1608
|
+
});
|
|
1609
|
+
break;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (role === `tool`) {
|
|
1614
|
+
return {
|
|
1615
|
+
...coreMessage,
|
|
1616
|
+
content: roleContent[role]
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
if (role === `user`) {
|
|
1620
|
+
return {
|
|
1621
|
+
...coreMessage,
|
|
1622
|
+
content: roleContent[role]
|
|
1623
|
+
};
|
|
1624
|
+
}
|
|
1625
|
+
if (role === `assistant`) {
|
|
1626
|
+
return {
|
|
1627
|
+
...coreMessage,
|
|
1628
|
+
content: roleContent[role]
|
|
1629
|
+
};
|
|
1630
|
+
}
|
|
1631
|
+
throw new Error(
|
|
1632
|
+
`Encountered unknown role ${role} when converting V4 CoreMessage -> V4 LanguageModelV1Prompt, input message: ${JSON.stringify(coreMessage, null, 2)}`
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
static aiV5ModelMessageToV2PromptMessage(modelMessage) {
|
|
1636
|
+
if (modelMessage.role === `system`) {
|
|
1637
|
+
return modelMessage;
|
|
1638
|
+
}
|
|
1639
|
+
if (typeof modelMessage.content === `string` && (modelMessage.role === `assistant` || modelMessage.role === `user`)) {
|
|
1640
|
+
return {
|
|
1641
|
+
role: modelMessage.role,
|
|
1642
|
+
content: [{ type: "text", text: modelMessage.content }],
|
|
1643
|
+
providerOptions: modelMessage.providerOptions
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
if (typeof modelMessage.content === `string`) {
|
|
1647
|
+
throw new Error(
|
|
1648
|
+
`Saw text content for input ModelMessage, but the role is ${modelMessage.role}. This is only allowed for "system", "assistant", and "user" roles.`
|
|
1649
|
+
);
|
|
1650
|
+
}
|
|
1651
|
+
const roleContent = {
|
|
1652
|
+
user: [],
|
|
1653
|
+
assistant: [],
|
|
1654
|
+
tool: []
|
|
1655
|
+
};
|
|
1656
|
+
const role = modelMessage.role;
|
|
1657
|
+
for (const part of modelMessage.content) {
|
|
1658
|
+
const incompatibleMessage = `Saw incompatible message content part type ${part.type} for message role ${role}`;
|
|
1659
|
+
switch (part.type) {
|
|
1660
|
+
case "text": {
|
|
1661
|
+
if (role === `tool`) {
|
|
1662
|
+
throw new Error(incompatibleMessage);
|
|
1663
|
+
}
|
|
1664
|
+
roleContent[role].push(part);
|
|
1665
|
+
break;
|
|
1666
|
+
}
|
|
1667
|
+
case "reasoning": {
|
|
1668
|
+
if (role === `tool` || role === `user`) {
|
|
1669
|
+
throw new Error(incompatibleMessage);
|
|
1670
|
+
}
|
|
1671
|
+
roleContent[role].push(part);
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
case "tool-call": {
|
|
1675
|
+
if (role !== `assistant`) {
|
|
1676
|
+
throw new Error(incompatibleMessage);
|
|
1677
|
+
}
|
|
1678
|
+
roleContent[role].push(part);
|
|
1679
|
+
break;
|
|
1680
|
+
}
|
|
1681
|
+
case "tool-result": {
|
|
1682
|
+
if (role === `assistant` || role === `user`) {
|
|
1683
|
+
throw new Error(incompatibleMessage);
|
|
1684
|
+
}
|
|
1685
|
+
roleContent[role].push(part);
|
|
1686
|
+
break;
|
|
1687
|
+
}
|
|
1688
|
+
case "file": {
|
|
1689
|
+
if (role === `tool`) {
|
|
1690
|
+
throw new Error(incompatibleMessage);
|
|
1691
|
+
}
|
|
1692
|
+
roleContent[role].push({
|
|
1693
|
+
...part,
|
|
1694
|
+
data: part.data instanceof ArrayBuffer ? new Uint8Array(part.data) : part.data
|
|
1695
|
+
});
|
|
1696
|
+
break;
|
|
1697
|
+
}
|
|
1698
|
+
case "image": {
|
|
1699
|
+
if (role === `tool`) {
|
|
1700
|
+
throw new Error(incompatibleMessage);
|
|
1701
|
+
}
|
|
1702
|
+
roleContent[role].push({
|
|
1703
|
+
...part,
|
|
1704
|
+
mediaType: part.mediaType || "image/unknown",
|
|
1705
|
+
type: "file",
|
|
1706
|
+
data: part.image instanceof ArrayBuffer ? new Uint8Array(part.image) : part.image
|
|
1707
|
+
});
|
|
1708
|
+
break;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
if (role === `tool`) {
|
|
1713
|
+
return {
|
|
1714
|
+
...modelMessage,
|
|
1715
|
+
content: roleContent[role]
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
if (role === `user`) {
|
|
1719
|
+
return {
|
|
1720
|
+
...modelMessage,
|
|
1721
|
+
content: roleContent[role]
|
|
1722
|
+
};
|
|
1723
|
+
}
|
|
1724
|
+
if (role === `assistant`) {
|
|
1725
|
+
return {
|
|
1726
|
+
...modelMessage,
|
|
1727
|
+
content: roleContent[role]
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1730
|
+
throw new Error(
|
|
1731
|
+
`Encountered unknown role ${role} when converting V5 ModelMessage -> V5 LanguageModelV2Message, input message: ${JSON.stringify(modelMessage, null, 2)}`
|
|
1732
|
+
);
|
|
1733
|
+
}
|
|
1734
|
+
static mastraMessageV3ToV2(v3Msg) {
|
|
1735
|
+
const toolInvocationParts = v3Msg.content.parts.filter((p) => AIV5.isToolUIPart(p));
|
|
1736
|
+
const hadToolInvocations = v3Msg.content.metadata?.__hadToolInvocations === true;
|
|
1737
|
+
let toolInvocations = void 0;
|
|
1738
|
+
if (toolInvocationParts.length > 0) {
|
|
1739
|
+
const invocations = toolInvocationParts.map((p) => {
|
|
1740
|
+
const toolName = getToolName(p);
|
|
1741
|
+
if (p.state === `output-available`) {
|
|
1742
|
+
return {
|
|
1743
|
+
args: p.input,
|
|
1744
|
+
result: typeof p.output === "object" && p.output && "value" in p.output ? p.output.value : p.output,
|
|
1745
|
+
toolCallId: p.toolCallId,
|
|
1746
|
+
toolName,
|
|
1747
|
+
state: "result"
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
return {
|
|
1751
|
+
args: p.input,
|
|
1752
|
+
state: "call",
|
|
1753
|
+
toolName,
|
|
1754
|
+
toolCallId: p.toolCallId
|
|
1755
|
+
};
|
|
1756
|
+
});
|
|
1757
|
+
toolInvocations = invocations;
|
|
1758
|
+
} else if (hadToolInvocations && v3Msg.role === "assistant") {
|
|
1759
|
+
toolInvocations = [];
|
|
1760
|
+
}
|
|
1761
|
+
const attachmentUrls = new Set(v3Msg.content.metadata?.__attachmentUrls || []);
|
|
1762
|
+
const v2Msg = {
|
|
1763
|
+
id: v3Msg.id,
|
|
1764
|
+
resourceId: v3Msg.resourceId,
|
|
1765
|
+
threadId: v3Msg.threadId,
|
|
1766
|
+
createdAt: v3Msg.createdAt,
|
|
1767
|
+
role: v3Msg.role,
|
|
1768
|
+
content: {
|
|
1769
|
+
format: 2,
|
|
1770
|
+
parts: v3Msg.content.parts.map((p) => {
|
|
1771
|
+
if (AIV5.isToolUIPart(p) || p.type === "dynamic-tool") {
|
|
1772
|
+
const toolName = getToolName(p);
|
|
1773
|
+
const shared = {
|
|
1774
|
+
state: p.state,
|
|
1775
|
+
args: p.input,
|
|
1776
|
+
toolCallId: p.toolCallId,
|
|
1777
|
+
toolName
|
|
1778
|
+
};
|
|
1779
|
+
if (p.state === `output-available`) {
|
|
1780
|
+
return {
|
|
1781
|
+
type: "tool-invocation",
|
|
1782
|
+
toolInvocation: {
|
|
1783
|
+
...shared,
|
|
1784
|
+
state: "result",
|
|
1785
|
+
result: typeof p.output === "object" && p.output && "value" in p.output ? p.output.value : p.output
|
|
1786
|
+
},
|
|
1787
|
+
providerMetadata: p.callProviderMetadata
|
|
1788
|
+
};
|
|
1789
|
+
}
|
|
1790
|
+
return {
|
|
1791
|
+
type: "tool-invocation",
|
|
1792
|
+
toolInvocation: {
|
|
1793
|
+
...shared,
|
|
1794
|
+
state: p.state === `input-available` ? `call` : `partial-call`
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
}
|
|
1798
|
+
switch (p.type) {
|
|
1799
|
+
case "text":
|
|
1800
|
+
return p;
|
|
1801
|
+
case "file":
|
|
1802
|
+
if (attachmentUrls.has(p.url)) {
|
|
1803
|
+
return null;
|
|
1804
|
+
}
|
|
1805
|
+
return {
|
|
1806
|
+
type: "file",
|
|
1807
|
+
mimeType: p.mediaType,
|
|
1808
|
+
data: p.url,
|
|
1809
|
+
providerMetadata: p.providerMetadata
|
|
1810
|
+
};
|
|
1811
|
+
case "reasoning":
|
|
1812
|
+
if (p.text === "") return null;
|
|
1813
|
+
return {
|
|
1814
|
+
type: "reasoning",
|
|
1815
|
+
reasoning: p.text,
|
|
1816
|
+
details: [{ type: "text", text: p.text }],
|
|
1817
|
+
providerMetadata: p.providerMetadata
|
|
1818
|
+
};
|
|
1819
|
+
case "source-url":
|
|
1820
|
+
return {
|
|
1821
|
+
type: "source",
|
|
1822
|
+
source: {
|
|
1823
|
+
url: p.url,
|
|
1824
|
+
id: p.sourceId,
|
|
1825
|
+
sourceType: "url"
|
|
1826
|
+
},
|
|
1827
|
+
providerMetadata: p.providerMetadata
|
|
1828
|
+
};
|
|
1829
|
+
case "step-start":
|
|
1830
|
+
return p;
|
|
1831
|
+
}
|
|
1832
|
+
return null;
|
|
1833
|
+
}).filter((p) => Boolean(p))
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
if (toolInvocations !== void 0) {
|
|
1837
|
+
v2Msg.content.toolInvocations = toolInvocations;
|
|
1838
|
+
}
|
|
1839
|
+
if (v3Msg.content.metadata) {
|
|
1840
|
+
const { __originalContent, __originalExperimentalAttachments, __attachmentUrls, ...userMetadata } = v3Msg.content.metadata;
|
|
1841
|
+
v2Msg.content.metadata = userMetadata;
|
|
1842
|
+
}
|
|
1843
|
+
const originalContent = v3Msg.content.metadata?.__originalContent;
|
|
1844
|
+
if (originalContent !== void 0) {
|
|
1845
|
+
if (typeof originalContent !== `string` || v2Msg.content.parts.every((p) => p.type === `step-start` || p.type === `text`)) {
|
|
1846
|
+
v2Msg.content.content = originalContent;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
const originalAttachments = v3Msg.content.metadata?.__originalExperimentalAttachments;
|
|
1850
|
+
if (originalAttachments && Array.isArray(originalAttachments)) {
|
|
1851
|
+
v2Msg.content.experimental_attachments = originalAttachments || [];
|
|
1852
|
+
}
|
|
1853
|
+
if (toolInvocations && toolInvocations.length > 0) {
|
|
1854
|
+
const resultToolInvocations = toolInvocations.filter((t) => t.state === "result");
|
|
1855
|
+
if (resultToolInvocations.length > 0) {
|
|
1856
|
+
v2Msg.content.toolInvocations = resultToolInvocations;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
if (v3Msg.type) v2Msg.type = v3Msg.type;
|
|
1860
|
+
return v2Msg;
|
|
1861
|
+
}
|
|
1862
|
+
mastraMessageV2ToMastraMessageV3(v2Msg) {
|
|
1863
|
+
const parts = [];
|
|
1864
|
+
const v3Msg = {
|
|
1865
|
+
id: v2Msg.id,
|
|
1866
|
+
content: {
|
|
1867
|
+
format: 3,
|
|
1868
|
+
parts
|
|
1869
|
+
},
|
|
1870
|
+
role: v2Msg.role,
|
|
1871
|
+
createdAt: v2Msg.createdAt instanceof Date ? v2Msg.createdAt : new Date(v2Msg.createdAt),
|
|
1872
|
+
resourceId: v2Msg.resourceId,
|
|
1873
|
+
threadId: v2Msg.threadId,
|
|
1874
|
+
type: v2Msg.type
|
|
1875
|
+
};
|
|
1876
|
+
if (v2Msg.content.metadata) {
|
|
1877
|
+
v3Msg.content.metadata = { ...v2Msg.content.metadata };
|
|
1878
|
+
}
|
|
1879
|
+
if (v2Msg.content.content !== void 0) {
|
|
1880
|
+
v3Msg.content.metadata = {
|
|
1881
|
+
...v3Msg.content.metadata || {},
|
|
1882
|
+
__originalContent: v2Msg.content.content
|
|
1883
|
+
};
|
|
1884
|
+
}
|
|
1885
|
+
if (v2Msg.content.experimental_attachments !== void 0) {
|
|
1886
|
+
v3Msg.content.metadata = {
|
|
1887
|
+
...v3Msg.content.metadata || {},
|
|
1888
|
+
__originalExperimentalAttachments: v2Msg.content.experimental_attachments
|
|
1889
|
+
};
|
|
1890
|
+
}
|
|
1891
|
+
const fileUrls = /* @__PURE__ */ new Set();
|
|
1892
|
+
for (const part of v2Msg.content.parts) {
|
|
1893
|
+
switch (part.type) {
|
|
1894
|
+
case "step-start":
|
|
1895
|
+
case "text":
|
|
1896
|
+
parts.push(part);
|
|
1897
|
+
break;
|
|
1898
|
+
case "tool-invocation":
|
|
1899
|
+
if (part.toolInvocation.state === `result`) {
|
|
1900
|
+
parts.push({
|
|
1901
|
+
type: `tool-${part.toolInvocation.toolName}`,
|
|
1902
|
+
toolCallId: part.toolInvocation.toolCallId,
|
|
1903
|
+
state: "output-available",
|
|
1904
|
+
input: part.toolInvocation.args,
|
|
1905
|
+
output: part.toolInvocation.result,
|
|
1906
|
+
callProviderMetadata: part.providerMetadata
|
|
1907
|
+
});
|
|
1908
|
+
} else {
|
|
1909
|
+
parts.push({
|
|
1910
|
+
type: `tool-${part.toolInvocation.toolName}`,
|
|
1911
|
+
toolCallId: part.toolInvocation.toolCallId,
|
|
1912
|
+
state: part.toolInvocation.state === `call` ? `input-available` : `input-streaming`,
|
|
1913
|
+
input: part.toolInvocation.args
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1916
|
+
break;
|
|
1917
|
+
case "source":
|
|
1918
|
+
parts.push({
|
|
1919
|
+
type: "source-url",
|
|
1920
|
+
sourceId: part.source.id,
|
|
1921
|
+
url: part.source.url,
|
|
1922
|
+
title: part.source.title,
|
|
1923
|
+
providerMetadata: part.source.providerMetadata || part.providerMetadata
|
|
1924
|
+
});
|
|
1925
|
+
break;
|
|
1926
|
+
case "reasoning":
|
|
1927
|
+
const text = part.reasoning || (part.details?.reduce((p, c) => {
|
|
1928
|
+
if (c.type === `text`) return p + c.text;
|
|
1929
|
+
return p;
|
|
1930
|
+
}, "") ?? "");
|
|
1931
|
+
if (text || part.details?.length) {
|
|
1932
|
+
parts.push({
|
|
1933
|
+
type: "reasoning",
|
|
1934
|
+
text: text || "",
|
|
1935
|
+
state: "done",
|
|
1936
|
+
providerMetadata: part.providerMetadata
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
break;
|
|
1940
|
+
case "file":
|
|
1941
|
+
parts.push({
|
|
1942
|
+
type: "file",
|
|
1943
|
+
url: part.data,
|
|
1944
|
+
mediaType: part.mimeType,
|
|
1945
|
+
providerMetadata: part.providerMetadata
|
|
1946
|
+
});
|
|
1947
|
+
fileUrls.add(part.data);
|
|
1948
|
+
break;
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
if (v2Msg.content.content && !v3Msg.content.parts?.some((p) => p.type === `text`)) {
|
|
1952
|
+
v3Msg.content.parts.push({ type: "text", text: v2Msg.content.content });
|
|
1953
|
+
}
|
|
1954
|
+
const attachmentUrls = [];
|
|
1955
|
+
if (v2Msg.content.experimental_attachments?.length) {
|
|
1956
|
+
for (const attachment of v2Msg.content.experimental_attachments) {
|
|
1957
|
+
if (fileUrls.has(attachment.url)) continue;
|
|
1958
|
+
attachmentUrls.push(attachment.url);
|
|
1959
|
+
parts.push({
|
|
1960
|
+
url: attachment.url,
|
|
1961
|
+
mediaType: attachment.contentType || "unknown",
|
|
1962
|
+
type: "file"
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
if (attachmentUrls.length > 0) {
|
|
1967
|
+
v3Msg.content.metadata = {
|
|
1968
|
+
...v3Msg.content.metadata || {},
|
|
1969
|
+
__attachmentUrls: attachmentUrls
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
return v3Msg;
|
|
1973
|
+
}
|
|
1974
|
+
aiV5UIMessagesToAIV5ModelMessages(messages) {
|
|
1975
|
+
return AIV5.convertToModelMessages(this.sanitizeV5UIMessages(messages));
|
|
1976
|
+
}
|
|
1977
|
+
sanitizeV5UIMessages(messages) {
|
|
1978
|
+
const msgs = messages.map((m) => {
|
|
1979
|
+
if (m.parts.length === 0) return false;
|
|
1980
|
+
const safeParts = m.parts.filter((p) => {
|
|
1981
|
+
if (!AIV5.isToolUIPart(p)) return true;
|
|
1982
|
+
return p.state === "output-available" || p.state === "output-error";
|
|
1983
|
+
});
|
|
1984
|
+
if (!safeParts.length) return false;
|
|
1985
|
+
const sanitized = {
|
|
1986
|
+
...m,
|
|
1987
|
+
parts: safeParts.map((part) => {
|
|
1988
|
+
if (AIV5.isToolUIPart(part) && part.state === "output-available") {
|
|
1989
|
+
return {
|
|
1990
|
+
...part,
|
|
1991
|
+
output: typeof part.output === "object" && part.output && "value" in part.output ? part.output.value : part.output
|
|
1992
|
+
};
|
|
1993
|
+
}
|
|
1994
|
+
return part;
|
|
1995
|
+
})
|
|
1996
|
+
};
|
|
1997
|
+
return sanitized;
|
|
1998
|
+
}).filter((m) => Boolean(m));
|
|
1999
|
+
return msgs;
|
|
2000
|
+
}
|
|
2001
|
+
static mastraMessageV3ToAIV5UIMessage(m) {
|
|
2002
|
+
const metadata = {
|
|
2003
|
+
...m.content.metadata || {}
|
|
2004
|
+
};
|
|
2005
|
+
if (m.createdAt) metadata.createdAt = m.createdAt;
|
|
2006
|
+
if (m.threadId) metadata.threadId = m.threadId;
|
|
2007
|
+
if (m.resourceId) metadata.resourceId = m.resourceId;
|
|
2008
|
+
const filteredParts = m.content.parts;
|
|
2009
|
+
return {
|
|
2010
|
+
id: m.id,
|
|
2011
|
+
role: m.role,
|
|
2012
|
+
metadata,
|
|
2013
|
+
parts: filteredParts
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
aiV5ModelMessagesToAIV4CoreMessages(messages, messageSource) {
|
|
2017
|
+
const v3 = messages.map((msg) => this.aiV5ModelMessageToMastraMessageV3(msg, messageSource));
|
|
2018
|
+
const v2 = v3.map(_MessageList.mastraMessageV3ToV2);
|
|
2019
|
+
const ui = v2.map(_MessageList.mastraMessageV2ToAIV4UIMessage);
|
|
2020
|
+
const core = this.aiV4UIMessagesToAIV4CoreMessages(ui);
|
|
2021
|
+
return core;
|
|
2022
|
+
}
|
|
2023
|
+
aiV4CoreMessagesToAIV5ModelMessages(messages, source) {
|
|
2024
|
+
return this.aiV5UIMessagesToAIV5ModelMessages(
|
|
2025
|
+
messages.map((m) => this.aiV4CoreMessageToMastraMessageV2(m, source)).map((m) => this.mastraMessageV2ToMastraMessageV3(m)).map((m) => _MessageList.mastraMessageV3ToAIV5UIMessage(m))
|
|
2026
|
+
);
|
|
2027
|
+
}
|
|
2028
|
+
aiV5UIMessageToMastraMessageV3(message, messageSource) {
|
|
2029
|
+
const content = {
|
|
2030
|
+
format: 3,
|
|
2031
|
+
parts: message.parts,
|
|
2032
|
+
metadata: message.metadata
|
|
2033
|
+
};
|
|
2034
|
+
const metadata = message.metadata;
|
|
2035
|
+
const createdAt = (() => {
|
|
2036
|
+
if ("createdAt" in message && message.createdAt instanceof Date) {
|
|
2037
|
+
return message.createdAt;
|
|
2038
|
+
}
|
|
2039
|
+
if (metadata && "createdAt" in metadata && metadata.createdAt instanceof Date) {
|
|
2040
|
+
return metadata.createdAt;
|
|
2041
|
+
}
|
|
2042
|
+
return void 0;
|
|
2043
|
+
})();
|
|
2044
|
+
if ("metadata" in message && message.metadata) {
|
|
2045
|
+
content.metadata = { ...message.metadata };
|
|
2046
|
+
}
|
|
2047
|
+
return {
|
|
2048
|
+
id: message.id || this.newMessageId(),
|
|
2049
|
+
role: _MessageList.getRole(message),
|
|
2050
|
+
createdAt: this.generateCreatedAt(messageSource, createdAt),
|
|
2051
|
+
threadId: this.memoryInfo?.threadId,
|
|
2052
|
+
resourceId: this.memoryInfo?.resourceId,
|
|
2053
|
+
content
|
|
2054
|
+
};
|
|
2055
|
+
}
|
|
2056
|
+
aiV5ModelMessageToMastraMessageV3(coreMessage, messageSource) {
|
|
2057
|
+
const id = `id` in coreMessage && typeof coreMessage.id === `string` ? coreMessage.id : this.newMessageId();
|
|
2058
|
+
const parts = [];
|
|
2059
|
+
if (messageSource === "input" && coreMessage.role === "user") {
|
|
2060
|
+
parts.push({ type: "step-start" });
|
|
2061
|
+
}
|
|
2062
|
+
if (typeof coreMessage.content === "string") {
|
|
2063
|
+
parts.push({
|
|
2064
|
+
type: "text",
|
|
2065
|
+
text: coreMessage.content
|
|
2066
|
+
});
|
|
2067
|
+
} else if (Array.isArray(coreMessage.content)) {
|
|
2068
|
+
for (const part of coreMessage.content) {
|
|
2069
|
+
switch (part.type) {
|
|
2070
|
+
case "text":
|
|
2071
|
+
parts.push({
|
|
2072
|
+
type: "text",
|
|
2073
|
+
text: part.text,
|
|
2074
|
+
providerMetadata: part.providerOptions
|
|
2075
|
+
});
|
|
2076
|
+
break;
|
|
2077
|
+
case "tool-call":
|
|
2078
|
+
parts.push({
|
|
2079
|
+
type: "dynamic-tool",
|
|
2080
|
+
toolName: part.toolName,
|
|
2081
|
+
state: "input-available",
|
|
2082
|
+
toolCallId: part.toolCallId,
|
|
2083
|
+
input: part.input
|
|
2084
|
+
});
|
|
2085
|
+
break;
|
|
2086
|
+
case "tool-result":
|
|
2087
|
+
parts.push({
|
|
2088
|
+
type: "dynamic-tool",
|
|
2089
|
+
toolName: part.toolName,
|
|
2090
|
+
state: "output-available",
|
|
2091
|
+
toolCallId: part.toolCallId,
|
|
2092
|
+
output: typeof part.output === "string" ? { type: "text", value: part.output } : part.output ?? { type: "text", value: "" },
|
|
2093
|
+
input: {},
|
|
2094
|
+
callProviderMetadata: part.providerOptions
|
|
2095
|
+
});
|
|
2096
|
+
break;
|
|
2097
|
+
case "reasoning":
|
|
2098
|
+
parts.push({
|
|
2099
|
+
type: "reasoning",
|
|
2100
|
+
text: part.text,
|
|
2101
|
+
providerMetadata: part.providerOptions
|
|
2102
|
+
});
|
|
2103
|
+
break;
|
|
2104
|
+
case "image":
|
|
2105
|
+
parts.push({
|
|
2106
|
+
type: "file",
|
|
2107
|
+
url: part.image.toString(),
|
|
2108
|
+
mediaType: part.mediaType || "unknown",
|
|
2109
|
+
providerMetadata: part.providerOptions
|
|
2110
|
+
});
|
|
2111
|
+
break;
|
|
2112
|
+
case "file":
|
|
2113
|
+
if (part.data instanceof URL) {
|
|
2114
|
+
parts.push({
|
|
2115
|
+
type: "file",
|
|
2116
|
+
url: part.data.toString(),
|
|
2117
|
+
mediaType: part.mediaType,
|
|
2118
|
+
providerMetadata: part.providerOptions
|
|
2119
|
+
});
|
|
2120
|
+
} else {
|
|
2121
|
+
try {
|
|
2122
|
+
parts.push({
|
|
2123
|
+
type: "file",
|
|
2124
|
+
mediaType: part.mediaType,
|
|
2125
|
+
url: convertDataContentToBase64String(part.data),
|
|
2126
|
+
providerMetadata: part.providerOptions
|
|
2127
|
+
});
|
|
2128
|
+
} catch (error) {
|
|
2129
|
+
console.error(`Failed to convert binary data to base64 in CoreMessage file part: ${error}`, error);
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
break;
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
const content = {
|
|
2137
|
+
format: 3,
|
|
2138
|
+
parts
|
|
2139
|
+
};
|
|
2140
|
+
if (coreMessage.content) {
|
|
2141
|
+
content.metadata = {
|
|
2142
|
+
...content.metadata || {},
|
|
2143
|
+
__originalContent: coreMessage.content
|
|
2144
|
+
};
|
|
2145
|
+
}
|
|
2146
|
+
return {
|
|
2147
|
+
id,
|
|
2148
|
+
role: _MessageList.getRole(coreMessage),
|
|
2149
|
+
createdAt: this.generateCreatedAt(messageSource),
|
|
2150
|
+
threadId: this.memoryInfo?.threadId,
|
|
2151
|
+
resourceId: this.memoryInfo?.resourceId,
|
|
2152
|
+
content
|
|
2153
|
+
};
|
|
2154
|
+
}
|
|
2155
|
+
static hasAIV5UIMessageCharacteristics(msg) {
|
|
2156
|
+
if (`toolInvocations` in msg || `reasoning` in msg || `experimental_attachments` in msg || `data` in msg || `annotations` in msg)
|
|
2157
|
+
return false;
|
|
2158
|
+
if (!msg.parts) return false;
|
|
2159
|
+
for (const part of msg.parts) {
|
|
2160
|
+
if (`metadata` in part) return true;
|
|
2161
|
+
if (`toolInvocation` in part) return false;
|
|
2162
|
+
if (`toolCallId` in part) return true;
|
|
2163
|
+
if (part.type === `source`) return false;
|
|
2164
|
+
if (part.type === `source-url`) return true;
|
|
2165
|
+
if (part.type === `reasoning`) {
|
|
2166
|
+
if (`state` in part || `text` in part) return true;
|
|
2167
|
+
if (`reasoning` in part || `details` in part) return false;
|
|
2168
|
+
}
|
|
2169
|
+
if (part.type === `file` && `mediaType` in part) return true;
|
|
2170
|
+
}
|
|
2171
|
+
return false;
|
|
2172
|
+
}
|
|
2173
|
+
static isAIV5UIMessage(msg) {
|
|
2174
|
+
return !_MessageList.isMastraMessage(msg) && !_MessageList.isAIV5CoreMessage(msg) && `parts` in msg && _MessageList.hasAIV5UIMessageCharacteristics(msg);
|
|
2175
|
+
}
|
|
2176
|
+
static hasAIV5CoreMessageCharacteristics(msg) {
|
|
2177
|
+
if (`experimental_providerMetadata` in msg) return false;
|
|
2178
|
+
if (typeof msg.content === `string`) return false;
|
|
2179
|
+
for (const part of msg.content) {
|
|
2180
|
+
if (part.type === `tool-result` && `output` in part) return true;
|
|
2181
|
+
if (part.type === `tool-call` && `input` in part) return true;
|
|
2182
|
+
if (part.type === `tool-result` && `result` in part) return false;
|
|
2183
|
+
if (part.type === `tool-call` && `args` in part) return false;
|
|
2184
|
+
if (`mediaType` in part) return true;
|
|
2185
|
+
if (`mimeType` in part) return false;
|
|
2186
|
+
if (`experimental_providerMetadata` in part) return false;
|
|
2187
|
+
if (part.type === `reasoning` && `signature` in part) return false;
|
|
2188
|
+
if (part.type === `redacted-reasoning`) return false;
|
|
2189
|
+
}
|
|
2190
|
+
return false;
|
|
2191
|
+
}
|
|
2192
|
+
static cacheKeyFromAIV5Parts(parts) {
|
|
2193
|
+
let key = ``;
|
|
2194
|
+
for (const part of parts) {
|
|
2195
|
+
key += part.type;
|
|
2196
|
+
if (part.type === `text`) {
|
|
2197
|
+
key += part.text;
|
|
2198
|
+
}
|
|
2199
|
+
if (AIV5.isToolUIPart(part) || part.type === "dynamic-tool") {
|
|
2200
|
+
key += part.toolCallId;
|
|
2201
|
+
key += part.state;
|
|
2202
|
+
}
|
|
2203
|
+
if (part.type === `reasoning`) {
|
|
2204
|
+
key += part.text;
|
|
2205
|
+
}
|
|
2206
|
+
if (part.type === `file`) {
|
|
2207
|
+
key += part.url.length;
|
|
2208
|
+
key += part.mediaType;
|
|
2209
|
+
key += part.filename || "";
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
return key;
|
|
2213
|
+
}
|
|
2214
|
+
static cacheKeyFromAIV5ModelMessageContent(content) {
|
|
2215
|
+
if (typeof content === `string`) return content;
|
|
2216
|
+
let key = ``;
|
|
2217
|
+
for (const part of content) {
|
|
2218
|
+
key += part.type;
|
|
2219
|
+
if (part.type === `text`) {
|
|
2220
|
+
key += part.text.length;
|
|
2221
|
+
}
|
|
2222
|
+
if (part.type === `reasoning`) {
|
|
2223
|
+
key += part.text.length;
|
|
2224
|
+
}
|
|
2225
|
+
if (part.type === `tool-call`) {
|
|
2226
|
+
key += part.toolCallId;
|
|
2227
|
+
key += part.toolName;
|
|
2228
|
+
}
|
|
2229
|
+
if (part.type === `tool-result`) {
|
|
2230
|
+
key += part.toolCallId;
|
|
2231
|
+
key += part.toolName;
|
|
2232
|
+
}
|
|
2233
|
+
if (part.type === `file`) {
|
|
2234
|
+
key += part.filename;
|
|
2235
|
+
key += part.mediaType;
|
|
2236
|
+
}
|
|
2237
|
+
if (part.type === `image`) {
|
|
2238
|
+
key += part.image instanceof URL ? part.image.toString() : part.image.toString().length;
|
|
2239
|
+
key += part.mediaType;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return key;
|
|
2243
|
+
}
|
|
1236
2244
|
};
|
|
1237
2245
|
|
|
1238
|
-
export { MessageList };
|
|
1239
|
-
//# sourceMappingURL=chunk-
|
|
1240
|
-
//# sourceMappingURL=chunk-
|
|
2246
|
+
export { DefaultGeneratedFile, DefaultGeneratedFileWithType, MessageList };
|
|
2247
|
+
//# sourceMappingURL=chunk-OPT2L5AM.js.map
|
|
2248
|
+
//# sourceMappingURL=chunk-OPT2L5AM.js.map
|