-
Notifications
You must be signed in to change notification settings - Fork 129
✨ Implement structured generation and Remote Config #141
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
Merged
peterfriese
merged 4 commits into
peterfriese/friendlymeals-functioncalling-optimize
from
peterfriese/friendlymeals-structure-output
Dec 1, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...eals/apple/FriendlyMeals/FriendlyMeals/Features/Services/GenerationConfig+Decodable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // | ||
| // FriendlyMeals | ||
| // | ||
| // Copyright © 2025 Google LLC. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import Foundation | ||
| import FirebaseAI | ||
|
|
||
| extension ResponseModality: @retroactive Decodable { | ||
| public init(from decoder: Decoder) throws { | ||
| let container = try decoder.singleValueContainer() | ||
| let rawValue = try container.decode(String.self) | ||
| switch rawValue { | ||
| case "TEXT": | ||
| self = .text | ||
| case "IMAGE": | ||
| self = .image | ||
| default: | ||
| throw DecodingError.dataCorruptedError( | ||
| in: container, | ||
| debugDescription: "Invalid ResponseModality raw value '\(rawValue)'" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension GenerationConfig: @retroactive Decodable { | ||
| private enum CodingKeys: String, CodingKey { | ||
| case temperature | ||
| case topP | ||
| case topK | ||
| case maxOutputTokens | ||
| case responseModalities | ||
| } | ||
|
|
||
| public init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
| let temperature = try container.decodeIfPresent(Float.self, forKey: .temperature) | ||
| let topP = try container.decodeIfPresent(Float.self, forKey: .topP) | ||
| let topK = try container.decodeIfPresent(Int.self, forKey: .topK) | ||
| let maxOutputTokens = try container.decodeIfPresent(Int.self, forKey: .maxOutputTokens) | ||
| let responseModalities = try container.decodeIfPresent([ResponseModality].self, forKey: .responseModalities) ?? [] | ||
|
|
||
| self.init(temperature: temperature, topP: topP, topK: topK, maxOutputTokens: maxOutputTokens, responseModalities: responseModalities) | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...endly-meals/apple/FriendlyMeals/FriendlyMeals/Features/Services/RemoteConfigService.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // | ||
| // FriendlyMeals | ||
| // | ||
| // Copyright © 2025 Google LLC. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import Foundation | ||
| import FirebaseRemoteConfig | ||
| import FirebaseAI | ||
|
|
||
| fileprivate enum RemoteConfigKey: String { | ||
| case maxImagesPerDay = "max_images_per_day" | ||
| case modelName = "model_name" | ||
| case generationConfig = "generation_config" | ||
| } | ||
|
|
||
| @Observable | ||
| class RemoteConfigService { | ||
| static let shared = RemoteConfigService() | ||
|
|
||
| var maxImagesPerDay: Int = 5 | ||
| var modelName: String = "gemini-2.0-flash-preview-image-generation" | ||
| var generationConfig: GenerationConfig? | ||
|
|
||
| private var remoteConfig: RemoteConfig | ||
|
|
||
| private init() { | ||
| remoteConfig = RemoteConfig.remoteConfig() | ||
| let settings = RemoteConfigSettings() | ||
| settings.minimumFetchInterval = 0 | ||
| remoteConfig.configSettings = settings | ||
| setDefaults() | ||
| listenForUpdates() | ||
| } | ||
|
|
||
| private func setDefaults() { | ||
| remoteConfig.setDefaults(fromPlist: "remote_config_defaults") | ||
| } | ||
|
|
||
| private func listenForUpdates() { | ||
| remoteConfig.addOnConfigUpdateListener { [weak self] configUpdate, error in | ||
| guard let self = self else { return } | ||
| if let error = error { | ||
| print("Error listening for config updates: \(error.localizedDescription)") | ||
| return | ||
| } | ||
|
|
||
| print("Updated keys: \(String(describing: configUpdate?.updatedKeys))") | ||
| Task { @MainActor in | ||
| do { | ||
| let changed = try await self.remoteConfig.activate() | ||
| if changed { | ||
| self.updateParameters() | ||
| } | ||
| } | ||
| catch { | ||
| print("Error activating config: \(error.localizedDescription)") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func updateParameters() { | ||
| maxImagesPerDay = remoteConfig[RemoteConfigKey.maxImagesPerDay.rawValue].numberValue.intValue | ||
| modelName = remoteConfig[RemoteConfigKey.modelName.rawValue].stringValue | ||
| do { | ||
| generationConfig = try remoteConfig[RemoteConfigKey.generationConfig.rawValue].decoded(asType: GenerationConfig.self) | ||
| } | ||
| catch { | ||
| print("Error decoding generation config: \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| func fetchConfig() async throws { | ||
| try await remoteConfig.fetch() | ||
| try await remoteConfig.activate() | ||
| self.updateParameters() | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
...ndly-meals/apple/FriendlyMeals/FriendlyMeals/Features/Services/UsageTrackingService.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // | ||
| // FriendlyMeals | ||
| // | ||
| // Copyright © 2025 Google LLC. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import Foundation | ||
|
|
||
| class UsageTrackingService { | ||
| static let shared = UsageTrackingService() | ||
|
|
||
| private let userDefaults = UserDefaults.standard | ||
| private let generationCountKey = "generationCount" | ||
| private let lastGenerationDateKey = "lastGenerationDate" | ||
|
|
||
| private init() {} | ||
|
|
||
| func canGenerate() -> Bool { | ||
| resetCountIfNeeded() | ||
| let count = userDefaults.integer(forKey: generationCountKey) | ||
| let maxImagesPerDay = RemoteConfigService.shared.maxImagesPerDay | ||
| print("Checking if user can generate images. Count: \(count), Max: \(maxImagesPerDay)") | ||
| return count < maxImagesPerDay | ||
| } | ||
|
|
||
| func incrementGenerationCount() { | ||
| resetCountIfNeeded() | ||
| let count = userDefaults.integer(forKey: generationCountKey) | ||
| userDefaults.set(count + 1, forKey: generationCountKey) | ||
| } | ||
|
|
||
| private func resetCounter() { | ||
| userDefaults.set(Date(), forKey: lastGenerationDateKey) | ||
| userDefaults.set(0, forKey: generationCountKey) | ||
| } | ||
|
|
||
| private func resetCountIfNeeded() { | ||
| guard let lastGenerationDate = userDefaults.object(forKey: lastGenerationDateKey) as? Date else { | ||
| resetCounter() | ||
| return | ||
| } | ||
|
|
||
| if !Calendar.current.isDateInToday(lastGenerationDate) { | ||
| resetCounter() | ||
| } | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
...friendly-meals/apple/FriendlyMeals/FriendlyMeals/Features/SuggestRecipe/PaywallView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // | ||
| // FriendlyMeals | ||
| // | ||
| // Copyright © 2025 Google LLC. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import SwiftUI | ||
|
|
||
| struct PaywallView: View { | ||
| @Environment(\.dismiss) private var dismiss | ||
|
|
||
| var body: some View { | ||
| ZStack(alignment: .topTrailing) { | ||
| // Close Button | ||
| Button(action: { dismiss() }) { | ||
| Image(systemName: "xmark.circle.fill") | ||
| .font(.title) | ||
| .foregroundColor(.gray.opacity(0.6)) | ||
| } | ||
| .padding() | ||
|
|
||
| VStack(spacing: 20) { | ||
| Spacer() | ||
|
|
||
| // Icon | ||
| Image(systemName: "crown.fill") | ||
| .font(.system(size: 60)) | ||
| .foregroundColor(.yellow) | ||
|
|
||
| // Title and Subtitle | ||
| Text("Upgrade to Premium") | ||
| .font(.largeTitle) | ||
| .fontWeight(.bold) | ||
| .multilineTextAlignment(.center) | ||
|
|
||
| Text("Unlock unlimited recipe generations and more!") | ||
| .font(.headline) | ||
| .multilineTextAlignment(.center) | ||
| .foregroundColor(.secondary) | ||
|
|
||
| Spacer() | ||
|
|
||
| // Feature List | ||
| VStack(alignment: .leading, spacing: 15) { | ||
| FeatureView(text: "Unlimited recipe suggestions") | ||
| FeatureView(text: "Generate images for every recipe") | ||
| FeatureView(text: "Save your favorite recipes") | ||
| FeatureView(text: "Access exclusive meal plans") | ||
| } | ||
| .padding(.horizontal) | ||
|
|
||
| Spacer() | ||
|
|
||
| // Call to Action Button | ||
| Button(action: { | ||
| // Mock action | ||
| print("Upgrade button tapped!") | ||
| dismiss() | ||
| }) { | ||
| Text("Unlock Premium") | ||
| .font(.headline) | ||
| .fontWeight(.semibold) | ||
| .foregroundColor(.white) | ||
| .frame(maxWidth: .infinity) | ||
| .padding() | ||
| .background(Color.blue) | ||
| .cornerRadius(12) | ||
| } | ||
| .padding(.horizontal, 40) | ||
|
|
||
| // Restore Purchases Button | ||
| Button(action: { | ||
| // Mock action | ||
| print("Restore purchases tapped!") | ||
| }) { | ||
| Text("Restore Purchases") | ||
| .font(.footnote) | ||
| .foregroundColor(.secondary) | ||
| } | ||
| .padding(.bottom) | ||
| } | ||
| .padding() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct FeatureView: View { | ||
| let text: String | ||
|
|
||
| var body: some View { | ||
| HStack(spacing: 12) { | ||
| Image(systemName: "checkmark.circle.fill") | ||
| .foregroundColor(.blue) | ||
| Text(text) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| PaywallView() | ||
| } |
31 changes: 31 additions & 0 deletions
31
...e-ai-friendly-meals/apple/FriendlyMeals/FriendlyMeals/Features/SuggestRecipe/Recipe.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // | ||
| // FriendlyMeals | ||
| // | ||
| // Copyright © 2025 Google LLC. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import Foundation | ||
|
|
||
| struct Recipe: Decodable { | ||
| struct Ingredient: Decodable { | ||
| var name: String | ||
| var amount: String | ||
| } | ||
|
|
||
| var title: String | ||
| var description: String | ||
| var cookingTime: Int | ||
| var ingredients: [Ingredient] | ||
| var instructions: [String] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.