diff --git a/FirebaseAI/Sources/Types/Internal/InternalPart.swift b/FirebaseAI/Sources/Types/Internal/InternalPart.swift index d543fb80f38..5a655062729 100644 --- a/FirebaseAI/Sources/Types/Internal/InternalPart.swift +++ b/FirebaseAI/Sources/Types/Internal/InternalPart.swift @@ -45,10 +45,12 @@ struct FileData: Codable, Equatable, Sendable { struct FunctionCall: Equatable, Sendable { let name: String let args: JSONObject + let id: String? - init(name: String, args: JSONObject) { + init(name: String, args: JSONObject, id: String?) { self.name = name self.args = args + self.id = id } } @@ -56,10 +58,12 @@ struct FunctionCall: Equatable, Sendable { struct FunctionResponse: Codable, Equatable, Sendable { let name: String let response: JSONObject + let id: String? - init(name: String, response: JSONObject) { + init(name: String, response: JSONObject, id: String?) { self.name = name self.response = response + self.id = id } } @@ -79,6 +83,7 @@ extension FunctionCall: Codable { init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) name = try container.decode(String.self, forKey: .name) + id = try container.decodeIfPresent(String.self, forKey: .id) if let args = try container.decodeIfPresent(JSONObject.self, forKey: .args) { self.args = args } else { diff --git a/FirebaseAI/Sources/Types/Public/Part.swift b/FirebaseAI/Sources/Types/Public/Part.swift index 4890b725f4d..1ec790f337d 100644 --- a/FirebaseAI/Sources/Types/Public/Part.swift +++ b/FirebaseAI/Sources/Types/Public/Part.swift @@ -112,6 +112,10 @@ public struct FunctionCallPart: Part { /// The function parameters and values. public var args: JSONObject { functionCall.args } + /// The unique ID of the function call. If specified, this identifier should be included in the + /// ``FunctionResponsePart``. + public var id: String? { functionCall.id } + /// Constructs a new function call part. /// /// > Note: A `FunctionCallPart` is typically received from the model, rather than created @@ -121,7 +125,7 @@ public struct FunctionCallPart: Part { /// - name: The name of the function to call. /// - args: The function parameters and values. public init(name: String, args: JSONObject) { - self.init(FunctionCall(name: name, args: args)) + self.init(FunctionCall(name: name, args: args, id: nil)) } init(_ functionCall: FunctionCall) { @@ -149,8 +153,10 @@ public struct FunctionResponsePart: Part { /// - Parameters: /// - name: The name of the function that was called. /// - response: The function's response. - public init(name: String, response: JSONObject) { - self.init(FunctionResponse(name: name, response: response)) + /// - id: The unique ID of the function call, if specified, that this response corresponds with; + /// see ``FunctionCallPart/id`` for more details. + public init(name: String, response: JSONObject, id: String? = nil) { + self.init(FunctionResponse(name: name, response: response, id: id)) } init(_ functionResponse: FunctionResponse) { diff --git a/FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift b/FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift index e8ef9bf512c..e3264cc4e00 100644 --- a/FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift +++ b/FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift @@ -92,7 +92,8 @@ final class FunctionCallingSnippets: XCTestCase { functionResponses.append(FunctionResponsePart( name: functionCall.name, - response: fetchWeather(city: city, state: state, date: date) + response: fetchWeather(city: city, state: state, date: date), + id: functionCall.id )) } // TODO(developer): Handle other potential function calls, if any.