perf: init coder agent in a goroutine (#1289)

Carlos Alexandro Becker created

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

internal/agent/coordinator.go | 25 ++++++++++++++++++++-----
internal/app/app.go           | 10 +++++-----
2 files changed, 25 insertions(+), 10 deletions(-)

Detailed changes

internal/agent/coordinator.go 🔗

@@ -289,11 +289,26 @@ func (c *coordinator) buildAgent(ctx context.Context, prompt *prompt.Prompt, age
 	}
 
 	largeProviderCfg, _ := c.cfg.Providers.Get(large.ModelCfg.Provider)
-	tools, err := c.buildTools(ctx, agent)
-	if err != nil {
-		return nil, err
-	}
-	return NewSessionAgent(SessionAgentOptions{large, small, largeProviderCfg.SystemPromptPrefix, systemPrompt, c.cfg.Options.DisableAutoSummarize, c.permissions.SkipRequests(), c.sessions, c.messages, tools}), nil
+	result := NewSessionAgent(SessionAgentOptions{
+		large,
+		small,
+		largeProviderCfg.SystemPromptPrefix,
+		systemPrompt,
+		c.cfg.Options.DisableAutoSummarize,
+		c.permissions.SkipRequests(),
+		c.sessions,
+		c.messages,
+		nil,
+	})
+	go func() {
+		tools, err := c.buildTools(ctx, agent)
+		if err != nil {
+			slog.Error("could not init agent tools", "err", err)
+			return
+		}
+		result.SetTools(tools)
+	}()
+	return result, nil
 }
 
 func (c *coordinator) buildTools(ctx context.Context, agent config.Agent) ([]fantasy.AgentTool, error) {

internal/app/app.go 🔗

@@ -86,12 +86,12 @@ func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
 	app.cleanupFuncs = append(app.cleanupFuncs, conn.Close)
 
 	// TODO: remove the concept of agent config, most likely.
-	if cfg.IsConfigured() {
-		if err := app.InitCoderAgent(ctx); err != nil {
-			return nil, fmt.Errorf("failed to initialize coder agent: %w", err)
-		}
-	} else {
+	if !cfg.IsConfigured() {
 		slog.Warn("No agent configuration found")
+		return app, nil
+	}
+	if err := app.InitCoderAgent(ctx); err != nil {
+		return nil, fmt.Errorf("failed to initialize coder agent: %w", err)
 	}
 	return app, nil
 }