Skip to content

refactor(web): extract JS results payload into payload module #249

Merged
onuralpszr merged 6 commits into
mainfrom
refactor/web-extract-payload
Jun 12, 2026
Merged

refactor(web): extract JS results payload into payload module #249
onuralpszr merged 6 commits into
mainfrom
refactor/web-extract-payload

Conversation

@onuralpszr

@onuralpszr onuralpszr commented Jun 12, 2026

Copy link
Copy Markdown
Member

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

🧹 This PR refactors the web inference package by moving JS result serialization into its own module and improving reuse of default settings and fetch error handling in the TypeScript API.

📊 Key Changes

  • ♻️ Extracted all JS-facing result payload logic from crates/web/src/lib.rs into a new dedicated file, crates/web/src/payload.rs.
  • 📦 Moved serialization structs and conversion helpers like JsResults::from_results() into the new module, without changing the core result format.
  • 🎨 Kept mask overlay and semantic segmentation payload generation intact, but relocated that logic to the new payload module for cleaner organization.
  • 🧭 Updated imports in lib.rs to use the new payload::JsResults module and removed now-unneeded direct result imports.
  • 🛠️ Added shared TypeScript constants for default thresholds:
    • confidence
    • IoU
    • keypoint visibility threshold
  • 🌐 Added a reusable fetchOk() helper in web/src/index.ts to standardize network requests and error messages for models and images.
  • ✏️ Cleaned up some documentation comments to better reflect that browser results are aligned with the native path.

🎯 Purpose & Impact

  • ✅ Improves maintainability by separating payload-building code from the main WebAssembly binding logic, making the Rust web crate easier to read and extend.
  • 🚀 Reduces duplication in the TypeScript frontend by centralizing default threshold values and fetch error handling.
  • 🐞 Makes network failures easier to understand with clearer, more consistent error messages when loading models or images.
  • 🔒 Low risk for users: this looks mainly like a refactor and cleanup, with behavior preserved rather than redesigned.
  • 🧑‍💻 Helps future development by making it simpler to evolve browser-side inference results and web APIs without cluttering the main library file.

Move the Js* serializable structs, JsResults::from_results, and the
build_mask_overlay/put/task_of helpers out of lib.rs into a dedicated
crates/web/src/payload.rs. This is the Results -> Serialize boundary
(~230 lines); separating it from the session/predict flow shrinks lib.rs
and groups the JS mapping in one place. JsResults and from_results are
pub(crate); the rest stay private to the module.

No behavior change.
Signed-off-by: Onuralp SEZER <onuralp@ultralytics.com>
Signed-off-by: Onuralp SEZER <onuralp@ultralytics.com>
@UltralyticsAssistant UltralyticsAssistant added enhancement New feature or request priority: low Low urgency; can wait behind higher-priority work. labels Jun 12, 2026
@UltralyticsAssistant

Copy link
Copy Markdown
Member

👋 Hello @onuralpszr, thank you for submitting a ultralytics/inference 🚀 PR! This is an automated message to help with review readiness while an engineer takes a look. Please review the checklist below for a smooth integration 😊

  • Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions.
  • Synchronize with Source: Confirm your PR is synchronized with the ultralytics/inference main branch. If it's behind, update it by clicking the 'Update branch' button or by running git pull and git merge main locally.
  • Ensure CI Checks Pass: Verify all Ultralytics Continuous Integration (CI) checks are passing. If any checks fail, please address the issues.
  • Update Documentation: Update the relevant documentation for any new or modified features.
  • Add Tests: If applicable, include or update tests to cover your changes, and confirm that all tests are passing.
  • Sign the CLA: Please ensure you have signed our Contributor License Agreement if this is your first Ultralytics PR by writing "I have read the CLA Document and I sign the CLA" in a new message.
  • Minimize Changes: Limit your changes to the minimum necessary for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee

For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀

@UltralyticsAssistant UltralyticsAssistant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔍 PR Review

Made with ❤️ by Ultralytics Actions

Clean refactor. The payload extraction appears behavior-preserving: imports were updated consistently, JsResults visibility is scoped appropriately with pub(crate), and the moved serialization/mask helper logic is unchanged in substance. I did not find any genuine bugs or regressions in the diff.

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…tation

Signed-off-by: Onuralp SEZER <onuralp@ultralytics.com>
…andling

Signed-off-by: Onuralp SEZER <onuralp@ultralytics.com>
@onuralpszr onuralpszr merged commit 2a47468 into main Jun 12, 2026
15 checks passed
@onuralpszr onuralpszr deleted the refactor/web-extract-payload branch June 12, 2026 23:32
@UltralyticsAssistant

Copy link
Copy Markdown
Member

🎉 Merged — thank you, @onuralpszr!

This is a thoughtful cleanup that makes the web inference stack easier to maintain and extend, from separating JS payload serialization into a dedicated module to centralizing TypeScript defaults and fetch error handling. As Leonardo da Vinci said, “Simplicity is the ultimate sophistication.” That fits this PR well: cleaner structure, less duplication, and clearer browser-side behavior without changing the user-facing results.

Really appreciate the care put into keeping behavior intact while improving the foundation for future work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request priority: low Low urgency; can wait behind higher-priority work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants