tools.py
9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""库存补货:暴露给智能体的工具 + 大模型不可用时的关键词兜底。工具直接给计划员发消息(replied=True)。
群里只做单次补货(群里必须 @ 机器人):「零件号 数量」→ 发起人本人回「订」建单截图(confirm_order,永远不提交)
→ 发起人本人明确回「确认提交 / 提交」正式提交(submit_order,原话不含「提交」一律不执行;回「不提交」先不下单)。
单次补货同一时间只开一单,群里的「订」「确认提交」只可能对应这一单。
定时补货不能在群里发起,也不能在群里确认:清单确认和建单后的「确认提交」都在单据页上点(回「确认」只回引导语和链接)。
订单类型固定 SO,任何工具都没有改订单类型的参数。
"""
from __future__ import annotations
import asyncio
import re
from ...core.types import Actor
from ...services import im
from .. import AgentTool, ToolResult
from .service import svc
async def _say(sid: int, target: dict | None, text: str, ref: str = "") -> None:
if not text:
return
if target:
await asyncio.to_thread(im.send_to, target, text, None, "reply", None, ref)
else:
await im.broadcast(sid, text, ref=ref)
def _scheduled_ref(sid: int) -> tuple[dict | None, str]:
cur = svc.current(sid)
return cur, svc.ref(cur["id"]) if cur else ""
async def single_restock(sid: int, actor: Actor, target: dict | None, part_no: str = "", qty: int = 0) -> ToolResult:
msg = await svc.single_request(sid, actor, part_no, int(qty or 0), actor.extra.get("text", ""))
single = svc.single_open(sid)
ref = svc.ref(single["id"]) if single else ""
await _say(sid, target, msg, ref)
return ToolResult(msg, ref=ref)
async def confirm_order(sid: int, actor: Actor, target: dict | None) -> ToolResult:
"""只作用于单次补货:待确认 → 建单截图(只认发起人)。永远不提交:待确认提交时只回「请回『确认提交』」。
只有定时补货在等时只回引导语(定时补货在单据页确认)。"""
msg, ref = await svc.chat_confirm(sid, actor)
await _say(sid, target, msg, ref)
return ToolResult(msg, ref=ref)
async def submit_order(sid: int, actor: Actor, target: dict | None) -> ToolResult:
"""明确说「确认提交 / 提交」:提交待确认提交的单次补货(只认发起人;原话不含「提交」一律不执行)。"""
msg, ref = await svc.chat_submit(sid, actor)
await _say(sid, target, msg, ref)
return ToolResult(msg, ref=ref)
async def cancel_order(sid: int, actor: Actor, target: dict | None) -> ToolResult:
"""取消单次补货:待确认 / 查询中的直接不订;建单后待确认提交的「不提交」(DMS 草稿保留待提交)。"""
single = svc.single_open(sid)
if single:
ref = svc.ref(single["id"])
msg = await svc.single_cancel(sid, actor) # 取消成功时 service 自己在群里回复
if msg:
await _say(sid, target, msg, ref)
return ToolResult(msg or "已取消单次补货", ref=ref)
pending = svc.scheduled_awaiting_submit(sid)
cur, ref = _scheduled_ref(sid)
if pending:
msg = "定时补货请在链接页面里点「先不提交」👉 " + svc.link(pending[-1])
ref = svc.ref(pending[-1]["id"])
elif cur and cur["status"] == "awaiting_confirm":
msg = "定时补货请在链接页面里点「今天不订」👉 " + svc.link(cur)
else:
msg = svc.t(sid, "msg_no_pending")
await _say(sid, target, msg, ref)
return ToolResult(msg, ref=ref)
async def get_status(sid: int, actor: Actor, target: dict | None) -> ToolResult:
msg = svc.status_text(sid)
await _say(sid, target, msg, _scheduled_ref(sid)[1])
return ToolResult(msg)
async def scheduled_restock(sid: int, actor: Actor, target: dict | None) -> ToolResult:
"""群里想发起 / 确认定时补货:只回引导语(定时补货只能定时推送或后台按钮触发,确认必须走页面)。"""
pending = svc.scheduled_awaiting_submit(sid)
cur, ref = _scheduled_ref(sid)
if pending or (cur and cur["status"] == "awaiting_confirm"):
tgt = pending[-1] if pending else cur
msg = svc.t(sid, "msg_scheduled_use_link", link=svc.link(tgt), stage=tgt["status"],
order_no=tgt["order_no"] or "")
ref = svc.ref(tgt["id"])
else:
msg = svc.t(sid, "msg_scheduled_no_chat", push_time=svc.cfg(sid).push_time)
svc.audit(sid, actor, "run.chat_start_blocked", target_type="run", target_id=str(cur["id"]) if cur else "",
ref=ref, detail={"text": actor.extra.get("text", "")})
await _say(sid, target, msg, ref)
return ToolResult(msg)
_OBJ = {"type": "object", "properties": {}}
TOOLS = [
AgentTool("single_restock", "单次补货:计划员在群里给出一个零件号和数量,查库存 / 在途 / 当日可订 / 关注仓后回复,等他回「订」再建单。",
{"type": "object", "properties": {
"part_no": {"type": "string", "description": "零件号,原样传入(如 8713902280、90915YZZM3)"},
"qty": {"type": "integer", "description": "要订的数量"}},
"required": ["part_no", "qty"]}, single_restock),
AgentTool("confirm_order", "计划员回「订」「可以」「确认」「没问题」「下单」时调用,只作用于单次补货:报价后回 → 在 DMS "
"建单(待提交)并截图(只认发起人本人)。它永远不会提交采购单。定时补货不能在群里确认,会回复请点开链接。",
_OBJ, confirm_order),
AgentTool("submit_order", "只有计划员的话里明确含「提交」两个字(如「确认提交」「提交」「可以,提交吧」)时才调用:"
"正式提交已经建好、截过图的单次补货采购单(只认发起人本人)。只说「订」「好的」「可以」「确认」「ok」时"
"绝对不要调用;说「不提交」时调用 cancel_order。", _OBJ, submit_order),
AgentTool("cancel_order", "计划员说不订 / 不定(「不订」的同音错字)/ 不要了 / 取消 / 不提交 / 先不提交:取消当前的单次补货"
"(查询中、待确认可以不订;建单后待确认提交的「不提交」,DMS 草稿保留待提交)。", _OBJ, cancel_order),
AgentTool("get_status", "查询今天补货的进度和状态。", _OBJ, get_status),
AgentTool("scheduled_restock", "计划员想在群里发起或确认今天的定时补货(如「帮我查一下库存补货」「今天补货确认」"
"「定时补货提交」):只回复引导语,定时补货每天定时推送,确认清单和确认提交都要点开链接。", _OBJ, scheduled_restock),
]
PART_RE = re.compile(r"(?<![0-9A-Za-z])([0-9A-Za-z][0-9A-Za-z\-]{6,18}[0-9A-Za-z])(?![0-9A-Za-z])")
QTY_RE = re.compile(r"(?:[×xX\*]\s*)?(\d{1,5})\s*(?:个|件|只|条|瓶|桶|支|套|盒|罐|升|L|pcs)?")
def parse_single(text: str) -> tuple[str, int] | None:
"""「@配件订货员 8713902280 空调滤 2 个」→ ("8713902280", 2)。零件号 = 8~20 位字母数字(必须含数字),
数量 = 零件号后面第一个独立的数字。"""
t = (text or "").replace(":", ":").replace(",", " ")
for m in PART_RE.finditer(t):
pn = m.group(1).replace("-", "")
if not any(ch.isdigit() for ch in pn) or len(pn) < 8:
continue
rest = t[m.end():]
q = QTY_RE.search(rest)
if q and int(q.group(1)) > 0:
return pn.upper(), int(q.group(1))
return None
CANCEL_RE = re.compile(r"不订|不定|不下单|别订|先不订|暂不订|不要了|不用了|不需要|取消|算了|不提交|先不提交|别提交|暂不提交|不用提交")
# 「订」「确认」「可以」「好的 下单」……:由若干个肯定词组合而成、没有别的内容(不含「提交」:提交必须明确说)
_YES = r"(?:订|订吧|订了|确认|确定|没问题|可以|好的?|行|ok|OK|Ok|下单|下吧)"
CONFIRM_RE = re.compile(rf"{_YES}(?:[,,、。\s]*(?:{_YES}|吧|的|了|啊|呀))*[。!!.~~\s]*")
# 「提交」「确认提交」「可以,提交吧」「没问题,提交」:肯定词(可选)+ 提交,没有别的内容
SUBMIT_RE = re.compile(rf"(?:{_YES}[,,、。\s]*)*提交(?:吧|了|啊|呀)?[。!!.~~\s]*")
def fallback_intent(text: str) -> tuple[str, dict] | None:
t = text.strip()
if CANCEL_RE.search(t): # 先判取消:「不订 8713902280 2个」不能被当成新的单次补货去查 DMS
return "cancel_order", {}
hit = parse_single(t)
if hit:
return "single_restock", {"part_no": hit[0], "qty": hit[1]}
if re.search(r"定时补货", t):
return "scheduled_restock", {}
if SUBMIT_RE.fullmatch(t): # 先判提交:「确认提交」不能被当成「确认」
return "submit_order", {}
if CONFIRM_RE.fullmatch(t):
return "confirm_order", {}
if re.search(r"状态|进度|到哪", t):
return "get_status", {}
if re.search(r"补货|库存", t):
return "scheduled_restock", {}
return None