lightfall 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lightfall/__init__.py +8 -0
- lightfall/__main__.py +10 -0
- lightfall/_version.py +24 -0
- lightfall/acquire/__init__.py +74 -0
- lightfall/acquire/buffer.py +363 -0
- lightfall/acquire/device_wrapper.py +416 -0
- lightfall/acquire/engine/__init__.py +155 -0
- lightfall/acquire/engine/base.py +587 -0
- lightfall/acquire/engine/bluesky.py +358 -0
- lightfall/acquire/engine/mock.py +165 -0
- lightfall/acquire/engine/plugins/__init__.py +9 -0
- lightfall/acquire/engine/plugins/bluesky_plugin.py +56 -0
- lightfall/acquire/engine/plugins/mock_plugin.py +55 -0
- lightfall/acquire/engine/protocol.py +145 -0
- lightfall/acquire/engine/registry.py +152 -0
- lightfall/acquire/engine/state.py +32 -0
- lightfall/acquire/engine/waiting_hook.py +225 -0
- lightfall/acquire/nats_bridge.py +62 -0
- lightfall/acquire/plan_ui.py +78 -0
- lightfall/acquire/plans/__init__.py +38 -0
- lightfall/acquire/plans/adaptive.py +288 -0
- lightfall/acquire/plans/example_plans.py +995 -0
- lightfall/acquire/plans/lightfall_plans.py +587 -0
- lightfall/acquire/plans/registry.py +543 -0
- lightfall/acquire/plans/standard_plans.py +186 -0
- lightfall/acquire/plans/user_plans.py +470 -0
- lightfall/acquire/runengine.py +36 -0
- lightfall/acquire/signals.py +452 -0
- lightfall/acquire/triggers/__init__.py +16 -0
- lightfall/acquire/triggers/base.py +25 -0
- lightfall/acquire/triggers/filter.py +37 -0
- lightfall/acquire/triggers/manager.py +73 -0
- lightfall/acquire/triggers/manual.py +36 -0
- lightfall/acquire/triggers/run_end.py +74 -0
- lightfall/acquire/triggers/run_start.py +52 -0
- lightfall/auth/__init__.py +38 -0
- lightfall/auth/policy.py +518 -0
- lightfall/auth/providers/__init__.py +30 -0
- lightfall/auth/providers/base.py +124 -0
- lightfall/auth/providers/keycloak.py +1017 -0
- lightfall/auth/providers/local.py +319 -0
- lightfall/auth/providers/pam.py +250 -0
- lightfall/auth/service_key.py +173 -0
- lightfall/auth/service_key_auth.py +79 -0
- lightfall/auth/session.py +656 -0
- lightfall/claude/__init__.py +52 -0
- lightfall/claude/_internal/__init__.py +1 -0
- lightfall/claude/_internal/serialization.py +195 -0
- lightfall/claude/_internal/threading.py +61 -0
- lightfall/claude/_internal/worker.py +757 -0
- lightfall/claude/_session_assembly.py +124 -0
- lightfall/claude/agent.py +596 -0
- lightfall/claude/lightfall_core_tools.py +410 -0
- lightfall/claude/markdown.py +103 -0
- lightfall/claude/permission_manager.py +588 -0
- lightfall/claude/tools/__init__.py +45 -0
- lightfall/claude/tools/controller.py +143 -0
- lightfall/claude/tools/interaction.py +177 -0
- lightfall/claude/tools/introspection.py +127 -0
- lightfall/claude/tools/logs.py +155 -0
- lightfall/claude/tools/screenshot.py +87 -0
- lightfall/claude/widget.py +851 -0
- lightfall/claude/widgets/__init__.py +5 -0
- lightfall/claude/widgets/permission_request.py +282 -0
- lightfall/claude/widgets/question_request.py +227 -0
- lightfall/claude/widgets/task_card.py +223 -0
- lightfall/config/__init__.py +23 -0
- lightfall/config/layers.py +317 -0
- lightfall/config/manager.py +244 -0
- lightfall/config/schema.py +154 -0
- lightfall/core/__init__.py +23 -0
- lightfall/core/application.py +727 -0
- lightfall/core/services.py +235 -0
- lightfall/devices/__init__.py +53 -0
- lightfall/devices/backends/__init__.py +17 -0
- lightfall/devices/backends/bcs.py +448 -0
- lightfall/devices/backends/happi.py +986 -0
- lightfall/devices/backends/mock.py +646 -0
- lightfall/devices/base.py +338 -0
- lightfall/devices/catalog.py +1039 -0
- lightfall/devices/connection_manager.py +676 -0
- lightfall/devices/model.py +290 -0
- lightfall/devices/monitoring.py +442 -0
- lightfall/devices/sim/__init__.py +26 -0
- lightfall/devices/sim/areadetector.py +436 -0
- lightfall/devices/sim/generators.py +223 -0
- lightfall/devices/sim/plugins.py +116 -0
- lightfall/epics/__init__.py +28 -0
- lightfall/epics/ca/__init__.py +10 -0
- lightfall/epics/ca/context.py +100 -0
- lightfall/epics/ca/pv.py +303 -0
- lightfall/epics/widgets/__init__.py +49 -0
- lightfall/epics/widgets/areadetector/__init__.py +16 -0
- lightfall/epics/widgets/areadetector/areadetector.py +281 -0
- lightfall/epics/widgets/areadetector/controls.py +478 -0
- lightfall/epics/widgets/areadetector/image_view.py +447 -0
- lightfall/epics/widgets/auto.py +319 -0
- lightfall/epics/widgets/base.py +404 -0
- lightfall/epics/widgets/checkbox.py +239 -0
- lightfall/epics/widgets/combobox.py +202 -0
- lightfall/epics/widgets/label.py +176 -0
- lightfall/epics/widgets/lineedit.py +253 -0
- lightfall/epics/widgets/motor.py +694 -0
- lightfall/epics/widgets/ophyd_base.py +469 -0
- lightfall/epics/widgets/ophyd_combobox.py +86 -0
- lightfall/epics/widgets/ophyd_label.py +54 -0
- lightfall/epics/widgets/ophyd_lineedit.py +170 -0
- lightfall/epics/widgets/ophyd_spinbox.py +79 -0
- lightfall/epics/widgets/slider.py +272 -0
- lightfall/epics/widgets/status_indicator.py +98 -0
- lightfall/epics/widgets/style.py +188 -0
- lightfall/exporter/__init__.py +1 -0
- lightfall/exporter/cli.py +67 -0
- lightfall/exporter/converters/__init__.py +26 -0
- lightfall/exporter/converters/base.py +39 -0
- lightfall/exporter/converters/noop.py +39 -0
- lightfall/exporter/converters/nxsas.py +73 -0
- lightfall/exporter/service.py +200 -0
- lightfall/exporter/tiled_utils.py +92 -0
- lightfall/ipc/__init__.py +6 -0
- lightfall/ipc/service.py +701 -0
- lightfall/ipc/trust.py +144 -0
- lightfall/logbook/__init__.py +46 -0
- lightfall/logbook/action_dialog.py +207 -0
- lightfall/logbook/action_logger.py +432 -0
- lightfall/logbook/block_mapper.py +562 -0
- lightfall/logbook/client.py +799 -0
- lightfall/logbook/converter.py +273 -0
- lightfall/logbook/editors/__init__.py +19 -0
- lightfall/logbook/editors/highlighter.py +305 -0
- lightfall/logbook/editors/markdown_editor.py +319 -0
- lightfall/logbook/editors/richtext_editor.py +1397 -0
- lightfall/logbook/entry_widget.py +1065 -0
- lightfall/logbook/event_listener.py +221 -0
- lightfall/logbook/fragment_widgets.py +925 -0
- lightfall/logbook/position_mapper.py +320 -0
- lightfall/logbook/protection.py +402 -0
- lightfall/logbook/style.py +394 -0
- lightfall/logbook/url.py +29 -0
- lightfall/logbook/visual_protection.py +330 -0
- lightfall/logbook/widget.py +680 -0
- lightfall/main.py +1091 -0
- lightfall/pipelines/__init__.py +4 -0
- lightfall/pipelines/client.py +148 -0
- lightfall/plugins/__init__.py +119 -0
- lightfall/plugins/agent_plugin.py +106 -0
- lightfall/plugins/agents/__init__.py +10 -0
- lightfall/plugins/agents/_mcp_helpers.py +52 -0
- lightfall/plugins/agents/alignment.py +94 -0
- lightfall/plugins/agents/autonomous_experiment/__init__.py +12 -0
- lightfall/plugins/agents/autonomous_experiment/nats_tools.py +210 -0
- lightfall/plugins/agents/autonomous_experiment/plugin.py +60 -0
- lightfall/plugins/agents/autonomous_experiment/prompts.py +164 -0
- lightfall/plugins/agents/current_esaf.py +435 -0
- lightfall/plugins/agents/device_tools.py +1043 -0
- lightfall/plugins/agents/engine_tools.py +720 -0
- lightfall/plugins/agents/ipython_tools.py +444 -0
- lightfall/plugins/agents/panel_builder.py +637 -0
- lightfall/plugins/agents/panel_design/references/cross_panel_patterns.md +95 -0
- lightfall/plugins/agents/panel_design/references/panel_design.md +680 -0
- lightfall/plugins/agents/panel_design.py +94 -0
- lightfall/plugins/agents/plan_design/references/plan_design.md +398 -0
- lightfall/plugins/agents/plan_design.py +90 -0
- lightfall/plugins/agents/plan_tools.py +878 -0
- lightfall/plugins/agents/scan_planning.py +101 -0
- lightfall/plugins/builtin_manifest.py +348 -0
- lightfall/plugins/controller_plugin.py +162 -0
- lightfall/plugins/engine_plugin.py +124 -0
- lightfall/plugins/errors.py +104 -0
- lightfall/plugins/info.py +112 -0
- lightfall/plugins/loader.py +878 -0
- lightfall/plugins/manifest.py +150 -0
- lightfall/plugins/panel_plugin.py +119 -0
- lightfall/plugins/plan_plugin.py +163 -0
- lightfall/plugins/registry.py +309 -0
- lightfall/plugins/settings_plugin.py +226 -0
- lightfall/plugins/statusbar_plugin.py +289 -0
- lightfall/plugins/theme_plugin.py +165 -0
- lightfall/plugins/types.py +136 -0
- lightfall/plugins/user_plugins.py +764 -0
- lightfall/project/__init__.py +37 -0
- lightfall/project/model.py +375 -0
- lightfall/project/service.py +427 -0
- lightfall/project/welcome.py +106 -0
- lightfall/resources/__init__.py +126 -0
- lightfall/resources/images/__init__.py +1 -0
- lightfall/resources/images/icon.ico +0 -0
- lightfall/resources/images/icon.png +0 -0
- lightfall/resources/images/logo.png +0 -0
- lightfall/services/__init__.py +33 -0
- lightfall/services/_alshub_client.py +73 -0
- lightfall/services/access_stamper.py +240 -0
- lightfall/services/als_beam_status.py +347 -0
- lightfall/services/ca_tunnel.py +325 -0
- lightfall/services/threaded_tiled_writer.py +174 -0
- lightfall/services/tiled_auth.py +21 -0
- lightfall/services/tiled_service.py +971 -0
- lightfall/services/tiled_writer_patch.py +301 -0
- lightfall/settings/__init__.py +1 -0
- lightfall/settings/image_helpers.py +30 -0
- lightfall/settings/user_settings_client.py +232 -0
- lightfall/ui/__init__.py +28 -0
- lightfall/ui/annotations.py +208 -0
- lightfall/ui/common/__init__.py +7 -0
- lightfall/ui/dialogs/__init__.py +26 -0
- lightfall/ui/dialogs/about_dialog.py +131 -0
- lightfall/ui/dialogs/add_trigger_dialog.py +152 -0
- lightfall/ui/dialogs/base.py +44 -0
- lightfall/ui/dialogs/bug_report_dialog.py +353 -0
- lightfall/ui/dialogs/create_plan_dialog.py +189 -0
- lightfall/ui/dialogs/device_edit_dialog.py +183 -0
- lightfall/ui/dialogs/export_dialog.py +565 -0
- lightfall/ui/dialogs/go_to_position_dialog.py +192 -0
- lightfall/ui/dialogs/login_dialog.py +756 -0
- lightfall/ui/dialogs/oauth_browser_dialog.py +305 -0
- lightfall/ui/dialogs/run_pipeline_dialog.py +131 -0
- lightfall/ui/dialogs/sample_metadata_dialog.py +316 -0
- lightfall/ui/docking/__init__.py +22 -0
- lightfall/ui/docking/icon_sidebar.py +806 -0
- lightfall/ui/docking/manager.py +682 -0
- lightfall/ui/docking/state.py +113 -0
- lightfall/ui/docking/theme.py +399 -0
- lightfall/ui/docking/widget.py +214 -0
- lightfall/ui/events.py +75 -0
- lightfall/ui/mainwindow.py +1006 -0
- lightfall/ui/models/__init__.py +49 -0
- lightfall/ui/models/device_selection.py +389 -0
- lightfall/ui/models/device_tree.py +1095 -0
- lightfall/ui/models/thread_model.py +667 -0
- lightfall/ui/models/tiled_model.py +384 -0
- lightfall/ui/panels/__init__.py +28 -0
- lightfall/ui/panels/base.py +525 -0
- lightfall/ui/panels/bluesky_panel.py +619 -0
- lightfall/ui/panels/claude/__init__.py +9 -0
- lightfall/ui/panels/claude/agent_registry.py +168 -0
- lightfall/ui/panels/claude_panel.py +887 -0
- lightfall/ui/panels/device_panel.py +370 -0
- lightfall/ui/panels/documents_panel.py +166 -0
- lightfall/ui/panels/ipython_panel.py +580 -0
- lightfall/ui/panels/logbook_entries_panel.py +84 -0
- lightfall/ui/panels/logbook_panel.py +627 -0
- lightfall/ui/panels/logging_panel.py +587 -0
- lightfall/ui/panels/pipeline_jobs_panel.py +160 -0
- lightfall/ui/panels/pipeline_triggers_panel.py +170 -0
- lightfall/ui/panels/plugins/__init__.py +34 -0
- lightfall/ui/panels/plugins/bluesky_plugin.py +27 -0
- lightfall/ui/panels/plugins/claude_plugin.py +29 -0
- lightfall/ui/panels/plugins/device_plugin.py +27 -0
- lightfall/ui/panels/plugins/documents_plugin.py +26 -0
- lightfall/ui/panels/plugins/ipython_plugin.py +27 -0
- lightfall/ui/panels/plugins/logbook_entries_plugin.py +23 -0
- lightfall/ui/panels/plugins/logbook_plugin.py +27 -0
- lightfall/ui/panels/plugins/logging_plugin.py +27 -0
- lightfall/ui/panels/plugins/pipeline_jobs_plugin.py +23 -0
- lightfall/ui/panels/plugins/pipeline_triggers_plugin.py +25 -0
- lightfall/ui/panels/plugins/queue_plugin.py +29 -0
- lightfall/ui/panels/plugins/synoptic_plugin.py +38 -0
- lightfall/ui/panels/plugins/threads_plugin.py +27 -0
- lightfall/ui/panels/plugins/tiled_browser_plugin.py +27 -0
- lightfall/ui/panels/plugins/visualization_plugin.py +27 -0
- lightfall/ui/panels/queue.py +754 -0
- lightfall/ui/panels/registry.py +483 -0
- lightfall/ui/panels/synoptic/__init__.py +27 -0
- lightfall/ui/panels/synoptic/editors.py +481 -0
- lightfall/ui/panels/synoptic/items.py +556 -0
- lightfall/ui/panels/synoptic/models.py +336 -0
- lightfall/ui/panels/synoptic/panel.py +889 -0
- lightfall/ui/panels/synoptic/serialization.py +300 -0
- lightfall/ui/panels/synoptic/view.py +504 -0
- lightfall/ui/panels/threads_panel.py +621 -0
- lightfall/ui/panels/tiled_browser_panel.py +854 -0
- lightfall/ui/panels/visualization_panel.py +508 -0
- lightfall/ui/preferences/__init__.py +24 -0
- lightfall/ui/preferences/backend.py +148 -0
- lightfall/ui/preferences/builtin.py +220 -0
- lightfall/ui/preferences/claude_settings.py +762 -0
- lightfall/ui/preferences/device_settings.py +461 -0
- lightfall/ui/preferences/dialog.py +234 -0
- lightfall/ui/preferences/editor_settings.py +227 -0
- lightfall/ui/preferences/ipc_settings.py +226 -0
- lightfall/ui/preferences/logbook_settings.py +232 -0
- lightfall/ui/preferences/login_settings.py +183 -0
- lightfall/ui/preferences/manager.py +562 -0
- lightfall/ui/preferences/plugin_settings.py +392 -0
- lightfall/ui/preferences/proxy_settings.py +561 -0
- lightfall/ui/preferences/skill_settings.py +28 -0
- lightfall/ui/preferences/tiled_settings.py +469 -0
- lightfall/ui/preferences/tool_settings.py +393 -0
- lightfall/ui/preferences/user_portable_backend.py +131 -0
- lightfall/ui/preferences/user_profile_settings.py +316 -0
- lightfall/ui/preferences/visualization_settings.py +159 -0
- lightfall/ui/resources/__init__.py +1 -0
- lightfall/ui/resources/logo.png +0 -0
- lightfall/ui/statusbar/__init__.py +11 -0
- lightfall/ui/statusbar/manager.py +379 -0
- lightfall/ui/statusbar/plugins/__init__.py +28 -0
- lightfall/ui/statusbar/plugins/als_beam_status.py +218 -0
- lightfall/ui/statusbar/plugins/auth_status.py +116 -0
- lightfall/ui/statusbar/plugins/connection_status.py +100 -0
- lightfall/ui/statusbar/plugins/thread_status.py +556 -0
- lightfall/ui/statusbar/plugins/tiled_status.py +179 -0
- lightfall/ui/statusbar/plugins/user_status.py +84 -0
- lightfall/ui/theater/__init__.py +28 -0
- lightfall/ui/theater/manager.py +124 -0
- lightfall/ui/theater/overlay.py +279 -0
- lightfall/ui/theater/proxy.py +111 -0
- lightfall/ui/theme/__init__.py +18 -0
- lightfall/ui/theme/builtin.py +1015 -0
- lightfall/ui/theme/manager.py +875 -0
- lightfall/ui/theme/registry.py +195 -0
- lightfall/ui/toast.py +221 -0
- lightfall/ui/tutorial.py +1100 -0
- lightfall/ui/widgets/__init__.py +110 -0
- lightfall/ui/widgets/base_control.py +259 -0
- lightfall/ui/widgets/camera/__init__.py +36 -0
- lightfall/ui/widgets/camera/base.py +815 -0
- lightfall/ui/widgets/camera/dark_frames.py +213 -0
- lightfall/ui/widgets/camera/image_view.py +649 -0
- lightfall/ui/widgets/camera/panels/__init__.py +14 -0
- lightfall/ui/widgets/camera/panels/cooler.py +320 -0
- lightfall/ui/widgets/camera/panels/temperature.py +192 -0
- lightfall/ui/widgets/camera/plan_based.py +202 -0
- lightfall/ui/widgets/compact_motor.py +522 -0
- lightfall/ui/widgets/controller_matcher.py +278 -0
- lightfall/ui/widgets/controller_registry.py +144 -0
- lightfall/ui/widgets/device_control.py +411 -0
- lightfall/ui/widgets/device_selector.py +438 -0
- lightfall/ui/widgets/device_tree_tab.py +461 -0
- lightfall/ui/widgets/document_stream.py +1028 -0
- lightfall/ui/widgets/favorites_tab.py +191 -0
- lightfall/ui/widgets/motor_control.py +1120 -0
- lightfall/ui/widgets/observers/__init__.py +8 -0
- lightfall/ui/widgets/observers/camera.py +48 -0
- lightfall/ui/widgets/observers/image_view.py +130 -0
- lightfall/ui/widgets/plan_config.py +954 -0
- lightfall/ui/widgets/plan_edit_dialog.py +294 -0
- lightfall/ui/widgets/plan_selector.py +452 -0
- lightfall/ui/widgets/profile_avatar.py +128 -0
- lightfall/ui/widgets/queue_view.py +647 -0
- lightfall/ui/widgets/runengine_control.py +549 -0
- lightfall/ui/widgets/signal_control.py +793 -0
- lightfall/ui/widgets/skill_trigger_button.py +183 -0
- lightfall/ui/widgets/tiled_filter_widget.py +314 -0
- lightfall/ui/widgets/tiled_status.py +149 -0
- lightfall/ui/widgets/warning_banner.py +185 -0
- lightfall/utils/__init__.py +53 -0
- lightfall/utils/crash_diagnostics.py +454 -0
- lightfall/utils/data_migration.py +22 -0
- lightfall/utils/editor_launcher.py +594 -0
- lightfall/utils/error_collector.py +245 -0
- lightfall/utils/git_tracker.py +197 -0
- lightfall/utils/log_buffer.py +233 -0
- lightfall/utils/logging.py +229 -0
- lightfall/utils/module_resolver.py +70 -0
- lightfall/utils/sentry.py +722 -0
- lightfall/utils/threads.py +991 -0
- lightfall/utils/tiled_helpers.py +96 -0
- lightfall/visualization/__init__.py +50 -0
- lightfall/visualization/base.py +5 -0
- lightfall/visualization/base_visualization.py +74 -0
- lightfall/visualization/fitting/__init__.py +24 -0
- lightfall/visualization/fitting/base.py +267 -0
- lightfall/visualization/fitting/fitters.py +325 -0
- lightfall/visualization/fitting/panel.py +265 -0
- lightfall/visualization/memory.py +392 -0
- lightfall/visualization/motor_mixin.py +524 -0
- lightfall/visualization/pg.py +156 -0
- lightfall/visualization/registry.py +144 -0
- lightfall/visualization/spec.py +79 -0
- lightfall/visualization/theme.py +266 -0
- lightfall/visualization/widgets/__init__.py +28 -0
- lightfall/visualization/widgets/adaptive/__init__.py +1 -0
- lightfall/visualization/widgets/adaptive/heatmap.py +655 -0
- lightfall/visualization/widgets/adaptive/plot.py +233 -0
- lightfall/visualization/widgets/heatmap.py +264 -0
- lightfall/visualization/widgets/image_stack.py +316 -0
- lightfall/visualization/widgets/image_view_toolbar.py +227 -0
- lightfall/visualization/widgets/lazy_image_view.py +590 -0
- lightfall/visualization/widgets/plot_1d.py +262 -0
- lightfall/visualization/widgets/scatter.py +247 -0
- lightfall/visualization/widgets/table.py +221 -0
- lightfall/visualization/widgets/time_axis.py +93 -0
- lightfall-0.1.1.dist-info/METADATA +223 -0
- lightfall-0.1.1.dist-info/RECORD +388 -0
- lightfall-0.1.1.dist-info/WHEEL +4 -0
- lightfall-0.1.1.dist-info/entry_points.txt +6 -0
- lightfall-0.1.1.dist-info/licenses/LEGAL.md +13 -0
- lightfall-0.1.1.dist-info/licenses/LICENSE.md +33 -0
|
@@ -0,0 +1,1043 @@
|
|
|
1
|
+
"""MCP tools for device interaction via the DeviceCatalog.
|
|
2
|
+
|
|
3
|
+
Provides tools for Claude to interact with NCS devices:
|
|
4
|
+
- List and search devices
|
|
5
|
+
- Read device values and state
|
|
6
|
+
- Control devices (with permission checks)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from lightfall.plugins.agent_plugin import AgentPlugin
|
|
14
|
+
from lightfall.plugins.agents._mcp_helpers import mcp_result
|
|
15
|
+
from lightfall.utils.logging import logger
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class DeviceToolsAgent(AgentPlugin):
|
|
19
|
+
"""MCP tools for device interaction via the DeviceCatalog.
|
|
20
|
+
|
|
21
|
+
This plugin provides tools for Claude to:
|
|
22
|
+
- List and search devices in the catalog
|
|
23
|
+
- Read device values and positions
|
|
24
|
+
- Get device state and status information
|
|
25
|
+
- Control devices (requires DEVICE_CONTROL permission)
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def name(self) -> str:
|
|
30
|
+
"""Plugin name."""
|
|
31
|
+
return "device_tools"
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def description(self) -> str:
|
|
35
|
+
"""Human-readable description of what this plugin provides."""
|
|
36
|
+
return "Tools for interacting with NCS devices"
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def category(self) -> str:
|
|
40
|
+
"""Category for grouping in settings UI."""
|
|
41
|
+
return "devices"
|
|
42
|
+
|
|
43
|
+
def _get_catalog(self):
|
|
44
|
+
"""Get the device catalog instance."""
|
|
45
|
+
from lightfall.devices import DeviceCatalog
|
|
46
|
+
|
|
47
|
+
return DeviceCatalog.get_instance()
|
|
48
|
+
|
|
49
|
+
def _get_session_manager(self):
|
|
50
|
+
"""Get the session manager instance."""
|
|
51
|
+
from lightfall.auth.session import SessionManager
|
|
52
|
+
|
|
53
|
+
return SessionManager.get_instance()
|
|
54
|
+
|
|
55
|
+
def _check_device_control_permission(self) -> tuple[bool, str | None]:
|
|
56
|
+
"""Check if user has DEVICE_CONTROL permission.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
Tuple of (has_permission, error_message).
|
|
60
|
+
"""
|
|
61
|
+
from lightfall.auth.policy import Permission
|
|
62
|
+
|
|
63
|
+
session = self._get_session_manager()
|
|
64
|
+
if not session.check_permission(Permission.DEVICE_CONTROL):
|
|
65
|
+
return False, "Permission denied: DEVICE_CONTROL required"
|
|
66
|
+
return True, None
|
|
67
|
+
|
|
68
|
+
def create_tools(self) -> list[Any]:
|
|
69
|
+
"""Create device MCP tools.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
List of tool functions.
|
|
73
|
+
"""
|
|
74
|
+
try:
|
|
75
|
+
from claude_agent_sdk import tool
|
|
76
|
+
except ImportError:
|
|
77
|
+
logger.warning("claude_agent_sdk not available, device tools disabled")
|
|
78
|
+
return []
|
|
79
|
+
|
|
80
|
+
@tool(
|
|
81
|
+
name="lightfall_list_devices",
|
|
82
|
+
description="List devices in the NCS catalog with optional filtering by category, beamline, or search query",
|
|
83
|
+
input_schema={
|
|
84
|
+
"type": "object",
|
|
85
|
+
"properties": {
|
|
86
|
+
"category": {
|
|
87
|
+
"type": "string",
|
|
88
|
+
"description": (
|
|
89
|
+
"Filter by device category. Must be one of the "
|
|
90
|
+
"values in the DeviceCategory enum."
|
|
91
|
+
),
|
|
92
|
+
"enum": [
|
|
93
|
+
"motor",
|
|
94
|
+
"detector",
|
|
95
|
+
"controller",
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
"beamline": {
|
|
99
|
+
"type": "string",
|
|
100
|
+
"description": "Filter by beamline name",
|
|
101
|
+
},
|
|
102
|
+
"query": {
|
|
103
|
+
"type": "string",
|
|
104
|
+
"description": "Search string for name/description/tags",
|
|
105
|
+
},
|
|
106
|
+
"active_only": {
|
|
107
|
+
"type": "boolean",
|
|
108
|
+
"description": "Only return active devices (default: true)",
|
|
109
|
+
"default": True,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
)
|
|
114
|
+
async def list_devices(args: dict) -> dict[str, Any]:
|
|
115
|
+
"""List devices with optional filtering."""
|
|
116
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
117
|
+
from lightfall.devices.model import DeviceCategory
|
|
118
|
+
|
|
119
|
+
category_str = args.get("category")
|
|
120
|
+
beamline = args.get("beamline")
|
|
121
|
+
query = args.get("query")
|
|
122
|
+
active_only = args.get("active_only", True)
|
|
123
|
+
|
|
124
|
+
def _list():
|
|
125
|
+
catalog = self._get_catalog()
|
|
126
|
+
|
|
127
|
+
if not catalog.is_connected:
|
|
128
|
+
# Provide helpful diagnostics about why catalog isn't connected
|
|
129
|
+
backend = catalog.backend
|
|
130
|
+
backend_info = backend.name if backend else "no backend configured"
|
|
131
|
+
return mcp_result({
|
|
132
|
+
"success": False,
|
|
133
|
+
"error": "Device catalog not connected",
|
|
134
|
+
"hint": (
|
|
135
|
+
"The device catalog is not connected to a backend. "
|
|
136
|
+
f"Current backend: {backend_info}. "
|
|
137
|
+
"Check that the application initialized correctly and "
|
|
138
|
+
"the backend (mock or BCS) has been connected. "
|
|
139
|
+
"Use lightfall_get_catalog_info for more details."
|
|
140
|
+
),
|
|
141
|
+
"devices": [],
|
|
142
|
+
"count": 0,
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
# If query is provided, use search
|
|
146
|
+
if query:
|
|
147
|
+
devices = catalog.search_devices(query)
|
|
148
|
+
else:
|
|
149
|
+
# Convert category string to enum
|
|
150
|
+
category = None
|
|
151
|
+
if category_str:
|
|
152
|
+
try:
|
|
153
|
+
category = DeviceCategory(category_str)
|
|
154
|
+
except ValueError:
|
|
155
|
+
return mcp_result({
|
|
156
|
+
"success": False,
|
|
157
|
+
"error": f"Invalid category: {category_str}",
|
|
158
|
+
"devices": [],
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
devices = catalog.list_devices(
|
|
162
|
+
category=category,
|
|
163
|
+
beamline=beamline,
|
|
164
|
+
active_only=active_only,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Build summary list
|
|
168
|
+
device_list = []
|
|
169
|
+
for device in devices:
|
|
170
|
+
state = device.state
|
|
171
|
+
device_list.append(
|
|
172
|
+
{
|
|
173
|
+
"id": str(device.id),
|
|
174
|
+
"name": device.name,
|
|
175
|
+
"description": device.description,
|
|
176
|
+
"category": device.category.value,
|
|
177
|
+
"prefix": device.prefix,
|
|
178
|
+
"beamline": device.beamline,
|
|
179
|
+
"status": state.status.value if state else "unknown",
|
|
180
|
+
"connected": state.connected if state else False,
|
|
181
|
+
"active": device.active,
|
|
182
|
+
}
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
return mcp_result({
|
|
186
|
+
"success": True,
|
|
187
|
+
"count": len(device_list),
|
|
188
|
+
"devices": device_list,
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
return run_on_main_thread(_list)
|
|
192
|
+
|
|
193
|
+
@tool(
|
|
194
|
+
name="lightfall_get_device",
|
|
195
|
+
description="Get detailed information about a device by its name",
|
|
196
|
+
input_schema={
|
|
197
|
+
"type": "object",
|
|
198
|
+
"properties": {
|
|
199
|
+
"name": {
|
|
200
|
+
"type": "string",
|
|
201
|
+
"description": "The device name",
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
"required": ["name"],
|
|
205
|
+
},
|
|
206
|
+
)
|
|
207
|
+
async def get_device(args: dict) -> dict[str, Any]:
|
|
208
|
+
"""Get detailed device information."""
|
|
209
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
210
|
+
|
|
211
|
+
device_name = args["name"]
|
|
212
|
+
|
|
213
|
+
def _get():
|
|
214
|
+
catalog = self._get_catalog()
|
|
215
|
+
|
|
216
|
+
if not catalog.is_connected:
|
|
217
|
+
return mcp_result({
|
|
218
|
+
"success": False,
|
|
219
|
+
"error": "Device catalog not connected",
|
|
220
|
+
"device": device_name,
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
device = catalog.get_device_by_name(device_name)
|
|
224
|
+
if device is None:
|
|
225
|
+
return mcp_result({
|
|
226
|
+
"success": False,
|
|
227
|
+
"error": f"Device '{device_name}' not found",
|
|
228
|
+
"device": device_name,
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
state = device.state
|
|
232
|
+
ophyd_dev = device.ophyd_device
|
|
233
|
+
|
|
234
|
+
# Determine capabilities
|
|
235
|
+
capabilities = []
|
|
236
|
+
if ophyd_dev is not None:
|
|
237
|
+
if hasattr(ophyd_dev, "read"):
|
|
238
|
+
capabilities.append("read")
|
|
239
|
+
if hasattr(ophyd_dev, "set"):
|
|
240
|
+
capabilities.append("set")
|
|
241
|
+
if hasattr(ophyd_dev, "move"):
|
|
242
|
+
capabilities.append("move")
|
|
243
|
+
if hasattr(ophyd_dev, "stop"):
|
|
244
|
+
capabilities.append("stop")
|
|
245
|
+
if hasattr(ophyd_dev, "position"):
|
|
246
|
+
capabilities.append("position")
|
|
247
|
+
|
|
248
|
+
return mcp_result({
|
|
249
|
+
"success": True,
|
|
250
|
+
"device": {
|
|
251
|
+
"id": str(device.id),
|
|
252
|
+
"name": device.name,
|
|
253
|
+
"description": device.description,
|
|
254
|
+
"category": device.category.value,
|
|
255
|
+
"device_class": device.device_class,
|
|
256
|
+
"connection_type": device.connection_type.value,
|
|
257
|
+
"prefix": device.prefix,
|
|
258
|
+
"beamline": device.beamline,
|
|
259
|
+
"location": device.location,
|
|
260
|
+
"tags": device.tags,
|
|
261
|
+
"active": device.active,
|
|
262
|
+
"capabilities": capabilities,
|
|
263
|
+
"state": {
|
|
264
|
+
"status": state.status.value if state else "unknown",
|
|
265
|
+
"connected": state.connected if state else False,
|
|
266
|
+
"position": state.position if state else None,
|
|
267
|
+
"value": state.value if state else None,
|
|
268
|
+
"alarm_status": state.alarm_status if state else None,
|
|
269
|
+
"alarm_severity": state.alarm_severity if state else None,
|
|
270
|
+
},
|
|
271
|
+
"metadata": device.metadata,
|
|
272
|
+
},
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
return run_on_main_thread(_get)
|
|
276
|
+
|
|
277
|
+
@tool(
|
|
278
|
+
name="lightfall_read_device",
|
|
279
|
+
description="Read the current value or position from a device",
|
|
280
|
+
input_schema={
|
|
281
|
+
"type": "object",
|
|
282
|
+
"properties": {
|
|
283
|
+
"name": {
|
|
284
|
+
"type": "string",
|
|
285
|
+
"description": "The device name",
|
|
286
|
+
},
|
|
287
|
+
"refresh": {
|
|
288
|
+
"type": "boolean",
|
|
289
|
+
"description": "Force refresh from hardware (default: false)",
|
|
290
|
+
"default": False,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
"required": ["name"],
|
|
294
|
+
},
|
|
295
|
+
)
|
|
296
|
+
async def read_device(args: dict) -> dict[str, Any]:
|
|
297
|
+
"""Read current value/position from a device."""
|
|
298
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
299
|
+
|
|
300
|
+
device_name = args["name"]
|
|
301
|
+
refresh = args.get("refresh", False)
|
|
302
|
+
|
|
303
|
+
def _read():
|
|
304
|
+
catalog = self._get_catalog()
|
|
305
|
+
|
|
306
|
+
if not catalog.is_connected:
|
|
307
|
+
return mcp_result({
|
|
308
|
+
"success": False,
|
|
309
|
+
"error": "Device catalog not connected",
|
|
310
|
+
"device": device_name,
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
device = catalog.get_device_by_name(device_name)
|
|
314
|
+
if device is None:
|
|
315
|
+
return mcp_result({
|
|
316
|
+
"success": False,
|
|
317
|
+
"error": f"Device '{device_name}' not found",
|
|
318
|
+
"device": device_name,
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
ophyd_dev = device.ophyd_device
|
|
322
|
+
if ophyd_dev is None:
|
|
323
|
+
return mcp_result({
|
|
324
|
+
"success": False,
|
|
325
|
+
"error": f"Device '{device_name}' has no hardware connection",
|
|
326
|
+
"device": device_name,
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
# Refresh state if requested
|
|
330
|
+
if refresh:
|
|
331
|
+
catalog.refresh_device_state(device.id)
|
|
332
|
+
|
|
333
|
+
result = {
|
|
334
|
+
"success": True,
|
|
335
|
+
"device": device_name,
|
|
336
|
+
"readings": {},
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
# Get position for positioners
|
|
340
|
+
if hasattr(ophyd_dev, "position"):
|
|
341
|
+
try:
|
|
342
|
+
result["position"] = ophyd_dev.position
|
|
343
|
+
except Exception as e:
|
|
344
|
+
logger.warning("Failed to read position for {}: {}", device_name, e)
|
|
345
|
+
|
|
346
|
+
# Get value for signals
|
|
347
|
+
if hasattr(ophyd_dev, "get"):
|
|
348
|
+
try:
|
|
349
|
+
result["value"] = ophyd_dev.get()
|
|
350
|
+
except Exception as e:
|
|
351
|
+
logger.warning("Failed to get value for {}: {}", device_name, e)
|
|
352
|
+
|
|
353
|
+
# Get full read data
|
|
354
|
+
if hasattr(ophyd_dev, "read"):
|
|
355
|
+
try:
|
|
356
|
+
read_data = ophyd_dev.read()
|
|
357
|
+
# Convert to serializable format
|
|
358
|
+
for key, val in read_data.items():
|
|
359
|
+
if isinstance(val, dict):
|
|
360
|
+
result["readings"][key] = {
|
|
361
|
+
"value": val.get("value"),
|
|
362
|
+
"timestamp": val.get("timestamp"),
|
|
363
|
+
}
|
|
364
|
+
else:
|
|
365
|
+
result["readings"][key] = val
|
|
366
|
+
except Exception as e:
|
|
367
|
+
logger.warning("Failed to read device {}: {}", device_name, e)
|
|
368
|
+
|
|
369
|
+
# Include unit if available
|
|
370
|
+
if hasattr(ophyd_dev, "egu"):
|
|
371
|
+
try:
|
|
372
|
+
result["unit"] = ophyd_dev.egu
|
|
373
|
+
except Exception:
|
|
374
|
+
pass
|
|
375
|
+
|
|
376
|
+
# Include state info
|
|
377
|
+
state = device.state
|
|
378
|
+
if state:
|
|
379
|
+
result["status"] = state.status.value
|
|
380
|
+
result["connected"] = state.connected
|
|
381
|
+
|
|
382
|
+
return mcp_result(result)
|
|
383
|
+
|
|
384
|
+
return run_on_main_thread(_read)
|
|
385
|
+
|
|
386
|
+
@tool(
|
|
387
|
+
name="lightfall_get_device_state",
|
|
388
|
+
description="Get the current state of a device including status, alarms, and connection info",
|
|
389
|
+
input_schema={
|
|
390
|
+
"type": "object",
|
|
391
|
+
"properties": {
|
|
392
|
+
"name": {
|
|
393
|
+
"type": "string",
|
|
394
|
+
"description": "The device name",
|
|
395
|
+
},
|
|
396
|
+
"refresh": {
|
|
397
|
+
"type": "boolean",
|
|
398
|
+
"description": "Force refresh from hardware (default: true)",
|
|
399
|
+
"default": True,
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
"required": ["name"],
|
|
403
|
+
},
|
|
404
|
+
)
|
|
405
|
+
async def get_device_state(args: dict) -> dict[str, Any]:
|
|
406
|
+
"""Get device state with status and alarms."""
|
|
407
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
408
|
+
|
|
409
|
+
device_name = args["name"]
|
|
410
|
+
refresh = args.get("refresh", True)
|
|
411
|
+
|
|
412
|
+
def _get_state():
|
|
413
|
+
catalog = self._get_catalog()
|
|
414
|
+
|
|
415
|
+
if not catalog.is_connected:
|
|
416
|
+
return mcp_result({
|
|
417
|
+
"success": False,
|
|
418
|
+
"error": "Device catalog not connected",
|
|
419
|
+
"device": device_name,
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
device = catalog.get_device_by_name(device_name)
|
|
423
|
+
if device is None:
|
|
424
|
+
return mcp_result({
|
|
425
|
+
"success": False,
|
|
426
|
+
"error": f"Device '{device_name}' not found",
|
|
427
|
+
"device": device_name,
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
# Refresh state if requested
|
|
431
|
+
if refresh and device.ophyd_device is not None:
|
|
432
|
+
catalog.refresh_device_state(device.id)
|
|
433
|
+
|
|
434
|
+
state = device.state
|
|
435
|
+
if state is None:
|
|
436
|
+
return mcp_result({
|
|
437
|
+
"success": True,
|
|
438
|
+
"device": device_name,
|
|
439
|
+
"state": {
|
|
440
|
+
"status": "unknown",
|
|
441
|
+
"connected": False,
|
|
442
|
+
"position": None,
|
|
443
|
+
"value": None,
|
|
444
|
+
"alarm_status": None,
|
|
445
|
+
"alarm_severity": None,
|
|
446
|
+
"timestamp": None,
|
|
447
|
+
},
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
return mcp_result({
|
|
451
|
+
"success": True,
|
|
452
|
+
"device": device_name,
|
|
453
|
+
"state": {
|
|
454
|
+
"status": state.status.value,
|
|
455
|
+
"connected": state.connected,
|
|
456
|
+
"position": state.position,
|
|
457
|
+
"value": state.value,
|
|
458
|
+
"alarm_status": state.alarm_status,
|
|
459
|
+
"alarm_severity": state.alarm_severity,
|
|
460
|
+
"timestamp": state.timestamp.isoformat() if state.timestamp else None,
|
|
461
|
+
"additional": state.additional,
|
|
462
|
+
},
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
return run_on_main_thread(_get_state)
|
|
466
|
+
|
|
467
|
+
@tool(
|
|
468
|
+
name="lightfall_set_device",
|
|
469
|
+
description="Set a value on a device. Requires DEVICE_CONTROL permission.",
|
|
470
|
+
input_schema={
|
|
471
|
+
"type": "object",
|
|
472
|
+
"properties": {
|
|
473
|
+
"name": {
|
|
474
|
+
"type": "string",
|
|
475
|
+
"description": "The device name",
|
|
476
|
+
},
|
|
477
|
+
"value": {
|
|
478
|
+
"description": "The value to set (number, string, or boolean)",
|
|
479
|
+
},
|
|
480
|
+
"wait": {
|
|
481
|
+
"type": "boolean",
|
|
482
|
+
"description": "Wait for the operation to complete (default: true)",
|
|
483
|
+
"default": True,
|
|
484
|
+
},
|
|
485
|
+
},
|
|
486
|
+
"required": ["name", "value"],
|
|
487
|
+
},
|
|
488
|
+
)
|
|
489
|
+
async def set_device(args: dict) -> dict[str, Any]:
|
|
490
|
+
"""Set a value on a device."""
|
|
491
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
492
|
+
|
|
493
|
+
device_name = args["name"]
|
|
494
|
+
value = args["value"]
|
|
495
|
+
wait = args.get("wait", True)
|
|
496
|
+
|
|
497
|
+
def _set():
|
|
498
|
+
# Check permission
|
|
499
|
+
has_perm, error = self._check_device_control_permission()
|
|
500
|
+
if not has_perm:
|
|
501
|
+
return mcp_result({
|
|
502
|
+
"success": False,
|
|
503
|
+
"error": error,
|
|
504
|
+
"device": device_name,
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
catalog = self._get_catalog()
|
|
508
|
+
|
|
509
|
+
if not catalog.is_connected:
|
|
510
|
+
return mcp_result({
|
|
511
|
+
"success": False,
|
|
512
|
+
"error": "Device catalog not connected",
|
|
513
|
+
"device": device_name,
|
|
514
|
+
})
|
|
515
|
+
|
|
516
|
+
device = catalog.get_device_by_name(device_name)
|
|
517
|
+
if device is None:
|
|
518
|
+
return mcp_result({
|
|
519
|
+
"success": False,
|
|
520
|
+
"error": f"Device '{device_name}' not found",
|
|
521
|
+
"device": device_name,
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
ophyd_dev = device.ophyd_device
|
|
525
|
+
if ophyd_dev is None:
|
|
526
|
+
return mcp_result({
|
|
527
|
+
"success": False,
|
|
528
|
+
"error": f"Device '{device_name}' has no hardware connection",
|
|
529
|
+
"device": device_name,
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
if not hasattr(ophyd_dev, "set"):
|
|
533
|
+
return mcp_result({
|
|
534
|
+
"success": False,
|
|
535
|
+
"error": f"Device '{device_name}' does not support set operation",
|
|
536
|
+
"device": device_name,
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
# Get old value if possible
|
|
540
|
+
old_value = None
|
|
541
|
+
if hasattr(ophyd_dev, "get"):
|
|
542
|
+
try:
|
|
543
|
+
old_value = ophyd_dev.get()
|
|
544
|
+
except Exception:
|
|
545
|
+
pass
|
|
546
|
+
|
|
547
|
+
try:
|
|
548
|
+
status = ophyd_dev.set(value)
|
|
549
|
+
if wait and hasattr(status, "wait"):
|
|
550
|
+
status.wait()
|
|
551
|
+
|
|
552
|
+
# Get new value
|
|
553
|
+
new_value = None
|
|
554
|
+
if hasattr(ophyd_dev, "get"):
|
|
555
|
+
try:
|
|
556
|
+
new_value = ophyd_dev.get()
|
|
557
|
+
except Exception:
|
|
558
|
+
pass
|
|
559
|
+
|
|
560
|
+
logger.info("Device {} set to {} (was {})", device_name, value, old_value)
|
|
561
|
+
|
|
562
|
+
return mcp_result({
|
|
563
|
+
"success": True,
|
|
564
|
+
"device": device_name,
|
|
565
|
+
"old_value": old_value,
|
|
566
|
+
"new_value": new_value,
|
|
567
|
+
"requested_value": value,
|
|
568
|
+
})
|
|
569
|
+
|
|
570
|
+
except Exception as e:
|
|
571
|
+
logger.error("Failed to set device {}: {}", device_name, e)
|
|
572
|
+
return mcp_result({
|
|
573
|
+
"success": False,
|
|
574
|
+
"error": f"Set operation failed: {e}",
|
|
575
|
+
"device": device_name,
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
return run_on_main_thread(_set)
|
|
579
|
+
|
|
580
|
+
@tool(
|
|
581
|
+
name="lightfall_move_motor",
|
|
582
|
+
description="Move a motor to a specific position. Requires DEVICE_CONTROL permission.",
|
|
583
|
+
input_schema={
|
|
584
|
+
"type": "object",
|
|
585
|
+
"properties": {
|
|
586
|
+
"name": {
|
|
587
|
+
"type": "string",
|
|
588
|
+
"description": "The motor device name",
|
|
589
|
+
},
|
|
590
|
+
"position": {
|
|
591
|
+
"type": "number",
|
|
592
|
+
"description": "The target position",
|
|
593
|
+
},
|
|
594
|
+
"wait": {
|
|
595
|
+
"type": "boolean",
|
|
596
|
+
"description": "Wait for the move to complete (default: true)",
|
|
597
|
+
"default": True,
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
"required": ["name", "position"],
|
|
601
|
+
},
|
|
602
|
+
)
|
|
603
|
+
async def move_motor(args: dict) -> dict[str, Any]:
|
|
604
|
+
"""Move a motor to a position."""
|
|
605
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
606
|
+
|
|
607
|
+
device_name = args["name"]
|
|
608
|
+
position = args["position"]
|
|
609
|
+
wait = args.get("wait", True)
|
|
610
|
+
|
|
611
|
+
def _move():
|
|
612
|
+
# Check permission
|
|
613
|
+
has_perm, error = self._check_device_control_permission()
|
|
614
|
+
if not has_perm:
|
|
615
|
+
return mcp_result({
|
|
616
|
+
"success": False,
|
|
617
|
+
"error": error,
|
|
618
|
+
"device": device_name,
|
|
619
|
+
})
|
|
620
|
+
|
|
621
|
+
catalog = self._get_catalog()
|
|
622
|
+
|
|
623
|
+
if not catalog.is_connected:
|
|
624
|
+
return mcp_result({
|
|
625
|
+
"success": False,
|
|
626
|
+
"error": "Device catalog not connected",
|
|
627
|
+
"device": device_name,
|
|
628
|
+
})
|
|
629
|
+
|
|
630
|
+
device = catalog.get_device_by_name(device_name)
|
|
631
|
+
if device is None:
|
|
632
|
+
return mcp_result({
|
|
633
|
+
"success": False,
|
|
634
|
+
"error": f"Device '{device_name}' not found",
|
|
635
|
+
"device": device_name,
|
|
636
|
+
})
|
|
637
|
+
|
|
638
|
+
ophyd_dev = device.ophyd_device
|
|
639
|
+
if ophyd_dev is None:
|
|
640
|
+
return mcp_result({
|
|
641
|
+
"success": False,
|
|
642
|
+
"error": f"Device '{device_name}' has no hardware connection",
|
|
643
|
+
"device": device_name,
|
|
644
|
+
})
|
|
645
|
+
|
|
646
|
+
# Check if device supports move
|
|
647
|
+
if not hasattr(ophyd_dev, "move"):
|
|
648
|
+
# Fall back to set if available
|
|
649
|
+
if hasattr(ophyd_dev, "set"):
|
|
650
|
+
try:
|
|
651
|
+
old_position = None
|
|
652
|
+
if hasattr(ophyd_dev, "position"):
|
|
653
|
+
old_position = ophyd_dev.position
|
|
654
|
+
|
|
655
|
+
status = ophyd_dev.set(position)
|
|
656
|
+
if wait and hasattr(status, "wait"):
|
|
657
|
+
status.wait()
|
|
658
|
+
|
|
659
|
+
new_position = None
|
|
660
|
+
if hasattr(ophyd_dev, "position"):
|
|
661
|
+
new_position = ophyd_dev.position
|
|
662
|
+
|
|
663
|
+
logger.info(
|
|
664
|
+
"Motor {} moved to {} (was {})",
|
|
665
|
+
device_name,
|
|
666
|
+
position,
|
|
667
|
+
old_position,
|
|
668
|
+
)
|
|
669
|
+
|
|
670
|
+
result = {
|
|
671
|
+
"success": True,
|
|
672
|
+
"device": device_name,
|
|
673
|
+
"old_position": old_position,
|
|
674
|
+
"new_position": new_position,
|
|
675
|
+
"requested_position": position,
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
# Include unit if available
|
|
679
|
+
if hasattr(ophyd_dev, "egu"):
|
|
680
|
+
try:
|
|
681
|
+
result["unit"] = ophyd_dev.egu
|
|
682
|
+
except Exception:
|
|
683
|
+
pass
|
|
684
|
+
|
|
685
|
+
return mcp_result(result)
|
|
686
|
+
|
|
687
|
+
except Exception as e:
|
|
688
|
+
logger.error("Failed to move motor {}: {}", device_name, e)
|
|
689
|
+
return mcp_result({
|
|
690
|
+
"success": False,
|
|
691
|
+
"error": f"Move operation failed: {e}",
|
|
692
|
+
"device": device_name,
|
|
693
|
+
})
|
|
694
|
+
else:
|
|
695
|
+
return mcp_result({
|
|
696
|
+
"success": False,
|
|
697
|
+
"error": f"Device '{device_name}' does not support move operation",
|
|
698
|
+
"device": device_name,
|
|
699
|
+
})
|
|
700
|
+
|
|
701
|
+
# Get old position
|
|
702
|
+
old_position = None
|
|
703
|
+
if hasattr(ophyd_dev, "position"):
|
|
704
|
+
try:
|
|
705
|
+
old_position = ophyd_dev.position
|
|
706
|
+
except Exception:
|
|
707
|
+
pass
|
|
708
|
+
|
|
709
|
+
try:
|
|
710
|
+
status = ophyd_dev.move(position, wait=wait)
|
|
711
|
+
if wait and hasattr(status, "wait"):
|
|
712
|
+
status.wait()
|
|
713
|
+
|
|
714
|
+
# Get new position
|
|
715
|
+
new_position = None
|
|
716
|
+
if hasattr(ophyd_dev, "position"):
|
|
717
|
+
try:
|
|
718
|
+
new_position = ophyd_dev.position
|
|
719
|
+
except Exception:
|
|
720
|
+
pass
|
|
721
|
+
|
|
722
|
+
logger.info(
|
|
723
|
+
"Motor {} moved to {} (was {})",
|
|
724
|
+
device_name,
|
|
725
|
+
position,
|
|
726
|
+
old_position,
|
|
727
|
+
)
|
|
728
|
+
|
|
729
|
+
result = {
|
|
730
|
+
"success": True,
|
|
731
|
+
"device": device_name,
|
|
732
|
+
"old_position": old_position,
|
|
733
|
+
"new_position": new_position,
|
|
734
|
+
"requested_position": position,
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
# Include unit if available
|
|
738
|
+
if hasattr(ophyd_dev, "egu"):
|
|
739
|
+
try:
|
|
740
|
+
result["unit"] = ophyd_dev.egu
|
|
741
|
+
except Exception:
|
|
742
|
+
pass
|
|
743
|
+
|
|
744
|
+
return mcp_result(result)
|
|
745
|
+
|
|
746
|
+
except Exception as e:
|
|
747
|
+
logger.error("Failed to move motor {}: {}", device_name, e)
|
|
748
|
+
return mcp_result({
|
|
749
|
+
"success": False,
|
|
750
|
+
"error": f"Move operation failed: {e}",
|
|
751
|
+
"device": device_name,
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
return run_on_main_thread(_move)
|
|
755
|
+
|
|
756
|
+
@tool(
|
|
757
|
+
name="lightfall_stop_device",
|
|
758
|
+
description="Stop a device (emergency stop). Requires DEVICE_CONTROL permission.",
|
|
759
|
+
input_schema={
|
|
760
|
+
"type": "object",
|
|
761
|
+
"properties": {
|
|
762
|
+
"name": {
|
|
763
|
+
"type": "string",
|
|
764
|
+
"description": "The device name to stop",
|
|
765
|
+
},
|
|
766
|
+
},
|
|
767
|
+
"required": ["name"],
|
|
768
|
+
},
|
|
769
|
+
)
|
|
770
|
+
async def stop_device(args: dict) -> dict[str, Any]:
|
|
771
|
+
"""Stop a device."""
|
|
772
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
773
|
+
|
|
774
|
+
device_name = args["name"]
|
|
775
|
+
|
|
776
|
+
def _stop():
|
|
777
|
+
# Check permission
|
|
778
|
+
has_perm, error = self._check_device_control_permission()
|
|
779
|
+
if not has_perm:
|
|
780
|
+
return mcp_result({
|
|
781
|
+
"success": False,
|
|
782
|
+
"error": error,
|
|
783
|
+
"device": device_name,
|
|
784
|
+
})
|
|
785
|
+
|
|
786
|
+
catalog = self._get_catalog()
|
|
787
|
+
|
|
788
|
+
if not catalog.is_connected:
|
|
789
|
+
return mcp_result({
|
|
790
|
+
"success": False,
|
|
791
|
+
"error": "Device catalog not connected",
|
|
792
|
+
"device": device_name,
|
|
793
|
+
})
|
|
794
|
+
|
|
795
|
+
device = catalog.get_device_by_name(device_name)
|
|
796
|
+
if device is None:
|
|
797
|
+
return mcp_result({
|
|
798
|
+
"success": False,
|
|
799
|
+
"error": f"Device '{device_name}' not found",
|
|
800
|
+
"device": device_name,
|
|
801
|
+
})
|
|
802
|
+
|
|
803
|
+
ophyd_dev = device.ophyd_device
|
|
804
|
+
if ophyd_dev is None:
|
|
805
|
+
return mcp_result({
|
|
806
|
+
"success": False,
|
|
807
|
+
"error": f"Device '{device_name}' has no hardware connection",
|
|
808
|
+
"device": device_name,
|
|
809
|
+
})
|
|
810
|
+
|
|
811
|
+
if not hasattr(ophyd_dev, "stop"):
|
|
812
|
+
return mcp_result({
|
|
813
|
+
"success": False,
|
|
814
|
+
"error": f"Device '{device_name}' does not support stop operation",
|
|
815
|
+
"device": device_name,
|
|
816
|
+
})
|
|
817
|
+
|
|
818
|
+
try:
|
|
819
|
+
ophyd_dev.stop()
|
|
820
|
+
logger.info("Device {} stopped", device_name)
|
|
821
|
+
return mcp_result({
|
|
822
|
+
"success": True,
|
|
823
|
+
"device": device_name,
|
|
824
|
+
"message": f"Device '{device_name}' stopped",
|
|
825
|
+
})
|
|
826
|
+
|
|
827
|
+
except Exception as e:
|
|
828
|
+
logger.error("Failed to stop device {}: {}", device_name, e)
|
|
829
|
+
return mcp_result({
|
|
830
|
+
"success": False,
|
|
831
|
+
"error": f"Stop operation failed: {e}",
|
|
832
|
+
"device": device_name,
|
|
833
|
+
})
|
|
834
|
+
|
|
835
|
+
return run_on_main_thread(_stop)
|
|
836
|
+
|
|
837
|
+
@tool(
|
|
838
|
+
name="lightfall_get_catalog_info",
|
|
839
|
+
description=(
|
|
840
|
+
"Get information about the device catalog including connection status, "
|
|
841
|
+
"backend type, device counts by category, and API usage hints. "
|
|
842
|
+
"Use this to understand what devices are available and how to access them."
|
|
843
|
+
),
|
|
844
|
+
input_schema={
|
|
845
|
+
"type": "object",
|
|
846
|
+
"properties": {},
|
|
847
|
+
},
|
|
848
|
+
)
|
|
849
|
+
async def get_catalog_info(args: dict) -> dict[str, Any]:
|
|
850
|
+
"""Get device catalog information and API hints."""
|
|
851
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
852
|
+
from lightfall.devices.model import DeviceCategory
|
|
853
|
+
|
|
854
|
+
def _get_info():
|
|
855
|
+
catalog = self._get_catalog()
|
|
856
|
+
backend = catalog.backend
|
|
857
|
+
|
|
858
|
+
# Basic connection info
|
|
859
|
+
info = {
|
|
860
|
+
"success": True,
|
|
861
|
+
"connected": catalog.is_connected,
|
|
862
|
+
"backend": backend.name if backend else None,
|
|
863
|
+
"backend_type": type(backend).__name__ if backend else None,
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
# If not connected, provide diagnostic info
|
|
867
|
+
if not catalog.is_connected:
|
|
868
|
+
info["hint"] = (
|
|
869
|
+
"The catalog is not connected. This usually means: "
|
|
870
|
+
"1) The backend wasn't configured in preferences, "
|
|
871
|
+
"2) The backend failed to connect during startup, or "
|
|
872
|
+
"3) The application is still initializing. "
|
|
873
|
+
"Check the application logs for more details."
|
|
874
|
+
)
|
|
875
|
+
info["devices_by_category"] = {}
|
|
876
|
+
info["total_devices"] = 0
|
|
877
|
+
info["example_devices"] = []
|
|
878
|
+
return mcp_result(info)
|
|
879
|
+
|
|
880
|
+
# Get device counts by category
|
|
881
|
+
devices = catalog.get_all_devices()
|
|
882
|
+
by_category: dict[str, list[str]] = {}
|
|
883
|
+
for device in devices:
|
|
884
|
+
cat = device.category.value
|
|
885
|
+
if cat not in by_category:
|
|
886
|
+
by_category[cat] = []
|
|
887
|
+
by_category[cat].append(device.name)
|
|
888
|
+
|
|
889
|
+
info["total_devices"] = len(devices)
|
|
890
|
+
info["devices_by_category"] = {
|
|
891
|
+
cat: {"count": len(names), "names": names}
|
|
892
|
+
for cat, names in by_category.items()
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
# Provide example devices for common categories
|
|
896
|
+
example_devices = []
|
|
897
|
+
for cat in ["motor", "detector", "sensor"]:
|
|
898
|
+
if cat in by_category and by_category[cat]:
|
|
899
|
+
example_devices.append({
|
|
900
|
+
"category": cat,
|
|
901
|
+
"example_name": by_category[cat][0],
|
|
902
|
+
})
|
|
903
|
+
info["example_devices"] = example_devices
|
|
904
|
+
|
|
905
|
+
# API usage hints for plugin developers
|
|
906
|
+
info["api_hints"] = {
|
|
907
|
+
"description": (
|
|
908
|
+
"How to access devices in panel plugins or plans. "
|
|
909
|
+
"Use DeviceCategory enum for filtering, and access "
|
|
910
|
+
"ophyd_device property for actual device control."
|
|
911
|
+
),
|
|
912
|
+
"imports": [
|
|
913
|
+
"from lightfall.devices import DeviceCatalog",
|
|
914
|
+
"from lightfall.devices.model import DeviceCategory",
|
|
915
|
+
],
|
|
916
|
+
"get_catalog": "catalog = DeviceCatalog.get_instance()",
|
|
917
|
+
"list_motors": "motors = catalog.list_devices(category=DeviceCategory.MOTOR)",
|
|
918
|
+
"get_by_name": 'device_info = catalog.get_device_by_name("motor")',
|
|
919
|
+
"get_ophyd": "ophyd_device = device_info.ophyd_device",
|
|
920
|
+
"move_motor": "ophyd_device.set(10).wait()",
|
|
921
|
+
"read_position": "position = ophyd_device.position",
|
|
922
|
+
"available_categories": [cat.value for cat in DeviceCategory],
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
return mcp_result(info)
|
|
926
|
+
|
|
927
|
+
return run_on_main_thread(_get_info)
|
|
928
|
+
|
|
929
|
+
@tool(
|
|
930
|
+
name="lightfall_manage_device",
|
|
931
|
+
description=(
|
|
932
|
+
"Add, remove, update, enable, or disable a device in the catalog. "
|
|
933
|
+
"Requires an editable backend (e.g., happi JSON). "
|
|
934
|
+
"Use 'add' to create new devices, 'update' to change fields, "
|
|
935
|
+
"'enable'/'disable' to toggle active state, 'remove' to delete."
|
|
936
|
+
),
|
|
937
|
+
input_schema={
|
|
938
|
+
"type": "object",
|
|
939
|
+
"properties": {
|
|
940
|
+
"action": {
|
|
941
|
+
"type": "string",
|
|
942
|
+
"enum": ["add", "remove", "update", "enable", "disable"],
|
|
943
|
+
"description": "The management action to perform",
|
|
944
|
+
},
|
|
945
|
+
"name": {
|
|
946
|
+
"type": "string",
|
|
947
|
+
"description": "Device name (identifier for all actions)",
|
|
948
|
+
},
|
|
949
|
+
"fields": {
|
|
950
|
+
"type": "object",
|
|
951
|
+
"description": (
|
|
952
|
+
"Fields to set (for add/update). Keys: display_name, "
|
|
953
|
+
"device_class, prefix, beamline, group, icon_override, "
|
|
954
|
+
"active, plus any extra metadata fields."
|
|
955
|
+
),
|
|
956
|
+
"additionalProperties": True,
|
|
957
|
+
},
|
|
958
|
+
},
|
|
959
|
+
"required": ["action", "name"],
|
|
960
|
+
},
|
|
961
|
+
)
|
|
962
|
+
async def manage_device(args: dict) -> dict[str, Any]:
|
|
963
|
+
"""Manage device catalog entries."""
|
|
964
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
965
|
+
from lightfall.devices.model import DeviceInfo as DI
|
|
966
|
+
|
|
967
|
+
action = args["action"]
|
|
968
|
+
name = args["name"]
|
|
969
|
+
fields = args.get("fields", {})
|
|
970
|
+
|
|
971
|
+
def _manage():
|
|
972
|
+
catalog = self._get_catalog()
|
|
973
|
+
if not catalog.is_connected:
|
|
974
|
+
return mcp_result({"success": False, "error": "Device catalog not connected"})
|
|
975
|
+
|
|
976
|
+
backend = catalog.backend
|
|
977
|
+
if backend is None or not backend.is_editable:
|
|
978
|
+
return mcp_result({"success": False, "error": "Backend does not support editing"})
|
|
979
|
+
|
|
980
|
+
if action == "add":
|
|
981
|
+
if not fields.get("device_class"):
|
|
982
|
+
return mcp_result({"success": False, "error": "device_class is required for add action"})
|
|
983
|
+
known = {"name", "display_name", "device_class", "prefix", "beamline", "group", "icon_override", "active"}
|
|
984
|
+
device_kwargs = {"name": name}
|
|
985
|
+
extra = {}
|
|
986
|
+
for k, v in fields.items():
|
|
987
|
+
if k in known:
|
|
988
|
+
device_kwargs[k] = v
|
|
989
|
+
else:
|
|
990
|
+
extra[k] = v
|
|
991
|
+
device_kwargs["metadata"] = extra
|
|
992
|
+
device = DI(**device_kwargs)
|
|
993
|
+
if catalog.add_device(device):
|
|
994
|
+
return mcp_result({"success": True, "action": "add", "device": name, "message": f"Device '{name}' added"})
|
|
995
|
+
return mcp_result({"success": False, "error": f"Failed to add device '{name}' (may already exist)"})
|
|
996
|
+
|
|
997
|
+
device = catalog.get_device_by_name(name)
|
|
998
|
+
if device is None:
|
|
999
|
+
return mcp_result({"success": False, "error": f"Device '{name}' not found"})
|
|
1000
|
+
|
|
1001
|
+
if action == "remove":
|
|
1002
|
+
if catalog.remove_device(device.id):
|
|
1003
|
+
return mcp_result({"success": True, "action": "remove", "device": name, "message": f"Device '{name}' removed"})
|
|
1004
|
+
return mcp_result({"success": False, "error": f"Failed to remove device '{name}'"})
|
|
1005
|
+
|
|
1006
|
+
if action == "enable":
|
|
1007
|
+
device.active = True
|
|
1008
|
+
if not catalog.update_device(device):
|
|
1009
|
+
return mcp_result({"success": False, "error": f"Failed to enable device '{name}'"})
|
|
1010
|
+
return mcp_result({"success": True, "action": "enable", "device": name, "message": f"Device '{name}' enabled"})
|
|
1011
|
+
|
|
1012
|
+
if action == "disable":
|
|
1013
|
+
device.active = False
|
|
1014
|
+
if not catalog.update_device(device):
|
|
1015
|
+
return mcp_result({"success": False, "error": f"Failed to disable device '{name}'"})
|
|
1016
|
+
return mcp_result({"success": True, "action": "disable", "device": name, "message": f"Device '{name}' disabled"})
|
|
1017
|
+
|
|
1018
|
+
if action == "update":
|
|
1019
|
+
known = {"display_name", "device_class", "prefix", "beamline", "group", "icon_override", "active"}
|
|
1020
|
+
for k, v in fields.items():
|
|
1021
|
+
if k in known:
|
|
1022
|
+
setattr(device, k, v)
|
|
1023
|
+
else:
|
|
1024
|
+
device.metadata[k] = v
|
|
1025
|
+
if not catalog.update_device(device):
|
|
1026
|
+
return mcp_result({"success": False, "error": f"Failed to update device '{name}'"})
|
|
1027
|
+
return mcp_result({"success": True, "action": "update", "device": name, "message": f"Device '{name}' updated", "fields_changed": list(fields.keys())})
|
|
1028
|
+
|
|
1029
|
+
return mcp_result({"success": False, "error": f"Unknown action: {action}"})
|
|
1030
|
+
|
|
1031
|
+
return run_on_main_thread(_manage)
|
|
1032
|
+
|
|
1033
|
+
return [
|
|
1034
|
+
list_devices,
|
|
1035
|
+
get_device,
|
|
1036
|
+
read_device,
|
|
1037
|
+
get_device_state,
|
|
1038
|
+
set_device,
|
|
1039
|
+
move_motor,
|
|
1040
|
+
stop_device,
|
|
1041
|
+
get_catalog_info,
|
|
1042
|
+
manage_device,
|
|
1043
|
+
]
|