Skip to content

feat(models): sync a provider's model list with its upstream endpoint - #916

Closed
jidekaixin2dian wants to merge 2 commits into
agegr:mainfrom
jidekaixin2dian:feat/models-sync-upstream
Closed

jidekaixin2dian wants to merge 2 commits into
agegr:mainfrom
jidekaixin2dian:feat/models-sync-upstream

Conversation

@jidekaixin2dian

@jidekaixin2dian jidekaixin2dian commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a Sync models action next to the existing Import models… flow, and makes discovery work for providers that do not repeat their endpoint in models.json.

Two problems this closes:

  1. Import models… (MODEL REFRESH BUTTON #880) could only hand-pick entries from the upstream list and never removed models the endpoint stopped offering, so a provider's list drifts until it is edited by hand (供应商模型列表过时/未能及时刷新 #860).
  2. Discovery required baseUrl in models.json, so a models-only provider entry — and every provider pi ships itself — could not be refreshed at all, even though pi already knows the endpoint.

Sync semantics

configured vs upstream result
upstream model missing locally added, always
local model no longer offered upstream reported, and removed only when the user ticks Also remove N models no longer offered upstream

Removal is opt-in on purpose: gateways, proxies, and paged endpoints can legitimately return a partial list, and silently deleting a working entry is much worse than leaving a stale one. Existing models keep their order; additions append. Sync edits the modal draft, so Save is still the only path that writes models.json.

Provider coverage

Discovery now resolves its endpoint from models.json first and falls back to pi's provider catalog (resolveModelDiscoveryAuth() returns the effective baseUrl/api it composed). That covers:

  • a models-only entry such as {"deepseek": {"models": [...]}} — pi's own endpoint is used
  • a built-in provider with no models.json entry
  • a fully configured custom provider, unchanged behaviour

The Import models… button is no longer disabled on an empty base URL, and the Base URL field states that leaving it empty uses pi's endpoint for that provider.

Relationship to the "Refresh catalog" button

#914 / #938 refreshes pi's remote provider catalog overlay (~/.pi/agent/models-store.json), which is a different mechanism: it can only surface models a catalog-backed provider publishes, and it never touches a provider's own GET /models. For a provider defined only in models.json (a local relay, a self-hosted gateway) that button has nothing to refresh, so this PR covers the remaining case.

How

  • lib/model-sync.ts (new) — pure planModelSync(configuredIds, upstreamModels) returning { additions, stale }. No React, no I/O; exact id comparison; dedupes and ignores blank ids on both sides.
  • lib/model-discovery-auth.ts — returns the effective baseUrl/api alongside the credentials.
  • app/api/models-config/discover/route.ts — uses the request values when present and the resolved ones otherwise; still reports Base URL required when neither exists.
  • components/ModelsConfig.tsx — sync summary/button/stale checkbox under the discovery list, wired into the modal's existing draft (merged with the new enabledModels controller from #930).
  • New i18n keys in en / zh-CN / zh-TW.

Verification

Rebased onto main (96966e5, pi 0.87.1) — the branch is currently MERGEABLE against main.

  • npx tsc --noEmit — clean.
  • npm run lint — clean.
  • npm test — 1316/1316 pass (Linux, the same environment CI uses).
  • npm run test:e2e — the full browser suite passes, including the new check:
    PASS: models sync imports upstream models and only drops opted-in stale ids.
  • New unit tests cover the sync planner (lib/model-sync.test.mjs), the modal wiring, the base-URL gate, and the resolved baseUrl/api.
  • e2e/models-sync.mjs drives the real modal against a local OpenAI-compatible fixture that serves GET /v1/models: it imports the upstream list, drops a stale id only when removal is ticked, verifies a second, unticked pass keeps that id, and asserts another provider is untouched.
  • Also exercised against the real DeepSeek /v1/models endpoint during development: add path, opt-in removal path, and preserve-without-opt-in path all behaved as above, with no browser console or page errors.

Demo

demo/components/ModelsConfig.tsx is a near-copy of the panel, but the demo answers /api/* from an in-browser mock that has no upstream to discover from. I left it untouched rather than ship a button that can only error in the public demo — happy to mirror the UI and add a mock /api/models-config/discover if you want the demo to show it.

Notes

  • #856 (discovery dropping capability metadata) is complementary: once parseDiscoveredModels() emits contextWindow / maxTokens / reasoning, synced additions pick those up with no change here.
  • e2e/models-sync.mjs opens the panel through the sidebar footer and falls back to Settings → Models, because the footer button's position depends on the layout the page loads with and a cold Turbopack compile can swallow the first clicks.

@jidekaixin2dian
jidekaixin2dian force-pushed the feat/models-sync-upstream branch from 2ab14d5 to 64d2462 Compare September 27, 2026 12:20
"Import models..." only supported hand-picking from the upstream list, and it
never removed models the endpoint stopped offering (agegr#880, agegr#860).

Add a "Sync models" action to the provider detail that reconciles the
configured model list with the discovered upstream list:

- upstream additions always apply
- models the upstream list no longer offers are reported and only dropped when
  the user opts in, because gateways, proxies, and paged endpoints can
  legitimately return a partial list

The comparison is a pure helper (lib/model-sync.ts), so it is unit-tested
without rendering the modal. Sync edits the existing draft, so Save is still
the only path that writes models.json.

Verification: npm test, tsc --noEmit, eslint, plus a browser run against the
DeepSeek /v1/models endpoint covering the add path, the opt-in removal path,
and the preserve-without-opt-in path.
Discovery required a baseUrl in models.json, so a models-only provider entry
-- and every provider pi ships itself -- could not be refreshed even though pi
already knows their endpoint.

- resolveModelDiscoveryAuth() now also returns the effective baseUrl and api it
  composed from pi's provider catalog, and the discover route uses them when the
  request omits either.
- The Models panel no longer disables "Import models..." on an empty base URL;
  the field explains that leaving it empty uses pi's endpoint.
- e2e/models-sync.mjs covers the flow against a local OpenAI-compatible fixture:
  importing upstream models, dropping a stale id only when removal is ticked, and
  leaving other providers untouched.
@jidekaixin2dian
jidekaixin2dian force-pushed the feat/models-sync-upstream branch from 64d2462 to 2245003 Compare September 27, 2026 12:56

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @jidekaixin2dian, the write-up and the e2e fixture are very thorough. I'm closing this one, though:

  • Scope. The Models panel already has two paths for this. Import models… can select every shown model and add the missing ones in one click, and Refresh catalog keeps pi's built-in providers current from the pi.dev catalog. A third reconcile action with its own summary, checkbox and hint adds UI weight for a narrow gain: removing stale ids, which the per-model delete already covers. We're trying to keep this panel lean.
  • A correctness issue in the current diff. syncProviderModels rewrites provider.models without mirroring the move into savedModelIdsRef. removeModel splices it and addDiscoveredModels calls trackAddedModels; the sync path does neither. So after "Also remove N stale models" and Save, collectModelRenames() compares the shifted index slots and reports renames that never happened (b → c, c → <first addition>). op: "resync" then rewrites enabledModels from those phantom renames.

If the missing piece for you is discovery on a models-only entry that relies on pi's built-in endpoint, please open an issue for that alone. The baseUrl/api fallback in resolveModelDiscoveryAuth() would make a small, focused PR on its own.


Generated by Claude Code

@jidekaixin2dian

Copy link
Copy Markdown
Contributor Author

两处都认,这个版本废弃。

bug 确认。syncProviderModels 重写 provider.models,没同步 savedModelIdsRef。removeModel 会 splice,addDiscoveredModels 会调 trackAddedModels,sync 这条路径两件都没做。collectModelRenames() 拿位移后的槽位比对,把没发生过的 rename 交给 op: "resync"。测试漏了「勾选删除后 Save」。

范围同意。删过期 id 用逐个删除够了,补新增有 Import models…,Refresh catalog 管 pi 内置 provider。这个 reconcile 动作是整份 PR 里偏弱的一半。

另一半开成 #1006:resolveModelDiscoveryAuth() 的 baseUrl/api fallback,加两行组件改动让它可达。你提的 models-only 条目 discovery 由 #1006 覆盖,没有另开 issue。

#1006 不带:lib/model-sync.ts、sync UI、e2e/models-sync.mjs、i18n key、AGENTS.md。

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