sycommon-python-lib 0.2.12a7__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 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
- return self.config.get('Nacos', {}).get('namespaceId', default)
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 — 自带前端浮窗页面(与 Go 版 web/devchat/assets/index.html 同源)。
1
+ """devchat._page — 自带前端页面壳(Go 版 web/devchat 同源方案)。
2
2
 
3
- 单文件 HTML,无外部依赖;独立访问 = 全页,``?embed=1`` / iframe 嵌入 =
4
- 悬浮球 + 面板。改动请同步两库(此文件是 Go 侧的同源拷贝)。
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
- DEVCHAT_HTML = (Path(__file__).parent / "_page.html").read_text(encoding="utf-8")
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__ = ["DEVCHAT_HTML"]
61
+ __all__ = ["DEVCHAT_SHELL_HTML", "DEVCHAT_FALLBACK_HTML", "widget_js"]
@@ -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
- # 自带前端页面(与 Go 版同源同样式,单文件无外部依赖)。
62
- from ._page import DEVCHAT_HTML
61
+ # 自带前端(Go 版同源方案):React 全页壳优先(widget.js 已构建),
62
+ # 缺席时零依赖原生兜底页保底。
63
+ from ._page import DEVCHAT_FALLBACK_HTML, DEVCHAT_SHELL_HTML, widget_js
63
64
 
64
65
  # ─────────────────────────── 配置 ───────────────────────────
65
66
 
@@ -789,7 +790,22 @@ def setup_devchat_handler(app: FastAPI, config: Optional[dict] = None) -> FastAP
789
790
  # ---- 页面 ----
790
791
  @router.get("/", include_in_schema=False)
791
792
  async def page(request: Request):
792
- return HTMLResponse(content=DEVCHAT_HTML, headers=_cors_headers(request))
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
+ )
793
809
 
794
810
  @router.get("/messages")
795
811
  async def messages(request: Request, limit: int = 200):