@@ -74,21 +74,21 @@ public struct Function: Codable, Sendable {
74
74
public struct ToolCall : Codable , Sendable {
75
75
public let id : String
76
76
public let function : FunctionCallDetails
77
- public let index : Int
77
+ public let index : Int ? // Make index optional
78
78
public let type : String
79
79
}
80
80
81
81
public struct FunctionCallDetails : Codable , Sendable {
82
82
public let name : String
83
- public let arguments : [ String : Any ]
83
+ public let arguments : [ String : String ]
84
84
85
85
// Custom decoding to handle JSON string for arguments
86
86
public init ( from decoder: Decoder ) throws {
87
87
let container = try decoder. container ( keyedBy: CodingKeys . self)
88
88
name = try container. decode ( String . self, forKey: . name)
89
89
let argumentsString = try container. decode ( String . self, forKey: . arguments)
90
90
if let data = argumentsString. data ( using: . utf8) ,
91
- let argumentsDict = try ? JSONSerialization . jsonObject ( with : data , options : [ ] ) as? [ String : Any ] {
91
+ let argumentsDict = try ? JSONDecoder ( ) . decode ( [ String : String ] . self , from : data ) {
92
92
arguments = argumentsDict
93
93
} else {
94
94
arguments = [ : ]
@@ -99,7 +99,7 @@ public struct FunctionCallDetails: Codable, Sendable {
99
99
public func encode( to encoder: Encoder ) throws {
100
100
var container = encoder. container ( keyedBy: CodingKeys . self)
101
101
try container. encode ( name, forKey: . name)
102
- let argumentsData = try JSONSerialization . data ( withJSONObject : arguments, options : [ ] )
102
+ let argumentsData = try JSONEncoder ( ) . encode ( arguments)
103
103
let argumentsString = String ( data: argumentsData, encoding: . utf8) ?? " {} "
104
104
try container. encode ( argumentsString, forKey: . arguments)
105
105
}
0 commit comments