sycommon-python-lib 0.2.12a3__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.
@@ -33,6 +33,7 @@ import shutil
33
33
  import signal
34
34
  import subprocess
35
35
  import sys
36
+ import threading
36
37
  import time
37
38
  import uuid
38
39
  from dataclasses import dataclass, field
@@ -189,6 +190,49 @@ class DevchatState:
189
190
  self.events = _open_event_store(cfg)
190
191
  # 历史会话列表口(「以后可恢复」);None = 端点回空。
191
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"
192
236
 
193
237
  def _open_message_store(self) -> None:
194
238
  from .store import open_message_store
@@ -299,10 +343,16 @@ async def _drive_claude(st: DevchatState, prompt: str, seq: int, task_id: str) -
299
343
  reply_parts: List[str] = []
300
344
  got_session_id = ""
301
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
302
352
 
303
353
  try:
304
354
  proc = await asyncio.create_subprocess_exec(
305
- *args, cwd=cwd,
355
+ *args, cwd=cwd, env=env,
306
356
  stdout=asyncio.subprocess.PIPE,
307
357
  stderr=asyncio.subprocess.DEVNULL,
308
358
  stdin=asyncio.subprocess.DEVNULL,
@@ -470,7 +520,6 @@ def _default_restart_self() -> str:
470
520
  with contextlib.suppress(Exception):
471
521
  os.kill(old_pid, signal.SIGTERM)
472
522
 
473
- import threading
474
523
  threading.Thread(target=_self_terminate, daemon=True).start()
475
524
  return f"重启已安排(新日志: {log_path})"
476
525
 
@@ -514,8 +563,18 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
514
563
  cfg.projectRoot = os.getcwd()
515
564
  st = DevchatState(cfg)
516
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
+
517
572
  router = APIRouter(prefix=cfg.basePath, tags=["Devchat"])
518
573
 
574
+ def _capture_host(request: Request) -> None:
575
+ """浏览器实际连接的 host:port = 宿主对外地址(仅首次记录)。"""
576
+ st.capture_host(request.headers.get("host", ""))
577
+
519
578
  # ---- 身份 ----
520
579
  async def _identity(request: Request):
521
580
  if cfg.identityFunc is not None:
@@ -547,6 +606,7 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
547
606
  ident = await _identity(request)
548
607
  if ident is None:
549
608
  return JSONResponse({"detail": "未登录或无权访问 DevChat"}, status_code=401)
609
+ _capture_host(request)
550
610
  msgs = await st.store.recent(limit)
551
611
  return {
552
612
  "messages": [m.to_dict() for m in msgs],
@@ -698,6 +758,7 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
698
758
  ident = await _identity(request)
699
759
  if ident is None:
700
760
  return JSONResponse({"detail": "未登录或无权访问 DevChat"}, status_code=401)
761
+ _capture_host(request)
701
762
  uid, _ = ident
702
763
  return {
703
764
  "running": st.running,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.2.12a3
3
+ Version: 0.2.12a4
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
@@ -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=ix_nU_8vllmt2wHhEMMFRwwrwraWSAGZ6f3gK7NHiBg,31957
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.12a3.dist-info/METADATA,sha256=rYJx3foOincQsDexTV4e2miEJ9dYwGVo7AwTLqs2ayA,8028
405
- sycommon_python_lib-0.2.12a3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
406
- sycommon_python_lib-0.2.12a3.dist-info/entry_points.txt,sha256=gsR4SssKxDWjRU8ggidzNcdMXDPRSKRS7UaGyNP84Qg,92
407
- sycommon_python_lib-0.2.12a3.dist-info/top_level.txt,sha256=RgphKrg7nJyZ7irJqbxFr-5H2LUYTvI7ivoWZH2hcD0,29
408
- sycommon_python_lib-0.2.12a3.dist-info/RECORD,,
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,,