@@ -465,6 +465,8 @@ def _process_anthropic(self) -> dict:
465465 credentials_tool_schema ["input_schema" ] = credentials_tool_schema ["parameters" ]
466466 del credentials_tool_schema ["parameters" ]
467467
468+ updated_session_data = None
469+
468470 with WebBrowserClient (
469471 f"{ settings .RUNNER_HOST } :{ settings .RUNNER_PORT } " ,
470472 interactive = self ._config .stream_video ,
@@ -477,6 +479,7 @@ def _process_anthropic(self) -> dict:
477479 else ""
478480 ),
479481 record_video = self ._config .record_session_video ,
482+ persist_session = bool (self ._config .connection_id ),
480483 ) as web_browser :
481484 browser_response = web_browser .run_commands (
482485 commands = [
@@ -667,6 +670,9 @@ def _process_anthropic(self) -> dict:
667670 if self ._config .record_session_video :
668671 session_videos = web_browser .get_videos ()
669672
673+ if self ._config .connection_id :
674+ updated_session_data = web_browser .terminate ()
675+
670676 if browser_response :
671677 output_text = "\n " .join (list (map (lambda entry : entry .output , browser_response .command_outputs )))
672678 browser_content = WebBrowserContent (** browser_response .model_dump (exclude = ("screenshot" , "downloads" )))
@@ -715,12 +721,26 @@ def _process_anthropic(self) -> dict:
715721 async_to_sync (self ._output_stream .write )(WebBrowserOutput (videos = encoded_videos ))
716722
717723 output = self ._output_stream .finalize ()
724+
725+ # Update the session data if we have a connection ID
726+ if self ._config .connection_id and updated_session_data :
727+ self .update_connection (
728+ {
729+ ** self ._env ["connections" ][self ._config .connection_id ],
730+ "configuration" : {
731+ ** self ._env ["connections" ][self ._config .connection_id ]["configuration" ],
732+ "_storage_state" : updated_session_data ,
733+ },
734+ }
735+ )
718736 return output
719737
720738 def process (self ) -> dict :
721739 if self ._config .provider_config .provider == "anthropic" :
722740 return self ._process_anthropic ()
723741
742+ updated_session_data = None
743+
724744 client = get_llm_client_from_provider_config (
725745 provider = self ._config .provider_config .provider ,
726746 model_slug = self ._config .provider_config .model .value ,
@@ -752,6 +772,7 @@ def process(self) -> dict:
752772 if self ._config .connection_id
753773 else ""
754774 ),
775+ persist_session = bool (self ._config .connection_id ),
755776 ) as web_browser :
756777 # Start streaming video if enabled
757778 if self ._config .stream_video and web_browser .get_wss_url ():
@@ -938,6 +959,9 @@ def process(self) -> dict:
938959 )
939960 )
940961
962+ if self ._config .connection_id :
963+ updated_session_data = web_browser .terminate ()
964+
941965 if browser_response :
942966 output_text = "\n " .join (list (map (lambda entry : entry .output , browser_response .command_outputs )))
943967 browser_content = browser_response .model_dump (exclude = ("screenshot" ,))
@@ -951,4 +975,17 @@ def process(self) -> dict:
951975 async_to_sync (self ._output_stream .write )(WebBrowserOutput (text = output_text , content = browser_content ))
952976
953977 output = self ._output_stream .finalize ()
978+
979+ # Update the session data if we have a connection ID
980+ if self ._config .connection_id and updated_session_data :
981+ self .update_connection (
982+ {
983+ ** self ._env ["connections" ][self ._config .connection_id ],
984+ "configuration" : {
985+ ** self ._env ["connections" ][self ._config .connection_id ]["configuration" ],
986+ "_storage_state" : updated_session_data ,
987+ },
988+ }
989+ )
990+
954991 return output
0 commit comments