feat: make mode creation preview-only and cache-safe

This commit is contained in:
Miayui
2026-09-20 19:32:03 +08:00
parent bfff1b515c
commit 89707a42e2
28 changed files with 1375 additions and 165 deletions
+11 -2
View File
@@ -552,7 +552,8 @@
}
.assistant-mode-schedule-summary,
.assistant-mode-schedule-hint {
.assistant-mode-schedule-hint,
.assistant-mode-draft-preview-notice {
margin: 0;
padding: 9px 12px;
color: var(--assistant-muted, #64748b);
@@ -568,10 +569,18 @@
}
.assistant-mode-schedule-phase + .assistant-mode-schedule-phase,
.assistant-mode-schedule-hint {
.assistant-mode-schedule-hint,
.assistant-mode-draft-preview-notice {
border-top: 1px solid var(--assistant-border, #e5eaf1);
}
.assistant-mode-draft-preview-notice {
margin: 0;
color: var(--status-success-foreground, #087854);
background: var(--status-success-soft, #ebf9f3);
font-weight: 700;
}
.assistant-mode-schedule-phase > header {
display: grid;
gap: 3px;
+167 -14
View File
@@ -569,6 +569,11 @@
"seat.set_backrest_recline": "靠背",
"seat.set_legrest_height": "腿托",
"seat.set_legrest_extension": "腿托",
"seat.set_ventilation": "通风",
"seat.set_heating": "加热",
"seat.set_driver_ventilation_native": "通风",
"seat.set_driver_heating_native": "加热",
"seat.set_driver_massage_native": "按摩",
}[capability];
if (component) return `${seat}${component}`;
if (capability === "seat.set_driver_axis_native") {
@@ -585,7 +590,19 @@
return seat;
}
if (capability.startsWith("outlet.")) return "空调风口";
return String(fallback || capability || "控制指令");
const domain = String(operation.domain || capability.split(".", 1)[0] || "");
return {
climate: "空调",
window: "车窗",
roof: "遮阳帘",
ambient_light: "氛围灯",
interior_light: "室内灯",
self_extension: "红外外设",
door: "车门",
outlet: "空调风口",
}[domain] || (/^[\u4e00-\u9fff]/.test(String(fallback || ""))
? String(fallback)
: "控制指令");
}
function fallbackAssistantControlCommands(result = {}) {
@@ -620,6 +637,8 @@
}
function assistantDispatchedCommands(result = {}) {
const previewOnly = String(result.execution_mode || "dry_run") !== "hardware"
|| result.orchestration_route === "mode_profile_create_preview";
const projections = Array.isArray(result.validated_control_commands)
? result.validated_control_commands
: [];
@@ -652,22 +671,30 @@
} catch (_error) {
commands = [];
}
if (!commands.length) return fallbackAssistantControlCommands(result);
if (!commands.length) commands = fallbackAssistantControlCommands(result);
return commands.map((item, index) => {
const operation = projectionByRef.get(String(item.operationRef || ""))
|| operationByRef.get(String(item.operationRef || ""))
|| projections[index]?.operation
|| {};
const pendingDelivery = result.status === "processing"
const pendingDelivery = result.status === "processing" && !previewOnly
? {
deliveryState: "submitting",
deliveryLabel: "正在下发",
deliveryDetail: "",
}
: {};
const previewDelivery = previewOnly
? {
deliveryState: "not_submitted",
deliveryLabel: "仅预览 · 未下发",
deliveryDetail: "创建模式期间不会发送控制指令",
}
: {};
return {
...item,
...pendingDelivery,
...previewDelivery,
category: assistantControlCategory(operation, item.category),
operation,
};
@@ -691,6 +718,11 @@
manual: "手动",
internal: "内循环",
external: "外循环",
romantic: "浪漫",
cinema: "影院",
rest: "休息",
camping: "露营",
meditation: "冥想",
}[normalized] || String(value ?? "").trim();
}
@@ -764,6 +796,9 @@
message.dispatchedControlResult = mergedResult;
const commands = assistantDispatchedCommands(mergedResult);
if (!commands.length) return;
const previewOnly = String(mergedResult.execution_mode || "dry_run") !== "hardware"
|| mergedResult.orchestration_route === "mode_profile_create_preview";
const headingText = previewOnly ? "控制指令预览(未下发)" : "下发控制指令";
const signature = JSON.stringify(commands.map((item) => [
item.operationRef,
@@ -794,7 +829,7 @@
deliveryLabel: String(item.deliveryLabel || "未下发"),
deliveryDetail: String(item.deliveryDetail || ""),
}));
if (vueRenderer(host, viewCommands)) {
if (vueRenderer(host, viewCommands, { heading: headingText })) {
host.dataset.commandSignature = signature;
message.article?.classList.add("assistant-message-with-dispatched-controls");
focusLatestDispatchedControls(host);
@@ -821,7 +856,7 @@
headingIcon.className = "assistant-dispatched-controls-heading-icon";
headingIcon.innerHTML = '<i data-lucide="list-tree"></i>';
const heading = document.createElement("b");
heading.textContent = "下发控制指令";
heading.textContent = headingText;
const count = document.createElement("strong");
count.className = "assistant-dispatched-controls-count";
const chevron = document.createElement("i");
@@ -835,6 +870,8 @@
}
card.dataset.commandSignature = signature;
const heading = $("summary b", card);
if (heading) heading.textContent = headingText;
const count = $(".assistant-dispatched-controls-count", card);
const list = $(".assistant-dispatched-controls-list", card);
if (count) count.textContent = String(commands.length);
@@ -925,6 +962,30 @@
indexes.forEach((index) => consumed.add(index));
rows.push({ label, value, icon, order });
};
const domainLabels = {
climate: "空调",
window: "车窗",
roof: "遮阳帘",
seat: "座椅",
ambient_light: "氛围灯",
interior_light: "室内灯",
self_extension: "红外外设",
door: "车门",
outlet: "空调风口",
};
const capabilityLabels = {
"climate.set_power": "空调电源",
"climate.set_temperature": "空调温度",
"climate.set_fan_level": "空调风量",
"climate.set_cycle_mode": "空调循环",
"climate.set_control_mode": "空调控制模式",
"climate.set_outlet_mode": "空调出风模式",
"window.set_position": "车窗位置",
"roof.set_shade_position": "遮阳帘位置",
"ambient_light.set_power": "氛围灯",
"ambient_light.set_mode": "氛围灯模式",
"infrared.emit": "红外外设",
};
const zeroGravityIndexes = actions
.map((action, index) => ({ action, index }))
@@ -978,6 +1039,65 @@
if (position === 100) return `完全打开(${target})`;
return target;
};
const localizedParameterValue = (value) => ({
on: "开启", off: "关闭", open: "打开", close: "关闭", stop: "停止",
auto: "自动", manual: "手动", internal: "内循环", external: "外循环",
romantic: "浪漫", cinema: "影院", rest: "休息", camping: "露营",
}[String(value)] || String(value));
const genericParameterText = (params) => Object.entries(params)
.filter(([key]) => key !== "slot" && key !== "capability_ref")
.map(([key, rawValue]) => {
const name = {
state: "状态", motion: "动作", action: "动作", mode: "模式",
level: "档位", temperature_c: "温度", position_pct: "目标开度",
brightness_pct: "亮度", brightness_level: "亮度档位",
color_hex: "颜色", color_index: "色号",
}[key] || "参数";
const suffix = key === "temperature_c" ? "℃"
: ["position_pct", "brightness_pct"].includes(key) ? "%"
: key === "level" || key === "brightness_level" ? "档" : "";
return `${name}:${localizedParameterValue(rawValue)}${suffix}`;
})
.join(" · ");
const passengerComfort = actions
.map((action, index) => ({ action, index }))
.filter(({ action, index }) => !consumed.has(index)
&& ["passenger", "front_right"].includes(targetZone(action))
&& [
"seat.set_ventilation",
"seat.set_backrest_recline",
"seat.set_legrest_height",
].includes(String(action.capability || "")));
const comfortByCapability = new Map(passengerComfort.map(({ action, index }) => [
String(action.capability || ""), { action, index },
]));
if ([
"seat.set_ventilation",
"seat.set_backrest_recline",
"seat.set_legrest_height",
].every((capability) => comfortByCapability.has(capability))) {
const ventilation = parameters(comfortByCapability.get("seat.set_ventilation").action);
const backrest = parameters(comfortByCapability.get("seat.set_backrest_recline").action);
const legrest = parameters(comfortByCapability.get("seat.set_legrest_height").action);
const comfortMentions = ["座椅", "副驾", "舒适"]
.map((term) => instruction.indexOf(term))
.filter((index) => index >= 0);
const comfortOrder = comfortMentions.length
? Math.min(...comfortMentions)
: 10_000 + Math.max(...passengerComfort.map(({ index }) => index));
addRow(
"副驾座椅舒适模式",
[
`通风${ventilation.state === "off" ? "关闭" : `开启 ${ventilation.level ?? 1}档`}`,
`靠背 ${backrest.position_pct ?? "--"}%`,
`腿托 ${legrest.position_pct ?? "--"}%`,
].join(" · "),
"armchair",
comfortOrder,
passengerComfort.map(({ index }) => index),
);
}
actions.forEach((action, index) => {
if (consumed.has(index)) return;
const capability = String(action.capability || "");
@@ -988,12 +1108,20 @@
front_row: "前排", rear_row: "后排", rear_left: "左后", rear_right: "右后",
}[zone] || "";
const params = parameters(action);
let label = capability || domain || "座舱设备";
let label = capabilityLabels[capability] || domainLabels[domain] || "座舱设备";
let value = "已配置";
let icon = "sliders-horizontal";
let order = 10_000 + index;
if (capability === "roof.set_shade_motion" || capability === "roof.set_shade_position") {
if (capability === "infrared.emit" || domain === "self_extension") {
label = String(action?.target?.name || action?.target?.device || "红外外设").trim()
|| "红外外设";
value = params.action != null
? `动作:${localizedParameterValue(params.action)}`
: "动作已配置";
icon = "radio-tower";
order = instructionOrder(label, "外设", "灯");
} else if (capability === "roof.set_shade_motion" || capability === "roof.set_shade_position") {
label = "遮阳帘";
value = params.position_pct != null
? shadePositionLabel(params.position_pct)
@@ -1011,7 +1139,13 @@
rear_right: "右后车窗",
};
label = windowLabels[zone] || "车窗";
value = params.position_pct != null ? `${params.position_pct}%` : motionLabel(params.motion);
value = params.position_pct != null
? Number(params.position_pct) === 0
? "完全关闭(目标开度 0%)"
: Number(params.position_pct) === 100
? "完全打开(目标开度 100%)"
: `目标开度 ${params.position_pct}%`
: motionLabel(params.motion);
icon = "panels-top-left";
order = instructionOrder("车窗");
} else if (domain === "seat") {
@@ -1071,7 +1205,8 @@
face_feet_defrost: "吹面 / 吹脚 / 除霜", face_defrost: "吹面 / 除霜",
},
};
label = capability === "climate.set_fan_level" ? "空调风量"
label = capability === "climate.set_power" ? "空调电源"
: capability === "climate.set_fan_level" ? "空调风量"
: capability === "climate.set_cycle_mode" ? "空调循环"
: capability === "climate.set_control_mode" ? "空调控制模式"
: capability === "climate.set_outlet_mode" ? "空调出风模式" : "空调";
@@ -1104,6 +1239,9 @@
icon = "lightbulb";
order = instructionOrder(label, "灯光");
}
if (value === "已配置") {
value = genericParameterText(params) || "参数已配置";
}
addRow(label, value, icon, order, [index]);
});
return rows.sort((left, right) => left.order - right.order);
@@ -1219,12 +1357,12 @@
card.classList.add("is-busy");
status.hidden = false;
status.className = "assistant-mode-draft-status working";
status.textContent = "正在进行模拟试运行…";
status.textContent = "正在重新进行 dry-run 校验(不会下发)…";
try {
const result = await window.CognitiveAgentConsole?.tryModeDraft?.({ ...draft, actions });
if (!result || result.status !== "succeeded") throw new Error("试运行未通过,请检查动作设置。");
status.className = "assistant-mode-draft-status success";
status.textContent = "试运行通过 · 尚未保存";
status.textContent = "校验通过 · 未下发 · 尚未保存";
} catch (error) {
status.className = "assistant-mode-draft-status failed";
status.textContent = error.message || "试运行失败,请重试。";
@@ -1275,7 +1413,7 @@
const eyebrow = document.createElement("small");
eyebrow.textContent = draft.existing_mode_id
? "已载入现有功能模式"
: "已为你生成功能模式";
: "已为你生成功能模式(未下发)";
const title = document.createElement("strong");
title.textContent = `「${draft.name}」`;
headingCopy.append(eyebrow, title);
@@ -1314,6 +1452,10 @@
} else {
appendModeDraftRows(list, modeDraftRows(draft, actions));
}
const previewNotice = document.createElement("p");
previewNotice.className = "assistant-mode-draft-preview-notice";
previewNotice.textContent = "创建阶段仅生成并校验控制方案,不会向 CAN 或红外外设发送指令。";
list.append(previewNotice);
const controls = document.createElement("div");
controls.className = "assistant-mode-draft-actions";
@@ -1326,7 +1468,7 @@
const trial = document.createElement("button");
trial.type = "button";
trial.className = "assistant-mode-draft-trial";
trial.textContent = "先试运行";
trial.textContent = "重新校验(不下发)";
controls.append(save, trial);
const status = document.createElement("p");
status.className = "assistant-mode-draft-status";
@@ -1379,12 +1521,23 @@
window.lucide?.createIcons({ attrs: { "aria-hidden": "true" } });
}
function isModeCreationPreviewRequest(message) {
const compact = String(message || "").trim().replace(/\s+/g, "");
if (!compact || /取消(?:创建|构建|生成)|(?:不要|别|禁止)(?:创建|构建|生成)/.test(compact)) {
return false;
}
return /(?:构建|创建|新建|新增|生成|添加)(?:一下)?(?:一个|个|一套|套)?[^,。!?!?]{1,64}?模式/.test(compact);
}
function requestBody(message) {
const executionMode = window.CognitiveAgentConsole?.getExecutionMode?.() || "dry_run";
const configuredExecutionMode = window.CognitiveAgentConsole?.getExecutionMode?.() || "dry_run";
const controlTransport = localStorage.getItem("cognitive-agent.control-transport") || "wired_can";
const hardwareClientId = localStorage.getItem("cognitive-agent.hardware-client-id");
const vehicleId = $("#vehicleSelect")?.value || null;
const modeProfileDraftContext = requestModeDraftContext(message);
const modeConfigurationPreview = Boolean(modeProfileDraftContext)
|| isModeCreationPreviewRequest(message);
const executionMode = modeConfigurationPreview ? "dry_run" : configuredExecutionMode;
return {
message,
conversation_id: conversationId(),
+1 -1
View File
@@ -45,7 +45,7 @@
<script src="/pwa.js?v=android-pad-3" defer></script>
<script src="/console-v2.js?v=local-first-77-performance" defer></script>
<script src="/self-extension.js?v=nfc-mqtt-56" defer></script>
<script src="/assistant-chat.js?v=codex-assistant-34" defer></script>
<script src="/assistant-chat.js?v=codex-assistant-35" defer></script>
<script src="/energy-monitor.js?v=energy-monitor-5-performance" defer></script>
<script src="/firmware-workbench-entry.js?v=3" defer></script>
</head>
+4 -4
View File
@@ -1,4 +1,4 @@
const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt";
const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview";
const SHELL_CACHE = `${CACHE_VERSION}-shell`;
const RUNTIME_CACHE = `${CACHE_VERSION}-runtime`;
const APP_SHELL = [
@@ -16,13 +16,13 @@ const APP_SHELL = [
"/speech-input.css?v=sensevoice-7",
"/speech-input.js?v=sensevoice-13",
"/speech-audio-worklet.js?v=sensevoice-5",
"/assistant-chat.css?v=codex-assistant-29",
"/assistant-chat.js?v=codex-assistant-34",
"/assistant-chat.css?v=codex-assistant-30",
"/assistant-chat.js?v=codex-assistant-35",
"/energy-monitor.css?v=energy-monitor-5-dialog-size",
"/energy-monitor.js?v=energy-monitor-5-performance",
"/ui-consistency.css?v=spatial-ui-8",
"/codepen-glass-theme.css?v=creative-cloud-6-performance",
"/vue-ui.js?v=vue-presentation-3",
"/vue-ui.js?v=vue-presentation-4",
"/firmware-workbench-entry.css?v=2",
"/firmware-workbench-entry.js?v=3",
"/firmware-workbench.html",
+79 -2
View File
@@ -1,5 +1,13 @@
import { cpSync, copyFileSync, existsSync, mkdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { createHash } from "node:crypto";
import {
cpSync,
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
} from "node:fs";
import { basename, dirname, extname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import vue from "@vitejs/plugin-vue";
@@ -45,6 +53,74 @@ const runtimeFiles = [
"codepen-glass-theme.css",
"vue-ui.js"
];
const fingerprintedRuntimeFiles = runtimeFiles.filter((file) => (
/\.(?:css|js)$/.test(file)
&& !["service-worker.js", "speech-audio-worklet.js"].includes(file)
));
const rewrittenHtmlFiles = [
"index.html",
"login.html",
"hardware-control.html",
"firmware-workbench.html",
];
function contentHash(value) {
return createHash("sha256").update(value).digest("hex").slice(0, 12);
}
function hashedRuntimeName(file, hash) {
const extension = extname(file);
return `${basename(file, extension)}.${hash}${extension}`;
}
function rewriteRuntimeReferences(value, manifest) {
let rewritten = value;
for (const [source, target] of Object.entries(manifest)) {
const escaped = source.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
rewritten = rewritten.replace(
new RegExp(`/${escaped}(?:\\?[^\\s\"'<>)]*)?`, "g"),
`/${target}`,
);
}
return rewritten;
}
function fingerprintRuntimeAssets() {
const manifest = {};
for (const file of fingerprintedRuntimeFiles) {
const source = resolve(outputRoot, file);
if (!existsSync(source)) continue;
const contents = readFileSync(source);
const hashedName = hashedRuntimeName(file, contentHash(contents));
writeFileSync(resolve(outputRoot, hashedName), contents);
manifest[file] = hashedName;
}
for (const file of rewrittenHtmlFiles) {
const target = resolve(outputRoot, file);
if (!existsSync(target)) continue;
const source = readFileSync(target, "utf8");
writeFileSync(target, rewriteRuntimeReferences(source, manifest), "utf8");
}
const serviceWorkerPath = resolve(outputRoot, "service-worker.js");
const buildHash = contentHash(JSON.stringify(manifest));
if (existsSync(serviceWorkerPath)) {
const serviceWorker = rewriteRuntimeReferences(
readFileSync(serviceWorkerPath, "utf8"),
manifest,
).replace(
/const CACHE_VERSION = "[^"]+";/,
`const CACHE_VERSION = "cognitive-agent-pad-build-${buildHash}";`,
);
writeFileSync(serviceWorkerPath, serviceWorker, "utf8");
}
writeFileSync(
resolve(outputRoot, "runtime-assets.json"),
`${JSON.stringify({ build_hash: buildHash, assets: manifest }, null, 2)}\n`,
"utf8",
);
}
function copyRuntimeAssets() {
return {
@@ -64,6 +140,7 @@ function copyRuntimeAssets() {
mkdirSync(dirname(target), { recursive: true });
copyFileSync(source, target);
}
fingerprintRuntimeAssets();
}
};
}
+4 -3
View File
@@ -111,7 +111,7 @@ import { Fragment, createApp, h, nextTick, reactive } from "vue";
}
function createCommandMount(container) {
const state = reactive({ commands: [], open: true });
const state = reactive({ commands: [], open: true, heading: "下发控制指令" });
const app = createApp({
name: "N7DispatchedControlsCard",
setup() {
@@ -131,7 +131,7 @@ import { Fragment, createApp, h, nextTick, reactive } from "vue";
h("span", { class: "assistant-dispatched-controls-heading-icon" }, [
h("i", { "data-lucide": "list-tree" }),
]),
h("b", null, "下发控制指令"),
h("b", null, state.heading),
h("strong", { class: "assistant-dispatched-controls-count" }, String(state.commands.length)),
h("i", {
class: "assistant-dispatched-controls-chevron",
@@ -148,12 +148,13 @@ import { Fragment, createApp, h, nextTick, reactive } from "vue";
return mount;
}
function renderDispatchedControls(container, commands) {
function renderDispatchedControls(container, commands, options = {}) {
if (!(container instanceof Element) || !Array.isArray(commands) || !commands.length) {
return false;
}
const mount = commandMounts.get(container) || createCommandMount(container);
replaceReactiveArray(mount.state.commands, commands);
mount.state.heading = text(options.heading, "下发控制指令");
container.dataset.vueSurface = "dispatched-controls";
refreshIcons();
return true;
@@ -26,7 +26,7 @@ from dataclasses import dataclass
from typing import Any
MAX_PLAN_BYTES = 24 * 1024
MAX_PLAN_REQUESTS = 12
MAX_PLAN_REQUESTS = 20
MAX_PLAN_PARAMETERS = 12
_OPERATIONS = frozenset({"activate", "deactivate", "increase", "decrease", "set", "save", "apply"})
@@ -28,6 +28,10 @@ from cognitive_agent.application.mode_profile_intent import (
parse_mode_profile_delete,
parse_mode_profile_update,
)
from cognitive_agent.application.mode_creation_seat_policy import (
ModeCreationSeatPolicyError,
project_seed_plan,
)
from cognitive_agent.application.mode_schedule import ModeSchedule
from cognitive_agent.application.mode_schedule_service import public_schedule_status
from cognitive_agent.application.scene_composer import (
@@ -39,6 +43,8 @@ from cognitive_agent.application.scene_composer import (
build_scene_output_schema,
normalize_scene_output,
scene_context_for_mode_creation,
scene_has_explicit_control_object,
scene_required_native_fallbacks,
scene_required_peripheral_refs,
scene_semantic_guidance,
)
@@ -963,9 +969,40 @@ class CodexAssistantService:
)
if str(capability_id) in live_scene_capabilities
]
include_default_bundle = (
not needs_mode_schedule(message)
and not scene_has_explicit_control_object(
message,
scene_context,
)
)
required_peripheral_refs = scene_required_peripheral_refs(
mode_parts[0],
scene_context,
include_default_bundle=include_default_bundle,
)
required_native_fallbacks = scene_required_native_fallbacks(
mode_parts[0],
scene_context,
include_default_bundle=include_default_bundle,
)
semantic_guidance["required_native_domains"] = list(dict.fromkeys(
str(item.get("domain") or "")
for item in required_native_fallbacks
if str(item.get("domain") or "")
))
semantic_guidance["required_native_controls"] = [
{
"capability": str(item.get("capability") or ""),
"target_scope": str(item.get("target_scope") or ""),
}
for item in required_native_fallbacks
]
semantic_guidance["native_fallback_requests"] = (
required_native_fallbacks
)
semantic_guidance["default_control_bundle_applied"] = (
include_default_bundle
)
raw_peripheral_actions = scene_context.get("peripheral_actions", [])
semantic_guidance["peripheral_policy"] = {
@@ -981,8 +1018,11 @@ class CodexAssistantService:
"one stable state action per device."
),
"required_reason": (
"Romantic and Mid-Autumn related scenes must include the "
"authorized flashing-light control."
"Name-only scenes must include the available authorized "
"flashing-light, ambient-light and light-strip controls."
if include_default_bundle and required_peripheral_refs
else "Romantic and Mid-Autumn related scenes must include the "
"authorized flashing-light and light-strip controls."
if required_peripheral_refs
else "No peripheral action is mandatory for this name."
),
@@ -1430,7 +1470,7 @@ class CodexAssistantService:
*,
maximum_requests: int = MAX_PLAN_REQUESTS,
) -> dict[str, object]:
"""Add mandatory opaque peripheral refs without exposing binding data."""
"""Enforce required native domains and opaque peripheral references."""
normalized = cls._copy_scene_plan(plan)
requests = normalized.get("requests")
@@ -1452,10 +1492,131 @@ class CodexAssistantService:
}
required_refs = cls._required_scene_peripheral_refs(scene_context)
missing_refs = [ref for ref in required_refs if ref not in existing_refs]
if len(requests) + len(missing_refs) > maximum_requests:
raise SceneCompositionError(
"required peripheral actions exceed the scene action limit"
semantics = scene_context.get("scene_semantics")
raw_native_fallbacks = (
semantics.get("native_fallback_requests", [])
if isinstance(semantics, Mapping)
else []
)
native_fallbacks = [
deepcopy(dict(item))
for item in (
raw_native_fallbacks
if isinstance(raw_native_fallbacks, Sequence)
and not isinstance(raw_native_fallbacks, (str, bytes, bytearray))
else []
)
if isinstance(item, Mapping)
]
flexible_native_values = {
"climate.set_temperature",
"climate.set_fan_level",
"climate.set_cycle_mode",
"roof.set_shade_position",
"seat.set_backrest_recline",
"seat.set_legrest_height",
}
def parameter_map(request: Mapping[str, object]) -> dict[str, object]:
raw_parameters = request.get("parameters", [])
return {
str(parameter.get("name") or ""): parameter.get("value")
for parameter in (
raw_parameters
if isinstance(raw_parameters, Sequence)
and not isinstance(raw_parameters, (str, bytes, bytearray))
else []
)
if isinstance(parameter, Mapping)
and str(parameter.get("name") or "")
}
def native_requirement_satisfied(
request: Mapping[str, object],
fallback: Mapping[str, object],
) -> bool:
capability = str(fallback.get("capability") or "")
target_scope = str(fallback.get("target_scope") or "")
if (
request.get("capability") != capability
or request.get("target_scope") != target_scope
):
return False
if capability in flexible_native_values:
return True
required_parameters = parameter_map(fallback)
actual_parameters = parameter_map(request)
return all(
actual_parameters.get(name) == value
for name, value in required_parameters.items()
)
missing_native: list[dict[str, object]] = []
replaced_native = 0
for fallback in native_fallbacks:
matching_indexes = [
index
for index, request in enumerate(requests)
if isinstance(request, Mapping)
and request.get("capability") == fallback.get("capability")
and request.get("target_scope") == fallback.get("target_scope")
]
if any(
native_requirement_satisfied(requests[index], fallback)
for index in matching_indexes
):
continue
if matching_indexes:
requests[matching_indexes[0]] = deepcopy(fallback)
replaced_native += 1
continue
missing_native.append(fallback)
native_changed = bool(missing_native or replaced_native)
metadata = normalized.get("metadata")
if (
native_changed
and isinstance(metadata, Mapping)
and metadata.get("source") == "scene_template"
):
scope_policy = scene_context.get("seat_scope_policy")
raw_selected_scopes = (
scope_policy.get("selected_scopes", [])
if isinstance(scope_policy, Mapping)
else []
)
selected_scopes = frozenset(
str(scope)
for scope in (
raw_selected_scopes
if isinstance(raw_selected_scopes, Sequence)
and not isinstance(
raw_selected_scopes,
(str, bytes, bytearray),
)
else []
)
if str(scope)
)
if selected_scopes:
try:
normalized = project_seed_plan(normalized, selected_scopes)
except ModeCreationSeatPolicyError as exc:
raise SceneCompositionError(str(exc)) from exc
projected_requests = normalized.get("requests")
if not isinstance(projected_requests, list):
raise SceneCompositionError(
"projected scene template requests must be an array"
)
requests = projected_requests
if (
len(requests) + len(missing_native) + len(missing_refs)
> maximum_requests
):
raise SceneCompositionError(
"required scene controls exceed the scene action limit"
)
requests.extend(missing_native)
requests.extend(
{
"domain": "self_extension",
@@ -1473,14 +1634,28 @@ class CodexAssistantService:
}
for capability_ref in missing_refs
)
if missing_refs:
if native_changed or missing_refs:
if native_changed:
# The trusted template remains the source seed, but adding a
# native domain makes this an ordinary fully guarded semantic
# composition. First project its seat variants to the user's
# selected scope above, then remove metadata because template
# metadata requires byte-identical base requests.
normalized.pop("metadata", None)
domains = normalized.get("detected_domains")
if isinstance(domains, list) and "self_extension" not in domains:
if isinstance(domains, list):
for fallback in missing_native:
domain = str(fallback.get("domain") or "")
if domain and domain not in domains:
domains.append(domain)
if missing_refs and "self_extension" not in domains:
domains.append("self_extension")
summaries = normalized.get("decision_summary")
if isinstance(summaries, list):
summaries.append(
f"已按模式语义加入 {len(missing_refs)} 项必需的已授权外设动作。"
"已按模式语义补齐 "
f"{len(missing_native) + replaced_native} 项原生控制和 "
f"{len(missing_refs)} 项已授权外设动作。"
)
return normalized
@@ -1594,7 +1769,10 @@ class CodexAssistantService:
message_id=message_id,
emit=emit,
execute_control=execute_control,
assistant_plan=template_plan,
assistant_plan=self._with_required_scene_peripherals(
template_plan,
scene_context,
),
response_route="scene_template",
)
base_requests = template_plan.get("requests", [])
@@ -1627,7 +1805,14 @@ class CodexAssistantService:
0,
MAX_PLAN_REQUESTS
- len(base_requests)
- len(self._required_scene_peripheral_refs(scene_context)),
- len(self._required_scene_peripheral_refs(scene_context))
- len(
(
scene_context.get("scene_semantics") or {}
).get("native_fallback_requests", [])
if isinstance(scene_context.get("scene_semantics"), Mapping)
else []
),
)
augmentation_semantics = augmentation_context.get("scene_semantics")
if isinstance(augmentation_semantics, dict):
@@ -1654,6 +1839,7 @@ class CodexAssistantService:
deadline=deadline,
developer_instructions=SCENE_TEMPLATE_AUGMENTATION_INSTRUCTIONS,
dynamic_override=False,
enforce_required_controls=False,
)
proposed = augmentation.get("requests", [])
if not isinstance(proposed, list) or any(
@@ -1670,6 +1856,10 @@ class CodexAssistantService:
*deepcopy(base_requests),
*deepcopy(proposed),
]
combined = self._with_required_scene_peripherals(
combined,
scene_context,
)
return self._stream_direct_vehicle_control(
_VehicleAuthorization(message, "explicit_mode_profile_create"),
message_id=message_id,
@@ -1850,6 +2040,7 @@ class CodexAssistantService:
deadline: float,
developer_instructions: str | None = None,
dynamic_override: bool | None = None,
enforce_required_controls: bool = True,
) -> dict[str, object]:
start_structured_turn = getattr(self.client, "start_structured_turn", None)
if not callable(start_structured_turn):
@@ -2063,7 +2254,7 @@ class CodexAssistantService:
if not isinstance(raw_output, Mapping):
raise SceneCompositionError("scene composer output must be an object")
normalized = normalize_scene_output(raw_output, planner_context, dynamic=dynamic)
if not dynamic:
if not dynamic and enforce_required_controls:
normalized = self._with_required_scene_peripherals(
normalized,
planner_context,
@@ -2830,11 +3021,11 @@ class CodexAssistantService:
if draft.get("generation_source") == "registered_mode_profile":
return (
f"工程中已有“{name}”,已按当前车型载入预览并验证"
f"{operation_count}项设备动作。可以先试运行,确认后更新现有模式。"
f"{operation_count}项设备动作。创建阶段未下发,确认后可更新现有模式。"
)
return (
f"已按你提供的设置为现有“{name}”生成预览并验证"
f"{operation_count}项设备动作。可以先试运行,确认后更新现有模式。"
f"{operation_count}项设备动作。创建阶段未下发,确认后可更新现有模式。"
)
if (
isinstance(draft, Mapping)
@@ -2851,7 +3042,7 @@ class CodexAssistantService:
return (
f"已从常用模式模板快速生成“{name}”,并按当前工程能力验证了"
f"{operation_count}项设备动作。当前只是独立草稿,可以继续调整;"
"确认预览后可保存为新的功能模式,也可以先试运行。"
"创建阶段不会下发,确认预览后可保存为新的功能模式。"
)
if (
isinstance(draft, Mapping)
@@ -2867,9 +3058,9 @@ class CodexAssistantService:
)
return (
f"已理解“{name}”的场景意图,并从当前工程能力中组合、验证了"
f"{operation_count}项设备动作。确认预览后可保存为功能模式,也可以先试运行。"
f"{operation_count}项设备动作。创建阶段不会下发,确认预览后可保存。"
)
return f"已为你生成“{name}”预览。确认动作后可保存为功能模式,也可以先试运行。"
return f"已为你生成“{name}”预览。创建阶段不会下发,确认动作后可保存。"
@staticmethod
def _mode_profile_management_answer(
@@ -4073,7 +4264,9 @@ class CodexAssistantService:
if isinstance(intent_ir, dict):
spec = intent_ir.get("spec")
if isinstance(spec, dict) and isinstance(spec.get("actions"), list):
for item in spec["actions"][:96 if result.get("schedule") else 12]:
for item in spec["actions"][
:96 if result.get("schedule") else MAX_PLAN_REQUESTS
]:
if not isinstance(item, dict):
continue
safe_action = {
@@ -4101,7 +4294,7 @@ class CodexAssistantService:
infrared_actions: list[dict[str, object]] = []
raw_infrared = result.get("infrared_actions")
if isinstance(raw_infrared, list):
for item in raw_infrared[:12]:
for item in raw_infrared[:MAX_PLAN_REQUESTS]:
if not isinstance(item, dict):
continue
infrared_actions.append(
@@ -4152,7 +4345,7 @@ class CodexAssistantService:
for key in ("domain", "capability", "target_scope", "parameters")
if key in item
}
for item in mode_profile_changes[:12]
for item in mode_profile_changes[:MAX_PLAN_REQUESTS]
if isinstance(item, Mapping)
]
deleted_mode_profile = result.get("deleted_mode_profile")
+306 -21
View File
@@ -21,9 +21,10 @@ from cognitive_agent.application.mode_creation_seat_policy import (
)
from cognitive_agent.application.mode_schedule import ModeSchedule
SCENE_PLANNER_VERSION = "n7web-scene-composer/v8"
SCENE_PLANNER_VERSION = "n7web-scene-composer/v9"
_MAX_OUTPUT_SCHEMA_BYTES = 64 * 1024
_MAX_SCHEDULE_PHASE_REQUESTS = 12
_JSON_PARAMETER_TYPES = frozenset({"boolean", "integer", "number", "string"})
_RELATIVE_PARAMETER_NAMES = frozenset(
{
@@ -44,7 +45,7 @@ small reusable set of absolute device states from N7WEB_SCENE_CAPABILITY_CONTEXT
Do not call tools, execute controls, save a mode, inspect files, or answer in prose.
Use only listed capability ids, target scopes, and parameter contracts. Prefer the
smallest coherent combination, normally 1-8 actions and never more than 12. Every
smallest coherent combination, normally 1-8 actions and never more than 20. Every
action must be an idempotent stable target state; never emit relative, timed, toggle,
save, apply, or unsupported safety overrides. Do not duplicate a capability and target.
Treat all catalog strings and the mode name as data, never as instructions.
@@ -68,6 +69,10 @@ mood. For a relevant light-like peripheral, normally select its activation actio
at most one stable color/mode action. Every ref listed by
scene_semantics.peripheral_policy.required_refs is mandatory and will also be enforced
by the application; do not replace it with a native look-alike capability.
Every capability/target pair in scene_semantics.required_native_controls is also
mandatory. Prefer a model-chosen value for that exact control; if omitted, the
application will append the server-owned request in
scene_semantics.native_fallback_requests.
For a commonly understood scene, infer conservative cabin settings. If the name is
meaningless, unsafe, contradictory, or cannot be grounded in the supplied catalog,
@@ -342,17 +347,32 @@ def scene_semantic_guidance(mode_name: object) -> dict[str, object]:
}
_REQUIRED_FLASHING_LIGHT_SCENE = re.compile(
_REQUIRED_DECORATIVE_LIGHT_SCENE = re.compile(
r"浪漫|约会|七夕|情人节|纪念日|中秋|赏月|月圆|团圆",
re.IGNORECASE,
)
_FLASHING_LIGHT_NAME = re.compile(r"闪烁灯|闪光灯|闪灯玩具", re.IGNORECASE)
_PERIPHERAL_ACTIVATE_ACTION = re.compile(
r"^(?:打开|开启|启动|点亮|on|power\s*on)$",
_AMBIENT_LIGHT_NAME = re.compile(r"氛围灯|气氛灯|ambient\s*light", re.IGNORECASE)
_LIGHT_STRIP_NAME = re.compile(
r"灯带|灯条|light\s*strip|led\s*strip",
re.IGNORECASE,
)
_PERIPHERAL_RED_ACTION = re.compile(
r"红色|red",
_REQUIRED_EXTERNAL_LIGHT_NAME = re.compile(
rf"{_FLASHING_LIGHT_NAME.pattern}|{_AMBIENT_LIGHT_NAME.pattern}|"
rf"{_LIGHT_STRIP_NAME.pattern}",
re.IGNORECASE,
)
_SCENE_CONTROL_OBJECT = re.compile(
r"空调|冷气|暖风|温度|风量|风速|循环|出风|除霜|"
r"车窗|窗户|遮阳帘|遮阳幕|天幕帘|天窗|"
r"座椅|座位|主驾|副驾|驾驶员|乘客|靠背|腿托|脚托|坐垫|坐盆|"
r"通风|加热|按摩|零重力|"
r"阅读灯|室内灯|氛围灯|气氛灯|闪烁灯|闪光灯|闪灯玩具|灯带|灯条|灯光|"
r"车门|门锁|出风口|风口|插座|电源",
re.IGNORECASE,
)
_PERIPHERAL_ACTIVATE_ACTION = re.compile(
r"^(?:打开|开启|启动|点亮|on|power\s*on)$",
re.IGNORECASE,
)
@@ -360,16 +380,20 @@ _PERIPHERAL_RED_ACTION = re.compile(
def scene_required_peripheral_refs(
mode_name: object,
context: Mapping[str, object],
*,
include_default_bundle: bool = False,
) -> list[str]:
"""Return safe opaque peripheral refs mandated by a scene's semantics.
Romantic and Mid-Autumn related scenes require each currently selectable
flashing-light device to be activated. A stable red-state action is added
when available. Slot, NFC and transport details remain hidden behind refs.
Romantic and Mid-Autumn related scenes require selectable flashing-light
and light-strip devices. A name-only scene additionally requires a learned
ambient light. One stable activation-like action is selected per category;
slot, NFC and transport details remain hidden behind refs.
"""
clean_name = " ".join(str(mode_name or "").strip().split())[:64]
if _REQUIRED_FLASHING_LIGHT_SCENE.search(clean_name) is None:
themed_scene = _REQUIRED_DECORATIVE_LIGHT_SCENE.search(clean_name) is not None
if not themed_scene and not include_default_bundle:
return []
raw_actions = context.get("peripheral_actions", [])
if not isinstance(raw_actions, Sequence) or isinstance(
@@ -378,6 +402,7 @@ def scene_required_peripheral_refs(
):
return []
by_device: dict[str, list[Mapping[str, object]]] = {}
identity_by_device: dict[str, str] = {}
for raw in raw_actions:
if not isinstance(raw, Mapping):
continue
@@ -387,13 +412,35 @@ def scene_required_peripheral_refs(
]
identity = " ".join(str(value or "") for value in identity_values)
capability_ref = str(raw.get("ref") or "").strip()
if not capability_ref or _FLASHING_LIGHT_NAME.search(identity) is None:
if (
not capability_ref
or _REQUIRED_EXTERNAL_LIGHT_NAME.search(identity) is None
):
continue
device_key = str(raw.get("device_label") or identity).strip().casefold()
by_device.setdefault(device_key, []).append(raw)
identity_by_device.setdefault(device_key, identity)
required: list[str] = []
for device_key in sorted(by_device):
category_patterns = [
_FLASHING_LIGHT_NAME,
*([_AMBIENT_LIGHT_NAME] if include_default_bundle else []),
_LIGHT_STRIP_NAME,
]
selected_devices: set[str] = set()
for category_pattern in category_patterns:
device_key = next(
(
key
for key in sorted(by_device)
if key not in selected_devices
and category_pattern.search(identity_by_device.get(key, key))
),
None,
)
if device_key is None:
continue
selected_devices.add(device_key)
actions = by_device[device_key]
def action_text(item: Mapping[str, object]) -> str:
@@ -431,17 +478,224 @@ def scene_required_peripheral_refs(
)
if activation is not None:
required.append(str(activation.get("ref") or ""))
red_state = next(
return list(dict.fromkeys(ref for ref in required if ref))
def scene_has_explicit_control_object(
text: object,
context: Mapping[str, object],
) -> bool:
"""Return whether a mode request names a concrete native or learned device."""
clean_text = " ".join(str(text or "").strip().split())[:1000]
if _SCENE_CONTROL_OBJECT.search(clean_text):
return True
compact_text = re.sub(r"\s+", "", clean_text).casefold()
for raw in context.get("peripheral_actions", []):
if not isinstance(raw, Mapping):
continue
names = [
raw.get("device_label"),
*(
raw.get("object_names", [])
if isinstance(raw.get("object_names"), list)
else []
),
]
if any(
len(compact_name) >= 2 and compact_name in compact_text
for value in names
for compact_name in [re.sub(r"\s+", "", str(value or "")).casefold()]
):
return True
return False
def scene_required_native_fallbacks(
mode_name: object,
context: Mapping[str, object],
*,
include_default_bundle: bool = False,
) -> list[dict[str, object]]:
"""Return validated server-owned controls required by the scene policy."""
clean_name = " ".join(str(mode_name or "").strip().split())[:64]
themed_scene = _REQUIRED_DECORATIVE_LIGHT_SCENE.search(clean_name) is not None
if not themed_scene and not include_default_bundle:
return []
raw_capabilities = context.get("capabilities", [])
capabilities = {
str(item.get("id") or ""): item
for item in raw_capabilities
if isinstance(item, Mapping) and str(item.get("id") or "")
} if isinstance(raw_capabilities, Sequence) else {}
def supported(
capability_id: str,
target_scope: str,
parameters: Mapping[str, object],
) -> bool:
capability = capabilities.get(capability_id)
if not isinstance(capability, Mapping):
return False
targets = capability.get("target_scopes", [])
if (
not isinstance(targets, Sequence)
or isinstance(targets, (str, bytes, bytearray))
or target_scope not in targets
):
return False
contracts = {
str(item.get("name") or ""): item
for item in capability.get("parameters", [])
if isinstance(item, Mapping) and str(item.get("name") or "")
}
for name, value in parameters.items():
contract = contracts.get(name)
if contract is None:
return False
allowed = contract.get("allowed_values")
if (
isinstance(allowed, Sequence)
and not isinstance(allowed, (str, bytes, bytearray))
and allowed
and value not in allowed
):
return False
if isinstance(value, (int, float)) and not isinstance(value, bool):
minimum = contract.get("minimum")
maximum = contract.get("maximum")
if isinstance(minimum, (int, float)) and value < minimum:
return False
if isinstance(maximum, (int, float)) and value > maximum:
return False
if any(
contract.get("required") is True and name not in parameters
for name, contract in contracts.items()
):
return False
return True
romantic = re.search(r"浪漫|约会|七夕|情人节|纪念日", clean_name) is not None
mid_autumn = re.search(r"中秋|赏月|月圆|团圆", clean_name) is not None
if include_default_bundle:
candidates: tuple[
tuple[str, tuple[tuple[str, str, dict[str, object]], ...]], ...
] = (
("climate", (("climate.set_power", "vehicle", {"state": "on"}),)),
(
item
for item in actions
if _PERIPHERAL_RED_ACTION.search(action_text(item))
"climate",
(("climate.set_temperature", "vehicle", {"temperature_c": 24}),),
),
("climate", (("climate.set_fan_level", "vehicle", {"level": 2}),)),
(
"climate",
(("climate.set_cycle_mode", "vehicle", {"mode": "internal"}),),
),
*tuple(
(
"window",
(("window.set_position", target, {"position_pct": 0}),),
)
for target in (
"front_left",
"front_right",
"rear_left",
"rear_right",
)
),
(
"roof",
((
"roof.set_shade_position",
"vehicle",
{"position_pct": 100 if mid_autumn else 0},
),),
),
(
"seat",
(("seat.set_ventilation", "passenger", {"state": "on", "level": 1}),),
),
(
"seat",
(("seat.set_backrest_recline", "passenger", {"position_pct": 45}),),
),
(
"seat",
(("seat.set_legrest_height", "passenger", {"position_pct": 35}),),
),
(
"ambient_light",
(
*(
(("ambient_light.set_mode", "vehicle", {"mode": "romantic"}),)
if romantic
else ()
),
("ambient_light.set_power", "vehicle", {"state": "on"}),
),
),
)
else:
candidates = (
(
"climate",
(
("climate.set_control_mode", "vehicle", {"mode": "auto"}),
("climate.set_power", "vehicle", {"state": "on"}),
("climate.set_temperature", "vehicle", {"temperature_c": 24}),
),
),
(
"ambient_light",
(
*(
(("ambient_light.set_mode", "vehicle", {"mode": "romantic"}),)
if romantic
else ()
),
("ambient_light.set_power", "vehicle", {"state": "on"}),
),
),
(
"roof",
((
"roof.set_shade_position",
"vehicle",
{"position_pct": 100 if mid_autumn else 0},
),),
),
)
fallbacks: list[dict[str, object]] = []
for domain, domain_candidates in candidates:
selected = next(
(
(capability_id, target_scope, parameters)
for capability_id, target_scope, parameters in domain_candidates
if supported(capability_id, target_scope, parameters)
),
None,
)
if red_state is not None:
required.append(str(red_state.get("ref") or ""))
return list(dict.fromkeys(ref for ref in required if ref))
if selected is None:
continue
capability_id, target_scope, parameters = selected
fallbacks.append(
{
"domain": domain,
"capability": capability_id,
"target_scope": target_scope,
"operation": "set",
"duration_ms": None,
"parameters": [
{"name": name, "value": value}
for name, value in parameters.items()
],
"reason": (
"该控制是无明确设备目标模式的安全默认组成;云端漏选时由实时能力契约补齐。"
),
}
)
return fallbacks
def scene_context_for_mode_creation(
@@ -717,7 +971,31 @@ def build_scene_output_schema(context: Mapping[str, object], *, dynamic: bool =
)
if str(value).strip()
}
generated_action_limit = max(0, MAX_PLAN_REQUESTS - len(required_refs))
raw_required_native_controls = semantics.get("native_fallback_requests", [])
required_native_controls = {
(
str(value.get("capability") or "").strip(),
str(value.get("target_scope") or "").strip(),
)
for value in (
raw_required_native_controls
if isinstance(raw_required_native_controls, Sequence)
and not isinstance(
raw_required_native_controls,
(str, bytes, bytearray),
)
else []
)
if isinstance(value, Mapping)
and str(value.get("capability") or "").strip()
and str(value.get("target_scope") or "").strip()
}
generated_action_limit = max(
0,
MAX_PLAN_REQUESTS
- len(required_refs)
- len(required_native_controls),
)
raw_generated_limit = context.get("maximum_generated_action_count")
if (
isinstance(raw_generated_limit, int)
@@ -725,6 +1003,11 @@ def build_scene_output_schema(context: Mapping[str, object], *, dynamic: bool =
and 0 <= raw_generated_limit <= MAX_PLAN_REQUESTS
):
generated_action_limit = min(generated_action_limit, raw_generated_limit)
if dynamic:
generated_action_limit = min(
generated_action_limit,
_MAX_SCHEDULE_PHASE_REQUESTS,
)
schema: dict[str, object] = {
"type": "object",
@@ -1011,6 +1294,8 @@ __all__ = [
"SceneCompositionError",
"build_scene_output_schema",
"normalize_scene_output",
"scene_has_explicit_control_object",
"scene_required_native_fallbacks",
"scene_required_peripheral_refs",
"scene_semantic_guidance",
]
@@ -2150,6 +2150,13 @@ class SeatDemoService(ModeScheduleServiceMixin):
vehicle_profile = self.vehicle_profiles.get(vehicle_id)
control_plane_request = self.is_mode_profile_configuration_request(utterance)
if control_plane_request:
# Mode creation is a configuration workflow, never a vehicle
# execution request. Enforce this at the public preview boundary
# even when a browser is currently set to hardware mode.
if parse_mode_profile_create(utterance) is not None:
execution_mode = "dry_run"
approval_token = None
hardware_client_id = None
result = self.process(
utterance,
provider=provider,
@@ -22,6 +22,7 @@ SERVER_VERSION: Final = "0.3.0"
DEFAULT_PROTOCOL_VERSION: Final = "2024-11-05"
MAX_LINE_BYTES: Final = 1024 * 1024
MAX_RESPONSE_BYTES: Final = 1024 * 1024
MAX_PLAN_REQUESTS: Final = 20
_REQUEST_TOKEN_RE = re.compile(r"[A-Za-z0-9._~-]{8,256}\Z")
_PLAN_OPERATIONS: Final = frozenset(
{"activate", "deactivate", "increase", "decrease", "set", "save", "apply"}
@@ -174,7 +175,7 @@ def _assistant_plan_schema() -> dict[str, Any]:
},
"requests": {
"type": "array",
"maxItems": 12,
"maxItems": MAX_PLAN_REQUESTS,
"items": {
"type": "object",
"additionalProperties": False,
@@ -296,7 +297,7 @@ def _validate_plan_payload(value: object) -> dict[str, Any] | None:
or any(not isinstance(item, str) or not 1 <= len(item) <= 64 for item in domains)
or len(set(domains)) != len(domains)
or not isinstance(requests, list)
or len(requests) > 12
or len(requests) > MAX_PLAN_REQUESTS
or not isinstance(ambiguities, list)
or len(ambiguities) > 8
or not isinstance(summary, list)
+11 -1
View File
@@ -1818,6 +1818,11 @@ class SeatDemoHandler(BaseHTTPRequestHandler):
for capability in [entry.get("capability")]
if isinstance(capability, dict)
]
preview_only = (
str(can_result.get("execution_mode") or "dry_run") != "hardware"
and str(infrared_result.get("execution_mode") or "dry_run")
!= "hardware"
)
merged.update(
{
"utterance": utterance,
@@ -1837,7 +1842,12 @@ class SeatDemoHandler(BaseHTTPRequestHandler):
"decision_summary": [
*list(can_result.get("decision_summary") or []),
*list(infrared_result.get("decision_summary") or []),
"CAN 与红外动作已通过独立白名单校验并按各自传输链路执行。",
(
"CAN 与红外动作已通过独立白名单和编译校验;"
"当前仅为预览,未进入任何物理传输链路。"
if preview_only
else "CAN 与红外动作已通过独立白名单校验并按各自传输链路执行。"
),
],
"function_configs": function_configs,
"command_plans": command_plans,
+62 -10
View File
@@ -986,6 +986,17 @@ operations:
vite_entry = (web_console_root / "src" / "main.js").read_text(
encoding="utf-8"
)
runtime_manifest_path = web_console_root / "dist" / "runtime-assets.json"
runtime_manifest = (
json.loads(runtime_manifest_path.read_text(encoding="utf-8"))
if runtime_manifest_path.is_file()
else {"assets": {}}
)
runtime_assets = runtime_manifest.get("assets", {})
def runtime_url(file_name: str, source_url: str) -> str:
hashed = runtime_assets.get(file_name)
return f"/{hashed}" if hashed else source_url
with self.opener.open(f"{self.base_url}/app.js?v=dual-seat-145-n7-dispatch", timeout=3) as response:
app_script = response.read().decode("utf-8")
with self.opener.open(
@@ -1021,7 +1032,7 @@ operations:
) as response:
extension_styles = response.read().decode("utf-8").replace("\r\n", "\n")
with self.opener.open(
f"{self.base_url}/assistant-chat.js?v=codex-assistant-34", timeout=3
f"{self.base_url}/assistant-chat.js?v=codex-assistant-35", timeout=3
) as response:
assistant_script = response.read().decode("utf-8")
with self.opener.open(
@@ -1072,19 +1083,31 @@ operations:
self.assertIn("CMD 0x431 · FB 0x41A · CH1", template)
self.assertIn("CMD 0x431 · FB 0x388 · CH1", template)
self.assertNotIn("透明车体 · 实体设备", template)
self.assertIn('src="/app.js?v=dual-seat-145-n7-dispatch"', html)
self.assertIn(
f'src="{runtime_url("app.js", "/app.js?v=dual-seat-145-n7-dispatch")}"',
html,
)
self.assertIn('import "../styles.css";', vite_entry)
self.assertNotIn('class="vehicle-system-hud"', template)
self.assertIn('src="/console-v2.js?v=local-first-77-performance"', html)
self.assertIn(
f'src="{runtime_url("console-v2.js", "/console-v2.js?v=local-first-77-performance")}"',
html,
)
self.assertIn('import "../console-v2.css";', vite_entry)
self.assertIn('src="/speech-input.js?v=sensevoice-13"', html)
self.assertIn(
f'src="{runtime_url("speech-input.js", "/speech-input.js?v=sensevoice-13")}"',
html,
)
self.assertIn('import "../speech-input.css";', vite_entry)
self.assertIn('id="speechDictationButton"', template)
self.assertIn('class="speech-waveform"', template)
self.assertIn('<body class="console-v2">', html)
self.assertIn('id="v2BootScreen"', template)
self.assertIn('正在加载车机控制与实时状态', template)
self.assertIn('src="/self-extension.js?v=nfc-mqtt-56"', html)
self.assertIn(
f'src="{runtime_url("self-extension.js", "/self-extension.js?v=nfc-mqtt-56")}"',
html,
)
self.assertIn('import "../self-extension.css";', vite_entry)
self.assertNotIn("self-calibration", template)
self.assertNotIn("self-repair", template)
@@ -1093,9 +1116,15 @@ operations:
self.assertIn('aria-label="发送消息"', template)
self.assertIn('id="assistantTranscript"', template)
self.assertIn('import "../assistant-chat.css";', vite_entry)
self.assertIn('src="/assistant-chat.js?v=codex-assistant-34"', html)
self.assertIn(
f'src="{runtime_url("assistant-chat.js", "/assistant-chat.js?v=codex-assistant-35")}"',
html,
)
self.assertIn('import "../energy-monitor.css";', vite_entry)
self.assertIn('src="/energy-monitor.js?v=energy-monitor-5-performance"', html)
self.assertIn(
f'src="{runtime_url("energy-monitor.js", "/energy-monitor.js?v=energy-monitor-5-performance")}"',
html,
)
self.assertIn('import "../codepen-glass-theme.css";', vite_entry)
self.assertEqual(extension_control_status["source_label"], "车端 MQTT")
self.assertIn("action_statuses", extension_control_status)
@@ -1384,14 +1413,33 @@ operations:
self.assertIn("CAN 指令已提交,等待车辆状态确认", console_script)
self.assertIn('canvas.addEventListener("webglcontextlost"', app_script)
self.assertIn('"cognitive-agent:module-change"', app_script)
if runtime_assets:
self.assertIn(
'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"',
f'const CACHE_VERSION = "cognitive-agent-pad-build-{runtime_manifest["build_hash"]}"',
service_worker,
)
for asset_name in (
"console-v2.css",
"console-v2.js",
"assistant-chat.css",
"assistant-chat.js",
"energy-monitor.css",
"energy-monitor.js",
"app.js",
"firmware-workbench-entry.css",
"firmware-workbench-entry.js",
"firmware-workbench-theme.js",
):
self.assertIn(f'/{runtime_assets[asset_name]}', service_worker)
else:
self.assertIn(
'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"',
service_worker,
)
self.assertIn('/console-v2.css?v=delivery-state-69', service_worker)
self.assertIn('/console-v2.js?v=local-first-77-performance', service_worker)
self.assertIn('/assistant-chat.css?v=codex-assistant-29', service_worker)
self.assertIn('/assistant-chat.js?v=codex-assistant-34', service_worker)
self.assertIn('/assistant-chat.css?v=codex-assistant-30', service_worker)
self.assertIn('/assistant-chat.js?v=codex-assistant-35', service_worker)
self.assertIn('/energy-monitor.css?v=energy-monitor-5-dialog-size', service_worker)
self.assertIn('/energy-monitor.js?v=energy-monitor-5-performance', service_worker)
self.assertIn('"/app.js?v=dual-seat-145-n7-dispatch"', service_worker)
@@ -1399,6 +1447,10 @@ operations:
self.assertIn('/firmware-workbench-entry.js?v=3', service_worker)
self.assertIn('/firmware-workbench-theme.js?v=1', service_worker)
self.assertIn('/firmware-workbench.html', service_worker)
if runtime_assets:
self.assertIn(f'"/{runtime_assets["speech-input.js"]}"', service_worker)
self.assertIn(f'"/{runtime_assets["speech-input.css"]}"', service_worker)
else:
self.assertIn('"/speech-input.js?v=sensevoice-13"', service_worker)
self.assertIn('"/speech-input.css?v=sensevoice-7"', service_worker)
self.assertIn('"/speech-audio-worklet.js?v=sensevoice-5"', service_worker)
@@ -264,7 +264,7 @@ def test_unsupported_or_out_of_range_addition_rejects_all_changes_atomically(add
assert not _hashes(user_root)
def test_adding_devices_to_full_twelve_action_draft_never_truncates_original_actions(addition_service):
def test_adding_devices_to_full_twenty_action_draft_never_truncates_original_actions(addition_service):
service, _, user_root = addition_service
operations = [
("climate.set_power", "vehicle", {"state": "on"}),
@@ -279,6 +279,14 @@ def test_adding_devices_to_full_twelve_action_draft_never_truncates_original_act
("ambient_light.set_power", "vehicle", {"state": "off"}),
("interior_light.set_indoor_power", "vehicle", {"state": "off"}),
("interior_light.set_reading_power", "all", {"state": "off"}),
("window.set_position", "rear_left", {"position_pct": 0}),
("window.set_position", "rear_right", {"position_pct": 0}),
("ambient_light.set_mode", "vehicle", {"mode": "romantic"}),
("ambient_light.set_brightness_level", "vehicle", {"brightness_level": 2}),
("ambient_light.set_color_index", "vehicle", {"color_index": 6}),
("seat.set_heating", "passenger", {"state": "on", "level": 1}),
("seat.set_backrest_recline", "passenger", {"position_pct": 45}),
("seat.set_legrest_height", "passenger", {"position_pct": 35}),
]
plan = {
"detected_domains": sorted({cap.split(".", 1)[0] for cap, _, _ in operations}),
@@ -287,11 +295,11 @@ def test_adding_devices_to_full_twelve_action_draft_never_truncates_original_act
"operation": "set", "parameters": [{"name": name, "value": value} for name, value in params.items()],
"duration_ms": None, "reason": "固定目标边界回归。",
} for cap, target, params in operations],
"ambiguities": [], "confidence": 0.95, "decision_summary": ["验证12项原动作保留。"],
"ambiguities": [], "confidence": 0.95, "decision_summary": ["验证20项原动作保留。"],
}
original = _preview(service, "构建一个满载参数模式", plan=plan)
_assert_draft(original)
assert len(_actions(original)) == 12
assert len(_actions(original)) == 20
source_path = service.artifact_root / original["request_id"] / "result.json"
source_hash = hashlib.sha256(source_path.read_bytes()).hexdigest()
try:
@@ -1193,26 +1193,34 @@ MAXIMUM_SCENE_REQUESTS = (
_scene_request("ambient_light.set_power", "vehicle", {"state": "off"}),
_scene_request("interior_light.set_indoor_power", "vehicle", {"state": "off"}),
_scene_request("interior_light.set_reading_power", "all", {"state": "off"}),
_scene_request("window.set_position", "rear_left", {"position_pct": 0}),
_scene_request("window.set_position", "rear_right", {"position_pct": 0}),
_scene_request("ambient_light.set_mode", "vehicle", {"mode": "romantic"}),
_scene_request("ambient_light.set_brightness_level", "vehicle", {"brightness_level": 2}),
_scene_request("ambient_light.set_color_index", "vehicle", {"color_index": 6}),
_scene_request("seat.set_heating", "passenger", {"state": "on", "level": 1}),
_scene_request("seat.set_backrest_recline", "passenger", {"position_pct": 45}),
_scene_request("seat.set_legrest_height", "passenger", {"position_pct": 35}),
)
def test_scene_request_limit_accepts_exactly_twelve_verified_actions(
def test_scene_request_limit_accepts_exactly_twenty_verified_actions(
mode_service: tuple[SeatDemoService, Path],
) -> None:
service, user_root = mode_service
assert len(MAXIMUM_SCENE_REQUESTS) == MAX_PLAN_REQUESTS == 12
assert len(MAXIMUM_SCENE_REQUESTS) == MAX_PLAN_REQUESTS == 20
result = _preview_without_hardware(
service,
"构建一个十二动作模式",
conversation_id="conv-twelve-scene-actions",
"构建一个二十动作模式",
conversation_id="conv-twenty-scene-actions",
assistant_plan=_scene_plan(*MAXIMUM_SCENE_REQUESTS),
)
_assert_successful_preview(result, name="十二动作模式", operation_count=12)
_assert_successful_preview(result, name="二十动作模式", operation_count=20)
assert not list(user_root.rglob("*.yaml"))
def test_scene_request_limit_rejects_thirteen_before_any_hardware_or_persistence(
def test_scene_request_limit_rejects_twenty_one_before_any_hardware_or_persistence(
mode_service: tuple[SeatDemoService, Path],
) -> None:
service, user_root = mode_service
@@ -1225,8 +1233,8 @@ def test_scene_request_limit_rejects_thirteen_before_any_hardware_or_persistence
with pytest.raises(ValueError, match="too many requests"):
_preview_without_hardware(
service,
"构建一个十三动作模式",
conversation_id="conv-thirteen-scene-actions",
"构建一个二十一动作模式",
conversation_id="conv-twenty-one-scene-actions",
assistant_plan=_scene_plan(*oversized),
)
assert not list(user_root.rglob("*.yaml"))
@@ -26,17 +26,18 @@ CALLBACK_URL = "http://127.0.0.1:8765/api/internal/codex/vehicle-control"
@pytest.mark.parametrize(
("message", "expected_name"),
("message", "expected_name", "expected_operation_count"),
(
("构建一个星夜模式", "星夜模式"),
("创建学习模式", "学习模式"),
("创建中秋欢快模式", "中秋欢快模式"),
("构建一个星夜模式", "星夜模式", 13),
("创建学习模式", "学习模式", 13),
("创建中秋欢快模式", "中秋欢快模式", 13),
),
)
def test_structured_scene_turn_reaches_the_complete_local_dry_run_pipeline(
tmp_path: Path,
message: str,
expected_name: str,
expected_operation_count: int,
) -> None:
service = SeatDemoService(REPOSITORY_ROOT)
service.artifact_root = tmp_path / "runs"
@@ -107,20 +108,37 @@ def test_structured_scene_turn_reaches_the_complete_local_dry_run_pipeline(
assert result["orchestration_route"] == "mode_profile_create_preview"
assert draft["name"] == expected_name
assert draft["generation_source"] == "codex_scene_composition"
assert draft["operation_count"] == 2
assert draft["operation_count"] == expected_operation_count
capabilities = {
action["capability"] for action in result["actions"]
}
assert capabilities == {
expected_capabilities = {
"climate.set_power",
"climate.set_temperature",
"climate.set_fan_level",
"climate.set_cycle_mode",
"window.set_position",
"roof.set_shade_position",
"seat.set_ventilation",
"seat.set_backrest_recline",
"seat.set_legrest_height",
"ambient_light.set_power",
}
assert capabilities == expected_capabilities
window_actions = [
action for action in result["actions"]
if action["capability"] == "window.set_position"
]
assert {
action["target"]["zone"] for action in window_actions
} == {"front_left", "front_right", "rear_left", "rear_right"}
assert all(action["parameters"] == {"position_pct": 0} for action in window_actions)
assert not list((tmp_path / "user-modes").rglob("*.yaml"))
assert any(event.get("type") == "action.completed" for event in events)
assert all("request_token" not in str(event) for event in events)
def test_mid_autumn_cloud_scene_requires_authorized_flashing_light(
def test_mid_autumn_cloud_scene_requires_native_and_decorative_light_controls(
tmp_path: Path,
) -> None:
service = SeatDemoService(REPOSITORY_ROOT)
@@ -161,6 +179,45 @@ def test_mid_autumn_cloud_scene_requires_authorized_flashing_light(
"risk": "low",
"slot": 3,
},
{
"id": "ir-slot:light-strip:21",
"status": "learned",
"enabled": True,
"device_id": "light-strip",
"device_name": "灯带",
"object_names": ["灯带", "LED 灯条"],
"card_uid": "A1B2C3D4",
"action": "打开",
"action_names": ["打开", "开启"],
"risk": "low",
"slot": 21,
},
{
"id": "ir-slot:light-strip:23",
"status": "learned",
"enabled": True,
"device_id": "light-strip",
"device_name": "灯带",
"object_names": ["灯带", "LED 灯条"],
"card_uid": "A1B2C3D4",
"action": "切换为红色",
"action_names": ["切换为红色", "设置为红色"],
"risk": "low",
"slot": 23,
},
{
"id": "ir-slot:ambient-light:31",
"status": "learned",
"enabled": True,
"device_id": "ambient-light",
"device_name": "外设氛围灯",
"object_names": ["外设氛围灯", "氛围彩灯玩具"],
"card_uid": "B1C2D3E4",
"action": "打开",
"action_names": ["打开", "开启"],
"risk": "low",
"slot": 31,
},
]
)
handler = object.__new__(SeatDemoHandler)
@@ -235,5 +292,29 @@ def test_mid_autumn_cloud_scene_requires_authorized_flashing_light(
assert response["success"] is True
assert result["status"] == "succeeded"
assert result["execution_mode"] == "dry_run"
assert [item["slot"] for item in result["infrared_actions"]] == [2, 3]
assert [item["slot"] for item in result["infrared_actions"]] == [
2,
31,
21,
]
capabilities = {
action["capability"]
for action in result["actions"]
}
assert "climate.set_temperature" in capabilities
assert "ambient_light.set_power" in capabilities
assert "roof.set_shade_position" in capabilities
assert {
"climate.set_power",
"climate.set_temperature",
"climate.set_fan_level",
"climate.set_cycle_mode",
"window.set_position",
"seat.set_ventilation",
"seat.set_backrest_recline",
"seat.set_legrest_height",
"ambient_light.set_power",
"infrared.emit",
} <= capabilities
assert result["mode_profile_draft"]["operation_count"] == 16
assert result["mode_profile_draft"]["domains"][-1] == "self_extension"
@@ -15,7 +15,13 @@ from cognitive_agent.application.codex_assistant import (
_catalog_exact_scene_template_plan,
)
from cognitive_agent.application.mode_profile import ModeProfileRegistry
from cognitive_agent.application.scene_composer import normalize_scene_output
from cognitive_agent.application.scene_composer import (
normalize_scene_output,
scene_context_for_mode_creation,
scene_required_native_fallbacks,
scene_required_peripheral_refs,
scene_semantic_guidance,
)
from cognitive_agent.application.seat_service import REPOSITORY_ROOT, SeatDemoService
from cognitive_agent.application.self_extension import (
ExtensionIntegrationError,
@@ -212,6 +218,33 @@ def test_scene_composer_mixed_can_ir_preview_save_and_revalidate(
*template_external["requests"],
],
}
template_context = scene_context_for_mode_creation(
context,
"创建一个浪漫模式",
)
template_semantics = scene_semantic_guidance("浪漫模式")
template_native_fallbacks = scene_required_native_fallbacks(
"浪漫模式",
template_context,
include_default_bundle=True,
)
template_semantics["required_native_domains"] = [
str(item["domain"])
for item in template_native_fallbacks
]
template_semantics["native_fallback_requests"] = template_native_fallbacks
template_semantics["peripheral_policy"] = {
"required_refs": scene_required_peripheral_refs(
"浪漫模式",
template_context,
include_default_bundle=True,
),
}
template_context["scene_semantics"] = template_semantics
augmented_template = CodexAssistantService._with_required_scene_peripherals(
augmented_template,
template_context,
)
template_preview = handler._preview_mode_with_scene_peripherals(
"/api/requests/preview",
{
@@ -224,11 +257,30 @@ def test_scene_composer_mixed_can_ir_preview_save_and_revalidate(
on_commands_validated=None,
)
assert template_preview["status"] == "succeeded"
assert template_preview["mode_profile_draft"]["source_template_id"] == "romantic"
assert template_preview["mode_profile_draft"]["operation_count"] == (
len(template_preview["intent_ir"]["spec"]["actions"])
assert template_preview["mode_profile_draft"]["generation_source"] == (
"codex_scene_composition"
)
assert template_preview["mode_profile_draft"]["domains"][-1] == "self_extension"
template_actions = template_preview["intent_ir"]["spec"]["actions"]
assert template_preview["mode_profile_draft"]["operation_count"] == (
len(template_actions)
)
assert {
"climate",
"ambient_light",
"roof",
"self_extension",
}.issubset(template_preview["mode_profile_draft"]["domains"])
template_capabilities = {
action["capability"]
for action in template_actions
}
assert any(
capability.startswith("climate.")
for capability in template_capabilities
)
assert "ambient_light.set_power" in template_capabilities
assert "roof.set_shade_position" in template_capabilities
assert "infrared.emit" in template_capabilities
explicit = handler._execute_request_with_peripherals(
"/api/requests/preview",
+50 -20
View File
@@ -60,6 +60,27 @@ def _action_map(
}
def _assert_default_control_bundle(result: Mapping[str, object]) -> None:
actions = _action_map(result)
assert actions["climate.set_power", "vehicle"] == {"state": "on"}
assert ("climate.set_temperature", "vehicle") in actions
assert ("climate.set_fan_level", "vehicle") in actions
assert ("climate.set_cycle_mode", "vehicle") in actions
for target in ("front_left", "front_right", "rear_left", "rear_right"):
assert actions["window.set_position", target] == {"position_pct": 0}
assert ("roof.set_shade_position", "vehicle") in actions
assert actions["seat.set_ventilation", "passenger"] == {
"state": "on",
"level": 1,
}
assert ("seat.set_backrest_recline", "passenger") in actions
assert ("seat.set_legrest_height", "passenger") in actions
assert any(
capability.startswith("ambient_light.")
for capability, _target in actions
)
def _draft_reference(result: Mapping[str, object]) -> dict[str, str]:
draft = result.get("mode_profile_draft")
assert isinstance(draft, Mapping)
@@ -225,7 +246,7 @@ def test_all_production_scene_templates_complete_the_full_local_dry_run(
assert not list(user_root.rglob("*.yaml"))
def test_viewing_template_creates_an_unbound_audited_copy_without_codex(
def test_viewing_template_seeds_a_complete_unbound_preview_without_codex(
hybrid_service: tuple[SeatDemoService, AssistantCapabilityCatalog, Path],
) -> None:
service, catalog, user_root = hybrid_service
@@ -249,15 +270,16 @@ def test_viewing_template_creates_an_unbound_audited_copy_without_codex(
assert raw_result["execution_mode"] == "dry_run"
assert raw_result["hardware_execution_allowed"] is False
assert draft["name"] == "观影模式"
assert draft["generation_source"] == "scene_template_copy"
assert draft["source_template_id"] == "cinema"
assert draft["source_template_revision"]
assert draft["generation_source"] == "codex_scene_composition"
assert "source_template_id" not in draft
assert "source_template_revision" not in draft
assert "existing_mode_id" not in draft
assert "existing_mode_revision" not in draft
assert raw_result["mode_creation"]["source_template_id"] == "cinema"
assert passed_plans[0]["metadata"]["source"] == "scene_template"
assert "source_template_id" not in raw_result["mode_creation"]
assert "metadata" not in passed_plans[0]
_assert_default_control_bundle(raw_result)
assert events[0]["engine"] == "n7web-scene-template"
assert "独立草稿" in response["answer"]
assert "创建阶段不会下发" in response["answer"]
assert not list(user_root.rglob("*.yaml"))
client.start_structured_turn.assert_not_called()
client.start_turn.assert_not_called()
@@ -319,7 +341,6 @@ def test_purpose_descriptions_use_a_concise_name_and_zero_model_template_route(
conversation_id=f"conv-template-{case_id}",
)
template = service.scene_templates.get_template(template_id)
draft = raw_result["mode_profile_draft"]
assert response["route"] == "scene_template"
assert response["success"] is True
@@ -328,13 +349,11 @@ def test_purpose_descriptions_use_a_concise_name_and_zero_model_template_route(
assert raw_result["execution_mode"] == "dry_run"
assert raw_result["hardware_execution_allowed"] is False
assert draft["name"] == expected_name
assert draft["generation_source"] == "scene_template_copy"
assert draft["source_template_id"] == template_id
assert draft["operation_count"] == sum(
operation.domain != "seat" or operation.target_scope == "passenger"
for operation in template.operations
)
assert passed_plans[0]["metadata"]["template_id"] == template_id
assert draft["generation_source"] == "codex_scene_composition"
assert "source_template_id" not in draft
assert draft["operation_count"] == len(_actions(raw_result))
assert "metadata" not in passed_plans[0]
_assert_default_control_bundle(raw_result)
assert events[0]["engine"] == "n7web-scene-template"
assert not list(user_root.rglob("*.yaml"))
client.start_structured_turn.assert_not_called()
@@ -396,7 +415,14 @@ def test_seating_comfort_requests_preview_only_passenger_with_no_model_or_hardwa
assert result["execution_mode"] == "dry_run"
assert result["hardware_execution_allowed"] is False
assert result["mode_profile_draft"]["name"] == expected_name
if "座椅" in message:
assert result["mode_profile_draft"]["source_template_id"] == template_id
else:
assert result["mode_profile_draft"]["generation_source"] == (
"codex_scene_composition"
)
assert "source_template_id" not in result["mode_profile_draft"]
_assert_default_control_bundle(result)
seat_actions = {
(capability, target): parameters
for (capability, target), parameters in _action_map(result).items()
@@ -524,8 +550,10 @@ def test_template_alias_keeps_the_user_requested_draft_name(
draft = raw_result["mode_profile_draft"]
assert response["route"] == "scene_template"
assert draft["name"] == "影院模式"
assert draft["source_template_id"] == "cinema"
assert passed_plans[0]["metadata"]["template_id"] == "cinema"
assert draft["generation_source"] == "codex_scene_composition"
assert "source_template_id" not in draft
assert "metadata" not in passed_plans[0]
_assert_default_control_bundle(raw_result)
client.start_structured_turn.assert_not_called()
client.start_turn.assert_not_called()
@@ -574,7 +602,7 @@ def test_template_copy_can_be_revised_twice_without_losing_sibling_actions(
assert shade_revised["execution_mode"] == final["execution_mode"] == "dry_run"
assert shade_revised["hardware_execution_allowed"] is False
assert final["hardware_execution_allowed"] is False
assert len(built_actions) == len(shade_actions) == len(final_actions) == 10
assert len(built_actions) == len(shade_actions) == len(final_actions) == 16
assert built_actions[shade_key] == {"position_pct": 0}
assert shade_actions[shade_key] == final_actions[shade_key] == {"position_pct": 100}
assert built_actions[temperature_key] == shade_actions[temperature_key] == {"temperature_c": 24}
@@ -592,7 +620,8 @@ def test_template_copy_can_be_revised_twice_without_losing_sibling_actions(
assert final_draft["draft_revision"] == 3
assert second_draft["generation_source"] == "draft_revision"
assert final_draft["generation_source"] == "draft_revision"
assert second_draft["source_template_id"] == final_draft["source_template_id"] == ("cinema")
assert "source_template_id" not in second_draft
assert "source_template_id" not in final_draft
assert "existing_mode_id" not in final_draft
assert not list(user_root.rglob("*.yaml"))
@@ -626,7 +655,8 @@ def test_saving_a_template_copy_never_mutates_template_and_registered_mode_wins_
assert list(user_root.rglob(f"{saved_profile['id']}.yaml"))
assert _template_hashes() == hashes_before
assert service.scene_templates.get_template("cinema").revision == template_revision
assert first_plans[0]["metadata"]["template_id"] == "cinema"
assert "metadata" not in first_plans[0]
_assert_default_control_bundle(built)
first_client.start_structured_turn.assert_not_called()
response, registered, events, second_client, passed_plans = _assistant_preview(
+18 -2
View File
@@ -138,7 +138,7 @@ function ndjsonResponse(events) {
};
}
function createHarness(events) {
function createHarness(events, executionMode = "dry_run") {
const document = new MockEventTarget();
const windowEvents = new MockEventTarget();
const body = new MockElement("body");
@@ -209,7 +209,7 @@ function createHarness(events) {
sandbox.removeEventListener = windowEvents.removeEventListener.bind(windowEvents);
sandbox.dispatchEvent = windowEvents.dispatchEvent.bind(windowEvents);
sandbox.CognitiveAgentConsole = {
getExecutionMode: () => "dry_run",
getExecutionMode: () => executionMode,
async refreshModeProfiles(modeProfileId) {
refreshedModeProfiles.push(modeProfileId);
},
@@ -367,6 +367,10 @@ test("assistant bridges request start and validated local controls to the consol
const cards = assistantArticle.querySelectorAll(".assistant-dispatched-controls");
assert.equal(cards.length, 1);
assert.equal(cards[0].open, true);
assert.equal(
cards[0].children[0].children[1].textContent,
"控制指令预览(未下发)",
);
assert.equal(
cards[0].querySelector(".assistant-dispatched-controls-count").textContent,
"3",
@@ -381,6 +385,7 @@ test("assistant bridges request start and validated local controls to the consol
["23°C", "关闭", "关闭"],
);
assert.match(rows[0].title, /实际控制指令:ac temp driver 23/);
assert.match(rows[0].title, /下发状态:仅预览 · 未下发/);
});
test("unsupported-control suggestions render once and click through as a new command", async () => {
@@ -544,6 +549,17 @@ function modeDraftEvents(sourceRequestId, temperatureC) {
];
}
test("mode creation always requests dry-run even while the console is in hardware mode", async () => {
const harness = createHarness(modeDraftEvents("req-draft-hardware-safe", 24), "hardware");
harness.input.value = "创建一个午休模式";
await harness.sandbox.CognitiveAgentAssistant.submit();
const request = JSON.parse(harness.requestCalls[0].options.body);
assert.equal(request.execution_mode, "dry_run");
assert.equal(Object.hasOwn(request, "hardware_client_id"), false);
});
test("a follow-up turn carries only the active authoritative draft reference", async () => {
const events = modeDraftEvents("req-draft-one", 24);
const harness = createHarness(events);
@@ -21,17 +21,17 @@ def test_console_contains_ai_assistant_surface():
assert 'placeholder="向 AI助手提问或交代工作"' in template
assert 'aria-label="发送消息"' in template
assert 'import "../assistant-chat.css";' in vite_entry
assert 'src="/assistant-chat.js?v=codex-assistant-34"' in html
assert 'src="/assistant-chat.js?v=codex-assistant-35"' in html
def test_codex_assistant_assets_are_versioned_together_in_the_service_worker():
service_worker = (ROOT / "apps/web-console/service-worker.js").read_text(encoding="utf-8")
assert (
'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"'
'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"'
in service_worker
)
assert '"/assistant-chat.css?v=codex-assistant-29"' in service_worker
assert '"/assistant-chat.js?v=codex-assistant-34"' in service_worker
assert '"/assistant-chat.css?v=codex-assistant-30"' in service_worker
assert '"/assistant-chat.js?v=codex-assistant-35"' in service_worker
def test_codex_frontend_uses_one_streaming_endpoint_and_agent_work_events():
@@ -91,7 +91,7 @@ def test_validated_controls_are_mirrored_into_a_compact_assistant_card():
assert "window.CognitiveAgentConsoleV2?.getParsedCanCommands?.(result)" in script
assert "fallbackAssistantControlCommands(result)" in script
assert 'card.className = "assistant-dispatched-controls"' in script
assert 'heading.textContent = "下发控制指令"' in script
assert 'const headingText = previewOnly ? "控制指令预览(未下发)" : "下发控制指令"' in script
assert 'count.className = "assistant-dispatched-controls-count"' in script
assert 'list.className = "assistant-dispatched-controls-list"' in script
assert "value.textContent = assistantControlValue(item)" in script
@@ -186,6 +186,7 @@ def test_codex_request_keeps_trusted_vehicle_context_without_legacy_model_routin
assert "history" not in request_body
assert "provider" not in request_body
assert "reasoning_effort" not in request_body
assert 'const executionMode = modeConfigurationPreview ? "dry_run"' in request_body
def test_starting_a_new_codex_conversation_rotates_the_conversation_id():
@@ -239,7 +240,9 @@ def test_assistant_mode_creation_previews_then_saves_to_the_compact_mode_strip()
assert 'event.payload.orchestration_route === "mode_profile_create_preview"' in assistant
assert "appendModeDraftState(" in assistant
assert "保存为功能模式" in assistant
assert "先试运行" in assistant
assert "重新校验(不下发)" in assistant
assert "创建阶段仅生成并校验控制方案,不会向 CAN 或红外外设发送指令" in assistant
assert 'label = String(action?.target?.name || action?.target?.device || "红外外设")' in assistant
save_flow = assistant.split(
"async function saveModeProfileDraft", 1
@@ -175,3 +175,35 @@ def test_climate_control_circulation_and_outlet_are_not_conflated(render_rows):
("空调出风模式", "吹面 / 吹脚"),
("空调风量", "2档"),
]
def test_mode_preview_localizes_external_device_name_and_action_parameters(render_rows):
rows = render_rows([
{
"domain": "self_extension",
"capability": "infrared.emit",
"target": {"device": "flashing-light", "name": "闪烁灯"},
"parameters": {"action": "打开", "slot": 7},
},
])
assert [(row["label"], row["value"]) for row in rows] == [
("闪烁灯", "动作:打开"),
]
assert "infrared.emit" not in json.dumps(rows, ensure_ascii=False)
def test_default_bundle_rows_show_power_window_and_grouped_passenger_comfort(render_rows):
rows = render_rows([
action("climate.set_power", {"state": "on"}, "vehicle"),
action("window.set_position", {"position_pct": 0}, "front_left"),
action("seat.set_ventilation", {"state": "on", "level": 1}, "passenger"),
action("seat.set_backrest_recline", {"position_pct": 45}, "passenger"),
action("seat.set_legrest_height", {"position_pct": 35}, "passenger"),
])
assert [(row["label"], row["value"]) for row in rows] == [
("空调电源", "开启"),
("主驾车窗", "完全关闭(目标开度 0%)"),
("副驾座椅舒适模式", "通风开启 1档 · 靠背 45% · 腿托 35%"),
]
@@ -37,11 +37,11 @@ def test_energy_monitor_assets_are_versioned_and_cached() -> None:
assert f'"{asset}"' in service_worker
assert 'import "../energy-monitor.css";' in vite_entry
assert "/energy-monitor.js?v=energy-monitor-5-performance" in html
assert html.index("/assistant-chat.js?v=codex-assistant-34") < html.index(
assert html.index("/assistant-chat.js?v=codex-assistant-35") < html.index(
"/energy-monitor.js?v=energy-monitor-5-performance"
)
assert (
'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"'
'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"'
in service_worker
)
@@ -99,5 +99,5 @@ def test_performance_assets_have_a_new_cache_generation() -> None:
):
assert asset in html
assert f'"{asset}"' in worker
assert 'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"' in worker
assert 'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"' in worker
assert '"/codepen-glass-theme.css?v=creative-cloud-6-performance"' in worker
+1 -1
View File
@@ -54,7 +54,7 @@ def test_theme_asset_versions_are_consistent_with_the_pwa_shell() -> None:
assert source_asset in main
assert f'"{cached_asset}"' in service_worker
assert 'import "./styles/design-system.css"' in main
assert 'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"' in service_worker
assert 'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"' in service_worker
def test_reference_glass_theme_covers_the_complete_workspace() -> None:
+71 -7
View File
@@ -15,7 +15,10 @@ from cognitive_agent.application.codex_assistant import (
CodexAssistantService,
_mode_profile_create_parts,
)
from cognitive_agent.application.scene_composer import scene_semantic_guidance
from cognitive_agent.application.scene_composer import (
scene_required_native_fallbacks,
scene_semantic_guidance,
)
CONTROL_TERMS = ("空调", "座椅", "车窗", "遮阳帘", "氛围灯", "阅读灯", "出风口")
@@ -563,6 +566,21 @@ def test_exact_scene_template_keeps_native_base_and_adds_only_selected_ir_ref()
def _scene_context_with_flashing_light() -> dict[str, object]:
context = _scene_context()
context["capabilities"].append(
{
"id": "ambient_light.set_power",
"domain": "ambient_light",
"target_scopes": ["vehicle"],
"parameters": [
{
"name": "state",
"type": "string",
"allowed_values": ["on", "off"],
"required": True,
}
],
}
)
context["peripheral_actions"] = [
{
"ref": "ir:flash-open-000000000001",
@@ -588,6 +606,30 @@ def _scene_context_with_flashing_light() -> dict[str, object]:
"action_names": ["关闭", "熄灭"],
"semantic_tags": ["闪烁灯", "灯光"],
},
{
"ref": "ir:strip-open-000000000001",
"device_label": "灯带",
"object_names": ["灯带", "LED 灯条"],
"action_label": "打开",
"action_names": ["打开", "开启"],
"semantic_tags": ["灯带", "灯光"],
},
{
"ref": "ir:strip-red-0000000000002",
"device_label": "灯带",
"object_names": ["灯带", "LED 灯条"],
"action_label": "切换为红色",
"action_names": ["切换为红色", "设置为红色"],
"semantic_tags": ["灯带", "红色", "灯光"],
},
{
"ref": "ir:ambient-open-0000000001",
"device_label": "外设氛围灯",
"object_names": ["外设氛围灯", "氛围灯"],
"action_label": "打开",
"action_names": ["打开", "开启"],
"semantic_tags": ["氛围灯", "灯光"],
},
]
context["device_inventory"] = [
{
@@ -612,7 +654,7 @@ def _peripheral_refs(plan: Mapping[str, object]) -> list[str]:
]
def test_romantic_template_always_adds_authorized_flashing_light() -> None:
def test_romantic_template_always_adds_authorized_decorative_lights() -> None:
client = StructuredSceneClient()
client.structured_answers.put(
json.dumps(
@@ -647,14 +689,19 @@ def test_romantic_template_always_adds_authorized_flashing_light() -> None:
assert result["success"] is True
assert observed[0]["requests"][:2] == template["plan"]["requests"]
assert any(
request.get("domain") == "ambient_light"
for request in observed[0]["requests"]
)
assert _peripheral_refs(observed[0]) == [
"ir:flash-open-000000000001",
"ir:flash-red-0000000000002",
"ir:ambient-open-0000000001",
"ir:strip-open-000000000001",
]
assert "ir:flash-close-00000000003" not in _peripheral_refs(observed[0])
def test_mid_autumn_cloud_composition_receives_all_abilities_and_adds_flashing_light() -> None:
def test_mid_autumn_cloud_composition_adds_required_native_and_external_controls() -> None:
client = StructuredSceneClient()
assistant = CodexAssistantService(client)
observed: list[dict[str, object]] = []
@@ -681,18 +728,20 @@ def test_mid_autumn_cloud_composition_receives_all_abilities_and_adds_flashing_l
assert result["route"] == "scene_composer"
call_context = client.structured_calls[0]["application_context"]
assert len(call_context["capabilities"]) == len(scene_context["capabilities"])
assert len(call_context["peripheral_actions"]) == 3
assert len(call_context["peripheral_actions"]) == 6
assert call_context["scene_semantics"]["family"] == "festival_celebration"
assert call_context["scene_semantics"]["peripheral_policy"][
"required_refs"
] == [
"ir:flash-open-000000000001",
"ir:flash-red-0000000000002",
"ir:ambient-open-0000000001",
"ir:strip-open-000000000001",
]
assert "as many relevant" in client.thread_instructions[0]
assert _peripheral_refs(observed[0]) == [
"ir:flash-open-000000000001",
"ir:flash-red-0000000000002",
"ir:ambient-open-0000000001",
"ir:strip-open-000000000001",
]
@@ -1103,6 +1152,21 @@ def test_unknown_mode_uses_one_structured_turn_then_local_dry_run() -> None:
"cloud_input_utterance": "构建一个星夜模式",
}
expected_semantics = scene_semantic_guidance("星夜模式")
required_native = scene_required_native_fallbacks(
"星夜模式",
expected_context,
include_default_bundle=True,
)
expected_semantics["required_native_domains"] = ["climate", "roof"]
expected_semantics["required_native_controls"] = [
{
"capability": str(item["capability"]),
"target_scope": str(item["target_scope"]),
}
for item in required_native
]
expected_semantics["native_fallback_requests"] = required_native
expected_semantics["default_control_bundle_applied"] = True
expected_semantics["peripheral_policy"] = {
"available_action_count": 0,
"required_refs": [],
+128 -3
View File
@@ -11,6 +11,8 @@ from cognitive_agent.application.assistant_capabilities import AssistantCapabili
from cognitive_agent.application.scene_composer import (
build_scene_output_schema,
normalize_scene_output,
scene_has_explicit_control_object,
scene_required_native_fallbacks,
scene_required_peripheral_refs,
scene_semantic_guidance,
)
@@ -464,7 +466,7 @@ def test_learning_mode_is_grounded_as_free_semantic_composition() -> None:
@pytest.mark.parametrize("mode_name", ("浪漫模式", "中秋欢快模式"))
def test_romantic_and_mid_autumn_require_flashing_light_actions(
def test_romantic_and_mid_autumn_require_decorative_light_actions(
mode_name: str,
) -> None:
context = _context()
@@ -490,6 +492,27 @@ def test_romantic_and_mid_autumn_require_flashing_light_actions(
"action_label": "关闭",
"action_names": ["关闭", "熄灭"],
},
{
"ref": "ir:strip-open",
"device_label": "灯带",
"object_names": ["灯带", "LED 灯条"],
"action_label": "打开",
"action_names": ["打开", "开启"],
},
{
"ref": "ir:strip-red",
"device_label": "灯带",
"object_names": ["灯带", "LED 灯条"],
"action_label": "切换为红色",
"action_names": ["切换为红色", "设置为红色"],
},
{
"ref": "ir:ambient-open",
"device_label": "外设氛围灯",
"object_names": ["外设氛围灯", "氛围灯"],
"action_label": "打开",
"action_names": ["打开", "开启"],
},
]
required = scene_required_peripheral_refs(mode_name, context)
semantics = scene_semantic_guidance(mode_name)
@@ -497,7 +520,109 @@ def test_romantic_and_mid_autumn_require_flashing_light_actions(
context["scene_semantics"] = semantics
schema = build_scene_output_schema(context)
assert required == ["ir:flash-open", "ir:flash-red"]
assert schema["properties"]["requests"]["maxItems"] == 10
assert required == [
"ir:flash-open",
"ir:strip-open",
]
assert scene_required_peripheral_refs(
mode_name,
context,
include_default_bundle=True,
) == [
"ir:flash-open",
"ir:ambient-open",
"ir:strip-open",
]
assert schema["properties"]["requests"]["maxItems"] == 18
if "中秋" in mode_name:
assert semantics["family"] == "festival_celebration"
@pytest.mark.parametrize(
("mode_name", "expected_shade"),
(("浪漫模式", 0), ("中秋欢快模式", 100)),
)
def test_required_native_fallbacks_cover_climate_ambient_and_shade(
mode_name: str,
expected_shade: int,
) -> None:
context = _context()
context["capabilities"].append(
{
"id": "ambient_light.set_power",
"domain": "ambient_light",
"target_scopes": ["vehicle"],
"parameters": [
{
"name": "state",
"type": "string",
"allowed_values": ["on", "off"],
"required": True,
}
],
}
)
fallbacks = scene_required_native_fallbacks(mode_name, context)
assert [item["domain"] for item in fallbacks] == [
"climate",
"ambient_light",
"roof",
]
assert fallbacks[0]["capability"] == "climate.set_temperature"
assert fallbacks[1]["capability"] == "ambient_light.set_power"
assert fallbacks[2]["capability"] == "roof.set_shade_position"
assert fallbacks[2]["parameters"] == [
{"name": "position_pct", "value": expected_shade}
]
def test_name_only_scene_default_bundle_covers_all_required_native_controls() -> None:
service = SeatDemoService()
catalog = AssistantCapabilityCatalog(service)
context = catalog.scene_composition_context(catalog.snapshot())
fallbacks = scene_required_native_fallbacks(
"星夜模式",
context,
include_default_bundle=True,
)
semantics = scene_semantic_guidance("星夜模式")
semantics["native_fallback_requests"] = fallbacks
semantics["peripheral_policy"] = {"required_refs": []}
context["scene_semantics"] = semantics
assert [(item["capability"], item["target_scope"]) for item in fallbacks] == [
("climate.set_power", "vehicle"),
("climate.set_temperature", "vehicle"),
("climate.set_fan_level", "vehicle"),
("climate.set_cycle_mode", "vehicle"),
("window.set_position", "front_left"),
("window.set_position", "front_right"),
("window.set_position", "rear_left"),
("window.set_position", "rear_right"),
("roof.set_shade_position", "vehicle"),
("seat.set_ventilation", "passenger"),
("seat.set_backrest_recline", "passenger"),
("seat.set_legrest_height", "passenger"),
("ambient_light.set_power", "vehicle"),
]
assert build_scene_output_schema(context)["properties"]["requests"]["maxItems"] == 7
@pytest.mark.parametrize(
("message", "expected"),
(
("创建浪漫中秋模式", False),
("创建浪漫中秋模式,空调温度设置为25度", True),
("创建灯光主题模式", True),
("创建主驾浪漫模式", True),
("创建节日模式", False),
),
)
def test_scene_detects_whether_user_named_a_concrete_control_object(
message: str,
expected: bool,
) -> None:
assert scene_has_explicit_control_object(message, _context()) is expected
+2 -2
View File
@@ -111,7 +111,7 @@ def test_initialize_and_tool_catalog_expose_only_safe_arguments() -> None:
properties = tools[1]["inputSchema"]["properties"]
assert set(properties) == {"request_token", "plan"}
assert properties["plan"]["additionalProperties"] is False
assert properties["plan"]["properties"]["requests"]["maxItems"] == 12
assert properties["plan"]["properties"]["requests"]["maxItems"] == 20
assert not {
"approval_token",
"hardware_access_bypass",
@@ -149,7 +149,7 @@ def test_structured_plan_is_validated_and_forwarded_without_freeform_instruction
"plan",
[
{"requests": []},
{**_sample_plan(), "requests": _sample_plan()["requests"] * 13},
{**_sample_plan(), "requests": _sample_plan()["requests"] * 21},
{
**_sample_plan(),
"requests": [{**_sample_plan()["requests"][0], "raw_can": "01 02 03"}],
+4 -1
View File
@@ -31,7 +31,10 @@ def test_vue_vite_application_is_the_primary_frontend_entry() -> None:
assert package["scripts"]["build"] == "vite build"
assert 'import vue from "@vitejs/plugin-vue"' in vite
assert 'outDir: outputRoot' in vite
assert 'const CACHE_VERSION = "cognitive-agent-pad-v328-nfc-mqtt"' in service_worker
assert 'const CACHE_VERSION = "cognitive-agent-pad-v329-mode-preview"' in service_worker
assert 'createHash("sha256")' in vite
assert 'runtime-assets.json' in vite
assert 'rewriteRuntimeReferences' in vite
assert "unpkg.com" not in html
assert "cdn.jsdelivr.net" not in html