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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ private enum NyxRelayStreamingGuardSource
Finalize,
}

private sealed record NyxRelayTextOperationInFlight(
NyxRelayTextOperationKind Operation,
long Sequence,
long Generation);

/// <summary>
/// Actor-scoped streaming state for one conversation turn, backed by
/// ConversationGAgentState.ActiveReplyLifecycles.
Expand All @@ -48,10 +53,27 @@ private sealed record NyxRelayStreamingState(
string? PlatformMessageId,
string LastFlushedText,
int EditCount,
string? TerminalReason)
string? TerminalReason,
NyxRelayTextOperationInFlight? InFlight,
long OperationGeneration,
string? PendingAccumulatedText,
string? PendingFinalizeText,
string? PendingFinalizeCommandId,
LlmReplyTerminalState PendingTerminalState)
{
public static NyxRelayStreamingState Initial { get; } =
new(NyxRelayStreamingPhase.Idle, null, string.Empty, 0, null);
new(
NyxRelayStreamingPhase.Idle,
PlatformMessageId: null,
LastFlushedText: string.Empty,
EditCount: 0,
TerminalReason: null,
InFlight: null,
OperationGeneration: 0,
PendingAccumulatedText: null,
PendingFinalizeText: null,
PendingFinalizeCommandId: null,
PendingTerminalState: LlmReplyTerminalState.Unspecified);

public bool AllowsInterimEdit =>
Phase is NyxRelayStreamingPhase.Idle
Expand All @@ -76,9 +98,11 @@ or NyxRelayStreamingPhase.TerminalSucceeded
private static bool IsLegalNyxRelayStreamingTransition(NyxRelayStreamingPhase from, NyxRelayStreamingPhase to) =>
(from, to) switch
{
(NyxRelayStreamingPhase.Idle, NyxRelayStreamingPhase.Idle) => true,
(NyxRelayStreamingPhase.Idle, NyxRelayStreamingPhase.PlaceholderSent) => true,
(NyxRelayStreamingPhase.Idle, NyxRelayStreamingPhase.DisabledPreSend) => true,

(NyxRelayStreamingPhase.PlaceholderSent, NyxRelayStreamingPhase.PlaceholderSent) => true,
(NyxRelayStreamingPhase.PlaceholderSent, NyxRelayStreamingPhase.Streaming) => true,
(NyxRelayStreamingPhase.PlaceholderSent, NyxRelayStreamingPhase.SuppressingInterim) => true,
(NyxRelayStreamingPhase.PlaceholderSent, NyxRelayStreamingPhase.TerminalSucceeded) => true,
Expand All @@ -89,6 +113,7 @@ private static bool IsLegalNyxRelayStreamingTransition(NyxRelayStreamingPhase fr
(NyxRelayStreamingPhase.Streaming, NyxRelayStreamingPhase.TerminalSucceeded) => true,
(NyxRelayStreamingPhase.Streaming, NyxRelayStreamingPhase.TerminalPartial) => true,

(NyxRelayStreamingPhase.SuppressingInterim, NyxRelayStreamingPhase.SuppressingInterim) => true,
(NyxRelayStreamingPhase.SuppressingInterim, NyxRelayStreamingPhase.TerminalSucceeded) => true,
(NyxRelayStreamingPhase.SuppressingInterim, NyxRelayStreamingPhase.TerminalPartial) => true,

Expand All @@ -109,7 +134,18 @@ private NyxRelayStreamingState GetOrInitNyxRelayStreamingState(string correlatio
NormalizeOptional(lifecycle.PlatformMessageId),
lifecycle.LastFlushedText ?? string.Empty,
lifecycle.EditCount,
NormalizeOptional(lifecycle.TerminalReason));
NormalizeOptional(lifecycle.TerminalReason),
lifecycle.NyxRelayInFlightOperation == NyxRelayTextOperationKind.Unspecified
? null
: new NyxRelayTextOperationInFlight(
lifecycle.NyxRelayInFlightOperation,
lifecycle.NyxRelayInFlightSequence,
lifecycle.NyxRelayOperationGeneration),
lifecycle.NyxRelayOperationGeneration,
NormalizeOptional(lifecycle.PendingAccumulatedText),
NormalizeOptional(lifecycle.PendingFinalizeText),
NormalizeOptional(lifecycle.PendingFinalizeCommandId),
lifecycle.PendingNyxRelayTerminalState);
}

/// <summary>
Expand Down Expand Up @@ -163,6 +199,13 @@ private async Task<NyxRelayStreamingState> TransitionNyxRelayStreamingPhaseAsync
var updated = carried with
{
Phase = next,
InFlight = IsTerminalNyxRelayStreamingPhase(next) ? null : carried.InFlight,
PendingAccumulatedText = IsTerminalNyxRelayStreamingPhase(next) ? null : carried.PendingAccumulatedText,
PendingFinalizeText = IsTerminalNyxRelayStreamingPhase(next) ? null : carried.PendingFinalizeText,
PendingFinalizeCommandId = IsTerminalNyxRelayStreamingPhase(next) ? null : carried.PendingFinalizeCommandId,
PendingTerminalState = IsTerminalNyxRelayStreamingPhase(next)
? LlmReplyTerminalState.Unspecified
: carried.PendingTerminalState,
TerminalReason = IsTerminalNyxRelayStreamingPhase(next)
? (terminalReason ?? carried.TerminalReason)
: carried.TerminalReason,
Expand Down Expand Up @@ -225,5 +268,41 @@ private static ConversationReplyLifecycleState ToLifecycleState(
EditCount = state.EditCount,
TerminalReason = state.TerminalReason ?? string.Empty,
UpdatedAtUnixMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
NyxRelayInFlightOperation = state.InFlight?.Operation ?? NyxRelayTextOperationKind.Unspecified,
NyxRelayInFlightSequence = state.InFlight?.Sequence ?? 0,
NyxRelayOperationGeneration = state.OperationGeneration,
PendingAccumulatedText = state.PendingAccumulatedText ?? string.Empty,
PendingFinalizeText = state.PendingFinalizeText ?? string.Empty,
PendingFinalizeCommandId = state.PendingFinalizeCommandId ?? string.Empty,
PendingNyxRelayTerminalState = state.PendingTerminalState,
};

private long NextNyxRelayTextOperationGeneration(NyxRelayStreamingState state) =>
Math.Max(state.OperationGeneration, state.InFlight?.Generation ?? 0) + 1;

private static string BuildNyxRelayTextOperationTimeoutCallbackId(
string correlationId,
NyxRelayTextOperationKind operation,
long generation) =>
$"conversation-nyx-relay-text:{correlationId}:{operation}:{generation}";

private static string BuildNyxRelayTextOperationId(
string correlationId,
NyxRelayTextOperationKind operation,
long sequence,
long generation) =>
$"{correlationId}:{operation}:{sequence}:{generation}";

private static bool MatchesNyxRelayTextInFlight(
NyxRelayStreamingState state,
NyxRelayTextOperationKind operation,
long sequence,
long generation)
{
if (state.InFlight is not { } inFlight)
return false;
return inFlight.Operation == operation &&
inFlight.Sequence == sequence &&
inFlight.Generation == generation;
}
}
Loading
Loading