|
|
@@ -0,0 +1,123 @@
|
|
|
+package com.agent.management.config;
|
|
|
+
|
|
|
+import lombok.Data;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 错误模式管理配置。
|
|
|
+ *
|
|
|
+ * <p>对应 {@code application.yml} 的 {@code app.error-pattern} 段:
|
|
|
+ * <pre>
|
|
|
+ * app:
|
|
|
+ * error-pattern:
|
|
|
+ * builtin:
|
|
|
+ * - name: LLM SDK 重试耗尽
|
|
|
+ * pattern: API call failed after
|
|
|
+ * description: OpenAI SDK 重试耗尽仍未成功
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * <p>启动时若数据库为空,{@code builtin} 列表会被写入作为内置默认模式;
|
|
|
+ * 用户自定义后不会被覆盖;可通过"重置为默认"恢复(清空自定义 + 重新启用内置)。</p>
|
|
|
+ *
|
|
|
+ * <p>未在 yml 中配置时,使用代码内的 {@link #DEFAULT_BUILTINS} 兜底,
|
|
|
+ * 覆盖常见 OpenAI 兼容服务(智谱 / DeepSeek / Moonshot / Anthropic 等)的典型错误描述。</p>
|
|
|
+ */
|
|
|
+@Data
|
|
|
+@Component
|
|
|
+@ConfigurationProperties(prefix = "app.error-pattern")
|
|
|
+public class ErrorPatternProperties {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 内置默认模式列表。yml 未配置时使用 {@link #DEFAULT_BUILTINS}。
|
|
|
+ */
|
|
|
+ private List<BuiltinPattern> builtin = new ArrayList<>(DEFAULT_BUILTINS);
|
|
|
+
|
|
|
+ @Data
|
|
|
+ public static class BuiltinPattern {
|
|
|
+ /** 模式名称(唯一) */
|
|
|
+ private String name;
|
|
|
+ /** 匹配子串(大小写不敏感) */
|
|
|
+ private String pattern;
|
|
|
+ /** 说明(可选) */
|
|
|
+ private String description;
|
|
|
+
|
|
|
+ public BuiltinPattern() {}
|
|
|
+
|
|
|
+ public BuiltinPattern(String name, String pattern, String description) {
|
|
|
+ this.name = name;
|
|
|
+ this.pattern = pattern;
|
|
|
+ this.description = description;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 代码内兜底默认列表(当 yml 未配置时使用)。
|
|
|
+ *
|
|
|
+ * <p>覆盖范围:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>SDK 重试 / HTTP 4xx / 5xx 通用错误</li>
|
|
|
+ * <li>OpenAI 标准错误:rate_limit / quota / insufficient_quota / invalid_api_key / context_length / server_error / model_not_found</li>
|
|
|
+ * <li>智谱 / DeepSeek / Moonshot 中文错误描述</li>
|
|
|
+ * <li>Anthropic: overloaded_error</li>
|
|
|
+ * <li>网络层错误:connection refused / timeout / ECONNREFUSED / ETIMEDOUT / socket hang up</li>
|
|
|
+ * <li>工具调用与 JSON 解析错误</li>
|
|
|
+ * <li>通用中文错误:服务暂时不可用 / 请求失败 / 系统繁忙</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+ public static final List<BuiltinPattern> DEFAULT_BUILTINS = List.of(
|
|
|
+ new BuiltinPattern("LLM SDK 重试耗尽", "API call failed after",
|
|
|
+ "OpenAI SDK 重试耗尽仍未成功,通常是限流或下游故障"),
|
|
|
+ new BuiltinPattern("HTTP 4xx 客户端错误", "HTTP 4",
|
|
|
+ "LLM 服务返回 4xx(含 429 限流、401 鉴权、400 参数等)"),
|
|
|
+ new BuiltinPattern("HTTP 5xx 服务端错误", "HTTP 5",
|
|
|
+ "LLM 服务返回 5xx,服务端临时故障"),
|
|
|
+ new BuiltinPattern("Rate Limit(英文)", "rate limit",
|
|
|
+ "OpenAI 兼容服务的标准限流错误"),
|
|
|
+ new BuiltinPattern("OpenAI 配额耗尽", "insufficient_quota",
|
|
|
+ "OpenAI 标准配额耗尽错误码"),
|
|
|
+ new BuiltinPattern("额度 / 配额", "quota",
|
|
|
+ "额度耗尽类错误(中英文 quota 关键词)"),
|
|
|
+ new BuiltinPattern("OpenAI 鉴权失败", "invalid_api_key",
|
|
|
+ "OpenAI 标准鉴权失败错误码"),
|
|
|
+ new BuiltinPattern("认证失败(英文)", "invalid api key",
|
|
|
+ "API Key 无效或被吊销"),
|
|
|
+ new BuiltinPattern("认证失败(中文)", "无效的 api",
|
|
|
+ "中文鉴权失败描述(兼容智谱等中文错误消息)"),
|
|
|
+ new BuiltinPattern("上下文超限(英文)", "context_length_exceeded",
|
|
|
+ "输入超过模型上下文窗口"),
|
|
|
+ new BuiltinPattern("上下文超限(中文)", "上下文长度",
|
|
|
+ "中文上下文超限描述"),
|
|
|
+ new BuiltinPattern("模型不存在", "model_not_found",
|
|
|
+ "指定的模型标识不存在或无权访问"),
|
|
|
+ new BuiltinPattern("OpenAI 服务端错误", "server_error",
|
|
|
+ "OpenAI 标准 5xx 错误码"),
|
|
|
+ new BuiltinPattern("Anthropic 过载", "overloaded_error",
|
|
|
+ "Anthropic 服务过载错误码"),
|
|
|
+ new BuiltinPattern("智谱限流(具体)", "该模型当前访问量过大",
|
|
|
+ "智谱 AI glm 系列特定限流描述"),
|
|
|
+ new BuiltinPattern("通用中文限流", "请稍后重试",
|
|
|
+ "通用中文限流提示(含 QPS/并发限制等场景)"),
|
|
|
+ new BuiltinPattern("通用中文系统繁忙", "系统繁忙",
|
|
|
+ "服务端繁忙类错误(多见于国内 LLM 服务)"),
|
|
|
+ new BuiltinPattern("通用中文请求失败", "请求失败",
|
|
|
+ "通用请求失败描述(兜底捕获)"),
|
|
|
+ new BuiltinPattern("网络连接拒绝", "connection refused",
|
|
|
+ "下游服务不可达(连接被拒绝)"),
|
|
|
+ new BuiltinPattern("网络超时", "connection timeout",
|
|
|
+ "下游服务响应超时"),
|
|
|
+ new BuiltinPattern("ECONNREFUSED", "ECONNREFUSED",
|
|
|
+ "Node.js / Python HTTP 客户端的连接拒绝错误码"),
|
|
|
+ new BuiltinPattern("ETIMEDOUT", "ETIMEDOUT",
|
|
|
+ "HTTP 客户端的连接超时错误码"),
|
|
|
+ new BuiltinPattern("Socket 挂起", "socket hang up",
|
|
|
+ "HTTP 连接被对端中途切断"),
|
|
|
+ new BuiltinPattern("JSON 解析失败", "unexpected token",
|
|
|
+ "LLM 返回非合法 JSON(结构化输出场景)"),
|
|
|
+ new BuiltinPattern("工具调用失败(英文)", "tool call failed",
|
|
|
+ "Agent 工具调用抛出异常")
|
|
|
+ );
|
|
|
+}
|