synapse-sdk 1.0.0b5__py3-none-any.whl → 2025.12.3__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.
- synapse_sdk/__init__.py +24 -0
- synapse_sdk/cli/code_server.py +305 -33
- synapse_sdk/clients/agent/__init__.py +2 -1
- synapse_sdk/clients/agent/container.py +143 -0
- synapse_sdk/clients/agent/ray.py +296 -38
- synapse_sdk/clients/backend/annotation.py +1 -1
- synapse_sdk/clients/backend/core.py +31 -4
- synapse_sdk/clients/backend/data_collection.py +82 -7
- synapse_sdk/clients/backend/hitl.py +1 -1
- synapse_sdk/clients/backend/ml.py +1 -1
- synapse_sdk/clients/base.py +211 -61
- synapse_sdk/loggers.py +46 -0
- synapse_sdk/plugins/README.md +1340 -0
- synapse_sdk/plugins/categories/base.py +59 -9
- synapse_sdk/plugins/categories/export/actions/__init__.py +3 -0
- synapse_sdk/plugins/categories/export/actions/export/__init__.py +28 -0
- synapse_sdk/plugins/categories/export/actions/export/action.py +165 -0
- synapse_sdk/plugins/categories/export/actions/export/enums.py +113 -0
- synapse_sdk/plugins/categories/export/actions/export/exceptions.py +53 -0
- synapse_sdk/plugins/categories/export/actions/export/models.py +74 -0
- synapse_sdk/plugins/categories/export/actions/export/run.py +195 -0
- synapse_sdk/plugins/categories/export/actions/export/utils.py +187 -0
- synapse_sdk/plugins/categories/export/templates/config.yaml +19 -1
- synapse_sdk/plugins/categories/export/templates/plugin/__init__.py +390 -0
- synapse_sdk/plugins/categories/export/templates/plugin/export.py +153 -177
- synapse_sdk/plugins/categories/neural_net/actions/train.py +1130 -32
- synapse_sdk/plugins/categories/neural_net/actions/tune.py +157 -4
- synapse_sdk/plugins/categories/neural_net/templates/config.yaml +7 -4
- synapse_sdk/plugins/categories/pre_annotation/actions/__init__.py +4 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/pre_annotation/__init__.py +3 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/pre_annotation/action.py +10 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/__init__.py +28 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/action.py +148 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/enums.py +269 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/exceptions.py +14 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/factory.py +76 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/models.py +100 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/orchestrator.py +248 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/run.py +64 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/__init__.py +17 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/annotation.py +265 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/base.py +170 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/extraction.py +83 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/metrics.py +92 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/preprocessor.py +243 -0
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task/strategies/validation.py +143 -0
- synapse_sdk/plugins/categories/upload/actions/upload/__init__.py +19 -0
- synapse_sdk/plugins/categories/upload/actions/upload/action.py +236 -0
- synapse_sdk/plugins/categories/upload/actions/upload/context.py +185 -0
- synapse_sdk/plugins/categories/upload/actions/upload/enums.py +493 -0
- synapse_sdk/plugins/categories/upload/actions/upload/exceptions.py +36 -0
- synapse_sdk/plugins/categories/upload/actions/upload/factory.py +138 -0
- synapse_sdk/plugins/categories/upload/actions/upload/models.py +214 -0
- synapse_sdk/plugins/categories/upload/actions/upload/orchestrator.py +183 -0
- synapse_sdk/plugins/categories/upload/actions/upload/registry.py +113 -0
- synapse_sdk/plugins/categories/upload/actions/upload/run.py +179 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/base.py +107 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/cleanup.py +62 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/collection.py +63 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/generate.py +91 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/initialize.py +82 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/metadata.py +235 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/organize.py +201 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/upload.py +104 -0
- synapse_sdk/plugins/categories/upload/actions/upload/steps/validate.py +71 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/base.py +82 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/data_unit/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/data_unit/batch.py +39 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/data_unit/single.py +29 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/file_discovery/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/file_discovery/flat.py +300 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/file_discovery/recursive.py +287 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/metadata/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/metadata/excel.py +174 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/metadata/none.py +16 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/upload/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/upload/sync.py +84 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/validation/__init__.py +1 -0
- synapse_sdk/plugins/categories/upload/actions/upload/strategies/validation/default.py +60 -0
- synapse_sdk/plugins/categories/upload/actions/upload/utils.py +250 -0
- synapse_sdk/plugins/categories/upload/templates/README.md +470 -0
- synapse_sdk/plugins/categories/upload/templates/config.yaml +28 -2
- synapse_sdk/plugins/categories/upload/templates/plugin/__init__.py +310 -0
- synapse_sdk/plugins/categories/upload/templates/plugin/upload.py +82 -20
- synapse_sdk/plugins/models.py +111 -9
- synapse_sdk/plugins/templates/plugin-config-schema.json +7 -0
- synapse_sdk/plugins/templates/schema.json +7 -0
- synapse_sdk/plugins/utils/__init__.py +3 -0
- synapse_sdk/plugins/utils/ray_gcs.py +66 -0
- synapse_sdk/shared/__init__.py +25 -0
- synapse_sdk/utils/converters/dm/__init__.py +42 -41
- synapse_sdk/utils/converters/dm/base.py +137 -0
- synapse_sdk/utils/converters/dm/from_v1.py +208 -562
- synapse_sdk/utils/converters/dm/to_v1.py +258 -304
- synapse_sdk/utils/converters/dm/tools/__init__.py +214 -0
- synapse_sdk/utils/converters/dm/tools/answer.py +95 -0
- synapse_sdk/utils/converters/dm/tools/bounding_box.py +132 -0
- synapse_sdk/utils/converters/dm/tools/bounding_box_3d.py +121 -0
- synapse_sdk/utils/converters/dm/tools/classification.py +75 -0
- synapse_sdk/utils/converters/dm/tools/keypoint.py +117 -0
- synapse_sdk/utils/converters/dm/tools/named_entity.py +111 -0
- synapse_sdk/utils/converters/dm/tools/polygon.py +122 -0
- synapse_sdk/utils/converters/dm/tools/polyline.py +124 -0
- synapse_sdk/utils/converters/dm/tools/prompt.py +94 -0
- synapse_sdk/utils/converters/dm/tools/relation.py +86 -0
- synapse_sdk/utils/converters/dm/tools/segmentation.py +141 -0
- synapse_sdk/utils/converters/dm/tools/segmentation_3d.py +83 -0
- synapse_sdk/utils/converters/dm/types.py +168 -0
- synapse_sdk/utils/converters/dm/utils.py +162 -0
- synapse_sdk/utils/converters/dm_legacy/__init__.py +56 -0
- synapse_sdk/utils/converters/dm_legacy/from_v1.py +627 -0
- synapse_sdk/utils/converters/dm_legacy/to_v1.py +367 -0
- synapse_sdk/utils/file/__init__.py +58 -0
- synapse_sdk/utils/file/archive.py +32 -0
- synapse_sdk/utils/file/checksum.py +56 -0
- synapse_sdk/utils/file/chunking.py +31 -0
- synapse_sdk/utils/file/download.py +385 -0
- synapse_sdk/utils/file/encoding.py +40 -0
- synapse_sdk/utils/file/io.py +22 -0
- synapse_sdk/utils/file/upload.py +165 -0
- synapse_sdk/utils/file/video/__init__.py +29 -0
- synapse_sdk/utils/file/video/transcode.py +307 -0
- synapse_sdk/utils/{file.py → file.py.backup} +77 -0
- synapse_sdk/utils/network.py +272 -0
- synapse_sdk/utils/storage/__init__.py +6 -2
- synapse_sdk/utils/storage/providers/file_system.py +6 -0
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/METADATA +19 -2
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/RECORD +134 -74
- synapse_sdk/devtools/docs/.gitignore +0 -20
- synapse_sdk/devtools/docs/README.md +0 -41
- synapse_sdk/devtools/docs/blog/2019-05-28-first-blog-post.md +0 -12
- synapse_sdk/devtools/docs/blog/2019-05-29-long-blog-post.md +0 -44
- synapse_sdk/devtools/docs/blog/2021-08-01-mdx-blog-post.mdx +0 -24
- synapse_sdk/devtools/docs/blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg +0 -0
- synapse_sdk/devtools/docs/blog/2021-08-26-welcome/index.md +0 -29
- synapse_sdk/devtools/docs/blog/authors.yml +0 -25
- synapse_sdk/devtools/docs/blog/tags.yml +0 -19
- synapse_sdk/devtools/docs/docusaurus.config.ts +0 -138
- synapse_sdk/devtools/docs/package-lock.json +0 -17455
- synapse_sdk/devtools/docs/package.json +0 -47
- synapse_sdk/devtools/docs/sidebars.ts +0 -44
- synapse_sdk/devtools/docs/src/components/HomepageFeatures/index.tsx +0 -71
- synapse_sdk/devtools/docs/src/components/HomepageFeatures/styles.module.css +0 -11
- synapse_sdk/devtools/docs/src/css/custom.css +0 -30
- synapse_sdk/devtools/docs/src/pages/index.module.css +0 -23
- synapse_sdk/devtools/docs/src/pages/index.tsx +0 -21
- synapse_sdk/devtools/docs/src/pages/markdown-page.md +0 -7
- synapse_sdk/devtools/docs/static/.nojekyll +0 -0
- synapse_sdk/devtools/docs/static/img/docusaurus-social-card.jpg +0 -0
- synapse_sdk/devtools/docs/static/img/docusaurus.png +0 -0
- synapse_sdk/devtools/docs/static/img/favicon.ico +0 -0
- synapse_sdk/devtools/docs/static/img/logo.png +0 -0
- synapse_sdk/devtools/docs/static/img/undraw_docusaurus_mountain.svg +0 -171
- synapse_sdk/devtools/docs/static/img/undraw_docusaurus_react.svg +0 -170
- synapse_sdk/devtools/docs/static/img/undraw_docusaurus_tree.svg +0 -40
- synapse_sdk/devtools/docs/tsconfig.json +0 -8
- synapse_sdk/plugins/categories/export/actions/export.py +0 -346
- synapse_sdk/plugins/categories/export/enums.py +0 -7
- synapse_sdk/plugins/categories/neural_net/actions/gradio.py +0 -151
- synapse_sdk/plugins/categories/pre_annotation/actions/to_task.py +0 -943
- synapse_sdk/plugins/categories/upload/actions/upload.py +0 -954
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/WHEEL +0 -0
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/entry_points.txt +0 -0
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/licenses/LICENSE +0 -0
- {synapse_sdk-1.0.0b5.dist-info → synapse_sdk-2025.12.3.dist-info}/top_level.txt +0 -0
synapse_sdk/__init__.py
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Export / Import Guidelines
|
|
3
|
+
--------------------------
|
|
4
|
+
|
|
5
|
+
1. Do NOT import the top-level package directly.
|
|
6
|
+
- All imports must start from at least two levels below the root package.
|
|
7
|
+
(e.g., `project.module.submodule` is allowed,
|
|
8
|
+
`project` 또는 `project.module` 단일 import는 금지)
|
|
9
|
+
|
|
10
|
+
2. Wildcard import (`from x import *`) is strictly prohibited.
|
|
11
|
+
- 모든 외부 노출(export)은 명시적인 이름 기반으로 관리해야 한다.
|
|
12
|
+
- `__all__` 리스트를 통해 공개할 API를 명확히 정의할 것.
|
|
13
|
+
|
|
14
|
+
3. Public API 를 구성할 때:
|
|
15
|
+
- 하위 모듈에서 export할 항목만 `__all__`에 선언한다.
|
|
16
|
+
- 내부 구현용 함수/클래스는 `_` prefix 를 사용하거나 `__all__`에 포함하지 않는다.
|
|
17
|
+
|
|
18
|
+
4. 모듈 간 의존성은 최단 경로만 허용한다.
|
|
19
|
+
- 불필요한 상위/평행 패키지 import 경로는 금지하여 순환 의존성(circular dependency)을 방지한다.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from synapse_sdk.shared import worker_process_setup_hook
|
|
23
|
+
|
|
24
|
+
__all__ = ['worker_process_setup_hook']
|
synapse_sdk/cli/code_server.py
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
import shutil
|
|
5
|
+
import socket
|
|
5
6
|
import subprocess
|
|
7
|
+
import webbrowser
|
|
6
8
|
from pathlib import Path
|
|
7
9
|
from typing import Optional
|
|
8
10
|
from urllib.parse import quote
|
|
@@ -141,6 +143,169 @@ def detect_and_encrypt_plugin(workspace_path: str) -> Optional[dict]:
|
|
|
141
143
|
return None
|
|
142
144
|
|
|
143
145
|
|
|
146
|
+
def is_ssh_session() -> bool:
|
|
147
|
+
"""Check if we're in an SSH session.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
bool: True if in SSH session, False otherwise
|
|
151
|
+
"""
|
|
152
|
+
return bool(os.environ.get('SSH_CONNECTION') or os.environ.get('SSH_CLIENT'))
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def is_vscode_terminal() -> bool:
|
|
156
|
+
"""Check if we're running in a VSCode terminal.
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
bool: True if in VSCode terminal, False otherwise
|
|
160
|
+
"""
|
|
161
|
+
return os.environ.get('TERM_PROGRAM') == 'vscode'
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def is_vscode_ssh_session() -> bool:
|
|
165
|
+
"""Check if we're in a VSCode terminal over SSH.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
bool: True if in VSCode terminal via SSH, False otherwise
|
|
169
|
+
"""
|
|
170
|
+
return is_vscode_terminal() and is_ssh_session()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def get_ssh_tunnel_instructions(port: int) -> str:
|
|
174
|
+
"""Get SSH tunnel setup instructions for VSCode users.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
port: The port number code-server is running on
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
str: Instructions for setting up SSH tunnel
|
|
181
|
+
"""
|
|
182
|
+
ssh_client = os.environ.get('SSH_CLIENT', '').split()[0] if os.environ.get('SSH_CLIENT') else 'your_server'
|
|
183
|
+
|
|
184
|
+
instructions = f"""
|
|
185
|
+
📡 VSCode SSH Tunnel Setup:
|
|
186
|
+
|
|
187
|
+
Since you're using VSCode's integrated terminal over SSH, you can access code-server locally by:
|
|
188
|
+
|
|
189
|
+
1. Using VSCode's built-in port forwarding:
|
|
190
|
+
• Open Command Palette (Cmd/Ctrl+Shift+P)
|
|
191
|
+
• Type "Forward a Port"
|
|
192
|
+
• Enter port: {port}
|
|
193
|
+
• VSCode will automatically forward the port
|
|
194
|
+
|
|
195
|
+
2. Or manually forward the port in a new local terminal:
|
|
196
|
+
ssh -L {port}:localhost:{port} {ssh_client}
|
|
197
|
+
|
|
198
|
+
3. Then open in your local browser:
|
|
199
|
+
http://localhost:{port}
|
|
200
|
+
"""
|
|
201
|
+
return instructions
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def open_browser_smart(url: str) -> bool:
|
|
205
|
+
"""Open browser with smart fallback handling.
|
|
206
|
+
|
|
207
|
+
Attempts to open a browser using multiple methods, with appropriate
|
|
208
|
+
handling for SSH sessions and headless environments.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
url: URL to open
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
bool: True if browser was opened successfully, False otherwise
|
|
215
|
+
"""
|
|
216
|
+
# Don't even try to open browser in SSH sessions (except VSCode can handle it)
|
|
217
|
+
if is_ssh_session() and not is_vscode_terminal():
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
# Try Python's webbrowser module first (cross-platform)
|
|
221
|
+
try:
|
|
222
|
+
if webbrowser.open(url):
|
|
223
|
+
return True
|
|
224
|
+
except Exception:
|
|
225
|
+
pass
|
|
226
|
+
|
|
227
|
+
# Try platform-specific commands
|
|
228
|
+
commands = []
|
|
229
|
+
|
|
230
|
+
# Check for macOS
|
|
231
|
+
if shutil.which('open'):
|
|
232
|
+
commands.append(['open', url])
|
|
233
|
+
|
|
234
|
+
# Check for Linux with display
|
|
235
|
+
if os.environ.get('DISPLAY'):
|
|
236
|
+
if shutil.which('xdg-open'):
|
|
237
|
+
commands.append(['xdg-open', url])
|
|
238
|
+
if shutil.which('gnome-open'):
|
|
239
|
+
commands.append(['gnome-open', url])
|
|
240
|
+
if shutil.which('kde-open'):
|
|
241
|
+
commands.append(['kde-open', url])
|
|
242
|
+
|
|
243
|
+
# Try each command
|
|
244
|
+
for cmd in commands:
|
|
245
|
+
try:
|
|
246
|
+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=2)
|
|
247
|
+
if result.returncode == 0:
|
|
248
|
+
return True
|
|
249
|
+
except Exception:
|
|
250
|
+
continue
|
|
251
|
+
|
|
252
|
+
return False
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def is_port_in_use(port: int) -> bool:
|
|
256
|
+
"""Check if a port is already in use.
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
port: Port number to check
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
bool: True if port is in use, False otherwise
|
|
263
|
+
"""
|
|
264
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
265
|
+
try:
|
|
266
|
+
s.bind(('127.0.0.1', port))
|
|
267
|
+
return False
|
|
268
|
+
except socket.error:
|
|
269
|
+
return True
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def get_process_on_port(port: int) -> Optional[str]:
|
|
273
|
+
"""Get the name of the process using a specific port.
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
port: Port number to check
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
str: Process name if found, None otherwise
|
|
280
|
+
"""
|
|
281
|
+
try:
|
|
282
|
+
# Use lsof to find the process
|
|
283
|
+
result = subprocess.run(['lsof', '-i', f':{port}', '-t'], capture_output=True, text=True, timeout=2)
|
|
284
|
+
|
|
285
|
+
if result.returncode == 0 and result.stdout.strip():
|
|
286
|
+
# Get the PID
|
|
287
|
+
pid = result.stdout.strip().split('\n')[0]
|
|
288
|
+
|
|
289
|
+
# Get process details
|
|
290
|
+
proc_result = subprocess.run(['ps', '-p', pid, '-o', 'comm='], capture_output=True, text=True, timeout=2)
|
|
291
|
+
|
|
292
|
+
if proc_result.returncode == 0:
|
|
293
|
+
process_name = proc_result.stdout.strip()
|
|
294
|
+
# Check if it's a code-server process
|
|
295
|
+
if 'node' in process_name or 'code-server' in process_name:
|
|
296
|
+
# Try to get more details
|
|
297
|
+
cmd_result = subprocess.run(
|
|
298
|
+
['ps', '-p', pid, '-o', 'args='], capture_output=True, text=True, timeout=2
|
|
299
|
+
)
|
|
300
|
+
if cmd_result.returncode == 0 and 'code-server' in cmd_result.stdout:
|
|
301
|
+
return 'code-server'
|
|
302
|
+
return process_name
|
|
303
|
+
except Exception:
|
|
304
|
+
pass
|
|
305
|
+
|
|
306
|
+
return None
|
|
307
|
+
|
|
308
|
+
|
|
144
309
|
def is_code_server_installed() -> bool:
|
|
145
310
|
"""Check if code-server is installed locally.
|
|
146
311
|
|
|
@@ -154,7 +319,7 @@ def get_code_server_port() -> int:
|
|
|
154
319
|
"""Get code-server port from config file.
|
|
155
320
|
|
|
156
321
|
Returns:
|
|
157
|
-
int: Port number from config, defaults to
|
|
322
|
+
int: Port number from config, defaults to 8070 if not found
|
|
158
323
|
"""
|
|
159
324
|
config_path = Path.home() / '.config' / 'code-server' / 'config.yaml'
|
|
160
325
|
|
|
@@ -163,7 +328,7 @@ def get_code_server_port() -> int:
|
|
|
163
328
|
with open(config_path, 'r') as f:
|
|
164
329
|
config = yaml.safe_load(f)
|
|
165
330
|
|
|
166
|
-
# Parse bind-addr which can be in format "127.0.0.1:
|
|
331
|
+
# Parse bind-addr which can be in format "127.0.0.1:8070" or just ":8070"
|
|
167
332
|
bind_addr = config.get('bind-addr', '')
|
|
168
333
|
if ':' in bind_addr:
|
|
169
334
|
port_str = bind_addr.split(':')[-1]
|
|
@@ -176,31 +341,133 @@ def get_code_server_port() -> int:
|
|
|
176
341
|
pass
|
|
177
342
|
|
|
178
343
|
# Default port if config not found or invalid
|
|
179
|
-
return
|
|
344
|
+
return 8070
|
|
180
345
|
|
|
181
346
|
|
|
182
|
-
def launch_local_code_server(workspace_path: str, open_browser: bool = True) -> None:
|
|
347
|
+
def launch_local_code_server(workspace_path: str, open_browser: bool = True, custom_port: Optional[int] = None) -> None:
|
|
183
348
|
"""Launch local code-server instance.
|
|
184
349
|
|
|
185
350
|
Args:
|
|
186
351
|
workspace_path: Directory to open in code-server
|
|
187
352
|
open_browser: Whether to open browser automatically
|
|
353
|
+
custom_port: Optional custom port to use instead of config/default
|
|
188
354
|
"""
|
|
189
355
|
try:
|
|
190
|
-
#
|
|
191
|
-
port = get_code_server_port()
|
|
356
|
+
# Use custom port if provided, otherwise get from config
|
|
357
|
+
port = custom_port if custom_port else get_code_server_port()
|
|
358
|
+
|
|
359
|
+
# Check if port is already in use
|
|
360
|
+
if is_port_in_use(port):
|
|
361
|
+
process_name = get_process_on_port(port)
|
|
362
|
+
|
|
363
|
+
if process_name == 'code-server':
|
|
364
|
+
# Code-server is already running
|
|
365
|
+
click.echo(f'⚠️ Code-server is already running on port {port}')
|
|
366
|
+
|
|
367
|
+
# Create URL with folder query parameter
|
|
368
|
+
encoded_path = quote(workspace_path)
|
|
369
|
+
url_with_folder = f'http://localhost:{port}/?folder={encoded_path}'
|
|
370
|
+
|
|
371
|
+
# Ask user what to do
|
|
372
|
+
questions = [
|
|
373
|
+
inquirer.List(
|
|
374
|
+
'action',
|
|
375
|
+
message='What would you like to do?',
|
|
376
|
+
choices=[
|
|
377
|
+
('Use existing code-server instance', 'use_existing'),
|
|
378
|
+
('Stop existing and start new instance', 'restart'),
|
|
379
|
+
('Cancel', 'cancel'),
|
|
380
|
+
],
|
|
381
|
+
)
|
|
382
|
+
]
|
|
383
|
+
|
|
384
|
+
try:
|
|
385
|
+
answers = inquirer.prompt(questions)
|
|
386
|
+
|
|
387
|
+
if not answers or answers['action'] == 'cancel':
|
|
388
|
+
click.echo('Cancelled')
|
|
389
|
+
return
|
|
390
|
+
|
|
391
|
+
if answers['action'] == 'use_existing':
|
|
392
|
+
click.echo('\n✅ Using existing code-server instance')
|
|
393
|
+
click.echo(f' URL: {url_with_folder}')
|
|
394
|
+
|
|
395
|
+
# Optionally open browser
|
|
396
|
+
if open_browser:
|
|
397
|
+
if is_vscode_ssh_session():
|
|
398
|
+
# Special handling for VSCode SSH sessions
|
|
399
|
+
click.echo(get_ssh_tunnel_instructions(port))
|
|
400
|
+
click.echo(f'🔗 Remote URL: {url_with_folder}')
|
|
401
|
+
click.echo(f'\n✨ After port forwarding, access at: http://localhost:{port}')
|
|
402
|
+
elif is_ssh_session():
|
|
403
|
+
click.echo('📝 SSH session detected - please open the URL in your local browser')
|
|
404
|
+
click.echo(f'👉 URL: {url_with_folder}')
|
|
405
|
+
elif open_browser_smart(url_with_folder):
|
|
406
|
+
click.echo('✅ Browser opened successfully')
|
|
407
|
+
else:
|
|
408
|
+
click.echo('⚠️ Could not open browser automatically')
|
|
409
|
+
click.echo(f'👉 URL: {url_with_folder}')
|
|
410
|
+
return
|
|
411
|
+
|
|
412
|
+
if answers['action'] == 'restart':
|
|
413
|
+
# Stop existing code-server
|
|
414
|
+
click.echo('Stopping existing code-server...')
|
|
415
|
+
try:
|
|
416
|
+
# Get PID of code-server process
|
|
417
|
+
result = subprocess.run(
|
|
418
|
+
['lsof', '-i', f':{port}', '-t'], capture_output=True, text=True, timeout=2
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
if result.returncode == 0 and result.stdout.strip():
|
|
422
|
+
pid = result.stdout.strip().split('\n')[0]
|
|
423
|
+
subprocess.run(['kill', pid], timeout=5)
|
|
424
|
+
|
|
425
|
+
# Wait a moment for process to stop
|
|
426
|
+
import time
|
|
427
|
+
|
|
428
|
+
time.sleep(2)
|
|
429
|
+
|
|
430
|
+
click.echo('✅ Existing code-server stopped')
|
|
431
|
+
except Exception as e:
|
|
432
|
+
click.echo(f'⚠️ Could not stop existing code-server: {e}')
|
|
433
|
+
click.echo('Please stop it manually and try again')
|
|
434
|
+
return
|
|
435
|
+
|
|
436
|
+
except (KeyboardInterrupt, EOFError):
|
|
437
|
+
click.echo('\nCancelled')
|
|
438
|
+
return
|
|
439
|
+
|
|
440
|
+
else:
|
|
441
|
+
# Another process is using the port
|
|
442
|
+
click.echo(f'❌ Port {port} is already in use by: {process_name or "unknown process"}')
|
|
443
|
+
if not custom_port:
|
|
444
|
+
click.echo('\nYou can:')
|
|
445
|
+
click.echo('1. Stop the process using the port')
|
|
446
|
+
click.echo('2. Use a different port with --port option (e.g., --port 8071)')
|
|
447
|
+
click.echo('3. Change the default port in ~/.config/code-server/config.yaml')
|
|
448
|
+
else:
|
|
449
|
+
click.echo(f'Please try a different port or stop the process using port {port}')
|
|
450
|
+
return
|
|
192
451
|
|
|
193
452
|
# Create URL with folder query parameter
|
|
194
453
|
encoded_path = quote(workspace_path)
|
|
195
454
|
url_with_folder = f'http://localhost:{port}/?folder={encoded_path}'
|
|
196
455
|
|
|
197
456
|
# Basic code-server command - let code-server handle the workspace internally
|
|
198
|
-
cmd = ['code-server'
|
|
457
|
+
cmd = ['code-server']
|
|
458
|
+
|
|
459
|
+
# Add custom port binding if specified
|
|
460
|
+
if custom_port:
|
|
461
|
+
cmd.extend(['--bind-addr', f'127.0.0.1:{port}'])
|
|
462
|
+
|
|
463
|
+
cmd.append(workspace_path)
|
|
199
464
|
|
|
200
465
|
if not open_browser:
|
|
201
466
|
cmd.append('--disable-getting-started-override')
|
|
202
467
|
|
|
203
468
|
click.echo(f'🚀 Starting local code-server for workspace: {workspace_path}')
|
|
469
|
+
if custom_port:
|
|
470
|
+
click.echo(f' Using custom port: {port}')
|
|
204
471
|
click.echo(f' URL: {url_with_folder}')
|
|
205
472
|
click.echo(' Press Ctrl+C to stop the server')
|
|
206
473
|
|
|
@@ -221,17 +488,18 @@ def launch_local_code_server(workspace_path: str, open_browser: bool = True) ->
|
|
|
221
488
|
time.sleep(3)
|
|
222
489
|
|
|
223
490
|
# Open browser with folder parameter
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
click.echo(
|
|
234
|
-
|
|
491
|
+
if is_vscode_ssh_session():
|
|
492
|
+
# Special handling for VSCode SSH sessions
|
|
493
|
+
click.echo(get_ssh_tunnel_instructions(port))
|
|
494
|
+
click.echo(f'🔗 Remote URL: {url_with_folder}')
|
|
495
|
+
click.echo(f'\n✨ After port forwarding, access at: http://localhost:{port}')
|
|
496
|
+
elif is_ssh_session():
|
|
497
|
+
click.echo('📝 SSH session detected - please open the URL in your local browser')
|
|
498
|
+
click.echo(f'👉 URL: {url_with_folder}')
|
|
499
|
+
elif open_browser_smart(url_with_folder):
|
|
500
|
+
click.echo('✅ Browser opened successfully')
|
|
501
|
+
else:
|
|
502
|
+
click.echo('⚠️ Could not open browser automatically')
|
|
235
503
|
click.echo(f'👉 Please manually open: {url_with_folder}')
|
|
236
504
|
|
|
237
505
|
# Wait for the server thread (blocking)
|
|
@@ -333,19 +601,22 @@ def run_agent_code_server(agent: Optional[str], workspace: str, open_browser: bo
|
|
|
333
601
|
if open_browser and url:
|
|
334
602
|
click.echo('\nAttempting to open in browser...')
|
|
335
603
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
click.echo(
|
|
347
|
-
|
|
348
|
-
|
|
604
|
+
if is_vscode_ssh_session():
|
|
605
|
+
# Extract port from URL for instructions
|
|
606
|
+
import re
|
|
607
|
+
|
|
608
|
+
port_match = re.search(r':(\d+)', url)
|
|
609
|
+
if port_match:
|
|
610
|
+
agent_port = int(port_match.group(1))
|
|
611
|
+
click.echo(get_ssh_tunnel_instructions(agent_port))
|
|
612
|
+
click.echo(f'🔗 Remote URL: {url}')
|
|
613
|
+
elif is_ssh_session():
|
|
614
|
+
click.echo('📝 SSH session detected - please open the URL in your local browser')
|
|
615
|
+
click.echo(f'👉 URL: {url}')
|
|
616
|
+
elif open_browser_smart(url):
|
|
617
|
+
click.echo('✅ Browser opened successfully')
|
|
618
|
+
else:
|
|
619
|
+
click.echo('⚠️ Could not open browser automatically')
|
|
349
620
|
click.echo(f'👉 Please manually open: {url}')
|
|
350
621
|
|
|
351
622
|
# Show additional instructions
|
|
@@ -364,7 +635,8 @@ def run_agent_code_server(agent: Optional[str], workspace: str, open_browser: bo
|
|
|
364
635
|
@click.option('--agent', help='Agent name or ID')
|
|
365
636
|
@click.option('--open-browser/--no-open-browser', default=True, help='Open in browser')
|
|
366
637
|
@click.option('--workspace', help='Workspace directory path (defaults to current directory)')
|
|
367
|
-
|
|
638
|
+
@click.option('--port', type=int, help='Port to bind code-server (default: from config or 8070)')
|
|
639
|
+
def code_server(agent: Optional[str], open_browser: bool, workspace: Optional[str], port: Optional[int]):
|
|
368
640
|
"""Open code-server either through agent or locally."""
|
|
369
641
|
|
|
370
642
|
# Get current working directory if workspace not specified
|
|
@@ -405,7 +677,7 @@ def code_server(agent: Optional[str], open_browser: bool, workspace: Optional[st
|
|
|
405
677
|
|
|
406
678
|
elif answers['option'] == 'local':
|
|
407
679
|
click.echo('\n💻 Starting local code-server...')
|
|
408
|
-
launch_local_code_server(workspace, open_browser)
|
|
680
|
+
launch_local_code_server(workspace, open_browser, port)
|
|
409
681
|
|
|
410
682
|
elif answers['option'] == 'install':
|
|
411
683
|
show_code_server_installation_help()
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
from synapse_sdk.clients.agent.container import ContainerClientMixin
|
|
1
2
|
from synapse_sdk.clients.agent.core import CoreClientMixin
|
|
2
3
|
from synapse_sdk.clients.agent.ray import RayClientMixin
|
|
3
4
|
from synapse_sdk.clients.agent.service import ServiceClientMixin
|
|
4
5
|
from synapse_sdk.clients.exceptions import ClientError
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
class AgentClient(CoreClientMixin, RayClientMixin, ServiceClientMixin):
|
|
8
|
+
class AgentClient(CoreClientMixin, RayClientMixin, ServiceClientMixin, ContainerClientMixin):
|
|
8
9
|
name = 'Agent'
|
|
9
10
|
agent_token = None
|
|
10
11
|
user_token = None
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, Dict, Iterable, Optional, Union
|
|
5
|
+
|
|
6
|
+
from synapse_sdk.clients.base import BaseClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ContainerClientMixin(BaseClient):
|
|
10
|
+
"""Client mixin exposing the agent container management API."""
|
|
11
|
+
|
|
12
|
+
def health_check(self):
|
|
13
|
+
"""Perform a health check on Docker sock."""
|
|
14
|
+
path = 'health/'
|
|
15
|
+
return self._get(path)
|
|
16
|
+
|
|
17
|
+
def list_containers(self, params: Optional[Dict[str, Any]] = None, *, list_all: bool = False):
|
|
18
|
+
"""List containers managed by the agent.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
params: Optional query parameters (e.g. {'status': 'running'}).
|
|
22
|
+
list_all: When True, returns ``(generator, count)`` covering every page.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
dict | tuple: Standard paginated response or a tuple for ``list_all``.
|
|
26
|
+
"""
|
|
27
|
+
path = 'containers/'
|
|
28
|
+
return self._list(path, params=params, list_all=list_all)
|
|
29
|
+
|
|
30
|
+
def get_container(self, container_id: Union[int, str]):
|
|
31
|
+
"""Retrieve details for a specific container."""
|
|
32
|
+
path = f'containers/{container_id}/'
|
|
33
|
+
return self._get(path)
|
|
34
|
+
|
|
35
|
+
def delete_container(self, container_id: Union[int, str]):
|
|
36
|
+
"""Stop and remove a container."""
|
|
37
|
+
path = f'containers/{container_id}/'
|
|
38
|
+
return self._delete(path)
|
|
39
|
+
|
|
40
|
+
def create_container(
|
|
41
|
+
self,
|
|
42
|
+
plugin_release: Optional[Union[str, Any]] = None,
|
|
43
|
+
*,
|
|
44
|
+
model: Optional[int] = None,
|
|
45
|
+
params: Optional[Dict[str, Any]] = None,
|
|
46
|
+
envs: Optional[Dict[str, str]] = None,
|
|
47
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
48
|
+
labels: Optional[Iterable[str]] = None,
|
|
49
|
+
plugin_file: Optional[Union[str, Path]] = None,
|
|
50
|
+
):
|
|
51
|
+
"""Create a Docker container running a plugin Gradio interface.
|
|
52
|
+
|
|
53
|
+
If a container with the same ``plugin_release`` and ``model`` already exists,
|
|
54
|
+
it will be restarted instead of creating a new one.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
plugin_release: Plugin identifier. Accepts either ``synapse_sdk.plugins.models.PluginRelease``
|
|
58
|
+
instances or the ``"<plugin_code>@<version>"`` shorthand string.
|
|
59
|
+
model: Optional model ID to associate with the container. Used together with
|
|
60
|
+
``plugin_release`` to uniquely identify a container for restart behavior.
|
|
61
|
+
params: Arbitrary parameters forwarded to ``plugin/gradio_interface.py``.
|
|
62
|
+
envs: Extra environment variables injected into the container.
|
|
63
|
+
metadata: Additional metadata stored with the container record.
|
|
64
|
+
labels: Optional container labels/tags for display or filtering.
|
|
65
|
+
plugin_file: Optional path to a packaged plugin release to upload directly.
|
|
66
|
+
The archive must contain ``plugin/gradio_interface.py``.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
dict: Container creation response that includes the exposed Gradio endpoint.
|
|
70
|
+
If an existing container was restarted, the response includes ``restarted: True``.
|
|
71
|
+
|
|
72
|
+
Raises:
|
|
73
|
+
FileNotFoundError: If ``plugin_file`` is provided but does not exist.
|
|
74
|
+
ValueError: If neither ``plugin_release`` nor ``plugin_file`` are provided.
|
|
75
|
+
"""
|
|
76
|
+
if not plugin_release and not plugin_file:
|
|
77
|
+
raise ValueError('Either "plugin_release" or "plugin_file" must be provided to create a container.')
|
|
78
|
+
|
|
79
|
+
data: Dict[str, Any] = {}
|
|
80
|
+
|
|
81
|
+
if plugin_release:
|
|
82
|
+
data.update(self._serialize_plugin_release(plugin_release))
|
|
83
|
+
|
|
84
|
+
if model is not None:
|
|
85
|
+
data['model'] = model
|
|
86
|
+
|
|
87
|
+
optional_payload = {
|
|
88
|
+
'params': params if params is not None else None,
|
|
89
|
+
'envs': envs or None,
|
|
90
|
+
'metadata': metadata or None,
|
|
91
|
+
'labels': list(labels) if labels else None,
|
|
92
|
+
}
|
|
93
|
+
data.update({key: value for key, value in optional_payload.items() if value is not None})
|
|
94
|
+
|
|
95
|
+
files = None
|
|
96
|
+
if plugin_file:
|
|
97
|
+
file_path = Path(plugin_file)
|
|
98
|
+
if not file_path.exists():
|
|
99
|
+
raise FileNotFoundError(f'Plugin release file not found: {file_path}')
|
|
100
|
+
files = {'file': file_path}
|
|
101
|
+
post_kwargs = {'data': data}
|
|
102
|
+
if files:
|
|
103
|
+
post_kwargs['files'] = files
|
|
104
|
+
|
|
105
|
+
return self._post('containers/', **post_kwargs)
|
|
106
|
+
|
|
107
|
+
@staticmethod
|
|
108
|
+
def _serialize_plugin_release(plugin_release: Union[str, Any]) -> Dict[str, Any]:
|
|
109
|
+
"""Normalize plugin release data for API payloads."""
|
|
110
|
+
if hasattr(plugin_release, 'code') and hasattr(plugin_release, 'version'):
|
|
111
|
+
payload = {
|
|
112
|
+
'plugin_release': plugin_release.code,
|
|
113
|
+
'plugin': getattr(plugin_release, 'plugin', None),
|
|
114
|
+
'version': plugin_release.version,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
# Extract action and entrypoint from the first action in the config
|
|
118
|
+
if hasattr(plugin_release, 'config') and 'actions' in plugin_release.config:
|
|
119
|
+
actions = plugin_release.config['actions']
|
|
120
|
+
if actions:
|
|
121
|
+
# Get the first action (typically 'gradio')
|
|
122
|
+
action_name = next(iter(actions.keys()))
|
|
123
|
+
action_config = actions[action_name]
|
|
124
|
+
payload['action'] = action_name
|
|
125
|
+
|
|
126
|
+
# Convert entrypoint from dotted path to file path
|
|
127
|
+
if 'entrypoint' in action_config:
|
|
128
|
+
entrypoint = action_config['entrypoint']
|
|
129
|
+
# Convert 'plugin.gradio_interface.app' to 'plugin/gradio_interface.py'
|
|
130
|
+
file_path = entrypoint.rsplit('.', 1)[0].replace('.', '/') + '.py'
|
|
131
|
+
payload['entrypoint'] = file_path
|
|
132
|
+
|
|
133
|
+
return payload
|
|
134
|
+
|
|
135
|
+
if isinstance(plugin_release, str):
|
|
136
|
+
payload = {'plugin_release': plugin_release}
|
|
137
|
+
if '@' in plugin_release:
|
|
138
|
+
plugin, version = plugin_release.rsplit('@', 1)
|
|
139
|
+
payload.setdefault('plugin', plugin)
|
|
140
|
+
payload.setdefault('version', version)
|
|
141
|
+
return payload
|
|
142
|
+
|
|
143
|
+
raise TypeError('plugin_release must be a PluginRelease instance or a formatted string "code@version"')
|