From 1f0406ec910276b6beac82c31a1fb902bf733727 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Mon, 13 Apr 2026 19:45:09 +0800 Subject: [PATCH] feat: implement context loader for AI agent and refactor plan service to utilize it --- internal/ai/skills/context_loader.go | 30 ++++++++++++++++++++++++++++ internal/ai/skills/plan_service.go | 27 +++++++++---------------- 2 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 internal/ai/skills/context_loader.go diff --git a/internal/ai/skills/context_loader.go b/internal/ai/skills/context_loader.go new file mode 100644 index 0000000..accd6b4 --- /dev/null +++ b/internal/ai/skills/context_loader.go @@ -0,0 +1,30 @@ +package skills + +import ( + "cs-agent/internal/models" + "cs-agent/internal/pkg/errorsx" + "cs-agent/internal/repositories" + + "github.com/mlogclub/simple/sqls" +) + +func newContextLoader() *contextLoader { + return &contextLoader{} +} + +type contextLoader struct{} + +func (l *contextLoader) loadAIAgentWithConfig(aiAgentID int64) (*models.AIAgent, *models.AIConfig, error) { + if aiAgentID <= 0 { + return nil, nil, errorsx.InvalidParam("AIAgentID不能为空") + } + aiAgent := repositories.AIAgentRepository.Get(sqls.DB(), aiAgentID) + if aiAgent == nil { + return nil, nil, errorsx.InvalidParam("AI Agent不存在") + } + aiConfig := repositories.AIConfigRepository.Get(sqls.DB(), aiAgent.AIConfigID) + if aiConfig == nil { + return nil, nil, errorsx.InvalidParam("AI Agent关联的AI配置不存在") + } + return aiAgent, aiConfig, nil +} diff --git a/internal/ai/skills/plan_service.go b/internal/ai/skills/plan_service.go index 6c5a83d..9548a88 100644 --- a/internal/ai/skills/plan_service.go +++ b/internal/ai/skills/plan_service.go @@ -3,33 +3,24 @@ package skills import ( "context" "strings" - - "cs-agent/internal/pkg/errorsx" - "cs-agent/internal/repositories" - - "github.com/mlogclub/simple/sqls" ) func newPlanService() *planService { - return &planService{} + return &planService{ + loader: newContextLoader(), + } } -type planService struct{} +type planService struct { + loader *contextLoader +} // BuildExecutionPlan 构建当前请求的 Skill 执行计划。 func (s *planService) BuildExecutionPlan(execCtx context.Context, ctx RuntimeContext) (*ExecutionPlan, error) { - if ctx.AIAgentID <= 0 { - return nil, errorsx.InvalidParam("AIAgentID不能为空") - } - - aiAgent := repositories.AIAgentRepository.Get(sqls.DB(), ctx.AIAgentID) - if aiAgent == nil { - return nil, errorsx.InvalidParam("AI Agent不存在") - } - aiConfig := repositories.AIConfigRepository.Get(sqls.DB(), aiAgent.AIConfigID) - if aiConfig == nil { - return nil, errorsx.InvalidParam("AI Agent关联的AI配置不存在") + if s.loader == nil { + s.loader = newContextLoader() } + aiAgent, aiConfig, err := s.loader.loadAIAgentWithConfig(ctx.AIAgentID) skill, matchReason, routeTrace, err := MatchSkill(execCtx, ctx, aiAgent, aiConfig) if err != nil {