Tiny Chat
实现了一套通用的 API 用于对话逻辑和存储管理,适用于多种聊天组件和存储端点。但它不包含消息的发送和响应,你可以根据你的需要使用ajax
、SSE
、websocket
等自行实现。
$ npm install tiny-chat-core
or
$ pnpm add tiny-chat-core
import TinyChat from "tiny-chat-core";
const tinyChat = new TinyChat({
sortDirection: {
chatSessions: "descend",
chatMessages: "descend",
},
});
// 定义存储驱动
tinyChat.defineDriver(ChatStorageDriver);
// 指定存储驱动
tinyChat.config({
name: "remote",
});
// 分页加载会话列表
tinyChat.getChatSessions({
current: 1,
pageSize: 10,
});
// 更新会话
tinyChat.updateChatSession({
id: "sessionId",
// 更新参数
});
// 获取会话
const chatSession = tinyChat.getChatSession("sessionId");
// 加载消息
tinyChat.getChatMessagess("sessionId", {
// 分页参数
});
// 添加会话
const chatSession = await tinyChat.addChatSession({
// 会话参数
});
// 发送消息
chatSession.sendMessage("Hello, World!");
interface TinyChatOptions {
sortDirection?: {
chatSessions?: "ascend" | "descend";
chatMessages?: "ascend" | "descend";
};
}
interface ChatStorageDriver {
/**
* 存储驱动唯一标识
*/
name: string;
/**
* Chat Session
*/
getChatSessions: (
pageParams: ChatPagination,
otherParams?: any
) => Promise<ChatSessionDto[] | undefined>;
getChatSession: (id: string) => Promise<ChatSessionDto | undefined>;
addChatSession: (
dto: AddChatSessionDto
) => Promise<ChatSessionDto["id"] | undefined>;
updateChatSession: (dto: UpdateChatSessionDto) => Promise<boolean>;
deleteChatSession: (id: string) => Promise<boolean>;
/**
* Chat Message
*/
getChatMessages: (
sessionId: ChatSessionDto["id"],
pageParams: ChatPagination,
otherParams?: any
) => Promise<ChatMessage[] | undefined>;
addChatMessage: (dto: AddChatMessageDto) => Promise<boolean>;
updateChatMessage: (dto: UpdateChatMessageDto) => Promise<boolean>;
deleteChatMessage: (id: string) => Promise<boolean>;
}
export interface ChatSessionDto {
title?: string;
/**
* 创建者
*/
creator?: string;
createAt: number;
id: string;
updateAt?: number;
/**
* 额外信息 序列化
*/
extra?: string;
}
export interface ChatMessage {
/**
* @title 内容
* @description 消息内容 markdown格式
*/
content: string;
error?: any;
model?: string;
/**
* 创建者
*/
creator?: string;
/**
* @title 父消息id
* @description 消息顺序是否需要链表结果?一般情况下按照创建时间顺序即可。该字段预留,不维护
*/
parentId?: string;
/**
* @title 角色
* @description 消息发送者的角色
*/
role: ModelRoleType;
createAt: number;
id: string;
sessionId: string;
updateAt?: number;
/**
* @title 额外信息
* @description 序列化保存
*/
extra?: string;
}
interface ChatPagination {
current: number;
pageSize: number;
}
interface DriverConfig {
name: string;
}
配置存储驱动
自定义存储驱动
根据 id 获取会话
获取已载入的会话列表
根据 id 从存储器中载入会话
从存储器中载入会话列表
创建并保存会话到存储器
修改会话(包括存储器)
删除会话(包括存储器)
tinyChat.loadChatMessagess( sessionId: string,pageParams: ChatPagination,otherParams?: any): Promise<ChatMessage[]>
载入指定会话中的消息
获取会话数据对象
消息内容和角色生成消息对象
创建临时消息,不会写入存储
chatSession.updateTempMessage(messageId: string, message: Partial<ChatMessage>): Promise<ChatMessage | null>
更新临时消息
获取指定 id 的消息(已载入的)
获取已载入的消息列表
chatSession.updateSession(dto: Pick<ChatSessionDto, "title" | "updateAt"> & { extra?: any }): Promise<boolean>
更新会话信息到存储器
从存储器中载入消息列表
删除消息
修改消息
用户发送消息
收到助手消息
收到消息
会话列表发生变化时触发
示例:
tinyChat.on("change:chatSessions", (chatSessionDtos) => {
console.log(chatSessionDtos);
});
消息列表发生变化时触发
示例:
tinyChat.on("change:chatMessages", (sessionId, chatMessages) => {
console.log(sessionId, chatMessages);
});
MIT License
Copyright (c) 2024 听风
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.