Rework LLM classification around user categories
Move category CRUD into the config route so renames sync historical deals, feed per-user categories into the LLM prompt, cache clients per user, and drop the separate classify route.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const express = require("express");
|
||||
const { getDB } = require("../db");
|
||||
const { runAutoReconciliation } = require("../services/matcher");
|
||||
const { classifyDeals, classifyAllDeals } = require("../services/llm");
|
||||
const { classifyDeals, startClassifyAll, getClassifyStatus } = require("../services/llm");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -111,25 +111,6 @@ router.get("/", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/needs-classify", (req, res) => {
|
||||
const db = getDB();
|
||||
const cats = db
|
||||
.prepare("SELECT name FROM categories WHERE user_id = ?")
|
||||
.all(req.userId)
|
||||
.map((r) => r.name);
|
||||
if (!cats.length) return res.json({ ids: [], total: 0 });
|
||||
const ph = cats.map(() => "?").join(",");
|
||||
const stale = db
|
||||
.prepare(
|
||||
`SELECT id FROM deals WHERE (category IS NULL OR category NOT IN (${ph})) AND user_id = ?`,
|
||||
)
|
||||
.all(...cats, req.userId);
|
||||
console.log(
|
||||
`[Reco] GET /transactions/needs-classify — 返回 ${stale.length} 个待分类 deal`,
|
||||
);
|
||||
res.json({ ids: stale.map((r) => r.id), total: stale.length });
|
||||
});
|
||||
|
||||
// POST /api/transactions/reconcile - 全量清除后重新对账
|
||||
router.post("/reconcile", (req, res) => {
|
||||
const db = getDB();
|
||||
@@ -225,19 +206,18 @@ router.post("/classify", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/transactions/classify-all — AI分类全部(并行)
|
||||
router.post("/classify-all", async (req, res) => {
|
||||
console.log(`[Reco] POST /transactions/classify-all — 开始`);
|
||||
try {
|
||||
const result = await classifyAllDeals(req.userId);
|
||||
console.log(
|
||||
`[Reco] POST /transactions/classify-all — 完成, classified: ${result.classified}, total: ${result.total}, source: ${result.source}`,
|
||||
);
|
||||
res.json({ success: true, ...result });
|
||||
} catch (err) {
|
||||
console.error("[Reco] 分类全部失败:", err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
// POST /api/transactions/classify-all — 启动全量分类(后台执行,前端轮询状态)
|
||||
router.post("/classify-all", (req, res) => {
|
||||
const result = startClassifyAll(req.userId);
|
||||
console.log(
|
||||
`[Reco] POST /transactions/classify-all — started=${result.started}, total=${result.total}`,
|
||||
);
|
||||
res.json({ success: true, ...result });
|
||||
});
|
||||
|
||||
// GET /api/transactions/classify-status — 全量分类进度
|
||||
router.get("/classify-status", (req, res) => {
|
||||
res.json(getClassifyStatus(req.userId));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user