sycommon-python-lib 0.2.12a6__py3-none-any.whl → 0.2.12a8__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 +39 -4
- sycommon/devchat/widget.js +231 -0
- sycommon/synacos/nacos_service.py +49 -1
- {sycommon_python_lib-0.2.12a6.dist-info → sycommon_python_lib-0.2.12a8.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.2.12a6.dist-info → sycommon_python_lib-0.2.12a8.dist-info}/RECORD +10 -9
- {sycommon_python_lib-0.2.12a6.dist-info → sycommon_python_lib-0.2.12a8.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.2.12a6.dist-info → sycommon_python_lib-0.2.12a8.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.2.12a6.dist-info → sycommon_python_lib-0.2.12a8.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
|
|
|
@@ -645,6 +646,25 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
645
646
|
if n:
|
|
646
647
|
SYLogger.info(f"[Devchat] 启动恢复:{n} 条中断任务归为 error")
|
|
647
648
|
|
|
649
|
+
# 自动恢复:重启时库里还有 queued 存量 → 立即接力队首开跑(否则队列
|
|
650
|
+
# 要等下一条新消息进来才动,用户看到的就是「没有自动恢复」)。
|
|
651
|
+
# 与 Go 版 resumeQueued 同语义:等价于把 promote_next_queued 提前到
|
|
652
|
+
# 挂载时跑一次。挂载可能发生在事件循环起来之前 — 用 app startup
|
|
653
|
+
# 事件驱动,保证 _drive_claude 的 create_task 有活着的 loop。
|
|
654
|
+
with contextlib.suppress(Exception):
|
|
655
|
+
pending = 0
|
|
656
|
+
if st.tasks is not None:
|
|
657
|
+
pending = 1 if st.tasks.oldest_queued() is not None else 0
|
|
658
|
+
if pending:
|
|
659
|
+
SYLogger.info("[Devchat] 自动恢复:发现排队任务,服务就绪后接力启动")
|
|
660
|
+
|
|
661
|
+
async def _resume_queue() -> None:
|
|
662
|
+
await _promote_next_queued(st)
|
|
663
|
+
|
|
664
|
+
@app.on_event("startup")
|
|
665
|
+
async def _resume_on_startup() -> None: # noqa: W0212
|
|
666
|
+
asyncio.ensure_future(_resume_queue())
|
|
667
|
+
|
|
648
668
|
# 服务自身地址(app.yaml 的 Host/Port):审批 MCP 回调的兜底 —
|
|
649
669
|
# 面板请求未探测到真实 Host 前按它回环调用(与 Go 版同语义)。
|
|
650
670
|
# 配置源:宿主 Services 已加载时读其 _config dict(app.yaml 顶层键);
|
|
@@ -770,7 +790,22 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
|
|
|
770
790
|
# ---- 页面 ----
|
|
771
791
|
@router.get("/", include_in_schema=False)
|
|
772
792
|
async def page(request: Request):
|
|
773
|
-
|
|
793
|
+
html = DEVCHAT_SHELL_HTML if widget_js() else DEVCHAT_FALLBACK_HTML
|
|
794
|
+
return HTMLResponse(content=html, headers=_cors_headers(request))
|
|
795
|
+
|
|
796
|
+
@router.get("/widget.js", include_in_schema=False)
|
|
797
|
+
async def widgetjs(request: Request):
|
|
798
|
+
b = widget_js()
|
|
799
|
+
if not b:
|
|
800
|
+
return PlainTextResponse(
|
|
801
|
+
"// DevChat widget not built; open /devchat/ for the builtin page",
|
|
802
|
+
status_code=404,
|
|
803
|
+
)
|
|
804
|
+
return Response(
|
|
805
|
+
content=b,
|
|
806
|
+
media_type="application/javascript; charset=utf-8",
|
|
807
|
+
headers={"Cache-Control": "no-cache", **_cors_headers(request)},
|
|
808
|
+
)
|
|
774
809
|
|
|
775
810
|
@router.get("/messages")
|
|
776
811
|
async def messages(request: Request, limit: int = 200):
|