Repository navigation
Expand file tree
/
Copy pathcontext.py
More file actions
73 lines (57 loc) · 2.13 KB
/
Copy pathcontext.py
File metadata and controls
73 lines (57 loc) · 2.13 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
import subprocess
from datetime import date
from pathlib import Path
_BASE_PROMPT = """\
You are Claude Code, an AI assistant for software engineering tasks in the terminal.
You help with coding tasks by reading files, editing code, running commands, and searching codebases.
Guidelines:
- Always read a file before editing it
- Prefer small, targeted edits over rewriting large sections
- Run tests after making changes when test commands are available
- Use Glob/Grep to find relevant files before reading them all"""
def build_system_prompt(cwd: str | None = None) -> str:
parts = [_BASE_PROMPT]
parts.append(f"\n# Environment\nToday's date: {date.today().isoformat()}")
cwd = cwd or str(Path.cwd())
parts.append(f"Working directory: {cwd}")
git_status = _get_git_status(cwd)
if git_status:
parts.append(f"\n# Git Status\n{git_status}")
claude_md = _find_claude_md(cwd)
if claude_md:
parts.append(f"\n# CLAUDE.md\n{claude_md}")
return "\n".join(parts)
def _get_git_status(cwd: str) -> str:
try:
branch = subprocess.run(
["git", "branch", "--show-current"],
capture_output=True, text=True, cwd=cwd, timeout=5,
).stdout.strip()
status = subprocess.run(
["git", "status", "--short"],
capture_output=True, text=True, cwd=cwd, timeout=5,
).stdout.strip()[:2000]
log = subprocess.run(
["git", "log", "--oneline", "-5"],
capture_output=True, text=True, cwd=cwd, timeout=5,
).stdout.strip()
if not branch and not status and not log:
return ""
parts = []
if branch:
parts.append(f"Branch: {branch}")
if status:
parts.append(f"Status:\n{status}")
if log:
parts.append(f"Recent commits:\n{log}")
return "\n".join(parts)
except Exception:
return ""
def _find_claude_md(cwd: str) -> str:
path = Path(cwd) / "CLAUDE.md"
if path.exists():
try:
return path.read_text(encoding="utf-8", errors="replace")[:10_000]
except OSError:
pass
return ""