Documentation
¶
Overview ¶
Package proxy implements HTTP handlers that forward requests to GitHub Copilot's backend. It provides Anthropic-to-OpenAI translation for the /v1/messages endpoint and near zero-copy passthrough for OpenAI endpoints.
Index ¶
- func MapStopReason(finishReason *string) string
- func NormalizeModelName(model string) string
- func StreamOpenAIPassthrough(w http.ResponseWriter, body io.ReadCloser)
- func StreamOpenAIToAnthropic(w http.ResponseWriter, body io.ReadCloser, model string, requestID string)
- func StreamOpenAIToGemini(w http.ResponseWriter, body io.ReadCloser)
- func TranslateAnthropicToOpenAI(req *models.AnthropicRequest) (*models.OpenAIRequest, error)
- func TranslateGeminiCountTokens(req *models.GeminiCountTokensRequest, pathModel string) (*models.OpenAIRequest, error)
- func TranslateGeminiToOpenAI(req *models.GeminiGenerateContentRequest, pathModel string, stream bool) (*models.OpenAIRequest, error)
- func TranslateOpenAIToAnthropic(resp *models.OpenAIResponse, model string) *models.AnthropicResponse
- func TranslateOpenAIToGemini(resp *models.OpenAIResponse) *models.GeminiGenerateContentResponse
- type CopilotHeaderConfig
- type Option
- type ProxyHandler
- func (h *ProxyHandler) HandleAnthropicMessages(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleCompact(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleGeminiModels(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleHealthz(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleMemorySummarize(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleModels(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleOpenAIChatCompletions(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleReadyz(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleResponses(w http.ResponseWriter, r *http.Request)
- func (h *ProxyHandler) HandleResponsesWebSocket(w http.ResponseWriter, r *http.Request)
- type ResponsesWebSocketConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapStopReason ¶
MapStopReason maps an OpenAI finish reason to an Anthropic stop reason.
func NormalizeModelName ¶
NormalizeModelName converts Anthropic model names to Copilot-compatible names.
func StreamOpenAIPassthrough ¶
func StreamOpenAIPassthrough(w http.ResponseWriter, body io.ReadCloser)
StreamOpenAIPassthrough streams OpenAI SSE bytes directly to the client with no parsing.
func StreamOpenAIToAnthropic ¶
func StreamOpenAIToAnthropic(w http.ResponseWriter, body io.ReadCloser, model string, requestID string)
StreamOpenAIToAnthropic translates an OpenAI SSE stream into Anthropic SSE format.
func StreamOpenAIToGemini ¶ added in v0.4.0
func StreamOpenAIToGemini(w http.ResponseWriter, body io.ReadCloser)
StreamOpenAIToGemini translates upstream OpenAI SSE into Gemini-style data-only SSE frames.
func TranslateAnthropicToOpenAI ¶
func TranslateAnthropicToOpenAI(req *models.AnthropicRequest) (*models.OpenAIRequest, error)
TranslateAnthropicToOpenAI converts an Anthropic Messages API request to OpenAI Chat Completions format.
func TranslateGeminiCountTokens ¶ added in v0.4.0
func TranslateGeminiCountTokens(req *models.GeminiCountTokensRequest, pathModel string) (*models.OpenAIRequest, error)
func TranslateGeminiToOpenAI ¶ added in v0.4.0
func TranslateGeminiToOpenAI(req *models.GeminiGenerateContentRequest, pathModel string, stream bool) (*models.OpenAIRequest, error)
func TranslateOpenAIToAnthropic ¶
func TranslateOpenAIToAnthropic(resp *models.OpenAIResponse, model string) *models.AnthropicResponse
TranslateOpenAIToAnthropic translates an OpenAI Chat Completions response to Anthropic Messages format.
func TranslateOpenAIToGemini ¶ added in v0.4.0
func TranslateOpenAIToGemini(resp *models.OpenAIResponse) *models.GeminiGenerateContentResponse
Types ¶
type CopilotHeaderConfig ¶ added in v0.5.1
type CopilotHeaderConfig struct {
EditorVersion string
EditorPluginVersion string
UserAgent string
IntegrationID string
GitHubAPIVersion string
OpenAIIntent string
}
CopilotHeaderConfig controls the synthetic editor-identifying headers sent to the upstream Copilot backend. Empty fields fall back to project defaults.
func DefaultCopilotHeaderConfig ¶ added in v0.5.1
func DefaultCopilotHeaderConfig() CopilotHeaderConfig
type Option ¶ added in v0.5.1
type Option func(*ProxyHandler)
Option customizes ProxyHandler behavior.
func WithCopilotHeaderConfig ¶ added in v0.5.1
func WithCopilotHeaderConfig(cfg CopilotHeaderConfig) Option
WithCopilotHeaderConfig overrides the synthetic Copilot-identifying headers used for upstream requests.
func WithResponsesWebSocketConfig ¶ added in v0.6.0
func WithResponsesWebSocketConfig(cfg ResponsesWebSocketConfig) Option
WithResponsesWebSocketConfig overrides websocket-session state behavior for GET /v1/responses Codex clients.
type ProxyHandler ¶
type ProxyHandler struct {
// contains filtered or unexported fields
}
ProxyHandler holds dependencies for all HTTP handlers.
func NewProxyHandler ¶
func NewProxyHandler(a *auth.Authenticator, log *logger.Logger, opts ...Option) *ProxyHandler
NewProxyHandler creates a ProxyHandler with connection pooling and HTTP/2.
func (*ProxyHandler) HandleAnthropicMessages ¶
func (h *ProxyHandler) HandleAnthropicMessages(w http.ResponseWriter, r *http.Request)
HandleAnthropicMessages handles POST /v1/messages by translating the Anthropic request to OpenAI format, forwarding to Copilot, and translating the response back.
func (*ProxyHandler) HandleCompact ¶ added in v0.3.1
func (h *ProxyHandler) HandleCompact(w http.ResponseWriter, r *http.Request)
HandleCompact handles POST /v1/responses/compact by forwarding the request to the upstream /responses endpoint with a compaction system prompt injected. The upstream response is then transformed into the compact response format that Codex expects. The returned compaction item is a proxy-owned token that this proxy can later expand back into summarized context for /responses.
func (*ProxyHandler) HandleGeminiModels ¶ added in v0.4.0
func (h *ProxyHandler) HandleGeminiModels(w http.ResponseWriter, r *http.Request)
HandleGeminiModels routes Gemini-native model actions to the corresponding translation handler.
func (*ProxyHandler) HandleHealthz ¶
func (h *ProxyHandler) HandleHealthz(w http.ResponseWriter, r *http.Request)
HandleHealthz handles GET /healthz and returns {"status":"ok"}.
func (*ProxyHandler) HandleMemorySummarize ¶ added in v0.3.1
func (h *ProxyHandler) HandleMemorySummarize(w http.ResponseWriter, r *http.Request)
HandleMemorySummarize handles POST /v1/memories/trace_summarize by sending the traces to the upstream /responses endpoint with a summarization prompt, then transforming the response into the format Codex expects.
func (*ProxyHandler) HandleModels ¶
func (h *ProxyHandler) HandleModels(w http.ResponseWriter, r *http.Request)
HandleModels handles GET /v1/models by proxying to the upstream Copilot API. Responses are cached for modelsCacheTTL to avoid repeated upstream calls. The response includes both the standard OpenAI "data" field and a Codex-compatible "models" field so that both OpenAI SDK clients and Codex CLI can parse it.
func (*ProxyHandler) HandleOpenAIChatCompletions ¶
func (h *ProxyHandler) HandleOpenAIChatCompletions(w http.ResponseWriter, r *http.Request)
HandleOpenAIChatCompletions handles POST /v1/chat/completions by forwarding the request to Copilot with only auth headers injected (near zero-copy passthrough).
func (*ProxyHandler) HandleReadyz ¶ added in v0.5.1
func (h *ProxyHandler) HandleReadyz(w http.ResponseWriter, r *http.Request)
HandleReadyz validates that the proxy can obtain an auth token and reach the upstream Copilot API.
func (*ProxyHandler) HandleResponses ¶
func (h *ProxyHandler) HandleResponses(w http.ResponseWriter, r *http.Request)
HandleResponses handles POST /v1/responses by forwarding the request to Copilot's responses endpoint with only auth headers injected.
func (*ProxyHandler) HandleResponsesWebSocket ¶ added in v0.6.0
func (h *ProxyHandler) HandleResponsesWebSocket(w http.ResponseWriter, r *http.Request)
HandleResponsesWebSocket handles GET /v1/responses websocket upgrades used by Codex. Each websocket request is translated into a normal upstream streaming /responses HTTP request and the SSE data payloads are forwarded back as websocket text frames.
type ResponsesWebSocketConfig ¶ added in v0.6.0
type ResponsesWebSocketConfig struct {
TurnStateDelta bool
DisableAutoCompact bool
AutoCompactMaxItems int
AutoCompactMaxBytes int
AutoCompactKeepTail int
}
ResponsesWebSocketConfig controls websocket-session state management for Codex-style GET /v1/responses clients.
func DefaultResponsesWebSocketConfig ¶ added in v0.6.0
func DefaultResponsesWebSocketConfig() ResponsesWebSocketConfig