Implement skill debugging functionality and refactor skill execution flow
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cs-agent/internal/ai"
|
||||
"cs-agent/internal/pkg/errorsx"
|
||||
)
|
||||
|
||||
func executeByPlan(ctx context.Context, plan *ExecutionPlan, runtimeCtx RuntimeContext) (string, *ExecutionTrace, error) {
|
||||
if plan == nil || plan.Skill == nil {
|
||||
return "", nil, nil
|
||||
}
|
||||
trace := &ExecutionTrace{
|
||||
Status: "started",
|
||||
ExecutionMode: "content",
|
||||
}
|
||||
replyText, err := executeContent(ctx, plan, runtimeCtx, trace)
|
||||
return replyText, trace, err
|
||||
}
|
||||
|
||||
func executeContent(ctx context.Context, plan *ExecutionPlan, runtimeCtx RuntimeContext, trace *ExecutionTrace) (string, error) {
|
||||
if plan == nil || plan.Skill == nil {
|
||||
return "", nil
|
||||
}
|
||||
if plan.AIConfig == nil {
|
||||
return "", errorsx.InvalidParam("Skill 关联的 AI 配置不可用")
|
||||
}
|
||||
systemPrompt := strings.TrimSpace(plan.Skill.Content)
|
||||
if systemPrompt == "" {
|
||||
return "", errorsx.InvalidParam("Skill Content 不能为空")
|
||||
}
|
||||
userPrompt := strings.TrimSpace(runtimeCtx.UserMessage)
|
||||
if userPrompt == "" {
|
||||
return "", errorsx.InvalidParam("用户消息不能为空")
|
||||
}
|
||||
promptTrace := &PromptTrace{Status: "started"}
|
||||
if trace != nil {
|
||||
trace.Prompt = promptTrace
|
||||
}
|
||||
startedAt := time.Now()
|
||||
result, err := ai.LLM.ChatWithConfig(ctx, plan.AIConfig, systemPrompt, userPrompt)
|
||||
promptTrace.LatencyMs = time.Since(startedAt).Milliseconds()
|
||||
if err != nil {
|
||||
promptTrace.Status = "error"
|
||||
promptTrace.Error = err.Error()
|
||||
if trace != nil {
|
||||
trace.Status = "error"
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
promptTrace.Status = "ok"
|
||||
promptTrace.ModelName = result.ModelName
|
||||
promptTrace.PromptTokens = result.PromptTokens
|
||||
promptTrace.CompletionTokens = result.CompletionTokens
|
||||
if trace != nil {
|
||||
trace.Status = "ok"
|
||||
}
|
||||
return strings.TrimSpace(result.Content), nil
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func BuildExecutionPlan(execCtx context.Context, ctx RuntimeContext) (*Execution
|
||||
}, nil
|
||||
}
|
||||
|
||||
// WriteRunLog 写入 Skill 运行日志。
|
||||
// WriteRunLog 写入 Skill 路由日志。
|
||||
func WriteRunLog(log *models.SkillRunLog) error {
|
||||
if log == nil {
|
||||
return nil
|
||||
@@ -48,37 +48,33 @@ func WriteRunLog(log *models.SkillRunLog) error {
|
||||
return repositories.SkillRunLogRepository.Create(sqls.DB(), log)
|
||||
}
|
||||
|
||||
// Execute 执行一次 Skill 运行,当前阶段仅支持 prompt_only 风格的手动 Skill。
|
||||
func Execute(ctx context.Context, runtimeCtx RuntimeContext) (*ExecutionResult, error) {
|
||||
// Select 执行一次 Skill 路由并记录路由日志。
|
||||
func Select(ctx context.Context, runtimeCtx RuntimeContext) (*ExecutionResult, error) {
|
||||
plan, err := BuildExecutionPlan(ctx, runtimeCtx)
|
||||
if err != nil {
|
||||
trace := &ExecutionTrace{Status: "plan_error"}
|
||||
trace := &ExecutionTrace{Status: "route_error"}
|
||||
log := BuildRunLog(runtimeCtx, nil, trace, err)
|
||||
_ = WriteRunLog(log)
|
||||
return nil, err
|
||||
}
|
||||
trace := &ExecutionTrace{Status: "ok"}
|
||||
if plan == nil || plan.Skill == nil {
|
||||
trace := &ExecutionTrace{Status: "noop"}
|
||||
if plan != nil {
|
||||
trace.Status = "not_matched"
|
||||
trace.MatchReason = strings.TrimSpace(plan.MatchReason)
|
||||
trace.Route = plan.RouteTrace
|
||||
}
|
||||
log := BuildRunLog(runtimeCtx, plan, trace, nil)
|
||||
_ = WriteRunLog(log)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
replyText, trace, err := executeByPlan(ctx, plan, runtimeCtx)
|
||||
if trace != nil {
|
||||
trace.MatchReason = strings.TrimSpace(plan.MatchReason)
|
||||
if trace.Route == nil {
|
||||
trace.Route = plan.RouteTrace
|
||||
}
|
||||
return &ExecutionResult{
|
||||
Plan: plan,
|
||||
RunLog: log,
|
||||
Trace: trace,
|
||||
}, nil
|
||||
}
|
||||
trace.MatchReason = strings.TrimSpace(plan.MatchReason)
|
||||
trace.Route = plan.RouteTrace
|
||||
log := BuildRunLog(runtimeCtx, plan, trace, err)
|
||||
if strings.TrimSpace(replyText) != "" && strings.TrimSpace(log.MatchReason) == "" {
|
||||
log.MatchReason = "content"
|
||||
}
|
||||
if writeErr := WriteRunLog(log); writeErr != nil && err == nil {
|
||||
err = writeErr
|
||||
}
|
||||
@@ -86,9 +82,8 @@ func Execute(ctx context.Context, runtimeCtx RuntimeContext) (*ExecutionResult,
|
||||
return nil, err
|
||||
}
|
||||
return &ExecutionResult{
|
||||
Plan: plan,
|
||||
ReplyText: strings.TrimSpace(replyText),
|
||||
RunLog: log,
|
||||
Trace: trace,
|
||||
Plan: plan,
|
||||
RunLog: log,
|
||||
Trace: trace,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ type RuntimeContext struct {
|
||||
IntentCode string // IntentCode 为上游识别出的意图编码。
|
||||
}
|
||||
|
||||
// ExecutionPlan 表示 Skill Runtime 计算出的最终执行计划。
|
||||
// ExecutionPlan 表示 Skill Runtime 计算出的最终路由结果。
|
||||
type ExecutionPlan struct {
|
||||
AIAgent *models.AIAgent // AIAgent 为本次请求所属的 AI Agent。
|
||||
AIConfig *models.AIConfig // AIConfig 为本次请求实际使用的模型配置。
|
||||
@@ -20,21 +20,17 @@ type ExecutionPlan struct {
|
||||
RouteTrace *RouteTrace // RouteTrace 为匹配阶段的路由追踪。
|
||||
}
|
||||
|
||||
// ExecutionResult 表示一次 Skill 执行的最终结果。
|
||||
// ExecutionResult 表示一次 Skill 路由的最终结果。
|
||||
type ExecutionResult struct {
|
||||
Plan *ExecutionPlan
|
||||
ReplyText string
|
||||
RunLog *models.SkillRunLog
|
||||
Trace *ExecutionTrace
|
||||
Plan *ExecutionPlan
|
||||
RunLog *models.SkillRunLog
|
||||
Trace *ExecutionTrace
|
||||
}
|
||||
|
||||
type ExecutionTrace struct {
|
||||
Status string `json:"status"`
|
||||
MatchReason string `json:"matchReason,omitempty"`
|
||||
Route *RouteTrace `json:"route,omitempty"`
|
||||
ExecutionMode string `json:"executionMode,omitempty"`
|
||||
Prompt *PromptTrace `json:"prompt,omitempty"`
|
||||
MCP *MCPExecutionTrace `json:"mcp,omitempty"`
|
||||
Status string `json:"status"`
|
||||
MatchReason string `json:"matchReason,omitempty"`
|
||||
Route *RouteTrace `json:"route,omitempty"`
|
||||
}
|
||||
|
||||
type RouteTrace struct {
|
||||
@@ -54,17 +50,3 @@ type PromptTrace struct {
|
||||
CompletionTokens int `json:"completionTokens,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type MCPExecutionTrace struct {
|
||||
Status string `json:"status"`
|
||||
ServerCode string `json:"serverCode,omitempty"`
|
||||
ToolName string `json:"toolName,omitempty"`
|
||||
Arguments map[string]any `json:"arguments,omitempty"`
|
||||
LatencyMs int64 `json:"latencyMs,omitempty"`
|
||||
IsError bool `json:"isError,omitempty"`
|
||||
ContentItemCount int `json:"contentItemCount,omitempty"`
|
||||
HasStructuredContent bool `json:"hasStructuredContent,omitempty"`
|
||||
ResultPreview string `json:"resultPreview,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
SummaryPrompt *PromptTrace `json:"summaryPrompt,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user