from dataclasses import dataclass
from typing import Any
import extism
import json
class HostFunctionError(Exception):
"""Raised when a host function returns an error."""
pass
@extism.import_fn("extism:host/user", "library_getlibrary")
def _library_getlibrary(offset: int) -> int:
"""Raw host function - do not call directly."""
...
@extism.import_fn("extism:host/user", "library_getalllibraries")
def _library_getalllibraries(offset: int) -> int:
"""Raw host function - do not call directly."""
...
def library_get_library(id: int) -> Any:
"""GetLibrary retrieves metadata for a specific library by ID.
Parameters:
- id: The library's unique identifier
Returns the library metadata, or an error if the library is not found.
Args:
id: int parameter.
Returns:
Any: The result value.
Raises:
HostFunctionError: If the host function returns an error.
"""
request = {
"id": id,
}
request_bytes = json.dumps(request).encode("utf-8")
request_mem = extism.memory.alloc(request_bytes)
response_offset = _library_getlibrary(request_mem.offset)
response_mem = extism.memory.find(response_offset)
response = json.loads(extism.memory.string(response_mem))
if response.get("error"):
raise HostFunctionError(response["error"])
return response.get("result", None)
def library_get_all_libraries() -> Any:
"""GetAllLibraries retrieves metadata for all configured libraries.
Returns a slice of all libraries with their metadata.
Returns:
Any: The result value.
Raises:
HostFunctionError: If the host function returns an error.
"""
request_bytes = b"{}"
request_mem = extism.memory.alloc(request_bytes)
response_offset = _library_getalllibraries(request_mem.offset)
response_mem = extism.memory.find(response_offset)
response = json.loads(extism.memory.string(response_mem))
if response.get("error"):
raise HostFunctionError(response["error"])
return response.get("result", None)