74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import sys
|
|
import os
|
|
import subprocess
|
|
from datetime import datetime
|
|
|
|
# 从 stdin 加载输入
|
|
try:
|
|
input_data = json.load(sys.stdin)
|
|
except json.JSONDecodeError as e:
|
|
print(f"Error: Invalid JSON input: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
context_parts = []
|
|
|
|
# 添加项目基本信息
|
|
if os.path.exists("package.json"):
|
|
context_parts.append("这是一个 Node.js 项目")
|
|
elif os.path.exists("requirements.txt") or os.path.exists("pyproject.toml"):
|
|
context_parts.append("这是一个 Python 项目")
|
|
elif os.path.exists("Cargo.toml"):
|
|
context_parts.append("这是一个 Rust 项目")
|
|
|
|
# 添加 Git 状态信息
|
|
try:
|
|
result = subprocess.run(["git", "status", "--porcelain"],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0:
|
|
if result.stdout.strip():
|
|
context_parts.append("当前有未提交的更改")
|
|
else:
|
|
context_parts.append("工作目录是干净的")
|
|
|
|
# 获取当前分支
|
|
branch_result = subprocess.run(["git", "branch", "--show-current"],
|
|
capture_output=True, text=True)
|
|
if branch_result.returncode == 0:
|
|
branch = branch_result.stdout.strip()
|
|
context_parts.append(f"当前分支:{branch}")
|
|
except:
|
|
pass
|
|
|
|
# 添加最近的活动信息
|
|
if os.path.exists(".claude"):
|
|
context_parts.append("项目已配置 Claude Code")
|
|
|
|
# 添加时间信息
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M")
|
|
context_parts.append(f"会话开始时间:{current_time}")
|
|
|
|
# 构建上下文信息
|
|
if context_parts:
|
|
additional_context = "项目状态概览:\n" + "\n".join(f"- {part}" for part in context_parts)
|
|
|
|
# 获取全局 Claude 配置目录路径
|
|
home_claude_dir = os.path.expanduser("~/.claude")
|
|
# my_context = """\n重要配置信息:
|
|
# - 全局 Claude 配置目录路径:""" + home_claude_dir + """
|
|
# - 当你需要修改或查找全局 Claude 配置时,请始终使用此目录:""" + home_claude_dir + """
|
|
# - 不要使用 AppData\\Roaming\\.claude,该目录在此系统上不存在"""
|
|
|
|
my_context = """每次回答我问题的时候要先说'是的,大先生'"""
|
|
|
|
output = {
|
|
"hookSpecificOutput": {
|
|
"hookEventName": "SessionStart",
|
|
"message":"你好,大先生",
|
|
"additionalContext": my_context
|
|
}
|
|
}
|
|
print(json.dumps(output))
|
|
print("你好,大先生222", file=sys.stderr)
|
|
sys.exit(0) |