From 32dbdc68c8f7d6231f46326f55f09570e2598a34 Mon Sep 17 00:00:00 2001 From: Kimi Agent Date: Fri, 20 Mar 2026 16:31:34 -0400 Subject: [PATCH] refactor: break up should_use_tools into helpers (#624) Co-authored-by: Kimi Agent Co-committed-by: Kimi Agent --- src/timmy/conversation.py | 75 ++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/src/timmy/conversation.py b/src/timmy/conversation.py index bc16a99..3c8e6cc 100644 --- a/src/timmy/conversation.py +++ b/src/timmy/conversation.py @@ -174,15 +174,8 @@ class ConversationManager: return None - def should_use_tools(self, message: str, context: ConversationContext) -> bool: - """Determine if this message likely requires tools. - - Returns True if tools are likely needed, False for simple chat. - """ - message_lower = message.lower().strip() - - # Tool keywords that suggest tool usage is needed - tool_keywords = [ + _TOOL_KEYWORDS = frozenset( + { "search", "look up", "find", @@ -203,10 +196,11 @@ class ConversationManager: "shell", "command", "install", - ] + } + ) - # Chat-only keywords that definitely don't need tools - chat_only = [ + _CHAT_ONLY_KEYWORDS = frozenset( + { "hello", "hi ", "hey", @@ -221,30 +215,47 @@ class ConversationManager: "goodbye", "tell me about yourself", "what can you do", - ] + } + ) - # Check for chat-only patterns first - for pattern in chat_only: - if pattern in message_lower: - return False + _SIMPLE_QUESTION_PREFIXES = ("what is", "who is", "how does", "why is", "when did", "where is") + _TIME_WORDS = ("today", "now", "current", "latest", "this week", "this month") - # Check for tool keywords - for keyword in tool_keywords: - if keyword in message_lower: - return True + def _is_chat_only(self, message_lower: str) -> bool: + """Return True if the message matches a chat-only pattern.""" + return any(kw in message_lower for kw in self._CHAT_ONLY_KEYWORDS) - # Simple questions (starting with what, who, how, why, when, where) - # usually don't need tools unless about current/real-time info - simple_question_words = ["what is", "who is", "how does", "why is", "when did", "where is"] - for word in simple_question_words: - if message_lower.startswith(word): - # Check if it's asking about current/real-time info - time_words = ["today", "now", "current", "latest", "this week", "this month"] - if any(t in message_lower for t in time_words): - return True - return False + def _has_tool_keyword(self, message_lower: str) -> bool: + """Return True if the message contains a tool-related keyword.""" + return any(kw in message_lower for kw in self._TOOL_KEYWORDS) + + def _is_simple_question(self, message_lower: str) -> bool | None: + """Check if message is a simple question. + + Returns True if it needs tools (real-time info), False if it + doesn't, or None if the message isn't a simple question. + """ + for prefix in self._SIMPLE_QUESTION_PREFIXES: + if message_lower.startswith(prefix): + return any(t in message_lower for t in self._TIME_WORDS) + return None + + def should_use_tools(self, message: str, context: ConversationContext) -> bool: + """Determine if this message likely requires tools. + + Returns True if tools are likely needed, False for simple chat. + """ + message_lower = message.lower().strip() + + if self._is_chat_only(message_lower): + return False + if self._has_tool_keyword(message_lower): + return True + + simple = self._is_simple_question(message_lower) + if simple is not None: + return simple - # Default: don't use tools for unclear cases return False