Skip to content

Commit 5b786c7

Browse files
committed
convert tool parts
1 parent 3399a6e commit 5b786c7

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

pkg/aiusechat/openaicomp/openaicomp-convertmessage.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,48 @@ func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {
213213
continue
214214
}
215215

216+
var parts []uctypes.UIMessagePart
217+
218+
// Add text content if present
219+
if compMsg.Message.Content != "" {
220+
parts = append(parts, uctypes.UIMessagePart{
221+
Type: "text",
222+
Text: compMsg.Message.Content,
223+
})
224+
}
225+
226+
// Add tool calls if present (assistant requesting tool use)
227+
if len(compMsg.Message.ToolCalls) > 0 {
228+
for _, toolCall := range compMsg.Message.ToolCalls {
229+
if toolCall.Type != "function" {
230+
continue
231+
}
232+
233+
// Only add if ToolUseData is available
234+
if toolCall.ToolUseData != nil {
235+
parts = append(parts, uctypes.UIMessagePart{
236+
Type: "data-tooluse",
237+
ID: toolCall.ID,
238+
Data: *toolCall.ToolUseData,
239+
})
240+
}
241+
}
242+
}
243+
244+
// Tool result messages (role "tool") are not converted to UIMessage
245+
if compMsg.Message.Role == "tool" && compMsg.Message.ToolCallID != "" {
246+
continue
247+
}
248+
249+
// Skip messages with no parts
250+
if len(parts) == 0 {
251+
continue
252+
}
253+
216254
uiMsg := uctypes.UIMessage{
217-
ID: compMsg.MessageId,
218-
Role: compMsg.Message.Role,
219-
Parts: []uctypes.UIMessagePart{
220-
{
221-
Type: "text",
222-
Text: compMsg.Message.Content,
223-
},
224-
},
255+
ID: compMsg.MessageId,
256+
Role: compMsg.Message.Role,
257+
Parts: parts,
225258
}
226259

227260
uiChat.Messages = append(uiChat.Messages, uiMsg)

pkg/aiusechat/openaicomp/openaicomp-types.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ type CompletionsMessage struct {
2828
Name string `json:"name,omitempty"` // tool name on role:"tool"
2929
}
3030

31+
func (cm *CompletionsMessage) clean() *CompletionsMessage {
32+
if len(cm.ToolCalls) == 0 {
33+
return cm
34+
}
35+
rtn := *cm
36+
rtn.ToolCalls = make([]ToolCall, len(cm.ToolCalls))
37+
for i, tc := range cm.ToolCalls {
38+
rtn.ToolCalls[i] = *tc.clean()
39+
}
40+
return &rtn
41+
}
42+
3143
type ToolDefinition struct {
3244
Type string `json:"type"` // "function"
3345
Function ToolFunctionDef `json:"function"`
@@ -40,9 +52,19 @@ type ToolFunctionDef struct {
4052
}
4153

4254
type ToolCall struct {
43-
ID string `json:"id"`
44-
Type string `json:"type"` // "function"
45-
Function ToolFunctionCall `json:"function"`
55+
ID string `json:"id"`
56+
Type string `json:"type"` // "function"
57+
Function ToolFunctionCall `json:"function"`
58+
ToolUseData *uctypes.UIMessageDataToolUse `json:"toolusedata,omitempty"` // Internal field (must be cleaned before sending to API)
59+
}
60+
61+
func (tc *ToolCall) clean() *ToolCall {
62+
if tc.ToolUseData == nil {
63+
return tc
64+
}
65+
rtn := *tc
66+
rtn.ToolUseData = nil
67+
return &rtn
4668
}
4769

4870
type ToolFunctionCall struct {

0 commit comments

Comments
 (0)