Skip to content

refactor: reduce cognitive complexity of get_ai_analysis#2586

Open
mohamedsolaiman wants to merge 2 commits intosoxoj:mainfrom
mohamedsolaiman:refactor/get-ai-analysis
Open

refactor: reduce cognitive complexity of get_ai_analysis#2586
mohamedsolaiman wants to merge 2 commits intosoxoj:mainfrom
mohamedsolaiman:refactor/get-ai-analysis

Conversation

@mohamedsolaiman
Copy link
Copy Markdown

Was looking through the AI analysis code and realized get_ai_analysis was doing too much in one function — HTTP error handling, SSE stream parsing, spinner management, all mixed together. The function had a cognitive complexity problem and also a bug where full_response was never actually populated (the return was always empty string).

Split into three helpers:

  • _check_api_response() — validates the HTTP status and raises descriptive errors
  • _stream_response() — parses the SSE stream, prints tokens, collects the full response text, and manages the spinner
  • _build_request_payload() — builds the URL, headers, and payload

The main function is now just ~10 lines: build payload, make request, check response, stream response. Much easier to follow.

Also fixes the bug where full_response was never appended to, so the function always returned an empty string.

Closes #2581

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant