Skip to content

Commit da80fc8

Browse files
authored
[VertexAI] Refactor the LiveSession responses (#1231)
* [VertexAI] Refactor the LiveSession responses * Rename the file
1 parent e06b4ce commit da80fc8

File tree

5 files changed

+257
-177
lines changed

5 files changed

+257
-177
lines changed

vertexai/src/Internal/InternalHelpers.cs

+20
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ public static T ParseObject<T>(this JsonDict jsonDict, string key,
163163
}
164164
}
165165

166+
// Tries to convert the found List of objects to a List of strings.
167+
public static bool TryParseStringList(this JsonDict jsonDict, string key,
168+
out List<string> values,
169+
JsonParseOptions options = JsonParseOptions.ThrowInvalidCast) {
170+
if (jsonDict.TryParseValue(key, out List<object> list, options)) {
171+
values = list.OfType<string>().ToList();
172+
return true;
173+
} else {
174+
values = null;
175+
return false;
176+
}
177+
}
178+
179+
// Casts the found List to a string List, otherwise returns default (or throws)
180+
public static List<string> ParseStringList(this JsonDict jsonDict, string key,
181+
JsonParseOptions options = JsonParseOptions.ThrowInvalidCast) {
182+
TryParseStringList(jsonDict, key, out List<string> values, options);
183+
return values;
184+
}
185+
166186
// Tries to convert the found List of Dictionaries, using the given function.
167187
public static bool TryParseObjectList<T>(this JsonDict jsonDict, string key,
168188
Func<JsonDict, T> parseFunc, out List<T> values,

vertexai/src/LiveContentResponse.cs

-174
This file was deleted.

vertexai/src/LiveSession.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public Task SendAudioAsync(float[] audioData, CancellationToken cancellationToke
175175
/// <param name="closeOnTurnComplete">Should the stream stop when the server returns TurnComplete.</param>
176176
/// <param name="cancellationToken">A token to cancel the operation.</param>
177177
/// <returns>A stream of `LiveContentResponse`s from the backend.</returns>
178-
public async IAsyncEnumerable<LiveContentResponse> ReceiveAsync(
178+
public async IAsyncEnumerable<LiveSessionResponse> ReceiveAsync(
179179
bool closeOnTurnComplete = true,
180180
[EnumeratorCancellation] CancellationToken cancellationToken = default) {
181181
if (_clientWebSocket.State != WebSocketState.Open) {
@@ -199,14 +199,16 @@ public async IAsyncEnumerable<LiveContentResponse> ReceiveAsync(
199199
messageBuilder.Append(Encoding.UTF8.GetString(receiveBuffer, 0, result.Count));
200200

201201
if (result.EndOfMessage) {
202-
LiveContentResponse? response = LiveContentResponse.FromJson(messageBuilder.ToString());
202+
LiveSessionResponse? response = LiveSessionResponse.FromJson(messageBuilder.ToString());
203203
// Reset for the next message.
204204
messageBuilder.Clear();
205205

206206
if (response != null) {
207207
yield return response.Value;
208208

209-
if (closeOnTurnComplete && response?.Status == LiveContentResponse.LiveResponseStatus.TurnComplete) {
209+
if (closeOnTurnComplete &&
210+
response?.Message is LiveSessionContent serverContent &&
211+
serverContent.TurnComplete) {
210212
break;
211213
}
212214
}

0 commit comments

Comments
 (0)