File tree Expand file tree Collapse file tree 2 files changed +25
-14
lines changed Expand file tree Collapse file tree 2 files changed +25
-14
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,20 @@ actor LiveSessionService {
125
125
}
126
126
}
127
127
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
+
128
142
/// Start a fresh websocket to the backend, and listen for responses.
129
143
///
130
144
/// Will hold off on sending any messages until the server sends a setupComplete mesage.
@@ -269,20 +283,6 @@ actor LiveSessionService {
269
283
}
270
284
}
271
285
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
-
286
286
/// Creates a websocket pointing to the backend.
287
287
///
288
288
/// Will apply the required app check and auth headers, as the backend expects them.
Original file line number Diff line number Diff line change @@ -126,4 +126,15 @@ public final class LiveSession: Sendable {
126
126
turnComplete: Bool ? = nil ) async {
127
127
await sendContent ( [ ModelContent ( parts: parts) ] , turnComplete: turnComplete)
128
128
}
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
+ }
129
140
}
You can’t perform that action at this time.
0 commit comments