-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullrc.py
More file actions
195 lines (148 loc) · 6.05 KB
/
nullrc.py
File metadata and controls
195 lines (148 loc) · 6.05 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Project-local .nullrc configuration support."""
import json
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import ClassVar
@dataclass
class ProjectConfig:
"""Project-specific configuration from .nullrc."""
# AI settings
provider: str | None = None
model: str | None = None
system_prompt: str | None = None
temperature: float | None = None
# Shell settings
shell: str | None = None
env: dict = field(default_factory=dict)
# Project context
context_files: list = field(default_factory=list) # Files to include in AI context
ignore_patterns: list = field(default_factory=list) # Files to ignore
# Command aliases
aliases: dict = field(default_factory=dict) # e.g., {"build": "npm run build"}
# Startup commands
on_start: list = field(default_factory=list) # Commands to run on project load
def to_dict(self) -> dict:
"""Convert to dictionary, filtering None values."""
result = {}
for key, value in asdict(self).items():
if value is not None and value != [] and value != {}:
result[key] = value
return result
@classmethod
def from_dict(cls, data: dict) -> "ProjectConfig":
"""Create ProjectConfig from dictionary."""
config = cls()
for key, value in data.items():
if hasattr(config, key):
setattr(config, key, value)
return config
class NullrcManager:
"""Manages .nullrc files in project directories."""
NULLRC_NAMES: ClassVar[list[str]] = [".nullrc", ".nullrc.json", "nullrc.json"]
def __init__(self):
self._cache: dict[str, ProjectConfig] = {}
self._file_cache: dict[str, Path] = {}
def find_nullrc(self, start_path: Path | None = None) -> Path | None:
"""Find .nullrc file by walking up from start_path or cwd."""
if start_path is None:
start_path = Path.cwd()
current = start_path.resolve()
home = Path.home()
while current != current.parent:
for name in self.NULLRC_NAMES:
nullrc_path = current / name
if nullrc_path.exists():
return nullrc_path
# Don't search above home directory
if current == home:
break
current = current.parent
return None
def load(self, start_path: Path | None = None) -> ProjectConfig | None:
"""Load .nullrc from current directory or parents."""
nullrc_path = self.find_nullrc(start_path)
if nullrc_path is None:
return None
# Check cache
cache_key = str(nullrc_path)
if cache_key in self._cache:
return self._cache[cache_key]
try:
content = nullrc_path.read_text(encoding="utf-8")
data = json.loads(content)
config = ProjectConfig.from_dict(data)
self._cache[cache_key] = config
self._file_cache[cache_key] = nullrc_path
return config
except Exception:
return None
def get_project_root(self, start_path: Path | None = None) -> Path | None:
"""Get the project root (directory containing .nullrc)."""
nullrc_path = self.find_nullrc(start_path)
if nullrc_path:
return nullrc_path.parent
return None
def create_default(self, path: Path | None = None) -> Path:
"""Create a default .nullrc in the specified directory."""
if path is None:
path = Path.cwd()
nullrc_path = path / ".nullrc"
default_config = {
"provider": None,
"model": None,
"system_prompt": "You are a helpful assistant working on this project.",
"context_files": ["README.md", "package.json", "pyproject.toml"],
"ignore_patterns": ["node_modules", ".git", "__pycache__", "*.pyc"],
"aliases": {
"test": "uv run pytest",
"build": "uv build",
"lint": "uv run ruff check .",
},
"env": {},
"on_start": [],
}
nullrc_path.write_text(json.dumps(default_config, indent=2), encoding="utf-8")
return nullrc_path
def apply_to_config(self, base_config: dict, project_config: ProjectConfig) -> dict:
"""Apply project config overrides to base config."""
config = base_config.copy()
if project_config.provider:
config.setdefault("ai", {})["provider"] = project_config.provider
if project_config.model:
config.setdefault("ai", {})["model"] = project_config.model
if project_config.shell:
config["shell"] = project_config.shell
if project_config.temperature is not None:
config.setdefault("ai", {})["temperature"] = project_config.temperature
return config
def get_context_files(self, project_config: ProjectConfig) -> list[Path]:
"""Get list of context files that exist in the project."""
project_root = self.get_project_root()
if not project_root:
return []
files: list[Path] = []
for pattern in project_config.context_files:
if "*" in pattern:
# Glob pattern
files.extend(project_root.glob(pattern))
else:
# Direct file
file_path = project_root / pattern
if file_path.exists():
files.append(file_path)
return files
def reload(self):
"""Clear cache and reload."""
self._cache.clear()
self._file_cache.clear()
# Singleton instance
_nullrc_manager: NullrcManager | None = None
def get_nullrc_manager() -> NullrcManager:
"""Get the singleton NullrcManager instance."""
global _nullrc_manager
if _nullrc_manager is None:
_nullrc_manager = NullrcManager()
return _nullrc_manager
def get_project_config() -> ProjectConfig | None:
"""Get project configuration for current directory."""
return get_nullrc_manager().load()