Skip to content

refactor(aichat): move request controller helpers to chat helpers#70249

Merged
drizco merged 3 commits into
stagingfrom
ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers
Jan 15, 2026
Merged

refactor(aichat): move request controller helpers to chat helpers#70249
drizco merged 3 commits into
stagingfrom
ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers

Conversation

@drizco

@drizco drizco commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

This pull request refactors and improves the handling of AI chat requests, focusing on centralizing attribute building and error handling logic, as well as adding comprehensive tests for helper methods. The main changes move request attribute construction and error reporting into helper methods, streamline controller logic, and add thorough test coverage for the new and existing helper methods.

Refactoring and Logic Centralization:

  • Moved the construction of AichatRequest attributes out of the controller into a new helper method AichatAiHelper.build_request_attributes, and replaced inline logic in AichatRequestsController with a call to a new create_request method.
  • Centralized error handling for AI chat request jobs by adding AichatAiHelper.handle_error.

Helper Method Enhancements:

  • Added AichatAiHelper.successful_stored_chat_messages to filter out non-OK or blank chat messages, ensuring only valid messages are stored.
  • Added AichatAiHelper.project_id_from_context to reliably extract the project ID from context, supporting both direct and encrypted channel ID sources.

Testing Improvements:

  • Added a comprehensive test suite in aichat_ai_helper_test.rb covering model selection, message formatting, JSON schema conversion, context/config generation, attribute building, message filtering, and project ID extraction for AichatAiHelper.

Links

  • Jira:

Testing story

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Creation Checklist:

  • Tests provide adequate coverage
  • Privacy impacts have been documented
  • Security impacts have been documented
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Follow-up work items (including potential tech debt) are tracked and linked

@drizco drizco requested a review from Copilot January 8, 2026 21:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors AI chat request handling by extracting controller helper methods into the AichatAiHelper module for better separation of concerns and reusability. The refactoring consolidates error handling, request building, and message filtering logic into shared helper methods that can be used across both the controller and background jobs.

Key Changes:

  • Extracted request building and error handling logic from controller into AichatAiHelper
  • Consolidated duplicate error handling code between controller and job
  • Added comprehensive test coverage for the newly extracted helper methods

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
dashboard/app/helpers/aichat_ai_helper.rb Added helper methods for error handling, request attribute building, message filtering, and project ID extraction
dashboard/app/controllers/aichat_requests_controller.rb Refactored to use new helper methods and added parameter whitelisting with create_request method
dashboard/app/jobs/aichat_request_chat_completion_job.rb Simplified error handling by delegating to new AichatAiHelper.handle_error method
dashboard/test/helpers/aichat_ai_helper_test.rb Added comprehensive test coverage for all new helper methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread dashboard/app/controllers/aichat_requests_controller.rb
Comment thread dashboard/app/helpers/aichat_ai_helper.rb Outdated
@drizco drizco force-pushed the ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers branch from 5ec65ec to 7bf7065 Compare January 8, 2026 21:25
@drizco drizco marked this pull request as ready for review January 8, 2026 21:28
@drizco drizco force-pushed the ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers branch from 7bf7065 to 70baea8 Compare January 8, 2026 21:58
# Filter out non-OK messages (e.g. errors).
Array(stored_messages).select do |message|
message[:status] == SharedConstants::AI_INTERACTION_STATUS[:OK] &&
message[:chatMessageText].present?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drizco just noting that the existing logic does not check message[:chatMessageText].present?. Could you talk about scenarios that we'd have an OK status but no chatMessageText and why you believe this wasn't filtered before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think they are being filtered on the front end in submitChatContents.ts, this is just a guard since we're filtering already

end
end

def self.project_id_from_channel_id(context)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could this be project_id_from_context as we don't pass in a channel id directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just noticing a pr went in yesterday that moves us toward using uuid channel ids rather than encrypted channel ids. will adjust!

#69293

)
end

def self.build_request_attributes(user_id, data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is there a more descriptive name we could use rather than data? It looks like we pass in safe_params so perhaps just params?

new_message: params[:newMessage],
level_id: context[:currentLevelId],
script_id: context[:scriptId],
project_id: get_project_id(context)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like get_project_id is no longer used. Any reason to keep around the definition?

@default_aichat_context = {
currentLevelId: @level.id,
scriptId: @script.id,
channelId: 'test',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, was this just never needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof, I don't remember why I removed this, but I'm adding it back now to handle the uuid vs encrypted ids situation

@edcodedotorg edcodedotorg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drizco thanks for refactoring to better share code with your streaming additions! Just a few nits and questions to make sure we're not changing anything unintentionally, but looks good!

@drizco drizco force-pushed the ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers branch from 70baea8 to 4612ee1 Compare January 14, 2026 16:04
@drizco drizco merged commit ace559d into staging Jan 15, 2026
6 checks passed
@drizco drizco deleted the ryan/aichat/refactor/move-request-controller-helpers-to-chat-helpers branch January 15, 2026 15:23
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.

3 participants