feat: confirm creating missing session cwd

This commit is contained in:
shiyue
2026-03-30 04:46:13 +08:00
parent a3df0cc6f0
commit 86d044f8a9
3 changed files with 160 additions and 7 deletions

View File

@@ -102,6 +102,7 @@
let currentSessionRunning = false;
let fileBrowserState = null;
let directoryPickerState = null;
let pendingNewSessionRequest = null;
let skipDeleteConfirm = localStorage.getItem('cc-web-skip-delete-confirm') === '1';
let pendingInitialSessionLoad = false;
@@ -1747,6 +1748,7 @@
break;
case 'session_info':
if (pendingNewSessionRequest) pendingNewSessionRequest = null;
const snapshot = normalizeSessionSnapshot(msg);
if (activeSessionLoad?.sessionId === msg.sessionId) {
activeSessionLoad.snapshot = snapshot;
@@ -1902,6 +1904,39 @@
break;
case 'error':
if (msg.code === 'new_session_cwd_missing' && pendingNewSessionRequest?.cwd) {
const request = pendingNewSessionRequest;
pendingNewSessionRequest = null;
showSimpleConfirm({
title: '目录不存在',
message: `${msg.cwd || request.cwd}\n\n要先创建这个目录再进入新会话吗?`,
confirmText: '创建目录',
cancelText: '返回修改',
onConfirm: () => {
pendingNewSessionRequest = { ...request };
send({
type: 'new_session',
cwd: request.cwd,
agent: request.agent,
mode: request.mode,
createCwd: true,
});
},
onCancel: () => {
showNewSessionModal({
agent: request.agent,
cwd: request.rawCwd || request.cwd,
mode: request.mode,
});
},
});
clearSessionLoading();
if (!isGenerating && currentSessionId) {
setCurrentSessionRunningState(!!getSessionMeta(currentSessionId)?.isRunning);
}
break;
}
if (pendingNewSessionRequest) pendingNewSessionRequest = null;
appendError(msg.message);
clearSessionLoading();
if (!isGenerating && currentSessionId) {
@@ -2732,6 +2767,47 @@
return '删除本会话将同步删去本地 Claude 中的会话历史,不可恢复。确认删除?';
}
function showSimpleConfirm(options = {}) {
const overlay = document.createElement('div');
overlay.className = 'settings-overlay';
overlay.style.zIndex = '10002';
const box = document.createElement('div');
box.className = 'settings-panel';
const title = options.title ? `<div style="font-size:1em;font-weight:700;color:var(--text-primary);margin-bottom:10px">${escapeHtml(options.title)}</div>` : '';
const confirmText = options.confirmText || '确认';
const cancelText = options.cancelText || '取消';
box.innerHTML = `
${title}
<div style="font-size:0.9em;color:var(--text-primary);margin-bottom:20px;line-height:1.7;word-break:break-word;white-space:pre-line">${escapeHtml(options.message || '')}</div>
<div style="display:flex;flex-direction:column;gap:8px">
<button id="simple-confirm-ok" style="width:100%;padding:10px;border:none;border-radius:10px;background:var(--accent);color:#fff;font-size:0.95em;font-weight:600;cursor:pointer;font-family:inherit">${escapeHtml(confirmText)}</button>
<button id="simple-confirm-cancel" style="width:100%;padding:9px;border:none;border-radius:10px;background:transparent;color:var(--text-muted);font-size:0.85em;cursor:pointer;font-family:inherit">${escapeHtml(cancelText)}</button>
</div>
`;
overlay.appendChild(box);
document.body.appendChild(overlay);
const close = () => {
if (overlay.parentNode) overlay.parentNode.removeChild(overlay);
};
box.querySelector('#simple-confirm-ok').addEventListener('click', () => {
close();
if (typeof options.onConfirm === 'function') options.onConfirm();
});
box.querySelector('#simple-confirm-cancel').addEventListener('click', () => {
close();
if (typeof options.onCancel === 'function') options.onCancel();
});
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
close();
if (typeof options.onCancel === 'function') options.onCancel();
}
});
}
function showDeleteConfirm(agent, onConfirm) {
const overlay = document.createElement('div');
overlay.className = 'settings-overlay';
@@ -4639,10 +4715,11 @@
// --- New Session Modal ---
let _onCwdSuggestions = null;
function showNewSessionModal() {
const targetAgent = currentAgent;
function showNewSessionModal(options = {}) {
const targetAgent = normalizeAgent(options.agent || currentAgent);
const targetLabel = AGENT_LABELS[targetAgent] || AGENT_LABELS.claude;
const recentCwds = getRecentCwds();
const requestedMode = ['default', 'plan', 'yolo'].includes(options.mode) ? options.mode : currentMode;
let suggestionsRequested = false;
let suggestionState = {
defaultPath: '',
@@ -4689,7 +4766,7 @@
const createBtn = overlay.querySelector('#ns-create-btn');
const pickDirBtn = overlay.querySelector('#ns-pick-dir-btn');
cwdInput.value = recentCwds[0] || '';
cwdInput.value = String(options.cwd || recentCwds[0] || '').trim();
function getMergedCwdSuggestions() {
const seen = new Set();
@@ -4759,9 +4836,16 @@
function createSession() {
const cwd = getEffectiveCwd();
const rawCwd = cwdInput.value.trim();
pendingNewSessionRequest = {
cwd,
rawCwd,
agent: targetAgent,
mode: requestedMode,
};
close();
if (cwd) saveRecentCwd(cwd);
send({ type: 'new_session', cwd, agent: targetAgent, mode: currentMode });
send({ type: 'new_session', cwd, agent: targetAgent, mode: requestedMode });
}
pickDirBtn.addEventListener('click', () => {