feat: add FAQ uncovered workflow

This commit is contained in:
姚章浩
2026-07-27 11:54:34 +08:00
parent e582d851a4
commit aa0513eb59
28 changed files with 1319 additions and 274 deletions
@@ -33,6 +33,7 @@ func (s *Store) Claim(ctx context.Context, limit int, maxAttempts int, staleBefo
FROM support_cases sc
WHERE (sc.status = 'pending' OR sc.status = 'notification_failed'
OR (sc.status = 'notifying' AND sc.last_notification_at < $3))
AND sc.faq_export_status = 'exported'
AND sc.notification_attempts < $2
ORDER BY sc.created_at, sc.id
LIMIT $1
@@ -47,8 +48,8 @@ func (s *Store) Claim(ctx context.Context, limit int, maxAttempts int, staleBefo
updated_at = NOW()
FROM candidates c
WHERE sc.id = c.id
RETURNING sc.id, sc.tenant_key, sc.channel, sc.turn_id, sc.coverage_decision_id,
sc.notification_attempts, sc.created_at
RETURNING sc.id, sc.tenant_key, sc.channel, sc.turn_id,
sc.notification_attempts, sc.created_at, sc.faq_record_id
)
SELECT
cl.id,
@@ -57,25 +58,37 @@ func (s *Store) Claim(ctx context.Context, limit int, maxAttempts int, staleBefo
cl.turn_id,
cl.notification_attempts,
COALESCE(t.question, '') AS question,
COALESCE(t.answer, '') AS answer,
COALESCE(m.actor_name_snapshot, '') AS sender_name,
COALESCE(c.conversation_type, 'direct') AS conversation_type,
COALESCE(cd.reference_count, 0) AS reference_count,
COALESCE(cd.top_score, 0) AS top_score
m.created_at AS asked_at,
COALESCE(history.previous_questions, ARRAY[]::TEXT[]) AS previous_questions,
cl.faq_record_id
FROM claimed cl
JOIN turns t ON t.id = cl.turn_id
AND t.tenant_key = cl.tenant_key
AND t.channel = cl.channel
LEFT JOIN messages m ON m.id = t.request_message_id
JOIN messages m ON m.id = t.request_message_id
AND m.tenant_key = cl.tenant_key
AND m.channel = cl.channel
LEFT JOIN channel_conversations c ON c.id = m.conversation_id
AND c.tenant_key = cl.tenant_key
AND c.channel = cl.channel
JOIN coverage_decisions cd ON cd.id = cl.coverage_decision_id
AND cd.tenant_key = cl.tenant_key
AND cd.channel = cl.channel
AND cd.turn_id = cl.turn_id
LEFT JOIN LATERAL (
SELECT ARRAY_AGG(previous.question ORDER BY previous.created_at DESC) AS previous_questions
FROM (
SELECT t2.question, t2.created_at
FROM turns t2
JOIN messages m2 ON m2.id = t2.request_message_id
AND m2.tenant_key = t2.tenant_key
AND m2.channel = t2.channel
WHERE t2.tenant_key = cl.tenant_key
AND t2.channel = cl.channel
AND m2.actor_id = m.actor_id
AND t2.created_at < t.created_at
ORDER BY t2.created_at DESC
LIMIT 5
) previous
) history ON TRUE
ORDER BY cl.created_at, cl.id`,
limit, maxAttempts, staleBefore)
if err != nil {
@@ -86,34 +99,34 @@ func (s *Store) Claim(ctx context.Context, limit int, maxAttempts int, staleBefo
var cases []coverage.PendingSupportCase
for rows.Next() {
var (
id string
tenantKey string
channel string
turnID string
attempts int
question string
answer string
senderName string
conversationType string
referenceCount int
topScore float64
id string
tenantKey string
channel string
turnID string
attempts int
question string
senderName string
conversationType string
askedAt time.Time
previousQuestions []string
faqRecordID string
)
if err := rows.Scan(&id, &tenantKey, &channel, &turnID, &attempts,
&question, &answer, &senderName, &conversationType,
&referenceCount, &topScore); err != nil {
&question, &senderName, &conversationType, &askedAt,
&previousQuestions, &faqRecordID); err != nil {
rows.Close()
return nil, fmt.Errorf("claim scan: %w", err)
}
cases = append(cases, coverage.PendingSupportCase{
SupportNotification: coverage.SupportNotification{
CaseID: id,
Question: question,
Answer: answer,
SenderName: senderName,
Channel: channel,
ConversationType: core.ConversationType(conversationType),
ReferenceCount: referenceCount,
TopScore: topScore,
CaseID: id,
Question: question,
SenderName: senderName,
Channel: channel,
ConversationType: core.ConversationType(conversationType),
AskedAt: askedAt,
PreviousQuestions: previousQuestions,
FAQRecordID: faqRecordID,
},
Attempts: attempts,
})