feat: Enhance session handling by adding cursor support and improving cursor messages order

This commit is contained in:
simos
2025-08-12 13:43:36 +03:00
parent cd6e5befb8
commit 3e7e60a3a8
7 changed files with 51 additions and 24 deletions

View File

@@ -546,14 +546,17 @@ function handleShellConnection(ws) {
const projectPath = data.projectPath || process.cwd(); const projectPath = data.projectPath || process.cwd();
const sessionId = data.sessionId; const sessionId = data.sessionId;
const hasSession = data.hasSession; const hasSession = data.hasSession;
const provider = data.provider || 'claude';
console.log('🚀 Starting shell in:', projectPath); console.log('🚀 Starting shell in:', projectPath);
console.log('📋 Session info:', hasSession ? `Resume session ${sessionId}` : 'New session'); console.log('📋 Session info:', hasSession ? `Resume session ${sessionId}` : 'New session');
console.log('🤖 Provider:', provider);
// First send a welcome message // First send a welcome message
const providerName = provider === 'cursor' ? 'Cursor' : 'Claude';
const welcomeMsg = hasSession ? const welcomeMsg = hasSession ?
`\x1b[36mResuming Claude session ${sessionId} in: ${projectPath}\x1b[0m\r\n` : `\x1b[36mResuming ${providerName} session ${sessionId} in: ${projectPath}\x1b[0m\r\n` :
`\x1b[36mStarting new Claude session in: ${projectPath}\x1b[0m\r\n`; `\x1b[36mStarting new ${providerName} session in: ${projectPath}\x1b[0m\r\n`;
ws.send(JSON.stringify({ ws.send(JSON.stringify({
type: 'output', type: 'output',
@@ -561,20 +564,38 @@ function handleShellConnection(ws) {
})); }));
try { try {
// Prepare the shell command adapted to the platform // Prepare the shell command adapted to the platform and provider
let shellCommand; let shellCommand;
if (os.platform() === 'win32') { if (provider === 'cursor') {
if (hasSession && sessionId) { // Use cursor-agent command
// Try to resume session, but with fallback to new session if it fails if (os.platform() === 'win32') {
shellCommand = `Set-Location -Path "${projectPath}"; claude --resume ${sessionId}; if ($LASTEXITCODE -ne 0) { claude }`; if (hasSession && sessionId) {
shellCommand = `Set-Location -Path "${projectPath}"; cursor-agent --resume="${sessionId}"`;
} else {
shellCommand = `Set-Location -Path "${projectPath}"; cursor-agent`;
}
} else { } else {
shellCommand = `Set-Location -Path "${projectPath}"; claude`; if (hasSession && sessionId) {
shellCommand = `cd "${projectPath}" && cursor-agent --resume="${sessionId}"`;
} else {
shellCommand = `cd "${projectPath}" && cursor-agent`;
}
} }
} else { } else {
if (hasSession && sessionId) { // Use claude command (default)
shellCommand = `cd "${projectPath}" && claude --resume ${sessionId} || claude`; if (os.platform() === 'win32') {
if (hasSession && sessionId) {
// Try to resume session, but with fallback to new session if it fails
shellCommand = `Set-Location -Path "${projectPath}"; claude --resume ${sessionId}; if ($LASTEXITCODE -ne 0) { claude }`;
} else {
shellCommand = `Set-Location -Path "${projectPath}"; claude`;
}
} else { } else {
shellCommand = `cd "${projectPath}" && claude`; if (hasSession && sessionId) {
shellCommand = `cd "${projectPath}" && claude --resume ${sessionId} || claude`;
} else {
shellCommand = `cd "${projectPath}" && claude`;
}
} }
} }

View File

@@ -351,7 +351,6 @@ router.get('/sessions', async (req, res) => {
const cwdId = crypto.createHash('md5').update(projectPath || process.cwd()).digest('hex'); const cwdId = crypto.createHash('md5').update(projectPath || process.cwd()).digest('hex');
const cursorChatsPath = path.join(os.homedir(), '.cursor', 'chats', cwdId); const cursorChatsPath = path.join(os.homedir(), '.cursor', 'chats', cwdId);
console.log(`🔍 Looking for Cursor sessions in: ${cursorChatsPath}`);
// Check if the directory exists // Check if the directory exists
try { try {
@@ -465,7 +464,7 @@ router.get('/sessions', async (req, res) => {
// Get the most recent blob for preview // Get the most recent blob for preview
const lastBlob = await db.get(` const lastBlob = await db.get(`
SELECT data FROM blobs SELECT data FROM blobs
ORDER BY id DESC ORDER BY rowid DESC
LIMIT 1 LIMIT 1
`); `);
@@ -593,9 +592,10 @@ router.get('/sessions/:sessionId', async (req, res) => {
}); });
// Get all blobs (conversation data) // Get all blobs (conversation data)
// Use rowid for chronological ordering (it's an auto-incrementing integer)
const blobs = await db.all(` const blobs = await db.all(`
SELECT id, data FROM blobs SELECT rowid, id, data FROM blobs
ORDER BY id ASC ORDER BY rowid ASC
`); `);
// Get metadata from meta table // Get metadata from meta table
@@ -659,7 +659,11 @@ router.get('/sessions/:sessionId', async (req, res) => {
if (role === 'system') { if (role === 'system') {
continue; // Skip only system messages continue; // Skip only system messages
} }
messages.push({ id: blob.id, content: parsed }); messages.push({
id: blob.id,
rowid: blob.rowid,
content: parsed
});
} }
// Skip non-JSON blobs (binary data) completely // Skip non-JSON blobs (binary data) completely
} catch (e) { } catch (e) {

View File

@@ -56,7 +56,6 @@ router.get('/status', async (req, res) => {
try { try {
const projectPath = await getActualProjectPath(project); const projectPath = await getActualProjectPath(project);
console.log('Git status for project:', project, '-> path:', projectPath);
// Validate git repository // Validate git repository
await validateGitRepository(projectPath); await validateGitRepository(projectPath);
@@ -136,13 +135,16 @@ router.get('/diff', async (req, res) => {
lines.map(line => `+${line}`).join('\n'); lines.map(line => `+${line}`).join('\n');
} else { } else {
// Get diff for tracked files // Get diff for tracked files
const { stdout } = await execAsync(`git diff HEAD -- "${file}"`, { cwd: projectPath }); // First check for unstaged changes (working tree vs index)
diff = stdout || ''; const { stdout: unstagedDiff } = await execAsync(`git diff -- "${file}"`, { cwd: projectPath });
// If no unstaged changes, check for staged changes if (unstagedDiff) {
if (!diff) { // Show unstaged changes if they exist
diff = unstagedDiff;
} else {
// If no unstaged changes, check for staged changes (index vs HEAD)
const { stdout: stagedDiff } = await execAsync(`git diff --cached -- "${file}"`, { cwd: projectPath }); const { stdout: stagedDiff } = await execAsync(`git diff --cached -- "${file}"`, { cwd: projectPath });
diff = stagedDiff; diff = stagedDiff || '';
} }
} }

View File

@@ -436,6 +436,7 @@ function Shell({ selectedProject, selectedSession, isActive }) {
projectPath: selectedProject.fullPath || selectedProject.path, projectPath: selectedProject.fullPath || selectedProject.path,
sessionId: selectedSession?.id, sessionId: selectedSession?.id,
hasSession: !!selectedSession, hasSession: !!selectedSession,
provider: selectedSession?.__provider || 'claude',
cols: terminal.current.cols, cols: terminal.current.cols,
rows: terminal.current.rows rows: terminal.current.rows
}; };

BIN
store.db-shm Normal file

Binary file not shown.

0
store.db-wal Normal file
View File

View File

@@ -1,2 +1 @@
world world 3 hello world 5
world world 4