refactor MixinSession._raw_ask: simplify retry logic, skip yielding first-only error chunk

This commit is contained in:
Liang Jiaqing
2026-03-29 10:53:26 +08:00
parent 1d94384ee5
commit 226fa69cc3
3 changed files with 14 additions and 20 deletions

View File

@@ -91,5 +91,5 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
next_prompt += f"<tool_result>\n{datastr}\n</tool_result>\n\n"
next_prompt += outcome.next_prompt
next_prompt = handler.next_prompt_patcher(next_prompt, None, turn+1)
messages = [{"role": "user", "content": next_prompt}]
messages = [{"role": "user", "content": next_prompt}] # just new message, history is kept in *Session
return {'result': 'MAX_TURNS_EXCEEDED'}

View File

@@ -1,7 +1,7 @@
[
{"type": "function", "function": {
"name": "code_run",
"description": "代码执行器。优先python系统操作用powershell。禁同时调用多个。为免转义问题,代码放正文 ```python/powershell 块中。禁硬编码大量数据",
"description": "代码执行器。优先使用python。禁同时调用多个。为免转义问题代码放正文 ```python/powershell 块中。禁硬编码大量数据",
"parameters": {"type": "object", "properties": {
"script": {"type": "string", "description": "[Optional] 要执行的代码。为免转义建议留空,改用正文代码块(与此参数互斥)"},
"type": {"type": "string", "enum": ["python", "powershell"], "description": "代码类型", "default": "python"},

View File

@@ -814,26 +814,20 @@ class MixinSession:
@property
def primary(self): return self._sessions[0]
def _raw_ask(self, *args, **kwargs):
last_err = None
for attempt in range(self._retries + 1):
gen = self._orig_raw_asks[attempt % len(self._sessions)](*args, **kwargs)
try: first = next(gen)
except StopIteration as e: return e.value or []
if isinstance(first, str) and first.startswith('Error:'):
last_err = first
for _ in gen: pass # drain
if attempt < self._retries:
delay = min(30, self._base_delay * (2 ** attempt))
print(f'[MixinSession] {first[:80]}, retry {attempt+1}/{self._retries} in {delay:.1f}s')
time.sleep(delay); continue
else:
yield first
last_chunk, return_val, yielded = None, [], False
try:
while True: yield next(gen)
except StopIteration as e: return e.value or []
yield last_err or 'Error: all retries exhausted'
return [{'type': 'text', 'text': last_err}]
while True:
chunk = next(gen); last_chunk = chunk
if not yielded and isinstance(chunk, str) and chunk.startswith('Error:'): continue
yield chunk; yielded = True
except StopIteration as e: return_val = e.value or []
if isinstance(last_chunk, str) and last_chunk.startswith('Error:') and attempt < self._retries:
delay = min(30, self._base_delay * (2 ** attempt))
print(f'[MixinSession] {last_chunk[:80]}, retry {attempt+1}/{self._retries} in {delay:.1f}s')
time.sleep(delay); continue
return return_val
class NativeToolClient:
THINKING_PROMPT = """