Skip to content

Commit 0bb9c80

Browse files
[feature/openrouter-provider] Refactor finish reason handling and tool call logic in agent and OpenAI provider
- Simplified finish reason check in `agent.go` by removing redundant variable assignment. - Updated `openai.go` to override the finish reason to `FinishReasonToolUse` when tool calls are present. - Ensured consistent finish reason handling in both `send` and `stream` methods of the OpenAI provider. [feature/openrouter-provider] Refactor finish reason handling and tool call logic in agent and OpenAI provider - Simplified finish reason check in `agent.go` by removing redundant variable assignment. - Updated `openai.go` to override the finish reason to `FinishReasonToolUse` when tool calls are present. - Ensured consistent finish reason handling in both `send` and `stream` methods of the OpenAI provider.
1 parent 86d0d1f commit 0bb9c80

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

internal/llm/agent/agent.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string
228228
return a.err(fmt.Errorf("failed to process events: %w", err))
229229
}
230230
logging.Info("Result", "message", agentMessage.FinishReason(), "toolResults", toolResults)
231-
finishReason := agentMessage.FinishReason()
232-
if (finishReason == message.FinishReasonToolUse || finishReason == message.FinishReasonUnknown) && toolResults != nil {
231+
if (agentMessage.FinishReason() == message.FinishReasonToolUse) && toolResults != nil {
233232
// We are not done, we need to respond with the tool response
234233
msgHistory = append(msgHistory, agentMessage, *toolResults)
235234
continue

internal/llm/provider/openai.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,18 @@ func (o *openaiClient) send(ctx context.Context, messages []message.Message, too
204204
content = openaiResponse.Choices[0].Message.Content
205205
}
206206

207+
toolCalls := o.toolCalls(*openaiResponse)
208+
finishReason := o.finishReason(string(openaiResponse.Choices[0].FinishReason))
209+
210+
if len(toolCalls) > 0 {
211+
finishReason = message.FinishReasonToolUse
212+
}
213+
207214
return &ProviderResponse{
208215
Content: content,
209-
ToolCalls: o.toolCalls(*openaiResponse),
216+
ToolCalls: toolCalls,
210217
Usage: o.usage(*openaiResponse),
211-
FinishReason: o.finishReason(string(openaiResponse.Choices[0].FinishReason)),
218+
FinishReason: finishReason,
212219
}, nil
213220
}
214221
}
@@ -267,13 +274,23 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
267274
err := openaiStream.Err()
268275
if err == nil || errors.Is(err, io.EOF) {
269276
// Stream completed successfully
277+
eventChan <- ProviderEvent{
278+
Type: EventContentStop,
279+
}
280+
281+
finishReason := o.finishReason(string(acc.ChatCompletion.Choices[0].FinishReason))
282+
283+
if len(toolCalls) > 0 {
284+
finishReason = message.FinishReasonToolUse
285+
}
286+
270287
eventChan <- ProviderEvent{
271288
Type: EventComplete,
272289
Response: &ProviderResponse{
273290
Content: currentContent,
274291
ToolCalls: toolCalls,
275292
Usage: o.usage(acc.ChatCompletion),
276-
FinishReason: o.finishReason(string(acc.ChatCompletion.Choices[0].FinishReason)),
293+
FinishReason: finishReason,
277294
},
278295
}
279296
close(eventChan)

0 commit comments

Comments
 (0)