-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathnormalize.go
More file actions
43 lines (38 loc) · 1004 Bytes
/
normalize.go
File metadata and controls
43 lines (38 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package codexutil
import (
"fmt"
"strings"
"github.com/google/uuid"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func NormalizeCodexInput(body []byte) []byte {
input := gjson.GetBytes(body, "input")
if !input.IsArray() {
return body
}
for i, item := range input.Array() {
itemType := item.Get("type").String()
if itemType != "message" {
if item.Get("role").Exists() {
body, _ = sjson.DeleteBytes(body, fmt.Sprintf("input.%d.role", i))
}
}
if itemType == "function_call" {
id := strings.TrimSpace(item.Get("id").String())
switch {
case strings.HasPrefix(id, "fc_"):
case id == "":
body, _ = sjson.SetBytes(body, fmt.Sprintf("input.%d.id", i), "fc_"+uuid.NewString())
default:
body, _ = sjson.SetBytes(body, fmt.Sprintf("input.%d.id", i), "fc_"+id)
}
}
if itemType == "function_call_output" {
if !item.Get("output").Exists() {
body, _ = sjson.SetBytes(body, fmt.Sprintf("input.%d.output", i), "")
}
}
}
return body
}