@@ -207,25 +207,20 @@ export const keyboardCommand: RuntimeCommand<
207207 throw new AppError ( 'UNSUPPORTED_OPERATION' , 'system.keyboard is not supported by this backend' ) ;
208208 }
209209 const action = options . action ?? 'status' ;
210- if (
211- action !== 'status' &&
212- action !== 'get' &&
213- action !== 'dismiss' &&
214- action !== 'enter' &&
215- action !== 'return'
216- ) {
210+ if ( ! isKeyboardAction ( action ) ) {
217211 throw new AppError (
218212 'INVALID_ARGS' ,
219213 'system.keyboard action must be status, get, dismiss, enter, or return' ,
220214 ) ;
221215 }
222216 const state = await runtime . backend . setKeyboard ( toBackendContext ( runtime , options ) , { action } ) ;
223217 const formattedBackendResult = toBackendResult ( state ) ;
218+ const keyboardState = isKeyboardResult ( state ) ? state : { } ;
224219 if ( action === 'enter' || action === 'return' ) {
225220 return {
226221 kind : 'keyboardEnterPressed' ,
227222 action : 'enter' ,
228- state : isKeyboardResult ( state ) ? state : { } ,
223+ state : keyboardState ,
229224 ...( formattedBackendResult ? { backendResult : formattedBackendResult } : { } ) ,
230225 ...successText ( 'Keyboard enter pressed' ) ,
231226 } ;
@@ -235,15 +230,15 @@ export const keyboardCommand: RuntimeCommand<
235230 return {
236231 kind : 'keyboardDismissed' ,
237232 action,
238- state : isKeyboardResult ( state ) ? state : { } ,
233+ state : keyboardState ,
239234 ...( formattedBackendResult ? { backendResult : formattedBackendResult } : { } ) ,
240235 ...successText ( dismissed === false ? 'Keyboard already hidden' : 'Keyboard dismissed' ) ,
241236 } ;
242237 }
243238 return {
244239 kind : 'keyboardState' ,
245240 action,
246- state : isKeyboardResult ( state ) ? state : { } ,
241+ state : keyboardState ,
247242 ...( formattedBackendResult ? { backendResult : formattedBackendResult } : { } ) ,
248243 } ;
249244} ;
@@ -373,25 +368,41 @@ function normalizeAlertResult(
373368 action : BackendAlertAction ,
374369 result : BackendAlertResult ,
375370) : SystemAlertCommandResult {
376- if ( action === 'get' ) {
377- if ( result . kind !== 'alertStatus' ) {
378- throw new AppError ( 'COMMAND_FAILED' , 'system.alert get returned an invalid backend result' ) ;
379- }
380- return { kind : 'alertStatus' , action, alert : result . alert } ;
371+ switch ( action ) {
372+ case 'get' :
373+ return normalizeAlertStatusResult ( result ) ;
374+ case 'wait' :
375+ return normalizeAlertWaitResult ( result ) ;
376+ default :
377+ return normalizeAlertHandledResult ( action , result ) ;
381378 }
382- if ( action === 'wait' ) {
383- if ( result . kind !== 'alertWait' ) {
384- throw new AppError ( 'COMMAND_FAILED' , 'system.alert wait returned an invalid backend result' ) ;
385- }
386- return {
387- kind : 'alertWait' ,
388- action,
389- alert : result . alert ,
390- ... ( result . waitedMs !== undefined ? { waitedMs : result . waitedMs } : { } ) ,
391- ... ( result . timedOut !== undefined ? { timedOut : result . timedOut } : { } ) ,
392- ... successText ( result . alert ? 'Alert visible' : 'Alert wait timed out' ) ,
393- } ;
379+ }
380+
381+ function normalizeAlertStatusResult ( result : BackendAlertResult ) : SystemAlertCommandResult {
382+ if ( result . kind !== 'alertStatus' ) {
383+ throw new AppError ( 'COMMAND_FAILED' , 'system.alert get returned an invalid backend result' ) ;
384+ }
385+ return { kind : 'alertStatus' , action : 'get' , alert : result . alert } ;
386+ }
387+
388+ function normalizeAlertWaitResult ( result : BackendAlertResult ) : SystemAlertCommandResult {
389+ if ( result . kind !== 'alertWait' ) {
390+ throw new AppError ( 'COMMAND_FAILED' , 'system.alert wait returned an invalid backend result' ) ;
394391 }
392+ return {
393+ kind : 'alertWait' ,
394+ action : 'wait' ,
395+ alert : result . alert ,
396+ ...( result . waitedMs !== undefined ? { waitedMs : result . waitedMs } : { } ) ,
397+ ...( result . timedOut !== undefined ? { timedOut : result . timedOut } : { } ) ,
398+ ...successText ( result . alert ? 'Alert visible' : 'Alert wait timed out' ) ,
399+ } ;
400+ }
401+
402+ function normalizeAlertHandledResult (
403+ action : Extract < BackendAlertAction , 'accept' | 'dismiss' > ,
404+ result : BackendAlertResult ,
405+ ) : SystemAlertCommandResult {
395406 if ( result . kind !== 'alertHandled' ) {
396407 throw new AppError (
397408 'COMMAND_FAILED' ,
@@ -408,6 +419,18 @@ function normalizeAlertResult(
408419 } ;
409420}
410421
422+ function isKeyboardAction (
423+ action : string ,
424+ ) : action is 'status' | 'get' | 'dismiss' | 'enter' | 'return' {
425+ return (
426+ action === 'status' ||
427+ action === 'get' ||
428+ action === 'dismiss' ||
429+ action === 'enter' ||
430+ action === 'return'
431+ ) ;
432+ }
433+
411434function isKeyboardResult ( value : unknown ) : value is BackendKeyboardResult {
412435 return Boolean ( value && typeof value === 'object' ) ;
413436}
0 commit comments