Skip to content

Commit 9a4c3d8

Browse files
committed
Update web session on use
1 parent b3651e5 commit 9a4c3d8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

llmstack/processors/providers/api_processor_interface.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import ujson as json
77
from django import db
8+
from django.shortcuts import get_object_or_404
89
from pydantic import BaseModel
910

1011
from llmstack.apps.app_session_utils import get_app_session_data, save_app_session_data
@@ -133,6 +134,13 @@ def _create_asset_stream(self, mime_type, file_name=None):
133134
return AssetStream(asset)
134135
return None
135136

137+
def update_connection(self, connection):
138+
from llmstack.base.models import Profile
139+
140+
if self._request_user and self._request_user.is_authenticated:
141+
profile = get_object_or_404(Profile, user=self._request_user)
142+
profile.add_connection(connection)
143+
136144
def __init__(
137145
self,
138146
input,

llmstack/processors/providers/promptly/static_web_browser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ def process(self) -> dict:
225225
output_stream = self._output_stream
226226
browser_response = None
227227
session_videos = []
228+
updated_session_data = None
229+
228230
with WebBrowser(
229231
f"{settings.RUNNER_HOST}:{settings.RUNNER_PORT}",
230232
interactive=self._config.stream_video,
@@ -237,6 +239,7 @@ def process(self) -> dict:
237239
else ""
238240
),
239241
record_video=self._config.capture_session_video,
242+
persist_session=bool(self._config.connection_id),
240243
) as web_browser:
241244
if self._config.stream_video and web_browser.get_wss_url():
242245
async_to_sync(
@@ -262,6 +265,9 @@ def process(self) -> dict:
262265
if self._config.capture_session_video:
263266
session_videos = web_browser.get_videos()
264267

268+
if self._config.connection_id:
269+
updated_session_data = web_browser.terminate()
270+
265271
if browser_response:
266272
output_text = browser_response.text or "\n".join(
267273
list(map(lambda entry: entry.output, browser_response.command_outputs))
@@ -315,4 +321,15 @@ def process(self) -> dict:
315321

316322
output = output_stream.finalize()
317323

324+
if self._config.connection_id and updated_session_data:
325+
self.update_connection(
326+
{
327+
**self._env["connections"][self._config.connection_id],
328+
"configuration": {
329+
**self._env["connections"][self._config.connection_id]["configuration"],
330+
"_storage_state": updated_session_data,
331+
},
332+
}
333+
)
334+
318335
return output

llmstack/processors/providers/promptly/web_browser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)