feat: implement context loader for AI agent and refactor plan service to utilize it

This commit is contained in:
mlogclub
2026-04-13 19:45:09 +08:00
parent 6fa2a465ed
commit 1f0406ec91
2 changed files with 39 additions and 18 deletions
+30
View File
@@ -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
}
+9 -18
View File
@@ -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 {