dsh-extra-plan 0.0.0-stage → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +179 -2
  3. package/assets/presets/extra-plan/agent.cordis.yml +580 -0
  4. package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
  5. package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
  6. package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
  7. package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
  8. package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
  9. package/assets/presets/extra-plan/preset.yml +2 -0
  10. package/cordis.patch.yml +7 -0
  11. package/index.js +1671 -0
  12. package/lib/agent-runtime.js +172 -0
  13. package/lib/agent-session.js +65 -0
  14. package/lib/assembly-presentation.js +267 -0
  15. package/lib/client-carrier.js +7 -0
  16. package/lib/client.js +314 -0
  17. package/lib/extra-plan-gate-tool.js +387 -0
  18. package/lib/gate-decisions.js +744 -0
  19. package/lib/gate-words.js +108 -0
  20. package/lib/live-config.js +293 -0
  21. package/lib/log-retention.js +132 -0
  22. package/lib/model-routing.js +648 -0
  23. package/lib/path-policy.js +464 -0
  24. package/lib/plan-draft-tool.js +294 -0
  25. package/lib/planner-budget.js +136 -0
  26. package/lib/preset-defaults.generated.js +17 -0
  27. package/lib/preset-settings.js +3 -0
  28. package/lib/preset-sync.js +809 -0
  29. package/lib/preset-yaml.js +438 -0
  30. package/lib/role-contract.js +11 -0
  31. package/lib/run-code-scanner.js +360 -0
  32. package/lib/run-code-static.js +621 -0
  33. package/lib/runtime-diagnostics.js +89 -0
  34. package/lib/runtime-lifecycle.js +162 -0
  35. package/lib/runtime-static.js +23 -0
  36. package/lib/save-contract.js +263 -0
  37. package/lib/save-persistence.js +223 -0
  38. package/lib/save-plan-markdown-parser.js +369 -0
  39. package/lib/save-plan-validation-diagnostics.js +49 -0
  40. package/lib/save-plan-validation.js +812 -0
  41. package/lib/save-probe-validation.js +173 -0
  42. package/lib/save-source-verification.js +195 -0
  43. package/lib/save-tool-factories.js +270 -0
  44. package/lib/sdk-text-cache.js +161 -0
  45. package/lib/settings-contract.js +191 -0
  46. package/lib/settings.js +369 -0
  47. package/lib/shell-mutation.js +484 -0
  48. package/lib/usage-ledger-background.js +214 -0
  49. package/lib/usage-ledger-worker.js +116 -0
  50. package/lib/usage-ledger.js +1393 -0
  51. package/locale/en.json +6 -0
  52. package/locale/settings/en.json +6 -0
  53. package/locale/settings/zh.json +6 -0
  54. package/locale/zh.json +6 -0
  55. package/package.json +56 -4
  56. package/scripts/generate-runtime-defaults.mjs +796 -0
@@ -0,0 +1,161 @@
1
+ // dsh-extra-plan lib/sdk-text-cache.js
2
+ // agent-keyed、仅内存的 SDK 文本缓存;不持有 sessionId,也不缓存 PromptAssembly。
3
+
4
+ const hasOwn = Object.prototype.hasOwnProperty
5
+ const FINGERPRINT_FAILED = Symbol('sdk-schema-fingerprint-failed')
6
+
7
+ function isWeakKey(value) {
8
+ return value !== null && (typeof value === 'object' || typeof value === 'function')
9
+ }
10
+
11
+ function numberTag(value) {
12
+ if (Number.isNaN(value)) return 'NaN'
13
+ if (value === Infinity) return '+Infinity'
14
+ if (value === -Infinity) return '-Infinity'
15
+ if (Object.is(value, -0)) return '-0'
16
+ return String(value)
17
+ }
18
+
19
+ function isArrayIndexKey(key) {
20
+ if (key === '') return false
21
+ const number = Number(key)
22
+ return Number.isInteger(number) && number >= 0 && number < 4294967295 && String(number) === key
23
+ }
24
+
25
+ // 只接受 renderer 可见的 JSON-like 数据;字段顺序、数组顺序、字段存在性均保留。
26
+ // 任何 getter、symbol、非枚举字段、循环引用或非 plain object 都保守地放弃签名。
27
+ function encode(value, active) {
28
+ if (value === null) return ['null']
29
+ if (value === undefined) return ['undefined']
30
+ if (typeof value === 'boolean') return ['boolean', value]
31
+ if (typeof value === 'string') return ['string', value]
32
+ if (typeof value === 'number') return ['number', numberTag(value)]
33
+ if (typeof value !== 'object') return FINGERPRINT_FAILED
34
+ if (active.has(value)) return FINGERPRINT_FAILED
35
+ active.add(value)
36
+ try {
37
+ if (Array.isArray(value)) {
38
+ const ownKeys = Reflect.ownKeys(value)
39
+ const enumerableKeys = Object.keys(value)
40
+ for (const key of ownKeys) {
41
+ if (key === 'length') continue
42
+ if (typeof key !== 'string' || !enumerableKeys.includes(key)) return FINGERPRINT_FAILED
43
+ const descriptor = Object.getOwnPropertyDescriptor(value, key)
44
+ if (descriptor === undefined || descriptor.get !== undefined || descriptor.set !== undefined || descriptor.enumerable !== true) return FINGERPRINT_FAILED
45
+ }
46
+ const items = []
47
+ for (let index = 0; index < value.length; index += 1) {
48
+ const key = String(index)
49
+ if (!hasOwn.call(value, key)) {
50
+ items.push(['hole'])
51
+ continue
52
+ }
53
+ const encoded = encode(value[index], active)
54
+ if (encoded === FINGERPRINT_FAILED) return FINGERPRINT_FAILED
55
+ items.push(['value', encoded])
56
+ }
57
+ const extras = []
58
+ for (const key of enumerableKeys) {
59
+ if (isArrayIndexKey(key)) continue
60
+ const descriptor = Object.getOwnPropertyDescriptor(value, key)
61
+ if (descriptor === undefined || descriptor.get !== undefined || descriptor.set !== undefined) return FINGERPRINT_FAILED
62
+ const encoded = encode(value[key], active)
63
+ if (encoded === FINGERPRINT_FAILED) return FINGERPRINT_FAILED
64
+ extras.push([key, encoded])
65
+ }
66
+ return ['array', value.length, items, extras]
67
+ }
68
+ const prototype = Object.getPrototypeOf(value)
69
+ if (prototype !== null && prototype !== Object.prototype) return FINGERPRINT_FAILED
70
+ const ownKeys = Reflect.ownKeys(value)
71
+ const enumerableKeys = Object.keys(value)
72
+ for (const key of ownKeys) {
73
+ if (typeof key !== 'string' || !enumerableKeys.includes(key)) return FINGERPRINT_FAILED
74
+ const descriptor = Object.getOwnPropertyDescriptor(value, key)
75
+ if (descriptor === undefined || descriptor.get !== undefined || descriptor.set !== undefined || descriptor.enumerable !== true) return FINGERPRINT_FAILED
76
+ }
77
+ const properties = []
78
+ for (const key of enumerableKeys) {
79
+ const encoded = encode(value[key], active)
80
+ if (encoded === FINGERPRINT_FAILED) return FINGERPRINT_FAILED
81
+ properties.push([key, encoded])
82
+ }
83
+ return ['object', prototype === null ? 'null' : 'object', properties]
84
+ } catch (error) {
85
+ return FINGERPRINT_FAILED
86
+ } finally {
87
+ active.delete(value)
88
+ }
89
+ }
90
+
91
+ export function sdkSchemasFingerprint(schemas) {
92
+ try {
93
+ const encoded = encode(schemas, new Set())
94
+ if (encoded === FINGERPRINT_FAILED) return undefined
95
+ return JSON.stringify(encoded)
96
+ } catch (error) {
97
+ return undefined
98
+ }
99
+ }
100
+
101
+ export function sdkTextCacheEntryMatches(entry, fingerprint, language, renderer) {
102
+ return entry !== null
103
+ && typeof entry === 'object'
104
+ && entry.fingerprint === fingerprint
105
+ && entry.language === language
106
+ && entry.renderer === renderer
107
+ }
108
+
109
+ function renderUncached(schemas, renderer) {
110
+ return Promise.resolve()
111
+ .then(() => {
112
+ if (typeof renderer !== 'function') throw new TypeError('extra-plan: SDK renderer must be a function')
113
+ return renderer(schemas)
114
+ })
115
+ .then((text) => {
116
+ if (typeof text !== 'string') throw new TypeError('extra-plan: SDK renderer must return text')
117
+ return text
118
+ })
119
+ }
120
+
121
+ export function createSdkTextCache() {
122
+ const entries = new WeakMap()
123
+
124
+ function dispose(agent) {
125
+ if (isWeakKey(agent)) entries.delete(agent)
126
+ }
127
+
128
+ function getOrCreate(agent, schemas, language, renderer) {
129
+ const fingerprint = sdkSchemasFingerprint(schemas)
130
+ if (!isWeakKey(agent) || fingerprint === undefined || typeof language !== 'string' || typeof renderer !== 'function') {
131
+ return renderUncached(schemas, renderer)
132
+ }
133
+
134
+ const current = entries.get(agent)
135
+ if (sdkTextCacheEntryMatches(current, fingerprint, language, renderer)) {
136
+ if (hasOwn.call(current, 'text')) return Promise.resolve(current.text)
137
+ if (current.promise !== undefined) return current.promise
138
+ }
139
+
140
+ const entry = { fingerprint, language, renderer, promise: undefined }
141
+ entries.set(agent, entry)
142
+ const promise = Promise.resolve()
143
+ .then(() => renderer(schemas))
144
+ .then((text) => {
145
+ if (typeof text !== 'string') throw new TypeError('extra-plan: SDK renderer must return text')
146
+ if (entries.get(agent) === entry) {
147
+ delete entry.promise
148
+ entry.text = text
149
+ }
150
+ return text
151
+ })
152
+ .catch((error) => {
153
+ if (entries.get(agent) === entry) entries.delete(agent)
154
+ throw error
155
+ })
156
+ entry.promise = promise
157
+ return promise
158
+ }
159
+
160
+ return { getOrCreate, dispose }
161
+ }
@@ -0,0 +1,191 @@
1
+ // 服务端设置的单一 descriptor 合同;默认值与生成常量同源。
2
+ import { DEFAULT_SETTINGS } from './preset-defaults.generated.js'
3
+ export { DEFAULT_SETTINGS }
4
+
5
+ const isString = (value) => typeof value === 'string'
6
+ const isPositiveInteger = (value) => typeof value === 'number' && Number.isInteger(value) && value > 0
7
+ const isBoolean = (value) => typeof value === 'boolean'
8
+ // 0.2.2:executor 行身份模型——呈现模式由宿主行 id 承载(tool-executor-native / tool-executor-ptc)。
9
+ const modeOptions = Object.freeze(['native', 'ptc'])
10
+ const isMode = (value) => typeof value === 'string' && modeOptions.includes(value)
11
+
12
+ /** 新载体行 id(settings 命名空间 = profile 行 id)。 */
13
+ export const SETTINGS_ROW_ID = 'dsh-extra-plan-settings'
14
+ /** settings 行的规范 name:定位落点 = 行 id + 该 name 双条件(旧 @local 别名行不再作候选)。 */
15
+ export const SETTINGS_ROW_NAME = 'dsh-extra-plan/settings'
16
+ /**
17
+ * 声明行 plugins 内承载宿主行设置的子行 id(投影落点的行 id)。
18
+ * 当前只剩 toolPresentationMode:用行身份承载模式(两个 executor 行 id),没有叶键;
19
+ * 原 webFetch 条目已删(权威值在 settings 行、由运行期 restrict 生效,不再有宿主行 id)。
20
+ */
21
+ export const HOST_ROW_IDS = Object.freeze({
22
+ toolPresentationMode: Object.freeze({ native: 'tool-executor-native', ptc: 'tool-executor-ptc' }),
23
+ })
24
+ /** 行 id → 呈现模式的唯一映射源:键由 HOST_ROW_IDS.toolPresentationMode 派生,不再另写字面量。 */
25
+ export const MODE_BY_ROW_ID = Object.freeze({
26
+ [HOST_ROW_IDS.toolPresentationMode.native]: 'native',
27
+ [HOST_ROW_IDS.toolPresentationMode.ptc]: 'ptc',
28
+ })
29
+ /** executor 宿主行包名前缀:name = 前缀 + 裸模式名(native/ptc);运行期行 id 是裸行 id(不带包名前缀),两种形态不得混用。 */
30
+ export const EXECUTOR_TOOL_NAME_PREFIX = '@deepseek-ai/dsh-tool-executor-'
31
+ /** 投影落点的叶键(宿主行 config 内键名):源模板与投影共用同一份 leaf 名,不新增第二份键名清单。
32
+ * 当前为空集(原 webFetch:'fetch' 已删:web_fetch 现由 extra-plan 行 config.webFetch + 运行期 restrict 生效),
33
+ * 表结构保留为叶键投影原语。 */
34
+ export const HOST_ROW_LEAF_KEYS = Object.freeze({})
35
+ /** descriptor 分组(消费方):11 项统一落 settings 全局行;1 项另派生到内存 definition。 */
36
+ export const SETTING_GROUPS = Object.freeze({ EXTRA_PLAN: 'extra-plan', HOST_ROWS: 'host-rows' })
37
+
38
+ const setting = (definition) => Object.freeze({
39
+ ...definition,
40
+ type: definition.scalarType,
41
+ sourceLocator: Object.freeze({
42
+ ...definition.sourceLocator,
43
+ ...(definition.sourceLocator.rowIds === undefined
44
+ ? {}
45
+ : { rowIds: Object.freeze([...definition.sourceLocator.rowIds]) }),
46
+ ...(definition.sourceLocator.modeByRowId === undefined
47
+ ? {}
48
+ : { modeByRowId: Object.freeze({ ...definition.sourceLocator.modeByRowId }) }),
49
+ }),
50
+ rowLocator: Object.freeze({ ...definition.rowLocator }),
51
+ locatorAliases: Object.freeze((definition.locatorAliases || []).map((alias) => Object.freeze({
52
+ rowId: alias.rowId,
53
+ path: alias.path,
54
+ }))),
55
+ ui: Object.freeze({
56
+ ...definition.ui,
57
+ options: definition.ui.options === undefined ? undefined : Object.freeze([...definition.ui.options]),
58
+ optionLocale: definition.ui.optionLocale === undefined
59
+ ? undefined
60
+ : Object.freeze({ ...definition.ui.optionLocale }),
61
+ }),
62
+ })
63
+
64
+ /**
65
+ * 权威值落点:settings 行(11 项统一)config.<key>,rowName 精确过滤规范名
66
+ * (旧 @local/dsh-extra-plan/settings 别名行不参与候选;descriptor 的 locatorAliases 均为空)。
67
+ */
68
+ export const settingsRowLocator = (key, path) => ({
69
+ rowId: SETTINGS_ROW_ID,
70
+ rowName: SETTINGS_ROW_NAME,
71
+ path: path === undefined ? 'config.' + key : path,
72
+ })
73
+ const sourceLocator = (key, path) => ({ rowId: 'extra-plan', path: path === undefined ? 'config.' + key : path })
74
+
75
+ export const SETTING_DEFINITIONS = Object.freeze([
76
+ setting({
77
+ key: 'anchoredBootstrap', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.anchoredBootstrap,
78
+ sourceLocator: sourceLocator('anchoredBootstrap'), rowLocator: settingsRowLocator('anchoredBootstrap'),
79
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'anchoredBootstrap', section: 'general', hint: '首轮极简工具 + 提示词 | 新会话/新子代理生效' }, locatorAliases: [],
80
+ }),
81
+ setting({
82
+ key: 'creativeMode', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.creativeMode,
83
+ sourceLocator: sourceLocator('creativeMode'), rowLocator: settingsRowLocator('creativeMode'),
84
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'creativeMode', section: 'general', hint: '是否开启dsh官方创造模式 | 立即生效' }, locatorAliases: [],
85
+ }),
86
+ setting({
87
+ key: 'webFetch', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.webFetch,
88
+ // 源模板值 = extra-plan 行 config.webFetch(作者源主 tool-web 行常开 fetch: true,运行期由 restrict 按本值逐会话改配置);
89
+ // 故不再指向宿主行同叶,权威值落点仍为 settings 行 config.webFetch。
90
+ sourceLocator: sourceLocator('webFetch'),
91
+ rowLocator: settingsRowLocator('webFetch'),
92
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'webFetch', section: 'general', hint: '是否开启web_fetch | 立即生效' }, locatorAliases: [],
93
+ }),
94
+ setting({
95
+ key: 'toolPresentationMode', group: SETTING_GROUPS.HOST_ROWS, scalarType: 'mode', defaultValue: DEFAULT_SETTINGS.toolPresentationMode,
96
+ // 行身份模型:值 = 命中的 executor 行 id 对应的 mode(两个行 id 合计恰 1 命中),无 config 叶键。
97
+ sourceLocator: {
98
+ rowIds: [HOST_ROW_IDS.toolPresentationMode.native, HOST_ROW_IDS.toolPresentationMode.ptc],
99
+ modeByRowId: MODE_BY_ROW_ID,
100
+ },
101
+ rowLocator: settingsRowLocator('toolPresentationMode'),
102
+ validator: isMode,
103
+ ui: {
104
+ control: 'select', options: modeOptions,
105
+ optionLocale: { native: 'toolPresentationModeNative', ptc: 'toolPresentationModePtc' },
106
+ locale: 'toolPresentationMode',
107
+ section: 'general',
108
+ hint: '工具执行器切换(默认/PTC模式),无法进行会话内切换 | 重启生效',
109
+ },
110
+ locatorAliases: [],
111
+ }),
112
+ setting({
113
+ key: 'runcodeCatchGate', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.runcodeCatchGate,
114
+ sourceLocator: sourceLocator('runcodeCatchGate'), rowLocator: settingsRowLocator('runcodeCatchGate'),
115
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'runcodeCatchGate', section: 'general', hint: 'PTC模式下,增加每个工具调用需要try catch的闸门。通过限制+建议的模式保障仅单个调用报错 | 立即生效' }, locatorAliases: [],
116
+ }),
117
+ setting({
118
+ key: 'crossProviderPlannerModel', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.crossProviderPlannerModel,
119
+ sourceLocator: sourceLocator('crossProviderPlannerModel'), rowLocator: settingsRowLocator('crossProviderPlannerModel'),
120
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'crossProviderPlannerModel', section: 'pro', hint: '允许跨提供方选择模型。开启时将以 其他提供方 - 主会话提供方 - deepseek官方 的顺序,获取可用模型。关闭时仅从主会话提供方获取。默认关闭 | 新会话/新子代理生效' }, locatorAliases: [],
121
+ }),
122
+ setting({
123
+ key: 'plannerModel', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'string', defaultValue: DEFAULT_SETTINGS.plannerModel,
124
+ sourceLocator: sourceLocator('plannerModel'), rowLocator: settingsRowLocator('plannerModel'),
125
+ // T4:允许空串(= 显式清空 = 继承主会话模型;解析侧只判 !=='',键缺失才用代码缺省值)。
126
+ // 空白串经 normalize trim 归一为 '',与空串同义;非 string(如 YAML 数字)仍非法。
127
+ validator: isString, normalize: (value) => value.trim(),
128
+ ui: { control: 'text', locale: 'plannerModel', section: 'pro', hint: '规划者默认使用模型。未匹配/置空时:使用主会话模型 | 新会话/新子代理生效' }, locatorAliases: [],
129
+ }),
130
+ setting({
131
+ key: 'plannerPromptSuffix', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'string', defaultValue: DEFAULT_SETTINGS.plannerPromptSuffix,
132
+ sourceLocator: sourceLocator('plannerPromptSuffix'), rowLocator: settingsRowLocator('plannerPromptSuffix'),
133
+ validator: isString, ui: { control: 'textarea', locale: 'plannerPromptSuffix', section: 'pro', hint: '在主会话发送给规划者的任务结尾,拼接上的内容。可能能增加规划者的智商(未验证)。可置空 | 立即生效' }, locatorAliases: [],
134
+ }),
135
+ setting({
136
+ key: 'exploreBudget', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'integer', defaultValue: DEFAULT_SETTINGS.exploreBudget,
137
+ sourceLocator: sourceLocator('exploreBudget'), rowLocator: settingsRowLocator('exploreBudget'),
138
+ validator: isPositiveInteger, ui: { control: 'number', min: 1, step: 1, locale: 'exploreBudget', section: 'pro', hint: '允许规划者调用工具的次数,避免后台无限制调用。同时限制一次runcode内可调用的工具上限数 | 立即生效' }, locatorAliases: [],
139
+ }),
140
+ setting({
141
+ key: 'otherAgentModel', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'string', defaultValue: DEFAULT_SETTINGS.otherAgentModel,
142
+ sourceLocator: sourceLocator('otherAgentModel'), rowLocator: settingsRowLocator('otherAgentModel'),
143
+ validator: isString, normalize: (value) => value.trim(),
144
+ ui: { control: 'text', locale: 'otherAgentModel', section: 'pro', hint: '其他子代理默认使用模型。未匹配/置空时:使用主会话模型 | 新会话/新子代理生效' }, locatorAliases: [],
145
+ }),
146
+ setting({
147
+ key: 'reviewerUsesPlannerModel', group: SETTING_GROUPS.EXTRA_PLAN, scalarType: 'boolean', defaultValue: DEFAULT_SETTINGS.reviewerUsesPlannerModel,
148
+ sourceLocator: sourceLocator('reviewerUsesPlannerModel'), rowLocator: settingsRowLocator('reviewerUsesPlannerModel'),
149
+ validator: isBoolean, ui: { control: 'select', options: [true, false], locale: 'reviewerUsesPlannerModel', section: 'pro', hint: '验收者是否改用规划者的模型。默认关闭 | 新会话/新子代理生效' }, locatorAliases: [],
150
+ }),
151
+ ])
152
+
153
+ export function createDefaultSettings() {
154
+ return Object.fromEntries(SETTING_DEFINITIONS.map((definition) => [definition.key, DEFAULT_SETTINGS[definition.key]]))
155
+ }
156
+ /** 10 项 UI 设置(权威值落 settings 行;= live-config 热读键集合)。 */
157
+ export const EXTRA_PLAN_SETTING_DEFINITIONS = Object.freeze(
158
+ SETTING_DEFINITIONS.filter((item) => item.group === SETTING_GROUPS.EXTRA_PLAN),
159
+ )
160
+ /** 1 项宿主行设置(权威值同样落 settings 行;内存 definition 由当前宿主 descriptor 派生)。 */
161
+ export const HOST_ROW_SETTING_DEFINITIONS = Object.freeze(
162
+ SETTING_DEFINITIONS.filter((item) => item.group === SETTING_GROUPS.HOST_ROWS),
163
+ )
164
+ /** 宿主行投影集合(= 1 项宿主行设置)——由当前宿主 descriptor 派生的唯一遍历源。 */
165
+ export const PROJECTION_SETTING_DEFINITIONS = Object.freeze([...HOST_ROW_SETTING_DEFINITIONS])
166
+
167
+ const descriptorByKey = new Map(SETTING_DEFINITIONS.map((item) => [item.key, item]))
168
+
169
+ export function getSettingDefinition(key) {
170
+ return descriptorByKey.get(key)
171
+ }
172
+
173
+ export function validateSettingValue(definition, value) {
174
+ return definition !== undefined && typeof definition.validator === 'function' && definition.validator(value)
175
+ }
176
+
177
+ export function normalizeSettingValue(definition, value) {
178
+ return typeof definition.normalize === 'function' ? definition.normalize(value) : value
179
+ }
180
+
181
+ // definition 恒来自 SETTING_DEFINITIONS,故此处不重复校验 definition 存在性。
182
+ export function checkManagedValues(source) {
183
+ const values = {}
184
+ for (const definition of SETTING_DEFINITIONS) {
185
+ if (!Object.prototype.hasOwnProperty.call(source, definition.key)) return { ok: false, key: definition.key, reason: 'missing' }
186
+ if (!validateSettingValue(definition, source[definition.key])) return { ok: false, key: definition.key, reason: 'invalid' }
187
+ values[definition.key] = normalizeSettingValue(definition, source[definition.key])
188
+ }
189
+ return { ok: true, values }
190
+ }
191
+