Skip to content

[Firebase AI] Add id property to FunctionCallPart #15167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions FirebaseAI/Sources/Types/Internal/InternalPart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ 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
}
}

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
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
}
}

Expand All @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions FirebaseAI/Sources/Types/Public/Part.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion FirebaseAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading