Files
alan 06001697c8 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.
2026-11-09 20:07:41 +00:00

114 lines
3.4 KiB
Go

package claude
import (
"context"
"errors"
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// effortLevels turns an initialize model's effort support into levels. A
// model without supportsEffort has none, whatever else it lists.
func effortLevels(supported bool, levels []string) []protocol.ChatEffort {
if !supported {
return nil
}
var efforts []protocol.ChatEffort
for _, level := range levels {
level = strings.TrimSpace(level)
if level != "" && !protocol.HasChatEffort(efforts, level) {
efforts = append(efforts, protocol.ChatEffort{ID: level, Name: protocol.ChatEffortName(level)})
}
}
return efforts
}
// rememberCatalog keeps the newest initialize model list for listing rows.
func (c *Chat) rememberCatalog(models []protocol.ChatModel) {
if len(models) == 0 {
return
}
c.catalogMu.Lock()
c.catalog = append([]protocol.ChatModel(nil), models...)
c.catalogMu.Unlock()
}
// Efforts reports the levels Claude Code's initialize catalog lists for the
// row's model: its selection, the model a reply reported, or the CLI default
// ("default"). Before any CLI of this server initialized there is no catalog
// and so no levels; Claude Code reports no default level in its catalog.
func (c *Chat) Efforts(_ context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) {
var models []protocol.ChatModel
if s := c.session(session.ID); s != nil {
s.mu.Lock()
models = append(models, s.models...)
s.mu.Unlock()
}
if len(models) == 0 {
c.catalogMu.Lock()
models = append(models, c.catalog...)
c.catalogMu.Unlock()
}
model := strings.TrimSpace(session.Model)
if model == "" {
model = "default"
}
for _, match := range []func(protocol.ChatModel) bool{
func(entry protocol.ChatModel) bool { return entry.ID == model },
func(entry protocol.ChatModel) bool { return entry.Resolved != "" && entry.Resolved == model },
} {
for _, entry := range models {
if match(entry) {
return append([]protocol.ChatEffort(nil), entry.Efforts...), "", nil
}
}
}
return nil, "", nil
}
// SetEffort records the session's thinking level. It reaches the CLI at the
// next turn's start (applyEffort), so a running turn keeps its level.
func (c *Chat) SetEffort(_ context.Context, id, effort string) error {
s := c.session(id)
if s == nil {
meta, ok := transcriptSession(id)
if !ok {
return errors.New("unknown Claude session " + id)
}
s = c.sessionFor(id, meta.Directory)
}
s.mu.Lock()
s.effort = effort
s.mu.Unlock()
return nil
}
// applyEffort tells the CLI the session's level before a turn when it was
// told another (or none) with apply_flag_settings, the control request the
// CLI documents for session-only effort changes. A null level restores the
// CLI's own default. The setting is session-only: it never edits a settings
// file.
func (s *session) applyEffort(ctx context.Context, proc *process) error {
s.mu.Lock()
requested, current := s.effort, proc.effort
s.mu.Unlock()
if requested == current {
return nil
}
var level any
if requested != "" {
level = requested
}
setCtx, cancel := context.WithTimeout(ctx, controlRequestTimeout)
defer cancel()
if _, err := proc.request(setCtx, "apply_flag_settings", map[string]any{"settings": map[string]any{"effortLevel": level}}); err != nil {
return fmt.Errorf("set Claude thinking level: %w", err)
}
s.mu.Lock()
proc.effort = requested
s.mu.Unlock()
return nil
}