50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package biz
|
|
|
|
import (
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/entitys"
|
|
"context"
|
|
)
|
|
|
|
type ChatHistoryBiz struct {
|
|
chatRepo *impl.ChatImpl
|
|
}
|
|
|
|
func NewChatHistoryBiz(chatRepo *impl.ChatImpl) *ChatHistoryBiz {
|
|
s := &ChatHistoryBiz{
|
|
chatRepo: chatRepo,
|
|
}
|
|
go s.AsyncProcess(context.Background())
|
|
return s
|
|
}
|
|
|
|
func (s *ChatHistoryBiz) create(ctx context.Context, sessionID, role, content string) error {
|
|
chat := model.AiChatHi{
|
|
SessionID: sessionID,
|
|
Role: role,
|
|
Content: content,
|
|
}
|
|
|
|
return s.chatRepo.Create(&chat)
|
|
}
|
|
|
|
// 添加会话历史
|
|
func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error {
|
|
return s.create(ctx, chat.SessionID, chat.Role.String(), chat.Content)
|
|
}
|
|
|
|
// 异步添加会话历史
|
|
func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) {
|
|
s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
|
SessionID: chat.SessionID,
|
|
Role: chat.Role.String(),
|
|
Content: chat.Content,
|
|
})
|
|
}
|
|
|
|
// 异步处理会话历史
|
|
func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) {
|
|
s.chatRepo.AsyncProcess(ctx)
|
|
}
|