Skip to content

refactor(chat): extract pure utility functions - #1551

Merged
steebchen merged 1 commit into
mainfrom
steebchen/chat-phase1-utilities
Jan 30, 2026
Merged

steebchen merged 1 commit into
mainfrom
steebchen/chat-phase1-utilities

Conversation

@steebchen

@steebchen steebchen commented Jan 30, 2026

Copy link
Copy Markdown
Member

Summary

  • Extract mightBeCompleteJson function to tools/might-be-complete-json.ts - JSON bracket balance heuristic for SSE parsing optimization
  • Extract messagesContainImages function to tools/messages-contain-images.ts - checks messages for vision content
  • Extract countInputImages function to tools/count-input-images.ts - counts images for Gemini cost calculation

This is Phase 1 of the chat.ts modularization effort. These are pure utility functions with zero external dependencies, making them easy to test and maintain.

Net change: -95 lines from chat.ts (103 removed, 8 added for imports)

Test plan

  • pnpm format - passed
  • pnpm build - passed
  • pnpm test:unit - all 376 tests passed

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved internal image detection and counting and added a lightweight JSON-completeness check to streamline message handling and parsing.
    • No user-facing behavior changes expected; these updates reduce internal complexity and improve reliability for messages that include images or JSON-like content.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Extracts image- and JSON-related helpers from apps/gateway/src/chat/chat.ts into three new utility modules and updates chat.ts to import and use countInputImages, messagesContainImages, and mightBeCompleteJson, replacing the previous in-file image-counting and JSON heuristics.

Changes

Cohort / File(s) Summary
Main chat update
apps/gateway/src/chat/chat.ts
Removed in-file IMAGE_URL_PATTERN and manual image-counting/maybe-JSON helpers; added imports for countInputImages, messagesContainImages, and mightBeCompleteJson; delegates image counting and image-detection decisions to new utilities.
New utilities
apps/gateway/src/chat/tools/count-input-images.ts, apps/gateway/src/chat/tools/messages-contain-images.ts, apps/gateway/src/chat/tools/might-be-complete-json.ts
Adds countInputImages(messages) (counts image_url parts and URLs in text using a shared RegExp), messagesContainImages(messages) (detects any image content parts), and mightBeCompleteJson(str) (lightweight brace/bracket balance heuristic).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

auto-merge

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(chat): extract pure utility functions' accurately describes the main change: extracting three utility functions from chat.ts into separate modules.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch steebchen/chat-phase1-utilities

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Extract three pure utility functions from chat.ts into separate modules:
- mightBeCompleteJson: JSON bracket balance heuristic
- messagesContainImages: vision content detection
- countInputImages: image counting for cost calculation

This is phase 1 of the chat.ts modularization effort.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@steebchen
steebchen force-pushed the steebchen/chat-phase1-utilities branch from dce7937 to d9ad844 Compare January 30, 2026 01:24
@steebchen
steebchen merged commit f38cd3d into main Jan 30, 2026
13 of 14 checks passed
@steebchen
steebchen deleted the steebchen/chat-phase1-utilities branch January 30, 2026 12:08
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