141 lines
3.1 KiB
TypeScript
141 lines
3.1 KiB
TypeScript
/**
|
|
* ConversationService
|
|
* 负责对话的管理
|
|
*
|
|
* 职责:
|
|
* - 对话的创建、读取、删除
|
|
* - 对话与话题的关联
|
|
* - 对话元数据管理
|
|
*/
|
|
|
|
import type { Conversation } from '../../types/chat'
|
|
|
|
export interface CreateConversationOptions {
|
|
topicId: string
|
|
model?: string
|
|
temperature?: number
|
|
maxTokens?: number
|
|
systemPrompt?: string
|
|
}
|
|
|
|
export class ConversationService {
|
|
private conversations: Map<string, Conversation>
|
|
|
|
constructor(conversations: Map<string, Conversation>) {
|
|
this.conversations = conversations
|
|
}
|
|
|
|
/**
|
|
* 创建新对话
|
|
*/
|
|
createConversation(options: CreateConversationOptions): Conversation {
|
|
const conversation: Conversation = {
|
|
id: this.generateId(),
|
|
topicId: options.topicId,
|
|
messages: [],
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
metadata: {
|
|
model: options.model,
|
|
temperature: options.temperature,
|
|
maxTokens: options.maxTokens,
|
|
systemPrompt: options.systemPrompt
|
|
}
|
|
}
|
|
|
|
this.conversations.set(conversation.id, conversation)
|
|
return conversation
|
|
}
|
|
|
|
/**
|
|
* 获取对话
|
|
*/
|
|
getConversation(conversationId: string): Conversation | undefined {
|
|
return this.conversations.get(conversationId)
|
|
}
|
|
|
|
/**
|
|
* 根据 topicId 获取对话
|
|
*/
|
|
getConversationByTopicId(topicId: string): Conversation | undefined {
|
|
for (const conv of this.conversations.values()) {
|
|
if (conv.topicId === topicId) {
|
|
return conv
|
|
}
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
/**
|
|
* 获取所有对话
|
|
*/
|
|
getAllConversations(): Conversation[] {
|
|
return Array.from(this.conversations.values())
|
|
}
|
|
|
|
/**
|
|
* 删除对话
|
|
*/
|
|
deleteConversation(conversationId: string): boolean {
|
|
return this.conversations.delete(conversationId)
|
|
}
|
|
|
|
/**
|
|
* 删除话题对应的对话
|
|
*/
|
|
deleteConversationByTopicId(topicId: string): boolean {
|
|
for (const [id, conv] of this.conversations.entries()) {
|
|
if (conv.topicId === topicId) {
|
|
this.conversations.delete(id)
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
/**
|
|
* 更新对话元数据
|
|
*/
|
|
updateMetadata(
|
|
conversationId: string,
|
|
metadata: Partial<Conversation['metadata']>
|
|
): boolean {
|
|
const conversation = this.conversations.get(conversationId)
|
|
if (!conversation) return false
|
|
|
|
conversation.metadata = {
|
|
...conversation.metadata,
|
|
...metadata
|
|
}
|
|
conversation.updatedAt = new Date()
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* 清空对话消息
|
|
*/
|
|
clearMessages(conversationId: string): boolean {
|
|
const conversation = this.conversations.get(conversationId)
|
|
if (!conversation) return false
|
|
|
|
conversation.messages = []
|
|
conversation.updatedAt = new Date()
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* 获取对话消息数量
|
|
*/
|
|
getMessageCount(conversationId: string): number {
|
|
const conversation = this.conversations.get(conversationId)
|
|
return conversation?.messages.length || 0
|
|
}
|
|
|
|
/**
|
|
* 生成唯一 ID
|
|
*/
|
|
private generateId(): string {
|
|
return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
|
}
|
|
}
|