UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

416 lines • 16.2 kB
import type { LanguageModelV2 } from '@ai-sdk/provider-v5'; import type { CallSettings, StepResult, ToolChoice, ToolSet } from '../_types/@internal_ai-sdk-v5/dist/index.js'; import { z } from 'zod/v4'; import type { MastraMessageContentV2, MessageList } from '../agent/message-list/index.js'; import type { ModelRouterModelId } from '../llm/model/index.js'; import type { MastraLanguageModel, OpenAICompatibleConfig, SharedProviderOptions } from '../llm/model/shared.types.js'; import type { InferSchemaOutput, OutputSchema } from '../stream/base/schema.js'; import type { StructuredOutputOptions } from './processors/index.js'; export type TextPartType = { type: 'text'; text: string; }; export type ImagePartType = { type: 'image'; image: string | URL | Uint8Array; mimeType?: string; }; export type FilePartType = { type: 'file'; data: string | URL | Uint8Array; mimeType: string; }; export type ToolInvocationPartType = { type: 'tool-invocation'; toolInvocation: { toolCallId: string; toolName: string; args?: unknown; state: 'partial-call' | 'call' | 'result'; result?: unknown; }; }; export type ReasoningPartType = { type: 'reasoning'; reasoning: string; details: Array<{ type: 'text' | 'redacted'; text?: string; data?: string; }>; }; export type SourcePartType = { type: 'source'; source: { sourceType: string; id: string; url?: string; title?: string; }; }; export type StepStartPartType = { type: 'step-start'; }; export type DataPartType = { type: string; id?: string; data?: unknown; }; export type MessagePartType = TextPartType | ImagePartType | FilePartType | ToolInvocationPartType | ReasoningPartType | SourcePartType | StepStartPartType | DataPartType; export type MessageContentType = { format: 2; parts: MessagePartType[]; content?: string; metadata?: Record<string, unknown>; providerMetadata?: Record<string, unknown>; }; type SystemMessageTextPartType = { type: 'text'; text: string; }; export type SystemMessageType = { role: 'system'; content: string | Array<SystemMessageTextPartType>; experimental_providerMetadata?: Record<string, unknown>; }; type CoreMessageType = { role: 'system' | 'user' | 'assistant' | 'tool'; content?: unknown; }; export type ProcessorMessageType = { id: string; role: 'user' | 'assistant' | 'system' | 'tool' | 'signal'; createdAt: Date; threadId?: string; resourceId?: string; type?: string; content: MessageContentType; }; /** * Model type for processor step schema. * In workflows, model configs may not yet be resolved, so we accept both resolved and unresolved types. */ export type ProcessorStepModelConfig = LanguageModelV2 | ModelRouterModelId | OpenAICompatibleConfig | MastraLanguageModel; /** * Tools type for processor step schema. * Accepts both AI SDK ToolSet and generic Record for flexibility. */ export type ProcessorStepToolsConfig = ToolSet | Record<string, unknown>; export type ProcessorInputPhaseType = { phase: 'input'; messages: ProcessorMessageType[]; messageList: MessageList; systemMessages?: CoreMessageType[]; retryCount?: number; }; export type ProcessorInputStepPhaseType = { phase: 'inputStep'; messages: ProcessorMessageType[]; messageList: MessageList; stepNumber: number; systemMessages?: CoreMessageType[]; retryCount?: number; model?: ProcessorStepModelConfig; tools?: ProcessorStepToolsConfig; toolChoice?: ToolChoice<ToolSet>; activeTools?: string[]; providerOptions?: SharedProviderOptions; modelSettings?: Omit<CallSettings, 'abortSignal'>; structuredOutput?: StructuredOutputOptions<InferSchemaOutput<OutputSchema>>; steps?: Array<StepResult<ToolSet>>; messageId?: string; rotateResponseMessageId?: () => string; }; export type ProcessorOutputStreamPhaseType = { phase: 'outputStream'; part?: unknown | null; streamParts: unknown[]; state: Record<string, unknown>; messageList?: MessageList; retryCount?: number; }; /** * Serializable version of OutputResult for use in workflow step schemas. * Uses Record<string, unknown> for usage instead of LanguageModelUsage * because zod schemas need to serialize across workflow step boundaries. */ export type SerializableOutputResult = { text: string; usage: Record<string, unknown>; finishReason: string; steps: unknown[]; }; export type ProcessorOutputResultPhaseType = { phase: 'outputResult'; messages: ProcessorMessageType[]; messageList: MessageList; retryCount?: number; result?: SerializableOutputResult; }; export type ProcessorOutputStepPhaseType = { phase: 'outputStep'; messages: ProcessorMessageType[]; messageList: MessageList; stepNumber: number; finishReason?: string; toolCalls?: Array<{ toolName: string; toolCallId: string; args?: unknown; }>; text?: string; usage?: Record<string, unknown>; systemMessages?: CoreMessageType[]; retryCount?: number; }; export type ProcessorStepInputType = ProcessorInputPhaseType | ProcessorInputStepPhaseType | ProcessorOutputStreamPhaseType | ProcessorOutputResultPhaseType | ProcessorOutputStepPhaseType; export type ProcessorStepOutputType = { phase: 'input' | 'inputStep' | 'outputStream' | 'outputResult' | 'outputStep'; messages?: ProcessorMessageType[]; messageList?: MessageList; systemMessages?: CoreMessageType[]; stepNumber?: number; part?: unknown | null; streamParts?: unknown[]; state?: Record<string, unknown>; result?: SerializableOutputResult; finishReason?: string; toolCalls?: Array<{ toolName: string; toolCallId: string; args?: unknown; }>; text?: string; usage?: Record<string, unknown>; retryCount?: number; model?: MastraLanguageModel; tools?: ProcessorStepToolsConfig; toolChoice?: ToolChoice<ToolSet>; activeTools?: string[]; providerOptions?: SharedProviderOptions; modelSettings?: Omit<CallSettings, 'abortSignal'>; structuredOutput?: StructuredOutputOptions<InferSchemaOutput<OutputSchema>>; steps?: Array<StepResult<ToolSet>>; messageId?: string; rotateResponseMessageId?: () => string; }; /** * Text part in a message */ export declare const TextPartSchema: z.ZodType<TextPartType>; /** * Image part in a message */ export declare const ImagePartSchema: z.ZodType<ImagePartType>; /** * File part in a message */ export declare const FilePartSchema: z.ZodType<FilePartType>; /** * Tool invocation part in a message (covers tool-call states) */ export declare const ToolInvocationPartSchema: z.ZodType<ToolInvocationPartType>; /** * Reasoning part in a message (for models that support reasoning) */ export declare const ReasoningPartSchema: z.ZodType<ReasoningPartType>; /** * Source part in a message (for citations/references) */ export declare const SourcePartSchema: z.ZodType<SourcePartType>; /** * Step start part (marks the beginning of a step in multi-step responses) */ export declare const StepStartPartSchema: z.ZodType<StepStartPartType>; /** * Custom data part (for data-* custom parts from AI SDK writer.custom()) * This uses a regex to match any type starting with "data-" */ export declare const DataPartSchema: z.ZodType<DataPartType>; /** * Union of all message part types. * Uses passthrough to allow additional fields from the AI SDK. * Note: We can't use discriminatedUnion here because DataPartSchema uses a regex pattern. */ export declare const MessagePartSchema: z.ZodType<MessagePartType>; /** * Message content structure (MastraMessageContentV2 format) * This is a documentation-friendly schema with properly typed parts. */ export declare const MessageContentSchema: z.ZodType<MessageContentType>; /** * Schema for message content in processor workflows. * Uses the MessagePartSchema discriminated union for proper UI rendering. */ export declare const ProcessorMessageContentSchema: z.ZodType<MessageContentType>; /** * Schema for a message in the processor workflow. * This represents MastraDBMessage with properly typed fields for UI usage. * * Key fields: * - id: string - Unique message identifier * - role: 'user' | 'assistant' | 'system' - Message role * - createdAt: Date - When the message was created * - threadId?: string - Thread identifier for conversation grouping * - resourceId?: string - Resource identifier * - type?: string - Message type * - content: Message content with parts array */ export declare const ProcessorMessageSchema: z.ZodType<ProcessorMessageType>; /** * Type for a processor message - inferred from schema for consistency. * Use this type when working with processor messages in TypeScript. */ export type ProcessorMessage = ProcessorMessageType; /** * Type for message content */ export type MessageContent = MastraMessageContentV2; /** * Type for message parts - union of all possible part types. * Common part types: * - { type: 'text', text: string } * - { type: 'tool-invocation', toolInvocation: { toolCallId, toolName, args, state, result? } } * - { type: 'reasoning', reasoning: string, details: [...] } * - { type: 'source', source: { sourceType, id, url?, title? } } * - { type: 'file', data, mimeType } * - { type: 'step-start' } */ export type MessagePart = MessagePartType; /** * Schema for a system message (CoreSystemMessage from AI SDK) * System messages provide context/instructions to the model. * * Note: This is exported for documentation purposes in the UI. * The actual systemMessages array in processor args may contain * other CoreMessage types depending on the context. */ export declare const SystemMessageSchema: z.ZodType<SystemMessageType>; /** * Schema for 'input' phase - processInput * Processes input messages before they are sent to the LLM (once at the start) */ export declare const ProcessorInputPhaseSchema: z.ZodObject<{ phase: z.ZodLiteral<"input">; messages: z.ZodArray<z.ZodType<ProcessorMessageType, unknown, z.core.$ZodTypeInternals<ProcessorMessageType, unknown>>>; messageList: z.ZodCustom<MessageList, MessageList>; systemMessages: z.ZodOptional<z.ZodArray<z.ZodType<CoreMessageType, unknown, z.core.$ZodTypeInternals<CoreMessageType, unknown>>>>; retryCount: z.ZodOptional<z.ZodNumber>; }, z.core.$strip>; /** * Schema for 'inputStep' phase - processInputStep * Processes input messages at each step of the agentic loop. * Includes model/tools configuration that can be modified per-step. */ export declare const ProcessorInputStepPhaseSchema: z.ZodObject<{ phase: z.ZodLiteral<"inputStep">; messages: z.ZodArray<z.ZodType<ProcessorMessageType, unknown, z.core.$ZodTypeInternals<ProcessorMessageType, unknown>>>; messageList: z.ZodCustom<MessageList, MessageList>; stepNumber: z.ZodNumber; systemMessages: z.ZodOptional<z.ZodArray<z.ZodType<CoreMessageType, unknown, z.core.$ZodTypeInternals<CoreMessageType, unknown>>>>; retryCount: z.ZodOptional<z.ZodNumber>; messageId: z.ZodOptional<z.ZodString>; rotateResponseMessageId: z.ZodOptional<z.ZodCustom<() => string, () => string>>; model: z.ZodOptional<z.ZodCustom<ProcessorStepModelConfig, ProcessorStepModelConfig>>; tools: z.ZodOptional<z.ZodCustom<ProcessorStepToolsConfig, ProcessorStepToolsConfig>>; toolChoice: z.ZodOptional<z.ZodCustom<ToolChoice<ToolSet>, ToolChoice<ToolSet>>>; activeTools: z.ZodOptional<z.ZodArray<z.ZodString>>; providerOptions: z.ZodOptional<z.ZodCustom<SharedProviderOptions, SharedProviderOptions>>; modelSettings: z.ZodOptional<z.ZodCustom<Omit<CallSettings, "abortSignal">, Omit<CallSettings, "abortSignal">>>; structuredOutput: z.ZodOptional<z.ZodCustom<StructuredOutputOptions<unknown>, StructuredOutputOptions<unknown>>>; steps: z.ZodOptional<z.ZodCustom<StepResult<ToolSet>[], StepResult<ToolSet>[]>>; }, z.core.$strip>; /** * Schema for 'outputStream' phase - processOutputStream * Processes output stream chunks with built-in state management */ export declare const ProcessorOutputStreamPhaseSchema: z.ZodObject<{ phase: z.ZodLiteral<"outputStream">; part: z.ZodNullable<z.ZodUnknown>; streamParts: z.ZodArray<z.ZodUnknown>; state: z.ZodRecord<z.ZodString, z.ZodUnknown>; messageList: z.ZodOptional<z.ZodCustom<MessageList, MessageList>>; retryCount: z.ZodOptional<z.ZodNumber>; }, z.core.$strip>; export declare const ProcessorOutputResultPhaseSchema: z.ZodObject<{ phase: z.ZodLiteral<"outputResult">; messages: z.ZodArray<z.ZodType<ProcessorMessageType, unknown, z.core.$ZodTypeInternals<ProcessorMessageType, unknown>>>; messageList: z.ZodCustom<MessageList, MessageList>; retryCount: z.ZodOptional<z.ZodNumber>; result: z.ZodOptional<z.ZodObject<{ text: z.ZodString; usage: z.ZodRecord<z.ZodString, z.ZodUnknown>; finishReason: z.ZodString; steps: z.ZodArray<z.ZodUnknown>; }, z.core.$strip>>; }, z.core.$strip>; /** * Schema for 'outputStep' phase - processOutputStep * Processes output after each LLM response in the agentic loop, before tool execution */ export declare const ProcessorOutputStepPhaseSchema: z.ZodObject<{ phase: z.ZodLiteral<"outputStep">; messages: z.ZodArray<z.ZodType<ProcessorMessageType, unknown, z.core.$ZodTypeInternals<ProcessorMessageType, unknown>>>; messageList: z.ZodCustom<MessageList, MessageList>; stepNumber: z.ZodNumber; finishReason: z.ZodOptional<z.ZodString>; toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{ toolName: z.ZodString; toolCallId: z.ZodString; args: z.ZodUnknown; }, z.core.$strip>>>; text: z.ZodOptional<z.ZodString>; usage: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; systemMessages: z.ZodOptional<z.ZodArray<z.ZodType<CoreMessageType, unknown, z.core.$ZodTypeInternals<CoreMessageType, unknown>>>>; retryCount: z.ZodOptional<z.ZodNumber>; }, z.core.$strip>; /** * Discriminated union schema for processor step input in workflows. * * This schema uses a discriminated union based on the `phase` field, * which determines what other fields are required/available. * This makes it much clearer what data is needed for each phase * and provides better UX in the playground UI. * * Phases: * - 'input': Process input messages before LLM (once at start) * - 'inputStep': Process input messages at each agentic loop step * - 'outputStream': Process streaming chunks * - 'outputResult': Process complete output after streaming * - 'outputStep': Process output after each LLM response (before tools) */ export declare const ProcessorStepInputSchema: z.ZodType<ProcessorStepInputType>; /** * Output schema for processor step data in workflows. * * This is a more flexible schema that allows all fields to be optional * since the output from one phase may need to be passed to another. * The workflow engine handles the type narrowing internally. */ export declare const ProcessorStepOutputSchema: z.ZodType<ProcessorStepOutputType>; /** * Combined schema that works for both input and output. * Uses the discriminated union for better type inference. */ export declare const ProcessorStepSchema: z.ZodType<ProcessorStepInputType>; /** * Type for processor step data - discriminated union based on phase. * Use this for external APIs where type safety is important. */ export type ProcessorStepData = ProcessorStepInputType; /** * Flexible type for internal processor code that needs to access all fields. * This is useful when you need to pass data through without knowing the exact phase. */ export type ProcessorStepDataFlexible = ProcessorStepOutputType; /** * Input type alias for processor steps. */ export type ProcessorStepInput = ProcessorStepData; /** * Output type alias for processor steps. * Uses the flexible schema since outputs may be passed between phases. */ export type ProcessorStepOutput = ProcessorStepDataFlexible; export {}; //# sourceMappingURL=step-schema.d.ts.map