auto-save 2026-05-11 17:27 (~4)
This commit is contained in:
@@ -1287,6 +1287,38 @@ def handle_notify(path: str, headers: dict[str, str], body: dict[str, Any]) -> t
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
server_version = "HermesFeishuBridge/1.0"
|
||||
|
||||
def proxy_chat_completions(self, body: dict[str, Any]) -> None:
|
||||
req, stream, status, error = build_profile_chat_request(body)
|
||||
if error is not None or req is None:
|
||||
self.send_json(status, error or {"code": status, "msg": "invalid chat request"})
|
||||
return
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=Config.request_timeout) as resp:
|
||||
content_type = resp.headers.get("Content-Type") or (
|
||||
"text/event-stream" if stream else "application/json"
|
||||
)
|
||||
self.send_response(resp.status)
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Cache-Control", "no-cache")
|
||||
self.end_headers()
|
||||
while True:
|
||||
chunk = resp.read(8192)
|
||||
if not chunk:
|
||||
break
|
||||
self.wfile.write(chunk)
|
||||
self.wfile.flush()
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw = exc.read()
|
||||
self.send_response(exc.code)
|
||||
self.send_header("Content-Type", exc.headers.get("Content-Type") or "application/json")
|
||||
self.end_headers()
|
||||
self.wfile.write(raw or json.dumps({"code": exc.code, "msg": exc.reason}).encode("utf-8"))
|
||||
except BrokenPipeError:
|
||||
logging.info("chat proxy client disconnected")
|
||||
except Exception as exc:
|
||||
logging.error("chat proxy failed:\n%s", traceback.format_exc())
|
||||
self.send_json(500, {"code": 500, "msg": str(exc)})
|
||||
|
||||
def do_GET(self) -> None:
|
||||
headers = {key.lower(): value for key, value in self.headers.items()}
|
||||
if self.path == "/health":
|
||||
@@ -1318,6 +1350,13 @@ class Handler(BaseHTTPRequestHandler):
|
||||
status, payload = handle_hermes_config_post(headers, body)
|
||||
elif self.path == "/feishu/ui-config":
|
||||
status, payload = handle_ui_config_post(headers, body)
|
||||
elif self.path == "/feishu/chat/completions":
|
||||
ok, status, message = is_admin_request(headers)
|
||||
if not ok:
|
||||
self.send_json(status, {"code": status, "msg": message})
|
||||
else:
|
||||
self.proxy_chat_completions(body)
|
||||
return
|
||||
elif self.path == "/feishu/notify" or self.path.startswith("/feishu/notify/"):
|
||||
status, payload = handle_notify(self.path, headers, body)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user