Skip to content

Commit 1648a41

Browse files
authored
Merge pull request #1162 from dgageot/simplifications
Misc simplifications
2 parents 560c10c + d18d8f7 commit 1648a41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+172
-513
lines changed

cmd/root/alias.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ func runAliasListCommand(cmd *cobra.Command, args []string) error {
139139
// Find max name width for alignment (using display width for proper Unicode handling)
140140
maxLen := 0
141141
for _, name := range names {
142-
if w := runewidth.StringWidth(name); w > maxLen {
143-
maxLen = w
144-
}
142+
maxLen = max(maxLen, runewidth.StringWidth(name))
145143
}
146144

147145
for _, name := range names {

cmd/root/catalog.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package root
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -98,10 +99,7 @@ func fetchHubRepos(ctx context.Context, org string) ([]hubRepo, error) {
9899
_ = resp.Body.Close()
99100

100101
for _, r := range page.Results {
101-
ns := r.Namespace
102-
if ns == "" {
103-
ns = org
104-
}
102+
ns := cmp.Or(r.Namespace, org)
105103
repos = append(repos, hubRepo{
106104
Namespace: ns,
107105
Name: r.Name,

cmd/root/root.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package root
22

33
import (
4+
"cmp"
45
"context"
56
"errors"
67
"fmt"
@@ -169,10 +170,7 @@ func (f *rootFlags) setupLogging() error {
169170
return nil
170171
}
171172

172-
path := strings.TrimSpace(f.logFilePath)
173-
if path == "" {
174-
path = filepath.Join(paths.GetDataDir(), "cagent.debug.log")
175-
}
173+
path := cmp.Or(strings.TrimSpace(f.logFilePath), filepath.Join(paths.GetDataDir(), "cagent.debug.log"))
176174

177175
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
178176
return err

pkg/a2a/adapter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package a2a
22

33
import (
4+
"cmp"
45
"fmt"
56
"iter"
67
"log/slog"
@@ -23,10 +24,7 @@ func newCAgentAdapter(t *team.Team, agentName string) (agent.Agent, error) {
2324
return nil, fmt.Errorf("failed to get agent %s: %w", agentName, err)
2425
}
2526

26-
desc := a.Description()
27-
if desc == "" {
28-
desc = fmt.Sprintf("Agent %s", agentName)
29-
}
27+
desc := cmp.Or(a.Description(), fmt.Sprintf("Agent %s", agentName))
3028

3129
return agent.New(agent.Config{
3230
Name: agentName,

pkg/acp/agent.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package acp
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -472,10 +473,7 @@ func (a *Agent) handleMaxIterationsReached(ctx context.Context, acpSess *Session
472473
// buildToolCallStart creates a tool call start update
473474
func buildToolCallStart(toolCall tools.ToolCall, tool tools.Tool) acp.SessionUpdate {
474475
kind := determineToolKind(toolCall.Function.Name, tool)
475-
title := tool.Annotations.Title
476-
if title == "" {
477-
title = toolCall.Function.Name
478-
}
476+
title := cmp.Or(tool.Annotations.Title, toolCall.Function.Name)
479477

480478
args := parseToolCallArguments(toolCall.Function.Arguments)
481479
locations := extractLocations(args)
@@ -670,10 +668,7 @@ func extractDiffContent(toolCall tools.ToolCall, _ string) *acp.ToolCallContent
670668
// buildToolCallUpdate creates a tool call update for permission requests
671669
func buildToolCallUpdate(toolCall tools.ToolCall, tool tools.Tool, status acp.ToolCallStatus) acp.RequestPermissionToolCall {
672670
kind := acp.ToolKindExecute
673-
title := tool.Annotations.Title
674-
if title == "" {
675-
title = toolCall.Function.Name
676-
}
671+
title := cmp.Or(tool.Annotations.Title, toolCall.Function.Name)
677672

678673
if tool.Annotations.ReadOnlyHint {
679674
kind = acp.ToolKindRead

pkg/agent/agent_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,27 @@ import (
1111
)
1212

1313
type stubToolSet struct {
14-
startErr error
15-
tools []tools.Tool
16-
listErr error
17-
instructions string
14+
tools.BaseToolSet
15+
startErr error
16+
tools []tools.Tool
17+
listErr error
1818
}
1919

2020
func newStubToolSet(startErr error, toolsList []tools.Tool, listErr error) tools.ToolSet {
2121
return &stubToolSet{
22-
startErr: startErr,
23-
tools: toolsList,
24-
listErr: listErr,
25-
instructions: "stub",
22+
startErr: startErr,
23+
tools: toolsList,
24+
listErr: listErr,
2625
}
2726
}
2827

2928
func (s *stubToolSet) Start(context.Context) error { return s.startErr }
30-
func (s *stubToolSet) Stop(context.Context) error { return nil }
3129
func (s *stubToolSet) Tools(context.Context) ([]tools.Tool, error) {
3230
if s.listErr != nil {
3331
return nil, s.listErr
3432
}
3533
return s.tools, nil
3634
}
37-
func (s *stubToolSet) Instructions() string { return s.instructions }
38-
func (s *stubToolSet) SetElicitationHandler(tools.ElicitationHandler) {}
39-
func (s *stubToolSet) SetOAuthSuccessHandler(func()) {}
40-
func (s *stubToolSet) SetManagedOAuth(bool) {}
4135

4236
func TestAgentTools(t *testing.T) {
4337
tests := []struct {

pkg/cli/runner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/base64"
67
"encoding/json"
@@ -69,10 +70,7 @@ func Run(ctx context.Context, out *Printer, cfg Config, rt runtime.Runtime, sess
6970
messageText, attachPath := parseAttachCommand(userInput)
7071

7172
// Use either the per-message attachment or the global one
72-
finalAttachPath := attachPath
73-
if finalAttachPath == "" {
74-
finalAttachPath = cfg.AttachmentPath
75-
}
73+
finalAttachPath := cmp.Or(attachPath, cfg.AttachmentPath)
7674

7775
sess.AddMessage(createUserMessageWithAttachment(messageText, finalAttachPath))
7876

pkg/config/resolve.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"cmp"
45
_ "embed"
56
"fmt"
67
"log/slog"
@@ -104,9 +105,7 @@ func Resolve(agentFilename string) (Source, error) {
104105

105106
// resolve resolves an agent reference, handling aliases and defaults
106107
func resolve(agentFilename string) (string, error) {
107-
if agentFilename == "" {
108-
agentFilename = "default"
109-
}
108+
agentFilename = cmp.Or(agentFilename, "default")
110109

111110
// Try to resolve as an alias first
112111
if aliasStore, err := aliases.Load(); err == nil {

pkg/creator/agent.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,13 @@ import (
1919
var agentBuilderInstructions string
2020

2121
type fsToolset struct {
22-
tools.ElicitationTool
23-
24-
inner tools.ToolSet
22+
tools.ToolSet
2523
originalWriteFileHandler tools.ToolHandler
2624
path string
2725
}
2826

29-
func (f *fsToolset) Instructions() string {
30-
return f.inner.Instructions()
31-
}
32-
33-
func (f *fsToolset) Start(ctx context.Context) error {
34-
return f.inner.Start(ctx)
35-
}
36-
37-
func (f *fsToolset) Stop(ctx context.Context) error {
38-
return f.inner.Stop(ctx)
39-
}
40-
4127
func (f *fsToolset) Tools(ctx context.Context) ([]tools.Tool, error) {
42-
innerTools, err := f.inner.Tools(ctx)
28+
innerTools, err := f.ToolSet.Tools(ctx)
4329
if err != nil {
4430
return nil, err
4531
}
@@ -111,7 +97,7 @@ Can you explain to me what the agent will be used for?`,
11197

11298
// Custom tool registry to include fsToolset
11399
fsToolset := fsToolset{
114-
inner: builtin.NewFilesystemTool([]string{runConfig.WorkingDir}),
100+
ToolSet: builtin.NewFilesystemTool([]string{runConfig.WorkingDir}),
115101
}
116102

117103
registry := teamloader.NewDefaultToolsetRegistry()

pkg/evaluation/save.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package evaluation
22

33
import (
4+
"cmp"
45
"encoding/json"
56
"fmt"
67
"os"
@@ -15,10 +16,7 @@ func Save(sess *session.Session, filename string) (string, error) {
1516
}
1617

1718
// Use provided filename if given, otherwise default to session ID
18-
baseName := filename
19-
if baseName == "" {
20-
baseName = sess.ID
21-
}
19+
baseName := cmp.Or(filename, sess.ID)
2220

2321
evalFile := filepath.Join("evals", fmt.Sprintf("%s.json", baseName))
2422
for number := 1; ; number++ {

0 commit comments

Comments
 (0)