@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
46 lines • 2.46 kB
TypeScript
import { TransformStream } from 'node:stream/web';
import type { StructuredOutputOptions } from '../../agent/types.js';
import type { IMastraLogger } from '../../logger/index.js';
import type { PublicSchema, StandardSchemaWithJSON } from '../../schema/index.js';
import type { ChunkType } from '../types.js';
type StreamTransformerStructuredOutput<OUTPUT> = Omit<StructuredOutputOptions<OUTPUT>, 'schema'> & {
schema: PublicSchema<OUTPUT>;
};
/**
* Escapes unescaped newlines, carriage returns, and tabs within JSON string values.
*
* LLMs often output actual newline characters inside JSON strings instead of properly
* escaped \n sequences, which breaks JSON parsing. This function fixes that by:
* 1. Tracking whether we're inside a JSON string (after an unescaped quote)
* 2. Replacing literal newlines/tabs with their escape sequences only inside strings
* 3. Preserving already-escaped sequences like \\n
*
* @param text - Raw JSON text that may contain unescaped control characters in strings
* @returns JSON text with control characters properly escaped inside string values
*/
export declare function escapeUnescapedControlCharsInJsonStrings(text: string): string;
/**
* Transforms raw text-delta chunks into structured object chunks for JSON mode streaming.
*
* For JSON response formats, this transformer:
* - Accumulates text deltas and parses them as partial JSON
* - Emits 'object' chunks when the parsed structure changes
* - For arrays: filters incomplete elements and unwraps from {elements: [...]} wrapper
* - For objects: emits the parsed object directly
* - For enums: unwraps from {result: ""} wrapper and provides partial matching
* - Always passes through original chunks for downstream processing
*/
export declare function createObjectStreamTransformer<OUTPUT = undefined>({ structuredOutput, logger, }: {
structuredOutput?: StreamTransformerStructuredOutput<OUTPUT>;
logger?: IMastraLogger;
}): TransformStream<ChunkType<OUTPUT>, ChunkType<OUTPUT>>;
/**
* Transforms object chunks into JSON text chunks for streaming.
*
* This transformer:
* - For arrays: emits opening bracket, new elements, and closing bracket
* - For objects/no-schema: emits the object as JSON
*/
export declare function createJsonTextStreamTransformer<OUTPUT = undefined>(schema?: StandardSchemaWithJSON<OUTPUT>): TransformStream<ChunkType<OUTPUT>, string>;
export {};
//# sourceMappingURL=output-format-handlers.d.ts.map