Drop `parent_id` from categories, remove `ai_category` and `review_notes` from transactions, and eliminate category/reviewed filters from transaction queries. Clean up code style and formatting.
90 lines
2.6 KiB
JavaScript
90 lines
2.6 KiB
JavaScript
const express = require("express");
|
|
const { getDB } = require("../db");
|
|
const {
|
|
loadConfig,
|
|
saveConfig,
|
|
getOpenAIClient,
|
|
resetClient,
|
|
} = require("../services/llm");
|
|
const router = express.Router();
|
|
|
|
// GET /api/config
|
|
router.get("/", (req, res) => {
|
|
res.json(loadConfig());
|
|
});
|
|
|
|
// PUT /api/config — 同时存 JSON + 清 LLM 缓存
|
|
router.put("/", (req, res) => {
|
|
const cfg = { ...loadConfig(), ...req.body };
|
|
saveConfig(cfg);
|
|
resetClient();
|
|
res.json({ success: true });
|
|
});
|
|
|
|
// POST /api/config/test-llm
|
|
router.post("/test-llm", async (req, res) => {
|
|
console.log("[Reco] === LLM 连接测试 ===");
|
|
const cfg = loadConfig();
|
|
console.log(
|
|
" API Key:",
|
|
cfg.llm_api_key ? cfg.llm_api_key.substring(0, 8) + "..." : "(无)",
|
|
);
|
|
console.log(" Base URL:", cfg.llm_base_url || "默认");
|
|
console.log(" Model:", cfg.llm_model || "默认");
|
|
|
|
const client = getOpenAIClient();
|
|
if (!client) return res.status(400).json({ error: "请先配置 LLM API Key" });
|
|
|
|
const t0 = Date.now();
|
|
try {
|
|
const resp = await client.chat.completions.create({
|
|
model: client._model,
|
|
messages: [{ role: "user", content: '回复"连接成功"' }],
|
|
max_tokens: 10,
|
|
timeout: 15000,
|
|
});
|
|
const reply = resp.choices?.[0]?.message?.content || "(空)";
|
|
console.log(` 成功 ${Date.now() - t0}ms: ${reply}`);
|
|
res.json({
|
|
success: true,
|
|
reply,
|
|
model: client._model,
|
|
elapsed_ms: Date.now() - t0,
|
|
});
|
|
} catch (err) {
|
|
console.error(` 失败 ${Date.now() - t0}ms:`, err.message);
|
|
res.status(500).json({ error: `连接失败: ${err.message}` });
|
|
}
|
|
});
|
|
|
|
// Categories
|
|
router.get("/categories", (req, res) => {
|
|
res.json(getDB().prepare("SELECT * FROM categories ORDER BY id").all());
|
|
});
|
|
|
|
router.put("/categories/:id", (req, res) => {
|
|
const { name, keywords } = req.body;
|
|
getDB()
|
|
.prepare(
|
|
"UPDATE categories SET name=COALESCE(?,name), keywords=COALESCE(?,keywords) WHERE id=?",
|
|
)
|
|
.run(name, keywords ? JSON.stringify(keywords) : null, req.params.id);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
router.post("/categories", (req, res) => {
|
|
const { name, keywords } = req.body;
|
|
if (!name) return res.status(400).json({ error: "分类名称不能为空" });
|
|
const r = getDB()
|
|
.prepare("INSERT INTO categories (name,keywords) VALUES (?,?)")
|
|
.run(name, JSON.stringify(keywords || []));
|
|
res.json({ success: true, id: r.lastInsertRowid });
|
|
});
|
|
|
|
router.delete("/categories/:id", (req, res) => {
|
|
getDB().prepare("DELETE FROM categories WHERE id=?").run(req.params.id);
|
|
res.json({ success: true });
|
|
});
|
|
|
|
module.exports = router;
|