Skip to content

Commit 7902349

Browse files
committed
Add the close method
1 parent dba2bc0 commit 7902349

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

FirebaseAI/Sources/Types/Internal/Live/LiveSessionService.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ actor LiveSessionService {
125125
}
126126
}
127127

128+
/// Cancel any running tasks and close the websocket.
129+
///
130+
/// This method is idempotent; if it's already ran once, it will effectively be a no-op.
131+
public func close() {
132+
setupTask.cancel()
133+
responsesTask?.cancel()
134+
messageQueueTask?.cancel()
135+
webSocket?.disconnect()
136+
137+
webSocket = nil
138+
responsesTask = nil
139+
messageQueueTask = nil
140+
}
141+
128142
/// Start a fresh websocket to the backend, and listen for responses.
129143
///
130144
/// Will hold off on sending any messages until the server sends a setupComplete mesage.
@@ -269,20 +283,6 @@ actor LiveSessionService {
269283
}
270284
}
271285

272-
/// Cancel any running tasks and close the websocket.
273-
///
274-
/// This method is idempotent; if it's already ran once, it will effectively be a no-op.
275-
private func close() {
276-
setupTask.cancel()
277-
responsesTask?.cancel()
278-
messageQueueTask?.cancel()
279-
webSocket?.disconnect()
280-
281-
webSocket = nil
282-
responsesTask = nil
283-
messageQueueTask = nil
284-
}
285-
286286
/// Creates a websocket pointing to the backend.
287287
///
288288
/// Will apply the required app check and auth headers, as the backend expects them.

FirebaseAI/Sources/Types/Public/Live/LiveSession.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,15 @@ public final class LiveSession: Sendable {
126126
turnComplete: Bool? = nil) async {
127127
await sendContent([ModelContent(parts: parts)], turnComplete: turnComplete)
128128
}
129+
130+
/// Permanently stop the conversation with the model, and close the connection to the server
131+
///
132+
/// This method will be called automatically when the ``LiveSession`` is deinitialized, but this method
133+
/// can be called manually to explicitly end the session.
134+
///
135+
/// Attempting to receive content from a closed session will cause a ``LiveSessionUnexpectedClosureError`` error
136+
/// to be thrown.
137+
public func close() async {
138+
await service.close()
139+
}
129140
}

0 commit comments

Comments
 (0)