-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace_context.py
More file actions
126 lines (108 loc) · 3.99 KB
/
Copy pathworkspace_context.py
File metadata and controls
126 lines (108 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Workspace determination ceremony — single orchestrator for shared maps."""
from __future__ import annotations
import sqlite3
from dataclasses import dataclass, replace
from services.workspace_db import (
build_composer_id_to_workspace_id,
build_composer_id_to_workspace_id_cached,
collect_invalid_workspace_ids,
collect_workspace_entries,
load_bubble_map,
load_project_layouts_map,
)
from services.workspace_resolver import (
create_project_name_to_workspace_id_map,
create_workspace_path_to_id_map,
)
@dataclass(frozen=True)
class WorkspaceContext:
"""Precomputed workspace-resolution maps for conversation assignment."""
workspace_entries: list[dict]
invalid_workspace_ids: set[str]
composer_id_to_workspace_id: dict[str, str]
project_name_to_workspace_id: dict[str, str]
workspace_path_to_id: dict[str, str]
project_layouts_map: dict[str, list]
bubble_map: dict[str, dict]
def _entries(
workspace_path: str,
workspace_entries: list[dict] | None,
) -> list[dict]:
if workspace_entries is not None:
return workspace_entries
return collect_workspace_entries(workspace_path)
def _assemble_context(
entries: list[dict],
*,
invalid_workspace_ids: set[str],
workspace_path_to_id: dict[str, str],
composer_id_to_workspace_id: dict[str, str],
) -> WorkspaceContext:
return WorkspaceContext(
workspace_entries=entries,
invalid_workspace_ids=invalid_workspace_ids,
composer_id_to_workspace_id=composer_id_to_workspace_id,
project_name_to_workspace_id=create_project_name_to_workspace_id_map(entries),
workspace_path_to_id=workspace_path_to_id,
project_layouts_map={},
bubble_map={},
)
def resolve_workspace_context(
workspace_path: str,
*,
workspace_entries: list[dict] | None = None,
) -> WorkspaceContext:
"""Full workspace maps with an uncached composer→workspace scan (CLI export)."""
entries = _entries(workspace_path, workspace_entries)
return _assemble_context(
entries,
invalid_workspace_ids=collect_invalid_workspace_ids(entries),
workspace_path_to_id=create_workspace_path_to_id_map(entries),
composer_id_to_workspace_id=build_composer_id_to_workspace_id(
workspace_path, entries,
),
)
def resolve_workspace_context_cached(
workspace_path: str,
rules: list,
*,
workspace_entries: list[dict] | None = None,
nocache: bool = False,
) -> WorkspaceContext:
"""Full workspace maps with a mtime-keyed composer map (listing / tabs)."""
entries = _entries(workspace_path, workspace_entries)
return _assemble_context(
entries,
invalid_workspace_ids=collect_invalid_workspace_ids(entries),
workspace_path_to_id=create_workspace_path_to_id_map(entries),
composer_id_to_workspace_id=build_composer_id_to_workspace_id_cached(
workspace_path, entries, rules, nocache=nocache,
),
)
def resolve_workspace_context_minimal(workspace_path: str) -> WorkspaceContext:
"""Entries, project-name, and composer maps only (HTTP export)."""
entries = collect_workspace_entries(workspace_path)
return _assemble_context(
entries,
invalid_workspace_ids=set(),
workspace_path_to_id={},
composer_id_to_workspace_id=build_composer_id_to_workspace_id(
workspace_path, entries,
),
)
def enrich_workspace_context_from_global_db(
ctx: WorkspaceContext,
global_db: sqlite3.Connection,
*,
populate_project_layouts: bool = False,
populate_bubble_map: bool = False,
) -> WorkspaceContext:
"""Return *ctx* with global KV maps loaded from an open global DB connection."""
updates: dict = {}
if populate_project_layouts:
updates["project_layouts_map"] = load_project_layouts_map(global_db)
if populate_bubble_map:
updates["bubble_map"] = load_bubble_map(global_db)
if not updates:
return ctx
return replace(ctx, **updates)