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,4 +1,4 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import {
|
||||
RotateCcw,
|
||||
Link2,
|
||||
@@ -17,9 +17,9 @@ import {
|
||||
getDeals,
|
||||
updateDeal,
|
||||
clearDeals,
|
||||
getNeedsClassify,
|
||||
runReconciliation,
|
||||
classifyTransactions,
|
||||
classifyAll,
|
||||
getClassifyStatus,
|
||||
formatAmount,
|
||||
SOURCE_TYPE_MAP,
|
||||
getCategories,
|
||||
@@ -80,6 +80,8 @@ export default function Transactions() {
|
||||
const [clearConfirm, setClearConfirm] = useState(false);
|
||||
const [editingDeal, setEditingDeal] = useState(null);
|
||||
const [dealFilters, setDealFilters] = useState({ ...DEAL_FILTER_DEFAULT });
|
||||
const classifyPollRef = useRef(null);
|
||||
const reloadRef = useRef(null);
|
||||
|
||||
/* sort helpers */
|
||||
function dealToggleSort(field) {
|
||||
@@ -141,38 +143,66 @@ export default function Transactions() {
|
||||
setToast({ message: msg, type, key: Date.now() });
|
||||
}
|
||||
|
||||
/* restore classify progress */
|
||||
useEffect(() => {
|
||||
const saved = sessionStorage.getItem("classifyTask");
|
||||
if (!saved) return;
|
||||
const task = JSON.parse(saved);
|
||||
if (Date.now() - task.ts > 300000) {
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
return;
|
||||
function stopClassifyPoll() {
|
||||
if (classifyPollRef.current) {
|
||||
clearInterval(classifyPollRef.current);
|
||||
classifyPollRef.current = null;
|
||||
}
|
||||
setClassifying(true);
|
||||
setClassifyProgress(task.progress || 0);
|
||||
const poll = setInterval(async () => {
|
||||
}
|
||||
|
||||
// 轮询服务端全量分类进度
|
||||
function pollClassifyStatus() {
|
||||
const tick = async () => {
|
||||
try {
|
||||
const { total } = await getNeedsClassify();
|
||||
const p =
|
||||
task.total > 0
|
||||
? Math.round(((task.total - total) / task.total) * 100)
|
||||
: 100;
|
||||
setClassifyProgress(Math.min(Math.max(p, 0), 100));
|
||||
if (total <= 0 || p >= 100) {
|
||||
clearInterval(poll);
|
||||
const s = await getClassifyStatus();
|
||||
if (s.running) {
|
||||
setClassifying(true);
|
||||
setClassifyProgress(
|
||||
s.total ? Math.min(100, Math.round((s.done / s.total) * 100)) : 0,
|
||||
);
|
||||
} else {
|
||||
stopClassifyPoll();
|
||||
setClassifying(false);
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
loadTransactions();
|
||||
setClassifyProgress(100);
|
||||
showToast("分类完成!", "success");
|
||||
if (reloadRef.current) reloadRef.current();
|
||||
}
|
||||
} catch (e) {
|
||||
clearInterval(poll);
|
||||
stopClassifyPoll();
|
||||
setClassifying(false);
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
}
|
||||
}, 10000);
|
||||
return () => clearInterval(poll);
|
||||
};
|
||||
stopClassifyPoll();
|
||||
classifyPollRef.current = setInterval(tick, 2000);
|
||||
tick();
|
||||
}
|
||||
|
||||
/* 保持 reload 回调最新,避免轮询里拿到旧 filters */
|
||||
useEffect(() => {
|
||||
reloadRef.current = () => {
|
||||
loadTransactions();
|
||||
loadDeals(dealFilters);
|
||||
};
|
||||
});
|
||||
|
||||
/* 恢复全量分类进度:任务在服务端后台执行,刷新页面后继续显示 */
|
||||
useEffect(() => {
|
||||
let stopped = false;
|
||||
(async () => {
|
||||
try {
|
||||
const s = await getClassifyStatus();
|
||||
if (stopped || !s.running) return;
|
||||
setClassifying(true);
|
||||
setClassifyProgress(
|
||||
s.total ? Math.min(100, Math.round((s.done / s.total) * 100)) : 0,
|
||||
);
|
||||
pollClassifyStatus();
|
||||
} catch (e) {}
|
||||
})();
|
||||
return () => {
|
||||
stopped = true;
|
||||
stopClassifyPoll();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -277,66 +307,21 @@ export default function Transactions() {
|
||||
}
|
||||
|
||||
async function handleClassifyAll() {
|
||||
setClassifying(true);
|
||||
setClassifyProgress(0);
|
||||
sessionStorage.setItem(
|
||||
"classifyTask",
|
||||
JSON.stringify({ total: 0, progress: 0, ts: Date.now() }),
|
||||
);
|
||||
try {
|
||||
const { ids, total } = await getNeedsClassify();
|
||||
if (!ids.length) {
|
||||
const { total } = await classifyAll();
|
||||
if (!total) {
|
||||
showToast("所有交易分类均有效!", "info");
|
||||
setClassifying(false);
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
return;
|
||||
}
|
||||
sessionStorage.setItem(
|
||||
"classifyTask",
|
||||
JSON.stringify({ total, progress: 0, ts: Date.now() }),
|
||||
);
|
||||
// 分批并行处理(每批30条,最多2批同时进行)
|
||||
const B = 30;
|
||||
const CONCURRENCY = 4;
|
||||
const batches = [];
|
||||
for (let i = 0; i < ids.length; i += B) {
|
||||
batches.push(ids.slice(i, i + B));
|
||||
}
|
||||
|
||||
let done = 0;
|
||||
let llmCount = 0;
|
||||
let rulesCount = 0;
|
||||
|
||||
for (let i = 0; i < batches.length; i += CONCURRENCY) {
|
||||
const chunk = batches.slice(i, i + CONCURRENCY);
|
||||
const results = await Promise.all(
|
||||
chunk.map((batch) => classifyTransactions(batch)),
|
||||
);
|
||||
for (const r of results) {
|
||||
done += r.classified || 0;
|
||||
if (r.source === "llm") llmCount += r.classified || 0;
|
||||
else rulesCount += r.classified || 0;
|
||||
}
|
||||
const pct = Math.round((done / total) * 100);
|
||||
setClassifyProgress(pct);
|
||||
sessionStorage.setItem(
|
||||
"classifyTask",
|
||||
JSON.stringify({ total, progress: pct, ts: Date.now() }),
|
||||
);
|
||||
}
|
||||
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
const detail =
|
||||
llmCount > 0
|
||||
? `(LLM: ${llmCount},规则: ${rulesCount})`
|
||||
: `(规则匹配: ${rulesCount})`;
|
||||
showToast(`分类完成!共处理 ${total} 笔 ${detail}`, "success");
|
||||
loadTransactions();
|
||||
setClassifying(true);
|
||||
setClassifyProgress(0);
|
||||
pollClassifyStatus();
|
||||
} catch (e) {
|
||||
sessionStorage.removeItem("classifyTask");
|
||||
showToast("分类失败: " + (e.response?.data?.error || e.message), "error");
|
||||
showToast(
|
||||
"分类启动失败: " + (e.response?.data?.error || e.message),
|
||||
"error",
|
||||
);
|
||||
}
|
||||
setClassifying(false);
|
||||
}
|
||||
|
||||
/* ============================== RENDER ============================== */
|
||||
|
||||
Reference in New Issue
Block a user