sycommon-python-lib 0.2.12a2__py3-none-any.whl → 0.2.12a4__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.
- sycommon/devchat/handler.py +96 -2
- {sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/RECORD +6 -6
- {sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/top_level.txt +0 -0
sycommon/devchat/handler.py
CHANGED
|
@@ -28,10 +28,12 @@ import asyncio
|
|
|
28
28
|
import contextlib
|
|
29
29
|
import json
|
|
30
30
|
import os
|
|
31
|
+
import re
|
|
31
32
|
import shutil
|
|
32
33
|
import signal
|
|
33
34
|
import subprocess
|
|
34
35
|
import sys
|
|
36
|
+
import threading
|
|
35
37
|
import time
|
|
36
38
|
import uuid
|
|
37
39
|
from dataclasses import dataclass, field
|
|
@@ -188,6 +190,49 @@ class DevchatState:
|
|
|
188
190
|
self.events = _open_event_store(cfg)
|
|
189
191
|
# 历史会话列表口(「以后可恢复」);None = 端点回空。
|
|
190
192
|
self.session_archive = _open_session_archive(cfg)
|
|
193
|
+
# 宿主地址(审批 MCP 回调用):面板请求探测值 > app.yaml 兜底。
|
|
194
|
+
self._host_lock = threading.Lock()
|
|
195
|
+
self._captured_host = ""
|
|
196
|
+
self._fallback_host = ""
|
|
197
|
+
self._fallback_port = 0
|
|
198
|
+
|
|
199
|
+
# ---- 宿主地址(与 Go 版 Devchat.serviceBase 同语义) ----
|
|
200
|
+
|
|
201
|
+
def set_fallback_addr(self, host: str, port: int) -> None:
|
|
202
|
+
"""服务自身地址(app.yaml Host/Port,setup 时注入)。"""
|
|
203
|
+
with self._host_lock:
|
|
204
|
+
self._fallback_host = host or ""
|
|
205
|
+
self._fallback_port = int(port or 0)
|
|
206
|
+
|
|
207
|
+
def capture_host(self, host: str) -> None:
|
|
208
|
+
"""面板请求真实 Host:port(仅首次记录,浏览器怎么连就怎么回调)。"""
|
|
209
|
+
if not host:
|
|
210
|
+
return
|
|
211
|
+
with self._host_lock:
|
|
212
|
+
if not self._captured_host:
|
|
213
|
+
self._captured_host = host
|
|
214
|
+
|
|
215
|
+
@staticmethod
|
|
216
|
+
def _normalize(host_port: str) -> str:
|
|
217
|
+
"""监听形态(0.0.0.0/::/空)归一回环;具体 IP 原样。"""
|
|
218
|
+
host, _, port = host_port.rpartition(":")
|
|
219
|
+
if not host or host in ("0.0.0.0", "::"):
|
|
220
|
+
host = "127.0.0.1"
|
|
221
|
+
return f"{host}:{port}"
|
|
222
|
+
|
|
223
|
+
def service_base(self) -> str:
|
|
224
|
+
with self._host_lock:
|
|
225
|
+
captured = self._captured_host
|
|
226
|
+
fb_host = self._fallback_host
|
|
227
|
+
fb_port = self._fallback_port
|
|
228
|
+
if captured:
|
|
229
|
+
return "http://" + self._normalize(captured)
|
|
230
|
+
if fb_port > 0:
|
|
231
|
+
return "http://" + self._normalize(f"{fb_host or '127.0.0.1'}:{fb_port}")
|
|
232
|
+
port = os.environ.get("PORT", "")
|
|
233
|
+
if port:
|
|
234
|
+
return f"http://127.0.0.1:{port}"
|
|
235
|
+
return "http://127.0.0.1:8080"
|
|
191
236
|
|
|
192
237
|
def _open_message_store(self) -> None:
|
|
193
238
|
from .store import open_message_store
|
|
@@ -298,10 +343,16 @@ async def _drive_claude(st: DevchatState, prompt: str, seq: int, task_id: str) -
|
|
|
298
343
|
reply_parts: List[str] = []
|
|
299
344
|
got_session_id = ""
|
|
300
345
|
final_status = "success"
|
|
346
|
+
# 宿主地址(探测 > app.yaml 兜底)注入子环境(审批链路预留,与 Go 版
|
|
347
|
+
# 同语义;当前 py 版仍 skip-permissions,环境变量仅占位不生效)。
|
|
348
|
+
env = dict(os.environ)
|
|
349
|
+
env["DEVCHAT_BASE_URL"] = st.service_base() + st.cfg.basePath
|
|
350
|
+
env["DEVCHAT_TASK_ID"] = task_id
|
|
351
|
+
env["DEVCHAT_SESSION_ID"] = resume_sid
|
|
301
352
|
|
|
302
353
|
try:
|
|
303
354
|
proc = await asyncio.create_subprocess_exec(
|
|
304
|
-
*args, cwd=cwd,
|
|
355
|
+
*args, cwd=cwd, env=env,
|
|
305
356
|
stdout=asyncio.subprocess.PIPE,
|
|
306
357
|
stderr=asyncio.subprocess.DEVNULL,
|
|
307
358
|
stdin=asyncio.subprocess.DEVNULL,
|
|
@@ -469,7 +520,6 @@ def _default_restart_self() -> str:
|
|
|
469
520
|
with contextlib.suppress(Exception):
|
|
470
521
|
os.kill(old_pid, signal.SIGTERM)
|
|
471
522
|
|
|
472
|
-
import threading
|
|
473
523
|
threading.Thread(target=_self_terminate, daemon=True).start()
|
|
474
524
|
return f"重启已安排(新日志: {log_path})"
|
|
475
525
|
|
|
@@ -513,8 +563,18 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
513
563
|
cfg.projectRoot = os.getcwd()
|
|
514
564
|
st = DevchatState(cfg)
|
|
515
565
|
|
|
566
|
+
# 服务自身地址(app.yaml 的 Host/Port):审批 MCP 回调的兜底 —
|
|
567
|
+
# 面板请求未探测到真实 Host 前按它回环调用(与 Go 版同语义)。
|
|
568
|
+
with contextlib.suppress(Exception):
|
|
569
|
+
from sycommon.config.config import Config
|
|
570
|
+
st.set_fallback_addr(getattr(Config, "Host", ""), int(getattr(Config, "Port", 0) or 0))
|
|
571
|
+
|
|
516
572
|
router = APIRouter(prefix=cfg.basePath, tags=["Devchat"])
|
|
517
573
|
|
|
574
|
+
def _capture_host(request: Request) -> None:
|
|
575
|
+
"""浏览器实际连接的 host:port = 宿主对外地址(仅首次记录)。"""
|
|
576
|
+
st.capture_host(request.headers.get("host", ""))
|
|
577
|
+
|
|
518
578
|
# ---- 身份 ----
|
|
519
579
|
async def _identity(request: Request):
|
|
520
580
|
if cfg.identityFunc is not None:
|
|
@@ -546,6 +606,7 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
546
606
|
ident = await _identity(request)
|
|
547
607
|
if ident is None:
|
|
548
608
|
return JSONResponse({"detail": "未登录或无权访问 DevChat"}, status_code=401)
|
|
609
|
+
_capture_host(request)
|
|
549
610
|
msgs = await st.store.recent(limit)
|
|
550
611
|
return {
|
|
551
612
|
"messages": [m.to_dict() for m in msgs],
|
|
@@ -660,11 +721,44 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
660
721
|
"current": st.session_id}
|
|
661
722
|
return {"sessions": [], "current": st.session_id}
|
|
662
723
|
|
|
724
|
+
@router.post("/upload")
|
|
725
|
+
async def upload(request: Request):
|
|
726
|
+
"""附件上传(文件/图片):落 {projectRoot}/.devchat/uploads/,
|
|
727
|
+
返回相对路径;消息文本引用路径,claude 用 Read 直接查看
|
|
728
|
+
(文本与图片多模态都支持)。与 Go 版同契约。"""
|
|
729
|
+
ident = await _identity(request)
|
|
730
|
+
if ident is None:
|
|
731
|
+
return JSONResponse({"detail": "未登录或无权访问 DevChat"}, status_code=401)
|
|
732
|
+
try:
|
|
733
|
+
form = await request.form()
|
|
734
|
+
up = form.get("file")
|
|
735
|
+
if up is None or not hasattr(up, "filename"):
|
|
736
|
+
return JSONResponse({"detail": "缺少 file 字段(multipart)"}, status_code=400)
|
|
737
|
+
data = await up.read()
|
|
738
|
+
if len(data) > 16 << 20:
|
|
739
|
+
return JSONResponse(
|
|
740
|
+
{"detail": f"文件过大({len(data) >> 20} MB),上限 16MB"},
|
|
741
|
+
status_code=413)
|
|
742
|
+
root = cfg.projectRoot or os.getcwd()
|
|
743
|
+
dir_path = os.path.join(root, ".devchat", "uploads")
|
|
744
|
+
os.makedirs(dir_path, exist_ok=True)
|
|
745
|
+
# 文件名清洗:取 base + 危险字符替换(路径穿越防御)。
|
|
746
|
+
safe = re.sub(r"[^\w.\-一-鿿]", "_",
|
|
747
|
+
os.path.basename(up.filename or "file")) or "file"
|
|
748
|
+
name = f"{time.time_ns()}-{safe[-80:]}"
|
|
749
|
+
with open(os.path.join(dir_path, name), "wb") as f:
|
|
750
|
+
f.write(data)
|
|
751
|
+
return {"path": f".devchat/uploads/{name}",
|
|
752
|
+
"name": up.filename, "size": len(data)}
|
|
753
|
+
except Exception as exc: # noqa: BLE001
|
|
754
|
+
return JSONResponse({"detail": f"上传失败: {exc}"}, status_code=500)
|
|
755
|
+
|
|
663
756
|
@router.get("/status")
|
|
664
757
|
async def status(request: Request):
|
|
665
758
|
ident = await _identity(request)
|
|
666
759
|
if ident is None:
|
|
667
760
|
return JSONResponse({"detail": "未登录或无权访问 DevChat"}, status_code=401)
|
|
761
|
+
_capture_host(request)
|
|
668
762
|
uid, _ = ident
|
|
669
763
|
return {
|
|
670
764
|
"running": st.running,
|
|
@@ -216,7 +216,7 @@ sycommon/database/token_usage_db_service.py,sha256=iYK51ZtGNpBA4nj2IhflltbftE5Fs
|
|
|
216
216
|
sycommon/database/txnref.py,sha256=sOFp85PlanCA0oryVpHu53JbM0aSHAVkELWgEJKalLI,7755
|
|
217
217
|
sycommon/devchat/__init__.py,sha256=Z2BgzDKrp_mR165d3a1kftP8lf1-WKz_5xuZMFXR-iw,2288
|
|
218
218
|
sycommon/devchat/_page.py,sha256=JzTX7Wv_4-nJky7wg5E44yftSQYvC9L9_YsQzNnaWhI,406
|
|
219
|
-
sycommon/devchat/handler.py,sha256=
|
|
219
|
+
sycommon/devchat/handler.py,sha256=cWKIDbAxYEMbk4ZeMvOxjNU-jJjv6b0dLGMmguJ2eG0,34609
|
|
220
220
|
sycommon/devchat/store.py,sha256=leNJunuCjS8t4dTrCxYooBO8EQl69dtAyV-2HHlJ_uM,3957
|
|
221
221
|
sycommon/devchat/tasks.py,sha256=hh4Gv04bkI_-MP-OCfKG09NEZXuURz-i2iAARTeEdZI,11688
|
|
222
222
|
sycommon/health/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -401,8 +401,8 @@ sycommon/tools/usercache.py,sha256=Zu2oiXISUOojleEYCAS9t06ZzgWSNHCIhCjv4yTtkRw,4
|
|
|
401
401
|
sycommon/xxljob/__init__.py,sha256=7eoBlQxv-B39IfRSCY2bkqdGYs1QRe1umAWd88VMEEM,86
|
|
402
402
|
sycommon/xxljob/sharding.py,sha256=LJ0tle7qSUS32S5ntjzri1aOcPzwL5CFde3W4Rf1AUE,2560
|
|
403
403
|
sycommon/xxljob/xxljob_service.py,sha256=1yifwIBNGsCIxLnQjHKiBlbsigc_zvPH-dMTZcNxe-Q,7649
|
|
404
|
-
sycommon_python_lib-0.2.
|
|
405
|
-
sycommon_python_lib-0.2.
|
|
406
|
-
sycommon_python_lib-0.2.
|
|
407
|
-
sycommon_python_lib-0.2.
|
|
408
|
-
sycommon_python_lib-0.2.
|
|
404
|
+
sycommon_python_lib-0.2.12a4.dist-info/METADATA,sha256=suTUXtXFAd_WHoQSKD5Mde_6h9nzYHPlLrALGE-JSJE,8028
|
|
405
|
+
sycommon_python_lib-0.2.12a4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
406
|
+
sycommon_python_lib-0.2.12a4.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
|
|
407
|
+
sycommon_python_lib-0.2.12a4.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
|
|
408
|
+
sycommon_python_lib-0.2.12a4.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{sycommon_python_lib-0.2.12a2.dist-info → sycommon_python_lib-0.2.12a4.dist-info}/top_level.txt
RENAMED
|
File without changes
|