feie9456 747dcd359a 家庭守护圈:多人加入、角色权限、24 小时邀请链接、旧设备绑定自动兼容。
家庭动态:老人/家属发言、三种可见范围,家庭内部内容不会播给老人。
共同照护:家庭备忘录、任务创建、认领、完成与交接记录。
用药闭环:计划、未来 7 天提醒实例、确认服用、延后、异常反馈。
风险闭环:红橙黄蓝分级、30 分钟去重、推送、认领、处理与误报记录。
报平安:家属发起、老人语音回应、状态同步。
Web 导航升级为守护、家庭、照护、陪伴、我的。
设备接口已升级为设备令牌认证,UUID 不再单独承担认证。
2026-07-18 19:59:20 +08:00

76 lines
2.0 KiB
TypeScript

import { ToolCallStatus } from "@prisma/client";
import { NextResponse } from "next/server";
import { recordToolCall } from "@/lib/monitor-data";
import { requireDeviceAuth } from "@/lib/device-auth";
function toToolCallStatus(rawStatus: unknown) {
return rawStatus === ToolCallStatus.FAILURE
? ToolCallStatus.FAILURE
: ToolCallStatus.SUCCESS;
}
function toArguments(rawArguments: unknown) {
if (
rawArguments &&
typeof rawArguments === "object" &&
!Array.isArray(rawArguments)
) {
return rawArguments as Record<string, unknown>;
}
return {};
}
export async function POST(request: Request) {
const body = (await request.json().catch(() => null)) as
| {
deviceUuid?: string;
toolName?: string;
arguments?: unknown;
outputText?: string;
status?: string;
callId?: string;
sessionId?: string;
}
| null;
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
return NextResponse.json(
{ error: "deviceUuid 是必填项。" },
{ status: 400 },
);
}
if (!body.toolName || typeof body.toolName !== "string") {
return NextResponse.json(
{ error: "toolName 是必填项。" },
{ status: 400 },
);
}
try {
await requireDeviceAuth(request, body.deviceUuid);
const log = await recordToolCall({
deviceUuid: body.deviceUuid,
toolName: body.toolName,
arguments: toArguments(body.arguments),
outputText: typeof body.outputText === "string" ? body.outputText : undefined,
status: toToolCallStatus(body.status),
callId: typeof body.callId === "string" ? body.callId : undefined,
sessionId:
typeof body.sessionId === "string" ? body.sessionId : undefined,
});
return NextResponse.json({ ok: true, logId: log.id });
} catch (error) {
return NextResponse.json(
{
error:
error instanceof Error ? error.message : "工具调用记录失败,请稍后再试。",
},
{ status: 400 },
);
}
}