mateclaw/mateclaw-ui/src/utils/modelProtocol.ts
matevip a168f91215 refactor(ui): split useProviders into 5 single-responsibility composables
Reshapes the Settings/Models frontend to match the channel-module split
convention (commit 22894ac4 'perf(channels): split Channels.vue...'),
zero behavior change. Paves the way for a follow-up that adds an enabled
column + AddProviderDrawer without bloating useProviders back to monolith.

Frontend split
- useProviders.ts goes from 615-line monolith to a 48-line facade that
  composes five single-responsibility slices:
    * useProviderList — providers / activeModels / currentProvider,
      loaders, status pill, icons
    * useProviderForm — create/edit modal + form, save/delete
    * useProviderDiscovery — manage-models modal, discovery, connection
      and per-model tests
    * useProviderOAuth — openai-chatgpt + claude-code OAuth flows
    * useProviderPool — manual reprobe (most pool surface inlined to
      ProviderInfo.liveness in the prior liveness change)
  Cross-composable refs flow via dep-injection arguments — no module-
  level state, no circular deps. Each composable stays independently
  testable.
- Pure helpers extracted to src/utils:
    * safeJson.ts — strict JSON-object parser
    * modelProtocol.ts — protocol <-> ChatModel class translation
- Modals (ProviderConfigModal, ManageModelsModal) loaded via
  defineAsyncComponent so the route's first paint doesn't drag along
  ~30KB of form/auth UI.
- el-skeleton placeholder during the initial Promise.all so the page
  paints something instead of blank-then-pop.

Layout regression fix
- MainLayout's <keep-alive> slot used :key='workspaceRouteKey' (a
  workspace-scoped string), shared between two <component v-if> blocks.
  Adding a second keepAlive route would have caused two components to
  mount side by side, because Vue saw identical keys and patched in
  place across the v-if boundary. Switched the key to
  ${workspaceRouteKey}:${route.path} so different routes get distinct
  vnode identities while workspace switching still busts the cache.
  Discovered while implementing the split — the multi-line HTML
  comment also had to live OUTSIDE <keep-alive>, since KeepAlive
  treats comments as children and rejects 'more than one'.

Embedding section title fix (drive-by)
- EmbeddingModelsSection.vue's scoped style didn't redeclare
  .group-title's flex layout, so the icon stacked above the title
  text instead of sitting inline. Added the missing flex rules
  locally — now matches the local-models / cloud-models group headers.

Verification
- vue-tsc 0 errors.
- Browser end-to-end: 27 cards render correctly, modals open via lazy
  load, /channels <-> /settings/models switch four times in a row with
  exactly one page title visible at each step (no stacking).
2026-04-28 15:01:14 +08:00

24 lines
1.0 KiB
TypeScript

/**
* Two-way translation between the high-level "protocol" the user picks in the
* provider form (openai-compatible / anthropic-messages / gemini-native /
* dashscope-native) and the Spring AI ChatModel class name persisted on the
* provider row.
*
* Default for unknown values is OpenAI-compatible — that's the protocol most
* third-party providers (Kimi, DeepSeek, Zhipu, OpenRouter, OpenCode, etc.)
* speak, so falling back there is safer than throwing.
*/
export function protocolToChatModel(protocol: string): string {
if (protocol === 'anthropic-messages') return 'AnthropicChatModel'
if (protocol === 'gemini-native') return 'GeminiChatModel'
if (protocol === 'dashscope-native') return 'DashScopeChatModel'
return 'OpenAIChatModel'
}
export function chatModelToProtocol(chatModel?: string): string {
if (chatModel === 'AnthropicChatModel') return 'anthropic-messages'
if (chatModel === 'GeminiChatModel') return 'gemini-native'
if (chatModel === 'DashScopeChatModel') return 'dashscope-native'
return 'openai-compatible'
}