openai_chatgpt_headers.go 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. func setOpenAIChatGPTAccountHeaders(headers http.Header, account *Account) {
  7. if headers == nil || account == nil || !account.IsOpenAIOAuthLike() {
  8. return
  9. }
  10. if chatgptAccountID := account.GetChatGPTAccountID(); chatgptAccountID != "" {
  11. headers.Set("chatgpt-account-id", chatgptAccountID)
  12. }
  13. if account.IsChatGPTAccountFedRAMP() {
  14. headers.Set("x-openai-fedramp", "true")
  15. } else {
  16. headers.Del("x-openai-fedramp")
  17. }
  18. }
  19. // resolveAndSetOpenAIChatGPTAccountHeaders 解析 spark 影子账号至其母账号(凭据透传),
  20. // 再调用 setOpenAIChatGPTAccountHeaders 写入 chatgpt-account-id / x-openai-fedramp 头。
  21. // 普通账号(非影子)为直通,行为与直接调用 setOpenAIChatGPTAccountHeaders 一致。
  22. func resolveAndSetOpenAIChatGPTAccountHeaders(ctx context.Context, repo AccountRepository, headers http.Header, account *Account) error {
  23. credAccount, err := resolveCredentialAccount(ctx, repo, account)
  24. if err != nil {
  25. return err
  26. }
  27. setOpenAIChatGPTAccountHeaders(headers, credAccount)
  28. return nil
  29. }