ai-editor-client-dev 1.0.244__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_editor_client/__init__.py +183 -0
- ai_editor_client/client.py +772 -0
- ai_editor_client/commands_proxy.py +60 -0
- ai_editor_client/config.py +126 -0
- ai_editor_client/editor_file.py +205 -0
- ai_editor_client/exceptions.py +48 -0
- ai_editor_client/file_session.py +692 -0
- ai_editor_client/local_edit_workspace.py +102 -0
- ai_editor_client/py.typed +0 -0
- ai_editor_client/responses.py +45 -0
- ai_editor_client/server_api.py +222 -0
- ai_editor_client/server_schema.py +57 -0
- ai_editor_client/server_version.py +171 -0
- ai_editor_client/universal_file.py +189 -0
- ai_editor_client/validation.py +98 -0
- ai_editor_client/version.txt +1 -0
- ai_editor_client_dev-1.0.244.dist-info/METADATA +236 -0
- ai_editor_client_dev-1.0.244.dist-info/RECORD +20 -0
- ai_editor_client_dev-1.0.244.dist-info/WHEEL +5 -0
- ai_editor_client_dev-1.0.244.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
On-disk edit workspace: baseline file outside session dir, working copy inside.
|
|
3
|
+
|
|
4
|
+
Mirrors the server ``EditSession`` layout without CST/sidecar dependencies.
|
|
5
|
+
Used by :class:`EditorFileClient` for the five-step editor workflow.
|
|
6
|
+
|
|
7
|
+
Author: Vasiliy Zdanovskiy
|
|
8
|
+
email: vasilyvz@gmail.com
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import shutil
|
|
14
|
+
import uuid
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Optional
|
|
18
|
+
|
|
19
|
+
from ai_editor_client.exceptions import ClientValidationError
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class EditorFileWorkflowError(ClientValidationError):
|
|
23
|
+
"""Invalid state or I/O failure in the editor file workflow."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class LocalEditWorkspace:
|
|
28
|
+
"""Local session directory with a working copy of a baseline file."""
|
|
29
|
+
|
|
30
|
+
workspace_id: str
|
|
31
|
+
baseline_path: Path
|
|
32
|
+
session_dir: Path
|
|
33
|
+
working_path: Path
|
|
34
|
+
is_open: bool = True
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def open(
|
|
38
|
+
cls,
|
|
39
|
+
baseline_path: Path,
|
|
40
|
+
*,
|
|
41
|
+
workspace_id: Optional[str] = None,
|
|
42
|
+
) -> LocalEditWorkspace:
|
|
43
|
+
"""Create ``{baseline.name}-{uuid}/`` and copy baseline into it."""
|
|
44
|
+
baseline = baseline_path.resolve()
|
|
45
|
+
if not baseline.is_file():
|
|
46
|
+
raise EditorFileWorkflowError(
|
|
47
|
+
"baseline file does not exist",
|
|
48
|
+
field="baseline_path",
|
|
49
|
+
details={"baseline_path": str(baseline)},
|
|
50
|
+
)
|
|
51
|
+
wid = (workspace_id or str(uuid.uuid4())).strip()
|
|
52
|
+
if not wid:
|
|
53
|
+
raise EditorFileWorkflowError(
|
|
54
|
+
"workspace_id is empty",
|
|
55
|
+
field="workspace_id",
|
|
56
|
+
)
|
|
57
|
+
session_dir = baseline.parent / f"{baseline.name}-{wid}"
|
|
58
|
+
if session_dir.exists():
|
|
59
|
+
raise EditorFileWorkflowError(
|
|
60
|
+
"session directory already exists",
|
|
61
|
+
field="session_dir",
|
|
62
|
+
details={"session_dir": str(session_dir)},
|
|
63
|
+
)
|
|
64
|
+
session_dir.mkdir(exist_ok=False)
|
|
65
|
+
working_path = session_dir / baseline.name
|
|
66
|
+
shutil.copy2(baseline, working_path)
|
|
67
|
+
return cls(
|
|
68
|
+
workspace_id=wid,
|
|
69
|
+
baseline_path=baseline,
|
|
70
|
+
session_dir=session_dir,
|
|
71
|
+
working_path=working_path,
|
|
72
|
+
is_open=True,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def sync_baseline(self) -> None:
|
|
76
|
+
"""Atomically overwrite the baseline file from the working copy."""
|
|
77
|
+
if not self.is_open:
|
|
78
|
+
raise EditorFileWorkflowError(
|
|
79
|
+
"workspace is not open",
|
|
80
|
+
field="workspace",
|
|
81
|
+
)
|
|
82
|
+
if not self.working_path.is_file():
|
|
83
|
+
raise EditorFileWorkflowError(
|
|
84
|
+
"working copy is missing",
|
|
85
|
+
field="working_path",
|
|
86
|
+
details={"working_path": str(self.working_path)},
|
|
87
|
+
)
|
|
88
|
+
tmp = self.baseline_path.with_suffix(self.baseline_path.suffix + ".tmp")
|
|
89
|
+
try:
|
|
90
|
+
shutil.copy2(self.working_path, tmp)
|
|
91
|
+
tmp.replace(self.baseline_path)
|
|
92
|
+
except Exception:
|
|
93
|
+
tmp.unlink(missing_ok=True)
|
|
94
|
+
raise
|
|
95
|
+
|
|
96
|
+
def close(self, *, delete_baseline: bool = True) -> None:
|
|
97
|
+
"""Remove session directory and optionally the baseline file."""
|
|
98
|
+
if self.session_dir.exists():
|
|
99
|
+
shutil.rmtree(self.session_dir)
|
|
100
|
+
self.is_open = False
|
|
101
|
+
if delete_baseline and self.baseline_path.is_file():
|
|
102
|
+
self.baseline_path.unlink()
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Parse JSON-RPC command envelopes from ai-editor-server."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict
|
|
6
|
+
|
|
7
|
+
from ai_editor_client.exceptions import ClientValidationError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def command_error_code(data: Dict[str, Any]) -> str:
|
|
11
|
+
"""Return domain or JSON-RPC error code from a failed command response."""
|
|
12
|
+
if data.get("success") is True:
|
|
13
|
+
return ""
|
|
14
|
+
top = data.get("code")
|
|
15
|
+
if top is not None and str(top).strip():
|
|
16
|
+
return str(top).strip()
|
|
17
|
+
err = data.get("error")
|
|
18
|
+
if isinstance(err, dict):
|
|
19
|
+
nested = err.get("code")
|
|
20
|
+
if nested is not None:
|
|
21
|
+
return str(nested).strip()
|
|
22
|
+
if isinstance(err, str) and err.strip():
|
|
23
|
+
return err.strip()
|
|
24
|
+
return ""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def unwrap_command_result(
|
|
28
|
+
data: Dict[str, Any],
|
|
29
|
+
*,
|
|
30
|
+
session_not_found_type: type[ClientValidationError] | None = None,
|
|
31
|
+
) -> Dict[str, Any]:
|
|
32
|
+
"""Return success ``data`` dict or raise :class:`ClientValidationError`."""
|
|
33
|
+
if data.get("success"):
|
|
34
|
+
inner = data.get("data")
|
|
35
|
+
return inner if isinstance(inner, dict) else data
|
|
36
|
+
code = command_error_code(data)
|
|
37
|
+
message = data.get("message")
|
|
38
|
+
err = data.get("error")
|
|
39
|
+
if message is None and isinstance(err, dict):
|
|
40
|
+
message = err.get("message")
|
|
41
|
+
if message is None:
|
|
42
|
+
message = str(data)
|
|
43
|
+
if code == "SESSION_NOT_FOUND" and session_not_found_type is not None:
|
|
44
|
+
raise session_not_found_type(message, field="session_id", details=data)
|
|
45
|
+
raise ClientValidationError(message, field="command", details=data)
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Canonical command names for the high-level client API vs the live server registry.
|
|
3
|
+
|
|
4
|
+
The generic client (:class:`CodeAnalysisAsyncClient.call`) can invoke any command
|
|
5
|
+
returned by server ``help``. The supported facade is :mod:`universal_file`; every
|
|
6
|
+
name in :data:`CLIENT_FACADE_COMMANDS` must be registered on the thin server.
|
|
7
|
+
:mod:`file_session` command mappings are deprecated legacy only and are not part
|
|
8
|
+
of the supported public surface.
|
|
9
|
+
|
|
10
|
+
Author: Vasiliy Zdanovskiy
|
|
11
|
+
email: vasilyvz@gmail.com
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from typing import Callable, Dict, FrozenSet, Tuple
|
|
17
|
+
|
|
18
|
+
# Removed from the public server surface — do not use in client facades or docs.
|
|
19
|
+
LEGACY_REMOVED_COMMANDS: FrozenSet[str] = frozenset(
|
|
20
|
+
{
|
|
21
|
+
"universal_file_read",
|
|
22
|
+
"universal_file_save",
|
|
23
|
+
"universal_file_replace",
|
|
24
|
+
"universal_file_delete",
|
|
25
|
+
"universal_file_move_nodes",
|
|
26
|
+
"session_write",
|
|
27
|
+
"read_project_text_file",
|
|
28
|
+
"write_project_text_lines",
|
|
29
|
+
"create_text_file",
|
|
30
|
+
"replace_file_lines",
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# CST tree MCP workflow — modules may exist in the repo but commands are not registered.
|
|
35
|
+
CST_REMOVED_COMMANDS: FrozenSet[str] = frozenset(
|
|
36
|
+
{
|
|
37
|
+
"cst_load_file",
|
|
38
|
+
"cst_save_tree",
|
|
39
|
+
"cst_modify_tree",
|
|
40
|
+
"cst_create_file",
|
|
41
|
+
"cst_find_node",
|
|
42
|
+
"cst_get_node_info",
|
|
43
|
+
"cst_get_node_by_range",
|
|
44
|
+
"cst_get_node_at_line",
|
|
45
|
+
"cst_reload_tree",
|
|
46
|
+
"cst_convert_and_save",
|
|
47
|
+
"list_cst_blocks",
|
|
48
|
+
"query_cst",
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
FILE_SESSION_COMMANDS: FrozenSet[str] = frozenset(
|
|
53
|
+
{
|
|
54
|
+
"session_create",
|
|
55
|
+
"session_delete",
|
|
56
|
+
"session_list",
|
|
57
|
+
"session_view",
|
|
58
|
+
"session_open_file",
|
|
59
|
+
"session_close_file",
|
|
60
|
+
"session_list_file_locks",
|
|
61
|
+
"subordinate_session_create",
|
|
62
|
+
"subordinate_session_get",
|
|
63
|
+
"subordinate_session_update",
|
|
64
|
+
"subordinate_session_delete",
|
|
65
|
+
"subordinate_session_list",
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
TRANSFER_AND_LOCK_COMMANDS: FrozenSet[str] = frozenset(
|
|
70
|
+
{
|
|
71
|
+
"project_file_transfer_download_begin",
|
|
72
|
+
"project_file_transfer_upload_save",
|
|
73
|
+
"project_file_advisory_lock_batch",
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
DEPRECATED_CLIENT_FACADE_COMMANDS: FrozenSet[str] = (
|
|
78
|
+
FILE_SESSION_COMMANDS | TRANSFER_AND_LOCK_COMMANDS
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
REMOVED_COMMANDS: FrozenSet[str] = (
|
|
82
|
+
LEGACY_REMOVED_COMMANDS | CST_REMOVED_COMMANDS | DEPRECATED_CLIENT_FACADE_COMMANDS
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
UNIVERSAL_FILE_COMMANDS: FrozenSet[str] = frozenset(
|
|
86
|
+
{
|
|
87
|
+
"universal_file_open",
|
|
88
|
+
"universal_file_edit",
|
|
89
|
+
"universal_file_write",
|
|
90
|
+
"universal_file_close",
|
|
91
|
+
"universal_file_preview",
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
INFRASTRUCTURE_COMMANDS: FrozenSet[str] = frozenset({"health"})
|
|
96
|
+
|
|
97
|
+
CLIENT_FACADE_COMMANDS: FrozenSet[str] = (
|
|
98
|
+
UNIVERSAL_FILE_COMMANDS | INFRASTRUCTURE_COMMANDS
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# ``FileSessionClient`` method names for each ``FILE_SESSION_COMMANDS`` entry.
|
|
102
|
+
# ``session_list_file_locks`` is also used by ``assert_session_exists``.
|
|
103
|
+
FILE_SESSION_FACADE_METHODS: Dict[str, str] = {
|
|
104
|
+
"session_create": "create_session",
|
|
105
|
+
"session_delete": "delete_session",
|
|
106
|
+
"session_list": "list_sessions",
|
|
107
|
+
"session_view": "view_session",
|
|
108
|
+
"session_open_file": "lock_file",
|
|
109
|
+
"session_close_file": "unlock_file",
|
|
110
|
+
"session_list_file_locks": "list_file_locks",
|
|
111
|
+
"subordinate_session_create": "create_subordinate_session",
|
|
112
|
+
"subordinate_session_get": "get_subordinate_session",
|
|
113
|
+
"subordinate_session_update": "update_subordinate_session",
|
|
114
|
+
"subordinate_session_delete": "delete_subordinate_session",
|
|
115
|
+
"subordinate_session_list": "list_subordinate_sessions",
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
# Transfer / advisory-lock commands may map to more than one façade method.
|
|
119
|
+
TRANSFER_FACADE_METHODS: Dict[str, Tuple[str, ...]] = {
|
|
120
|
+
"project_file_transfer_download_begin": ("download",),
|
|
121
|
+
"project_file_transfer_upload_save": ("upload", "upload_new"),
|
|
122
|
+
"project_file_advisory_lock_batch": (
|
|
123
|
+
"lock_files_advisory",
|
|
124
|
+
"unlock_files_advisory",
|
|
125
|
+
),
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
UNIVERSAL_FILE_FACADE_METHODS: Dict[str, str] = {
|
|
130
|
+
"universal_file_open": "open",
|
|
131
|
+
"universal_file_edit": "edit",
|
|
132
|
+
"universal_file_write": "write",
|
|
133
|
+
"universal_file_close": "close",
|
|
134
|
+
"universal_file_preview": "preview",
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def assert_universal_file_facade_complete() -> None:
|
|
139
|
+
"""Raise ``AssertionError`` if ``UNIVERSAL_FILE_FACADE_METHODS`` is incomplete."""
|
|
140
|
+
missing_cmds = UNIVERSAL_FILE_COMMANDS - set(UNIVERSAL_FILE_FACADE_METHODS)
|
|
141
|
+
extra_cmds = set(UNIVERSAL_FILE_FACADE_METHODS) - UNIVERSAL_FILE_COMMANDS
|
|
142
|
+
if missing_cmds or extra_cmds:
|
|
143
|
+
raise AssertionError(
|
|
144
|
+
"UNIVERSAL_FILE_FACADE_METHODS out of sync with UNIVERSAL_FILE_COMMANDS: "
|
|
145
|
+
f"missing={sorted(missing_cmds)!r} extra={sorted(extra_cmds)!r}"
|
|
146
|
+
)
|
|
147
|
+
from ai_editor_client.universal_file import UniversalFileClient
|
|
148
|
+
|
|
149
|
+
for command, method_name in UNIVERSAL_FILE_FACADE_METHODS.items():
|
|
150
|
+
if not hasattr(UniversalFileClient, method_name):
|
|
151
|
+
raise AssertionError(
|
|
152
|
+
f"UniversalFileClient missing method {method_name!r} "
|
|
153
|
+
f"for command {command!r}"
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def assert_file_session_facade_complete() -> None:
|
|
158
|
+
"""Raise ``AssertionError`` if ``FILE_SESSION_FACADE_METHODS`` is incomplete."""
|
|
159
|
+
missing_cmds = FILE_SESSION_COMMANDS - set(FILE_SESSION_FACADE_METHODS)
|
|
160
|
+
extra_cmds = set(FILE_SESSION_FACADE_METHODS) - FILE_SESSION_COMMANDS
|
|
161
|
+
if missing_cmds or extra_cmds:
|
|
162
|
+
raise AssertionError(
|
|
163
|
+
"FILE_SESSION_FACADE_METHODS out of sync with FILE_SESSION_COMMANDS: "
|
|
164
|
+
f"missing={sorted(missing_cmds)!r} extra={sorted(extra_cmds)!r}"
|
|
165
|
+
)
|
|
166
|
+
from ai_editor_client.file_session import FileSessionClient
|
|
167
|
+
|
|
168
|
+
for command, method_name in FILE_SESSION_FACADE_METHODS.items():
|
|
169
|
+
if not hasattr(FileSessionClient, method_name):
|
|
170
|
+
raise AssertionError(
|
|
171
|
+
f"FileSessionClient missing method {method_name!r} for command {command!r}"
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def assert_transfer_facade_complete() -> None:
|
|
176
|
+
"""Raise ``AssertionError`` if ``TRANSFER_FACADE_METHODS`` is incomplete."""
|
|
177
|
+
missing_cmds = TRANSFER_AND_LOCK_COMMANDS - set(TRANSFER_FACADE_METHODS)
|
|
178
|
+
extra_cmds = set(TRANSFER_FACADE_METHODS) - TRANSFER_AND_LOCK_COMMANDS
|
|
179
|
+
if missing_cmds or extra_cmds:
|
|
180
|
+
raise AssertionError(
|
|
181
|
+
"TRANSFER_FACADE_METHODS out of sync with TRANSFER_AND_LOCK_COMMANDS: "
|
|
182
|
+
f"missing={sorted(missing_cmds)!r} extra={sorted(extra_cmds)!r}"
|
|
183
|
+
)
|
|
184
|
+
from ai_editor_client.file_session import FileSessionClient
|
|
185
|
+
|
|
186
|
+
for command, method_names in TRANSFER_FACADE_METHODS.items():
|
|
187
|
+
for method_name in method_names:
|
|
188
|
+
if not hasattr(FileSessionClient, method_name):
|
|
189
|
+
raise AssertionError(
|
|
190
|
+
f"FileSessionClient missing method {method_name!r} "
|
|
191
|
+
f"for command {command!r}"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _command_registered(get_command: Callable[[str], object], name: str) -> bool:
|
|
196
|
+
try:
|
|
197
|
+
get_command(name)
|
|
198
|
+
return True
|
|
199
|
+
except KeyError:
|
|
200
|
+
return False
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def assert_facade_commands_registered(get_command: Callable[[str], object]) -> None:
|
|
204
|
+
"""Raise ``KeyError`` if any facade command is missing from the server registry."""
|
|
205
|
+
missing = [
|
|
206
|
+
name
|
|
207
|
+
for name in sorted(CLIENT_FACADE_COMMANDS)
|
|
208
|
+
if not _command_registered(get_command, name)
|
|
209
|
+
]
|
|
210
|
+
if missing:
|
|
211
|
+
raise KeyError(f"Client facade commands not registered on server: {missing}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def assert_removed_commands_absent(get_command: Callable[[str], object]) -> None:
|
|
215
|
+
"""Raise ``AssertionError`` if a removed command is still registered."""
|
|
216
|
+
present = [
|
|
217
|
+
name
|
|
218
|
+
for name in sorted(REMOVED_COMMANDS)
|
|
219
|
+
if _command_registered(get_command, name)
|
|
220
|
+
]
|
|
221
|
+
if present:
|
|
222
|
+
raise AssertionError(f"Removed commands still registered: {present}")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Load per-command JSON schema from the running server via ``help`` (cmdname).
|
|
3
|
+
|
|
4
|
+
Author: Vasiliy Zdanovskiy
|
|
5
|
+
email: vasilyvz@gmail.com
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Dict
|
|
11
|
+
|
|
12
|
+
from ai_editor_client.exceptions import ClientValidationError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def parse_schema_from_help_payload(
|
|
16
|
+
payload: Dict[str, Any], *, command_name: str
|
|
17
|
+
) -> Dict[str, Any]:
|
|
18
|
+
"""Extract ``get_schema()``-style dict from ``help`` JSON-RPC result."""
|
|
19
|
+
if not payload.get("success"):
|
|
20
|
+
raise ClientValidationError(
|
|
21
|
+
f"{command_name}: help returned success=false: {payload!r}",
|
|
22
|
+
field="help",
|
|
23
|
+
details={"payload": payload},
|
|
24
|
+
)
|
|
25
|
+
data = payload.get("data")
|
|
26
|
+
if not isinstance(data, dict):
|
|
27
|
+
raise ClientValidationError(
|
|
28
|
+
f"{command_name}: help response missing object 'data'",
|
|
29
|
+
field="help",
|
|
30
|
+
)
|
|
31
|
+
if data.get("error") and "schema" not in data:
|
|
32
|
+
raise ClientValidationError(
|
|
33
|
+
f"{command_name}: {data.get('error')}",
|
|
34
|
+
field="help",
|
|
35
|
+
details={"note": data.get("note"), "example": data.get("example")},
|
|
36
|
+
)
|
|
37
|
+
schema = data.get("schema")
|
|
38
|
+
if not isinstance(schema, dict):
|
|
39
|
+
raise ClientValidationError(
|
|
40
|
+
f"{command_name}: help response has no 'schema' object",
|
|
41
|
+
field="help",
|
|
42
|
+
)
|
|
43
|
+
return schema
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def fetch_command_schema_from_server(
|
|
47
|
+
rpc: Any,
|
|
48
|
+
command_name: str,
|
|
49
|
+
) -> Dict[str, Any]:
|
|
50
|
+
"""Call server ``help`` with ``cmdname`` and return that command's input schema."""
|
|
51
|
+
raw = await rpc.help(command_name)
|
|
52
|
+
if not isinstance(raw, dict):
|
|
53
|
+
raise ClientValidationError(
|
|
54
|
+
f"{command_name}: help returned non-dict: {type(raw).__name__}",
|
|
55
|
+
field="help",
|
|
56
|
+
)
|
|
57
|
+
return parse_schema_from_help_payload(raw, command_name=command_name)
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""THE VERSION DECLARATION of this build of the ai-editor client.
|
|
2
|
+
|
|
3
|
+
Author: Vasiliy Zdanovskiy
|
|
4
|
+
email: vasilyvz@gmail.com
|
|
5
|
+
|
|
6
|
+
The owner's ruling is that the server (``ai-editor``), the client
|
|
7
|
+
(``ai-editor-client``) and the engine (``ai-editor-tree-engine``) always carry
|
|
8
|
+
the SAME version, enforced by the CODE and not only by tests: "if the server
|
|
9
|
+
and client versions differ -- an error. There must be a declaration."
|
|
10
|
+
|
|
11
|
+
This module is that declaration for the client distribution, and it is the only
|
|
12
|
+
place in ``ai_editor_client`` that decides what version this build is or what it
|
|
13
|
+
requires of a server. ``ai_editor_client.__version__`` is re-exported from here.
|
|
14
|
+
|
|
15
|
+
Why the client cannot simply import the server's declaration
|
|
16
|
+
------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
``ai-editor-client`` is a separate distribution, installed on its own from PyPI
|
|
19
|
+
with no ``ai_editor`` package anywhere on the machine. So there are two
|
|
20
|
+
declaration modules -- this one and ``ai_editor/version.py`` -- but only ONE
|
|
21
|
+
number: both read the same repository-root ``VERSION`` file, each through a
|
|
22
|
+
symlink of its own (``client/ai_editor_client/version.txt`` here), because
|
|
23
|
+
setuptools refuses to read a version file outside a distribution's own root
|
|
24
|
+
(``setuptools.config.expand._assert_local``). sdist and wheel builds copy the
|
|
25
|
+
resolved content, so an installed wheel carries a real file here.
|
|
26
|
+
|
|
27
|
+
The exemption, and why it is not a loophole
|
|
28
|
+
-------------------------------------------
|
|
29
|
+
|
|
30
|
+
``health`` and ``info`` are exempt from the check and keep working against a
|
|
31
|
+
server of any version. That is operational, not a concession: they are exactly
|
|
32
|
+
the commands an operator reaches for to SEE that the versions differ. Refusing
|
|
33
|
+
them would take away the instrument needed to diagnose the very failure this
|
|
34
|
+
module reports. Nothing else is exempt, and ``health`` is also how the server's
|
|
35
|
+
version is learned in the first place.
|
|
36
|
+
|
|
37
|
+
Failure policy
|
|
38
|
+
--------------
|
|
39
|
+
|
|
40
|
+
The check fails CLOSED. A server that does not answer ``health``, or answers
|
|
41
|
+
without a usable ``version``, is a server whose version could not be
|
|
42
|
+
established -- that raises :class:`ServerVersionMismatch` just as a wrong
|
|
43
|
+
number does. There is no "assume it is fine" path.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
from __future__ import annotations
|
|
47
|
+
|
|
48
|
+
from pathlib import Path
|
|
49
|
+
from typing import Any, Mapping, Optional
|
|
50
|
+
|
|
51
|
+
from ai_editor_client.exceptions import ServerVersionMismatch
|
|
52
|
+
|
|
53
|
+
# The declaration file, shipped as package data (see `[tool.setuptools.
|
|
54
|
+
# package-data]` in client/pyproject.toml). In the repository it is a symlink to
|
|
55
|
+
# the root VERSION file; in an installed wheel it is that file's content.
|
|
56
|
+
VERSION_FILE: Path = Path(__file__).resolve().parent / "version.txt"
|
|
57
|
+
|
|
58
|
+
#: The command whose response carries the server's version.
|
|
59
|
+
SERVER_VERSION_PROBE_COMMAND: str = "health"
|
|
60
|
+
|
|
61
|
+
#: Commands that must keep working across a version mismatch, because they are
|
|
62
|
+
#: the ones used to diagnose it. Deliberately minimal.
|
|
63
|
+
VERSION_EXEMPT_COMMANDS: frozenset[str] = frozenset({"health", "info"})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class VersionDeclarationError(RuntimeError):
|
|
67
|
+
"""This build carries no readable version declaration.
|
|
68
|
+
|
|
69
|
+
Raised at import. There is no fallback number on purpose: a guessed client
|
|
70
|
+
version would silently "match" or silently mismatch every server.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _read_declared_version() -> str:
|
|
75
|
+
"""Return the one number this build declares, or refuse to load."""
|
|
76
|
+
try:
|
|
77
|
+
raw = VERSION_FILE.read_text(encoding="utf-8")
|
|
78
|
+
except OSError as exc:
|
|
79
|
+
raise VersionDeclarationError(
|
|
80
|
+
f"the ai-editor-client version declaration is unreadable at "
|
|
81
|
+
f"{VERSION_FILE}: {exc.__class__.__name__}: {exc}. This file is "
|
|
82
|
+
"package data copied from the repository-root VERSION file; a build "
|
|
83
|
+
"without it cannot state which version it is and must not run."
|
|
84
|
+
) from exc
|
|
85
|
+
declared = raw.strip()
|
|
86
|
+
if not declared:
|
|
87
|
+
raise VersionDeclarationError(
|
|
88
|
+
f"the ai-editor-client version declaration at {VERSION_FILE} is "
|
|
89
|
+
"empty; it must hold exactly one release number, e.g. '1.0.93'."
|
|
90
|
+
)
|
|
91
|
+
return declared
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
#: The version THIS build of the client is.
|
|
95
|
+
DECLARED_VERSION: str = _read_declared_version()
|
|
96
|
+
|
|
97
|
+
#: The version of the ai-editor server this build talks to, exactly. Equality
|
|
98
|
+
#: is exact, never a compatible range.
|
|
99
|
+
REQUIRED_SERVER_VERSION: str = DECLARED_VERSION
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def is_version_exempt(command: str) -> bool:
|
|
103
|
+
"""True for the commands that must survive a version mismatch."""
|
|
104
|
+
return command in VERSION_EXEMPT_COMMANDS
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def server_version_from_health(envelope: Any) -> Optional[str]:
|
|
108
|
+
"""Pull the server version out of a ``health`` response envelope.
|
|
109
|
+
|
|
110
|
+
``None`` means the envelope did not carry a usable version -- an error
|
|
111
|
+
result, an unexpected shape, or a non-string value. The caller treats that
|
|
112
|
+
as an unverified server, not as a pass.
|
|
113
|
+
"""
|
|
114
|
+
if not isinstance(envelope, Mapping):
|
|
115
|
+
return None
|
|
116
|
+
data = envelope.get("data")
|
|
117
|
+
if not isinstance(data, Mapping):
|
|
118
|
+
return None
|
|
119
|
+
version = data.get("version")
|
|
120
|
+
if isinstance(version, str) and version.strip():
|
|
121
|
+
return version.strip()
|
|
122
|
+
return None
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def assert_server_version_matches(
|
|
126
|
+
server_version: Optional[str],
|
|
127
|
+
*,
|
|
128
|
+
command: Optional[str] = None,
|
|
129
|
+
) -> None:
|
|
130
|
+
"""Raise :class:`ServerVersionMismatch` unless the server is the declared one."""
|
|
131
|
+
if server_version == REQUIRED_SERVER_VERSION:
|
|
132
|
+
return
|
|
133
|
+
where = f" while dispatching {command!r}" if command else ""
|
|
134
|
+
if server_version is None:
|
|
135
|
+
detail = (
|
|
136
|
+
"the server did not report a usable version"
|
|
137
|
+
f" -- {SERVER_VERSION_PROBE_COMMAND!r} returned no string "
|
|
138
|
+
"'version' field, so which build is answering could not be "
|
|
139
|
+
"established. A server whose version is unknown is not a server "
|
|
140
|
+
"this client may send work to."
|
|
141
|
+
)
|
|
142
|
+
else:
|
|
143
|
+
detail = (
|
|
144
|
+
f"the server is version {server_version}. The versions must be "
|
|
145
|
+
"EQUAL, not merely compatible. Install ai-editor-client=="
|
|
146
|
+
f"{server_version} to talk to this server, or deploy an ai-editor "
|
|
147
|
+
f"server of version {REQUIRED_SERVER_VERSION}."
|
|
148
|
+
)
|
|
149
|
+
raise ServerVersionMismatch(
|
|
150
|
+
f"ai-editor-client {REQUIRED_SERVER_VERSION} refuses to continue"
|
|
151
|
+
f"{where}: {detail} The requirement is declared in {VERSION_FILE} and "
|
|
152
|
+
"read by ai_editor_client/server_version.py as REQUIRED_SERVER_VERSION. "
|
|
153
|
+
f"The commands {sorted(VERSION_EXEMPT_COMMANDS)} stay available across a "
|
|
154
|
+
"mismatch so the problem can be inspected.",
|
|
155
|
+
client_version=REQUIRED_SERVER_VERSION,
|
|
156
|
+
server_version=server_version,
|
|
157
|
+
command=command,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
__all__ = [
|
|
162
|
+
"DECLARED_VERSION",
|
|
163
|
+
"REQUIRED_SERVER_VERSION",
|
|
164
|
+
"SERVER_VERSION_PROBE_COMMAND",
|
|
165
|
+
"VERSION_EXEMPT_COMMANDS",
|
|
166
|
+
"VERSION_FILE",
|
|
167
|
+
"VersionDeclarationError",
|
|
168
|
+
"assert_server_version_matches",
|
|
169
|
+
"is_version_exempt",
|
|
170
|
+
"server_version_from_health",
|
|
171
|
+
]
|