45 lines
2.8 KiB
Python
45 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# patch_sbx_v6.py — фолбэк: если второй раунд после инструмента пуст/упал,
|
|
# юзеру показывается реальный вывод инструмента (а не только пре-текст модели).
|
|
import time, shutil
|
|
|
|
SRC = "/opt/grok-bot/iishko/legacy.py"
|
|
|
|
A1_OLD = ''' for _tc in _tc_list:
|
|
_fn = _tc.get("function") or {}'''
|
|
A1_NEW = ''' _tool_results = []
|
|
for _tc in _tc_list:
|
|
_fn = _tc.get("function") or {}'''
|
|
|
|
A2_OLD = ''' _res = await _bot_tool_exec(_fn.get("name", ""), _args if isinstance(_args, dict) else {})
|
|
new_msgs.append({"role": "tool", "tool_call_id": _tc.get("id", ""), "content": _res})'''
|
|
A2_NEW = ''' _res = await _bot_tool_exec(_fn.get("name", ""), _args if isinstance(_args, dict) else {})
|
|
_tool_results.append(_res)
|
|
new_msgs.append({"role": "tool", "tool_call_id": _tc.get("id", ""), "content": _res})'''
|
|
|
|
A3_OLD = ''' _done = True
|
|
return await gw_chat_stream(new_msgs, model, _raw_send, t, _tool_round=_tool_round + 1, _deadline=_deadline)'''
|
|
A3_NEW = ''' _done = True
|
|
_res_out = await gw_chat_stream(new_msgs, model, _raw_send, t, _tool_round=_tool_round + 1, _deadline=_deadline)
|
|
if not (isinstance(_res_out, str) and _res_out.strip()):
|
|
# [SBX-FALLBACK 1114] второй раунд пуст или упал: показываем
|
|
# реальный вывод инструмента, чтобы юзер не видел «пустоту».
|
|
_tail = "\\n\\n".join([_r for _r in _tool_results if _r])
|
|
await _raw_send(_pfx + (str(_tail)[:1800] if _tail else "инструмент выполнен, вывода нет"))
|
|
return _res_out'''
|
|
|
|
src = open(SRC, encoding="utf-8").read()
|
|
if "SBX-FALLBACK 1114" in src:
|
|
print("ALREADY PATCHED v6")
|
|
else:
|
|
for tag, a in (("A1", A1_OLD), ("A2", A2_OLD), ("A3", A3_OLD)):
|
|
assert src.count(a) == 1, "%s count=%d" % (tag, src.count(a))
|
|
bak = SRC + ".bak-sbx6-" + time.strftime("%Y%m%dT%H%M%S")
|
|
shutil.copy2(SRC, bak)
|
|
src = src.replace(A1_OLD, A1_NEW).replace(A2_OLD, A2_NEW).replace(A3_OLD, A3_NEW)
|
|
open(SRC, "w", encoding="utf-8").write(src)
|
|
print("PATCHED OK, backup:", bak)
|
|
|
|
import py_compile
|
|
py_compile.compile(SRC, doraise=True)
|
|
print("compile OK") |