Skip to content

Commit

Permalink
Add pydantic, add theme reactive to app
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Sep 10, 2024
1 parent 35d9068 commit f1cabd4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
3 changes: 3 additions & 0 deletions elia_chat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from textual.app import App
from textual.binding import Binding
from textual.reactive import Reactive, reactive
from textual.signal import Signal

from elia_chat.chats_manager import ChatsManager
Expand Down Expand Up @@ -52,6 +53,8 @@ def __init__(self, config: LaunchConfig, startup_prompt: str = ""):
put users into the chat window, rather than going to the home screen.
"""

theme: Reactive[str] = reactive("elia", init=False)

@property
def runtime_config(self) -> RuntimeConfig:
return self._runtime_config
Expand Down
5 changes: 2 additions & 3 deletions elia_chat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def get_builtin_anthropic_models() -> list[EliaChatModel]:
display_name="Claude 3.5 Sonnet",
provider="Anthropic",
product="Claude 3.5",
description=(
"Anthropic's most intelligent model"
),
description=("Anthropic's most intelligent model"),
),
EliaChatModel(
id="elia-claude-3-haiku-20240307",
Expand Down Expand Up @@ -172,6 +170,7 @@ class LaunchConfig(BaseModel):
builtin_models: list[EliaChatModel] = Field(
default_factory=get_builtin_models, init=False
)
theme: str = Field(default="elia")

@property
def all_models(self) -> list[EliaChatModel]:
Expand Down
7 changes: 7 additions & 0 deletions elia_chat/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ def config_directory() -> Path:

def config_file() -> Path:
return config_directory() / "config.toml"


def theme_directory() -> Path:
"""Return (possibly creating) the themes directory."""
theme_dir = data_directory() / "themes"
theme_dir.mkdir(exist_ok=True, parents=True)
return theme_dir
39 changes: 39 additions & 0 deletions elia_chat/themes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pydantic import BaseModel, Field
import yaml

from elia_chat.locations import theme_directory


class Theme(BaseModel):
name: str = Field(exclude=True)
primary: str
secondary: str | None = None
background: str | None = None
surface: str | None = None
panel: str | None = None
warning: str | None = None
error: str | None = None
success: str | None = None
accent: str | None = None
dark: bool = True


def load_user_themes() -> dict[str, Theme]:
"""Load user themes from "~/.config/elia/themes".
Returns:
A dictionary mapping theme names to theme objects.
"""
themes: dict[str, Theme] = {}
for path in theme_directory().iterdir():
path_suffix = path.suffix
if path_suffix == ".yaml" or path_suffix == ".yml":
with path.open() as theme_file:
theme_content = yaml.load(theme_file, Loader=yaml.FullLoader) or {}
try:
themes[theme_content["name"]] = Theme(**theme_content)
except KeyError:
raise ValueError(
f"Invalid theme file {path}. A `name` is required."
)
return themes
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"google-generativeai>=0.5.3",
"pyperclip>=1.8.2",
"litellm>=1.37.19",
"pydantic>=2.9.0",
]
readme = "README.md"
requires-python = ">= 3.11"
Expand Down
4 changes: 3 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1cabd4

Please sign in to comment.