Skip to content

Commit 46b5b9d

Browse files
yangsijia-celinaagentkit-git
authored andcommitted
feat: 消息会话支持 ws_scheme 参数以选择 ws/wss 协议
GitOrigin-RevId: efff209be34477a31b51980dc5bf82d805248003
1 parent e6af9d5 commit 46b5b9d

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Message sessions accept an explicit `ws_scheme` (`"ws"` or `"wss"`, default
10+
`"wss"`) so callers can dial `ws://` when the ArkClaw endpoint returned by
11+
`GetClawInstanceChatToken` has no protocol prefix and the environment only
12+
exposes plain WebSocket. The new `arkclaw message send|shell --ws-scheme`
13+
CLI flag mirrors the SDK option. Endpoints already carrying an explicit
14+
`http(s)://` / `ws(s)://` prefix keep their scheme.
15+
716
## [0.2.0] - 2026-07-31
817

918
### Added

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ with client.create_message_session(
383383
instance_id="ci-xxx",
384384
wait=True,
385385
receive_timeout=120,
386+
ws_scheme="wss", # optional; when set, use the declared protocol
386387
) as session:
387388
result = session.send_message("你好")
388389
print(result)
@@ -396,7 +397,8 @@ arkclaw message send \
396397
--instance-id ci-xxx \
397398
--message "你好" \
398399
--stream \
399-
--text-only
400+
--text-only \
401+
--ws-scheme wss # optional; when set, use the declared protocol
400402

401403
arkclaw message shell \
402404
--space-id csi-xxx \

arkclaw/cli/messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def _add_common_session_args(parser: argparse.ArgumentParser) -> None:
6262
default="agent:main:main",
6363
help="Logical ArkClaw chat session key to reuse across reconnects",
6464
)
65+
parser.add_argument(
66+
"--ws-scheme",
67+
choices=["ws", "wss"],
68+
default="wss",
69+
help="WebSocket scheme to use when the token endpoint has no protocol prefix (default: wss)",
70+
)
6571

6672

6773
def _build_session(args: argparse.Namespace) -> ArkClawMessageSession:
@@ -76,6 +82,7 @@ def _build_session(args: argparse.Namespace) -> ArkClawMessageSession:
7682
receive_timeout=args.receive_timeout,
7783
connect_retries=args.connect_retries,
7884
session_key=args.session_key,
85+
ws_scheme=args.ws_scheme,
7986
)
8087

8188

arkclaw/message.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,15 @@ def _render_stream_message(data: str, *, pretty: bool, text_only: bool) -> list[
153153
return []
154154

155155

156-
def _build_websocket_url(*, endpoint: str, chat_token: str, claw_instance_id: str) -> str:
156+
def _build_websocket_url(
157+
*,
158+
endpoint: str,
159+
chat_token: str,
160+
claw_instance_id: str,
161+
default_scheme: str = "wss",
162+
) -> str:
163+
if default_scheme not in ("ws", "wss"):
164+
raise ValidationError(f"ws_scheme must be 'ws' or 'wss', got {default_scheme!r}")
157165
normalized = endpoint.strip()
158166
if normalized.startswith("wss://") or normalized.startswith("ws://"):
159167
base = normalized
@@ -162,7 +170,7 @@ def _build_websocket_url(*, endpoint: str, chat_token: str, claw_instance_id: st
162170
elif normalized.startswith("http://"):
163171
base = "ws://" + normalized[len("http://") :]
164172
else:
165-
base = f"wss://{normalized}"
173+
base = f"{default_scheme}://{normalized}"
166174

167175
if "?" not in base:
168176
scheme_split = base.split("://", 1)
@@ -235,9 +243,12 @@ def __init__(
235243
connect_retries: int = 2,
236244
session_key: str = "agent:main:main",
237245
protocol_version: int = 4,
246+
ws_scheme: str = "wss",
238247
) -> None:
239248
if connect_retries < 0:
240249
raise ValidationError("connect_retries must be >= 0")
250+
if ws_scheme not in ("ws", "wss"):
251+
raise ValidationError(f"ws_scheme must be 'ws' or 'wss', got {ws_scheme!r}")
241252
self.client = client
242253
self.space_id = space_id
243254
self.instance_id = instance_id
@@ -251,6 +262,7 @@ def __init__(
251262
if protocol_version not in (3, 4):
252263
raise ValidationError(f"protocol_version must be 3 or 4, got {protocol_version}")
253264
self.protocol_version = protocol_version
265+
self.ws_scheme = ws_scheme
254266

255267
self._websocket_module: Any | None = None
256268
self._timeout_exc: type[BaseException] = TimeoutError
@@ -438,6 +450,7 @@ def _refresh_chat_access(self) -> None:
438450
endpoint=endpoint,
439451
chat_token=chat_token,
440452
claw_instance_id=claw_instance_id,
453+
default_scheme=self.ws_scheme,
441454
)
442455

443456
def _get_websocket_module(self) -> Any:

0 commit comments

Comments
 (0)