feat: implement skill debug resume functionality and related API endpoints
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"cs-agent/internal/ai/runtime/graphs"
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/dto/response"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
|
||||
func init() {
|
||||
svc.SkillDebugRunHook = DebugRunSkill
|
||||
svc.SkillDebugResumeHook = DebugResumeSkill
|
||||
}
|
||||
|
||||
func DebugRunSkill(ctx context.Context, req request.SkillDebugRunRequest) (*response.SkillDebugRunResponse, error) {
|
||||
@@ -55,6 +57,71 @@ func DebugRunSkill(ctx context.Context, req request.SkillDebugRunRequest) (*resp
|
||||
return buildSkillDebugRunResponse(req, summary, selectedSkill), nil
|
||||
}
|
||||
|
||||
func DebugResumeSkill(ctx context.Context, req request.SkillDebugResumeRequest) (*response.SkillDebugRunResponse, error) {
|
||||
aiAgent := svc.AIAgentService.Get(req.AIAgentID)
|
||||
if aiAgent == nil || aiAgent.Status != enums.StatusOk {
|
||||
return nil, errorsx.InvalidParam("AI Agent不存在或未启用")
|
||||
}
|
||||
aiConfig := svc.AIConfigService.Get(aiAgent.AIConfigID)
|
||||
if aiConfig == nil {
|
||||
return nil, errorsx.InvalidParam("AI Agent关联的AI配置不存在")
|
||||
}
|
||||
pendingInterrupt := svc.ConversationInterruptService.GetByCheckPointID(strings.TrimSpace(req.CheckPointID))
|
||||
if pendingInterrupt == nil {
|
||||
return nil, errorsx.InvalidParam("CheckPoint 不存在")
|
||||
}
|
||||
if pendingInterrupt.AIAgentID > 0 && pendingInterrupt.AIAgentID != req.AIAgentID {
|
||||
return nil, errorsx.InvalidParam("CheckPoint 与 AI Agent 不匹配")
|
||||
}
|
||||
conversationID := req.ConversationID
|
||||
if conversationID <= 0 {
|
||||
conversationID = pendingInterrupt.ConversationID
|
||||
}
|
||||
if conversationID <= 0 {
|
||||
return nil, errorsx.InvalidParam("会话不存在")
|
||||
}
|
||||
conversation := svc.ConversationService.Get(conversationID)
|
||||
if conversation == nil {
|
||||
return nil, errorsx.InvalidParam("会话不存在")
|
||||
}
|
||||
if conversation.AIAgentID > 0 && conversation.AIAgentID != req.AIAgentID {
|
||||
return nil, errorsx.InvalidParam("会话与 AI Agent 不匹配")
|
||||
}
|
||||
resumeText := strings.TrimSpace(req.UserMessage)
|
||||
summary, err := Service.Resume(ctx, ResumeRequest{
|
||||
Conversation: conversation,
|
||||
AIAgent: aiAgent,
|
||||
AIConfig: aiConfig,
|
||||
CheckPointID: strings.TrimSpace(req.CheckPointID),
|
||||
ResumeData: map[string]any{
|
||||
strings.TrimSpace(pendingInterrupt.InterruptID): resumeText,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
if isCheckpointMissingError(err) {
|
||||
summary = &Summary{
|
||||
Status: "expired",
|
||||
ReplyText: graphs.ConfirmationExpiredReply,
|
||||
}
|
||||
if pendingInterrupt.ID > 0 {
|
||||
_ = svc.ConversationInterruptService.MarkExpired(pendingInterrupt.ID, 0)
|
||||
}
|
||||
return buildSkillDebugResumeResponse(req, summary, conversationID), nil
|
||||
}
|
||||
return buildSkillDebugResumeResponse(req, summary, conversationID), err
|
||||
}
|
||||
if pendingInterrupt.ID > 0 {
|
||||
if summary != nil && summary.Interrupted {
|
||||
_ = svc.ConversationInterruptService.MarkPendingAgain(pendingInterrupt.ID, firstInterruptID(summary), resolveInterruptPrompt(summary), 0)
|
||||
} else if summary != nil && graphs.IsCancellationReply(summary.ReplyText) {
|
||||
_ = svc.ConversationInterruptService.MarkCancelled(pendingInterrupt.ID, 0)
|
||||
} else {
|
||||
_ = svc.ConversationInterruptService.MarkResolved(pendingInterrupt.ID, 0)
|
||||
}
|
||||
}
|
||||
return buildSkillDebugResumeResponse(req, summary, conversationID), nil
|
||||
}
|
||||
|
||||
func buildSkillDebugRunResponse(req request.SkillDebugRunRequest, summary *Summary, skill *models.SkillDefinition) *response.SkillDebugRunResponse {
|
||||
resp := &response.SkillDebugRunResponse{
|
||||
ConversationID: req.ConversationID,
|
||||
@@ -86,3 +153,30 @@ func buildSkillDebugRunResponse(req request.SkillDebugRunRequest, summary *Summa
|
||||
resp.ErrorMessage = summary.ErrorMessage
|
||||
return resp
|
||||
}
|
||||
|
||||
func buildSkillDebugResumeResponse(req request.SkillDebugResumeRequest, summary *Summary, conversationID int64) *response.SkillDebugRunResponse {
|
||||
resp := &response.SkillDebugRunResponse{
|
||||
ConversationID: conversationID,
|
||||
AIAgentID: req.AIAgentID,
|
||||
}
|
||||
if summary == nil {
|
||||
return resp
|
||||
}
|
||||
resp.SkillCode = strings.TrimSpace(summary.PlannedSkillCode)
|
||||
resp.SkillName = strings.TrimSpace(summary.PlannedSkillName)
|
||||
resp.ReplyText = summary.ReplyText
|
||||
resp.PlanReason = summary.PlanReason
|
||||
resp.SkillRouteTrace = summary.SkillRouteTrace
|
||||
resp.SkillAllowedToolCodes = append([]string(nil), summary.SkillAllowedToolCodes...)
|
||||
resp.ToolCodes = append([]string(nil), summary.ToolCodes...)
|
||||
resp.InvokedToolCodes = append([]string(nil), summary.InvokedToolCodes...)
|
||||
resp.ToolSearchTrace = extractToolSearchTrace(summary)
|
||||
resp.GraphToolTrace = extractGraphToolTrace(summary)
|
||||
resp.GraphToolCode = firstGraphToolCode(summary)
|
||||
resp.InterruptType = firstInterruptType(summary)
|
||||
resp.CheckPointID = summary.CheckPointID
|
||||
resp.Interrupted = summary.Interrupted
|
||||
resp.TraceData = summary.TraceData
|
||||
resp.ErrorMessage = summary.ErrorMessage
|
||||
return resp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user