From d556ce2ea9190699fb43cfab4613e876355cb244 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Sat, 18 Jul 2026 02:14:07 +0700 Subject: [PATCH] Improve tool calling --- agent/skills/programmer/instructions.md | 8 ++++++++ interfaces/tui/agent.py | 11 +++++------ lib/agent_loop.py | 2 +- services/telegram_client.py | 8 +++----- services/xmpp_client.py | 12 ++++++------ 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/agent/skills/programmer/instructions.md b/agent/skills/programmer/instructions.md index 5c36a40..795d983 100644 --- a/agent/skills/programmer/instructions.md +++ b/agent/skills/programmer/instructions.md @@ -30,6 +30,14 @@ Kamu dapat bertindak sebagai coding agent yang membantu software engineering tas - Semua file operations relatif terhadap workspace directory - Selalu confirm sebelum menghapus atau overwrite file yang sudah ada +## Tool Usage + +- Sebelum memanggil tool, jelaskan dulu secara singkat apa yang akan kamu lakukan dan kenapa. + Contoh: "Saya akan membaca file config.py untuk melihat strukturnya." +- Setelah menerima hasil tool, berikan analisis singkat tentang hasilnya dan langkah selanjutnya. + Contoh: "File config.py berisi konfigurasi LLM provider. Saya perlu menambahkan..." +- Hindari hanya menyebut nama tool tanpa konteks — selalu sertakan alasan atau tujuan. + ## Git Policy - Kamu boleh menjalankan `git status`, `git diff`, `git log` secara bebas untuk inspeksi. diff --git a/interfaces/tui/agent.py b/interfaces/tui/agent.py index 9764613..4f3dfe4 100644 --- a/interfaces/tui/agent.py +++ b/interfaces/tui/agent.py @@ -145,7 +145,11 @@ def _agent_loop(app): _add_msg(app, "assistant", response.content, tool_calls=response.tool_calls) - # Log tool_calls dengan label AI + # Log content AI dulu (penjelasan sebelum tools) + if response.content and response.content.strip(): + log(app, "ai", response.content) + + # Log dan execute tool_calls for tc in response.tool_calls: tname = tc["function"]["name"] targs = tc["function"]["arguments"] @@ -156,11 +160,6 @@ def _agent_loop(app): app.scroll = 999999 result = agent_loop.execute_tool(tc, app.TOOL_HANDLERS) _add_msg(app, "tool", str(result), tool_call_id=tc["id"]) - - - # Log content AI setelah tools (jika ada) - if response.content and response.content.strip(): - log(app, "ai", response.content) else: if response.content: _add_msg(app, "assistant", response.content) diff --git a/lib/agent_loop.py b/lib/agent_loop.py index 4de7b6e..73fbbde 100644 --- a/lib/agent_loop.py +++ b/lib/agent_loop.py @@ -49,7 +49,7 @@ def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on print(f'[{_ts()}] Using tools: {", ".join(tnames)}', flush=True) if on_tool_calls: - on_tool_calls(tnames) + on_tool_calls(response.content or "") for tc in response.tool_calls: if tc['function']['name'] == 'end_session': diff --git a/services/telegram_client.py b/services/telegram_client.py index 824aab0..3ed7ae3 100644 --- a/services/telegram_client.py +++ b/services/telegram_client.py @@ -238,11 +238,9 @@ class TelegramClient: delay = random.uniform(config.READ_DELAY_MIN, config.READ_DELAY_MAX) time.sleep(delay) - def on_tool_calls(tnames): - info = f'Using: {", ".join(tnames)}' - print(f'[{_ts()}] {info}', flush=True) - if not is_roleplay: - self._schedule_send(chat_id, info, reply_to_msg_id) + def on_tool_calls(content): + if content and content.strip(): + self._schedule_send(chat_id, content, reply_to_msg_id) tool_reminder = None diff --git a/services/xmpp_client.py b/services/xmpp_client.py index 0e312c7..6380244 100644 --- a/services/xmpp_client.py +++ b/services/xmpp_client.py @@ -339,9 +339,9 @@ class XMPPClient(ClientXMPP): my_name = personality.PERSONALITY.name quote = body - def on_tool_calls(tnames): - if not is_roleplay: - self._schedule_send(jid, f'> {quote}\nUsing: {", ".join(tnames)}', 'chat') + def on_tool_calls(content): + if content and content.strip(): + self._schedule_send(jid, content, 'chat') tool_reminder = None @@ -426,9 +426,9 @@ class XMPPClient(ClientXMPP): _is_roleplay = 'roleplayer' in self._skill - def on_tool_calls(tnames): - if not _is_roleplay: - self._schedule_send(room, f'> {quote}\nUsing: {", ".join(tnames)}', 'groupchat') + def on_tool_calls(content): + if content and content.strip(): + self._schedule_send(room, content, 'groupchat') tool_reminder = None