Skip to content

Commit bd75b6d

Browse files
authored
fix: update CopilotSession to handle workspace_path as os.PathLike and ensure proper initialization (#901)
Also have the workspace_path property return a cached pathlib.Path instance.
1 parent 864eab1 commit bd75b6d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

python/copilot/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ async def create_session(
12521252

12531253
# Create and register the session before issuing the RPC so that
12541254
# events emitted by the CLI (e.g. session.start) are not dropped.
1255-
session = CopilotSession(actual_session_id, self._client, None)
1255+
session = CopilotSession(actual_session_id, self._client, workspace_path=None)
12561256
session._register_tools(tools)
12571257
session._register_permission_handler(on_permission_request)
12581258
if on_user_input_request:
@@ -1456,7 +1456,7 @@ async def resume_session(
14561456

14571457
# Create and register the session before issuing the RPC so that
14581458
# events emitted by the CLI (e.g. session.start) are not dropped.
1459-
session = CopilotSession(session_id, self._client, None)
1459+
session = CopilotSession(session_id, self._client, workspace_path=None)
14601460
session._register_tools(tools)
14611461
session._register_permission_handler(on_permission_request)
14621462
if on_user_input_request:

python/copilot/session.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
from __future__ import annotations
1010

1111
import asyncio
12+
import functools
1213
import inspect
14+
import os
15+
import pathlib
1316
import threading
1417
from collections.abc import Awaitable, Callable
1518
from dataclasses import dataclass
@@ -639,7 +642,9 @@ class CopilotSession:
639642
... unsubscribe()
640643
"""
641644

642-
def __init__(self, session_id: str, client: Any, workspace_path: str | None = None):
645+
def __init__(
646+
self, session_id: str, client: Any, workspace_path: os.PathLike[str] | str | None = None
647+
):
643648
"""
644649
Initialize a new CopilotSession.
645650
@@ -655,7 +660,7 @@ def __init__(self, session_id: str, client: Any, workspace_path: str | None = No
655660
"""
656661
self.session_id = session_id
657662
self._client = client
658-
self._workspace_path = workspace_path
663+
self._workspace_path = os.fsdecode(workspace_path) if workspace_path is not None else None
659664
self._event_handlers: set[Callable[[SessionEvent], None]] = set()
660665
self._event_handlers_lock = threading.Lock()
661666
self._tool_handlers: dict[str, ToolHandler] = {}
@@ -677,15 +682,19 @@ def rpc(self) -> SessionRpc:
677682
self._rpc = SessionRpc(self._client, self.session_id)
678683
return self._rpc
679684

680-
@property
681-
def workspace_path(self) -> str | None:
685+
@functools.cached_property
686+
def workspace_path(self) -> pathlib.Path | None:
682687
"""
683688
Path to the session workspace directory when infinite sessions are enabled.
684689
685690
Contains checkpoints/, plan.md, and files/ subdirectories.
686691
None if infinite sessions are disabled.
687692
"""
688-
return self._workspace_path
693+
# Done as a property as self._workspace_path is directly set from a server
694+
# response post-init. So it was either make sure all places directly setting
695+
# the attribute handle the None case appropriately, use a setter for the
696+
# attribute to do the conversion, or just do the conversion lazily via a getter.
697+
return pathlib.Path(self._workspace_path) if self._workspace_path else None
689698

690699
async def send(
691700
self,

0 commit comments

Comments
 (0)