feat(chat): persist safe Codex next-turn model selection
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
}
|
||||
if !found || session.ModelProvider == "" {
|
||||
return nil, errors.New("Codex thread model provider is unavailable")
|
||||
}
|
||||
models := []protocol.ChatModel{}
|
||||
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"`
|
||||
} `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 {
|
||||
models = append(models, protocol.ChatModel{ID: model.Model, Provider: session.ModelProvider, Name: model.DisplayName})
|
||||
}
|
||||
}
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user