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 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) HandleResponses(w http.ResponseWriter, r *http.Request)
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 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) *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) 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.