Skip to content

Add result builder api to Firebase AI #14957

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions FirebaseAI/Sources/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public final class GenerativeModel: Sendable {
return try await generateContent([ModelContent(parts: parts)])
}

public func generateContent(@ModelContentBuilder _ contentBuilder: ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any scenarios where this will make it ambiguous which method will get called?

Copy link
Contributor Author

@morganchen12 morganchen12 Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might conflict with a generateContent(@autoclosure () -> ModelContent), but the other method takes a [ModelContent] parameter, so in its current state it doesn't have any ambiguity conflicts.

-> ModelContent) async throws -> GenerateContentResponse {
let content = contentBuilder()
return try await generateContent([content])
}

/// Generates new content from input content given to the model as a prompt.
///
/// - Parameter content: The input(s) given to the model as a prompt.
Expand Down
52 changes: 52 additions & 0 deletions FirebaseAI/Sources/ModelContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,55 @@
}
}
}

// MARK: - ModelContentBuilder

@resultBuilder
public struct ModelContentBuilder {
typealias Expression = PartsRepresentable

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 177 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
typealias Component = [PartsRepresentable]

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 178 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
typealias Result = ModelContent

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'ModelContent' is only available in macOS 12.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'ModelContent' is only available in tvOS 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'ModelContent' is only available in watchOS 8.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'ModelContent' is only available in iOS 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'ModelContent' is only available in iOS 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'ModelContent' is only available in Mac Catalyst 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'ModelContent' is only available in macOS 12.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'ModelContent' is only available in watchOS 8.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'ModelContent' is only available in tvOS 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'ModelContent' is only available in iOS 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'ModelContent' is only available in Mac Catalyst 15.0 or newer

Check failure on line 179 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'ModelContent' is only available in iOS 15.0 or newer

public static func buildExpression(_ expression: PartsRepresentable) -> [any PartsRepresentable] {

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 181 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
return [expression]
}

public static func buildBlock(_ components: [any PartsRepresentable]...)

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 185 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
-> [any PartsRepresentable] {

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 186 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
return components

Check warning on line 187 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in tvOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 187 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 187 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 187 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 187 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode
}

public static func buildEither(first component: [any PartsRepresentable])

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 190 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
-> [any PartsRepresentable] {

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 191 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
return component
}

public static func buildEither(second component: [any PartsRepresentable])

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, macOS)

'PartsRepresentable' is only available in macOS 12.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, watchOS)

'PartsRepresentable' is only available in watchOS 8.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

'PartsRepresentable' is only available in tvOS 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, catalyst)

'PartsRepresentable' is only available in Mac Catalyst 15.0 or newer

Check failure on line 195 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

'PartsRepresentable' is only available in iOS 15.0 or newer
-> [any PartsRepresentable] {
return component
}

public static func buildArray(_ components: [any PartsRepresentable])
-> [any PartsRepresentable] {
return components
}

public static func buildArray(_ components: [[any PartsRepresentable]])
-> [any PartsRepresentable] {
return components.flatMap { $0 }
}

public static func buildOptional(_ component: [any PartsRepresentable]?)
-> [any PartsRepresentable] {
return component ?? []
}

public static func buildLimitedAvailability(_ component: [any PartsRepresentable])
-> [any PartsRepresentable] {
return component
}

public static func buildFinalResult(_ component: [any PartsRepresentable]) -> ModelContent {
return ModelContent(parts: component)

Check warning on line 221 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, tvOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in tvOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 221 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-15, Xcode_16.3, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 221 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 221 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode

Check warning on line 221 in FirebaseAI/Sources/ModelContent.swift

View workflow job for this annotation

GitHub Actions / spm / spm (macos-14, Xcode_16.2, iOS)

conformance of 'Array<Element>' to 'PartsRepresentable' is only available in iOS 15.0 or newer; this is an error in the Swift 6 language mode
}
}
27 changes: 27 additions & 0 deletions FirebaseAI/Tests/Unit/PartsRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ import XCTest

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class PartsRepresentableTests: XCTestCase {
func testModelContentBuilderBuildsFromParts() {
let testConditional = false
@ModelContentBuilder func builderTester() -> ModelContent {
"A string prompt of some kind"

if testConditional {
"A single-conditional string prompt"
}

if Int.random(in: 0 ..< 5) > 2 {
"A true-branch conditional string prompt"
} else {
"A false-branch conditional string prompt"
}

for _ in 0 ..< 10 {
"A looped string prompt"
}

"Finally, a non-conditional string prompt"
}

let content = builderTester()
XCTAssert(content.parts.count == 13,
"Expected 14 parts, got \(content.parts.count): \(content.parts)")
}

#if !os(watchOS)
func testModelContentFromCGImageIsNotEmpty() throws {
// adapted from https://forums.swift.org/t/creating-a-cgimage-from-color-array/18634/2
Expand Down
Loading