11import * as NodeZlib from "node:zlib" ;
22import {
3+ DesktopPreviewAnnotationVoiceEventSchema ,
34 DesktopPreviewRecordingInputSchema ,
45 DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER ,
56} from "@t3tools/contracts" ;
67import type {
78 DesktopPreviewAnnotationTheme ,
9+ DesktopPreviewAnnotationVoiceConfig ,
10+ DesktopPreviewAnnotationVoiceState ,
811 DesktopPreviewColorScheme ,
912 DesktopPreviewFavicon ,
1013 DesktopPreviewOpenLinkEvent ,
@@ -56,13 +59,19 @@ import * as SynchronizedRef from "effect/SynchronizedRef";
5659
5760import * as DesktopEnvironment from "../app/DesktopEnvironment.ts" ;
5861import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts" ;
59- import { MENU_ACTION_CHANNEL , PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL } from "../ipc/channels.ts" ;
62+ import {
63+ MENU_ACTION_CHANNEL ,
64+ PREVIEW_ANNOTATION_VOICE_EVENT_CHANNEL ,
65+ PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL ,
66+ } from "../ipc/channels.ts" ;
6067import * as DesktopBrowserHost from "./DesktopBrowserHost.ts" ;
6168import * as PreviewPasskeys from "./Passkeys.ts" ;
6269import * as BrowserSession from "./BrowserSession.ts" ;
6370import {
6471 ANNOTATION_CAPTURED_CHANNEL ,
6572 ANNOTATION_SEND_ENABLED_CHANNEL ,
73+ ANNOTATION_VOICE_EVENT_CHANNEL ,
74+ ANNOTATION_VOICE_STATE_CHANNEL ,
6675 ANNOTATION_THEME_CHANNEL ,
6776 CANCEL_PICK_CHANNEL ,
6877 ELEMENT_PICKED_CHANNEL ,
@@ -396,9 +405,14 @@ interface PendingRecording {
396405}
397406
398407interface PickSession {
408+ readonly voiceSessionId ?: string | undefined ;
399409 readonly cancel : Effect . Effect < void > ;
400410}
401411
412+ const decodeAnnotationVoiceEvent = Schema . decodeUnknownOption (
413+ DesktopPreviewAnnotationVoiceEventSchema ,
414+ ) ;
415+
402416interface BrowserControlSession {
403417 readonly webContentsId : number ;
404418 // Retain the attached Debugger to prevent GC crashes (electron/electron#53376); wc.debugger throws after destruction.
@@ -2074,16 +2088,35 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20742088 } ) ;
20752089 } ) ;
20762090
2077- const pickElement = Effect . fn ( "PreviewManager.pickElement" ) ( function * ( tabId : string ) {
2091+ // Voice state reaches a guest only while its pick session, by id, still owns the tab.
2092+ const updateAnnotationVoice = Effect . fn ( "PreviewManager.updateAnnotationVoice" ) ( function * (
2093+ state : DesktopPreviewAnnotationVoiceState ,
2094+ ) {
2095+ const sessions = yield * Ref . get ( pickSessionsRef ) ;
2096+ if ( sessions . get ( state . tabId ) ?. voiceSessionId !== state . sessionId ) return ;
2097+ const wc = yield * requireWebContents ( state . tabId ) ;
2098+ yield * attempt ( { operation : "annotationVoice.update" , tabId : state . tabId } , ( ) =>
2099+ wc . send ( ANNOTATION_VOICE_STATE_CHANNEL , state ) ,
2100+ ) ;
2101+ } ) ;
2102+
2103+ const pickElement = Effect . fn ( "PreviewManager.pickElement" ) ( function * (
2104+ tabId : string ,
2105+ voice ?: DesktopPreviewAnnotationVoiceConfig ,
2106+ ) {
20782107 const wc = yield * requireWebContents ( tabId ) ;
20792108 yield * cancelPickElement ( tabId ) ;
20802109 const annotationTheme = yield * Ref . get ( annotationThemeRef ) ;
20812110 return yield * Effect . callback < PreviewAnnotationSubmissionResult | null , PreviewManagerError > (
20822111 ( resume ) => {
2083- const session : PickSession = { cancel : Effect . suspend ( ( ) => cancelPickSession ( ) ) } ;
2112+ const session : PickSession = {
2113+ voiceSessionId : voice ?. sessionId ,
2114+ cancel : Effect . suspend ( ( ) => cancelPickSession ( ) ) ,
2115+ } ;
20842116 const cleanup = Effect . fn ( "PreviewManager.cleanupPickElement" ) ( function * ( ) {
20852117 yield * attempt ( { operation : "pickElement.cleanup" , tabId, webContentsId : wc . id } , ( ) => {
20862118 wc . ipc . removeListener ( ELEMENT_PICKED_CHANNEL , onMessage ) ;
2119+ wc . ipc . removeListener ( ANNOTATION_VOICE_EVENT_CHANNEL , onVoiceEvent ) ;
20872120 wc . off ( "destroyed" , onDestroyed ) ;
20882121 wc . off ( "did-start-navigation" , onNavigated ) ;
20892122 } ) . pipe ( Effect . ignore ) ;
@@ -2191,6 +2224,25 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21912224 ) ,
21922225 ) ;
21932226 } ;
2227+ // Only this guest's listener sees these; the renderer still checks the session id.
2228+ const onVoiceEvent = ( _event : Electron . IpcMainEvent , value : unknown ) => {
2229+ const decoded = decodeAnnotationVoiceEvent ( value ) ;
2230+ if ( settled || ! voice || Option . isNone ( decoded ) ) return ;
2231+ if ( decoded . value . sessionId !== voice . sessionId ) return ;
2232+ runFork (
2233+ Effect . gen ( function * ( ) {
2234+ const window = yield * Ref . get ( mainWindowRef ) ;
2235+ if ( Option . isNone ( window ) || window . value . isDestroyed ( ) ) return ;
2236+ yield * attempt ( { operation : "annotationVoice.forward" , tabId } , ( ) =>
2237+ window . value . webContents . send (
2238+ PREVIEW_ANNOTATION_VOICE_EVENT_CHANNEL ,
2239+ tabId ,
2240+ decoded . value ,
2241+ ) ,
2242+ ) . pipe ( Effect . ignore ) ;
2243+ } ) ,
2244+ ) ;
2245+ } ;
21942246 const onDestroyed = ( ) => settle ( null ) ;
21952247 const onNavigated = (
21962248 event : Electron . Event < Electron . WebContentsDidStartNavigationEventParams > ,
@@ -2210,10 +2262,16 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
22102262 if ( settled ) return ;
22112263 yield * attempt ( { operation : "pickElement.register" , tabId, webContentsId : wc . id } , ( ) => {
22122264 wc . ipc . on ( ELEMENT_PICKED_CHANNEL , onMessage ) ;
2265+ if ( voice ) wc . ipc . on ( ANNOTATION_VOICE_EVENT_CHANNEL , onVoiceEvent ) ;
22132266 wc . once ( "destroyed" , onDestroyed ) ;
22142267 wc . on ( "did-start-navigation" , onNavigated ) ;
22152268 if ( ! wc . isFocused ( ) ) wc . focus ( ) ;
2216- wc . send ( START_PICK_CHANNEL , annotationTheme , annotationSendEnabled . get ( tabId ) === true ) ;
2269+ wc . send (
2270+ START_PICK_CHANNEL ,
2271+ annotationTheme ,
2272+ annotationSendEnabled . get ( tabId ) === true ,
2273+ voice ,
2274+ ) ;
22172275 } ) ;
22182276 } ) ;
22192277 runFork (
@@ -3312,6 +3370,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
33123370 openPictureInPicture,
33133371 openDevTools,
33143372 pickElement,
3373+ updateAnnotationVoice,
33153374 prepareWebview,
33163375 reapplyZoom,
33173376 refresh,
@@ -3552,7 +3611,11 @@ export class PreviewManager extends Context.Service<
35523611 ) => Effect . Effect < void , PreviewManagerError > ;
35533612 readonly pickElement : (
35543613 tabId : string ,
3614+ voice ?: DesktopPreviewAnnotationVoiceConfig ,
35553615 ) => Effect . Effect < PreviewAnnotationSubmissionResult | null , PreviewManagerError > ;
3616+ readonly updateAnnotationVoice : (
3617+ state : DesktopPreviewAnnotationVoiceState ,
3618+ ) => Effect . Effect < void , PreviewManagerError > ;
35563619 readonly cancelPickElement : ( tabId : string ) => Effect . Effect < void , PreviewManagerError > ;
35573620 readonly captureScreenshot : (
35583621 tabId : string ,
@@ -3703,6 +3766,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
37033766 setAnnotationTheme : operations . setAnnotationTheme ,
37043767 setAnnotationSendEnabled : operations . setAnnotationSendEnabled ,
37053768 pickElement : operations . pickElement ,
3769+ updateAnnotationVoice : operations . updateAnnotationVoice ,
37063770 cancelPickElement : operations . cancelPickElement ,
37073771 captureScreenshot : operations . captureScreenshot ,
37083772 revealArtifact : operations . revealArtifact ,
0 commit comments