2026-07-05 15:58:52 +08:00
|
|
|
"""Interaction menu and user action helpers."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VALID_ACTIONS = {
|
|
|
|
|
"open_workbench",
|
|
|
|
|
"quick_profile",
|
|
|
|
|
"create_workflow",
|
|
|
|
|
"generate_node",
|
|
|
|
|
"retry_task",
|
|
|
|
|
"open_logs",
|
|
|
|
|
"open_settings",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class InteractionAction:
|
|
|
|
|
action_id: str
|
|
|
|
|
label: str
|
|
|
|
|
action_type: str
|
|
|
|
|
payload: dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
requires_permission: str = "read"
|
|
|
|
|
enabled: bool = True
|
|
|
|
|
|
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
"action_id": self.action_id,
|
|
|
|
|
"label": self.label,
|
|
|
|
|
"action_type": self.action_type,
|
|
|
|
|
"payload": self.payload,
|
|
|
|
|
"requires_permission": self.requires_permission,
|
|
|
|
|
"enabled": self.enabled,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_default_actions(*, api_base: str = "") -> list[InteractionAction]:
|
|
|
|
|
return [
|
|
|
|
|
InteractionAction("open_workbench", "Workbench", "open_workbench", {"url": f"{api_base}/web"}),
|
|
|
|
|
InteractionAction("quick_profile", "Quick profile", "quick_profile", requires_permission="write"),
|
|
|
|
|
InteractionAction("open_logs", "Logs", "open_logs", {"view": "logs"}),
|
|
|
|
|
InteractionAction("open_settings", "Settings", "open_settings", {"view": "settings"}, "config"),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_action(payload: dict[str, Any]) -> InteractionAction:
|
|
|
|
|
action_type = str(payload.get("action_type") or payload.get("type") or "").strip()
|
|
|
|
|
if action_type not in VALID_ACTIONS:
|
|
|
|
|
raise ValueError("unsupported interaction action")
|
|
|
|
|
action_id = str(payload.get("action_id") or action_type).strip()
|
|
|
|
|
label = str(payload.get("label") or action_type).strip()
|
|
|
|
|
return InteractionAction(
|
|
|
|
|
action_id=action_id,
|
|
|
|
|
label=label,
|
|
|
|
|
action_type=action_type,
|
|
|
|
|
payload=dict(payload.get("payload") or {}),
|
|
|
|
|
requires_permission=str(payload.get("requires_permission") or "read"),
|
|
|
|
|
enabled=bool(payload.get("enabled", True)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filter_actions_for_permissions(
|
|
|
|
|
actions: list[InteractionAction],
|
|
|
|
|
permissions: set[str] | frozenset[str],
|
|
|
|
|
) -> list[InteractionAction]:
|
|
|
|
|
allowed = set(permissions)
|
|
|
|
|
return [
|
|
|
|
|
action
|
|
|
|
|
for action in actions
|
|
|
|
|
if action.enabled and (action.requires_permission == "read" or action.requires_permission in allowed)
|
|
|
|
|
]
|