From fd583132d79d1115503453d39c5729b07f500926 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sat, 11 Apr 2026 11:58:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81Matrix=20access=5Ftok?= =?UTF-8?q?en=E7=99=BB=E5=BD=95=EF=BC=8C=E9=85=8D=E7=BD=AEAI=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai_chat.db | Bin 61440 -> 61440 bytes init_config.py | 40 +++++++ logs/ai-chat.log | 111 +++++++++++++++--- main.py | 24 +++- .../__pycache__/ai_service.cpython-310.pyc | Bin 2542 -> 2956 bytes .../matrix_service.cpython-310.pyc | Bin 4869 -> 5038 bytes services/ai_service.py | 20 +++- services/matrix_service.py | 13 +- 8 files changed, 186 insertions(+), 22 deletions(-) create mode 100644 init_config.py diff --git a/ai_chat.db b/ai_chat.db index 05dc5b4209a431ae0243c35aba10b2578e1fb6b9..be46afd048ccc1f3c0cfa8ed1a2890b1b2bb2ec2 100644 GIT binary patch delta 1048 zcmZ`%Pe|Kf6#tU14eMC@EaG<9go=kUifPicO`A?y!8V(bbVa9=z}I`@Q#j@3**%7kBaU zRVMu6S`QQc@Mr@ru+WEK1NuIOn{Gm7gbko*v>B;tV22IW3UdTZH#6k?K>#Y9%lcvUi~iwGeW zju8BTBS*?1T=rvzVOfj=fqHEl!8ZJKYTrN(<0#W0iEYBsn&Q)`f({`UJVx*q#vvB^ z8Y%^k9q>W0Qsy`$J-DG7x>YXBYkEFUOjEN;MKWdDmNt6({(|${8X1@y8Y2rm!*}iO z@xF!GqA_V}$n`L$OYY_N>sRGbSld z=?3Yth-s0sQlEvlrYH0QpD!6_4Gvyz?mzuhU0(7gf9T^-yi;}sO_U7c(9e?cgk?>e zN<7}cUOA=TUtOv`TdTg>tgdcVUoUz#NGq5_tu5D8y`UMTQ8KB|SS7W8e{-$+=D9Q3 z!MiO_Y^syxP+LokV)f~QRwNaB-uYa(I@lk%Lu^lca+u*zdvDyu=X8@$S&k|(o8ZNC zhIgW@h*?39SFzqSxZWlF1$#s_2><{9 delta 36 pcmZp8z})bFd4e<}-$WT_M!t;+OZYdl6#VgD)F7~EfdZHe008%%4h{eS diff --git a/init_config.py b/init_config.py new file mode 100644 index 0000000..d192134 --- /dev/null +++ b/init_config.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""初始化配置""" +import sys +sys.path.insert(0, '/home/xian/.openclaw/workspace-coder/works/ai-chat') + +from models import SessionLocal, SystemConfig, init_db + +# 初始化数据库 +init_db() + +db = SessionLocal() + +# 配置AI模型 +configs = [ + {'key': 'ai_api_base', 'value': 'http://192.168.2.17:19007/v1', 'description': 'AI模型API地址'}, + {'key': 'ai_api_key', 'value': 'xxxx', 'description': 'AI模型API Key'}, + {'key': 'ai_model', 'value': 'auto', 'description': 'AI模型名称'}, + + # 配置Matrix Bot + {'key': 'matrix_homeserver', 'value': 'https://matrix.tphai.com', 'description': 'Matrix服务器地址'}, + {'key': 'matrix_username', 'value': '@tester:matrix.tphai.com', 'description': 'Matrix Bot用户名'}, + {'key': 'matrix_access_token', 'value': 'syt_dGVzdGVy_eMwWfezCXSyBgHzvkmly_4dWFtM', 'description': 'Matrix Bot Access Token'}, +] + +for config in configs: + existing = db.query(SystemConfig).filter(SystemConfig.key == config['key']).first() + if existing: + existing.value = config['value'] + existing.description = config['description'] + else: + db.add(SystemConfig(**config)) + +db.commit() +print("配置已写入数据库") + +# 显示配置 +for c in db.query(SystemConfig).all(): + print(f"{c.key}: {c.value}") + +db.close() \ No newline at end of file diff --git a/logs/ai-chat.log b/logs/ai-chat.log index 67a3c00..3a4be5f 100644 --- a/logs/ai-chat.log +++ b/logs/ai-chat.log @@ -1,18 +1,93 @@ -Traceback (most recent call last): - File "/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py", line 17, in - from models import init_db, get_db, User, Conversation, Message, SystemConfig - File "/home/xian/.openclaw/workspace-coder/works/ai-chat/models/__init__.py", line 1, in - from .database import Base, engine, SessionLocal, get_db, init_db - File "/home/xian/.openclaw/workspace-coder/works/ai-chat/models/database.py", line 33, in - class Conversation(Base): - File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_api.py", line 195, in __init__ - _as_declarative(reg, cls, dict_) - File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 247, in _as_declarative - return _MapperConfig.setup_mapping(registry, cls, dict_, None, {}) - File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 328, in setup_mapping - return _ClassScanMapperConfig( - File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 574, in __init__ - self._extract_mappable_attributes() - File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 1507, in _extract_mappable_attributes - raise exc.InvalidRequestError( -sqlalchemy.exc.InvalidRequestError: Attribute name 'metadata' is reserved when using the Declarative API. +/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:445: DeprecationWarning: + on_event is deprecated, use lifespan event handlers instead. + + Read more about it in the + [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/). + + @app.on_event("startup") +/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:470: DeprecationWarning: + on_event is deprecated, use lifespan event handlers instead. + + Read more about it in the + [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/). + + @app.on_event("shutdown") +INFO: Started server process [1416772] +INFO: Waiting for application startup. +INFO:__main__:数据库初始化完成 +INFO:services.ai_service:AI配置已更新: http://192.168.2.17:19007/v1, model=auto +INFO:__main__:AI配置已加载: http://192.168.2.17:19007/v1, model=auto +INFO:services.matrix_service:Matrix连接成功(使用token): @tester:matrix.tphai.com +INFO:__main__:Matrix Bot已启动 +INFO: Application startup complete. +INFO: Uvicorn running on http://0.0.0.0:19020 (Press CTRL+C to quit) +WARNING:nio.client.async_client:Timed out, sleeping for 0s +WARNING:nio.client.async_client:Timed out, sleeping for 0s +WARNING:nio.client.async_client:Timed out, sleeping for 0s +WARNING:nio.client.async_client:Timed out, sleeping for 0s +WARNING:nio.client.async_client:Timed out, sleeping for 1s +WARNING:nio.client.async_client:Timed out, sleeping for 3s +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58534 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING:nio.client.async_client:Timed out, sleeping for 6s +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58535 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING:nio.client.async_client:Timed out, sleeping for 12s +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58536 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58541 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58542 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING:nio.client.async_client:Timed out, sleeping for 25s +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58544 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58549 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +INFO: 127.0.0.1:53464 - "GET /api/admin/config HTTP/1.1" 200 OK +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58554 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58555 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58560 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58561 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING:nio.client.async_client:Timed out, sleeping for 51s +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58562 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58567 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58572 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58573 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58574 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58579 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58580 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found +INFO: 127.0.0.1:37888 - "GET / HTTP/1.1" 200 OK +INFO: 127.0.0.1:37892 - "GET /admin HTTP/1.1" 200 OK +WARNING: Unsupported upgrade request. +WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually. +INFO: 192.168.2.10:58583 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found diff --git a/main.py b/main.py index 2d388bc..8a1d4fc 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ import logging from datetime import datetime import os -from models import init_db, get_db, User, Conversation, Message, SystemConfig +from models import init_db, get_db, SessionLocal, User, Conversation, Message, SystemConfig from services import ai_service, ConversationService, matrix_bot # 配置日志 @@ -379,7 +379,15 @@ async def update_config(data: dict, db: Session = Depends(get_db)): db.commit() - # 如果更新了Matrix配置,重新连接 + # 根据配置类型执行相应操作 + if key.startswith('ai_'): + # 更新AI服务配置 + configs = {c.key: c.value for c in db.query(SystemConfig).all()} + api_base = configs.get('ai_api_base', 'http://192.168.2.17:19007/v1') + api_key = configs.get('ai_api_key', 'xxxx') + model = configs.get('ai_model', 'auto') + ai_service.update_config(api_base, api_key, model) + if key.startswith('matrix_') and matrix_bot.is_running: await matrix_bot.disconnect() await matrix_bot.init_from_config() @@ -440,6 +448,18 @@ async def startup(): init_db() logger.info("数据库初始化完成") + # 从数据库加载AI配置 + db = SessionLocal() + try: + configs = {c.key: c.value for c in db.query(SystemConfig).all()} + api_base = configs.get('ai_api_base', 'http://192.168.2.17:19007/v1') + api_key = configs.get('ai_api_key', 'xxxx') + model = configs.get('ai_model', 'auto') + ai_service.update_config(api_base, api_key, model) + logger.info(f"AI配置已加载: {api_base}, model={model}") + finally: + db.close() + # 初始化Matrix Bot await matrix_bot.init_from_config() if matrix_bot.is_running: diff --git a/services/__pycache__/ai_service.cpython-310.pyc b/services/__pycache__/ai_service.cpython-310.pyc index 20013699f434be775e19d3f48a86083d8d111dc1..362c92ae3f89f3f3e3bf0e6c2b2bb773fa4ca023 100644 GIT binary patch delta 1299 zcmZvb&2Jk;6u@V8XTP(Ko6v;PP+TZ&mIM@_m8uE~4sol9;Iwj3=~l2>JY#1i*K5tL z0a+{g&~gF})=C_RklG^`geVdxuKW+osTYcR;cwtQM{PpXSX+|8}vG z`o2qGT>bN}dXbJ0{Eu8x_bU6z>ZiF-34E?KwTim`&xP3

ecrCCIR??p8iL90n=_lhkFv?7T zOI=!_qq(P#eItsJev(H~o5A*zfijwZ05iJ`C@8#`6c_<5bfA{&_ZX)I-6LEp^gZ$; zyHDve?6}?{d$o2u<)Zg) zGcXPXgz+e|mv*~CDwgy+sjPrXR-fV+TKK~Ok8=^V(|#xEE`lnnkN~35Olr~vm?orP zw0Mw!|2aIkrCrcGyh|=17Rx;*JgmlkTHVq!)|QrG9XN9Kzzig%4`olDD{mOD9zXIZ z*4~Zt(+?)bLpnZzPA?!%B9;I<@)U|MA!>+M5N7~Q#jb6)cH~)*MyY z8{5ssrxHRVk^+dOL-o5E+UQu?_uz0BI4Wcl@mL11HIX72J}kc zm6r2bjLj*JBQ%5#Q0((G?a2$ct|Jc4oJv?~iN87u8J5c?&(j5C$z3=#)dy8&MG;Ti zQ6yK(Z=923teLW~f0BMzIbD%&O?yeM!-m9WlpX^6RQd=!U%7D-W&8j2vmT-Bcc58^ bJ#iUpkYUd-V(ZUQ8$MTKI-`tMbocsyH3=E$ delta 823 zcmZvaO-~a+7{_P!W%i})7VHbqv;;7yOD!6KiyERvC0vTJ9CVG@ES)JdF0IZMUq}pz z^k_(${S=-&@DcRj)uVIKs~0~&55#$Du%>a7{pFeYKhHDspPBcQzcOyyacm^N-A~_| z@9dsi#J76oa$y}&jp|)Q^&`bMm~n)BlPWdjk>2s0UcW#e${L1zOp&k9b5vt0)#Xo^ z+91@RW>@z$nxQt+8JR{j#SA(&&>ZSY%{)^Y3Qg1TuI5{GLblHe>-I*C@t4gy>-WBv z@_jRYR*Bm6FzVYcUa{8l(rRU8J2}QHIG=o1a?2FSGF$CRdB|Qt~W5HcXR|Q1^Mn z=aSE4A?JWHfD4LN-wiwIlT-2oA7((x0&;-7#1@}|a2ikmlmIglRiQoDtZngG86OZD zcEankKVicMbWA#}>3PRJKz>^UQj`2|S3D4|G8fmOou$y@8vp?JZDoE$e y^JZ&jxB%CnE8N?lK&;teGl>7ZvHvY5%$s{{x*xGMINKP`NkgWZ#U^%Dd;K@oSfTv@ diff --git a/services/__pycache__/matrix_service.cpython-310.pyc b/services/__pycache__/matrix_service.cpython-310.pyc index e189998753c799605fea1f3f14498a27aae57302..e6d6fb2b62f3de3748cfa3ee65aa973552e68c03 100644 GIT binary patch delta 1501 zcmaJ>TW=dh6rP#A+iS--RqNDE)Jhb0(_$(?+o;fjlH$08_5r6vq=J>RyD?j0ubJI8 zP32&!2niunAq{UpYYQRq6p5A>9-t2hegHy3-B(Jv{0h!lJA{S`ulDnqGw1s4nR)Mt z%~CeVWKslvM}NHMUmW`?+h(*Zx@_M#?GRgb8KB}4K-DERVoB#?Qn55~n^_sN?l@i; z+EMV9*Ag*1LW@F^>S`9ksZ^{;C$pO%UZ)Y+AnWwnIq|hLaNA%2o?6ynilJ>vdV``^8sx6;BxwckL+b0G;MGO3-kyl0NA@KW3)iC!zVBPQfBCVQC9bNtC0*G0N0o3 zM#4KtDla3#tJrG9dYUWY{wL%!)~CP6_ge_kBb_KG>}X*wdh+QH0#W!Y~n z2Yip{YX=q;kMn@X#*0@SuO0b8Ym0N8dtp0hgB~;S&12}cQYr{0z_+@T^;r7uJi@DpA7?$rr7<)vNli$NIX`YpM>EOhY7O@G3Nw;^0=aBU2^A=1x2yNrqi|m< zd*{+?$|QMY(JZX8T^2>wn$42efy5f8$qePRLFHE^9#nFz5O4*2CT_`fAo)~L>pDyC zs-Rgnu-i}PC@ZVN6sp*IAH<>racxV+$+cytzMq{uPs=RI>ypWx=n9Zqj=q>XrGLov z!WsQ@?f{f@k29zzogEj^{tUIhJ}^DT7i8~M>eZiBBgJpWY^RYz4zS`B|>|Yp;XU9WLy;i0r=vMV zddBn@?k?*2lRLDOH87{*poOQH*v&+jKKMe2Nu33E8hVuX@|koTVd7DS;iv2xZDAqu zLp;Ixlbp}Z*PG3#F@xth^{hsGlX4SuE*6U`FsA=5KIl6~88VmUrS4-fHPvc{IH7+l z9XfG