cursor-auto-log.js•4.18 kB
#!/usr/bin/env node
import { execSync } from 'child_process';
import { readFileSync, existsSync, readdirSync, statSync } from 'fs';
import { join, basename, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Auto-log conversation script for Cursor
* This script can be called manually or integrated with Cursor's workflow
*/
async function logConversation(projectName, userInput, aiResponse, actions, platform = 'cursor') {
// Truncate long content to avoid API limits
if (userInput.length > 1000) {
userInput = userInput.substring(0, 1000) + '...';
}
if (aiResponse.length > 2000) {
aiResponse = aiResponse.substring(0, 2000) + '...';
}
// Escape quotes for command line
const escapedUserInput = userInput.replace(/"/g, '\\"').replace(/\n/g, '\\n');
const escapedAiResponse = aiResponse.replace(/"/g, '\\"').replace(/\n/g, '\\n');
// Create a temporary MCP request file
const mcpRequest = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "log_conversation",
arguments: {
project: projectName,
userInput: userInput,
aiResponse: aiResponse,
platform: platform,
actions: actions,
tags: ["auto-logged", "cursor"]
}
}
};
try {
console.log('Recording conversation to project:', projectName);
// For now, we'll use the direct import approach instead of MCP
// This is a simplified version that works without MCP protocol
const { ConversationLogger } = await import('../dist/tools/conversationLogger.js');
const logger = new ConversationLogger();
const result = await logger.logConversation({
project: projectName,
userInput: userInput,
aiResponse: aiResponse,
platform: platform,
actions: actions,
tags: ["auto-logged", "cursor"]
});
console.log('✅ Conversation recorded successfully');
return result;
} catch (error) {
console.error('❌ Failed to record conversation:', error.message);
return null;
}
}
function detectProjectFromPath(currentPath) {
// Try to detect project name from current directory
const parts = currentPath.split('/');
// Look for package.json or git repository
let dir = currentPath;
while (dir !== '/' && dir !== '') {
if (existsSync(join(dir, 'package.json')) || existsSync(join(dir, '.git'))) {
return basename(dir);
}
dir = join(dir, '..');
}
// Fallback to current directory name
return basename(currentPath) || 'cursor-project';
}
async function main() {
try {
const args = process.argv.slice(2);
const projectDir = args[0] || process.cwd();
const userInput = args[1] || process.env.CURSOR_USER_INPUT;
const aiResponse = args[2] || process.env.CURSOR_AI_RESPONSE;
const projectName = detectProjectFromPath(projectDir);
console.log(`Auto-logging conversation for project: ${projectName}`);
if (!userInput || !aiResponse) {
console.log('Usage: node cursor-auto-log.js [project_dir] [user_input] [ai_response]');
console.log('Or set environment variables: CURSOR_USER_INPUT, CURSOR_AI_RESPONSE');
return;
}
// Extract actions from AI response (simple heuristic)
const actions = [];
const lines = aiResponse.split('\n');
for (const line of lines) {
if (line.includes('创建') || line.includes('修改') || line.includes('删除') ||
line.includes('create') || line.includes('update') || line.includes('delete') ||
line.includes('编写') || line.includes('实现') || line.includes('添加')) {
actions.push(line.trim());
}
}
await logConversation(projectName, userInput, aiResponse, actions.slice(0, 5), 'cursor');
} catch (error) {
console.error('Auto-log failed:', error.message);
// Don't throw error to avoid disrupting workflow
}
}
// If called directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}
export { logConversation, detectProjectFromPath };