oclaw/tests/test_anthropic_transport.py
oliver ba3836f00f 初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库
- 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等)
- 提交当前工程代码与配置

Made-with: Cursor
2026-04-24 22:31:22 +08:00

27 lines
1.2 KiB
Python

from __future__ import annotations
from oclaw.platform.llm.transports.anthropic_messages import parse_anthropic_stream_events
def test_anthropic_stream_parses_text_and_tool_use() -> None:
events = [
{"type": "message_start"},
{"type": "content_block_start", "content_block": {"type": "text", "text": ""}},
{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "Hi "}},
{"type": "content_block_delta", "delta": {"type": "text_delta", "text": "there"}},
{"type": "content_block_stop"},
{"type": "content_block_start", "content_block": {"type": "tool_use", "id": "toolu_1", "name": "query_route", "input": {}}},
{"type": "content_block_delta", "delta": {"type": "input_json_delta", "partial_json": "{\"destination\":\"1.1.1.1\"}"}},
{"type": "content_block_stop"},
{"type": "message_stop"},
]
buf: list[str] = []
text, tool_calls = parse_anthropic_stream_events(events, on_token=buf.append)
assert text == "Hi there"
assert "".join(buf) == "Hi there"
assert len(tool_calls) == 1
tc = tool_calls[0]
assert tc.id == "toolu_1"
assert tc.name == "query_route"
assert tc.arguments.get("destination") == "1.1.1.1"