2025-12-11 16:38:54 +01:00
|
|
|
// Package aibridge provides utilities for the AI Bridge feature.
|
|
|
|
|
package aibridge
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-25 14:17:56 -04:00
|
|
|
// HeaderCoderToken is a header set by clients opting into BYOK
|
|
|
|
|
// (Bring Your Own Key) mode. It carries the Coder token so
|
|
|
|
|
// that Authorization and X-Api-Key can carry the user's own LLM
|
|
|
|
|
// credentials. When present, AI Bridge forwards the user's LLM
|
|
|
|
|
// headers unchanged instead of injecting the centralized key.
|
|
|
|
|
//
|
|
|
|
|
// The AI Bridge proxy also sets this header automatically for clients
|
|
|
|
|
// that use per-user LLM credentials but cannot set custom headers.
|
|
|
|
|
const HeaderCoderToken = "X-Coder-AI-Governance-Token" //nolint:gosec // This is a header name, not a credential.
|
2026-01-21 19:06:19 +00:00
|
|
|
|
2026-03-30 15:21:30 +01:00
|
|
|
// HeaderCoderRequestID is a header set by aibridgeproxyd on each
|
|
|
|
|
// request forwarded to aibridged for cross-service log correlation.
|
|
|
|
|
const HeaderCoderRequestID = "X-Coder-AI-Governance-Request-Id"
|
|
|
|
|
|
2026-06-30 14:01:27 +02:00
|
|
|
// HeaderAgentFirewallSessionID is injected by Agent Firewall on requests
|
|
|
|
|
// routed through it. It carries the firewall session UUID so that AI
|
|
|
|
|
// Gateway can correlate interceptions with firewall audit events.
|
|
|
|
|
const HeaderAgentFirewallSessionID = "X-Coder-Agent-Firewall-Session-Id"
|
|
|
|
|
|
|
|
|
|
// HeaderAgentFirewallSequenceNumber is injected alongside the session ID
|
|
|
|
|
// by Agent Firewall. It carries a monotonically increasing sequence
|
|
|
|
|
// number that orders network requests within a single firewall session.
|
|
|
|
|
const HeaderAgentFirewallSequenceNumber = "X-Coder-Agent-Firewall-Sequence-Number"
|
|
|
|
|
|
2026-03-31 16:00:37 +01:00
|
|
|
// Copilot provider.
|
|
|
|
|
const (
|
|
|
|
|
ProviderCopilotBusiness = "copilot-business"
|
|
|
|
|
HostCopilotBusiness = "api.business.githubcopilot.com"
|
|
|
|
|
ProviderCopilotEnterprise = "copilot-enterprise"
|
|
|
|
|
HostCopilotEnterprise = "api.enterprise.githubcopilot.com"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-31 12:08:45 -04:00
|
|
|
// ChatGPT provider.
|
|
|
|
|
const (
|
|
|
|
|
ProviderChatGPT = "chatgpt"
|
2026-03-31 12:54:38 -04:00
|
|
|
HostChatGPT = "chatgpt.com"
|
|
|
|
|
BaseURLChatGPT = "https://" + HostChatGPT + "/backend-api/codex"
|
2026-03-31 12:08:45 -04:00
|
|
|
)
|
|
|
|
|
|
2026-06-23 12:15:10 +01:00
|
|
|
// API route prefixes for the AI Gateway and legacy AI Bridge endpoints.
|
|
|
|
|
const (
|
|
|
|
|
// AIGatewayRootPath is the URL prefix the AI Gateway handler
|
|
|
|
|
// registers all of its routes under.
|
|
|
|
|
AIGatewayRootPath = "/api/v2/ai-gateway"
|
|
|
|
|
// AIBridgeRootPath is the legacy prefix kept for backward compatibility.
|
|
|
|
|
AIBridgeRootPath = "/api/v2/aibridge"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-25 14:17:56 -04:00
|
|
|
// IsBYOK reports whether the request is using BYOK mode, determined
|
|
|
|
|
// by the presence of the X-Coder-AI-Governance-Token header.
|
|
|
|
|
func IsBYOK(header http.Header) bool {
|
|
|
|
|
return strings.TrimSpace(header.Get(HeaderCoderToken)) != ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExtractAuthToken extracts a token from HTTP headers.
|
|
|
|
|
// It checks the BYOK header first (set by clients opting into BYOK),
|
|
|
|
|
// then falls back to Authorization: Bearer and X-Api-Key for direct
|
|
|
|
|
// centralized mode. If none are present, an empty string is returned.
|
2025-12-11 16:38:54 +01:00
|
|
|
func ExtractAuthToken(header http.Header) string {
|
2026-03-25 14:17:56 -04:00
|
|
|
if token := strings.TrimSpace(header.Get(HeaderCoderToken)); token != "" {
|
2026-01-21 19:06:19 +00:00
|
|
|
return token
|
|
|
|
|
}
|
2025-12-11 16:38:54 +01:00
|
|
|
if auth := strings.TrimSpace(header.Get("Authorization")); auth != "" {
|
|
|
|
|
fields := strings.Fields(auth)
|
|
|
|
|
if len(fields) == 2 && strings.EqualFold(fields[0], "Bearer") {
|
|
|
|
|
return fields[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if apiKey := strings.TrimSpace(header.Get("X-Api-Key")); apiKey != "" {
|
|
|
|
|
return apiKey
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|