error_passthrough_rule.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Package schema 定义 Ent ORM 的数据库 schema。
  2. package schema
  3. import (
  4. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  5. "entgo.io/ent"
  6. "entgo.io/ent/dialect"
  7. "entgo.io/ent/dialect/entsql"
  8. "entgo.io/ent/schema"
  9. "entgo.io/ent/schema/field"
  10. "entgo.io/ent/schema/index"
  11. )
  12. // ErrorPassthroughRule 定义全局错误透传规则的 schema。
  13. //
  14. // 错误透传规则用于控制上游错误如何返回给客户端:
  15. // - 匹配条件:错误码 + 关键词组合
  16. // - 响应行为:透传原始信息 或 自定义错误信息
  17. // - 响应状态码:可指定返回给客户端的状态码
  18. // - 平台范围:规则适用的平台(Anthropic、OpenAI、Gemini、Antigravity)
  19. type ErrorPassthroughRule struct {
  20. ent.Schema
  21. }
  22. // Annotations 返回 schema 的注解配置。
  23. func (ErrorPassthroughRule) Annotations() []schema.Annotation {
  24. return []schema.Annotation{
  25. entsql.Annotation{Table: "error_passthrough_rules"},
  26. }
  27. }
  28. // Mixin 返回该 schema 使用的混入组件。
  29. func (ErrorPassthroughRule) Mixin() []ent.Mixin {
  30. return []ent.Mixin{
  31. mixins.TimeMixin{},
  32. }
  33. }
  34. // Fields 定义错误透传规则实体的所有字段。
  35. func (ErrorPassthroughRule) Fields() []ent.Field {
  36. return []ent.Field{
  37. // name: 规则名称,用于在界面中标识规则
  38. field.String("name").
  39. MaxLen(100).
  40. NotEmpty(),
  41. // enabled: 是否启用该规则
  42. field.Bool("enabled").
  43. Default(true),
  44. // priority: 规则优先级,数值越小优先级越高
  45. // 匹配时按优先级顺序检查,命中第一个匹配的规则
  46. field.Int("priority").
  47. Default(0),
  48. // error_codes: 匹配的错误码列表(OR关系)
  49. // 例如:[422, 400] 表示匹配 422 或 400 错误码
  50. field.JSON("error_codes", []int{}).
  51. Optional().
  52. SchemaType(map[string]string{dialect.Postgres: "jsonb"}),
  53. // keywords: 匹配的关键词列表(OR关系)
  54. // 例如:["context limit", "model not supported"]
  55. // 关键词匹配不区分大小写
  56. field.JSON("keywords", []string{}).
  57. Optional().
  58. SchemaType(map[string]string{dialect.Postgres: "jsonb"}),
  59. // match_mode: 匹配模式
  60. // - "any": 错误码匹配 OR 关键词匹配(任一条件满足即可)
  61. // - "all": 错误码匹配 AND 关键词匹配(所有条件都必须满足)
  62. field.String("match_mode").
  63. MaxLen(10).
  64. Default("any"),
  65. // platforms: 适用平台列表
  66. // 例如:["anthropic", "openai", "gemini", "antigravity"]
  67. // 空列表表示适用于所有平台
  68. field.JSON("platforms", []string{}).
  69. Optional().
  70. SchemaType(map[string]string{dialect.Postgres: "jsonb"}),
  71. // passthrough_code: 是否透传上游原始状态码
  72. // true: 使用上游返回的状态码
  73. // false: 使用 response_code 指定的状态码
  74. field.Bool("passthrough_code").
  75. Default(true),
  76. // response_code: 自定义响应状态码
  77. // 当 passthrough_code=false 时使用此状态码
  78. field.Int("response_code").
  79. Optional().
  80. Nillable(),
  81. // passthrough_body: 是否透传上游原始错误信息
  82. // true: 使用上游返回的错误信息
  83. // false: 使用 custom_message 指定的错误信息
  84. field.Bool("passthrough_body").
  85. Default(true),
  86. // custom_message: 自定义错误信息
  87. // 当 passthrough_body=false 时使用此错误信息
  88. field.Text("custom_message").
  89. Optional().
  90. Nillable(),
  91. // skip_monitoring: 是否跳过运维监控记录
  92. // true: 匹配此规则的错误不会被记录到 ops_error_logs
  93. // false: 正常记录到运维监控(默认行为)
  94. field.Bool("skip_monitoring").
  95. Default(false),
  96. // description: 规则描述,用于说明规则的用途
  97. field.Text("description").
  98. Optional().
  99. Nillable(),
  100. }
  101. }
  102. // Indexes 定义数据库索引,优化查询性能。
  103. func (ErrorPassthroughRule) Indexes() []ent.Index {
  104. return []ent.Index{
  105. index.Fields("enabled"), // 筛选启用的规则
  106. index.Fields("priority"), // 按优先级排序
  107. }
  108. }