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,588 @@
|
|
|
1
|
+
"""Permission manager for coordinating tool approval between threads."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from PySide6.QtCore import QObject, QSettings, Signal
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PermissionManager(QObject):
|
|
11
|
+
"""
|
|
12
|
+
Manages tool permissions and user preferences.
|
|
13
|
+
|
|
14
|
+
This class coordinates between the background worker thread (running
|
|
15
|
+
async Claude operations) and the main Qt thread (displaying UI).
|
|
16
|
+
|
|
17
|
+
Thread Safety:
|
|
18
|
+
- permission_requested signal is emitted from worker thread
|
|
19
|
+
- Qt handles cross-thread signal delivery automatically
|
|
20
|
+
- respond() is called from main thread, uses call_soon_threadsafe
|
|
21
|
+
to wake the waiting coroutine
|
|
22
|
+
|
|
23
|
+
Signals:
|
|
24
|
+
permission_requested(str, str, dict): Emitted when a tool needs approval
|
|
25
|
+
(request_id, tool_name, tool_input)
|
|
26
|
+
auto_approvals_changed(set): Emitted when auto-approval list changes
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# Signals
|
|
30
|
+
permission_requested = Signal(str, str, dict) # request_id, tool_name, tool_input
|
|
31
|
+
question_requested = Signal(str, list) # request_id, questions
|
|
32
|
+
auto_approvals_changed = Signal(set) # Current set of auto-approved tools
|
|
33
|
+
|
|
34
|
+
# File-edit tools auto-approved when permission_mode == "acceptEdits"
|
|
35
|
+
# (matches the Claude Agent SDK's own acceptEdits semantics).
|
|
36
|
+
EDIT_TOOLS = frozenset({
|
|
37
|
+
"Edit", "edit",
|
|
38
|
+
"Write", "write",
|
|
39
|
+
"NotebookEdit", "notebook_edit",
|
|
40
|
+
"MultiEdit", "multi_edit",
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
# Default read-only tools that are auto-approved (whitelist design).
|
|
44
|
+
# Only tools explicitly listed here skip the approval prompt.
|
|
45
|
+
# Everything else requires user approval.
|
|
46
|
+
DEFAULT_AUTO_APPROVED = frozenset({
|
|
47
|
+
# Claude Code SDK built-in tools (read-only)
|
|
48
|
+
"Read", "read",
|
|
49
|
+
"Glob", "glob",
|
|
50
|
+
"Grep", "grep",
|
|
51
|
+
"LS", "ls",
|
|
52
|
+
"ListFiles", "list_files",
|
|
53
|
+
# Qt inspection tools
|
|
54
|
+
"mcp__qt__screenshot",
|
|
55
|
+
"mcp__qt__get_widget_tree",
|
|
56
|
+
"mcp__qt__find_widget",
|
|
57
|
+
"mcp__qt__get_recent_logs",
|
|
58
|
+
# Read-only Lightfall tools (per-plugin namespaces after SDK-native migration).
|
|
59
|
+
# LFCoreToolPlugin is manifest-driven via AgentRegistry (lightfall_core_tools server).
|
|
60
|
+
"mcp__lightfall_core_tools__lightfall_list_panels",
|
|
61
|
+
"mcp__lightfall_core_tools__lightfall_get_panel_info",
|
|
62
|
+
"mcp__lightfall_core_tools__lightfall_get_application_info",
|
|
63
|
+
"mcp__lightfall_core_tools__lightfall_set_emotion",
|
|
64
|
+
# device_tools agent
|
|
65
|
+
"mcp__device_tools__lightfall_list_devices",
|
|
66
|
+
"mcp__device_tools__lightfall_get_device",
|
|
67
|
+
"mcp__device_tools__lightfall_read_device",
|
|
68
|
+
"mcp__device_tools__lightfall_get_device_state",
|
|
69
|
+
"mcp__device_tools__lightfall_get_catalog_info",
|
|
70
|
+
# plan_tools agent
|
|
71
|
+
"mcp__plan_tools__lightfall_list_plans",
|
|
72
|
+
"mcp__plan_tools__lightfall_get_user_plan",
|
|
73
|
+
# engine_tools agent
|
|
74
|
+
"mcp__engine_tools__lightfall_get_run_status",
|
|
75
|
+
"mcp__engine_tools__lightfall_get_run_history",
|
|
76
|
+
"mcp__engine_tools__lightfall_get_scan_data",
|
|
77
|
+
"mcp__engine_tools__lightfall_get_last_run",
|
|
78
|
+
"mcp__engine_tools__lightfall_wait_for_idle",
|
|
79
|
+
# ipython_tools agent
|
|
80
|
+
"mcp__ipython_tools__lightfall_ipython_get_namespace",
|
|
81
|
+
# Bare names (fallback for SDK that may dispatch without prefix)
|
|
82
|
+
"lightfall_set_emotion",
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
parent: QObject | None = None,
|
|
88
|
+
permission_mode: str = "default",
|
|
89
|
+
):
|
|
90
|
+
"""
|
|
91
|
+
Initialize the permission manager.
|
|
92
|
+
|
|
93
|
+
Follows a whitelist design: all tools require approval by default.
|
|
94
|
+
Only tools explicitly added to the auto-approved set skip the prompt.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
parent: Parent QObject
|
|
98
|
+
permission_mode: SDK permission mode. When 'acceptEdits', file-edit
|
|
99
|
+
tools (Edit/Write/NotebookEdit/MultiEdit) are added to the
|
|
100
|
+
auto-approved set, matching the SDK's own acceptEdits semantics.
|
|
101
|
+
"""
|
|
102
|
+
super().__init__(parent)
|
|
103
|
+
|
|
104
|
+
# Auto-approved tools — whitelist only (configurable by external UI)
|
|
105
|
+
self._auto_approved: set[str] = set(self.DEFAULT_AUTO_APPROVED)
|
|
106
|
+
if permission_mode == "acceptEdits":
|
|
107
|
+
self._auto_approved |= self.EDIT_TOOLS
|
|
108
|
+
|
|
109
|
+
# User's "Always Allow" choices (session-based, can be persisted)
|
|
110
|
+
self._user_always_allowed: set[str] = set()
|
|
111
|
+
|
|
112
|
+
# Pending permission requests
|
|
113
|
+
self._pending_requests: dict[str, asyncio.Event] = {}
|
|
114
|
+
self._pending_loops: dict[str, asyncio.AbstractEventLoop] = {}
|
|
115
|
+
self._responses: dict[str, tuple[bool, str]] = {} # (allowed, message)
|
|
116
|
+
|
|
117
|
+
# Pending AskUserQuestion requests — parallel plumbing to permissions,
|
|
118
|
+
# but the response is a free-form answers dict, not a (bool, str) pair.
|
|
119
|
+
self._pending_questions: dict[str, asyncio.Event] = {}
|
|
120
|
+
self._pending_question_loops: dict[str, asyncio.AbstractEventLoop] = {}
|
|
121
|
+
self._question_responses: dict[str, dict[str, str] | None] = {}
|
|
122
|
+
|
|
123
|
+
# Load saved preferences (but validate against whitelist policy)
|
|
124
|
+
self._load_preferences()
|
|
125
|
+
|
|
126
|
+
# --- Public API for external configuration ---
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def auto_approved_tools(self) -> set[str]:
|
|
130
|
+
"""
|
|
131
|
+
Get current set of auto-approved tools.
|
|
132
|
+
|
|
133
|
+
This includes both configured auto-approved tools and
|
|
134
|
+
user's "Always Allow" choices.
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
Set of tool names that are auto-approved
|
|
138
|
+
"""
|
|
139
|
+
return self._auto_approved | self._user_always_allowed
|
|
140
|
+
|
|
141
|
+
def set_auto_approved(self, tools: set[str]) -> None:
|
|
142
|
+
"""
|
|
143
|
+
Set the base auto-approved tools.
|
|
144
|
+
|
|
145
|
+
Called by external settings UI to configure which tools
|
|
146
|
+
are auto-approved by default.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
tools: Set of tool names to auto-approve
|
|
150
|
+
"""
|
|
151
|
+
self._auto_approved = set(tools)
|
|
152
|
+
self.auto_approvals_changed.emit(self.auto_approved_tools)
|
|
153
|
+
|
|
154
|
+
def add_always_allowed(self, tool_name: str) -> None:
|
|
155
|
+
"""
|
|
156
|
+
Add tool to user's 'Always Allow' list (session-scoped).
|
|
157
|
+
|
|
158
|
+
This approval is cleared on next application startup
|
|
159
|
+
(whitelist design — tools must be re-approved each session).
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
tool_name: Name of tool to always allow
|
|
163
|
+
"""
|
|
164
|
+
self._user_always_allowed.add(tool_name)
|
|
165
|
+
self.auto_approvals_changed.emit(self.auto_approved_tools)
|
|
166
|
+
|
|
167
|
+
def remove_always_allowed(self, tool_name: str) -> None:
|
|
168
|
+
"""
|
|
169
|
+
Remove tool from user's 'Always Allow' list.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
tool_name: Name of tool to remove
|
|
173
|
+
"""
|
|
174
|
+
self._user_always_allowed.discard(tool_name)
|
|
175
|
+
self.auto_approvals_changed.emit(self.auto_approved_tools)
|
|
176
|
+
|
|
177
|
+
def clear_always_allowed(self) -> None:
|
|
178
|
+
"""Clear all user 'Always Allow' choices."""
|
|
179
|
+
self._user_always_allowed.clear()
|
|
180
|
+
self.auto_approvals_changed.emit(self.auto_approved_tools)
|
|
181
|
+
|
|
182
|
+
def is_auto_approved(self, tool_name: str) -> bool:
|
|
183
|
+
"""
|
|
184
|
+
Check if a tool is auto-approved.
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
tool_name: Name of the tool to check
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
True if tool is auto-approved
|
|
191
|
+
"""
|
|
192
|
+
return tool_name in self.auto_approved_tools
|
|
193
|
+
|
|
194
|
+
# --- Permission request handling ---
|
|
195
|
+
|
|
196
|
+
def cancel_all_pending(self) -> None:
|
|
197
|
+
"""Cancel all pending permission requests.
|
|
198
|
+
|
|
199
|
+
Denies all waiting requests, waking their coroutines.
|
|
200
|
+
Called when the user cancels a query so pending approval
|
|
201
|
+
dialogs don't block the cancellation.
|
|
202
|
+
"""
|
|
203
|
+
for request_id in list(self._pending_requests.keys()):
|
|
204
|
+
self._responses[request_id] = (False, "Query cancelled by user")
|
|
205
|
+
loop = self._pending_loops.get(request_id)
|
|
206
|
+
event = self._pending_requests.get(request_id)
|
|
207
|
+
if loop and event:
|
|
208
|
+
try:
|
|
209
|
+
loop.call_soon_threadsafe(event.set)
|
|
210
|
+
except RuntimeError:
|
|
211
|
+
pass
|
|
212
|
+
# Also wake pending question requests with a cancel response.
|
|
213
|
+
for request_id in list(self._pending_questions.keys()):
|
|
214
|
+
self._question_responses[request_id] = None
|
|
215
|
+
loop = self._pending_question_loops.get(request_id)
|
|
216
|
+
event = self._pending_questions.get(request_id)
|
|
217
|
+
if loop and event:
|
|
218
|
+
try:
|
|
219
|
+
loop.call_soon_threadsafe(event.set)
|
|
220
|
+
except RuntimeError:
|
|
221
|
+
pass
|
|
222
|
+
|
|
223
|
+
async def request_permission(
|
|
224
|
+
self,
|
|
225
|
+
tool_name: str,
|
|
226
|
+
tool_input: dict[str, Any]
|
|
227
|
+
) -> tuple[bool, str]:
|
|
228
|
+
"""
|
|
229
|
+
Request permission for a tool use.
|
|
230
|
+
|
|
231
|
+
Called from the worker thread's async context. This method:
|
|
232
|
+
1. Checks if tool is auto-approved
|
|
233
|
+
2. If not, emits signal to UI and waits for response
|
|
234
|
+
3. Returns the permission result
|
|
235
|
+
|
|
236
|
+
Args:
|
|
237
|
+
tool_name: Name of the tool requesting permission
|
|
238
|
+
tool_input: The tool's input parameters
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
Tuple of (allowed: bool, message: str)
|
|
242
|
+
"""
|
|
243
|
+
# Check auto-approval first
|
|
244
|
+
if self.is_auto_approved(tool_name):
|
|
245
|
+
return (True, "")
|
|
246
|
+
|
|
247
|
+
# Generate request ID
|
|
248
|
+
request_id = str(uuid.uuid4())
|
|
249
|
+
|
|
250
|
+
# Create event for waiting
|
|
251
|
+
loop = asyncio.get_running_loop()
|
|
252
|
+
event = asyncio.Event()
|
|
253
|
+
|
|
254
|
+
self._pending_requests[request_id] = event
|
|
255
|
+
self._pending_loops[request_id] = loop
|
|
256
|
+
|
|
257
|
+
# Emit signal to main thread (Qt handles cross-thread delivery)
|
|
258
|
+
self.permission_requested.emit(request_id, tool_name, tool_input)
|
|
259
|
+
|
|
260
|
+
# Wait for response with timeout to prevent hanging if CLI dies
|
|
261
|
+
try:
|
|
262
|
+
await asyncio.wait_for(event.wait(), timeout=300) # 5 min timeout
|
|
263
|
+
except TimeoutError:
|
|
264
|
+
# Clean up and deny — user didn't respond in time
|
|
265
|
+
self._pending_requests.pop(request_id, None)
|
|
266
|
+
self._pending_loops.pop(request_id, None)
|
|
267
|
+
self._responses.pop(request_id, None)
|
|
268
|
+
return (False, "Permission request timed out")
|
|
269
|
+
|
|
270
|
+
# Clean up and return result
|
|
271
|
+
self._pending_requests.pop(request_id, None)
|
|
272
|
+
self._pending_loops.pop(request_id, None)
|
|
273
|
+
|
|
274
|
+
return self._responses.pop(request_id, (False, "No response received"))
|
|
275
|
+
|
|
276
|
+
def respond(
|
|
277
|
+
self,
|
|
278
|
+
request_id: str,
|
|
279
|
+
allowed: bool,
|
|
280
|
+
always: bool = False,
|
|
281
|
+
message: str = ""
|
|
282
|
+
) -> None:
|
|
283
|
+
"""
|
|
284
|
+
Respond to a permission request.
|
|
285
|
+
|
|
286
|
+
Called from the main thread when user clicks a button.
|
|
287
|
+
|
|
288
|
+
Args:
|
|
289
|
+
request_id: The request ID to respond to
|
|
290
|
+
allowed: Whether permission is granted
|
|
291
|
+
always: If True and allowed, add tool to always-allowed list
|
|
292
|
+
message: Optional message (used for deny reason)
|
|
293
|
+
"""
|
|
294
|
+
if request_id not in self._pending_requests:
|
|
295
|
+
# Request already handled or expired
|
|
296
|
+
return
|
|
297
|
+
|
|
298
|
+
# Store response
|
|
299
|
+
self._responses[request_id] = (allowed, message)
|
|
300
|
+
|
|
301
|
+
# If "Always Allow" was selected, add to the list
|
|
302
|
+
# We need to get the tool name - it's not directly available here,
|
|
303
|
+
# so we rely on the caller to handle that separately via add_always_allowed()
|
|
304
|
+
|
|
305
|
+
# Wake up the waiting coroutine (thread-safe)
|
|
306
|
+
loop = self._pending_loops.get(request_id)
|
|
307
|
+
event = self._pending_requests.get(request_id)
|
|
308
|
+
|
|
309
|
+
if loop and event:
|
|
310
|
+
try:
|
|
311
|
+
loop.call_soon_threadsafe(event.set)
|
|
312
|
+
except RuntimeError:
|
|
313
|
+
# Loop already closed — clean up silently
|
|
314
|
+
self._pending_requests.pop(request_id, None)
|
|
315
|
+
self._pending_loops.pop(request_id, None)
|
|
316
|
+
self._responses.pop(request_id, None)
|
|
317
|
+
|
|
318
|
+
async def request_question(
|
|
319
|
+
self, questions: list[dict[str, Any]]
|
|
320
|
+
) -> tuple[bool, dict[str, str]]:
|
|
321
|
+
"""Request the user to answer one or more multi-choice questions.
|
|
322
|
+
|
|
323
|
+
Emits ``question_requested`` and waits for a response via
|
|
324
|
+
``respond_to_question``. Returns ``(answered, answers)`` where
|
|
325
|
+
``answered=False`` means the user cancelled.
|
|
326
|
+
"""
|
|
327
|
+
request_id = str(uuid.uuid4())
|
|
328
|
+
loop = asyncio.get_running_loop()
|
|
329
|
+
event = asyncio.Event()
|
|
330
|
+
|
|
331
|
+
self._pending_questions[request_id] = event
|
|
332
|
+
self._pending_question_loops[request_id] = loop
|
|
333
|
+
|
|
334
|
+
self.question_requested.emit(request_id, questions)
|
|
335
|
+
|
|
336
|
+
try:
|
|
337
|
+
await asyncio.wait_for(event.wait(), timeout=300)
|
|
338
|
+
except TimeoutError:
|
|
339
|
+
self._pending_questions.pop(request_id, None)
|
|
340
|
+
self._pending_question_loops.pop(request_id, None)
|
|
341
|
+
self._question_responses.pop(request_id, None)
|
|
342
|
+
return (False, {})
|
|
343
|
+
|
|
344
|
+
self._pending_questions.pop(request_id, None)
|
|
345
|
+
self._pending_question_loops.pop(request_id, None)
|
|
346
|
+
answers = self._question_responses.pop(request_id, None)
|
|
347
|
+
if answers is None:
|
|
348
|
+
return (False, {})
|
|
349
|
+
return (True, answers)
|
|
350
|
+
|
|
351
|
+
def respond_to_question(
|
|
352
|
+
self, request_id: str, answers: dict[str, str] | None
|
|
353
|
+
) -> None:
|
|
354
|
+
"""Provide answers to a pending question request.
|
|
355
|
+
|
|
356
|
+
Pass ``answers=None`` to indicate the user cancelled.
|
|
357
|
+
"""
|
|
358
|
+
if request_id not in self._pending_questions:
|
|
359
|
+
return
|
|
360
|
+
self._question_responses[request_id] = answers
|
|
361
|
+
loop = self._pending_question_loops.get(request_id)
|
|
362
|
+
event = self._pending_questions.get(request_id)
|
|
363
|
+
if loop and event:
|
|
364
|
+
try:
|
|
365
|
+
loop.call_soon_threadsafe(event.set)
|
|
366
|
+
except RuntimeError:
|
|
367
|
+
self._pending_questions.pop(request_id, None)
|
|
368
|
+
self._pending_question_loops.pop(request_id, None)
|
|
369
|
+
self._question_responses.pop(request_id, None)
|
|
370
|
+
|
|
371
|
+
def has_pending_request(self, request_id: str) -> bool:
|
|
372
|
+
"""
|
|
373
|
+
Check if a request is still pending.
|
|
374
|
+
|
|
375
|
+
Args:
|
|
376
|
+
request_id: The request ID to check
|
|
377
|
+
|
|
378
|
+
Returns:
|
|
379
|
+
True if request is still pending
|
|
380
|
+
"""
|
|
381
|
+
return request_id in self._pending_requests
|
|
382
|
+
|
|
383
|
+
# --- Persistence ---
|
|
384
|
+
|
|
385
|
+
def save_preferences(self) -> None:
|
|
386
|
+
"""Save user preferences to QSettings.
|
|
387
|
+
|
|
388
|
+
Note: Under the whitelist design, persisted always-allowed tools
|
|
389
|
+
are cleared on next startup. This save is only useful for
|
|
390
|
+
within-session widget recreation (e.g., panel re-initialization).
|
|
391
|
+
"""
|
|
392
|
+
settings = QSettings("ncs", "pyside-claude")
|
|
393
|
+
settings.setValue(
|
|
394
|
+
"permission/user_always_allowed",
|
|
395
|
+
list(self._user_always_allowed)
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
def _load_preferences(self) -> None:
|
|
399
|
+
"""Load user preferences from QSettings.
|
|
400
|
+
|
|
401
|
+
Follows whitelist design: persisted "Always Allow" choices are
|
|
402
|
+
cleared on startup. Users must re-approve tools each session
|
|
403
|
+
for security. Only the base auto-approved set (read-only Qt
|
|
404
|
+
introspection tools) persists across sessions.
|
|
405
|
+
"""
|
|
406
|
+
# Clear any previously persisted always-allowed tools.
|
|
407
|
+
# This ensures a clean slate each session (whitelist design).
|
|
408
|
+
settings = QSettings("ncs", "pyside-claude")
|
|
409
|
+
settings.remove("permission/user_always_allowed")
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def create_pre_tool_use_hook(
|
|
413
|
+
permission_manager: PermissionManager,
|
|
414
|
+
require_approval: bool = True,
|
|
415
|
+
):
|
|
416
|
+
"""
|
|
417
|
+
Create a PreToolUse hook callback for the Claude Agent SDK.
|
|
418
|
+
|
|
419
|
+
This hook fires before any tool is executed, providing robust
|
|
420
|
+
permission interception for all tool types including MCP tools.
|
|
421
|
+
|
|
422
|
+
Args:
|
|
423
|
+
permission_manager: The PermissionManager instance
|
|
424
|
+
require_approval: When True, gate normal tools through the
|
|
425
|
+
approval UI. When False (e.g. ``permission_mode=
|
|
426
|
+
bypassPermissions``), skip the gate for normal tools but
|
|
427
|
+
still force ``AskUserQuestion`` through ``can_use_tool``
|
|
428
|
+
so the question UI fires.
|
|
429
|
+
|
|
430
|
+
Returns:
|
|
431
|
+
Async hook callback compatible with HookMatcher.hooks
|
|
432
|
+
"""
|
|
433
|
+
|
|
434
|
+
async def pre_tool_use_hook(
|
|
435
|
+
hook_input,
|
|
436
|
+
tool_use_id,
|
|
437
|
+
context,
|
|
438
|
+
):
|
|
439
|
+
tool_name = hook_input.get("tool_name", "")
|
|
440
|
+
tool_input = hook_input.get("tool_input", {})
|
|
441
|
+
|
|
442
|
+
# AskUserQuestion needs to be handled in can_use_tool (which can
|
|
443
|
+
# return updated_input with the user's answers); a hook can only
|
|
444
|
+
# allow / deny / ask. Returning empty dict ("no decision") lets
|
|
445
|
+
# the SDK fall back to its CLI default — which in headless mode
|
|
446
|
+
# silently dismisses the question without ever asking us.
|
|
447
|
+
# Explicitly returning permissionDecision="ask" forces the SDK
|
|
448
|
+
# to route through can_use_tool where we render the question UI.
|
|
449
|
+
# This applies regardless of ``require_approval`` — the question
|
|
450
|
+
# tool is interactive by design.
|
|
451
|
+
if tool_name == "AskUserQuestion":
|
|
452
|
+
return {
|
|
453
|
+
"hookSpecificOutput": {
|
|
454
|
+
"hookEventName": "PreToolUse",
|
|
455
|
+
"permissionDecision": "ask",
|
|
456
|
+
},
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
# When approvals are bypassed, defer to the CLI's permission
|
|
460
|
+
# rules for all other tools — don't interpose our approval UI.
|
|
461
|
+
if not require_approval:
|
|
462
|
+
return {}
|
|
463
|
+
|
|
464
|
+
# Check auto-approval first
|
|
465
|
+
if permission_manager.is_auto_approved(tool_name):
|
|
466
|
+
return {} # Empty dict = allow (no override)
|
|
467
|
+
|
|
468
|
+
# Request permission from UI with error handling
|
|
469
|
+
try:
|
|
470
|
+
allowed, message = await permission_manager.request_permission(
|
|
471
|
+
tool_name, tool_input
|
|
472
|
+
)
|
|
473
|
+
except Exception as e:
|
|
474
|
+
# If permission request fails (e.g., event loop dying),
|
|
475
|
+
# deny to be safe rather than crashing
|
|
476
|
+
return {
|
|
477
|
+
"hookSpecificOutput": {
|
|
478
|
+
"hookEventName": "PreToolUse",
|
|
479
|
+
"permissionDecision": "deny",
|
|
480
|
+
"permissionDecisionReason": f"Permission system error: {e}",
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if allowed:
|
|
485
|
+
return {} # Allow
|
|
486
|
+
else:
|
|
487
|
+
return {
|
|
488
|
+
"hookSpecificOutput": {
|
|
489
|
+
"hookEventName": "PreToolUse",
|
|
490
|
+
"permissionDecision": "deny",
|
|
491
|
+
"permissionDecisionReason": message or "User denied permission",
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return pre_tool_use_hook
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def create_can_use_tool_callback(
|
|
499
|
+
permission_manager: PermissionManager,
|
|
500
|
+
require_approval: bool = True,
|
|
501
|
+
):
|
|
502
|
+
"""
|
|
503
|
+
Create a can_use_tool callback for the Claude Agent SDK.
|
|
504
|
+
|
|
505
|
+
This factory function creates an async callback that integrates
|
|
506
|
+
with the PermissionManager for UI-based approval.
|
|
507
|
+
|
|
508
|
+
Args:
|
|
509
|
+
permission_manager: The PermissionManager instance
|
|
510
|
+
require_approval: When True, route normal tools through the
|
|
511
|
+
approval UI. When False, auto-allow normal tools but still
|
|
512
|
+
handle ``AskUserQuestion`` via the question UI.
|
|
513
|
+
|
|
514
|
+
Returns:
|
|
515
|
+
Async callback function compatible with ClaudeAgentOptions.can_use_tool
|
|
516
|
+
"""
|
|
517
|
+
from claude_agent_sdk import (
|
|
518
|
+
PermissionResult,
|
|
519
|
+
PermissionResultAllow,
|
|
520
|
+
PermissionResultDeny,
|
|
521
|
+
ToolPermissionContext,
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
async def can_use_tool(
|
|
525
|
+
tool_name: str,
|
|
526
|
+
tool_input: dict[str, Any],
|
|
527
|
+
context: ToolPermissionContext
|
|
528
|
+
) -> PermissionResult:
|
|
529
|
+
"""
|
|
530
|
+
Permission callback that waits for UI approval.
|
|
531
|
+
|
|
532
|
+
Special case: ``AskUserQuestion`` is the CLI's built-in clarifying
|
|
533
|
+
question tool. We render it as a question UI and inject the user's
|
|
534
|
+
answers via ``updated_input``; ``PermissionResultAllow`` can carry
|
|
535
|
+
that, hooks cannot, so this is the only place it can be handled.
|
|
536
|
+
|
|
537
|
+
Args:
|
|
538
|
+
tool_name: Name of the tool
|
|
539
|
+
tool_input: Tool input parameters
|
|
540
|
+
context: Permission context (unused currently)
|
|
541
|
+
|
|
542
|
+
Returns:
|
|
543
|
+
PermissionResult allowing or denying the tool use
|
|
544
|
+
"""
|
|
545
|
+
if tool_name == "AskUserQuestion":
|
|
546
|
+
questions = (
|
|
547
|
+
tool_input.get("questions", [])
|
|
548
|
+
if isinstance(tool_input, dict) else []
|
|
549
|
+
)
|
|
550
|
+
if not questions:
|
|
551
|
+
return PermissionResultDeny(
|
|
552
|
+
message="AskUserQuestion called with no questions"
|
|
553
|
+
)
|
|
554
|
+
try:
|
|
555
|
+
answered, answers = await permission_manager.request_question(
|
|
556
|
+
questions
|
|
557
|
+
)
|
|
558
|
+
except Exception as e:
|
|
559
|
+
return PermissionResultDeny(
|
|
560
|
+
message=f"Question system error: {e}"
|
|
561
|
+
)
|
|
562
|
+
if not answered:
|
|
563
|
+
return PermissionResultDeny(message="User declined to answer")
|
|
564
|
+
return PermissionResultAllow(
|
|
565
|
+
updated_input={"questions": questions, "answers": answers}
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
# If approvals are bypassed, auto-allow non-AskUserQuestion tools.
|
|
569
|
+
# We're only here because some hook said "ask" (or the CLI's rules
|
|
570
|
+
# otherwise routed through can_use_tool); honor the bypass intent.
|
|
571
|
+
if not require_approval:
|
|
572
|
+
return PermissionResultAllow()
|
|
573
|
+
|
|
574
|
+
try:
|
|
575
|
+
allowed, message = await permission_manager.request_permission(
|
|
576
|
+
tool_name, tool_input
|
|
577
|
+
)
|
|
578
|
+
except Exception as e:
|
|
579
|
+
return PermissionResultDeny(
|
|
580
|
+
message=f"Permission system error: {e}"
|
|
581
|
+
)
|
|
582
|
+
|
|
583
|
+
if allowed:
|
|
584
|
+
return PermissionResultAllow()
|
|
585
|
+
else:
|
|
586
|
+
return PermissionResultDeny(message=message or "User denied permission")
|
|
587
|
+
|
|
588
|
+
return can_use_tool
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""MCP tools for Qt widget interaction."""
|
|
2
|
+
|
|
3
|
+
from .controller import create_controller_tool
|
|
4
|
+
from .interaction import create_interaction_tools
|
|
5
|
+
from .introspection import create_introspection_tools
|
|
6
|
+
from .logs import create_logs_tool
|
|
7
|
+
from .screenshot import create_screenshot_tool
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def create_qt_tools_server(target_window):
|
|
11
|
+
"""
|
|
12
|
+
Create MCP server with all Qt tools.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
target_window: The QWidget to interact with
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
MCP server instance with Qt tools
|
|
19
|
+
"""
|
|
20
|
+
from claude_agent_sdk import create_sdk_mcp_server
|
|
21
|
+
|
|
22
|
+
# Create all tools bound to the target window
|
|
23
|
+
screenshot_tool = create_screenshot_tool(target_window)
|
|
24
|
+
get_tree_tool, find_widget_tool = create_introspection_tools(target_window)
|
|
25
|
+
click_tool, type_tool = create_interaction_tools(target_window)
|
|
26
|
+
controller_tool = create_controller_tool(target_window)
|
|
27
|
+
logs_tool = create_logs_tool()
|
|
28
|
+
|
|
29
|
+
# Create and return the server
|
|
30
|
+
return create_sdk_mcp_server(
|
|
31
|
+
name="qt-tools",
|
|
32
|
+
version="0.1.0",
|
|
33
|
+
tools=[
|
|
34
|
+
screenshot_tool,
|
|
35
|
+
get_tree_tool,
|
|
36
|
+
find_widget_tool,
|
|
37
|
+
click_tool,
|
|
38
|
+
type_tool,
|
|
39
|
+
controller_tool,
|
|
40
|
+
logs_tool,
|
|
41
|
+
]
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
__all__ = ["create_qt_tools_server"]
|