Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion omlx/model_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ def apply_profile(self, model_id: str, name: str) -> Optional[ModelSettings]:
# ==================== Templates ====================

def _load_templates(self) -> None:
# Built-in defaults ship inside the package (omlx/default_global_templates.json)
# and are merged in at read time — they are NEVER copied to disk and never
# appear in `self._templates`. The user file under <base_path> holds
# ONLY user-created templates; a missing/empty file is the legitimate
# initial state.
if not self.templates_file.exists():
self._templates = {}
return
Expand Down Expand Up @@ -684,12 +689,18 @@ def _save_templates(self) -> None:
raise

def list_templates(self) -> list[dict]:
# Shipped JSON seeds were retired in favor of the client-side preset
# bundle (`omlx/admin/static/omlx_preset.json`); every entry on this
# surface is user-created. Callers that distinguish presets from
# user templates do so via the preset bundle, not an `is_builtin`
# flag on this response.
with self._lock:
return [dict(t) for t in self._templates.values()]

def get_template(self, name: str) -> Optional[dict]:
with self._lock:
return dict(self._templates.get(name, {})) or None
u = self._templates.get(name)
return dict(u) if u is not None else None

def save_template(
self,
Expand Down
35 changes: 34 additions & 1 deletion tests/test_model_settings_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Tests for profile/template CRUD on ModelSettingsManager."""


import json

import pytest

from omlx.model_profiles import InvalidProfileNameError
Expand Down Expand Up @@ -145,7 +147,10 @@ def test_save_filters_excluded_fields(self, mgr):


class TestTemplatesCRUD:
def test_list_templates_empty(self, mgr):
def test_list_templates_empty_by_default(self, mgr):
# Shipped builtins were retired in favor of the client-side preset
# bundle (`omlx/admin/static/omlx_preset.json`); the server's
# /api/profile-templates surface now exposes user templates only.
assert mgr.list_templates() == []

def test_save_template_universal_only(self, mgr):
Expand Down Expand Up @@ -202,3 +207,31 @@ def test_templates_persist_across_instances(self, tmp_path):
m1.save_template("coding", "Coding", None, {"temperature": 0.0})
m2 = ModelSettingsManager(tmp_path)
assert m2.get_template("coding") is not None


class TestTemplatesPersistence:
"""The on-disk template file holds only user-created entries. Built-in
seed templates were retired in favor of the client-side preset bundle
(`omlx/admin/static/omlx_preset.json`); /api/profile-templates is now a
pure user-store surface."""

def test_no_file_created_when_empty(self, tmp_path):
ModelSettingsManager(tmp_path)
# With no user templates and no shipped builtins, the manager must
# not create the templates file proactively.
assert not (tmp_path / "global_templates.json").exists()

def test_user_template_persists_only_itself(self, tmp_path):
m1 = ModelSettingsManager(tmp_path)
m1.save_template("custom", "Custom", None, {"temperature": 0.1})

on_disk = json.loads((tmp_path / "global_templates.json").read_text())
assert set(on_disk["templates"].keys()) == {"custom"}

m2 = ModelSettingsManager(tmp_path)
names = {t["name"] for t in m2.list_templates()}
assert names == {"custom"}
# No `is_builtin` is emitted now that builtins are retired; preset
# vs user classification lives on the client (preset bundle), not
# on this response.
assert "is_builtin" not in m2.get_template("custom")