feat: Add RTK (Rust Token Killer) integration for token optimization

- Add RTK utility module (src/utils/rtk.js)
- Integrate RTK into BashTool for all bash commands
- Integrate RTK into GitTool for git operations
- Initialize RTK on bot startup
- Support 60+ command types (git, npm, cargo, pytest, docker, etc.)
- Track and report token savings per command
- Graceful fallback when RTK is not available

Expected savings: 60-90% token reduction for supported commands
This commit is contained in:
admin
2026-05-05 09:53:41 +00:00
Unverified
parent 875c7f9b91
commit f98c5ad3f6
4 changed files with 252 additions and 2 deletions

View File

@@ -1,14 +1,35 @@
import { logger } from '../utils/logger.js';
import { getRTK } from '../utils/rtk.js';
import { spawn } from 'child_process';
export class BashTool {
constructor() {
this.name = 'bash';
this.description = 'Execute shell commands';
this.description = 'Execute shell commands with RTK token optimization';
}
async execute(command, options = {}) {
const { timeout = 300000, cwd = process.cwd() } = options;
// Check if RTK is available and command is supported
const rtk = getRTK();
const commandName = command.split(' ')[0];
if (rtk.enabled && rtk.isCommandSupported(commandName)) {
const rtkResult = await rtk.optimizeCommand(commandName, command.split(' ').slice(1));
if (rtkResult.success) {
return {
success: true,
stdout: rtkResult.stdout,
stderr: rtkResult.stderr,
optimized: true,
savings: rtkResult.savings,
};
}
}
// Fallback to original command if RTK is not available or failed
logger.info(`🚀 Executing: ${command.substring(0, 100)}...`);
return new Promise((resolve, reject) => {
@@ -35,6 +56,8 @@ export class BashTool {
success: true,
stdout: stdout.trim(),
stderr: stderr.trim(),
optimized: false,
savings: 0,
});
} else {
reject({
@@ -42,6 +65,8 @@ export class BashTool {
stdout: stdout.trim(),
stderr: stderr.trim(),
code,
optimized: false,
savings: 0,
});
}
});
@@ -50,6 +75,8 @@ export class BashTool {
reject({
success: false,
error: error.message,
optimized: false,
savings: 0,
});
});
});

View File

@@ -1,20 +1,38 @@
import { logger } from '../utils/logger.js';
import { execa } from 'execa';
import { getRTK } from '../utils/rtk.js';
export class GitTool {
constructor() {
this.name = 'git';
this.description = 'Git operations';
this.description = 'Git operations with RTK token optimization';
}
async status() {
try {
const rtk = getRTK();
if (rtk.enabled) {
const rtkResult = await rtk.optimizeCommand('git', ['status', '--short']);
if (rtkResult.success) {
return {
success: true,
status: rtkResult.stdout.trim() || 'clean',
optimized: true,
savings: rtkResult.savings,
};
}
}
// Fallback to original command
const { stdout } = await execa('git', ['status', '--short'], {
cwd: process.cwd(),
});
return {
success: true,
status: stdout.trim() || 'clean',
optimized: false,
savings: 0,
};
} catch (error) {
return {
@@ -26,14 +44,30 @@ export class GitTool {
async log(options = {}) {
const { lines = 10 } = options;
const rtk = getRTK();
try {
if (rtk.enabled) {
const rtkResult = await rtk.optimizeCommand('git', ['log', '--oneline', `-${lines}`]);
if (rtkResult.success) {
return {
success: true,
commits: rtkResult.stdout.trim().split('\n'),
optimized: true,
savings: rtkResult.savings,
};
}
}
// Fallback to original command
const { stdout } = await execa('git', ['log', '--oneline', `-${lines}`], {
cwd: process.cwd(),
});
return {
success: true,
commits: stdout.trim().split('\n'),
optimized: false,
savings: 0,
};
} catch (error) {
return {
@@ -44,13 +78,30 @@ export class GitTool {
}
async branch() {
const rtk = getRTK();
try {
if (rtk.enabled) {
const rtkResult = await rtk.optimizeCommand('git', ['branch', '--show-current']);
if (rtkResult.success) {
return {
success: true,
branch: rtkResult.stdout.trim(),
optimized: true,
savings: rtkResult.savings,
};
}
}
// Fallback to original command
const { stdout } = await execa('git', ['branch', '--show-current'], {
cwd: process.cwd(),
});
return {
success: true,
branch: stdout.trim(),
optimized: false,
savings: 0,
};
} catch (error) {
return {