feat: enforce DSH assistant capability boundaries
This commit is contained in:
1 parent
b84afbfa8a
commit
d667fa6c57
6 files changed
+67
-7
No files matched your search
@@ -6,6 +6,7 @@ from typing import Any, Mapping
|
||||
|
||||
|
||||
ALLOWED_ACTIONS = {"retry_failed", "pause_task", "refresh_snapshot"}
|
||||
AUTO_ACTIONS = {"retry_failed"}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -31,4 +32,19 @@ def validate_action(raw: Mapping[str, Any]) -> ActionProposal:
|
||||
if not task_id or len(task_id) > 128:
|
||||
raise ValueError("invalid_dsh_task_id")
|
||||
reason = str(raw.get("reason", ""))[:500]
|
||||
return ActionProposal(str(raw["name"]), task_id, reason, bool(raw.get("requires_confirmation", False)))
|
||||
name = str(raw["name"])
|
||||
# The model cannot lower the confirmation requirement by setting a flag.
|
||||
requires_confirmation = name not in AUTO_ACTIONS
|
||||
return ActionProposal(name, task_id, reason, requires_confirmation)
|
||||
|
||||
|
||||
def validate_model_actions(raw_actions: Any, *, task_id: str) -> list[ActionProposal]:
|
||||
if not isinstance(raw_actions, list) or len(raw_actions) > 3:
|
||||
raise ValueError("invalid_dsh_actions")
|
||||
proposals = []
|
||||
for raw in raw_actions:
|
||||
proposal = validate_action(raw)
|
||||
if proposal.task_id != task_id:
|
||||
raise ValueError("dsh_action_task_mismatch")
|
||||
proposals.append(proposal)
|
||||
return proposals
|
||||
@@ -6,6 +6,8 @@ import urllib.request
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Mapping
|
||||
|
||||
from .actions import validate_model_actions
|
||||
|
||||
|
||||
class DshClientError(RuntimeError):
|
||||
pass
|
||||
@@ -42,6 +44,10 @@ class OpenAICompatibleClient:
|
||||
result = json.loads(content)
|
||||
except (OSError, KeyError, IndexError, TypeError, ValueError, urllib.error.URLError) as exc:
|
||||
raise DshClientError("dsh_model_response_invalid") from exc
|
||||
if not isinstance(result, dict) or not isinstance(result.get("reply"), str) or not isinstance(result.get("actions", []), list):
|
||||
if not isinstance(result, dict) or not isinstance(result.get("reply"), str):
|
||||
raise DshClientError("dsh_model_response_invalid")
|
||||
try:
|
||||
result["actions"] = validate_model_actions(result.get("actions", []), task_id=str(snapshot.get("task_id", "")))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise DshClientError("dsh_model_action_rejected") from exc
|
||||
return result
|
||||
@@ -2,7 +2,9 @@ from __future__ import annotations
|
||||
|
||||
from PySide6.QtWidgets import QDialog, QFormLayout, QHBoxLayout, QLabel, QListWidget, QLineEdit, QMessageBox, QPushButton, QTextEdit, QVBoxLayout
|
||||
|
||||
from ...dsh.actions import ActionProposal
|
||||
from ...dsh.client import DshClientError, OpenAICompatibleClient
|
||||
from ...dsh.service_adapter import DshServiceAdapter
|
||||
from ...dsh.snapshot import TaskSnapshot
|
||||
from ...dsh.supervisor import DshSupervisor
|
||||
|
||||
@@ -10,9 +12,10 @@ from ...dsh.supervisor import DshSupervisor
|
||||
class DshPanel(QDialog):
|
||||
def __init__(self, service, *, task_id: str = "", snapshot: TaskSnapshot | None = None, client=None, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setWindowTitle("DSH AI 监管助手")
|
||||
self.setWindowTitle("DSH助手")
|
||||
self.resize(720, 520)
|
||||
self.client = client
|
||||
self.adapter = DshServiceAdapter(service)
|
||||
self.snapshot = snapshot or TaskSnapshot(task_id, "observe")
|
||||
self.supervisor = DshSupervisor()
|
||||
self.issue = QLabel("本地监管:等待异常事件")
|
||||
@@ -41,6 +44,30 @@ class DshPanel(QDialog):
|
||||
try:
|
||||
result = self.client.chat(message, self.snapshot.safe_dict())
|
||||
self.chat.append(f"助手:{result['reply']}")
|
||||
self._show_actions(result.get("actions", []))
|
||||
except DshClientError as error:
|
||||
self.chat.append(f"助手:{error}")
|
||||
|
||||
def _show_actions(self, actions):
|
||||
for proposal in actions:
|
||||
if not isinstance(proposal, ActionProposal):
|
||||
continue
|
||||
button = QPushButton(f"执行:{proposal.name} · {proposal.reason or 'DSH建议'}")
|
||||
button.clicked.connect(lambda _checked=False, p=proposal, b=button: self._execute(p, b))
|
||||
self.actions.addItem("")
|
||||
self.actions.setItemWidget(self.actions.item(self.actions.count() - 1), button)
|
||||
|
||||
def _execute(self, proposal: ActionProposal, button: QPushButton):
|
||||
if proposal.requires_confirmation and QMessageBox.question(
|
||||
self, "确认DSH动作", f"确认执行 {proposal.name}?\n\n{proposal.reason}",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
QMessageBox.StandardButton.No) != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
button.setEnabled(False)
|
||||
try:
|
||||
result = self.adapter.execute(proposal)
|
||||
self.chat.append(f"DSH:动作已提交 {result.get('action', proposal.name)}")
|
||||
except Exception as error:
|
||||
button.setEnabled(True)
|
||||
self.chat.append(f"DSH:动作未执行:{error}")
|
||||
|
||||
@@ -94,7 +94,7 @@ class TrialTaskCenterWindow(QMainWindow):
|
||||
self.move_mapping_down_button.clicked.connect(lambda: self._move_mapping(1))
|
||||
self.plan_panel = ProjectPlanPanel(self)
|
||||
self.run_panel = RunPanel()
|
||||
self.dsh_button = QPushButton('DSH AI 监管')
|
||||
self.dsh_button = QPushButton('DSH助手')
|
||||
self.dsh_button.clicked.connect(self._open_dsh)
|
||||
self._log_presenter = TaskLogPresenter(service, self.run_panel, self)
|
||||
self.run_panel.quick_run_button.clicked.connect(self._prepare_quick_run)
|
||||
|
||||
Reference in new issue
Block a user