@@ -74,21 +74,21 @@ public struct Function: Codable, Sendable {
7474public struct ToolCall : Codable , Sendable {
7575 public let id : String
7676 public let function : FunctionCallDetails
77- public let index : Int
77+ public let index : Int ? // Make index optional
7878 public let type : String
7979}
8080
8181public struct FunctionCallDetails : Codable , Sendable {
8282 public let name : String
83- public let arguments : [ String : Any ]
83+ public let arguments : [ String : String ]
8484
8585 // Custom decoding to handle JSON string for arguments
8686 public init ( from decoder: Decoder ) throws {
8787 let container = try decoder. container ( keyedBy: CodingKeys . self)
8888 name = try container. decode ( String . self, forKey: . name)
8989 let argumentsString = try container. decode ( String . self, forKey: . arguments)
9090 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 ) {
9292 arguments = argumentsDict
9393 } else {
9494 arguments = [ : ]
@@ -99,7 +99,7 @@ public struct FunctionCallDetails: Codable, Sendable {
9999 public func encode( to encoder: Encoder ) throws {
100100 var container = encoder. container ( keyedBy: CodingKeys . self)
101101 try container. encode ( name, forKey: . name)
102- let argumentsData = try JSONSerialization . data ( withJSONObject : arguments, options : [ ] )
102+ let argumentsData = try JSONEncoder ( ) . encode ( arguments)
103103 let argumentsString = String ( data: argumentsData, encoding: . utf8) ?? " {} "
104104 try container. encode ( argumentsString, forKey: . arguments)
105105 }
0 commit comments