0.0.4 • Published 4 months ago

@tmesoft/ai-tools-core v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Hooks 组件文档

MessageManager

消息管理器,用于处理聊天消息的增删改查操作。

类型定义

export interface Message {
  /** 消息ID */
  _id: IMessageId
  /** 消息类型 */
  type: string
  /** 消息内容 */
  content: string
  /** 消息发送者 */
  sender: IMessageSender
  /** 消息创建时间 */
  createdAt: number
  /** 消息状态:等待中、成功、失败 */
  status: IMessageStatus
  /** 消息附带文件 */
  file?: File

  // 消息渲染方式二选一
  /** 消息内容渲染函数 */
  renderMessageContent?: (msg: Message) => DefineComponent
  /** 消息组件 */
  renderComponentName?: string

  /** 消息模型 */
  model?: String
  /** 知识库名称 */
  knowlegeName?: string
  /** 文件id列表 */
  fileIds?: string[]
  /** 文档名称列表 */
  documentNames?: string[]
  /** 自定义消息属性 */
  custom?: Record<string, any>
}

主要方法

方法名参数返回值说明
getMessages-Message[]获取所有消息
prependMsgsmsgs: Message[]void在消息列表前添加消息
appendMsgmsg: MessageWithoutIdMessage在消息列表后追加消息
updateMsgmsg: Messagevoid更新指定消息
deleteMsgid: stringvoid删除指定消息
clearMsgslist?: Message[]void清空消息列表或重置为指定列表

useRoom

房间管理 Hook,用于管理聊天房间。

类型定义

interface Room {
  /** 房间ID */
  roomId: IRoomId
  /** 房间名称 */
  roomName: string
  /** 房间消息列表 */
  messages: MessageManager
  /** 房间创建时间 */
  createTime: number
  /** 房间自定义信息 */
  custom?: Record<string, any>
}

主要方法

方法名参数返回值说明
addRoomroom?: RoomWithoutIdRoom创建新房间
updateRoomroom: Roomvoid更新房间信息
prependRoomsrooms: Room[]void在房间列表前添加房间
deleteRoomByIdroomId: stringvoid删除指定房间
changeCurrentRoomByIdroomId: stringvoid切换当前房间

使用示例

// 创建消息管理器
const messageManager = new MessageManager()

// 添加消息
messageManager.appendMsg({
  type: 'text',
  content: '你好',
  sender: IMessageSender.USER,
  status: IMessageStatus.SUCCESS
})

// 使用房间管理
const { rooms, currentRoom, addRoom } = useRoom()

// 创建新房间
const newRoom = addRoom({
  roomName: '新房间',
  messages: new MessageManager()
})