@@ -183,4 +183,46 @@ final class APITests: XCTestCase {
183
183
let _: String ? = response. text
184
184
let _: [ FunctionCallPart ] = response. functionCalls
185
185
}
186
+
187
+ func liveSessionAPI( ) async throws {
188
+ let firebaseAI = FirebaseAI . firebaseAI ( )
189
+
190
+ // Initialize a Live Model
191
+ let liveModel = firebaseAI. liveModel ( modelName: " gemini-2.0-flash-live-001 " )
192
+
193
+ // Start a Live session
194
+ let session = try await liveModel. connect ( )
195
+
196
+ // Add history incrementally to the session
197
+ await session. sendContent ( " Where is Google headquarters located? " )
198
+ await session. sendContent ( " Respond in the format 'City, State' " , turnComplete: true )
199
+ await session. sendContent (
200
+ [ ModelContent ( role: " model " , parts: [ TextPart ( " Mountain View, California " ) ] ) ] ,
201
+ turnComplete: true
202
+ )
203
+
204
+ // Send realtime data
205
+ await session. sendTextRealtime ( " What year was it founded? " )
206
+ await session. sendAudioRealtime ( Data ( ) )
207
+ await session. sendVideoRealtime ( Data ( ) , format: " mp4 " )
208
+
209
+ // Handle response content
210
+ for try await response in session. responses {
211
+ switch response {
212
+ case let serverContent as LiveServerContent :
213
+ print ( " Server content: \( serverContent) " )
214
+ case let toolCall as LiveServerToolCall :
215
+ print ( " Tool call: \( toolCall) " )
216
+ case let toolCallCancellation as LiveServerToolCallCancellation :
217
+ print ( " Tool call cancellation: \( toolCallCancellation) " )
218
+ case let goingAway as LiveServerGoingAwayNotice :
219
+ print ( " Session is going away: \( goingAway) " )
220
+ default :
221
+ print ( " Unexpected response type: \( response) " )
222
+ }
223
+ }
224
+
225
+ // Close a Live session
226
+ await session. close ( )
227
+ }
186
228
}
0 commit comments