mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 21:57:14 +08:00
feat(channels): add multi-channel management UI with real-time connect/disconnect
- Web console Channels page: display active channels as config cards, support save/connect/disconnect with real-time start/stop of channel processes - Custom dropdown for channel selection (consistent with model selector style), custom confirmation dialog for disconnect - Fix channel stop: use sys.modules['__main__'] to access live ChannelManager - Fix web request pending: move stop logic outside lock, set daemon_threads=True - Fix reconnect: new asyncio event loop per startup, ctypes thread interrupt, 5s grace period before re-establishing remote connection - Filter stale offline messages (>60s) pushed after reconnect
This commit is contained in:
@@ -304,6 +304,8 @@ class WebChannel(ChatChannel):
|
||||
'/stream', 'StreamHandler',
|
||||
'/chat', 'ChatHandler',
|
||||
'/config', 'ConfigHandler',
|
||||
'/api/channels', 'ChannelsHandler',
|
||||
'/api/tools', 'ToolsHandler',
|
||||
'/api/skills', 'SkillsHandler',
|
||||
'/api/memory', 'MemoryHandler',
|
||||
'/api/memory/content', 'MemoryContentHandler',
|
||||
@@ -325,6 +327,8 @@ class WebChannel(ChatChannel):
|
||||
func = web.httpserver.StaticMiddleware(app.wsgifunc())
|
||||
func = web.httpserver.LogMiddleware(func)
|
||||
server = web.httpserver.WSGIServer(("0.0.0.0", port), func)
|
||||
# Allow concurrent requests by not blocking on in-flight handler threads
|
||||
server.daemon_threads = True
|
||||
self._http_server = server
|
||||
try:
|
||||
server.start()
|
||||
@@ -572,6 +576,323 @@ class ConfigHandler:
|
||||
return json.dumps({"status": "error", "message": str(e)})
|
||||
|
||||
|
||||
class ChannelsHandler:
|
||||
"""API for managing external channel configurations (feishu, dingtalk, etc)."""
|
||||
|
||||
CHANNEL_DEFS = OrderedDict([
|
||||
("feishu", {
|
||||
"label": {"zh": "飞书", "en": "Feishu"},
|
||||
"icon": "fa-paper-plane",
|
||||
"color": "blue",
|
||||
"fields": [
|
||||
{"key": "feishu_app_id", "label": "App ID", "type": "text"},
|
||||
{"key": "feishu_app_secret", "label": "App Secret", "type": "secret"},
|
||||
{"key": "feishu_token", "label": "Verification Token", "type": "secret"},
|
||||
{"key": "feishu_bot_name", "label": "Bot Name", "type": "text"},
|
||||
],
|
||||
}),
|
||||
("dingtalk", {
|
||||
"label": {"zh": "钉钉", "en": "DingTalk"},
|
||||
"icon": "fa-comments",
|
||||
"color": "blue",
|
||||
"fields": [
|
||||
{"key": "dingtalk_client_id", "label": "Client ID", "type": "text"},
|
||||
{"key": "dingtalk_client_secret", "label": "Client Secret", "type": "secret"},
|
||||
],
|
||||
}),
|
||||
("wechatcom_app", {
|
||||
"label": {"zh": "企微自建应用", "en": "WeCom App"},
|
||||
"icon": "fa-building",
|
||||
"color": "emerald",
|
||||
"fields": [
|
||||
{"key": "wechatcom_corp_id", "label": "Corp ID", "type": "text"},
|
||||
{"key": "wechatcomapp_agent_id", "label": "Agent ID", "type": "text"},
|
||||
{"key": "wechatcomapp_secret", "label": "Secret", "type": "secret"},
|
||||
{"key": "wechatcomapp_token", "label": "Token", "type": "secret"},
|
||||
{"key": "wechatcomapp_aes_key", "label": "AES Key", "type": "secret"},
|
||||
{"key": "wechatcomapp_port", "label": "Port", "type": "number", "default": 9898},
|
||||
],
|
||||
}),
|
||||
("wechatmp", {
|
||||
"label": {"zh": "公众号", "en": "WeChat MP"},
|
||||
"icon": "fa-comment-dots",
|
||||
"color": "emerald",
|
||||
"fields": [
|
||||
{"key": "wechatmp_app_id", "label": "App ID", "type": "text"},
|
||||
{"key": "wechatmp_app_secret", "label": "App Secret", "type": "secret"},
|
||||
{"key": "wechatmp_token", "label": "Token", "type": "secret"},
|
||||
{"key": "wechatmp_aes_key", "label": "AES Key", "type": "secret"},
|
||||
{"key": "wechatmp_port", "label": "Port", "type": "number", "default": 8080},
|
||||
],
|
||||
}),
|
||||
])
|
||||
|
||||
@staticmethod
|
||||
def _mask_secret(value: str) -> str:
|
||||
if not value or len(value) <= 8:
|
||||
return value
|
||||
return value[:4] + "*" * (len(value) - 8) + value[-4:]
|
||||
|
||||
@staticmethod
|
||||
def _parse_channel_list(raw) -> list:
|
||||
if isinstance(raw, list):
|
||||
return [ch.strip() for ch in raw if ch.strip()]
|
||||
if isinstance(raw, str):
|
||||
return [ch.strip() for ch in raw.split(",") if ch.strip()]
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def _active_channel_set(cls) -> set:
|
||||
return set(cls._parse_channel_list(conf().get("channel_type", "")))
|
||||
|
||||
def GET(self):
|
||||
web.header('Content-Type', 'application/json; charset=utf-8')
|
||||
try:
|
||||
local_config = conf()
|
||||
active_channels = self._active_channel_set()
|
||||
channels = []
|
||||
for ch_name, ch_def in self.CHANNEL_DEFS.items():
|
||||
fields_out = []
|
||||
for f in ch_def["fields"]:
|
||||
raw_val = local_config.get(f["key"], f.get("default", ""))
|
||||
if f["type"] == "secret" and raw_val:
|
||||
display_val = self._mask_secret(str(raw_val))
|
||||
else:
|
||||
display_val = raw_val
|
||||
fields_out.append({
|
||||
"key": f["key"],
|
||||
"label": f["label"],
|
||||
"type": f["type"],
|
||||
"value": display_val,
|
||||
"default": f.get("default", ""),
|
||||
})
|
||||
channels.append({
|
||||
"name": ch_name,
|
||||
"label": ch_def["label"],
|
||||
"icon": ch_def["icon"],
|
||||
"color": ch_def["color"],
|
||||
"active": ch_name in active_channels,
|
||||
"fields": fields_out,
|
||||
})
|
||||
return json.dumps({"status": "success", "channels": channels}, ensure_ascii=False)
|
||||
except Exception as e:
|
||||
logger.error(f"[WebChannel] Channels API error: {e}")
|
||||
return json.dumps({"status": "error", "message": str(e)})
|
||||
|
||||
def POST(self):
|
||||
web.header('Content-Type', 'application/json; charset=utf-8')
|
||||
try:
|
||||
body = json.loads(web.data())
|
||||
action = body.get("action")
|
||||
channel_name = body.get("channel")
|
||||
|
||||
if not action or not channel_name:
|
||||
return json.dumps({"status": "error", "message": "action and channel required"})
|
||||
|
||||
if channel_name not in self.CHANNEL_DEFS:
|
||||
return json.dumps({"status": "error", "message": f"unknown channel: {channel_name}"})
|
||||
|
||||
if action == "save":
|
||||
return self._handle_save(channel_name, body.get("config", {}))
|
||||
elif action == "connect":
|
||||
return self._handle_connect(channel_name, body.get("config", {}))
|
||||
elif action == "disconnect":
|
||||
return self._handle_disconnect(channel_name)
|
||||
else:
|
||||
return json.dumps({"status": "error", "message": f"unknown action: {action}"})
|
||||
except Exception as e:
|
||||
logger.error(f"[WebChannel] Channels POST error: {e}")
|
||||
return json.dumps({"status": "error", "message": str(e)})
|
||||
|
||||
def _handle_save(self, channel_name: str, updates: dict):
|
||||
ch_def = self.CHANNEL_DEFS[channel_name]
|
||||
valid_keys = {f["key"] for f in ch_def["fields"]}
|
||||
secret_keys = {f["key"] for f in ch_def["fields"] if f["type"] == "secret"}
|
||||
|
||||
local_config = conf()
|
||||
applied = {}
|
||||
for key, value in updates.items():
|
||||
if key not in valid_keys:
|
||||
continue
|
||||
if key in secret_keys:
|
||||
if not value or (len(value) > 8 and "*" * 4 in value):
|
||||
continue
|
||||
field_def = next((f for f in ch_def["fields"] if f["key"] == key), None)
|
||||
if field_def:
|
||||
if field_def["type"] == "number":
|
||||
value = int(value)
|
||||
elif field_def["type"] == "bool":
|
||||
value = bool(value)
|
||||
local_config[key] = value
|
||||
applied[key] = value
|
||||
|
||||
if not applied:
|
||||
return json.dumps({"status": "error", "message": "no valid fields to update"})
|
||||
|
||||
config_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
|
||||
os.path.abspath(__file__)))), "config.json")
|
||||
if os.path.exists(config_path):
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
file_cfg = json.load(f)
|
||||
else:
|
||||
file_cfg = {}
|
||||
file_cfg.update(applied)
|
||||
with open(config_path, "w", encoding="utf-8") as f:
|
||||
json.dump(file_cfg, f, indent=4, ensure_ascii=False)
|
||||
|
||||
logger.info(f"[WebChannel] Channel '{channel_name}' config updated: {list(applied.keys())}")
|
||||
|
||||
should_restart = False
|
||||
active_channels = self._active_channel_set()
|
||||
if channel_name in active_channels:
|
||||
should_restart = True
|
||||
try:
|
||||
import sys
|
||||
app_module = sys.modules.get('__main__') or sys.modules.get('app')
|
||||
mgr = getattr(app_module, '_channel_mgr', None) if app_module else None
|
||||
if mgr:
|
||||
threading.Thread(
|
||||
target=mgr.restart,
|
||||
args=(channel_name,),
|
||||
daemon=True,
|
||||
).start()
|
||||
logger.info(f"[WebChannel] Channel '{channel_name}' restart triggered")
|
||||
except Exception as e:
|
||||
logger.warning(f"[WebChannel] Failed to restart channel '{channel_name}': {e}")
|
||||
|
||||
return json.dumps({
|
||||
"status": "success",
|
||||
"applied": list(applied.keys()),
|
||||
"restarted": should_restart,
|
||||
}, ensure_ascii=False)
|
||||
|
||||
def _handle_connect(self, channel_name: str, updates: dict):
|
||||
"""Save config fields, add channel to channel_type, and start it."""
|
||||
ch_def = self.CHANNEL_DEFS[channel_name]
|
||||
valid_keys = {f["key"] for f in ch_def["fields"]}
|
||||
secret_keys = {f["key"] for f in ch_def["fields"] if f["type"] == "secret"}
|
||||
|
||||
# Feishu connected via web console must use websocket (long connection) mode
|
||||
if channel_name == "feishu":
|
||||
updates.setdefault("feishu_event_mode", "websocket")
|
||||
valid_keys.add("feishu_event_mode")
|
||||
|
||||
local_config = conf()
|
||||
applied = {}
|
||||
for key, value in updates.items():
|
||||
if key not in valid_keys:
|
||||
continue
|
||||
if key in secret_keys:
|
||||
if not value or (len(value) > 8 and "*" * 4 in value):
|
||||
continue
|
||||
field_def = next((f for f in ch_def["fields"] if f["key"] == key), None)
|
||||
if field_def:
|
||||
if field_def["type"] == "number":
|
||||
value = int(value)
|
||||
elif field_def["type"] == "bool":
|
||||
value = bool(value)
|
||||
local_config[key] = value
|
||||
applied[key] = value
|
||||
|
||||
existing = self._parse_channel_list(conf().get("channel_type", ""))
|
||||
if channel_name not in existing:
|
||||
existing.append(channel_name)
|
||||
new_channel_type = ",".join(existing)
|
||||
local_config["channel_type"] = new_channel_type
|
||||
|
||||
config_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
|
||||
os.path.abspath(__file__)))), "config.json")
|
||||
if os.path.exists(config_path):
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
file_cfg = json.load(f)
|
||||
else:
|
||||
file_cfg = {}
|
||||
file_cfg.update(applied)
|
||||
file_cfg["channel_type"] = new_channel_type
|
||||
with open(config_path, "w", encoding="utf-8") as f:
|
||||
json.dump(file_cfg, f, indent=4, ensure_ascii=False)
|
||||
|
||||
logger.info(f"[WebChannel] Channel '{channel_name}' connecting, channel_type={new_channel_type}")
|
||||
|
||||
def _do_start():
|
||||
try:
|
||||
import sys
|
||||
app_module = sys.modules.get('__main__') or sys.modules.get('app')
|
||||
clear_fn = getattr(app_module, '_clear_singleton_cache', None) if app_module else None
|
||||
mgr = getattr(app_module, '_channel_mgr', None) if app_module else None
|
||||
if mgr is None:
|
||||
logger.warning(f"[WebChannel] ChannelManager not available, cannot start '{channel_name}'")
|
||||
return
|
||||
# Stop existing instance first if still running (e.g. re-connect without disconnect)
|
||||
existing_ch = mgr.get_channel(channel_name)
|
||||
if existing_ch is not None:
|
||||
logger.info(f"[WebChannel] Stopping existing '{channel_name}' before reconnect...")
|
||||
mgr.stop(channel_name)
|
||||
# Always wait for the remote service to release the old connection before
|
||||
# establishing a new one (DingTalk drops callbacks on duplicate connections)
|
||||
logger.info(f"[WebChannel] Waiting for '{channel_name}' old connection to close...")
|
||||
time.sleep(5)
|
||||
if clear_fn:
|
||||
clear_fn(channel_name)
|
||||
logger.info(f"[WebChannel] Starting channel '{channel_name}'...")
|
||||
mgr.start([channel_name], first_start=False)
|
||||
logger.info(f"[WebChannel] Channel '{channel_name}' start completed")
|
||||
except Exception as e:
|
||||
logger.error(f"[WebChannel] Failed to start channel '{channel_name}': {e}",
|
||||
exc_info=True)
|
||||
|
||||
threading.Thread(target=_do_start, daemon=True).start()
|
||||
|
||||
return json.dumps({
|
||||
"status": "success",
|
||||
"channel_type": new_channel_type,
|
||||
}, ensure_ascii=False)
|
||||
|
||||
def _handle_disconnect(self, channel_name: str):
|
||||
existing = self._parse_channel_list(conf().get("channel_type", ""))
|
||||
existing = [ch for ch in existing if ch != channel_name]
|
||||
new_channel_type = ",".join(existing)
|
||||
|
||||
local_config = conf()
|
||||
local_config["channel_type"] = new_channel_type
|
||||
|
||||
config_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(
|
||||
os.path.abspath(__file__)))), "config.json")
|
||||
if os.path.exists(config_path):
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
file_cfg = json.load(f)
|
||||
else:
|
||||
file_cfg = {}
|
||||
file_cfg["channel_type"] = new_channel_type
|
||||
with open(config_path, "w", encoding="utf-8") as f:
|
||||
json.dump(file_cfg, f, indent=4, ensure_ascii=False)
|
||||
|
||||
def _do_stop():
|
||||
try:
|
||||
import sys
|
||||
app_module = sys.modules.get('__main__') or sys.modules.get('app')
|
||||
mgr = getattr(app_module, '_channel_mgr', None) if app_module else None
|
||||
clear_fn = getattr(app_module, '_clear_singleton_cache', None) if app_module else None
|
||||
if mgr:
|
||||
mgr.stop(channel_name)
|
||||
else:
|
||||
logger.warning(f"[WebChannel] ChannelManager not found, cannot stop '{channel_name}'")
|
||||
if clear_fn:
|
||||
clear_fn(channel_name)
|
||||
logger.info(f"[WebChannel] Channel '{channel_name}' disconnected, "
|
||||
f"channel_type={new_channel_type}")
|
||||
except Exception as e:
|
||||
logger.warning(f"[WebChannel] Failed to stop channel '{channel_name}': {e}",
|
||||
exc_info=True)
|
||||
|
||||
threading.Thread(target=_do_stop, daemon=True).start()
|
||||
|
||||
return json.dumps({
|
||||
"status": "success",
|
||||
"channel_type": new_channel_type,
|
||||
}, ensure_ascii=False)
|
||||
|
||||
|
||||
def _get_workspace_root():
|
||||
"""Resolve the agent workspace directory."""
|
||||
from common.utils import expand_path
|
||||
@@ -584,8 +905,19 @@ class ToolsHandler:
|
||||
try:
|
||||
from agent.tools.tool_manager import ToolManager
|
||||
tm = ToolManager()
|
||||
loaded = list(tm.tool_classes.keys())
|
||||
return json.dumps({"status": "success", "tools": loaded}, ensure_ascii=False)
|
||||
if not tm.tool_classes:
|
||||
tm.load_tools()
|
||||
tools = []
|
||||
for name, cls in tm.tool_classes.items():
|
||||
try:
|
||||
instance = cls()
|
||||
tools.append({
|
||||
"name": name,
|
||||
"description": instance.description,
|
||||
})
|
||||
except Exception:
|
||||
tools.append({"name": name, "description": ""})
|
||||
return json.dumps({"status": "success", "tools": tools}, ensure_ascii=False)
|
||||
except Exception as e:
|
||||
logger.error(f"[WebChannel] Tools API error: {e}")
|
||||
return json.dumps({"status": "error", "message": str(e)})
|
||||
|
||||
Reference in New Issue
Block a user