@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
61 lines • 3.02 kB
TypeScript
import type { LanguageModelV2FinishReason, LanguageModelV2StreamPart, LanguageModelV2Usage, SharedV2ProviderMetadata } from '@ai-sdk/provider-v5';
import type { ModelMessage, ObjectStreamPart, TextStreamPart, ToolSet } from '../../../_types/@internal_ai-sdk-v5/dist/index.js';
import type { AIV5ResponseMessage } from '../../../agent/message-list/index.js';
import type { ChunkType } from '../../types.js';
/**
* Sanitizes tool-call input strings for safe JSON parsing.
*
* LLMs sometimes append internal tokens like `<|call|>`, `<|endoftext|>`, or `<|end|>`
* to otherwise valid JSON in streamed tool-call arguments, causing JSON.parse to fail.
*
* This function first attempts JSON.parse on the original input. If parsing succeeds,
* the original string is returned unchanged — this avoids corrupting valid JSON payloads
* that legitimately contain `<|...|>` patterns inside string values.
*
* Only when the original input is not valid JSON does the function fall back to stripping
* `<|...|>` token patterns and surrounding whitespace via regex.
*
* @see https://github.com/mastra-ai/mastra/issues/13261
* @see https://github.com/mastra-ai/mastra/issues/13185
*/
export declare function sanitizeToolCallInput(input: string): string;
/**
* Attempts to repair common JSON malformations produced by LLM providers.
*
* Some LLM providers (e.g., Kimi/K2) occasionally generate malformed JSON for
* tool call arguments. This function applies a sequence of targeted fixes for
* the most common errors before giving up.
*
* Repairs applied (in order):
* 1. Missing quote before property name: `{"a":"b",c":"d"}` → `{"a":"b","c":"d"}`
* 2. Unquoted property names: `{command:"value"}` → `{"command":"value"}`
* 3. Single quotes → double quotes (only outside already-double-quoted strings)
* 4. Trailing commas: `{"a":1,}` → `{"a":1}`
*
* @returns The parsed object if repair succeeds, or null if the JSON is unrecoverable.
* @see https://github.com/mastra-ai/mastra/issues/11078
*/
export declare function tryRepairJson(input: string): Record<string, any> | null;
export type StreamPart = Exclude<LanguageModelV2StreamPart, {
type: 'finish';
}> | {
type: 'finish';
/** Includes 'tripwire' and 'retry' for processor scenarios */
finishReason: LanguageModelV2FinishReason | 'tripwire' | 'retry';
usage: LanguageModelV2Usage;
providerMetadata: SharedV2ProviderMetadata;
messages: {
all: ModelMessage[];
user: ModelMessage[];
nonUser: AIV5ResponseMessage[];
};
};
export declare function convertFullStreamChunkToMastra(value: StreamPart, ctx: {
runId: string;
}): ChunkType | undefined;
export type OutputChunkType<OUTPUT = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<Partial<OUTPUT>> | undefined;
export declare function convertMastraChunkToAISDKv5<OUTPUT = undefined>({ chunk, mode, }: {
chunk: ChunkType<OUTPUT>;
mode?: 'generate' | 'stream';
}): OutputChunkType<OUTPUT>;
//# sourceMappingURL=transform.d.ts.map