Skip to content

API Reference parrot.handlers.models.ui_surfaces

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.handlers.models.ui_surfaces

View source

PgUISurfaceStore — persistence for the ui_surfaces plane (FEAT-492).

Owns two auto-created Postgres tables:

  • navigator.ui_surfaces — the persisted A2UI surface (dashboard, infographic, or widget) with its rehydratable envelope and, when it was produced by a recipe, the recipe_ref needed for in-place refresh.
  • navigator.ui_surface_shares — opaque, revocable, DB-stored share tokens granting read+refresh access; claimed_by/claimed_at record which authenticated user first redeemed a token (shared-with-me listing).

Follows the AsyncDB("pg", dsn=default_dsn) per-call idiom used by handlers/comm_center.py (_get_db() + async with await db.connection() as conn:) and the CREATE TABLE IF NOT EXISTS auto-create convention illustrated by handlers/models/bots.py / autonomous/ledger.py.

Class PgUISurfaceStore

Postgres store for persisted A2UI surfaces + share tokens.

Parameter Type Default Description
dsn str | None None Optional connection string; defaults to :data:parrot.conf.default_dsn (spec §8 resolved decision).

Method PgUISurfaceStore.ensure_schema

Create navigator.ui_surfaces / ui_surface_shares if missing.

Idempotent — CREATE ... IF NOT EXISTS everywhere. Tolerates duplicate-object races under concurrent first-calls (two workers racing to auto-create the same index/table).

ensure_schema() -> None

Method PgUISurfaceStore.save

Persist a :class:UISurfaceRecord.

save(record: UISurfaceRecord, *, overwrite: bool = False) -> str

Parameter Type Default Description
record UISurfaceRecord The surface row to persist.
overwrite bool False When True, upsert (ON CONFLICT ... DO UPDATE). When False (default), a duplicate surface_id raises ValueError.

Returns: str — The persisted surface_id.

Method PgUISurfaceStore.get

Fetch a surface by id, or None if it does not exist.

get(surface_id: str) -> UISurfaceRecord | None

Parameter Type Default Description
surface_id str

Returns: UISurfaceRecord | None

Method PgUISurfaceStore.list

List surfaces owned by user_id, optionally filtered by kind.

list(user_id: str, *, kind: UISurfaceKind | None = None) -> list[UISurfaceRecord]

Parameter Type Default Description
user_id str
kind UISurfaceKind | None None

Returns: list[UISurfaceRecord]

Method PgUISurfaceStore.list_shared_with

List surfaces reachable via a live share token claimed by user_id.

list_shared_with(user_id: str) -> list[UISurfaceRecord]

Parameter Type Default Description
user_id str

Returns: list[UISurfaceRecord]

Method PgUISurfaceStore.list_visible

List surfaces owned by, or tenant/group/superuser-visible to, scope.

list_visible(scope: Any, *, kind: UISurfaceKind | None = None) -> list[UISurfaceRecord]

Parameter Type Default Description
scope Any A SurfaceScope-shaped object exposing user_id, tenant, groups (an iterable of str) and is_superuser. Declared as Any and accessed by duck-typed attribute so this module never imports the handler-package SurfaceScope type (spec §3 Module 1).
kind UISurfaceKind | None None Optional kind filter.

Returns: list[UISurfaceRecord] — Rows ordered by updated_at descending. Does NOT include share-token-only surfaces — see :meth:list_shared_with.

Method PgUISurfaceStore.update_visibility

Change visibility/allowed_groups for a surface, owner-only.

Enforced in SQL (WHERE surface_id = $1 AND user_id = $2), not by comparing in Python after a read (spec §7 pattern).

update_visibility(surface_id: str, user_id: str, visibility: SurfaceVisibility, allowed_groups: list[str]) -> bool

Parameter Type Default Description
surface_id str
user_id str
visibility SurfaceVisibility
allowed_groups list[str]

Returns: bool — True if the owner's row was updated, False for an unknown surface or a non-owner caller — indistinguishable by design (no existence oracle).

Method PgUISurfaceStore.update_envelope

Replace envelope/recipe_params in place, bumping updated_at.

With expected_updated_at the update is conditional (optimistic concurrency, FEAT-598 S11): it only applies when the row's updated_at still equals the value the caller read; a newer snapshot wins.

update_envelope(surface_id: str, envelope: dict[str, Any], recipe_params: dict[str, Any], *, expected_updated_at: datetime | None = None) -> bool

Parameter Type Default Description
surface_id str
envelope dict[str, Any]
recipe_params dict[str, Any]
expected_updated_at datetime | None None

Returns: bool — True when a row was updated; False for an unknown surface or a lost race.

Method PgUISurfaceStore.delete

Delete a surface owned by user_id. Returns True if a row was removed.

delete(surface_id: str, user_id: str) -> bool

Parameter Type Default Description
surface_id str
user_id str

Returns: bool

Method PgUISurfaceStore.mint_share

Mint a new opaque share token granting read+refresh access.

mint_share(surface_id: str, *, expires_at: datetime | None = None, use_default_ttl: bool = False) -> UISurfaceShare

Parameter Type Default Description
surface_id str The surface this token grants access to.
expires_at datetime | None None Explicit expiry. Takes precedence over use_default_ttl when set.
use_default_ttl bool False When True and expires_at is not given, default to :attr:DEFAULT_SHARE_TTL_DAYS (90 days) from now (spec §8 resolved decision). Default: no expiry.

Returns: UISurfaceShare

Method PgUISurfaceStore.resolve_share

Resolve a live (non-revoked, non-expired) share token.

Returns None for a missing, revoked, or expired token — indistinguishable by design (no existence oracle).

resolve_share(token: str) -> UISurfaceShare | None

Parameter Type Default Description
token str

Returns: UISurfaceShare | None

Method PgUISurfaceStore.claim_share

Record user_id as the first authenticated claimant of token.

Idempotent: only sets claimed_by/claimed_at when unset — the first authenticated user wins; later users still get read access via the token, but shared-with-me listing keeps the original claimant.

claim_share(token: str, user_id: str) -> None

Parameter Type Default Description
token str
user_id str

Method PgUISurfaceStore.revoke_share

Revoke a share token. Returns True if a matching token was found.

revoke_share(token: str, surface_id: str) -> bool

Parameter Type Default Description
token str
surface_id str

Returns: bool

Method PgUISurfaceStore.list_shares

List every share token (live or not) minted for surface_id.

list_shares(surface_id: str) -> list[UISurfaceShare]

Parameter Type Default Description
surface_id str

Returns: list[UISurfaceShare]

Class SurfaceVisibility

Who, besides the owner, may see a surface (FEAT-535).

  • private: owner + share tokens only (today's behaviour, the default).
  • tenant: every caller whose scope tenant matches the record's tenant.
  • groups: tenant match AND the caller's groups intersect allowed_groups.

Class UISurfaceKind

Kind of persisted A2UI surface.

Class UISurfaceRecord

Row shape of navigator.ui_surfaces.

Fields:

Field Type Default Description
surface_id str
kind UISurfaceKind
title str
envelope dict[str, Any]
catalog_id str | None None
agent_id str
user_id str
session_id str | None None
recipe_name str | None None
recipe_owner str | None None
recipe_params dict[str, Any]
tenant str | None None
visibility SurfaceVisibility SurfaceVisibility.private
allowed_groups list[str]
created_at datetime
updated_at datetime

Attributes:

Attribute Type Description
refreshable bool Refreshable via RecipeRunner replay (recipe_ref) or via its linked data-source descriptor.

Class UISurfaceShare

Row shape of navigator.ui_surface_shares.

Fields:

Field Type Default Description
token str
surface_id str
permissions Literal['read+refresh'] 'read+refresh'
expires_at datetime | None None
revoked bool False
claimed_by str | None None
claimed_at datetime | None None
created_at datetime

Alias default_dsn

Re-exported from parrot.conf.default_dsn.

Alias has_data_sources

Re-exported from parrot.outputs.a2ui.linked.has_data_sources.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally