feat(qa): add synthesis conversations with source retention (#52)

This commit is contained in:
2026-11-25 19:46:06 +01:00
parent 90dd2a65d2
commit 287811184f
49 changed files with 2636 additions and 47 deletions

View File

@ -0,0 +1,122 @@
// Package summaryqa owns the new per-summary conversation workflow. It has no
// dependency on retrieval, legacy reports or a concrete AI client.
package summaryqa
import (
"context"
"errors"
"time"
)
var (
ErrNotFound = errors.New("qa_not_found")
ErrInvalid = errors.New("qa_invalid_request")
ErrConflict = errors.New("qa_idempotency_conflict")
ErrBusy = errors.New("qa_question_in_progress")
ErrLimit = errors.New("qa_context_limit")
ErrModel = errors.New("qa_model_unavailable")
ErrProvider = errors.New("qa_provider_failed")
ErrRateLimited = errors.New("qa_rate_limited")
)
const CallsPerMinute = 10
const CallsPerDay = 100
const ConcurrentCalls = 2
type RateLimitError struct{ RetryAfterSeconds int }
func (e *RateLimitError) Error() string { return ErrRateLimited.Error() }
func (e *RateLimitError) Unwrap() error { return ErrRateLimited }
type ProviderPermit interface{ Release() }
const MaxPassageRunes = 8000
const MaxQuestionRunes = 4000
const MaxAnswerRunes = 32000
const MaxTurns = 100
const MaxContextBytes = 128000
const PendingLifetime = 2 * time.Minute
type Question struct {
RequestID string `json:"request_id"`
Passage string `json:"passage"`
Question string `json:"question"`
}
type Turn struct {
ID string `json:"id"`
RequestID string `json:"request_id"`
Passage string `json:"passage"`
Question string `json:"question"`
Answer string `json:"answer"`
Status string `json:"status"`
FailureCode string `json:"failure_code,omitempty"`
CreatedAt time.Time `json:"created_at"`
Replayed bool `json:"replayed"`
}
type Conversation struct {
ID string `json:"id"`
SummaryID string `json:"summary_id"`
Mode string `json:"mode"`
UsesSourceArticles bool `json:"uses_source_articles"`
ProviderSessionSupported bool `json:"provider_session_supported"`
SourceExpiresAt time.Time `json:"source_expires_at"`
SourcesExpired bool `json:"sources_expired"`
SourceCount int `json:"source_count"`
Messages []Turn `json:"messages"`
}
type Exchange struct {
Passage string `json:"passage"`
Question string `json:"question"`
Answer string `json:"answer"`
}
// StatelessRequest deliberately has no article/source field.
type StatelessRequest struct {
Summary string `json:"summary"`
Language string `json:"language"`
History []Exchange `json:"history"`
Passage string `json:"passage"`
Question string `json:"question"`
}
type Capabilities struct {
Create bool `json:"create"`
Continue bool `json:"continue"`
Purge bool `json:"purge"`
}
type Provider interface {
Capabilities() Capabilities
Answer(context.Context, StatelessRequest) (string, error)
}
// A future session adapter must implement all three lifecycle operations before
// session use can be introduced. No current adapter implements this port.
type SessionProvider interface {
Provider
CreateSession(context.Context, StatelessRequest) (string, error)
ContinueSession(context.Context, string, Question) (string, error)
PurgeSession(context.Context, string) error
}
type Resolver interface {
ResolveQA(context.Context, string, string) (Provider, error)
}
type Work struct {
Conversation Conversation
Summary, Language string
History []Exchange
Turn Turn
Replay bool
}
type Repository interface {
AcquireQAProviderCall(context.Context, string, string, string) (ProviderPermit, error)
CancelQAQuestion(context.Context, string, string, string) error
GetQAConversation(context.Context, string, string, time.Time) (Conversation, error)
BeginQAQuestion(context.Context, string, string, Question, time.Time) (Work, error)
FinishQAQuestion(context.Context, string, string, string, string, string, time.Time) (Turn, error)
DeleteQAConversation(context.Context, string, string) error
PurgeExpiredQASources(context.Context, time.Time) error
}
type UseCase interface {
Get(context.Context, string, string) (Conversation, error)
Ask(context.Context, string, string, Question) (Turn, error)
Delete(context.Context, string, string) error
}