feat: add coverage decision persistence
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package coverage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
// Service evaluates turns and persists coverage decisions.
|
||||
type Service struct {
|
||||
evaluator *Evaluator
|
||||
repo Repository
|
||||
}
|
||||
|
||||
// NewService creates a coverage Service. repo must not be nil.
|
||||
func NewService(repo Repository) (*Service, error) {
|
||||
if repo == nil {
|
||||
return nil, errors.New("coverage service: repository must not be nil")
|
||||
}
|
||||
return &Service{
|
||||
evaluator: NewEvaluator(),
|
||||
repo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// EvaluateAndPersist evaluates a turn and persists the resulting decision.
|
||||
// For uncovered verdicts the Repository atomically creates a pending
|
||||
// support case. The operation is idempotent — replaying the same
|
||||
// (tenant_key, channel, turn_id) is safe.
|
||||
//
|
||||
// Failed and processing turns are rejected with ErrTurnNotSucceeded.
|
||||
func (s *Service) EvaluateAndPersist(ctx context.Context, turn core.Turn) (Decision, error) {
|
||||
d, err := s.evaluator.Evaluate(turn)
|
||||
if err != nil {
|
||||
return Decision{}, fmt.Errorf("evaluate: %w", err)
|
||||
}
|
||||
|
||||
if err := s.repo.SaveCoverageDecision(ctx, d); err != nil {
|
||||
return Decision{}, fmt.Errorf("save coverage decision: %w", err)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
Reference in New Issue
Block a user