on main help sign in
refactor: simplify some code by using `cmp.Or` (#2253)
699c8d7Andrey Nering
internal/agent/agent.go+1−4
@@ -849,10 +849,7 @@ func (a *sessionAgent) generateTitle(ctx context.Context, sessionID string, user
849 title = thinkTagRegex.ReplaceAllString(title, "")
849 title = thinkTagRegex.ReplaceAllString(title, "")
850
850
851 title = strings.TrimSpace(title)
851 title = strings.TrimSpace(title)
852− if title == "" {
853− slog.Debug("Empty title; using fallback")
854− title = defaultSessionName
852+ title = cmp.Or(title, defaultSessionName)
855− }
856
853
857 // Calculate usage and cost.
854 // Calculate usage and cost.
858 var openrouterCost *float64
855 var openrouterCost *float64
internal/agent/tools/glob.go+2−4
@@ -2,6 +2,7 @@ package tools
2
2
3import (
3import (
4 "bytes"
4 "bytes"
5+ "cmp"
5 "context"
6 "context"
6 _ "embed"
7 _ "embed"
7 "fmt"
8 "fmt"
@@ -39,10 +40,7 @@ func NewGlobTool(workingDir string) fantasy.AgentTool {
39 return fantasy.NewTextErrorResponse("pattern is required"), nil
40 return fantasy.NewTextErrorResponse("pattern is required"), nil
40 }
41 }
41
42
42− searchPath := params.Path
43+ searchPath := cmp.Or(params.Path, workingDir)
43− if searchPath == "" {
44− searchPath = workingDir
45− }
46
44
47 files, truncated, err := globFiles(ctx, params.Pattern, searchPath, 100)
45 files, truncated, err := globFiles(ctx, params.Pattern, searchPath, 100)
48 if err != nil {
46 if err != nil {
internal/agent/tools/grep.go+2−4
@@ -3,6 +3,7 @@ package tools
3import (
3import (
4 "bufio"
4 "bufio"
5 "bytes"
5 "bytes"
6+ "cmp"
6 "context"
7 "context"
7 _ "embed"
8 _ "embed"
8 "encoding/json"
9 "encoding/json"
@@ -115,10 +116,7 @@ func NewGrepTool(workingDir string, config config.ToolGrep) fantasy.AgentTool {
115 searchPattern = escapeRegexPattern(params.Pattern)
116 searchPattern = escapeRegexPattern(params.Pattern)
116 }
117 }
117
118
118− searchPath := params.Path
119+ searchPath := cmp.Or(params.Path, workingDir)
119− if searchPath == "" {
120− searchPath = workingDir
121− }
122
120
123 searchCtx, cancel := context.WithTimeout(ctx, config.GetTimeout())
121 searchCtx, cancel := context.WithTimeout(ctx, config.GetTimeout())
124 defer cancel()
122 defer cancel()
internal/agent/tools/list_mcp_resources.go+2−7
@@ -1,6 +1,7 @@
1package tools
1package tools
2
2
3import (
3import (
4+ "cmp"
4 "context"
5 "context"
5 _ "embed"
6 _ "embed"
6 "fmt"
7 "fmt"
@@ -74,13 +75,7 @@ func NewListMCPResourcesTool(cfg *config.Config, permissions permission.Service)
74 if resource == nil {
75 if resource == nil {
75 continue
76 continue
76 }
77 }
77− title := resource.Title
78+ title := cmp.Or(resource.Title, resource.Name, resource.URI)
78− if title == "" {
79− title = resource.Name
80− }
81− if title == "" {
82− title = resource.URI
83− }
84 line := fmt.Sprintf("- %s", title)
79 line := fmt.Sprintf("- %s", title)
85 if resource.URI != "" {
80 if resource.URI != "" {
86 line = fmt.Sprintf("%s (%s)", line, resource.URI)
81 line = fmt.Sprintf("%s (%s)", line, resource.URI)
internal/config/load.go+4−12
@@ -278,13 +278,9 @@ func (c *Config) configureProviders(env env.Env, resolver VariableResolver, know
278
278
279 // Make sure the provider ID is set
279 // Make sure the provider ID is set
280 providerConfig.ID = id
280 providerConfig.ID = id
281− if providerConfig.Name == "" {
282− providerConfig.Name = id // Use ID as name if not set
281+ providerConfig.Name = cmp.Or(providerConfig.Name, id) // Use ID as name if not set
283− }
284 // default to OpenAI if not set
282 // default to OpenAI if not set
285− if providerConfig.Type == "" {
286− providerConfig.Type = catwalk.TypeOpenAICompat
283+ providerConfig.Type = cmp.Or(providerConfig.Type, catwalk.TypeOpenAICompat)
287− }
288 if !slices.Contains(catwalk.KnownProviderTypes(), providerConfig.Type) && providerConfig.Type != hyper.Name {
284 if !slices.Contains(catwalk.KnownProviderTypes(), providerConfig.Type) && providerConfig.Type != hyper.Name {
289 slog.Warn("Skipping custom provider due to unsupported provider type", "provider", id)
285 slog.Warn("Skipping custom provider due to unsupported provider type", "provider", id)
290 c.Providers.Del(id)
286 c.Providers.Del(id)
@@ -412,9 +408,7 @@ func (c *Config) setDefaults(workingDir, dataDir string) {
412 c.Options.Attribution.TrailerStyle = TrailerStyleAssistedBy
408 c.Options.Attribution.TrailerStyle = TrailerStyleAssistedBy
413 }
409 }
414 }
410 }
415− if c.Options.InitializeAs == "" {
416− c.Options.InitializeAs = defaultInitializeAs
411+ c.Options.InitializeAs = cmp.Or(c.Options.InitializeAs, defaultInitializeAs)
417− }
418}
412}
419
413
420// applyLSPDefaults applies default values from powernap to LSP configurations
414// applyLSPDefaults applies default values from powernap to LSP configurations
@@ -445,9 +439,7 @@ func (c *Config) applyLSPDefaults() {
445 if len(cfg.RootMarkers) == 0 {
439 if len(cfg.RootMarkers) == 0 {
446 cfg.RootMarkers = base.RootMarkers
440 cfg.RootMarkers = base.RootMarkers
447 }
441 }
448− if cfg.Command == "" {
449− cfg.Command = base.Command
442+ cfg.Command = cmp.Or(cfg.Command, base.Command)
450− }
451 if len(cfg.Args) == 0 {
443 if len(cfg.Args) == 0 {
452 cfg.Args = base.Args
444 cfg.Args = base.Args
453 }
445 }
internal/ui/dialog/arguments.go+2−4
@@ -1,6 +1,7 @@
1package dialog
1package dialog
2
2
3import (
3import (
4+ "cmp"
4 "strings"
5 "strings"
5
6
6 "charm.land/bubbles/v2/help"
7 "charm.land/bubbles/v2/help"
@@ -311,10 +312,7 @@ func (a *Arguments) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
311 // Use standard header
312 // Use standard header
312 titleStyle := s.Dialog.Title
313 titleStyle := s.Dialog.Title
313
314
314− titleText := a.title
315+ titleText := cmp.Or(a.title, "Arguments")
315− if titleText == "" {
316− titleText = "Arguments"
317− }
318
316
319 header := common.DialogTitle(s, titleText, width, s.Primary, s.Secondary)
317 header := common.DialogTitle(s, titleText, width, s.Primary, s.Secondary)
320
318
internal/ui/dialog/models.go+2−7
@@ -445,18 +445,13 @@ func (m *Models) setProviderItems() error {
445 }
445 }
446 continue
446 continue
447 }
447 }
448− if model.Name == "" {
449− model.Name = model.ID
448+ model.Name = cmp.Or(model.Name, model.ID)
450− }
451 displayProvider.Models = append(displayProvider.Models, model)
449 displayProvider.Models = append(displayProvider.Models, model)
452 modelIndex[model.ID] = len(displayProvider.Models) - 1
450 modelIndex[model.ID] = len(displayProvider.Models) - 1
453 }
451 }
454 }
452 }
455
453
456− name := displayProvider.Name
454+ name := cmp.Or(displayProvider.Name, providerID)
457− if name == "" {
458− name = providerID
459− }
460
455
461 group := NewModelGroup(t, name, providerConfigured)
456 group := NewModelGroup(t, name, providerConfigured)
462 for _, model := range displayProvider.Models {
457 for _, model := range displayProvider.Models {
internal/ui/model/ui.go+3−8
@@ -2,6 +2,7 @@ package model
2
2
3import (
3import (
4 "bytes"
4 "bytes"
5+ "cmp"
5 "context"
6 "context"
6 "errors"
7 "errors"
7 "fmt"
8 "fmt"
@@ -1393,10 +1394,7 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
1393 case dialog.ActionRunMCPPrompt:
1394 case dialog.ActionRunMCPPrompt:
1394 if len(msg.Arguments) > 0 && msg.Args == nil {
1395 if len(msg.Arguments) > 0 && msg.Args == nil {
1395 m.dialog.CloseFrontDialog()
1396 m.dialog.CloseFrontDialog()
1396− title := msg.Title
1397− if title == "" {
1398− title = "MCP Prompt Arguments"
1397+ title := cmp.Or(msg.Title, "MCP Prompt Arguments")
1399− }
1400 argsDialog := dialog.NewArguments(
1398 argsDialog := dialog.NewArguments(
1401 m.com,
1399 m.com,
1402 title,
1400 title,
@@ -2563,10 +2561,7 @@ func (m *UI) insertFileCompletion(path string) tea.Cmd {
2563// insertMCPResourceCompletion inserts the selected resource into the textarea,
2561// insertMCPResourceCompletion inserts the selected resource into the textarea,
2564// replacing the @query, and adds the resource as an attachment.
2562// replacing the @query, and adds the resource as an attachment.
2565func (m *UI) insertMCPResourceCompletion(item completions.ResourceCompletionValue) tea.Cmd {
2563func (m *UI) insertMCPResourceCompletion(item completions.ResourceCompletionValue) tea.Cmd {
2566− displayText := item.Title
2564+ displayText := cmp.Or(item.Title, item.URI)
2567− if displayText == "" {
2568− displayText = item.URI
2569− }
2570
2565
2571 if !m.insertCompletionText(displayText) {
2566 if !m.insertCompletionText(displayText) {
2572 return nil
2567 return nil