@@ -99,11 +99,9 @@ def _workspace_choices(workspaces: list[WorkspaceInfo]) -> str:
9999async def get_available_workspaces (context : Optional [Context ] = None ) -> list [WorkspaceInfo ]:
100100 """Load available cloud workspaces for the current authenticated user."""
101101 if context :
102- cached_workspaces = context .get_state ("available_workspaces" )
103- if isinstance (cached_workspaces , list ) and all (
104- isinstance (item , WorkspaceInfo ) for item in cached_workspaces
105- ):
106- return cached_workspaces
102+ cached_raw = await context .get_state ("available_workspaces" )
103+ if isinstance (cached_raw , list ):
104+ return [WorkspaceInfo .model_validate (item ) for item in cached_raw ]
107105
108106 from basic_memory .mcp .async_client import get_cloud_control_plane_client
109107 from basic_memory .mcp .tools .utils import call_get
@@ -113,7 +111,10 @@ async def get_available_workspaces(context: Optional[Context] = None) -> list[Wo
113111 workspace_list = WorkspaceListResponse .model_validate (response .json ())
114112
115113 if context :
116- context .set_state ("available_workspaces" , workspace_list .workspaces )
114+ await context .set_state (
115+ "available_workspaces" ,
116+ [ws .model_dump () for ws in workspace_list .workspaces ],
117+ )
117118
118119 return workspace_list .workspaces
119120
@@ -124,12 +125,12 @@ async def resolve_workspace_parameter(
124125) -> WorkspaceInfo :
125126 """Resolve workspace using explicit input, session cache, and cloud discovery."""
126127 if context :
127- cached_workspace = context .get_state ("active_workspace" )
128- if isinstance (cached_workspace , WorkspaceInfo ) and (
129- workspace is None or _workspace_matches_identifier ( cached_workspace , workspace )
130- ):
131- logger .debug (f"Using cached workspace from context: { cached_workspace .tenant_id } " )
132- return cached_workspace
128+ cached_raw = await context .get_state ("active_workspace" )
129+ if isinstance (cached_raw , dict ):
130+ cached_workspace = WorkspaceInfo . model_validate ( cached_raw )
131+ if workspace is None or _workspace_matches_identifier ( cached_workspace , workspace ):
132+ logger .debug (f"Using cached workspace from context: { cached_workspace .tenant_id } " )
133+ return cached_workspace
133134
134135 workspaces = await get_available_workspaces (context = context )
135136 if not workspaces :
@@ -164,7 +165,7 @@ async def resolve_workspace_parameter(
164165 )
165166
166167 if context :
167- context .set_state ("active_workspace" , selected_workspace )
168+ await context .set_state ("active_workspace" , selected_workspace . model_dump () )
168169 logger .debug (f"Cached workspace in context: { selected_workspace .tenant_id } " )
169170
170171 return selected_workspace
@@ -206,10 +207,12 @@ async def get_active_project(
206207
207208 # Check if already cached in context
208209 if context :
209- cached_project = context .get_state ("active_project" )
210- if cached_project and cached_project .name == project :
211- logger .debug (f"Using cached project from context: { project } " )
212- return cached_project
210+ cached_raw = await context .get_state ("active_project" )
211+ if isinstance (cached_raw , dict ):
212+ cached_project = ProjectItem .model_validate (cached_raw )
213+ if cached_project .name == project :
214+ logger .debug (f"Using cached project from context: { project } " )
215+ return cached_project
213216
214217 # Validate project exists by calling API
215218 logger .debug (f"Validating project: { project } " )
@@ -230,7 +233,7 @@ async def get_active_project(
230233
231234 # Cache in context if available
232235 if context :
233- context .set_state ("active_project" , active_project )
236+ await context .set_state ("active_project" , active_project . model_dump () )
234237 logger .debug (f"Cached project in context: { project } " )
235238
236239 logger .debug (f"Validated project: { active_project .name } " )
@@ -307,7 +310,7 @@ async def resolve_project_and_path(
307310 is_default = resolved .is_default ,
308311 )
309312 if context :
310- context .set_state ("active_project" , active_project )
313+ await context .set_state ("active_project" , active_project . model_dump () )
311314
312315 resolved_path = f"{ resolved .permalink } /{ remainder } " if include_project else remainder
313316 return active_project , resolved_path , True
0 commit comments