Skip to content

feat(models): resolve the discovery endpoint from pi's provider catalog - #1006

Merged
agegr merged 3 commits into
agegr:mainfrom
jidekaixin2dian:feat/discovery-baseurl-fallback
Sep 30, 2026
Merged

agegr merged 3 commits into
agegr:mainfrom
jidekaixin2dian:feat/discovery-baseurl-fallback

Conversation

@jidekaixin2dian

@jidekaixin2dian jidekaixin2dian commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

#916 关闭后的另一半。@agegr 在那条评论里写 resolveModelDiscoveryAuth() 的 baseUrl/api fallback「would make a small, focused PR on its own」,这个 PR 就是它。#916 里的 model-sync 动作、它的 UI(summary、checkbox、hint)、e2e/models-sync.mjs、新增的 i18n key、AGENTS.md 改动没有带过来。

问题

Import models… 需要 Base URL 才能拼出上游模型列表的地址。两类 provider 条目给不出这个值:

  • models-only 条目,靠 pi 为该 provider 内置的 endpoint(比如 deepseek: { models: [...] });
  • pi 内置、models.json 里没有对应条目的 provider。

这两类 provider 的 endpoint 和协议由 pi 解析,Models 面板是唯一拒绝拉取的地方。

改动

  • lib/model-discovery-auth.ts:resolveModelDiscoveryAuth() 返回它通过 ModelRuntime 解析出的 baseUrl 与 api(model.baseUrl 回退到 provider 的,model.api 回退到第一个模型的)。
  • app/api/models-config/discover/route.ts:models.json 没给值时用上面两个,协议回退到 pi 的默认。models.json 里显式配置的 baseUrl/api 优先,已配置的 provider 行为不变。
  • components/ModelsConfig.tsx:Base URL 为空时 Import models… 不再禁用,字段下加一行提示(与相邻的 API Key、Headers 提示同格式)。

这两行组件改动是 fallback 可达的前提:没有它们,这些 provider 不会调到这个 route。要收服务端改动也可以,代价是 fallback 到不了。

测试

  • lib/model-discovery.test.mjs:断言配置了 baseUrl/api 的 provider,以及依赖 pi 内置 endpoint 的 models-only 条目,两者解析出的 baseUrl/api。
  • components/ModelsConfig.test.mjs:源码级断言 discovery 不再以 provider.baseUrl 为门槛。

本机 npm test:1325 tests,1309 pass,11 fail,5 skipped。11 个失败在 pristine main 上同样复现(node-pty 原生模块没编译,加上 file-viewer、PATH、symlink 三处),与本次 diff 无关;两个新测试通过。Models 面板没有现成的 e2e 覆盖。

分支上的后续提交

@agegr 在分支上推了三个 commit:

  • 0bdc46b0:合并 main,解掉 ModelsConfig.test.mjs 与 fix(models): save a typed provider name with the Save button #969 provider rename 测试的冲突,上面的断言留成独立测试。
  • 66c02318:resolveModelDiscoveryAuth() 外包 try/catch。自定义 provider 没配 Base URL 时返回 400 Base URL is required,不再漏出 500 和内部占位名 model __pi_web_model_discovery__: no "api" specified;配了 Base URL 的仍然抛。提示语改成 Leave empty for a built-in provider to use the endpoint pi ships,自定义 provider 没有可回退的 endpoint。lib/model-discovery.test.mjs 加了这条路径的 route 测试。

他核对过的解析结果:deepseek → https://api.deepseek.com / openai-completions,anthropic → https://api.anthropic.com / anthropic-messages,openrouter → https://openrouter.ai/api/v1。浏览器里验过提示与 400。

Model discovery required models.json to repeat the provider's baseUrl and
api. Provider entries that only list models, and built-in providers pi
ships without a models.json entry, therefore could not import their
upstream model list -- even though pi itself resolves the endpoint and
protocol for exactly those providers.

resolveModelDiscoveryAuth() now also returns the effective baseUrl and
api, read from the ModelRuntime, and the discover route falls back to
them when models.json does not set its own. The Models panel no longer
gates "Import models..." on a non-empty Base URL, since pi resolves the
endpoint for it, and no longer sends the upstream default protocol.
agegr and others added 2 commits September 30, 2026 21:47
# Conflicts:
#	components/ModelsConfig.test.mjs
… one

With the Import button no longer gated on a Base URL, a custom provider without
one reached resolveModelDiscoveryAuth(), which failed with a 500 naming the
internal __pi_web_model_discovery__ placeholder. When no Base URL is configured,
that failure now answers the old 400 "Base URL is required". The field hint
says the fallback only applies to built-in providers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @jidekaixin2dian, this is exactly the focused follow-up to #916. The fallback reuses what resolveModelDiscoveryAuth() already resolves through ModelRuntime, and an explicit baseUrl / api in models.json still wins. I checked the resolution against the real catalogue: a models-only deepseek entry → https://api.deepseek.com / openai-completions, anthropic → https://api.anthropic.com / anthropic-messages, openrouter → https://openrouter.ai/api/v1.

I pushed a few changes:

  • Custom provider with no Base URL. Now that the Import button isn't gated on the field, a custom provider without one reached resolveModelDiscoveryAuth() and failed with a 500 naming the internal placeholder: model __pi_web_model_discovery__: no "api" specified. When no Base URL is configured, that failure now returns the previous 400 Base URL is required. There's a route test for it.
  • Hint wording. It now reads "Leave empty for a built-in provider to use the endpoint pi ships", since a custom provider has no shipped endpoint to fall back to.
  • Merged main and resolved the ModelsConfig.test.mjs conflict with the provider-rename tests from fix(models): save a typed provider name with the Save button #969, keeping your assertion as its own test.

Checked in the browser: the hint shows under Base URL, and Import on an empty custom provider shows "Base URL is required". tsc --noEmit, eslint, and the full unit suite pass (the one failure, worktree.test.mjs force removal, also fails on main in my macOS temp dir). LGTM 👍


Generated by Claude Code

@agegr
agegr merged commit d8f89c5 into agegr:main Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
解开 agegr#981 的 DirectoryPicker 冲突后,这一段(390e70f..f52fd84)实测 0 冲突:

- agegr#992 移动端输入框停在键盘上方并给打字更多空间
- agegr#997 限制每客户端 SSE 积压
- agegr#1001 可配置发送键(Enter / Ctrl+Enter)
- agegr#1006 从 pi 的 provider catalog 解析 discovery endpoint
- agegr#1017 空闲关闭被禁用时回收 Stop 无法解除的运行
- agegr#1016 Stop 结束输出被幸存进程持住的命令
- agegr#1015 保留用户消息里输入的换行
- agegr#1019 在 Safari / iOS 16.2 上可加载(新增 browserslist,无新依赖)
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…og (agegr#1006)

* feat(models): resolve the discovery endpoint from pi's provider catalog

Model discovery required models.json to repeat the provider's baseUrl and
api. Provider entries that only list models, and built-in providers pi
ships without a models.json entry, therefore could not import their
upstream model list -- even though pi itself resolves the endpoint and
protocol for exactly those providers.

resolveModelDiscoveryAuth() now also returns the effective baseUrl and
api, read from the ModelRuntime, and the discover route falls back to
them when models.json does not set its own. The Models panel no longer
gates "Import models..." on a non-empty Base URL, since pi resolves the
endpoint for it, and no longer sends the upstream default protocol.

* fix(models): keep "Base URL is required" for custom providers without one

With the Import button no longer gated on a Base URL, a custom provider without
one reached resolveModelDiscoveryAuth(), which failed with a 500 naming the
internal __pi_web_model_discovery__ placeholder. When no Base URL is configured,
that failure now answers the old 400 "Base URL is required". The field hint
says the fallback only applies to built-in providers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: jidekaixin2dian <[email protected]>
Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…og (agegr#1006)

* feat(models): resolve the discovery endpoint from pi's provider catalog

Model discovery required models.json to repeat the provider's baseUrl and
api. Provider entries that only list models, and built-in providers pi
ships without a models.json entry, therefore could not import their
upstream model list -- even though pi itself resolves the endpoint and
protocol for exactly those providers.

resolveModelDiscoveryAuth() now also returns the effective baseUrl and
api, read from the ModelRuntime, and the discover route falls back to
them when models.json does not set its own. The Models panel no longer
gates "Import models..." on a non-empty Base URL, since pi resolves the
endpoint for it, and no longer sends the upstream default protocol.

* fix(models): keep "Base URL is required" for custom providers without one

With the Import button no longer gated on a Base URL, a custom provider without
one reached resolveModelDiscoveryAuth(), which failed with a 500 naming the
internal __pi_web_model_discovery__ placeholder. When no Base URL is configured,
that failure now answers the old 400 "Base URL is required". The field hint
says the fallback only applies to built-in providers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: jidekaixin2dian <[email protected]>
Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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