98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package audit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
defaultLimit = 20
|
|
maxLimit = 100
|
|
maxChannelLen = 128
|
|
maxTurnIDLen = 128
|
|
maxSummaryLen = 200
|
|
)
|
|
|
|
type Service struct {
|
|
tenantKey string
|
|
repo TurnRepository
|
|
}
|
|
|
|
func NewService(tenantKey string, repo TurnRepository) (*Service, error) {
|
|
tenantKey = strings.TrimSpace(tenantKey)
|
|
if tenantKey == "" || repo == nil {
|
|
return nil, fmt.Errorf("%w: tenant and repository are required", ErrInvalidArgument)
|
|
}
|
|
return &Service{tenantKey: tenantKey, repo: repo}, nil
|
|
}
|
|
|
|
func (s *Service) ListTurns(ctx context.Context, req ListTurnsRequest) (ListTurnsResult, error) {
|
|
if req.Limit == 0 {
|
|
req.Limit = defaultLimit
|
|
}
|
|
if req.Limit < 1 || req.Limit > maxLimit {
|
|
return ListTurnsResult{}, fmt.Errorf("%w: limit must be between 1 and %d", ErrInvalidArgument, maxLimit)
|
|
}
|
|
req.Channel = strings.TrimSpace(req.Channel)
|
|
if len(req.Channel) > maxChannelLen {
|
|
return ListTurnsResult{}, fmt.Errorf("%w: channel too long", ErrInvalidArgument)
|
|
}
|
|
if !validStatus(req.Status) {
|
|
return ListTurnsResult{}, fmt.Errorf("%w: invalid status", ErrInvalidArgument)
|
|
}
|
|
|
|
query := ListQuery{Limit: req.Limit + 1, Channel: req.Channel, Status: req.Status}
|
|
if req.Cursor != "" {
|
|
cursor, err := decodeCursor(req.Cursor)
|
|
if err != nil {
|
|
return ListTurnsResult{}, fmt.Errorf("%w: invalid cursor", ErrInvalidArgument)
|
|
}
|
|
query.BeforeCreatedAt = &cursor.CreatedAt
|
|
query.BeforeID = cursor.ID
|
|
}
|
|
items, err := s.repo.ListTurns(ctx, s.tenantKey, query)
|
|
if err != nil {
|
|
return ListTurnsResult{}, err
|
|
}
|
|
for i := range items {
|
|
items[i].QuestionSummary = truncateRunes(items[i].QuestionSummary, maxSummaryLen)
|
|
items[i].AnswerSummary = truncateRunes(items[i].AnswerSummary, maxSummaryLen)
|
|
}
|
|
result := ListTurnsResult{Items: items}
|
|
if result.Items == nil {
|
|
result.Items = []TurnSummary{}
|
|
}
|
|
if len(result.Items) > req.Limit {
|
|
result.Items = result.Items[:req.Limit]
|
|
last := result.Items[len(result.Items)-1]
|
|
result.NextCursor = encodeCursor(last.CreatedAt, last.ID)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Service) GetTurn(ctx context.Context, turnID string) (TurnDetail, error) {
|
|
turnID = strings.TrimSpace(turnID)
|
|
if turnID == "" || len(turnID) > maxTurnIDLen {
|
|
return TurnDetail{}, fmt.Errorf("%w: invalid turn id", ErrInvalidArgument)
|
|
}
|
|
return s.repo.GetTurn(ctx, s.tenantKey, turnID)
|
|
}
|
|
|
|
func validStatus(status TurnStatus) bool {
|
|
switch status {
|
|
case "", StatusProcessing, StatusSucceeded, StatusFailed, StatusCancelled:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func truncateRunes(value string, limit int) string {
|
|
runes := []rune(value)
|
|
if len(runes) <= limit {
|
|
return value
|
|
}
|
|
return string(runes[:limit])
|
|
}
|