diff --git a/README.md b/README.md index 12c625b..28b992a 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ Harnesses read their configuration at startup. Restart `claude`, `codex`, `openc | [Claude Code](https://docs.requesty.ai/integrations/claude-code) | `claude` on `PATH` | `~/.claude/settings.json` | `ANTHROPIC_BASE_URL`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_MODEL` in the `env` block | | [Codex](https://docs.requesty.ai/integrations/openai-codex) | `codex` on `PATH` | `~/.codex/config.toml` | A `requesty` model provider on `.../v1`, the selected model, and command-backed auth through `requesty auth token` | | [OpenCode](https://docs.requesty.ai/integrations/opencode) | `opencode` on `PATH` | `~/.config/opencode/opencode.json` | A `requesty` provider on `.../v1` plus the model as `requesty/` | -| [Pi](https://docs.requesty.ai/integrations/pi) | `pi` on `PATH` | `~/.pi/agent/models.json` | A `requesty` provider using the native Anthropic Messages API | +| [Pi](https://docs.requesty.ai/integrations/pi) | `pi` on `PATH` | `~/.pi/agent/models.json` | A `requesty` provider using the native Anthropic Messages API, listing the selected model first and then every other model and managed policy the key can use | | [Hermes](https://docs.requesty.ai/integrations/hermes) | `hermes` on `PATH` | `~/.hermes/config.yaml` | A `requesty` entry in `custom_providers` and `model.default` | | [DeepSeek Harness](https://docs.requesty.ai/integrations/deepseek-harness) | `dsh` on `PATH` | `$DSH_HOME/settings.yaml`, `$DSH_HOME/.credentials.yaml` (`~/.dsh` by default) | A `requesty` provider on `.../v1` under `llm-pi-ai`, the selected model as `agent-default-model`, and the key stored as `REQUESTY_API_KEY` | diff --git a/internal/harnesses/pi.go b/internal/harnesses/pi.go index 55250dd..2d9bc60 100644 --- a/internal/harnesses/pi.go +++ b/internal/harnesses/pi.go @@ -43,13 +43,7 @@ type piProviderConfig struct { API string `json:"api"` APIKey string `json:"apiKey"` Headers map[string]string `json:"headers,omitempty"` - Models []piModelConfig `json:"models"` -} - -type piModelConfig struct { - ID string `json:"id"` - ContextWindow int `json:"contextWindow"` - MaxTokens int `json:"maxTokens"` + Models []piCatalogModel `json:"models"` } type PiHarness struct { @@ -379,6 +373,33 @@ func readPiCatalog(path string) piCatalog { return catalog } +// configuredPiModels is what Configure lists in models.json: the picked model +// first, then every other model and managed policy the profile can use, so +// plain `pi` can switch between them. When the list cannot be fetched, only +// the picked model is written. +func (p *PiHarness) configuredPiModels(opts ConfigureOptions) []piCatalogModel { + picked := piModelConfigFrom(opts) + + ctx, cancel := context.WithTimeout(context.Background(), piCatalogTimeout) + defer cancel() + + catalog, err := p.fetchCatalog(ctx) + if err != nil { + return []piCatalogModel{picked} + } + + models := []piCatalogModel{picked} + for _, model := range catalog.Models { + if model.ID == picked.ID { + models[0].Cost = model.Cost + continue + } + models = append(models, model) + } + + return models +} + func (p *PiHarness) Configure(opts ConfigureOptions) error { if opts.Overwrite { return p.configureOverwrite(opts) @@ -390,6 +411,12 @@ func (p *PiHarness) Configure(opts ConfigureOptions) error { func (p *PiHarness) configureMerge(opts ConfigureOptions) error { modelsPath := p.modelsPath() + listed := p.configuredPiModels(opts) + configuredModels := make([]any, 0, len(listed)) + for _, model := range listed { + configuredModels = append(configuredModels, model) + } + models, err := mergeOrCreateJSONConfigFile(modelsPath, map[string]any{ "providers": map[string]any{ piProvider: map[string]any{ @@ -398,9 +425,7 @@ func (p *PiHarness) configureMerge(opts ConfigureOptions) error { "api": piAPI, "apiKey": p.config.APIKey, "headers": attributionHeaders(piTitle), - "models": []any{ - piModelConfigFrom(opts), - }, + "models": configuredModels, }, }, }) @@ -438,9 +463,7 @@ func (p *PiHarness) configureOverwrite(opts ConfigureOptions) error { API: piAPI, APIKey: p.config.APIKey, Headers: attributionHeaders(piTitle), - Models: []piModelConfig{ - piModelConfigFrom(opts), - }, + Models: p.configuredPiModels(opts), }, }, } @@ -464,18 +487,12 @@ func (p *PiHarness) configureOverwrite(opts ConfigureOptions) error { // piModelConfigFrom describes the configured model with its capacities, since // Pi caps a model without them at 128k tokens. -func piModelConfigFrom(opts ConfigureOptions) piModelConfig { - entry := piCatalogModelFrom(client.Model{ +func piModelConfigFrom(opts ConfigureOptions) piCatalogModel { + return piCatalogModelFrom(client.Model{ ID: opts.Model, ContextWindow: opts.ContextWindow, MaxOutputTokens: opts.MaxOutputTokens, }) - - return piModelConfig{ - ID: entry.ID, - ContextWindow: entry.ContextWindow, - MaxTokens: entry.MaxTokens, - } } func (p *PiHarness) launchEnv(opts LaunchOptions, catalogPath string) (map[string]string, error) { diff --git a/internal/harnesses/pi_test.go b/internal/harnesses/pi_test.go index 7465aa4..fa6c7b1 100644 --- a/internal/harnesses/pi_test.go +++ b/internal/harnesses/pi_test.go @@ -1,18 +1,44 @@ package harnesses import ( + "encoding/json" + "net/http" + "net/http/httptest" "os" "path/filepath" "testing" + "github.com/requestyai/cli/internal/client" "github.com/requestyai/cli/internal/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +// newPiRouter is a router whose model lists hold the given models and +// policies. With neither, every list fails, as when offline. +func newPiRouter(t *testing.T, models, policies []client.Model) string { + t.Helper() + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + listed := models + if r.URL.Path == "/v1/models/managed" { + listed = policies + } + if listed == nil { + w.WriteHeader(http.StatusUnauthorized) + return + } + require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"data": listed})) + })) + t.Cleanup(server.Close) + + return server.URL +} + func TestPiHarnessRoundTrip(t *testing.T) { + routerURL := newPiRouter(t, nil, nil) config := config.Config{ - RouterBaseURL: "https://router.requesty.ai", + RouterBaseURL: routerURL, APIKey: "my-api-key", } @@ -57,7 +83,7 @@ func TestPiHarnessRoundTrip(t *testing.T) { "other": {"name": "Other provider"}, "requesty": { "name": "Requesty", - "baseUrl": "https://router.requesty.ai", + "baseUrl": "`+routerURL+`", "api": "anthropic-messages", "apiKey": "my-api-key", "headers": { @@ -66,7 +92,15 @@ func TestPiHarnessRoundTrip(t *testing.T) { "X-Requesty-Client-Version": "dev" }, "models": [ - {"id": "anthropic/claude-fable-5", "contextWindow": 1000000, "maxTokens": 64000} + { + "id": "anthropic/claude-fable-5", + "name": "anthropic/claude-fable-5", + "reasoning": true, + "input": ["text", "image"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 1000000, + "maxTokens": 64000 + } ] } } @@ -96,8 +130,9 @@ func TestPiHarnessLaunchEnvPassesHeaders(t *testing.T) { } func TestPiHarnessConfigureCreatesMissingConfig(t *testing.T) { + routerURL := newPiRouter(t, nil, nil) config := config.Config{ - RouterBaseURL: "https://router.requesty.ai", + RouterBaseURL: routerURL, APIKey: "my-api-key", } configDir := t.TempDir() @@ -115,7 +150,7 @@ func TestPiHarnessConfigureCreatesMissingConfig(t *testing.T) { "providers": { "requesty": { "name": "Requesty", - "baseUrl": "https://router.requesty.ai", + "baseUrl": "`+routerURL+`", "api": "anthropic-messages", "apiKey": "my-api-key", "headers": { @@ -124,7 +159,15 @@ func TestPiHarnessConfigureCreatesMissingConfig(t *testing.T) { "X-Requesty-Client-Version": "dev" }, "models": [ - {"id": "anthropic/claude-fable-5", "contextWindow": 200000, "maxTokens": 8192} + { + "id": "anthropic/claude-fable-5", + "name": "anthropic/claude-fable-5", + "reasoning": true, + "input": ["text", "image"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 200000, + "maxTokens": 8192 + } ] } } @@ -138,3 +181,47 @@ func TestPiHarnessConfigureCreatesMissingConfig(t *testing.T) { "defaultThinkingLevel": "medium" }`, string(settings)) } + +func TestPiHarnessConfigureListsEveryModelTheProfileCanUse(t *testing.T) { + models := []client.Model{ + {ID: "openai/gpt-6", ContextWindow: 400_000, MaxOutputTokens: 128_000}, + {ID: "anthropic/claude-fable-5", ContextWindow: 1_000_000, MaxOutputTokens: 64_000, InputPrice: 0.000003}, + } + policies := []client.Model{ + {ID: "claude-fable-5"}, + } + routerURL := newPiRouter(t, models, policies) + config := config.Config{ + RouterBaseURL: routerURL, + APIKey: "my-api-key", + } + configDir := t.TempDir() + modelsPath := filepath.Join(configDir, "models.json") + require.NoError(t, os.WriteFile(modelsPath, []byte(`{ + "providers": { + "requesty": { + "models": [ + {"id": "openai/gpt-5"} + ] + } + } + }`), 0o600)) + harness := newPiHarness(config, configDir) + + require.NoError(t, harness.Configure(ConfigureOptions{ + Model: "anthropic/claude-fable-5", + ContextWindow: 1_000_000, + MaxOutputTokens: 64_000, + })) + + written, err := os.ReadFile(modelsPath) + require.NoError(t, err) + var got piModels + require.NoError(t, json.Unmarshal(written, &got)) + wantModels := []piCatalogModel{ + piCatalogModelFrom(models[1]), + piCatalogModelFrom(policies[0]), + piCatalogModelFrom(models[0]), + } + assert.Equal(t, wantModels, got.Providers["requesty"].Models) +}