Repository navigation
refactor: enhance provider registration with display names and icons - #8236
Conversation
| ) | ||
| .register("model_regolo_provider", "regolo_model_provider", "RegoloModelProvider", "Regolo", "regolo_icon_svg") | ||
| .register("model_minimax_provider", "minimax_model_provider", "MiniMaxModelProvider", "MiniMax", "minimax_icon_svg") | ||
| ) |
There was a problem hiding this comment.
Code Review
Potential Issues
1. Thread safety in get_provider_info_list / register
_provider_info is written without the lock in register, while _instances uses self._lock. If registration is only done once at import time this is fine, but it's inconsistent — consider guarding both or documenting that register is import-time-only.
2. get_provider_info_list does I/O every call (no caching)
- It reads and serializes icon files on every invocation. These are static assets; cache the result (e.g.,
functools.lru_cacheon a module-level helper, or store loaded icons after first build). from common.utils.common import get_file_contentandPROJECT_DIRare imported inside the method — fine for avoiding circular imports, but repeated per-call imports add overhead; hoist if safe.- Errors are not handled: if an icon file is missing/misnamed,
get_file_contentwill throw and break the whole list. Consider try/except with a log + skip (or empty string).
3. Fragile dir extraction
provider_dir = self._factories[name][0].rsplit(".", 1)[-1]This couples display-info lookup to the factory module path. Cleaner: store provider_dir directly in _provider_info, e.g.:
self._provider_info[name] = (display_name, icon_name)and pass the dir you already have in register() (you do — provider_dir param), so just key by it.
4. Lazy translation (gettext_lazy) mixed with plain strings
Some entries use _("..."), others are hardcoded ("OpenAI", "DeepSeek", ...). That's probably intentional (brand names unchanged), but verify consistency. More importantly — display_name may be a lazy proxy object stored in a dict; when returned in a plain list[dict], it will render per current locale at access time, which is usually desired, but make sure callers don't need concrete str (e.g., for JSONKeyError with ensure_ascii). If serialized elsewhere, force str(...).
5. Naming typos / inconsistencies
"model_siliconCloud_provider"and"siliconCloud_model_provider"use camelCase inconsistently with the rest (snake_case). Changing keys is a breaking change, but worth flagging.- Icon filenames end in
_svg(e.g.,"azure_icon_svg") — presumably.svgextension appended internally. Ensureget_file_contenthandles this correctly. Double-check names like"zhipuai_icon_svg"vs provider name"zhipu","bedrock_icon_svg"vs dir"aws_bedrock_model_provider"— easy to mismatch.
6. Return type docstring/signature
-> list[dict] is vague; prefer list[ProviderInfo] with a TypedDict / dataclass for clarity.
7. _provider_info may desync from _factories
Two parallel dicts keyed by name must stay in sync. If register ever fails partway (exception between assignments), state is inconsistent. Consider a single dict of small records, e.g.:
self._providers[name] = ProviderRecord(module=..., class_name=..., display_name=..., icon_name=..., dir=...)Optimization suggestions
- Cache
get_provider_info_list()results (icons rarely change). - Use a dict-comprehension / loop over a declarative list instead of a 60-line
.register(...)chain — easier to maintain and test:_PROVIDERS = [ ("model_openai_provider", "openai_model_provider", "OpenAIModelProvider", "OpenAI", "openai_icon_svg"), ... ]
- Add validation in
register(duplicate name check, non-empty args) to fail fast. - Consider storing icon bytes lazily and a
get_icon(name)accessor, so callers who only need names/icons don't pay full cost.
Summary
Main concerns: no caching on file I/O, fragile path-to-dir derivation, dual-dict desync risk, and a few naming typos. None are blocking, but addressing #2, #3, and #7 would improve robustness significantly.
| return result.success(ModelProvideConstants.get_provider_info_list()) | ||
|
|
||
| class ModelTypeList(APIView): | ||
| authentication_classes = [TokenAuth] |
There was a problem hiding this comment.
Looking at this diff, here's my analysis:
What the change does
It replaces an inline list comprehension (that iterated ModelProvideConstants.__members__) with a call to a new class method ModelProvideConstants.get_provider_info_list().
Potential issues & suggestions
-
Verify the new method exists and behaves identically
- Confirm
get_provider_info_list()is actually defined on the enum. If it's missing or still in progress, this will raiseAttributeErrorat runtime. - Ensure it iterates over
__members__(not something likelist(EnumClass)), because__members__includes aliases, whereas plain iteration may not. Behavior could silently differ if aliases exist.
- Confirm
-
Consistency between branches
- The non-
returnbranch above still callskey.get_model_provide_info().to_dict()per key individually. Ifget_provider_info_list()encapsulates this logic, consider refactoring the earlier branch to reuse it too, to avoid duplication and drift.
- The non-
-
Encapsulation is good, but watch for hidden side effects
- Moving logic into the enum couples data (the enum) with serialization/API concerns (
to_dict, result-list building). Consider whether provider-info listing belongs in a service layer rather than on the enum. This improves testability and separation of concerns.
- Moving logic into the enum couples data (the enum) with serialization/API concerns (
-
Ordering guarantees
- Previously the list order followed
__members__insertion order. Verify the new method preserves the same deterministic ordering; otherwise API clients relying on order may break.
- Previously the list order followed
-
Caching / performance
- If the provider info is static, consider caching the result (e.g.,
@lru_cacheor module-level constant) since this endpoint may be hit frequently, avoiding repeated dict construction.
- If the provider info is static, consider caching the result (e.g.,
-
Return type / mutation safety
- If
get_provider_info_list()returns a shared/cached list, callers mutating it could corrupt state across requests. Return a fresh copy or ensure immutability.
- If
-
Missing context / truncation
- The diff snippet appears truncated (line ends with
+ return result.success(ModelProvideConstants.get_provider_info_list())). Confirm there are no leftover/unreachable lines after this return and that the indentation aligns with the enclosing method.
- The diff snippet appears truncated (line ends with
-
Test coverage
- Add/adjust unit tests asserting the endpoint output is identical before and after this refactor (same content, order, and structure), especially if any enum members are aliases.
Summary: The refactor is reasonable and reduces duplication, but verify the new method exists, preserves iteration semantics (__members__ vs aliases), maintains ordering, and avoids returning mutable shared state.
refactor: enhance provider registration with display names and icons