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,727 @@
|
|
|
1
|
+
"""LFApplication - Central application singleton.
|
|
2
|
+
|
|
3
|
+
This module provides the main application class that coordinates
|
|
4
|
+
initialization, manages services, and controls the application lifecycle.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import sys
|
|
10
|
+
import threading
|
|
11
|
+
from enum import Enum, auto
|
|
12
|
+
from typing import TYPE_CHECKING, Any
|
|
13
|
+
|
|
14
|
+
from PySide6.QtCore import QCoreApplication, QEvent, QObject
|
|
15
|
+
from PySide6.QtWidgets import QApplication
|
|
16
|
+
|
|
17
|
+
from lightfall.core.services import ServiceRegistry
|
|
18
|
+
from lightfall.ipc.service import IPCService
|
|
19
|
+
from lightfall.ipc.trust import TrustDialog, TrustManager, TrustState
|
|
20
|
+
from lightfall.utils.logging import configure_logging, logger
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from PySide6.QtWidgets import QMainWindow
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ApplicationState(Enum):
|
|
29
|
+
"""Application lifecycle states."""
|
|
30
|
+
|
|
31
|
+
UNINITIALIZED = auto()
|
|
32
|
+
INITIALIZING = auto()
|
|
33
|
+
READY = auto()
|
|
34
|
+
RUNNING = auto()
|
|
35
|
+
SHUTTING_DOWN = auto()
|
|
36
|
+
TERMINATED = auto()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class LFEvent(QEvent):
|
|
40
|
+
"""Base class for NCS custom events.
|
|
41
|
+
|
|
42
|
+
Use QCoreApplication.postEvent() to dispatch these events
|
|
43
|
+
and install event filters to handle them.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
_event_type_registry: dict[str, QEvent.Type] = {}
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def register_type(cls, name: str) -> QEvent.Type:
|
|
50
|
+
"""Register a custom event type by name.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
name: Unique name for the event type.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
The registered event type.
|
|
57
|
+
"""
|
|
58
|
+
if name not in cls._event_type_registry:
|
|
59
|
+
cls._event_type_registry[name] = QEvent.Type(QEvent.registerEventType())
|
|
60
|
+
return cls._event_type_registry[name]
|
|
61
|
+
|
|
62
|
+
def __init__(self, event_type: QEvent.Type, data: dict[str, Any] | None = None) -> None:
|
|
63
|
+
super().__init__(event_type)
|
|
64
|
+
self.data = data or {}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# Pre-registered event types
|
|
68
|
+
class LFEventTypes:
|
|
69
|
+
"""Standard NCS event types."""
|
|
70
|
+
|
|
71
|
+
CONFIG_CHANGED = LFEvent.register_type("lightfall.config.changed")
|
|
72
|
+
SERVICE_REGISTERED = LFEvent.register_type("lightfall.service.registered")
|
|
73
|
+
STATE_CHANGED = LFEvent.register_type("lightfall.state.changed")
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class LFApplication(QObject):
|
|
77
|
+
"""
|
|
78
|
+
Central application singleton managing NCS lifecycle.
|
|
79
|
+
|
|
80
|
+
LFApplication coordinates:
|
|
81
|
+
- Service registration and initialization
|
|
82
|
+
- Configuration loading
|
|
83
|
+
- Main window management
|
|
84
|
+
- Application state transitions
|
|
85
|
+
- Graceful shutdown
|
|
86
|
+
|
|
87
|
+
The application follows an async initialization pattern:
|
|
88
|
+
Config -> Services -> Plugins -> UI
|
|
89
|
+
|
|
90
|
+
Example:
|
|
91
|
+
>>> app = LFApplication.get_instance()
|
|
92
|
+
>>> app.initialize()
|
|
93
|
+
>>> return app.run()
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
_instance: LFApplication | None = None
|
|
97
|
+
_lock = threading.RLock()
|
|
98
|
+
|
|
99
|
+
def __init__(self, argv: list[str] | None = None) -> None:
|
|
100
|
+
"""
|
|
101
|
+
Initialize LFApplication.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
argv: Command line arguments. If None, uses sys.argv.
|
|
105
|
+
"""
|
|
106
|
+
super().__init__()
|
|
107
|
+
self._state = ApplicationState.UNINITIALIZED
|
|
108
|
+
self._qt_app: QApplication | None = None
|
|
109
|
+
self._main_window: QMainWindow | None = None
|
|
110
|
+
self._services = ServiceRegistry.get_instance()
|
|
111
|
+
self._argv = argv if argv is not None else sys.argv
|
|
112
|
+
self._auth_dialog_active: bool = False
|
|
113
|
+
|
|
114
|
+
@classmethod
|
|
115
|
+
def get_instance(cls, argv: list[str] | None = None) -> LFApplication:
|
|
116
|
+
"""
|
|
117
|
+
Get the singleton LFApplication instance.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
argv: Command line arguments (only used on first call).
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
The shared LFApplication instance.
|
|
124
|
+
"""
|
|
125
|
+
if cls._instance is None:
|
|
126
|
+
with cls._lock:
|
|
127
|
+
if cls._instance is None:
|
|
128
|
+
cls._instance = cls(argv)
|
|
129
|
+
return cls._instance
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def reset(cls) -> None:
|
|
133
|
+
"""
|
|
134
|
+
Reset the singleton instance.
|
|
135
|
+
|
|
136
|
+
Primarily used for testing.
|
|
137
|
+
"""
|
|
138
|
+
with cls._lock:
|
|
139
|
+
if cls._instance is not None:
|
|
140
|
+
cls._instance._shutdown()
|
|
141
|
+
cls._instance = None
|
|
142
|
+
ServiceRegistry.reset()
|
|
143
|
+
|
|
144
|
+
@property
|
|
145
|
+
def state(self) -> ApplicationState:
|
|
146
|
+
"""Current application state."""
|
|
147
|
+
return self._state
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def services(self) -> ServiceRegistry:
|
|
151
|
+
"""Access to the service registry."""
|
|
152
|
+
return self._services
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def qt_app(self) -> QApplication:
|
|
156
|
+
"""The Qt application instance."""
|
|
157
|
+
if self._qt_app is None:
|
|
158
|
+
raise RuntimeError("Qt application not initialized. Call initialize() first.")
|
|
159
|
+
return self._qt_app
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def main_window(self) -> QMainWindow | None:
|
|
163
|
+
"""The main application window."""
|
|
164
|
+
return self._main_window
|
|
165
|
+
|
|
166
|
+
def _set_state(self, new_state: ApplicationState) -> None:
|
|
167
|
+
"""Change application state and post event."""
|
|
168
|
+
old_state = self._state
|
|
169
|
+
self._state = new_state
|
|
170
|
+
logger.info("Application state: {} -> {}", old_state.name, new_state.name)
|
|
171
|
+
|
|
172
|
+
# Post state change event
|
|
173
|
+
if self._qt_app:
|
|
174
|
+
event = LFEvent(
|
|
175
|
+
LFEventTypes.STATE_CHANGED,
|
|
176
|
+
{"old_state": old_state, "new_state": new_state},
|
|
177
|
+
)
|
|
178
|
+
QCoreApplication.postEvent(self, event)
|
|
179
|
+
|
|
180
|
+
def initialize(
|
|
181
|
+
self,
|
|
182
|
+
*,
|
|
183
|
+
log_level: str = "INFO",
|
|
184
|
+
log_file: Path | str | None = None,
|
|
185
|
+
config_paths: list[Path | str] | None = None,
|
|
186
|
+
) -> None:
|
|
187
|
+
"""
|
|
188
|
+
Initialize the application.
|
|
189
|
+
|
|
190
|
+
This sets up logging, creates the Qt application, loads configuration,
|
|
191
|
+
and registers core services.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
log_level: Minimum log level.
|
|
195
|
+
log_file: Optional path to log file.
|
|
196
|
+
config_paths: Additional configuration file paths.
|
|
197
|
+
"""
|
|
198
|
+
if self._state != ApplicationState.UNINITIALIZED:
|
|
199
|
+
logger.warning("Application already initialized, skipping")
|
|
200
|
+
return
|
|
201
|
+
|
|
202
|
+
self._set_state(ApplicationState.INITIALIZING)
|
|
203
|
+
|
|
204
|
+
# Configure logging first
|
|
205
|
+
configure_logging(level=log_level, log_file=log_file)
|
|
206
|
+
logger.info("Initializing Lightfall application")
|
|
207
|
+
|
|
208
|
+
# NOTE: Windows AppUserModelID is set in main.py BEFORE any Qt imports.
|
|
209
|
+
# It must be called before COM/Qt initialization for taskbar icon to work.
|
|
210
|
+
|
|
211
|
+
# Create Qt application with Sentry exception capture
|
|
212
|
+
self._qt_app = QApplication.instance() # type: ignore[assignment]
|
|
213
|
+
if self._qt_app is None:
|
|
214
|
+
from lightfall.utils.sentry import create_sentry_application
|
|
215
|
+
|
|
216
|
+
self._qt_app = create_sentry_application(self._argv)
|
|
217
|
+
else:
|
|
218
|
+
logger.warning(
|
|
219
|
+
"QApplication already exists ({}), Sentry exception capture disabled",
|
|
220
|
+
type(self._qt_app).__name__,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
self._qt_app.setApplicationName("Lightfall")
|
|
224
|
+
self._qt_app.setOrganizationName("ALS")
|
|
225
|
+
self._qt_app.setOrganizationDomain("lbl.gov")
|
|
226
|
+
self._qt_app.setDesktopFileName("gov.lbl.als.lightfall")
|
|
227
|
+
|
|
228
|
+
# Set application icon
|
|
229
|
+
from lightfall.resources import get_app_icon
|
|
230
|
+
|
|
231
|
+
app_icon = get_app_icon()
|
|
232
|
+
if not app_icon.isNull():
|
|
233
|
+
self._qt_app.setWindowIcon(app_icon)
|
|
234
|
+
sizes = app_icon.availableSizes()
|
|
235
|
+
logger.info("App icon set on QApplication ({} sizes: {})", len(sizes), sizes)
|
|
236
|
+
else:
|
|
237
|
+
logger.warning("App icon is null, cannot set on QApplication")
|
|
238
|
+
|
|
239
|
+
# Register core services
|
|
240
|
+
self._register_core_services(config_paths)
|
|
241
|
+
|
|
242
|
+
self._set_state(ApplicationState.READY)
|
|
243
|
+
logger.info("Lightfall application initialized")
|
|
244
|
+
|
|
245
|
+
def _register_core_services(
|
|
246
|
+
self, config_paths: list[Path | str] | None = None
|
|
247
|
+
) -> None:
|
|
248
|
+
"""Register core application services."""
|
|
249
|
+
# Import here to avoid circular imports
|
|
250
|
+
from lightfall.config.manager import ConfigManager
|
|
251
|
+
|
|
252
|
+
# Register ConfigManager
|
|
253
|
+
self._services.register(
|
|
254
|
+
ConfigManager,
|
|
255
|
+
lambda: ConfigManager(extra_paths=config_paths),
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
# Register IPC trust manager and service
|
|
259
|
+
trust_manager = TrustManager()
|
|
260
|
+
self._services.register(TrustManager, lambda: trust_manager)
|
|
261
|
+
self._services.register(
|
|
262
|
+
IPCService, lambda: self._create_ipc_service(trust_manager)
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
logger.debug("Core services registered")
|
|
266
|
+
|
|
267
|
+
def set_main_window(self, window: QMainWindow) -> None:
|
|
268
|
+
"""
|
|
269
|
+
Set the main application window.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
window: The main window widget.
|
|
273
|
+
"""
|
|
274
|
+
self._main_window = window
|
|
275
|
+
logger.debug("Main window set: {}", window.__class__.__name__)
|
|
276
|
+
|
|
277
|
+
# ------------------------------------------------------------------
|
|
278
|
+
# IPC helpers
|
|
279
|
+
# ------------------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
def _create_ipc_service(self, trust_manager: TrustManager) -> IPCService:
|
|
282
|
+
"""Factory that builds a configured :class:`IPCService`."""
|
|
283
|
+
from lightfall.ui.preferences.manager import PreferencesManager
|
|
284
|
+
|
|
285
|
+
prefs = PreferencesManager.get_instance()
|
|
286
|
+
nats_url = prefs.get("ipc_nats_url", "")
|
|
287
|
+
topic_prefix = prefs.get("ipc_topic_prefix", "als.7011")
|
|
288
|
+
svc = IPCService(nats_url=nats_url, topic_prefix=topic_prefix)
|
|
289
|
+
svc.set_trust_manager(trust_manager)
|
|
290
|
+
svc.register_meta_endpoints()
|
|
291
|
+
return svc
|
|
292
|
+
|
|
293
|
+
def _start_ipc(self) -> None:
|
|
294
|
+
"""Start the IPC service and register the auth handler."""
|
|
295
|
+
ipc = self._services.get(IPCService)
|
|
296
|
+
ipc.start()
|
|
297
|
+
ipc.register_action(
|
|
298
|
+
"auth.request",
|
|
299
|
+
self._handle_ipc_auth_request,
|
|
300
|
+
description="Trust handshake + token sharing",
|
|
301
|
+
)
|
|
302
|
+
logger.info("IPC service started")
|
|
303
|
+
|
|
304
|
+
# Wire engine signals → IPC events & plan commands
|
|
305
|
+
try:
|
|
306
|
+
self._wire_engine_ipc()
|
|
307
|
+
self._wire_plan_commands()
|
|
308
|
+
except Exception:
|
|
309
|
+
logger.exception("Failed to wire engine IPC (engine may not be initialized yet)")
|
|
310
|
+
|
|
311
|
+
# Wire logbook + agent IPC commands
|
|
312
|
+
try:
|
|
313
|
+
self._wire_logbook_ipc()
|
|
314
|
+
except Exception:
|
|
315
|
+
logger.exception("Failed to wire logbook IPC")
|
|
316
|
+
|
|
317
|
+
try:
|
|
318
|
+
self._wire_agent_ipc()
|
|
319
|
+
except Exception:
|
|
320
|
+
logger.exception("Failed to wire agent IPC")
|
|
321
|
+
|
|
322
|
+
def _wire_engine_ipc(self) -> None:
|
|
323
|
+
"""Connect engine signals to IPC events.
|
|
324
|
+
|
|
325
|
+
Publishes ``runs.new``, ``runs.complete``, and ``state.engine``
|
|
326
|
+
events so that external IPC clients can track run lifecycle and
|
|
327
|
+
engine state changes.
|
|
328
|
+
"""
|
|
329
|
+
from lightfall.acquire.engine import get_engine
|
|
330
|
+
|
|
331
|
+
engine = get_engine()
|
|
332
|
+
ipc = self._services.get(IPCService)
|
|
333
|
+
|
|
334
|
+
current_run: dict[str, str] = {}
|
|
335
|
+
|
|
336
|
+
def on_output(name: str, doc: dict) -> None:
|
|
337
|
+
if name == "start":
|
|
338
|
+
run_id = doc.get("uid", "")
|
|
339
|
+
plan_name = doc.get("plan_name", "unknown")
|
|
340
|
+
current_run["uid"] = run_id
|
|
341
|
+
current_run["plan_name"] = plan_name
|
|
342
|
+
ipc.publish(
|
|
343
|
+
ipc.topic("runs.new"),
|
|
344
|
+
{"run_id": run_id, "plan_name": plan_name},
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
def on_finish() -> None:
|
|
348
|
+
run_id = current_run.get("uid", "")
|
|
349
|
+
ipc.publish(
|
|
350
|
+
ipc.topic("runs.complete"),
|
|
351
|
+
{"run_id": run_id, "exit_status": "success"},
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
def on_abort() -> None:
|
|
355
|
+
run_id = current_run.get("uid", "")
|
|
356
|
+
ipc.publish(
|
|
357
|
+
ipc.topic("runs.complete"),
|
|
358
|
+
{"run_id": run_id, "exit_status": "abort"},
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
def on_exception(exc: Exception) -> None:
|
|
362
|
+
run_id = current_run.get("uid", "")
|
|
363
|
+
ipc.publish(
|
|
364
|
+
ipc.topic("runs.complete"),
|
|
365
|
+
{"run_id": run_id, "exit_status": "error"},
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
def on_state_changed(state: str) -> None:
|
|
369
|
+
ipc.publish(ipc.topic("state.engine"), {"state": state})
|
|
370
|
+
|
|
371
|
+
engine.sigOutput.connect(on_output)
|
|
372
|
+
engine.sigFinish.connect(on_finish)
|
|
373
|
+
engine.sigAbort.connect(on_abort)
|
|
374
|
+
engine.sigException.connect(on_exception)
|
|
375
|
+
engine.sigStateChanged.connect(on_state_changed)
|
|
376
|
+
|
|
377
|
+
# Register outbound events in catalog
|
|
378
|
+
ipc.register_event(
|
|
379
|
+
"runs.new",
|
|
380
|
+
description="Fired when a new run starts",
|
|
381
|
+
schema={"run_id": "str", "plan_name": "str"},
|
|
382
|
+
)
|
|
383
|
+
ipc.register_event(
|
|
384
|
+
"runs.complete",
|
|
385
|
+
description="Fired when a run finishes",
|
|
386
|
+
schema={"run_id": "str", "exit_status": "str"},
|
|
387
|
+
)
|
|
388
|
+
ipc.register_event(
|
|
389
|
+
"state.engine",
|
|
390
|
+
description="Engine state change",
|
|
391
|
+
schema={"state": "str"},
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
logger.debug("Engine → IPC event wiring complete")
|
|
395
|
+
|
|
396
|
+
def _wire_plan_commands(self) -> None:
|
|
397
|
+
"""Register IPC commands for plan execution.
|
|
398
|
+
|
|
399
|
+
Registers ``commands.plan.run`` and ``commands.plan.abort`` so that
|
|
400
|
+
external IPC clients can submit plans and abort the active run.
|
|
401
|
+
"""
|
|
402
|
+
from lightfall.acquire.engine import get_engine
|
|
403
|
+
|
|
404
|
+
engine = get_engine()
|
|
405
|
+
ipc = self._services.get(IPCService)
|
|
406
|
+
|
|
407
|
+
def handle_plan_run(subject: str, data: dict, reply: str | None) -> None:
|
|
408
|
+
from lightfall.acquire.plans.registry import get_registry
|
|
409
|
+
|
|
410
|
+
plan_name = data.get("plan_name")
|
|
411
|
+
params = data.get("params", {})
|
|
412
|
+
if not plan_name:
|
|
413
|
+
if reply:
|
|
414
|
+
ipc.reply(reply, {"error": True, "message": "plan_name is required"})
|
|
415
|
+
return
|
|
416
|
+
|
|
417
|
+
registry = get_registry()
|
|
418
|
+
plan_info = registry.get_plan(plan_name)
|
|
419
|
+
if plan_info is None:
|
|
420
|
+
if reply:
|
|
421
|
+
ipc.reply(
|
|
422
|
+
reply,
|
|
423
|
+
{"error": True, "message": f"Plan '{plan_name}' not found"},
|
|
424
|
+
)
|
|
425
|
+
return
|
|
426
|
+
|
|
427
|
+
try:
|
|
428
|
+
plan_generator = plan_info.func(**params)
|
|
429
|
+
proc_id = engine.submit(plan_generator, name=plan_name)
|
|
430
|
+
if reply:
|
|
431
|
+
ipc.reply(
|
|
432
|
+
reply,
|
|
433
|
+
{"status": "submitted", "plan_name": plan_name, "procedure_id": proc_id},
|
|
434
|
+
)
|
|
435
|
+
except Exception as exc:
|
|
436
|
+
if reply:
|
|
437
|
+
ipc.reply(reply, {"error": True, "message": str(exc)})
|
|
438
|
+
|
|
439
|
+
def handle_plan_abort(subject: str, data: dict, reply: str | None) -> None:
|
|
440
|
+
reason = data.get("reason", "")
|
|
441
|
+
try:
|
|
442
|
+
engine.abort(reason=reason)
|
|
443
|
+
if reply:
|
|
444
|
+
ipc.reply(reply, {"status": "abort_requested"})
|
|
445
|
+
except Exception as exc:
|
|
446
|
+
if reply:
|
|
447
|
+
ipc.reply(reply, {"error": True, "message": str(exc)})
|
|
448
|
+
|
|
449
|
+
ipc.register_action(
|
|
450
|
+
"commands.plan.run",
|
|
451
|
+
handle_plan_run,
|
|
452
|
+
description="Submit a plan to the BlueskyEngine",
|
|
453
|
+
schema={"plan_name": "str", "params": "dict"},
|
|
454
|
+
)
|
|
455
|
+
ipc.register_action(
|
|
456
|
+
"commands.plan.abort",
|
|
457
|
+
handle_plan_abort,
|
|
458
|
+
description="Abort the active run",
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
logger.debug("Plan IPC commands registered")
|
|
462
|
+
|
|
463
|
+
def _wire_logbook_ipc(self) -> None:
|
|
464
|
+
"""Register IPC commands for logbook entry creation.
|
|
465
|
+
|
|
466
|
+
Registers ``commands.logbook.add`` so that external IPC clients
|
|
467
|
+
can create logbook entries and fragments via the local-first
|
|
468
|
+
:class:`LogbookClient`.
|
|
469
|
+
"""
|
|
470
|
+
from lightfall.logbook.client import LogbookClient
|
|
471
|
+
|
|
472
|
+
ipc = self._services.get(IPCService)
|
|
473
|
+
|
|
474
|
+
def handle_logbook_add(subject: str, data: dict, reply: str | None) -> None:
|
|
475
|
+
title = data.get("title")
|
|
476
|
+
content = data.get("content")
|
|
477
|
+
tags = data.get("tags", [])
|
|
478
|
+
|
|
479
|
+
client = LogbookClient.get_instance()
|
|
480
|
+
|
|
481
|
+
# Determine the active logbook ID from the current user
|
|
482
|
+
user_id: str | None = None
|
|
483
|
+
try:
|
|
484
|
+
from lightfall.auth.session import SessionManager
|
|
485
|
+
|
|
486
|
+
sm = SessionManager.get_instance()
|
|
487
|
+
user = sm.current_user
|
|
488
|
+
if user and user.username:
|
|
489
|
+
user_id = user.username
|
|
490
|
+
except Exception:
|
|
491
|
+
pass
|
|
492
|
+
|
|
493
|
+
if not user_id:
|
|
494
|
+
if reply:
|
|
495
|
+
ipc.reply(reply, {"error": True, "message": "No active logbook (no user)"})
|
|
496
|
+
return
|
|
497
|
+
|
|
498
|
+
try:
|
|
499
|
+
logbook_id = client.get_or_create_logbook(user_id)
|
|
500
|
+
except Exception as exc:
|
|
501
|
+
if reply:
|
|
502
|
+
ipc.reply(reply, {"error": True, "message": str(exc)})
|
|
503
|
+
return
|
|
504
|
+
|
|
505
|
+
entry_id = client.create_entry(logbook_id, title=title, tags=tags)
|
|
506
|
+
if content:
|
|
507
|
+
client.add_fragment(entry_id, content=content)
|
|
508
|
+
|
|
509
|
+
if reply:
|
|
510
|
+
ipc.reply(reply, {"status": "created", "entry_id": entry_id})
|
|
511
|
+
|
|
512
|
+
ipc.register_action(
|
|
513
|
+
"commands.logbook.add",
|
|
514
|
+
handle_logbook_add,
|
|
515
|
+
description="Create a logbook entry with optional content fragment",
|
|
516
|
+
schema={"title": "str", "content": "str (optional)", "tags": "list[str]"},
|
|
517
|
+
)
|
|
518
|
+
|
|
519
|
+
logger.debug("Logbook IPC commands registered")
|
|
520
|
+
|
|
521
|
+
def _wire_agent_ipc(self) -> None:
|
|
522
|
+
"""Register IPC commands for the Claude agent.
|
|
523
|
+
|
|
524
|
+
Registers ``commands.agent.message`` so that external IPC clients
|
|
525
|
+
can send messages to the :class:`QtClaudeAgent`.
|
|
526
|
+
"""
|
|
527
|
+
ipc = self._services.get(IPCService)
|
|
528
|
+
|
|
529
|
+
def handle_agent_message(subject: str, data: dict, reply: str | None) -> None:
|
|
530
|
+
message = data.get("message", "")
|
|
531
|
+
if not message:
|
|
532
|
+
if reply:
|
|
533
|
+
ipc.reply(reply, {"error": True, "message": "message is required"})
|
|
534
|
+
return
|
|
535
|
+
|
|
536
|
+
# Find the agent via the main window widget tree
|
|
537
|
+
agent = None
|
|
538
|
+
if self._main_window:
|
|
539
|
+
from lightfall.claude.widget import ClaudeAssistantWidget
|
|
540
|
+
|
|
541
|
+
widget = self._main_window.findChild(ClaudeAssistantWidget)
|
|
542
|
+
if widget and hasattr(widget, "agent"):
|
|
543
|
+
agent = widget.agent
|
|
544
|
+
|
|
545
|
+
if agent is None:
|
|
546
|
+
if reply:
|
|
547
|
+
ipc.reply(reply, {"error": True, "message": "Claude agent not available"})
|
|
548
|
+
return
|
|
549
|
+
|
|
550
|
+
agent.query_sync(message)
|
|
551
|
+
if reply:
|
|
552
|
+
ipc.reply(reply, {"status": "sent"})
|
|
553
|
+
|
|
554
|
+
ipc.register_action(
|
|
555
|
+
"commands.agent.message",
|
|
556
|
+
handle_agent_message,
|
|
557
|
+
description="Send a message to the Claude agent",
|
|
558
|
+
schema={"message": "str"},
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
logger.debug("Agent IPC commands registered")
|
|
562
|
+
|
|
563
|
+
def _handle_ipc_auth_request(
|
|
564
|
+
self, subject: str, data: dict, reply: str | None
|
|
565
|
+
) -> None:
|
|
566
|
+
"""Handle an incoming IPC auth handshake request."""
|
|
567
|
+
if not reply:
|
|
568
|
+
return
|
|
569
|
+
|
|
570
|
+
ipc = self._services.get(IPCService)
|
|
571
|
+
app_name = data.get("app_name", "unknown")
|
|
572
|
+
app_version = data.get("app_version", "")
|
|
573
|
+
|
|
574
|
+
state = ipc.evaluate_trust(app_name)
|
|
575
|
+
|
|
576
|
+
if state == TrustState.APPROVED:
|
|
577
|
+
session = self._get_current_session()
|
|
578
|
+
tiled_url = self._get_tiled_url()
|
|
579
|
+
ipc.reply(
|
|
580
|
+
reply,
|
|
581
|
+
ipc.build_auth_response(
|
|
582
|
+
approved=True, session=session, tiled_url=tiled_url
|
|
583
|
+
),
|
|
584
|
+
)
|
|
585
|
+
return
|
|
586
|
+
|
|
587
|
+
if state == TrustState.DENIED:
|
|
588
|
+
ipc.reply(reply, ipc.build_auth_response(approved=False))
|
|
589
|
+
return
|
|
590
|
+
|
|
591
|
+
# Unknown app — show dialog with 60 s timeout
|
|
592
|
+
if self._auth_dialog_active:
|
|
593
|
+
ipc.reply(reply, ipc.build_auth_response(approved=False, reason="busy"))
|
|
594
|
+
return
|
|
595
|
+
|
|
596
|
+
trust = self._services.get(TrustManager)
|
|
597
|
+
self._auth_dialog_active = True
|
|
598
|
+
try:
|
|
599
|
+
dialog = TrustDialog(app_name, app_version, parent=self._main_window)
|
|
600
|
+
from PySide6.QtCore import QTimer
|
|
601
|
+
|
|
602
|
+
timer = QTimer()
|
|
603
|
+
timer.setSingleShot(True)
|
|
604
|
+
timer.timeout.connect(dialog.reject)
|
|
605
|
+
timer.start(60_000)
|
|
606
|
+
|
|
607
|
+
if dialog.exec() == TrustDialog.DialogCode.Accepted:
|
|
608
|
+
trust.approve(app_name)
|
|
609
|
+
session = self._get_current_session()
|
|
610
|
+
tiled_url = self._get_tiled_url()
|
|
611
|
+
ipc.reply(
|
|
612
|
+
reply,
|
|
613
|
+
ipc.build_auth_response(
|
|
614
|
+
approved=True, session=session, tiled_url=tiled_url
|
|
615
|
+
),
|
|
616
|
+
)
|
|
617
|
+
else:
|
|
618
|
+
trust.deny(app_name)
|
|
619
|
+
reason = "denied" if timer.isActive() else "timeout"
|
|
620
|
+
ipc.reply(
|
|
621
|
+
reply,
|
|
622
|
+
ipc.build_auth_response(approved=False, reason=reason),
|
|
623
|
+
)
|
|
624
|
+
finally:
|
|
625
|
+
self._auth_dialog_active = False
|
|
626
|
+
|
|
627
|
+
def _get_current_session(self):
|
|
628
|
+
"""Return the current :class:`Session` or ``None``."""
|
|
629
|
+
try:
|
|
630
|
+
from lightfall.auth.session import SessionManager
|
|
631
|
+
|
|
632
|
+
sm = SessionManager.get_instance()
|
|
633
|
+
return sm.session
|
|
634
|
+
except Exception:
|
|
635
|
+
return None
|
|
636
|
+
|
|
637
|
+
def _get_tiled_url(self) -> str:
|
|
638
|
+
"""Return the configured Tiled server URL."""
|
|
639
|
+
try:
|
|
640
|
+
from lightfall.ui.preferences.manager import PreferencesManager
|
|
641
|
+
|
|
642
|
+
return PreferencesManager.get_instance().get("tiled_url", "")
|
|
643
|
+
except Exception:
|
|
644
|
+
return ""
|
|
645
|
+
|
|
646
|
+
def run(self) -> int:
|
|
647
|
+
"""
|
|
648
|
+
Run the application event loop.
|
|
649
|
+
|
|
650
|
+
Returns:
|
|
651
|
+
The application exit code.
|
|
652
|
+
"""
|
|
653
|
+
if self._state not in (ApplicationState.READY, ApplicationState.RUNNING):
|
|
654
|
+
raise RuntimeError(
|
|
655
|
+
f"Cannot run application in state {self._state.name}. "
|
|
656
|
+
"Call initialize() first."
|
|
657
|
+
)
|
|
658
|
+
|
|
659
|
+
self._set_state(ApplicationState.RUNNING)
|
|
660
|
+
|
|
661
|
+
# Show main window if set
|
|
662
|
+
if self._main_window:
|
|
663
|
+
self._main_window.show()
|
|
664
|
+
|
|
665
|
+
# Start IPC service (after main window is visible)
|
|
666
|
+
try:
|
|
667
|
+
self._start_ipc()
|
|
668
|
+
except Exception:
|
|
669
|
+
logger.exception("Failed to start IPC service")
|
|
670
|
+
|
|
671
|
+
logger.info("Starting application event loop")
|
|
672
|
+
exit_code = self._qt_app.exec()
|
|
673
|
+
|
|
674
|
+
self._shutdown()
|
|
675
|
+
return exit_code
|
|
676
|
+
|
|
677
|
+
def _shutdown(self) -> None:
|
|
678
|
+
"""Perform graceful shutdown."""
|
|
679
|
+
if self._state == ApplicationState.TERMINATED:
|
|
680
|
+
return
|
|
681
|
+
|
|
682
|
+
self._set_state(ApplicationState.SHUTTING_DOWN)
|
|
683
|
+
logger.info("Shutting down Lightfall application")
|
|
684
|
+
|
|
685
|
+
# Stop IPC service before clearing the registry
|
|
686
|
+
try:
|
|
687
|
+
ipc = self._services.get(IPCService, None)
|
|
688
|
+
if ipc:
|
|
689
|
+
ipc.stop()
|
|
690
|
+
except Exception:
|
|
691
|
+
logger.exception("Error stopping IPC service")
|
|
692
|
+
|
|
693
|
+
# Clear services
|
|
694
|
+
self._services.clear()
|
|
695
|
+
|
|
696
|
+
self._set_state(ApplicationState.TERMINATED)
|
|
697
|
+
logger.info("Lightfall application terminated")
|
|
698
|
+
|
|
699
|
+
def quit(self, exit_code: int = 0) -> None:
|
|
700
|
+
"""
|
|
701
|
+
Request application quit.
|
|
702
|
+
|
|
703
|
+
Args:
|
|
704
|
+
exit_code: Exit code to return from run().
|
|
705
|
+
"""
|
|
706
|
+
if self._qt_app:
|
|
707
|
+
self._qt_app.exit(exit_code)
|
|
708
|
+
|
|
709
|
+
def post_event(self, receiver: QObject, event: LFEvent) -> None:
|
|
710
|
+
"""
|
|
711
|
+
Post an NCS event to a receiver.
|
|
712
|
+
|
|
713
|
+
Args:
|
|
714
|
+
receiver: The object to receive the event.
|
|
715
|
+
event: The event to post.
|
|
716
|
+
"""
|
|
717
|
+
QCoreApplication.postEvent(receiver, event)
|
|
718
|
+
|
|
719
|
+
def broadcast_event(self, event: LFEvent) -> None:
|
|
720
|
+
"""
|
|
721
|
+
Broadcast an event to the application.
|
|
722
|
+
|
|
723
|
+
Args:
|
|
724
|
+
event: The event to broadcast.
|
|
725
|
+
"""
|
|
726
|
+
if self._qt_app:
|
|
727
|
+
QCoreApplication.postEvent(self._qt_app, event)
|