Add per-chat thinking levels for Codex, Claude Code and OpenCode
Codex: model/list supportedReasoningEfforts per model, turn/start effort, thread reasoningEffort as the current level. Claude Code: initialize supportedEffortLevels, apply_flag_settings effortLevel at the next turn start. OpenCode v2: model variants via the session model endpoint. chat_effort sets a level; rows and chat_models carry the levels; Codex and Claude levels persist in operator_sessions.chat_effort and are re-applied before every turn.
This commit is contained in:
@@ -49,6 +49,10 @@ type Chat struct {
|
||||
agents map[string]string
|
||||
modelMu sync.Mutex
|
||||
sessionModels map[string]sessionModelCache
|
||||
// catalog caches /api/model for thinking levels (efforts.go).
|
||||
catalogMu sync.Mutex
|
||||
catalog []protocol.ChatModel
|
||||
catalogAt time.Time
|
||||
}
|
||||
|
||||
// NewChat brokers chat for one owned OpenCode endpoint. The connection must be
|
||||
@@ -182,6 +186,9 @@ func (c *Chat) Models(ctx context.Context, _ string) ([]protocol.ChatModel, erro
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Status string `json:"status"`
|
||||
Variants []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"variants"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &envelope); err != nil {
|
||||
@@ -204,7 +211,13 @@ func (c *Chat) Models(ctx context.Context, _ string) ([]protocol.ChatModel, erro
|
||||
if name == "" {
|
||||
name = id
|
||||
}
|
||||
models = append(models, protocol.ChatModel{ID: id, Provider: provider, Name: name})
|
||||
model := protocol.ChatModel{ID: id, Provider: provider, Name: name}
|
||||
for _, variant := range item.Variants {
|
||||
if variant.ID != "" && !protocol.HasChatEffort(model.Efforts, variant.ID) {
|
||||
model.Efforts = append(model.Efforts, protocol.ChatEffort{ID: variant.ID, Name: protocol.ChatEffortName(variant.ID)})
|
||||
}
|
||||
}
|
||||
models = append(models, model)
|
||||
}
|
||||
sort.Slice(models, func(i, j int) bool {
|
||||
if models[i].Provider != models[j].Provider {
|
||||
@@ -1686,6 +1699,8 @@ type nativeChatFile struct {
|
||||
type nativeChatModel struct {
|
||||
ID string `json:"id"`
|
||||
ProviderID string `json:"providerID"`
|
||||
// Variant is the session model's chosen variant (its thinking level).
|
||||
Variant string `json:"variant"`
|
||||
}
|
||||
|
||||
// nativeChatPart is one content element shared by both message envelopes. Raw
|
||||
@@ -1908,6 +1923,7 @@ func chatSessionFields(record nativeChatSession) protocol.ChatSession {
|
||||
Agent: record.Agent,
|
||||
Model: record.Model.ID,
|
||||
ModelProvider: record.Model.ProviderID,
|
||||
Effort: record.Model.Variant,
|
||||
Directory: record.Location.Directory,
|
||||
ParentID: record.ParentID,
|
||||
CreatedAt: record.Time.Created,
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// catalogTTL bounds how long one /api/model read answers listed rows, so a
|
||||
// session list never reads the catalog once per row.
|
||||
const catalogTTL = time.Minute
|
||||
|
||||
// modelCatalog is Models, cached for catalogTTL.
|
||||
func (c *Chat) modelCatalog(ctx context.Context) ([]protocol.ChatModel, error) {
|
||||
c.catalogMu.Lock()
|
||||
cached, at := c.catalog, c.catalogAt
|
||||
c.catalogMu.Unlock()
|
||||
if cached != nil && time.Since(at) < catalogTTL {
|
||||
return cached, nil
|
||||
}
|
||||
models, err := c.Models(ctx, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.catalogMu.Lock()
|
||||
c.catalog, c.catalogAt = models, time.Now()
|
||||
c.catalogMu.Unlock()
|
||||
return models, nil
|
||||
}
|
||||
|
||||
// Efforts reports the variants the endpoint lists for the session's model.
|
||||
// OpenCode stores the chosen variant on the session's model itself (the row's
|
||||
// Effort); with none chosen it uses the model's base settings, so there is no
|
||||
// separate default level. The v1 API has no variants.
|
||||
func (c *Chat) Efforts(ctx context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) {
|
||||
if c.conn == nil || c.conn.Backend == "v1" || session.Model == "" || session.ModelProvider == "" {
|
||||
return nil, "", nil
|
||||
}
|
||||
models, err := c.modelCatalog(ctx)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
for _, model := range models {
|
||||
if model.ID == session.Model && model.Provider == session.ModelProvider {
|
||||
return append([]protocol.ChatEffort(nil), model.Efforts...), "", nil
|
||||
}
|
||||
}
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
// SetEffort selects the variant on the session's current model with the
|
||||
// documented session model endpoint, which OpenCode persists with the session
|
||||
// and applies from the next turn. "" selects the model without a variant.
|
||||
func (c *Chat) SetEffort(ctx context.Context, id, effort string) error {
|
||||
if !sessionID.MatchString(id) {
|
||||
return fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
if c.conn == nil || c.conn.Backend == "v1" {
|
||||
return errors.New("this OpenCode endpoint has no model variants")
|
||||
}
|
||||
session, found, err := c.Session(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found || session.Model == "" || session.ModelProvider == "" {
|
||||
return errors.New("the OpenCode session has no model to set a thinking level on")
|
||||
}
|
||||
ref := map[string]string{"id": session.Model, "providerID": session.ModelProvider}
|
||||
if effort = strings.TrimSpace(effort); effort != "" {
|
||||
ref["variant"] = effort
|
||||
}
|
||||
if evidence, err := c.call(ctx, "post", "/api/session/"+id+"/model", map[string]any{"model": ref}); err != nil {
|
||||
if reason := strings.TrimSpace(string(evidence)); reason != "" {
|
||||
if len(reason) > 300 {
|
||||
reason = reason[:300]
|
||||
}
|
||||
return fmt.Errorf("%w: %s", err, reason)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
const effortSessionList = `{"data":[{"id":"ses_1","title":"Chat","model":{"id":"thinker","providerID":"prov","variant":"high"},"location":{"directory":"/fixture"},"time":{"created":1,"updated":2}}]}`
|
||||
|
||||
const effortModelList = `{"data":[
|
||||
{"modelID":"thinker","providerID":"prov","name":"Thinker","enabled":true,"status":"active","variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"high","settings":{"reasoningEffort":"high"}},{"id":"xhigh"}]},
|
||||
{"modelID":"plain","providerID":"prov","name":"Plain","enabled":true,"status":"active","variants":[]}
|
||||
]}`
|
||||
|
||||
func TestOpenCodeEffortsAreModelVariants(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
modelReads := 0
|
||||
var posted []map[string]string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/session":
|
||||
_, _ = w.Write([]byte(effortSessionList))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/model":
|
||||
modelReads++
|
||||
_, _ = w.Write([]byte(effortModelList))
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/model":
|
||||
var body struct {
|
||||
Model map[string]string `json:"model"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
posted = append(posted, body.Model)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
ctx := context.Background()
|
||||
session, found, err := chat.Session(ctx, "ses_1")
|
||||
if err != nil || !found || session.Effort != "high" || session.Model != "thinker" {
|
||||
t.Fatalf("session = %+v, %v", session, err)
|
||||
}
|
||||
efforts, fallback, err := chat.Efforts(ctx, session)
|
||||
if err != nil || fallback != "" || len(efforts) != 3 || efforts[2] != (protocol.ChatEffort{ID: "xhigh", Name: "Extra high"}) {
|
||||
t.Fatalf("efforts = %+v %q, %v", efforts, fallback, err)
|
||||
}
|
||||
for _, row := range []protocol.ChatSession{{ID: "ses_1", Model: "plain", ModelProvider: "prov"}, {ID: "ses_1", Model: "thinker", ModelProvider: "other"}, {ID: "ses_1"}} {
|
||||
if none, _, err := chat.Efforts(ctx, row); err != nil || len(none) != 0 {
|
||||
t.Fatalf("row %+v offers %+v, %v", row, none, err)
|
||||
}
|
||||
}
|
||||
models, err := chat.Models(ctx, "ses_1")
|
||||
if err != nil || len(models) != 2 || len(models[1].Efforts) != 3 || len(models[0].Efforts) != 0 {
|
||||
t.Fatalf("models = %+v, %v", models, err)
|
||||
}
|
||||
if err := chat.SetEffort(ctx, "ses_1", "low"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SetEffort(ctx, "ses_1", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
// Rows share one cached catalog read; Models itself still reads live.
|
||||
if modelReads != 2 {
|
||||
t.Fatalf("model reads = %d", modelReads)
|
||||
}
|
||||
if len(posted) != 2 || posted[0]["variant"] != "low" || posted[0]["id"] != "thinker" || posted[0]["providerID"] != "prov" {
|
||||
t.Fatalf("posted = %+v", posted)
|
||||
}
|
||||
if _, has := posted[1]["variant"]; has || posted[1]["id"] != "thinker" {
|
||||
t.Fatalf("cleared level posted %+v", posted[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenCodeV1HasNoEfforts(t *testing.T) {
|
||||
chat := NewChat(&Connection{Backend: "v1"})
|
||||
if efforts, _, err := chat.Efforts(context.Background(), protocol.ChatSession{ID: "ses_1", Model: "m", ModelProvider: "p"}); err != nil || len(efforts) != 0 {
|
||||
t.Fatalf("v1 efforts = %+v, %v", efforts, err)
|
||||
}
|
||||
if err := chat.SetEffort(context.Background(), "ses_1", "high"); err == nil {
|
||||
t.Fatal("v1 accepted a thinking level")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user