3. Agent Plugins#
The agent loop in pycsamt.agents is general (route a request,
build a plan, gate it, run a chain of agents, record what happened),
but three of its pieces are specific to CSAMT/MT processing: the
catalogue of workflows, the rules that decide whether a plan may run,
and the run record. pycsamt.agents.plugins names those pieces as
typing.Protocol classes, so another package can supply its own
domain without forking pyCSAMT. A bundle of the three is an
agent plugin.
See also
- Review Gate
How the validator of a plugin is used before execution.
- Workflow Orchestrator
The dispatcher that looks up the active plugin.
pycsamt.agents.pluginsAPI reference.
3.1. The Three Protocols#
The protocols are structural: a plain class that has the listed attributes and methods satisfies them, no inheritance is needed.
Protocol |
Members and contract |
|---|---|
|
|
|
|
|
|
DomainPlugin bundles a registry, a
validator and a record_factory(payload) -> RecordWriter under a
name. It checks each member against its protocol when it is built and
raises TypeError when one does not fit.
3.2. The Built-In Plugin#
get_plugin() with no name returns the built-in "pycsamt" plugin.
Its adapters delegate to the existing workflow tables, plan validation
and route checks, so the default behaviour of the agents is the same as
before the interface existed.
1>>> from pycsamt.agents.plugins import get_plugin
2>>> base = get_plugin()
3>>> base.name, base.registry.normalise("PINN")
4('pycsamt', 'pinn_inversion')
5>>> base.registry.classify("compute the phase tensor ellipses")
6'pt_psection'
7>>> base.registry.family("pt_psection")
8'phase_analysis'
The built-in name is reserved: register_plugin refuses a plugin
called "pycsamt".
In this release, WorkflowOrchestratorAgent
consults the active plugin for the list of dispatchable workflows, the
step chain, the orchestrator-level route flags, plan validation and the
review decision. The intent router and the run record still use the
built-in pieces, and the agent class names in a step chain are resolved
against the pyCSAMT agent registry. A plugin whose steps name agents of
its own can therefore be registered, classified and validated, but its
chain cannot yet be executed by the orchestrator.
3.3. Registering A Plugin#
examples/agent_plugins/mtpy_example in the repository is a complete
plugin for three MTpy-style workflows: reading EDI files and plotting
apparent resistivity and phase, computing the phase tensor, and
estimating strike from it. MTpy is needed only by its step functions;
importing, registering, classifying and validating work without it.
Run from the repository root:
1>>> import sys
2>>> sys.path.insert(0, "examples/agent_plugins")
3>>> from mtpy_example import make_plugin
4>>> from pycsamt.agents.plugins import available_plugins, register_plugin
5
6>>> plugin = register_plugin(make_plugin(), replace=True)
7>>> sorted(available_plugins())
8['mtpy_example', 'pycsamt']
9>>> plugin.registry.classify("estimate strike for site 12")
10'strike'
11>>> plugin.registry.family("strike"), plugin.registry.normalise("PT")
12('phase_tensor', 'phase_tensor')
13>>> plugin.validator.cross_check(
14... "plot the phase tensor ellipses", "workflow", "read_edi_plot_rhophi")
15"LLM workflow 'read_edi_plot_rhophi' disagrees with keyword route 'phase_tensor'."
16>>> plugin.validator.route_flags("plot the phase tensor ellipses", "strike")
17[]
strike belongs to the phase_tensor family of this plugin, so a
language model that chose strike for a phase-tensor request is not
flagged, while one that chose the resistivity plot is. The example
validator also checks its own parameters: freq_band must lie in
\(10^{-5}\) to \(10^{5}\) Hz and increase,
strike_tolerance_deg must be in \((0, 90]\) degrees and
n_periods must be an integer between 1 and 500.
replace=True overwrites a plugin of the same name; without it a
second registration raises ValueError.
To make the orchestrator use a plugin, set its plugin_name class
attribute, for example in a subclass:
from pycsamt.agents import WorkflowOrchestratorAgent
class MTpyOrchestrator(WorkflowOrchestratorAgent):
plugin_name = "mtpy_example"
3.4. Shipping A Plugin#
A third-party distribution advertises its plugin in the
pycsamt.agent_plugins entry-point group, distinct from the
pycsamt.pipeline.steps group used by pipeline step plugins. The entry
point may resolve to a DomainPlugin instance or to a zero-argument
callable that returns one:
[project.entry-points."pycsamt.agent_plugins"]
mtpy_example = "mtpy_example:make_plugin"
After the package is installed, get_plugin("mtpy_example") finds it:
an unknown name triggers discover_plugins()
once before KeyError is raised. Discovery runs once per
process, never replaces a plugin that is already registered, and logs
a failing entry point at WARNING level without stopping the agent loop.
discover_plugins() returns the status of each entry point
("loaded", "skipped" or the error message), which is the first
thing to check when a plugin does not appear.