Skip to content

[VertexAI] Refactor the LiveSession responses #1231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions vertexai/src/Internal/InternalHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ public static T ParseObject<T>(this JsonDict jsonDict, string key,
}
}

// Tries to convert the found List of objects to a List of strings.
public static bool TryParseStringList(this JsonDict jsonDict, string key,
out List<string> values,
JsonParseOptions options = JsonParseOptions.ThrowInvalidCast) {
if (jsonDict.TryParseValue(key, out List<object> list, options)) {
values = list.OfType<string>().ToList();
return true;
} else {
values = null;
return false;
}
}

// Casts the found List to a string List, otherwise returns default (or throws)
public static List<string> ParseStringList(this JsonDict jsonDict, string key,
JsonParseOptions options = JsonParseOptions.ThrowInvalidCast) {
TryParseStringList(jsonDict, key, out List<string> values, options);
return values;
}

// Tries to convert the found List of Dictionaries, using the given function.
public static bool TryParseObjectList<T>(this JsonDict jsonDict, string key,
Func<JsonDict, T> parseFunc, out List<T> values,
Expand Down
174 changes: 0 additions & 174 deletions vertexai/src/LiveContentResponse.cs

This file was deleted.

8 changes: 5 additions & 3 deletions vertexai/src/LiveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public Task SendAudioAsync(float[] audioData, CancellationToken cancellationToke
/// <param name="closeOnTurnComplete">Should the stream stop when the server returns TurnComplete.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A stream of `LiveContentResponse`s from the backend.</returns>
public async IAsyncEnumerable<LiveContentResponse> ReceiveAsync(
public async IAsyncEnumerable<LiveSessionResponse> ReceiveAsync(
bool closeOnTurnComplete = true,
[EnumeratorCancellation] CancellationToken cancellationToken = default) {
if (_clientWebSocket.State != WebSocketState.Open) {
Expand All @@ -199,14 +199,16 @@ public async IAsyncEnumerable<LiveContentResponse> ReceiveAsync(
messageBuilder.Append(Encoding.UTF8.GetString(receiveBuffer, 0, result.Count));

if (result.EndOfMessage) {
LiveContentResponse? response = LiveContentResponse.FromJson(messageBuilder.ToString());
LiveSessionResponse? response = LiveSessionResponse.FromJson(messageBuilder.ToString());
// Reset for the next message.
messageBuilder.Clear();

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

if (closeOnTurnComplete && response?.Status == LiveContentResponse.LiveResponseStatus.TurnComplete) {
if (closeOnTurnComplete &&
response?.Message is LiveSessionContent serverContent &&
serverContent.TurnComplete) {
break;
}
}
Expand Down
Loading