131 lines
4.8 KiB
Go
131 lines
4.8 KiB
Go
// 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 (
|
|
ErrWebSearchUnsupported = errors.New("web_search_unsupported")
|
|
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 {
|
|
UseWebSearch bool `json:"use_web_search"`
|
|
RequestID string `json:"request_id"`
|
|
Passage string `json:"passage"`
|
|
Question string `json:"question"`
|
|
}
|
|
type Turn struct {
|
|
UseWebSearch bool `json:"use_web_search"`
|
|
WebSearchPerformed bool `json:"web_search_performed"`
|
|
Citations []Citation `json:"citations"`
|
|
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 {
|
|
WebSearchSupported bool `json:"web_search_supported"`
|
|
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 {
|
|
UseWebSearch bool `json:"use_web_search,omitempty"`
|
|
Summary string `json:"summary"`
|
|
Language string `json:"language"`
|
|
History []Exchange `json:"history"`
|
|
Passage string `json:"passage"`
|
|
Question string `json:"question"`
|
|
}
|
|
type Capabilities struct {
|
|
WebSearch bool `json:"web_search"`
|
|
Create bool `json:"create"`
|
|
Continue bool `json:"continue"`
|
|
Purge bool `json:"purge"`
|
|
}
|
|
type Provider interface {
|
|
Capabilities() Capabilities
|
|
Answer(context.Context, StatelessRequest) (AnswerResult, 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, AnswerResult, 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
|
|
}
|