Skip to content

Commit 75fdbf0

Browse files
committed
fix: windows execution
1 parent 699acdb commit 75fdbf0

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

  • packages/uipath-openai-agents/src/uipath_openai_agents/runtime

packages/uipath-openai-agents/src/uipath_openai_agents/runtime/runtime.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def __init__(
6161
self.storage_path: str | None = storage_path
6262
self.loaded_object: Any | None = loaded_object
6363
self.storage: SqliteAgentStorage | None = storage
64-
self._session: SQLiteSession | None = None
6564

6665
# Configure OpenAI Agents SDK to use Responses API
6766
# UiPath supports both APIs via X-UiPath-LlmGateway-ApiFlavor header
@@ -210,38 +209,32 @@ async def _run_agent(
210209
agent_input = self._prepare_agent_input(input)
211210
is_resuming = bool(options and options.resume)
212211

213-
# Get or create session for state persistence
214-
# Close any existing session before creating a new one
215-
if self._session:
216-
self._session.close()
217-
self._session = None
218-
212+
# Create session for state persistence (local to this run)
219213
# SQLiteSession automatically loads existing data from the database when created
214+
session: SQLiteSession | None = None
220215
if self.storage_path:
221-
self._session = SQLiteSession(self.runtime_id, self.storage_path)
222-
else:
223-
self._session = None
216+
session = SQLiteSession(self.runtime_id, self.storage_path)
224217

225218
# Run the agent with streaming if events requested
226219
try:
227220
if stream_events:
228221
# Use streaming for events
229222
async for event_or_result in self._run_agent_streamed(
230-
agent_input, options, stream_events
223+
agent_input, options, stream_events, session
231224
):
232225
yield event_or_result
233226
else:
234227
# Use non-streaming for simple execution
235228
result = await Runner.run(
236229
starting_agent=self.agent,
237230
input=agent_input,
238-
session=self._session,
231+
session=session,
239232
)
240233
yield self._create_success_result(result.final_output)
241234

242235
except Exception:
243236
# Clean up session on error
244-
if self._session and self.storage_path and not is_resuming:
237+
if session and self.storage_path and not is_resuming:
245238
# Delete incomplete session
246239
try:
247240
import os
@@ -251,12 +244,17 @@ async def _run_agent(
251244
except Exception:
252245
pass # Best effort cleanup
253246
raise
247+
finally:
248+
# Always close session after run completes
249+
if session:
250+
session.close()
254251

255252
async def _run_agent_streamed(
256253
self,
257254
agent_input: str | list[Any],
258255
options: UiPathExecuteOptions | UiPathStreamOptions | None,
259256
stream_events: bool,
257+
session: SQLiteSession | None,
260258
) -> AsyncGenerator[UiPathRuntimeEvent | UiPathRuntimeResult, None]:
261259
"""
262260
Run agent using streaming API to enable event streaming.
@@ -274,7 +272,7 @@ async def _run_agent_streamed(
274272
result = Runner.run_streamed(
275273
starting_agent=self.agent,
276274
input=agent_input,
277-
session=self._session,
275+
session=session,
278276
)
279277

280278
# Stream events from the agent
@@ -492,10 +490,6 @@ async def get_schema(self) -> UiPathRuntimeSchema:
492490

493491
async def dispose(self) -> None:
494492
"""Cleanup runtime resources."""
495-
# Close OpenAI Agents SDK session if it exists
496-
if self._session:
497-
self._session.close()
498-
self._session = None
499-
493+
# Sessions are closed immediately after each run in _run_agent()
500494
# Storage is shared across runtimes and managed by the factory
501-
# Do not dispose it here
495+
pass

0 commit comments

Comments
 (0)