2026-04-13 20:37:22 +08:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2026-04-14 12:13:33 +08:00
|
|
|
"cs-agent/internal/ai/runtime/executor"
|
2026-04-18 22:24:30 +08:00
|
|
|
"cs-agent/internal/pkg/utils"
|
2026-04-13 20:37:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
2026-04-17 17:31:28 +08:00
|
|
|
runtime *executor.Service
|
2026-04-13 20:43:37 +08:00
|
|
|
catalog *toolCatalog
|
|
|
|
|
prepare *prepareService
|
2026-04-13 20:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewService() *Service {
|
2026-04-13 20:43:37 +08:00
|
|
|
catalog := newToolCatalog()
|
2026-04-13 20:37:22 +08:00
|
|
|
return &Service{
|
2026-04-17 17:31:28 +08:00
|
|
|
runtime: executor.NewService(),
|
2026-04-13 20:43:37 +08:00
|
|
|
catalog: catalog,
|
|
|
|
|
prepare: newPrepareService(catalog),
|
2026-04-13 20:37:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Run(ctx context.Context, req Request) (*Summary, error) {
|
2026-04-13 20:43:37 +08:00
|
|
|
if s == nil || s.runtime == nil || s.prepare == nil {
|
2026-04-13 20:37:22 +08:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2026-04-18 22:24:30 +08:00
|
|
|
req.UserMessage.Content = utils.BuildRuntimeMessageText(req.UserMessage.MessageType, req.UserMessage.Content)
|
2026-04-13 20:43:37 +08:00
|
|
|
selectedSkill, skillReason, skillTrace, skillErr := s.prepare.selectSkill(ctx, req)
|
|
|
|
|
req.SelectedSkill = selectedSkill
|
|
|
|
|
req.SkillRouteReason = skillReason
|
|
|
|
|
req.SkillRouteTrace = skillTrace
|
|
|
|
|
if req.SelectedSkill != nil {
|
|
|
|
|
req.SelectedSkill = cloneSkillDefinition(req.SelectedSkill)
|
|
|
|
|
}
|
|
|
|
|
if err := s.prepare.prepareToolsForRun(&req); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-14 12:13:33 +08:00
|
|
|
summary, err := s.runtime.ExecuteRun(ctx, executor.RunInput{
|
2026-04-13 20:43:37 +08:00
|
|
|
Conversation: req.Conversation,
|
|
|
|
|
UserMessage: req.UserMessage,
|
|
|
|
|
AIAgent: req.AIAgent,
|
|
|
|
|
AIConfig: req.AIConfig,
|
|
|
|
|
SelectedSkill: req.SelectedSkill,
|
|
|
|
|
SkillRouteReason: req.SkillRouteReason,
|
|
|
|
|
SkillRouteTrace: req.SkillRouteTrace,
|
|
|
|
|
CheckPointID: req.CheckPointID,
|
|
|
|
|
ToolSet: req.ToolSet,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret := toSummary(summary)
|
|
|
|
|
if ret != nil && skillErr != nil && ret.PlanReason == "" {
|
|
|
|
|
ret.PlanReason = "skill_failed_fallback_runtime"
|
|
|
|
|
}
|
|
|
|
|
return ret, err
|
|
|
|
|
}
|
|
|
|
|
ret := toSummary(summary)
|
|
|
|
|
if ret != nil && skillErr != nil && ret.PlanReason == "" {
|
|
|
|
|
ret.PlanReason = "skill_failed_fallback_runtime"
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
2026-04-13 20:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Resume(ctx context.Context, req ResumeRequest) (*Summary, error) {
|
2026-04-13 20:43:37 +08:00
|
|
|
if s == nil || s.runtime == nil || s.prepare == nil {
|
2026-04-13 20:37:22 +08:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2026-04-13 20:43:37 +08:00
|
|
|
if err := s.prepare.prepareToolsForResume(&req); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-14 12:13:33 +08:00
|
|
|
summary, err := s.runtime.ExecuteResume(ctx, executor.ResumeInput{
|
2026-04-13 20:43:37 +08:00
|
|
|
Conversation: req.Conversation,
|
|
|
|
|
AIAgent: req.AIAgent,
|
|
|
|
|
AIConfig: req.AIConfig,
|
|
|
|
|
CheckPointID: req.CheckPointID,
|
|
|
|
|
ResumeData: req.ResumeData,
|
|
|
|
|
ToolSet: req.ToolSet,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return toSummary(summary), err
|
|
|
|
|
}
|
|
|
|
|
return toSummary(summary), nil
|
2026-04-13 20:37:22 +08:00
|
|
|
}
|