ai-tools-core
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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[] 获取所有消息
prependMsgs msgs: Message[] void 在消息列表前添加消息
appendMsg msg: MessageWithoutId Message 在消息列表后追加消息
updateMsg msg: Message void 更新指定消息
deleteMsg id: string void 删除指定消息
clearMsgs list?: Message[] void 清空消息列表或重置为指定列表

useRoom

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

类型定义

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

主要方法

方法名 参数 返回值 说明
addRoom room?: RoomWithoutId Room 创建新房间
updateRoom room: Room void 更新房间信息
prependRooms rooms: Room[] void 在房间列表前添加房间
deleteRoomById roomId: string void 删除指定房间
changeCurrentRoomById roomId: string void 切换当前房间

使用示例

// 创建消息管理器
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()
})

Readme

Keywords

Package Sidebar

Install

npm i ai-tools-core

Weekly Downloads

6

Version

0.0.1

License

MIT

Unpacked Size

14.6 kB

Total Files

6

Last publish

Collaborators

  • peoren