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.
135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package codex
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// catalogTTL bounds how long a model/list read answers rows and choices; the
|
|
// account's catalog changes rarely, and a session list must not issue one
|
|
// model/list per row.
|
|
const catalogTTL = time.Minute
|
|
|
|
// catalogModel is one visible model/list entry. Model.model, not the catalog
|
|
// row's opaque id, is the turn/start override.
|
|
type catalogModel struct {
|
|
ID string
|
|
Name string
|
|
Efforts []protocol.ChatEffort
|
|
DefaultEffort string
|
|
}
|
|
|
|
// Models uses the thread's actual provider and the account's paginated catalog.
|
|
func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, error) {
|
|
session, found, err := c.Session(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !found || session.ModelProvider == "" {
|
|
return nil, errors.New("Codex thread model provider is unavailable")
|
|
}
|
|
catalog, err := c.catalog(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
models := make([]protocol.ChatModel, 0, len(catalog))
|
|
for _, model := range catalog {
|
|
models = append(models, protocol.ChatModel{ID: model.ID, Provider: session.ModelProvider, Name: model.Name, Efforts: model.Efforts, DefaultEffort: model.DefaultEffort})
|
|
}
|
|
return models, nil
|
|
}
|
|
|
|
// catalog returns the visible model/list entries, read at most once a minute.
|
|
func (c *Chat) catalog(ctx context.Context) ([]catalogModel, error) {
|
|
c.mu.Lock()
|
|
cached, at := c.models, c.modelsAt
|
|
c.mu.Unlock()
|
|
if cached != nil && time.Since(at) < catalogTTL {
|
|
return cached, nil
|
|
}
|
|
models, err := c.readCatalog(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.mu.Lock()
|
|
c.models, c.modelsAt = models, time.Now()
|
|
c.mu.Unlock()
|
|
return models, nil
|
|
}
|
|
|
|
func (c *Chat) readCatalog(ctx context.Context) ([]catalogModel, error) {
|
|
models := []catalogModel{}
|
|
cursor := ""
|
|
seen := map[string]bool{}
|
|
for page := 0; page < 20; page++ {
|
|
params := map[string]any{"limit": 100, "includeHidden": false}
|
|
if cursor != "" {
|
|
params["cursor"] = cursor
|
|
}
|
|
raw, err := c.call(ctx, "model/list", params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var response struct {
|
|
Data *[]struct {
|
|
Model string `json:"model"`
|
|
DisplayName string `json:"displayName"`
|
|
Hidden bool `json:"hidden"`
|
|
DefaultReasoningEffort string `json:"defaultReasoningEffort"`
|
|
SupportedReasoningEfforts []struct {
|
|
ReasoningEffort string `json:"reasoningEffort"`
|
|
Description string `json:"description"`
|
|
} `json:"supportedReasoningEfforts"`
|
|
} `json:"data"`
|
|
NextCursor *string `json:"nextCursor"`
|
|
}
|
|
if err := json.Unmarshal(raw, &response); err != nil || response.Data == nil {
|
|
return nil, errors.New("invalid Codex model catalog")
|
|
}
|
|
for _, model := range *response.Data {
|
|
if model.Model == "" || model.Hidden {
|
|
continue
|
|
}
|
|
entry := catalogModel{ID: model.Model, Name: model.DisplayName}
|
|
for _, option := range model.SupportedReasoningEfforts {
|
|
if option.ReasoningEffort != "" && !protocol.HasChatEffort(entry.Efforts, option.ReasoningEffort) {
|
|
entry.Efforts = append(entry.Efforts, protocol.ChatEffort{ID: option.ReasoningEffort, Name: protocol.ChatEffortName(option.ReasoningEffort), Description: option.Description})
|
|
}
|
|
}
|
|
if protocol.HasChatEffort(entry.Efforts, model.DefaultReasoningEffort) {
|
|
entry.DefaultEffort = model.DefaultReasoningEffort
|
|
}
|
|
models = append(models, entry)
|
|
}
|
|
if response.NextCursor == nil || *response.NextCursor == "" {
|
|
return models, nil
|
|
}
|
|
cursor = *response.NextCursor
|
|
if seen[cursor] {
|
|
return nil, errors.New("Codex model catalog repeated its cursor")
|
|
}
|
|
seen[cursor] = true
|
|
}
|
|
return nil, errors.New("Codex model catalog exceeds pagination limit")
|
|
}
|
|
|
|
// SwitchModel validates a selection only. Pipelines persists it before replying
|
|
// and supplies it to the next explicit turn/start. Never use thread/resume here:
|
|
// resumption may start a queued goal and is not a safe settings operation.
|
|
func (c *Chat) SwitchModel(ctx context.Context, id, provider, model string) error {
|
|
models, err := c.Models(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, available := range models {
|
|
if available.ID == model && available.Provider == provider {
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("the selected Codex model is not available for this thread's provider")
|
|
}
|