/** * Generate Text - AI SDK Wrapper * * Provides generateText and streamText functions that wrap the AI SDK. * These are the primary text generation primitives. */ import type { Message } from './types'; export interface GenerateTextOptions { /** Model to use (e.g., 'gpt-4o', 'claude-3-sonnet', 'openai/gpt-4o') */ model: string; /** Simple text prompt */ prompt?: string; /** Chat messages (alternative to prompt) */ messages?: Message[]; /** System message */ system?: string; /** Tools available to the model */ tools?: Record; /** Tool choice strategy */ toolChoice?: 'auto' | 'none' | 'required' | { type: 'tool'; toolName: string; }; /** Maximum tokens to generate */ maxTokens?: number; /** Temperature (0-2) */ temperature?: number; /** Top P (0-1) */ topP?: number; /** Top K */ topK?: number; /** Presence penalty */ presencePenalty?: number; /** Frequency penalty */ frequencyPenalty?: number; /** Stop sequences */ stopSequences?: string[]; /** Seed for reproducibility */ seed?: number; /** Maximum retries */ maxRetries?: number; /** Abort signal */ abortSignal?: AbortSignal; /** Additional headers */ headers?: Record; /** Maximum steps for tool calling */ maxSteps?: number; /** Callback on step finish */ onStepFinish?: (step: StepResult) => void | Promise; /** Callback on finish */ onFinish?: (result: GenerateTextResult) => void | Promise; } export interface StepResult { text: string; toolCalls: ToolCall[]; toolResults: ToolResult[]; usage: TokenUsage; finishReason: string; } export interface ToolCall { toolCallId: string; toolName: string; args: any; } export interface ToolResult { toolCallId: string; toolName: string; result: any; } export interface TokenUsage { promptTokens: number; completionTokens: number; totalTokens: number; } export interface GenerateTextResult { /** Generated text */ text: string; /** Tool calls made */ toolCalls: ToolCall[]; /** Tool results */ toolResults: ToolResult[]; /** Token usage */ usage: TokenUsage; /** Finish reason */ finishReason: string; /** All steps (for multi-step) */ steps: StepResult[]; /** Response messages */ responseMessages: Message[]; /** Warnings */ warnings?: any[]; } export interface StreamTextOptions extends GenerateTextOptions { /** Callback for each text chunk */ onChunk?: (chunk: TextStreamPart) => void | Promise; } export interface TextStreamPart { type: 'text-delta' | 'tool-call' | 'tool-result' | 'finish' | 'error'; textDelta?: string; toolCall?: ToolCall; toolResult?: ToolResult; finishReason?: string; usage?: TokenUsage; error?: Error; } export interface StreamTextResult { /** Async iterator for text chunks */ textStream: AsyncIterable; /** Full text stream with all events */ fullStream: AsyncIterable; /** Promise that resolves to the final text */ text: Promise; /** Promise that resolves to tool calls */ toolCalls: Promise; /** Promise that resolves to tool results */ toolResults: Promise; /** Promise that resolves to usage */ usage: Promise; /** Promise that resolves to finish reason */ finishReason: Promise; /** Promise that resolves to all steps */ steps: Promise; /** Promise that resolves to response messages */ responseMessages: Promise; /** Convert to Response object for streaming */ toDataStreamResponse(options?: { headers?: Record; }): Response; /** Pipe to a writable stream */ pipeDataStreamToResponse(response: any, options?: { headers?: Record; }): void; } /** * Generate text using a language model. * * @example Simple prompt * ```typescript * const result = await generateText({ * model: 'gpt-4o', * prompt: 'What is the capital of France?' * }); * console.log(result.text); * ``` * * @example Chat messages * ```typescript * const result = await generateText({ * model: 'claude-3-sonnet', * messages: [ * { role: 'user', content: 'Hello!' } * ] * }); * ``` * * @example With tools * ```typescript * const result = await generateText({ * model: 'gpt-4o', * prompt: 'What is the weather in Paris?', * tools: { * getWeather: { * description: 'Get weather for a city', * parameters: z.object({ city: z.string() }), * execute: async ({ city }) => `Weather in ${city}: 20°C` * } * }, * maxSteps: 5 * }); * ``` */ export declare function generateText(options: GenerateTextOptions): Promise; /** * Stream text using a language model. * * @example Simple streaming * ```typescript * const result = await streamText({ * model: 'gpt-4o', * prompt: 'Write a poem about AI' * }); * * for await (const chunk of result.textStream) { * process.stdout.write(chunk); * } * ``` * * @example With tools and multi-step * ```typescript * const result = await streamText({ * model: 'gpt-4o', * prompt: 'Search for AI news and summarize', * tools: { webSearch }, * maxSteps: 5, * onChunk: (chunk) => console.log(chunk) * }); * * const text = await result.text; * ``` */ export declare function streamText(options: StreamTextOptions): Promise;