| 219 | } |
| 220 | |
| 221 | func (p *Server) aiGatewayProviderAuthForUser( |
| 222 | ctx context.Context, |
| 223 | ownerID uuid.UUID, |
| 224 | provider database.AIProvider, |
| 225 | format aiGatewayRequestFormat, |
| 226 | ) (aiGatewayProviderAuth, error) { |
| 227 | if !p.allowBYOK { |
| 228 | return aiGatewayProviderAuth{}, nil |
| 229 | } |
| 230 | userKey, err := p.db.GetUserAIProviderKeyByProviderID(ctx, database.GetUserAIProviderKeyByProviderIDParams{ |
| 231 | UserID: ownerID, |
| 232 | AIProviderID: provider.ID, |
| 233 | }) |
| 234 | if err != nil { |
| 235 | if xerrors.Is(err, sql.ErrNoRows) { |
| 236 | return aiGatewayProviderAuth{}, nil |
| 237 | } |
| 238 | return aiGatewayProviderAuth{}, xerrors.Errorf("get user AI provider key: %w", err) |
| 239 | } |
| 240 | apiKey := strings.TrimSpace(userKey.APIKey) |
| 241 | if apiKey == "" { |
| 242 | return aiGatewayProviderAuth{}, nil |
| 243 | } |
| 244 | |
| 245 | headers := map[string]string{} |
| 246 | switch format { |
| 247 | case aiGatewayRequestFormatAnthropic: |
| 248 | headers["X-Api-Key"] = apiKey |
| 249 | default: |
| 250 | headers["Authorization"] = "Bearer " + apiKey |
| 251 | } |
| 252 | return aiGatewayProviderAuth{Headers: headers}, nil |
| 253 | } |
| 254 | |
| 255 | func (p *Server) resolveAIGatewayRoute( |
| 256 | ctx context.Context, |