Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

13.0 KB
Raw Blame
"""Plugin Marketplace catalog schema.

Loaded from `data/plugin_catalog.json` at server startup and re-served to
the frontend via `/api/marketplace/plugins`. Five auth modes are modelled
today; each is its own Pydantic submodel and `AuthConfig` is a
discriminated union over the `mode` field.

Packaging policy: new marketplace plugins are submitted in the Agent
Plugins v1.0.0 format (https://agent-plugins.org/) and the existing
entries migrate to it — see docs/marketplace/agent-plugins-standard.md
for the field mapping and the per-plugin migration tracker before adding
or changing a catalog entry.
"""

from __future__ import annotations

from typing import Annotated, Any, Literal

from pydantic import BaseModel, ConfigDict, Field


class _BaseAuth(BaseModel):
    """`extra="forbid"`: a typo in the JSON raises ValidationError instead
    of silently dropping fields. Catalog drift is the failure mode we paid
    for once already."""

    model_config = ConfigDict(extra="forbid")


class InstanceUrlSpec(_BaseAuth):
    """A per-user base address, for services the user hosts themselves.

    Home Assistant, Jellyfin, Nextcloud, Paperless and Immich have no fixed
    public endpoint: the address IS user data, so the catalog can only describe
    the field, never fill it. When present, the connect dialog asks for the
    address alongside the token and `validation_endpoint` is treated as a path
    RELATIVE to it.
    """

    label: str
    placeholder: str
    # Appended to the entered address to check the credential works. Kept
    # separate from the address so a user pasting a trailing slash, a path, or
    # the full URL of some page still validates.
    validation_path: str = "/"
    help_md: str | None = None


class PatPasteAuth(_BaseAuth):
    mode: Literal["pat_paste"]
    token_creation_url: str
    token_prefix: str
    # Additional accepted token prefixes (e.g. GitHub fine-grained PATs start
    # with ``github_pat_`` while classic tokens start with ``ghp_``). The
    # primary ``token_prefix`` above stays the canonical one; this list only
    # widens the format pre-check so valid provider-issued tokens are not
    # rejected before validation. Empty by default (no behaviour change).
    token_prefixes: list[str] = Field(default_factory=list)
    validation_endpoint: str
    instruction_md: str
    # How the pasted token is proven to work. ``http_get`` expects a 200 from
    # ``validation_endpoint``; ``mcp_initialize`` sends an MCP handshake to it
    # with the plugin's own auth header, for custom connectors whose server
    # has no REST "who am I" endpoint to ask.
    validation_method: Literal["http_get", "mcp_initialize"] = "http_get"
    # Set for self-hosted services: the user supplies the address, so
    # `validation_endpoint` above is ignored in favour of
    # `instance_url.validation_path` appended to what they entered.
    instance_url: InstanceUrlSpec | None = None
    # How to present the pasted token when validating + wiring downstream:
    #   bearer        -> Authorization: Bearer <token>  (GitHub/Vercel/Supabase)
    #   bot           -> Authorization: Bot <token>      (Discord)
    #   telegram_path -> token spliced into the URL {token}, no header; body ok==true
    auth_scheme: Literal["bearer", "bot", "telegram_path", "figma"] = "bearer"


class InstanceBrowserAuth(_BaseAuth):
    """Browser approval against a user-owned instance, without app registration."""

    mode: Literal["instance_browser"]
    protocol: Literal["home_assistant"] = "home_assistant"
    instance_url: InstanceUrlSpec


class OAuthDeviceFlowAuth(_BaseAuth):
    mode: Literal["oauth_device_flow"]
    device_url: str
    verify_url: str
    token_url: str
    client_id: str
    scopes: list[str]
    access_token_ttl_seconds: int | None = None
    refresh_token_ttl_seconds: int | None = None


class HostedMcpOAuthDcrAuth(_BaseAuth):
    mode: Literal["hosted_mcp_oauth_dcr"]
    discovery_url: str
    mcp_url: str
    fallback_mcp_url: str | None = None
    access_token_ttl_seconds: int | None = None
    refresh_supported: bool = False
    capabilities: list[str] = Field(default_factory=list)
    scopes: list[str] | None = None


class OAuthPkceLoopbackAuth(_BaseAuth):
    mode: Literal["oauth_pkce_loopback"]
    authorization_url: str
    token_url: str
    # Figma refreshes at its own endpoint (/v1/oauth/refresh), not token_url.
    refresh_url: str | None = None
    revocation_url: str | None = None
    client_id: str
    client_secret: str | None = Field(default=None, exclude=True)
    callback_port: int = 0
    callback_path: str = "/oauth/callback"
    scopes: list[str]
    scope_separator: Literal["comma", "space"] = "comma"
    user_scopes_only: bool = False
    refresh_supported: bool = False
    refresh_token_ttl_days: int | None = None
    # RFC 8707 resource indicator (Asana V2 MCP needs resource=…/v2).
    resource: str | None = None
    # Google desktop clients need access_type=offline + prompt=consent to
    # return a refresh token.
    offline_access: bool = False
    client_auth_method: Literal["client_secret_post", "client_secret_basic"] = "client_secret_post"


class HostedMcpAllowlistAuth(_BaseAuth):
    mode: Literal["hosted_mcp_allowlist"]
    mcp_url: str
    application_url: str | None = None


class LocalAuth(_BaseAuth):
    """Enable a local connector only after its capability probe succeeds."""

    mode: Literal["local"]


class HostedMcpOpenAuth(_BaseAuth):
    """A remote MCP server that answers without any sign-in.

    Connecting is still an explicit step: the server is probed once, and only
    an answered handshake marks it connected, so the on/off state stays the
    user's choice like every other plugin's.
    """

    mode: Literal["hosted_mcp_open"]
    mcp_url: str


AuthConfig = Annotated[
    PatPasteAuth
    | InstanceBrowserAuth
    | OAuthDeviceFlowAuth
    | HostedMcpOAuthDcrAuth
    | OAuthPkceLoopbackAuth
    | HostedMcpAllowlistAuth
    | LocalAuth
    | HostedMcpOpenAuth,
    Field(discriminator="mode"),
]


# Display order for the Plugins view, served to the frontend alongside the
# catalog. `category` is deliberately a plain string, NOT a Literal: the value
# crosses Python -> JSON -> TS -> the section grouping, and pinning it in every
# layer is the multi-layer drift class this repo has paid for four times
# (docs/anti-drift-three-layer.md). With the order declared here and the
# frontend appending anything it does not recognize, adding a category is a
# backend-only change. `test_catalog_seed` asserts every category the shipped
# seed uses appears below, so a typo is still caught — just not by breaking a
# user's local catalog on upgrade.
CATEGORY_ORDER: tuple[str, ...] = (
    "Home & Devices",
    "Lists & Tasks",
    "Calendar & Mail",
    "Messaging",
    "Knowledge & Reading",
    "Media & Creativity",
    "Files & Photos",
    "Developer",
)


# How long a connection survives once made — the honest answer to "this must
# never expire", surfaced on the card BEFORE the user connects.
#   permanent        - the credential has no expiry mechanism at all
#   self_renewing    - it expires, but background refresh keeps it alive
#                      indefinitely (the common OAuth case)
#   provider_limited - the provider forces re-authorization on a schedule we
#                      cannot extend; `longevity_note` must say how often
Longevity = Literal["permanent", "self_renewing", "provider_limited"]

# Whether the full browser journey of a plugin was observed end to end
# (docs/marketplace/plugin-e2e-audit.json). Rendered as a badge on the card.
#   verified - a completed PASS row exists for the current auth/execution
#              configuration; scripts/ci/check_plugin_auth_contract.py
#              rejects this label on any other row
#   preview  - shipped, but not yet proven end to end; sign-in may still need
#              publisher setup. The default, so nothing is verified by omission.
Acceptance = Literal["verified", "preview"]


class PluginSpec(_BaseAuth):
    id: str
    display_name: str
    description: str
    category: str
    # Provenance. `source` is a plain string, not a Literal, for the same
    # multi-layer-drift reason as `category` above: the value crosses
    # Python -> JSON -> TS, and the frontend only ever compares it against
    # "community" to decide whether to render the not-reviewed badge. Every
    # existing seed entry and user override stays valid via the default.
    source: str = "seed"
    # GitHub login that published a community entry; None for shipped seeds.
    publisher: str | None = None
    # Manifest version string from the Agent Plugins package (community
    # entries); the seed catalog versions as a whole via `PluginCatalog`.
    version: str | None = None
    # Where the entry came from (registry page for community entries).
    source_url: str | None = None
    logo_slug: str
    logo_color: str | None = None
    # When set, the frontend uses this URL instead of the simpleicons CDN.
    # Use for brands whose original logo is multicolor (e.g. Slack's hash).
    logo_url: str | None = None
    featured: bool = False
    # Honest connection lifetime, rendered as a badge on the card. Defaulted so
    # every already-shipped entry stays valid; each catalog entry should still
    # state it explicitly rather than inherit the default.
    longevity: Longevity = "self_renewing"
    # One plain sentence shown next to the badge — mandatory in spirit for
    # `provider_limited` ("Google requires re-approval every 7 days while the
    # OAuth app is in Testing mode"), optional otherwise.
    longevity_note: str | None = None
    # End-to-end acceptance label (see `Acceptance`). Only the seed catalog
    # sets "verified"; community and uploaded entries carry their own
    # not-reviewed badges instead.
    acceptance: Acceptance = "preview"
    # Which `<family>_oauth_client_id` secret pair overrides this plugin's
    # catalog client, for providers where the downloader must register their
    # own OAuth app. Lives here so a new plugin stays a catalog-only change:
    # this replaced a hand-maintained map that was duplicated in the connect
    # helper AND the frontend, where a missing entry silently removed the
    # "use your own OAuth client" affordance.
    oauth_client_family: str | None = None
    # Browser-auth standard (see docs/marketplace/browser-auth-standard.md).
    # `browser_flow` names the standard-path flow the normal user takes:
    #   "dcr"            — hosted MCP + Dynamic Client Registration (no static
    #                      client at all; the reference standard path)
    #   "publisher_pkce" — Authorization Code + PKCE loopback against the
    #                      publisher-provisioned shared client
    #   "device"         — official provider device flow (short-lived user
    #                      code only, never a self-made API token)
    #   "hosted"         — server-side OAuth via the hosted callback
    #   "local"          — no account needed (capability probe instead)
    #   "manual_token"   — provider offers no browser flow; pasting a
    #                      provider-issued key is the only official way (does
    #                      NOT satisfy the standard — honest fallback label)
    # `publisher_managed` promises the publisher ships/registers the client,
    # so no end-user developer setup is needed. `verification_hook` names the
    # side-effect-free check run before "Connected" is shown. All optional so
    # every already-shipped entry stays valid; the CI contract gate
    # (scripts/ci/check_plugin_auth_contract.py) requires them for new
    # auth-bearing plugins.
    browser_flow: str | None = None
    publisher_managed: bool = False
    verification_hook: str | None = None
    # Expert fallback when the primary browser flow has no publisher client
    # yet: a PAT block the dialog offers ONLY as a collapsed alternative
    # ("paste a token instead"). PAT-only by design — a browser primary with
    # a browser fallback is a contradiction, and two browser flows would need
    # two callbacks. Lets an OAuth-capable plugin ship its browser flow as
    # the default while the previously working token path keeps working until
    # the publisher registration lands. None = no fallback (previous
    # behaviour verbatim).
    fallback_auth: PatPasteAuth | None = None
    auth: AuthConfig
    # Installer/transport metadata for the eventual MCP-spawn wave. The route
    # layer does not consume this today, but the catalog already carries it,
    # and rejecting unknown subkeys here would just force a schema bump every
    # time a new transport variant lands. Keep it loosely typed.
    mcp_server: dict[str, Any] | None = None
    # Native in-process router tool backing this plugin when no MCP server
    # exists (Gmail uses the REST API directly with marketplace-stored tokens).
    native_tool: str | None = None
    post_install_hint_md: str | None = None
    future_v2_note: str | None = None


class PluginCatalog(_BaseAuth):
    version: int
    schema_version: str
    plugins: list[PluginSpec]

    def by_id(self, plugin_id: str) -> PluginSpec | None:
        return next((p for p in self.plugins if p.id == plugin_id), None)