1- import { headerBuilder } from '../api/config' ;
1+ import { getApiUrl , headerBuilder } from '../api/config' ;
22import { PlanDataService } from './PlanDataService' ;
33import { MPlanData , ParsedPlanApprovalRequest , StreamingPlanUpdate , StreamMessage , WebsocketMessageType } from '../models' ;
44
@@ -12,8 +12,24 @@ class WebSocketService {
1212 private planSubscriptions : Set < string > = new Set ( ) ;
1313 private reconnectTimer : NodeJS . Timeout | null = null ;
1414 private isConnecting = false ;
15- private baseWsUrl = process . env . REACT_APP_BACKEND_URL ?. replace ( 'http' , 'ws' ) || 'ws://localhost:8000' ;
15+ private baseWsUrl = getApiUrl ( ) || 'ws://localhost:8000' ;
1616
17+ private buildSocketUrl ( processId ?: string , sessionId ?: string ) : string {
18+ // Trim and remove trailing slashes
19+ let base = ( this . baseWsUrl || '' ) . trim ( ) . replace ( / \/ + $ / , '' ) ;
20+ // Normalize protocol: http -> ws, https -> wss
21+ base = base . replace ( / ^ h t t p : \/ \/ / i, 'ws://' )
22+ . replace ( / ^ h t t p s : \/ \/ / i, 'wss://' ) ;
23+
24+ // Leave ws/wss as-is; anything else is assumed already correct
25+
26+ // Decide path addition
27+ const hasApiSegment = / \/ a p i ( \/ | $ ) / i. test ( base ) ;
28+ const socketPath = hasApiSegment ? '/v3/socket' : '/api/v3/socket' ;
29+ const url = `${ base } ${ socketPath } ${ processId ? `/${ processId } ` : `/${ sessionId } ` } ` ;
30+ console . log ( "Constructed WebSocket URL:" , url ) ;
31+ return url ;
32+ }
1733 connect ( sessionId : string , processId ?: string ) : Promise < void > {
1834 return new Promise ( ( resolve , reject ) => {
1935 if ( this . isConnecting ) {
@@ -26,9 +42,7 @@ class WebSocketService {
2642 }
2743 try {
2844 this . isConnecting = true ;
29- const wsUrl = processId
30- ? `${ this . baseWsUrl } /api/v3/socket/${ processId } `
31- : `${ this . baseWsUrl } /api/v3/socket/${ sessionId } ` ;
45+ const wsUrl = this . buildSocketUrl ( processId , sessionId ) ;
3246 this . ws = new WebSocket ( wsUrl ) ;
3347
3448 this . ws . onopen = ( ) => {
@@ -210,7 +224,9 @@ class WebSocketService {
210224
211225 case WebsocketMessageType . USER_CLARIFICATION_REQUEST : {
212226 if ( message . data ) {
213- //const transformed = PlanDataService.parseUserClarificationRequest(message);
227+ //\const transformed = PlanDataService.parseUserClarificationRequest(message);
228+ console . log ( 'WebSocket USER_CLARIFICATION_REQUEST message received:' , message ) ;
229+
214230 this . emit ( WebsocketMessageType . USER_CLARIFICATION_REQUEST , message ) ;
215231 }
216232 break ;
0 commit comments