channel_monitor.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package schema
  2. import (
  3. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect/entsql"
  6. "entgo.io/ent/schema"
  7. "entgo.io/ent/schema/edge"
  8. "entgo.io/ent/schema/field"
  9. "entgo.io/ent/schema/index"
  10. )
  11. // ChannelMonitor holds the schema definition for the ChannelMonitor entity.
  12. // 渠道监控配置:定期对指定 provider/endpoint/api_key 下的模型做心跳测试。
  13. type ChannelMonitor struct {
  14. ent.Schema
  15. }
  16. func (ChannelMonitor) Annotations() []schema.Annotation {
  17. return []schema.Annotation{
  18. entsql.Annotation{Table: "channel_monitors"},
  19. }
  20. }
  21. func (ChannelMonitor) Mixin() []ent.Mixin {
  22. return []ent.Mixin{
  23. mixins.TimeMixin{},
  24. }
  25. }
  26. func (ChannelMonitor) Fields() []ent.Field {
  27. return []ent.Field{
  28. field.String("name").
  29. NotEmpty().
  30. MaxLen(100),
  31. field.Enum("provider").
  32. Values("openai", "anthropic", "gemini", "grok",
  33. "antigravity", "kimi", "zhipu", "deepseek", "minimax"),
  34. // check_mode: 'probe' | 'quota' | 'quota_probe'
  35. // probe - LLM 探活(默认,原有行为)
  36. // quota - 仅查关联账号的用量/余额(零 LLM 成本;endpoint/api_key 可空)
  37. // quota_probe - 探活 + 配额并存(配额快照挂到主模型历史行)
  38. // antigravity 无探活 adapter,仅允许 quota。
  39. field.String("check_mode").
  40. Default("probe").
  41. MaxLen(32).
  42. Comment("probe = LLM probe (default); quota = account usage only; quota_probe = both"),
  43. // account_id: 配额模式的数据源账号(复用账号侧用量服务,不直接对接上游)。
  44. // 普通字段而非 edge(FK 由 SQL 迁移管理);账号删除时数据库置空,
  45. // 监控保留并报「账号未关联」。
  46. field.Int64("account_id").
  47. Optional().
  48. Nillable(),
  49. field.String("api_mode").
  50. Default("chat_completions").
  51. MaxLen(32).
  52. Comment("OpenAI request protocol: chat_completions or responses; non-OpenAI uses chat_completions"),
  53. // endpoint: 探活模式必填(service 层校验);quota 模式存空串
  54. // (列保持 NOT NULL,去掉 NotEmpty 校验器即可)。
  55. field.String("endpoint").
  56. MaxLen(500).
  57. Comment("Provider base origin, e.g. https://api.openai.com; empty for quota-only monitors"),
  58. field.String("api_key_encrypted").
  59. NotEmpty().
  60. Sensitive().
  61. Comment("AES-256-GCM encrypted API key"),
  62. field.String("primary_model").
  63. NotEmpty().
  64. MaxLen(200),
  65. field.JSON("extra_models", []string{}).
  66. Default([]string{}).
  67. Comment("Additional model names to test alongside primary_model"),
  68. field.String("group_name").
  69. Optional().
  70. Default("").
  71. MaxLen(100),
  72. field.Bool("enabled").
  73. Default(true),
  74. field.Int("interval_seconds").
  75. Range(15, 3600),
  76. field.Int("jitter_seconds").
  77. Default(0).
  78. Range(0, 3600).
  79. Comment("每次调度在 interval 基础上 ± [0, jitter] 的均匀随机偏移(秒);0 表示固定间隔。service 层另保证 interval - jitter >= 15"),
  80. field.Time("last_checked_at").
  81. Optional().
  82. Nillable(),
  83. field.Int64("created_by"),
  84. // ---- 自定义请求快照字段(来自模板 / 手动编辑) ----
  85. // template_id: 关联的请求模板 ID(仅用于 UI 分组 + 一键应用)。
  86. // 实际运行时 checker 只读下面 3 个快照字段,**不再回查模板表**。
  87. // 模板被删除时此字段会被 SET NULL(见 Edges 的 OnDelete 注解)。
  88. field.Int64("template_id").
  89. Optional().
  90. Nillable(),
  91. // extra_headers: 自定义 HTTP 头快照(来自模板 or 用户手填)。
  92. // 运行时 merge 进 adapter 默认 headers。
  93. field.JSON("extra_headers", map[string]string{}).
  94. Default(map[string]string{}),
  95. // body_override_mode: 同 ChannelMonitorRequestTemplate.body_override_mode
  96. field.String("body_override_mode").
  97. Default("off").
  98. MaxLen(10),
  99. // body_override: 同 ChannelMonitorRequestTemplate.body_override
  100. field.JSON("body_override", map[string]any{}).
  101. Optional(),
  102. }
  103. }
  104. func (ChannelMonitor) Edges() []ent.Edge {
  105. return []ent.Edge{
  106. edge.To("history", ChannelMonitorHistory.Type).
  107. Annotations(entsql.OnDelete(entsql.Cascade)),
  108. edge.To("daily_rollups", ChannelMonitorDailyRollup.Type).
  109. Annotations(entsql.OnDelete(entsql.Cascade)),
  110. // 关联请求模板:模板被删除时 template_id 自动置空,
  111. // 监控本身保留(继续用快照字段跑)。
  112. edge.To("request_template", ChannelMonitorRequestTemplate.Type).
  113. Field("template_id").
  114. Unique().
  115. Annotations(entsql.OnDelete(entsql.SetNull)),
  116. }
  117. }
  118. func (ChannelMonitor) Indexes() []ent.Index {
  119. return []ent.Index{
  120. index.Fields("enabled", "last_checked_at"),
  121. index.Fields("provider"),
  122. index.Fields("provider", "api_mode"),
  123. index.Fields("group_name"),
  124. index.Fields("template_id"),
  125. index.Fields("account_id"),
  126. }
  127. }