feat: 添加系统命令支持 (:clear)

- 新增系统命令模块 (core/system-commands)
  - 支持 :clear/:cls/:c 清空对话历史
  - 命令注册表支持别名
  - 可扩展的命令执行器

- Server 端支持
  - 新增 /api/system-commands API
  - WebSocket 处理系统命令消息
  - 会话清空 API 端点

- UI 端支持
  - 新增 SystemCommandMenu 组件
  - 输入 : 时显示命令建议菜单
  - 键盘导航和选择
  - 底部提示添加 : 快捷键
This commit is contained in:
2025-12-17 19:25:42 +08:00
parent 4fc6b61134
commit e0444a966f
21 changed files with 1109 additions and 9 deletions
+58 -1
View File
@@ -6,9 +6,18 @@
import type { WSContext } from 'hono/ws';
import { getSessionManager } from './session/manager.js';
import { processMessage, cancelProcessing, getOrCreateAgent, submitUserInput } from './agent/index.js';
import { processMessage, cancelProcessing, getOrCreateAgent, submitUserInput, clearAgentHistory } from './agent/index.js';
import { handlePermissionResponse, setSessionAutoApprove } from './permission/handler.js';
import type { ClientMessage, ServerMessage } from './types.js';
import {
isSystemCommand,
executeSystemCommand,
initializeSystemCommands,
MessageStorage,
} from '@ai-assistant/core';
// 初始化系统命令
initializeSystemCommands();
// 存储活跃的 WebSocket 连接
const connections: Map<string, Set<WSContext>> = new Map();
@@ -109,6 +118,12 @@ export async function handleWebSocketMessage(
const agentMode = message.payload?.agentMode as 'build' | 'plan' | undefined;
const autoApprove = message.payload?.autoApprove as boolean | undefined;
// 检测系统命令(: 前缀)
if (isSystemCommand(content)) {
await handleSystemCommand(sessionId, content);
break;
}
// 将 @filepath 转换为 ./filepath 格式(方便 AI 识别为文件路径)
content = content.replace(/@([\w./-]+)/g, './$1');
@@ -281,3 +296,45 @@ export function getConnectionStats(): { sessions: number; connections: number }
connections: totalConnections,
};
}
/**
* 处理系统命令(: 前缀)
*/
async function handleSystemCommand(sessionId: string, content: string): Promise<void> {
const sessionManager = getSessionManager();
console.log(`[WS] System command: ${content}`);
// 执行系统命令
const result = await executeSystemCommand(content, { sessionId });
// 处理特殊操作
if (result.success && result.action) {
switch (result.action.type) {
case 'clear_messages': {
// 清空存储的消息
await MessageStorage.removeBySession(sessionId);
// 清空 Agent 内存中的对话历史
clearAgentHistory(sessionId);
// 重置会话状态
sessionManager.updateStatus(sessionId, 'idle');
break;
}
// 可以在这里添加其他操作类型的处理
}
}
// 发送结果给客户端
broadcastToSession(sessionId, {
type: 'system_command_result',
sessionId,
payload: {
success: result.success,
message: result.message,
error: result.error,
action: result.action,
},
});
}