sycommon-python-lib 0.2.12a7__py3-none-any.whl → 0.2.12a9__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/config/Config.py +10 -1
- sycommon/devchat/_page.py +55 -5
- sycommon/devchat/handler.py +49 -4
- sycommon/devchat/widget.js +231 -0
- sycommon/synacos/nacos_service.py +49 -1
- {sycommon_python_lib-0.2.12a7.dist-info → sycommon_python_lib-0.2.12a9.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.2.12a7.dist-info → sycommon_python_lib-0.2.12a9.dist-info}/RECORD +10 -9
- {sycommon_python_lib-0.2.12a7.dist-info → sycommon_python_lib-0.2.12a9.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.12a7.dist-info → sycommon_python_lib-0.2.12a9.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.12a7.dist-info → sycommon_python_lib-0.2.12a9.dist-info}/top_level.txt +0 -0
sycommon/config/Config.py
CHANGED
|
@@ -122,11 +122,20 @@ class Config(metaclass=SingletonMeta):
|
|
|
122
122
|
def get_namespace_id(self, default=''):
|
|
123
123
|
"""获取当前环境标识(Nacos namespaceId,如 'prod'/'wsuat1'/'dev')。
|
|
124
124
|
|
|
125
|
+
键大小写不敏感(对齐 synacos._normalize_nacos_section 的归一:
|
|
126
|
+
兼容大写驼峰 NamespaceId 写法)。
|
|
127
|
+
|
|
125
128
|
Args:
|
|
126
129
|
default: 配置缺失时的默认值,按调用场景传入:
|
|
127
130
|
告警展示用 '未知环境',功能判断用 'dev'/'prod',缓存哨兵用 None。
|
|
128
131
|
"""
|
|
129
|
-
|
|
132
|
+
section = self.config.get('Nacos', {})
|
|
133
|
+
if not isinstance(section, dict):
|
|
134
|
+
return default
|
|
135
|
+
for k, v in section.items():
|
|
136
|
+
if isinstance(k, str) and k.lower() == 'namespaceid':
|
|
137
|
+
return v
|
|
138
|
+
return default
|
|
130
139
|
|
|
131
140
|
def get_tenant_id(self):
|
|
132
141
|
"""根据当前环境推导系统默认租户 ID。
|
sycommon/devchat/_page.py
CHANGED
|
@@ -1,11 +1,61 @@
|
|
|
1
|
-
"""devchat._page —
|
|
1
|
+
"""devchat._page — 自带前端页面壳(Go 版 web/devchat 同源方案)。
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
React 全页壳:引同源 ``/devchat/widget.js`` 以 standalone 全页形态自动
|
|
4
|
+
挂载(原生 JS 兜底页 ``_page.html`` 已退役,直开页面 = React 同一产物)。
|
|
5
|
+
``widget.js`` 缺席时降级为可见提示,不静默白屏。
|
|
6
|
+
|
|
7
|
+
改动请同步两库(此文件是 Go 侧 widgetShellHTML 的同源拷贝)。
|
|
5
8
|
"""
|
|
6
9
|
|
|
7
10
|
from pathlib import Path
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
# widget.js(Go 侧 web/devchat/ui 构建产物)。尝试两个位置:
|
|
13
|
+
# 1. 包内 devchat/widget.js(随包分发的拷贝,pyproject package-data);
|
|
14
|
+
# 2. Go 仓 web/devchat/assets/widget.js(开发机同 checkout 联调,免拷贝)。
|
|
15
|
+
_WJS_CANDIDATES = [
|
|
16
|
+
Path(__file__).parent / "widget.js",
|
|
17
|
+
Path(__file__).resolve().parents[5] / "sycommon-go-lib" / "web" / "devchat" / "assets" / "widget.js",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def widget_js() -> bytes:
|
|
22
|
+
"""React bundle 内容;缺席(<1KB 视为占位)返回 None。"""
|
|
23
|
+
for p in _WJS_CANDIDATES:
|
|
24
|
+
try:
|
|
25
|
+
if p.is_file() and p.stat().st_size >= 1000:
|
|
26
|
+
return p.read_bytes()
|
|
27
|
+
except OSError:
|
|
28
|
+
continue
|
|
29
|
+
return b""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# React 全页壳(与 Go widgetShellHTML 同源;script 引用相对路径 ./widget.js)。
|
|
33
|
+
DEVCHAT_SHELL_HTML = """<!DOCTYPE html>
|
|
34
|
+
<html lang="zh-CN">
|
|
35
|
+
<head>
|
|
36
|
+
<meta charset="UTF-8">
|
|
37
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
38
|
+
<title>DevChat 开发协作</title>
|
|
39
|
+
<style>
|
|
40
|
+
html, body { height: 100%; margin: 0; font-family: 'PingFang SC', 'Microsoft YaHei', 'Segoe UI', sans-serif; }
|
|
41
|
+
.dc-boot { display: flex; flex-direction: column; gap: 8px; align-items: center; justify-content: center; height: 100vh; color: #8a919c; }
|
|
42
|
+
</style>
|
|
43
|
+
</head>
|
|
44
|
+
<body>
|
|
45
|
+
<div class="dc-boot"><div id="dc-boot-hint">DevChat 加载中…</div></div>
|
|
46
|
+
<script src="./widget.js" data-devchat-auto="standalone"></script>
|
|
47
|
+
<script>
|
|
48
|
+
// widget.js 加载失败(网络/服务异常)时给可见提示,不静默白屏。
|
|
49
|
+
if (!window.DevChatWidget) {
|
|
50
|
+
document.getElementById('dc-boot-hint').textContent =
|
|
51
|
+
'DevChat 加载失败:widget.js 不可用,请刷新重试或联系管理员';
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
54
|
+
</body>
|
|
55
|
+
</html>
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
# 零依赖兜底页(原生 JS)—— 仅在 widget.js 缺席时 page 端点保底返回。
|
|
59
|
+
DEVCHAT_FALLBACK_HTML = (Path(__file__).parent / "_page.html").read_text(encoding="utf-8")
|
|
10
60
|
|
|
11
|
-
__all__ = ["
|
|
61
|
+
__all__ = ["DEVCHAT_SHELL_HTML", "DEVCHAT_FALLBACK_HTML", "widget_js"]
|
sycommon/devchat/handler.py
CHANGED
|
@@ -40,7 +40,7 @@ from dataclasses import dataclass, field
|
|
|
40
40
|
from typing import Any, Awaitable, Callable, Dict, List, Optional
|
|
41
41
|
|
|
42
42
|
from fastapi import FastAPI, APIRouter, Request
|
|
43
|
-
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
|
|
43
|
+
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, Response, StreamingResponse
|
|
44
44
|
|
|
45
45
|
from sycommon.logging.kafka_log import SYLogger
|
|
46
46
|
|
|
@@ -58,8 +58,9 @@ from .login import (
|
|
|
58
58
|
new_login_token,
|
|
59
59
|
)
|
|
60
60
|
|
|
61
|
-
#
|
|
62
|
-
|
|
61
|
+
# 自带前端(Go 版同源方案):React 全页壳优先(widget.js 已构建),
|
|
62
|
+
# 缺席时零依赖原生兜底页保底。
|
|
63
|
+
from ._page import DEVCHAT_FALLBACK_HTML, DEVCHAT_SHELL_HTML, widget_js
|
|
63
64
|
|
|
64
65
|
# ─────────────────────────── 配置 ───────────────────────────
|
|
65
66
|
|
|
@@ -209,6 +210,8 @@ class DevchatState:
|
|
|
209
210
|
self._captured_host = ""
|
|
210
211
|
self._fallback_host = ""
|
|
211
212
|
self._fallback_port = 0
|
|
213
|
+
# 优雅退出等待者(wait_idle 注册,release 唤醒;与 Go hub.idleWaiters 对齐)。
|
|
214
|
+
self._idle_waiters: List[asyncio.Future] = []
|
|
212
215
|
|
|
213
216
|
# ---- 宿主地址(与 Go 版 Devchat.serviceBase 同语义) ----
|
|
214
217
|
|
|
@@ -297,6 +300,33 @@ class DevchatState:
|
|
|
297
300
|
def release(self, status: str) -> None:
|
|
298
301
|
self.running = False
|
|
299
302
|
self.last_status = status
|
|
303
|
+
# 优雅退出等待者:任务落地即唤醒(与 Go hub.releaseRunning 同语义)。
|
|
304
|
+
waiters, self._idle_waiters = self._idle_waiters, []
|
|
305
|
+
for fut in waiters:
|
|
306
|
+
if not fut.done():
|
|
307
|
+
fut.set_result(True)
|
|
308
|
+
|
|
309
|
+
async def wait_idle(self, timeout: float) -> bool:
|
|
310
|
+
"""优雅退出等待:在飞任务落地(终态落库/消息回流完)返回 True;
|
|
311
|
+
空闲立即 True;超时返回 False。宿主挂 lifespan/shutdown 钩子用,
|
|
312
|
+
防止换版重启把在飞 claude 任务杀成「服务重启,任务中断」。
|
|
313
|
+
(与 Go Devchat.WaitIdle 对齐。)
|
|
314
|
+
"""
|
|
315
|
+
if not self.running:
|
|
316
|
+
return True
|
|
317
|
+
if timeout <= 0:
|
|
318
|
+
return False
|
|
319
|
+
loop = asyncio.get_running_loop()
|
|
320
|
+
fut: asyncio.Future = loop.create_future()
|
|
321
|
+
self._idle_waiters.append(fut)
|
|
322
|
+
try:
|
|
323
|
+
await asyncio.wait_for(fut, timeout)
|
|
324
|
+
return True
|
|
325
|
+
except asyncio.TimeoutError:
|
|
326
|
+
# 摘除自己(release 时不再触碰已弃用 future)。
|
|
327
|
+
with contextlib.suppress(ValueError):
|
|
328
|
+
self._idle_waiters.remove(fut)
|
|
329
|
+
return False
|
|
300
330
|
|
|
301
331
|
|
|
302
332
|
# _shared_conns 同一 devchat.db 路径 → 唯一连接(任务/消息/事件/会话
|
|
@@ -789,7 +819,22 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
789
819
|
# ---- 页面 ----
|
|
790
820
|
@router.get("/", include_in_schema=False)
|
|
791
821
|
async def page(request: Request):
|
|
792
|
-
|
|
822
|
+
html = DEVCHAT_SHELL_HTML if widget_js() else DEVCHAT_FALLBACK_HTML
|
|
823
|
+
return HTMLResponse(content=html, headers=_cors_headers(request))
|
|
824
|
+
|
|
825
|
+
@router.get("/widget.js", include_in_schema=False)
|
|
826
|
+
async def widgetjs(request: Request):
|
|
827
|
+
b = widget_js()
|
|
828
|
+
if not b:
|
|
829
|
+
return PlainTextResponse(
|
|
830
|
+
"// DevChat widget not built; open /devchat/ for the builtin page",
|
|
831
|
+
status_code=404,
|
|
832
|
+
)
|
|
833
|
+
return Response(
|
|
834
|
+
content=b,
|
|
835
|
+
media_type="application/javascript; charset=utf-8",
|
|
836
|
+
headers={"Cache-Control": "no-cache", **_cors_headers(request)},
|
|
837
|
+
)
|
|
793
838
|
|
|
794
839
|
@router.get("/messages")
|
|
795
840
|
async def messages(request: Request, limit: int = 200):
|