mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor: extract parseFunctionResponse helper to reduce duplication
This commit is contained in:
@@ -122,6 +122,38 @@ type FunctionCallGroup struct {
|
|||||||
ResponsesNeeded int
|
ResponsesNeeded int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseFunctionResponse attempts to unmarshal a function response part.
|
||||||
|
// Falls back to gjson extraction if standard json.Unmarshal fails.
|
||||||
|
func parseFunctionResponse(response gjson.Result) map[string]interface{} {
|
||||||
|
var responseMap map[string]interface{}
|
||||||
|
err := json.Unmarshal([]byte(response.Raw), &responseMap)
|
||||||
|
if err == nil {
|
||||||
|
return responseMap
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("unmarshal function response failed, using fallback: %v", err)
|
||||||
|
funcResp := response.Get("functionResponse")
|
||||||
|
if funcResp.Exists() {
|
||||||
|
fr := map[string]interface{}{
|
||||||
|
"name": funcResp.Get("name").String(),
|
||||||
|
"response": map[string]interface{}{
|
||||||
|
"result": funcResp.Get("response").String(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if id := funcResp.Get("id").String(); id != "" {
|
||||||
|
fr["id"] = id
|
||||||
|
}
|
||||||
|
return map[string]interface{}{"functionResponse": fr}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map[string]interface{}{
|
||||||
|
"functionResponse": map[string]interface{}{
|
||||||
|
"name": "unknown",
|
||||||
|
"response": map[string]interface{}{"result": response.String()},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fixCLIToolResponse performs sophisticated tool response format conversion and grouping.
|
// fixCLIToolResponse performs sophisticated tool response format conversion and grouping.
|
||||||
// This function transforms the CLI tool response format by intelligently grouping function calls
|
// This function transforms the CLI tool response format by intelligently grouping function calls
|
||||||
// with their corresponding responses, ensuring proper conversation flow and API compatibility.
|
// with their corresponding responses, ensuring proper conversation flow and API compatibility.
|
||||||
@@ -180,33 +212,7 @@ func fixCLIToolResponse(input string) (string, error) {
|
|||||||
// Create merged function response content
|
// Create merged function response content
|
||||||
var responseParts []interface{}
|
var responseParts []interface{}
|
||||||
for _, response := range groupResponses {
|
for _, response := range groupResponses {
|
||||||
var responseMap map[string]interface{}
|
responseParts = append(responseParts, parseFunctionResponse(response))
|
||||||
errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap)
|
|
||||||
if errUnmarshal != nil {
|
|
||||||
log.Debugf("unmarshal function response failed, using fallback: %v", errUnmarshal)
|
|
||||||
funcResp := response.Get("functionResponse")
|
|
||||||
if funcResp.Exists() {
|
|
||||||
fr := map[string]interface{}{
|
|
||||||
"name": funcResp.Get("name").String(),
|
|
||||||
"response": map[string]interface{}{
|
|
||||||
"result": funcResp.Get("response").String(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if id := funcResp.Get("id").String(); id != "" {
|
|
||||||
fr["id"] = id
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, map[string]interface{}{"functionResponse": fr})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, map[string]interface{}{
|
|
||||||
"functionResponse": map[string]interface{}{
|
|
||||||
"name": "unknown",
|
|
||||||
"response": map[string]interface{}{"result": response.String()},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, responseMap)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(responseParts) > 0 {
|
if len(responseParts) > 0 {
|
||||||
@@ -285,33 +291,7 @@ func fixCLIToolResponse(input string) (string, error) {
|
|||||||
|
|
||||||
var responseParts []interface{}
|
var responseParts []interface{}
|
||||||
for _, response := range groupResponses {
|
for _, response := range groupResponses {
|
||||||
var responseMap map[string]interface{}
|
responseParts = append(responseParts, parseFunctionResponse(response))
|
||||||
errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap)
|
|
||||||
if errUnmarshal != nil {
|
|
||||||
log.Debugf("unmarshal function response failed, using fallback: %v", errUnmarshal)
|
|
||||||
funcResp := response.Get("functionResponse")
|
|
||||||
if funcResp.Exists() {
|
|
||||||
fr := map[string]interface{}{
|
|
||||||
"name": funcResp.Get("name").String(),
|
|
||||||
"response": map[string]interface{}{
|
|
||||||
"result": funcResp.Get("response").String(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if id := funcResp.Get("id").String(); id != "" {
|
|
||||||
fr["id"] = id
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, map[string]interface{}{"functionResponse": fr})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, map[string]interface{}{
|
|
||||||
"functionResponse": map[string]interface{}{
|
|
||||||
"name": "unknown",
|
|
||||||
"response": map[string]interface{}{"result": response.String()},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
responseParts = append(responseParts, responseMap)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(responseParts) > 0 {
|
if len(responseParts) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user