@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
93 lines • 4.57 kB
TypeScript
import type { CoreMessage as CoreMessageV4 } from '../../../_types/@internal_ai-sdk-v4/dist/index.js';
import type { ModelMessage, ToolResultPart } from '../../../_types/@internal_ai-sdk-v5/dist/index.js';
import type { IMastraLogger } from '../../../logger/index.js';
import type { MastraDBMessage } from '../state/types.js';
import type { ResponseItemIdProvider } from './response-item-metadata.js';
/**
* Tool result with input field (Anthropic requirement)
*/
export type ToolResultWithInput = ToolResultPart & {
input: Record<string, unknown>;
};
/**
* Ensures message array is compatible with Gemini API requirements.
*
* Gemini API requires:
* 1. The first non-system message must be from the user role
* 2. Cannot have only system messages - at least one user/assistant is required
*
* @param messages - Array of model messages to validate and fix
* @param logger - Optional logger for warnings
* @returns Modified messages array that satisfies Gemini requirements
*
* @see https://github.com/mastra-ai/mastra/issues/7287 - Tool call ordering
* @see https://github.com/mastra-ai/mastra/issues/8053 - Single turn validation
* @see https://github.com/mastra-ai/mastra/issues/13045 - Empty thread support
*/
export declare function ensureGeminiCompatibleMessages<T extends ModelMessage | CoreMessageV4>(messages: T[], logger?: IMastraLogger): T[];
/**
* Ensures model messages are compatible with Anthropic API requirements.
*
* Anthropic API requires tool-result parts to include an 'input' field
* that matches the original tool call arguments.
*
* @param messages - Array of model messages to transform
* @param dbMessages - MastraDB messages to look up tool call args from
* @returns Messages with tool-result parts enriched with input field
*
* @see https://github.com/mastra-ai/mastra/issues/11376 - Anthropic models fail with empty object tool input
*/
export declare function ensureAnthropicCompatibleMessages(messages: ModelMessage[], dbMessages: MastraDBMessage[]): ModelMessage[];
/**
* Removes orphan tool_use / tool_result blocks. Anthropic requires every tool_result
* to be in the message immediately after its matching tool_use, and every tool_use
* to have a matching tool_result in the next message. Recall windows can slice
* through a parallel tool-call group and leave behind half a pair.
*/
export declare function sanitizeOrphanedToolPairs(messages: ModelMessage[]): ModelMessage[];
/**
* Checks if a message part has an OpenAI reasoning itemId.
*
* OpenAI Responses reasoning items are tracked via `providerMetadata.openai.itemId`.
* Each reasoning item has a unique itemId that must be preserved for proper deduplication.
*
* @param part - A message part to check
* @returns true if the part has an OpenAI itemId
*
* @see https://github.com/mastra-ai/mastra/issues/9005 - OpenAI reasoning items filtering
*/
export declare function hasOpenAIReasoningItemId(part: unknown): boolean;
/**
* Checks if a message part has an OpenAI-compatible Responses itemId.
*
* Provider-neutral Responses item IDs are tracked via provider metadata or
* provider options fields such as `openai.itemId` or `azure.itemId`.
*/
export declare function hasResponseProviderItemId(part: unknown): boolean;
/**
* Extracts an OpenAI itemId from a message part if present.
*
* This only inspects `providerMetadata.openai.itemId`; use
* `getResponseProviderItemIdFromPart` for provider-aware Azure/OpenAI lookups.
*
* @param part - A message part to extract from
* @returns The itemId string or undefined if not present
*/
export declare function getOpenAIReasoningItemId(part: unknown): string | undefined;
export declare function getResponseProviderItemIdFromPart(part: unknown): {
provider: ResponseItemIdProvider;
itemId: string;
} | undefined;
/**
* Finds the tool call args for a given toolCallId by searching through messages.
* This is used to reconstruct the input field when converting tool-result parts to StaticToolResult.
*
* Searches through messages in reverse order (most recent first) for better performance.
* Checks both content.parts (v2 format) and toolInvocations (legacy AIV4 format).
*
* @param messages - Array of MastraDB messages to search through
* @param toolCallId - The ID of the tool call to find args for
* @returns The args object from the matching tool call, or an empty object if not found
*/
export declare function findToolCallArgs(messages: MastraDBMessage[], toolCallId: string): Record<string, unknown>;
//# sourceMappingURL=provider-compat.d.ts.map