Skip to content

Commit 2644d9b

Browse files
committed
fix so a return message has EITHER tool calls OR content
1 parent 09fd9bb commit 2644d9b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

pkg/aiusechat/openaicomp/openaicomp-backend.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ func processCompletionsStream(
198198
stopKind = uctypes.StopKindToolUse
199199
}
200200

201+
var validToolCalls []ToolCall
202+
for _, tc := range toolCallsInProgress {
203+
if tc.ID != "" && tc.Function.Name != "" {
204+
validToolCalls = append(validToolCalls, tc)
205+
}
206+
}
207+
201208
var waveToolCalls []uctypes.WaveToolCall
202-
if len(toolCallsInProgress) > 0 {
203-
for _, tc := range toolCallsInProgress {
209+
if len(validToolCalls) > 0 {
210+
for _, tc := range validToolCalls {
204211
var inputJSON any
205212
if tc.Function.Arguments != "" {
206213
if err := json.Unmarshal([]byte(tc.Function.Arguments), &inputJSON); err != nil {
@@ -225,12 +232,16 @@ func processCompletionsStream(
225232
assistantMsg := &CompletionsChatMessage{
226233
MessageId: msgID,
227234
Message: CompletionsMessage{
228-
Role: "assistant",
229-
Content: textBuilder.String(),
230-
ToolCalls: toolCallsInProgress,
235+
Role: "assistant",
231236
},
232237
}
233238

239+
if len(validToolCalls) > 0 {
240+
assistantMsg.Message.ToolCalls = validToolCalls
241+
} else {
242+
assistantMsg.Message.Content = textBuilder.String()
243+
}
244+
234245
if textStarted {
235246
_ = sseHandler.AiMsgTextEnd(textID)
236247
}

0 commit comments

Comments
 (0)