让你的项目更加简单的嵌入openclaw
Project description
OpenClaw Gateway Python 适配器(WebChat 协议)
让你的项目更加简单的嵌入openclaw
这是一个可复用的 Python 包:基于 OpenClaw Gateway 的 WebSocket 协议完成握手与 RPC 调用,并提供开箱即用的流式聊天接口(chat.send + event=chat)。
如何工作(简版)
调用接口(你的 Python 代码 / CLI)
│
▼
伪造的 WebChat 终端(本适配器模拟的 Web 客户端)
│ ws://127.0.0.1:18789(WebChat 协议:connect / sessions.patch / chat.send)
▼
OpenClaw Gateway
特性
- 一键连接:
OpenClawGatewayWsAdapter.create_connected()自动握手并准备会话 - 配置统一:
AdapterSettings.from_env()从.env/ 环境变量读取参数 - 流式输出:
stream_chat()增量产出 assistant 文本片段 - CLI 入口:支持一次性请求与交互式 REPL,并输出连接就绪日志
- 可测试:可注入
ws_factory,便于在单元测试中模拟网关收发
快速开始
1) 安装依赖
py -m pip install --index-url https://test.pypi.org/simple/ --no-deps openclaw-webchat-adapter
2) 配置环境变量
复制 .env.example 为 .env 并按注释块填写(每个变量的含义与影响以 .env.example 为准)。
最少需要:
OPENCLAW_GATEWAY_URLOPENCLAW_SESSION_KEY
鉴权(按你的网关策略选择其一或同时提供):
OPENCLAW_GATEWAY_TOKENOPENCLAW_GATEWAY_PASSWORD
3) 使用示例(推荐:代码调用)
"""为 OpenClaw Gateway 适配器提供一个最小可用的命令行入口。"""
from openclaw_webchat_adapter.ws_adapter import OpenClawChatWsAdapter as adapter
def main() -> int:
"""基于 .env 配置启动交互式 REPL 或执行一次性请求。"""
connect = adapter.create_connected_from_env()
# 进入交互式 REPL
try:
while True:
line = input("> ").strip()
if not line:
continue
if line.lower() in ("/exit", "/quit"):
break
for chunk in connect.stream_chat(line):
print(chunk, end="", flush=True)
print("")
finally:
connect.stop()
return 0
if __name__ == "__main__":
raise SystemExit(main())
退出交互式聊天:
- 输入
/exit或/quit
安全建议
- 不要在日志中输出
token/password等敏感信息(本适配器不会主动打印这些字段)。 - 如需远程访问网关,建议通过安全通道(例如内网/VPN/反向代理)并启用鉴权。
文档
- 结构化设计文档:ADAPTER_DESIGN_REPORT.md
- 本仓库设计变更说明:DESIGN_REPORT.md
- 本仓库测试报告:TEST_REPORT.md
- 协议与握手细节参考:README_3.md
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openclaw_webchat_adapter-0.0.2.tar.gz.
File metadata
- Download URL: openclaw_webchat_adapter-0.0.2.tar.gz
- Upload date:
- Size: 30.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fbb8d39f96715c30f45e93316fa87014c4a6f45e50e0ec0e18f9f142fc74573
|
|
| MD5 |
556139ff78141d5a016d07da3570c816
|
|
| BLAKE2b-256 |
4ace7e0de04eec678f33a4b14577e519766480e83efedcb54b124e8ed05cd572
|
File details
Details for the file openclaw_webchat_adapter-0.0.2-py3-none-any.whl.
File metadata
- Download URL: openclaw_webchat_adapter-0.0.2-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
509d85ccff6727ea62f9daabb22b7c186590465f8371f322cb45c2bce7ef5d9e
|
|
| MD5 |
ef3228a53c458b33f75bf41cdca8db72
|
|
| BLAKE2b-256 |
81946adb6fc84b7c2b335c0d0c1ef7c3536b195b4302a85afc9199f1185408cf
|