feat: update xianyu web login and Hermes optimizer
This commit is contained in:
@@ -7,8 +7,9 @@ import { TaskManager } from './lib/task.mjs';
|
||||
import { checkLogin, getLoginStatus, openLoginPage, importCookiesFromJson, getLoginQr } from './lib/login.mjs';
|
||||
import { closeBrowser, getOpenPagesInfo } from './lib/browser.mjs';
|
||||
import {
|
||||
DEFAULT_PERSONA, DEFAULT_COARSE_PROMPT, DEFAULT_FINE_PROMPT, chatCompletion,
|
||||
DEFAULT_PERSONA, DEFAULT_COARSE_PROMPT, DEFAULT_FINE_PROMPT,
|
||||
} from './lib/ai.mjs';
|
||||
import { optimizeTaskWithHermes, buildLocalOptimizedTask } from './lib/hermes_optimizer.mjs';
|
||||
import { buildAnalysis, renderAnalysisMarkdown } from './lib/analyzer.mjs';
|
||||
import { getNotifyConfig, saveNotifyConfig, sendNotifications } from './lib/notifier.mjs';
|
||||
|
||||
@@ -161,43 +162,17 @@ app.post('/api/notify-test', async (req, res) => {
|
||||
app.post('/api/tasks/optimize', async (req, res) => {
|
||||
const { seed = '', product = '', budget = '', region = '', currentRequirements = '' } = req.body || {};
|
||||
const base = [product, seed, currentRequirements].filter(Boolean).join('\n');
|
||||
if (!base.trim()) return res.status(400).json({ error: '请输入商品/需求描述后再优化' });
|
||||
if (!base.trim()) return res.status(400).json({ error: '请输入商品/需求描述后再润色' });
|
||||
|
||||
const fallback = buildLocalOptimizedTask({ seed, product, budget, region, currentRequirements });
|
||||
try {
|
||||
const prompt = `你是闲鱼二手商品监测任务配置助手。根据用户想找的商品,生成更适合闲鱼搜索和风控筛选的任务配置。只输出JSON,不要解释。
|
||||
字段:name:string, queries:string[], priceMin:number|null, priceMax:number|null, maxPages:number, maxItems:number, personalSeller:boolean, customRequirements:string, coarsePrompt:string, finePrompt:string。
|
||||
要求:queries 3-6个,包含常见别名/空格/大小写变体;customRequirements 写清排除配件/维修/求购/商家批量/异常低价;maxItems 保守在20-80。
|
||||
用户输入:${JSON.stringify({ seed, product, budget, region, currentRequirements })}`;
|
||||
const reply = await chatCompletion([{ role: 'user', content: prompt }], { temperature: 0.2, maxTokens: 1200, timeoutMs: 20000 });
|
||||
const cleaned = reply.replace(/```json?\n?/g, '').replace(/```/g, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
res.json({ ok: true, source: 'ai', task: { ...fallback, ...parsed } });
|
||||
const result = await optimizeTaskWithHermes({ seed, product, budget, region, currentRequirements });
|
||||
res.json({ ok: true, source: result.source || 'hermes', task: result.task });
|
||||
} catch (err) {
|
||||
res.json({ ok: true, source: 'local-fallback', warning: err.message, task: fallback });
|
||||
}
|
||||
});
|
||||
|
||||
function buildLocalOptimizedTask({ seed = '', product = '', budget = '', region = '', currentRequirements = '' }) {
|
||||
const text = `${product || seed}`.trim();
|
||||
const queries = [...new Set(text.split(/[,,、]/).map(s => s.trim()).filter(Boolean))];
|
||||
if (queries.length === 0 && text) queries.push(text);
|
||||
const budgetNum = Number(String(budget).match(/\d+(?:\.\d+)?/)?.[0] || '') || null;
|
||||
return {
|
||||
name: text ? `${text} 监测` : '闲鱼商品监测',
|
||||
queries: queries.slice(0, 6),
|
||||
priceMin: null,
|
||||
priceMax: budgetNum,
|
||||
region,
|
||||
maxPages: 2,
|
||||
maxItems: 40,
|
||||
personalSeller: true,
|
||||
customRequirements: `${currentRequirements || `目标:${text}${budgetNum ? `,预算${budgetNum}左右` : ''}`};只做采集分析,不自动联系卖家;排除配件/维修/求购/租赁/回收/商家批量/标题党;优先个人自用、描述清楚、价格合理、可核验。`,
|
||||
coarsePrompt: DEFAULT_COARSE_PROMPT,
|
||||
finePrompt: DEFAULT_FINE_PROMPT,
|
||||
};
|
||||
}
|
||||
|
||||
app.get('/api/default-persona', (req, res) => {
|
||||
res.json({ persona: DEFAULT_PERSONA });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user