mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +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" ```
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package intercept
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
ant_option "github.com/anthropics/anthropic-sdk-go/option"
|
|
oai_option "github.com/openai/openai-go/v3/option"
|
|
|
|
"github.com/coder/coder/v2/aibridge/context"
|
|
)
|
|
|
|
const (
|
|
prefix = "X-AI-Bridge-Actor"
|
|
)
|
|
|
|
func ActorIDHeader() string {
|
|
return fmt.Sprintf("%s-ID", prefix)
|
|
}
|
|
|
|
func ActorMetadataHeader(name string) string {
|
|
return fmt.Sprintf("%s-Metadata-%s", prefix, name)
|
|
}
|
|
|
|
func IsActorHeader(name string) bool {
|
|
return strings.HasPrefix(strings.ToLower(name), strings.ToLower(prefix))
|
|
}
|
|
|
|
// ActorHeadersAsOpenAIOpts produces a slice of headers using OpenAI's RequestOption type.
|
|
func ActorHeadersAsOpenAIOpts(actor *context.Actor) []oai_option.RequestOption {
|
|
var opts []oai_option.RequestOption
|
|
|
|
headers := headersFromActor(actor)
|
|
if len(headers) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for k, v := range headers {
|
|
// [k] will be canonicalized, see [http.Header]'s [Add] method.
|
|
opts = append(opts, oai_option.WithHeaderAdd(k, v))
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
// ActorHeadersAsAnthropicOpts produces a slice of headers using Anthropic's RequestOption type.
|
|
func ActorHeadersAsAnthropicOpts(actor *context.Actor) []ant_option.RequestOption {
|
|
var opts []ant_option.RequestOption
|
|
|
|
headers := headersFromActor(actor)
|
|
if len(headers) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for k, v := range headers {
|
|
// [k] will be canonicalized, see [http.Header]'s [Add] method.
|
|
opts = append(opts, ant_option.WithHeaderAdd(k, v))
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
// headersFromActor produces a map of headers from a given [context.Actor].
|
|
func headersFromActor(actor *context.Actor) map[string]string {
|
|
if actor == nil {
|
|
return nil
|
|
}
|
|
|
|
headers := make(map[string]string, len(actor.Metadata)+1)
|
|
|
|
// Add actor ID.
|
|
headers[ActorIDHeader()] = actor.ID
|
|
|
|
// Add headers for provided metadata.
|
|
for k, v := range actor.Metadata {
|
|
headers[ActorMetadataHeader(k)] = fmt.Sprintf("%v", v)
|
|
}
|
|
|
|
return headers
|
|
}
|