Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Models newer than the catalog now route instead of failing with `unknown provider for model`.
A request for a model no credential lists is adopted on the provider it belongs to: the
request's `/provider/<name>` scope, a credential prefix or ID namespace (`vibe/...`,
`devin/...`), or otherwise the provider serving the most models of its family (`claude`,
`gpt`, `gemini`, `grok`, `mistral`, `devstral`, ...). This covers every provider, including
Antigravity, plugins and OpenAI-compatible upstreams. The model copies the capabilities of
its closest sibling, survives catalog and token refreshes, and reaches credentials added
later. Excluded-models settings still apply and there is no cap; the provider's own error
answers names it does not know. Claude Mythos 5.1 and 5.2 are also built-in Claude models.
- `GET /opencode/<access key>/api.json` serves the hub's models as a models.dev catalog for
OpenCode's `OPENCODE_MODELS_URL`, so OpenCode lists every hub model live. The key in the path
is masked in logs.

- Provider-scoped client endpoints under `/provider/<name>` with filtered OpenAI, Anthropic and
Gemini model catalogs, provider-pinned text execution and isolated session affinity. Named
OpenAI-compatible upstreams such as `mistral` are supported. Antigravity CLI setup now uses
Expand Down
29 changes: 29 additions & 0 deletions docs/t3code.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ Setup on the machine running T3 Code:

Clearing the binary path returns to T3 Code's bundled runtime and its fixed Gemini list.

## OpenCode

OpenCode takes its model catalog from a models.dev-style URL. The hub serves its own catalog at
`https://<hub>/opencode/<access key>/api.json`: every model the hub serves, under one provider
called `t3code-proxy`, refreshed by OpenCode every few minutes. Claude models use the Anthropic
Messages API, Codex models the Responses API, and the rest OpenAI chat completions, all on the hub.
OpenCode cannot send a header with that fetch, so the access key is in the path; the hub masks it
in its logs.

1. Enable OpenCode in **Settings → Providers** on the environment's machine. Its driver is off
until you do.
2. Give the instance these environment variables, the first two marked sensitive:

```sh
OPENCODE_MODELS_URL=https://<hub>.<tailnet>.ts.net:8317/opencode/<access key>
T3CODE_PROXY_API_KEY=<access key>
OPENCODE_AUTH_CONTENT={}
```

`OPENCODE_AUTH_CONTENT={}` hides OpenCode's own saved logins from this instance. OpenCode
1.18 fails to load its providers when a saved login names a provider the catalog lacks.

3. Pick a `t3code-proxy/<model>` model in the thread. The instance lists only the hub's models;
your own OpenCode setup outside T3 Code is unchanged.

Claude subscription accounts are meant for Claude Code (see the note at the top of this page).
OpenCode can reach them through this catalog, so exclude Claude models for it, or keep it to
API-key accounts, if that matters to you.

## Mistral (OpenCode and Vibe)

Mistral has two monthly budgets on one account: API usage and Vibe Code. Which one a request
Expand Down
4 changes: 2 additions & 2 deletions internal/api/middleware/request_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func shouldCaptureRequestBody(loggerEnabled bool, req *http.Request) bool {
func captureRequestInfo(c *gin.Context, captureBody bool) (*RequestInfo, error) {
// Capture URL with sensitive query parameters masked
maskedQuery := util.MaskSensitiveQuery(c.Request.URL.RawQuery)
url := c.Request.URL.Path
url := util.MaskSensitivePath(c.Request.URL.Path)
if maskedQuery != "" {
url += "?" + maskedQuery
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func decodeCapturedZstdRequestBodyWithLimit(raw []byte, limit int64) ([]byte, bo
// It skips management endpoints to avoid leaking secrets but allows
// all other routes, including module-provided ones, to honor request-log.
func shouldLogRequest(path string) bool {
if strings.HasPrefix(path, "/v0/management") || strings.HasPrefix(path, "/v8/management") || strings.HasPrefix(path, "/management") {
if strings.HasPrefix(path, "/v0/management") || strings.HasPrefix(path, "/v8/management") || strings.HasPrefix(path, "/management") || strings.HasPrefix(path, "/opencode/") {
return false
}

Expand Down
237 changes: 237 additions & 0 deletions internal/api/opencode_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package api

import (
"net/http"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/rune-oai/t3code-proxy/internal/registry"
)

// OpenCode reads its model catalog from OPENCODE_MODELS_URL + "/api.json" in the
// models.dev format and refreshes it every few minutes. Pointing that variable
// at https://<hub>/opencode/<access key> makes OpenCode list every model the
// hub serves, live, under one provider whose requests go back to the hub.
// OpenCode sends no credentials with that fetch, so the access key travels in
// the path; request logs mask it (see util.MaskSensitivePath).

const (
openCodeProviderID = "t3code-proxy"
// openCodeAPIKeyEnv is the variable OpenCode reads the hub access key from.
openCodeAPIKeyEnv = "T3CODE_PROXY_API_KEY"

openCodeNPMAnthropic = "@ai-sdk/anthropic"
openCodeNPMOpenAIResponses = "@ai-sdk/openai"
openCodeNPMOpenAICompatible = "@ai-sdk/openai-compatible"

openCodeDefaultContext = 200000
openCodeDefaultOutput = 32000
)

func (s *Server) setupOpenCodeRoutes() {
s.engine.GET("/opencode/:key/api.json", openCodePathKeyToHeader, AuthMiddleware(s.accessManager), s.openCodeCatalogHandler)
}

// openCodePathKeyToHeader hands the access key from the path to the normal
// access check, which reads it from the Authorization header.
func openCodePathKeyToHeader(c *gin.Context) {
if key := strings.TrimSpace(c.Param("key")); key != "" {
c.Request.Header.Set("Authorization", "Bearer "+key)
}
c.Next()
}

func (s *Server) openCodeCatalogHandler(c *gin.Context) {
c.Header("Cache-Control", "no-store")
c.JSON(http.StatusOK, buildOpenCodeCatalog(requestBaseURL(c.Request)+"/v1", registry.GetGlobalRegistry().RegisteredModels()))
}

// requestBaseURL is the scheme and host the client used, honoring the
// X-Forwarded-* headers a TLS front (Tailscale serve) adds.
func requestBaseURL(r *http.Request) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if proto := strings.TrimSpace(strings.Split(r.Header.Get("X-Forwarded-Proto"), ",")[0]); proto == "https" || proto == "http" {
scheme = proto
}
host := r.Host
if forwarded := strings.TrimSpace(strings.Split(r.Header.Get("X-Forwarded-Host"), ",")[0]); forwarded != "" {
host = forwarded
}
return scheme + "://" + host
}

type openCodeProvider struct {
ID string `json:"id"`
Name string `json:"name"`
Env []string `json:"env"`
NPM string `json:"npm"`
API string `json:"api"`
Models map[string]openCodeModel `json:"models"`
}

type openCodeModel struct {
ID string `json:"id"`
Name string `json:"name"`
Family string `json:"family,omitempty"`
ReleaseDate string `json:"release_date"`
Attachment bool `json:"attachment"`
Reasoning bool `json:"reasoning"`
Temperature bool `json:"temperature"`
ToolCall bool `json:"tool_call"`
ReasoningOptions []openCodeReasoningOpt `json:"reasoning_options,omitempty"`
Limit openCodeLimit `json:"limit"`
Modalities openCodeModalities `json:"modalities"`
Provider openCodeModelProviderAPI `json:"provider"`
}

type openCodeReasoningOpt struct {
Type string `json:"type"`
Values []string `json:"values,omitempty"`
}

type openCodeLimit struct {
Context int `json:"context"`
Output int `json:"output"`
}

type openCodeModalities struct {
Input []string `json:"input"`
Output []string `json:"output"`
}

type openCodeModelProviderAPI struct {
NPM string `json:"npm"`
API string `json:"api"`
}

// buildOpenCodeCatalog renders the hub's text models as one models.dev
// provider. Each model names the API shape that serves it best: Claude models
// the Anthropic Messages API, Codex models the Responses API, and the rest
// OpenAI chat completions.
func buildOpenCodeCatalog(apiBase string, models []registry.RegisteredModel) map[string]openCodeProvider {
provider := openCodeProvider{
ID: openCodeProviderID,
Name: "t3code-proxy",
Env: []string{openCodeAPIKeyEnv},
NPM: openCodeNPMOpenAICompatible,
API: apiBase,
Models: make(map[string]openCodeModel, len(models)),
}
for _, model := range models {
info := model.Info
if info == nil || strings.TrimSpace(info.ID) == "" || !isOpenCodeTextModel(info) {
continue
}
provider.Models[info.ID] = openCodeModelFor(info, model.Providers, apiBase)
}
return map[string]openCodeProvider{openCodeProviderID: provider}
}

func openCodeModelFor(info *registry.ModelInfo, providers []string, apiBase string) openCodeModel {
name := strings.TrimSpace(info.DisplayName)
if name == "" {
name = info.ID
}
input := openCodeInputModalities(info.SupportedInputModalities)
reasoning := info.Thinking != nil || info.UserDefined
model := openCodeModel{
ID: info.ID,
Name: name,
Family: registry.ModelFamily(info.ID),
ReleaseDate: openCodeReleaseDate(info.Created),
Attachment: len(input) > 1,
Reasoning: reasoning,
Temperature: true,
ToolCall: true,
Limit: openCodeLimit{Context: firstPositive(info.ContextLength, info.MaxContextLength, info.InputTokenLimit, openCodeDefaultContext), Output: firstPositive(info.MaxCompletionTokens, info.OutputTokenLimit, openCodeDefaultOutput)},
Modalities: openCodeModalities{Input: input, Output: []string{"text"}},
Provider: openCodeModelProviderAPI{NPM: openCodeNPMFor(info.ID, providers), API: apiBase},
}
if info.Thinking != nil && len(info.Thinking.Levels) > 0 {
model.ReasoningOptions = []openCodeReasoningOpt{{Type: "effort", Values: append([]string(nil), info.Thinking.Levels...)}}
}
return model
}

func openCodeNPMFor(modelID string, providers []string) string {
has := func(name string) bool {
for _, provider := range providers {
if strings.EqualFold(provider, name) {
return true
}
}
return false
}
switch {
case has("claude") || registry.ModelFamily(modelID) == "claude":
return openCodeNPMAnthropic
case has("codex"):
return openCodeNPMOpenAIResponses
default:
return openCodeNPMOpenAICompatible
}
}

// isOpenCodeTextModel keeps chat models and drops image, video and speech ones,
// which OpenCode cannot drive.
func isOpenCodeTextModel(info *registry.ModelInfo) bool {
if info.Type == registry.OpenAIImageModelType {
return false
}
if len(info.SupportedOutputModalities) > 0 {
text := false
for _, modality := range info.SupportedOutputModalities {
text = text || strings.EqualFold(modality, "text")
}
if !text {
return false
}
}
base := strings.ToLower(info.ID)
if slash := strings.LastIndexByte(base, '/'); slash >= 0 {
base = base[slash+1:]
}
for _, marker := range []string{"gpt-image", "imagine", "imagen", "veo-", "-tts", "tts-"} {
if strings.Contains(base, marker) {
return false
}
}
return base != "grok-tts"
}

func openCodeInputModalities(raw []string) []string {
out := []string{"text"}
for _, modality := range raw {
switch m := strings.ToLower(strings.TrimSpace(modality)); m {
case "image", "audio", "video", "pdf":
found := false
for _, existing := range out {
found = found || existing == m
}
if !found {
out = append(out, m)
}
}
}
return out
}

func openCodeReleaseDate(created int64) string {
if created <= 0 {
return ""
}
return time.Unix(created, 0).UTC().Format("2006-01-02")
}

func firstPositive(values ...int) int {
for _, value := range values {
if value > 0 {
return value
}
}
return 0
}
Loading
Loading