MCPcopy Create free account
hub / github.com/aliyun/data-agent-skill / create_or_reuse

Method create_or_reuse

scripts/data_agent/session.py:282–334  ·  view source on GitHub ↗

Create a new session or reuse an existing one asynchronously. Args: session_id: Optional existing session ID to reuse. database_id: Optional database ID to bind to new session. wait_for_running: Whether to wait for session to be RUNNING. works

(
        self,
        session_id: Optional[str] = None,
        database_id: Optional[str] = None,
        wait_for_running: bool = True,
        workspace_id: Optional[str] = None,
        custom_agent_id: Optional[str] = None,
    )

Source from the content-addressed store, hash-verified

280 self._client = client
281 self._active_sessions: Dict[str, SessionInfo] = {}
282
283 async def create_or_reuse(
284 self,
285 session_id: Optional[str] = None,
286 database_id: Optional[str] = None,
287 wait_for_running: bool = True,
288 workspace_id: Optional[str] = None,
289 custom_agent_id: Optional[str] = None,
290 ) -> SessionInfo:
291 """Create a new session or reuse an existing one asynchronously.
292
293 Args:
294 session_id: Optional existing session ID to reuse.
295 database_id: Optional database ID to bind to new session.
296 wait_for_running: Whether to wait for session to be RUNNING.
297 workspace_id: Optional workspace ID to bind the session to a workspace.
298 custom_agent_id: Optional custom agent ID to use for the session.
299
300 Returns:
301 SessionInfo for the active session.
302 """
303 # Try to reuse existing session
304 if session_id:
305 if session_id in self._active_sessions:
306 session = self._active_sessions[session_id]
307 if await self.is_session_active(session_id):
308 session.update_last_used()
309 return session
310
311 try:
312 session = await self._client.describe_session(
313 session_id=session_id,
314 agent_id="",
315 workspace_id=workspace_id or "",
316 )
317 if session.is_running():
318 self._active_sessions[session_id] = session
319 return session
320 except Exception:
321 pass
322
323 # Create new session
324 session = await self._client.create_session(database_id=database_id, workspace_id=workspace_id, custom_agent_id=custom_agent_id)
325
326 # Only wait if session is not already running
327 if wait_for_running and not session.is_running():
328 session = await self.wait_until_running(
329 session_id=session.session_id,
330 agent_id=session.agent_id,
331 workspace_id=workspace_id or "",
332 )
333
334 self._active_sessions[session.session_id] = session
335 return session
336
337 async def wait_until_running(

Callers

nothing calls this directly

Calls 6

is_session_activeMethod · 0.95
wait_until_runningMethod · 0.95
update_last_usedMethod · 0.80
is_runningMethod · 0.80
describe_sessionMethod · 0.45
create_sessionMethod · 0.45

Tested by

no test coverage detected