Skip to content

Context

GeoAgentContext carries the runtime state passed into every agent invocation: the live map widget, the QGIS interface and project, the working directory, and free-form user preferences.

geoagent.core.context

Runtime context for GeoAgent tools and prompts.

GeoAgentContext dataclass

Scalar and handle fields for a GeoAgent run.

Live objects (map widgets, QGIS iface) are normally captured via closure in tool factories; this dataclass holds references for prompts and tools that read context explicitly.

Attributes:

Name Type Description
map_obj Optional[Any]

leafmap.Map, anymap.Map, or similar.

qgis_iface Optional[Any]

QGIS QgisInterface when inside QGIS.

qgis_project Optional[Any]

Optional QgsProject (tools may fall back to instance).

workdir Path

Working directory for downloads and exports.

current_layer Optional[str]

Optional layer name hint for follow-up commands.

user_preferences dict[str, Any]

Free-form preferences for prompts/tools.

metadata dict[str, Any]

Arbitrary JSON-serializable metadata for integrations.

Source code in geoagent/core/context.py
@dataclass
class GeoAgentContext:
    """Scalar and handle fields for a GeoAgent run.

    Live objects (map widgets, QGIS iface) are normally captured via closure
    in tool factories; this dataclass holds references for prompts and tools
    that read context explicitly.

    Attributes:
        map_obj: leafmap.Map, anymap.Map, or similar.
        qgis_iface: QGIS ``QgisInterface`` when inside QGIS.
        qgis_project: Optional ``QgsProject`` (tools may fall back to instance).
        workdir: Working directory for downloads and exports.
        current_layer: Optional layer name hint for follow-up commands.
        user_preferences: Free-form preferences for prompts/tools.
        metadata: Arbitrary JSON-serializable metadata for integrations.
    """

    map_obj: Optional[Any] = None
    qgis_iface: Optional[Any] = None
    qgis_project: Optional[Any] = None
    workdir: Path = field(default_factory=Path.cwd)
    current_layer: Optional[str] = None
    user_preferences: dict[str, Any] = field(default_factory=dict)
    metadata: dict[str, Any] = field(default_factory=dict)

    def with_overrides(self, **kwargs: Any) -> "GeoAgentContext":
        """Return a copy with selected fields replaced."""
        from dataclasses import replace

        return replace(self, **kwargs)
with_overrides(self, **kwargs)

Return a copy with selected fields replaced.

Source code in geoagent/core/context.py
def with_overrides(self, **kwargs: Any) -> "GeoAgentContext":
    """Return a copy with selected fields replaced."""
    from dataclasses import replace

    return replace(self, **kwargs)