refactor: remove AgentRunLog related code and components
- Deleted AgentRunLogResponse struct from skill_response.go. - Removed agent_run_log_repository.go and agent_run_log_service.go files. - Eliminated AgentRunLogDetailDialog component and its related logic from the dashboard. - Removed agent run log fetching functions from admin API. - Updated navigation and localization files to remove references to agent run logs.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
applicationruntime "agent-desk/internal/ai/application/runtime"
|
||||
@@ -9,64 +8,33 @@ import (
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
)
|
||||
|
||||
func TestSummaryPrimaryToolCodePrefersToolSearchTarget(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
InvokedToolCodes: []string{toolx.BuiltinToolSearch.Code},
|
||||
TraceData: `{
|
||||
"toolSearch": {
|
||||
"items": [
|
||||
{"targetToolCode":"mcp/server/tool_a"}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
func TestRuntimeTraceFinalAction(t *testing.T) {
|
||||
if got := runtimeTraceFinalAction(&applicationruntime.Summary{Status: "completed", ReplyText: "ok"}); got != "reply" {
|
||||
t.Fatalf("expected reply final action, got %q", got)
|
||||
}
|
||||
|
||||
if got := summaryPrimaryToolCode(summary); got != "mcp/server/tool_a" {
|
||||
t.Fatalf("unexpected primary tool code: %q", got)
|
||||
if got := runtimeTraceFinalAction(&applicationruntime.Summary{Status: "completed"}); got != "completed" {
|
||||
t.Fatalf("expected completed final action, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToRunLogFinalAction(t *testing.T) {
|
||||
if got := toRunLogFinalAction(&applicationruntime.Summary{WorkflowVersionID: 66, ReplyText: "ok"}); got != "workflow_reply" {
|
||||
t.Fatalf("expected workflow_reply final action, got %q", got)
|
||||
}
|
||||
|
||||
if got := toRunLogFinalAction(&applicationruntime.Summary{PlannedSkillID: 44, ReplyText: "ok"}); got != "skill" {
|
||||
t.Fatalf("expected skill final action, got %q", got)
|
||||
}
|
||||
|
||||
graphSummary := &applicationruntime.Summary{
|
||||
ReplyText: "ok",
|
||||
TraceData: `{
|
||||
"graphTools": {
|
||||
"items": [
|
||||
{"toolCode":"` + toolx.GraphAnalyzeConversation.Code + `"}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
}
|
||||
if got := toRunLogFinalAction(graphSummary); got != "graph" {
|
||||
t.Fatalf("expected graph final action, got %q", got)
|
||||
}
|
||||
|
||||
if got := toRunLogFinalAction(&applicationruntime.Summary{Status: "fallback"}); got != "fallback" {
|
||||
if got := runtimeTraceFinalAction(&applicationruntime.Summary{Status: "fallback"}); got != "fallback" {
|
||||
t.Fatalf("expected fallback final action, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRunLogPlanUsesWorkflowSummary(t *testing.T) {
|
||||
plannedAction, plannedToolCode, planReason := buildRunLogPlan(&applicationruntime.Summary{
|
||||
WorkflowVersionID: 66,
|
||||
Status: "completed",
|
||||
})
|
||||
if plannedAction != "workflow" {
|
||||
t.Fatalf("expected workflow planned action, got %q", plannedAction)
|
||||
func TestExtractRuntimeToolTraces(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
TraceData: `{
|
||||
"toolSearch": {"items": [{"targetToolCode":"mcp/server/tool_a"}]},
|
||||
"graphTools": {"items": [{"toolCode":"` + toolx.GraphAnalyzeConversation.Code + `"}]}
|
||||
}`,
|
||||
}
|
||||
if plannedToolCode != "workflow/66" {
|
||||
t.Fatalf("expected workflow planned tool code, got %q", plannedToolCode)
|
||||
if got := extractToolSearchTrace(summary); got == "" {
|
||||
t.Fatalf("expected tool search trace")
|
||||
}
|
||||
if planReason == "" {
|
||||
t.Fatalf("expected plan reason")
|
||||
if got := extractGraphToolTrace(summary); got == "" {
|
||||
t.Fatalf("expected graph tool trace")
|
||||
}
|
||||
if got := firstGraphToolCode(summary); got != toolx.GraphAnalyzeConversation.Code {
|
||||
t.Fatalf("unexpected graph tool code: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,44 +71,6 @@ func TestBuildConversationInterruptStoresWorkflowCheckpointData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGraphPlanReason(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
TraceData: `{
|
||||
"graphTools": {
|
||||
"items": [
|
||||
{
|
||||
"toolCode":"` + toolx.GraphTriageServiceRequest.Code + `",
|
||||
"recommendedAction":"create_ticket",
|
||||
"ticketDraftReady": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
}
|
||||
got := graphPlanReason(summary)
|
||||
if !strings.Contains(got, "create_ticket") || !strings.Contains(got, "ready ticket draft") {
|
||||
t.Fatalf("unexpected graph plan reason: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractHandoffReason(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
TraceData: `{
|
||||
"graphTools": {
|
||||
"items": [
|
||||
{
|
||||
"toolCode":"` + toolx.GraphHandoffConversation.Code + `",
|
||||
"arguments":{"reason":" 用户明确要求人工处理 "}
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
}
|
||||
if got := extractHandoffReason(summary); got != "用户明确要求人工处理" {
|
||||
t.Fatalf("unexpected handoff reason: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeErr string
|
||||
|
||||
func (e fakeErr) Error() string {
|
||||
|
||||
@@ -1,407 +0,0 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
applicationruntime "agent-desk/internal/ai/application/runtime"
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
svc "agent-desk/internal/services"
|
||||
)
|
||||
|
||||
func newReplyRunLogService() *replyRunLogService {
|
||||
return &replyRunLogService{}
|
||||
}
|
||||
|
||||
type replyRunLogService struct{}
|
||||
|
||||
type replyRunLogInput struct {
|
||||
StartedAt time.Time
|
||||
Message models.Message
|
||||
Conversation models.Conversation
|
||||
AIAgent models.AIAgent
|
||||
Question string
|
||||
RunErr error
|
||||
Trace *aiReplyTraceData
|
||||
Summary *applicationruntime.Summary
|
||||
}
|
||||
|
||||
func (s *replyRunLogService) Write(input replyRunLogInput) {
|
||||
errorMessage := ""
|
||||
if input.RunErr != nil {
|
||||
errorMessage = input.RunErr.Error()
|
||||
} else if input.Summary != nil && strings.TrimSpace(input.Summary.ErrorMessage) != "" {
|
||||
errorMessage = strings.TrimSpace(input.Summary.ErrorMessage)
|
||||
}
|
||||
traceData := buildAIReplyTraceData(input.Trace)
|
||||
plannedAction, plannedToolCode, planReason := buildRunLogPlan(input.Summary)
|
||||
logItem := &models.AgentRunLog{
|
||||
ConversationID: input.Conversation.ID,
|
||||
MessageID: input.Message.ID,
|
||||
RequestID: input.Message.RequestID,
|
||||
AIAgentID: input.AIAgent.ID,
|
||||
AIConfigID: input.AIAgent.AIConfigID,
|
||||
UserMessage: strings.TrimSpace(input.Question),
|
||||
PlannedAction: plannedAction,
|
||||
PlannedSkillID: summaryPlannedSkillID(input.Summary),
|
||||
PlannedSkillName: strings.TrimSpace(summaryPlannedSkillName(input.Summary)),
|
||||
SkillRouteTrace: strings.TrimSpace(summarySkillRouteTrace(input.Summary)),
|
||||
ToolSearchTrace: extractToolSearchTrace(input.Summary),
|
||||
GraphToolTrace: extractGraphToolTrace(input.Summary),
|
||||
GraphToolCode: firstGraphToolCode(input.Summary),
|
||||
HandoffReason: extractHandoffReason(input.Summary),
|
||||
PlannedToolCode: plannedToolCode,
|
||||
PlanReason: planReason,
|
||||
InterruptType: firstInterruptType(input.Summary),
|
||||
ResumeSource: runLogResumeSource(input.Trace),
|
||||
FinalAction: toRunLogFinalAction(input.Summary),
|
||||
FinalStatus: runLogFinalStatus(input.Summary),
|
||||
ReplyText: buildRunLogReplyText(input.Summary),
|
||||
ErrorMessage: errorMessage,
|
||||
LatencyMs: time.Since(input.StartedAt).Milliseconds(),
|
||||
TraceData: traceData,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if err := svc.AgentRunLogService.Create(logItem); err != nil {
|
||||
slog.Warn("create agent run log failed",
|
||||
"requestId", input.Message.RequestID,
|
||||
"message_id", input.Message.ID,
|
||||
"conversation_id", logItem.ConversationID,
|
||||
"ai_agent_id", input.AIAgent.ID,
|
||||
"error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func buildAIReplyTraceData(trace *aiReplyTraceData) string {
|
||||
if trace == nil {
|
||||
return ""
|
||||
}
|
||||
data, err := json.Marshal(trace)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func buildRunLogPlan(summary *applicationruntime.Summary) (plannedAction, plannedToolCode, planReason string) {
|
||||
if summary == nil {
|
||||
return "", "", ""
|
||||
}
|
||||
if isWorkflowSummary(summary) {
|
||||
return "workflow", workflowPlannedToolCode(summary), "workflow executed"
|
||||
}
|
||||
if summaryPlannedSkillID(summary) > 0 {
|
||||
reason := strings.TrimSpace(summary.PlanReason)
|
||||
if reason == "" {
|
||||
reason = "skill_selected"
|
||||
}
|
||||
return "skill", "", reason
|
||||
}
|
||||
if strings.TrimSpace(summary.Status) == "expired" {
|
||||
return "interrupt", "", "pending interrupt checkpoint expired"
|
||||
}
|
||||
if summary.Interrupted {
|
||||
if graphToolCode := firstGraphToolCode(summary); graphToolCode != "" {
|
||||
reason := graphPlanReason(summary)
|
||||
if reason == "" {
|
||||
reason = "graph tool interrupted and is waiting for user confirmation"
|
||||
}
|
||||
return "graph", graphToolCode, reason
|
||||
}
|
||||
return "tool", summaryPrimaryToolCode(summary), "agent interrupted and is waiting for user confirmation"
|
||||
}
|
||||
if len(summary.InvokedToolCodes) > 0 {
|
||||
if graphToolCode := firstGraphToolCode(summary); graphToolCode != "" {
|
||||
reason := graphPlanReason(summary)
|
||||
if reason == "" {
|
||||
reason = "agent invoked graph tool"
|
||||
}
|
||||
return "graph", graphToolCode, reason
|
||||
}
|
||||
toolCode := summaryPrimaryToolCode(summary)
|
||||
reason := "agent invoked MCP tool"
|
||||
if toolCode != "" && toolCode != firstInvokedToolCode(summary) {
|
||||
reason = "agent invoked dynamic tool via tool_search"
|
||||
}
|
||||
return "tool", toolCode, reason
|
||||
}
|
||||
if strings.TrimSpace(summary.ReplyText) != "" {
|
||||
return "reply", "", "agent replied directly"
|
||||
}
|
||||
if strings.TrimSpace(summary.ErrorMessage) != "" {
|
||||
return "error", "", "runtime execution failed"
|
||||
}
|
||||
return "fallback", "", "runtime produced empty reply"
|
||||
}
|
||||
|
||||
func toRunLogFinalAction(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
if isWorkflowSummary(summary) {
|
||||
return workflowFinalAction(summary)
|
||||
}
|
||||
if summaryPlannedSkillID(summary) > 0 && strings.TrimSpace(summary.ReplyText) != "" {
|
||||
return "skill"
|
||||
}
|
||||
if graphToolCode := firstGraphToolCode(summary); graphToolCode != "" && strings.TrimSpace(summary.ReplyText) != "" {
|
||||
return "graph"
|
||||
}
|
||||
switch strings.TrimSpace(summary.Status) {
|
||||
case "completed":
|
||||
return "reply"
|
||||
case "fallback":
|
||||
return "fallback"
|
||||
case "error":
|
||||
return "error"
|
||||
case "interrupted":
|
||||
return "interrupted"
|
||||
case "expired":
|
||||
return "expired"
|
||||
default:
|
||||
return strings.TrimSpace(summary.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func isWorkflowSummary(summary *applicationruntime.Summary) bool {
|
||||
return summary != nil && summary.WorkflowVersionID > 0
|
||||
}
|
||||
|
||||
func workflowPlannedToolCode(summary *applicationruntime.Summary) string {
|
||||
if summary == nil || summary.WorkflowVersionID <= 0 {
|
||||
return ""
|
||||
}
|
||||
return "workflow/" + strconv.FormatInt(summary.WorkflowVersionID, 10)
|
||||
}
|
||||
|
||||
func workflowFinalAction(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
switch strings.TrimSpace(summary.Status) {
|
||||
case "interrupted":
|
||||
return "workflow_interrupted"
|
||||
case "error":
|
||||
return "workflow_error"
|
||||
case "expired":
|
||||
return "workflow_expired"
|
||||
}
|
||||
if strings.TrimSpace(summary.ReplyText) != "" {
|
||||
return "workflow_reply"
|
||||
}
|
||||
return "workflow_completed"
|
||||
}
|
||||
|
||||
func buildRunLogReplyText(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(summary.ReplyText)
|
||||
}
|
||||
|
||||
func summaryPlannedSkillID(summary *applicationruntime.Summary) int64 {
|
||||
if summary == nil {
|
||||
return 0
|
||||
}
|
||||
return summary.PlannedSkillID
|
||||
}
|
||||
|
||||
func summaryPlannedSkillName(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(summary.PlannedSkillName)
|
||||
}
|
||||
|
||||
func summarySkillRouteTrace(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(summary.SkillRouteTrace)
|
||||
}
|
||||
|
||||
func runLogResumeSource(trace *aiReplyTraceData) string {
|
||||
if trace == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(trace.ResumeSource)
|
||||
}
|
||||
|
||||
func runLogFinalStatus(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(summary.Status)
|
||||
}
|
||||
|
||||
func summaryPrimaryToolCode(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
toolCode := firstInvokedToolCode(summary)
|
||||
if toolCode != toolx.BuiltinToolSearch.Code {
|
||||
return toolCode
|
||||
}
|
||||
if targetToolCode := firstToolSearchTargetToolCode(summary); targetToolCode != "" {
|
||||
return targetToolCode
|
||||
}
|
||||
return toolCode
|
||||
}
|
||||
|
||||
func extractToolSearchTrace(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
if len(trace.ToolSearch.Items) == 0 {
|
||||
return ""
|
||||
}
|
||||
buf, err := json.Marshal(trace.ToolSearch)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func extractGraphToolTrace(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
if len(trace.GraphTools.Items) == 0 {
|
||||
return ""
|
||||
}
|
||||
buf, err := json.Marshal(trace.GraphTools)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func firstToolSearchTargetToolCode(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
for _, item := range trace.ToolSearch.Items {
|
||||
toolCode := strings.TrimSpace(item.TargetToolCode)
|
||||
if toolCode != "" {
|
||||
return toolCode
|
||||
}
|
||||
if len(item.CandidateToolCodes) == 1 {
|
||||
toolCode = strings.TrimSpace(item.CandidateToolCodes[0])
|
||||
if toolCode != "" {
|
||||
return toolCode
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstGraphToolCode(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
for _, item := range trace.GraphTools.Items {
|
||||
toolCode := strings.TrimSpace(item.ToolCode)
|
||||
if toolCode != "" {
|
||||
return toolCode
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractHandoffReason(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
for _, item := range trace.GraphTools.Items {
|
||||
if strings.TrimSpace(item.ToolCode) != toolx.GraphHandoffConversation.Code {
|
||||
continue
|
||||
}
|
||||
if len(item.Arguments) == 0 {
|
||||
return ""
|
||||
}
|
||||
var args runtimeTraceHandoffArguments
|
||||
if err := json.Unmarshal(item.Arguments, &args); err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(args.Reason)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func graphPlanReason(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
for _, item := range trace.GraphTools.Items {
|
||||
toolCode := strings.TrimSpace(item.ToolCode)
|
||||
switch toolCode {
|
||||
case toolx.GraphTriageServiceRequest.Code:
|
||||
recommendedAction := strings.TrimSpace(item.RecommendedAction)
|
||||
if recommendedAction == "" {
|
||||
return "graph tool triaged service request"
|
||||
}
|
||||
if item.TicketDraftReady {
|
||||
return "graph tool triaged service request: " + recommendedAction + " with ready ticket draft"
|
||||
}
|
||||
return "graph tool triaged service request: " + recommendedAction
|
||||
case toolx.GraphAnalyzeConversation.Code:
|
||||
recommendedAction := strings.TrimSpace(item.RecommendedAction)
|
||||
riskLevel := strings.TrimSpace(item.RiskLevel)
|
||||
switch {
|
||||
case recommendedAction != "" && riskLevel != "":
|
||||
return "graph tool analyzed conversation: " + recommendedAction + " (" + riskLevel + " risk)"
|
||||
case recommendedAction != "":
|
||||
return "graph tool analyzed conversation: " + recommendedAction
|
||||
case riskLevel != "":
|
||||
return "graph tool analyzed conversation (" + riskLevel + " risk)"
|
||||
default:
|
||||
return "graph tool analyzed conversation"
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type runtimeTraceProjection struct {
|
||||
ToolSearch struct {
|
||||
Items []struct {
|
||||
TargetToolCode string `json:"targetToolCode"`
|
||||
CandidateToolCodes []string `json:"candidateToolCodes"`
|
||||
} `json:"items"`
|
||||
} `json:"toolSearch"`
|
||||
GraphTools struct {
|
||||
Items []struct {
|
||||
ToolCode string `json:"toolCode"`
|
||||
Arguments json.RawMessage `json:"arguments"`
|
||||
RecommendedAction string `json:"recommendedAction"`
|
||||
RiskLevel string `json:"riskLevel"`
|
||||
TicketDraftReady bool `json:"ticketDraftReady"`
|
||||
} `json:"items"`
|
||||
} `json:"graphTools"`
|
||||
}
|
||||
|
||||
type runtimeTraceHandoffArguments struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func parseRuntimeTraceData(raw string) runtimeTraceProjection {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return runtimeTraceProjection{}
|
||||
}
|
||||
var trace runtimeTraceProjection
|
||||
if err := json.Unmarshal([]byte(raw), &trace); err != nil {
|
||||
return runtimeTraceProjection{}
|
||||
}
|
||||
return trace
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/enums"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func TestReplyRunLogStoresRequestID(t *testing.T) {
|
||||
dbName := "reply_runlog_trace_test_" + strings.NewReplacer("/", "_").Replace(t.Name())
|
||||
db, err := gorm.Open(sqlite.Open("file:"+dbName+"?mode=memory&cache=shared"), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: "t_",
|
||||
SingularTable: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite db: %v", err)
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
t.Fatalf("get sqlite db: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := sqlDB.Close(); err != nil {
|
||||
t.Fatalf("close sqlite db: %v", err)
|
||||
}
|
||||
})
|
||||
if err := db.AutoMigrate(&models.AgentRunLog{}); err != nil {
|
||||
t.Fatalf("auto migrate: %v", err)
|
||||
}
|
||||
sqls.SetDB(db)
|
||||
|
||||
newReplyRunLogService().Write(replyRunLogInput{
|
||||
StartedAt: time.Now(),
|
||||
Message: models.Message{ID: 22, RequestID: "trace-123", SenderType: enums.IMSenderTypeCustomer, Content: "hello"},
|
||||
Conversation: models.Conversation{ID: 11},
|
||||
AIAgent: models.AIAgent{ID: 33, AIConfigID: 44},
|
||||
Question: "hello",
|
||||
})
|
||||
|
||||
var item models.AgentRunLog
|
||||
if err := db.First(&item).Error; err != nil {
|
||||
t.Fatalf("find run log: %v", err)
|
||||
}
|
||||
if item.RequestID != "trace-123" {
|
||||
t.Fatalf("RequestID=%q want %q", item.RequestID, "trace-123")
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ func newAIReplyService() *aiReplyService {
|
||||
executor: newRuntimeReplyExecutor(),
|
||||
interrupts: newReplyInterruptService(),
|
||||
commit: newReplyCommitService(),
|
||||
runlog: newReplyRunLogService(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +27,6 @@ type aiReplyService struct {
|
||||
executor *runtimeReplyExecutor
|
||||
interrupts *replyInterruptService
|
||||
commit *replyCommitService
|
||||
runlog *replyRunLogService
|
||||
}
|
||||
|
||||
func firstInvokedToolCode(summary *applicationruntime.Summary) string {
|
||||
|
||||
@@ -4,11 +4,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
applicationruntime "agent-desk/internal/ai/application/runtime"
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/enums"
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
|
||||
applicationruntime "agent-desk/internal/ai/application/runtime"
|
||||
)
|
||||
|
||||
func TestReplyEligibilityCanReply(t *testing.T) {
|
||||
@@ -70,59 +68,6 @@ func TestResolveReplyTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRunLogPlan(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
PlannedSkillID: 44,
|
||||
PlanReason: "manual",
|
||||
}
|
||||
action, toolCode, reason := buildRunLogPlan(summary)
|
||||
if action != "skill" || toolCode != "" || reason != "manual" {
|
||||
t.Fatalf("unexpected skill plan result: action=%q toolCode=%q reason=%q", action, toolCode, reason)
|
||||
}
|
||||
|
||||
summary = &applicationruntime.Summary{
|
||||
Interrupted: true,
|
||||
TraceData: `{
|
||||
"graphTools": {
|
||||
"items": [
|
||||
{
|
||||
"toolCode": "` + toolx.GraphTriageServiceRequest.Code + `",
|
||||
"recommendedAction": "create_ticket",
|
||||
"ticketDraftReady": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
}
|
||||
action, toolCode, reason = buildRunLogPlan(summary)
|
||||
if action != "graph" || toolCode != toolx.GraphTriageServiceRequest.Code || reason == "" {
|
||||
t.Fatalf("unexpected graph interrupt result: action=%q toolCode=%q reason=%q", action, toolCode, reason)
|
||||
}
|
||||
|
||||
summary = &applicationruntime.Summary{
|
||||
InvokedToolCodes: []string{toolx.BuiltinToolSearch.Code},
|
||||
TraceData: `{
|
||||
"toolSearch": {
|
||||
"items": [
|
||||
{
|
||||
"targetToolCode": "mcp/test/search"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`,
|
||||
}
|
||||
action, toolCode, reason = buildRunLogPlan(summary)
|
||||
if action != "tool" || toolCode != "mcp/test/search" || reason != "agent invoked dynamic tool via tool_search" {
|
||||
t.Fatalf("unexpected dynamic tool result: action=%q toolCode=%q reason=%q", action, toolCode, reason)
|
||||
}
|
||||
|
||||
summary = &applicationruntime.Summary{ReplyText: "done"}
|
||||
action, toolCode, reason = buildRunLogPlan(summary)
|
||||
if action != "reply" || toolCode != "" || reason != "agent replied directly" {
|
||||
t.Fatalf("unexpected reply result: action=%q toolCode=%q reason=%q", action, toolCode, reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveInterruptPrompt(t *testing.T) {
|
||||
summary := &applicationruntime.Summary{
|
||||
Interrupts: []applicationruntime.InterruptContextSummary{
|
||||
|
||||
@@ -45,7 +45,6 @@ func (s *aiReplyService) TriggerReplyAsync(conversation models.Conversation, mes
|
||||
}
|
||||
|
||||
func (s *aiReplyService) TriggerReply(ctx context.Context, conversation models.Conversation, message models.Message, aiAgent models.AIAgent) (retErr error) {
|
||||
startedAt := time.Now()
|
||||
trace := &aiReplyTraceData{Status: "started"}
|
||||
var summary *applicationruntime.Summary
|
||||
replyCtx := aiReplyContext{
|
||||
@@ -61,18 +60,6 @@ func (s *aiReplyService) TriggerReply(ctx context.Context, conversation models.C
|
||||
if s.eligibility != nil && !s.eligibility.CanReply(conversation, message, aiAgent) {
|
||||
return nil
|
||||
}
|
||||
defer func() {
|
||||
s.runlog.Write(replyRunLogInput{
|
||||
StartedAt: startedAt,
|
||||
Message: message,
|
||||
Conversation: conversation,
|
||||
AIAgent: aiAgent,
|
||||
Question: message.Content,
|
||||
RunErr: retErr,
|
||||
Trace: trace,
|
||||
Summary: summary,
|
||||
})
|
||||
}()
|
||||
if pendingInterrupt := svc.ConversationInterruptService.FindLatestPendingByConversationID(conversation.ID); pendingInterrupt != nil {
|
||||
replyCtx.PendingInterrupt = pendingInterrupt
|
||||
return s.resumePendingInterrupt(ctx, replyCtx)
|
||||
|
||||
@@ -95,7 +95,7 @@ func (e *runtimeReplyExecutor) fillTraceFromSummary(trace *aiReplyTraceData, sum
|
||||
return
|
||||
}
|
||||
trace.Status = "runtime_prepared"
|
||||
trace.FinalAction = toRunLogFinalAction(summary)
|
||||
trace.FinalAction = runtimeTraceFinalAction(summary)
|
||||
if summary != nil && strings.TrimSpace(summary.TraceData) != "" {
|
||||
trace.Runtime = json.RawMessage(summary.TraceData)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
applicationruntime "agent-desk/internal/ai/application/runtime"
|
||||
)
|
||||
|
||||
func runtimeTraceFinalAction(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
switch strings.TrimSpace(summary.Status) {
|
||||
case "completed":
|
||||
if strings.TrimSpace(summary.ReplyText) != "" {
|
||||
return "reply"
|
||||
}
|
||||
return "completed"
|
||||
case "fallback":
|
||||
return "fallback"
|
||||
case "error":
|
||||
return "error"
|
||||
case "interrupted":
|
||||
return "interrupted"
|
||||
case "expired":
|
||||
return "expired"
|
||||
default:
|
||||
return strings.TrimSpace(summary.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func extractToolSearchTrace(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
if len(trace.ToolSearch.Items) == 0 {
|
||||
return ""
|
||||
}
|
||||
buf, err := json.Marshal(trace.ToolSearch)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func extractGraphToolTrace(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
if len(trace.GraphTools.Items) == 0 {
|
||||
return ""
|
||||
}
|
||||
buf, err := json.Marshal(trace.GraphTools)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
func firstGraphToolCode(summary *applicationruntime.Summary) string {
|
||||
if summary == nil {
|
||||
return ""
|
||||
}
|
||||
trace := parseRuntimeTraceData(summary.TraceData)
|
||||
for _, item := range trace.GraphTools.Items {
|
||||
toolCode := strings.TrimSpace(item.ToolCode)
|
||||
if toolCode != "" {
|
||||
return toolCode
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type runtimeTraceProjection struct {
|
||||
ToolSearch struct {
|
||||
Items []struct {
|
||||
TargetToolCode string `json:"targetToolCode"`
|
||||
CandidateToolCodes []string `json:"candidateToolCodes"`
|
||||
} `json:"items"`
|
||||
} `json:"toolSearch"`
|
||||
GraphTools struct {
|
||||
Items []struct {
|
||||
ToolCode string `json:"toolCode"`
|
||||
Arguments json.RawMessage `json:"arguments"`
|
||||
RecommendedAction string `json:"recommendedAction"`
|
||||
RiskLevel string `json:"riskLevel"`
|
||||
TicketDraftReady bool `json:"ticketDraftReady"`
|
||||
} `json:"items"`
|
||||
} `json:"graphTools"`
|
||||
}
|
||||
|
||||
func parseRuntimeTraceData(raw string) runtimeTraceProjection {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return runtimeTraceProjection{}
|
||||
}
|
||||
var trace runtimeTraceProjection
|
||||
if err := json.Unmarshal([]byte(raw), &trace); err != nil {
|
||||
return runtimeTraceProjection{}
|
||||
}
|
||||
return trace
|
||||
}
|
||||
Reference in New Issue
Block a user