2026-04-09 10:01:23 +08:00
package services
import (
"cs-agent/internal/models"
2026-04-12 14:08:16 +08:00
"cs-agent/internal/pkg/toolx"
2026-04-09 10:01:23 +08:00
"cs-agent/internal/repositories"
2026-04-10 16:46:18 +08:00
"strings"
2026-04-09 10:01:23 +08:00
"github.com/mlogclub/simple/sqls"
"github.com/mlogclub/simple/web/params"
)
var AgentRunLogService = newAgentRunLogService ()
func newAgentRunLogService () * agentRunLogService {
return & agentRunLogService {}
}
type agentRunLogService struct {}
2026-04-12 14:08:16 +08:00
type AgentRunGraphSummary struct {
TriageCount int64
TriagePrepareTicket int64
TriagePrepareTicketReady int64
TriageHandoff int64
TriageContinueAnswering int64
AnalyzeCount int64
PrepareDraftCount int64
CreateTicketCount int64
HandoffCount int64
}
2026-04-09 10:01:23 +08:00
func ( s * agentRunLogService ) Get ( id int64 ) * models . AgentRunLog {
return repositories . AgentRunLogRepository . Get ( sqls . DB (), id )
}
func ( s * agentRunLogService ) Take ( where ... interface {}) * models . AgentRunLog {
return repositories . AgentRunLogRepository . Take ( sqls . DB (), where ... )
}
func ( s * agentRunLogService ) Find ( cnd * sqls . Cnd ) [] models . AgentRunLog {
return repositories . AgentRunLogRepository . Find ( sqls . DB (), cnd )
}
func ( s * agentRunLogService ) FindOne ( cnd * sqls . Cnd ) * models . AgentRunLog {
return repositories . AgentRunLogRepository . FindOne ( sqls . DB (), cnd )
}
func ( s * agentRunLogService ) FindPageByParams ( params * params . QueryParams ) ( list [] models . AgentRunLog , paging * sqls . Paging ) {
return repositories . AgentRunLogRepository . FindPageByParams ( sqls . DB (), params )
}
func ( s * agentRunLogService ) FindPageByCnd ( cnd * sqls . Cnd ) ( list [] models . AgentRunLog , paging * sqls . Paging ) {
return repositories . AgentRunLogRepository . FindPageByCnd ( sqls . DB (), cnd )
}
func ( s * agentRunLogService ) Count ( cnd * sqls . Cnd ) int64 {
return repositories . AgentRunLogRepository . Count ( sqls . DB (), cnd )
}
func ( s * agentRunLogService ) Create ( t * models . AgentRunLog ) error {
return repositories . AgentRunLogRepository . Create ( sqls . DB (), t )
}
func ( s * agentRunLogService ) Update ( t * models . AgentRunLog ) error {
return repositories . AgentRunLogRepository . Update ( sqls . DB (), t )
}
func ( s * agentRunLogService ) Updates ( id int64 , columns map [ string ] interface {}) error {
return repositories . AgentRunLogRepository . Updates ( sqls . DB (), id , columns )
}
func ( s * agentRunLogService ) UpdateColumn ( id int64 , name string , value interface {}) error {
return repositories . AgentRunLogRepository . UpdateColumn ( sqls . DB (), id , name , value )
}
func ( s * agentRunLogService ) Delete ( id int64 ) {
repositories . AgentRunLogRepository . Delete ( sqls . DB (), id )
}
2026-04-10 16:46:18 +08:00
func ( s * agentRunLogService ) ApplyHITLStatusFilter ( cnd * sqls . Cnd , hitlStatus string ) * sqls . Cnd {
if cnd == nil {
cnd = sqls . NewCnd ()
}
switch strings . TrimSpace ( hitlStatus ) {
case "pending" :
cnd . Eq ( "final_status" , "interrupted" )
case "expired" :
cnd . Eq ( "final_status" , "expired" )
case "cancelled" :
cnd . Where ( "(reply_text LIKE ? OR reply_text LIKE ?)" , "%已取消本次工单创建%" , "%已取消本次转人工%" )
case "confirmed" :
cnd . Where ( "resume_source <> ''" )
case "triggered" :
cnd . Where ( "interrupt_type <> ''" )
}
return cnd
}
2026-04-12 14:08:16 +08:00
func ( s * agentRunLogService ) BuildGraphSummary ( aiAgentID int64 ) * AgentRunGraphSummary {
buildBaseCnd := func () * sqls . Cnd {
cnd := sqls . NewCnd ()
if aiAgentID > 0 {
cnd . Eq ( "ai_agent_id" , aiAgentID )
}
return cnd
}
return & AgentRunGraphSummary {
2026-04-12 22:27:09 +08:00
TriageCount : s . countByGraphTool ( buildBaseCnd (), toolx . GraphTriageServiceRequest . Code ),
TriagePrepareTicket : s . countByGraphToolAndPlanReason ( buildBaseCnd (), toolx . GraphTriageServiceRequest . Code , "%prepare_ticket%" ),
TriagePrepareTicketReady : s . countByGraphToolAndPlanReason ( buildBaseCnd (), toolx . GraphTriageServiceRequest . Code , "%prepare_ticket with ready ticket draft%" ),
TriageHandoff : s . countByGraphToolAndPlanReason ( buildBaseCnd (), toolx . GraphTriageServiceRequest . Code , "%handoff_to_human%" ),
TriageContinueAnswering : s . countByGraphToolAndPlanReason ( buildBaseCnd (), toolx . GraphTriageServiceRequest . Code , "%continue_answering%" ),
AnalyzeCount : s . countByGraphTool ( buildBaseCnd (), toolx . GraphAnalyzeConversation . Code ),
PrepareDraftCount : s . countByGraphTool ( buildBaseCnd (), toolx . GraphPrepareTicketDraft . Code ),
CreateTicketCount : s . countByGraphTool ( buildBaseCnd (), toolx . GraphCreateTicketConfirm . Code ),
HandoffCount : s . countByGraphTool ( buildBaseCnd (), toolx . GraphHandoffConversation . Code ),
2026-04-12 14:08:16 +08:00
}
}
func ( s * agentRunLogService ) countByGraphTool ( baseCnd * sqls . Cnd , graphToolCode string ) int64 {
cnd := baseCnd
cnd . Eq ( "graph_tool_code" , strings . TrimSpace ( graphToolCode ))
return s . Count ( cnd )
}
func ( s * agentRunLogService ) countByGraphToolAndPlanReason ( baseCnd * sqls . Cnd , graphToolCode string , planReasonLike string ) int64 {
cnd := baseCnd
cnd . Eq ( "graph_tool_code" , strings . TrimSpace ( graphToolCode ))
cnd . Where ( "plan_reason LIKE ?" , strings . TrimSpace ( planReasonLike ))
return s . Count ( cnd )
}