-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(catalog): operator display labels for live-discovered models #2299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
abhisheksharma2411
wants to merge
8
commits into
lidge-jun:dev
from
abhisheksharma2411:feat/2201-operator-model-display-labels
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a973aba
feat(catalog): operator display labels for live-discovered models
abhisheksharma2411 e9b4fe9
feat(catalog): declare modelDisplayNames, and keep custom-model labels
abhisheksharma2411 eb7f194
test(catalog): pin that no control character reaches a stored display…
abhisheksharma2411 b4909d1
fix(catalog): widen the label control class, and preserve labels acro…
abhisheksharma2411 79201cb
fix(config): accept a null modelDisplayNames map at the write boundary
abhisheksharma2411 13eac06
fix(catalog): validate the merged display-label map, not the submitte…
abhisheksharma2411 2b875f7
fix(catalog): keep a __proto__ model id from vanishing between check …
abhisheksharma2411 dc77114
fix(catalog): label models at one post-gather boundary, not just conv…
abhisheksharma2411 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /** | ||
| * Operator-supplied display labels for live-discovered provider models (#2201). | ||
| * | ||
| * A discovered row's label is its routed slug, so NVIDIA NIM surfaces as | ||
| * `nvidia/deepseek-ai-deepseek-v4-flash-0731` in the picker, the dashboard, | ||
| * `/v1/models`, and client exports. `customModels[].displayName` and combo | ||
| * display labels already relabel their rows display-only; live discovery is the | ||
| * one row source with no equivalent. | ||
| * | ||
| * The single invariant: a display label is never routing identity. Nothing here | ||
| * touches `provider`, `id`, or the routed slug — the resolved label lands on | ||
| * `CatalogModel.displayName`, which `applyCatalogModelMetadata` already treats | ||
| * as display-only, and which client exports already read. | ||
| */ | ||
|
|
||
| import { COMBO_NAMESPACE } from "../../combos/types"; | ||
| import type { OcxConfig } from "../../types/config"; | ||
| import { CODEX_CUSTOM_MODEL_CATALOG_KIND } from "./parsing"; | ||
| import type { CatalogModel } from "./parsing"; | ||
|
|
||
| /** Same bound as the combo display label (src/combos/types.ts) so every label surface agrees. */ | ||
| export const MAX_DISPLAY_LABEL_LENGTH = 128; | ||
|
|
||
| // Control characters corrupt picker rendering, so a label carrying one is rejected. | ||
| // The range is C0, DEL, and C1 (U+0080-U+009F). C1 was originally missing, which let | ||
| // a label such as `Label<U+0085>More` through — U+0085 is NEL, a line break, and | ||
| // `trim()` does not touch a mid-string one. U+2028/U+2029 are added for the same | ||
| // reason: they are line and paragraph separators, so a label carrying one is not | ||
| // single-line whatever its width. | ||
| const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/; | ||
|
|
||
| /** | ||
| * Checked against the *untrimmed* value, unlike `CONTROL_CHARS`. | ||
| * | ||
| * `trim()` counts U+2028/U+2029 as whitespace and would strip an edge one, so a | ||
| * trailing line separator would otherwise be normalised away and reported as | ||
| * valid. A stray space or newline is plausible slop in a hand-edited config and | ||
| * is still forgiven; a Unicode line separator is not, so it is rejected wherever | ||
| * it appears rather than quietly removed. | ||
| */ | ||
| const CONTROLS_TRIM_WOULD_HIDE = /[\u0080-\u009f\u2028\u2029]/; | ||
|
|
||
| /** | ||
| * A label is usable when it is a non-empty single-line string within the shared bound. | ||
| * | ||
| * Slashes are rejected, matching the `customModels[].displayName` rule: a label | ||
| * containing `/` reads as a routed slug, and this field must never be mistaken | ||
| * for one. | ||
| */ | ||
| export function isValidDisplayLabel(value: unknown): value is string { | ||
| if (typeof value !== "string") return false; | ||
| if (CONTROLS_TRIM_WOULD_HIDE.test(value)) return false; | ||
| const trimmed = value.trim(); | ||
| if (trimmed.length === 0 || trimmed.length > MAX_DISPLAY_LABEL_LENGTH) return false; | ||
| if (CONTROL_CHARS.test(trimmed)) return false; | ||
| return !trimmed.includes("/"); | ||
| } | ||
|
|
||
| /** | ||
| * The precedence chain, in one place: | ||
| * | ||
| * 1. operator override — `providers[<provider>].modelDisplayNames[<native id>]` | ||
| * 2. trusted discovery metadata — a label discovery already attached | ||
| * 3. undefined — caller keeps its derived slug, i.e. today's behaviour | ||
| * | ||
| * Rows that already own an operator-supplied label keep it, and the provider map | ||
| * must not outrank them: | ||
| * - combo rows validate their own bounded label independently, so an entry under | ||
| * the combo namespace is never relabelled from provider config; | ||
| * - an explicit `customModels[]` row carries the label the operator typed there, | ||
| * and #2201 requires those to continue unchanged. Matching on `catalogKind` | ||
| * rather than on the provider name is what makes that hold, because a custom | ||
| * model shares its provider with the discovered rows this function exists for. | ||
| * | ||
| * Native OpenAI rows never reach here at all — they come from the pinned snapshot | ||
| * path with no `CatalogModel` — so upstream marketing names stay untouched. | ||
| */ | ||
| export function resolveModelDisplayLabel( | ||
| config: OcxConfig, | ||
| model: CatalogModel, | ||
| ): string | undefined { | ||
| if (model.provider === COMBO_NAMESPACE || model.catalogKind === CODEX_CUSTOM_MODEL_CATALOG_KIND) { | ||
| return isValidDisplayLabel(model.displayName) ? model.displayName.trim() : undefined; | ||
| } | ||
| const override = config.providers?.[model.provider]?.modelDisplayNames?.[model.id]; | ||
| if (isValidDisplayLabel(override)) return override.trim(); | ||
| if (isValidDisplayLabel(model.displayName)) return model.displayName.trim(); | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve labels across a discovered model list — the post-gather boundary. | ||
| * | ||
| * Called at exactly two places, one per gather entry point, so every surface that | ||
| * reads live routed models is covered: | ||
| * | ||
| * - `fetchAllModels` (src/server/management/shared.ts) — `/v1/models`, | ||
| * `/api/models` via `listManagementModelRows`, and client exports via | ||
| * `loadExportModels` all funnel through it; | ||
| * - `prepareCatalog` (src/codex/convergence.ts) — reaches the gather by the | ||
| * separate catalog-gather entry point, which never passes through the above. | ||
| * | ||
| * Anything reading models *without* going through one of those two is a surface | ||
| * that will emit the routed slug as its label. That is not hypothetical: labelling | ||
| * only the convergence call site is what left the live `/v1/models` route wrong | ||
| * while the on-disk catalog was right. | ||
| * | ||
| * Both calls sit after the gather rather than inside it, because the gather is | ||
| * TTL-cached on provider identity and a label baked into a cached entry would | ||
| * outlive an operator's edit to it. | ||
| * | ||
| * Idempotent, so the two boundaries overlapping is harmless: `resolveModelDisplayLabel` | ||
| * reads the config and the row's own kind, never a previously applied result. | ||
| * | ||
| * Returns the input array unchanged when nothing resolves, and otherwise a new | ||
| * array of new objects — the input models are never mutated, so a caller holding | ||
| * the pre-label list keeps it intact. | ||
| */ | ||
| export function applyOperatorDisplayLabels( | ||
| models: CatalogModel[], | ||
| config: OcxConfig, | ||
| ): CatalogModel[] { | ||
| let changed = false; | ||
| const labeled = models.map(model => { | ||
| const label = resolveModelDisplayLabel(config, model); | ||
| if (label === undefined || label === model.displayName) return model; | ||
| changed = true; | ||
| return { ...model, displayName: label }; | ||
| }); | ||
| return changed ? labeled : models; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.