76 lines
2.6 KiB
Go
76 lines
2.6 KiB
Go
// Package application holds conversation's use cases. Thin by design —
|
|
// the monolith's own conversation handling was mostly direct db calls
|
|
// with little business logic beyond ownership scoping, which the
|
|
// repository layer already enforces (see domain.ConversationRepository's
|
|
// doc comment).
|
|
package application
|
|
|
|
import (
|
|
"context"
|
|
|
|
"enkly/pkg/eventbus"
|
|
"enkly/pkg/eventbus/events"
|
|
"enkly/services/conversation/internal/domain"
|
|
)
|
|
|
|
type Publisher interface {
|
|
PublishBestEffort(ctx context.Context, routingKey string, payload any)
|
|
}
|
|
|
|
type Service struct {
|
|
Conversations domain.ConversationRepository
|
|
Messages domain.MessageRepository
|
|
Events Publisher
|
|
}
|
|
|
|
func (s *Service) CreateConversation(ctx context.Context, userID int64, title string) (*domain.Conversation, error) {
|
|
return s.Conversations.Create(ctx, userID, title)
|
|
}
|
|
|
|
func (s *Service) GetConversation(ctx context.Context, userID, id int64) (*domain.Conversation, error) {
|
|
return s.Conversations.Get(ctx, userID, id)
|
|
}
|
|
|
|
func (s *Service) ListConversations(ctx context.Context, userID int64) ([]*domain.Conversation, error) {
|
|
return s.Conversations.List(ctx, userID)
|
|
}
|
|
|
|
func (s *Service) MostRecentConversation(ctx context.Context, userID int64) (*domain.Conversation, error) {
|
|
return s.Conversations.MostRecent(ctx, userID)
|
|
}
|
|
|
|
func (s *Service) RenameConversation(ctx context.Context, userID, id int64, title string) (*domain.Conversation, error) {
|
|
return s.Conversations.Rename(ctx, userID, id, title)
|
|
}
|
|
|
|
func (s *Service) AutoTitleConversation(ctx context.Context, userID, id int64, title string) (bool, error) {
|
|
return s.Conversations.AutoTitle(ctx, userID, id, title)
|
|
}
|
|
|
|
func (s *Service) DeleteConversation(ctx context.Context, userID, id int64) error {
|
|
return s.Conversations.Delete(ctx, userID, id)
|
|
}
|
|
|
|
func (s *Service) CountMessages(ctx context.Context, conversationID int64) (int, error) {
|
|
return s.Conversations.CountMessages(ctx, conversationID)
|
|
}
|
|
|
|
func (s *Service) AddMessage(ctx context.Context, conversationID int64, role domain.Role, content, toolName string) (*domain.Message, error) {
|
|
m, err := s.Messages.Add(ctx, conversationID, role, content, toolName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.Events.PublishBestEffort(ctx, eventbus.RoutingKeyMessageCreated, events.MessageCreated{
|
|
ConversationID: conversationID, MessageID: m.ID, Role: string(role),
|
|
})
|
|
return m, nil
|
|
}
|
|
|
|
func (s *Service) ListMessages(ctx context.Context, conversationID int64) ([]*domain.Message, error) {
|
|
return s.Messages.List(ctx, conversationID)
|
|
}
|
|
|
|
func (s *Service) AddToolCall(ctx context.Context, tc domain.ToolCall) error {
|
|
return s.Messages.AddToolCall(ctx, tc)
|
|
}
|