Skip to content
Closed
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
30 changes: 30 additions & 0 deletions internal/engine/codex_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package engine

import (
"strings"

"agentflow/internal/domain"
"agentflow/internal/runtime"
)

// codexCatalogModels lists the models a Codex run may use, so that Codex
// does not fall back to its built-in metadata for a model it has never heard
// of. The fallback understates the context window of a model served by
// another gateway.
//
// The personal catalog is the source of truth: it is what the user chose in
// 个人配置. A run without one keeps Codex's own catalog.
func (e *Engine) codexCatalogModels(user domain.User) []runtime.CodexModel {
if len(user.CodexModels) == 0 {
return nil
}
out := make([]runtime.CodexModel, 0, len(user.CodexModels))
for _, model := range user.CodexModels {
id := strings.TrimSpace(model.ID)
if id == "" {
continue
}
out = append(out, runtime.CodexModel{ID: id, Name: strings.TrimSpace(model.Name)})
}
return out
}
29 changes: 29 additions & 0 deletions internal/engine/codex_catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package engine

import (
"testing"

"agentflow/internal/domain"
)

func TestCodexCatalogModelsUsesThePersonalCatalog(t *testing.T) {
e := &Engine{}
user := domain.User{CodexModels: []domain.CLIModel{
{ID: " domestic-model ", Name: " Domestic Model "},
{ID: "", Name: "no id"},
}}
got := e.codexCatalogModels(user)
if len(got) != 1 {
t.Fatalf("models = %+v", got)
}
if got[0].ID != "domestic-model" || got[0].Name != "Domestic Model" {
t.Fatalf("model = %+v", got[0])
}
}

func TestCodexCatalogModelsWithoutAPersonalCatalog(t *testing.T) {
e := &Engine{}
if got := e.codexCatalogModels(domain.User{}); got != nil {
t.Fatalf("a user without a catalog keeps the CLI one: %+v", got)
}
}
1 change: 1 addition & 0 deletions internal/engine/run_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (e *Engine) resolveRunCredentials(run domain.Run) runtime.RunCredentials {
credentials.GitUserEmail = identity.email
if user, ok := e.repo.User(userID); ok {
credentials.Codex = runtime.ProviderCredential{APIKey: user.CodexAPIKey, BaseURL: user.CodexBaseURL}
credentials.CodexModels = e.codexCatalogModels(user)
credentials.ClaudeCode = runtime.ProviderCredential{APIKey: user.ClaudeCodeAPIKey, BaseURL: user.ClaudeCodeBaseURL}
credentials.AgentCLI = runtime.AgentCLIUserCredentials(user)
}
Expand Down
1 change: 1 addition & 0 deletions internal/httpapi/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.U
case assistantEngineCodex:
if !req.RunnerLogin {
engineOpts.User = runtime.ProviderCredential{APIKey: user.CodexAPIKey, BaseURL: user.CodexBaseURL}
engineOpts.Models = codexCatalogModels(ctx, user, engineModel)
}
inv, err = prepareAssistantCodexInvocation(engineOpts)
default:
Expand Down
47 changes: 47 additions & 0 deletions internal/httpapi/assistant_codex_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package httpapi

import (
"context"
"strings"

"agentflow/internal/domain"
"agentflow/internal/runtime"
)

// codexCatalogModels lists the models the Codex catalog describes: the
// personal catalog first, then the gateway's published models. The model of
// the current turn is always included, even when the gateway does not publish
// it, because Codex would fall back for it otherwise.
//
// Codex falls back to built-in metadata for a model it does not know, and
// that metadata understates the context window of a model served by another
// gateway. The catalog gives the CLI the real numbers.
func codexCatalogModels(ctx context.Context, user domain.User, model string) []runtime.CodexModel {
seen := map[string]bool{}
var out []runtime.CodexModel
add := func(id, name string) {
id = strings.TrimSpace(id)
if id == "" || seen[id] {
return
}
seen[id] = true
out = append(out, runtime.CodexModel{
ID: id,
Name: strings.TrimSpace(name),
Efforts: assistantModelEffortsForBaseURL(id, user.CodexModels, ""),
})
}
for _, personal := range user.CodexModels {
add(personal.ID, personal.Name)
}
// The gateway's own list is the source of truth for what it serves.
if baseURL := strings.TrimSpace(user.CodexBaseURL); baseURL != "" && strings.TrimSpace(user.CodexAPIKey) != "" {
for _, discovered := range cachedAssistantEngineDiscovery(ctx, assistantEngineProtocol(user, assistantEngineCodex), user.CodexAPIKey, baseURL) {
add(discovered.ID, discovered.Name)
}
}
if engine, id := assistantModelEngine(model); engine == assistantEngineCodex {
add(id, id)
}
return out
}
77 changes: 77 additions & 0 deletions internal/httpapi/assistant_codex_catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package httpapi

import (
"context"
"testing"

"agentflow/internal/domain"
"agentflow/internal/runtime"
)

func TestCodexCatalogModelsCombinesThePersonalAndThePublishedCatalog(t *testing.T) {
restore := assistantEngineDiscover
assistantEngineDiscover = func(ctx context.Context, anthropic bool, apiKey, baseURL string) ([]assistantDiscoveredModel, error) {
return []assistantDiscoveredModel{
{ID: "published-model", Name: "Published Model"},
{ID: "domestic-model", Name: "Published Domestic"},
}, nil
}
t.Cleanup(func() { assistantEngineDiscover = restore })

user := domain.User{
CodexAPIKey: "turn-key",
CodexBaseURL: "https://gateway.example/v1",
CodexModels: []domain.CLIModel{{ID: "domestic-model", Name: "Domestic Model"}},
}
got := codexCatalogModels(context.Background(), user, "")
if len(got) != 2 {
t.Fatalf("models = %+v", got)
}
// The personal catalog comes first: it is what the user chose.
if got[0].ID != "domestic-model" || got[0].Name != "Domestic Model" {
t.Fatalf("the personal catalog must come first: %+v", got[0])
}
if got[1].ID != "published-model" {
t.Fatalf("models = %+v", got)
}
}

func TestCodexCatalogModelsIncludesTheModelOfTheTurn(t *testing.T) {
restore := assistantEngineDiscover
assistantEngineDiscover = func(ctx context.Context, anthropic bool, apiKey, baseURL string) ([]assistantDiscoveredModel, error) {
return nil, nil
}
t.Cleanup(func() { assistantEngineDiscover = restore })
// The discovery cache is shared by the process, so the entry of an earlier
// test would answer this one.
assistantDiscoveryCache.Delete(assistantDiscoveryKey("", "turn-key", "https://gateway.example/v1"))

user := domain.User{CodexAPIKey: "turn-key", CodexBaseURL: "https://gateway.example/v1"}
// The gateway does not publish this model, but the turn runs on it, so the
// catalog has to describe it or Codex falls back for it.
got := codexCatalogModels(context.Background(), user, "codex:turn-model")
if len(got) != 1 {
t.Fatalf("models = %+v", got)
}
if got[0].ID != "turn-model" {
t.Fatalf("model = %+v", got[0])
}
}

func TestCodexCatalogModelsWithoutAGateway(t *testing.T) {
// A user on the official endpoint keeps the CLI's own catalog.
user := domain.User{CodexModels: []domain.CLIModel{{ID: "personal-model"}}}
got := codexCatalogModels(context.Background(), user, "")
if len(got) != 1 || got[0].ID != "personal-model" {
t.Fatalf("models = %+v", got)
}
}

func TestCodexCatalogModelsSkipsAnEmptyID(t *testing.T) {
user := domain.User{CodexModels: []domain.CLIModel{{ID: " ", Name: "blank"}}}
if got := codexCatalogModels(context.Background(), user, ""); len(got) != 0 {
t.Fatalf("a model without an ID must be skipped: %+v", got)
}
}

var _ = runtime.CodexModel{}
4 changes: 4 additions & 0 deletions internal/httpapi/assistant_engine_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type assistantEngineInvocationOpts struct {
ReviewOnly bool
ReportOnly bool
User runtime.ProviderCredential
// Models is the model catalog written for a CLI that falls back to its
// own metadata for an unknown model. Codex uses it so a model served by
// another gateway keeps its real context window.
Models []runtime.CodexModel
}

func assistantEngineConfigDir(engine string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/httpapi/assistant_engine_codex.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func prepareAssistantCodexInvocation(o assistantEngineInvocationOpts) (assistant
server.request("initialize", map[string]any{"clientInfo": map[string]string{"name": "agentflow", "title": "AgentFlow", "version": "1"}})
return assistantCLIInvocation{
command: runtime.CommandSpec{Argv: argv, Provider: "codex", CredentialBindings: []runtime.CredentialBinding{runtime.BindCodex}, Stdin: stdin},
credentials: runtime.RunCredentials{Codex: o.User},
credentials: runtime.RunCredentials{Codex: o.User, CodexModels: o.Models},
sessionRef: thread,
translate: server.translate,
finish: func(ok bool) {
Expand Down
Loading
Loading