feat: add /context command to display context information#817
feat: add /context command to display context information#817anilzeybek wants to merge 2 commits intoMoonshotAI:mainfrom
/context command to display context information#817Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new /context command to the CLI that displays contextual information about the current session, including message counts, token usage, checkpoints, and message distribution by role. This is particularly useful for users accessing Kimi through ACP (Agent Communication Protocol) to monitor their context usage.
Changes:
- Added
/contextslash command implementation insrc/kimi_cli/soul/slash.py - Updated English and Chinese documentation with command description and usage examples
- Updated changelogs to document the new feature
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/kimi_cli/soul/slash.py | Implements the new /context command that displays session info including messages, tokens, checkpoints, and role distribution |
| docs/zh/release-notes/changelog.md | Adds Chinese changelog entry for the new command |
| docs/zh/reference/slash-commands.md | Adds Chinese documentation for the /context command with usage examples |
| docs/en/release-notes/changelog.md | Adds English changelog entry for the new command |
| docs/en/reference/slash-commands.md | Adds English documentation for the /context command with usage examples |
| CHANGELOG.md | Adds top-level changelog entry for the new command |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/kimi_cli/soul/slash.py
Outdated
| f" Total messages: {len(history)}\n", | ||
| f" Checkpoints: {ctx.n_checkpoints}\n", | ||
| ] | ||
|
|
||
| # Add token usage with percentage if LLM is available | ||
| if soul.runtime.llm is not None: | ||
| max_context = soul.runtime.llm.max_context_size | ||
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | ||
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n") | ||
| else: | ||
| lines.append(f" Token count: {token_count:,}\n") |
There was a problem hiding this comment.
The newline characters are being added inconsistently. Lines 96 and 97 append \n to the strings, but this creates extra blank lines in the output since "\n".join(lines) on line 118 will already insert newlines between elements. This results in double line breaks for these entries. Remove the \n characters from these lines to match the pattern used for other lines in the output.
| f" Total messages: {len(history)}\n", | |
| f" Checkpoints: {ctx.n_checkpoints}\n", | |
| ] | |
| # Add token usage with percentage if LLM is available | |
| if soul.runtime.llm is not None: | |
| max_context = soul.runtime.llm.max_context_size | |
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | |
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n") | |
| else: | |
| lines.append(f" Token count: {token_count:,}\n") | |
| f" Total messages: {len(history)}", | |
| f" Checkpoints: {ctx.n_checkpoints}", | |
| ] | |
| # Add token usage with percentage if LLM is available | |
| if soul.runtime.llm is not None: | |
| max_context = soul.runtime.llm.max_context_size | |
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | |
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)") | |
| else: | |
| lines.append(f" Token count: {token_count:,}") |
src/kimi_cli/soul/slash.py
Outdated
| f" Total messages: {len(history)}\n", | ||
| f" Checkpoints: {ctx.n_checkpoints}\n", | ||
| ] | ||
|
|
||
| # Add token usage with percentage if LLM is available | ||
| if soul.runtime.llm is not None: | ||
| max_context = soul.runtime.llm.max_context_size | ||
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | ||
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n") | ||
| else: | ||
| lines.append(f" Token count: {token_count:,}\n") |
There was a problem hiding this comment.
The newline character is being added inconsistently. Lines 104 and 106 append \n to the strings, but this creates extra blank lines in the output since "\n".join(lines) on line 118 will already insert newlines between elements. This results in double line breaks for these entries. Remove the \n characters from these lines to match the pattern used for other lines in the output.
| f" Total messages: {len(history)}\n", | |
| f" Checkpoints: {ctx.n_checkpoints}\n", | |
| ] | |
| # Add token usage with percentage if LLM is available | |
| if soul.runtime.llm is not None: | |
| max_context = soul.runtime.llm.max_context_size | |
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | |
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n") | |
| else: | |
| lines.append(f" Token count: {token_count:,}\n") | |
| f" Total messages: {len(history)}", | |
| f" Checkpoints: {ctx.n_checkpoints}", | |
| ] | |
| # Add token usage with percentage if LLM is available | |
| if soul.runtime.llm is not None: | |
| max_context = soul.runtime.llm.max_context_size | |
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | |
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)") | |
| else: | |
| lines.append(f" Token count: {token_count:,}") |
| @registry.command | ||
| async def context(soul: KimiSoul, args: str): | ||
| """Display context information (messages, tokens, checkpoints)""" | ||
| ctx = soul.context | ||
| history = ctx.history | ||
|
|
||
| if not history: | ||
| wire_send(TextPart(text="Context is empty - no messages yet.")) | ||
| return | ||
|
|
||
| token_count = ctx.token_count | ||
| lines = [ | ||
| "Context Info:", | ||
| f" Total messages: {len(history)}\n", | ||
| f" Checkpoints: {ctx.n_checkpoints}\n", | ||
| ] | ||
|
|
||
| # Add token usage with percentage if LLM is available | ||
| if soul.runtime.llm is not None: | ||
| max_context = soul.runtime.llm.max_context_size | ||
| usage_percent = (token_count / max_context * 100) if max_context > 0 else 0 | ||
| lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n") | ||
| else: | ||
| lines.append(f" Token count: {token_count:,}\n") | ||
|
|
||
| # Count messages by role | ||
| role_counts: dict[str, int] = {} | ||
| for msg in history: | ||
| role_counts[msg.role] = role_counts.get(msg.role, 0) + 1 | ||
|
|
||
| if role_counts: | ||
| lines.append(" Messages by role:") | ||
| for role, count in sorted(role_counts.items()): | ||
| lines.append(f" {role}: {count}") | ||
|
|
||
| wire_send(TextPart(text="\n".join(lines))) |
There was a problem hiding this comment.
The new /context command lacks test coverage. The PR description claims "I have added tests that prove my fix is effective or that my feature works," but no tests were found for this command in the test suite. While other slash commands like /yolo, /compact, /clear, and /debug may also lack dedicated tests, the PR explicitly states that tests were added, which is not reflected in the changes. Consider adding tests similar to the pattern in tests/core/test_kimisoul_slash_commands.py to verify the command's functionality, especially for different scenarios like empty context, context with messages, and with/without LLM availability.
|
I've addressed the review comments. Could you resolve them if the changes look good? |
Related Issue
No related issue.
Description
This commit introduces
/contextcommand that shows the current context related information such as the usage ratio. This command is especially important for using Kimi through ACP.Checklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.