mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
e00e85765b
This PR merges code from `coder/aibridge` repository into `coder/coder`. It was split into 4 PRs for easier review but stacked PRs will need to be merged into this PR so all checks pass. * https://github.com/coder/coder/pull/24190 -> raw code copy (this PR, before merging PRs on top of it, it was just 1 commit: https://github.com/coder/coder/commit/70d33f33200c7e77df910957595715f81f9bec24) * https://github.com/coder/coder/pull/24570 -> update imports in `coder/coder` to use copied code * https://github.com/coder/coder/pull/24586 -> linter fixes and CI integration (also added README.md) * https://github.com/coder/coder/pull/24571 -> added exclude to scripts/check_emdash.sh check Original PR message (before PR squash): Moves coder/aibridge code into coder/coder repository. Omitted files: - `go.mod`, `go.sum`, `.gitignore`, `.github/workflows/ci.yml,` `Makefile`, `LICENSE`, `README.md` (modified README.md is added later) - `.github`, `example`, `buildinfo,` `scripts` directories Simple verification script (will list omitted files) ``` tmp=$(mktemp -d) echo "$tmp" git clone --depth=1 https://github.com/coder/aibridge "$tmp/aibridge" git clone --depth=1 --branch pb/aibridge-code-move https://github.com/coder/coder "$tmp/coder" diff -rq --exclude=.git "$tmp/aibridge" "$tmp/coder/aibridge" # rm -rf "$tmp" ```
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package intercept
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// hopByHopHeaders are connection-level headers specific to the connection
|
|
// between client and AI Bridge, not meant for the upstream.
|
|
// See https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1
|
|
var hopByHopHeaders = []string{
|
|
"Connection",
|
|
"Keep-Alive",
|
|
"Proxy-Authenticate",
|
|
"Proxy-Authorization",
|
|
"Te",
|
|
"Trailer",
|
|
"Transfer-Encoding",
|
|
"Upgrade",
|
|
}
|
|
|
|
// nonForwardedHeaders are transport-level headers managed by aibridge or
|
|
// Go's HTTP transport that must not be forwarded to the upstream provider.
|
|
var nonForwardedHeaders = []string{
|
|
"Host",
|
|
"Accept-Encoding",
|
|
"Content-Length",
|
|
}
|
|
|
|
// authHeaders are headers that carry authentication credentials from the
|
|
// client. The upstream request is built by the SDK, which sets the correct
|
|
// provider credentials via option.WithAPIKey. Client auth headers are
|
|
// stripped here and the provider credentials are re-injected by
|
|
// BuildUpstreamHeaders from the SDK-built request.
|
|
var authHeaders = []string{
|
|
"Authorization",
|
|
"X-Api-Key",
|
|
}
|
|
|
|
// PrepareClientHeaders returns a copy of the client headers with hop-by-hop,
|
|
// transport, and auth headers removed.
|
|
func PrepareClientHeaders(clientHeaders http.Header) http.Header {
|
|
prepared := clientHeaders.Clone()
|
|
for _, h := range hopByHopHeaders {
|
|
prepared.Del(h)
|
|
}
|
|
for _, h := range nonForwardedHeaders {
|
|
prepared.Del(h)
|
|
}
|
|
for _, h := range authHeaders {
|
|
prepared.Del(h)
|
|
}
|
|
return prepared
|
|
}
|
|
|
|
// BuildUpstreamHeaders produces the header set for an upstream SDK request.
|
|
// It starts from the prepared client headers, then preserves specific
|
|
// headers from the SDK-built request that must not be overwritten.
|
|
func BuildUpstreamHeaders(sdkHeader http.Header, clientHeaders http.Header, authHeaderName string) http.Header {
|
|
headers := PrepareClientHeaders(clientHeaders)
|
|
|
|
// Preserve the auth header set by the SDK from the provider configuration.
|
|
if v := sdkHeader.Get(authHeaderName); v != "" {
|
|
headers.Set(authHeaderName, v)
|
|
}
|
|
|
|
// Preserve actor headers injected by aibridge as per-request SDK options.
|
|
for name, values := range sdkHeader {
|
|
if IsActorHeader(name) {
|
|
headers[name] = values
|
|
}
|
|
}
|
|
|
|
return headers
|
|
}
|