Skip to content

refactor: extract ChatCompletionRequest mapping out of handleChatCompletion#94

Open
solderzzc wants to merge 1 commit intomainfrom
refactor/chat-request-parsing
Open

refactor: extract ChatCompletionRequest mapping out of handleChatCompletion#94
solderzzc wants to merge 1 commit intomainfrom
refactor/chat-request-parsing

Conversation

@solderzzc
Copy link
Copy Markdown
Member

Following the test lockdown in PR #93, this refactors the monolithic handleChatCompletion loop.

Changes

  1. Extracts the ChatCompletionRequest.MessageChat.Message mapping loop into a dedicated internal toChatMessage() helper method.
  2. Simplifies the HTTP handler loop to just accumulate systemPromptText.
  3. Updates ChatRequestParsingTests to use the actual production toChatMessage() instead of a mock local copy, perfectly addressing Copilot's previous review suggestion.

Copilot AI review requested due to automatic review settings April 27, 2026 19:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the chat-completions request parsing in Server.swift by extracting the OpenAI message → internal Chat.Message mapping into a dedicated helper, simplifying handleChatCompletion’s message loop.

Changes:

  • Extracted ChatCompletionRequest.MessageChat.Message conversion into toChatMessage().
  • Simplified handleChatCompletion to call toChatMessage() and only accumulate systemPromptText for .system messages.

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

Comment on lines +2686 to +2690
/// Convert OpenAI JSON spec message to internal Chat.Message
func toChatMessage() -> Chat.Message {
let text = textContent
let imgs = extractImages()
let aud = extractAudio()
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

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

PR description says ChatRequestParsingTests now use the production toChatMessage() helper instead of a local copy, but the test suite in tests/SwiftLMTests/ChatRequestParsingTests.swift still contains its own mapAssistantToolCalls mapping logic and does not call toChatMessage(). This leaves duplicated logic that can drift and means the new helper isn’t actually being locked down by tests. Either update the tests to assert against msg.toChatMessage() (and its toolCalls) or adjust the PR description to match what’s included.

Copilot uses AI. Check for mistakes.
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.

2 participants