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:
2025-04-12 02:22:10 -04:00
parent 7a77d74624
commit 1be5cc047c
6 changed files with 51 additions and 20 deletions

View File

@@ -3,9 +3,10 @@ package agents
import (
"context"
"fmt"
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
"sync"
"sync/atomic"
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
)
// Agent is essentially the bones of a chat agent. It has a model and a toolbox, and can be used to call the model
@@ -15,11 +16,12 @@ import (
// This package contains a number of helper functions to make it easier to create and use agents.
type Agent struct {
model gollm.ChatCompletion
toolbox *gollm.ToolBox
toolbox gollm.ToolBox
systemPrompt string
contextualInformation []string
systemPromptSuffix string
maxCalls *int32
insertReason bool
calls *atomic.Int32
}
@@ -27,7 +29,7 @@ type Agent struct {
// NewAgent creates a new agent struct with the given model and toolbox.
// Any inherited agents (e.g.: all made from sharing pointers or from the With... helpers) from this one will
// share the same call count.
func NewAgent(model gollm.ChatCompletion, toolbox *gollm.ToolBox) Agent {
func NewAgent(model gollm.ChatCompletion, toolbox gollm.ToolBox) Agent {
return Agent{
model: model,
toolbox: toolbox,
@@ -44,8 +46,10 @@ func (a Agent) WithModel(model gollm.ChatCompletion) Agent {
return a
}
func (a Agent) WithToolbox(toolbox *gollm.ToolBox) Agent {
a.toolbox = toolbox
func (a Agent) WithToolbox(toolbox gollm.ToolBox) Agent {
a.toolbox = toolbox.WithSyntheticFieldsAddedToAllFunctions(map[string]string{
"reason": "The reason you are calling this function. This will be remembered and presenting to the LLM when it continues after the function call.",
})
return a
}

View File

@@ -55,7 +55,6 @@ Here is the knowledge I have gathered from ` + fmt.Sprint(len(sources)) + ` sour
res, err := a.WithSystemPrompt(systemPrompt).
WithSystemPromptSuffix(``).
WithToolbox(nil).
CallAndExecute(ctx)
if err != nil {
return "", err
@@ -102,7 +101,6 @@ The moon is 4.5 billion years old [1,2], 238,855 miles away from the Earth [3],
res, err = a.WithSystemPrompt(systemPrompt).
WithSystemPromptSuffix(``).
WithToolbox(nil).
CallAndExecute(ctx, gollm.Message{Role: gollm.RoleSystem, Text: providedIntel}, summarizedData)
if err != nil {

View File

@@ -92,7 +92,7 @@ func (a Agent) SearchAndUseTools(ctx context.Context, searchQuery string, questi
var lock sync.Mutex
var analyzed []int
var converted []*gollm.Function
var converted []gollm.Function
for _, t := range tools {
fn := gollm.NewFunction(t.Name, t.Description,