feat(mcp): 新增 Excel 批量清单匹配工具并调整分页与采纳锚策略
This commit is contained in:
@@ -100,11 +100,12 @@ def _sess(fn):
|
||||
|
||||
|
||||
def _paging(args):
|
||||
"""limit 归一:默认 50,1~200。"""
|
||||
"""limit 归一:默认 10,1~100。
|
||||
2026-11-02 主人定:智能体命令没说条数=只给匹配前 10 条;说了最多给到 100。"""
|
||||
try:
|
||||
return min(max(int(args.get("limit") or 50), 1), 200)
|
||||
return min(max(int(args.get("limit") or 10), 1), 100)
|
||||
except (TypeError, ValueError):
|
||||
return 50
|
||||
return 10
|
||||
|
||||
|
||||
def _need_scope(args):
|
||||
@@ -531,13 +532,82 @@ def _t_attachment_download(args):
|
||||
return {"ok": True, "files": out}
|
||||
|
||||
|
||||
def _t_excel_bill_match(args):
|
||||
"""⑩ Excel 批量清单匹配(2026-11-02 主人定,只读):Excel **只放一列清单名称**(A 列,
|
||||
首行若是「清单名称/名称」表头自动跳过),逐行在全部入库项目清单里匹配**每条最优一条**
|
||||
——期次近优先、同批次名称相似度高优先。期次/地区筛选=对比框同款:
|
||||
period_from/period_to(YYYY-MM,可选,同时是期次距离基准)、province/city/district(可选)。
|
||||
行数不限;Excel 无编码无单位 → 走名称相似匹配(match_bill unit_free),无「精确编码」档属预期。"""
|
||||
a = _api()
|
||||
b64 = (args.get("file_b64") or "").strip()
|
||||
if not b64:
|
||||
return {"ok": False, "msg": "file_b64(Excel 的 base64 内容,A 列=清单名称)必填——先向用户要 Excel 或请用户粘贴名称清单"}
|
||||
import base64 as _b64
|
||||
try:
|
||||
raw = _b64.b64decode(b64, validate=True)
|
||||
except Exception:
|
||||
return {"ok": False, "msg": "file_b64 不是合法 base64"}
|
||||
pf = _period_to_m(args.get("period_from"))
|
||||
pt = _period_to_m(args.get("period_to") or args.get("period_from"))
|
||||
prov = (args.get("province") or "").strip()
|
||||
city = (args.get("city") or "").strip()
|
||||
dist = (args.get("district") or "").strip()
|
||||
area = [(prov, city, dist)] if (prov or city or dist) else None
|
||||
try:
|
||||
import io as _io
|
||||
import openpyxl
|
||||
wb = openpyxl.load_workbook(_io.BytesIO(raw), read_only=True, data_only=True)
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": "Excel 读取失败:%s" % e}
|
||||
names = []
|
||||
try:
|
||||
ws = wb.worksheets[0]
|
||||
for i, rx in enumerate(ws.iter_rows(min_col=1, max_col=1, values_only=True)):
|
||||
v = rx[0] if rx else None
|
||||
v = str(v).strip() if v is not None else ""
|
||||
if not v:
|
||||
continue
|
||||
if i == 0 and v in ("清单名称", "名称", "项目名称", "清单"):
|
||||
continue # 表头行
|
||||
names.append(v)
|
||||
finally:
|
||||
wb.close()
|
||||
if not names:
|
||||
return {"ok": False, "msg": "Excel A 列没有读到任何清单名称"}
|
||||
hc = a.conn()
|
||||
rows_out = []
|
||||
try:
|
||||
base = pf if pf is not None else 0
|
||||
for nm in names:
|
||||
fake = {"Name": nm, "Unit": "", "Attr": "", "x_hash_exact": None, "x_hash_semi": None}
|
||||
try:
|
||||
res = a.match.match_bill(hc, fake, base, [], [], [], limit=8,
|
||||
pfrom=pf, pto=pt, area=area, unit_free=True)
|
||||
except Exception:
|
||||
res = []
|
||||
best = sorted(res, key=lambda r: (r.get("dist") or 0, -(r.get("sim") or 0)))[0] if res else None
|
||||
if best:
|
||||
rows_out.append({"name": nm, "hit": True, "tier": best.get("tier"),
|
||||
"match_name": best.get("name"), "unit": best.get("unit"),
|
||||
"price": best.get("price"), "proj": best.get("proj"),
|
||||
"region": best.get("region"), "period": best.get("period"),
|
||||
"sim": best.get("sim")})
|
||||
else:
|
||||
rows_out.append({"name": nm, "hit": False, "msg": "没有匹配到足够相似的历史清单"})
|
||||
finally:
|
||||
hc.close()
|
||||
return {"ok": True, "total": len(rows_out), "hit": sum(1 for r in rows_out if r["hit"]),
|
||||
"hint": "每行=Excel 一条清单的最优匹配:期次近优先、同批次名称相似度优先;price=该清单历史单价(未匹配行带 hit=false)",
|
||||
"rows": rows_out}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 工具清单
|
||||
|
||||
def TOOLS():
|
||||
return [
|
||||
{"name": "bills_query",
|
||||
"description": "查入库项目的清单:编号 Number、名称 Name、项目特征 Attr、单价 Price。"
|
||||
"keyword(编号/名称关键词)或 batch_id 至少给一个;limit 默认 50 上限 200。"
|
||||
"keyword(编号/名称关键词)或 batch_id 至少给一个;limit 默认 10 上限 100(不传 limit=只返回匹配前 10 条)。"
|
||||
"响应不含附件信息。",
|
||||
"inputSchema": {"type": "object", "properties": {
|
||||
"keyword": {"type": "string"}, "batch_id": {"type": "integer"},
|
||||
@@ -622,6 +692,18 @@ def TOOLS():
|
||||
"ref": {"type": "string"}},
|
||||
"required": ["session_id", "lme_id", "ref"]},
|
||||
"run": _t_lme_adopt},
|
||||
{"name": "excel_bill_match",
|
||||
"description": "Excel 批量清单匹配(只读,数量不限)。Excel 只放一列清单名称(A 列,"
|
||||
"可有「清单名称」表头),file_b64=Excel 的 base64;逐行在全部入库项目清单里"
|
||||
"匹配每条最优一条(期次近优先、同批次名称相似度优先)。期次/地区沿用对比框"
|
||||
"筛选口径:period_from/period_to(YYYY-MM,可选)、province/city/district(可选)。"
|
||||
"适合「按某期查一批清单的历史单价」。",
|
||||
"inputSchema": {"type": "object", "properties": {
|
||||
"file_b64": {"type": "string"}, "period_from": {"type": "string"},
|
||||
"period_to": {"type": "string"}, "province": {"type": "string"},
|
||||
"city": {"type": "string"}, "district": {"type": "string"}},
|
||||
"required": ["file_b64"]},
|
||||
"run": _t_excel_bill_match},
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user