feat: 添加 :new 系统命令创建新会话

- Core: 新增 :new/:n 命令返回 new_session action
- Server: 处理 new_session action 创建新会话
- UI: useChat 添加 onSessionSwitch 回调
- Web/Desktop: ChatPage 和 App 实现会话切换逻辑
This commit is contained in:
2025-12-17 19:36:47 +08:00
parent e0444a966f
commit 48a11ff077
7 changed files with 63 additions and 4 deletions
+11 -2
View File
@@ -33,6 +33,8 @@ interface UseChatOptions {
onSessionUpdated?: (sessionId: string, name: string) => void;
/** 配置错误回调(如 API Key 未配置) */
onConfigError?: (error: ConfigErrorPayload) => void;
/** 切换会话回调(如 :new 命令创建新会话) */
onSessionSwitch?: (newSessionId: string) => void;
}
interface ChatState {
@@ -52,7 +54,7 @@ interface ChatState {
currentSubagent: SubagentState | null;
}
export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdated, onConfigError }: UseChatOptions) {
export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdated, onConfigError, onSessionSwitch }: UseChatOptions) {
const [state, setState] = useState<ChatState>({
messages: [],
isConnected: false,
@@ -77,10 +79,12 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate
const onSessionNotFoundRef = useRef(onSessionNotFound);
const onSessionUpdatedRef = useRef(onSessionUpdated);
const onConfigErrorRef = useRef(onConfigError);
const onSessionSwitchRef = useRef(onSessionSwitch);
onErrorRef.current = onError;
onSessionNotFoundRef.current = onSessionNotFound;
onSessionUpdatedRef.current = onSessionUpdated;
onConfigErrorRef.current = onConfigError;
onSessionSwitchRef.current = onSessionSwitch;
// 加载历史消息
const loadMessages = useCallback(async () => {
@@ -572,7 +576,7 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate
success: boolean;
message?: string;
error?: string;
action?: { type: string };
action?: { type: string; sessionId?: string };
};
// 处理清空消息操作
@@ -585,6 +589,11 @@ export function useChat({ sessionId, onError, onSessionNotFound, onSessionUpdate
}));
}
// 处理新建会话操作
if (payload.success && payload.action?.type === 'new_session' && payload.action.sessionId) {
onSessionSwitchRef.current?.(payload.action.sessionId);
}
// 如果有错误,通过 onError 回调通知
if (!payload.success && payload.error) {
onErrorRef.current?.(new Error(payload.error));