PotionUI Docs v0.3.0

Examples

An admin-only route:

python
from fastapi import APIRouter, Depends, HTTPException

from src.plugin_api import AccountType, User, get_current_active_user

router = APIRouter(prefix="/api/plugins/my-plugin")


@router.post("/reindex")
async def reindex(user: User = Depends(get_current_active_user)):
    if user.account_type != AccountType.ADMIN:
        raise HTTPException(status_code=403, detail="Admins only")
    ...

A chat tool:

python
from src.plugin_api import BaseTool, ToolContext, ToolResult


class CountDatasetsTool(BaseTool):
    name = "count_datasets"

    async def execute(self, context: ToolContext, **kwargs) -> ToolResult:
        ...

Declare it in manifest.yml:

yaml
tools:
  - class: "backend.tools.count_datasets_tool:CountDatasetsTool"
    modes: ["my-plugin"]

A tool without modes is global — it appears in every chat mode, including the built-in generation chat. That is rarely what you want for a tool that only makes sense on your plugin's own page. Give it a modes list naming the chat mode(s) it belongs to (see "Contributing a chat mode" below); the host applies it as an instance-level override after constructing your tool, so the class itself stays free of any registry wiring.

Say what the tool does and when to call it in description. That text is what the model sees with native tool calling and with prompt-injected tools alike, and what MCP clients see. hint is optional and only fills your own mode's {{TOOL_HINTS}} placeholder; keep it to what the description cannot say. The builtin tools set no hint.

A tool is sent to the model up front unless the context window is small. Set the class attribute context_group to something other than "core" for a tool the model only needs now and then; on small windows it is then held back until the model asks for it (see "Tools by context window" in docs/chat-modes.md). Tools your mode lists under tools: are always sent.

A pipe that finishes a mesh pipeline:

python
from src.plugin_api import GalleryGenerationOutput, MeshGenerationOutput


def process(self, pipe_input, generation_outputs):
    generation_outputs(GalleryGenerationOutput(
        images=[],
        meshes=[MeshGenerationOutput(mesh_path=path, temporary=False, seed=seed)],
    ))

The core gallery pipe takes only IOType.IMAGE and IOType.VIDEO, so a mesh pipeline builds its own GalleryGenerationOutput in its terminal pipe rather than routing through it. temporary=False is what persists the file and writes its files row; temporary=True saves to tmp/ and records nothing. Only .glb is accepted, and the file is validated as glTF-binary before it is stored — an emitted file that is not one fails the save rather than being served to a viewer as a broken mesh.

A hook handler:

python
from src.plugin_api import HookContext


def on_generation_complete(context: HookContext) -> HookContext:
    context.data["my-plugin.notified"] = True
    context.metadata["my-plugin.seen"] = context.data["generation_id"]
    return context

Handlers are synchronous. The hook chain calls your handler and reads the returned HookContext immediately, so an async def handler is rejected at registration and a handler that returns a coroutine is recorded as a failed hook result. Return the context you were given (data changes and metadata annotations both propagate to the next handler and to the caller); returning anything else is a contract error.

To hand long-running work back to the caller, schedule it and leave the awaitables under HookContext.data[HOOK_BLOCKING_WAITS_KEY] (the constant lives in src/platform/plugins/hooks.py; its value is the literal key "__hook_blocking_waits__"). Only call sites that drain them wait: today generation.before_start (src/features/generation/orchestrator.py) and chat.message.before_send (src/features/chat/conversation.py). Every other hook point ignores the key, so a handler for one of them must not rely on the work being finished when the hook returns.

Reaching the application's managers — always inside the function that needs them, never at import time, because the container does not exist yet while your module is being imported:

python
from src.plugin_api import get_container


async def start_something():
    orchestrator = get_container().generation_orchestrator
    ...