Skip to content

Commit 90857d6

Browse files
committed
[Firebase AI] Add id property to FunctionResponsePart
1 parent 690d973 commit 90857d6

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

FirebaseAI/Sources/Types/Internal/InternalPart.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,25 @@ struct FileData: Codable, Equatable, Sendable {
4444
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
4545
struct FunctionCall: Equatable, Sendable {
4646
let name: String
47+
let id: String?
4748
let args: JSONObject
4849

49-
init(name: String, args: JSONObject) {
50+
init(name: String, id: String?, args: JSONObject) {
5051
self.name = name
52+
self.id = id
5153
self.args = args
5254
}
5355
}
5456

5557
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
5658
struct FunctionResponse: Codable, Equatable, Sendable {
5759
let name: String
60+
let id: String?
5861
let response: JSONObject
5962

60-
init(name: String, response: JSONObject) {
63+
init(name: String, id: String?, response: JSONObject) {
6164
self.name = name
65+
self.id = id
6266
self.response = response
6367
}
6468
}
@@ -79,6 +83,7 @@ extension FunctionCall: Codable {
7983
init(from decoder: Decoder) throws {
8084
let container = try decoder.container(keyedBy: CodingKeys.self)
8185
name = try container.decode(String.self, forKey: .name)
86+
id = try container.decodeIfPresent(String.self, forKey: .id)
8287
if let args = try container.decodeIfPresent(JSONObject.self, forKey: .args) {
8388
self.args = args
8489
} else {

FirebaseAI/Sources/Types/Public/Part.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public struct FunctionCallPart: Part {
109109
/// The name of the function to call.
110110
public var name: String { functionCall.name }
111111

112+
/// The unique ID of the function call. If specified, this identifier should be included in the
113+
/// ``FunctionResponsePart``.
114+
public var id: String? { functionCall.id }
115+
112116
/// The function parameters and values.
113117
public var args: JSONObject { functionCall.args }
114118

@@ -121,7 +125,7 @@ public struct FunctionCallPart: Part {
121125
/// - name: The name of the function to call.
122126
/// - args: The function parameters and values.
123127
public init(name: String, args: JSONObject) {
124-
self.init(FunctionCall(name: name, args: args))
128+
self.init(FunctionCall(name: name, id: nil, args: args))
125129
}
126130

127131
init(_ functionCall: FunctionCall) {
@@ -148,9 +152,11 @@ public struct FunctionResponsePart: Part {
148152
///
149153
/// - Parameters:
150154
/// - name: The name of the function that was called.
155+
/// - id: The unique ID of the function call, if specified, that this response corresponds with;
156+
/// see ``FunctionCallPart/id`` for more details.
151157
/// - response: The function's response.
152-
public init(name: String, response: JSONObject) {
153-
self.init(FunctionResponse(name: name, response: response))
158+
public init(name: String, id: String? = nil, response: JSONObject) {
159+
self.init(FunctionResponse(name: name, id: id, response: response))
154160
}
155161

156162
init(_ functionResponse: FunctionResponse) {

FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ final class FunctionCallingSnippets: XCTestCase {
9292

9393
functionResponses.append(FunctionResponsePart(
9494
name: functionCall.name,
95+
id: functionCall.id,
9596
response: fetchWeather(city: city, state: state, date: date)
9697
))
9798
}

0 commit comments

Comments
 (0)