Skip to content

Commit 00b329a

Browse files
committed
Refactor: Returned original claude major code
Details: due to previous commits (which handled async flow), the previous AnsariClaude adjustments are now redundant (at least based on multiple tests before this commit)
1 parent f858100 commit 00b329a

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

src/ansari/agents/ansari_claude.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -755,44 +755,21 @@ def _process_tool_calls(self, tool_calls):
755755
except json.JSONDecodeError:
756756
logger.warning(f"Failed to parse source data to JSON for ref: {doc}")
757757

758-
# Validation check: If an old "tool_result" message (OF THE SAME TOOL CALL ID)
759-
# is already appended to self.message_history (from a previous _process_tool_calls() call),
760-
# then tool result is getting duplicated (i.e., old document_blocks is same as new ones)
761-
# so don't append it
762-
# Corner case: In very rare cases, the old tool_result message will contain a single element in "contents":
763-
# "Please see the references below." (i.e., no document_blocks are added).
764-
# If so, then (and only then): we append new document_blocks to old "tool_result" message
765-
# NOTE: We're doing this as Claude's API requires a message history to have
766-
# a SINGLE "tool_result" message (i.e., dict) directly after a SINGLE "tool_use" message
767-
if (
768-
(c := self.message_history[-1].get("content"))
769-
and isinstance(c, list)
770-
and len(c) > 0
771-
and c[0].get("type", "") == "tool_result"
772-
and c[0].get("tool_use_id", "") == tc["id"] # <- most important condition check
773-
):
774-
if len(c) == 1:
775-
logger.debug(f"appending {len(document_blocks)} to last tool_result message")
776-
self.message_history[-1]["content"].append(document_blocks)
777-
else:
778-
logger.warning("last message in history is already a 'tool_result' message; Skipping... ")
779-
continue
780-
else:
781-
# Add tool result message
782-
logger.debug("Adding a 'tool_result' message to history")
783-
self.message_history.append(
784-
{
785-
"role": "user",
786-
"content": [
787-
{
788-
"type": "tool_result",
789-
"tool_use_id": tc["id"],
790-
"content": "Please see the references below.",
791-
}
792-
]
793-
+ document_blocks,
794-
}
795-
)
758+
# Add tool result message
759+
logger.debug("Adding a 'tool_result' message to history")
760+
self.message_history.append(
761+
{
762+
"role": "user",
763+
"content": [
764+
{
765+
"type": "tool_result",
766+
"tool_use_id": tc["id"],
767+
"content": "Please see the references below.",
768+
}
769+
]
770+
+ document_blocks,
771+
}
772+
)
796773

797774
# Log the tool result message
798775
self._log_message(self.message_history[-1])
@@ -946,8 +923,9 @@ def _finish_response(self, assistant_text, tool_calls):
946923
if content_blocks:
947924
message_content = content_blocks
948925
else:
949-
# If no content blocks, use a single empty text element
950-
message_content = [{"type": "text", "text": ""}]
926+
# If no content blocks, use a fallback non-empty text element
927+
# Claude API requires text content blocks to be non-empty
928+
message_content = [{"type": "text", "text": "I'm processing your request."}]
951929

952930
# Create the assistant message for the message history
953931
# Don't include tool_name in the message sent to Claude API

0 commit comments

Comments
 (0)