package acp import ( "errors" "slices" "albedo/cli/internal/app" "albedo/cli/internal/daemon" ) func modelOptions(models app.ACPModels) []map[string]any { configuration := []map[string]any{} if len(models.Available) == 0 { return configuration } options := make([]map[string]string, 0, len(models.Available)) for _, model := range models.Available { options = append(options, map[string]string{"value": model, "name": model}) } return append(configuration, map[string]any{"id": "model", "name": "Model", "category": "model", "type": "select", "currentValue": models.Current, "options": options}) } func (a *adapter) setConfigOption(message request) { var params struct { SessionID string `json:"sessionId"` ConfigID string `json:"configId"` Value string `json:"value"` } if decodeParams(message.Params, ¶ms) != nil || params.ConfigID != "model" || params.Value == "" { a.respond(message.ID, nil, problem(-32602, "invalid_params", "Choose an available model")) return } a.mu.Lock() current := a.sessions[params.SessionID] a.mu.Unlock() if current == nil { a.respond(message.ID, nil, problem(-32002, "session_not_found", "Session is not attached")) return } current.mu.Lock() if current.uncertain || current.ctx.Err() != nil { current.mu.Unlock() a.respond(message.ID, nil, problem(-32603, "observation_uncertain", "Resume the session before changing its model")) return } if current.buffering || current.active != nil || current.turn != "" || len(current.inherited) > 0 { current.mu.Unlock() a.respond(message.ID, nil, problem(-32602, "busy", "Wait for the current turn before changing its model")) return } if !slices.Contains(current.models.Available, params.Value) { current.mu.Unlock() a.respond(message.ID, nil, problem(-32602, "invalid_params", "Choose an available model")) return } current.configuring = true current.mu.Unlock() models, err := a.backend.SetModel(current.ctx, current.id, params.Value) current.mu.Lock() defer current.mu.Unlock() current.configuring = false if err != nil { if _, uncertain := errors.AsType[*daemon.UncertainOutcomeError](err); uncertain { current.lose("observation_uncertain", "Model selection may have changed; resume the session") } a.respond(message.ID, nil, err) return } if models.Available == nil { models = current.models.WithCurrent(models.Current) } current.models = models a.respond(message.ID, map[string]any{"configOptions": modelOptions(models)}, nil) }