Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/firebase_ai/firebase_ai/lib/firebase_ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export 'src/live_api.dart'
LiveServerToolCall,
LiveServerToolCallCancellation,
LiveServerResponse,
GoingAwayNotice,
Transcription;
export 'src/live_session.dart' show LiveSession;
export 'src/schema.dart' show Schema, SchemaType;
Expand Down
16 changes: 16 additions & 0 deletions packages/firebase_ai/firebase_ai/lib/src/live_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ class LiveServerToolCallCancellation implements LiveServerMessage {
final List<String>? functionIds;
}

/// A server message indicating that the server will not be able to service the
/// client soon.
class GoingAwayNotice implements LiveServerMessage {
/// Creates a [GoingAwayNotice] instance.
///
/// [timeLeft] (optional): The remaining time before the connection will be
/// terminated.
const GoingAwayNotice({this.timeLeft});

/// The remaining time before the connection will be terminated as ABORTED.
final String? timeLeft;
}

/// A single response chunk received during a live content generation.
///
/// It can contain generated content, function calls to be executed, or
Expand Down Expand Up @@ -435,6 +448,9 @@ LiveServerMessage _parseServerMessage(Object jsonObject) {
return LiveServerToolCallCancellation(functionIds: toolCancelJson['ids']);
} else if (json.containsKey('setupComplete')) {
return LiveServerSetupComplete();
} else if (json.containsKey('goAway')) {
final goAwayJson = json['goAway'] as Map;
return GoingAwayNotice(timeLeft: goAwayJson['timeLeft'] as String?);
} else {
throw unhandledFormat('LiveServerMessage', json);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/firebase_ai/firebase_ai/test/live_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ void main() {
expect(response.message, isA<LiveServerSetupComplete>());
});

test('parseServerMessage parses goAway message correctly', () {
final jsonObject = {
'goAway': {'timeLeft': '50s'}
};
final response = parseServerResponse(jsonObject);
expect(response.message, isA<GoingAwayNotice>());
final goAwayMessage = response.message as GoingAwayNotice;
expect(goAwayMessage.timeLeft, '50s');
});

test('parseServerMessage throws VertexAIException for error message', () {
final jsonObject = {'error': {}};
expect(() => parseServerResponse(jsonObject),
Expand Down
Loading