Skip to content

Commit e9ffbad

Browse files
authored
feat(firebase_ai): add LiveServerGoAway message for session termination (#17843)
* feat(firebase_ai): add LiveServerGoAway message for session termination * feat(firebase_ai): add test for parsing goAway message in LiveServer response * chore: rename LiveServerGoAway to GoingAwayNotice and update related tests
1 parent 5f8c8e8 commit e9ffbad

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

packages/firebase_ai/firebase_ai/lib/firebase_ai.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export 'src/live_api.dart'
107107
LiveServerToolCall,
108108
LiveServerToolCallCancellation,
109109
LiveServerResponse,
110+
GoingAwayNotice,
110111
Transcription;
111112
export 'src/live_session.dart' show LiveSession;
112113
export 'src/schema.dart' show Schema, SchemaType;

packages/firebase_ai/firebase_ai/lib/src/live_api.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ class LiveServerToolCallCancellation implements LiveServerMessage {
209209
final List<String>? functionIds;
210210
}
211211

212+
/// A server message indicating that the server will not be able to service the
213+
/// client soon.
214+
class GoingAwayNotice implements LiveServerMessage {
215+
/// Creates a [GoingAwayNotice] instance.
216+
///
217+
/// [timeLeft] (optional): The remaining time before the connection will be
218+
/// terminated.
219+
const GoingAwayNotice({this.timeLeft});
220+
221+
/// The remaining time before the connection will be terminated as ABORTED.
222+
final String? timeLeft;
223+
}
224+
212225
/// A single response chunk received during a live content generation.
213226
///
214227
/// It can contain generated content, function calls to be executed, or
@@ -435,6 +448,9 @@ LiveServerMessage _parseServerMessage(Object jsonObject) {
435448
return LiveServerToolCallCancellation(functionIds: toolCancelJson['ids']);
436449
} else if (json.containsKey('setupComplete')) {
437450
return LiveServerSetupComplete();
451+
} else if (json.containsKey('goAway')) {
452+
final goAwayJson = json['goAway'] as Map;
453+
return GoingAwayNotice(timeLeft: goAwayJson['timeLeft'] as String?);
438454
} else {
439455
throw unhandledFormat('LiveServerMessage', json);
440456
}

packages/firebase_ai/firebase_ai/test/live_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ void main() {
228228
expect(response.message, isA<LiveServerSetupComplete>());
229229
});
230230

231+
test('parseServerMessage parses goAway message correctly', () {
232+
final jsonObject = {
233+
'goAway': {'timeLeft': '50s'}
234+
};
235+
final response = parseServerResponse(jsonObject);
236+
expect(response.message, isA<GoingAwayNotice>());
237+
final goAwayMessage = response.message as GoingAwayNotice;
238+
expect(goAwayMessage.timeLeft, '50s');
239+
});
240+
231241
test('parseServerMessage throws VertexAIException for error message', () {
232242
final jsonObject = {'error': {}};
233243
expect(() => parseServerResponse(jsonObject),

0 commit comments

Comments
 (0)