Skip to content

Refactor (packages/app/src/utils/prompt.ts:57): Function with high complexity (count = 64): extractPromptFromParts - #76

Open
dinoflask wants to merge 4 commits into
CMU-313:mainfrom
dinoflask:extractPromptFromPartsRefactor
Open

Refactor (packages/app/src/utils/prompt.ts:57): Function with high complexity (count = 64): extractPromptFromParts#76
dinoflask wants to merge 4 commits into
CMU-313:mainfrom
dinoflask:extractPromptFromPartsRefactor

Conversation

@dinoflask

@dinoflask dinoflask commented Sep 6, 2026

Copy link
Copy Markdown

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:

  1. "restores files without text"
  2. "interleaves inline files and text"
  3. "interleaves agent mentions and text"
  4. "restores a prompt with only images"
  5. "restores text when there are no inline parts"
image image

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.

Screenshot 2026-09-06 at 1 31 00 PM

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.

Screenshot 2026-09-06 at 1 54 01 PM

All tests pass on GitHub Actions.

Screenshot 2026-09-06 at 2 06 33 PM

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.

@dinoflask dinoflask changed the title extractPromptFromParts() refactor Refactor (packages/app/src/utils/prompt.ts): ExtractPromptFromParts() Sep 6, 2026
@dinoflask dinoflask changed the title Refactor (packages/app/src/utils/prompt.ts): ExtractPromptFromParts() Refactor (packages/app/src/utils/prompt.ts:57): Function with high complexity (count = 64): extractPromptFromParts Sep 6, 2026
@dinoflask
dinoflask force-pushed the extractPromptFromPartsRefactor branch from 54c5a3b to 92ef8a5 Compare September 6, 2026 18:26
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