@@ -17,6 +17,9 @@ public sealed partial class VoiceSettingsPage : Page
1717{
1818 private static App CurrentApp => ( App ) Microsoft . UI . Xaml . Application . Current ! ;
1919 private VoiceService ? _voiceService ;
20+ private AssistantBridgeService ? _assistantBridgeService ;
21+ private CancellationTokenSource ? _assistantRequestCts ;
22+ private int _assistantOperationVersion ;
2023 private bool _suppressEvents = true ; // suppress until Initialize/LoadSettings runs
2124 // Per-asset CTS so a Piper download doesn't cancel an in-flight Whisper
2225 // download (and vice versa). Each download type owns its own token.
@@ -34,9 +37,12 @@ public VoiceSettingsPage()
3437 {
3538 UpdateModelStatus ( ) ;
3639 UpdatePiperVoiceState ( ) ;
40+ _ = RefreshAssistantAsync ( ) ;
3741 } ;
3842 Unloaded += async ( _ , _ ) =>
3943 {
44+ CancelAssistantRequest ( ) ;
45+
4046 if ( App . Current is App app )
4147 app . SpeakerMuteChanged -= OnAppSpeakerMuteChanged ;
4248
@@ -52,6 +58,7 @@ public VoiceSettingsPage()
5258 public void Initialize ( VoiceService ? voiceService )
5359 {
5460 _voiceService = voiceService ;
61+ _assistantBridgeService ??= new AssistantBridgeService ( new AppLogger ( ) ) ;
5562 if ( App . Current is App app )
5663 {
5764 app . SpeakerMuteChanged -= OnAppSpeakerMuteChanged ;
@@ -64,6 +71,248 @@ public void Initialize(VoiceService? voiceService)
6471 PiperPreviewLabel . Text = L ( "VoiceSettingsPage_PiperPreviewButtonContent" ) ;
6572 PreviewVoiceLabel . Text = L ( "VoiceSettingsPage_PreviewVoiceButtonContent" ) ;
6673 LoadSettings ( ) ;
74+ _ = RefreshAssistantAsync ( ) ;
75+ }
76+
77+ private void OnAssistantRefreshClick ( object sender , RoutedEventArgs e ) =>
78+ AsyncEventHandlerGuard . Run (
79+ RefreshAssistantAsync ,
80+ new OpenClawTray . AppLogger ( ) ,
81+ nameof ( OnAssistantRefreshClick ) ) ;
82+
83+ private void OnAssistantStartClick ( object sender , RoutedEventArgs e ) =>
84+ AsyncEventHandlerGuard . Run (
85+ OnAssistantStartClickAsync ,
86+ new OpenClawTray . AppLogger ( ) ,
87+ nameof ( OnAssistantStartClick ) ) ;
88+
89+ private async Task OnAssistantStartClickAsync ( )
90+ {
91+ var bridge = EnsureAssistantBridge ( ) ;
92+ var busyVersion = BeginAssistantOperation ( ) ;
93+ AssistantStatusText . Text = L ( "VoiceSettingsPage_AssistantStarting" ) ;
94+ AssistantDetailText . Text = L ( "VoiceSettingsPage_AssistantStartingDetail" ) ;
95+ try
96+ {
97+ var result = await bridge . StartListenServiceAsync ( NewAssistantRequestToken ( ) ) ;
98+ if ( ! result . Success )
99+ {
100+ AssistantStatusText . Text = L ( "VoiceSettingsPage_AssistantStartFailed" ) ;
101+ AssistantDetailText . Text = result . ErrorMessage ;
102+ return ;
103+ }
104+
105+ await RefreshAssistantAsync ( ) ;
106+ }
107+ catch ( OperationCanceledException )
108+ {
109+ }
110+ finally
111+ {
112+ EndAssistantOperation ( busyVersion ) ;
113+ }
114+ }
115+
116+ private void OnAssistantStopClick ( object sender , RoutedEventArgs e ) =>
117+ AsyncEventHandlerGuard . Run (
118+ OnAssistantStopClickAsync ,
119+ new OpenClawTray . AppLogger ( ) ,
120+ nameof ( OnAssistantStopClick ) ) ;
121+
122+ private async Task OnAssistantStopClickAsync ( )
123+ {
124+ var bridge = EnsureAssistantBridge ( ) ;
125+ var busyVersion = BeginAssistantOperation ( ) ;
126+ AssistantStatusText . Text = L ( "VoiceSettingsPage_AssistantStopping" ) ;
127+ AssistantDetailText . Text = "" ;
128+ try
129+ {
130+ var result = await bridge . StopListenServiceAsync ( NewAssistantRequestToken ( ) ) ;
131+ if ( ! result . Success )
132+ {
133+ AssistantStatusText . Text = L ( "VoiceSettingsPage_AssistantStopFailed" ) ;
134+ AssistantDetailText . Text = result . ErrorMessage ;
135+ return ;
136+ }
137+
138+ await RefreshAssistantAsync ( ) ;
139+ }
140+ catch ( OperationCanceledException )
141+ {
142+ }
143+ finally
144+ {
145+ EndAssistantOperation ( busyVersion ) ;
146+ }
147+ }
148+
149+ private async Task RefreshAssistantAsync ( )
150+ {
151+ var bridge = EnsureAssistantBridge ( ) ;
152+ var busyVersion = BeginAssistantOperation ( ) ;
153+ try
154+ {
155+ var snapshot = await bridge . GetStatusAsync ( NewAssistantRequestToken ( ) ) ;
156+ RenderAssistantSnapshot ( snapshot ) ;
157+ }
158+ catch ( OperationCanceledException )
159+ {
160+ }
161+ finally
162+ {
163+ EndAssistantOperation ( busyVersion ) ;
164+ }
165+ }
166+
167+ private AssistantBridgeService EnsureAssistantBridge ( ) =>
168+ _assistantBridgeService ??= new AssistantBridgeService ( new AppLogger ( ) ) ;
169+
170+ private CancellationToken NewAssistantRequestToken ( )
171+ {
172+ CancelAssistantRequest ( ) ;
173+ _assistantRequestCts = new CancellationTokenSource ( ) ;
174+ return _assistantRequestCts . Token ;
175+ }
176+
177+ private void CancelAssistantRequest ( )
178+ {
179+ try { _assistantRequestCts ? . Cancel ( ) ; }
180+ catch ( Exception ex ) { Logger . Debug ( $ "VoiceSettingsPage: assistant request cancel failed: { ex . Message } ") ; }
181+ _assistantRequestCts ? . Dispose ( ) ;
182+ _assistantRequestCts = null ;
183+ }
184+
185+ private void SetAssistantBusy ( bool busy )
186+ {
187+ AssistantRefreshButton . IsEnabled = ! busy ;
188+ AssistantStartButton . IsEnabled = ! busy ;
189+ AssistantStopButton . IsEnabled = ! busy ;
190+ }
191+
192+ private int BeginAssistantOperation ( )
193+ {
194+ var version = Interlocked . Increment ( ref _assistantOperationVersion ) ;
195+ SetAssistantBusy ( true ) ;
196+ return version ;
197+ }
198+
199+ private void EndAssistantOperation ( int version )
200+ {
201+ if ( Volatile . Read ( ref _assistantOperationVersion ) == version )
202+ SetAssistantBusy ( false ) ;
203+ }
204+
205+ private void RenderAssistantSnapshot ( AssistantBridgeSnapshot snapshot )
206+ {
207+ if ( ! snapshot . IsAvailable )
208+ {
209+ AssistantStatusText . Text = L ( "VoiceSettingsPage_AssistantBridgeUnavailable" ) ;
210+ AssistantDetailText . Text = snapshot . ErrorMessage ;
211+ AssistantLastRefreshText . Text = "" ;
212+ AssistantTurnsPanel . Children . Clear ( ) ;
213+ AssistantTurnsPanel . Children . Add ( new TextBlock
214+ {
215+ Text = L ( "VoiceSettingsPage_AssistantNoTurnsLoaded" ) ,
216+ Style = ( Style ) Application . Current . Resources [ "CaptionTextBlockStyle" ] ,
217+ Foreground = ( Brush ) Application . Current . Resources [ "TextFillColorSecondaryBrush" ] ,
218+ TextWrapping = TextWrapping . Wrap
219+ } ) ;
220+ return ;
221+ }
222+
223+ var listen = snapshot . ListenService ;
224+ var unknown = L ( "VoiceSettingsPage_AssistantUnknown" ) ;
225+ var status = string . IsNullOrWhiteSpace ( listen . Status ) ? unknown : listen . Status ;
226+ AssistantStatusText . Text = listen . IsRunning
227+ ? Lf ( "VoiceSettingsPage_AssistantListeningFormat" , listen . Pid ? . ToString ( CultureInfo . CurrentCulture ) ?? unknown )
228+ : listen . IsStopped
229+ ? L ( "VoiceSettingsPage_AssistantStopped" )
230+ : Lf ( "VoiceSettingsPage_AssistantStatusFormat" , status ) ;
231+
232+ var details = new [ ]
233+ {
234+ string . IsNullOrWhiteSpace ( listen . Transcriber ) ? "" : Lf ( "VoiceSettingsPage_AssistantTranscriberFormat" , listen . Transcriber ) ,
235+ string . IsNullOrWhiteSpace ( snapshot . PreferredInputDevice ) ? "" : Lf ( "VoiceSettingsPage_AssistantMicFormat" , snapshot . PreferredInputDevice ) ,
236+ string . IsNullOrWhiteSpace ( snapshot . PreferredOutputDevice ) ? "" : Lf ( "VoiceSettingsPage_AssistantSpeakerFormat" , snapshot . PreferredOutputDevice ) ,
237+ Lf ( "VoiceSettingsPage_AssistantCloudRoutingFormat" , listen . AllowCloud ? L ( "VoiceSettingsPage_AssistantOn" ) : L ( "VoiceSettingsPage_AssistantOff" ) ) ,
238+ Lf ( "VoiceSettingsPage_AssistantSpeechOutputFormat" , listen . SpeakAloud ? L ( "VoiceSettingsPage_AssistantOn" ) : L ( "VoiceSettingsPage_AssistantOff" ) )
239+ } . Where ( s => ! string . IsNullOrWhiteSpace ( s ) ) ;
240+ AssistantDetailText . Text = string . Join ( " | " , details ) ;
241+ AssistantLastRefreshText . Text = string . IsNullOrWhiteSpace ( snapshot . GeneratedAt )
242+ ? ""
243+ : Lf ( "VoiceSettingsPage_AssistantLastBridgeUpdateFormat" , snapshot . GeneratedAt ) ;
244+
245+ AssistantTurnsPanel . Children . Clear ( ) ;
246+ if ( snapshot . RecentTurns . Count == 0 )
247+ {
248+ AssistantTurnsPanel . Children . Add ( new TextBlock
249+ {
250+ Text = L ( "VoiceSettingsPage_AssistantNoConversationTurns" ) ,
251+ Style = ( Style ) Application . Current . Resources [ "CaptionTextBlockStyle" ] ,
252+ Foreground = ( Brush ) Application . Current . Resources [ "TextFillColorSecondaryBrush" ] ,
253+ TextWrapping = TextWrapping . Wrap
254+ } ) ;
255+ return ;
256+ }
257+
258+ foreach ( var turn in snapshot . RecentTurns . Take ( 5 ) )
259+ AssistantTurnsPanel . Children . Add ( CreateAssistantTurnView ( turn ) ) ;
260+ }
261+
262+ private static Border CreateAssistantTurnView ( AssistantTurnSnapshot turn )
263+ {
264+ var panel = new StackPanel { Spacing = 6 } ;
265+ var source = string . IsNullOrWhiteSpace ( turn . Source ) ? L ( "VoiceSettingsPage_AssistantSourceDefault" ) : turn . Source ;
266+ var stage = string . IsNullOrWhiteSpace ( turn . Stage ) ? L ( "VoiceSettingsPage_AssistantUnknown" ) : turn . Stage ;
267+ var model = string . IsNullOrWhiteSpace ( turn . Provider )
268+ ? turn . ModelProfile
269+ : $ "{ turn . Provider } { turn . ModelProfile } ". Trim ( ) ;
270+ var metadata = turn . TotalMs is int ms
271+ ? Lf ( "VoiceSettingsPage_AssistantTurnMetadataWithLatencyFormat" , source , stage , ms . ToString ( CultureInfo . CurrentCulture ) )
272+ : Lf ( "VoiceSettingsPage_AssistantTurnMetadataFormat" , source , stage ) ;
273+ panel . Children . Add ( new TextBlock
274+ {
275+ Text = metadata ,
276+ Style = ( Style ) Application . Current . Resources [ "CaptionTextBlockStyle" ] ,
277+ Foreground = ( Brush ) Application . Current . Resources [ "TextFillColorSecondaryBrush" ] ,
278+ TextWrapping = TextWrapping . Wrap
279+ } ) ;
280+ panel . Children . Add ( new TextBlock
281+ {
282+ Text = Lf ( "VoiceSettingsPage_AssistantUserTurnFormat" , TrimForDisplay ( turn . InputText ) ) ,
283+ TextWrapping = TextWrapping . Wrap
284+ } ) ;
285+ panel . Children . Add ( new TextBlock
286+ {
287+ Text = Lf ( "VoiceSettingsPage_AssistantResponseTurnFormat" , TrimForDisplay ( turn . ResponseText ) ) ,
288+ TextWrapping = TextWrapping . Wrap
289+ } ) ;
290+ if ( ! string . IsNullOrWhiteSpace ( model ) )
291+ {
292+ panel . Children . Add ( new TextBlock
293+ {
294+ Text = model ,
295+ Style = ( Style ) Application . Current . Resources [ "CaptionTextBlockStyle" ] ,
296+ Foreground = ( Brush ) Application . Current . Resources [ "TextFillColorSecondaryBrush" ] ,
297+ TextWrapping = TextWrapping . Wrap
298+ } ) ;
299+ }
300+
301+ return new Border
302+ {
303+ Child = panel ,
304+ Background = ( Brush ) Application . Current . Resources [ "CardBackgroundFillColorSecondaryBrush" ] ,
305+ CornerRadius = new CornerRadius ( 8 ) ,
306+ Padding = new Thickness ( 12 )
307+ } ;
308+ }
309+
310+ private static string TrimForDisplay ( string value )
311+ {
312+ if ( string . IsNullOrWhiteSpace ( value ) )
313+ return L ( "VoiceSettingsPage_AssistantEmptyValue" ) ;
314+ value = value . Trim ( ) ;
315+ return value . Length <= 220 ? value : value [ .. 217 ] + "..." ;
67316 }
68317
69318 private void OnAppSpeakerMuteChanged ( bool muted )
0 commit comments