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,692 @@
|
|
|
1
|
+
"""
|
|
2
|
+
High-level file transfer and session workflow on top of CodeAnalysisAsyncClient.
|
|
3
|
+
|
|
4
|
+
Wraps ``session_*`` and ``subordinate_session_*`` MCP commands plus transfer and
|
|
5
|
+
advisory-lock commands that accept ``session_id``.
|
|
6
|
+
|
|
7
|
+
Transfer mapping (client façade → server command):
|
|
8
|
+
|
|
9
|
+
* **Download** — ``download`` → ``project_file_transfer_download_begin`` (``file_id``
|
|
10
|
+
required; optional ``project_id`` scopes the lookup). Chunk streaming uses the
|
|
11
|
+
adapter ``download_file`` helper via ``download_to_path``.
|
|
12
|
+
* **Upload** — two façade methods share one save command
|
|
13
|
+
``project_file_transfer_upload_save``:
|
|
14
|
+
|
|
15
|
+
- ``upload`` — update mode: ``file_id`` only (overwrite an indexed file).
|
|
16
|
+
- ``upload_new`` — create mode: ``project_id`` + ``file_path`` (path must not
|
|
17
|
+
be indexed yet).
|
|
18
|
+
|
|
19
|
+
Both methods upload bytes through the adapter, then call the same save command
|
|
20
|
+
with the completed ``transfer_id``.
|
|
21
|
+
|
|
22
|
+
Author: Vasiliy Zdanovskiy
|
|
23
|
+
email: vasilyvz@gmail.com
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
|
|
30
|
+
|
|
31
|
+
from ai_editor_client.exceptions import ClientValidationError
|
|
32
|
+
from ai_editor_client.responses import unwrap_command_result
|
|
33
|
+
|
|
34
|
+
if TYPE_CHECKING:
|
|
35
|
+
from ai_editor_client.client import CodeAnalysisAsyncClient
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class SessionNotFoundError(ClientValidationError):
|
|
39
|
+
"""Raised when session_id is absent from client_sessions."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _unwrap(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
43
|
+
unwrapped: Dict[str, Any] = unwrap_command_result(
|
|
44
|
+
data,
|
|
45
|
+
session_not_found_type=SessionNotFoundError,
|
|
46
|
+
)
|
|
47
|
+
return unwrapped
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _require_non_empty(value: str, *, field: str) -> str:
|
|
51
|
+
text = str(value or "").strip()
|
|
52
|
+
if not text:
|
|
53
|
+
raise ClientValidationError(f"{field} is required", field=field)
|
|
54
|
+
return text
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _validate_download_target(*, file_id: Optional[str]) -> None:
|
|
58
|
+
"""Download requires ``file_id`` (server rejects ``file_path``)."""
|
|
59
|
+
fid = str(file_id or "").strip()
|
|
60
|
+
if not fid:
|
|
61
|
+
raise ClientValidationError("file_id is required", field="file_id")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _validate_upload_selector(
|
|
65
|
+
*,
|
|
66
|
+
file_id: Optional[str] = None,
|
|
67
|
+
file_path: Optional[str] = None,
|
|
68
|
+
project_id: Optional[str] = None,
|
|
69
|
+
) -> None:
|
|
70
|
+
"""Mirror server ``_validate_upload_selector_params`` for save payloads."""
|
|
71
|
+
fid = str(file_id or "").strip()
|
|
72
|
+
fp = str(file_path or "").strip()
|
|
73
|
+
pid = str(project_id or "").strip()
|
|
74
|
+
if fid and fp:
|
|
75
|
+
raise ClientValidationError(
|
|
76
|
+
"Specify exactly one of file_id or file_path, not both",
|
|
77
|
+
field="file_id",
|
|
78
|
+
)
|
|
79
|
+
if fid:
|
|
80
|
+
return
|
|
81
|
+
if not pid:
|
|
82
|
+
raise ClientValidationError(
|
|
83
|
+
"project_id is required when file_id is omitted",
|
|
84
|
+
field="project_id",
|
|
85
|
+
)
|
|
86
|
+
if not fp:
|
|
87
|
+
raise ClientValidationError(
|
|
88
|
+
"file_path is required when file_id is omitted",
|
|
89
|
+
field="file_path",
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _lock_mode_from_flag(lock: bool) -> str:
|
|
94
|
+
return "full" if lock else "none"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _extract_file_id(payload: Dict[str, Any], *, field: str = "file_id") -> str:
|
|
98
|
+
"""Return non-empty ``file_id`` from a command payload or raise."""
|
|
99
|
+
fid = str(payload.get(field) or "").strip()
|
|
100
|
+
if not fid:
|
|
101
|
+
raise ClientValidationError(
|
|
102
|
+
f"{field} missing from server response",
|
|
103
|
+
field=field,
|
|
104
|
+
details=payload,
|
|
105
|
+
)
|
|
106
|
+
return fid
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class FileSessionClient:
|
|
110
|
+
"""Session-scoped DB sessions, subordinate links, file locks, and transfer helpers."""
|
|
111
|
+
|
|
112
|
+
__slots__ = ("_client",)
|
|
113
|
+
|
|
114
|
+
def __init__(self, client: CodeAnalysisAsyncClient) -> None:
|
|
115
|
+
self._client = client
|
|
116
|
+
|
|
117
|
+
async def create_session(
|
|
118
|
+
self,
|
|
119
|
+
comment: str,
|
|
120
|
+
*,
|
|
121
|
+
role_ids: Optional[List[str]] = None,
|
|
122
|
+
) -> str:
|
|
123
|
+
"""Create a client session (``session_create``) and return ``session_id``."""
|
|
124
|
+
params: Dict[str, Any] = {"comment": comment}
|
|
125
|
+
if role_ids is not None:
|
|
126
|
+
params["role_ids"] = role_ids
|
|
127
|
+
payload = _unwrap(await self._client.call_validated("session_create", params))
|
|
128
|
+
sid = str(payload.get("session_id") or "").strip()
|
|
129
|
+
if not sid:
|
|
130
|
+
raise ClientValidationError(
|
|
131
|
+
"session_create returned no session_id",
|
|
132
|
+
field="session_id",
|
|
133
|
+
details=payload,
|
|
134
|
+
)
|
|
135
|
+
return sid
|
|
136
|
+
|
|
137
|
+
async def assert_session_exists(self, session_id: str) -> None:
|
|
138
|
+
"""Verify ``session_id`` is registered (touch via ``session_list_file_locks``)."""
|
|
139
|
+
sid = str(session_id).strip()
|
|
140
|
+
if not sid:
|
|
141
|
+
raise ClientValidationError("session_id is required", field="session_id")
|
|
142
|
+
_unwrap(
|
|
143
|
+
await self._client.call_validated(
|
|
144
|
+
"session_list_file_locks",
|
|
145
|
+
{"session_id": sid},
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
async def delete_session(
|
|
150
|
+
self, session_id: str, *, force: bool = False
|
|
151
|
+
) -> Dict[str, Any]:
|
|
152
|
+
"""Delete a client session (``session_delete``).
|
|
153
|
+
|
|
154
|
+
``force`` defaults to false (omit on the wire when false). Safe delete
|
|
155
|
+
requires no ``session_file_locks`` and no ``subordinate_sessions`` rows
|
|
156
|
+
where this session is ``parent_session_id``. When ``force`` is true,
|
|
157
|
+
subordinate server link rows and file locks for this session are removed
|
|
158
|
+
before the session row is deleted.
|
|
159
|
+
|
|
160
|
+
Returns ``session_id``, ``deleted``, ``released_lock_count``, and
|
|
161
|
+
``released_subordinate_count``.
|
|
162
|
+
"""
|
|
163
|
+
params: Dict[str, Any] = {"session_id": session_id}
|
|
164
|
+
if force:
|
|
165
|
+
params["force"] = True
|
|
166
|
+
return _unwrap(
|
|
167
|
+
await self._client.call_validated(
|
|
168
|
+
"session_delete",
|
|
169
|
+
params,
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
async def view_session(self, session_id: str) -> Dict[str, Any]:
|
|
174
|
+
"""Aggregated session view (``session_view``).
|
|
175
|
+
|
|
176
|
+
Returns ``locked_files_by_project`` (file_id, file_path,
|
|
177
|
+
project_presentation per project) and ``subordinate_sessions``
|
|
178
|
+
(server_uuid, session_presentation, server_presentation, link_comment).
|
|
179
|
+
"""
|
|
180
|
+
sid = _require_non_empty(session_id, field="session_id")
|
|
181
|
+
return _unwrap(
|
|
182
|
+
await self._client.call_validated(
|
|
183
|
+
"session_view",
|
|
184
|
+
{"session_id": sid},
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
async def create_subordinate_session(
|
|
189
|
+
self,
|
|
190
|
+
parent_session_id: str,
|
|
191
|
+
comment: str,
|
|
192
|
+
*,
|
|
193
|
+
server_uuid: Optional[str] = None,
|
|
194
|
+
) -> Dict[str, Any]:
|
|
195
|
+
"""Register leading session on a subordinate server (``subordinate_session_create``).
|
|
196
|
+
|
|
197
|
+
The same ``parent_session_id`` is used on the remote server. ``server_uuid``
|
|
198
|
+
defaults to the server's ``registration.instance_uuid`` when omitted.
|
|
199
|
+
"""
|
|
200
|
+
params: Dict[str, Any] = {
|
|
201
|
+
"parent_session_id": _require_non_empty(
|
|
202
|
+
parent_session_id, field="parent_session_id"
|
|
203
|
+
),
|
|
204
|
+
"comment": comment,
|
|
205
|
+
}
|
|
206
|
+
if server_uuid is not None:
|
|
207
|
+
params["server_uuid"] = _require_non_empty(server_uuid, field="server_uuid")
|
|
208
|
+
return _unwrap(
|
|
209
|
+
await self._client.call_validated("subordinate_session_create", params)
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
async def get_subordinate_session(
|
|
213
|
+
self,
|
|
214
|
+
parent_session_id: str,
|
|
215
|
+
server_uuid: str,
|
|
216
|
+
) -> Dict[str, Any]:
|
|
217
|
+
"""Fetch one subordinate link (``subordinate_session_get``)."""
|
|
218
|
+
return _unwrap(
|
|
219
|
+
await self._client.call_validated(
|
|
220
|
+
"subordinate_session_get",
|
|
221
|
+
{
|
|
222
|
+
"parent_session_id": _require_non_empty(
|
|
223
|
+
parent_session_id, field="parent_session_id"
|
|
224
|
+
),
|
|
225
|
+
"server_uuid": _require_non_empty(server_uuid, field="server_uuid"),
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
async def update_subordinate_session(
|
|
231
|
+
self,
|
|
232
|
+
parent_session_id: str,
|
|
233
|
+
server_uuid: str,
|
|
234
|
+
comment: str,
|
|
235
|
+
) -> Dict[str, Any]:
|
|
236
|
+
"""Update link comment (``subordinate_session_update``)."""
|
|
237
|
+
return _unwrap(
|
|
238
|
+
await self._client.call_validated(
|
|
239
|
+
"subordinate_session_update",
|
|
240
|
+
{
|
|
241
|
+
"parent_session_id": _require_non_empty(
|
|
242
|
+
parent_session_id, field="parent_session_id"
|
|
243
|
+
),
|
|
244
|
+
"server_uuid": _require_non_empty(server_uuid, field="server_uuid"),
|
|
245
|
+
"comment": comment,
|
|
246
|
+
},
|
|
247
|
+
)
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
async def delete_subordinate_session(
|
|
251
|
+
self,
|
|
252
|
+
parent_session_id: str,
|
|
253
|
+
server_uuid: str,
|
|
254
|
+
) -> Dict[str, Any]:
|
|
255
|
+
"""Remove subordinate link only (``subordinate_session_delete``).
|
|
256
|
+
|
|
257
|
+
Does not delete the ``client_sessions`` row for ``parent_session_id``.
|
|
258
|
+
"""
|
|
259
|
+
return _unwrap(
|
|
260
|
+
await self._client.call_validated(
|
|
261
|
+
"subordinate_session_delete",
|
|
262
|
+
{
|
|
263
|
+
"parent_session_id": _require_non_empty(
|
|
264
|
+
parent_session_id, field="parent_session_id"
|
|
265
|
+
),
|
|
266
|
+
"server_uuid": _require_non_empty(server_uuid, field="server_uuid"),
|
|
267
|
+
},
|
|
268
|
+
)
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
async def list_subordinate_sessions(
|
|
272
|
+
self,
|
|
273
|
+
*,
|
|
274
|
+
parent_session_id: Optional[str] = None,
|
|
275
|
+
server_uuid: Optional[str] = None,
|
|
276
|
+
) -> Dict[str, Any]:
|
|
277
|
+
"""List subordinate links (``subordinate_session_list``).
|
|
278
|
+
|
|
279
|
+
Returns ``links`` and ``count``. All filter parameters are optional.
|
|
280
|
+
"""
|
|
281
|
+
params: Dict[str, Any] = {}
|
|
282
|
+
if parent_session_id is not None:
|
|
283
|
+
params["parent_session_id"] = parent_session_id
|
|
284
|
+
if server_uuid is not None:
|
|
285
|
+
params["server_uuid"] = server_uuid
|
|
286
|
+
return _unwrap(
|
|
287
|
+
await self._client.call_validated("subordinate_session_list", params)
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
async def list_sessions(
|
|
291
|
+
self,
|
|
292
|
+
*,
|
|
293
|
+
session_id: Optional[str] = None,
|
|
294
|
+
stale_threshold_seconds: Optional[int] = None,
|
|
295
|
+
) -> Dict[str, Any]:
|
|
296
|
+
"""List client sessions (``session_list``)."""
|
|
297
|
+
params: Dict[str, Any] = {}
|
|
298
|
+
if session_id is not None:
|
|
299
|
+
params["session_id"] = session_id
|
|
300
|
+
if stale_threshold_seconds is not None:
|
|
301
|
+
params["stale_threshold_seconds"] = stale_threshold_seconds
|
|
302
|
+
return _unwrap(await self._client.call_validated("session_list", params))
|
|
303
|
+
|
|
304
|
+
async def lock_file(
|
|
305
|
+
self,
|
|
306
|
+
session_id: str,
|
|
307
|
+
project_id: str,
|
|
308
|
+
file_id: str,
|
|
309
|
+
) -> Dict[str, Any]:
|
|
310
|
+
"""Acquire a DB file lock without transfer (``session_open_file``)."""
|
|
311
|
+
await self.assert_session_exists(session_id)
|
|
312
|
+
return _unwrap(
|
|
313
|
+
await self._client.call_validated(
|
|
314
|
+
"session_open_file",
|
|
315
|
+
{
|
|
316
|
+
"session_id": session_id,
|
|
317
|
+
"project_id": project_id,
|
|
318
|
+
"file_id": file_id,
|
|
319
|
+
},
|
|
320
|
+
)
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
async def unlock_file(
|
|
324
|
+
self,
|
|
325
|
+
session_id: str,
|
|
326
|
+
project_id: str,
|
|
327
|
+
file_id: str,
|
|
328
|
+
) -> Dict[str, Any]:
|
|
329
|
+
"""Release a DB file lock without transfer (``session_close_file``)."""
|
|
330
|
+
await self.assert_session_exists(session_id)
|
|
331
|
+
return _unwrap(
|
|
332
|
+
await self._client.call_validated(
|
|
333
|
+
"session_close_file",
|
|
334
|
+
{
|
|
335
|
+
"session_id": session_id,
|
|
336
|
+
"project_id": project_id,
|
|
337
|
+
"file_id": file_id,
|
|
338
|
+
},
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
async def list_file_locks(self, session_id: str) -> Dict[str, Any]:
|
|
343
|
+
"""Return locks held by the session (``session_list_file_locks``)."""
|
|
344
|
+
return _unwrap(
|
|
345
|
+
await self._client.call_validated(
|
|
346
|
+
"session_list_file_locks",
|
|
347
|
+
{"session_id": session_id},
|
|
348
|
+
)
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
async def lock_files_advisory(
|
|
352
|
+
self,
|
|
353
|
+
session_id: str,
|
|
354
|
+
project_id: str,
|
|
355
|
+
file_paths: List[str],
|
|
356
|
+
*,
|
|
357
|
+
lock_mode: str = "full",
|
|
358
|
+
timeout_seconds: Optional[float] = None,
|
|
359
|
+
) -> Dict[str, Any]:
|
|
360
|
+
"""Cooperative flock + DB lease for paths (``project_file_advisory_lock_batch``)."""
|
|
361
|
+
await self.assert_session_exists(session_id)
|
|
362
|
+
items = [
|
|
363
|
+
{
|
|
364
|
+
"session_id": session_id,
|
|
365
|
+
"project_id": project_id,
|
|
366
|
+
"file_path": path,
|
|
367
|
+
"action": "lock",
|
|
368
|
+
"lock_mode": lock_mode,
|
|
369
|
+
}
|
|
370
|
+
for path in file_paths
|
|
371
|
+
]
|
|
372
|
+
params: Dict[str, Any] = {
|
|
373
|
+
"items": items,
|
|
374
|
+
"allow_foreign_session": True,
|
|
375
|
+
}
|
|
376
|
+
if timeout_seconds is not None:
|
|
377
|
+
params["timeout_seconds"] = timeout_seconds
|
|
378
|
+
return _unwrap(
|
|
379
|
+
await self._client.call_validated(
|
|
380
|
+
"project_file_advisory_lock_batch",
|
|
381
|
+
params,
|
|
382
|
+
)
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
async def unlock_files_advisory(
|
|
386
|
+
self,
|
|
387
|
+
session_id: str,
|
|
388
|
+
project_id: str,
|
|
389
|
+
file_paths: List[str],
|
|
390
|
+
) -> Dict[str, Any]:
|
|
391
|
+
"""Release cooperative locks without transfer."""
|
|
392
|
+
await self.assert_session_exists(session_id)
|
|
393
|
+
items = [
|
|
394
|
+
{
|
|
395
|
+
"session_id": session_id,
|
|
396
|
+
"project_id": project_id,
|
|
397
|
+
"file_path": path,
|
|
398
|
+
"action": "unlock",
|
|
399
|
+
}
|
|
400
|
+
for path in file_paths
|
|
401
|
+
]
|
|
402
|
+
return _unwrap(
|
|
403
|
+
await self._client.call_validated(
|
|
404
|
+
"project_file_advisory_lock_batch",
|
|
405
|
+
{
|
|
406
|
+
"items": items,
|
|
407
|
+
"allow_foreign_session": True,
|
|
408
|
+
},
|
|
409
|
+
)
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
async def _begin_download(
|
|
413
|
+
self,
|
|
414
|
+
session_id: str,
|
|
415
|
+
*,
|
|
416
|
+
file_id: str,
|
|
417
|
+
compression: str = "identity",
|
|
418
|
+
lock: bool = True,
|
|
419
|
+
chunk_size: Optional[int] = None,
|
|
420
|
+
include_backup_history: bool = True,
|
|
421
|
+
project_id: Optional[str] = None,
|
|
422
|
+
) -> Dict[str, Any]:
|
|
423
|
+
"""Internal: call ``project_file_transfer_download_begin``."""
|
|
424
|
+
fid = _require_non_empty(file_id, field="file_id")
|
|
425
|
+
_validate_download_target(file_id=fid)
|
|
426
|
+
await self.assert_session_exists(session_id)
|
|
427
|
+
params: Dict[str, Any] = {
|
|
428
|
+
"session_id": session_id,
|
|
429
|
+
"compression": compression,
|
|
430
|
+
"lock_mode": _lock_mode_from_flag(lock),
|
|
431
|
+
"include_backup_history": include_backup_history,
|
|
432
|
+
"file_id": fid,
|
|
433
|
+
}
|
|
434
|
+
if chunk_size is not None:
|
|
435
|
+
params["chunk_size"] = chunk_size
|
|
436
|
+
if project_id is not None:
|
|
437
|
+
params["project_id"] = _require_non_empty(project_id, field="project_id")
|
|
438
|
+
payload = _unwrap(
|
|
439
|
+
await self._client.call_validated(
|
|
440
|
+
"project_file_transfer_download_begin",
|
|
441
|
+
params,
|
|
442
|
+
)
|
|
443
|
+
)
|
|
444
|
+
_extract_file_id(payload)
|
|
445
|
+
return payload
|
|
446
|
+
|
|
447
|
+
async def download(
|
|
448
|
+
self,
|
|
449
|
+
session_id: str,
|
|
450
|
+
destination: Union[str, Path],
|
|
451
|
+
file_id: str,
|
|
452
|
+
*,
|
|
453
|
+
compression: str = "identity",
|
|
454
|
+
lock: bool = True,
|
|
455
|
+
include_backup_history: bool = True,
|
|
456
|
+
project_id: Optional[str] = None,
|
|
457
|
+
) -> Tuple[Dict[str, Any], Any]:
|
|
458
|
+
"""Download an indexed file by ``file_id`` (``project_file_transfer_download_begin``).
|
|
459
|
+
|
|
460
|
+
Returns ``(begin_payload, adapter_receipt)``. ``file_path`` is not accepted —
|
|
461
|
+
resolve ``file_id`` from ``list_project_files`` or ``session_view`` first.
|
|
462
|
+
"""
|
|
463
|
+
fid = _require_non_empty(file_id, field="file_id")
|
|
464
|
+
begin = await self._begin_download(
|
|
465
|
+
session_id,
|
|
466
|
+
file_id=fid,
|
|
467
|
+
compression=compression,
|
|
468
|
+
lock=lock,
|
|
469
|
+
include_backup_history=include_backup_history,
|
|
470
|
+
project_id=project_id,
|
|
471
|
+
)
|
|
472
|
+
transfer_id = str(begin.get("transfer_id") or "").strip()
|
|
473
|
+
if not transfer_id:
|
|
474
|
+
raise ClientValidationError(
|
|
475
|
+
"download begin returned no transfer_id",
|
|
476
|
+
field="transfer_id",
|
|
477
|
+
details=begin,
|
|
478
|
+
)
|
|
479
|
+
receipt = await self.download_to_path(transfer_id, destination)
|
|
480
|
+
return begin, receipt
|
|
481
|
+
|
|
482
|
+
async def download_to_path(
|
|
483
|
+
self,
|
|
484
|
+
transfer_id: str,
|
|
485
|
+
destination: Union[str, Path],
|
|
486
|
+
) -> Any:
|
|
487
|
+
"""Stream download chunks to ``destination`` via the adapter client."""
|
|
488
|
+
# This is the one workflow path that reaches the adapter transport
|
|
489
|
+
# directly instead of going through CodeAnalysisAsyncClient.call, so the
|
|
490
|
+
# per-call server-version guard has to be invoked explicitly here; it is
|
|
491
|
+
# moving real file bytes and must not run against a foreign server.
|
|
492
|
+
await self._client.ensure_server_version("download_to_path")
|
|
493
|
+
return await self._client.rpc.download_file(
|
|
494
|
+
str(transfer_id).strip(),
|
|
495
|
+
str(destination),
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
async def upload_bytes(
|
|
499
|
+
self,
|
|
500
|
+
payload: bytes,
|
|
501
|
+
*,
|
|
502
|
+
filename: str,
|
|
503
|
+
compression: str = "identity",
|
|
504
|
+
) -> Any:
|
|
505
|
+
"""Upload raw bytes through the adapter buffer; returns upload receipt."""
|
|
506
|
+
# Same reason as download_to_path: this reaches the adapter transport
|
|
507
|
+
# directly, so the version guard is invoked explicitly. Checked BEFORE
|
|
508
|
+
# the temporary file is written, so a refused call leaves no residue.
|
|
509
|
+
await self._client.ensure_server_version("upload_bytes")
|
|
510
|
+
dest = Path(filename)
|
|
511
|
+
dest.write_bytes(payload)
|
|
512
|
+
try:
|
|
513
|
+
return await self._client.rpc.upload_file(
|
|
514
|
+
str(dest),
|
|
515
|
+
filename=filename,
|
|
516
|
+
compression=compression,
|
|
517
|
+
)
|
|
518
|
+
finally:
|
|
519
|
+
if dest.is_file():
|
|
520
|
+
dest.unlink(missing_ok=True)
|
|
521
|
+
|
|
522
|
+
async def _commit_upload(
|
|
523
|
+
self,
|
|
524
|
+
session_id: str,
|
|
525
|
+
transfer_id: str,
|
|
526
|
+
*,
|
|
527
|
+
project_id: Optional[str] = None,
|
|
528
|
+
file_id: Optional[str] = None,
|
|
529
|
+
file_path: Optional[str] = None,
|
|
530
|
+
unlock_after_write: bool = True,
|
|
531
|
+
dry_run: bool = False,
|
|
532
|
+
backup: bool = True,
|
|
533
|
+
diff: bool = False,
|
|
534
|
+
diff_context_lines: Optional[int] = None,
|
|
535
|
+
commit_message: Optional[str] = None,
|
|
536
|
+
validate_syntax_only: bool = False,
|
|
537
|
+
tree_id: Optional[str] = None,
|
|
538
|
+
lock_mode: Optional[str] = None,
|
|
539
|
+
) -> Dict[str, Any]:
|
|
540
|
+
"""Persist a completed adapter upload (``project_file_transfer_upload_save``)."""
|
|
541
|
+
_validate_upload_selector(
|
|
542
|
+
file_id=file_id,
|
|
543
|
+
file_path=file_path,
|
|
544
|
+
project_id=project_id,
|
|
545
|
+
)
|
|
546
|
+
await self.assert_session_exists(session_id)
|
|
547
|
+
params: Dict[str, Any] = {
|
|
548
|
+
"session_id": session_id,
|
|
549
|
+
"transfer_id": transfer_id,
|
|
550
|
+
"unlock_after_write": unlock_after_write,
|
|
551
|
+
"dry_run": dry_run,
|
|
552
|
+
"backup": backup,
|
|
553
|
+
}
|
|
554
|
+
if project_id is not None:
|
|
555
|
+
params["project_id"] = project_id
|
|
556
|
+
if file_id is not None:
|
|
557
|
+
params["file_id"] = file_id
|
|
558
|
+
if file_path is not None:
|
|
559
|
+
params["file_path"] = file_path
|
|
560
|
+
if diff:
|
|
561
|
+
params["diff"] = True
|
|
562
|
+
if diff_context_lines is not None:
|
|
563
|
+
params["diff_context_lines"] = diff_context_lines
|
|
564
|
+
if commit_message is not None:
|
|
565
|
+
params["commit_message"] = commit_message
|
|
566
|
+
if validate_syntax_only:
|
|
567
|
+
params["validate_syntax_only"] = True
|
|
568
|
+
if tree_id is not None:
|
|
569
|
+
params["tree_id"] = tree_id
|
|
570
|
+
if lock_mode is not None:
|
|
571
|
+
params["lock_mode"] = lock_mode
|
|
572
|
+
return _unwrap(
|
|
573
|
+
await self._client.call_validated(
|
|
574
|
+
"project_file_transfer_upload_save",
|
|
575
|
+
params,
|
|
576
|
+
)
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
async def upload_new(
|
|
580
|
+
self,
|
|
581
|
+
session_id: str,
|
|
582
|
+
payload: bytes,
|
|
583
|
+
project_id: str,
|
|
584
|
+
file_path: str,
|
|
585
|
+
*,
|
|
586
|
+
filename: Optional[str] = None,
|
|
587
|
+
compression: str = "identity",
|
|
588
|
+
unlock: bool = True,
|
|
589
|
+
dry_run: bool = False,
|
|
590
|
+
backup: bool = True,
|
|
591
|
+
diff: bool = False,
|
|
592
|
+
diff_context_lines: Optional[int] = None,
|
|
593
|
+
commit_message: Optional[str] = None,
|
|
594
|
+
validate_syntax_only: bool = False,
|
|
595
|
+
tree_id: Optional[str] = None,
|
|
596
|
+
lock_mode: Optional[str] = None,
|
|
597
|
+
) -> str:
|
|
598
|
+
"""Create a new project file (``project_file_transfer_upload_save`` create mode).
|
|
599
|
+
|
|
600
|
+
Pass ``project_id`` and project-relative ``file_path`` only — do not pass
|
|
601
|
+
``file_id``. The path must not already be indexed (``FILE_ALREADY_INDEXED``
|
|
602
|
+
otherwise). Returns the new ``file_id`` (may be empty on ``dry_run``).
|
|
603
|
+
"""
|
|
604
|
+
pid = _require_non_empty(project_id, field="project_id")
|
|
605
|
+
fp = _require_non_empty(file_path, field="file_path")
|
|
606
|
+
name = filename or Path(fp).name
|
|
607
|
+
receipt = await self.upload_bytes(
|
|
608
|
+
payload,
|
|
609
|
+
filename=name,
|
|
610
|
+
compression=compression,
|
|
611
|
+
)
|
|
612
|
+
if not getattr(receipt, "completed", False):
|
|
613
|
+
raise ClientValidationError(
|
|
614
|
+
"upload did not complete",
|
|
615
|
+
field="transfer_id",
|
|
616
|
+
details={"receipt": repr(receipt)},
|
|
617
|
+
)
|
|
618
|
+
saved = await self._commit_upload(
|
|
619
|
+
session_id,
|
|
620
|
+
str(receipt.transfer_id),
|
|
621
|
+
project_id=pid,
|
|
622
|
+
file_path=fp,
|
|
623
|
+
unlock_after_write=unlock,
|
|
624
|
+
backup=backup,
|
|
625
|
+
dry_run=dry_run,
|
|
626
|
+
diff=diff,
|
|
627
|
+
diff_context_lines=diff_context_lines,
|
|
628
|
+
commit_message=commit_message,
|
|
629
|
+
validate_syntax_only=validate_syntax_only,
|
|
630
|
+
tree_id=tree_id,
|
|
631
|
+
lock_mode=lock_mode,
|
|
632
|
+
)
|
|
633
|
+
if dry_run:
|
|
634
|
+
fid = str(saved.get("file_id") or "").strip()
|
|
635
|
+
return fid
|
|
636
|
+
return _extract_file_id(saved)
|
|
637
|
+
|
|
638
|
+
async def upload(
|
|
639
|
+
self,
|
|
640
|
+
session_id: str,
|
|
641
|
+
payload: bytes,
|
|
642
|
+
file_id: str,
|
|
643
|
+
*,
|
|
644
|
+
project_id: Optional[str] = None,
|
|
645
|
+
filename: Optional[str] = None,
|
|
646
|
+
compression: str = "identity",
|
|
647
|
+
unlock: bool = True,
|
|
648
|
+
backup: bool = True,
|
|
649
|
+
dry_run: bool = False,
|
|
650
|
+
diff: bool = False,
|
|
651
|
+
diff_context_lines: Optional[int] = None,
|
|
652
|
+
commit_message: Optional[str] = None,
|
|
653
|
+
validate_syntax_only: bool = False,
|
|
654
|
+
tree_id: Optional[str] = None,
|
|
655
|
+
lock_mode: Optional[str] = None,
|
|
656
|
+
) -> Dict[str, Any]:
|
|
657
|
+
"""Overwrite an existing indexed file (``project_file_transfer_upload_save`` update mode).
|
|
658
|
+
|
|
659
|
+
Pass ``file_id`` only. Optional ``project_id`` must match the row if provided.
|
|
660
|
+
Do not pass ``file_path``. Returns the server save payload.
|
|
661
|
+
"""
|
|
662
|
+
fid = _require_non_empty(file_id, field="file_id")
|
|
663
|
+
name = filename or "payload.bin"
|
|
664
|
+
receipt = await self.upload_bytes(
|
|
665
|
+
payload,
|
|
666
|
+
filename=name,
|
|
667
|
+
compression=compression,
|
|
668
|
+
)
|
|
669
|
+
if not getattr(receipt, "completed", False):
|
|
670
|
+
raise ClientValidationError(
|
|
671
|
+
"upload did not complete",
|
|
672
|
+
field="transfer_id",
|
|
673
|
+
details={"receipt": repr(receipt)},
|
|
674
|
+
)
|
|
675
|
+
saved = await self._commit_upload(
|
|
676
|
+
session_id,
|
|
677
|
+
str(receipt.transfer_id),
|
|
678
|
+
file_id=fid,
|
|
679
|
+
project_id=project_id,
|
|
680
|
+
unlock_after_write=unlock,
|
|
681
|
+
backup=backup,
|
|
682
|
+
dry_run=dry_run,
|
|
683
|
+
diff=diff,
|
|
684
|
+
diff_context_lines=diff_context_lines,
|
|
685
|
+
commit_message=commit_message,
|
|
686
|
+
validate_syntax_only=validate_syntax_only,
|
|
687
|
+
tree_id=tree_id,
|
|
688
|
+
lock_mode=lock_mode,
|
|
689
|
+
)
|
|
690
|
+
if not dry_run:
|
|
691
|
+
_extract_file_id(saved)
|
|
692
|
+
return saved
|