File size: 6,729 Bytes
f56a29b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 * Derived Playback State - Pure function that computes a high-level PlaybackView
 * from the ~15 raw state variables scattered across Stage.
 *
 * This centralises all "what is happening now?" derivation logic so that
 * both Stage and Roundtable can consume a single, consistent view object
 * instead of re-deriving the same conditions inline.
 */

import type { EngineMode, TriggerEvent } from './types';

// ---------------------------------------------------------------------------
// Input: raw state collected from Stage's useState variables
// ---------------------------------------------------------------------------

export interface PlaybackRawState {
  engineMode: EngineMode;
  lectureSpeech: string | null;
  liveSpeech: string | null;
  speakingAgentId: string | null;
  thinkingState: { stage: string; agentId?: string } | null;
  isCueUser: boolean;
  isTopicPending: boolean;
  chatIsStreaming: boolean;
  discussionTrigger: TriggerEvent | null;
  playbackCompleted: boolean;
  idleText: string | null;
  /** Whether the speaking agent is a student (not teacher). Provided by caller. */
  speakingStudent: boolean;
  /** Active session type — stays set between agent-loop turns (cleared only by doSessionCleanup). */
  sessionType: string | null;
}

// ---------------------------------------------------------------------------
// Output: a single derived view consumed by Roundtable (and Stage for gating)
// ---------------------------------------------------------------------------

export type PlaybackPhase =
  | 'idle'
  | 'lecturePlaying'
  | 'lecturePaused'
  | 'waitingProactive'
  | 'discussionActive'
  | 'discussionPaused'
  | 'cueUser'
  | 'completed';

export type BubbleButtonState = 'bars' | 'play' | 'restart' | 'none';

export interface PlaybackView {
  /** High-level phase — "what is happening right now?" */
  phase: PlaybackPhase;

  /** Text to display in the speech bubble (without userMessage overlay) */
  sourceText: string;

  /** Who owns the speech bubble */
  bubbleRole: 'teacher' | 'agent' | 'user' | null;

  /** Who is actively speaking (avatar highlight) */
  activeRole: 'teacher' | 'agent' | 'user' | null;

  /** Bubble button state */
  buttonState: BubbleButtonState;

  /** Whether we're in a live SSE flow (suppresses lecture text) */
  isInLiveFlow: boolean;

  /** Whether any topic-related activity blocks scene switching */
  isTopicActive: boolean;
}

// ---------------------------------------------------------------------------
// Pure computation
// ---------------------------------------------------------------------------

export function computePlaybackView(raw: PlaybackRawState): PlaybackView {
  const {
    engineMode,
    lectureSpeech,
    liveSpeech,
    speakingAgentId,
    thinkingState,
    isCueUser,
    isTopicPending,
    chatIsStreaming,
    discussionTrigger,
    playbackCompleted,
    idleText,
    speakingStudent,
    sessionType,
  } = raw;

  // ---- isInLiveFlow ----
  // True when there's any live SSE activity (agent speaking, thinking, or streaming).
  // Includes chatIsStreaming to cover the entire QA session (gaps between
  // agent response completion and user's next message).
  // Includes sessionType to bridge the gap between agent-loop turns: the `done`
  // event clears chatIsStreaming, but the session is still active until
  // doSessionCleanup runs. Without this, bubbleRole briefly falls through to
  // the 'teacher' idleText case, causing a visible flash.
  const isInLiveFlow = !!(speakingAgentId || thinkingState || chatIsStreaming || sessionType);

  // ---- phase ----
  // Live flow states MUST be checked before playbackCompleted so that
  // starting a QA from the completed state doesn't leak the restart icon
  // into agent bubbles.
  let phase: PlaybackPhase;
  if (isCueUser) {
    phase = 'cueUser';
  } else if (isTopicPending) {
    phase = 'discussionPaused';
  } else if (speakingAgentId || thinkingState || chatIsStreaming || sessionType) {
    phase = 'discussionActive';
  } else if (discussionTrigger) {
    phase = 'waitingProactive';
  } else if (playbackCompleted) {
    phase = 'completed';
  } else if (engineMode === 'playing') {
    phase = 'lecturePlaying';
  } else if (engineMode === 'paused') {
    phase = 'lecturePaused';
  } else {
    phase = 'idle';
  }

  // ---- sourceText (without userMessage — Roundtable overlays that locally) ----
  let sourceText: string;
  if (liveSpeech) {
    sourceText = liveSpeech;
  } else if (isInLiveFlow) {
    // In live flow but no text yet — show empty (loading dots handled by bubble)
    sourceText = '';
  } else if (lectureSpeech) {
    sourceText = lectureSpeech;
  } else if (phase === 'completed') {
    sourceText = '';
  } else {
    sourceText = idleText || '';
  }

  // ---- bubble loading states ----
  const isBubbleLoading = !!(speakingAgentId && !liveSpeech);
  const isAgentLoading = !!(speakingStudent && !liveSpeech);

  // ---- activeRole ----
  let activeRole: 'teacher' | 'agent' | 'user' | null;
  if (liveSpeech && speakingStudent) {
    activeRole = 'agent';
  } else if (liveSpeech) {
    activeRole = 'teacher';
  } else if (isAgentLoading) {
    activeRole = 'agent';
  } else if (isBubbleLoading) {
    activeRole = 'teacher';
  } else if (isCueUser) {
    activeRole = null;
  } else if (lectureSpeech) {
    activeRole = 'teacher';
  } else {
    activeRole = null;
  }

  // ---- bubbleRole ----
  let bubbleRole: 'teacher' | 'agent' | 'user' | null;
  if (liveSpeech && speakingStudent) {
    bubbleRole = 'agent';
  } else if (liveSpeech) {
    bubbleRole = 'teacher';
  } else if (isAgentLoading) {
    bubbleRole = 'agent';
  } else if (isBubbleLoading) {
    bubbleRole = 'teacher';
  } else if (isInLiveFlow) {
    bubbleRole = null;
  } else if (isCueUser) {
    bubbleRole = null;
  } else if (lectureSpeech || idleText) {
    bubbleRole = 'teacher';
  } else {
    bubbleRole = null;
  }

  // ---- buttonState ----
  let buttonState: BubbleButtonState;
  if (isTopicPending) {
    buttonState = 'play'; // resume topic
  } else if (phase === 'lecturePlaying') {
    buttonState = 'bars'; // breathing bars + hover pause
  } else if (phase === 'discussionActive') {
    buttonState = 'bars';
  } else if (phase === 'completed') {
    buttonState = 'restart';
  } else if (phase === 'idle' || phase === 'lecturePaused') {
    buttonState = 'play';
  } else {
    buttonState = 'none';
  }

  // ---- isTopicActive ----
  const isTopicActive =
    chatIsStreaming || isTopicPending || isCueUser || engineMode === 'live' || !!discussionTrigger;

  return {
    phase,
    sourceText,
    bubbleRole,
    activeRole,
    buttonState,
    isInLiveFlow,
    isTopicActive,
  };
}