Loader
Load an installed extension’s code into the running process.
Extensions are installed at run time, on a platform whose app, worker and beat processes
run in many pods. The archive of each ExtensionVersion
is stored in the database, so each process loads an extension the first time it needs it:
Materialize: the archive is extracted to
<tempdir>/smarter-extensions/<slug>/<version>-<sha256 prefix>/, once per pod. It is extracted to a temporary directory and renamed into place, so that two processes that extract it at the same time cannot see a partial copy.Import: the package is imported under a namespace that is unique to the version and its contents,
smarter_extensions.<slug>.v<version>_<sha256 prefix>.<package>. An update imports the new version beside the old one, rather than reloading modules, which Python does not do reliably: a tool call that is running finishes on the old code. Old modules stay in memory until the process restarts.
An extension’s own modules must import each other with relative imports
(from .models import WeatherArguments), because the package is not on sys.path.
- smarter.apps.llmtool.extension.loader.ARCHIVE_CACHE_KEY_PREFIX = 'smarter:extension:archive:'
The cache key of an extension version’s archive, followed by its sha256, for the tool worker.
- smarter.apps.llmtool.extension.loader.ARCHIVE_CACHE_SECONDS = 86400
Seconds that an archive stays in the cache since it was last published for a tool call.
- class smarter.apps.llmtool.extension.loader.LoadedExtension(slug, version, sha256, manifest, module_name, path, tools=<factory>, lifecycle=None)[source]
Bases:
objectAn extension version whose code is imported into this process.
- Parameters:
slug (
str) – The extension’s slug.version (
str) – The version.sha256 (
str) – The sha256 of its archive.manifest (
ExtensionManifest) – Its manifest.module_name (
str) – The full name under which its package is imported.tools (
dict[str,type[SmarterExtensionTool]]) – Its tool classes, by tool name.lifecycle (
type[SmarterExtensionLifecycle] |None) – Its lifecycle hooks class, if it has one.path (
Path) – The directory that holds its extracted archive.
- __init__(slug, version, sha256, manifest, module_name, path, tools=<factory>, lifecycle=None)
- lifecycle: type[SmarterExtensionLifecycle] | None = None
- manifest: ExtensionManifest
- tools: dict[str, type[SmarterExtensionTool]]
- smarter.apps.llmtool.extension.loader.archive_cache_key(sha256)[source]
Return the cache key of an archive, by its sha256.
- smarter.apps.llmtool.extension.loader.cached_archive(sha256)[source]
Return an archive that
publish_archive()put in the cache, if it is there, and its sha256 matches.- Parameters:
sha256 (
str) – The archive’s sha256.- Return type:
- Returns:
The archive, or None.
- Raises:
SmarterExtensionLoadError – If the cached bytes aren’t an archive, or not the one with that sha256.
- smarter.apps.llmtool.extension.loader.extensions_root()[source]
The directory into which extension archives are extracted.
- Return type:
- smarter.apps.llmtool.extension.loader.load_archive(archive)[source]
Import an extension archive’s code into this process, and find its tools and lifecycle hooks.
Loading the same archive again returns the extension that is already loaded.
- Parameters:
archive (
ExtensionArchive) – The archive.- Return type:
- Returns:
The loaded extension.
- Raises:
SmarterExtensionLoadError – If the code cannot be imported, or its tools are not valid.
- smarter.apps.llmtool.extension.loader.load_by_key(slug, sha256)[source]
Return an extension version’s code, by its slug and its archive’s sha256, as a tool call names it.
A version already loaded into this process is returned as it is. Otherwise its archive is read from the cache, where
publish_archive()put it for the tool worker, which has no database, and checked against its sha256; or else from the database, once per process; and loaded.- Parameters:
- Return type:
- Returns:
The loaded extension.
- Raises:
SmarterExtensionLoadError – If the archive is in neither the cache nor the database, its bytes don’t match the sha256, or its code can’t be loaded.
- smarter.apps.llmtool.extension.loader.load_version(version)[source]
Import an installed extension version’s code into this process.
The archive is read from the database only the first time a process loads the version.
- Parameters:
version (
ExtensionVersion) – The installed version.- Return type:
- Returns:
The loaded extension.
- Raises:
SmarterExtensionLoadError – If the code cannot be imported, or its tools are not valid.
- smarter.apps.llmtool.extension.loader.materialize(archive)[source]
Extract an archive into this pod’s extensions directory, unless it is already there.
- Parameters:
archive (
ExtensionArchive) – The archive.- Return type:
- Returns:
The directory that holds the archive’s files.
- smarter.apps.llmtool.extension.loader.module_name_for(manifest, sha256)[source]
Return the full name under which an extension version’s package is imported.
- Parameters:
manifest (
ExtensionManifest) – The version’s manifest.sha256 (
str) – The sha256 of its archive.
- Return type:
- Returns:
e.g.
smarter_extensions.weather.v1_2_0_0123456789ab.weather.
- smarter.apps.llmtool.extension.loader.publish_archive(version)[source]
Put an installed version’s archive in the cache, Redis, for the tool worker to load, unless it is there.
The tool worker has no database (ADR-038), so it reads an extension’s code from the cache, keyed by the archive’s sha256, which it checks. The app publishes it when it resolves a tool call for the remote runner. A published archive’s time to live is renewed on each call, so the archive is read from the database and copied only when it isn’t in the cache, e.g. after
manage.py reset_cache.- Parameters:
version (
ExtensionVersion) – The installed version.- Return type: