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:
@@ -4,12 +4,26 @@ 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.
|
||||
// Model.model, not the catalog row's opaque id, is the turn/start override.
|
||||
func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, error) {
|
||||
session, found, err := c.Session(ctx, id)
|
||||
if err != nil {
|
||||
@@ -18,7 +32,37 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err
|
||||
if !found || session.ModelProvider == "" {
|
||||
return nil, errors.New("Codex thread model provider is unavailable")
|
||||
}
|
||||
models := []protocol.ChatModel{}
|
||||
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++ {
|
||||
@@ -32,9 +76,14 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err
|
||||
}
|
||||
var response struct {
|
||||
Data *[]struct {
|
||||
Model string `json:"model"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Hidden bool `json:"hidden"`
|
||||
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"`
|
||||
}
|
||||
@@ -42,9 +91,19 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err
|
||||
return nil, errors.New("invalid Codex model catalog")
|
||||
}
|
||||
for _, model := range *response.Data {
|
||||
if model.Model != "" && !model.Hidden {
|
||||
models = append(models, protocol.ChatModel{ID: model.Model, Provider: session.ModelProvider, Name: model.DisplayName})
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user