@mastra/core 0.16.3-alpha.0 → 0.16.3
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/CHANGELOG.md +50 -0
- package/dist/agent/index.cjs +11 -11
- package/dist/agent/index.js +2 -2
- package/dist/agent/input-processor/index.cjs +6 -6
- package/dist/agent/input-processor/index.js +1 -1
- package/dist/{chunk-GOA5GU6X.cjs → chunk-2LEMTQEN.cjs} +4 -4
- package/dist/{chunk-GOA5GU6X.cjs.map → chunk-2LEMTQEN.cjs.map} +1 -1
- package/dist/{chunk-PUTAA3ZN.cjs → chunk-37GX66ZQ.cjs} +7 -7
- package/dist/{chunk-PUTAA3ZN.cjs.map → chunk-37GX66ZQ.cjs.map} +1 -1
- package/dist/{chunk-L52VPDJB.cjs → chunk-3CYEVZGC.cjs} +4 -4
- package/dist/{chunk-L52VPDJB.cjs.map → chunk-3CYEVZGC.cjs.map} +1 -1
- package/dist/{chunk-ASFX33CM.js → chunk-46ZMVRXN.js} +4 -4
- package/dist/{chunk-ASFX33CM.js.map → chunk-46ZMVRXN.js.map} +1 -1
- package/dist/{chunk-6SPMCTI2.js → chunk-6ILOFPTU.js} +3 -3
- package/dist/{chunk-6SPMCTI2.js.map → chunk-6ILOFPTU.js.map} +1 -1
- package/dist/{chunk-3JG5JEKX.js → chunk-7ARB2XCY.js} +3 -3
- package/dist/chunk-7ARB2XCY.js.map +1 -0
- package/dist/{chunk-3WWYKE4R.js → chunk-BE6HGZV7.js} +3 -3
- package/dist/{chunk-3WWYKE4R.js.map → chunk-BE6HGZV7.js.map} +1 -1
- package/dist/{chunk-KETY3ZMA.cjs → chunk-CM4UEIAB.cjs} +21 -21
- package/dist/chunk-CM4UEIAB.cjs.map +1 -0
- package/dist/{chunk-RAR5G5S3.js → chunk-DWGGSV3L.js} +3 -3
- package/dist/{chunk-RAR5G5S3.js.map → chunk-DWGGSV3L.js.map} +1 -1
- package/dist/{chunk-NEBAQHIS.js → chunk-HHRWDBFK.js} +8 -8
- package/dist/chunk-HHRWDBFK.js.map +1 -0
- package/dist/{chunk-HT57W63C.cjs → chunk-I77GWNQI.cjs} +3 -3
- package/dist/chunk-I77GWNQI.cjs.map +1 -0
- package/dist/{chunk-HYKEC3OC.cjs → chunk-PPMRSBCA.cjs} +7 -7
- package/dist/{chunk-HYKEC3OC.cjs.map → chunk-PPMRSBCA.cjs.map} +1 -1
- package/dist/{chunk-4MVIJIOL.js → chunk-SLQGF75B.js} +3 -3
- package/dist/{chunk-4MVIJIOL.js.map → chunk-SLQGF75B.js.map} +1 -1
- package/dist/{chunk-EJB4UMEV.cjs → chunk-VK7LOYGD.cjs} +6 -6
- package/dist/{chunk-EJB4UMEV.cjs.map → chunk-VK7LOYGD.cjs.map} +1 -1
- package/dist/index.cjs +21 -21
- package/dist/index.js +6 -6
- package/dist/loop/index.cjs +2 -2
- package/dist/loop/index.js +1 -1
- package/dist/loop/test-utils/streamObject.d.ts.map +1 -1
- package/dist/loop/workflow/llm-execution.d.ts.map +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +4 -4
- package/dist/memory/index.js +1 -1
- package/dist/network/index.cjs +2 -2
- package/dist/network/index.js +1 -1
- package/dist/network/vNext/index.cjs +29 -29
- package/dist/network/vNext/index.js +2 -2
- package/dist/processors/index.cjs +8 -8
- package/dist/processors/index.js +2 -2
- package/dist/relevance/index.cjs +4 -4
- package/dist/relevance/index.js +1 -1
- package/dist/scores/index.cjs +8 -8
- package/dist/scores/index.js +1 -1
- package/dist/storage/index.cjs +3 -3
- package/dist/storage/index.js +1 -1
- package/dist/stream/base/output.d.ts.map +1 -1
- package/dist/stream/index.cjs +3 -3
- package/dist/stream/index.js +1 -1
- package/dist/workflows/evented/index.cjs +10 -10
- package/dist/workflows/evented/index.js +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/package.json +4 -4
- package/dist/chunk-3JG5JEKX.js.map +0 -1
- package/dist/chunk-HT57W63C.cjs.map +0 -1
- package/dist/chunk-KETY3ZMA.cjs.map +0 -1
- package/dist/chunk-NEBAQHIS.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { augmentWithInit } from './chunk-436FFEF6.js';
|
|
2
|
-
import { MessageList } from './chunk-
|
|
2
|
+
import { MessageList } from './chunk-7ARB2XCY.js';
|
|
3
3
|
import { deepMerge } from './chunk-W5K2KOZX.js';
|
|
4
4
|
import { MastraBase } from './chunk-6GF5M4GX.js';
|
|
5
5
|
|
|
@@ -248,5 +248,5 @@ https://mastra.ai/en/docs/memory/overview`
|
|
|
248
248
|
};
|
|
249
249
|
|
|
250
250
|
export { MastraMemory, MemoryProcessor, memoryDefaultOptions };
|
|
251
|
-
//# sourceMappingURL=chunk-
|
|
252
|
-
//# sourceMappingURL=chunk-
|
|
251
|
+
//# sourceMappingURL=chunk-DWGGSV3L.js.map
|
|
252
|
+
//# sourceMappingURL=chunk-DWGGSV3L.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/memory/memory.ts"],"names":[],"mappings":";;;;;;AA6BO,IAAe,eAAA,GAAf,cAAuC,UAAA,CAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMvD,OAAA,CAAQ,UAAyB,KAAA,EAAoE;AACnG,IAAA,OAAO,QAAA;AAAA,EACT;AACF;AAEO,IAAM,oBAAA,GAAuB;AAAA,EAClC,YAAA,EAAc,EAAA;AAAA,EACd,cAAA,EAAgB,KAAA;AAAA,EAChB,OAAA,EAAS;AAAA,IACP,aAAA,EAAe;AAAA,GACjB;AAAA,EACA,aAAA,EAAe;AAAA,IACb,OAAA,EAAS,KAAA;AAAA,IACT,QAAA,EAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA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OAAA;AAAA,IACA,IAAA;AAAA,IACA,IAAA;AAAA,IACA,SAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACF,EAU6B;AAC3B,IAAA,MAAM,OAAA,GAA2B;AAAA,MAC/B,EAAA,EAAI,KAAK,UAAA,EAAW;AAAA,MACpB,OAAA;AAAA,MACA,IAAA;AAAA,MACA,SAAA,sBAAe,IAAA,EAAK;AAAA,MACpB,QAAA;AAAA,MACA,UAAA;AAAA,MACA,IAAA;AAAA,MACA,SAAA;AAAA,MACA,YAAA;AAAA,MACA;AAAA,KACF;AAEA,IAAA,MAAM,aAAA,GAAgB,MAAM,IAAA,CAAK,YAAA,CAAa,EAAE,QAAA,EAAU,CAAC,OAAO,CAAA,EAAG,YAAA,EAAc,MAAA,EAAQ,CAAA;AAC3F,IAAA,MAAM,IAAA,GAAO,IAAI,WAAA,CAAY,EAAE,QAAA,EAAU,UAAA,EAAY,CAAA,CAAE,GAAA,CAAI,aAAA,CAAc,CAAC,CAAA,EAAI,QAAQ,CAAA;AACtF,IAAA,OAAO,IAAA,CAAK,GAAA,CAAI,GAAA,CAAI,EAAA,GAAK,CAAC,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,UAAA,GAAqB;AAC1B,IAAA,OAAO,IAAA,CAAK,OAAA,EAAS,UAAA,EAAW,IAAK,OAAO,UAAA,EAAW;AAAA,EACzD;AAiEF","file":"chunk-RAR5G5S3.js","sourcesContent":["import type { EmbeddingModelV2 } from '@ai-sdk/provider-v5';\nimport type { AssistantContent, UserContent, CoreMessage, EmbeddingModel } from 'ai';\nimport { MessageList } from '../agent/message-list';\nimport type { MastraMessageV2, UIMessageWithMetadata } from '../agent/message-list';\nimport { MastraBase } from '../base';\nimport type { Mastra } from '../mastra';\nimport type { MastraStorage, PaginationInfo, StorageGetMessagesArg, ThreadSortOptions } from '../storage';\nimport { augmentWithInit } from '../storage/storageWithInit';\nimport type { ToolAction } from '../tools';\nimport { deepMerge } from '../utils';\nimport type { MastraVector } from '../vector';\n\nimport type {\n SharedMemoryConfig,\n StorageThreadType,\n MemoryConfig,\n MastraMessageV1,\n WorkingMemoryTemplate,\n} from './types';\n\nexport type MemoryProcessorOpts = {\n systemMessage?: string;\n memorySystemMessage?: string;\n newMessages?: CoreMessage[];\n};\n/**\n * Interface for message processors that can filter or transform messages\n * before they're sent to the LLM.\n */\nexport abstract class MemoryProcessor extends MastraBase {\n /**\n * Process a list of messages and return a filtered or transformed list.\n * @param messages The messages to process\n * @returns The processed messages\n */\n process(messages: CoreMessage[], _opts: MemoryProcessorOpts): CoreMessage[] | Promise<CoreMessage[]> {\n return messages;\n }\n}\n\nexport const memoryDefaultOptions = {\n lastMessages: 10,\n semanticRecall: false,\n threads: {\n generateTitle: false,\n },\n workingMemory: {\n enabled: false,\n template: `\n# User Information\n- **First Name**: \n- **Last Name**: \n- **Location**: \n- **Occupation**: \n- **Interests**: \n- **Goals**: \n- **Events**: \n- **Facts**: \n- **Projects**: \n`,\n },\n} satisfies MemoryConfig;\n\n/**\n * Abstract Memory class that defines the interface for storing and retrieving\n * conversation threads and messages.\n */\nexport abstract class MastraMemory extends MastraBase {\n MAX_CONTEXT_TOKENS?: number;\n\n protected _storage?: MastraStorage;\n vector?: MastraVector;\n embedder?: EmbeddingModel<string> | EmbeddingModelV2<string>;\n private processors: MemoryProcessor[] = [];\n protected threadConfig: MemoryConfig = { ...memoryDefaultOptions };\n #mastra?: Mastra;\n\n constructor(config: { name: string } & SharedMemoryConfig) {\n super({ component: 'MEMORY', name: config.name });\n\n if (config.options) this.threadConfig = this.getMergedThreadConfig(config.options);\n if (config.processors) this.processors = config.processors;\n if (config.storage) {\n this._storage = augmentWithInit(config.storage);\n this._hasOwnStorage = true;\n }\n\n if (this.threadConfig.semanticRecall) {\n if (!config.vector) {\n throw new Error(\n `Semantic recall requires a vector store to be configured.\\n\\nhttps://mastra.ai/en/docs/memory/semantic-recall`,\n );\n }\n this.vector = config.vector;\n\n if (!config.embedder) {\n throw new Error(\n `Semantic recall requires an embedder to be configured.\\n\\nhttps://mastra.ai/en/docs/memory/semantic-recall`,\n );\n }\n this.embedder = config.embedder;\n }\n }\n\n /**\n * Internal method used by Mastra to register itself with the memory.\n * @param mastra The Mastra instance.\n * @internal\n */\n __registerMastra(mastra: Mastra): void {\n this.#mastra = mastra;\n }\n\n protected _hasOwnStorage = false;\n get hasOwnStorage() {\n return this._hasOwnStorage;\n }\n\n get storage() {\n if (!this._storage) {\n throw new Error(\n `Memory requires a storage provider to function. Add a storage configuration to Memory or to your Mastra instance.\\n\\nhttps://mastra.ai/en/docs/memory/overview`,\n );\n }\n return this._storage;\n }\n\n public setStorage(storage: MastraStorage) {\n this._storage = augmentWithInit(storage);\n }\n\n public setVector(vector: MastraVector) {\n this.vector = vector;\n }\n\n public setEmbedder(embedder: EmbeddingModel<string>) {\n this.embedder = embedder;\n }\n\n /**\n * Get a system message to inject into the conversation.\n * This will be called before each conversation turn.\n * Implementations can override this to inject custom system messages.\n */\n public async getSystemMessage(_input: {\n threadId: string;\n resourceId?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<string | null> {\n return null;\n }\n\n /**\n * Get tools that should be available to the agent.\n * This will be called when converting tools for the agent.\n * Implementations can override this to provide additional tools.\n */\n public getTools(_config?: MemoryConfig): Record<string, ToolAction<any, any, any>> {\n return {};\n }\n\n protected async createEmbeddingIndex(dimensions?: number): Promise<{ indexName: string }> {\n const defaultDimensions = 1536;\n const isDefault = dimensions === defaultDimensions;\n const usedDimensions = dimensions ?? defaultDimensions;\n const separator = this.vector?.indexSeparator ?? '_';\n const indexName = isDefault\n ? `memory${separator}messages`\n : `memory${separator}messages${separator}${usedDimensions}`;\n\n if (typeof this.vector === `undefined`) {\n throw new Error(`Tried to create embedding index but no vector db is attached to this Memory instance.`);\n }\n await this.vector.createIndex({\n indexName,\n dimension: usedDimensions,\n });\n return { indexName };\n }\n\n public getMergedThreadConfig(config?: MemoryConfig): MemoryConfig {\n if (config?.workingMemory && 'use' in config.workingMemory) {\n throw new Error('The workingMemory.use option has been removed. Working memory always uses tool-call mode.');\n }\n const mergedConfig = deepMerge(this.threadConfig, config || {});\n\n if (config?.workingMemory?.schema) {\n if (mergedConfig.workingMemory) {\n mergedConfig.workingMemory.schema = config.workingMemory.schema;\n }\n }\n\n return mergedConfig;\n }\n\n /**\n * Apply all configured message processors to a list of messages.\n * @param messages The messages to process\n * @returns The processed messages\n */\n protected async applyProcessors(\n messages: CoreMessage[],\n opts: {\n processors?: MemoryProcessor[];\n } & MemoryProcessorOpts,\n ): Promise<CoreMessage[]> {\n const processors = opts.processors || this.processors;\n if (!processors || processors.length === 0) {\n return messages;\n }\n\n let processedMessages = [...messages];\n\n for (const processor of processors) {\n processedMessages = await processor.process(processedMessages, {\n systemMessage: opts.systemMessage,\n newMessages: opts.newMessages,\n memorySystemMessage: opts.memorySystemMessage,\n });\n }\n\n return processedMessages;\n }\n\n processMessages({\n messages,\n processors,\n ...opts\n }: {\n messages: CoreMessage[];\n processors?: MemoryProcessor[];\n } & MemoryProcessorOpts) {\n return this.applyProcessors(messages, { processors: processors || this.processors, ...opts });\n }\n\n abstract rememberMessages({\n threadId,\n resourceId,\n vectorMessageSearch,\n config,\n }: {\n threadId: string;\n resourceId?: string;\n vectorMessageSearch?: string;\n config?: MemoryConfig;\n }): Promise<{ messages: MastraMessageV1[]; messagesV2: MastraMessageV2[] }>;\n\n estimateTokens(text: string): number {\n return Math.ceil(text.split(' ').length * 1.3);\n }\n\n /**\n * Retrieves a specific thread by its ID\n * @param threadId - The unique identifier of the thread\n * @returns Promise resolving to the thread or null if not found\n */\n abstract getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null>;\n\n /**\n * Retrieves all threads that belong to the specified resource.\n * @param resourceId - The unique identifier of the resource\n * @param orderBy - Which timestamp field to sort by (`'createdAt'` or `'updatedAt'`);\n * defaults to `'createdAt'`\n * @param sortDirection - Sort order for the results (`'ASC'` or `'DESC'`);\n * defaults to `'DESC'`\n * @returns Promise resolving to an array of matching threads; resolves to an empty array\n * if the resource has no threads\n */\n abstract getThreadsByResourceId({\n resourceId,\n orderBy,\n sortDirection,\n }: { resourceId: string } & ThreadSortOptions): Promise<StorageThreadType[]>;\n\n abstract getThreadsByResourceIdPaginated(\n args: {\n resourceId: string;\n page: number;\n perPage: number;\n } & ThreadSortOptions,\n ): Promise<PaginationInfo & { threads: StorageThreadType[] }>;\n\n /**\n * Saves or updates a thread\n * @param thread - The thread data to save\n * @returns Promise resolving to the saved thread\n */\n abstract saveThread({\n thread,\n memoryConfig,\n }: {\n thread: StorageThreadType;\n memoryConfig?: MemoryConfig;\n }): Promise<StorageThreadType>;\n\n /**\n * Saves messages to a thread\n * @param messages - Array of messages to save\n * @returns Promise resolving to the saved messages\n */\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format?: 'v1';\n }): Promise<MastraMessageV1[]>;\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format: 'v2';\n }): Promise<MastraMessageV2[]>;\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format?: 'v1' | 'v2';\n }): Promise<MastraMessageV2[] | MastraMessageV1[]>;\n\n /**\n * Retrieves all messages for a specific thread\n * @param threadId - The unique identifier of the thread\n * @returns Promise resolving to array of messages and uiMessages\n */\n abstract query({\n threadId,\n resourceId,\n selectBy,\n }: StorageGetMessagesArg): Promise<{ messages: CoreMessage[]; uiMessages: UIMessageWithMetadata[] }>;\n\n /**\n * Helper method to create a new thread\n * @param title - Optional title for the thread\n * @param metadata - Optional metadata for the thread\n * @returns Promise resolving to the created thread\n */\n async createThread({\n threadId,\n resourceId,\n title,\n metadata,\n memoryConfig,\n saveThread = true,\n }: {\n resourceId: string;\n threadId?: string;\n title?: string;\n metadata?: Record<string, unknown>;\n memoryConfig?: MemoryConfig;\n saveThread?: boolean;\n }): Promise<StorageThreadType> {\n const thread: StorageThreadType = {\n id: threadId || this.generateId(),\n title: title || `New Thread ${new Date().toISOString()}`,\n resourceId,\n createdAt: new Date(),\n updatedAt: new Date(),\n metadata,\n };\n\n return saveThread ? this.saveThread({ thread, memoryConfig }) : thread;\n }\n\n /**\n * Helper method to delete a thread\n * @param threadId - the id of the thread to delete\n */\n abstract deleteThread(threadId: string): Promise<void>;\n\n /**\n * Helper method to add a single message to a thread\n * @param threadId - The thread to add the message to\n * @param content - The message content\n * @param role - The role of the message sender\n * @param type - The type of the message\n * @param toolNames - Optional array of tool names that were called\n * @param toolCallArgs - Optional array of tool call arguments\n * @param toolCallIds - Optional array of tool call ids\n * @returns Promise resolving to the saved message\n * @deprecated use saveMessages instead\n */\n async addMessage({\n threadId,\n resourceId,\n config,\n content,\n role,\n type,\n toolNames,\n toolCallArgs,\n toolCallIds,\n }: {\n threadId: string;\n resourceId: string;\n config?: MemoryConfig;\n content: UserContent | AssistantContent;\n role: 'user' | 'assistant';\n type: 'text' | 'tool-call' | 'tool-result';\n toolNames?: string[];\n toolCallArgs?: Record<string, unknown>[];\n toolCallIds?: string[];\n }): Promise<MastraMessageV1> {\n const message: MastraMessageV1 = {\n id: this.generateId(),\n content,\n role,\n createdAt: new Date(),\n threadId,\n resourceId,\n type,\n toolNames,\n toolCallArgs,\n toolCallIds,\n };\n\n const savedMessages = await this.saveMessages({ messages: [message], memoryConfig: config });\n const list = new MessageList({ threadId, resourceId }).add(savedMessages[0]!, 'memory');\n return list.get.all.v1()[0]!;\n }\n\n /**\n * Generates a unique identifier\n * @returns A unique string ID\n */\n public generateId(): string {\n return this.#mastra?.generateId() || crypto.randomUUID();\n }\n\n /**\n * Retrieves working memory for a specific thread\n * @param threadId - The unique identifier of the thread\n * @param resourceId - The unique identifier of the resource\n * @param memoryConfig - Optional memory configuration\n * @returns Promise resolving to working memory data or null if not found\n */\n abstract getWorkingMemory({\n threadId,\n resourceId,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<string | null>;\n\n /**\n * Retrieves working memory template for a specific thread\n * @param memoryConfig - Optional memory configuration\n * @returns Promise resolving to working memory template or null if not found\n */\n abstract getWorkingMemoryTemplate({\n memoryConfig,\n }?: {\n memoryConfig?: MemoryConfig;\n }): Promise<WorkingMemoryTemplate | null>;\n\n abstract updateWorkingMemory({\n threadId,\n resourceId,\n workingMemory,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n workingMemory: string;\n memoryConfig?: MemoryConfig;\n }): Promise<void>;\n\n /**\n * @warning experimental! can be removed or changed at any time\n */\n abstract __experimental_updateWorkingMemoryVNext({\n threadId,\n resourceId,\n workingMemory,\n searchString,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n workingMemory: string;\n searchString?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<{ success: boolean; reason: string }>;\n\n /**\n * Deletes multiple messages by their IDs\n * @param messageIds - Array of message IDs to delete\n * @returns Promise that resolves when all messages are deleted\n */\n abstract deleteMessages(messageIds: string[]): Promise<void>;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/memory/memory.ts"],"names":[],"mappings":";;;;;;AA6BO,IAAe,eAAA,GAAf,cAAuC,UAAA,CAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMvD,OAAA,CAAQ,UAAyB,KAAA,EAAoE;AACnG,IAAA,OAAO,QAAA;AAAA,EACT;AACF;AAEO,IAAM,oBAAA,GAAuB;AAAA,EAClC,YAAA,EAAc,EAAA;AAAA,EACd,cAAA,EAAgB,KAAA;AAAA,EAChB,OAAA,EAAS;AAAA,IACP,aAAA,EAAe;AAAA,GACjB;AAAA,EACA,aAAA,EAAe;AAAA,IACb,OAAA,EAAS,KAAA;AAAA,IACT,QAAA,EAAU;A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UAAA,CAAW;AAAA,IACf,QAAA;AAAA,IACA,UAAA;AAAA,IACA,MAAA;AAAA,IACA,OAAA;AAAA,IACA,IAAA;AAAA,IACA,IAAA;AAAA,IACA,SAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACF,EAU6B;AAC3B,IAAA,MAAM,OAAA,GAA2B;AAAA,MAC/B,EAAA,EAAI,KAAK,UAAA,EAAW;AAAA,MACpB,OAAA;AAAA,MACA,IAAA;AAAA,MACA,SAAA,sBAAe,IAAA,EAAK;AAAA,MACpB,QAAA;AAAA,MACA,UAAA;AAAA,MACA,IAAA;AAAA,MACA,SAAA;AAAA,MACA,YAAA;AAAA,MACA;AAAA,KACF;AAEA,IAAA,MAAM,aAAA,GAAgB,MAAM,IAAA,CAAK,YAAA,CAAa,EAAE,QAAA,EAAU,CAAC,OAAO,CAAA,EAAG,YAAA,EAAc,MAAA,EAAQ,CAAA;AAC3F,IAAA,MAAM,IAAA,GAAO,IAAI,WAAA,CAAY,EAAE,QAAA,EAAU,UAAA,EAAY,CAAA,CAAE,GAAA,CAAI,aAAA,CAAc,CAAC,CAAA,EAAI,QAAQ,CAAA;AACtF,IAAA,OAAO,IAAA,CAAK,GAAA,CAAI,GAAA,CAAI,EAAA,GAAK,CAAC,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,UAAA,GAAqB;AAC1B,IAAA,OAAO,IAAA,CAAK,OAAA,EAAS,UAAA,EAAW,IAAK,OAAO,UAAA,EAAW;AAAA,EACzD;AAiEF","file":"chunk-DWGGSV3L.js","sourcesContent":["import type { EmbeddingModelV2 } from '@ai-sdk/provider-v5';\nimport type { AssistantContent, UserContent, CoreMessage, EmbeddingModel } from 'ai';\nimport { MessageList } from '../agent/message-list';\nimport type { MastraMessageV2, UIMessageWithMetadata } from '../agent/message-list';\nimport { MastraBase } from '../base';\nimport type { Mastra } from '../mastra';\nimport type { MastraStorage, PaginationInfo, StorageGetMessagesArg, ThreadSortOptions } from '../storage';\nimport { augmentWithInit } from '../storage/storageWithInit';\nimport type { ToolAction } from '../tools';\nimport { deepMerge } from '../utils';\nimport type { MastraVector } from '../vector';\n\nimport type {\n SharedMemoryConfig,\n StorageThreadType,\n MemoryConfig,\n MastraMessageV1,\n WorkingMemoryTemplate,\n} from './types';\n\nexport type MemoryProcessorOpts = {\n systemMessage?: string;\n memorySystemMessage?: string;\n newMessages?: CoreMessage[];\n};\n/**\n * Interface for message processors that can filter or transform messages\n * before they're sent to the LLM.\n */\nexport abstract class MemoryProcessor extends MastraBase {\n /**\n * Process a list of messages and return a filtered or transformed list.\n * @param messages The messages to process\n * @returns The processed messages\n */\n process(messages: CoreMessage[], _opts: MemoryProcessorOpts): CoreMessage[] | Promise<CoreMessage[]> {\n return messages;\n }\n}\n\nexport const memoryDefaultOptions = {\n lastMessages: 10,\n semanticRecall: false,\n threads: {\n generateTitle: false,\n },\n workingMemory: {\n enabled: false,\n template: `\n# User Information\n- **First Name**: \n- **Last Name**: \n- **Location**: \n- **Occupation**: \n- **Interests**: \n- **Goals**: \n- **Events**: \n- **Facts**: \n- **Projects**: \n`,\n },\n} satisfies MemoryConfig;\n\n/**\n * Abstract Memory class that defines the interface for storing and retrieving\n * conversation threads and messages.\n */\nexport abstract class MastraMemory extends MastraBase {\n MAX_CONTEXT_TOKENS?: number;\n\n protected _storage?: MastraStorage;\n vector?: MastraVector;\n embedder?: EmbeddingModel<string> | EmbeddingModelV2<string>;\n private processors: MemoryProcessor[] = [];\n protected threadConfig: MemoryConfig = { ...memoryDefaultOptions };\n #mastra?: Mastra;\n\n constructor(config: { name: string } & SharedMemoryConfig) {\n super({ component: 'MEMORY', name: config.name });\n\n if (config.options) this.threadConfig = this.getMergedThreadConfig(config.options);\n if (config.processors) this.processors = config.processors;\n if (config.storage) {\n this._storage = augmentWithInit(config.storage);\n this._hasOwnStorage = true;\n }\n\n if (this.threadConfig.semanticRecall) {\n if (!config.vector) {\n throw new Error(\n `Semantic recall requires a vector store to be configured.\\n\\nhttps://mastra.ai/en/docs/memory/semantic-recall`,\n );\n }\n this.vector = config.vector;\n\n if (!config.embedder) {\n throw new Error(\n `Semantic recall requires an embedder to be configured.\\n\\nhttps://mastra.ai/en/docs/memory/semantic-recall`,\n );\n }\n this.embedder = config.embedder;\n }\n }\n\n /**\n * Internal method used by Mastra to register itself with the memory.\n * @param mastra The Mastra instance.\n * @internal\n */\n __registerMastra(mastra: Mastra): void {\n this.#mastra = mastra;\n }\n\n protected _hasOwnStorage = false;\n get hasOwnStorage() {\n return this._hasOwnStorage;\n }\n\n get storage() {\n if (!this._storage) {\n throw new Error(\n `Memory requires a storage provider to function. Add a storage configuration to Memory or to your Mastra instance.\\n\\nhttps://mastra.ai/en/docs/memory/overview`,\n );\n }\n return this._storage;\n }\n\n public setStorage(storage: MastraStorage) {\n this._storage = augmentWithInit(storage);\n }\n\n public setVector(vector: MastraVector) {\n this.vector = vector;\n }\n\n public setEmbedder(embedder: EmbeddingModel<string>) {\n this.embedder = embedder;\n }\n\n /**\n * Get a system message to inject into the conversation.\n * This will be called before each conversation turn.\n * Implementations can override this to inject custom system messages.\n */\n public async getSystemMessage(_input: {\n threadId: string;\n resourceId?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<string | null> {\n return null;\n }\n\n /**\n * Get tools that should be available to the agent.\n * This will be called when converting tools for the agent.\n * Implementations can override this to provide additional tools.\n */\n public getTools(_config?: MemoryConfig): Record<string, ToolAction<any, any, any>> {\n return {};\n }\n\n protected async createEmbeddingIndex(dimensions?: number): Promise<{ indexName: string }> {\n const defaultDimensions = 1536;\n const isDefault = dimensions === defaultDimensions;\n const usedDimensions = dimensions ?? defaultDimensions;\n const separator = this.vector?.indexSeparator ?? '_';\n const indexName = isDefault\n ? `memory${separator}messages`\n : `memory${separator}messages${separator}${usedDimensions}`;\n\n if (typeof this.vector === `undefined`) {\n throw new Error(`Tried to create embedding index but no vector db is attached to this Memory instance.`);\n }\n await this.vector.createIndex({\n indexName,\n dimension: usedDimensions,\n });\n return { indexName };\n }\n\n public getMergedThreadConfig(config?: MemoryConfig): MemoryConfig {\n if (config?.workingMemory && 'use' in config.workingMemory) {\n throw new Error('The workingMemory.use option has been removed. Working memory always uses tool-call mode.');\n }\n const mergedConfig = deepMerge(this.threadConfig, config || {});\n\n if (config?.workingMemory?.schema) {\n if (mergedConfig.workingMemory) {\n mergedConfig.workingMemory.schema = config.workingMemory.schema;\n }\n }\n\n return mergedConfig;\n }\n\n /**\n * Apply all configured message processors to a list of messages.\n * @param messages The messages to process\n * @returns The processed messages\n */\n protected async applyProcessors(\n messages: CoreMessage[],\n opts: {\n processors?: MemoryProcessor[];\n } & MemoryProcessorOpts,\n ): Promise<CoreMessage[]> {\n const processors = opts.processors || this.processors;\n if (!processors || processors.length === 0) {\n return messages;\n }\n\n let processedMessages = [...messages];\n\n for (const processor of processors) {\n processedMessages = await processor.process(processedMessages, {\n systemMessage: opts.systemMessage,\n newMessages: opts.newMessages,\n memorySystemMessage: opts.memorySystemMessage,\n });\n }\n\n return processedMessages;\n }\n\n processMessages({\n messages,\n processors,\n ...opts\n }: {\n messages: CoreMessage[];\n processors?: MemoryProcessor[];\n } & MemoryProcessorOpts) {\n return this.applyProcessors(messages, { processors: processors || this.processors, ...opts });\n }\n\n abstract rememberMessages({\n threadId,\n resourceId,\n vectorMessageSearch,\n config,\n }: {\n threadId: string;\n resourceId?: string;\n vectorMessageSearch?: string;\n config?: MemoryConfig;\n }): Promise<{ messages: MastraMessageV1[]; messagesV2: MastraMessageV2[] }>;\n\n estimateTokens(text: string): number {\n return Math.ceil(text.split(' ').length * 1.3);\n }\n\n /**\n * Retrieves a specific thread by its ID\n * @param threadId - The unique identifier of the thread\n * @returns Promise resolving to the thread or null if not found\n */\n abstract getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null>;\n\n /**\n * Retrieves all threads that belong to the specified resource.\n * @param resourceId - The unique identifier of the resource\n * @param orderBy - Which timestamp field to sort by (`'createdAt'` or `'updatedAt'`);\n * defaults to `'createdAt'`\n * @param sortDirection - Sort order for the results (`'ASC'` or `'DESC'`);\n * defaults to `'DESC'`\n * @returns Promise resolving to an array of matching threads; resolves to an empty array\n * if the resource has no threads\n */\n abstract getThreadsByResourceId({\n resourceId,\n orderBy,\n sortDirection,\n }: { resourceId: string } & ThreadSortOptions): Promise<StorageThreadType[]>;\n\n abstract getThreadsByResourceIdPaginated(\n args: {\n resourceId: string;\n page: number;\n perPage: number;\n } & ThreadSortOptions,\n ): Promise<PaginationInfo & { threads: StorageThreadType[] }>;\n\n /**\n * Saves or updates a thread\n * @param thread - The thread data to save\n * @returns Promise resolving to the saved thread\n */\n abstract saveThread({\n thread,\n memoryConfig,\n }: {\n thread: StorageThreadType;\n memoryConfig?: MemoryConfig;\n }): Promise<StorageThreadType>;\n\n /**\n * Saves messages to a thread\n * @param messages - Array of messages to save\n * @returns Promise resolving to the saved messages\n */\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format?: 'v1';\n }): Promise<MastraMessageV1[]>;\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format: 'v2';\n }): Promise<MastraMessageV2[]>;\n abstract saveMessages(args: {\n messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];\n memoryConfig?: MemoryConfig | undefined;\n format?: 'v1' | 'v2';\n }): Promise<MastraMessageV2[] | MastraMessageV1[]>;\n\n /**\n * Retrieves all messages for a specific thread\n * @param threadId - The unique identifier of the thread\n * @returns Promise resolving to array of messages and uiMessages\n */\n abstract query({\n threadId,\n resourceId,\n selectBy,\n }: StorageGetMessagesArg): Promise<{ messages: CoreMessage[]; uiMessages: UIMessageWithMetadata[] }>;\n\n /**\n * Helper method to create a new thread\n * @param title - Optional title for the thread\n * @param metadata - Optional metadata for the thread\n * @returns Promise resolving to the created thread\n */\n async createThread({\n threadId,\n resourceId,\n title,\n metadata,\n memoryConfig,\n saveThread = true,\n }: {\n resourceId: string;\n threadId?: string;\n title?: string;\n metadata?: Record<string, unknown>;\n memoryConfig?: MemoryConfig;\n saveThread?: boolean;\n }): Promise<StorageThreadType> {\n const thread: StorageThreadType = {\n id: threadId || this.generateId(),\n title: title || `New Thread ${new Date().toISOString()}`,\n resourceId,\n createdAt: new Date(),\n updatedAt: new Date(),\n metadata,\n };\n\n return saveThread ? this.saveThread({ thread, memoryConfig }) : thread;\n }\n\n /**\n * Helper method to delete a thread\n * @param threadId - the id of the thread to delete\n */\n abstract deleteThread(threadId: string): Promise<void>;\n\n /**\n * Helper method to add a single message to a thread\n * @param threadId - The thread to add the message to\n * @param content - The message content\n * @param role - The role of the message sender\n * @param type - The type of the message\n * @param toolNames - Optional array of tool names that were called\n * @param toolCallArgs - Optional array of tool call arguments\n * @param toolCallIds - Optional array of tool call ids\n * @returns Promise resolving to the saved message\n * @deprecated use saveMessages instead\n */\n async addMessage({\n threadId,\n resourceId,\n config,\n content,\n role,\n type,\n toolNames,\n toolCallArgs,\n toolCallIds,\n }: {\n threadId: string;\n resourceId: string;\n config?: MemoryConfig;\n content: UserContent | AssistantContent;\n role: 'user' | 'assistant';\n type: 'text' | 'tool-call' | 'tool-result';\n toolNames?: string[];\n toolCallArgs?: Record<string, unknown>[];\n toolCallIds?: string[];\n }): Promise<MastraMessageV1> {\n const message: MastraMessageV1 = {\n id: this.generateId(),\n content,\n role,\n createdAt: new Date(),\n threadId,\n resourceId,\n type,\n toolNames,\n toolCallArgs,\n toolCallIds,\n };\n\n const savedMessages = await this.saveMessages({ messages: [message], memoryConfig: config });\n const list = new MessageList({ threadId, resourceId }).add(savedMessages[0]!, 'memory');\n return list.get.all.v1()[0]!;\n }\n\n /**\n * Generates a unique identifier\n * @returns A unique string ID\n */\n public generateId(): string {\n return this.#mastra?.generateId() || crypto.randomUUID();\n }\n\n /**\n * Retrieves working memory for a specific thread\n * @param threadId - The unique identifier of the thread\n * @param resourceId - The unique identifier of the resource\n * @param memoryConfig - Optional memory configuration\n * @returns Promise resolving to working memory data or null if not found\n */\n abstract getWorkingMemory({\n threadId,\n resourceId,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<string | null>;\n\n /**\n * Retrieves working memory template for a specific thread\n * @param memoryConfig - Optional memory configuration\n * @returns Promise resolving to working memory template or null if not found\n */\n abstract getWorkingMemoryTemplate({\n memoryConfig,\n }?: {\n memoryConfig?: MemoryConfig;\n }): Promise<WorkingMemoryTemplate | null>;\n\n abstract updateWorkingMemory({\n threadId,\n resourceId,\n workingMemory,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n workingMemory: string;\n memoryConfig?: MemoryConfig;\n }): Promise<void>;\n\n /**\n * @warning experimental! can be removed or changed at any time\n */\n abstract __experimental_updateWorkingMemoryVNext({\n threadId,\n resourceId,\n workingMemory,\n searchString,\n memoryConfig,\n }: {\n threadId: string;\n resourceId?: string;\n workingMemory: string;\n searchString?: string;\n memoryConfig?: MemoryConfig;\n }): Promise<{ success: boolean; reason: string }>;\n\n /**\n * Deletes multiple messages by their IDs\n * @param messageIds - Array of message IDs to delete\n * @returns Promise that resolves when all messages are deleted\n */\n abstract deleteMessages(messageIds: string[]): Promise<void>;\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { DefaultVoice } from './chunk-QQIBOVFQ.js';
|
|
|
2
2
|
import { STREAM_FORMAT_SYMBOL, EMITTER_SYMBOL } from './chunk-NLNKQD2T.js';
|
|
3
3
|
import { InstrumentClass, Telemetry } from './chunk-TLJPVRO5.js';
|
|
4
4
|
import { MastraLLMV1 } from './chunk-WAN6BRTN.js';
|
|
5
|
-
import { MessageList, DefaultGeneratedFile, DefaultGeneratedFileWithType } from './chunk-
|
|
5
|
+
import { MessageList, DefaultGeneratedFile, DefaultGeneratedFileWithType } from './chunk-7ARB2XCY.js';
|
|
6
6
|
import { executeHook } from './chunk-TTELJD4F.js';
|
|
7
7
|
import { zodToJsonSchema } from './chunk-PJKCPRYF.js';
|
|
8
8
|
import { getValidTraceId, wrapMastra, selectFields, getOrCreateSpan, ensureToolProperties, makeCoreTool, createMastraProxy, delay } from './chunk-W5K2KOZX.js';
|
|
@@ -100,8 +100,8 @@ steps:new DelayedPromise(),totalUsage:new DelayedPromise(),content:new DelayedPr
|
|
|
100
100
|
*/messageList;/**
|
|
101
101
|
* Trace ID used on the execution (if the execution was traced).
|
|
102
102
|
*/traceId;constructor({model:_model,stream,messageList,options}){super({component:"LLM",name:"MastraModelOutput"});this.#options=options;this.#returnScorerData=!!options.returnScorerData;this.runId=options.runId;this.traceId=getValidTraceId(options.tracingContext?.currentSpan);if(options.outputProcessors?.length){this.processorRunner=new ProcessorRunner({inputProcessors:[],outputProcessors:options.outputProcessors,logger:this.logger,agentName:"MastraModelOutput"});}if(options.outputProcessorRunnerMode){this.outputProcessorRunnerMode=options.outputProcessorRunnerMode;}this.messageList=messageList;const self=this;let processedStream=stream;const processorRunner=this.processorRunner;if(processorRunner&&options.outputProcessorRunnerMode===`stream`){const processorStates=/* @__PURE__ */new Map();processedStream=stream.pipeThrough(new TransformStream({async transform(chunk,controller){const{part:processed,blocked,reason}=await processorRunner.processPart(chunk,processorStates);if(blocked){controller.enqueue({type:"tripwire",payload:{tripwireReason:reason||"Output processor blocked content"}});return;}if(processed){controller.enqueue(processed);}}}));}this.#baseStream=processedStream.pipeThrough(new TransformStream({transform:async(chunk,controller)=>{switch(chunk.type){case "source":self.#bufferedSources.push(chunk);self.#bufferedByStep.sources.push(chunk);break;case "text-delta":self.#bufferedText.push(chunk.payload.text);self.#bufferedByStep.text+=chunk.payload.text;if(chunk.payload.id){const ary=self.#bufferedTextChunks[chunk.payload.id]??[];ary.push(chunk.payload.text);self.#bufferedTextChunks[chunk.payload.id]=ary;}break;case "tool-call-input-streaming-start":self.#toolCallDeltaIdNameMap[chunk.payload.toolCallId]=chunk.payload.toolName;break;case "tool-call-delta":if(!self.#toolCallArgsDeltas[chunk.payload.toolCallId]){self.#toolCallArgsDeltas[chunk.payload.toolCallId]=[];}self.#toolCallArgsDeltas?.[chunk.payload.toolCallId]?.push(chunk.payload.argsTextDelta);chunk.payload.toolName||=self.#toolCallDeltaIdNameMap[chunk.payload.toolCallId];break;case "file":self.#bufferedFiles.push(chunk);self.#bufferedByStep.files.push(chunk);break;case "reasoning-start":self.#bufferedReasoningDetails[chunk.payload.id]={type:"reasoning",text:"",providerMetadata:chunk.payload.providerMetadata||{}};break;case "reasoning-delta":{self.#bufferedReasoning.push(chunk.payload.text);self.#bufferedByStep.reasoning+=chunk.payload.text;const bufferedReasoning=self.#bufferedReasoningDetails[chunk.payload.id];if(bufferedReasoning){bufferedReasoning.text+=chunk.payload.text;if(chunk.payload.providerMetadata){bufferedReasoning.providerMetadata=chunk.payload.providerMetadata;}}break;}case "reasoning-end":{const bufferedReasoning=self.#bufferedReasoningDetails[chunk.payload.id];if(chunk.payload.providerMetadata&&bufferedReasoning){bufferedReasoning.providerMetadata=chunk.payload.providerMetadata;}break;}case "tool-call":self.#toolCalls.push(chunk);self.#bufferedByStep.toolCalls.push(chunk);if(chunk.payload?.output?.from==="AGENT"&&chunk.payload?.output?.type==="finish"){const finishPayload=chunk.payload?.output.payload;self.updateUsageCount(finishPayload.usage);}break;case "tool-result":self.#toolResults.push(chunk);self.#bufferedByStep.toolResults.push(chunk);break;case "step-finish":{self.updateUsageCount(chunk.payload.output.usage);self.#warnings=chunk.payload.stepResult.warnings||[];if(chunk.payload.metadata.request){self.#request=chunk.payload.metadata.request;}const reasoningDetails=reasoningDetailsFromMessages(chunk.payload.messages.all.slice(self.#bufferedByStep.msgCount));const{providerMetadata,request,...otherMetadata}=chunk.payload.metadata;const stepResult={stepType:self.#bufferedSteps.length===0?"initial":"tool-result",text:self.#bufferedByStep.text,reasoning:self.#bufferedByStep.reasoning||void 0,sources:self.#bufferedByStep.sources,files:self.#bufferedByStep.files,toolCalls:self.#bufferedByStep.toolCalls,toolResults:self.#bufferedByStep.toolResults,warnings:self.#warnings,reasoningDetails,providerMetadata,experimental_providerMetadata:providerMetadata,isContinued:chunk.payload.stepResult.isContinued,logprobs:chunk.payload.stepResult.logprobs,finishReason:chunk.payload.stepResult.reason,response:{...otherMetadata,messages:chunk.payload.messages.nonUser},request,usage:chunk.payload.output.usage,// TODO: need to be able to pass a step id into this fn to get the content for a specific step id
|
|
103
|
-
content:messageList.get.response.aiV5.stepContent()};await options?.onStepFinish?.(stepResult);self.#bufferedSteps.push(stepResult);self.#bufferedByStep={text:"",reasoning:"",sources:[],files:[],toolCalls:[],toolResults:[],msgCount:chunk.payload.messages.all.length};break;}case "tripwire":self.#tripwire=true;self.#tripwireReason=chunk.payload?.tripwireReason||"Content blocked";self.#finishReason="other";self.#delayedPromises.text.resolve(self.#bufferedText.join(""));self.#delayedPromises.finishReason.resolve("other");self.#delayedPromises.object.resolve(void 0);self.#delayedPromises.usage.resolve(self.#usageCount);self.#delayedPromises.warnings.resolve(self.#warnings);self.#delayedPromises.providerMetadata.resolve(void 0);self.#delayedPromises.response.resolve({});self.#delayedPromises.request.resolve({});self.#delayedPromises.reasoning.resolve("");self.#delayedPromises.reasoningText.resolve(void 0);self.#delayedPromises.sources.resolve([]);self.#delayedPromises.files.resolve([]);self.#delayedPromises.toolCalls.resolve([]);self.#delayedPromises.toolResults.resolve([]);self.#delayedPromises.steps.resolve(self.#bufferedSteps);self.#delayedPromises.totalUsage.resolve(self.#usageCount);self.#delayedPromises.content.resolve([]);self.#delayedPromises.reasoningDetails.resolve([]);controller.enqueue(chunk);controller.terminate();return;case "finish":if(chunk.payload.stepResult.reason){self.#finishReason=chunk.payload.stepResult.reason;}let response={};if(chunk.payload.metadata){const{providerMetadata,request,...otherMetadata}=chunk.payload.metadata;response={...otherMetadata,messages:messageList.get.response.aiV5.model(),uiMessages:messageList.get.response.aiV5.ui()};}this.populateUsageCount(chunk.payload.output.usage);chunk.payload.output.usage=self.#usageCount;try{if(self.processorRunner&&self.outputProcessorRunnerMode===`result`){self.messageList=await self.processorRunner.runOutputProcessors(self.messageList);const outputText=self.messageList.get.response.aiV4.core().map(m=>MessageList.coreContentToString(m.content)).join("\n");const messages=self.messageList.get.response.v2();const messagesWithStructuredData=messages.filter(msg=>msg.content.metadata&&msg.content.metadata.structuredOutput);if(messagesWithStructuredData[0]&&messagesWithStructuredData[0].content.metadata?.structuredOutput){const structuredOutput=messagesWithStructuredData[0].content.metadata.structuredOutput;self.#delayedPromises.object.resolve(structuredOutput);}else if(!self.#options.output){self.#delayedPromises.object.resolve(void 0);}self.#delayedPromises.text.resolve(outputText);self.#delayedPromises.finishReason.resolve(self.#finishReason);if(chunk.payload.metadata){const{providerMetadata,request,...otherMetadata}=chunk.payload.metadata;response={...otherMetadata,messages:messageList.get.response.aiV5.model(),uiMessages:messageList.get.response.aiV5.ui()};}}else {self.#
|
|
104
|
-
content:messageList.get.response.aiV5.stepContent(),request:await self.request,error:self.error,reasoning:await self.aisdk.v5.reasoning,reasoningText:await self.aisdk.v5.reasoningText,sources:await self.aisdk.v5.sources,files:await self.aisdk.v5.files,steps:transformSteps({steps:self.#bufferedSteps}),response:{...(await self.response),messages:messageList.get.response.aiV5.model()},usage:chunk.payload.output.usage,totalUsage:self.#getTotalUsage(),toolCalls:await self.aisdk.v5.toolCalls,toolResults:await self.aisdk.v5.toolResults,staticToolCalls:(await self.aisdk.v5.toolCalls).filter(toolCall=>toolCall.dynamic===false),staticToolResults:(await self.aisdk.v5.toolResults).filter(toolResult=>toolResult.dynamic===false),dynamicToolCalls:(await self.aisdk.v5.toolCalls).filter(toolCall=>toolCall.dynamic===true),dynamicToolResults:(await self.aisdk.v5.toolResults).filter(toolResult=>toolResult.dynamic===true)};await options?.onFinish?.(onFinishPayload);}if(options?.rootSpan){options.rootSpan.setAttributes({...(baseFinishStep?.usage?.reasoningTokens?{"stream.usage.reasoningTokens":baseFinishStep.usage.reasoningTokens}:{}),...(baseFinishStep?.usage?.totalTokens?{"stream.usage.totalTokens":baseFinishStep.usage.totalTokens}:{}),...(baseFinishStep?.usage?.inputTokens?{"stream.usage.inputTokens":baseFinishStep.usage.inputTokens}:{}),...(baseFinishStep?.usage?.outputTokens?{"stream.usage.outputTokens":baseFinishStep.usage.outputTokens}:{}),...(baseFinishStep?.usage?.cachedInputTokens?{"stream.usage.cachedInputTokens":baseFinishStep.usage.cachedInputTokens}:{}),...(baseFinishStep?.providerMetadata?{"stream.response.providerMetadata":JSON.stringify(baseFinishStep?.providerMetadata)}:{}),...(baseFinishStep?.finishReason?{"stream.response.finishReason":baseFinishStep?.finishReason}:{}),...(options?.telemetry_settings?.recordOutputs!==false?{"stream.response.text":baseFinishStep?.text}:{}),...(baseFinishStep?.toolCalls&&options?.telemetry_settings?.recordOutputs!==false?{"stream.response.toolCalls":JSON.stringify(baseFinishStep?.toolCalls?.map(chunk2=>{return {type:"tool-call",toolCallId:chunk2.payload.toolCallId,args:chunk2.payload.args,toolName:chunk2.payload.toolName};}))}:{})});options.rootSpan.end();}break;case "error":self.#error=chunk.payload.error;const error=typeof self.#error==="object"?new Error(self.#error.message):new Error(String(self.#error));Object.values(self.#delayedPromises).forEach(promise=>promise.reject(error));break;}controller.enqueue(chunk);}}));this.#aisdkv5=new AISDKV5OutputStream({modelOutput:this,messageList,options:{toolCallStreaming:options?.toolCallStreaming,output:options?.output}});}#getDelayedPromise(promise){if(!this.#streamConsumed){void this.consumeStream();}return promise.promise;}/**
|
|
103
|
+
content:messageList.get.response.aiV5.stepContent()};await options?.onStepFinish?.(stepResult);self.#bufferedSteps.push(stepResult);self.#bufferedByStep={text:"",reasoning:"",sources:[],files:[],toolCalls:[],toolResults:[],msgCount:chunk.payload.messages.all.length};break;}case "tripwire":self.#tripwire=true;self.#tripwireReason=chunk.payload?.tripwireReason||"Content blocked";self.#finishReason="other";self.#delayedPromises.text.resolve(self.#bufferedText.join(""));self.#delayedPromises.finishReason.resolve("other");self.#delayedPromises.object.resolve(void 0);self.#delayedPromises.usage.resolve(self.#usageCount);self.#delayedPromises.warnings.resolve(self.#warnings);self.#delayedPromises.providerMetadata.resolve(void 0);self.#delayedPromises.response.resolve({});self.#delayedPromises.request.resolve({});self.#delayedPromises.reasoning.resolve("");self.#delayedPromises.reasoningText.resolve(void 0);self.#delayedPromises.sources.resolve([]);self.#delayedPromises.files.resolve([]);self.#delayedPromises.toolCalls.resolve([]);self.#delayedPromises.toolResults.resolve([]);self.#delayedPromises.steps.resolve(self.#bufferedSteps);self.#delayedPromises.totalUsage.resolve(self.#usageCount);self.#delayedPromises.content.resolve([]);self.#delayedPromises.reasoningDetails.resolve([]);controller.enqueue(chunk);controller.terminate();return;case "finish":if(chunk.payload.stepResult.reason){self.#finishReason=chunk.payload.stepResult.reason;}let response={};if(chunk.payload.metadata){const{providerMetadata,request,...otherMetadata}=chunk.payload.metadata;response={...otherMetadata,messages:messageList.get.response.aiV5.model(),uiMessages:messageList.get.response.aiV5.ui()};}this.populateUsageCount(chunk.payload.output.usage);chunk.payload.output.usage=self.#usageCount;try{if(self.processorRunner&&self.outputProcessorRunnerMode===`result`){self.messageList=await self.processorRunner.runOutputProcessors(self.messageList);const outputText=self.messageList.get.response.aiV4.core().map(m=>MessageList.coreContentToString(m.content)).join("\n");const messages=self.messageList.get.response.v2();const messagesWithStructuredData=messages.filter(msg=>msg.content.metadata&&msg.content.metadata.structuredOutput);if(messagesWithStructuredData[0]&&messagesWithStructuredData[0].content.metadata?.structuredOutput){const structuredOutput=messagesWithStructuredData[0].content.metadata.structuredOutput;self.#delayedPromises.object.resolve(structuredOutput);}else if(!self.#options.output){self.#delayedPromises.object.resolve(void 0);}self.#delayedPromises.text.resolve(outputText);self.#delayedPromises.finishReason.resolve(self.#finishReason);if(chunk.payload.metadata){const{providerMetadata,request,...otherMetadata}=chunk.payload.metadata;response={...otherMetadata,messages:messageList.get.response.aiV5.model(),uiMessages:messageList.get.response.aiV5.ui()};}}else {const textContent=self.#bufferedText.join("");self.#delayedPromises.text.resolve(textContent);self.#delayedPromises.finishReason.resolve(self.#finishReason);if(!self.#options.output&&self.#delayedPromises.object.status.type!=="resolved"){self.#delayedPromises.object.resolve(void 0);}}}catch(error2){if(error2 instanceof TripWire){self.#tripwire=true;self.#tripwireReason=error2.message;self.#delayedPromises.finishReason.resolve("other");self.#delayedPromises.text.resolve("");}else {self.#error=error2 instanceof Error?error2.message:String(error2);self.#delayedPromises.finishReason.resolve("error");self.#delayedPromises.text.resolve("");}self.#delayedPromises.object.resolve(void 0);}self.#delayedPromises.usage.resolve(self.#usageCount);self.#delayedPromises.warnings.resolve(self.#warnings);self.#delayedPromises.providerMetadata.resolve(chunk.payload.metadata?.providerMetadata);self.#delayedPromises.response.resolve(response);self.#delayedPromises.request.resolve(self.#request||{});self.#delayedPromises.text.resolve(self.#bufferedText.join(""));self.#delayedPromises.reasoning.resolve(self.#bufferedReasoning.join(""));const reasoningText=self.#bufferedReasoning.length>0?self.#bufferedReasoning.join(""):void 0;self.#delayedPromises.reasoningText.resolve(reasoningText);self.#delayedPromises.sources.resolve(self.#bufferedSources);self.#delayedPromises.files.resolve(self.#bufferedFiles);self.#delayedPromises.toolCalls.resolve(self.#toolCalls);self.#delayedPromises.toolResults.resolve(self.#toolResults);self.#delayedPromises.steps.resolve(self.#bufferedSteps);self.#delayedPromises.totalUsage.resolve(self.#getTotalUsage());self.#delayedPromises.content.resolve(messageList.get.response.aiV5.stepContent());self.#delayedPromises.reasoningDetails.resolve(Object.values(self.#bufferedReasoningDetails||{}));const baseFinishStep=self.#bufferedSteps[self.#bufferedSteps.length-1];if(baseFinishStep){const onFinishPayload={text:baseFinishStep.text,warnings:baseFinishStep.warnings??[],finishReason:chunk.payload.stepResult.reason,// TODO: we should add handling for step IDs in message list so you can retrieve step content by step id. And on finish should the content here be from all steps?
|
|
104
|
+
content:messageList.get.response.aiV5.stepContent(),request:await self.request,error:self.error,reasoning:await self.aisdk.v5.reasoning,reasoningText:await self.aisdk.v5.reasoningText,sources:await self.aisdk.v5.sources,files:await self.aisdk.v5.files,steps:transformSteps({steps:self.#bufferedSteps}),response:{...(await self.response),messages:messageList.get.response.aiV5.model()},usage:chunk.payload.output.usage,totalUsage:self.#getTotalUsage(),toolCalls:await self.aisdk.v5.toolCalls,toolResults:await self.aisdk.v5.toolResults,staticToolCalls:(await self.aisdk.v5.toolCalls).filter(toolCall=>toolCall.dynamic===false),staticToolResults:(await self.aisdk.v5.toolResults).filter(toolResult=>toolResult.dynamic===false),dynamicToolCalls:(await self.aisdk.v5.toolCalls).filter(toolCall=>toolCall.dynamic===true),dynamicToolResults:(await self.aisdk.v5.toolResults).filter(toolResult=>toolResult.dynamic===true),object:self.#delayedPromises.object.status.type==="resolved"?self.#delayedPromises.object.status.value:self.#options.output&&baseFinishStep.text?(()=>{try{return JSON.parse(baseFinishStep.text);}catch{return void 0;}})():void 0};await options?.onFinish?.(onFinishPayload);}if(options?.rootSpan){options.rootSpan.setAttributes({...(baseFinishStep?.usage?.reasoningTokens?{"stream.usage.reasoningTokens":baseFinishStep.usage.reasoningTokens}:{}),...(baseFinishStep?.usage?.totalTokens?{"stream.usage.totalTokens":baseFinishStep.usage.totalTokens}:{}),...(baseFinishStep?.usage?.inputTokens?{"stream.usage.inputTokens":baseFinishStep.usage.inputTokens}:{}),...(baseFinishStep?.usage?.outputTokens?{"stream.usage.outputTokens":baseFinishStep.usage.outputTokens}:{}),...(baseFinishStep?.usage?.cachedInputTokens?{"stream.usage.cachedInputTokens":baseFinishStep.usage.cachedInputTokens}:{}),...(baseFinishStep?.providerMetadata?{"stream.response.providerMetadata":JSON.stringify(baseFinishStep?.providerMetadata)}:{}),...(baseFinishStep?.finishReason?{"stream.response.finishReason":baseFinishStep?.finishReason}:{}),...(options?.telemetry_settings?.recordOutputs!==false?{"stream.response.text":baseFinishStep?.text}:{}),...(baseFinishStep?.toolCalls&&options?.telemetry_settings?.recordOutputs!==false?{"stream.response.toolCalls":JSON.stringify(baseFinishStep?.toolCalls?.map(chunk2=>{return {type:"tool-call",toolCallId:chunk2.payload.toolCallId,args:chunk2.payload.args,toolName:chunk2.payload.toolName};}))}:{})});options.rootSpan.end();}break;case "error":self.#error=chunk.payload.error;const error=typeof self.#error==="object"?new Error(self.#error.message):new Error(String(self.#error));Object.values(self.#delayedPromises).forEach(promise=>promise.reject(error));break;}controller.enqueue(chunk);}}));this.#aisdkv5=new AISDKV5OutputStream({modelOutput:this,messageList,options:{toolCallStreaming:options?.toolCallStreaming,output:options?.output}});}#getDelayedPromise(promise){if(!this.#streamConsumed){void this.consumeStream();}return promise.promise;}/**
|
|
105
105
|
* Resolves to the complete text response after streaming completes.
|
|
106
106
|
*/get text(){return this.#getDelayedPromise(this.#delayedPromises.text);}/**
|
|
107
107
|
* Resolves to complete reasoning text for models that support reasoning.
|
|
@@ -302,7 +302,7 @@ async function processOutputStream({tools,messageId,messageList,outputStream,run
|
|
|
302
302
|
// BUT does this cause other issues?
|
|
303
303
|
// Alternative solution: in message list allow combining text deltas together when the message source is "response" and the text parts are directly next to each other
|
|
304
304
|
// simple solution for now is to not flush text deltas on response-metadata
|
|
305
|
-
chunk.type!=="response-metadata"&&runState.state.isStreaming){if(runState.state.textDeltas.length){const textStartPayload=chunk.payload;const providerMetadata=textStartPayload.providerMetadata??runState.state.providerOptions;messageList.add({id:messageId,role:"assistant",content:[providerMetadata?{type:"text",text:runState.state.textDeltas.join(""),providerOptions:providerMetadata}:{type:"text",text:runState.state.textDeltas.join("")}]},"response");}runState.setState({isStreaming:false,textDeltas:[]});}switch(chunk.type){case "response-metadata":runState.setState({responseMetadata:{id:chunk.payload.id,timestamp:chunk.payload.timestamp,modelId:chunk.payload.modelId,headers:chunk.payload.headers}});break;case "text-delta":{const textDeltasFromState=runState.state.textDeltas;textDeltasFromState.push(chunk.payload.text);runState.setState({textDeltas:textDeltasFromState,isStreaming:true});controller.enqueue(chunk);break;}case "tool-call-input-streaming-start":{const tool=tools?.[chunk.payload.toolName]||Object.values(tools||{})?.find(tool2=>`id`in tool2&&tool2.id===chunk.payload.toolName);if(tool&&"onInputStart"in tool){try{await tool?.onInputStart?.({toolCallId:chunk.payload.toolCallId,messages:messageList.get.input.aiV5.model(),abortSignal:options?.abortSignal});}catch(error){console.error("Error calling onInputStart",error);}}controller.enqueue(chunk);break;}case "tool-call-delta":{const tool=tools?.[chunk.payload.toolName||""]||Object.values(tools||{})?.find(tool2=>`id`in tool2&&tool2.id===chunk.payload.toolName);if(tool&&"onInputDelta"in tool){try{await tool?.onInputDelta?.({inputTextDelta:chunk.payload.argsTextDelta,toolCallId:chunk.payload.toolCallId,messages:messageList.get.input.aiV5.model(),abortSignal:options?.abortSignal});}catch(error){console.error("Error calling onInputDelta",error);}}controller.enqueue(chunk);break;}case "reasoning-start":{runState.setState({providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions});if(Object.values(chunk.payload.providerMetadata||{}).find(v=>v?.redactedData)){messageList.add({id:messageId,role:"assistant",content:[{type:"reasoning",text:"",providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions}]},"response");controller.enqueue(chunk);break;}controller.enqueue(chunk);break;}case "reasoning-delta":{const reasoningDeltasFromState=runState.state.reasoningDeltas;reasoningDeltasFromState.push(chunk.payload.text);runState.setState({isReasoning:true,reasoningDeltas:reasoningDeltasFromState,providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions});controller.enqueue(chunk);break;}case "file":messageList.add({id:messageId,role:"assistant",content:[{type:"file",data:chunk.payload.data,mimeType:chunk.payload.mimeType}]},"response");controller.enqueue(chunk);break;case "source":messageList.add({id:messageId,role:"assistant",content:{format:2,parts:[{type:"source",source:{sourceType:"url",id:chunk.payload.id,url:chunk.payload.url||"",title:chunk.payload.title,providerMetadata:chunk.payload.providerMetadata}}]},createdAt:/* @__PURE__ */new Date()},"response");controller.enqueue(chunk);break;case "finish":runState.setState({providerOptions:chunk.payload.metadata.providerMetadata,stepResult:{reason:chunk.payload.reason,logprobs:chunk.payload.logprobs,warnings:responseFromModel.warnings,totalUsage:chunk.payload.totalUsage,headers:responseFromModel.rawResponse?.headers,messageId,isContinued:!["stop","error"].includes(chunk.payload.reason),request:responseFromModel.request}});break;case "error":if(isAbortError(chunk.payload.error)&&options?.abortSignal?.aborted){break;}runState.setState({hasErrored:true});runState.setState({stepResult:{isContinued:false,reason:"error"}});let e=chunk.payload.error;if(typeof e==="object"){e=new Error(e?.message||"Unknown error");Object.assign(e,chunk.payload.error);}controller.enqueue({...chunk,payload:{...chunk.payload,error:e}});await options?.onError?.({error:e});break;default:controller.enqueue(chunk);}if(["text-delta","reasoning-delta","source","tool-call","tool-call-input-streaming-start","tool-call-delta","raw"].includes(chunk.type)){const transformedChunk=convertMastraChunkToAISDKv5({chunk});if(chunk.type==="raw"&&!includeRawChunks){return;}await options?.onChunk?.({chunk:transformedChunk});}if(runState.state.hasErrored){break;}}}function createLLMExecutionStep({model,_internal,messageId,runId,modelStreamSpan,telemetry_settings,tools,toolChoice,messageList,includeRawChunks,modelSettings,providerOptions,options,toolCallStreaming,controller,output,outputProcessors,headers,downloadRetries,downloadConcurrency}){return createStep({id:"llm-execution",inputSchema:llmIterationOutputSchema,outputSchema:llmIterationOutputSchema,execute:async({inputData,bail,tracingContext})=>{const runState=new AgenticRunState({_internal,model});let modelResult;let warnings;let request;let rawResponse;switch(model.specificationVersion){case "v2":{const messageListPromptArgs={downloadRetries,downloadConcurrency,supportedUrls:model?.supportedUrls};let inputMessages=await messageList.get.all.aiV5.llmPrompt(messageListPromptArgs);
|
|
305
|
+
chunk.type!=="response-metadata"&&runState.state.isStreaming){if(runState.state.textDeltas.length){const textStartPayload=chunk.payload;const providerMetadata=textStartPayload.providerMetadata??runState.state.providerOptions;messageList.add({id:messageId,role:"assistant",content:[providerMetadata?{type:"text",text:runState.state.textDeltas.join(""),providerOptions:providerMetadata}:{type:"text",text:runState.state.textDeltas.join("")}]},"response");}runState.setState({isStreaming:false,textDeltas:[]});}switch(chunk.type){case "response-metadata":runState.setState({responseMetadata:{id:chunk.payload.id,timestamp:chunk.payload.timestamp,modelId:chunk.payload.modelId,headers:chunk.payload.headers}});break;case "text-delta":{const textDeltasFromState=runState.state.textDeltas;textDeltasFromState.push(chunk.payload.text);runState.setState({textDeltas:textDeltasFromState,isStreaming:true});controller.enqueue(chunk);break;}case "tool-call-input-streaming-start":{const tool=tools?.[chunk.payload.toolName]||Object.values(tools||{})?.find(tool2=>`id`in tool2&&tool2.id===chunk.payload.toolName);if(tool&&"onInputStart"in tool){try{await tool?.onInputStart?.({toolCallId:chunk.payload.toolCallId,messages:messageList.get.input.aiV5.model(),abortSignal:options?.abortSignal});}catch(error){console.error("Error calling onInputStart",error);}}controller.enqueue(chunk);break;}case "tool-call-delta":{const tool=tools?.[chunk.payload.toolName||""]||Object.values(tools||{})?.find(tool2=>`id`in tool2&&tool2.id===chunk.payload.toolName);if(tool&&"onInputDelta"in tool){try{await tool?.onInputDelta?.({inputTextDelta:chunk.payload.argsTextDelta,toolCallId:chunk.payload.toolCallId,messages:messageList.get.input.aiV5.model(),abortSignal:options?.abortSignal});}catch(error){console.error("Error calling onInputDelta",error);}}controller.enqueue(chunk);break;}case "reasoning-start":{runState.setState({providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions});if(Object.values(chunk.payload.providerMetadata||{}).find(v=>v?.redactedData)){messageList.add({id:messageId,role:"assistant",content:[{type:"reasoning",text:"",providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions}]},"response");controller.enqueue(chunk);break;}controller.enqueue(chunk);break;}case "reasoning-delta":{const reasoningDeltasFromState=runState.state.reasoningDeltas;reasoningDeltasFromState.push(chunk.payload.text);runState.setState({isReasoning:true,reasoningDeltas:reasoningDeltasFromState,providerOptions:chunk.payload.providerMetadata??runState.state.providerOptions});controller.enqueue(chunk);break;}case "file":messageList.add({id:messageId,role:"assistant",content:[{type:"file",data:chunk.payload.data,mimeType:chunk.payload.mimeType}]},"response");controller.enqueue(chunk);break;case "source":messageList.add({id:messageId,role:"assistant",content:{format:2,parts:[{type:"source",source:{sourceType:"url",id:chunk.payload.id,url:chunk.payload.url||"",title:chunk.payload.title,providerMetadata:chunk.payload.providerMetadata}}]},createdAt:/* @__PURE__ */new Date()},"response");controller.enqueue(chunk);break;case "finish":runState.setState({providerOptions:chunk.payload.metadata.providerMetadata,stepResult:{reason:chunk.payload.reason,logprobs:chunk.payload.logprobs,warnings:responseFromModel.warnings,totalUsage:chunk.payload.totalUsage,headers:responseFromModel.rawResponse?.headers,messageId,isContinued:!["stop","error"].includes(chunk.payload.reason),request:responseFromModel.request}});break;case "error":if(isAbortError(chunk.payload.error)&&options?.abortSignal?.aborted){break;}runState.setState({hasErrored:true});runState.setState({stepResult:{isContinued:false,reason:"error"}});let e=chunk.payload.error;if(typeof e==="object"){e=new Error(e?.message||"Unknown error");Object.assign(e,chunk.payload.error);}controller.enqueue({...chunk,payload:{...chunk.payload,error:e}});await options?.onError?.({error:e});break;default:controller.enqueue(chunk);}if(["text-delta","reasoning-delta","source","tool-call","tool-call-input-streaming-start","tool-call-delta","raw"].includes(chunk.type)){const transformedChunk=convertMastraChunkToAISDKv5({chunk});if(chunk.type==="raw"&&!includeRawChunks){return;}await options?.onChunk?.({chunk:transformedChunk});}if(runState.state.hasErrored){break;}}}function createLLMExecutionStep({model,_internal,messageId,runId,modelStreamSpan,telemetry_settings,tools,toolChoice,messageList,includeRawChunks,modelSettings,providerOptions,options,toolCallStreaming,controller,output,outputProcessors,headers,downloadRetries,downloadConcurrency}){return createStep({id:"llm-execution",inputSchema:llmIterationOutputSchema,outputSchema:llmIterationOutputSchema,execute:async({inputData,bail,tracingContext})=>{const runState=new AgenticRunState({_internal,model});let modelResult;let warnings;let request;let rawResponse;switch(model.specificationVersion){case "v2":{const messageListPromptArgs={downloadRetries,downloadConcurrency,supportedUrls:model?.supportedUrls};let inputMessages=await messageList.get.all.aiV5.llmPrompt(messageListPromptArgs);let stepModel=model;let stepToolChoice=toolChoice;let stepTools=tools;if(options?.prepareStep){try{const prepareStepResult=await options.prepareStep({stepNumber:inputData.output?.steps?.length||0,steps:inputData.output?.steps||[],model,messages:messageList.get.all.aiV5.model()});if(prepareStepResult){if(prepareStepResult.model){stepModel=prepareStepResult.model;}if(prepareStepResult.toolChoice){stepToolChoice=prepareStepResult.toolChoice;}if(prepareStepResult.activeTools&&stepTools){const activeToolsSet=new Set(prepareStepResult.activeTools);stepTools=Object.fromEntries(Object.entries(stepTools).filter(([toolName])=>activeToolsSet.has(toolName)));}if(prepareStepResult.messages){const newMessages=prepareStepResult.messages;const newMessageList=new MessageList();for(const message of newMessages){if(message.role==="system"){newMessageList.addSystem(message);}else if(message.role==="user"){newMessageList.add(message,"input");}else if(message.role==="assistant"||message.role==="tool"){newMessageList.add(message,"response");}}inputMessages=await newMessageList.get.all.aiV5.llmPrompt(messageListPromptArgs);}}}catch(error){console.error("Error in prepareStep callback:",error);}}modelResult=execute({runId,model:stepModel,providerOptions,inputMessages,tools:stepTools,toolChoice:stepToolChoice,options,modelSettings,telemetry_settings,includeRawChunks,output,headers,onResult:({warnings:warningsFromStream,request:requestFromStream,rawResponse:rawResponseFromStream})=>{warnings=warningsFromStream;request=requestFromStream||{};rawResponse=rawResponseFromStream;controller.enqueue({runId,from:"AGENT"/* AGENT */,type:"step-start",payload:{request:request||{},warnings:[],messageId}});},modelStreamSpan});break;}default:{throw new Error(`Unsupported model version: ${model.specificationVersion}`);}}const outputStream=new MastraModelOutput({model:{modelId:model.modelId,provider:model.provider,version:model.specificationVersion},stream:modelResult,messageList,options:{runId,rootSpan:modelStreamSpan,toolCallStreaming,telemetry_settings,includeRawChunks,output,outputProcessors,outputProcessorRunnerMode:"stream",tracingContext}});try{await processOutputStream({outputStream,includeRawChunks,tools,messageId,messageList,runState,options,controller,responseFromModel:{warnings,request,rawResponse}});}catch(error){console.log("Error in LLM Execution Step",error);if(isAbortError(error)&&options?.abortSignal?.aborted){await options?.onAbort?.({steps:inputData?.output?.steps??[]});controller.enqueue({type:"abort",runId,from:"AGENT"/* AGENT */,payload:{}});const usage2=outputStream._getImmediateUsage();const responseMetadata2=runState.state.responseMetadata;const text2=outputStream._getImmediateText();return bail({messageId,stepResult:{reason:"abort",warnings,isContinued:false},metadata:{providerMetadata:providerOptions,...responseMetadata2,headers:rawResponse?.headers,request},output:{text:text2,toolCalls:[],usage:usage2??inputData.output?.usage,steps:[]},messages:{all:messageList.get.all.aiV5.model(),user:messageList.get.input.aiV5.model(),nonUser:messageList.get.response.aiV5.model()}});}controller.enqueue({type:"error",runId,from:"AGENT"/* AGENT */,payload:{error}});runState.setState({hasErrored:true,stepResult:{isContinued:false,reason:"error"}});}if(outputStream.tripwire){controller.enqueue({type:"tripwire",runId,from:"AGENT"/* AGENT */,payload:{tripwireReason:outputStream.tripwireReason||"Content blocked by output processor"}});runState.setState({stepResult:{isContinued:false,reason:"abort"}});}const toolCalls=outputStream._getImmediateToolCalls()?.map(chunk=>{return chunk.payload;});if(toolCalls.length>0){const assistantContent=[...toolCalls.map(toolCall=>{return {type:"tool-call",toolCallId:toolCall.toolCallId,toolName:toolCall.toolName,args:toolCall.args};})];messageList.add({id:messageId,role:"assistant",content:assistantContent},"response");}const finishReason=runState?.state?.stepResult?.reason??outputStream._getImmediateFinishReason();const hasErrored=runState.state.hasErrored;const usage=outputStream._getImmediateUsage();const responseMetadata=runState.state.responseMetadata;const text=outputStream._getImmediateText();const tripwireTriggered=outputStream.tripwire;const steps=inputData.output?.steps||[];steps.push(new DefaultStepResult({warnings:outputStream._getImmediateWarnings(),providerMetadata:providerOptions,finishReason:runState.state.stepResult?.reason,content:messageList.get.response.aiV5.modelContent(),// @ts-ignore this is how it worked internally for transformResponse which was removed TODO: how should this actually work?
|
|
306
306
|
response:{...responseMetadata,...rawResponse,messages:messageList.get.response.aiV5.model()},request,usage:outputStream._getImmediateUsage()}));const messages={all:messageList.get.all.aiV5.model(),user:messageList.get.input.aiV5.model(),nonUser:messageList.get.response.aiV5.model()};return {messageId,stepResult:{reason:tripwireTriggered?"abort":hasErrored?"error":finishReason,warnings,isContinued:tripwireTriggered?false:!["stop","error"].includes(finishReason)},metadata:{providerMetadata:runState.state.providerOptions,...responseMetadata,...rawResponse,headers:rawResponse?.headers,request},output:{text,toolCalls,usage:usage??inputData.output?.usage,steps},messages};}});}// src/loop/workflow/tool-call-step.ts
|
|
307
307
|
function createToolCallStep({tools,messageList,options,telemetry_settings,writer}){return createStep({id:"toolCallStep",inputSchema:toolCallInputSchema,outputSchema:toolCallOutputSchema,execute:async({inputData})=>{if(inputData.providerExecuted){const tracer2=getTracer({isEnabled:telemetry_settings?.isEnabled,tracer:telemetry_settings?.tracer});const span2=tracer2.startSpan("mastra.stream.toolCall").setAttributes({...assembleOperationName({operationId:"mastra.stream.toolCall",telemetry:telemetry_settings}),"stream.toolCall.toolName":inputData.toolName,"stream.toolCall.toolCallId":inputData.toolCallId,"stream.toolCall.args":JSON.stringify(inputData.args),"stream.toolCall.providerExecuted":true});if(inputData.output){span2.setAttributes({"stream.toolCall.result":JSON.stringify(inputData.output)});}span2.end();return {...inputData,result:inputData.output};}const tool=tools?.[inputData.toolName]||Object.values(tools||{})?.find(tool2=>`id`in tool2&&tool2.id===inputData.toolName);if(!tool){throw new Error(`Tool ${inputData.toolName} not found`);}if(tool&&"onInputAvailable"in tool){try{await tool?.onInputAvailable?.({toolCallId:inputData.toolCallId,input:inputData.args,messages:messageList.get.input.aiV5.model(),abortSignal:options?.abortSignal});}catch(error){console.error("Error calling onInputAvailable",error);}}if(!tool.execute){return inputData;}const tracer=getTracer({isEnabled:telemetry_settings?.isEnabled,tracer:telemetry_settings?.tracer});const span=tracer.startSpan("mastra.stream.toolCall").setAttributes({...assembleOperationName({operationId:"mastra.stream.toolCall",telemetry:telemetry_settings}),"stream.toolCall.toolName":inputData.toolName,"stream.toolCall.toolCallId":inputData.toolCallId,"stream.toolCall.args":JSON.stringify(inputData.args)});try{const result=await tool.execute(inputData.args,{abortSignal:options?.abortSignal,toolCallId:inputData.toolCallId,messages:messageList.get.input.aiV5.model(),writableStream:writer});span.setAttributes({"stream.toolCall.result":JSON.stringify(result)});span.end();return {result,...inputData};}catch(error){span.setStatus({code:2,message:error?.message??error});span.recordException(error);return {error,...inputData};}}});}// src/loop/workflow/outer-llm-step.ts
|
|
308
308
|
function createOuterLLMWorkflow({model,telemetry_settings,_internal,modelStreamSpan,...rest}){const llmExecutionStep=createLLMExecutionStep({model,_internal,modelStreamSpan,telemetry_settings,...rest});const toolCallStep=createToolCallStep({telemetry_settings,...rest});const messageList=rest.messageList;const llmMappingStep=createStep({id:"llmExecutionMappingStep",inputSchema:z6.array(toolCallOutputSchema),outputSchema:llmIterationOutputSchema,execute:async({inputData,getStepResult:getStepResult2,bail})=>{const initialResult=getStepResult2(llmExecutionStep);if(inputData?.every(toolCall=>toolCall?.result===void 0)){const errorResults=inputData.filter(toolCall=>toolCall?.error);const toolResultMessageId=rest.experimental_generateMessageId?.()||_internal?.generateId?.();if(errorResults?.length){errorResults.forEach(toolCall=>{const chunk={type:"tool-error",runId:rest.runId,from:"AGENT"/* AGENT */,payload:{error:toolCall.error,args:toolCall.args,toolCallId:toolCall.toolCallId,toolName:toolCall.toolName,providerMetadata:toolCall.providerMetadata}};rest.controller.enqueue(chunk);});rest.messageList.add({id:toolResultMessageId,role:"tool",content:errorResults.map(toolCall=>{return {type:"tool-result",args:toolCall.args,toolCallId:toolCall.toolCallId,toolName:toolCall.toolName,result:{tool_execution_error:toolCall.error?.message??toolCall.error}};})},"response");}initialResult.stepResult.isContinued=false;return bail(initialResult);}if(inputData?.length){for(const toolCall of inputData){const chunk={type:"tool-result",runId:rest.runId,from:"AGENT"/* AGENT */,payload:{args:toolCall.args,toolCallId:toolCall.toolCallId,toolName:toolCall.toolName,result:toolCall.result,providerMetadata:toolCall.providerMetadata,providerExecuted:toolCall.providerExecuted}};rest.controller.enqueue(chunk);if(model.specificationVersion==="v2"){await rest.options?.onChunk?.({chunk:convertMastraChunkToAISDKv5({chunk})});}const toolResultMessageId=rest.experimental_generateMessageId?.()||_internal?.generateId?.();messageList.add({id:toolResultMessageId,role:"tool",content:inputData.map(toolCall2=>{return {type:"tool-result",args:toolCall2.args,toolCallId:toolCall2.toolCallId,toolName:toolCall2.toolName,result:toolCall2.result};})},"response");}return {...initialResult,messages:{all:messageList.get.all.aiV5.model(),user:messageList.get.input.aiV5.model(),nonUser:messageList.get.response.aiV5.model()}};}}});return createWorkflow({id:"executionWorkflow",inputSchema:llmIterationOutputSchema,outputSchema:z6.any()}).then(llmExecutionStep).map(({inputData})=>{if(modelStreamSpan&&telemetry_settings?.recordOutputs!==false&&inputData.output.toolCalls?.length){modelStreamSpan.setAttribute("stream.response.toolCalls",JSON.stringify(inputData.output.toolCalls?.map(toolCall=>{return {toolCallId:toolCall.toolCallId,args:toolCall.args,toolName:toolCall.toolName};})));}return inputData.output.toolCalls||[];}).foreach(toolCallStep,{concurrency:10}).then(llmMappingStep).commit();}// src/loop/workflow/stream.ts
|
|
@@ -549,9 +549,9 @@ messages:messageList.get.remembered.v1(),// these are here for inspecting but sh
|
|
|
549
549
|
// - ex TokenLimiter needs to measure all tokens even though it's only processing remembered messages
|
|
550
550
|
newMessages:messageList.get.input.v1(),systemMessage,memorySystemMessage:memorySystemMessage||void 0});const processedList=new MessageList({threadId:threadObject.id,resourceId,generateMessageId:this.#mastra?.generateId?.bind(this.#mastra),// @ts-ignore Flag for agent network messages
|
|
551
551
|
_agentNetworkAppend:this._agentNetworkAppend}).addSystem(instructions||`${this.instructions}.`).addSystem(memorySystemMessage).addSystem(systemMessages).add(options.context||[],"context").add(processedMemoryMessages,"memory").add(messageList.get.input.v2(),"user");return {thread:threadObject,messageList:processedList,// add old processed messages + new input messages
|
|
552
|
-
...(tripwireTriggered&&{tripwire:true,tripwireReason}),threadExists:!!existingThread};}});const streamStep=createStep({id:"stream-text-step",inputSchema:z.any(),outputSchema:z.any(),execute:async({inputData,tracingContext})=>{this.logger.debug(`Starting agent ${this.name} llm stream call`,{runId});const outputProcessors=inputData.outputProcessors||(this.#outputProcessors?typeof this.#outputProcessors==="function"?await this.#outputProcessors({runtimeContext:inputData.runtimeContext||new RuntimeContext()}):this.#outputProcessors:[]);const streamResult=llm.stream({...inputData,outputProcessors,returnScorerData:options.returnScorerData,tracingContext,_internal:{generateId:inputData.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)}});if(format==="aisdk"){return streamResult.aisdk.v5;}return streamResult;}});const executionWorkflow=createWorkflow({id:"execution-workflow",inputSchema:z.any(),outputSchema:z.any(),steps:[prepareToolsStep,prepareMemory]}).parallel([prepareToolsStep,prepareMemory]).map(async({inputData,bail})=>{const result2={...options,tools:inputData["prepare-tools-step"].convertedTools,runId,
|
|
552
|
+
...(tripwireTriggered&&{tripwire:true,tripwireReason}),threadExists:!!existingThread};}});const streamStep=createStep({id:"stream-text-step",inputSchema:z.any(),outputSchema:z.any(),execute:async({inputData,tracingContext})=>{this.logger.debug(`Starting agent ${this.name} llm stream call`,{runId});const outputProcessors=inputData.outputProcessors||(this.#outputProcessors?typeof this.#outputProcessors==="function"?await this.#outputProcessors({runtimeContext:inputData.runtimeContext||new RuntimeContext()}):this.#outputProcessors:[]);const streamResult=llm.stream({...inputData,outputProcessors,returnScorerData:options.returnScorerData,tracingContext,_internal:{generateId:inputData.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)}});if(format==="aisdk"){return streamResult.aisdk.v5;}return streamResult;}});const executionWorkflow=createWorkflow({id:"execution-workflow",inputSchema:z.any(),outputSchema:z.any(),steps:[prepareToolsStep,prepareMemory]}).parallel([prepareToolsStep,prepareMemory]).map(async({inputData,bail})=>{const result2={...options,tools:inputData["prepare-tools-step"].convertedTools,runId,toolChoice:options.toolChoice,thread:inputData["prepare-memory-step"].thread,threadId:inputData["prepare-memory-step"].thread?.id,resourceId,runtimeContext,onStepFinish:async props=>{if(options.savePerStep){if(!inputData["prepare-memory-step"].threadExists&&memory&&inputData["prepare-memory-step"].thread){await memory.createThread({threadId:inputData["prepare-memory-step"].thread?.id,title:inputData["prepare-memory-step"].thread?.title,metadata:inputData["prepare-memory-step"].thread?.metadata,resourceId:inputData["prepare-memory-step"].thread?.resourceId,memoryConfig});inputData["prepare-memory-step"].threadExists=true;}await this.saveStepMessages({saveQueueManager,result:props,messageList:inputData["prepare-memory-step"].messageList,threadId:inputData["prepare-memory-step"].thread?.id,memoryConfig,runId});}return options.onStepFinish?.({...props,runId});},...(inputData["prepare-memory-step"].tripwire&&{tripwire:inputData["prepare-memory-step"].tripwire,tripwireReason:inputData["prepare-memory-step"].tripwireReason})};if(result2.tripwire){const emptyResult={textStream:async function*(){}(),fullStream:new globalThis.ReadableStream({start(controller){controller.enqueue({type:"tripwire",runId:result2.runId,from:"AGENT"/* AGENT */,payload:{tripwireReason:result2.tripwireReason}});controller.close();}}),objectStream:new globalThis.ReadableStream({start(controller){controller.close();}}),text:Promise.resolve(""),usage:Promise.resolve({inputTokens:0,outputTokens:0,totalTokens:0}),finishReason:Promise.resolve("other"),tripwire:true,tripwireReason:result2.tripwireReason,response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},toolCalls:Promise.resolve([]),toolResults:Promise.resolve([]),warnings:Promise.resolve(void 0),request:{body:JSON.stringify({messages:[]})},object:void 0,experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0};return bail(emptyResult);}let effectiveOutputProcessors=options.outputProcessors||(this.#outputProcessors?typeof this.#outputProcessors==="function"?await this.#outputProcessors({runtimeContext:result2.runtimeContext}):this.#outputProcessors:[]);if(options.structuredOutput){const agentModel=await this.getModel({runtimeContext:result2.runtimeContext});const structuredProcessor=new StructuredOutputProcessor(options.structuredOutput,agentModel);effectiveOutputProcessors=effectiveOutputProcessors?[...effectiveOutputProcessors,structuredProcessor]:[structuredProcessor];}const loopOptions={runtimeContext:result2.runtimeContext,tracingContext:{currentSpan:agentAISpan},runId,toolChoice:result2.toolChoice,tools:result2.tools,resourceId:result2.resourceId,threadId:result2.threadId,structuredOutput:result2.structuredOutput,stopWhen:result2.stopWhen,maxSteps:result2.maxSteps,providerOptions:result2.providerOptions,options:{...(options.prepareStep&&{prepareStep:options.prepareStep}),onFinish:async payload=>{if(payload.finishReason==="error"){this.logger.error("Error in agent stream",{error:payload.error,runId});return;}const messageList=inputData["prepare-memory-step"].messageList;try{const outputText=messageList.get.all.core().map(m=>m.content).join("\n");await this.#executeOnFinish({result:payload,outputText,instructions,thread:result2.thread,threadId:result2.threadId,readOnlyMemory:options.memory?.readOnly,resourceId,memoryConfig,runtimeContext,agentAISpan,runId,messageList,threadExists:inputData["prepare-memory-step"].threadExists,structuredOutput:!!options.output,saveQueueManager,overrideScorers:options.scorers});}catch(e){this.logger.error("Error saving memory on finish",{error:e,runId});}await options?.onFinish?.({...payload,runId,messages:messageList.get.response.aiV5.model(),usage:payload.usage,totalUsage:payload.totalUsage});},onStepFinish:result2.onStepFinish,onChunk:options.onChunk,onError:options.onError,onAbort:options.onAbort,activeTools:options.activeTools,abortSignal:options.abortSignal},output:options.output,outputProcessors:effectiveOutputProcessors,modelSettings:{temperature:0,...(options.modelSettings||{})},messageList:inputData["prepare-memory-step"].messageList};return loopOptions;}).then(streamStep).commit();const run=await executionWorkflow.createRunAsync();const result=await run.start({tracingContext:{currentSpan:agentAISpan}});return result;}async#executeOnFinish({result,instructions,readOnlyMemory,thread:threadAfter,threadId,resourceId,memoryConfig,outputText,runtimeContext,agentAISpan,runId,messageList,threadExists,structuredOutput=false,saveQueueManager,overrideScorers}){const resToLog={text:result?.text,object:result?.object,toolResults:result?.toolResults,toolCalls:result?.toolCalls,usage:result?.usage,steps:result?.steps?.map(s=>{return {stepType:s?.stepType,text:result?.text,object:result?.object,toolResults:result?.toolResults,toolCalls:result?.toolCalls,usage:result?.usage};})};this.logger.debug(`[Agent:${this.name}] - Post processing LLM response`,{runId,result:resToLog,threadId,resourceId});const messageListResponses=messageList.get.response.aiV4.core();const usedWorkingMemory=messageListResponses?.some(m=>m.role==="tool"&&m?.content?.some(c=>c?.toolName==="updateWorkingMemory"));const memory=await this.getMemory({runtimeContext});const thread=usedWorkingMemory?threadId?await memory?.getThreadById({threadId}):void 0:threadAfter;if(memory&&resourceId&&thread&&!readOnlyMemory){try{let responseMessages=result.response.messages;if(!responseMessages&&result.object){responseMessages=[{role:"assistant",content:[{type:"text",text:outputText// outputText contains the stringified object
|
|
553
553
|
}]}];}if(responseMessages){const filteredMessages=responseMessages.filter(m=>m.role!=="user");messageList.add(filteredMessages,"response");}if(!threadExists){await memory.createThread({threadId:thread.id,metadata:thread.metadata,title:thread.title,memoryConfig,resourceId:thread.resourceId});}const promises=[saveQueueManager.flushMessages(messageList,threadId,memoryConfig)];if(thread.title?.startsWith("New Thread")){const config=memory.getMergedThreadConfig(memoryConfig);const userMessage=this.getMostRecentUserMessage(messageList.get.all.ui());const{shouldGenerate,model:titleModel,instructions:titleInstructions}=this.resolveTitleGenerationConfig(config?.threads?.generateTitle);if(shouldGenerate&&userMessage){promises.push(this.genTitle(userMessage,runtimeContext,{currentSpan:agentAISpan},titleModel,titleInstructions).then(title=>{if(title){return memory.createThread({threadId:thread.id,resourceId,memoryConfig,title,metadata:thread.metadata});}}));}}await Promise.all(promises);}catch(e){await saveQueueManager.flushMessages(messageList,threadId,memoryConfig);if(e instanceof MastraError){throw e;}const mastraError=new MastraError({id:"AGENT_MEMORY_PERSIST_RESPONSE_MESSAGES_FAILED",domain:"AGENT"/* AGENT */,category:"SYSTEM"/* SYSTEM */,details:{agentName:this.name,runId:runId||"",threadId:threadId||"",result:JSON.stringify(resToLog)}},e);this.logger.trackException(mastraError);this.logger.error(mastraError.toString());throw mastraError;}}else {let responseMessages=result.response.messages;if(!responseMessages&&result.object){responseMessages=[{role:"assistant",content:[{type:"text",text:outputText// outputText contains the stringified object
|
|
554
|
-
}]}];}if(responseMessages){messageList.add(responseMessages,"response");}}await this.#runScorers({messageList,runId,outputText,instructions,runtimeContext,structuredOutput,overrideScorers,tracingContext:{currentSpan:agentAISpan}});agentAISpan?.end({output:{text:result?.text,object:result?.object,files:result?.files}});}async network(messages,options){const runId=options?.runId||this.#mastra?.generateId()||randomUUID();const runtimeContextToUse=options?.runtimeContext||new RuntimeContext();return await networkLoop({networkName:this.name,runtimeContext:runtimeContextToUse,runId,routingAgent:this,routingAgentOptions:{telemetry:options?.telemetry,modelSettings:options?.modelSettings},generateId:()=>this.#mastra?.generateId()||randomUUID(),maxIterations:options?.maxSteps||1,messages,threadId:typeof options?.memory?.thread==="string"?options?.memory?.thread:options?.memory?.thread?.id,resourceId:options?.memory?.resource});}async generateVNext(messages,options){const result=await this.streamVNext(messages,options);if(result.tripwire){return result;}let fullOutput=await result.getFullOutput();const error=fullOutput.error;if(fullOutput.finishReason==="error"&&error){throw error;}return fullOutput;}async streamVNext(messages,streamOptions){const defaultStreamOptions=await this.getDefaultVNextStreamOptions({runtimeContext:streamOptions?.runtimeContext});const mergedStreamOptions={...defaultStreamOptions,...streamOptions,onFinish:this.#mergeOnFinishWithTelemetry(streamOptions,defaultStreamOptions)};const llm=await this.getLLM({runtimeContext:mergedStreamOptions.runtimeContext});if(llm.getModel().specificationVersion!=="v2"){throw new MastraError({id:"AGENT_STREAM_VNEXT_V1_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:"V1 models are not supported for streamVNext. Please use stream instead."});}const result=await this.#execute({...mergedStreamOptions,messages,methodType:"streamVNext"});if(result.status!=="success"){if(result.status==="failed"){throw new MastraError({id:"AGENT_STREAM_VNEXT_FAILED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:result.error.message,details:{error:result.error.message}});}throw new MastraError({id:"AGENT_STREAM_VNEXT_UNKNOWN_ERROR",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:"An unknown error occurred while streaming"});}return result.result;}async generate(messages,generateOptions={}){this.logger.warn("Deprecation NOTICE:\nGenerate method will switch to use generateVNext implementation September 16th, 2025. Please use generateLegacy if you don't want to upgrade just yet.");return this.generateLegacy(messages,generateOptions);}async generateLegacy(messages,generateOptions={}){const defaultGenerateOptions=await this.getDefaultGenerateOptions({runtimeContext:generateOptions.runtimeContext});const mergedGenerateOptions={...defaultGenerateOptions,...generateOptions,experimental_generateMessageId:defaultGenerateOptions.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)};const{llm,before,after}=await this.prepareLLMOptions(messages,mergedGenerateOptions,"generate");if(llm.getModel().specificationVersion!=="v1"){this.logger.error("V2 models are not supported for the current version of generate. Please use generateVNext instead.",{modelId:llm.getModel().modelId});throw new MastraError({id:"AGENT_GENERATE_V2_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,details:{modelId:llm.getModel().modelId},text:"V2 models are not supported for the current version of generate. Please use generateVNext instead."});}let llmToUse=llm;const beforeResult=await before();const traceId=getValidTraceId(beforeResult.agentAISpan);if(beforeResult.tripwire){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:beforeResult.tripwireReason,traceId};return tripwireResult;}const{experimental_output,output,agentAISpan,...llmOptions}=beforeResult;const tracingContext={currentSpan:agentAISpan};let finalOutputProcessors=mergedGenerateOptions.outputProcessors;if(mergedGenerateOptions.structuredOutput){const agentModel=await this.getModel({runtimeContext:mergedGenerateOptions.runtimeContext});const structuredProcessor=new StructuredOutputProcessor(mergedGenerateOptions.structuredOutput,agentModel);finalOutputProcessors=finalOutputProcessors?[...finalOutputProcessors,structuredProcessor]:[structuredProcessor];}if(!output||experimental_output){const result2=await llmToUse.__text({...llmOptions,tracingContext,experimental_output});const outputProcessorResult2=await this.__runOutputProcessors({runtimeContext:mergedGenerateOptions.runtimeContext||new RuntimeContext(),tracingContext,outputProcessorOverrides:finalOutputProcessors,messageList:new MessageList({threadId:llmOptions.threadId||"",resourceId:llmOptions.resourceId||""}).add({role:"assistant",content:[{type:"text",text:result2.text}]},"response")});if(outputProcessorResult2.tripwireTriggered){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:outputProcessorResult2.tripwireReason,traceId};return tripwireResult;}const newText2=outputProcessorResult2.messageList.get.response.v2().map(msg=>msg.content.parts.map(part=>part.type==="text"?part.text:"").join("")).join("");result2.text=newText2;if(finalOutputProcessors&&finalOutputProcessors.length>0){const messages2=outputProcessorResult2.messageList.get.response.v2();this.logger.debug("Checking messages for experimentalOutput metadata:",messages2.map(m=>({role:m.role,hasContentMetadata:!!m.content.metadata,contentMetadata:m.content.metadata})));const messagesWithStructuredData=messages2.filter(msg=>msg.content.metadata&&msg.content.metadata.structuredOutput);this.logger.debug("Messages with structured data:",messagesWithStructuredData.length);if(messagesWithStructuredData[0]&&messagesWithStructuredData[0].content.metadata?.structuredOutput){result2.object=messagesWithStructuredData[0].content.metadata.structuredOutput;this.logger.debug("Using structured data from processor metadata for result.object");}else {try{const processedOutput=JSON.parse(newText2);result2.object=processedOutput;this.logger.debug("Using fallback JSON parsing for result.object");}catch(error){this.logger.warn("Failed to parse processed output as JSON, updating text only",{error});}}}const overrideScorers=mergedGenerateOptions.scorers;const afterResult2=await after({result:result2,outputText:newText2,agentAISpan,...(overrideScorers?{overrideScorers}:{})});if(generateOptions.returnScorerData){result2.scoringData=afterResult2.scoringData;}result2.traceId=traceId;return result2;}const result=await llmToUse.__textObject({...llmOptions,tracingContext,structuredOutput:output});const outputText=JSON.stringify(result.object);const outputProcessorResult=await this.__runOutputProcessors({runtimeContext:mergedGenerateOptions.runtimeContext||new RuntimeContext(),tracingContext,messageList:new MessageList({threadId:llmOptions.threadId||"",resourceId:llmOptions.resourceId||""}).add({role:"assistant",content:[{type:"text",text:outputText}]},"response")});if(outputProcessorResult.tripwireTriggered){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:outputProcessorResult.tripwireReason,traceId};return tripwireResult;}const newText=outputProcessorResult.messageList.get.response.v2().map(msg=>msg.content.parts.map(part=>part.type==="text"?part.text:"").join("")).join("");try{const processedObject=JSON.parse(newText);result.object=processedObject;}catch(error){this.logger.warn("Failed to parse processed output as JSON, keeping original result",{error});}const afterResult=await after({result,outputText:newText,...(generateOptions.scorers?{overrideScorers:generateOptions.scorers}:{}),structuredOutput:true,agentAISpan});if(generateOptions.returnScorerData){result.scoringData=afterResult.scoringData;}result.traceId=traceId;return result;}async stream(messages,streamOptions={}){this.logger.warn("Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 16th, 2025. Please use streamLegacy if you don't want to upgrade just yet.");return this.streamLegacy(messages,streamOptions);}async streamLegacy(messages,streamOptions={}){const defaultStreamOptions=await this.getDefaultStreamOptions({runtimeContext:streamOptions.runtimeContext});const mergedStreamOptions={...defaultStreamOptions,...streamOptions,onFinish:this.#mergeOnFinishWithTelemetry(streamOptions,defaultStreamOptions),experimental_generateMessageId:defaultStreamOptions.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)};const{llm,before,after}=await this.prepareLLMOptions(messages,mergedStreamOptions,"stream");if(llm.getModel().specificationVersion!=="v1"){this.logger.error("V2 models are not supported for stream. Please use streamVNext instead.",{modelId:llm.getModel().modelId});throw new MastraError({id:"AGENT_STREAM_V2_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,details:{modelId:llm.getModel().modelId},text:"V2 models are not supported for stream. Please use streamVNext instead."});}const beforeResult=await before();const traceId=getValidTraceId(beforeResult.agentAISpan);if(beforeResult.tripwire){const emptyResult={textStream:async function*(){}(),fullStream:Promise.resolve("").then(()=>{const emptyStream=new globalThis.ReadableStream({start(controller){controller.close();}});return emptyStream;}),text:Promise.resolve(""),usage:Promise.resolve({totalTokens:0,promptTokens:0,completionTokens:0}),finishReason:Promise.resolve("other"),tripwire:true,tripwireReason:beforeResult.tripwireReason,response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},toolCalls:Promise.resolve([]),toolResults:Promise.resolve([]),warnings:Promise.resolve(void 0),request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,traceId,toAIStream:()=>Promise.resolve("").then(()=>{const emptyStream=new globalThis.ReadableStream({start(controller){controller.close();}});return emptyStream;}),get experimental_partialOutputStream(){return async function*(){}();},pipeDataStreamToResponse:()=>Promise.resolve(),pipeTextStreamToResponse:()=>Promise.resolve(),toDataStreamResponse:()=>new Response("",{status:200,headers:{"Content-Type":"text/plain"}}),toTextStreamResponse:()=>new Response("",{status:200,headers:{"Content-Type":"text/plain"}})};return emptyResult;}const{onFinish,runId,output,experimental_output,agentAISpan,...llmOptions}=beforeResult;const overrideScorers=mergedStreamOptions.scorers;const tracingContext={currentSpan:agentAISpan};if(!output||experimental_output){this.logger.debug(`Starting agent ${this.name} llm stream call`,{runId});const streamResult=llm.__stream({...llmOptions,experimental_output,tracingContext,outputProcessors:await this.getResolvedOutputProcessors(mergedStreamOptions.runtimeContext),onFinish:async result=>{try{const outputText=result.text;await after({result,outputText,agentAISpan,...(overrideScorers?{overrideScorers}:{})});}catch(e){this.logger.error("Error saving memory on finish",{error:e,runId});}await onFinish?.({...result,runId});},runId});streamResult.traceId=traceId;return streamResult;}this.logger.debug(`Starting agent ${this.name} llm streamObject call`,{runId});const streamObjectResult=llm.__streamObject({...llmOptions,tracingContext,onFinish:async result=>{try{const outputText=JSON.stringify(result.object);await after({result,outputText,structuredOutput:true,agentAISpan,...(overrideScorers?{overrideScorers}:{})});}catch(e){this.logger.error("Error saving memory on finish",{error:e,runId});}await onFinish?.({...result,runId});},runId,structuredOutput:output});streamObjectResult.traceId=traceId;return streamObjectResult;}/**
|
|
554
|
+
}]}];}if(responseMessages){messageList.add(responseMessages,"response");}}await this.#runScorers({messageList,runId,outputText,instructions,runtimeContext,structuredOutput,overrideScorers,tracingContext:{currentSpan:agentAISpan}});agentAISpan?.end({output:{text:result?.text,object:result?.object,files:result?.files}});}async network(messages,options){const runId=options?.runId||this.#mastra?.generateId()||randomUUID();const runtimeContextToUse=options?.runtimeContext||new RuntimeContext();return await networkLoop({networkName:this.name,runtimeContext:runtimeContextToUse,runId,routingAgent:this,routingAgentOptions:{telemetry:options?.telemetry,modelSettings:options?.modelSettings},generateId:()=>this.#mastra?.generateId()||randomUUID(),maxIterations:options?.maxSteps||1,messages,threadId:typeof options?.memory?.thread==="string"?options?.memory?.thread:options?.memory?.thread?.id,resourceId:options?.memory?.resource});}async generateVNext(messages,options){const result=await this.streamVNext(messages,options);if(result.tripwire){return result;}let fullOutput=await result.getFullOutput();const error=fullOutput.error;if(fullOutput.finishReason==="error"&&error){throw error;}return fullOutput;}async streamVNext(messages,streamOptions){const defaultStreamOptions=await this.getDefaultVNextStreamOptions({runtimeContext:streamOptions?.runtimeContext});const mergedStreamOptions={...defaultStreamOptions,...streamOptions,onFinish:this.#mergeOnFinishWithTelemetry(streamOptions,defaultStreamOptions)};const llm=await this.getLLM({runtimeContext:mergedStreamOptions.runtimeContext});if(llm.getModel().specificationVersion!=="v2"){throw new MastraError({id:"AGENT_STREAM_VNEXT_V1_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:"V1 models are not supported for streamVNext. Please use stream instead."});}const result=await this.#execute({...mergedStreamOptions,messages,methodType:"streamVNext"});if(result.status!=="success"){if(result.status==="failed"){throw new MastraError({id:"AGENT_STREAM_VNEXT_FAILED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:result.error.message,details:{error:result.error.message}});}throw new MastraError({id:"AGENT_STREAM_VNEXT_UNKNOWN_ERROR",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,text:"An unknown error occurred while streaming"});}return result.result;}async generate(messages,generateOptions={}){this.logger.warn("Deprecation NOTICE:\nGenerate method will switch to use generateVNext implementation September 23rd, 2025. Please use generateLegacy if you don't want to upgrade just yet.");return this.generateLegacy(messages,generateOptions);}async generateLegacy(messages,generateOptions={}){const defaultGenerateOptions=await this.getDefaultGenerateOptions({runtimeContext:generateOptions.runtimeContext});const mergedGenerateOptions={...defaultGenerateOptions,...generateOptions,experimental_generateMessageId:defaultGenerateOptions.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)};const{llm,before,after}=await this.prepareLLMOptions(messages,mergedGenerateOptions,"generate");if(llm.getModel().specificationVersion!=="v1"){this.logger.error("V2 models are not supported for the current version of generate. Please use generateVNext instead.",{modelId:llm.getModel().modelId});throw new MastraError({id:"AGENT_GENERATE_V2_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,details:{modelId:llm.getModel().modelId},text:"V2 models are not supported for the current version of generate. Please use generateVNext instead."});}let llmToUse=llm;const beforeResult=await before();const traceId=getValidTraceId(beforeResult.agentAISpan);if(beforeResult.tripwire){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:beforeResult.tripwireReason,traceId};return tripwireResult;}const{experimental_output,output,agentAISpan,...llmOptions}=beforeResult;const tracingContext={currentSpan:agentAISpan};let finalOutputProcessors=mergedGenerateOptions.outputProcessors;if(mergedGenerateOptions.structuredOutput){const agentModel=await this.getModel({runtimeContext:mergedGenerateOptions.runtimeContext});const structuredProcessor=new StructuredOutputProcessor(mergedGenerateOptions.structuredOutput,agentModel);finalOutputProcessors=finalOutputProcessors?[...finalOutputProcessors,structuredProcessor]:[structuredProcessor];}if(!output||experimental_output){const result2=await llmToUse.__text({...llmOptions,tracingContext,experimental_output});const outputProcessorResult2=await this.__runOutputProcessors({runtimeContext:mergedGenerateOptions.runtimeContext||new RuntimeContext(),tracingContext,outputProcessorOverrides:finalOutputProcessors,messageList:new MessageList({threadId:llmOptions.threadId||"",resourceId:llmOptions.resourceId||""}).add({role:"assistant",content:[{type:"text",text:result2.text}]},"response")});if(outputProcessorResult2.tripwireTriggered){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:outputProcessorResult2.tripwireReason,traceId};return tripwireResult;}const newText2=outputProcessorResult2.messageList.get.response.v2().map(msg=>msg.content.parts.map(part=>part.type==="text"?part.text:"").join("")).join("");result2.text=newText2;if(finalOutputProcessors&&finalOutputProcessors.length>0){const messages2=outputProcessorResult2.messageList.get.response.v2();this.logger.debug("Checking messages for experimentalOutput metadata:",messages2.map(m=>({role:m.role,hasContentMetadata:!!m.content.metadata,contentMetadata:m.content.metadata})));const messagesWithStructuredData=messages2.filter(msg=>msg.content.metadata&&msg.content.metadata.structuredOutput);this.logger.debug("Messages with structured data:",messagesWithStructuredData.length);if(messagesWithStructuredData[0]&&messagesWithStructuredData[0].content.metadata?.structuredOutput){result2.object=messagesWithStructuredData[0].content.metadata.structuredOutput;this.logger.debug("Using structured data from processor metadata for result.object");}else {try{const processedOutput=JSON.parse(newText2);result2.object=processedOutput;this.logger.debug("Using fallback JSON parsing for result.object");}catch(error){this.logger.warn("Failed to parse processed output as JSON, updating text only",{error});}}}const overrideScorers=mergedGenerateOptions.scorers;const afterResult2=await after({result:result2,outputText:newText2,agentAISpan,...(overrideScorers?{overrideScorers}:{})});if(generateOptions.returnScorerData){result2.scoringData=afterResult2.scoringData;}result2.traceId=traceId;return result2;}const result=await llmToUse.__textObject({...llmOptions,tracingContext,structuredOutput:output});const outputText=JSON.stringify(result.object);const outputProcessorResult=await this.__runOutputProcessors({runtimeContext:mergedGenerateOptions.runtimeContext||new RuntimeContext(),tracingContext,messageList:new MessageList({threadId:llmOptions.threadId||"",resourceId:llmOptions.resourceId||""}).add({role:"assistant",content:[{type:"text",text:outputText}]},"response")});if(outputProcessorResult.tripwireTriggered){const tripwireResult={text:"",object:void 0,usage:{totalTokens:0,promptTokens:0,completionTokens:0},finishReason:"other",response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},responseMessages:[],toolCalls:[],toolResults:[],warnings:void 0,request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,tripwire:true,tripwireReason:outputProcessorResult.tripwireReason,traceId};return tripwireResult;}const newText=outputProcessorResult.messageList.get.response.v2().map(msg=>msg.content.parts.map(part=>part.type==="text"?part.text:"").join("")).join("");try{const processedObject=JSON.parse(newText);result.object=processedObject;}catch(error){this.logger.warn("Failed to parse processed output as JSON, keeping original result",{error});}const afterResult=await after({result,outputText:newText,...(generateOptions.scorers?{overrideScorers:generateOptions.scorers}:{}),structuredOutput:true,agentAISpan});if(generateOptions.returnScorerData){result.scoringData=afterResult.scoringData;}result.traceId=traceId;return result;}async stream(messages,streamOptions={}){this.logger.warn("Deprecation NOTICE:\nStream method will switch to use streamVNext implementation September 23rd, 2025. Please use streamLegacy if you don't want to upgrade just yet.");return this.streamLegacy(messages,streamOptions);}async streamLegacy(messages,streamOptions={}){const defaultStreamOptions=await this.getDefaultStreamOptions({runtimeContext:streamOptions.runtimeContext});const mergedStreamOptions={...defaultStreamOptions,...streamOptions,onFinish:this.#mergeOnFinishWithTelemetry(streamOptions,defaultStreamOptions),experimental_generateMessageId:defaultStreamOptions.experimental_generateMessageId||this.#mastra?.generateId?.bind(this.#mastra)};const{llm,before,after}=await this.prepareLLMOptions(messages,mergedStreamOptions,"stream");if(llm.getModel().specificationVersion!=="v1"){this.logger.error("V2 models are not supported for stream. Please use streamVNext instead.",{modelId:llm.getModel().modelId});throw new MastraError({id:"AGENT_STREAM_V2_MODEL_NOT_SUPPORTED",domain:"AGENT"/* AGENT */,category:"USER"/* USER */,details:{modelId:llm.getModel().modelId},text:"V2 models are not supported for stream. Please use streamVNext instead."});}const beforeResult=await before();const traceId=getValidTraceId(beforeResult.agentAISpan);if(beforeResult.tripwire){const emptyResult={textStream:async function*(){}(),fullStream:Promise.resolve("").then(()=>{const emptyStream=new globalThis.ReadableStream({start(controller){controller.close();}});return emptyStream;}),text:Promise.resolve(""),usage:Promise.resolve({totalTokens:0,promptTokens:0,completionTokens:0}),finishReason:Promise.resolve("other"),tripwire:true,tripwireReason:beforeResult.tripwireReason,response:{id:randomUUID(),timestamp:/* @__PURE__ */new Date(),modelId:"tripwire",messages:[]},toolCalls:Promise.resolve([]),toolResults:Promise.resolve([]),warnings:Promise.resolve(void 0),request:{body:JSON.stringify({messages:[]})},experimental_output:void 0,steps:void 0,experimental_providerMetadata:void 0,traceId,toAIStream:()=>Promise.resolve("").then(()=>{const emptyStream=new globalThis.ReadableStream({start(controller){controller.close();}});return emptyStream;}),get experimental_partialOutputStream(){return async function*(){}();},pipeDataStreamToResponse:()=>Promise.resolve(),pipeTextStreamToResponse:()=>Promise.resolve(),toDataStreamResponse:()=>new Response("",{status:200,headers:{"Content-Type":"text/plain"}}),toTextStreamResponse:()=>new Response("",{status:200,headers:{"Content-Type":"text/plain"}})};return emptyResult;}const{onFinish,runId,output,experimental_output,agentAISpan,...llmOptions}=beforeResult;const overrideScorers=mergedStreamOptions.scorers;const tracingContext={currentSpan:agentAISpan};if(!output||experimental_output){this.logger.debug(`Starting agent ${this.name} llm stream call`,{runId});const streamResult=llm.__stream({...llmOptions,experimental_output,tracingContext,outputProcessors:await this.getResolvedOutputProcessors(mergedStreamOptions.runtimeContext),onFinish:async result=>{try{const outputText=result.text;await after({result,outputText,agentAISpan,...(overrideScorers?{overrideScorers}:{})});}catch(e){this.logger.error("Error saving memory on finish",{error:e,runId});}await onFinish?.({...result,runId});},runId});streamResult.traceId=traceId;return streamResult;}this.logger.debug(`Starting agent ${this.name} llm streamObject call`,{runId});const streamObjectResult=llm.__streamObject({...llmOptions,tracingContext,onFinish:async result=>{try{const outputText=JSON.stringify(result.object);await after({result,outputText,structuredOutput:true,agentAISpan,...(overrideScorers?{overrideScorers}:{})});}catch(e){this.logger.error("Error saving memory on finish",{error:e,runId});}await onFinish?.({...result,runId});},runId,structuredOutput:output});streamObjectResult.traceId=traceId;return streamObjectResult;}/**
|
|
555
555
|
* Convert text to speech using the configured voice provider
|
|
556
556
|
* @param input Text or text stream to convert to speech
|
|
557
557
|
* @param options Speech options including speaker and provider-specific options
|
|
@@ -733,5 +733,5 @@ Target: ${this.targetLanguages.join("/")}${translate}`;}};// src/agent/input-pro
|
|
|
733
733
|
var LanguageDetectorInputProcessor=class{name="language-detector";processor;constructor(options){this.processor=new LanguageDetector(options);}async process(args){return this.processor.processInput(args);}};
|
|
734
734
|
|
|
735
735
|
export { AISDKV5OutputStream, Agent, DefaultExecutionEngine, ExecutionEngine, LanguageDetector, LanguageDetectorInputProcessor, LegacyStep, LegacyWorkflow, MastraModelOutput, ModerationInputProcessor, ModerationProcessor, PIIDetector, PIIDetectorInputProcessor, PromptInjectionDetector, PromptInjectionDetectorInputProcessor, RESOURCE_TYPES, Run, StructuredOutputProcessor, TripWire, UnicodeNormalizer, UnicodeNormalizerInputProcessor, WhenConditionReturnValue, Workflow, agentToStep, cloneStep, cloneWorkflow, createStep, createWorkflow, getActivePathsAndStatus, getResultActivePaths, getStepResult, getSuspendedPaths, isAgent, isConditionalKey, isErrorEvent, isFinalState, isLimboState, isTransitionEvent, isVariableReference, isWorkflow, loop, mapVariable, mergeChildValue, recursivelyCheckForFinalState, resolveVariables, updateStepInHierarchy, workflowToStep };
|
|
736
|
-
//# sourceMappingURL=chunk-
|
|
737
|
-
//# sourceMappingURL=chunk-
|
|
736
|
+
//# sourceMappingURL=chunk-HHRWDBFK.js.map
|
|
737
|
+
//# sourceMappingURL=chunk-HHRWDBFK.js.map
|