Skip to content

Commit 06e19aa

Browse files
committed
Fix build on Xcode 15
1 parent 839bbca commit 06e19aa

File tree

5 files changed

+203
-187
lines changed

5 files changed

+203
-187
lines changed

FirebaseAI/Sources/GenerativeModel.swift

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -366,49 +366,51 @@ public final class GenerativeModel: Sendable {
366366
return try await generativeAIService.loadRequest(request: countTokensRequest)
367367
}
368368

369-
/// Produces a generable object as a response to a prompt.
370-
///
371-
/// - Parameters:
372-
/// - prompt: A prompt for the model to respond to.
373-
/// - type: A type to produce as the response.
374-
/// - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.
375-
@available(iOS 26.0, macOS 26.0, *)
376-
@available(tvOS, unavailable)
377-
@available(watchOS, unavailable)
378-
public final func generateObject<Content>(_ type: Content.Type = Content.self,
379-
parts: any PartsRepresentable...) async throws
380-
-> Response<Content>
381-
where Content: FoundationModels.Generable {
382-
let jsonSchema = try type.generationSchema.asGeminiJSONSchema()
383-
384-
let generationConfig = {
385-
var generationConfig = self.generationConfig ?? GenerationConfig()
386-
if generationConfig.candidateCount != nil {
387-
generationConfig.candidateCount = nil
388-
}
389-
generationConfig.responseMIMEType = "application/json"
390-
if generationConfig.responseSchema != nil {
391-
generationConfig.responseSchema = nil
392-
}
393-
generationConfig.responseJSONSchema = jsonSchema
394-
if generationConfig.responseModalities != nil {
395-
generationConfig.responseModalities = nil
396-
}
369+
#if canImport(FoundationModels)
370+
/// Produces a generable object as a response to a prompt.
371+
///
372+
/// - Parameters:
373+
/// - prompt: A prompt for the model to respond to.
374+
/// - type: A type to produce as the response.
375+
/// - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.
376+
@available(iOS 26.0, macOS 26.0, *)
377+
@available(tvOS, unavailable)
378+
@available(watchOS, unavailable)
379+
public final func generateObject<Content>(_ type: Content.Type = Content.self,
380+
parts: any PartsRepresentable...) async throws
381+
-> Response<Content>
382+
where Content: FoundationModels.Generable {
383+
let jsonSchema = try type.generationSchema.asGeminiJSONSchema()
384+
385+
let generationConfig = {
386+
var generationConfig = self.generationConfig ?? GenerationConfig()
387+
if generationConfig.candidateCount != nil {
388+
generationConfig.candidateCount = nil
389+
}
390+
generationConfig.responseMIMEType = "application/json"
391+
if generationConfig.responseSchema != nil {
392+
generationConfig.responseSchema = nil
393+
}
394+
generationConfig.responseJSONSchema = jsonSchema
395+
if generationConfig.responseModalities != nil {
396+
generationConfig.responseModalities = nil
397+
}
397398

398-
return generationConfig
399-
}()
399+
return generationConfig
400+
}()
400401

401-
let response = try await generateContent(
402-
[ModelContent(parts: parts)],
403-
generationConfig: generationConfig
404-
)
402+
let response = try await generateContent(
403+
[ModelContent(parts: parts)],
404+
generationConfig: generationConfig
405+
)
405406

406-
let generatedContent = try GeneratedContent(json: response.text ?? "")
407-
let content = try Content(generatedContent)
408-
let rawContent = try ModelOutput(generatedContent)
407+
let generatedContent = try GeneratedContent(json: response.text ?? "")
408+
let content = try Content(generatedContent)
409+
let rawContent = try ModelOutput(generatedContent)
409410

410-
return Response(content: content, rawContent: rawContent)
411-
}
411+
return Response(content: content, rawContent: rawContent)
412+
}
413+
#endif // canImport(FoundationModels)
412414

413415
/// Returns a `GenerateContentError` (for public consumption) from an internal error.
414416
///
@@ -421,9 +423,7 @@ public final class GenerativeModel: Sendable {
421423
}
422424

423425
/// A structure that stores the output of a response call.
424-
@available(iOS 26.0, macOS 26.0, *)
425-
@available(tvOS, unavailable)
426-
@available(watchOS, unavailable)
426+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
427427
public struct Response<Content> {
428428
/// The response content.
429429
public let content: Content

FirebaseAI/Sources/Types/Public/Generable/ConvertibleFromModelOutput.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,21 @@ public protocol ConvertibleFromModelOutput: SendableMetatype {
4646
init(_ content: ModelOutput) throws
4747
}
4848

49-
@available(iOS 26.0, macOS 26.0, *)
50-
@available(tvOS, unavailable)
51-
@available(watchOS, unavailable)
52-
extension FoundationModels.GeneratedContent: ConvertibleFromModelOutput {}
49+
#if canImport(FoundationModels)
50+
@available(iOS 26.0, macOS 26.0, *)
51+
@available(tvOS, unavailable)
52+
@available(watchOS, unavailable)
53+
extension FoundationModels.GeneratedContent: ConvertibleFromModelOutput {}
54+
#endif // canImport(FoundationModels)
5355

54-
@available(iOS 26.0, macOS 26.0, *)
55-
@available(tvOS, unavailable)
56-
@available(watchOS, unavailable)
57-
public extension FoundationModels.ConvertibleFromGeneratedContent
58-
where Self: ConvertibleFromModelOutput {
59-
init(_ content: ModelOutput) throws {
60-
try self.init(content.generatedContent)
56+
#if canImport(FoundationModels)
57+
@available(iOS 26.0, macOS 26.0, *)
58+
@available(tvOS, unavailable)
59+
@available(watchOS, unavailable)
60+
public extension FoundationModels.ConvertibleFromGeneratedContent
61+
where Self: ConvertibleFromModelOutput {
62+
init(_ content: ModelOutput) throws {
63+
try self.init(content.generatedContent)
64+
}
6165
}
62-
}
66+
#endif // canImport(FoundationModels)

FirebaseAI/Sources/Types/Public/Generable/ConvertibleToModelOutput.swift

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,48 +46,54 @@ public protocol ConvertibleToModelOutput {
4646
var modelOutput: ModelOutput { get }
4747
}
4848

49-
@available(iOS 26.0, macOS 26.0, *)
50-
@available(tvOS, unavailable)
51-
@available(watchOS, unavailable)
52-
extension FoundationModels.GeneratedContent: ConvertibleToModelOutput {
53-
public var modelOutput: ModelOutput {
54-
switch kind {
55-
case .null:
56-
return ModelOutput(kind: .null)
57-
case let .bool(value):
58-
return ModelOutput(kind: .bool(value))
59-
case let .number(value):
60-
return ModelOutput(kind: .number(value))
61-
case let .string(value):
62-
return ModelOutput(kind: .string(value))
63-
case let .array(values):
64-
return ModelOutput(kind: .array(values.map { $0.modelOutput }))
65-
case let .structure(properties: properties, orderedKeys: orderedKeys):
66-
return ModelOutput(kind: .structure(
67-
properties: properties.mapValues { $0.modelOutput }, orderedKeys: orderedKeys
68-
))
69-
@unknown default:
70-
fatalError("Unsupported GeneratedContent kind: \(kind)")
49+
#if canImport(FoundationModels)
50+
@available(iOS 26.0, macOS 26.0, *)
51+
@available(tvOS, unavailable)
52+
@available(watchOS, unavailable)
53+
extension FoundationModels.GeneratedContent: ConvertibleToModelOutput {
54+
public var modelOutput: ModelOutput {
55+
switch kind {
56+
case .null:
57+
return ModelOutput(kind: .null)
58+
case let .bool(value):
59+
return ModelOutput(kind: .bool(value))
60+
case let .number(value):
61+
return ModelOutput(kind: .number(value))
62+
case let .string(value):
63+
return ModelOutput(kind: .string(value))
64+
case let .array(values):
65+
return ModelOutput(kind: .array(values.map { $0.modelOutput }))
66+
case let .structure(properties: properties, orderedKeys: orderedKeys):
67+
return ModelOutput(kind: .structure(
68+
properties: properties.mapValues { $0.modelOutput }, orderedKeys: orderedKeys
69+
))
70+
@unknown default:
71+
fatalError("Unsupported GeneratedContent kind: \(kind)")
72+
}
7173
}
7274
}
73-
}
75+
#endif // canImport(FoundationModels)
7476

75-
@available(iOS 26.0, macOS 26.0, *)
76-
@available(tvOS, unavailable)
77-
@available(watchOS, unavailable)
78-
public extension FoundationModels.ConvertibleToGeneratedContent
79-
where Self: ConvertibleToModelOutput {
80-
var generatedContent: GeneratedContent {
81-
modelOutput.generatedContent
77+
#if canImport(FoundationModels)
78+
@available(iOS 26.0, macOS 26.0, *)
79+
@available(tvOS, unavailable)
80+
@available(watchOS, unavailable)
81+
public extension FoundationModels.ConvertibleToGeneratedContent
82+
where Self: ConvertibleToModelOutput {
83+
var generatedContent: GeneratedContent {
84+
modelOutput.generatedContent
85+
}
8286
}
83-
}
87+
#endif // canImport(FoundationModels)
8488

85-
@available(iOS 26.0, macOS 26.0, *)
86-
@available(tvOS, unavailable)
87-
@available(watchOS, unavailable)
88-
public extension ConvertibleToModelOutput
89-
where Self: FoundationModels.ConvertibleToGeneratedContent {
90-
var modelOutput: ModelOutput {
91-
generatedContent.modelOutput
89+
#if canImport(FoundationModels)
90+
@available(iOS 26.0, macOS 26.0, *)
91+
@available(tvOS, unavailable)
92+
@available(watchOS, unavailable)
93+
public extension ConvertibleToModelOutput
94+
where Self: FoundationModels.ConvertibleToGeneratedContent {
95+
var modelOutput: ModelOutput {
96+
generatedContent.modelOutput
97+
}
9298
}
93-
}
99+
#endif // canImport(FoundationModels)

FirebaseAI/Sources/Types/Public/Generable/ModelOutput.swift

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -257,52 +257,56 @@ public extension ModelOutput {
257257
}
258258
}
259259

260-
@available(iOS 26.0, macOS 26.0, *)
261-
@available(tvOS, unavailable)
262-
@available(watchOS, unavailable)
263-
extension ModelOutput: FoundationModels.ConvertibleFromGeneratedContent {
264-
public init(_ content: GeneratedContent) throws {
265-
switch content.kind {
266-
case .null:
267-
self.init(kind: .null)
268-
case let .bool(value):
269-
self.init(kind: .bool(value))
270-
case let .number(value):
271-
self.init(kind: .number(value))
272-
case let .string(value):
273-
self.init(kind: .string(value))
274-
case let .array(values):
275-
self.init(kind: .array(values.map { $0.modelOutput }))
276-
case let .structure(properties: properties, orderedKeys: orderedKeys):
277-
self.init(kind: .structure(
278-
properties: properties.mapValues { $0.modelOutput }, orderedKeys: orderedKeys
279-
))
280-
@unknown default:
281-
fatalError("Unsupported GeneratedContent kind: \(content.kind)")
260+
#if canImport(FoundationModels)
261+
@available(iOS 26.0, macOS 26.0, *)
262+
@available(tvOS, unavailable)
263+
@available(watchOS, unavailable)
264+
extension ModelOutput: FoundationModels.ConvertibleFromGeneratedContent {
265+
public init(_ content: GeneratedContent) throws {
266+
switch content.kind {
267+
case .null:
268+
self.init(kind: .null)
269+
case let .bool(value):
270+
self.init(kind: .bool(value))
271+
case let .number(value):
272+
self.init(kind: .number(value))
273+
case let .string(value):
274+
self.init(kind: .string(value))
275+
case let .array(values):
276+
self.init(kind: .array(values.map { $0.modelOutput }))
277+
case let .structure(properties: properties, orderedKeys: orderedKeys):
278+
self.init(kind: .structure(
279+
properties: properties.mapValues { $0.modelOutput }, orderedKeys: orderedKeys
280+
))
281+
@unknown default:
282+
fatalError("Unsupported GeneratedContent kind: \(content.kind)")
283+
}
282284
}
283285
}
284-
}
286+
#endif // canImport(FoundationModels)
285287

286-
@available(iOS 26.0, macOS 26.0, *)
287-
@available(tvOS, unavailable)
288-
@available(watchOS, unavailable)
289-
extension ModelOutput: FoundationModels.ConvertibleToGeneratedContent {
290-
public var generatedContent: GeneratedContent {
291-
switch modelOutput.kind {
292-
case .null:
293-
return GeneratedContent(kind: .null)
294-
case let .bool(value):
295-
return GeneratedContent(kind: .bool(value))
296-
case let .number(value):
297-
return GeneratedContent(kind: .number(value))
298-
case let .string(value):
299-
return GeneratedContent(kind: .string(value))
300-
case let .array(values):
301-
return GeneratedContent(kind: .array(values.map { $0.generatedContent }))
302-
case let .structure(properties: properties, orderedKeys: orderedKeys):
303-
return GeneratedContent(kind: .structure(
304-
properties: properties.mapValues { $0.generatedContent }, orderedKeys: orderedKeys
305-
))
288+
#if canImport(FoundationModels)
289+
@available(iOS 26.0, macOS 26.0, *)
290+
@available(tvOS, unavailable)
291+
@available(watchOS, unavailable)
292+
extension ModelOutput: FoundationModels.ConvertibleToGeneratedContent {
293+
public var generatedContent: GeneratedContent {
294+
switch modelOutput.kind {
295+
case .null:
296+
return GeneratedContent(kind: .null)
297+
case let .bool(value):
298+
return GeneratedContent(kind: .bool(value))
299+
case let .number(value):
300+
return GeneratedContent(kind: .number(value))
301+
case let .string(value):
302+
return GeneratedContent(kind: .string(value))
303+
case let .array(values):
304+
return GeneratedContent(kind: .array(values.map { $0.generatedContent }))
305+
case let .structure(properties: properties, orderedKeys: orderedKeys):
306+
return GeneratedContent(kind: .structure(
307+
properties: properties.mapValues { $0.generatedContent }, orderedKeys: orderedKeys
308+
))
309+
}
306310
}
307311
}
308-
}
312+
#endif // canImport(FoundationModels)

0 commit comments

Comments
 (0)