88from agents import (
99 Agent ,
1010 Runner ,
11- SQLiteSession ,
1211)
1312from uipath .runtime import (
1413 UiPathExecuteOptions ,
2726from ._serialize import serialize_output
2827from .errors import UiPathOpenAIAgentsErrorCode , UiPathOpenAIAgentsRuntimeError
2928from .schema import get_agent_schema , get_entrypoints_schema
30- from .storage import SqliteAgentStorage
3129
3230
3331class UiPathOpenAIAgentRuntime :
@@ -40,8 +38,6 @@ def __init__(
4038 agent : Agent ,
4139 runtime_id : str | None = None ,
4240 entrypoint : str | None = None ,
43- storage_path : str | None = None ,
44- storage : SqliteAgentStorage | None = None ,
4541 ):
4642 """
4743 Initialize the runtime.
@@ -50,14 +46,10 @@ def __init__(
5046 agent: The OpenAI Agent to execute
5147 runtime_id: Unique identifier for this runtime instance
5248 entrypoint: Optional entrypoint name (for schema generation)
53- storage_path: Path to SQLite database for session persistence
54- storage: Optional storage instance for state persistence
5549 """
5650 self .agent : Agent = agent
5751 self .runtime_id : str = runtime_id or "default"
5852 self .entrypoint : str | None = entrypoint
59- self .storage_path : str | None = storage_path
60- self .storage : SqliteAgentStorage | None = storage
6153
6254 # Configure OpenAI Agents SDK to use Responses API
6355 # UiPath supports both APIs via X-UiPath-LlmGateway-ApiFlavor header
@@ -204,54 +196,27 @@ async def _run_agent(
204196 Runtime events if stream_events=True, then final result
205197 """
206198 agent_input = self ._prepare_agent_input (input )
207- is_resuming = bool (options and options .resume )
208-
209- # Create session for state persistence (local to this run)
210- # SQLiteSession automatically loads existing data from the database when created
211- session : SQLiteSession | None = None
212- if self .storage_path :
213- session = SQLiteSession (self .runtime_id , self .storage_path )
214199
215200 # Run the agent with streaming if events requested
216- try :
217- if stream_events :
218- # Use streaming for events
219- async for event_or_result in self ._run_agent_streamed (
220- agent_input , options , stream_events , session
221- ):
222- yield event_or_result
223- else :
224- # Use non-streaming for simple execution
225- result = await Runner .run (
226- starting_agent = self .agent ,
227- input = agent_input ,
228- session = session ,
229- )
230- yield self ._create_success_result (result .final_output )
231-
232- except Exception :
233- # Clean up session on error
234- if session and self .storage_path and not is_resuming :
235- # Delete incomplete session
236- try :
237- import os
238-
239- if os .path .exists (self .storage_path ):
240- os .remove (self .storage_path )
241- except Exception :
242- pass # Best effort cleanup
243- raise
244- finally :
245- # Always close session after run completes with proper WAL checkpoint
246- if session :
247- self ._close_session_with_checkpoint (session )
201+ if stream_events :
202+ # Use streaming for events
203+ async for event_or_result in self ._run_agent_streamed (
204+ agent_input , options , stream_events
205+ ):
206+ yield event_or_result
207+ else :
208+ # Use non-streaming for simple execution
209+ result = await Runner .run (
210+ starting_agent = self .agent ,
211+ input = agent_input ,
212+ )
213+ yield self ._create_success_result (result .final_output )
248214
249215 async def _run_agent_streamed (
250216 self ,
251217 agent_input : str | list [Any ],
252218 options : UiPathExecuteOptions | UiPathStreamOptions | None ,
253219 stream_events : bool ,
254- session : SQLiteSession | None ,
255220 ) -> AsyncGenerator [UiPathRuntimeEvent | UiPathRuntimeResult , None ]:
256221 """
257222 Run agent using streaming API to enable event streaming.
@@ -269,7 +234,6 @@ async def _run_agent_streamed(
269234 result = Runner .run_streamed (
270235 starting_agent = self .agent ,
271236 input = agent_input ,
272- session = session ,
273237 )
274238
275239 # Stream events from the agent
@@ -485,45 +449,6 @@ async def get_schema(self) -> UiPathRuntimeSchema:
485449 graph = get_agent_schema (self .agent ),
486450 )
487451
488- def _close_session_with_checkpoint (self , session : SQLiteSession ) -> None :
489- """Close SQLite session with WAL checkpoint to release file locks.
490-
491- OpenAI SDK uses sync sqlite3 which doesn't release file locks on Windows
492- without explicit WAL checkpoint. This is especially important for cleanup.
493-
494- Args:
495- session: The SQLiteSession to close
496- """
497- try :
498- # Get the underlying connection
499- conn = session ._get_connection ()
500-
501- # Commit any pending transactions
502- try :
503- conn .commit ()
504- except Exception :
505- pass # Best effort
506-
507- # Force WAL checkpoint to release shared memory files
508- # This is especially important on Windows
509- try :
510- conn .execute ("PRAGMA wal_checkpoint(TRUNCATE)" )
511- conn .commit ()
512- except Exception :
513- pass # Best effort
514-
515- except Exception :
516- pass # Best effort cleanup
517-
518- finally :
519- # Always call the session's close method
520- try :
521- session .close ()
522- except Exception :
523- pass # Best effort
524-
525452 async def dispose (self ) -> None :
526453 """Cleanup runtime resources."""
527- # Sessions are closed immediately after each run in _run_agent()
528- # Storage is shared across runtimes and managed by the factory
529454 pass
0 commit comments