EasyAgent/demo/lib/commands.js
2026-02-28 03:00:08 +08:00

93 lines
2.9 KiB
JavaScript

'use strict';
const { newConversation } = require('./state');
async function handleCommand(input, state, context) {
const { rl, workspacePath } = context;
const [cmd, ...rest] = input.split(/\s+/);
const arg = rest.join(' ').trim();
if (cmd === '/exit') {
if (context && typeof context.markClosing === 'function') {
context.markClosing();
}
rl.close();
return;
}
if (cmd === '/help') {
printHelp();
return;
}
switch (cmd) {
case '/new':
newConversation(state);
console.log(`已创建新对话: ${state.conversationId}`);
return;
case '/resume':
if (arg) {
state.conversationId = arg;
console.log(`已加载对话: ${state.conversationId}`);
} else {
console.log('Updated Conversation');
console.log('2分钟前 帮我看看...');
console.log('14小时前 查看所有...');
console.log('13天前 ...');
console.log('提示:使用 /resume <id> 直接加载。');
}
return;
case '/allow':
state.allow = state.allow === 'full_access' ? 'read_only' : 'full_access';
console.log(`运行模式已切换为: ${state.allow}`);
return;
case '/model': {
if (!arg) {
state.thinking = state.thinking === 'thinking' ? 'fast' : 'thinking';
console.log(`思考模式已切换为: ${state.thinking}`);
return;
}
const parts = arg.split(/\s+/).filter(Boolean);
state.model = parts[0] || state.model;
if (parts[1]) state.thinking = parts[1];
console.log(`模型已切换为: ${state.model} | 思考模式: ${state.thinking}`);
return;
}
case '/status':
console.log(`model: ${state.model} | 思考: ${state.thinking}`);
console.log(`workspace: ${workspacePath}`);
console.log(`allow: ${state.allow}`);
console.log(`conversation: ${state.conversationId}`);
console.log(`token usage: ${state.tokenUsage}`);
return;
case '/compact': {
const before = state.tokenUsage;
const after = Math.max(0, Math.floor(before * 0.6));
state.tokenUsage = after;
console.log(`压缩完成:${before} -> ${after}`);
return;
}
case '/config':
console.log(`base_url: ${state.baseUrl}`);
console.log(`modelname: ${state.model}`);
console.log(`apikey: ${state.apiKey}`);
return;
default:
console.log(`未知指令: ${cmd},使用 /help 查看指令列表。`);
}
}
function printHelp() {
console.log('/new 创建新对话');
console.log('/resume 加载旧对话');
console.log('/allow 切换运行模式(只读/无限制)');
console.log('/model 切换模型和思考模式');
console.log('/status 查看当前对话状态');
console.log('/compact 压缩当前对话');
console.log('/config 查看当前配置');
console.log('/exit 退出程序');
console.log('/help 显示指令列表');
}
module.exports = { handleCommand, printHelp };