Skip to content

Add auto-sync upstream models feature for providers#8

Merged
GoJam11 merged 2 commits into
mainfrom
claude/channel-auto-sync-models-p8bebm
Jul 1, 2026
Merged

Add auto-sync upstream models feature for providers#8
GoJam11 merged 2 commits into
mainfrom
claude/channel-auto-sync-models-p8bebm

Conversation

@GoJam11

@GoJam11 GoJam11 commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Summary

This PR adds an "auto-sync upstream models" feature that allows providers to automatically synchronize their model lists from upstream /v1/models endpoints. When enabled, the model list is fetched immediately on save and then synced every 24 hours via a scheduled task.

Key Changes

  • New upstream models utility (src/upstream-models.ts): Centralized logic for fetching model IDs from upstream /v1/models endpoints, supporting both OpenAI and Anthropic APIs with proper authentication header handling and timeout management.

  • Provider auto-sync configuration:

    • Added autoSyncModels boolean field to provider configuration and database schema
    • Added modelsSyncedAt timestamp to track last sync time
    • New syncEntryModelsFromUpstream() function that validates upstream connectivity and replaces the provider's model list with upstream models
  • Immediate sync on save: When creating or updating a provider with autoSyncModels enabled, the upstream models are fetched and validated before persisting to the database. If the fetch fails, the entire operation fails with a clear error message.

  • 24-hour scheduled sync: Added runAutoModelSync() task in server.ts that runs every 24 hours to refresh model lists for all providers with auto-sync enabled.

  • Console UI updates:

    • Added checkbox for autoSyncModels in the provider form
    • Added i18n labels for both English and Chinese
    • Refactored console routes to use the new centralized fetchUpstreamModelIds() function
  • Database migration: Added auto_sync_models and models_synced_at columns to console_providers table

Implementation Details

  • The fetchUpstreamModelIds() function handles URL construction, authentication header normalization (auto-prefixing Bearer for authorization headers), and response parsing for both provider types
  • Error messages are user-friendly and include HTTP status codes and response snippets for debugging
  • The sync operation is atomic at the provider level—either all models are updated or none are
  • Existing console routes that fetch upstream models now delegate to the centralized utility, reducing code duplication

https://claude.ai/code/session_01S4YoQNdo1TzCjerUGFZoJy

claude added 2 commits July 1, 2026 09:27
- 渠道表单新增「自动同步上游模型」选项:开启并保存时先校验上游
  /v1/models,失败则整体报错、不落库;成功则立即用上游列表覆盖模型列表。
- 服务端每 24h 定时任务遍历开启该选项且启用中的渠道,重新拉取上游
  模型列表并写库;单渠道失败只记日志、上游空列表时保留原有模型。
- console_providers 新增 auto_sync_models / models_synced_at 两列
  (迁移 0010)。
- 新增 src/upstream-models.ts 统一上游 /v1/models 拉取逻辑,console 两个
  上游模型拉取路由与自动同步复用同一实现。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S4YoQNdo1TzCjerUGFZoJy
- bunfig.toml 增加 [install] registry = npmjs,避免默认走
  registry.npmmirror.com 经代理超时挂起。
- bun.lock 中 701 个 tarball URL 由 npmmirror 主机改写为 npmjs,
  锁定版本保持不变(仅换源)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S4YoQNdo1TzCjerUGFZoJy
@GoJam11 GoJam11 merged commit a7ee9b0 into main Jul 1, 2026
1 check passed
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