janito 0.14.0__py3-none-any.whl → 0.15.0__py3-none-any.whl

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.
janito/config.py DELETED
@@ -1,375 +0,0 @@
1
- """
2
- Configuration module for Janito.
3
- Provides a singleton Config class to access configuration values.
4
- """
5
- import os
6
- import json
7
- from pathlib import Path
8
- import typer
9
- from typing import Dict, Any, Optional
10
-
11
- # Predefined parameter profiles
12
- PROFILES = {
13
- "precise": {
14
- "temperature": 0.2,
15
- "top_p": 0.85,
16
- "top_k": 20,
17
- "description": "Factual answers, documentation, structured data, avoiding hallucinations"
18
- },
19
- "balanced": {
20
- "temperature": 0.5,
21
- "top_p": 0.9,
22
- "top_k": 40,
23
- "description": "Professional writing, summarization, everyday tasks with moderate creativity"
24
- },
25
- "conversational": {
26
- "temperature": 0.7,
27
- "top_p": 0.9,
28
- "top_k": 45,
29
- "description": "Natural dialogue, educational content, support conversations"
30
- },
31
- "creative": {
32
- "temperature": 0.9,
33
- "top_p": 0.95,
34
- "top_k": 70,
35
- "description": "Storytelling, brainstorming, marketing copy, poetry"
36
- },
37
- "technical": {
38
- "temperature": 0.3,
39
- "top_p": 0.95,
40
- "top_k": 15,
41
- "description": "Code generation, debugging, decision analysis, technical problem-solving"
42
- }
43
- }
44
-
45
- class Config:
46
- """Singleton configuration class for Janito."""
47
- _instance = None
48
-
49
- def __new__(cls):
50
- if cls._instance is None:
51
- cls._instance = super(Config, cls).__new__(cls)
52
- cls._instance._workspace_dir = os.getcwd()
53
- cls._instance._verbose = False
54
- # Chat history context feature has been removed
55
- cls._instance._ask_mode = False
56
- cls._instance._trust_mode = False # New trust mode setting
57
- cls._instance._no_tools = False # New no-tools mode setting
58
- # Set technical profile as default
59
- profile_data = PROFILES["technical"]
60
- cls._instance._temperature = profile_data["temperature"]
61
- cls._instance._profile = "technical"
62
- cls._instance._role = "software engineer"
63
- cls._instance._gitbash_path = None # Default to None for auto-detection
64
- cls._instance._load_config()
65
- return cls._instance
66
-
67
- def _load_config(self) -> None:
68
- """Load configuration from file."""
69
- config_path = Path(self._workspace_dir) / ".janito" / "config.json"
70
- if config_path.exists():
71
- try:
72
- with open(config_path, "r", encoding="utf-8") as f:
73
- config_data = json.load(f)
74
- # Chat history context feature has been removed
75
- if "debug_mode" in config_data:
76
- self._verbose = config_data["debug_mode"]
77
- if "ask_mode" in config_data:
78
- self._ask_mode = config_data["ask_mode"]
79
- if "trust_mode" in config_data:
80
- self._trust_mode = config_data["trust_mode"]
81
- if "temperature" in config_data:
82
- self._temperature = config_data["temperature"]
83
- if "profile" in config_data:
84
- self._profile = config_data["profile"]
85
- if "role" in config_data:
86
- self._role = config_data["role"]
87
- if "gitbash_path" in config_data:
88
- self._gitbash_path = config_data["gitbash_path"]
89
- except Exception as e:
90
- print(f"Warning: Failed to load configuration: {str(e)}")
91
-
92
- def _save_config(self) -> None:
93
- """Save configuration to file."""
94
- config_dir = Path(self._workspace_dir) / ".janito"
95
- config_dir.mkdir(parents=True, exist_ok=True)
96
- config_path = config_dir / "config.json"
97
-
98
- config_data = {
99
- # Chat history context feature has been removed
100
- "verbose": self._verbose,
101
- "ask_mode": self._ask_mode,
102
- # trust_mode is not saved as it's a per-session setting
103
- "temperature": self._temperature,
104
- "role": self._role
105
- }
106
-
107
- # Save profile name if one is set
108
- if self._profile:
109
- config_data["profile"] = self._profile
110
-
111
- # Save GitBash path if one is set
112
- if self._gitbash_path:
113
- config_data["gitbash_path"] = self._gitbash_path
114
-
115
- try:
116
- with open(config_path, "w", encoding="utf-8") as f:
117
- json.dump(config_data, f, indent=2)
118
- except Exception as e:
119
- print(f"Warning: Failed to save configuration: {str(e)}")
120
-
121
- def set_profile(self, profile_name: str) -> None:
122
- """Set parameter values based on a predefined profile.
123
-
124
- Args:
125
- profile_name: Name of the profile to use (precise, balanced, conversational, creative, technical)
126
-
127
- Raises:
128
- ValueError: If the profile name is not recognized
129
- """
130
- profile_name = profile_name.lower()
131
- if profile_name not in PROFILES:
132
- valid_profiles = ", ".join(PROFILES.keys())
133
- raise ValueError(f"Unknown profile: {profile_name}. Valid profiles are: {valid_profiles}")
134
-
135
- profile = PROFILES[profile_name]
136
- self._temperature = profile["temperature"]
137
- self._profile = profile_name
138
- self._save_config()
139
-
140
- @property
141
- def profile(self) -> Optional[str]:
142
- """Get the current profile name."""
143
- return self._profile
144
-
145
- @staticmethod
146
- def get_available_profiles() -> Dict[str, Dict[str, Any]]:
147
- """Get all available predefined profiles."""
148
- return PROFILES
149
-
150
- @staticmethod
151
- def set_api_key(api_key: str) -> None:
152
- """Set the API key in the global configuration file.
153
-
154
- Args:
155
- api_key: The Anthropic API key to store
156
-
157
- Returns:
158
- None
159
- """
160
- # Create .janito directory in user's home directory if it doesn't exist
161
- home_dir = Path.home()
162
- config_dir = home_dir / ".janito"
163
- config_dir.mkdir(parents=True, exist_ok=True)
164
-
165
- # Create or update the config.json file
166
- config_path = config_dir / "config.json"
167
-
168
- # Load existing config if it exists
169
- config_data = {}
170
- if config_path.exists():
171
- try:
172
- with open(config_path, "r", encoding="utf-8") as f:
173
- config_data = json.load(f)
174
- except Exception as e:
175
- print(f"Warning: Failed to load global configuration: {str(e)}")
176
-
177
- # Update the API key
178
- config_data["api_key"] = api_key
179
-
180
- # Save the updated config
181
- try:
182
- with open(config_path, "w", encoding="utf-8") as f:
183
- json.dump(config_data, f, indent=2)
184
- print(f"API key saved to {config_path}")
185
- except Exception as e:
186
- raise ValueError(f"Failed to save API key: {str(e)}")
187
-
188
- @staticmethod
189
- def get_api_key() -> Optional[str]:
190
- """Get the API key from the global configuration file.
191
-
192
- Returns:
193
- The API key if found, None otherwise
194
- """
195
- # Look for config.json in user's home directory
196
- home_dir = Path.home()
197
- config_path = home_dir / ".janito" / "config.json"
198
-
199
- if config_path.exists():
200
- try:
201
- with open(config_path, "r", encoding="utf-8") as f:
202
- config_data = json.load(f)
203
- return config_data.get("api_key")
204
- except Exception:
205
- # Silently fail and return None
206
- pass
207
-
208
- return None
209
-
210
- @property
211
- def workspace_dir(self) -> str:
212
- """Get the current workspace directory."""
213
- return self._workspace_dir
214
-
215
- @workspace_dir.setter
216
- def workspace_dir(self, path: str) -> None:
217
- """Set the workspace directory."""
218
- # Convert to absolute path if not already
219
- if not os.path.isabs(path):
220
- path = os.path.normpath(os.path.abspath(path))
221
- else:
222
- # Ensure Windows paths are properly formatted
223
- path = os.path.normpath(path)
224
-
225
- # Check if the directory exists
226
- if not os.path.isdir(path):
227
- create_dir = typer.confirm(f"Workspace directory does not exist: {path}\nDo you want to create it?")
228
- if create_dir:
229
- try:
230
- os.makedirs(path, exist_ok=True)
231
- print(f"Created workspace directory: {path}")
232
- except Exception as e:
233
- raise ValueError(f"Failed to create workspace directory: {str(e)}") from e
234
- else:
235
- raise ValueError(f"Workspace directory does not exist: {path}")
236
-
237
- self._workspace_dir = path
238
-
239
- @property
240
- def verbose(self) -> bool:
241
- """Get the verbose mode status."""
242
- return self._verbose
243
-
244
- @verbose.setter
245
- def verbose(self, value: bool) -> None:
246
- """Set the verbose mode status."""
247
- self._verbose = value
248
-
249
- # For backward compatibility
250
- @property
251
- def debug_mode(self) -> bool:
252
- """Get the debug mode status (alias for verbose)."""
253
- return self._verbose
254
-
255
- @debug_mode.setter
256
- def debug_mode(self, value: bool) -> None:
257
- """Set the debug mode status (alias for verbose)."""
258
- self._verbose = value
259
-
260
- # Chat history context feature has been removed
261
-
262
- @property
263
- def ask_mode(self) -> bool:
264
- """Get the ask mode status."""
265
- return self._ask_mode
266
-
267
- @ask_mode.setter
268
- def ask_mode(self, value: bool) -> None:
269
- """Set the ask mode status."""
270
- self._ask_mode = value
271
- self._save_config()
272
-
273
- @property
274
- def trust_mode(self) -> bool:
275
- """Get the trust mode status."""
276
- return self._trust_mode
277
-
278
- @trust_mode.setter
279
- def trust_mode(self, value: bool) -> None:
280
- """Set the trust mode status.
281
-
282
- Note: This setting is not persisted to config file
283
- as it's meant to be a per-session setting.
284
- """
285
- self._trust_mode = value
286
- # Don't save to config file - this is a per-session setting
287
-
288
- @property
289
- def no_tools(self) -> bool:
290
- """Get the no-tools mode status."""
291
- return self._no_tools
292
-
293
- @no_tools.setter
294
- def no_tools(self, value: bool) -> None:
295
- """Set the no-tools mode status.
296
-
297
- Note: This setting is not persisted to config file
298
- as it's meant to be a per-session setting.
299
- """
300
- self._no_tools = value
301
- # Don't save to config file - this is a per-session setting
302
-
303
- @property
304
- def temperature(self) -> float:
305
- """Get the temperature value for model generation."""
306
- return self._temperature
307
-
308
- @temperature.setter
309
- def temperature(self, value: float) -> None:
310
- """Set the temperature value for model generation."""
311
- if value < 0.0 or value > 1.0:
312
- raise ValueError("Temperature must be between 0.0 and 1.0")
313
- self._temperature = value
314
- self._save_config()
315
-
316
- # top_k and top_p are now only accessible through profiles
317
-
318
- @property
319
- def role(self) -> str:
320
- """Get the role for the assistant."""
321
- return self._role
322
-
323
- @role.setter
324
- def role(self, value: str) -> None:
325
- """Set the role for the assistant."""
326
- self._role = value
327
- self._save_config()
328
-
329
- @property
330
- def gitbash_path(self) -> Optional[str]:
331
- """Get the path to the GitBash executable."""
332
- return self._gitbash_path
333
-
334
- @gitbash_path.setter
335
- def gitbash_path(self, value: Optional[str]) -> None:
336
- """Set the path to the GitBash executable.
337
-
338
- Args:
339
- value: Path to the GitBash executable, or None to use auto-detection
340
- """
341
- # If a path is provided, verify it exists
342
- if value is not None and not os.path.exists(value):
343
- raise ValueError(f"GitBash executable not found at: {value}")
344
-
345
- self._gitbash_path = value
346
- self._save_config()
347
-
348
- def reset_config(self) -> bool:
349
- """Reset configuration by removing the config file.
350
-
351
- Returns:
352
- bool: True if the config file was removed, False if it didn't exist
353
- """
354
- config_path = Path(self._workspace_dir) / ".janito" / "config.json"
355
- if config_path.exists():
356
- config_path.unlink()
357
- # Reset instance variables to defaults
358
- self._verbose = False
359
- # Chat history context feature has been removed
360
- self._ask_mode = False
361
- self._trust_mode = False
362
- self._no_tools = False
363
- # Set technical profile as default
364
- profile_data = PROFILES["technical"]
365
- self._temperature = profile_data["temperature"]
366
- self._profile = "technical"
367
- self._role = "software engineer"
368
- self._gitbash_path = None # Reset to auto-detection
369
- return True
370
- return False
371
-
372
- # Convenience function to get the config instance
373
- def get_config() -> Config:
374
- """Get the singleton Config instance."""
375
- return Config()