interface.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package adaptor
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "net/http"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/labring/aiproxy/core/model"
  10. "github.com/labring/aiproxy/core/relay/meta"
  11. "github.com/labring/aiproxy/core/relay/mode"
  12. )
  13. type StoreCache struct {
  14. ID string
  15. GroupID string
  16. TokenID int
  17. ChannelID int
  18. Model string
  19. ExpiresAt time.Time
  20. }
  21. type Store interface {
  22. GetStore(group string, tokenID int, id string) (StoreCache, error)
  23. SaveStore(store StoreCache) error
  24. }
  25. type Metadata struct {
  26. Config ConfigTemplates
  27. KeyHelp string
  28. Features []string
  29. Models []model.ModelConfig
  30. }
  31. type RequestURL struct {
  32. Method string
  33. URL string
  34. }
  35. type GetRequestURL interface {
  36. GetRequestURL(meta *meta.Meta, store Store) (RequestURL, error)
  37. }
  38. type SetupRequestHeader interface {
  39. SetupRequestHeader(meta *meta.Meta, store Store, c *gin.Context, req *http.Request) error
  40. }
  41. type ConvertRequest interface {
  42. ConvertRequest(meta *meta.Meta, store Store, req *http.Request) (ConvertResult, error)
  43. }
  44. type DoRequest interface {
  45. DoRequest(
  46. meta *meta.Meta,
  47. store Store,
  48. c *gin.Context,
  49. req *http.Request,
  50. ) (*http.Response, error)
  51. }
  52. type DoResponse interface {
  53. DoResponse(
  54. meta *meta.Meta,
  55. store Store,
  56. c *gin.Context,
  57. resp *http.Response,
  58. ) (model.Usage, Error)
  59. }
  60. type Adaptor interface {
  61. Metadata() Metadata
  62. SupportMode(mode mode.Mode) bool
  63. DefaultBaseURL() string
  64. GetRequestURL
  65. SetupRequestHeader
  66. ConvertRequest
  67. DoRequest
  68. DoResponse
  69. }
  70. type ConvertResult struct {
  71. Header http.Header
  72. Body io.Reader
  73. }
  74. type Error interface {
  75. json.Marshaler
  76. error
  77. StatusCode() int
  78. }
  79. var ErrGetBalanceNotImplemented = errors.New("get balance not implemented")
  80. type Balancer interface {
  81. GetBalance(channel *model.Channel) (float64, error)
  82. }
  83. type KeyValidator interface {
  84. ValidateKey(key string) error
  85. }
  86. type ConfigType string
  87. const (
  88. ConfigTypeString ConfigType = "string"
  89. ConfigTypeNumber ConfigType = "number"
  90. ConfigTypeBool ConfigType = "bool"
  91. ConfigTypeObject ConfigType = "object"
  92. )
  93. type ConfigTemplate struct {
  94. Name string `json:"name"`
  95. Description string `json:"description"`
  96. Example any `json:"example,omitempty"`
  97. Validator func(any) error `json:"-"`
  98. Required bool `json:"required"`
  99. Type ConfigType `json:"type"`
  100. }
  101. type ConfigTemplates = map[string]ConfigTemplate