-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-user-questions.js
More file actions
106 lines (86 loc) · 2.97 KB
/
test-user-questions.js
File metadata and controls
106 lines (86 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Test script to verify getUserQuestions functionality
*/
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const projectPath = '/Users/junwoobang/project/claude-code-spec';
const sessionId = '2e3d94ab-a4c0-4c5b-aaff-07dedd37f39e';
// Convert path to dash format
const pathToDashFormat = (fsPath) => {
return `-${fsPath.replace(/^\//, '').replace(/\//g, '-')}`;
};
// Get session file path
const getClaudeProjectDir = (projectPath) => {
const dashName = pathToDashFormat(projectPath);
return path.join(os.homedir(), '.claude', 'projects', dashName);
};
// Read session log
const readSessionLog = (projectPath, sessionId) => {
const projectDir = getClaudeProjectDir(projectPath);
const sessionFile = path.join(projectDir, `${sessionId}.jsonl`);
if (!fs.existsSync(sessionFile)) {
console.warn(`Session file not found: ${sessionFile}`);
return [];
}
try {
const content = fs.readFileSync(sessionFile, 'utf-8');
return content
.split('\n')
.filter((line) => line.trim())
.map((line) => JSON.parse(line));
} catch (error) {
console.error('Failed to read session log:', error);
return [];
}
};
// Get user questions from session log
const getUserQuestions = (projectPath, sessionId) => {
const entries = readSessionLog(projectPath, sessionId);
return entries.filter((entry) => {
// Check if entry has a message with role 'user'
if (entry.message && typeof entry.message === 'object') {
const message = entry.message;
return message.role === 'user';
}
return false;
});
};
// Test the function
console.log('=== Testing getUserQuestions ===\n');
console.log(`Project: ${projectPath}`);
console.log(`Session ID: ${sessionId}\n`);
const userQuestions = getUserQuestions(projectPath, sessionId);
console.log(`Total entries in session: ${readSessionLog(projectPath, sessionId).length}`);
console.log(`User questions found: ${userQuestions.length}\n`);
console.log('=== User Questions ===\n');
let actualUserQuestions = 0;
userQuestions.forEach((entry, _idx) => {
const content = entry.message.content;
// Skip tool results
if (typeof content === 'string' && content.startsWith('[{"tool_use_id')) {
return;
}
// Skip system messages
if (typeof content === 'string' && content.startsWith('Caveat:')) {
return;
}
// Skip command messages
if (typeof content === 'string' && content.includes('<command-name>')) {
return;
}
// Skip empty stdout
if (typeof content === 'string' && content === '<local-command-stdout></local-command-stdout>') {
return;
}
actualUserQuestions++;
console.log(`\n[Question ${actualUserQuestions}]`);
console.log(`Type: ${entry.type}`);
console.log(
`Content: ${typeof content === 'string' ? content.substring(0, 200) : JSON.stringify(content).substring(0, 200)}`,
);
console.log('---');
});
console.log(
`\n\nActual user questions (excluding tool results and system messages): ${actualUserQuestions}`,
);