(session_id: str, cwd: str, perm_mode: str | None)
| 1295 | cfg = config or AgentServerConfig() |
| 1296 | |
| 1297 | async def spawn(session_id: str, cwd: str, perm_mode: str | None) -> AgentHandle: |
| 1298 | loop = asyncio.get_running_loop() |
| 1299 | out_queue: asyncio.Queue[dict | None] = asyncio.Queue() |
| 1300 | sess = _AgentSession( |
| 1301 | session_id=session_id, |
| 1302 | cwd=cwd, |
| 1303 | config=cfg, |
| 1304 | loop=loop, |
| 1305 | out_queue=out_queue, |
| 1306 | ) |
| 1307 | # Build the provider/registry/tool_context off the event loop — these |
| 1308 | # touch config/filesystem and must not block the WS pump. |
| 1309 | await loop.run_in_executor(None, lambda: _build_runtime(sess, perm_mode)) |
| 1310 | # Wire the permission handler now that tool_context exists. |
| 1311 | if sess.tool_context is not None and sess.init_error is None: |
| 1312 | sess.tool_context.permission_handler = sess.permission_handler |
| 1313 | sess.start() |
| 1314 | sess.emit_init() |
| 1315 | |
| 1316 | async def messages_from_agent() -> AsyncIterator[dict]: |
| 1317 | while True: |
| 1318 | item = await out_queue.get() |
| 1319 | if item is None: |
| 1320 | return |
| 1321 | yield item |
| 1322 | |
| 1323 | return AgentHandle( |
| 1324 | send_to_agent=sess.send_to_agent, |
| 1325 | messages_from_agent=messages_from_agent, |
| 1326 | shutdown=sess.shutdown, |
| 1327 | ) |
| 1328 | |
| 1329 | return spawn |
| 1330 |
no test coverage detected