feat: integrate rtk for reducing LLM token consumption on agent shell commands#13615
feat: integrate rtk for reducing LLM token consumption on agent shell commands#13615
Conversation
…ll commands Bundle rtk and jq binaries into the app package at build time, extract to ~/.cherrystudio/bin/ on first run, and add a PreToolUse hook that rewrites Bash commands via `rtk rewrite` for 60-90% token savings on common shell commands. Closes #13600 Signed-off-by: Vaayne <liu.vaayne@gmail.com>
|
ci failed |
Signed-off-by: Vaayne <liu.vaayne@gmail.com>
| 'darwin-x64': { file: 'rtk-x86_64-apple-darwin.tar.gz', binary: 'rtk' }, | ||
| 'linux-x64': { file: 'rtk-x86_64-unknown-linux-musl.tar.gz', binary: 'rtk' }, | ||
| 'linux-arm64': { file: 'rtk-aarch64-unknown-linux-gnu.tar.gz', binary: 'rtk' }, | ||
| 'win32-x64': { file: 'rtk-x86_64-pc-windows-msvc.zip', binary: 'rtk.exe' } |
There was a problem hiding this comment.
Note
This comment was translated by Claude.
No Windows ARM?
Original Content
没有 win arm 的?
There was a problem hiding this comment.
rtk does not provide it.
| } | ||
|
|
||
| function getBundledBinariesDir(): string { | ||
| const dir = path.join(getResourcePath(), 'binaries', getPlatformKey()) |
There was a problem hiding this comment.
Note
This issue/comment/review was translated by Claude.
If Windows ARM is not supported, consider filtering it out.
Original Content
windows arm 不支持的话,要考虑过滤掉
src/main/utils/rtk.ts
Outdated
| encoding: 'utf8', | ||
| stdio: ['pipe', 'pipe', 'pipe'] | ||
| }) | ||
| const match = output.match(/(\d+)\.(\d+)\.(\d+)/) |
There was a problem hiding this comment.
Note
This issue/comment/review was translated by Claude.
It would be better to use the semver library.
Original Content
使用semver库更好。
| // Download rtk and jq binaries for the target platform | ||
| try { | ||
| console.log(`Downloading rtk/jq binaries for ${platform}-${arch}...`) | ||
| execSync(`node "${path.join(__dirname, 'download-rtk-binaries.js')}" ${platform} ${arch}`, { stdio: 'inherit' }) |
There was a problem hiding this comment.
Note
This comment was translated by Claude.
This will cause an issue. When building packages for both architectures (arm/intel) on the same platform, the second package built will contain binaries for both architectures.
Original Content
这个会有问题,就是在一个平台上面打两个架构(arm/intel)的包的时候,后面打的那个包,会包含两个架构的 binary
There was a problem hiding this comment.
Note
This comment was translated by Claude.
It needs to be added to the file filter like the others, so that packages for other architectures can be filtered out.
Original Content
需要像其它的一样,加到 file filter 里面这样可以过滤掉其它架构的包
There was a problem hiding this comment.
Addressed in caca181 — added excludeRtkFilters in before-pack.js (lines 129-134) that excludes resources/binaries/<other-platform>/** for non-target platform-arch combinations, following the same pattern as excludeRipgrepFilters.
- Filter out unsupported win32-arm64 platform at runtime - Use semver library for version comparison instead of manual parsing - Add file filters in before-pack.js to exclude other-arch rtk binaries - Remove unused jq binary from download script and extraction - Make extractRtkBinaries and version check fully async - Use version file for upgrade detection instead of file size - Upgrade rewrite log level from debug to info - Fix import consistency (node:os) - Add unit tests for rtk utils Signed-off-by: Vaayne <liu.vaayne@gmail.com>
What this PR does
Before this PR:
Agent Bash tool calls output raw, verbose shell command results that consume excessive LLM tokens.
After this PR:
Bash commands are transparently rewritten via rtk to produce concise, LLM-friendly output — reducing token consumption by 60-90% on common shell commands (
cat,grep,find,ls, etc.).Fixes #13600
Why we need it and why it was done in this way
rtk is a single Rust binary (MIT licensed, zero dependencies) that rewrites shell commands into optimized versions. When a command has no rtk equivalent, it passes through unchanged — zero risk of breaking existing workflows.
The integration follows three layers:
scripts/download-rtk-binaries.jsdownloads platform-specific rtk and jq binaries intoresources/binaries/(bundled via existingasarUnpack: resources/**)extractRtkBinaries()copies binaries from app resources to~/.cherrystudio/bin/(follows existing binary distribution pattern used by bun, uv, openclaw)PreToolUsehook in the Claude Code service intercepts Bash tool calls, runsrtk rewrite "<command>", and substitutes the optimized command if availableThe following tradeoffs were made:
rtkRewrite()is async (non-blocking) to avoid stalling the main process event loopThe following alternatives were considered:
Breaking changes
None. The rtk rewrite is transparent and falls back gracefully when rtk is unavailable or a command has no optimized version.
Special notes for your reviewer
preToolUseHook(permission handling), so commands are rewritten before permission checksrtk rewritesubcommand was introduced)Checklist
Release note