This commit is contained in:
Alvin-Zilverstand committed 2026-11-13 10:02:13 +01:00
1 parent e6db824792
commit ed76261303
5 files changed
+170 -9

No files matched your search

+22 -4
View File
@@ -16,16 +16,34 @@ def load_settings(path=DEFAULT_PATH):
raise ValueError('sloppy.json must contain a models object')
data.setdefault('models', {})
data.setdefault('selected', {'ai1': '', 'ai2': ''})
if not isinstance(data['selected'], dict):
raise ValueError('sloppy.json selected must be an object')
for slot, value in data['selected'].items():
if not isinstance(slot, str) or not isinstance(value, str):
raise ValueError(f'sloppy.json selected.{slot} must be a string')
for identifier, values in data['models'].items():
if not isinstance(identifier, str) or not isinstance(values, dict):
raise ValueError(f'sloppy.json models.{identifier} must be an object')
for field in ('name', 'base_url', 'api_key', 'model', 'system_prompt'):
if field in values and not isinstance(values[field], str):
raise ValueError(f'sloppy.json models.{identifier}.{field} must be a string')
return data
def save_settings(data, path=DEFAULT_PATH):
path = Path(path)
temporary = path.with_name(path.name + '.tmp')
temporary.write_text(json.dumps(data, indent=2, ensure_ascii=False) + '\n', encoding='utf-8')
os.replace(temporary, path)
if os.name != 'nt':
path.chmod(0o600)
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
if hasattr(os, 'O_BINARY'):
flags |= os.O_BINARY
try:
descriptor = os.open(temporary, flags, 0o600)
with os.fdopen(descriptor, 'w', encoding='utf-8') as output:
output.write(json.dumps(data, indent=2, ensure_ascii=False) + '\n')
os.replace(temporary, path)
except BaseException:
temporary.unlink(missing_ok=True)
raise
def model_from_settings(data, identifier):