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,720 @@
|
|
|
1
|
+
"""MCP tools for RunEngine interaction and run data access.
|
|
2
|
+
|
|
3
|
+
Provides tools for Claude to interact with the NCS RunEngine:
|
|
4
|
+
- Get engine status, pause, resume, abort
|
|
5
|
+
- Access run history and scan data via Tiled
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
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
|
+
# Upper bound on lightfall_wait_for_idle.timeout_seconds. Long enough for any
|
|
18
|
+
# realistic scan, short enough that a typo can't strand the model for
|
|
19
|
+
# the rest of the session.
|
|
20
|
+
_WAIT_TIMEOUT_MAX_SECONDS = 3600.0
|
|
21
|
+
# Smallest practical poll cadence — prevents busy-spinning if the model
|
|
22
|
+
# passes 0 or a negative number. Crossing to the GUI thread on every
|
|
23
|
+
# tick is cheap but not free.
|
|
24
|
+
_WAIT_POLL_MIN_SECONDS = 0.01
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _last_run_payload() -> dict[str, Any] | None:
|
|
28
|
+
"""Return metadata for the most recent Tiled run, or None if unavailable.
|
|
29
|
+
|
|
30
|
+
Designed to be called on the GUI thread (it touches ``TiledService``).
|
|
31
|
+
Errors are swallowed and surfaced as ``None`` — callers embed this in
|
|
32
|
+
larger payloads and shouldn't fail just because Tiled is down.
|
|
33
|
+
"""
|
|
34
|
+
try:
|
|
35
|
+
from lightfall.services.tiled_service import TiledService
|
|
36
|
+
|
|
37
|
+
service = TiledService.get_instance()
|
|
38
|
+
if not service.is_connected or service._client is None:
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
client = service._client
|
|
42
|
+
keys = [k for k, _ in client.items()]
|
|
43
|
+
if not keys:
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
uid = keys[-1]
|
|
47
|
+
run = client[uid]
|
|
48
|
+
metadata = run.metadata
|
|
49
|
+
start = metadata.get("start", {})
|
|
50
|
+
stop = metadata.get("stop", {})
|
|
51
|
+
return {
|
|
52
|
+
"uid": uid,
|
|
53
|
+
"plan_name": start.get("plan_name", "unknown"),
|
|
54
|
+
"start_time": start.get("time"),
|
|
55
|
+
"stop_time": stop.get("time"),
|
|
56
|
+
"exit_status": stop.get("exit_status", "unknown"),
|
|
57
|
+
"num_points": stop.get("num_events", {}).get("primary"),
|
|
58
|
+
"streams": list(run),
|
|
59
|
+
}
|
|
60
|
+
except Exception:
|
|
61
|
+
logger.exception("_last_run_payload failed")
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _read_engine_state_snapshot() -> tuple[str, bool]:
|
|
66
|
+
"""Return ``(EngineState.name, is_idle)`` for the current engine.
|
|
67
|
+
|
|
68
|
+
Runs on the GUI thread via ``run_on_main_thread``. Failures bubble up
|
|
69
|
+
as a synthetic ``ERROR`` state so the wait loop can report something
|
|
70
|
+
sensible on timeout instead of looping silently on an exception.
|
|
71
|
+
"""
|
|
72
|
+
try:
|
|
73
|
+
from lightfall.acquire.engine import get_engine
|
|
74
|
+
|
|
75
|
+
engine = get_engine()
|
|
76
|
+
return engine.state.name, engine.is_idle
|
|
77
|
+
except Exception as exc: # pragma: no cover — defensive
|
|
78
|
+
logger.exception("lightfall_wait_for_idle: engine state read failed: {}", exc)
|
|
79
|
+
return "ERROR", False
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
async def _wait_for_idle_payload(
|
|
83
|
+
*,
|
|
84
|
+
timeout_seconds: float = 600.0,
|
|
85
|
+
poll_interval_seconds: float = 0.5,
|
|
86
|
+
include_last_run: bool = True,
|
|
87
|
+
) -> dict[str, Any]:
|
|
88
|
+
"""Block until the RunEngine reaches IDLE, the timeout elapses, or cancelled.
|
|
89
|
+
|
|
90
|
+
Polls ``engine.is_idle`` on the GUI thread between cooperative
|
|
91
|
+
``asyncio.sleep`` calls so the SDK event loop stays responsive (cancel
|
|
92
|
+
requests can still land, the worker can still drain).
|
|
93
|
+
|
|
94
|
+
Returns the response envelope documented in the tool's MCP description.
|
|
95
|
+
On ``CancelledError`` the exception propagates — callers (the SDK worker)
|
|
96
|
+
drain the response stream on cancel, so we must not block shutdown by
|
|
97
|
+
swallowing it.
|
|
98
|
+
"""
|
|
99
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
100
|
+
|
|
101
|
+
timeout = max(0.0, min(float(timeout_seconds), _WAIT_TIMEOUT_MAX_SECONDS))
|
|
102
|
+
interval = max(_WAIT_POLL_MIN_SECONDS, float(poll_interval_seconds))
|
|
103
|
+
|
|
104
|
+
loop = asyncio.get_event_loop()
|
|
105
|
+
start = loop.time()
|
|
106
|
+
last_state = "UNKNOWN"
|
|
107
|
+
reached_idle = False
|
|
108
|
+
|
|
109
|
+
while True:
|
|
110
|
+
state_name, is_idle = run_on_main_thread(_read_engine_state_snapshot)
|
|
111
|
+
last_state = state_name
|
|
112
|
+
|
|
113
|
+
if is_idle:
|
|
114
|
+
reached_idle = True
|
|
115
|
+
break
|
|
116
|
+
|
|
117
|
+
elapsed = loop.time() - start
|
|
118
|
+
if elapsed >= timeout:
|
|
119
|
+
break
|
|
120
|
+
|
|
121
|
+
# Cap sleep so we don't overshoot the deadline on the final tick.
|
|
122
|
+
remaining = timeout - elapsed
|
|
123
|
+
await asyncio.sleep(min(interval, remaining))
|
|
124
|
+
|
|
125
|
+
elapsed = loop.time() - start
|
|
126
|
+
last_run: dict[str, Any] | None = None
|
|
127
|
+
if reached_idle and include_last_run:
|
|
128
|
+
last_run = run_on_main_thread(_last_run_payload)
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
"success": True,
|
|
132
|
+
"reached_idle": reached_idle,
|
|
133
|
+
"state": last_state,
|
|
134
|
+
"elapsed_seconds": elapsed,
|
|
135
|
+
"last_run": last_run,
|
|
136
|
+
"reason": "" if reached_idle else "timeout",
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _beam_status_payload(force_refresh: bool = False) -> dict[str, Any]:
|
|
141
|
+
"""Read the ALS beam status via the polling service.
|
|
142
|
+
|
|
143
|
+
Values reflect the service's most recent successful poll. ``force_refresh``
|
|
144
|
+
kicks an immediate background poll; freshly-polled values may not be
|
|
145
|
+
reflected until that poll completes, so the returned snapshot can lag by
|
|
146
|
+
one cycle.
|
|
147
|
+
"""
|
|
148
|
+
from lightfall.services.als_beam_status import ALSBeamStatusService
|
|
149
|
+
|
|
150
|
+
service = ALSBeamStatusService.get_instance()
|
|
151
|
+
if force_refresh:
|
|
152
|
+
service.force_refresh()
|
|
153
|
+
data = service.get_introspection_data()
|
|
154
|
+
data["success"] = True
|
|
155
|
+
return data
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
class EngineToolsAgent(AgentPlugin):
|
|
159
|
+
"""MCP tools for RunEngine control and run data access."""
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def name(self) -> str:
|
|
163
|
+
return "engine_tools"
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def description(self) -> str:
|
|
167
|
+
return "Tools for RunEngine control and run data access"
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def category(self) -> str:
|
|
171
|
+
return "acquisition"
|
|
172
|
+
|
|
173
|
+
def _get_session_manager(self):
|
|
174
|
+
from lightfall.auth.session import SessionManager
|
|
175
|
+
return SessionManager.get_instance()
|
|
176
|
+
|
|
177
|
+
def _check_device_control_permission(self) -> tuple[bool, str | None]:
|
|
178
|
+
from lightfall.auth.policy import Permission
|
|
179
|
+
session = self._get_session_manager()
|
|
180
|
+
if not session.check_permission(Permission.DEVICE_CONTROL):
|
|
181
|
+
return False, "Permission denied: DEVICE_CONTROL required"
|
|
182
|
+
return True, None
|
|
183
|
+
|
|
184
|
+
def create_tools(self) -> list[Any]:
|
|
185
|
+
try:
|
|
186
|
+
from claude_agent_sdk import tool
|
|
187
|
+
except ImportError:
|
|
188
|
+
logger.warning("claude_agent_sdk not available, engine tools disabled")
|
|
189
|
+
return []
|
|
190
|
+
|
|
191
|
+
@tool(
|
|
192
|
+
name="lightfall_get_run_status",
|
|
193
|
+
description="Get the current RunEngine status including state, busy flag, and current procedure info.",
|
|
194
|
+
input_schema={"type": "object", "properties": {}},
|
|
195
|
+
)
|
|
196
|
+
async def get_run_status(args: dict) -> dict[str, Any]:
|
|
197
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
198
|
+
|
|
199
|
+
def _get():
|
|
200
|
+
try:
|
|
201
|
+
from lightfall.acquire.engine import get_engine
|
|
202
|
+
engine = get_engine()
|
|
203
|
+
|
|
204
|
+
result = {
|
|
205
|
+
"success": True,
|
|
206
|
+
"state": engine.state_name,
|
|
207
|
+
"state_value": engine.state.name,
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
# Check current procedure
|
|
211
|
+
proc = engine.get_current_procedure()
|
|
212
|
+
if proc is not None:
|
|
213
|
+
result["current_procedure"] = {
|
|
214
|
+
"name": getattr(proc, "name", None),
|
|
215
|
+
"priority": getattr(proc, "priority", None),
|
|
216
|
+
}
|
|
217
|
+
else:
|
|
218
|
+
result["current_procedure"] = None
|
|
219
|
+
|
|
220
|
+
# Queue info
|
|
221
|
+
if hasattr(engine, "_queue"):
|
|
222
|
+
result["queue_size"] = engine._queue.qsize()
|
|
223
|
+
|
|
224
|
+
return mcp_result(result)
|
|
225
|
+
except Exception as e:
|
|
226
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
227
|
+
|
|
228
|
+
return run_on_main_thread(_get)
|
|
229
|
+
|
|
230
|
+
@tool(
|
|
231
|
+
name="lightfall_pause_plan",
|
|
232
|
+
description="Pause the currently running plan. Use defer=true (default) for safe pause at next checkpoint, or defer=false for immediate pause.",
|
|
233
|
+
input_schema={
|
|
234
|
+
"type": "object",
|
|
235
|
+
"properties": {
|
|
236
|
+
"defer": {
|
|
237
|
+
"type": "boolean",
|
|
238
|
+
"description": "If true, pause at next checkpoint (safe). If false, pause immediately.",
|
|
239
|
+
"default": True,
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
)
|
|
244
|
+
async def pause_plan(args: dict) -> dict[str, Any]:
|
|
245
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
246
|
+
|
|
247
|
+
defer = args.get("defer", True)
|
|
248
|
+
|
|
249
|
+
def _pause():
|
|
250
|
+
has_perm, error = self._check_device_control_permission()
|
|
251
|
+
if not has_perm:
|
|
252
|
+
return mcp_result({"success": False, "error": error}, is_error=True)
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
from lightfall.acquire.engine import get_engine
|
|
256
|
+
engine = get_engine()
|
|
257
|
+
engine.pause(defer=defer)
|
|
258
|
+
logger.info("Plan pause requested (defer={})", defer)
|
|
259
|
+
return mcp_result({
|
|
260
|
+
"success": True,
|
|
261
|
+
"message": f"Pause requested (defer={defer})",
|
|
262
|
+
"state": engine.state_name,
|
|
263
|
+
})
|
|
264
|
+
except Exception as e:
|
|
265
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
266
|
+
|
|
267
|
+
return run_on_main_thread(_pause)
|
|
268
|
+
|
|
269
|
+
@tool(
|
|
270
|
+
name="lightfall_resume_plan",
|
|
271
|
+
description="Resume a paused plan.",
|
|
272
|
+
input_schema={"type": "object", "properties": {}},
|
|
273
|
+
)
|
|
274
|
+
async def resume_plan(args: dict) -> dict[str, Any]:
|
|
275
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
276
|
+
|
|
277
|
+
def _resume():
|
|
278
|
+
has_perm, error = self._check_device_control_permission()
|
|
279
|
+
if not has_perm:
|
|
280
|
+
return mcp_result({"success": False, "error": error}, is_error=True)
|
|
281
|
+
|
|
282
|
+
try:
|
|
283
|
+
from lightfall.acquire.engine import get_engine
|
|
284
|
+
engine = get_engine()
|
|
285
|
+
engine.resume()
|
|
286
|
+
logger.info("Plan resumed")
|
|
287
|
+
return mcp_result({
|
|
288
|
+
"success": True,
|
|
289
|
+
"message": "Plan resumed",
|
|
290
|
+
"state": engine.state_name,
|
|
291
|
+
})
|
|
292
|
+
except Exception as e:
|
|
293
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
294
|
+
|
|
295
|
+
return run_on_main_thread(_resume)
|
|
296
|
+
|
|
297
|
+
@tool(
|
|
298
|
+
name="lightfall_abort_plan",
|
|
299
|
+
description="Abort the currently running plan.",
|
|
300
|
+
input_schema={
|
|
301
|
+
"type": "object",
|
|
302
|
+
"properties": {
|
|
303
|
+
"reason": {
|
|
304
|
+
"type": "string",
|
|
305
|
+
"description": "Optional reason for aborting",
|
|
306
|
+
"default": "",
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
)
|
|
311
|
+
async def abort_plan(args: dict) -> dict[str, Any]:
|
|
312
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
313
|
+
|
|
314
|
+
reason = args.get("reason", "")
|
|
315
|
+
|
|
316
|
+
def _abort():
|
|
317
|
+
has_perm, error = self._check_device_control_permission()
|
|
318
|
+
if not has_perm:
|
|
319
|
+
return mcp_result({"success": False, "error": error}, is_error=True)
|
|
320
|
+
|
|
321
|
+
try:
|
|
322
|
+
from lightfall.acquire.engine import get_engine
|
|
323
|
+
engine = get_engine()
|
|
324
|
+
engine.abort(reason=reason)
|
|
325
|
+
logger.info("Plan aborted: {}", reason or "(no reason)")
|
|
326
|
+
return mcp_result({
|
|
327
|
+
"success": True,
|
|
328
|
+
"message": f"Plan aborted{': ' + reason if reason else ''}",
|
|
329
|
+
"state": engine.state_name,
|
|
330
|
+
})
|
|
331
|
+
except Exception as e:
|
|
332
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
333
|
+
|
|
334
|
+
return run_on_main_thread(_abort)
|
|
335
|
+
|
|
336
|
+
@tool(
|
|
337
|
+
name="lightfall_get_run_history",
|
|
338
|
+
description="Get recent run history from the Tiled data catalog. Returns UIDs, plan names, start times, and exit status.",
|
|
339
|
+
input_schema={
|
|
340
|
+
"type": "object",
|
|
341
|
+
"properties": {
|
|
342
|
+
"limit": {
|
|
343
|
+
"type": "integer",
|
|
344
|
+
"description": "Maximum number of runs to return (default: 10)",
|
|
345
|
+
"default": 10,
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
)
|
|
350
|
+
async def get_run_history(args: dict) -> dict[str, Any]:
|
|
351
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
352
|
+
|
|
353
|
+
limit = args.get("limit", 10)
|
|
354
|
+
|
|
355
|
+
def _get():
|
|
356
|
+
try:
|
|
357
|
+
from lightfall.services.tiled_service import TiledService
|
|
358
|
+
service = TiledService.get_instance()
|
|
359
|
+
|
|
360
|
+
if not service.is_connected or service._client is None:
|
|
361
|
+
return mcp_result({
|
|
362
|
+
"success": False,
|
|
363
|
+
"error": "Tiled service not connected",
|
|
364
|
+
"hint": "Check Tiled connection in preferences.",
|
|
365
|
+
}, is_error=True)
|
|
366
|
+
|
|
367
|
+
client = service._client
|
|
368
|
+
runs = []
|
|
369
|
+
# Tiled client is a catalog; iterate in reverse for most recent.
|
|
370
|
+
# NOTE: list(client) only returns the first page (default 100).
|
|
371
|
+
# Use client.items() to get ALL entries.
|
|
372
|
+
keys = [k for k, _ in client.items()]
|
|
373
|
+
for uid in reversed(keys[-limit:]):
|
|
374
|
+
try:
|
|
375
|
+
run = client[uid]
|
|
376
|
+
metadata = run.metadata
|
|
377
|
+
start = metadata.get("start", {})
|
|
378
|
+
stop = metadata.get("stop", {})
|
|
379
|
+
runs.append({
|
|
380
|
+
"uid": uid,
|
|
381
|
+
"plan_name": start.get("plan_name", "unknown"),
|
|
382
|
+
"time": start.get("time"),
|
|
383
|
+
"exit_status": stop.get("exit_status", "unknown"),
|
|
384
|
+
"num_points": stop.get("num_events", {}).get("primary"),
|
|
385
|
+
})
|
|
386
|
+
except Exception as e:
|
|
387
|
+
runs.append({"uid": uid, "error": str(e)})
|
|
388
|
+
|
|
389
|
+
return mcp_result({
|
|
390
|
+
"success": True,
|
|
391
|
+
"count": len(runs),
|
|
392
|
+
"runs": runs,
|
|
393
|
+
})
|
|
394
|
+
except Exception as e:
|
|
395
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
396
|
+
|
|
397
|
+
return run_on_main_thread(_get)
|
|
398
|
+
|
|
399
|
+
@tool(
|
|
400
|
+
name="lightfall_get_scan_data",
|
|
401
|
+
description="Get data from a specific run by UID. Returns column names, shape, and first N rows of the primary data stream.",
|
|
402
|
+
input_schema={
|
|
403
|
+
"type": "object",
|
|
404
|
+
"properties": {
|
|
405
|
+
"uid": {
|
|
406
|
+
"type": "string",
|
|
407
|
+
"description": "Run UID",
|
|
408
|
+
},
|
|
409
|
+
"max_rows": {
|
|
410
|
+
"type": "integer",
|
|
411
|
+
"description": "Maximum number of rows to return (default: 50)",
|
|
412
|
+
"default": 50,
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
"required": ["uid"],
|
|
416
|
+
},
|
|
417
|
+
)
|
|
418
|
+
async def get_scan_data(args: dict) -> dict[str, Any]:
|
|
419
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
420
|
+
|
|
421
|
+
uid = args["uid"]
|
|
422
|
+
max_rows = args.get("max_rows", 50)
|
|
423
|
+
|
|
424
|
+
def _get():
|
|
425
|
+
from lightfall.services.tiled_service import TiledService
|
|
426
|
+
service = TiledService.get_instance()
|
|
427
|
+
|
|
428
|
+
if not service.is_connected or service._client is None:
|
|
429
|
+
return mcp_result({
|
|
430
|
+
"success": False,
|
|
431
|
+
"error": "Tiled service not connected",
|
|
432
|
+
}, is_error=True)
|
|
433
|
+
|
|
434
|
+
client = service._client
|
|
435
|
+
try:
|
|
436
|
+
run = client[uid]
|
|
437
|
+
except KeyError:
|
|
438
|
+
return mcp_result({
|
|
439
|
+
"success": False,
|
|
440
|
+
"error": f"Run '{uid}' not found",
|
|
441
|
+
}, is_error=True)
|
|
442
|
+
|
|
443
|
+
try:
|
|
444
|
+
if "primary" not in run:
|
|
445
|
+
return mcp_result({
|
|
446
|
+
"success": False,
|
|
447
|
+
"error": "No 'primary' stream found",
|
|
448
|
+
"available_streams": list(run),
|
|
449
|
+
"uid": uid,
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
from lightfall.utils.tiled_helpers import read_events
|
|
453
|
+
stream = run["primary"]
|
|
454
|
+
events = read_events(stream)
|
|
455
|
+
|
|
456
|
+
if events is None:
|
|
457
|
+
return mcp_result({
|
|
458
|
+
"success": False,
|
|
459
|
+
"error": "No readable data in primary stream",
|
|
460
|
+
"stream_keys": list(stream.keys()),
|
|
461
|
+
"uid": uid,
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
# events is an xarray Dataset (or similar mapping).
|
|
465
|
+
# Use dict-style access — .keys() and dataset[col].
|
|
466
|
+
import numpy as np
|
|
467
|
+
all_cols = list(events.keys())
|
|
468
|
+
# Filter out timestamp columns (ts_*) for cleaner output
|
|
469
|
+
columns = [c for c in all_cols if not c.startswith("ts_")]
|
|
470
|
+
n_rows = len(np.asarray(events[columns[0]])) if columns else 0
|
|
471
|
+
shape = [n_rows, len(columns)]
|
|
472
|
+
|
|
473
|
+
# Build row dicts from the column arrays
|
|
474
|
+
rows = []
|
|
475
|
+
limit = min(n_rows, max_rows)
|
|
476
|
+
col_arrays = {c: np.asarray(events[c])[:limit] for c in columns}
|
|
477
|
+
for i in range(limit):
|
|
478
|
+
row = {}
|
|
479
|
+
for c in columns:
|
|
480
|
+
val = col_arrays[c][i]
|
|
481
|
+
# Convert numpy types to Python natives for JSON
|
|
482
|
+
row[c] = val.item() if hasattr(val, "item") else val
|
|
483
|
+
rows.append(row)
|
|
484
|
+
|
|
485
|
+
return mcp_result({
|
|
486
|
+
"success": True,
|
|
487
|
+
"uid": uid,
|
|
488
|
+
"columns": columns,
|
|
489
|
+
"shape": shape,
|
|
490
|
+
"rows_returned": len(rows),
|
|
491
|
+
"data": rows,
|
|
492
|
+
})
|
|
493
|
+
except Exception as e:
|
|
494
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
495
|
+
|
|
496
|
+
return run_on_main_thread(_get)
|
|
497
|
+
|
|
498
|
+
@tool(
|
|
499
|
+
name="lightfall_get_last_run",
|
|
500
|
+
description="Get metadata for the most recent run (shortcut). Returns UID, plan_name, start/stop time, and exit status.",
|
|
501
|
+
input_schema={"type": "object", "properties": {}},
|
|
502
|
+
)
|
|
503
|
+
async def get_last_run(args: dict) -> dict[str, Any]:
|
|
504
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
505
|
+
|
|
506
|
+
def _get():
|
|
507
|
+
try:
|
|
508
|
+
from lightfall.services.tiled_service import TiledService
|
|
509
|
+
service = TiledService.get_instance()
|
|
510
|
+
|
|
511
|
+
if not service.is_connected or service._client is None:
|
|
512
|
+
return mcp_result({
|
|
513
|
+
"success": False,
|
|
514
|
+
"error": "Tiled service not connected",
|
|
515
|
+
}, is_error=True)
|
|
516
|
+
|
|
517
|
+
client = service._client
|
|
518
|
+
# NOTE: list(client) only returns the first page (default 100).
|
|
519
|
+
# Use client.items() to get ALL entries.
|
|
520
|
+
keys = [k for k, _ in client.items()]
|
|
521
|
+
if not keys:
|
|
522
|
+
return mcp_result({
|
|
523
|
+
"success": False,
|
|
524
|
+
"error": "No runs found in catalog",
|
|
525
|
+
})
|
|
526
|
+
|
|
527
|
+
uid = keys[-1]
|
|
528
|
+
run = client[uid]
|
|
529
|
+
metadata = run.metadata
|
|
530
|
+
start = metadata.get("start", {})
|
|
531
|
+
stop = metadata.get("stop", {})
|
|
532
|
+
|
|
533
|
+
return mcp_result({
|
|
534
|
+
"success": True,
|
|
535
|
+
"uid": uid,
|
|
536
|
+
"plan_name": start.get("plan_name", "unknown"),
|
|
537
|
+
"start_time": start.get("time"),
|
|
538
|
+
"stop_time": stop.get("time"),
|
|
539
|
+
"exit_status": stop.get("exit_status", "unknown"),
|
|
540
|
+
"num_points": stop.get("num_events", {}).get("primary"),
|
|
541
|
+
"streams": list(run),
|
|
542
|
+
})
|
|
543
|
+
except Exception as e:
|
|
544
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
545
|
+
|
|
546
|
+
return run_on_main_thread(_get)
|
|
547
|
+
|
|
548
|
+
@tool(
|
|
549
|
+
name="lightfall_show_run",
|
|
550
|
+
description=(
|
|
551
|
+
"Display a Bluesky run by uid in the Visualization panel. "
|
|
552
|
+
"Opens the panel if it isn't already shown. "
|
|
553
|
+
"Use lightfall_get_last_run or lightfall_get_run_history to find a uid."
|
|
554
|
+
),
|
|
555
|
+
input_schema={
|
|
556
|
+
"type": "object",
|
|
557
|
+
"properties": {
|
|
558
|
+
"uid": {
|
|
559
|
+
"type": "string",
|
|
560
|
+
"description": "Run uid (start document uid).",
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
"required": ["uid"],
|
|
564
|
+
},
|
|
565
|
+
)
|
|
566
|
+
async def show_run(args: dict) -> dict[str, Any]:
|
|
567
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
568
|
+
|
|
569
|
+
uid = args.get("uid")
|
|
570
|
+
if not uid:
|
|
571
|
+
return mcp_result(
|
|
572
|
+
{"success": False, "error": "uid is required"},
|
|
573
|
+
is_error=True,
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
def _show():
|
|
577
|
+
try:
|
|
578
|
+
from lightfall.core.services import ServiceRegistry
|
|
579
|
+
from lightfall.services.tiled_service import TiledService
|
|
580
|
+
from lightfall.ui.docking import DockingManager
|
|
581
|
+
from lightfall.ui.panels.visualization_panel import VisualizationPanel
|
|
582
|
+
|
|
583
|
+
service = TiledService.get_instance()
|
|
584
|
+
if not service.is_connected or service._client is None:
|
|
585
|
+
return mcp_result(
|
|
586
|
+
{"success": False, "error": "Tiled service not connected"},
|
|
587
|
+
is_error=True,
|
|
588
|
+
)
|
|
589
|
+
try:
|
|
590
|
+
entry = service._client[uid]
|
|
591
|
+
except KeyError:
|
|
592
|
+
return mcp_result(
|
|
593
|
+
{"success": False, "error": f"Run uid {uid!r} not found"},
|
|
594
|
+
is_error=True,
|
|
595
|
+
)
|
|
596
|
+
|
|
597
|
+
dm = ServiceRegistry.get_instance().get(DockingManager, None)
|
|
598
|
+
if dm is None:
|
|
599
|
+
return mcp_result(
|
|
600
|
+
{"success": False, "error": "DockingManager not available"},
|
|
601
|
+
is_error=True,
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
viz_panel_id = "lightfall.panels.visualization"
|
|
605
|
+
dm.show_panel(viz_panel_id)
|
|
606
|
+
panel = dm.get_panel(viz_panel_id)
|
|
607
|
+
if not isinstance(panel, VisualizationPanel):
|
|
608
|
+
return mcp_result(
|
|
609
|
+
{"success": False, "error": "Visualization panel unavailable"},
|
|
610
|
+
is_error=True,
|
|
611
|
+
)
|
|
612
|
+
|
|
613
|
+
panel.open_run(entry)
|
|
614
|
+
return mcp_result({"success": True, "uid": uid})
|
|
615
|
+
except Exception as e:
|
|
616
|
+
logger.exception("lightfall_show_run failed")
|
|
617
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
618
|
+
|
|
619
|
+
return run_on_main_thread(_show)
|
|
620
|
+
|
|
621
|
+
@tool(
|
|
622
|
+
name="lightfall_wait_for_idle",
|
|
623
|
+
description=(
|
|
624
|
+
"Block until the RunEngine returns to IDLE, then return its state "
|
|
625
|
+
"and (optionally) the most recent run's metadata. Use this to wait "
|
|
626
|
+
"for a scan to finish before fitting data or running follow-up "
|
|
627
|
+
"plans. This suspends the model inside the tool call until the "
|
|
628
|
+
"engine is idle, so prefer it over polling lightfall_get_run_status in a "
|
|
629
|
+
"loop, and over ScheduleWakeup — ScheduleWakeup currently does not "
|
|
630
|
+
"fire in Lightfall's embedded Claude session. "
|
|
631
|
+
"Defaults: timeout_seconds=600 (max 3600), poll_interval=0.5s."
|
|
632
|
+
),
|
|
633
|
+
input_schema={
|
|
634
|
+
"type": "object",
|
|
635
|
+
"properties": {
|
|
636
|
+
"timeout_seconds": {
|
|
637
|
+
"type": "number",
|
|
638
|
+
"description": "Abandon and return reason='timeout' if the engine isn't idle by then. Clamped to [0, 3600].",
|
|
639
|
+
"default": 600.0,
|
|
640
|
+
},
|
|
641
|
+
"poll_interval_seconds": {
|
|
642
|
+
"type": "number",
|
|
643
|
+
"description": "How often to re-check engine state. Clamped to a minimum of 0.01s.",
|
|
644
|
+
"default": 0.5,
|
|
645
|
+
},
|
|
646
|
+
"include_last_run": {
|
|
647
|
+
"type": "boolean",
|
|
648
|
+
"description": "When idle, include the most recent run's metadata (uid, plan_name, exit_status, …) so you don't need a second lightfall_get_last_run call.",
|
|
649
|
+
"default": True,
|
|
650
|
+
},
|
|
651
|
+
},
|
|
652
|
+
},
|
|
653
|
+
)
|
|
654
|
+
async def wait_for_idle(args: dict) -> dict[str, Any]:
|
|
655
|
+
timeout_seconds = float(args.get("timeout_seconds", 600.0))
|
|
656
|
+
poll_interval_seconds = float(args.get("poll_interval_seconds", 0.5))
|
|
657
|
+
include_last_run = bool(args.get("include_last_run", True))
|
|
658
|
+
|
|
659
|
+
try:
|
|
660
|
+
payload = await _wait_for_idle_payload(
|
|
661
|
+
timeout_seconds=timeout_seconds,
|
|
662
|
+
poll_interval_seconds=poll_interval_seconds,
|
|
663
|
+
include_last_run=include_last_run,
|
|
664
|
+
)
|
|
665
|
+
return mcp_result(payload)
|
|
666
|
+
except asyncio.CancelledError:
|
|
667
|
+
# Propagate cleanly — the SDK worker drains the response
|
|
668
|
+
# stream on cancel, and we must not block shutdown.
|
|
669
|
+
raise
|
|
670
|
+
except Exception as e:
|
|
671
|
+
logger.exception("lightfall_wait_for_idle failed")
|
|
672
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
673
|
+
|
|
674
|
+
@tool(
|
|
675
|
+
name="lightfall_get_beam_status",
|
|
676
|
+
description=(
|
|
677
|
+
"Get ALS storage-ring beam status: ring current (mA), beam/shutter "
|
|
678
|
+
"availability, energy (GeV), lifetime (hours), beam-position stability, "
|
|
679
|
+
"and the operations comment. Values reflect the most recent completed poll "
|
|
680
|
+
"(the service polls about every 60 s). Use this to explain why a beamline "
|
|
681
|
+
"diode reads no beam (ring dump vs shutter closed vs mis-steering). "
|
|
682
|
+
"force_refresh=true schedules an immediate background poll, but its results "
|
|
683
|
+
"appear on a SUBSEQUENT call, not this one."
|
|
684
|
+
),
|
|
685
|
+
input_schema={
|
|
686
|
+
"type": "object",
|
|
687
|
+
"properties": {
|
|
688
|
+
"force_refresh": {
|
|
689
|
+
"type": "boolean",
|
|
690
|
+
"description": "Schedule an immediate background poll. Its result appears on a later call, not this one.",
|
|
691
|
+
"default": False,
|
|
692
|
+
},
|
|
693
|
+
},
|
|
694
|
+
},
|
|
695
|
+
)
|
|
696
|
+
async def get_beam_status(args: dict) -> dict[str, Any]:
|
|
697
|
+
from lightfall.claude._internal.threading import run_on_main_thread
|
|
698
|
+
|
|
699
|
+
force = args.get("force_refresh", False)
|
|
700
|
+
|
|
701
|
+
def _get():
|
|
702
|
+
try:
|
|
703
|
+
return mcp_result(_beam_status_payload(force_refresh=force))
|
|
704
|
+
except Exception as e:
|
|
705
|
+
return mcp_result({"success": False, "error": str(e)}, is_error=True)
|
|
706
|
+
|
|
707
|
+
return run_on_main_thread(_get)
|
|
708
|
+
|
|
709
|
+
return [
|
|
710
|
+
get_run_status,
|
|
711
|
+
pause_plan,
|
|
712
|
+
resume_plan,
|
|
713
|
+
abort_plan,
|
|
714
|
+
get_run_history,
|
|
715
|
+
get_scan_data,
|
|
716
|
+
get_last_run,
|
|
717
|
+
show_run,
|
|
718
|
+
get_beam_status,
|
|
719
|
+
wait_for_idle,
|
|
720
|
+
]
|