22
33from __future__ import annotations
44
5+ import sqlite3
56from dataclasses import dataclass , replace
6- from typing import TYPE_CHECKING
77
88from services .workspace_db import (
99 build_composer_id_to_workspace_id ,
1818 create_workspace_path_to_id_map ,
1919)
2020
21- if TYPE_CHECKING :
22- import sqlite3
23-
2421
2522@dataclass (frozen = True )
2623class WorkspaceContext :
2724 """Precomputed workspace-resolution maps for conversation assignment."""
2825
29- workspace_path : str
3026 workspace_entries : list [dict ]
3127 invalid_workspace_ids : set [str ]
3228 composer_id_to_workspace_id : dict [str , str ]
@@ -36,82 +32,79 @@ class WorkspaceContext:
3632 bubble_map : dict [str , dict ]
3733
3834
35+ def _entries (
36+ workspace_path : str ,
37+ workspace_entries : list [dict ] | None ,
38+ ) -> list [dict ]:
39+ if workspace_entries is not None :
40+ return workspace_entries
41+ return collect_workspace_entries (workspace_path )
42+
43+
44+ def _assemble_context (
45+ entries : list [dict ],
46+ * ,
47+ invalid_workspace_ids : set [str ],
48+ workspace_path_to_id : dict [str , str ],
49+ composer_id_to_workspace_id : dict [str , str ],
50+ ) -> WorkspaceContext :
51+ return WorkspaceContext (
52+ workspace_entries = entries ,
53+ invalid_workspace_ids = invalid_workspace_ids ,
54+ composer_id_to_workspace_id = composer_id_to_workspace_id ,
55+ project_name_to_workspace_id = create_project_name_to_workspace_id_map (entries ),
56+ workspace_path_to_id = workspace_path_to_id ,
57+ project_layouts_map = {},
58+ bubble_map = {},
59+ )
60+
61+
3962def resolve_workspace_context (
4063 workspace_path : str ,
4164 * ,
4265 workspace_entries : list [dict ] | None = None ,
43- rules : list | None = None ,
44- nocache : bool = False ,
45- use_composer_cache : bool = False ,
46- include_invalid_workspace_ids : bool = True ,
47- include_workspace_path_map : bool = True ,
48- global_db : sqlite3 .Connection | None = None ,
49- populate_project_layouts : bool = False ,
50- populate_bubble_map : bool = False ,
5166) -> WorkspaceContext :
52- """Run the workspace-determination ceremony and return a typed context.
53-
54- Always resolves ``workspace_entries`` (when not supplied), composer and
55- project-name maps. Optional pieces are controlled by flags so lightweight
56- consumers (e.g. HTTP export) can omit unused maps.
57-
58- Args:
59- workspace_path: Cursor ``workspaceStorage`` root.
60- workspace_entries: Pre-collected entries; when ``None``, scanned from disk.
61- rules: Exclusion rules; required when ``use_composer_cache`` is ``True``.
62- nocache: Skip the mtime-keyed composer-map disk cache.
63- use_composer_cache: Use :func:`build_composer_id_to_workspace_id_cached`.
64- include_invalid_workspace_ids: When ``False``, ``invalid_workspace_ids`` is empty.
65- include_workspace_path_map: When ``False``, ``workspace_path_to_id`` is empty.
66- global_db: Open global ``state.vscdb`` connection for optional KV loads.
67- populate_project_layouts: Populate ``project_layouts_map`` from *global_db*.
68- populate_bubble_map: Populate ``bubble_map`` from *global_db*.
69-
70- Returns:
71- :class:`WorkspaceContext` with all requested maps populated.
72- """
73- entries = (
74- workspace_entries
75- if workspace_entries is not None
76- else collect_workspace_entries (workspace_path )
67+ """Full workspace maps with an uncached composer→workspace scan (CLI export)."""
68+ entries = _entries (workspace_path , workspace_entries )
69+ return _assemble_context (
70+ entries ,
71+ invalid_workspace_ids = collect_invalid_workspace_ids (entries ),
72+ workspace_path_to_id = create_workspace_path_to_id_map (entries ),
73+ composer_id_to_workspace_id = build_composer_id_to_workspace_id (
74+ workspace_path , entries ,
75+ ),
7776 )
78- invalid_ids = (
79- collect_invalid_workspace_ids (entries )
80- if include_invalid_workspace_ids
81- else set ()
82- )
83- project_name_map = create_project_name_to_workspace_id_map (entries )
84- workspace_path_map = (
85- create_workspace_path_to_id_map (entries )
86- if include_workspace_path_map
87- else {}
88- )
89- if use_composer_cache :
90- if rules is None :
91- raise ValueError ("rules is required when use_composer_cache=True" )
92- composer_id_to_ws = build_composer_id_to_workspace_id_cached (
77+
78+
79+ def resolve_workspace_context_cached (
80+ workspace_path : str ,
81+ rules : list ,
82+ * ,
83+ workspace_entries : list [dict ] | None = None ,
84+ nocache : bool = False ,
85+ ) -> WorkspaceContext :
86+ """Full workspace maps with a mtime-keyed composer map (listing / tabs)."""
87+ entries = _entries (workspace_path , workspace_entries )
88+ return _assemble_context (
89+ entries ,
90+ invalid_workspace_ids = collect_invalid_workspace_ids (entries ),
91+ workspace_path_to_id = create_workspace_path_to_id_map (entries ),
92+ composer_id_to_workspace_id = build_composer_id_to_workspace_id_cached (
9393 workspace_path , entries , rules , nocache = nocache ,
94- )
95- else :
96- composer_id_to_ws = build_composer_id_to_workspace_id (workspace_path , entries )
97-
98- project_layouts : dict [str , list ] = {}
99- bubble_map : dict [str , dict ] = {}
100- if global_db is not None :
101- if populate_project_layouts :
102- project_layouts = load_project_layouts_map (global_db )
103- if populate_bubble_map :
104- bubble_map = load_bubble_map (global_db )
94+ ),
95+ )
10596
106- return WorkspaceContext (
107- workspace_path = workspace_path ,
108- workspace_entries = entries ,
109- invalid_workspace_ids = invalid_ids ,
110- composer_id_to_workspace_id = composer_id_to_ws ,
111- project_name_to_workspace_id = project_name_map ,
112- workspace_path_to_id = workspace_path_map ,
113- project_layouts_map = project_layouts ,
114- bubble_map = bubble_map ,
97+
98+ def resolve_workspace_context_minimal (workspace_path : str ) -> WorkspaceContext :
99+ """Entries, project-name, and composer maps only (HTTP export)."""
100+ entries = collect_workspace_entries (workspace_path )
101+ return _assemble_context (
102+ entries ,
103+ invalid_workspace_ids = set (),
104+ workspace_path_to_id = {},
105+ composer_id_to_workspace_id = build_composer_id_to_workspace_id (
106+ workspace_path , entries ,
107+ ),
115108 )
116109
117110
0 commit comments