| name | chat-export |
| description | Use when user says '导出对话', '导出记录', 'export chat', or wants to save conversation - automatically exports Claude Code conversations to markdown files with timestamp naming |
Chat Export
将 Claude Code 对话记录导出为 Markdown 文件,聚焦用户-AI 真实交互。
Overview
核心功能:
- 自动导出:关闭对话/终端窗口时通过 SessionEnd Hook 自动触发
- 交互优先:默认导出摘要 thinking,保留完整的用户-AI 对话上下文
- 抗中断:使用 nohup 后台执行,即使终端被强制关闭也能完成导出
When to Use
触发关键词:
- 中文:导出对话、导出记录、保存对话、保存聊天
- English:export chat, save conversation, export transcript
自动触发场景:
- 正常退出对话 (
/exit, Ctrl+C)
- 直接关闭终端窗口
- 执行
/clear 清空对话
Quick Reference
| 场景 | 动作 |
|---|
| 自动导出 | 关闭对话时自动执行,无需操作 |
| 手动导出 | 说"导出对话"触发 skill |
| 查看历史 | ls chat/ |
| 查看日志 | cat ~/.claude/chat-export.log |
导出内容
导出的 Markdown 包含:
| 内容类型 | 导出格式 |
|---|
| 用户消息 | ### User 下的原文 |
| AI 回复 | ### Assistant 下的原文 |
| AI Thinking | 摘要形式(前 200 字符) |
| 工具调用 | *[Tool: name]* 简洁标记 |
文件命名
格式:YYYYMMDDHHmmss.md
示例:20251213143052.md → 2025-12-13 14:30:52
导出位置
$PROJECT_DIR/chat/
├── 20251213143052.md
├── 20251213120000.md
└── ...
架构说明
SessionEnd Hook 触发
│
▼
export-wrapper.sh (Bash)
- 快速读取 stdin 到临时文件
- nohup 启动后台进程
│
▼
export-chat.py (Python, 后台执行)
- 读取 JSONL transcript
- 转换为 Markdown
- 保存到 chat/ 目录
为什么使用两层脚本?
- Bash 启动快(<10ms),确保在终端关闭前完成输入读取
- nohup 确保 Python 脚本不受终端关闭影响
- 即使用户直接关闭终端窗口,导出也能完成
配置选项
通过环境变量配置:
| 变量 | 默认值 | 说明 |
|---|
CHAT_EXPORT_FULL_THINKING | 0 | 设为 1 导出完整 thinking 内容 |
CHAT_EXPORT_LOG | 0 | 启用详细日志到文件 |
Troubleshooting
| 问题 | 原因 | 解决 |
|---|
| 没有自动导出 | Hook 未配置 | 检查 .claude/settings.json |
| 文件为空 | Transcript 未生成 | 确保对话有内容 |
| 终端关闭没导出 | 进程被 SIGKILL | 检查 ~/.claude/chat-export.log |
诊断命令:
cat ~/.claude/chat-export.log
ls ~/.claude/chat-export-tmp/
echo '{"transcript_path":"","cwd":""}' | python3 .claude/skills/chat-export/scripts/export-chat.py
Hook 配置
确保 .claude/settings.json 包含:
{
"hooks": {
"SessionEnd": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "bash $CLAUDE_PROJECT_DIR/.claude/skills/chat-export/scripts/export-wrapper.sh"
}
]
}
]
}
}
Resources
scripts/export-wrapper.sh - Bash 包装脚本(快速启动 + nohup)
scripts/export-chat.py - Python 导出脚本(JSONL → Markdown)