11import { swallow } from "./diagnostics" ;
22import type { HfColorGradingTarget } from "../colorGrading" ;
33import type { RuntimeBridgeControlMessage , RuntimeOutboundMessage } from "./types" ;
4+ import {
5+ inspectRuntimeProtocol ,
6+ runtimeProtocolFpsToNumber ,
7+ runtimeProtocolMetadata ,
8+ type RuntimeProtocolV1 ,
9+ } from "./protocol" ;
410
511type BridgeDeps = {
612 onPlay : ( ) => void ;
713 onPause : ( ) => void ;
814 onStopMedia : ( ) => void ;
9- onSeek : ( frame : number , seekMode : "drag" | "commit" ) => void ;
15+ onSeek : ( timeSeconds : number , seekMode : "drag" | "commit" ) => void ;
1016 onTick : ( ) => void ;
1117 onSetMuted : ( muted : boolean ) => void ;
1218 onSetVolume : ( volume : number ) => void ;
@@ -22,18 +28,25 @@ type BridgeDeps = {
2228 ) => void ;
2329 onEnablePickMode : ( ) => void ;
2430 onDisablePickMode : ( ) => void ;
31+ getCanonicalFps : ( ) => number ;
2532} ;
2633
34+ let runtimeProtocolFps = 30 ;
35+
36+ export function setRuntimeProtocolFps ( fps : number ) : void {
37+ runtimeProtocolFps = Number . isFinite ( fps ) && fps > 0 ? fps : 30 ;
38+ }
39+
2740export function postRuntimeMessage ( payload : RuntimeOutboundMessage ) : void {
2841 try {
29- window . parent . postMessage ( payload , "*" ) ;
42+ window . parent . postMessage ( { ... payload , ... runtimeProtocolMetadata ( runtimeProtocolFps ) } , "*" ) ;
3043 } catch ( err ) {
3144 // Cross-frame posting can throw if the parent is gone or origin-isolated.
3245 swallow ( "bridge.postMessage" , err ) ;
3346 }
3447}
3548
36- type BridgeControlData = Partial < RuntimeBridgeControlMessage > ;
49+ type BridgeControlData = Partial < RuntimeBridgeControlMessage & RuntimeProtocolV1 > ;
3750type ControlHandler = ( data : BridgeControlData , deps : BridgeDeps ) => void ;
3851
3952// Per-action dispatchers. Splitting the handler into a lookup table keeps the
@@ -43,7 +56,7 @@ const CONTROL_HANDLERS: Record<string, ControlHandler> = {
4356 play : ( _d , deps ) => deps . onPlay ( ) ,
4457 pause : ( _d , deps ) => deps . onPause ( ) ,
4558 "stop-media" : ( _d , deps ) => deps . onStopMedia ( ) ,
46- seek : ( data , deps ) => deps . onSeek ( Number ( data . frame ?? 0 ) , data . seekMode ?? "commit" ) ,
59+ seek : ( data , deps ) => deps . onSeek ( resolveSeekTimeSeconds ( data , deps ) , data . seekMode ?? "commit" ) ,
4760 tick : ( _d , deps ) => deps . onTick ( ) ,
4861 "set-muted" : ( data , deps ) => deps . onSetMuted ( Boolean ( data . muted ) ) ,
4962 "set-volume" : ( data , deps ) =>
@@ -64,6 +77,31 @@ const CONTROL_HANDLERS: Record<string, ControlHandler> = {
6477 "flash-elements" : ( data ) => handleFlashElements ( data ) ,
6578} ;
6679
80+ function resolveSeekTimeSeconds ( data : BridgeControlData , deps : BridgeDeps ) : number {
81+ const explicitSeconds = Number ( data . timeSeconds ) ;
82+ if ( Number . isFinite ( explicitSeconds ) ) return Math . max ( 0 , explicitSeconds ) ;
83+ const messageFps = runtimeProtocolFpsToNumber ( data . fps ) ;
84+ const fps = messageFps ?? deps . getCanonicalFps ( ) ;
85+ return Math . max ( 0 , Number ( data . frame ?? 0 ) ) / fps ;
86+ }
87+
88+ function rejectUnsupportedProtocol ( data : BridgeControlData ) : boolean {
89+ const protocol = inspectRuntimeProtocol ( data ) ;
90+ if ( protocol . status !== "unsupported" ) return false ;
91+ postRuntimeMessage ( {
92+ source : "hf-preview" ,
93+ type : "diagnostic" ,
94+ code : `runtime.protocol.${ protocol . code } ` ,
95+ details : {
96+ receivedVersion :
97+ typeof protocol . receivedVersion === "string" || typeof protocol . receivedVersion === "number"
98+ ? protocol . receivedVersion
99+ : null ,
100+ } ,
101+ } ) ;
102+ return true ;
103+ }
104+
67105function handleFlashElements ( data : BridgeControlData ) : void {
68106 // Briefly highlight elements — used by the chat-canvas bridge
69107 // to show what changed after an agent edit
@@ -78,6 +116,7 @@ export function installRuntimeControlBridge(deps: BridgeDeps): (event: MessageEv
78116 const handler = ( event : MessageEvent ) => {
79117 const data = event . data as BridgeControlData | null ;
80118 if ( ! data || data . source !== "hf-parent" || data . type !== "control" ) return ;
119+ if ( rejectUnsupportedProtocol ( data ) ) return ;
81120 const action = data . action ;
82121 if ( typeof action !== "string" ) return ;
83122 const fn = CONTROL_HANDLERS [ action ] ;
0 commit comments