mqtt切换为远程服务器部署,优化简单指令解析时长
This commit is contained in:
@@ -15,7 +15,7 @@ import uuid
|
||||
from contextlib import closing
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, Callable
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlparse
|
||||
from urllib.request import Request, urlopen
|
||||
@@ -2387,6 +2387,7 @@ class SelfExtensionService:
|
||||
conversation_turn: int = 0,
|
||||
execution_mode: str = "dry_run",
|
||||
provider_requested: str = "auto",
|
||||
on_commands_validated: Callable[[dict[str, Any]], None] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute a live-NFC peripheral match through the main request API."""
|
||||
|
||||
@@ -2398,41 +2399,8 @@ class SelfExtensionService:
|
||||
request_id = f"req-extension-{uuid.uuid4().hex}"
|
||||
conversation_id = conversation_id or f"conv-{uuid.uuid4().hex}"
|
||||
hardware_request = execution_mode == "hardware"
|
||||
if hardware_request:
|
||||
gateway_result = self.execute_gateway("emit", {"slot": slot})
|
||||
transport = str(gateway_result.get("transport") or self.gateway.transport)
|
||||
gateway_acknowledged = gateway_result.get("gateway_acknowledged") is True
|
||||
command = gateway_result.get("request")
|
||||
operation_message = (
|
||||
f"{capability.get('device_name')} · {capability.get('action')} 已发送至 "
|
||||
"GSCU1B;网关已确认接收,外设物理效果待独立确认。"
|
||||
)
|
||||
delivery_status = "published"
|
||||
else:
|
||||
command = build_gateway_command("emit", {"slot": slot})
|
||||
transport = "dry_run"
|
||||
gateway_acknowledged = False
|
||||
operation_message = (
|
||||
f"{capability.get('device_name')} · {capability.get('action')} 已完成红外路由校验;"
|
||||
"当前为 dry-run,未向 GSCU1B 发射。"
|
||||
)
|
||||
delivery_status = "planned"
|
||||
|
||||
transport = self.gateway.transport if hardware_request else "dry_run"
|
||||
operation_ref = str(capability.get("id") or f"ir-slot-{slot}")
|
||||
operation = {
|
||||
"ref": operation_ref,
|
||||
"domain": "self_extension",
|
||||
"status": "succeeded",
|
||||
"message": operation_message,
|
||||
"telemetry": {
|
||||
"transport": transport,
|
||||
"slot": slot,
|
||||
"delivery_status": delivery_status,
|
||||
"gateway_acknowledged": gateway_acknowledged,
|
||||
"physical_effect_confirmed": False,
|
||||
"command": command,
|
||||
},
|
||||
}
|
||||
function_config = {
|
||||
"apiVersion": "cognitive-agent.io/v1alpha1",
|
||||
"kind": "FunctionConfig",
|
||||
@@ -2455,7 +2423,9 @@ class SelfExtensionService:
|
||||
"action": capability.get("action"),
|
||||
"slot": slot,
|
||||
},
|
||||
"parse_source": capability.get("parse_source", "local_rule"),
|
||||
"parse_source": capability.get(
|
||||
"parse_source", "local_rule"
|
||||
),
|
||||
"timeout_ms": int(self.command_timeout * 1000),
|
||||
"failure_policy": "abort",
|
||||
}
|
||||
@@ -2479,6 +2449,78 @@ class SelfExtensionService:
|
||||
],
|
||||
},
|
||||
}
|
||||
if on_commands_validated is not None:
|
||||
on_commands_validated(
|
||||
{
|
||||
"request_id": request_id,
|
||||
"utterance": utterance,
|
||||
"conversation_id": conversation_id,
|
||||
"conversation_turn": conversation_turn,
|
||||
"status": "processing",
|
||||
"phase": "commands_validated",
|
||||
"orchestration_route": "nfc_peripheral",
|
||||
"provider_requested": provider_requested,
|
||||
"provider_used": "nfc_peripheral_router",
|
||||
"execution_mode": execution_mode,
|
||||
"control_transport": (
|
||||
"wireless_mqtt"
|
||||
if transport == "mqtt"
|
||||
else f"gscu_{transport}"
|
||||
),
|
||||
"trace_events": [
|
||||
{
|
||||
"stage": "nfc_peripheral_route",
|
||||
"status": "succeeded",
|
||||
"summary": "本地规则已完成红外能力路由与指令编译",
|
||||
"duration_ms": 0,
|
||||
}
|
||||
],
|
||||
"validation": [],
|
||||
"function_config": function_config,
|
||||
"function_configs": {"self_extension": function_config},
|
||||
"command_plan": command_plan,
|
||||
"command_plans": {"self_extension": command_plan},
|
||||
"execution_report": None,
|
||||
"operation_results": [],
|
||||
"deferred_requests": [],
|
||||
"domain_state": {},
|
||||
"actuator_state": {},
|
||||
}
|
||||
)
|
||||
if hardware_request:
|
||||
gateway_result = self.execute_gateway("emit", {"slot": slot})
|
||||
transport = str(gateway_result.get("transport") or self.gateway.transport)
|
||||
gateway_acknowledged = gateway_result.get("gateway_acknowledged") is True
|
||||
command = gateway_result.get("request")
|
||||
operation_message = (
|
||||
f"{capability.get('device_name')} · {capability.get('action')} 已发送至 "
|
||||
"GSCU1B;网关已确认接收,外设物理效果待独立确认。"
|
||||
)
|
||||
delivery_status = "published"
|
||||
else:
|
||||
command = build_gateway_command("emit", {"slot": slot})
|
||||
transport = "dry_run"
|
||||
gateway_acknowledged = False
|
||||
operation_message = (
|
||||
f"{capability.get('device_name')} · {capability.get('action')} 已完成红外路由校验;"
|
||||
"当前为 dry-run,未向 GSCU1B 发射。"
|
||||
)
|
||||
delivery_status = "planned"
|
||||
|
||||
operation = {
|
||||
"ref": operation_ref,
|
||||
"domain": "self_extension",
|
||||
"status": "succeeded",
|
||||
"message": operation_message,
|
||||
"telemetry": {
|
||||
"transport": transport,
|
||||
"slot": slot,
|
||||
"delivery_status": delivery_status,
|
||||
"gateway_acknowledged": gateway_acknowledged,
|
||||
"physical_effect_confirmed": False,
|
||||
"command": command,
|
||||
},
|
||||
}
|
||||
control_transport = (
|
||||
"wireless_mqtt" if transport == "mqtt" else f"gscu_{transport}"
|
||||
)
|
||||
@@ -2548,6 +2590,7 @@ class SelfExtensionService:
|
||||
conversation_turn: int = 0,
|
||||
execution_mode: str = "dry_run",
|
||||
provider_requested: str = "auto",
|
||||
on_commands_validated: Callable[[dict[str, Any]], None] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute a validated ordered peripheral/delay plan for the live NFC."""
|
||||
|
||||
@@ -2642,6 +2685,76 @@ class SelfExtensionService:
|
||||
}
|
||||
)
|
||||
|
||||
function_config = {
|
||||
"apiVersion": "cognitive-agent.io/v1alpha1",
|
||||
"kind": "FunctionConfig",
|
||||
"metadata": {
|
||||
"name": f"{request_id}-sequence",
|
||||
"labels": {"execution_scope": "nfc_bound_ir_peripheral"},
|
||||
},
|
||||
"spec": {
|
||||
"domain": "self_extension",
|
||||
"operations": function_operations,
|
||||
},
|
||||
}
|
||||
command_plan = {
|
||||
"apiVersion": "cognitive-agent.io/v1alpha1",
|
||||
"kind": "CommandPlan",
|
||||
"metadata": {"name": f"{request_id}-gscu-sequence"},
|
||||
"spec": {
|
||||
"domain": "self_extension",
|
||||
"platform": "gscu1b",
|
||||
"commands": commands,
|
||||
},
|
||||
}
|
||||
if on_commands_validated is not None:
|
||||
on_commands_validated(
|
||||
{
|
||||
"request_id": request_id,
|
||||
"utterance": utterance,
|
||||
"conversation_id": conversation_id,
|
||||
"conversation_turn": conversation_turn,
|
||||
"status": "processing",
|
||||
"phase": "commands_validated",
|
||||
"orchestration_route": "nfc_peripheral_sequence",
|
||||
"provider_requested": provider_requested,
|
||||
"provider_used": str(
|
||||
plan_result.get("provider_used")
|
||||
or f"gpt:{plan_result.get('model') or self.openai_model}"
|
||||
),
|
||||
"execution_mode": execution_mode,
|
||||
"control_transport": (
|
||||
"wireless_mqtt"
|
||||
if transport == "mqtt"
|
||||
else f"gscu_{transport}"
|
||||
),
|
||||
"trace_events": [
|
||||
{
|
||||
"stage": (
|
||||
"local_peripheral_plan"
|
||||
if str(plan_result.get("provider_used") or "").startswith(
|
||||
"infrared_local"
|
||||
)
|
||||
else "gpt_peripheral_plan"
|
||||
),
|
||||
"status": "succeeded",
|
||||
"summary": f"已编译并校验 {len(steps)} 个有序步骤",
|
||||
"duration_ms": 0,
|
||||
}
|
||||
],
|
||||
"validation": [],
|
||||
"function_config": function_config,
|
||||
"function_configs": {"self_extension": function_config},
|
||||
"command_plan": command_plan,
|
||||
"command_plans": {"self_extension": command_plan},
|
||||
"execution_report": None,
|
||||
"operation_results": [],
|
||||
"deferred_requests": [],
|
||||
"domain_state": {},
|
||||
"actuator_state": {},
|
||||
}
|
||||
)
|
||||
|
||||
for index, step in enumerate(steps):
|
||||
if step.get("kind") == "delay":
|
||||
delay_ms = int(step.get("delay_ms", 0))
|
||||
@@ -2781,28 +2894,6 @@ class SelfExtensionService:
|
||||
)
|
||||
report_status = "succeeded" if succeeded else "failed"
|
||||
first_capability = next(iter(catalog_by_id.values()))
|
||||
function_config = {
|
||||
"apiVersion": "cognitive-agent.io/v1alpha1",
|
||||
"kind": "FunctionConfig",
|
||||
"metadata": {
|
||||
"name": f"{request_id}-sequence",
|
||||
"labels": {"execution_scope": "nfc_bound_ir_peripheral"},
|
||||
},
|
||||
"spec": {
|
||||
"domain": "self_extension",
|
||||
"operations": function_operations,
|
||||
},
|
||||
}
|
||||
command_plan = {
|
||||
"apiVersion": "cognitive-agent.io/v1alpha1",
|
||||
"kind": "CommandPlan",
|
||||
"metadata": {"name": f"{request_id}-gscu-sequence"},
|
||||
"spec": {
|
||||
"domain": "self_extension",
|
||||
"platform": "gscu1b",
|
||||
"commands": commands,
|
||||
},
|
||||
}
|
||||
control_transport = (
|
||||
"wireless_mqtt" if transport == "mqtt" else f"gscu_{transport}"
|
||||
)
|
||||
@@ -2988,12 +3079,22 @@ class SelfExtensionService:
|
||||
item for item in vehicle_devices[:32] if isinstance(item, dict)
|
||||
]
|
||||
allowed_vehicle_commands = set(_vehicle_catalog_commands(vehicle_devices))
|
||||
response_text = self._request_gpt_plan(
|
||||
utterance,
|
||||
planner_peripherals,
|
||||
vehicle_devices,
|
||||
api_key,
|
||||
)
|
||||
raw_routing = payload.get("natural_language_routing")
|
||||
if isinstance(raw_routing, dict):
|
||||
response_text = self._request_gpt_plan(
|
||||
utterance,
|
||||
planner_peripherals,
|
||||
vehicle_devices,
|
||||
api_key,
|
||||
natural_language_routing=dict(raw_routing),
|
||||
)
|
||||
else:
|
||||
response_text = self._request_gpt_plan(
|
||||
utterance,
|
||||
planner_peripherals,
|
||||
vehicle_devices,
|
||||
api_key,
|
||||
)
|
||||
try:
|
||||
proposed = json.loads(response_text)
|
||||
except json.JSONDecodeError as exc:
|
||||
@@ -3120,6 +3221,8 @@ class SelfExtensionService:
|
||||
peripherals: list[dict[str, Any]],
|
||||
vehicle_devices: list[dict[str, Any]],
|
||||
api_key: str,
|
||||
*,
|
||||
natural_language_routing: dict[str, Any] | None = None,
|
||||
) -> str:
|
||||
peripheral_ids = [
|
||||
str(item.get("id"))
|
||||
@@ -3172,6 +3275,9 @@ class SelfExtensionService:
|
||||
"required": ["summary", "needs_confirmation", "question", "steps"],
|
||||
}
|
||||
system_prompt = f"""你是车载外设自扩展工作台的意图规划器。只输出 JSON 对象。
|
||||
输入中的 natural_language_routing 是可信路由数据:original_utterance 为完整原句,
|
||||
parsed_clauses/resolved_clauses 已由本地规则处理,只能为 unresolved_clauses 和当前 utterance
|
||||
补充计划,禁止重复、替换或重新执行已解析子句。
|
||||
peripheral 只能逐字引用目录中 status=learned 的短 capability_id,绝不能编造或改写 ID。
|
||||
仅当同类存在多台可匹配的外设玩具且用户未说明目标时,设置 needs_confirmation=true 并追问;
|
||||
此追问规则不适用于车窗、空调、座椅、电动风口等车端能力。
|
||||
@@ -3218,6 +3324,15 @@ delay 步骤的 capability_id 和 command 必须为空字符串,不能位于
|
||||
"text": json.dumps(
|
||||
{
|
||||
"utterance": utterance,
|
||||
"natural_language_routing": (
|
||||
natural_language_routing or {
|
||||
"original_utterance": utterance,
|
||||
"parsed_clauses": [],
|
||||
"resolved_clauses": [],
|
||||
"unresolved_clauses": [utterance],
|
||||
"cloud_input_utterance": utterance,
|
||||
}
|
||||
),
|
||||
"learned_peripheral_capabilities": peripherals,
|
||||
"can_dev_vehicle_capabilities": vehicle_devices,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user