myagent-ai 1.47.22 → 1.47.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agents/main_agent.py +73 -12
- package/package.json +1 -1
package/agents/main_agent.py
CHANGED
|
@@ -943,16 +943,77 @@ class MainAgent(BaseAgent):
|
|
|
943
943
|
_reasoning_parts.append(delta_text)
|
|
944
944
|
await self._emit_v2_event("v2_reasoning", {"content": delta_text}, stream_callback)
|
|
945
945
|
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
946
|
+
# 可重试的 LLM 调用(最多 5 次)
|
|
947
|
+
_max_llm_retries = 5
|
|
948
|
+
_llm_retry_count = 0
|
|
949
|
+
response = None
|
|
950
|
+
|
|
951
|
+
while _llm_retry_count < _max_llm_retries:
|
|
952
|
+
try:
|
|
953
|
+
if stream_response and self.llm:
|
|
954
|
+
response = await self._call_llm_stream(
|
|
955
|
+
messages,
|
|
956
|
+
tools=tools if tools else None,
|
|
957
|
+
text_delta_callback=text_delta_callback,
|
|
958
|
+
reasoning_delta_callback=_reasoning_delta_cb,
|
|
959
|
+
stream_response=stream_response,
|
|
960
|
+
)
|
|
961
|
+
else:
|
|
962
|
+
response = await self._call_llm(messages, tools=tools if tools else None)
|
|
963
|
+
except Exception as _llm_exc:
|
|
964
|
+
# 异常类错误 → 判断是否可重试
|
|
965
|
+
_llm_exc_str = str(_llm_exc).lower()
|
|
966
|
+
_is_retryable = any(kw in _llm_exc_str for kw in (
|
|
967
|
+
"connection", "timeout", "timed out",
|
|
968
|
+
"429", "500", "502", "503", "504",
|
|
969
|
+
"rate_limit", "rate limit", "overloaded", "capacity",
|
|
970
|
+
"network", "eof",
|
|
971
|
+
))
|
|
972
|
+
_llm_retry_count += 1
|
|
973
|
+
if _is_retryable and _llm_retry_count < _max_llm_retries:
|
|
974
|
+
_delay = 2.0 * (2 ** (_llm_retry_count - 1)) # 2s, 4s, 8s, 16s
|
|
975
|
+
logger.warning(
|
|
976
|
+
f"[{task_id}] LLM 调用异常 (第 {_llm_retry_count}/{_max_llm_retries} 次),"
|
|
977
|
+
f"{_delay:.0f}s 后重试: {_llm_exc}"
|
|
978
|
+
)
|
|
979
|
+
await self._emit_v2_event("v2_reasoning", {
|
|
980
|
+
"content": f"⏳ 网络不稳定,正在重试 ({_llm_retry_count}/{_max_llm_retries})..."
|
|
981
|
+
}, stream_callback)
|
|
982
|
+
await asyncio.sleep(_delay)
|
|
983
|
+
continue
|
|
984
|
+
else:
|
|
985
|
+
# 不可重试 或 已达上限 → 包装为失败 response
|
|
986
|
+
logger.error(f"[{task_id}] LLM 调用异常 (已重试 {_llm_retry_count} 次): {_llm_exc}")
|
|
987
|
+
response = type("LLMResponse", (), {"success": False, "error": str(_llm_exc)})()
|
|
988
|
+
break
|
|
989
|
+
|
|
990
|
+
# 没有异常 → 检查 response.success
|
|
991
|
+
if response.success:
|
|
992
|
+
break # 成功 → 跳出重试循环
|
|
993
|
+
|
|
994
|
+
# response.success == False 但没抛异常 → 判断错误是否可重试
|
|
995
|
+
_llm_error = (response.error or "").lower()
|
|
996
|
+
_is_retryable = any(kw in _llm_error for kw in (
|
|
997
|
+
"connection", "timeout", "timed out",
|
|
998
|
+
"429", "500", "502", "503", "504",
|
|
999
|
+
"rate_limit", "rate limit", "overloaded", "capacity",
|
|
1000
|
+
"network", "eof",
|
|
1001
|
+
))
|
|
1002
|
+
_llm_retry_count += 1
|
|
1003
|
+
if _is_retryable and _llm_retry_count < _max_llm_retries:
|
|
1004
|
+
_delay = 2.0 * (2 ** (_llm_retry_count - 1))
|
|
1005
|
+
logger.warning(
|
|
1006
|
+
f"[{task_id}] LLM 返回失败 (第 {_llm_retry_count}/{_max_llm_retries} 次),"
|
|
1007
|
+
f"{_delay:.0f}s 后重试: {response.error}"
|
|
1008
|
+
)
|
|
1009
|
+
await self._emit_v2_event("v2_reasoning", {
|
|
1010
|
+
"content": f"⏳ 网络不稳定,正在重试 ({_llm_retry_count}/{_max_llm_retries})..."
|
|
1011
|
+
}, stream_callback)
|
|
1012
|
+
await asyncio.sleep(_delay)
|
|
1013
|
+
continue
|
|
1014
|
+
else:
|
|
1015
|
+
# 不可重试 或 已达上限 → 保持失败 response,退出循环
|
|
1016
|
+
break
|
|
956
1017
|
|
|
957
1018
|
# LLM 调用失败处理
|
|
958
1019
|
if not response.success:
|
|
@@ -982,8 +1043,8 @@ class MainAgent(BaseAgent):
|
|
|
982
1043
|
await self._emit_v2_event("v2_reasoning", {"content": _vision_skip_msg}, stream_callback)
|
|
983
1044
|
break
|
|
984
1045
|
|
|
985
|
-
#
|
|
986
|
-
error_msg = f"LLM
|
|
1046
|
+
# 其他错误(含已耗尽重试次数的连接错误)
|
|
1047
|
+
error_msg = f"LLM 调用失败 (已重试 {_llm_retry_count} 次): {_llm_error}"
|
|
987
1048
|
context.working_memory["final_response"] = error_msg
|
|
988
1049
|
await self._emit_v2_event("v2_reasoning", {"content": error_msg}, stream_callback)
|
|
989
1050
|
break
|