Files
Elydora-Open-Source/sdks/python/elydora/plugins/letta_sources.py
T
雷电芽衣 bc6983a62e chore(oss): audit the server package and resync the SDK mirrors
Split the 657-line operation service into eor, submit, and verify
modules, add the AGENT_REVOKED and KEY_RETIRED codes the Backend already
emits, and condense every multi-line comment. Mirror Node, Python, and
Go at 2.1.0 with the deep-health 503, non-idempotent retry, rotation
grace, and path-encoding fixes. Document which routes and response
fields the self-hosted server does not provide.

Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
2026-09-03 20:48:20 -04:00

151 lines
4.6 KiB
Python

"""Letta Code 0.28 settings discovery and effective hook controls."""
from __future__ import annotations
from dataclasses import dataclass
import os
from typing import Optional, Tuple
from ._managed_files import (
MAX_SOURCE_BYTES,
physical_directory_exists,
read_physical_file,
)
from ._runtime import same_path, unique_preconditions
from ._transaction import FilePrecondition
from .letta_config import (
LettaDocument,
create_letta_document,
letta_document_label,
letta_source_label,
parse_letta_document,
)
@dataclass(frozen=True)
class LettaDisableControl:
disabled: bool
source: Optional[LettaDocument]
@dataclass(frozen=True)
class LettaSources:
home_directory: str
global_settings: LettaDocument
project: LettaDocument
project_local: LettaDocument
project_active: bool
disable_control: LettaDisableControl
preconditions: Tuple[FilePrecondition, ...]
def _home_directory() -> str:
home = os.environ.get("HOME") or os.path.expanduser("~")
return os.path.abspath(home)
def _comparison_path(file_path: str) -> str:
return os.path.normcase(os.path.abspath(file_path))
def _canonical_path(file_path: str) -> str:
return os.path.realpath(file_path)
def _read_document(kind: str, file_path: str) -> LettaDocument:
snapshot = read_physical_file(
file_path, letta_source_label(kind), MAX_SOURCE_BYTES
)
if snapshot is None:
return create_letta_document(kind, file_path)
return parse_letta_document(
kind=kind,
exists=True,
file_path=file_path,
raw=snapshot.contents,
snapshot=snapshot,
)
def _source_precondition(document: LettaDocument) -> FilePrecondition:
return FilePrecondition(
document.file_path,
letta_document_label(document),
document.snapshot,
MAX_SOURCE_BYTES,
)
def _effective_disable(
global_settings: LettaDocument,
project: LettaDocument,
project_local: LettaDocument,
project_active: bool,
) -> LettaDisableControl:
if global_settings.hooks.get("disabled") is False:
return LettaDisableControl(False, global_settings)
if global_settings.hooks.get("disabled") is True:
return LettaDisableControl(True, global_settings)
if project_active and project.hooks.get("disabled") is True:
return LettaDisableControl(True, project)
if project_local.hooks.get("disabled") is True:
return LettaDisableControl(True, project_local)
return LettaDisableControl(False, None)
def read_letta_sources() -> LettaSources:
home_directory = _home_directory()
workspace = os.path.abspath(os.getcwd())
global_directory = os.path.join(home_directory, ".letta")
project_directory = os.path.join(workspace, ".letta")
physical_directory_exists(home_directory, "Letta Code home directory")
physical_directory_exists(
global_directory, "Letta Code global configuration directory"
)
if not same_path(project_directory, global_directory):
physical_directory_exists(
project_directory, "Letta Code project configuration directory"
)
global_path = os.path.join(global_directory, "settings.json")
project_path = os.path.join(project_directory, "settings.json")
local_path = os.path.join(project_directory, "settings.local.json")
project_active = _comparison_path(_canonical_path(workspace)) != _comparison_path(
_canonical_path(home_directory)
)
global_settings = _read_document("global", global_path)
project = (
_read_document("project", project_path)
if project_active
else create_letta_document("project", project_path)
)
project_local = _read_document("project-local", local_path)
preconditions = [_source_precondition(global_settings)]
if project_active:
preconditions.append(_source_precondition(project))
preconditions.append(_source_precondition(project_local))
return LettaSources(
home_directory,
global_settings,
project,
project_local,
project_active,
_effective_disable(
global_settings, project, project_local, project_active
),
unique_preconditions(preconditions),
)
def require_letta_hooks_enabled(sources: LettaSources) -> None:
if not sources.disable_control.disabled:
return
source = sources.disable_control.source
location = (
f"{letta_document_label(source)} at {source.file_path}"
if source is not None
else "effective settings"
)
raise ValueError(
f"Letta Code hooks are disabled by hooks.disabled in {location}"
)