Skip to content

feat(models): expose auto-routing choices in models API#4396

Merged
johnnyeric merged 1 commit into
mainfrom
johnnyeric/expose-auto-efficient-models-organization
Jul 6, 2026
Merged

feat(models): expose auto-routing choices in models API#4396
johnnyeric merged 1 commit into
mainfrom
johnnyeric/expose-auto-efficient-models-organization

Conversation

@johnnyeric

@johnnyeric johnnyeric commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Expose the concrete models behind the virtual Auto Efficient (kilo-auto/efficient) and Auto Free (kilo-auto/free) routing choices in the /api/openrouter/models response, so the model selector can show which models auto-routing picks among.

  • Adds addAutoRoutingModels(), which annotates the auto models with autoRouting.models — the concrete candidate ids from the benchmark routing table (efficient) and free-model resolution (free), intersected with the models actually available to the caller.
  • Applied on both paths: org (getAvailableModelsForOrganization) and public/BYOK (route handler).
  • The benchmark routing table is cached in-process (5-min TTL, serves last-known-good on fetch failure via createCachedFetch), so it is not an uncached admin-worker round-trip per request and a transient worker outage does not blank the choices.
  • Adds the first direct test coverage for getAvailableModelsForOrganization (previously only ever mocked).

This is the server-side counterpart that lets us drop the client-side merge fallback in kilo-gateway.

Verification

Ran local web (pnpm dev) against a docker Redis seeded via /api/cron/sync-providers (935-model catalog), with a mock benchmark routing table served on the local worker port, same approach as #4274.

curl -s http://localhost:3000/api/openrouter/models
  | jq '.data[] | select(.id=="kilo-auto/efficient" or .id=="kilo-auto/free") | {id, autoRouting}'
{
  "id": "kilo-auto/efficient",
  "autoRouting": { "models": ["anthropic/claude-opus-4.8", "openai/gpt-5.5"] }
}
{
  "id": "kilo-auto/free",
  "autoRouting": { "models": ["stepfun/step-3.7-flash:free"] }
}

Visual Changes

N/A - server-side API change; the model-selector rendering already lives on main in the extension.

Reviewer Notes

@johnnyeric johnnyeric force-pushed the johnnyeric/expose-auto-efficient-models-organization branch 3 times, most recently from 3fada81 to aca83ed Compare July 5, 2026 22:48
@johnnyeric johnnyeric marked this pull request as ready for review July 5, 2026 23:19
@kilo-code-bot

kilo-code-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

Reviewed the incremental changes that extend the filter/enrich reordering fix to the organization models route, adding addAutoRoutingModels after filterByFeature and covering both the org route and addAutoRoutingModels with new unit tests; the logic and tests are correct and no bugs were found in changed code.

Files Reviewed (3 files)
  • apps/web/src/app/api/organizations/[id]/models/route.ts
  • apps/web/src/app/api/organizations/[id]/models/route.test.ts
  • apps/web/src/lib/ai-gateway/auto-routing-models.test.ts
Previous Review Summary (commit aca83ed)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit aca83ed)

Status: No Issues Found | Recommendation: Merge

Executive Summary

Reviewed the auto-routing enrichment refactor and the filter/enrich reordering across both model-listing paths (org and public/BYOK); the fix that runs filterByFeature before addAutoRoutingModels is correctly applied and covered by new tests, with no bugs found in changed code.

Files Reviewed (4 files)
  • apps/web/src/app/api/openrouter/models/route.ts
  • apps/web/src/app/api/openrouter/models/route.test.ts
  • apps/web/src/lib/ai-gateway/auto-routing-models.ts
  • apps/web/src/lib/ai-gateway/auto-routing-table-cache.ts

Reviewed by claude-sonnet-5 · Input: 34 · Output: 9.3K · Cached: 752.6K

Review guidance: REVIEW.md from base branch main

@chrarnoldus

Copy link
Copy Markdown
Contributor

My guess is you also have to add these changes here for the organization-specific endpoint:

const result = await getAvailableModelsForOrganization(organizationId);

but wouldn't it make more sense to add the extra metadata the first time the auto model definitions are created?

Annotate the virtual kilo-auto/efficient and kilo-auto/free models with
their concrete candidate ids (autoRouting.models) in the models API
responses, so the model selector can show what auto-routing picks among.
Applied on /api/openrouter/models (org and public/BYOK paths) and on
/api/organizations/[id]/models — the endpoint the extension uses for
organization catalogs, where the choices were originally missing.

Candidates are filtered to models available to the caller: enrichment
runs after org restrictions and feature filtering, so a deny-listed or
feature-excluded model never surfaces as a routing choice. The benchmark
routing table is cached in-process (5-min, stale-on-error) so it is not
a per-request round-trip and a transient worker outage does not blank
the choices. Adds unit coverage for addAutoRoutingModels and first
direct test coverage for getAvailableModelsForOrganization.
@johnnyeric johnnyeric force-pushed the johnnyeric/expose-auto-efficient-models-organization branch from aca83ed to e377f87 Compare July 6, 2026 11:54
@johnnyeric

Copy link
Copy Markdown
Contributor Author

Good catch, added to the organization endpoint too (that was the original point, but missed after refactoring).

On the second part, I looked into it and there's an issue: org/feature filtering happens later and only drops top-level model ids - it doesn't touch the nested autoRouting.models. Adding the choices at definition time would let the org endpoint list candidates that were filtered out for that org (deny-listed models would show up as choices routing can never pick). Added a test locking this invariant in.

Let me know if I am missing something.

@johnnyeric johnnyeric merged commit b78a8db into main Jul 6, 2026
15 checks passed
@johnnyeric johnnyeric deleted the johnnyeric/expose-auto-efficient-models-organization branch July 6, 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.

2 participants