package acp import ( "context" "errors" "slices" "strconv" "albedo/cli/internal/app" ) type settingID string const ( idModel settingID = "model" idEffort settingID = "effort" idWindow settingID = "contextWindow" idStrategy settingID = "compactionStrategy" ) const ( effortDefault = "default" strategyAuto = "auto" automaticWindow = "0" selectType = "select" ) var automaticChoice = choice{Value: automaticWindow, Name: "Automatic"} type settingCategory string const ( categoryModel settingCategory = "model" categoryModifier settingCategory = "model_config" categoryThinking settingCategory = "thought_level" ) type choice struct { Value string `json:"value"` Name string `json:"name"` } type option struct { ID settingID `json:"id"` Name string `json:"name"` Category settingCategory `json:"category"` Type string `json:"type"` Current string `json:"currentValue"` Choices []choice `json:"options"` } func newOption(id settingID, name string, category settingCategory, current string, choices []choice) option { return option{ID: id, Name: name, Category: category, Type: selectType, Current: current, Choices: choices} } func (o option) offered(value string) bool { return slices.ContainsFunc(o.Choices, func(candidate choice) bool { return candidate.Value == value }) } func addChoice(choices []choice, value string) []choice { if value == "" || slices.ContainsFunc(choices, func(candidate choice) bool { return candidate.Value == value }) { return choices } return append(choices, choice{Value: value, Name: value}) } func addWindowChoice(choices []choice, tokens int) []choice { value := strconv.Itoa(tokens) if slices.ContainsFunc(choices, func(candidate choice) bool { return candidate.Value == value }) { return choices } name := value switch { case tokens >= 950_000 && tokens <= 1_000_000: name = "1M" case tokens%1_000_000 == 0: name = strconv.Itoa(tokens/1_000_000) + "M" case tokens%1_000 == 0: name = strconv.Itoa(tokens/1_000) + "k" } return append(choices, choice{Value: value, Name: name}) } type setting interface { id() settingID offer(app.ACPSettings) (option, bool) write(context.Context, Backend, string, string) error applied(app.ACPSettings, string) app.ACPSettings invalidatesWindow() bool } func allSettings() []setting { return []setting{modelOption{}, effortOption{}, windowOption{}, strategyOption{}} } func settingFor(id settingID) (setting, bool) { for _, candidate := range allSettings() { if candidate.id() == id { return candidate, true } } return nil, false } func configOptions(settings app.ACPSettings) []option { options := []option{} for _, candidate := range allSettings() { if offered, ok := candidate.offer(settings); ok { options = append(options, offered) } } return options } type modelOption struct{} func (modelOption) id() settingID { return idModel } func (modelOption) offer(settings app.ACPSettings) (option, bool) { if len(settings.Models) == 0 { return option{}, false } choices := make([]choice, 0, len(settings.Models)) for _, model := range settings.Models { choices = append(choices, choice{Value: model, Name: model}) } // A session saved under a profile that is gone selects a model the // catalogue cannot list; it must still be one of the offered values. return newOption(idModel, "Model", categoryModel, settings.Model, addChoice(choices, settings.Model)), true } func (modelOption) write(ctx context.Context, backend Backend, session, value string) error { return backend.SetModel(ctx, session, value) } func (modelOption) applied(settings app.ACPSettings, value string) app.ACPSettings { settings.Model = value return settings } func (modelOption) invalidatesWindow() bool { return true } type effortOption struct{} func (effortOption) id() settingID { return idEffort } // offer lists the model's own levels after "Default", the choice that asks for // the model's default. A session with no saved level, or one saved as the // daemon's `inherit`, reads as "Default": picking it is then a no-op and leaves // the profile's choice in place. func (effortOption) offer(settings app.ACPSettings) (option, bool) { choices := []choice{{Value: effortDefault, Name: "Default"}} for _, effort := range settings.EffortLevels() { choices = addChoice(choices, effort) } current := settings.Effort if current == "" || current == app.EffortInherit { current = effortDefault } return newOption(idEffort, "Reasoning effort", categoryThinking, current, addChoice(choices, current)), true } func (effortOption) write(ctx context.Context, backend Backend, session, value string) error { return backend.SetEffort(ctx, session, value) } func (effortOption) applied(settings app.ACPSettings, value string) app.ACPSettings { settings.Effort = value return settings } func (effortOption) invalidatesWindow() bool { return false } type windowOption struct{} func (windowOption) id() settingID { return idWindow } func (windowOption) offer(settings app.ACPSettings) (option, bool) { current := 0 if settings.Window != nil { current = *settings.Window } windows := settings.WindowLevels() if len(windows) == 0 && current == 0 { return option{}, false } // Automatic is always offered: it is the only way back to the model's own // window once this session saved one. choices := []choice{automaticChoice} for _, tokens := range windows { if tokens > 0 { choices = addWindowChoice(choices, tokens) } } if current > 0 { choices = addWindowChoice(choices, current) } return newOption(idWindow, "Context window", categoryModifier, strconv.Itoa(current), choices), true } func (windowOption) write(ctx context.Context, backend Backend, session, value string) error { if value == automaticWindow { return backend.SetContextWindow(ctx, session, nil) } tokens, err := strconv.Atoi(value) if err != nil || tokens <= 0 { return errors.New("a context window must be a positive number of tokens") } return backend.SetContextWindow(ctx, session, &tokens) } func (windowOption) applied(settings app.ACPSettings, value string) app.ACPSettings { if value == automaticWindow { settings.Window = nil return settings } if tokens, err := strconv.Atoi(value); err == nil && tokens > 0 { settings.Window = &tokens } return settings } func (windowOption) invalidatesWindow() bool { return true } type strategyOption struct{} func (strategyOption) id() settingID { return idStrategy } func (strategyOption) offer(settings app.ACPSettings) (option, bool) { choices := []choice{{Value: strategyAuto, Name: "Automatic"}} for _, strategy := range settings.Strategies { choices = addChoice(choices, strategy) } current := settings.Strategy if current == "" { current = strategyAuto } return newOption(idStrategy, "Compaction strategy", categoryModifier, current, addChoice(choices, current)), true } func (strategyOption) write(ctx context.Context, backend Backend, session, value string) error { if value == strategyAuto { return backend.SetCompactionStrategy(ctx, session, nil) } return backend.SetCompactionStrategy(ctx, session, &value) } func (strategyOption) applied(settings app.ACPSettings, value string) app.ACPSettings { if value == strategyAuto { value = "" } settings.Strategy = value return settings } func (strategyOption) invalidatesWindow() bool { return false }