Skip to content

[codex] fix ai-remover edit routing - #5

Merged
aditzel merged 2 commits into
mainfrom
codex/fix-ai-remover-edit-routing
Apr 4, 2026
Merged

[codex] fix ai-remover edit routing#5
aditzel merged 2 commits into
mainfrom
codex/fix-ai-remover-edit-routing

Conversation

@aditzel

@aditzel aditzel commented Apr 4, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #4.

This change makes edit work correctly for Wavespeed ai-remover models such as wavespeed-ai/image-background-remover and bria/remove-background.

Before this change, users hitting remover models through the CLI or MCP edit tool could get 400 responses because the request was routed like a normal image-edit model and sent the wrong payload shape.

Root Cause

The runtime treated all image edit models the same:

  • submit routing appended /edit for non-canonical aliases
  • edit payloads always sent images as an array plus generic edit fields

That breaks remover models because those endpoints use the base model path and expect a singular image field instead of images.

A second issue was that model resolution discarded the Wavespeed catalog type, so the command layer could not reliably distinguish remover models from normal edit models when resolving aliases.

What Changed

The fix preserves and infers API model type during model resolution, then uses that metadata to apply remover-specific edit behavior.

In practice this PR:

  • preserves cached API model type metadata on resolved models
  • infers ai-remover for known remover model IDs when cache metadata is unavailable
  • routes edit requests for remover models to the base model path instead of appending /edit
  • builds remover payloads with a single image field
  • keeps prompt forwarding only for remover models that plausibly support prompt-driven removal (currently image-eraser)
  • rejects multiple input images for remover models with a clear error instead of sending an invalid request upstream

Additional Test Fix

While validating the change, the CLI integration tests were failing in this environment because they spawned bun by name and assumed it was on PATH.

This PR updates the CLI test harness to spawn the current Bun executable via process.execPath, which makes the existing tests pass consistently without changing product behavior.

Validation

I ran:

  • /Users/allan/.bun/bin/bun test
  • /Users/allan/.bun/bin/bun run lint
  • /Users/allan/.bun/bin/bun run build

Results:

  • bun test: 86 passing, 4 skipped, 0 failing
  • bun run lint: passed
  • bun run build: passed

Summary by CodeRabbit

  • New Features

    • Improved image-editing: smarter routing and tailored request behavior for AI remover models (single-image handling, optional prompt support).
  • Tests

    • Expanded test coverage for AI remover routing, edit payloads, and error cases (multiple-image validation).
  • Chores

    • Internal model-type detection and submit-target resolution improved for more reliable edit submissions.

@coderabbitai

coderabbitai Bot commented Apr 4, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a71e201-6e21-4d78-af5b-106be3391e07

📥 Commits

Reviewing files that changed from the base of the PR and between ea41fba and 1ea22f4.

📒 Files selected for processing (4)
  • src/config/models.ts
  • src/utils/model-routing.ts
  • tests/api/client.test.ts
  • tests/config/models.test.ts
✅ Files skipped from review due to trivial changes (1)
  • tests/api/client.test.ts

Walkthrough

Detects AI-remover models, routes their edit requests to the base model path (no /edit suffix), and builds edit payloads using a singular image field (with single-image validation); adds model-type resolution and tests covering routing and payload behavior.

Changes

Cohort / File(s) Summary
Model config & types
src/config/types.ts, src/config/models.ts
Added optional apiModelType to config/resolved types; extended API model cache entries with optional type; added utilities to resolve/normalize model type and canonical suffixes.
Model routing utilities
src/utils/model-routing.ts
New module: inferApiModelType, isAiRemoverModel, and buildEditPayload to detect ai-remover models and produce appropriate edit payloads (single image vs images array).
Client routing change
src/api/client.ts
Updated buildSubmitTarget to treat ai-remover models (and canonical submitMode) as base submissions for edit (no /edit suffix).
Edit operation payload
src/core/operations.ts
Refactored editImage to use buildEditPayload(params) and adjusted debug logging to reflect ai-remover payload shape.
Tests — client & routing
tests/api/client.test.ts, tests/config/models.test.ts
Added fixtures and assertions verifying ai-remover type resolution, base-path submit targets, canonical normalization, and cache-preserved typing.
Tests — operations
tests/core/operations.test.ts
Added tests ensuring ai-remover edit posts singular image, includes optional prompt for prompt-capable removers, and rejects multiple images.
Tests — CLI helper
tests/commands/cli.test.ts
Updated test helper runCLI to spawn Bun via process.execPath instead of hard bun binary.

Sequence Diagram

sequenceDiagram
    participant Client as Client
    participant Resolver as ModelResolver
    participant Infer as TypeInference
    participant Router as SubmitRouter
    participant Builder as PayloadBuilder
    participant API as API

    Client->>Resolver: resolveModel(modelRef)
    Resolver->>Infer: inferApiModelType(modelName)
    Infer-->>Resolver: "ai-remover" / undefined
    Resolver-->>Client: ResolvedModel {apiModelType, modelName,...}

    Client->>Router: buildSubmitTarget(model,"edit")
    Router->>Infer: isAiRemoverModel(model)
    Infer-->>Router: true / false
    alt ai-remover
        Router-->>Client: /api/v3/{modelName} (base)
    else standard
        Router-->>Client: /api/v3/{modelName}/edit
    end

    Client->>Builder: buildEditPayload(params)
    Builder->>Infer: isAiRemoverModel(model)
    Infer-->>Builder: true / false
    alt ai-remover
        Builder-->>Client: { image, enable_base64_output, enable_sync_mode, prompt? }
    else standard
        Builder-->>Client: { images, prompt, size, enable_base64_output, enable_sync_mode }
    end

    Client->>API: POST request
    API-->>Client: Task / result
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.53% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '[codex] fix ai-remover edit routing' directly and clearly summarizes the main change: fixing AI-remover model edit routing, which is the core objective of the PR.
Linked Issues check ✅ Passed All requirements from issue #4 are fully addressed: AI-remover detection and routing with base model path (no /edit suffix), singular 'image' field in payload, single-image validation, prompt forwarding for capable models, and cached metadata preservation.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing AI-remover edit routing (model detection, routing, payload construction, type metadata resolution) and fixing the CLI test harness to use process.execPath—all aligned with PR objectives.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-ai-remover-edit-routing

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea41fbab85

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/utils/model-routing.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/config/models.ts`:
- Around line 59-88: findCachedModelType and resolveApiModelType must strip any
CANONICAL_MODEL_SUFFIXES from refs/ids before looking up the cache or inferring
submit mode so canonical edit suffixes (e.g., provider/model/edit) map to the
cached provider/model and correctly classify ai-remover models; update
findCachedModelType to normalize each ref by removing CANONICAL_MODEL_SUFFIXES
before calling apiCache?.models.find, and update resolveApiModelType (and any
code paths that call inferSubmitMode for configured/direct ids) to strip those
suffixes from id and modelName prior to calling findCachedModelType or
inferApiModelType so remover models get the authoritative cached type and
correct submit mode.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b550f948-d18e-4584-b271-22b9c7d13ccf

📥 Commits

Reviewing files that changed from the base of the PR and between 0dd4c01 and ea41fba.

📒 Files selected for processing (9)
  • src/api/client.ts
  • src/config/models.ts
  • src/config/types.ts
  • src/core/operations.ts
  • src/utils/model-routing.ts
  • tests/api/client.test.ts
  • tests/commands/cli.test.ts
  • tests/config/models.test.ts
  • tests/core/operations.test.ts

Comment thread src/config/models.ts
@aditzel
aditzel merged commit e78276a into main Apr 4, 2026
5 checks passed
@aditzel
aditzel deleted the codex/fix-ai-remover-edit-routing branch April 4, 2026 20:38
@github-actions

github-actions Bot commented Apr 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit command fails with ai-remover models (e.g. image-background-remover)

1 participant