feat: add DingTalk answer feedback workflow

This commit is contained in:
姚章浩
2026-07-21 15:23:57 +08:00
parent f0d3d5b50d
commit 38560c5f41
19 changed files with 1450 additions and 36 deletions
+30 -3
View File
@@ -19,12 +19,21 @@ const listAuditTurnsSQL = `SELECT
COALESCE(a.external_actor_id,''),COALESCE(rm.actor_name_snapshot,''),
LEFT(t.question,200),LEFT(t.answer,200),
CASE WHEN jsonb_typeof(t.references_json)='array' THEN jsonb_array_length(t.references_json) ELSE 0 END,
COALESCE(am.status,''),COALESCE(am.delivery_method,'')
COALESCE(am.status,''),COALESCE(am.delivery_method,''),
COALESCE(fc.up_count,0),COALESCE(fc.down_count,0),COALESCE(fc.unresolved_count,0)
FROM turns t
LEFT JOIN messages rm ON rm.id=t.request_message_id AND rm.tenant_key=t.tenant_key AND rm.channel=t.channel
LEFT JOIN messages am ON am.id=t.answer_message_id AND am.tenant_key=t.tenant_key AND am.channel=t.channel
LEFT JOIN channel_conversations c ON c.id=rm.conversation_id AND c.tenant_key=t.tenant_key AND c.channel=t.channel
LEFT JOIN channel_actors a ON a.id=rm.actor_id AND a.tenant_key=t.tenant_key AND a.channel=t.channel
LEFT JOIN LATERAL (
SELECT
(COUNT(*) FILTER (WHERE af.value='up'))::int AS up_count,
(COUNT(*) FILTER (WHERE af.value='down'))::int AS down_count,
(COUNT(*) FILTER (WHERE af.value='down' AND af.reason_code=''))::int AS unresolved_count
FROM answer_feedback af
WHERE af.tenant_key=t.tenant_key AND af.channel=t.channel AND af.turn_id=t.id
) fc ON TRUE
WHERE t.tenant_key=$1
AND ($2='' OR t.channel=$2)
AND ($3='' OR t.status=$3)
@@ -38,12 +47,25 @@ const getAuditTurnSQL = `SELECT
COALESCE(a.external_actor_id,''),COALESCE(rm.actor_name_snapshot,''),
t.question,t.answer,t.references_json,t.media_json,
COALESCE(rm.id,''),COALESCE(rm.direction,''),COALESCE(rm.role,''),COALESCE(rm.message_type,''),COALESCE(rm.status,''),COALESCE(rm.actor_name_snapshot,''),rm.created_at,
COALESCE(am.id,''),COALESCE(am.direction,''),COALESCE(am.role,''),COALESCE(am.message_type,''),COALESCE(am.status,''),am.created_at,am.delivered_at,COALESCE(am.delivery_method,''),COALESCE(am.failure_code,'')
COALESCE(am.id,''),COALESCE(am.direction,''),COALESCE(am.role,''),COALESCE(am.message_type,''),COALESCE(am.status,''),am.created_at,am.delivered_at,COALESCE(am.delivery_method,''),COALESCE(am.failure_code,''),
COALESCE(ff.feedback_json,'[]'::jsonb)
FROM turns t
LEFT JOIN messages rm ON rm.id=t.request_message_id AND rm.tenant_key=t.tenant_key AND rm.channel=t.channel
LEFT JOIN messages am ON am.id=t.answer_message_id AND am.tenant_key=t.tenant_key AND am.channel=t.channel
LEFT JOIN channel_conversations c ON c.id=rm.conversation_id AND c.tenant_key=t.tenant_key AND c.channel=t.channel
LEFT JOIN channel_actors a ON a.id=rm.actor_id AND a.tenant_key=t.tenant_key AND a.channel=t.channel
LEFT JOIN LATERAL (
SELECT jsonb_agg(jsonb_build_object(
'actor_external_id',fa.external_actor_id,
'actor_display_name',fa.display_name,
'value',af.value,
'reason_code',af.reason_code,
'updated_at',af.updated_at
) ORDER BY af.updated_at DESC,af.id DESC) AS feedback_json
FROM answer_feedback af
JOIN channel_actors fa ON fa.id=af.actor_id AND fa.tenant_key=af.tenant_key AND fa.channel=af.channel
WHERE af.tenant_key=t.tenant_key AND af.channel=t.channel AND af.turn_id=t.id
) ff ON TRUE
WHERE t.tenant_key=$1 AND t.id=$2`
func (s *Store) ListTurns(ctx context.Context, tenantKey string, query audit.ListQuery) ([]audit.TurnSummary, error) {
@@ -66,6 +88,7 @@ func (s *Store) ListTurns(ctx context.Context, tenantKey string, query audit.Lis
&item.ActorExternalID, &item.ActorDisplayName,
&item.QuestionSummary, &item.AnswerSummary, &item.ReferenceCount,
&item.AnswerDeliveryStatus, &item.AnswerDeliveryMethod,
&item.FeedbackUpCount, &item.FeedbackDownCount, &item.FeedbackUnresolvedCount,
); err != nil {
return nil, fmt.Errorf("scan audit turn summary: %w", err)
}
@@ -81,7 +104,7 @@ func (s *Store) ListTurns(ctx context.Context, tenantKey string, query audit.Lis
func (s *Store) GetTurn(ctx context.Context, tenantKey, turnID string) (audit.TurnDetail, error) {
var detail audit.TurnDetail
var status string
var referencesJSON, mediaJSON []byte
var referencesJSON, mediaJSON, feedbackJSON []byte
var request audit.MessageMeta
var answer audit.MessageMeta
var requestCreated, answerCreated, deliveredAt *time.Time
@@ -92,6 +115,7 @@ func (s *Store) GetTurn(ctx context.Context, tenantKey, turnID string) (audit.Tu
&detail.Question, &detail.Answer, &referencesJSON, &mediaJSON,
&request.ID, &request.Direction, &request.Role, &request.MessageType, &request.Status, &request.ActorName, &requestCreated,
&answer.ID, &answer.Direction, &answer.Role, &answer.MessageType, &answer.Status, &answerCreated, &deliveredAt, &answer.DeliveryMethod, &answer.FailureCode,
&feedbackJSON,
)
if errors.Is(err, pgx.ErrNoRows) {
return audit.TurnDetail{}, audit.ErrNotFound
@@ -117,6 +141,9 @@ func (s *Store) GetTurn(ctx context.Context, tenantKey, turnID string) (audit.Tu
}
}
detail.Media = safeAuditMedia(media)
if err := json.Unmarshal(feedbackJSON, &detail.Feedback); err != nil {
return audit.TurnDetail{}, fmt.Errorf("decode audit feedback: %w", err)
}
if request.ID != "" {
if requestCreated != nil {