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.
222 lines
7.9 KiB
Go
222 lines
7.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// chatEfforter is the optional thinking-level surface of a chat harness.
|
|
type chatEfforter interface {
|
|
// Efforts reports the levels the row's model accepts (session.Model is
|
|
// already the saved next-turn model) and the harness default, from what
|
|
// the adapter knows without starting a process. None: no control.
|
|
Efforts(ctx context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error)
|
|
// SetEffort hands a level to the harness for the next turn; "" restores
|
|
// its default. A running turn keeps the level it started with.
|
|
SetEffort(ctx context.Context, sessionID, effort string) error
|
|
}
|
|
|
|
// chatEffortRegistry stores each Codex or Claude chat's level. OpenCode keeps
|
|
// the level natively as the session model's variant.
|
|
type chatEffortRegistry interface {
|
|
SetChatEffort(ctx context.Context, id, effort string) error
|
|
ChatEffort(ctx context.Context, id string) (string, error)
|
|
}
|
|
|
|
var (
|
|
errChatEffortsUnsupported = errors.New("this chat harness does not support thinking levels")
|
|
errChatEffortsNone = errors.New("this chat's model has no thinking levels")
|
|
)
|
|
|
|
// storedEffort is a model backend's saved level, "" when none.
|
|
func (m *chatManager) storedEffort(ctx context.Context, id string) string {
|
|
registry, ok := m.registry.(chatEffortRegistry)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
effort, err := registry.ChatEffort(ctx, id)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return effort
|
|
}
|
|
|
|
// withEffort adds the row model's levels and the current level. A level the
|
|
// model does not accept is reported as the default rather than as chosen.
|
|
func (m *chatManager) withEffort(ctx context.Context, session protocol.ChatSession) protocol.ChatSession {
|
|
efforter, ok := m.adapter(session.Backend).(chatEfforter)
|
|
if !ok {
|
|
session.Effort, session.Efforts, session.DefaultEffort = "", nil, ""
|
|
return session
|
|
}
|
|
efforts, fallback, err := efforter.Efforts(ctx, session)
|
|
if err != nil || len(efforts) == 0 {
|
|
session.Effort, session.Efforts, session.DefaultEffort = "", nil, ""
|
|
return session
|
|
}
|
|
// A stored choice wins; otherwise the harness's own reported level (a
|
|
// Codex thread's reasoningEffort, an OpenCode variant) is current.
|
|
if isModelBackend(session.Backend) {
|
|
if stored := m.storedEffort(ctx, session.ID); stored != "" {
|
|
session.Effort = stored
|
|
}
|
|
}
|
|
if !protocol.HasChatEffort(efforts, session.Effort) {
|
|
session.Effort = ""
|
|
}
|
|
if !protocol.HasChatEffort(efforts, fallback) {
|
|
fallback = ""
|
|
}
|
|
session.Efforts, session.DefaultEffort = efforts, fallback
|
|
return session
|
|
}
|
|
|
|
// EffortState is the chat's current level with its model's levels.
|
|
func (m *chatManager) EffortState(ctx context.Context, sessionID string) (protocol.ChatEffortRequest, error) {
|
|
session, found, err := m.Session(ctx, sessionID)
|
|
if err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
if !found {
|
|
return protocol.ChatEffortRequest{}, errors.New("unknown chat session " + sessionID)
|
|
}
|
|
return effortState(session), nil
|
|
}
|
|
|
|
func effortState(session protocol.ChatSession) protocol.ChatEffortRequest {
|
|
return protocol.ChatEffortRequest{SessionID: session.ID, Effort: session.Effort, Efforts: session.Efforts, DefaultEffort: session.DefaultEffort}
|
|
}
|
|
|
|
// SetEffort validates a level against the chat's current model, hands it to
|
|
// the harness and, for Codex and Claude, stores it so it survives a restart
|
|
// and is re-applied before every later turn.
|
|
func (m *chatManager) SetEffort(ctx context.Context, sessionID, effort string) (protocol.ChatEffortRequest, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
efforter, ok := adapter.(chatEfforter)
|
|
if !ok {
|
|
return protocol.ChatEffortRequest{}, errChatEffortsUnsupported
|
|
}
|
|
state, err := m.EffortState(ctx, sessionID)
|
|
if err == nil && len(state.Efforts) == 0 {
|
|
// A harness whose catalog arrives with its process (Claude Code)
|
|
// loads it on an explicit choice, never just to list a row.
|
|
if modeler, ok := adapter.(chatModeler); ok {
|
|
if _, err := modeler.Models(ctx, sessionID); err == nil {
|
|
state, err = m.EffortState(ctx, sessionID)
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
if len(state.Efforts) == 0 {
|
|
return protocol.ChatEffortRequest{}, errChatEffortsNone
|
|
}
|
|
if effort != "" && !protocol.HasChatEffort(state.Efforts, effort) {
|
|
return protocol.ChatEffortRequest{}, errors.New("thinking level " + effort + " is not offered by this chat's model")
|
|
}
|
|
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
|
registry, ok := m.registry.(chatEffortRegistry)
|
|
if !ok {
|
|
return protocol.ChatEffortRequest{}, errors.New("durable chat thinking levels are not configured")
|
|
}
|
|
if err := efforter.SetEffort(ctx, sessionID, effort); err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
if err := registry.SetChatEffort(ctx, sessionID, effort); err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
} else if err := efforter.SetEffort(ctx, sessionID, effort); err != nil {
|
|
return protocol.ChatEffortRequest{}, err
|
|
}
|
|
state.Effort = effort
|
|
return state, nil
|
|
}
|
|
|
|
// applyStoredEffort hands a Codex or Claude chat's stored level to the harness
|
|
// before a turn; it covers a restart that lost the adapter's in-memory level.
|
|
func (m *chatManager) applyStoredEffort(ctx context.Context, adapter chatBroker, sessionID string) error {
|
|
efforter, ok := adapter.(chatEfforter)
|
|
if !ok || !isModelBackend(m.backendFor(ctx, sessionID)) {
|
|
return nil
|
|
}
|
|
effort := m.storedEffort(ctx, sessionID)
|
|
if effort == "" {
|
|
return nil
|
|
}
|
|
return efforter.SetEffort(ctx, sessionID, effort)
|
|
}
|
|
|
|
// reconcileEffort runs after a saved model switch: a stored level the new
|
|
// model does not accept becomes that model's default (or the harness's own),
|
|
// so the next turn never sends a level the model refuses.
|
|
func (m *chatManager) reconcileEffort(ctx context.Context, sessionID string) error {
|
|
stored := m.storedEffort(ctx, sessionID)
|
|
if stored == "" {
|
|
return nil
|
|
}
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
efforter, ok := adapter.(chatEfforter)
|
|
registry, stores := m.registry.(chatEffortRegistry)
|
|
if !ok || !stores {
|
|
return nil
|
|
}
|
|
session, found, err := m.Session(ctx, sessionID)
|
|
if err != nil || !found || protocol.HasChatEffort(session.Efforts, stored) {
|
|
return err
|
|
}
|
|
next := session.DefaultEffort
|
|
if err := efforter.SetEffort(ctx, sessionID, next); err != nil {
|
|
return err
|
|
}
|
|
return registry.SetChatEffort(ctx, sessionID, next)
|
|
}
|
|
|
|
// handleChatEffort sets one chat's thinking level and echoes the stored level
|
|
// with the model's levels.
|
|
func handleChatEffort(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
|
body := request.EffortRequest
|
|
if body == nil || !validChatSessionID(body.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
|
return
|
|
}
|
|
setter, ok := broker.(interface {
|
|
SetEffort(context.Context, string, string) (protocol.ChatEffortRequest, error)
|
|
})
|
|
if !ok {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatEffortsUnsupported.Error()})
|
|
return
|
|
}
|
|
reply, err := setter.SetEffort(ctx, body.SessionID, strings.TrimSpace(body.Effort))
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatEffortSelect, ID: request.ID, EffortRequest: &reply})
|
|
}
|
|
|
|
// chatModelsEffort is the chat's current level for a chat_models reply, nil
|
|
// when the broker cannot report one.
|
|
func chatModelsEffort(ctx context.Context, broker chatBroker, sessionID string) *protocol.ChatEffortRequest {
|
|
reporter, ok := broker.(interface {
|
|
EffortState(context.Context, string) (protocol.ChatEffortRequest, error)
|
|
})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
state, err := reporter.EffortState(ctx, sessionID)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return &state
|
|
}
|