feat(sdk-python): add get_context_usage() method - #6471
Conversation
Add `get_context_usage(show_details=False)` method to the Python SDK's Query class, sending the `get_context_usage` control request to the CLI. Also add `CLIControlGetContextUsageRequest` protocol type. The TS SDK already has `getContextUsage()`; the CLI already handles the `get_context_usage` control request in SystemController.
qwen-code-ci-bot
left a comment
There was a problem hiding this comment.
Hey @juhuan — thanks for the contribution! The code change itself looks clean, but the PR body doesn't follow our PR template. Could you update the description to include the required sections?
Specifically, the template asks for:
- What this PR does (currently
## Summary) - Why it's needed (motivation / problem being solved)
- Reviewer Test Plan with subsections: How to verify, Evidence (Before & After), Tested on
- Risk & Scope
- Linked Issues
- A
<details>中文说明</details>block with a Chinese translation
This helps reviewers (and future-you) understand the context quickly. Happy to re-run triage once it's updated. 🙏
中文说明
嗨 @juhuan — 感谢贡献!代码改动本身看起来没问题,但 PR 描述没有按照我们的 PR 模板 填写。能否更新一下描述,补上必要的章节?
具体来说,模板要求包含:
- What this PR does(目前是
## Summary) - Why it's needed(动机 / 要解决的问题)
- Reviewer Test Plan 及子章节:验证方法、Evidence(Before & After)、Tested on
- Risk & Scope
- Linked Issues
- 一个
<details>中文说明</details>块,包含中文翻译
这些信息能帮助 reviewer 更快理解上下文。更新后可以重新跑 triage。🙏
— Qwen Code · qwen3.7-max
| await self._ensure_started() | ||
| return await self._send_control_request("mcp_server_status") | ||
|
|
||
| async def get_context_usage( |
There was a problem hiding this comment.
[Suggestion] The async Query class now has get_context_usage(), but SyncQuery in sync_query.py is missing the corresponding sync wrapper. Every other control-request method (supported_commands, mcp_server_status, set_model, set_permission_mode) has a sync counterpart. Sync SDK consumers currently have no way to call this new feature.
Consider adding to SyncQuery (after mcp_server_status):
def get_context_usage(self, show_details: bool = False) -> Any:
q = self._require_query()
return asyncio.run_coroutine_threadsafe(
q.get_context_usage(show_details),
self._loop,
).result(timeout=q.control_request_timeout + _SYNC_TIMEOUT_MARGIN)— qwen3.7-max via Qwen Code /review
| await self._ensure_started() | ||
| return await self._send_control_request( | ||
| "get_context_usage", {"show_details": show_details} | ||
| ) |
There was a problem hiding this comment.
[Suggestion] No tests were added for get_context_usage(). The TS SDK has dedicated unit tests (Query.test.ts lines 1211-1287) covering the request payload, show_details parameter forwarding, response deserialization, and closed-query error handling. The Python SDK has analogous tests for supported_commands() and mcp_server_status() in test_query_core.py and integration tests.
Additionally, the integration test mock in tests/integration/conftest.py has no handler for the get_context_usage subtype — it falls through to the error branch, so integration tests cannot exercise this path.
Suggested additions:
- Unit test in
test_query_core.pyverifying the outgoing payload contains{"subtype": "get_context_usage", "show_details": True/False}and that a mock response is returned correctly - A
get_context_usagebranch inconftest.py's mock CLI handler returning a representative response - Optionally, an integration test in
test_async_query.py
— qwen3.7-max via Qwen Code /review
💡 Suggestion: Consolidate SDK PRsHi @juhuan, thanks for the comprehensive SDK work! We noticed you have 15 open PRs that all modify the same core files ( The problem
Suggestion: regroup into 2 PRsWe recommend closing the current 15 PRs and reopening them as 2 consolidated PRs: PR 1 — Covers pure SDK-side option additions (~9 current PRs):
PR 2 — Covers features that also involve CLI-side
This keeps a reasonable separation of concerns while eliminating the merge-conflict chain and making review much more manageable. /cc @juhuan |
|
Closing in favor of consolidated PRs (see suggestion comment above). Please reopen as 2 grouped PRs. |
Summary
Add
get_context_usage(show_details=False)method to the Python SDK'sQueryclass, which sends theget_context_usagecontrol request to the CLI. Also adds theCLIControlGetContextUsageRequestprotocol type.The TS SDK already has
getContextUsage(), and the CLI already handles theget_context_usagecontrol request inSystemController.Test plan
pytest— 58 passed)