Skip to content

Commit b3cca07

Browse files
committed
Fix tool call and tool result message conversion for CommandCode API
1 parent 74eef88 commit b3cca07

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

internal/api/commandcode.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package api
33
// CommandCode API types (internal)
44

55
type CCContentPart struct {
6-
Type string `json:"type"`
7-
Text string `json:"text"`
6+
Type string `json:"type"`
7+
Text string `json:"text,omitempty"`
8+
ToolCallID string `json:"toolCallId,omitempty"`
9+
ToolName string `json:"toolName,omitempty"`
810
}
911

1012
type CCMessage struct {

internal/proxy/convert.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ import (
1010
func ConvertMessages(openAIMsgs []api.OpenAIMessage) []api.CCMessage {
1111
var ccMsgs []api.CCMessage
1212
for _, m := range openAIMsgs {
13+
// Convert tool role to user with tool-result type
14+
if m.Role == "tool" {
15+
ccMsgs = append(ccMsgs, api.CCMessage{
16+
Role: "user",
17+
Content: []api.CCContentPart{{
18+
Type: "tool-result",
19+
ToolCallID: m.ToolCallID,
20+
ToolName: m.Name,
21+
Text: contentToString(m.Content),
22+
}},
23+
})
24+
continue
25+
}
26+
27+
// Convert assistant with tool_calls
28+
if m.Role == "assistant" && len(m.ToolCalls) > 0 {
29+
contentParts := parseContent(m.Content)
30+
for _, tc := range m.ToolCalls {
31+
contentParts = append(contentParts, api.CCContentPart{
32+
Type: "tool_use",
33+
ToolCallID: tc.ID,
34+
ToolName: tc.Function.Name,
35+
Text: tc.Function.Arguments,
36+
})
37+
}
38+
ccMsgs = append(ccMsgs, api.CCMessage{
39+
Role: m.Role,
40+
Content: contentParts,
41+
})
42+
continue
43+
}
44+
1345
contentParts := parseContent(m.Content)
1446
ccMsgs = append(ccMsgs, api.CCMessage{
1547
Role: m.Role,
@@ -19,6 +51,22 @@ func ConvertMessages(openAIMsgs []api.OpenAIMessage) []api.CCMessage {
1951
return ccMsgs
2052
}
2153

54+
func contentToString(content interface{}) string {
55+
switch v := content.(type) {
56+
case string:
57+
return v
58+
case []any:
59+
for _, part := range v {
60+
if partMap, ok := part.(map[string]any); ok {
61+
if text, ok := partMap["text"].(string); ok {
62+
return text
63+
}
64+
}
65+
}
66+
}
67+
return ""
68+
}
69+
2270
func parseContent(content interface{}) []api.CCContentPart {
2371
switch v := content.(type) {
2472
case string:

0 commit comments

Comments
 (0)