Skip to content

feat(translator): add Baidu-general-translate backend#2

Open
Gilgamesh-lzq wants to merge 1 commit into
linyp:mainfrom
Gilgamesh-lzq:main
Open

feat(translator): add Baidu-general-translate backend#2
Gilgamesh-lzq wants to merge 1 commit into
linyp:mainfrom
Gilgamesh-lzq:main

Conversation

@Gilgamesh-lzq

Copy link
Copy Markdown

The Service preset dropdown gains a "Baidu API" entry. Layout is unchanged — existing inputs are reused with per-kind labels, backed by a new providerKind discriminator on settings that defaults to "openai" so pre-existing data.json still loads.

Settings/UI:

  • API key holds the Baidu secret; Base URL holds the appid; Model is displayed disabled with a note that it's ignored (no such wire field).
  • cacheModel() pins the Baidu cache identity to a stable constant, so whatever sits in the ignored Model input can never drift the cache.
  • matchPreset() matches by providerKind first (baseUrl is an appid for Baidu, not a URL); switching to Custom flips providerKind back to "openai".

Provider (pure, unit-tested):

  • Ships a small MD5 (RFC 1321, UTF-8 aware) in-tree — Node's crypto is absent in the Obsidian renderer and SubtleCrypto omits MD5. Used ONLY for Baidu's non-cryptographic sign = md5(appid + q + salt + secret), computed over the raw q per the docs' 54001 troubleshooting.
  • BaiduProvider aligns with the DeepSeek provider: same TranslationProvider surface, \n-join batching with per-segment
    line-count regroup, and per-segment fallback on SegmentCountMismatchError.
  • Baidu error_code maps to the existing typed errors (52003/54001/… → Auth, 54003/54005/52001/52002 → RateLimit, else → Malformed).

Wiring:

  • New createProvider(settings, http) factory; the settings-tab Test button and both translateButton.ts entry points route through it instead of constructing DeepSeekProvider directly.
  • Baidu preset ships a standard-tier shape (concurrency 1, minIntervalMs 1100, small batches) so QPS=1 accounts pass untuned.

The Service preset dropdown gains a "Baidu API" entry; the settings layout is unchanged and the existing API key / Base URL / Model inputs are reused with per-kind labels — for Baidu the key holds the secret, Base URL holds the appid, and Model is disabled and ignored. A providerKind discriminator persists the choice and defaults to "openai", so pre-existing data.json loads unchanged. Cache identity for Baidu is pinned to a stable constant so text in the ignored Model input can never drift the cache. Baidu's error_code table maps into the existing typed errors, so retry / auth / fallback flows behave identically to the OpenAI-compatible path. All call sites now go through a createProvider(settings, http) factory.

Signed-off-by: Gilgamesh <1794896442@qq.com>
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.

1 participant