Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
*.tsbuildinfo
.env
Expand Down
94 changes: 94 additions & 0 deletions dist/agent/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,49 @@ const DIRECT_COMMANDS = {
});
emitDone(ctx);
},
'/history': (ctx) => {
const { history, config } = ctx;
const modelName = config.model.split('/').pop() || config.model;
let output = '**Conversation History**\n\n';
if (history.length === 0) {
output += 'No history in the current session yet.\n';
}
else {
for (let i = 0; i < history.length; i++) {
const turn = history[i];
const rolePrefix = turn.role === 'user' ? '[user]' : `[${modelName}]`;
const numPrefix = `[${i + 1}]`;
let turnText = '';
if (typeof turn.content === 'string') {
turnText = turn.content;
}
else if (Array.isArray(turn.content)) {
const textParts = turn.content
.filter(p => p.type === 'text' && p.text.trim())
.map(p => p.text.trim());
if (textParts.length > 0) {
turnText = textParts.join(' ');
}
else {
const toolCall = turn.content.find(p => p.type === 'tool_use');
if (toolCall) {
turnText = `(Thinking and using tool: ${toolCall.name})`;
}
const toolResult = turn.content.find(p => p.type === 'tool_result');
if (toolResult) {
turnText = `(Processing tool result)`;
}
}
}
if (turnText.trim()) {
output += `${numPrefix} ${rolePrefix} ${turnText.trim()}\n\n`;
}
}
}
output += '\nUse `/delete <number>` to remove turns (e.g., `/delete 2` or `/delete 3-5`).\n';
ctx.onEvent({ kind: 'text_delta', text: output });
emitDone(ctx);
},
'/bug': (ctx) => {
ctx.onEvent({ kind: 'text_delta', text: 'Report issues at: https://github.com/BlockRunAI/runcode/issues\n' });
emitDone(ctx);
Expand Down Expand Up @@ -439,6 +482,57 @@ export async function handleSlashCommand(input, ctx) {
emitDone(ctx);
return { handled: true };
}
// /delete <...>
if (input.startsWith('/delete ')) {
const arg = input.slice('/delete '.length).trim();
if (!arg) {
ctx.onEvent({ kind: 'text_delta', text: 'Usage: /delete <turn_number> (e.g., /delete 3, /delete 2,5, /delete 4-7)\n' });
emitDone(ctx);
return { handled: true };
}
const indicesToDelete = new Set();
const parts = arg.split(',').map(p => p.trim());
for (const part of parts) {
if (part.includes('-')) {
const [start, end] = part.split('-').map(n => parseInt(n, 10));
if (!isNaN(start) && !isNaN(end) && start <= end) {
for (let i = start; i <= end; i++) {
indicesToDelete.add(i - 1); // User sees 1-based, we use 0-based
}
}
}
else {
const index = parseInt(part, 10);
if (!isNaN(index)) {
indicesToDelete.add(index - 1); // 0-based
}
}
}
if (indicesToDelete.size === 0) {
ctx.onEvent({ kind: 'text_delta', text: 'No valid turn numbers provided.\n' });
emitDone(ctx);
return { handled: true };
}
const sortedIndices = Array.from(indicesToDelete).sort((a, b) => b - a); // Sort descending
let deletedCount = 0;
const deletedNumbers = [];
for (const index of sortedIndices) {
if (index >= 0 && index < ctx.history.length) {
ctx.history.splice(index, 1);
deletedCount++;
deletedNumbers.push(index + 1);
}
}
if (deletedCount > 0) {
resetTokenAnchor();
ctx.onEvent({ kind: 'text_delta', text: `Deleted turn(s) ${deletedNumbers.reverse().join(', ')} from history.\n` });
}
else {
ctx.onEvent({ kind: 'text_delta', text: `No matching turns found to delete.\n` });
}
emitDone(ctx);
return { handled: true };
}
// /resume <id>
if (input.startsWith('/resume ')) {
const targetId = input.slice(8).trim();
Expand Down
1 change: 0 additions & 1 deletion dist/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function saveConfig(config) {
}
catch (err) {
console.error(chalk.red(`Failed to save config: ${err.message}`));
process.exit(1);
}
}
function isValidKey(key) {
Expand Down
5 changes: 5 additions & 0 deletions dist/commands/history.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface HistoryOptions {
n?: string;
}
export declare function historyCommand(options: HistoryOptions): void;
export {};
31 changes: 31 additions & 0 deletions dist/commands/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import chalk from 'chalk';
import { loadStats } from '../stats/tracker.js';
export function historyCommand(options) {
const { history } = loadStats();
const limit = Math.min(parseInt(options.n || '20', 10), history.length);
console.log(chalk.bold(`
📜 Last ${limit} Requests\n`));
console.log('─'.repeat(55));
if (history.length === 0) {
console.log(chalk.gray('\n No history recorded yet.\n'));
console.log('─'.repeat(55) + '\n');
return;
}
const recent = history.slice(-limit).reverse();
for (const record of recent) {
const time = new Date(record.timestamp).toLocaleString();
const model = record.model.split('/').pop() || record.model;
const cost = '$' + record.costUsd.toFixed(5);
const tokens = `${record.inputTokens}+${record.outputTokens}`.padEnd(10);
const latency = `${record.latencyMs}ms`.padEnd(8);
const fallbackMark = record.fallback ? chalk.yellow(' ↺') : '';
console.log(chalk.gray(`[${time}]`) +
` ${model.padEnd(20)}${fallbackMark} ` +
chalk.cyan(tokens) +
chalk.magenta(latency) +
chalk.green(cost));
}
console.log('\n' + '─'.repeat(55));
console.log(chalk.gray(` Showing ${limit} of ${history.length} total records.`));
console.log(chalk.gray(' Run `runcode stats` for more detailed statistics.\n'));
}
19 changes: 2 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions src/agent/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,50 @@ const DIRECT_COMMANDS: Record<string, (ctx: CommandContext) => Promise<void> | v
});
emitDone(ctx);
},
'/history': (ctx) => {
const { history, config } = ctx;
const modelName = config.model.split('/').pop() || config.model;
let output = '**Conversation History**\n\n';

if (history.length === 0) {
output += 'No history in the current session yet.\n';
} else {
for (let i = 0; i < history.length; i++) {
const turn = history[i];
const rolePrefix = turn.role === 'user' ? '[user]' : `[${modelName}]`;
const numPrefix = `[${i + 1}]`;
let turnText = '';

if (typeof turn.content === 'string') {
turnText = turn.content;
} else if (Array.isArray(turn.content)) {
const textParts = turn.content
.filter(p => p.type === 'text' && p.text.trim())
.map(p => (p as { text: string }).text.trim());

if (textParts.length > 0) {
turnText = textParts.join(' ');
} else {
const toolCall = turn.content.find(p => p.type === 'tool_use');
if (toolCall) {
turnText = `(Thinking and using tool: ${toolCall.name})`;
}
const toolResult = turn.content.find(p => p.type === 'tool_result');
if (toolResult) {
turnText = `(Processing tool result)`;
}
}
}

if (turnText.trim()) {
output += `${numPrefix} ${rolePrefix} ${turnText.trim()}\n\n`;
}
}
}
output += '\nUse `/delete <number>` to remove turns (e.g., `/delete 2` or `/delete 3-5`).\n';
ctx.onEvent({ kind: 'text_delta', text: output });
emitDone(ctx);
},
'/bug': (ctx) => {
ctx.onEvent({ kind: 'text_delta', text: 'Report issues at: https://github.com/BlockRunAI/runcode/issues\n' });
emitDone(ctx);
Expand Down Expand Up @@ -445,6 +489,63 @@ export async function handleSlashCommand(
return { handled: true };
}

// /delete <...>
if (input.startsWith('/delete ')) {
const arg = input.slice('/delete '.length).trim();
if (!arg) {
ctx.onEvent({ kind: 'text_delta', text: 'Usage: /delete <turn_number> (e.g., /delete 3, /delete 2,5, /delete 4-7)\n' });
emitDone(ctx);
return { handled: true };
}

const indicesToDelete = new Set<number>();
const parts = arg.split(',').map(p => p.trim());

for (const part of parts) {
if (part.includes('-')) {
const [start, end] = part.split('-').map(n => parseInt(n, 10));
if (!isNaN(start) && !isNaN(end) && start <= end) {
for (let i = start; i <= end; i++) {
indicesToDelete.add(i - 1); // User sees 1-based, we use 0-based
}
}
} else {
const index = parseInt(part, 10);
if (!isNaN(index)) {
indicesToDelete.add(index - 1); // 0-based
}
}
}

if (indicesToDelete.size === 0) {
ctx.onEvent({ kind: 'text_delta', text: 'No valid turn numbers provided.\n' });
emitDone(ctx);
return { handled: true };
}

const sortedIndices = Array.from(indicesToDelete).sort((a, b) => b - a); // Sort descending
let deletedCount = 0;
const deletedNumbers: number[] = [];

for (const index of sortedIndices) {
if (index >= 0 && index < ctx.history.length) {
ctx.history.splice(index, 1);
deletedCount++;
deletedNumbers.push(index + 1);
}
}

if (deletedCount > 0) {
resetTokenAnchor();
ctx.onEvent({ kind: 'text_delta', text: `Deleted turn(s) ${deletedNumbers.reverse().join(', ')} from history.\n` });
} else {
ctx.onEvent({ kind: 'text_delta', text: `No matching turns found to delete.\n` });
}

emitDone(ctx);
return { handled: true };
}

// /resume <id>
if (input.startsWith('/resume ')) {
const targetId = input.slice(8).trim();
Expand Down
1 change: 0 additions & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function saveConfig(config: AppConfig): void {
});
} catch (err) {
console.error(chalk.red(`Failed to save config: ${(err as Error).message}`));
process.exit(1);
}
}

Expand Down