loggorilla implementation for agent_loop

This commit is contained in:
Dita Aji Pratama 2026-09-27 06:21:49 +07:00
parent d84328d7a1
commit de4e3cadb4

View File

@ -2,6 +2,7 @@ import json
from datetime import datetime
from lib.personality import PERSONALITY
from tools.vision import ImagePayload
from scripts import loggorilla
def _ts():
return datetime.now().strftime('%H:%M:%S')
@ -30,7 +31,7 @@ def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on
session_ended = False
for step in range(max_iterations):
print(f'[{_ts()}] Step {step + 1} — calling LLM...', flush=True)
loggorilla.prcss("agent_loop", f"Step {step + 1} — calling LLM...")
disable_reasoning = PERSONALITY.disable_reasoning
@ -45,7 +46,7 @@ def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on
session.messages.append(amsg)
tnames = [tc['function']['name'] for tc in response.tool_calls]
print(f'[{_ts()}] Using tools: {", ".join(tnames)}', flush=True)
loggorilla.fyinf("agent_loop", f"Using tools: {', '.join(tnames)}")
if on_tool_calls:
on_tool_calls(response.content or "")
@ -75,20 +76,19 @@ def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on
session.messages.append({'role': 'assistant', 'content': response.content})
if isinstance(response.content, list):
text_parts = [b.get('text', '') for b in response.content
if isinstance(b, dict) and b.get('type') == 'text']
if isinstance(b, dict) and b.get('type') == 'text']
text = '\n'.join(text_parts) if text_parts else "[Image generated]"
print(f'[{_ts()}] Response generated (multimodal)', flush=True)
loggorilla.prcss("agent_loop", "Response generated (multimodal)")
return text, session_ended
print(f'[{_ts()}] Response generated ({len(response.content)} chars)', flush=True)
loggorilla.prcss("agent_loop", f"Response generated ({len(response.content)} chars)")
return response.content, session_ended
# Respons kosong: tetap bawa flag session_ended — bisa jadi
# end_session sudah dipanggil di iterasi sebelumnya.
return None, session_ended
print(f'[{_ts()}] Max iterations ({max_iterations}) reached', flush=True)
loggorilla.prcss("agent_loop", f"Max iterations ({max_iterations}) reached")
session.messages.append({
'role': 'assistant',
'content': 'Max iterations reached without final answer.',
})
return None, session_ended