Skip to content

Commit a65a132

Browse files
committed
working on tools
1 parent 5b786c7 commit a65a132

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

pkg/aiusechat/openaicomp/openaicomp-backend.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func processCompletionsStream(
108108
textID := uuid.New().String()
109109
var finishReason string
110110
textStarted := false
111+
var toolCallsInProgress []ToolCall
111112

112113
_ = sseHandler.AiMsgStart(msgID)
113114
_ = sseHandler.AiMsgStartStep()
@@ -160,6 +161,31 @@ func processCompletionsStream(
160161
_ = sseHandler.AiMsgTextDelta(textID, choice.Delta.Content)
161162
}
162163

164+
if len(choice.Delta.ToolCalls) > 0 {
165+
for _, tcDelta := range choice.Delta.ToolCalls {
166+
idx := tcDelta.Index
167+
for len(toolCallsInProgress) <= idx {
168+
toolCallsInProgress = append(toolCallsInProgress, ToolCall{})
169+
}
170+
171+
tc := &toolCallsInProgress[idx]
172+
if tcDelta.ID != "" {
173+
tc.ID = tcDelta.ID
174+
}
175+
if tcDelta.Type != "" {
176+
tc.Type = tcDelta.Type
177+
}
178+
if tcDelta.Function != nil {
179+
if tcDelta.Function.Name != "" {
180+
tc.Function.Name = tcDelta.Function.Name
181+
}
182+
if tcDelta.Function.Arguments != "" {
183+
tc.Function.Arguments += tcDelta.Function.Arguments
184+
}
185+
}
186+
}
187+
}
188+
163189
if choice.FinishReason != nil && *choice.FinishReason != "" {
164190
finishReason = *choice.FinishReason
165191
}
@@ -168,18 +194,40 @@ func processCompletionsStream(
168194
stopKind := uctypes.StopKindDone
169195
if finishReason == "length" {
170196
stopKind = uctypes.StopKindMaxTokens
197+
} else if finishReason == "tool_calls" {
198+
stopKind = uctypes.StopKindToolUse
199+
}
200+
201+
var waveToolCalls []uctypes.WaveToolCall
202+
if len(toolCallsInProgress) > 0 {
203+
for _, tc := range toolCallsInProgress {
204+
var inputJSON any
205+
if tc.Function.Arguments != "" {
206+
if err := json.Unmarshal([]byte(tc.Function.Arguments), &inputJSON); err != nil {
207+
log.Printf("openaicomp: failed to parse tool call arguments: %v\n", err)
208+
continue
209+
}
210+
}
211+
waveToolCalls = append(waveToolCalls, uctypes.WaveToolCall{
212+
ID: tc.ID,
213+
Name: tc.Function.Name,
214+
Input: inputJSON,
215+
})
216+
}
171217
}
172218

173219
stopReason := &uctypes.WaveStopReason{
174220
Kind: stopKind,
175221
RawReason: finishReason,
222+
ToolCalls: waveToolCalls,
176223
}
177224

178225
assistantMsg := &CompletionsChatMessage{
179226
MessageId: msgID,
180227
Message: CompletionsMessage{
181-
Role: "assistant",
182-
Content: textBuilder.String(),
228+
Role: "assistant",
229+
Content: textBuilder.String(),
230+
ToolCalls: toolCallsInProgress,
183231
},
184232
}
185233

pkg/aiusechat/openaicomp/openaicomp-types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type StreamChunk struct {
8383
type StreamChoice struct {
8484
Index int `json:"index"`
8585
Delta ContentDelta `json:"delta"`
86-
FinishReason *string `json:"finish_reason"`
86+
FinishReason *string `json:"finish_reason"` // "stop", "length" | "tool_calls" | "content_filter"
8787
}
8888

8989
// This is the important part:

0 commit comments

Comments
 (0)