Skip to content

refactor: extract 6 helpers from convert_messages_to_anthropic#23968

Closed
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:refactor/anthropic-adapter
Closed

refactor: extract 6 helpers from convert_messages_to_anthropic#23968
kshitijk4poor wants to merge 1 commit into
NousResearch:mainfrom
kshitijk4poor:refactor/anthropic-adapter

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Split convert_messages_to_anthropic (complexity 79) into 6 focused helpers:

Helper Purpose Complexity
_convert_assistant_message Assistant msg → content blocks + tool calls ≤10
_convert_tool_message_to_result Tool msg → tool_result + merge logic 12
_convert_user_message User msg validation + conversion ≤10
_strip_orphaned_tool_blocks Remove orphaned tool_use + tool_result 15
_merge_consecutive_roles Role alternation enforcement 13
_manage_thinking_signatures Strip/preserve/downgrade by endpoint 18
_evict_old_screenshots Keep only 3 most recent images ≤10

Results

  • Main function complexity: 79 → ≤10 (cleared C901)
  • 1 file, +222/−226 (net −4)
  • Zero logic changes — pure extraction
  • All helpers are self-contained, single-purpose functions

Test results

  • test_anthropic_adapter.py: 26 passed (same 5 pre-existing failures on main)
  • Broader run_agent + agent: 1,144 passed (same 2 pre-existing failures on main)
  • Zero regressions

Part 5 of the complexity reduction series.

Split convert_messages_to_anthropic (complexity 79) into 6 focused helpers:

- _convert_assistant_message    — assistant msg to content blocks
- _convert_tool_message_to_result — tool msg to tool_result + merge
- _convert_user_message         — user msg validation + conversion
- _strip_orphaned_tool_blocks   — orphan tool_use + tool_result removal
- _merge_consecutive_roles      — role alternation enforcement
- _manage_thinking_signatures   — strip/preserve/downgrade by endpoint
- _evict_old_screenshots        — keep only 3 most recent images

Main function complexity: 79 → 10 (below C901 threshold).
Zero logic changes — pure extraction. Net -4 lines.
@kshitijk4poor

Copy link
Copy Markdown
Collaborator Author

Superseded by #27784 — same commit rebased onto current origin/main (clean cherry-pick, no conflicts since none of the 4 intervening commits to anthropic_adapter.py touched convert_messages_to_anthropic).

Both this PR and the new one are mine, so no contributor authorship to preserve. The new PR body has more honest complexity accounting — the original 79→10 claim was inaccurate; the real number is 79→18 max with 4 still-complex helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P3 Low — cosmetic, nice to have provider/anthropic Anthropic native Messages API type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants