【capacity 唤醒式核心(77)】internal/runtime/capacity:(Decider L1启发式+L2上下文+人格覆盖→三态静息/唤醒/全能→允许工具集+capacity_awaken事件)。loop 外围允许集过滤(不硬阻断=能力≠边界红线)。config 加 Capacity(Enabled/MinConfidence/ToolSets)。wire buildCapacity 接入 loop。 【L4 记忆驱动意图(80)】recall.go:从记忆召回用户习惯(搞一下xx=办事)→唤醒L1/L2漏掉/升置信;群聊陌生人不读owner习惯(隔离红线);RecordIntent闭环(判定写回记忆越用越准);默认保守静息。 【会话消息编排(78)】gateway/session_orchestrator.go:每会话(模式/pending有界5/tail上下文尾巴20);Submit非阻塞(idle立即/busy入队ack/满拒绝);完成回调按序续接(tail+新消息合并增量注入非全量重放);Interrupt中断;REST 202/409+WS session_busy/pending_ack事件;不重造任务引擎复用loop。 【前端集成(79)】ws.ts 加 capacity_awaken/session_busy/pending_ack 解析;store 加 awakenState/sessionBusy;TopBar/ChatShell 加唤醒态徽标+忙态提示(不阻塞输入框);状态组件 status.tsx;Miuix风格。 【管理面板写CRUD(81)】20个写端点(POST/PUT/DELETE /admin/*,owner+审计落库):personas(真持久化YAML)/users apikey(签发掩码+撤销)/iot控制(真下发)/automations(建/触发/删)/llm(test真调用+临时)/channels(connect/close/身份测试actor白名单)/plugins(卸载)/memories(search真检索)/sessions(export)。无存储如实501不伪造。拆4文件满足≤500红线。前端9模块接真写。 【wire收口】buildCapacity接入loop+OnCapacity事件;server.go newOrchestrator接hub。 实机验证:iot ac.living control 200 真实改变 on:true;observability/metrics 200、health ok;全仓 build/vet/test 47包绿+前端tsc/vite。
162 lines
5.4 KiB
TypeScript
162 lines
5.4 KiB
TypeScript
import { useState } from "react";
|
||
import { useAsyncData } from "../../lib/useAsyncData";
|
||
import { adminListIoT, adminIoTControl, adminIoTScenario, adminIoTRule } from "../../lib/api";
|
||
import type { AdminIoTDevice } from "../../lib/types";
|
||
import { useAutoReload } from "../hooks";
|
||
import { useAuditStore } from "../audit";
|
||
import { AdminTable, Notice, PageHeader, type AdminColumn } from "../components";
|
||
import { Button, Surface, Tag } from "../../components/primitives";
|
||
|
||
const columns: AdminColumn<AdminIoTDevice>[] = [
|
||
{ key: "name", title: "设备", render: (d) => <span className="font-medium">{d.name}</span> },
|
||
{ key: "type", title: "类型", render: (d) => <Tag tone="accent">{d.type}</Tag> },
|
||
{ key: "id", title: "ID", render: (d) => <span className="font-mono text-xs text-[var(--mu-text-lo)]">{d.id}</span> },
|
||
{
|
||
key: "online",
|
||
title: "状态",
|
||
render: (d) => (
|
||
<Tag tone={d.online ? "ok" : "err"}>
|
||
<span className={`mr-1 inline-block h-1.5 w-1.5 rounded-full ${d.online ? "bg-[var(--mu-ok)]" : "bg-[var(--mu-err)]"}`} />
|
||
{d.online ? "在线" : "离线"}
|
||
</Tag>
|
||
),
|
||
},
|
||
{
|
||
key: "state",
|
||
title: "实时状态",
|
||
render: (d) => (
|
||
<span className="block max-w-md font-mono text-xs text-[var(--mu-text-mid)]">
|
||
{Object.entries(d.state)
|
||
.map(([k, v]) => `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`)
|
||
.join(" · ") || "—"}
|
||
</span>
|
||
),
|
||
},
|
||
{
|
||
key: "control",
|
||
title: "控制",
|
||
render: (d) => (
|
||
<DeviceControl device={d} disabled={!d.online} />
|
||
),
|
||
},
|
||
];
|
||
|
||
function DeviceControl({ device, disabled }: { device: AdminIoTDevice; disabled: boolean }): React.ReactNode {
|
||
const [action, setAction] = useState("on");
|
||
const [params, setParams] = useState("");
|
||
const [res, setRes] = useState<string | null>(null);
|
||
const [err, setErr] = useState<string | null>(null);
|
||
const [busy, setBusy] = useState(false);
|
||
const log = useAuditStore((s) => s.log);
|
||
|
||
const send = () => {
|
||
setBusy(true);
|
||
setErr(null);
|
||
let parsed: Record<string, unknown> | undefined;
|
||
if (params.trim()) {
|
||
try {
|
||
parsed = JSON.parse(params) as Record<string, unknown>;
|
||
} catch {
|
||
setErr("参数需为合法 JSON");
|
||
setBusy(false);
|
||
return;
|
||
}
|
||
}
|
||
adminIoTControl(device.id, action.trim() || "on", parsed)
|
||
.then((d) => {
|
||
log("iot.control", "ok");
|
||
setRes(`${d.name} → ${action}:${JSON.stringify(d.state)}`);
|
||
})
|
||
.catch((e: unknown) => {
|
||
log("iot.control", "error");
|
||
setErr(e instanceof Error ? e.message : "控制失败");
|
||
})
|
||
.finally(() => setBusy(false));
|
||
};
|
||
|
||
return (
|
||
<div className="flex flex-wrap items-center gap-1.5">
|
||
<input
|
||
value={action}
|
||
onChange={(e) => setAction(e.target.value)}
|
||
placeholder="on / off / set_temperature"
|
||
className="w-28 rounded-[var(--mu-radius-sm)] bg-[var(--mu-surface-2)] px-2 py-1 text-xs outline-none focus:ring-1 focus:ring-[var(--mu-accent)]"
|
||
/>
|
||
<input
|
||
value={params}
|
||
onChange={(e) => setParams(e.target.value)}
|
||
placeholder='{"temp":24}'
|
||
className="w-28 rounded-[var(--mu-radius-sm)] bg-[var(--mu-surface-2)] px-2 py-1 text-xs outline-none focus:ring-1 focus:ring-[var(--mu-accent)]"
|
||
/>
|
||
<Button size="sm" variant="ghost" disabled={disabled || busy} onClick={send}>
|
||
控制
|
||
</Button>
|
||
{res ? <span className="text-[11px] text-[var(--mu-ok)]">{res}</span> : null}
|
||
{err ? <span className="text-[11px] text-[var(--mu-err)]">{err}</span> : null}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function IoTModule(): React.ReactNode {
|
||
const { data, loading, error, reload } = useAsyncData(adminListIoT, []);
|
||
useAutoReload(reload, 8000);
|
||
const log = useAuditStore((s) => s.log);
|
||
const [msg, setMsg] = useState<string | null>(null);
|
||
|
||
const scenario = () =>
|
||
adminIoTScenario({ name: "场景-夜灯" })
|
||
.then(() => {
|
||
log("iot.scene", "ok");
|
||
setMsg("场景保存成功");
|
||
})
|
||
.catch((e: unknown) => {
|
||
log("iot.scene", "error");
|
||
setMsg(e instanceof Error ? e.message : "场景保存失败");
|
||
});
|
||
|
||
const rule = () =>
|
||
adminIoTRule({ name: "规则-定时", expression: "0 22 * * *" })
|
||
.then(() => {
|
||
log("iot.rule", "ok");
|
||
setMsg("规则保存成功");
|
||
})
|
||
.catch((e: unknown) => {
|
||
log("iot.rule", "error");
|
||
setMsg(e instanceof Error ? e.message : "规则保存失败");
|
||
});
|
||
|
||
return (
|
||
<div className="space-y-5">
|
||
<PageHeader
|
||
title="IoT 设备"
|
||
subtitle="GET /api/v1/admin/iot · 设备列表/实时状态 + 控制(真实下发) + 场景 + 自动化规则"
|
||
actions={
|
||
<>
|
||
<Button variant="soft" onClick={scenario}>
|
||
场景与规则
|
||
</Button>
|
||
<Button variant="primary" onClick={rule}>
|
||
新增规则
|
||
</Button>
|
||
</>
|
||
}
|
||
/>
|
||
|
||
{msg ? <Notice tone="err">{msg}</Notice> : null}
|
||
|
||
<Surface className="space-y-2">
|
||
<AdminTable
|
||
columns={columns}
|
||
rows={loading ? null : data}
|
||
loading={loading}
|
||
error={error}
|
||
emptyTitle="无 IoT 设备"
|
||
rowKey={(d) => d.id}
|
||
/>
|
||
</Surface>
|
||
|
||
<Notice>设备控制真实下发到 IoT provider(在线设备可操作);场景/规则存储由后端 M10 提供,当前返回明确待实现。</Notice>
|
||
</div>
|
||
);
|
||
}
|