/** * Message protocol types for Incremark Chat-UI * Based on Vercel AI SDK & TanStack AI's parts-based message structure * * Extensibility: Users can extend MessagePart via module augmentation: * ```ts * declare module '@incremark/chat-core' { * interface CustomParts { * 'my-custom': { type: 'my-custom'; data: string }; * } * } * ``` */ /** * Supported message roles */ type MessageRole = 'user' | 'assistant' | 'system' | 'agent'; /** * Message status during lifecycle */ type MessageStatus = 'pending' | 'streaming' | 'success' | 'error'; /** * Base part interface - all parts must have a type */ interface BasePart { type: string; } /** * Text content part */ interface TextPart extends BasePart { type: 'text'; content: string; format?: 'markdown' | 'plain'; } /** * Tool call state - 用户可自定义任意状态 * 提供预定义常量供参考,但不限制用户使用其他值 */ type ToolCallState = string; /** * 预定义的工具调用状态常量(参考 Vercel AI SDK) * 用户可以使用这些常量,也可以使用自定义状态 */ declare const TOOL_CALL_STATES: { readonly INPUT_STREAMING: "input-streaming"; readonly INPUT_AVAILABLE: "input-available"; readonly APPROVAL_REQUESTED: "approval-requested"; readonly APPROVAL_RESPONDED: "approval-responded"; readonly EXECUTING: "executing"; readonly OUTPUT_AVAILABLE: "output-available"; readonly OUTPUT_ERROR: "output-error"; readonly OUTPUT_DENIED: "output-denied"; }; /** * Tool call part (when AI wants to use a tool) */ interface ToolCallPart extends BasePart { type: 'tool-call'; toolCallId: string; toolName: string; args: Record; state: ToolCallState; /** 工具执行结果 */ output?: unknown; /** 错误信息(当 state 为 output-error 时) */ error?: string; } /** * Source reference type */ type SourceType = 'url' | 'document'; /** * Source part (reference to external content) */ interface SourcePart extends BasePart { type: 'source'; sourceId: string; sourceType: SourceType; url?: string; title?: string; /** 文档类型(当 sourceType 为 document 时) */ mediaType?: string; } /** * File part (generated or uploaded file) */ interface FilePart extends BasePart { type: 'file'; fileId: string; /** base64 编码的文件内容或 URL */ data: string; mediaType: string; filename?: string; } /** * UI component part (for A2UI - Agent to UI) */ interface UIPart extends BasePart { type: 'ui'; component: string; props: Record; } /** * Reasoning/thought part (for chain-of-thought display) */ interface ReasoningPart extends BasePart { type: 'reasoning'; content: string; /** 推理状态 */ status?: 'thinking' | 'completed' | 'error'; /** 思考开始时间(毫秒时间戳) */ startTime?: number; /** 思考结束时间(毫秒时间戳) */ endTime?: number; /** 思考标题 */ title?: string; } /** * Built-in parts registry */ interface BuiltinParts { text: TextPart; 'tool-call': ToolCallPart; source: SourcePart; file: FilePart; ui: UIPart; reasoning: ReasoningPart; } /** * Custom parts registry - extend via module augmentation * @example * ```ts * declare module '@incremark/chat-core' { * interface CustomParts { * 'weather-card': { type: 'weather-card'; city: string; temp: number }; * } * } * ``` */ interface CustomParts { } /** * All registered parts */ type PartsRegistry = BuiltinParts & CustomParts; /** * Union type for all message parts (built-in + custom) */ type MessagePart = PartsRegistry[keyof PartsRegistry]; /** * Get part type by name */ type PartByType = PartsRegistry[T]; /** * Generic type guard for any part type */ declare function isPartType(part: MessagePart, type: T): part is PartsRegistry[T]; /** * Type guard for TextPart */ declare function isTextPart(part: MessagePart): part is TextPart; /** * Type guard for ToolCallPart */ declare function isToolCallPart(part: MessagePart): part is ToolCallPart; /** * Type guard for SourcePart */ declare function isSourcePart(part: MessagePart): part is SourcePart; /** * Type guard for FilePart */ declare function isFilePart(part: MessagePart): part is FilePart; /** * Type guard for UIPart */ declare function isUIPart(part: MessagePart): part is UIPart; /** * Type guard for ReasoningPart */ declare function isReasoningPart(part: MessagePart): part is ReasoningPart; /** * Complete message structure */ interface ChatMessage { id: string; parentId?: string | null; role: MessageRole; parts: MessagePart[]; status: MessageStatus; createdAt: number; metadata?: Record; } /** * Create a new text message */ declare function createTextMessage(role: MessageRole, content: string, format?: 'markdown' | 'plain'): ChatMessage; /** * Create a new empty streaming message */ declare function createStreamingMessage(role: 'assistant' | 'agent'): ChatMessage; export { type BasePart as B, type CustomParts as C, type FilePart as F, type MessageRole as M, type PartsRegistry as P, type ReasoningPart as R, type SourceType as S, type TextPart as T, type UIPart as U, type MessageStatus as a, type ToolCallState as b, TOOL_CALL_STATES as c, type ToolCallPart as d, type SourcePart as e, type BuiltinParts as f, type MessagePart as g, type PartByType as h, isPartType as i, isTextPart as j, isToolCallPart as k, isSourcePart as l, isFilePart as m, isUIPart as n, isReasoningPart as o, type ChatMessage as p, createTextMessage as q, createStreamingMessage as r };