06001697c8
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.
87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
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
|
|
}
|