feat(assets): add DeepSeek assistant with streaming results and voice input
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { Hono } from "hono";
|
||||
import { assetResult, validatePlan, visibleVehicles } from "./model.js";
|
||||
import router from "./index.js";
|
||||
import { vehicleRepository } from "../vehicles/repository.js";
|
||||
import type { Vehicle } from "../../types.js";
|
||||
import type { AuthUser } from "../../auth/types.js";
|
||||
|
||||
const user: AuthUser = {
|
||||
userId: "u1",
|
||||
userName: "测试",
|
||||
loginName: "test",
|
||||
depName: "甲部门",
|
||||
depCode: "A",
|
||||
permissionLevel: "personal",
|
||||
roles: [],
|
||||
};
|
||||
const plan = {
|
||||
kind: "assets",
|
||||
category: "inventory",
|
||||
groupBy: "region",
|
||||
view: "chart",
|
||||
filters: {},
|
||||
};
|
||||
const vehicles = ["Inventory", "Abnormal", "Pending", "Operating"].map(
|
||||
(status, index) =>
|
||||
({
|
||||
id: String(index),
|
||||
status,
|
||||
operationStatus: index === 3 ? "1" : "3",
|
||||
managerId: index === 1 ? "u2" : "u1",
|
||||
departmentName: "甲部门",
|
||||
subjectOrg: index === 2 ? "乙公司" : "甲公司",
|
||||
province: "浙江省",
|
||||
city: "嘉兴市",
|
||||
plateNumber: `测试${index}`,
|
||||
type: "4.5T",
|
||||
model: "测试型号",
|
||||
}) as Vehicle,
|
||||
);
|
||||
|
||||
test("permissions and company filters apply before aggregation and detail generation", () => {
|
||||
const visible = visibleVehicles(vehicles, user, "甲公司");
|
||||
const result = assetResult(visible, validatePlan(plan));
|
||||
assert.equal(result.metrics[0].value, 1);
|
||||
assert.equal(result.groups[0].count, result.rows.length);
|
||||
assert.deepEqual(
|
||||
result.rows.map((v) => v.id),
|
||||
["0"],
|
||||
);
|
||||
});
|
||||
test("inventory includes abnormal and pending; group totals reconcile with detail", () => {
|
||||
const result = assetResult(vehicles, validatePlan(plan));
|
||||
assert.equal(result.rows.length, 3);
|
||||
assert.equal(
|
||||
result.groups.reduce((n, g) => n + g.count, 0),
|
||||
3,
|
||||
);
|
||||
assert.equal(result.metrics[1].value, 3);
|
||||
});
|
||||
test("unsafe or unsupported query plans, impossible dates and long ranges are rejected", () => {
|
||||
assert.throws(() => validatePlan({ ...plan, filters: { sql: "SELECT *" } }));
|
||||
assert.throws(() => validatePlan({ ...plan, category: "delete" }));
|
||||
assert.throws(() => validatePlan({ ...plan, start: "2026-01-01" }));
|
||||
assert.throws(() =>
|
||||
validatePlan({
|
||||
...plan,
|
||||
kind: "flow",
|
||||
start: "2026-02-30",
|
||||
end: "2026-03-05",
|
||||
}),
|
||||
);
|
||||
assert.throws(() =>
|
||||
validatePlan({
|
||||
...plan,
|
||||
kind: "flow",
|
||||
start: "2026-01-01",
|
||||
end: "2026-09-01",
|
||||
}),
|
||||
);
|
||||
});
|
||||
test("assistant rejects unauthenticated direct calls before contacting a model", async () => {
|
||||
const response = await router.request("/ask", { method: "POST", body: "{}" });
|
||||
assert.equal(response.status, 401);
|
||||
});
|
||||
test("model only receives aggregate data; permission-filtered rows stay in the response", async (t) => {
|
||||
const oldKey = process.env.DEEPSEEK_API_KEY;
|
||||
process.env.DEEPSEEK_API_KEY = "test-only";
|
||||
t.after(() => {
|
||||
if (oldKey === undefined) delete process.env.DEEPSEEK_API_KEY;
|
||||
else process.env.DEEPSEEK_API_KEY = oldKey;
|
||||
});
|
||||
t.mock.method(vehicleRepository, "getVehicles", async () => vehicles);
|
||||
const prompts: string[] = [];
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
"fetch",
|
||||
async (_url: unknown, init: RequestInit) => {
|
||||
prompts.push(String(init.body));
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content:
|
||||
prompts.length === 1
|
||||
? JSON.stringify(plan)
|
||||
: "共有 2 台库存车辆。",
|
||||
},
|
||||
finish_reason: "stop",
|
||||
},
|
||||
],
|
||||
}),
|
||||
{ status: 200 },
|
||||
);
|
||||
},
|
||||
);
|
||||
const app = new Hono<{ Variables: { user: AuthUser } }>();
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("user", user);
|
||||
await next();
|
||||
});
|
||||
app.route("/assistant", router);
|
||||
const response = await app.request("/assistant/ask", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ message: "统计库存" }),
|
||||
});
|
||||
const events = (await response.text())
|
||||
.split("\n")
|
||||
.filter((s) => s.startsWith("data:"))
|
||||
.map((s) => JSON.parse(s.slice(5)));
|
||||
const result = events.find((e) => e.type === "result").result;
|
||||
assert.deepEqual(
|
||||
result.rows.map((r: { id: string }) => r.id),
|
||||
["0", "2"],
|
||||
);
|
||||
assert.equal(prompts.length, 2);
|
||||
assert.ok(
|
||||
prompts.every(
|
||||
(p) =>
|
||||
!p.includes("测试0") &&
|
||||
!p.includes("测试2") &&
|
||||
!p.includes("plateNumber"),
|
||||
),
|
||||
);
|
||||
assert.equal(events.filter((e) => e.type === "step").length, 4);
|
||||
});
|
||||
Reference in New Issue
Block a user