From 25ec6fd9963c3a4c9a1dd23cb0bea0e0734e9283 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Tue, 12 May 2026 09:32:16 +0700 Subject: [PATCH] Pemisahan script ansicolor dan separator --- hendrik.py | 44 +++++++++++++++++++++++++++----------------- scripts/ansicolor.py | 3 +++ scripts/separator.py | 5 +++++ 3 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 scripts/ansicolor.py create mode 100644 scripts/separator.py diff --git a/hendrik.py b/hendrik.py index 068dfc3..9342f78 100644 --- a/hendrik.py +++ b/hendrik.py @@ -3,10 +3,8 @@ import config from llm_client import LLMClient from tools import coder from scripts import gadget - -TEXT_COLOR_YELLOW = '\033[93m' -TEXT_COLOR_GREEN = '\033[92m' -TEXT_COLOR_RESET = '\033[0m' +from scripts.ansicolor import TEXT_COLOR_YELLOW, TEXT_COLOR_GREEN, TEXT_COLOR_RESET +from scripts.separator import separator tools_definition = [ gadget.tools_mapping( coder.schema_read_file, coder.read_file ), @@ -21,18 +19,26 @@ TOOLS = gadget.tool_schemas(tools_definition) TOOL_HANDLERS = gadget.tool_handlers(tools_definition) -def interactive_input(): +def interactive_input(header_mode='full'): fd = sys.stdin.fileno() old = termios.tcgetattr(fd) print() - print("\u2500" * 50) - print("Hendrik AI Agent - Interactive Mode") - print("\u2500" * 50) - print(f"Workspace: {os.getcwd()}") - print("\u2500" * 50) - print("[Ctrl+W] Change workspace | :workspace | [Ctrl+D] Submit") - print("\u2500" * 50) + if header_mode == 'full': + print(separator()) + print("Hendrik AI Agent - Interactive Mode") + print(separator()) + print(f"Workspace: {os.getcwd()}") + print(separator()) + print("[Ctrl+W] Change workspace | :workspace | [Ctrl+D] Submit") + print(separator()) + elif header_mode == 'workspace': + print(separator()) + print(f"Workspace: {os.getcwd()}") + print(separator()) + else: + print("[Ctrl+W] Change workspace | :workspace | [Ctrl+D] Submit") + print(separator()) buffer = bytearray() try: @@ -56,7 +62,7 @@ def interactive_input(): else: os.chdir(resolved) print(f"\u2192 Workspace changed to {os.getcwd()}") - return interactive_input() + return interactive_input(header_mode='workspace') elif ch in (b'\r', b'\n'): # Enter buffer.extend(b'\n') sys.stdout.buffer.write(b'\r\n') @@ -80,10 +86,10 @@ def interactive_input(): resolved = os.path.abspath(ws) if not os.path.isdir(resolved): print(f"Error: '{resolved}' is not a valid directory") - return interactive_input() + return interactive_input(header_mode=header_mode) os.chdir(resolved) print(f"\u2192 Workspace changed to {os.getcwd()}") - return interactive_input() + return interactive_input(header_mode='workspace') return full_query @@ -167,14 +173,18 @@ def main(): print("No query provided.") return messages = [{"role": "system", "content": gadget.build_system_prompt(tools_definition)}] - print(f"{TEXT_COLOR_YELLOW}Thinking...{TEXT_COLOR_RESET}") + print(f"\n{TEXT_COLOR_YELLOW}Thinking...{TEXT_COLOR_RESET}") final_answer, messages = agent_loop(user_query, messages, llm_client) print(f"\n{TEXT_COLOR_GREEN}Final Answer:{TEXT_COLOR_RESET}") print(final_answer) return + first_interaction = True while True: - user_query = interactive_input() + user_query = interactive_input( + header_mode='full' if first_interaction else 'compact' + ) + first_interaction = False if not user_query: break if user_query.lower() in ('/exit', '/quit'): diff --git a/scripts/ansicolor.py b/scripts/ansicolor.py new file mode 100644 index 0000000..96823a0 --- /dev/null +++ b/scripts/ansicolor.py @@ -0,0 +1,3 @@ +TEXT_COLOR_YELLOW = '\033[93m' +TEXT_COLOR_GREEN = '\033[92m' +TEXT_COLOR_RESET = '\033[0m' diff --git a/scripts/separator.py b/scripts/separator.py new file mode 100644 index 0000000..fb14bcb --- /dev/null +++ b/scripts/separator.py @@ -0,0 +1,5 @@ +import shutil + + +def separator(): + return "\u2500" * shutil.get_terminal_size().columns