@jhihjian/claude-daemon 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/QUICKSTART.md ADDED
@@ -0,0 +1,214 @@
1
+ # Claude Code 会话历史系统 - 快速开始
2
+
3
+ ## 🚀 在当前电脑上使用
4
+
5
+ 系统已经安装并运行!
6
+
7
+ ### 查询会话
8
+
9
+ ```bash
10
+ # 查看最近的会话
11
+ ~/.bun/bin/bun /data/app/claude-history/tools/SessionQuery.ts recent 5
12
+
13
+ # 查看会话详情(包含完整对话)
14
+ /data/app/claude-history/tools/show-conversation.sh <session_id>
15
+
16
+ # 查看统计信息
17
+ ~/.bun/bin/bun /data/app/claude-history/tools/SessionStats.ts global
18
+ ```
19
+
20
+ ---
21
+
22
+ ## 📦 部署到其他电脑
23
+
24
+ ### 方法 1: 打包传输(最简单)
25
+
26
+ **在当前电脑上:**
27
+ ```bash
28
+ cd /data/app
29
+ ./claude-history/package.sh
30
+ # 生成: claude-history-system-YYYYMMDD.tar.gz
31
+ ```
32
+
33
+ **传输到目标电脑:**
34
+ ```bash
35
+ scp claude-history-system-*.tar.gz user@target:/tmp/
36
+ ```
37
+
38
+ **在目标电脑上:**
39
+ ```bash
40
+ cd /tmp
41
+ tar -xzf claude-history-system-*.tar.gz
42
+ cd claude-history
43
+ ./install.sh
44
+ ```
45
+
46
+ ### 方法 2: Git 克隆
47
+
48
+ **在目标电脑上:**
49
+ ```bash
50
+ git clone <你的仓库地址> /data/app/claude-history
51
+ cd /data/app/claude-history
52
+ ./install.sh
53
+ ```
54
+
55
+ ---
56
+
57
+ ## 🔄 数据同步设置
58
+
59
+ ### 选项 A: Git 同步(推荐)
60
+
61
+ **1. 初始化 Git 仓库(在第一台电脑上):**
62
+ ```bash
63
+ cd /data/app/claude-history
64
+ ./setup-git.sh
65
+ ```
66
+
67
+ 按提示操作:
68
+ - 选择 GitHub/GitLab
69
+ - 输入用户名
70
+ - 创建私有仓库
71
+
72
+ **2. 设置自动同步:**
73
+ ```bash
74
+ ./setup-auto-sync.sh
75
+ ```
76
+
77
+ 选择同步频率(推荐:每小时)
78
+
79
+ **3. 在其他电脑上克隆数据:**
80
+ ```bash
81
+ # 先安装系统
82
+ cd /tmp && tar -xzf claude-history-system-*.tar.gz
83
+ cd claude-history && ./install.sh
84
+
85
+ # 克隆会话数据
86
+ git clone git@github.com:你的用户名/claude-sessions.git ~/.claude/SESSIONS
87
+
88
+ # 设置自动同步
89
+ cd /data/app/claude-history
90
+ ./setup-auto-sync.sh
91
+ ```
92
+
93
+ ### 选项 B: 云存储同步
94
+
95
+ **使用 Dropbox/Google Drive:**
96
+ ```bash
97
+ # 移动数据到云存储
98
+ mv ~/.claude/SESSIONS ~/Dropbox/claude-sessions
99
+
100
+ # 创建符号链接
101
+ ln -s ~/Dropbox/claude-sessions ~/.claude/SESSIONS
102
+ ```
103
+
104
+ 在其他电脑上重复相同操作。
105
+
106
+ ---
107
+
108
+ ## 🔍 验证安装
109
+
110
+ ```bash
111
+ # 1. 测试会话记录
112
+ echo "测试会话记录" | claude -p
113
+
114
+ # 2. 等待几秒
115
+ sleep 3
116
+
117
+ # 3. 查询最新会话
118
+ ~/.bun/bin/bun /data/app/claude-history/tools/SessionQuery.ts recent 1
119
+
120
+ # 4. 查看对话内容
121
+ ~/.bun/bin/bun /data/app/claude-history/tools/SessionQuery.ts recent 1 | \
122
+ jq -r '.[0].session_id' | \
123
+ xargs /data/app/claude-history/tools/show-conversation.sh
124
+ ```
125
+
126
+ ---
127
+
128
+ ## 📊 常用命令
129
+
130
+ ```bash
131
+ # 查询
132
+ claude-sessions recent 10 # 最近10个会话
133
+ claude-sessions type coding # 编码类会话
134
+ claude-sessions stats global # 全局统计
135
+ claude-sessions show <session_id> # 会话详情
136
+
137
+ # 同步(如果使用 Git)
138
+ cd ~/.claude/SESSIONS && git pull # 拉取更新
139
+ cd ~/.claude/SESSIONS && git push # 推送更新
140
+ /data/app/claude-history/sync-git.sh # 自动同步
141
+
142
+ # 日志
143
+ tail -f ~/.claude/sync.log # 查看同步日志
144
+ ```
145
+
146
+ ---
147
+
148
+ ## ⚠️ 重要提示
149
+
150
+ ### 数据隐私
151
+ - ✅ 使用**私有仓库**存储会话数据
152
+ - ✅ 不要提交包含敏感信息的会话
153
+ - ✅ 定期清理旧数据
154
+
155
+ ### 冲突处理
156
+ 如果多台电脑同时工作导致冲突:
157
+ ```bash
158
+ cd ~/.claude/SESSIONS
159
+ git pull --rebase
160
+ # 如果有冲突,sync-git.sh 会自动合并 JSONL 文件
161
+ ```
162
+
163
+ ### 性能优化
164
+ ```bash
165
+ # 归档旧数据(3个月前)
166
+ find ~/.claude/SESSIONS/raw -type f -mtime +90 -exec gzip {} \;
167
+ ```
168
+
169
+ ---
170
+
171
+ ## 🆘 故障排除
172
+
173
+ ### Hooks 不工作
174
+ ```bash
175
+ # 检查 hooks 权限
176
+ ls -la ~/.claude/hooks/
177
+
178
+ # 手动测试 hook
179
+ echo '{"session_id":"test"}' | ~/.claude/hooks/SessionRecorder.hook.ts
180
+ ```
181
+
182
+ ### 同步失败
183
+ ```bash
184
+ # 查看同步日志
185
+ tail -50 ~/.claude/sync.log
186
+
187
+ # 手动同步
188
+ cd ~/.claude/SESSIONS
189
+ git status
190
+ git pull
191
+ git push
192
+ ```
193
+
194
+ ### 查询工具找不到
195
+ ```bash
196
+ # 重新加载 shell 配置
197
+ source ~/.zshrc # 或 ~/.bashrc
198
+
199
+ # 检查 PATH
200
+ echo $PATH | grep "$HOME/bin"
201
+ ```
202
+
203
+ ---
204
+
205
+ ## 📚 更多信息
206
+
207
+ - 完整文档: `/data/app/claude-history/README.md`
208
+ - 同步指南: `/data/app/claude-history/SYNC-GUIDE.md`
209
+ - 故障排除: `/data/app/claude-history/README.md#故障排除`
210
+
211
+ ---
212
+
213
+ **系统版本**: v1.0
214
+ **最后更新**: 2026-01-24
package/README.md ADDED
@@ -0,0 +1,173 @@
1
+ # Claude Code 会话历史记录系统
2
+
3
+ > 自动记录、分类和分析 Claude Code 会话历史
4
+
5
+ ## 功能特性
6
+
7
+ - ✅ **自动记录**:捕获每个会话的启动目录、Git 信息、工具调用
8
+ - ✅ **智能分类**:自动识别会话类型(编码、调试、研究、写作、Git 操作等)
9
+ - ✅ **多维索引**:按类型、按目录、按时间快速查询
10
+ - ✅ **统计分析**:会话统计、类型分布、活跃目录分析
11
+ - ✅ **JSONL 存储**:流式写入,易于解析和处理
12
+
13
+ ## 目录结构
14
+
15
+ ```
16
+ /data/app/claude-history/
17
+ ├── hooks/ # Claude Code Hooks
18
+ │ ├── SessionRecorder.hook.ts # 会话启动时记录
19
+ │ ├── SessionToolCapture.hook.ts # 工具调用时记录
20
+ │ └── SessionAnalyzer.hook.ts # 会话结束时分析
21
+ ├── tools/ # 查询工具
22
+ │ ├── SessionQuery.ts # 会话查询
23
+ │ └── SessionStats.ts # 统计分析
24
+ ├── lib/ # 共享库(预留)
25
+ └── test/ # 测试脚本(预留)
26
+ ```
27
+
28
+ ## 数据存储结构
29
+
30
+ ```
31
+ ~/.claude/SESSIONS/
32
+ ├── raw/ # 原始事件流(JSONL)
33
+ │ └── 2026-01/
34
+ │ └── session-{id}.jsonl
35
+ ├── analysis/
36
+ │ ├── by-type/ # 按类型索引
37
+ │ │ ├── coding/sessions.json
38
+ │ │ ├── debugging/sessions.json
39
+ │ │ └── ...
40
+ │ ├── by-directory/ # 按目录索引
41
+ │ │ └── {base64-path}/
42
+ │ │ ├── path.txt
43
+ │ │ └── sessions.json
44
+ │ └── summaries/ # 会话摘要
45
+ │ └── 2026-01/
46
+ │ └── summary-{id}.json
47
+ └── index/
48
+ └── metadata.json # 全局元数据
49
+ ```
50
+
51
+ ## 快速开始
52
+
53
+ ### 1. 安装 Hooks
54
+
55
+ 将 hooks 注册到 Claude Code:
56
+
57
+ ```bash
58
+ # 假设 PAI_DIR=~/.claude
59
+ cp hooks/*.hook.ts ~/.claude/hooks/
60
+
61
+ # 或者创建符号链接
62
+ ln -s /data/app/claude-history/hooks/*.hook.ts ~/.claude/hooks/
63
+ ```
64
+
65
+ ### 2. 配置 Hook 触发器
66
+
67
+ 在 `~/.claude/settings.json` 中添加:
68
+
69
+ ```json
70
+ {
71
+ "hooks": {
72
+ "SessionStart": ["SessionRecorder.hook.ts"],
73
+ "PostToolUse": ["SessionToolCapture.hook.ts"],
74
+ "Stop": ["SessionAnalyzer.hook.ts"]
75
+ }
76
+ }
77
+ ```
78
+
79
+ ### 3. 使用查询工具
80
+
81
+ ```bash
82
+ # 查询最近的会话
83
+ bun /data/app/claude-history/tools/SessionQuery.ts recent 10
84
+
85
+ # 查询编码类会话
86
+ bun /data/app/claude-history/tools/SessionQuery.ts type coding
87
+
88
+ # 查询特定目录的会话
89
+ bun /data/app/claude-history/tools/SessionQuery.ts dir /path/to/project
90
+
91
+ # 查看统计信息
92
+ bun /data/app/claude-history/tools/SessionStats.ts global
93
+ bun /data/app/claude-history/tools/SessionStats.ts types
94
+ bun /data/app/claude-history/tools/SessionStats.ts dirs 10
95
+ ```
96
+
97
+ ## 会话类型
98
+
99
+ 系统自动识别以下会话类型:
100
+
101
+ | 类型 | 描述 | 判断依据 |
102
+ |------|------|---------|
103
+ | `coding` | 编码 | Edit/Write 操作 > 40% |
104
+ | `debugging` | 调试 | 有测试命令 + Read > Edit |
105
+ | `research` | 研究 | Grep/Glob > 30% + Read > Edit |
106
+ | `writing` | 写作 | Markdown 文件编辑 > 50% |
107
+ | `git` | Git 操作 | Git 命令 > 50% |
108
+ | `mixed` | 混合 | 无明显模式 |
109
+
110
+ ## 技术细节
111
+
112
+ - **运行时**:Bun
113
+ - **存储格式**:JSONL(每行一个 JSON 对象)
114
+ - **索引策略**:增量更新,按时间倒序
115
+ - **路径编码**:Base64(避免文件系统特殊字符)
116
+ - **性能**:Hook 执行时间 < 50ms,不阻塞 Claude Code
117
+
118
+ ## 开发
119
+
120
+ ```bash
121
+ # 测试 Hook
122
+ echo '{"session_id":"test123","timestamp":"2026-01-23T10:00:00Z"}' | \
123
+ bun hooks/SessionRecorder.hook.ts
124
+
125
+ # 测试查询
126
+ bun tools/SessionQuery.ts recent 5
127
+
128
+ # 测试统计
129
+ bun tools/SessionStats.ts global
130
+ ```
131
+
132
+ ## 故障排除
133
+
134
+ ### Hooks 不执行
135
+
136
+ **问题**: Hooks 配置正确但不记录数据
137
+
138
+ **原因**: Hook 脚本的 shebang 使用 `#!/usr/bin/env bun`,但 Claude Code 执行 hooks 时 `~/.bun/bin` 不在 PATH 中
139
+
140
+ **解决方案**: 使用 Bun 的完整路径
141
+ ```bash
142
+ # 修改所有 hook 文件的第一行
143
+ #!/home/jhihjian/.bun/bin/bun
144
+ ```
145
+
146
+ ### 工具输出为空或 success_rate 为 0
147
+
148
+ **问题**: 会话记录显示 `tool_output: ""` 和 `success: false`
149
+
150
+ **原因**: 早期版本的 SessionToolCapture hook 没有正确读取 PostToolUse 事件中的 `tool_response` 字段
151
+
152
+ **解决方案**: 使用最新版本的 SessionToolCapture-v2.hook.ts,它会:
153
+ 1. 优先从 `event.tool_response.stdout/stderr` 读取输出
154
+ 2. 从 `!event.tool_response.interrupted` 判断成功状态
155
+ 3. 备用方案:从 transcript 文件读取
156
+
157
+ ### 验证 Hooks 是否工作
158
+
159
+ ```bash
160
+ # 1. 手动测试 hook
161
+ echo '{"session_id":"test","tool_name":"Bash"}' | ~/.bun/bin/bun ~/.claude/hooks/SessionRecorder.hook.ts
162
+
163
+ # 2. 运行测试会话
164
+ echo "请运行 date 命令" | claude -p --dangerously-skip-permissions
165
+
166
+ # 3. 检查会话文件
167
+ ls -lt ~/.claude/SESSIONS/raw/2026-01/ | head -3
168
+ cat ~/.claude/SESSIONS/raw/2026-01/session-*.jsonl | tail -1 | jq '.'
169
+ ```
170
+
171
+ ## 许可证
172
+
173
+ MIT
package/bin/cli.js ADDED
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * claude-daemon CLI
5
+ * npm 包的命令行入口
6
+ */
7
+
8
+ const { execSync } = require('child_process');
9
+ const { join } = require('path');
10
+ const { existsSync } = require('fs');
11
+
12
+ const COLORS = {
13
+ reset: '\x1b[0m',
14
+ green: '\x1b[32m',
15
+ yellow: '\x1b[33m',
16
+ blue: '\x1b[34m',
17
+ red: '\x1b[31m',
18
+ };
19
+
20
+ function log(color, message) {
21
+ console.log(`${COLORS[color]}${message}${COLORS.reset}`);
22
+ }
23
+
24
+ function printBanner() {
25
+ log('blue', '╔════════════════════════════════════════╗');
26
+ log('blue', '║ Claude Code 会话历史系统 ║');
27
+ log('blue', '║ @jhihjian/claude-daemon ║');
28
+ log('blue', '╚════════════════════════════════════════╝');
29
+ console.log('');
30
+ }
31
+
32
+ function printUsage() {
33
+ console.log('使用方法:');
34
+ console.log('');
35
+ log('green', ' claude-daemon install');
36
+ console.log(' 安装 Claude Code 会话历史系统');
37
+ console.log('');
38
+ log('green', ' claude-daemon --help');
39
+ console.log(' 显示帮助信息');
40
+ console.log('');
41
+ log('green', ' claude-daemon --version');
42
+ console.log(' 显示版本信息');
43
+ console.log('');
44
+ }
45
+
46
+ function install() {
47
+ printBanner();
48
+ log('green', '[1/3] 定位安装脚本...');
49
+
50
+ // 获取包的安装路径
51
+ const packageDir = join(__dirname, '..');
52
+ const installScript = join(packageDir, 'install.sh');
53
+
54
+ if (!existsSync(installScript)) {
55
+ log('red', '✗ 找不到 install.sh');
56
+ log('yellow', '请确保包安装正确');
57
+ process.exit(1);
58
+ }
59
+
60
+ log('green', ' ✓ 找到安装脚本');
61
+ console.log('');
62
+
63
+ log('green', '[2/3] 执行安装...');
64
+ console.log('');
65
+
66
+ try {
67
+ // 执行安装脚本
68
+ execSync(`bash "${installScript}"`, {
69
+ stdio: 'inherit',
70
+ cwd: packageDir,
71
+ });
72
+ } catch (error) {
73
+ log('red', '\n✗ 安装失败');
74
+ log('yellow', '请查看错误信息并重试');
75
+ process.exit(1);
76
+ }
77
+
78
+ console.log('');
79
+ log('green', '[3/3] 安装完成!');
80
+ console.log('');
81
+ log('yellow', '下一步:');
82
+ console.log(' 1. 重新加载 shell: source ~/.bashrc');
83
+ console.log(' 2. 测试: claude-sessions recent 5');
84
+ console.log('');
85
+ }
86
+
87
+ // 主函数
88
+ function main() {
89
+ const args = process.argv.slice(2);
90
+ const command = args[0];
91
+
92
+ switch (command) {
93
+ case 'install':
94
+ install();
95
+ break;
96
+
97
+ case '--version':
98
+ case '-v':
99
+ const pkg = require('../package.json');
100
+ console.log(`v${pkg.version}`);
101
+ break;
102
+
103
+ case '--help':
104
+ case '-h':
105
+ case undefined:
106
+ printBanner();
107
+ printUsage();
108
+ break;
109
+
110
+ default:
111
+ log('red', `未知命令: ${command}`);
112
+ console.log('');
113
+ printUsage();
114
+ process.exit(1);
115
+ }
116
+ }
117
+
118
+ main();