feat: add DingTalk directory-backed session titles
This commit is contained in:
@@ -67,6 +67,8 @@ func (s *Store) BeginTurn(ctx context.Context, turn core.Turn) (core.Turn, bool,
|
||||
conversationTitle := ""
|
||||
if turn.ConversationType == core.ConversationDirect {
|
||||
conversationTitle = strings.TrimSpace(turn.SenderName)
|
||||
} else if turn.ConversationType == core.ConversationGroup {
|
||||
conversationTitle = strings.TrimSpace(turn.ConversationTitle)
|
||||
}
|
||||
if err = tx.QueryRow(ctx, `INSERT INTO channel_conversations
|
||||
(id,tenant_key,channel,external_conversation_id,conversation_type,title,status)
|
||||
@@ -81,13 +83,13 @@ func (s *Store) BeginTurn(ctx context.Context, turn core.Turn) (core.Turn, bool,
|
||||
|
||||
if err = tx.QueryRow(ctx, `INSERT INTO messages
|
||||
(id,tenant_key,channel,conversation_id,actor_id,external_message_id,correlation_id,
|
||||
direction,role,message_type,text_content,media_json,status,actor_name_snapshot,safe_metadata_json)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,'inbound','user',$8,$9,$10,'received',$11,'{}'::JSONB)
|
||||
direction,role,message_type,text_content,media_json,status,actor_name_snapshot,department_snapshot,region_snapshot,safe_metadata_json)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,'inbound','user',$8,$9,$10,'received',$11,$12,$13,'{}'::JSONB)
|
||||
ON CONFLICT (tenant_key,channel,external_message_id) WHERE external_message_id!=''
|
||||
DO UPDATE SET updated_at=messages.updated_at
|
||||
RETURNING id`, turn.RequestMessageID, turn.TenantKey, turn.Channel, turn.ConversationID,
|
||||
turn.ActorID, turn.ExternalMessageID, turn.ID, turn.MessageType, turn.Question, mediaJSON,
|
||||
turn.SenderName).Scan(&turn.RequestMessageID); err != nil {
|
||||
turn.SenderName, turn.DepartmentSnapshot, turn.RegionSnapshot).Scan(&turn.RequestMessageID); err != nil {
|
||||
return core.Turn{}, false, err
|
||||
}
|
||||
|
||||
@@ -137,14 +139,43 @@ func (s *Store) ResolveSession(ctx context.Context, mapping core.SessionMapping,
|
||||
return "", err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO session_mappings
|
||||
(session_key,tenant_key,channel,agent_id,conversation_type,external_subject_id,weknora_session_id)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7)`, mapping.SessionKey, mapping.TenantKey, mapping.Channel, mapping.AgentID, string(mapping.ConversationType), mapping.ExternalSubjectID, sessionID)
|
||||
(session_key,tenant_key,channel,agent_id,conversation_type,external_subject_id,weknora_session_id,display_title)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8)`, mapping.SessionKey, mapping.TenantKey, mapping.Channel, mapping.AgentID, string(mapping.ConversationType), mapping.ExternalSubjectID, sessionID, mapping.DisplayTitle)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionID, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) SyncSessionTitle(ctx context.Context, mapping core.SessionMapping, title string, update core.SessionTitleUpdate) error {
|
||||
title = strings.TrimSpace(title)
|
||||
if strings.TrimSpace(mapping.SessionKey) == "" || title == "" || update == nil {
|
||||
return errors.New("session key, title and update callback are required")
|
||||
}
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, mapping.SessionKey); err != nil {
|
||||
return err
|
||||
}
|
||||
var sessionID, currentTitle string
|
||||
if err = tx.QueryRow(ctx, `SELECT weknora_session_id,display_title FROM session_mappings WHERE session_key=$1`, mapping.SessionKey).Scan(&sessionID, ¤tTitle); err != nil {
|
||||
return err
|
||||
}
|
||||
if currentTitle == title {
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
if err = update(ctx, sessionID, title); err != nil {
|
||||
return fmt.Errorf("update session title: %w", err)
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE session_mappings SET display_title=$2,updated_at=NOW() WHERE session_key=$1`, mapping.SessionKey, title); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) FinishTurn(ctx context.Context, turn core.Turn) error {
|
||||
references, err := json.Marshal(turn.References)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user