Fix browser closure timing in search agent

Move defer statement to ensure browser closure occurs only after assigning the browser to the context. This prevents potential issues of premature resource release.
This commit is contained in:
Steve Dudenhoeffer 2025-04-07 11:24:58 -04:00
parent e874791f66
commit 7a77d74624

View File

@ -54,9 +54,9 @@ func (a Agent) SearchAndUseTools(ctx context.Context, searchQuery string, questi
return knowledge, err
}
defer deferClose(browser)
ctx = context.WithValue(ctx, "browser", b)
browser = b
defer deferClose(browser)
}
cfg := duckduckgo.Config{
@ -133,7 +133,7 @@ func (a Agent) SearchAndUseTools(ctx context.Context, searchQuery string, questi
}
analyzed = analyzed[:0]
for k, _ := range unique {
for k := range unique {
analyzed = append(analyzed, k)
}
@ -179,10 +179,10 @@ Use appropriate tools to analyze the search results and determine if they answer
results, err = a.CallAndExecute(ctx, messages...)
}
slog.Info("search results called and executed", "error", err, "results text", results.Text, "results", results.CallResults)
if err != nil {
return knowledge, fmt.Errorf("error executing search function: %w", err)
}
slog.Info("search results called and executed", "error", err, "results text", results.Text, "results", results.CallResults)
var learned []Knowledge
for _, r := range results.CallResults {