|
| 1 | +"""Canonical registry of semantic roles for Dash component props. |
| 2 | +
|
| 3 | +A ``PropRole`` bundles the set of ``(component_type, property)`` pairs |
| 4 | +that play the same role with the metadata attached to that role: |
| 5 | +an LLM-facing description, an input JSON Schema, etc. Tool descriptions, |
| 6 | +input-schema overrides, and result formatters all consume this registry |
| 7 | +so they can't drift. |
| 8 | +
|
| 9 | +Use ``ANY_COMPONENT`` as the component_type sentinel to match any component with |
| 10 | +the given property name. |
| 11 | +
|
| 12 | +Declaration order matters: ``iter_prop_roles()`` yields roles in the |
| 13 | +order they're defined in this module, and the first match wins. List |
| 14 | +concrete-match roles before wildcard-match roles that share a prop |
| 15 | +name (e.g. ``MARKDOWN`` before ``CONTENT`` for ``children``). |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from typing import Any, Callable, Dict, Iterator, NamedTuple, Union |
| 21 | + |
| 22 | +from typing_extensions import TypeAlias |
| 23 | + |
| 24 | +from dash.mcp.types import MCPInput |
| 25 | + |
| 26 | +PropSchema = Union[ |
| 27 | + Dict[str, Any], |
| 28 | + Callable[[MCPInput], Dict[str, Any]], |
| 29 | +] |
| 30 | + |
| 31 | +COMPONENT: TypeAlias = Union[str, None] |
| 32 | +ANY_COMPONENT: None = None |
| 33 | +PROP: TypeAlias = str |
| 34 | + |
| 35 | + |
| 36 | +class PropRole(NamedTuple): |
| 37 | + identifiers: set[tuple[COMPONENT, PROP]] |
| 38 | + description: str | None = None |
| 39 | + input_schema: PropSchema | None = None |
| 40 | + |
| 41 | + def matches(self, component_type: COMPONENT, prop: PROP) -> bool: |
| 42 | + """True if this role applies to the given ``(component_type, prop)``. |
| 43 | +
|
| 44 | + Matches either a concrete entry or an ``ANY_COMPONENT`` wildcard |
| 45 | + entry in ``identifiers``. Shared by every consumer so all metadata |
| 46 | + fields apply uniformly to every identifier in the role. |
| 47 | + """ |
| 48 | + return (component_type, prop) in self.identifiers or ( |
| 49 | + ANY_COMPONENT, |
| 50 | + prop, |
| 51 | + ) in self.identifiers |
| 52 | + |
| 53 | + |
| 54 | +def _compute_dropdown_value_schema(param: MCPInput) -> dict[str, Any]: |
| 55 | + """Dropdown values are an array if ``multi=True``; scalar otherwise.""" |
| 56 | + _DROPDOWN_SCALAR_TYPE = { |
| 57 | + "anyOf": [{"type": "string"}, {"type": "number"}, {"type": "boolean"}] |
| 58 | + } |
| 59 | + component = param.get("component") |
| 60 | + if getattr(component, "multi", False): |
| 61 | + return {"type": "array", "items": _DROPDOWN_SCALAR_TYPE} |
| 62 | + return _DROPDOWN_SCALAR_TYPE |
| 63 | + |
| 64 | + |
| 65 | +TABULAR = PropRole( |
| 66 | + identifiers={("DataTable", "data"), ("AgGrid", "rowData")}, |
| 67 | + description="Returns tabular data", |
| 68 | +) |
| 69 | + |
| 70 | +DATE = PropRole( |
| 71 | + identifiers={ |
| 72 | + ("DatePickerSingle", "date"), |
| 73 | + ("DatePickerRange", "start_date"), |
| 74 | + ("DatePickerRange", "end_date"), |
| 75 | + }, |
| 76 | + input_schema={ |
| 77 | + "type": "string", |
| 78 | + "format": "date", |
| 79 | + "pattern": r"^\d{4}-\d{2}-\d{2}$", |
| 80 | + }, |
| 81 | +) |
| 82 | + |
| 83 | +DROPDOWN_VALUE = PropRole( |
| 84 | + identifiers={("Dropdown", "value")}, |
| 85 | + input_schema=_compute_dropdown_value_schema, |
| 86 | +) |
| 87 | + |
| 88 | +STORE_DATA = PropRole( |
| 89 | + identifiers={("Store", "data")}, |
| 90 | + description="Returns data to be remembered client-side", |
| 91 | +) |
| 92 | + |
| 93 | +DOWNLOAD = PropRole( |
| 94 | + identifiers={("Download", "data")}, |
| 95 | + description="Returns downloadable content", |
| 96 | +) |
| 97 | + |
| 98 | +MARKDOWN = PropRole( |
| 99 | + identifiers={("Markdown", "children")}, |
| 100 | + description="Returns formatted text", |
| 101 | +) |
| 102 | + |
| 103 | +GENERIC_FIGURE = PropRole( |
| 104 | + identifiers={(ANY_COMPONENT, "figure")}, |
| 105 | + description="Returns chart/visualization data", |
| 106 | + input_schema={ |
| 107 | + "type": "object", |
| 108 | + "properties": { |
| 109 | + "data": {"type": "array", "items": {"type": "object"}}, |
| 110 | + "layout": {"type": "object"}, |
| 111 | + "frames": {"type": "array", "items": {"type": "object"}}, |
| 112 | + }, |
| 113 | + }, |
| 114 | +) |
| 115 | + |
| 116 | +GENERIC_CONTENT = PropRole( |
| 117 | + identifiers={(ANY_COMPONENT, "children")}, |
| 118 | + description="Returns content", |
| 119 | +) |
| 120 | + |
| 121 | +GENERIC_VALUE = PropRole( |
| 122 | + identifiers={(ANY_COMPONENT, "value")}, |
| 123 | + description="Returns the current value", |
| 124 | +) |
| 125 | + |
| 126 | +GENERIC_OPTIONS = PropRole( |
| 127 | + identifiers={(ANY_COMPONENT, "options")}, |
| 128 | + description="Returns available options", |
| 129 | +) |
| 130 | + |
| 131 | +GENERIC_COLUMNS = PropRole( |
| 132 | + identifiers={(ANY_COMPONENT, "columns")}, |
| 133 | + description="Returns column definitions", |
| 134 | +) |
| 135 | + |
| 136 | +GENERIC_STYLE = PropRole( |
| 137 | + identifiers={(ANY_COMPONENT, "style")}, |
| 138 | + description="Updates styling", |
| 139 | +) |
| 140 | + |
| 141 | +GENERIC_DISABLED = PropRole( |
| 142 | + identifiers={(ANY_COMPONENT, "disabled")}, |
| 143 | + description="Updates enabled/disabled state", |
| 144 | +) |
| 145 | + |
| 146 | + |
| 147 | +def iter_prop_roles() -> Iterator[PropRole]: |
| 148 | + """Yield every PropRole defined in this module in declaration order.""" |
| 149 | + for value in globals().values(): |
| 150 | + if isinstance(value, PropRole): |
| 151 | + yield value |
0 commit comments