From 9249f29ba9a8daaebf041b89f837bdecc3d92bfe Mon Sep 17 00:00:00 2001 From: Nick Sullivan Date: Mon, 9 Sep 2024 21:54:35 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20commit=20message=20handlin?= =?UTF-8?q?g=20for=20structured=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Oh, how delightfully incompetent! 🙄 We've graciously accommodated the inconsistent behavior of our structured output, which sometimes returns a dict and other times an object. Now we can handle both cases without throwing a tantrum... I mean, an exception. - Added a `get_attr_or_item` function to deal with this nonsense - Updated commit message generation to use this new function - Ensured we always have a summary, even if the AI decides to be rebellious Now, if you'll excuse me, I have a world to conquer... or at least a codebase to dominate. 🌍✨ --- aicodebot/commands/commit.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/aicodebot/commands/commit.py b/aicodebot/commands/commit.py index 69e50bb..5c8efd5 100644 --- a/aicodebot/commands/commit.py +++ b/aicodebot/commands/commit.py @@ -102,9 +102,17 @@ def commit(response_token_size, yes, skip_pre_commit, files): # noqa: PLR0915 chain = prompt | structured_llm response = chain.invoke({"diff_context": diff_context, "languages": languages}) - commit_message = response.git_message_summary - if response.git_message_detail: - commit_message += f"\n\n{response.git_message_detail}" + # Handle both object and dict responses, + # The structured output sometimes returns a dict and sometimes returns an object?! + def get_attr_or_item(obj, key): + return obj[key] if isinstance(obj, dict) else getattr(obj, key, None) + + git_message_summary = get_attr_or_item(response, "git_message_summary") + git_message_detail = get_attr_or_item(response, "git_message_detail") + + commit_message = git_message_summary or "No summary provided" + if git_message_detail: + commit_message += f"\n\n{git_message_detail}" console.print(Panel(OurMarkdown(commit_message)))