diff --git a/internal/agent/omp.go b/internal/agent/omp.go index bdb546c8f..f1ba8a1fb 100644 --- a/internal/agent/omp.go +++ b/internal/agent/omp.go @@ -16,6 +16,8 @@ import ( "strings" "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/provider" ) // ompEfforts are the thinking levels omp knows. @@ -146,6 +148,8 @@ func omp(home string) *Agent { type ompModel struct { ID string `yaml:"id"` Name string `yaml:"name,omitempty"` + API string `yaml:"api,omitempty"` + BaseURL string `yaml:"baseUrl,omitempty"` Reasoning bool `yaml:"reasoning"` Thinking *ompThinking `yaml:"thinking,omitempty"` Context int `yaml:"contextWindow,omitempty"` @@ -166,11 +170,32 @@ type ompProviderEntry struct { } // ompProvider is magpie's entry in models.yml. The thinking efforts are the -// levels omp offers for the model; it sends them as reasoning_effort. +// levels omp offers for the model; on Chat it sends them as +// reasoning_effort. +// +// As for Pi, each model is asked on the API its provider speaks natively, so +// the gateway relays what omp sent as it is instead of translating Chat: a +// Claude over Chat lost its thinking's signatures between tool turns, as +// Chat has no place for them. One served on OpenAI's Responses API goes to +// baseUrl/responses; one on Anthropic's Messages API to the gateway's +// /v1/messages (omp adds the /v1). That one thinks adaptively when it takes +// nothing else, else on a budget: omp's anthropic-budget-effort would also +// send output_config.effort, which Sonnet 4.5 and Haiku 4.5 refuse. func ompProvider() ompProviderEntry { ms := []ompModel{} for _, m := range magpieModels("omp") { e := ompModel{ID: m.ID, Name: m.Name, Context: m.Context, MaxTokens: m.Output} + mode := "effort" + switch { + case slices.Contains(m.APIs, string(provider.Responses)): + e.API = "openai-responses" + case slices.Contains(m.APIs, string(provider.Anthropic)): + e.API, e.BaseURL = "anthropic-messages", gateway.URL() + mode = "budget" + if gateway.AdaptiveThinking(m.ID) { + mode = "anthropic-adaptive" + } + } // Without input omp takes it from a bundled model its fuzzy id match // finds, else text only; a model the source never answered for is // left to that guess. @@ -182,13 +207,16 @@ func ompProvider() ompProviderEntry { } var efforts []string for _, x := range ompEfforts { // in omp's order - if slices.Contains(m.Efforts, x) { + // a model's efforts stop at xhigh (omp 16.3.5 turns the whole + // file away over a max): a model whose top is max offers xhigh, + // which the gateway fits to max + if x == "xhigh" && slices.Contains(m.Efforts, "max") || x != "max" && slices.Contains(m.Efforts, x) { efforts = append(efforts, x) } } if len(efforts) > 0 { e.Reasoning = true - e.Thinking = &ompThinking{Mode: "effort", Efforts: efforts} + e.Thinking = &ompThinking{Mode: mode, Efforts: efforts} } ms = append(ms, e) } diff --git a/internal/agent/omp_api_test.go b/internal/agent/omp_api_test.go new file mode 100644 index 000000000..a726b3fe0 --- /dev/null +++ b/internal/agent/omp_api_test.go @@ -0,0 +1,92 @@ +package agent + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/gateway" + "github.com/yetone/magpie/internal/provider" + "gopkg.in/yaml.v3" +) + +// omp asks each model on the API its provider serves it on, as Pi does: a +// Claude over Chat lost its thinking's signatures between tool turns. A +// Messages-only provider's models go on anthropic-messages at the gateway, +// a Responses-only one's on openai-responses, and a Chat provider's and a +// group's stay on the provider's openai-completions, keyless. +func TestOmpModelsAskedOnTheirNativeAPI(t *testing.T) { + home := syncHome(t) + for _, p := range []provider.Provider{ + {ID: "resp", Name: "Resp", Key: "k", Responses: "http://127.0.0.1:1/v1", Models: []string{"grok-5", "gpt-5.5"}}, + {ID: "openai", Name: "OpenAI", Key: "k", Chat: "https://api.openai.com/v1", Responses: "https://api.openai.com/v1", Models: []string{"gpt-5.5"}}, + {ID: "anth", Name: "Anth", Key: "k", Anthropic: "http://127.0.0.1:1", Models: []string{"claude-sonnet-5", "claude-sonnet-4-5", "kimi-k3"}}, + } { + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + } + var live []catalog.Model + for _, id := range []string{"claude-sonnet-5", "claude-sonnet-4-5", "kimi-k3"} { + live = append(live, catalog.Model{ID: id, Efforts: []string{"low", "high", "max"}}) + } + if err := catalog.SaveLive("anth", "http://127.0.0.1:1", live); err != nil { + t.Fatal(err) + } + if err := omp(home).Field("model").Set("magpie/anth/claude-sonnet-5"); err != nil { + t.Fatal(err) + } + b, err := os.ReadFile(filepath.Join(home, ".omp", "agent", "models.yml")) + if err != nil { + t.Fatal(err) + } + var file map[string]any + if err := yaml.Unmarshal(b, &file); err != nil { + t.Fatal(err) + } + pm := file["providers"].(map[string]any)["magpie"].(map[string]any) + if pm["api"] != "openai-completions" || pm["baseUrl"] != gatewayV1() || pm["auth"] != "none" { + t.Fatalf("provider: %v", pm) + } + entries := map[string]map[string]any{} + for _, raw := range pm["models"].([]any) { + m := raw.(map[string]any) + entries[m["id"].(string)] = m + } + for id, want := range map[string]any{ + "resp/grok-5": "openai-responses", + "relay/glm-4.6": nil, + "anth/claude-sonnet-5": "anthropic-messages", + "anth/claude-sonnet-4-5": "anthropic-messages", + "anth/kimi-k3": "anthropic-messages", + "group/auto-gpt-5-5": nil, + } { + e, ok := entries[id] + if !ok { + t.Errorf("%s not listed: %s", id, b) + } else if e["api"] != want { + t.Errorf("%s: api %v, want %v", id, e["api"], want) + } + } + // omp posts to baseUrl + /v1/messages; the Claude that refuses a + // thinking budget thinks adaptively, the others on a budget alone + for id, mode := range map[string]string{"anth/claude-sonnet-5": "anthropic-adaptive", "anth/claude-sonnet-4-5": "budget", "anth/kimi-k3": "budget"} { + e := entries[id] + if e["baseUrl"] != gateway.URL() { + t.Errorf("%s: baseUrl %v, want %s", id, e["baseUrl"], gateway.URL()) + } + // and its efforts stop at xhigh, the gateway's way to max: omp + // 16.3.5 turns the whole models.yml away over a max + th, _ := e["thinking"].(map[string]any) + if th["mode"] != mode || fmt.Sprint(th["efforts"]) != "[low high xhigh]" { + t.Errorf("%s: thinking %v, want mode %s, efforts [low high xhigh]", id, e["thinking"], mode) + } + } + for _, id := range []string{"resp/grok-5", "relay/glm-4.6"} { + if _, ok := entries[id]["baseUrl"]; ok { + t.Errorf("%s keeps the provider's baseUrl: %v", id, entries[id]) + } + } +} diff --git a/internal/gateway/omp_messages_test.go b/internal/gateway/omp_messages_test.go new file mode 100644 index 000000000..2acd39cb1 --- /dev/null +++ b/internal/gateway/omp_messages_test.go @@ -0,0 +1,65 @@ +package gateway + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/tidwall/gjson" + + "github.com/yetone/magpie/internal/provider" +) + +// omp asks a Claude served on Anthropic's Messages API at /v1/messages +// (#329), with fields of its own: context_management keeping every thinking +// block, thinking.display, and output_config.effort beside adaptive +// thinking. They reach the provider as omp sent them, and so do the signed +// thinking of a tool turn going back and the signature of the one coming. +func TestOmpMessagesRelayedAsSent(t *testing.T) { + sig := "EqQBCkYIBxgCKkDsig==" + f := &fake{t: t, reply: "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\n" + + "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\"}}\n\n" + + "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"hm\"}}\n\n" + + "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"" + sig + "\"}}\n\n" + + "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n" + + "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":2}}\n\n" + + "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"} + setup(t, provider.Anthropic, f) + p, _ := provider.Find("fake") + p.Models = []string{"claude-sonnet-5", "claude-sonnet-4-5"} + provider.Save(*p) + + history := `[{"role":"user","content":[{"type":"text","text":"list files"}]},` + + `{"role":"assistant","content":[{"type":"thinking","thinking":"use ls","signature":"` + sig + `"},{"type":"tool_use","id":"toolu_1","name":"bash","input":{"command":"ls"}}]},` + + `{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"a.go"}]}]` + for _, c := range []struct{ model, thinking, extra string }{ + {"claude-sonnet-5", `{"type":"adaptive","display":"summarized"}`, `,"output_config":{"effort":"high"}`}, + {"claude-sonnet-4-5", `{"type":"enabled","budget_tokens":16384,"display":"summarized"}`, ``}, + } { + body := `{"model":"fake/` + c.model + `","max_tokens":32000,"stream":true,"system":[{"type":"text","text":"You are omp."}],"messages":` + history + + `,"thinking":` + c.thinking + c.extra + `,"context_management":{"edits":[{"type":"clear_thinking_20251015","keep":"all"}]}}` + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)) + req.Header.Set("User-Agent", "Anthropic/JS 0.71.2") + req.Header.Set("Anthropic-Beta", "context-management-2025-06-27,interleaved-thinking-2025-05-14") + New().Handler().ServeHTTP(rec, req) + if rec.Code != 200 { + t.Fatalf("%s: %d %s", c.model, rec.Code, rec.Body.String()) + } + got := string(f.got) + if f.path != "/v1/messages" || gjson.Get(got, "model").Str != c.model { + t.Errorf("%s: sent to %s as %s", c.model, f.path, gjson.Get(got, "model").Str) + } + for _, k := range []string{"thinking", "context_management", "output_config", "messages"} { + if want := gjson.Get(body, k).Raw; gjson.Get(got, k).Raw != want { + t.Errorf("%s: %s %s, want %s", c.model, k, gjson.Get(got, k).Raw, want) + } + } + if !strings.Contains(f.head.Get("Anthropic-Beta"), "context-management-2025-06-27") { + t.Errorf("%s: beta %q", c.model, f.head.Get("Anthropic-Beta")) + } + if !strings.Contains(rec.Body.String(), `"signature":"`+sig+`"`) { + t.Errorf("%s: signature lost on the way back:\n%s", c.model, rec.Body.String()) + } + } +}