返回源码地图

packages/preset/agent-preset-registry/src/display.ts

main snapshot · da00f7f5358f · 正文引用章节 04;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/**
2 * Display resolution for roster presets, shared by every surface that renders
3 * preset names: shipped presets resolve through locale dictionary keys, and
4 * user-authored metadata is never translated. A pure fold with no imports, so
5 * browser bundles inline it and the Host uses the same single home for which
6 * shipped id carries which copy key.
7 * @module @deepseek-ai/dsh-agent-preset-registry/display
8 */
9
10/** Dictionary keys carrying one shipped preset's display copy. */
11export type BuiltInPresetCopyKey =
12 | 'presetStandardName' | 'presetStandardDescription'
13 | 'presetPtcName' | 'presetPtcDescription'
14 | 'presetMinimalName' | 'presetMinimalDescription'
15 | 'presetCordisName' | 'presetCordisDescription'
16
17/** Preset roster fields needed to resolve display copy. */
18export interface PresetDisplaySource {
19 /** Stable preset id. */
20 readonly id: string
21 /** Unlocalized name published by the preset. */
22 readonly name?: string
23 /** Unlocalized description published by the preset. */
24 readonly description?: string
25}
26
27/** Display copy resolved for the active locale. */
28export interface PresetDisplayText {
29 /** Localized built-in name or the preset's own fallback name. */
30 readonly name: string
31 /** Localized built-in description or the preset's own description. */
32 readonly description?: string
33}
34
35interface PresetLocaleKeys {
36 readonly name: BuiltInPresetCopyKey
37 readonly description: BuiltInPresetCopyKey
38}
39
40const BUILT_IN_PRESET_KEYS: Readonly<Partial<Record<string, PresetLocaleKeys>>> = {
41 standard: { name: 'presetStandardName', description: 'presetStandardDescription' },
42 ptc: { name: 'presetPtcName', description: 'presetPtcDescription' },
43 minimal: { name: 'presetMinimalName', description: 'presetMinimalDescription' },
44 cordis: { name: 'presetCordisName', description: 'presetCordisDescription' },
45}
46
47/**
48 * Whether a roster row is one of the shipped presets whose copy the dictionaries carry.
49 * A shipped preset publishes no `name`; a declaration that names itself owns its copy.
50 * @param preset - roster row.
51 * @returns true for a shipped preset id without a published name.
52 */
53export function isBuiltInPreset(preset: PresetDisplaySource): boolean {
54 return preset.name === undefined && BUILT_IN_PRESET_KEYS[preset.id] !== undefined
55}
56
57/**
58 * Resolve preset display copy without making user-authored metadata translatable.
59 * @param preset - roster row whose copy is being rendered.
60 * @param t - active locale lookup covering {@link BuiltInPresetCopyKey}.
61 * @returns localized copy for a known shipped preset, otherwise declaration metadata.
62 */
63export function presetDisplayText(
64 preset: PresetDisplaySource,
65 t: (key: BuiltInPresetCopyKey) => string,
66): PresetDisplayText {
67 const keys = isBuiltInPreset(preset) ? BUILT_IN_PRESET_KEYS[preset.id] : undefined
68 if (keys !== undefined) return { name: t(keys.name), description: t(keys.description) }
69 return {
70 name: preset.name ?? preset.id,
71 ...preset.description === undefined ? {} : { description: preset.description },
72 }
73}