优化后端和前端逻辑
This commit is contained in:
@@ -671,8 +671,43 @@ class SeatDemoService:
|
||||
) -> dict[str, Any]:
|
||||
"""Persist one edited mode from a server-generated control flow."""
|
||||
|
||||
return self._persist_mode_profile_card(
|
||||
mode_id,
|
||||
name,
|
||||
instruction,
|
||||
source_request_id,
|
||||
vehicle_id,
|
||||
)
|
||||
|
||||
def create_mode_profile_card(
|
||||
self,
|
||||
name: str,
|
||||
instruction: str,
|
||||
source_request_id: str,
|
||||
vehicle_id: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Create one user mode from a server-generated control flow."""
|
||||
|
||||
return self._persist_mode_profile_card(
|
||||
None,
|
||||
name,
|
||||
instruction,
|
||||
source_request_id,
|
||||
vehicle_id,
|
||||
)
|
||||
|
||||
def _persist_mode_profile_card(
|
||||
self,
|
||||
mode_id: str | None,
|
||||
name: str,
|
||||
instruction: str,
|
||||
source_request_id: str,
|
||||
vehicle_id: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Validate and persist a created or edited mode command flow."""
|
||||
|
||||
resolved_vehicle_id = vehicle_id or self.vehicle_profiles.default().id
|
||||
clean_mode_id = str(mode_id).strip()
|
||||
clean_mode_id = str(mode_id or "").strip()
|
||||
clean_name = str(name).strip()
|
||||
clean_instruction = str(instruction).strip()
|
||||
clean_request_id = str(source_request_id).strip()
|
||||
@@ -690,15 +725,19 @@ class SeatDemoService:
|
||||
visible_profiles = self.mode_profiles._profiles_for_vehicle( # noqa: SLF001
|
||||
resolved_vehicle_id
|
||||
)
|
||||
profile = next(
|
||||
(item for item in visible_profiles if item.id == clean_mode_id),
|
||||
None,
|
||||
profile = (
|
||||
next(
|
||||
(item for item in visible_profiles if item.id == clean_mode_id),
|
||||
None,
|
||||
)
|
||||
if clean_mode_id
|
||||
else None
|
||||
)
|
||||
if profile is None:
|
||||
if clean_mode_id and profile is None:
|
||||
raise KeyError(clean_mode_id)
|
||||
normalized_name = re.sub(r"\s+", "", clean_name).casefold()
|
||||
if any(
|
||||
item.id != profile.id
|
||||
(profile is None or item.id != profile.id)
|
||||
and re.sub(r"\s+", "", item.name).casefold() == normalized_name
|
||||
for item in visible_profiles
|
||||
):
|
||||
@@ -813,14 +852,18 @@ class SeatDemoService:
|
||||
saved = self.mode_profiles.save_mode(
|
||||
clean_name,
|
||||
operations,
|
||||
mode_id=profile.id,
|
||||
aliases=list(profile.aliases),
|
||||
mode_id=profile.id if profile is not None else None,
|
||||
aliases=list(profile.aliases) if profile is not None else [],
|
||||
vehicle_id=resolved_vehicle_id,
|
||||
description=profile.description,
|
||||
icon=profile.icon,
|
||||
description=(
|
||||
profile.description
|
||||
if profile is not None
|
||||
else "由自然语言生成的自定义模式。"
|
||||
),
|
||||
icon=profile.icon if profile is not None else "bookmark-plus",
|
||||
instruction=clean_instruction,
|
||||
command_flow=command_flow,
|
||||
timeline=profile.timeline,
|
||||
timeline=profile.timeline if profile is not None else None,
|
||||
)
|
||||
public_profile = next(
|
||||
(
|
||||
|
||||
@@ -418,7 +418,7 @@ class SeatDemoHandler(BaseHTTPRequestHandler):
|
||||
if path.startswith("/api/self-repair/"):
|
||||
self._handle_self_repair_post(path)
|
||||
return
|
||||
if path.startswith("/api/mode-profiles/"):
|
||||
if path == "/api/mode-profiles" or path.startswith("/api/mode-profiles/"):
|
||||
self._handle_mode_profile_post(path)
|
||||
return
|
||||
supplement_match = re.fullmatch(
|
||||
@@ -1506,8 +1506,9 @@ class SeatDemoHandler(BaseHTTPRequestHandler):
|
||||
self._handle_mode_profile_execute(execute_match.group("mode_id"))
|
||||
return
|
||||
|
||||
create_mode = path == "/api/mode-profiles"
|
||||
mode_id = path.removeprefix("/api/mode-profiles/").strip()
|
||||
if not mode_id:
|
||||
if not create_mode and not mode_id:
|
||||
self._json(
|
||||
{"error": "invalid_request", "message": "模式编号不能为空。"},
|
||||
HTTPStatus.BAD_REQUEST,
|
||||
@@ -1515,14 +1516,18 @@ class SeatDemoHandler(BaseHTTPRequestHandler):
|
||||
return
|
||||
try:
|
||||
payload = self._read_json_body(max_bytes=16 * 1024)
|
||||
result = self.service.save_mode_profile_card(
|
||||
mode_id,
|
||||
arguments = (
|
||||
str(payload.get("name", "")),
|
||||
str(payload.get("instruction", "")),
|
||||
str(payload.get("source_request_id", "")),
|
||||
str(payload.get("vehicle_id", "")) or None,
|
||||
)
|
||||
self._json(result, HTTPStatus.OK)
|
||||
result = (
|
||||
self.service.create_mode_profile_card(*arguments)
|
||||
if create_mode
|
||||
else self.service.save_mode_profile_card(mode_id, *arguments)
|
||||
)
|
||||
self._json(result, HTTPStatus.CREATED if create_mode else HTTPStatus.OK)
|
||||
except KeyError:
|
||||
self._json(
|
||||
{"error": "not_found", "message": "未找到需要编辑的模式卡片。"},
|
||||
|
||||
Reference in New Issue
Block a user