feat(qa): add web search provider port (#60)
This commit is contained in:
@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/tradarr/backend/internal/summaryqa"
|
||||
"time"
|
||||
@ -39,17 +40,33 @@ func purgeOwnedSources(ctx context.Context, tx *sql.Tx, id string, now time.Time
|
||||
_, err := tx.ExecContext(ctx, `DELETE FROM summary_qa_sources a USING summary_qa_conversations c WHERE a.conversation_id=c.id AND c.id=$1 AND c.source_expires_at<=$2`, id, now)
|
||||
return err
|
||||
}
|
||||
|
||||
type qaScanner interface{ Scan(...any) error }
|
||||
|
||||
func scanQATurn(row qaScanner) (summaryqa.Turn, error) {
|
||||
var t summaryqa.Turn
|
||||
var raw []byte
|
||||
err := row.Scan(&t.ID, &t.RequestID, &t.Passage, &t.Question, &t.Answer, &t.Status, &t.FailureCode, &t.CreatedAt, &t.UseWebSearch, &t.WebSearchPerformed, &raw)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
if err = json.Unmarshal(raw, &t.Citations); err != nil {
|
||||
return t, err
|
||||
}
|
||||
t.Citations = summaryqa.NormalizeCitations(t.Citations)
|
||||
return t, nil
|
||||
}
|
||||
func qaTurns(ctx context.Context, tx *sql.Tx, id string) ([]summaryqa.Turn, error) {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT id,request_id,passage,question,answer,status,failure_code,created_at FROM summary_qa_turns WHERE conversation_id=$1 ORDER BY ordinal`, id)
|
||||
rows, err := tx.QueryContext(ctx, `SELECT id,request_id,passage,question,answer,status,failure_code,created_at,use_web_search,web_search_performed,citations FROM summary_qa_turns WHERE conversation_id=$1 ORDER BY ordinal`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
turns := []summaryqa.Turn{}
|
||||
for rows.Next() {
|
||||
var t summaryqa.Turn
|
||||
if err = rows.Scan(&t.ID, &t.RequestID, &t.Passage, &t.Question, &t.Answer, &t.Status, &t.FailureCode, &t.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
t, scanErr := scanQATurn(rows)
|
||||
if scanErr != nil {
|
||||
return nil, scanErr
|
||||
}
|
||||
turns = append(turns, t)
|
||||
}
|
||||
@ -109,7 +126,7 @@ func (r *Repository) BeginQAQuestion(ctx context.Context, owner, summary string,
|
||||
busy := false
|
||||
for _, turn := range turns {
|
||||
if turn.RequestID == q.RequestID {
|
||||
if turn.Passage != q.Passage || turn.Question != q.Question {
|
||||
if turn.Passage != q.Passage || turn.Question != q.Question || turn.UseWebSearch != q.UseWebSearch {
|
||||
return work, summaryqa.ErrConflict
|
||||
}
|
||||
if turn.Status == "pending" {
|
||||
@ -135,14 +152,14 @@ func (r *Repository) BeginQAQuestion(ctx context.Context, owner, summary string,
|
||||
if err = tx.QueryRowContext(ctx, `SELECT s.content,u.language FROM summaries s JOIN users u ON u.id=s.user_id WHERE s.id=$1 AND s.user_id=$2`, summary, owner).Scan(&work.Summary, &work.Language); err != nil {
|
||||
return work, err
|
||||
}
|
||||
work.Turn = summaryqa.Turn{RequestID: q.RequestID, Passage: q.Passage, Question: q.Question, Status: "pending", CreatedAt: now}
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO summary_qa_turns(conversation_id,request_id,passage,question,status,created_at,ordinal) VALUES($1,$2,$3,$4,'pending',$5,$6) RETURNING id`, c.ID, q.RequestID, q.Passage, q.Question, now, len(turns)+1).Scan(&work.Turn.ID)
|
||||
work.Turn = summaryqa.Turn{RequestID: q.RequestID, Passage: q.Passage, Question: q.Question, Status: "pending", CreatedAt: now, UseWebSearch: q.UseWebSearch, Citations: []summaryqa.Citation{}}
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO summary_qa_turns(conversation_id,request_id,passage,question,status,created_at,ordinal,use_web_search) VALUES($1,$2,$3,$4,'pending',$5,$6,$7) RETURNING id`, c.ID, q.RequestID, q.Passage, q.Question, now, len(turns)+1, q.UseWebSearch).Scan(&work.Turn.ID)
|
||||
if err != nil {
|
||||
return work, err
|
||||
}
|
||||
return work, tx.Commit()
|
||||
}
|
||||
func (r *Repository) FinishQAQuestion(ctx context.Context, owner, summary, turnID, answer, code string, now time.Time) (summaryqa.Turn, error) {
|
||||
func (r *Repository) FinishQAQuestion(ctx context.Context, owner, summary, turnID string, result summaryqa.AnswerResult, code string, now time.Time) (summaryqa.Turn, error) {
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return summaryqa.Turn{}, err
|
||||
@ -155,10 +172,18 @@ func (r *Repository) FinishQAQuestion(ctx context.Context, owner, summary, turnI
|
||||
status := "answered"
|
||||
if code != "" {
|
||||
status = "failed"
|
||||
answer = ""
|
||||
result = summaryqa.AnswerResult{}
|
||||
}
|
||||
var t summaryqa.Turn
|
||||
err = tx.QueryRowContext(ctx, `UPDATE summary_qa_turns SET answer=$1,status=$2,failure_code=$3 WHERE id=$4 AND conversation_id=$5 AND status='pending' AND created_at>$6 RETURNING id,request_id,passage,question,answer,status,failure_code,created_at`, answer, status, code, turnID, c.ID, now.Add(-summaryqa.PendingLifetime)).Scan(&t.ID, &t.RequestID, &t.Passage, &t.Question, &t.Answer, &t.Status, &t.FailureCode, &t.CreatedAt)
|
||||
|
||||
citations := []summaryqa.Citation{}
|
||||
if result.WebSearchPerformed {
|
||||
citations = summaryqa.NormalizeCitations(result.Citations)
|
||||
}
|
||||
raw, err := json.Marshal(citations)
|
||||
if err != nil {
|
||||
return summaryqa.Turn{}, err
|
||||
}
|
||||
t, err := scanQATurn(tx.QueryRowContext(ctx, `UPDATE summary_qa_turns SET answer=$1,status=$2,failure_code=$3,web_search_performed=$7,citations=$8 WHERE id=$4 AND conversation_id=$5 AND status='pending' AND created_at>$6 RETURNING id,request_id,passage,question,answer,status,failure_code,created_at,use_web_search,web_search_performed,citations`, result.Answer, status, code, turnID, c.ID, now.Add(-summaryqa.PendingLifetime), result.WebSearchPerformed, string(raw)))
|
||||
if err == sql.ErrNoRows {
|
||||
return t, summaryqa.ErrConflict
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user