129 lines
4.4 KiB
Go
129 lines
4.4 KiB
Go
package multiagent
|
||
|
||
import (
|
||
"context"
|
||
"cyberstrike-ai/internal/config"
|
||
"errors"
|
||
"fmt"
|
||
"math/rand/v2"
|
||
"regexp"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
const defaultRunRetryMaxBackoff = 30 * time.Second
|
||
const modelOutputRecoveryKey = "_cyberstrike_model_output_recovery"
|
||
|
||
var modelRouteTagPattern = regexp.MustCompile(`(?i)\[endpoint=([^,\]]+),\s*route=([^\]]+)\]`)
|
||
|
||
func formatModelEndpointHint(raw string) string {
|
||
m := modelRouteTagPattern.FindStringSubmatch(raw)
|
||
if len(m) != 3 {
|
||
return ""
|
||
}
|
||
endpoint := strings.TrimSpace(m[1])
|
||
route := strings.TrimSpace(m[2])
|
||
if endpoint == "" {
|
||
return ""
|
||
}
|
||
lowerRoute := strings.ToLower(route)
|
||
switch {
|
||
case lowerRoute == "direct":
|
||
return fmt.Sprintf("(%s,当前直连未走代理)", endpoint)
|
||
case strings.HasPrefix(lowerRoute, "proxy "):
|
||
return fmt.Sprintf("(%s,经代理 %s)", endpoint, strings.TrimSpace(route[len("proxy "):]))
|
||
default:
|
||
return fmt.Sprintf("(%s)", endpoint)
|
||
}
|
||
}
|
||
|
||
func transientRetryBackoff(attempt int, maxBackoff time.Duration) time.Duration {
|
||
if attempt < 0 {
|
||
attempt = 0
|
||
}
|
||
if attempt > 30 {
|
||
attempt = 30
|
||
}
|
||
ceiling := time.Duration(1<<uint(attempt+1)) * time.Second
|
||
if maxBackoff > 0 && ceiling > maxBackoff {
|
||
ceiling = maxBackoff
|
||
}
|
||
if ceiling <= 1 {
|
||
return ceiling
|
||
}
|
||
half := ceiling / 2
|
||
return half + time.Duration(rand.Int64N(int64(ceiling-half)+1))
|
||
}
|
||
|
||
func ClientRunErrorMessage(err error) string {
|
||
if err == nil {
|
||
return ""
|
||
}
|
||
if _, detail := piModelTransportFailure(err.Error()); detail != "" {
|
||
return detail
|
||
}
|
||
return err.Error()
|
||
}
|
||
|
||
func piModelTransportFailure(raw string) (code, detail string) {
|
||
text := strings.ToLower(raw)
|
||
hint := formatModelEndpointHint(raw)
|
||
switch {
|
||
case strings.Contains(text, "und_err_headers_timeout"):
|
||
return "model_headers_timeout", "等待模型服务" + hint + "响应头超时"
|
||
case strings.Contains(text, "und_err_body_timeout"):
|
||
return "model_stream_timeout", "模型" + hint + "响应流长时间未收到新数据"
|
||
case strings.Contains(text, "stream ended without finish_reason"):
|
||
return "model_stream_incomplete", "模型" + hint + "响应流提前关闭,未收到结束标记"
|
||
case strings.Contains(text, "before secure tls connection was established") || strings.Contains(text, "tls handshake"):
|
||
return "model_tls_reset", "与模型服务" + hint + "的 TLS 握手中断,连接被重置"
|
||
case strings.Contains(text, "und_err_connect_timeout") || strings.Contains(text, "connecttimeouterror") || strings.Contains(text, "etimedout"):
|
||
return "model_connect_timeout", "连接模型服务" + hint + "超时"
|
||
case strings.Contains(text, "econnrefused") || strings.Contains(text, "connection refused"):
|
||
return "model_connection_refused", "模型服务" + hint + "拒绝连接"
|
||
case strings.Contains(text, "eai_again") || strings.Contains(text, "getaddrinfo"):
|
||
return "model_dns_failure", "模型服务" + hint + "域名解析暂时失败"
|
||
case strings.Contains(text, "econnreset") || strings.Contains(text, "econnaborted") ||
|
||
strings.Contains(text, "connection reset") || strings.Contains(text, "socket disconnected") ||
|
||
strings.Contains(text, "socket hang up") || strings.Contains(text, "other side closed") ||
|
||
strings.Contains(text, "und_err_socket") || strings.Contains(text, "connection error"):
|
||
return "model_connection_reset", "与模型服务" + hint + "的网络连接中断"
|
||
default:
|
||
return "", ""
|
||
}
|
||
}
|
||
|
||
func ClientRunErrorFields(err error) map[string]interface{} {
|
||
fields := map[string]interface{}{}
|
||
if err == nil {
|
||
return fields
|
||
}
|
||
kind := "runtime_error"
|
||
text := strings.ToLower(err.Error())
|
||
modelReason, _ := piModelTransportFailure(text)
|
||
switch {
|
||
case errors.Is(err, context.Canceled):
|
||
kind = "cancelled"
|
||
case errors.Is(err, context.DeadlineExceeded):
|
||
kind = "timeout"
|
||
case strings.Contains(text, "context") && strings.Contains(text, "exceed"):
|
||
kind = "context_overflow"
|
||
case strings.Contains(text, "401") || strings.Contains(text, "403"):
|
||
kind = "authentication"
|
||
case modelReason != "":
|
||
kind = "model_transient"
|
||
fields["reasonCode"] = modelReason
|
||
case strings.Contains(text, "429") || strings.Contains(text, "503") || strings.Contains(text, "timeout") || strings.Contains(text, "connection"):
|
||
kind = "model_transient"
|
||
}
|
||
fields["errorKind"] = kind
|
||
fields["technicalError"] = err.Error()
|
||
return fields
|
||
}
|
||
func agentToolTimeoutMinutes(cfg *config.Config) int {
|
||
if cfg == nil {
|
||
return 0
|
||
}
|
||
return cfg.Agent.ToolTimeoutMinutes
|
||
}
|