Skip to content

Refactor(packages/app/src/components/prompt-input/history.ts):115 - #119

Open
jana-hussien wants to merge 2 commits into
CMU-17313Q:mainfrom
jana-hussien:refactor/history-high-complexity
Open

jana-hussien wants to merge 2 commits into
CMU-17313Q:mainfrom
jana-hussien:refactor/history-high-complexity

Conversation

@jana-hussien

@jana-hussien jana-hussien commented Sep 6, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #23

Type of change

  • Bug fix
  • New feature
  • [ X ] Refactor / code improvement
  • Documentation

What does this PR do?

isPromptEqual() in packages/app/src/components/prompt-input/history.ts was doing too much in one place (i.e. checking lengths, looping over prompt parts by hand, and running a big if/else chain to compare text vs file vs agent vs image parts). Qlty flagged it as high complexity (42) at line 115.

To fix it I split the per-type equality checks out of that if/else chain and into their own small functions, one per part type, collected in an object called PART_COMPARATORS. So instead of isPromptEqual checking "is this a text part? is this a file part? is this an agent part?" every time, it just looks up the right function for partA.type and runs it. The file-selection comparison (checking start/end line and char) was its own chunk of logic buried inside the file-part branch, so that became its own isFileSelectionEqual function too.

Once the per-type checks were pulled out, the two for-loops in isPromptEqual (one looping over prompt parts, one over comments) didn't really need to be loops anymore. They were just checking "does every element match the one at the same index," which is what Array.every does, so I replaced them with that.

I didn't change what counts as "equal" for any part type, just how the checks are organized, so the function should behave the same as before.

How did you verify your code works?

I ran the tests that already existed for this file, then added a few more for cases they didn't cover: deduping file/agent/image parts, a file part with a different selection, mismatched part types, and mismatched comment fields, since the old tests only really exercised the text-part path. Everything passes. I also ran bun lint and tsgo -b and both came back clean, and I re-ran qlty smells on the file before and after the change. The message about high-complexity on isPromptEqual is gone afterward.

Screenshots / recordings

Smell before:
before

Smell after:

after

Bun lint and test:

bu

Note: history.ts is at 100% function coverage here, and the only uncovered lines (174–253) are inside navigatePromptHistory, which I didn't touch as it is not part of the smell I targeted

Checklist

  • [ X ] I have tested my changes locally
  • [ X ] I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

…ion with high complexity (count = 42)

isPromptEqual had a length check, two manual for-loops,
and a separate if-check for every prompt part type. Qlty flagged it
for high complexity (count = 42).

I pulled the per-type comparison logic out into PART_COMPARATORS, an
object with one small function per part type ("text", "file", "agent",
"image"). arePromptPartsEqual just looks up the right function by
partA.type and calls it, instead of walking through an if-chain that
checked types.length. File parts also needed to compare an optional
selection object field by field, so that logic became its own
isFileSelectionEqual helper rather than living inline inside the
"file" branch. With the per-part checks factored out, the two manual
for-loops (one over prompt parts, one over comments) could each be
replaced by a single Array.every call, since all they were doing was
checking every element pairwise and bailing on the first mismatch.

Added tests for deduping file/agent/image parts, mismatched
selections, mismatched part types, and mismatched comments, since the
old tests only covered the text-part case.
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.

P1B: Refactor (packages/app/src/components/prompt-input/history.ts:115): Function with high complexity (count = 42)

1 participant