message_request.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package model
  2. import (
  3. "time"
  4. "github.com/quagmt/udecimal"
  5. "github.com/uptrace/bun"
  6. )
  7. // ProviderChainItem 供应商链项
  8. type ProviderChainItem struct {
  9. ID int `json:"id"`
  10. Name string `json:"name"`
  11. }
  12. // SpecialSettingChangeValue 特殊设置变更值
  13. type SpecialSettingChangeValue interface{}
  14. // SpecialSettingChange 特殊设置变更项
  15. type SpecialSettingChange struct {
  16. Path string `json:"path"`
  17. Before SpecialSettingChangeValue `json:"before"`
  18. After SpecialSettingChangeValue `json:"after"`
  19. Changed bool `json:"changed"`
  20. }
  21. // SpecialSetting 特殊设置(通用审计字段)
  22. // 用于记录请求在代理链路中发生的"特殊行为/特殊覆写"的命中与生效情况
  23. type SpecialSetting struct {
  24. Type string `json:"type"` // provider_parameter_override
  25. Scope string `json:"scope"` // provider
  26. ProviderID *int `json:"providerId"`
  27. ProviderName *string `json:"providerName"`
  28. ProviderType *string `json:"providerType"`
  29. Hit bool `json:"hit"`
  30. Changed bool `json:"changed"`
  31. Changes []SpecialSettingChange `json:"changes"`
  32. }
  33. // MessageRequest 请求日志模型
  34. type MessageRequest struct {
  35. bun.BaseModel `bun:"table:message_request,alias:mr"`
  36. ID int `bun:"id,pk,autoincrement" json:"id"`
  37. ProviderID int `bun:"provider_id,notnull" json:"providerId"`
  38. UserID int `bun:"user_id,notnull" json:"userId"`
  39. Key string `bun:"key,notnull" json:"key"`
  40. Model string `bun:"model" json:"model"`
  41. DurationMs *int `bun:"duration_ms" json:"durationMs"`
  42. // 费用 (precision: 21, scale: 15)
  43. CostUSD udecimal.Decimal `bun:"cost_usd,type:numeric(21,15),default:0" json:"costUsd"`
  44. // 供应商倍率 (precision: 10, scale: 4)
  45. CostMultiplier *udecimal.Decimal `bun:"cost_multiplier,type:numeric(10,4)" json:"costMultiplier"`
  46. // Session ID
  47. SessionID *string `bun:"session_id" json:"sessionId"`
  48. // Request Sequence
  49. RequestSequence int `bun:"request_sequence,default:1" json:"requestSequence"`
  50. // 上游决策链
  51. ProviderChain []ProviderChainItem `bun:"provider_chain,type:jsonb" json:"providerChain"`
  52. // HTTP 状态码
  53. StatusCode *int `bun:"status_code" json:"statusCode"`
  54. // API 类型
  55. ApiType *string `bun:"api_type" json:"apiType"`
  56. // 请求端点路径
  57. Endpoint *string `bun:"endpoint" json:"endpoint"`
  58. // 原始模型名称
  59. OriginalModel *string `bun:"original_model" json:"originalModel"`
  60. // Token 使用信息
  61. InputTokens *int `bun:"input_tokens" json:"inputTokens"`
  62. OutputTokens *int `bun:"output_tokens" json:"outputTokens"`
  63. TtfbMs *int `bun:"ttfb_ms" json:"ttfbMs"`
  64. CacheCreationInputTokens *int `bun:"cache_creation_input_tokens" json:"cacheCreationInputTokens"`
  65. CacheReadInputTokens *int `bun:"cache_read_input_tokens" json:"cacheReadInputTokens"`
  66. CacheCreation5mInputTokens *int `bun:"cache_creation_5m_input_tokens" json:"cacheCreation5mInputTokens"`
  67. CacheCreation1hInputTokens *int `bun:"cache_creation_1h_input_tokens" json:"cacheCreation1hInputTokens"`
  68. CacheTtlApplied *string `bun:"cache_ttl_applied" json:"cacheTtlApplied"`
  69. // 1M Context Window 应用状态
  70. Context1mApplied bool `bun:"context_1m_applied,default:false" json:"context1mApplied"`
  71. // 特殊设置
  72. SpecialSettings []SpecialSetting `bun:"special_settings,type:jsonb" json:"specialSettings"`
  73. // 错误信息
  74. ErrorMessage *string `bun:"error_message" json:"errorMessage"`
  75. ErrorStack *string `bun:"error_stack" json:"errorStack"`
  76. ErrorCause *string `bun:"error_cause" json:"errorCause"`
  77. // 拦截原因
  78. BlockedBy *string `bun:"blocked_by" json:"blockedBy"`
  79. BlockedReason *string `bun:"blocked_reason" json:"blockedReason"`
  80. // User-Agent
  81. UserAgent *string `bun:"user_agent" json:"userAgent"`
  82. // Messages 数量
  83. MessagesCount *int `bun:"messages_count" json:"messagesCount"`
  84. CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp" json:"createdAt"`
  85. UpdatedAt time.Time `bun:"updated_at,notnull,default:current_timestamp" json:"updatedAt"`
  86. DeletedAt *time.Time `bun:"deleted_at,soft_delete" json:"deletedAt"`
  87. // 关联
  88. User *User `bun:"rel:belongs-to,join:user_id=id" json:"user,omitempty"`
  89. Provider *Provider `bun:"rel:belongs-to,join:provider_id=id" json:"provider,omitempty"`
  90. }
  91. // TotalTokens 返回总 token 数
  92. func (m *MessageRequest) TotalTokens() int {
  93. var total int
  94. if m.InputTokens != nil {
  95. total += *m.InputTokens
  96. }
  97. if m.OutputTokens != nil {
  98. total += *m.OutputTokens
  99. }
  100. if m.CacheReadInputTokens != nil {
  101. total += *m.CacheReadInputTokens
  102. }
  103. if m.CacheCreationInputTokens != nil {
  104. total += *m.CacheCreationInputTokens
  105. }
  106. return total
  107. }
  108. // IsSuccess 检查请求是否成功
  109. func (m *MessageRequest) IsSuccess() bool {
  110. if m.StatusCode == nil {
  111. return false
  112. }
  113. return *m.StatusCode >= 200 && *m.StatusCode < 300
  114. }
  115. // IsError 检查请求是否失败
  116. func (m *MessageRequest) IsError() bool {
  117. if m.StatusCode == nil {
  118. return false
  119. }
  120. return *m.StatusCode >= 400
  121. }
  122. // IsBlocked 检查请求是否被拦截
  123. func (m *MessageRequest) IsBlocked() bool {
  124. return m.BlockedBy != nil && *m.BlockedBy != ""
  125. }