package claude import ( "context" "errors" "fmt" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" ) // effortLevels turns an initialize model's effort support into levels. A // model without supportsEffort has none, whatever else it lists. func effortLevels(supported bool, levels []string) []protocol.ChatEffort { if !supported { return nil } var efforts []protocol.ChatEffort for _, level := range levels { level = strings.TrimSpace(level) if level != "" && !protocol.HasChatEffort(efforts, level) { efforts = append(efforts, protocol.ChatEffort{ID: level, Name: protocol.ChatEffortName(level)}) } } return efforts } // rememberCatalog keeps the newest initialize model list for listing rows. func (c *Chat) rememberCatalog(models []protocol.ChatModel) { if len(models) == 0 { return } c.catalogMu.Lock() c.catalog = append([]protocol.ChatModel(nil), models...) c.catalogMu.Unlock() } // Efforts reports the levels Claude Code's initialize catalog lists for the // row's model: its selection, the model a reply reported, or the CLI default // ("default"). Before any CLI of this server initialized there is no catalog // and so no levels; Claude Code reports no default level in its catalog. func (c *Chat) Efforts(_ context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) { var models []protocol.ChatModel if s := c.session(session.ID); s != nil { s.mu.Lock() models = append(models, s.models...) s.mu.Unlock() } if len(models) == 0 { c.catalogMu.Lock() models = append(models, c.catalog...) c.catalogMu.Unlock() } model := strings.TrimSpace(session.Model) if model == "" { model = "default" } for _, match := range []func(protocol.ChatModel) bool{ func(entry protocol.ChatModel) bool { return entry.ID == model }, func(entry protocol.ChatModel) bool { return entry.Resolved != "" && entry.Resolved == model }, } { for _, entry := range models { if match(entry) { return append([]protocol.ChatEffort(nil), entry.Efforts...), "", nil } } } return nil, "", nil } // SetEffort records the session's thinking level. It reaches the CLI at the // next turn's start (applyEffort), so a running turn keeps its level. func (c *Chat) SetEffort(_ context.Context, id, effort string) error { s := c.session(id) if s == nil { meta, ok := transcriptSession(id) if !ok { return errors.New("unknown Claude session " + id) } s = c.sessionFor(id, meta.Directory) } s.mu.Lock() s.effort = effort s.mu.Unlock() return nil } // applyEffort tells the CLI the session's level before a turn when it was // told another (or none) with apply_flag_settings, the control request the // CLI documents for session-only effort changes. A null level restores the // CLI's own default. The setting is session-only: it never edits a settings // file. func (s *session) applyEffort(ctx context.Context, proc *process) error { s.mu.Lock() requested, current := s.effort, proc.effort s.mu.Unlock() if requested == current { return nil } var level any if requested != "" { level = requested } setCtx, cancel := context.WithTimeout(ctx, controlRequestTimeout) defer cancel() if _, err := proc.request(setCtx, "apply_flag_settings", map[string]any{"settings": map[string]any{"effortLevel": level}}); err != nil { return fmt.Errorf("set Claude thinking level: %w", err) } s.mu.Lock() proc.effort = requested s.mu.Unlock() return nil }