Skip to content

Commit d246c84

Browse files
committed
Fix OpenAI tools conversion for CommandCode API
1 parent 67f879d commit d246c84

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

internal/proxy/convert.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,52 @@ func ConvertMessages(openAIMsgs []api.OpenAIMessage) []api.CCMessage {
5151
return ccMsgs
5252
}
5353

54+
func ConvertTools(openAITools []any) []any {
55+
if len(openAITools) == 0 {
56+
return []any{}
57+
}
58+
59+
tools := make([]any, 0, len(openAITools))
60+
for _, tool := range openAITools {
61+
toolMap, ok := tool.(map[string]any)
62+
if !ok {
63+
continue
64+
}
65+
66+
toolType, _ := toolMap["type"].(string)
67+
if toolType != "function" {
68+
tools = append(tools, toolMap)
69+
continue
70+
}
71+
72+
fn, ok := toolMap["function"].(map[string]any)
73+
if !ok {
74+
continue
75+
}
76+
77+
name, _ := fn["name"].(string)
78+
if name == "" {
79+
continue
80+
}
81+
82+
inputSchema, ok := fn["parameters"].(map[string]any)
83+
if !ok || inputSchema == nil {
84+
inputSchema = map[string]any{"type": "object", "properties": map[string]any{}}
85+
}
86+
87+
ccTool := map[string]any{
88+
"name": name,
89+
"input_schema": inputSchema,
90+
}
91+
if description, ok := fn["description"].(string); ok && description != "" {
92+
ccTool["description"] = description
93+
}
94+
tools = append(tools, ccTool)
95+
}
96+
97+
return tools
98+
}
99+
54100
func strPtr(s string) *string {
55101
if s == "" {
56102
return nil

internal/proxy/proxy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ func (p *Proxy) BuildRequest(openAIReq api.OpenAIChatRequest) (api.CCRequestBody
5151
maxTokens = *openAIReq.MaxTokens
5252
}
5353

54-
tools := openAIReq.Tools
55-
if tools == nil {
56-
tools = []any{}
57-
}
54+
tools := ConvertTools(openAIReq.Tools)
5855

5956
ccBody := api.CCRequestBody{
6057
Config: api.CCConfig{

0 commit comments

Comments
 (0)