Refactor (packages/app/src/utils/prompt.ts:57): Function with high complexity (count = 64): extractPromptFromParts - #76
Open
dinoflask wants to merge 4 commits into
Conversation
…plate w/ bun and genHtml
…FromParts() into two main helpers
dinoflask
force-pushed
the
extractPromptFromPartsRefactor
branch
from
September 6, 2026 18:26
54c5a3b to
92ef8a5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Issue
Issue #52
packages/app/src/utils/prompt.ts
The main purpose of prompt.ts is to provide extractPromptFromParts(), which converts a user message that has been split into categorical parts and reconstructs the original message as a Prompt type.
Refactored: extractPromptFromParts
Added helpers: collectInlineParts, collectFilePart, collectAgentPart,
findInlineMatch, reconstructPrompt
Qlty Issue Fixed:
Function with high complexity (count = 64): extractPromptFromParts
2. Refactoring
The original file had only one comment (for the main function). This function handled many tasks, such as converting parts into an intermediary Inline type (JSON blocks representing either agents, files, images, or text), looping over the text to find Inline references, and the reconstruction of the prompt converting Inline types back into the original input as a Prompt type.
The production changes made were purely structural—the changes to the function were extracting blocks of code into relevant helper functions with comments explaining their functionality.
I had considered the alternative of implementing the Strategy Pattern, as I had noticed the functions pushAgent, pushFile, and pushText were doing similar things (pushing differently-shaped JSONs). I implemented it partway until I realized these were arrow functions which took in context from the current function anyway. It made more sense to deal with those by extracting them into helpers (and it was less complicated).
In my opinion, my changes improve maintainability by providing an explanation of how the entire process works through comments on each helper, while making the main function much shorter and easier to understand through relevant function names.
Other quality-of-life changes I made were:
-Moving the public function to be at the top of the file for visibility.
-Adding high-level comments explaining why the helpers exist.
3. Validation
Before, there was only one test that "restores multiple uploaded attachments". However, both fixtures only covered image-attachment handling, so my new tests cover inline file references and agent mentions in these combinations:
Running bun lint packages/app/src/utils/prompt.ts
and
cd packages/app
bun test:unit
provide no issues. Again—the production changes were purely structural, so type-checking is the main indicator that the change is correct.
100% of functions in prompt.ts are covered (includes all the helpers I extracted), with 87.7% overall file coverage —missed paths have to do with specific URL parsing and file name variations not included in the tests.
All tests pass on GitHub Actions.
The file-level complexity is still there regarding the entire file logic, but function-level complexity is gone. To remove file-level compexity, I would need to simplify actual branching, which I believe to be out-of-scope for this assignment.