import type { AIMessageContent, ChatMessagesData, ChatMessageSetterMode, ChatRequestParams, ChatServiceConfigSetter, ChatStatus } from '../chat-engine/type'; import type { TdChatMessageProps } from '../chat-message'; import type { TdChatSenderProps } from '../chat-sender'; import type { StyledProps, TNode } from '../common'; export interface TdChatProps extends StyledProps { children?: TNode; /** 布局模式 */ layout?: 'single' | 'both'; /** 倒序渲染 */ reverse?: boolean; /** 消息列表配置(透传至t-chat-list) */ listProps?: TdChatListProps; /** 消息数据源 */ autoSendPrompt?: string; defaultMessages?: Array; /** 角色消息配置 */ messageProps?: TdChatMessageConfig | ((msg: ChatMessagesData) => TdChatMessageConfigItem); /** 输入框配置(透传至t-chat-sender) */ senderProps?: TdChatSenderProps; /** 模型服务配置 */ chatServiceConfig?: ChatServiceConfigSetter; /** 消息内容更新回调 */ onMessageChange?: (e: CustomEvent) => void; /** 消息引擎初始化完成回调 */ onChatReady?: (e: CustomEvent) => void; /** 消息发送完回调 */ onChatAfterSend?: (e: CustomEvent) => void; } export interface TdChatListScrollToOptions { behavior?: 'auto' | 'smooth'; to?: 'top' | 'bottom'; } export interface TdChatbotApi { /** * 发送用户消息 * @param params - 请求参数 */ sendUserMessage: (params: ChatRequestParams) => Promise; /** * 发送系统消息 * @param msg - 系统消息内容 */ sendSystemMessage: (msg: string) => void; /** * 发送AI回答 * @param msg - 系统消息内容 */ sendAIMessage: (options?: { params?: ChatRequestParams; content?: AIMessageContent[]; sendRequest?: boolean; }) => Promise; /** * 批量设置消息 * @param messages 要设置的消息数组 * @param mode 模式: * - replace: 完全替换当前消息(默认) * - prepend: 将消息添加到现有消息前面 * - append: 将消息追加到现有消息后面 */ setMessages: (messages: ChatMessagesData[], mode: ChatMessageSetterMode) => void; /** * 清空消息列表 */ clearMessages: () => void; /** * 中止当前聊天 */ abortChat: () => Promise; /** * 添加提示信息到输入框 * @param prompt - 提示文本 * @param autoFocus - 是否自动聚焦到输入框 */ addPrompt: (prompt: string, autoFocus?: boolean) => void; /** * 受控滚动 */ scrollList: (opt?: TdChatListScrollToOptions) => void; /** * 获取当前消息列表 */ readonly chatMessageValue: ChatMessagesData[]; /** * 当前聊天状态 */ readonly chatStatus: ChatStatus; /** * 当前输入框状态 */ readonly senderLoading: boolean; /** * ChatEngine是否就绪 */ readonly isChatEngineReady: boolean; /** * 重新发送消息 * * @param keepVersion - 是否保留之前版本(默认false) */ regenerate: (keepVersion?: boolean) => Promise; /** * 注册自定义消息内容合并策略 */ registerMergeStrategy: (type: T['type'], // 使用类型中定义的type字段作为参数类型 handler: (chunk: T, existing?: T) => T) => void; /** * 触发选择文件 */ selectFile: () => void; } export type TdChatMessageConfigItem = Omit; export type TdChatMessageConfig = { [key in ModelRoleEnum]?: TdChatMessageConfigItem; }; export type ScrollPosition = 'top' | 'bottom'; export interface TdChatListProps { children?: TNode; /** 自动滚动底部 */ autoScroll?: boolean; /** 初始滚动条停留的位置 */ defaultScrollTo?: ScrollPosition; onScroll?: (e: CustomEvent<{ scrollTop: number; }>) => void; } export interface TdChatListApi { /** 滚动到 */ scrollList: (opt?: TdChatListScrollToOptions) => void; } export interface TdChatCodeProps { lang: string; code: string; } export interface MetaData { /** * 角色头像 * @description 可选参数,如果不传则使用默认头像 */ avatar?: string; /** * 名称 * @description 可选参数,如果不传则使用默认名称 */ name?: string; /** * 附加数据 * @description 可选参数,如果不传则使用默认名称 */ [key: string]: any; } export type ModelRoleEnum = 'assistant' | 'user' | 'system'; export type Layout = 'single' | 'both'; export interface FetchSSEOptions { success?: (res: SSEEvent) => void; fail?: () => void; complete?: (isOk: Boolean, msg?: String, requestid?: String) => void; } export interface SSEEvent { type: string | null; data: string | null; } export interface BackBottomParams { behavior?: 'auto' | 'smooth'; }