import { LanguageModelV3 } from '@ai-sdk/provider'; import { FinishReason, LanguageModelUsage, ModelMessage, Tool, UIMessage, UIMessageStreamOptions, UIMessageStreamWriter, streamText } from 'ai'; import { ChatToolAgentCode, ProjectContext, ToolBuiltInCommands } from '../types'; export declare enum ChatPromptKeys { SELECTED_PAGE_ID = "SELECTED_PAGE_ID", SELECTED_PAGE_NAME = "SELECTED_PAGE_NAME", SELECTED_COMPONENT_IDS = "SELECTED_COMPONENT_IDS", IS_PROJECT_EMPTY = "IS_PROJECT_EMPTY" } export type StreamTextOptions = Parameters[0]; export interface AgentCodePromptOptions { preamble?: string; postamble?: string; currentCode?: string; mainPage?: string; design?: string; imageRefs?: string; media?: string; icons?: string; pageLinking?: string; css?: string; responsiveStyles?: string; globalStyles?: string; plugins?: string; scripts?: string; output?: string; } export type AgentCodeStreamOptions = Partial>; export interface AgentCodeStreamOptionsFnProps { options: AgentCodeStreamOptions & Pick; toolName: ChatToolAgentCode; projectContext: ProjectContext; input: TInput; prompt: string | ModelMessage[]; getSystemPrompt: (projectContext: ProjectContext, options?: AgentCodePromptOptions) => string; } export type AgentCodeStreamOptionsFn = (props: AgentCodeStreamOptionsFnProps) => AgentCodeStreamOptions | void; export interface AgentCodePostStreamProps { content: string; statusWriter: ToolStatusWriter; toolName: ChatToolAgentCode; projectContext: ProjectContext; input: TInput; } export type AgentCodePostStream = (props: AgentCodePostStreamProps) => void | Promise; /** * Callback invoked when a streamText call finishes, useful for tracking LLM usage. */ export interface OnEndCallback { (props: { /** Token usage for this call. */ usage: LanguageModelUsage; /** Reason the model stopped generating. */ finishReason: FinishReason; /** Model used for this call. */ model: LanguageModelV3; /** The agent code tool name, when the call originates from a built-in agent tool. */ toolName?: string; }): void; } /** * Options for configuring an agent that generates code (e.g., add/edit components). */ export interface AgentCodeOptions { /** * The AI model to use for code generation. */ model: LanguageModelV3; /** * Additional stream text options passed to the agent's internal streamText calls */ streamOptions?: AgentCodeStreamOptions | AgentCodeStreamOptionsFn; /** * Hook executed after the sub-agent stream ends and before the final done status is emitted. */ postStream?: AgentCodePostStream; } /** * Return type of createToolStatusWriter function */ export interface ToolStatusWriter { /** Write in-progress status when tool starts */ start(): void; /** Write streaming status with content chunk */ update(content: string, props?: { replace?: boolean; }): void; /** Write done status */ done(): void; /** Write complete status (persisted) with metadata */ complete(metadata: Record): void; /** Write error status */ error(err: unknown): void; } /** * Options for creating a tool status writer */ export interface CreateToolStatusWriterOptions { writer: UIMessageStreamWriter; /** The tool call ID from ToolExecutionOptions */ toolCallId: string; /** The name of the tool */ name: string; /** The input passed to the tool */ input: unknown; } /** * Options for creating a tool status writer */ export interface CreateToolStatusWriterOptionsFactory extends Omit { } export interface CommandToolServerDefinition { description?: string; } export type CommandToolServerDefinitions = Partial>; export interface CreateServerCommandToolOptions { skipDefault?: boolean; commands?: CommandToolServerDefinitions; projectContext?: Partial; } /** * Options passed to the tools callback function */ export interface ToolsCallbackOptions { /** * Stream writer for tools that need to write data-tool-status or merge sub-streams */ writer: UIMessageStreamWriter; /** * Default tools based on project context. */ defaultTools: Record; /** * Abort signal passed from the request, useful for cancelling long-running tool operations */ abortSignal?: AbortSignal; /** * Create a tool status writer for sending progress updates. * Use this in custom tool execute functions to report status. * * @example * ```ts * execute: async (input, { toolCallId }) => { * const statusWriter = createToolStatusWriter({ toolCallId, name: 'myTool', input }); * statusWriter.writeStart(); * try { * // ... do work ... * statusWriter.writeDone(); * return result; * } catch (err) { * statusWriter.writeError(err); * throw err; * } * } * ``` */ createToolStatusWriter: (options: CreateToolStatusWriterOptionsFactory) => ToolStatusWriter; /** * Create a server-side runCommand tool using built-in command definitions. */ createCommandTool: (options?: CreateServerCommandToolOptions) => Tool; } export interface ChatSystemPrompt { /** * Initial part of the system prompt that sets the overall context and instructions for the AI assistant. */ preamble?: string; /** * Additional instructions or information that can be appended to the system prompt. */ postamble?: string; } /** * Options for creating a streaming response */ export interface CreateStreamResponseOptions extends Pick { /** * The AI model to use */ model: LanguageModelV3; /** * Messages from the UI conversation */ messages: UIMessage[]; /** * Project context containing information about the current project */ projectContext: ProjectContext; /** * System prompt for the chat */ systemPrompt?: string | ChatSystemPrompt; /** * Abort signal for cancelling the stream and tool operations */ abortSignal?: AbortSignal; /** * Configuration for the code generation agent. * When provided, default tools will be automatically constructed and passed to the tools callback. */ agentCode?: AgentCodeOptions; /** * Tools available to the AI. Can be a function that receives options including * the stream writer, default tools (when agentCode is provided), and abort signal. * @see https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream * @see https://ai-sdk.dev/docs/ai-sdk-ui/streaming-data */ tools?: Record | ((options: ToolsCallbackOptions) => Record); /** * Additional stream text options */ streamOptions?: Partial>; /** * Additional response options */ responseOptions?: Partial>; /** * Whether to prune messages before sending to the model. * When true (default), removes reasoning/tool calls from older messages to reduce token usage. * @default true * @see https://ai-sdk.dev/docs/reference/ai-sdk-ui/prune-messages */ pruneMessages?: boolean; /** * Callback invoked when a stream call finishes (including built-in agent code tools). * Useful for tracking LLM model usage. */ onEnd?: OnEndCallback; /** * Extract metadata to attach to each message. * Called on `start` and `finish` stream events. */ getMessageMetadata?: (options: Parameters['messageMetadata']>>[0] & { model: LanguageModelV3; }) => ReturnType['messageMetadata']>>; } /** * Options for creating a text streaming response (using toTextStreamResponse) * Similar to CreateStreamResponseOptions but excludes responseOptions and uses prompt instead of messages */ export interface CreateStreamTextResponseOptions extends Pick { /** * The AI model to use */ model: LanguageModelV3; /** * System prompt for the chat */ system?: string; /** * User prompt (single string instead of messages array) */ prompt: string; /** * Tools available to the AI */ tools?: Record; /** * Additional stream text options */ streamOptions?: Partial>; } export interface CreateModelOptions { apiKey?: string; baseURL?: string; [key: string]: unknown; }