Skip to content

Commit b0de1de

Browse files
author
Marek Safarik
committed
fix: lock agent state around callLLM and toolQueue mutations
Signed-off-by: Marek Safarik <msafarik@redhat.com>
1 parent 376485e commit b0de1de

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

internal/agent/agent.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,18 @@ func (a *Agent) SendMessage(ctx context.Context, userMessage string, onMessage f
113113
}
114114

115115
func (a *Agent) callLLM(ctx context.Context, onMessage func(Message)) error {
116-
response, err := a.provider.Chat(ctx, ChatOptions{
116+
a.mu.Lock()
117+
opts := ChatOptions{
117118
Model: a.config.Model,
118119
MaxTokens: a.config.MaxTokens,
119120
SystemPrompt: a.config.SystemPrompt,
120121
Messages: a.messages,
121122
Tools: a.tools,
122-
})
123+
}
124+
provider := a.provider
125+
a.mu.Unlock()
126+
127+
response, err := provider.Chat(ctx, opts)
123128
if err != nil {
124129
return err
125130
}
@@ -129,18 +134,23 @@ func (a *Agent) callLLM(ctx context.Context, onMessage func(Message)) error {
129134
Text: strings.Join(response.TextBlocks, "\n"),
130135
ToolCalls: response.ToolUses,
131136
}
132-
a.messages = append(a.messages, msg)
133-
134-
if msg.Text != "" {
135-
onMessage(Message{Role: RoleAssistant, Text: msg.Text})
136-
}
137137

138+
a.mu.Lock()
139+
a.messages = append(a.messages, msg)
138140
a.toolQueue = make([]ToolRequest, len(response.ToolUses))
139141
copy(a.toolQueue, response.ToolUses)
140142

143+
var firstTool *ToolRequest
141144
if len(a.toolQueue) > 0 {
142-
tc := a.toolQueue[0]
143-
onMessage(Message{Role: RoleAssistant, ToolCalls: []ToolRequest{tc}})
145+
firstTool = &a.toolQueue[0]
146+
}
147+
a.mu.Unlock()
148+
149+
if msg.Text != "" {
150+
onMessage(Message{Role: RoleAssistant, Text: msg.Text})
151+
}
152+
if firstTool != nil {
153+
onMessage(Message{Role: RoleAssistant, ToolCalls: []ToolRequest{*firstTool}})
144154
}
145155

146156
return nil
@@ -208,13 +218,18 @@ func (a *Agent) ToolDeny(ctx context.Context, tool *ToolRequest, onMessage func(
208218
}
209219

210220
func (a *Agent) advanceToolQueue(ctx context.Context, onMessage func(Message)) error {
221+
a.mu.Lock()
211222
if len(a.toolQueue) > 0 {
212223
a.toolQueue = a.toolQueue[1:]
213224
}
214-
225+
var nextTool *ToolRequest
215226
if len(a.toolQueue) > 0 {
216-
tc := a.toolQueue[0]
217-
onMessage(Message{Role: RoleAssistant, ToolCalls: []ToolRequest{tc}})
227+
nextTool = &a.toolQueue[0]
228+
}
229+
a.mu.Unlock()
230+
231+
if nextTool != nil {
232+
onMessage(Message{Role: RoleAssistant, ToolCalls: []ToolRequest{*nextTool}})
218233
return nil
219234
}
220235

0 commit comments

Comments
 (0)