2026-04-13 17:17:13 +08:00
|
|
|
|
package skills
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
2026-06-20 20:42:07 +08:00
|
|
|
|
"strconv"
|
2026-04-13 17:17:13 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-05-31 18:43:48 +08:00
|
|
|
|
"agent-desk/internal/ai"
|
|
|
|
|
|
"agent-desk/internal/models"
|
2026-04-17 18:57:34 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/mlogclub/simple/common/strs"
|
2026-04-13 17:17:13 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-06-20 20:42:07 +08:00
|
|
|
|
const routeSkillSystemPrompt = `你是客服技能路由器。你只能在候选 Skill 中选择一个最合适的 skillId,或者返回 NONE。
|
2026-04-17 18:57:34 +08:00
|
|
|
|
只有当用户问题与 Skill 的职责边界明确匹配时才选择;
|
|
|
|
|
|
如果不明确、信息不足、多个 Skill 都不够确定,就返回 NONE。
|
2026-06-20 20:42:07 +08:00
|
|
|
|
输出只能是 skillId 或 NONE,不能输出其他内容。`
|
2026-04-17 18:57:34 +08:00
|
|
|
|
|
|
|
|
|
|
func routeSkillWithLLM(ctx context.Context, runtimeCtx RuntimeContext, candidates []models.SkillDefinition) (*models.SkillDefinition, *RouteTrace, error) {
|
2026-04-13 17:17:13 +08:00
|
|
|
|
trace := &RouteTrace{Status: "started"}
|
|
|
|
|
|
if len(candidates) == 0 {
|
|
|
|
|
|
trace.Status = "no_candidate"
|
|
|
|
|
|
return nil, trace, nil
|
|
|
|
|
|
}
|
2026-04-17 18:57:34 +08:00
|
|
|
|
if strs.IsBlank(runtimeCtx.UserMessage) {
|
2026-04-13 17:17:13 +08:00
|
|
|
|
trace.Status = "empty_user_message"
|
|
|
|
|
|
return nil, trace, nil
|
|
|
|
|
|
}
|
2026-04-17 18:57:34 +08:00
|
|
|
|
userPrompt := buildSkillRoutePrompt(runtimeCtx.UserMessage, candidates)
|
2026-04-13 17:17:13 +08:00
|
|
|
|
startedAt := time.Now()
|
2026-04-17 18:57:34 +08:00
|
|
|
|
result, err := ai.LLM.ChatWithConfig(ctx, runtimeCtx.AIConfig, routeSkillSystemPrompt, userPrompt)
|
2026-04-13 17:17:13 +08:00
|
|
|
|
trace.LatencyMs = time.Since(startedAt).Milliseconds()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
trace.Status = "route_error"
|
|
|
|
|
|
trace.Error = err.Error()
|
|
|
|
|
|
return nil, trace, err
|
|
|
|
|
|
}
|
|
|
|
|
|
decision := normalizeRouteDecision(result.Content)
|
|
|
|
|
|
trace.RawDecision = strings.TrimSpace(result.Content)
|
|
|
|
|
|
if decision == "" || decision == "NONE" {
|
|
|
|
|
|
trace.Status = "not_matched"
|
|
|
|
|
|
return nil, trace, nil
|
|
|
|
|
|
}
|
2026-06-20 20:42:07 +08:00
|
|
|
|
selectedID, parseErr := strconv.ParseInt(decision, 10, 64)
|
|
|
|
|
|
if parseErr != nil || selectedID <= 0 {
|
|
|
|
|
|
trace.Status = "invalid_decision"
|
|
|
|
|
|
trace.Error = fmt.Sprintf("invalid route decision: %s", decision)
|
|
|
|
|
|
return nil, trace, nil
|
|
|
|
|
|
}
|
2026-04-13 17:17:13 +08:00
|
|
|
|
for _, item := range candidates {
|
2026-06-20 20:42:07 +08:00
|
|
|
|
if item.ID == selectedID {
|
2026-04-13 17:17:13 +08:00
|
|
|
|
trace.Status = "llm_selected"
|
2026-06-20 20:42:07 +08:00
|
|
|
|
trace.SelectedSkillID = item.ID
|
2026-04-13 17:17:13 +08:00
|
|
|
|
return &item, trace, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
trace.Status = "invalid_decision"
|
|
|
|
|
|
trace.Error = fmt.Sprintf("invalid route decision: %s", decision)
|
|
|
|
|
|
return nil, trace, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func buildSkillRoutePrompt(userMessage string, candidates []models.SkillDefinition) string {
|
|
|
|
|
|
lines := make([]string, 0, len(candidates)+4)
|
|
|
|
|
|
lines = append(lines, "用户问题:")
|
|
|
|
|
|
lines = append(lines, strings.TrimSpace(userMessage))
|
|
|
|
|
|
lines = append(lines, "")
|
|
|
|
|
|
lines = append(lines, "候选 Skills:")
|
|
|
|
|
|
for _, item := range candidates {
|
2026-06-20 20:42:07 +08:00
|
|
|
|
line := fmt.Sprintf("- skillId=%d; name=%s; description=%s", item.ID, strings.TrimSpace(item.Name), strings.TrimSpace(item.Description))
|
2026-04-13 17:17:13 +08:00
|
|
|
|
if examples := parseSkillExamples(item.Examples); len(examples) > 0 {
|
|
|
|
|
|
line += "; examples=" + strings.Join(examples, " | ")
|
|
|
|
|
|
}
|
|
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
|
}
|
|
|
|
|
|
lines = append(lines, "")
|
2026-06-20 20:42:07 +08:00
|
|
|
|
lines = append(lines, "请只输出一个 skillId 或 NONE。")
|
2026-04-13 17:17:13 +08:00
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func parseSkillExamples(raw string) []string {
|
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
|
if raw == "" {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var items []string
|
|
|
|
|
|
if err := json.Unmarshal([]byte(raw), &items); err != nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
ret := make([]string, 0, len(items))
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
item = strings.TrimSpace(item)
|
|
|
|
|
|
if item == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
ret = append(ret, item)
|
|
|
|
|
|
if len(ret) >= 3 {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func normalizeRouteDecision(raw string) string {
|
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
|
if raw == "" {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
if idx := strings.Index(raw, "\n"); idx >= 0 {
|
|
|
|
|
|
raw = raw[:idx]
|
|
|
|
|
|
}
|
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
|
raw = strings.Trim(raw, "`")
|
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
|
raw = strings.Trim(raw, "\"'")
|
|
|
|
|
|
if strings.EqualFold(raw, "NONE") {
|
|
|
|
|
|
return "NONE"
|
|
|
|
|
|
}
|
|
|
|
|
|
return raw
|
|
|
|
|
|
}
|