openclaw-webchat-adapter 0.0.2__tar.gz

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.
@@ -0,0 +1,8 @@
1
+ OPENCLAW_GATEWAY_URL=ws://127.0.0.1:18789
2
+ OPENCLAW_GATEWAY_TOKEN=hhadage
3
+ OPENCLAW_SESSION_KEY=agent:main:main
4
+
5
+ #OPENCLAW_PROTOCOL_VERSION=3
6
+ #OPENCLAW_CLIENT_ID=webchat-ui
7
+ #OPENCLAW_CLIENT_MODE=webchat
8
+ #OPENCLAW_CLIENT_PLATFORM=browser
@@ -0,0 +1,91 @@
1
+ ###############################################################################
2
+ # [必须/可选]:必须
3
+ # [配置效果]:配置 OpenClaw Gateway 的 WebSocket 连接地址;修改后会连接到不同的网关实例。
4
+ # [格式/默认值]:字符串;示例 ws://127.0.0.1:18789;默认值 ws://127.0.0.1:18789
5
+ ###############################################################################
6
+ OPENCLAW_GATEWAY_URL=ws://127.0.0.1:18789
7
+
8
+ ###############################################################################
9
+ # [必须/可选]:可选(与 OPENCLAW_GATEWAY_PASSWORD 二选一,或两者同时提供)
10
+ # [配置效果]:使用 token 方式通过网关鉴权;提供后将在 connect.auth.token 中发送。
11
+ # [格式/默认值]:字符串;示例 sk-xxxx;默认值为空
12
+ ###############################################################################
13
+ OPENCLAW_GATEWAY_TOKEN=
14
+
15
+ ###############################################################################
16
+ # [必须/可选]:可选(与 OPENCLAW_GATEWAY_TOKEN 二选一,或两者同时提供)
17
+ # [配置效果]:使用 password 方式通过网关鉴权;提供后将在 connect.auth.password 中发送。
18
+ # [格式/默认值]:字符串;示例 my-password;默认值为空
19
+ ###############################################################################
20
+ OPENCLAW_GATEWAY_PASSWORD=
21
+
22
+ ###############################################################################
23
+ # [必须/可选]:必须
24
+ # [配置效果]:chat.send 使用的 sessionKey;不同 sessionKey 会影响会话上下文/聊天记录隔离。
25
+ # [格式/默认值]:字符串;示例 agent:main:main;默认值 agent:main:main
26
+ ###############################################################################
27
+ OPENCLAW_SESSION_KEY=agent:main:main
28
+
29
+ ###############################################################################
30
+ # [必须/可选]:可选
31
+ # [配置效果]:用于 connect 握手时声明协议版本;一般无需修改。
32
+ # [格式/默认值]:整数;示例 3;默认值 3
33
+ ###############################################################################
34
+ OPENCLAW_PROTOCOL_VERSION=3
35
+
36
+ ###############################################################################
37
+ # [必须/可选]:可选
38
+ # [配置效果]:connect.client.id;用于标识客户端来源(便于网关侧统计/审计)。
39
+ # [格式/默认值]:字符串;示例 webchat-ui;默认值 webchat-ui
40
+ ###############################################################################
41
+ OPENCLAW_CLIENT_ID=webchat-ui
42
+
43
+ ###############################################################################
44
+ # [必须/可选]:可选
45
+ # [配置效果]:connect.client.mode;用于标识客户端模式(例如 webchat)。
46
+ # [格式/默认值]:字符串;示例 webchat;默认值 webchat
47
+ ###############################################################################
48
+ OPENCLAW_CLIENT_MODE=webchat
49
+
50
+ ###############################################################################
51
+ # [必须/可选]:可选
52
+ # [配置效果]:connect.client.displayName;用于在网关侧展示/日志中识别客户端。
53
+ # [格式/默认值]:字符串;示例 py-adapter;默认值 py-adapter
54
+ ###############################################################################
55
+ OPENCLAW_CLIENT_DISPLAY_NAME=py-adapter
56
+
57
+ ###############################################################################
58
+ # [必须/可选]:可选
59
+ # [配置效果]:connect.client.version;用于在网关侧展示客户端版本信息。
60
+ # [格式/默认值]:字符串;示例 dev;默认值 dev
61
+ ###############################################################################
62
+ OPENCLAW_CLIENT_VERSION=dev
63
+
64
+ ###############################################################################
65
+ # [必须/可选]:可选
66
+ # [配置效果]:connect.client.platform;用于在网关侧识别平台(browser/server 等)。
67
+ # [格式/默认值]:字符串;示例 browser;默认值 browser
68
+ ###############################################################################
69
+ OPENCLAW_CLIENT_PLATFORM=browser
70
+
71
+ ###############################################################################
72
+ # [必须/可选]:可选
73
+ # [配置效果]:connect.client.instanceId;用于区分同一客户端的不同实例;为空时会自动生成。
74
+ # [格式/默认值]:字符串;示例 py-<uuid>;默认值为空(自动生成)
75
+ ###############################################################################
76
+ OPENCLAW_CLIENT_INSTANCE_ID=
77
+
78
+ ###############################################################################
79
+ # [必须/可选]:可选
80
+ # [配置效果]:connect.role;用于声明连接角色(取决于网关侧策略)。
81
+ # [格式/默认值]:字符串;示例 operator;默认值 operator
82
+ ###############################################################################
83
+ OPENCLAW_CONNECT_ROLE=operator
84
+
85
+ ###############################################################################
86
+ # [必须/可选]:可选
87
+ # [配置效果]:connect.scopes;声明请求权限范围(逗号分隔),取决于网关侧策略。
88
+ # [格式/默认值]:字符串(CSV);示例 operator.admin,operator.read;默认值 operator.admin
89
+ ###############################################################################
90
+ OPENCLAW_CONNECT_SCOPES=operator.admin
91
+
@@ -0,0 +1,31 @@
1
+ ### IntelliJ IDEA ###
2
+ out/
3
+ !**/src/main/**/out/
4
+ !**/src/test/**/out/
5
+
6
+ ### Eclipse ###
7
+ .xml
8
+ .apt_generated
9
+ .classpath
10
+ .factorypath
11
+ .project
12
+ .settings
13
+ .springBeans
14
+ .sts4-cache
15
+ bin/
16
+ .idea
17
+ !**/src/main/**/bin/
18
+ !**/src/test/**/bin/
19
+
20
+ ### NetBeans ###
21
+ /nbproject/private/
22
+ /nbbuild/
23
+ /dist/
24
+ /nbdist/
25
+ /.nb-gradle/
26
+
27
+ ### VS Code ###
28
+ .vscode/
29
+
30
+ ### Mac OS ###
31
+ .DS_Store
@@ -0,0 +1,98 @@
1
+ # OpenClaw Gateway Python 适配器(结构化版)设计文档
2
+
3
+ ### 让你的项目更加简单的嵌入openclaw
4
+
5
+ ## 修改摘要
6
+ - 将原先单文件的 WebSocket 适配器拆分为可复用的包:配置加载、.env 解析、协议适配、异常定义、CLI 入口分离。
7
+ - 新增统一配置入口 `AdapterSettings.from_env()`:从 `.env` 或系统环境变量读取 `token/password` 用于网关鉴权。
8
+ - 对外提供核心流式接口 `OpenClawGatewayWsAdapter.stream_chat(user_request)`:按 runId 订阅 chat 事件并增量输出。
9
+ - 增加可注入 `ws_factory` 以便单元测试模拟网关收发,降低耦合。
10
+
11
+ ## 代码结构与职责划分
12
+ - `openclaw_webchat_adapter/env.py`
13
+ - 负责解析/加载 `.env`,不引入第三方 dotenv 依赖。
14
+ - `openclaw_webchat_adapter/config.py`
15
+ - 定义 `AdapterSettings`,统一读取并校验环境变量(含鉴权字段存在性校验)。
16
+ - `openclaw_webchat_adapter/exceptions.py`
17
+ - 定义适配器对外抛出的异常类型,便于上层调用方精确捕获与处理。
18
+ - `openclaw_webchat_adapter/ws_adapter.py`
19
+ - 协议适配核心:握手(connect.challenge -> connect -> hello-ok)、RPC request/response pending 映射、chat 事件路由与增量拼接输出。
20
+ - `openclaw_webchat_adapter/__main__.py`
21
+ - 提供最小 CLI:用于快速验证连通性与流式输出效果。
22
+
23
+ ## 关键设计点
24
+ ### 1) 配置与鉴权
25
+ - 配置读取策略:
26
+ - 优先从 `.env` 加载(若存在),再从 `os.environ` 获取最终值。
27
+ - 默认不覆盖既有 `os.environ`(可通过 `dotenv_override=True` 允许覆盖)。
28
+ - 鉴权策略:
29
+ - 强制要求至少配置 `OPENCLAW_GATEWAY_TOKEN` 或 `OPENCLAW_GATEWAY_PASSWORD` 其一。
30
+ - 适配器在 `connect` 请求中仅在配置存在时才发送 `auth.token/auth.password` 字段,避免发送空字段。
31
+
32
+ ### 2) 协议适配与流式输出
33
+ - 握手:
34
+ - WebSocket 建连后可能先收到 `connect.challenge`,也可能不保证顺序;适配器同时提供事件触发和定时兜底,确保 connect 能发送。
35
+ - 收到 `res.payload.type == "hello-ok"` 视为握手成功。
36
+ - RPC:
37
+ - `req.id` 为一次请求的唯一标识;适配器用 `pending` 映射将 `res.id` 回投到对应等待队列。
38
+ - chat 流:
39
+ - `chat.send` 使用 `idempotencyKey` 作为 `runId`(一次请求一次 run)。
40
+ - 服务端 `event=chat` 按 `payload.runId` 路由到对应队列。
41
+ - 对 `delta/final` 采用“前缀差分”输出新增文本,避免重复输出累计内容。
42
+
43
+ ### 3) 可测试性与解耦
44
+ - `ws_factory` 注入点:
45
+ - 默认使用 `websocket-client` 的 `WebSocketApp`。
46
+ - 测试中用 FakeWebSocketApp 模拟握手、RPC、chat 事件,避免依赖真实网关。
47
+
48
+ ## 环境变量配置指南
49
+ 请复制 `.env.example` 为 `.env` 并填写。每个变量的含义与影响已在 `.env.example` 中以注释块完整说明:
50
+ - 网关连接:
51
+ - `OPENCLAW_GATEWAY_URL`
52
+ - 鉴权(二选一或同时提供):
53
+ - `OPENCLAW_GATEWAY_TOKEN`
54
+ - `OPENCLAW_GATEWAY_PASSWORD`
55
+ - 会话:
56
+ - `OPENCLAW_SESSION_KEY`
57
+ - 可选握手参数:
58
+ - `OPENCLAW_PROTOCOL_VERSION`
59
+ - `OPENCLAW_CLIENT_ID`
60
+ - `OPENCLAW_CLIENT_MODE`
61
+ - `OPENCLAW_CLIENT_DISPLAY_NAME`
62
+ - `OPENCLAW_CLIENT_VERSION`
63
+ - `OPENCLAW_CLIENT_PLATFORM`
64
+ - `OPENCLAW_CLIENT_INSTANCE_ID`
65
+ - `OPENCLAW_CONNECT_ROLE`
66
+ - `OPENCLAW_CONNECT_SCOPES`
67
+
68
+ ## 使用方式
69
+ ### 依赖安装(必须)
70
+ ```bash
71
+ pip install -r requirements.txt
72
+ ```
73
+
74
+ ### 方式 A:代码调用(推荐集成)
75
+ ```python
76
+ from openclaw_webchat_adapter import AdapterSettings, OpenClawGatewayWsAdapter
77
+
78
+ settings = AdapterSettings.from_env(dotenv_path=".env")
79
+ adapter = OpenClawGatewayWsAdapter(settings=settings)
80
+
81
+ adapter.start()
82
+ adapter.ensure_session("main")
83
+
84
+ for chunk in adapter.stream_chat("你好"):
85
+ print(chunk, end="", flush=True)
86
+
87
+ adapter.stop()
88
+ ```
89
+
90
+ ### 方式 B:命令行快速验证
91
+ ```bash
92
+ python -m openclaw_webchat_adapter --once "你好"
93
+ ```
94
+
95
+ ## 安全性说明
96
+ - 本实现不打印/不记录 token/password;上层如需日志,建议对敏感字段脱敏后再输出。
97
+ - `stream_chat` 对输入做类型与空字符串校验,避免异常协议请求。
98
+
@@ -0,0 +1,56 @@
1
+ # OpenClaw Gateway Adapter 设计文档(本次变更)
2
+
3
+ ## 修改摘要
4
+ - 修复 `OpenClawGatewayWsAdapter.create_connected()` 的类方法绑定,并新增 `create_connected_from_env()`:支持无参从 `.env`/环境变量读取并连接,也支持显式覆盖 `OPENCLAW_GATEWAY_URL/token/password`。
5
+ - 将 `openclaw_webchat_adapter` 包内的模块/类/函数 Docstring 统一翻译为中文,提升可读性与团队协作效率(不改变任何运行逻辑)。
6
+ - 新增 `env.py` 的解析与加载单元测试,覆盖引号处理、注释/空行跳过、override 行为等核心路径。
7
+ - 将 CLI 的“连接/握手/会话准备”逻辑迁移到 `ws_adapter.create_connected()`,CLI 仅负责读取输入并调用聊天接口。
8
+ - 适配器在“连接就绪/会话就绪”时输出 INFO 日志,便于启动排障与联调。
9
+ - 新增 `TEST_REPORT.md` 作为本次交付的测试报告。
10
+
11
+ ## 架构设计思路
12
+ 该适配器以“配置加载 + 协议适配 + 异常分层”的方式拆分职责,降低耦合并便于测试:
13
+ - `openclaw_webchat_adapter/env.py`
14
+ - 负责 `.env` 文本解析与写入 `os.environ`,不引入第三方依赖。
15
+ - `openclaw_webchat_adapter/config.py`
16
+ - 负责从环境变量/`.env` 读取配置并做基本合法性校验,最终构造 `AdapterSettings`。
17
+ - `openclaw_webchat_adapter/ws_adapter.py`
18
+ - 负责 WebSocket 连接、握手、RPC request/response 的 pending 映射、chat 事件路由与增量输出;并提供 `create_connected()` 封装一键启动流程。
19
+ - `openclaw_webchat_adapter/exceptions.py`
20
+ - 对外暴露类型化异常,便于上层做精确捕获与处理分支。
21
+ - `openclaw_webchat_adapter/__main__.py`
22
+ - 提供最小 CLI 入口,方便验证连通性与进行一次性请求;连接与会话准备由 `ws_adapter.create_connected()` 承担。
23
+
24
+ ## 环境变量详细配置指南
25
+ 建议复制 `.env.example` 为 `.env`,并按注释块填写。代码侧读取入口为 `AdapterSettings.from_env()`,主要变量如下:
26
+
27
+ ### 必填(代码会校验非空)
28
+ - `OPENCLAW_GATEWAY_URL`
29
+ - 网关 WebSocket 地址,例如:`ws://127.0.0.1:18789`
30
+ - `OPENCLAW_SESSION_KEY`
31
+ - chat 请求使用的会话 key,例如:`agent:main:main`
32
+
33
+ ### 选填(是否必需由网关端策略决定)
34
+ - `OPENCLAW_GATEWAY_TOKEN`
35
+ - token 鉴权凭据(若提供则在 connect 时携带)。
36
+ - `OPENCLAW_GATEWAY_PASSWORD`
37
+ - password 鉴权凭据(若提供则在 connect 时携带)。
38
+
39
+ ### 选填(握手与客户端信息)
40
+ - `OPENCLAW_PROTOCOL_VERSION`
41
+ - `OPENCLAW_CLIENT_ID`
42
+ - `OPENCLAW_CLIENT_MODE`
43
+ - `OPENCLAW_CLIENT_DISPLAY_NAME`
44
+ - `OPENCLAW_CLIENT_VERSION`
45
+ - `OPENCLAW_CLIENT_PLATFORM`
46
+ - `OPENCLAW_CLIENT_INSTANCE_ID`
47
+ - `OPENCLAW_CONNECT_ROLE`
48
+ - `OPENCLAW_CONNECT_SCOPES`
49
+
50
+ 说明:
51
+ - 变量的 `[必须/可选]`、`[配置效果]`、`[格式/默认值]` 以 `.env.example` 为准;该文件是配置说明的单一可信来源。
52
+ - `.env` 加载策略:默认不覆盖已存在的 `os.environ` 同名值;如需覆盖可在代码调用 `dotenv_override=True`。
53
+
54
+ ## 安全性说明
55
+ - 适配器不输出 `token/password` 等敏感字段;上层若需要日志,请自行脱敏后再记录。
56
+ - `.env` 解析逻辑仅处理 `KEY=VALUE` 形式,并会忽略注释/空行/非法行,降低误解析风险。
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2026] [hadage]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ Metadata-Version: 2.4
2
+ Name: openclaw_webchat_adapter
3
+ Version: 0.0.2
4
+ Summary: 让你的项目更加简单的嵌入openclaw
5
+ Project-URL: Homepage, https://github.com/tangdeyx2333-beep/openclaw-websocket-adapter
6
+ Project-URL: Issues, https://github.com/tangdeyx2333-beep/openclaw-websocket-adapter/issues
7
+ Author-email: hadage <tangdeyx2333@gmail.com>
8
+ License: MIT
9
+ License-File: LICENSE
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+
16
+ # OpenClaw Gateway Python 适配器(WebChat 协议)
17
+
18
+ 让你的项目更加简单的嵌入openclaw
19
+
20
+ 这是一个可复用的 Python 包:基于 OpenClaw Gateway 的 WebSocket 协议完成握手与 RPC 调用,并提供开箱即用的流式聊天接口(chat.send + event=chat)。
21
+
22
+ ## 如何工作(简版)
23
+
24
+ ```text
25
+ 调用接口(你的 Python 代码 / CLI)
26
+
27
+
28
+ 伪造的 WebChat 终端(本适配器模拟的 Web 客户端)
29
+ │ ws://127.0.0.1:18789(WebChat 协议:connect / sessions.patch / chat.send)
30
+
31
+ OpenClaw Gateway
32
+ ```
33
+
34
+ ## 特性
35
+ - 一键连接:`OpenClawGatewayWsAdapter.create_connected()` 自动握手并准备会话
36
+ - 配置统一:`AdapterSettings.from_env()` 从 `.env` / 环境变量读取参数
37
+ - 流式输出:`stream_chat()` 增量产出 assistant 文本片段
38
+ - CLI 入口:支持一次性请求与交互式 REPL,并输出连接就绪日志
39
+ - 可测试:可注入 `ws_factory`,便于在单元测试中模拟网关收发
40
+
41
+ ## 快速开始
42
+
43
+ ### 1) 安装依赖
44
+ ```bash
45
+ py -m pip install --index-url https://test.pypi.org/simple/ --no-deps openclaw-webchat-adapter
46
+ ```
47
+
48
+ ### 2) 配置环境变量
49
+ 复制 `.env.example` 为 `.env` 并按注释块填写(每个变量的含义与影响以 `.env.example` 为准)。
50
+
51
+ 最少需要:
52
+ - `OPENCLAW_GATEWAY_URL`
53
+ - `OPENCLAW_SESSION_KEY`
54
+
55
+ 鉴权(按你的网关策略选择其一或同时提供):
56
+ - `OPENCLAW_GATEWAY_TOKEN`
57
+ - `OPENCLAW_GATEWAY_PASSWORD`
58
+
59
+ ### 3) 使用示例(推荐:代码调用)
60
+
61
+ ```python
62
+ """为 OpenClaw Gateway 适配器提供一个最小可用的命令行入口。"""
63
+
64
+ from openclaw_webchat_adapter.ws_adapter import OpenClawChatWsAdapter as adapter
65
+
66
+ def main() -> int:
67
+ """基于 .env 配置启动交互式 REPL 或执行一次性请求。"""
68
+ connect = adapter.create_connected_from_env()
69
+ # 进入交互式 REPL
70
+ try:
71
+ while True:
72
+ line = input("> ").strip()
73
+ if not line:
74
+ continue
75
+ if line.lower() in ("/exit", "/quit"):
76
+ break
77
+ for chunk in connect.stream_chat(line):
78
+ print(chunk, end="", flush=True)
79
+ print("")
80
+ finally:
81
+ connect.stop()
82
+ return 0
83
+
84
+
85
+ if __name__ == "__main__":
86
+ raise SystemExit(main())
87
+
88
+
89
+ ```
90
+
91
+ 退出交互式聊天:
92
+ - 输入 `/exit` 或 `/quit`
93
+
94
+ ## 安全建议
95
+ - 不要在日志中输出 `token/password` 等敏感信息(本适配器不会主动打印这些字段)。
96
+ - 如需远程访问网关,建议通过安全通道(例如内网/VPN/反向代理)并启用鉴权。
97
+
98
+ ## 文档
99
+ - 结构化设计文档:[ADAPTER_DESIGN_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/ADAPTER_DESIGN_REPORT.md)
100
+ - 本仓库设计变更说明:[DESIGN_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/DESIGN_REPORT.md)
101
+ - 本仓库测试报告:[TEST_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/TEST_REPORT.md)
102
+ - 协议与握手细节参考:[README_3.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/README_3.md)
@@ -0,0 +1,87 @@
1
+ # OpenClaw Gateway Python 适配器(WebChat 协议)
2
+
3
+ 让你的项目更加简单的嵌入openclaw
4
+
5
+ 这是一个可复用的 Python 包:基于 OpenClaw Gateway 的 WebSocket 协议完成握手与 RPC 调用,并提供开箱即用的流式聊天接口(chat.send + event=chat)。
6
+
7
+ ## 如何工作(简版)
8
+
9
+ ```text
10
+ 调用接口(你的 Python 代码 / CLI)
11
+
12
+
13
+ 伪造的 WebChat 终端(本适配器模拟的 Web 客户端)
14
+ │ ws://127.0.0.1:18789(WebChat 协议:connect / sessions.patch / chat.send)
15
+
16
+ OpenClaw Gateway
17
+ ```
18
+
19
+ ## 特性
20
+ - 一键连接:`OpenClawGatewayWsAdapter.create_connected()` 自动握手并准备会话
21
+ - 配置统一:`AdapterSettings.from_env()` 从 `.env` / 环境变量读取参数
22
+ - 流式输出:`stream_chat()` 增量产出 assistant 文本片段
23
+ - CLI 入口:支持一次性请求与交互式 REPL,并输出连接就绪日志
24
+ - 可测试:可注入 `ws_factory`,便于在单元测试中模拟网关收发
25
+
26
+ ## 快速开始
27
+
28
+ ### 1) 安装依赖
29
+ ```bash
30
+ py -m pip install --index-url https://test.pypi.org/simple/ --no-deps openclaw-webchat-adapter
31
+ ```
32
+
33
+ ### 2) 配置环境变量
34
+ 复制 `.env.example` 为 `.env` 并按注释块填写(每个变量的含义与影响以 `.env.example` 为准)。
35
+
36
+ 最少需要:
37
+ - `OPENCLAW_GATEWAY_URL`
38
+ - `OPENCLAW_SESSION_KEY`
39
+
40
+ 鉴权(按你的网关策略选择其一或同时提供):
41
+ - `OPENCLAW_GATEWAY_TOKEN`
42
+ - `OPENCLAW_GATEWAY_PASSWORD`
43
+
44
+ ### 3) 使用示例(推荐:代码调用)
45
+
46
+ ```python
47
+ """为 OpenClaw Gateway 适配器提供一个最小可用的命令行入口。"""
48
+
49
+ from openclaw_webchat_adapter.ws_adapter import OpenClawChatWsAdapter as adapter
50
+
51
+ def main() -> int:
52
+ """基于 .env 配置启动交互式 REPL 或执行一次性请求。"""
53
+ connect = adapter.create_connected_from_env()
54
+ # 进入交互式 REPL
55
+ try:
56
+ while True:
57
+ line = input("> ").strip()
58
+ if not line:
59
+ continue
60
+ if line.lower() in ("/exit", "/quit"):
61
+ break
62
+ for chunk in connect.stream_chat(line):
63
+ print(chunk, end="", flush=True)
64
+ print("")
65
+ finally:
66
+ connect.stop()
67
+ return 0
68
+
69
+
70
+ if __name__ == "__main__":
71
+ raise SystemExit(main())
72
+
73
+
74
+ ```
75
+
76
+ 退出交互式聊天:
77
+ - 输入 `/exit` 或 `/quit`
78
+
79
+ ## 安全建议
80
+ - 不要在日志中输出 `token/password` 等敏感信息(本适配器不会主动打印这些字段)。
81
+ - 如需远程访问网关,建议通过安全通道(例如内网/VPN/反向代理)并启用鉴权。
82
+
83
+ ## 文档
84
+ - 结构化设计文档:[ADAPTER_DESIGN_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/ADAPTER_DESIGN_REPORT.md)
85
+ - 本仓库设计变更说明:[DESIGN_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/DESIGN_REPORT.md)
86
+ - 本仓库测试报告:[TEST_REPORT.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/TEST_REPORT.md)
87
+ - 协议与握手细节参考:[README_3.md](file:///f:/aaa_desktop_file/python-study/openclaw_webchat_adapter/README_3.md)