fix(openai): 允许 Agent Identity 无令牌拨号 WS

This commit is contained in:
cat
2026-07-14 17:41:17 +08:00
parent 10aa88aab9
commit bd1399f81e
4 changed files with 40 additions and 4 deletions
@@ -189,6 +189,32 @@ func TestOpenAIWSAgentIdentityRecoveryRequiresTaskInvalidBody(t *testing.T) {
}))
}
func TestValidateOpenAIWSBearerTokenAllowsAgentIdentityWithoutStoredToken(t *testing.T) {
t.Run("Given Agent Identity When a WS path receives no bearer token Then dial-time assertion auth is allowed", func(t *testing.T) {
account := &Account{
Platform: PlatformOpenAI,
Type: AccountTypeOAuth,
Credentials: map[string]any{
"auth_mode": OpenAIAuthModeAgentIdentity,
},
}
require.NoError(t, validateOpenAIWSBearerToken(account, ""))
})
t.Run("Given bearer credentials When a WS path receives no token Then the request is rejected", func(t *testing.T) {
accounts := []*Account{
{Platform: PlatformOpenAI, Type: AccountTypeOAuth},
{Platform: PlatformOpenAI, Type: AccountTypeOAuth, Credentials: map[string]any{"auth_mode": OpenAIAuthModePersonalAccessToken}},
{Platform: PlatformOpenAI, Type: AccountTypeAPIKey},
}
for _, account := range accounts {
require.EqualError(t, validateOpenAIWSBearerToken(account, ""), "token is empty")
}
})
}
func TestOpenAIWSConnPoolHeadersFactoryRunsAtDialAndStalePrewarmIsDiscarded(t *testing.T) {
cfg := &config.Config{}
cfg.Gateway.OpenAIWS.MaxConnsPerAccount = 1
@@ -39,8 +39,8 @@ func (s *OpenAIGatewayService) ProxyResponsesWebSocketFromClient(
if account == nil {
return errors.New("account is nil")
}
if strings.TrimSpace(token) == "" {
return errors.New("token is empty")
if err := validateOpenAIWSBearerToken(account, token); err != nil {
return err
}
// 预取一次 OpenAI Fast Policy settings,绑定到 ctx,让该 WS session
@@ -15,6 +15,16 @@ import (
"github.com/tidwall/sjson"
)
func validateOpenAIWSBearerToken(account *Account, token string) error {
if account == nil {
return errors.New("account is nil")
}
if strings.TrimSpace(token) == "" && !account.IsOpenAIAgentIdentity() {
return errors.New("token is empty")
}
return nil
}
func (s *OpenAIGatewayService) buildOpenAIResponsesWSURL(account *Account) (string, error) {
if account == nil {
return "", errors.New("account is nil")
@@ -243,8 +243,8 @@ func (s *OpenAIGatewayService) proxyResponsesWebSocketV2Passthrough(
if account == nil {
return errors.New("account is nil")
}
if strings.TrimSpace(token) == "" {
return errors.New("token is empty")
if err := validateOpenAIWSBearerToken(account, token); err != nil {
return err
}
requestModel := strings.TrimSpace(gjson.GetBytes(firstClientMessage, "model").String())
requestPreviousResponseID := strings.TrimSpace(gjson.GetBytes(firstClientMessage, "previous_response_id").String())