feat: refactor logging and execution plan handling, introducing RunLogService for improved structure
This commit is contained in:
@@ -1,56 +1,13 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"cs-agent/internal/models"
|
||||
)
|
||||
import "cs-agent/internal/models"
|
||||
|
||||
// BuildRunLog 根据执行计划与运行结果构建 Skill 运行日志。
|
||||
func BuildRunLog(ctx RuntimeContext, plan *ExecutionPlan, trace *ExecutionTrace, err error) *models.SkillRunLog {
|
||||
log := &models.SkillRunLog{
|
||||
ConversationID: ctx.ConversationID,
|
||||
AIAgentID: ctx.AIAgentID,
|
||||
ManualSkillCode: ctx.ManualSkillCode,
|
||||
IntentCode: ctx.IntentCode,
|
||||
UserMessage: ctx.UserMessage,
|
||||
TraceData: buildTraceData(trace),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if plan != nil {
|
||||
if plan.AIConfig != nil {
|
||||
log.AIConfigID = plan.AIConfig.ID
|
||||
log.UsedModel = plan.AIConfig.ModelName
|
||||
log.UsedProvider = plan.AIConfig.Provider
|
||||
}
|
||||
if plan.Skill != nil {
|
||||
log.SkillDefinitionID = plan.Skill.ID
|
||||
log.SkillCode = plan.Skill.Code
|
||||
log.Matched = true
|
||||
log.FinalSelected = true
|
||||
log.MatchReason = plan.MatchReason
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.ErrorMessage = err.Error()
|
||||
} else if !log.Matched {
|
||||
if plan != nil && plan.MatchReason != "" {
|
||||
log.MatchReason = plan.MatchReason
|
||||
} else {
|
||||
log.MatchReason = "not_matched"
|
||||
}
|
||||
}
|
||||
return log
|
||||
return RuntimeService.runlog.Build(ctx, plan, trace, err)
|
||||
}
|
||||
|
||||
func buildTraceData(trace *ExecutionTrace) string {
|
||||
if trace == nil {
|
||||
return ""
|
||||
}
|
||||
data, err := json.Marshal(trace)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
// WriteRunLog 写入 Skill 路由日志。
|
||||
func WriteRunLog(log *models.SkillRunLog) error {
|
||||
return RuntimeService.runlog.Write(log)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/repositories"
|
||||
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
)
|
||||
|
||||
func newRunLogService() *RunLogService {
|
||||
return &RunLogService{}
|
||||
}
|
||||
|
||||
type RunLogService struct{}
|
||||
|
||||
// Build 根据执行计划与运行结果构建 Skill 运行日志。
|
||||
func (s *RunLogService) Build(ctx RuntimeContext, plan *ExecutionPlan, trace *ExecutionTrace, err error) *models.SkillRunLog {
|
||||
log := &models.SkillRunLog{
|
||||
ConversationID: ctx.ConversationID,
|
||||
AIAgentID: ctx.AIAgentID,
|
||||
ManualSkillCode: ctx.ManualSkillCode,
|
||||
IntentCode: ctx.IntentCode,
|
||||
UserMessage: ctx.UserMessage,
|
||||
TraceData: s.buildTraceData(trace),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if plan != nil {
|
||||
if plan.AIConfig != nil {
|
||||
log.AIConfigID = plan.AIConfig.ID
|
||||
log.UsedModel = plan.AIConfig.ModelName
|
||||
log.UsedProvider = plan.AIConfig.Provider
|
||||
}
|
||||
if plan.Skill != nil {
|
||||
log.SkillDefinitionID = plan.Skill.ID
|
||||
log.SkillCode = plan.Skill.Code
|
||||
log.Matched = true
|
||||
log.FinalSelected = true
|
||||
log.MatchReason = plan.MatchReason
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.ErrorMessage = err.Error()
|
||||
} else if !log.Matched {
|
||||
if plan != nil && plan.MatchReason != "" {
|
||||
log.MatchReason = plan.MatchReason
|
||||
} else {
|
||||
log.MatchReason = "not_matched"
|
||||
}
|
||||
}
|
||||
return log
|
||||
}
|
||||
|
||||
// Write 写入 Skill 路由日志。
|
||||
func (s *RunLogService) Write(log *models.SkillRunLog) error {
|
||||
if log == nil {
|
||||
return nil
|
||||
}
|
||||
return repositories.SkillRunLogRepository.Create(sqls.DB(), log)
|
||||
}
|
||||
|
||||
func (s *RunLogService) buildTraceData(trace *ExecutionTrace) string {
|
||||
if trace == nil {
|
||||
return ""
|
||||
}
|
||||
data, err := json.Marshal(trace)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package skills
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cs-agent/internal/models"
|
||||
)
|
||||
|
||||
// BuildExecutionPlan 构建当前请求的 Skill 执行计划。
|
||||
@@ -11,11 +9,6 @@ func BuildExecutionPlan(execCtx context.Context, ctx RuntimeContext) (*Execution
|
||||
return RuntimeService.BuildExecutionPlan(execCtx, ctx)
|
||||
}
|
||||
|
||||
// WriteRunLog 写入 Skill 路由日志。
|
||||
func WriteRunLog(log *models.SkillRunLog) error {
|
||||
return RuntimeService.WriteRunLog(log)
|
||||
}
|
||||
|
||||
// Select 执行一次 Skill 路由并记录路由日志。
|
||||
func Select(ctx context.Context, runtimeCtx RuntimeContext) (*ExecutionResult, error) {
|
||||
return RuntimeService.Select(ctx, runtimeCtx)
|
||||
|
||||
@@ -14,10 +14,14 @@ import (
|
||||
var RuntimeService = newService()
|
||||
|
||||
func newService() *Service {
|
||||
return &Service{}
|
||||
return &Service{
|
||||
runlog: newRunLogService(),
|
||||
}
|
||||
}
|
||||
|
||||
type Service struct{}
|
||||
type Service struct {
|
||||
runlog *RunLogService
|
||||
}
|
||||
|
||||
// BuildExecutionPlan 构建当前请求的 Skill 执行计划。
|
||||
func (s *Service) BuildExecutionPlan(execCtx context.Context, ctx RuntimeContext) (*ExecutionPlan, error) {
|
||||
@@ -50,10 +54,7 @@ func (s *Service) BuildExecutionPlan(execCtx context.Context, ctx RuntimeContext
|
||||
|
||||
// WriteRunLog 写入 Skill 路由日志。
|
||||
func (s *Service) WriteRunLog(log *models.SkillRunLog) error {
|
||||
if log == nil {
|
||||
return nil
|
||||
}
|
||||
return repositories.SkillRunLogRepository.Create(sqls.DB(), log)
|
||||
return s.runlog.Write(log)
|
||||
}
|
||||
|
||||
// Select 执行一次 Skill 路由并记录路由日志。
|
||||
@@ -61,7 +62,7 @@ func (s *Service) Select(ctx context.Context, runtimeCtx RuntimeContext) (*Execu
|
||||
plan, err := s.BuildExecutionPlan(ctx, runtimeCtx)
|
||||
if err != nil {
|
||||
trace := &ExecutionTrace{Status: "route_error"}
|
||||
log := BuildRunLog(runtimeCtx, nil, trace, err)
|
||||
log := s.runlog.Build(runtimeCtx, nil, trace, err)
|
||||
_ = s.WriteRunLog(log)
|
||||
return nil, err
|
||||
}
|
||||
@@ -72,7 +73,7 @@ func (s *Service) Select(ctx context.Context, runtimeCtx RuntimeContext) (*Execu
|
||||
trace.MatchReason = strings.TrimSpace(plan.MatchReason)
|
||||
trace.Route = plan.RouteTrace
|
||||
}
|
||||
log := BuildRunLog(runtimeCtx, plan, trace, nil)
|
||||
log := s.runlog.Build(runtimeCtx, plan, trace, nil)
|
||||
_ = s.WriteRunLog(log)
|
||||
return &ExecutionResult{
|
||||
Plan: plan,
|
||||
@@ -82,7 +83,7 @@ func (s *Service) Select(ctx context.Context, runtimeCtx RuntimeContext) (*Execu
|
||||
}
|
||||
trace.MatchReason = strings.TrimSpace(plan.MatchReason)
|
||||
trace.Route = plan.RouteTrace
|
||||
log := BuildRunLog(runtimeCtx, plan, trace, err)
|
||||
log := s.runlog.Build(runtimeCtx, plan, trace, err)
|
||||
if writeErr := s.WriteRunLog(log); writeErr != nil && err == nil {
|
||||
err = writeErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user