@surfishs/dsh-prompt-enhance 0.0.5 → 0.0.7
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.
- package/README.en.md +17 -8
- package/README.md +16 -7
- package/docs/images/panel-en-915.png +0 -0
- package/docs/images/panel.png +0 -0
- package/lib/client.js +351 -31
- package/package.json +1 -1
- package/src/client/locales.ts +10 -4
- package/src/client/settings-page.tsx +295 -43
- package/src/client/styles.ts +99 -0
package/src/client/locales.ts
CHANGED
|
@@ -68,7 +68,10 @@ export const zh = {
|
|
|
68
68
|
'settings.description': '把输入框草稿改写为结构化提示词:角色与目标、可执行步骤、输出格式、验收标准、边界条件。原文保留,失败不改草稿;保存后下一次调用即生效。',
|
|
69
69
|
'settings.save': '保存',
|
|
70
70
|
'settings.saving': '保存中…',
|
|
71
|
+
'settings.saved': '已保存',
|
|
71
72
|
'settings.saveFailed': '本部署没有接受这些值,已保留供你修改。',
|
|
73
|
+
'settings.saveFailedShort': '保存失败',
|
|
74
|
+
'settings.advanced': '高级设置',
|
|
72
75
|
'settings.readOnly': '本部署的设置为只读。',
|
|
73
76
|
'settings.unavailable': '该插件当前未加载,暂时无法配置。',
|
|
74
77
|
'settings.reset': '恢复默认',
|
|
@@ -106,7 +109,7 @@ export const zh = {
|
|
|
106
109
|
'settings.row.temperature.label': '温度',
|
|
107
110
|
'settings.row.temperature.hint': '采样温度(0~1,步进 0.05),越低越忠实原文;默认 0.3。',
|
|
108
111
|
'settings.row.reasoningEffort.label': '推理预算',
|
|
109
|
-
'settings.row.reasoningEffort.hint': '档位选项来自当前 provider/model
|
|
112
|
+
'settings.row.reasoningEffort.hint': '档位选项来自当前 provider/model 的声明;自定义模型时按需读取。提供方默认表示不传该字段。',
|
|
110
113
|
'settings.row.maxOutputTokens.label': '输出 token 上限',
|
|
111
114
|
'settings.row.maxOutputTokens.hint': '单次增强的输出预算;默认 8192。',
|
|
112
115
|
'settings.row.maxInputChars.label': '输入字符上限',
|
|
@@ -117,7 +120,7 @@ export const zh = {
|
|
|
117
120
|
'settings.row.strategyMode.hint': '自定义系统提示词非空时:整体替换内置策略(仍保留最小输出契约),或追加在内置策略之后(内置规则继续生效)。',
|
|
118
121
|
'settings.row.systemPrompt.label': '自定义系统提示词',
|
|
119
122
|
'settings.row.systemPrompt.hint': '留空使用内置增强策略。',
|
|
120
|
-
'settings.row.systemPrompt.placeholder': '
|
|
123
|
+
'settings.row.systemPrompt.placeholder': '例:把用户的中文输入翻译成英文。',
|
|
121
124
|
'settings.row.contextMaxMessages.label': '上下文消息条数',
|
|
122
125
|
'settings.row.contextMaxMessages.hint': '最多参考最近多少轮对话;0 表示不参考;默认 8。',
|
|
123
126
|
'settings.row.contextMaxChars.label': '上下文字符上限',
|
|
@@ -131,7 +134,7 @@ export const zh = {
|
|
|
131
134
|
'settings.row.shortcut.placeholder': '例:ctrl+alt+shift+e',
|
|
132
135
|
'settings.strategy.replace': '替换内置策略',
|
|
133
136
|
'settings.strategy.extend': '追加在内置策略后',
|
|
134
|
-
'settings.reasoning.inherit': '
|
|
137
|
+
'settings.reasoning.inherit': '提供方默认',
|
|
135
138
|
'settings.reasoning.loading': '正在读取该模型的档位…',
|
|
136
139
|
'settings.reasoning.unavailable': '无法读取该模型的档位,仅可跟随提供方默认。',
|
|
137
140
|
'settings.reasoning.noTiers': '该模型未声明推理档位,调用时将跟随提供方默认。',
|
|
@@ -211,7 +214,10 @@ export const en: Record<PromptEnhanceKey, string> = {
|
|
|
211
214
|
'settings.description': 'Turns a composer draft into a structured prompt: role and goal, executable steps, output format, acceptance criteria, edge cases. The draft is preserved, failures never touch it, and saved values apply to the next call.',
|
|
212
215
|
'settings.save': 'Save',
|
|
213
216
|
'settings.saving': 'Saving…',
|
|
217
|
+
'settings.saved': 'Saved',
|
|
214
218
|
'settings.saveFailed': 'The deployment did not accept these values; they were left for you to correct.',
|
|
219
|
+
'settings.saveFailedShort': 'Save failed',
|
|
220
|
+
'settings.advanced': 'Advanced',
|
|
215
221
|
'settings.readOnly': 'This deployment stores settings read-only.',
|
|
216
222
|
'settings.unavailable': 'This plugin is not loaded, so it cannot be configured right now.',
|
|
217
223
|
'settings.reset': 'Reset to default',
|
|
@@ -249,7 +255,7 @@ export const en: Record<PromptEnhanceKey, string> = {
|
|
|
249
255
|
'settings.row.temperature.label': 'Temperature',
|
|
250
256
|
'settings.row.temperature.hint': 'Sampling temperature (0–1, step 0.05); lower stays closer to the original. Default 0.3.',
|
|
251
257
|
'settings.row.reasoningEffort.label': 'Reasoning effort',
|
|
252
|
-
'settings.row.reasoningEffort.hint': 'Tier choices come from what the configured provider/model declares; custom models are read on demand.
|
|
258
|
+
'settings.row.reasoningEffort.hint': 'Tier choices come from what the configured provider/model declares; custom models are read on demand. Provider default omits the field.',
|
|
253
259
|
'settings.row.maxOutputTokens.label': 'Output token budget',
|
|
254
260
|
'settings.row.maxOutputTokens.hint': 'Output budget for one enhancement call. Default 8192.',
|
|
255
261
|
'settings.row.maxInputChars.label': 'Input character cap',
|
|
@@ -31,7 +31,6 @@ import { fetchReasoningCapabilities } from './reasoning-options'
|
|
|
31
31
|
import { useLlmRouting, type LlmRoutingState } from './llm-catalog'
|
|
32
32
|
import {
|
|
33
33
|
SegmentedControl,
|
|
34
|
-
SettingsForm,
|
|
35
34
|
SettingsFormModel,
|
|
36
35
|
Switch,
|
|
37
36
|
settingsNumberField,
|
|
@@ -163,26 +162,33 @@ interface Row {
|
|
|
163
162
|
* display/context switches first, then routing and generation, then the
|
|
164
163
|
* context window, then the host-side resource caps, and the shortcut last.
|
|
165
164
|
*/
|
|
166
|
-
const
|
|
165
|
+
const BASIC_ROWS: readonly Row[] = [
|
|
167
166
|
{ key: 'enabled', kind: 'boolean', label: 'settings.row.enabled.label', hint: 'settings.row.enabled.hint' },
|
|
168
|
-
{ key: 'contextAware', kind: 'boolean', label: 'settings.row.contextAware.label', hint: 'settings.row.contextAware.hint' },
|
|
169
|
-
{ key: 'streaming', kind: 'boolean', label: 'settings.row.streaming.label', hint: 'settings.row.streaming.hint' },
|
|
170
167
|
{ key: 'provider', kind: 'provider', label: 'settings.row.provider.label', hint: 'settings.row.provider.hint' },
|
|
171
168
|
{ key: 'model', kind: 'model', label: 'settings.row.model.label', hint: 'settings.row.model.hint' },
|
|
172
|
-
{ key: 'temperature', kind: 'number', label: 'settings.row.temperature.label', hint: 'settings.row.temperature.hint' },
|
|
173
169
|
{ key: 'reasoningEffort', kind: 'reasoning', label: 'settings.row.reasoningEffort.label', hint: 'settings.row.reasoningEffort.hint' },
|
|
174
|
-
{ key: '
|
|
170
|
+
{ key: 'temperature', kind: 'number', label: 'settings.row.temperature.label', hint: 'settings.row.temperature.hint' },
|
|
171
|
+
{ key: 'contextAware', kind: 'boolean', label: 'settings.row.contextAware.label', hint: 'settings.row.contextAware.hint' },
|
|
172
|
+
{ key: 'streaming', kind: 'boolean', label: 'settings.row.streaming.label', hint: 'settings.row.streaming.hint' },
|
|
173
|
+
{ key: 'systemPrompt', kind: 'text', label: 'settings.row.systemPrompt.label', hint: 'settings.row.systemPrompt.hint', placeholder: 'settings.row.systemPrompt.placeholder' },
|
|
174
|
+
{ key: 'shortcut', kind: 'text', label: 'settings.row.shortcut.label', hint: 'settings.row.shortcut.hint', placeholder: 'settings.row.shortcut.placeholder' },
|
|
175
|
+
]
|
|
176
|
+
|
|
177
|
+
/** Tuning and deployment limits, hidden behind the Advanced disclosure by default. */
|
|
178
|
+
const ADVANCED_ROWS: readonly Row[] = [
|
|
179
|
+
{ key: 'strategyMode', kind: 'strategy', label: 'settings.row.strategyMode.label', hint: 'settings.row.strategyMode.hint' },
|
|
175
180
|
{ key: 'maxInputChars', kind: 'number', label: 'settings.row.maxInputChars.label', hint: 'settings.row.maxInputChars.hint' },
|
|
181
|
+
{ key: 'maxOutputTokens', kind: 'number', label: 'settings.row.maxOutputTokens.label', hint: 'settings.row.maxOutputTokens.hint' },
|
|
176
182
|
{ key: 'timeoutMs', kind: 'number', label: 'settings.row.timeoutMs.label', hint: 'settings.row.timeoutMs.hint' },
|
|
177
|
-
{ key: 'strategyMode', kind: 'strategy', label: 'settings.row.strategyMode.label', hint: 'settings.row.strategyMode.hint' },
|
|
178
|
-
{ key: 'systemPrompt', kind: 'text', label: 'settings.row.systemPrompt.label', hint: 'settings.row.systemPrompt.hint', placeholder: 'settings.row.systemPrompt.placeholder' },
|
|
179
183
|
{ key: 'contextMaxMessages', kind: 'number', label: 'settings.row.contextMaxMessages.label', hint: 'settings.row.contextMaxMessages.hint' },
|
|
180
184
|
{ key: 'contextMaxChars', kind: 'number', label: 'settings.row.contextMaxChars.label', hint: 'settings.row.contextMaxChars.hint' },
|
|
181
185
|
{ key: 'maxConcurrent', kind: 'number', label: 'settings.row.maxConcurrent.label', hint: 'settings.row.maxConcurrent.hint' },
|
|
182
186
|
{ key: 'rateLimitPerMinute', kind: 'number', label: 'settings.row.rateLimitPerMinute.label', hint: 'settings.row.rateLimitPerMinute.hint' },
|
|
183
|
-
{ key: 'shortcut', kind: 'text', label: 'settings.row.shortcut.label', hint: 'settings.row.shortcut.hint', placeholder: 'settings.row.shortcut.placeholder' },
|
|
184
187
|
]
|
|
185
188
|
|
|
189
|
+
/** Every row both pages edit; the renderer splits it at the Advanced boundary. */
|
|
190
|
+
const ROWS: readonly Row[] = [...BASIC_ROWS, ...ADVANCED_ROWS]
|
|
191
|
+
|
|
186
192
|
/**
|
|
187
193
|
* The staged form's descriptor for one row.
|
|
188
194
|
* @param row - the row to describe.
|
|
@@ -280,8 +286,11 @@ function sectionCopy(t: Translate, chinese: boolean): RowCopy {
|
|
|
280
286
|
unavailable: t('settings.unavailable'),
|
|
281
287
|
readOnly: t('settings.readOnly'),
|
|
282
288
|
saveFailed: t('settings.saveFailed'),
|
|
289
|
+
saveFailedShort: t('settings.saveFailedShort'),
|
|
283
290
|
save: t('settings.save'),
|
|
284
291
|
saving: t('settings.saving'),
|
|
292
|
+
saved: t('settings.saved'),
|
|
293
|
+
advanced: t('settings.advanced'),
|
|
285
294
|
reset: t('settings.reset'),
|
|
286
295
|
invalidNumber: t('settings.invalidNumber'),
|
|
287
296
|
},
|
|
@@ -305,8 +314,11 @@ function pageCopy(t: Translate): RowCopy {
|
|
|
305
314
|
unavailable: t('settings.unavailable'),
|
|
306
315
|
readOnly: t('settings.readOnly'),
|
|
307
316
|
saveFailed: t('settings.saveFailed'),
|
|
317
|
+
saveFailedShort: t('settings.saveFailedShort'),
|
|
308
318
|
save: t('settings.save'),
|
|
309
319
|
saving: t('settings.saving'),
|
|
320
|
+
saved: t('settings.saved'),
|
|
321
|
+
advanced: t('settings.advanced'),
|
|
310
322
|
reset: t('settings.reset'),
|
|
311
323
|
invalidNumber: t('settings.invalidNumber'),
|
|
312
324
|
},
|
|
@@ -353,12 +365,12 @@ const CHIP_STYLE: CSSProperties = {
|
|
|
353
365
|
padding: '1px 8px',
|
|
354
366
|
border: 'none',
|
|
355
367
|
borderRadius: 999,
|
|
356
|
-
background: 'var(--dsw-alias-bg-module-platform, rgba(128, 128, 128, 0.12))',
|
|
368
|
+
background: 'var(--pe-chip-bg, var(--dsw-alias-bg-module-platform, rgba(128, 128, 128, 0.12)))',
|
|
357
369
|
fontFamily: 'inherit',
|
|
358
370
|
fontSize: 11,
|
|
359
371
|
fontWeight: 500,
|
|
360
372
|
lineHeight: '17px',
|
|
361
|
-
color: 'var(--dsw-alias-label-secondary, inherit)',
|
|
373
|
+
color: 'var(--pe-chip-color, var(--dsw-alias-label-secondary, inherit))',
|
|
362
374
|
textDecoration: 'underline',
|
|
363
375
|
textUnderlineOffset: 2,
|
|
364
376
|
cursor: 'pointer',
|
|
@@ -526,6 +538,7 @@ interface FormProps {
|
|
|
526
538
|
resetField?: (key: string) => void
|
|
527
539
|
save?: () => void
|
|
528
540
|
discard?: () => void
|
|
541
|
+
writeField?: (key: string, text: string) => Promise<'saved' | 'invalid' | 'failed'>
|
|
529
542
|
}
|
|
530
543
|
|
|
531
544
|
/** One locale snapshot, as the client locale service exposes it. */
|
|
@@ -577,6 +590,8 @@ interface RowProps {
|
|
|
577
590
|
onEdit: (text: string) => void
|
|
578
591
|
/** Clear the stored value so the row re-inherits the composition default. */
|
|
579
592
|
onReset: () => void
|
|
593
|
+
/** Commit the current text now, e.g. on blur or Enter. */
|
|
594
|
+
onCommit?: () => void
|
|
580
595
|
}
|
|
581
596
|
|
|
582
597
|
/**
|
|
@@ -611,6 +626,18 @@ function viewOf(snapshot: SettingsSnapshot, key: string): FieldView {
|
|
|
611
626
|
* @param text - the row's current staged text.
|
|
612
627
|
* @returns true when editing can no longer change anything from the default.
|
|
613
628
|
*/
|
|
629
|
+
/** The display text a reset row falls back to, from the shared defaults. */
|
|
630
|
+
function defaultTextOf(key: string): string {
|
|
631
|
+
const descriptor = FIELD_SPECS.find((item) => item.field === key)
|
|
632
|
+
const fallback = (DEFAULT_VALUES as Record<string, unknown>)[key]
|
|
633
|
+
if (descriptor === undefined || fallback === undefined) return ''
|
|
634
|
+
try {
|
|
635
|
+
return descriptor.format(fallback)
|
|
636
|
+
} catch {
|
|
637
|
+
return ''
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
614
641
|
function atDefaultValue(key: string, text: string): boolean {
|
|
615
642
|
const fallback = (DEFAULT_VALUES as Record<string, unknown>)[key]
|
|
616
643
|
// provider/model are absent from the defaults on purpose: empty means follow.
|
|
@@ -632,6 +659,7 @@ function OverrideActions(props: { copy: RowCopy; disabled: boolean; onReset: ()
|
|
|
632
659
|
return (
|
|
633
660
|
<button
|
|
634
661
|
type="button"
|
|
662
|
+
className="dsh-pe-chip-reset"
|
|
635
663
|
style={{ ...RESET_STYLE, ...props.disabled ? { opacity: 0.5, cursor: 'default' } : {} }}
|
|
636
664
|
disabled={props.disabled}
|
|
637
665
|
onClick={props.onReset}
|
|
@@ -669,7 +697,7 @@ function BooleanRow(props: RowProps): ReactElement {
|
|
|
669
697
|
* @param props - the row, its choices, its state, and its actions.
|
|
670
698
|
* @returns the row element.
|
|
671
699
|
*/
|
|
672
|
-
function ChoiceRow(props: RowProps & { options: readonly { value: string; label: string }[] }): ReactElement {
|
|
700
|
+
function ChoiceRow(props: RowProps & { options: readonly { value: string; label: string; title?: string }[] }): ReactElement {
|
|
673
701
|
return (
|
|
674
702
|
<div style={ROW_STYLE}>
|
|
675
703
|
<div style={HEAD_STYLE}>
|
|
@@ -678,8 +706,9 @@ function ChoiceRow(props: RowProps & { options: readonly { value: string; label:
|
|
|
678
706
|
</div>
|
|
679
707
|
<SegmentedControl
|
|
680
708
|
id={props.id}
|
|
709
|
+
className="dsh-pe-segmented-labels"
|
|
681
710
|
value={props.view.text}
|
|
682
|
-
options={props.options}
|
|
711
|
+
options={props.options.map((option) => ({ ...option, title: option.title ?? option.label }))}
|
|
683
712
|
disabled={props.disabled}
|
|
684
713
|
label={props.label}
|
|
685
714
|
onChange={(next: string) => props.onEdit(next)}
|
|
@@ -783,11 +812,15 @@ function ReasoningRow(props: RowProps & { provider: string; model: string }): Re
|
|
|
783
812
|
options.push({
|
|
784
813
|
value: effort.id,
|
|
785
814
|
label: effort.name,
|
|
786
|
-
|
|
815
|
+
title: effort.description === undefined ? effort.name : `${effort.name} — ${effort.description}`,
|
|
787
816
|
})
|
|
788
817
|
}
|
|
789
818
|
}
|
|
790
|
-
options.push({
|
|
819
|
+
options.push({
|
|
820
|
+
value: 'inherit',
|
|
821
|
+
label: props.copy.choice('settings.reasoning.inherit'),
|
|
822
|
+
title: props.copy.choice('settings.reasoning.inherit'),
|
|
823
|
+
})
|
|
791
824
|
const unsupported = !noTiers && phase === 'ready' && capabilities?.source !== 'unavailable'
|
|
792
825
|
&& current !== '' && current !== 'inherit' && !realTierKnown
|
|
793
826
|
const refreshing = feedback === 'refreshing'
|
|
@@ -803,6 +836,7 @@ function ReasoningRow(props: RowProps & { provider: string; model: string }): Re
|
|
|
803
836
|
? (
|
|
804
837
|
<button
|
|
805
838
|
type="button"
|
|
839
|
+
className="dsh-pe-chip-refresh"
|
|
806
840
|
style={{ ...REFRESH_STYLE, opacity: props.disabled || refreshing ? 0.5 : 1 }}
|
|
807
841
|
disabled={props.disabled || refreshing}
|
|
808
842
|
onClick={refresh}
|
|
@@ -815,6 +849,7 @@ function ReasoningRow(props: RowProps & { provider: string; model: string }): Re
|
|
|
815
849
|
</div>
|
|
816
850
|
<SegmentedControl
|
|
817
851
|
id={props.id}
|
|
852
|
+
className="dsh-pe-segmented-labels"
|
|
818
853
|
// A route that declares no tiers is presented as "follow provider
|
|
819
854
|
// default"; the stored tier stays in the config and the host omits it
|
|
820
855
|
// on the call, so the displayed state matches what actually happens.
|
|
@@ -995,9 +1030,15 @@ function TextControl(props: {
|
|
|
995
1030
|
invalid?: boolean
|
|
996
1031
|
disabled?: boolean
|
|
997
1032
|
onChange: (value: string) => void
|
|
1033
|
+
onCommit?: () => void
|
|
998
1034
|
}): ReactElement {
|
|
999
1035
|
const [focused, setFocused] = useState(false)
|
|
1000
|
-
const
|
|
1036
|
+
const [revealedInvalid, setRevealedInvalid] = useState(false)
|
|
1037
|
+
useEffect(() => {
|
|
1038
|
+
if (props.invalid !== true) setRevealedInvalid(false)
|
|
1039
|
+
}, [props.invalid])
|
|
1040
|
+
const showInvalid = props.invalid === true && revealedInvalid
|
|
1041
|
+
const borderColor = showInvalid
|
|
1001
1042
|
? 'var(--dsw-alias-state-error-primary, #f85149)'
|
|
1002
1043
|
: focused ? 'var(--dsw-alias-state-business-primary, #4f8cff)' : undefined
|
|
1003
1044
|
return (
|
|
@@ -1005,7 +1046,7 @@ function TextControl(props: {
|
|
|
1005
1046
|
id={props.id}
|
|
1006
1047
|
type="text"
|
|
1007
1048
|
inputMode={props.numeric === true ? 'numeric' : undefined}
|
|
1008
|
-
aria-invalid={
|
|
1049
|
+
aria-invalid={showInvalid || undefined}
|
|
1009
1050
|
disabled={props.disabled}
|
|
1010
1051
|
placeholder={props.placeholder}
|
|
1011
1052
|
value={props.value}
|
|
@@ -1015,7 +1056,17 @@ function TextControl(props: {
|
|
|
1015
1056
|
...borderColor === undefined ? {} : { borderColor },
|
|
1016
1057
|
}}
|
|
1017
1058
|
onFocus={() => setFocused(true)}
|
|
1018
|
-
onBlur={() =>
|
|
1059
|
+
onBlur={() => {
|
|
1060
|
+
setFocused(false)
|
|
1061
|
+
if (props.invalid === true) setRevealedInvalid(true)
|
|
1062
|
+
props.onCommit?.()
|
|
1063
|
+
}}
|
|
1064
|
+
onKeyDown={(event) => {
|
|
1065
|
+
if (event.key !== 'Enter') return
|
|
1066
|
+
event.preventDefault()
|
|
1067
|
+
if (props.invalid === true) setRevealedInvalid(true)
|
|
1068
|
+
props.onCommit?.()
|
|
1069
|
+
}}
|
|
1019
1070
|
onChange={(event) => props.onChange(event.target.value)}
|
|
1020
1071
|
/>
|
|
1021
1072
|
)
|
|
@@ -1037,6 +1088,7 @@ function ValueRow(props: RowProps & { numeric: boolean; placeholder?: string }):
|
|
|
1037
1088
|
invalid={invalid}
|
|
1038
1089
|
disabled={props.disabled}
|
|
1039
1090
|
onChange={props.onEdit}
|
|
1091
|
+
onCommit={props.onCommit}
|
|
1040
1092
|
/>
|
|
1041
1093
|
<p style={HINT_STYLE}>{props.hint}</p>
|
|
1042
1094
|
{invalid ? <p style={INVALID_STYLE}>{props.copy.labels.invalidNumber}</p> : null}
|
|
@@ -1049,7 +1101,7 @@ function ValueRow(props: RowProps & { numeric: boolean; placeholder?: string }):
|
|
|
1049
1101
|
* @param props - the row, its copy, its state, its actions, and the pinned pair.
|
|
1050
1102
|
* @returns the row element.
|
|
1051
1103
|
*/
|
|
1052
|
-
function SettingsRow(props: { row: Row; copy: RowCopy; view: FieldView; disabled: boolean; reasoningPair: { provider: string; model: string }; routing: LlmRoutingState; providerValue: string; onEdit: (text: string) => void; onReset: () => void }): ReactElement {
|
|
1104
|
+
function SettingsRow(props: { row: Row; copy: RowCopy; view: FieldView; disabled: boolean; reasoningPair: { provider: string; model: string }; routing: LlmRoutingState; providerValue: string; onEdit: (text: string) => void; onReset: () => void; onCommit?: () => void }): ReactElement {
|
|
1053
1105
|
const shared = {
|
|
1054
1106
|
id: `dsh-prompt-enhance-${props.row.key}`,
|
|
1055
1107
|
label: props.copy.title(props.row),
|
|
@@ -1059,6 +1111,7 @@ function SettingsRow(props: { row: Row; copy: RowCopy; view: FieldView; disabled
|
|
|
1059
1111
|
disabled: props.disabled,
|
|
1060
1112
|
onEdit: props.onEdit,
|
|
1061
1113
|
onReset: props.onReset,
|
|
1114
|
+
onCommit: props.onCommit,
|
|
1062
1115
|
}
|
|
1063
1116
|
switch (props.row.kind) {
|
|
1064
1117
|
case 'boolean':
|
|
@@ -1088,36 +1141,205 @@ function SettingsRows(props: {
|
|
|
1088
1141
|
resetField: (key: string) => void
|
|
1089
1142
|
save?: () => void
|
|
1090
1143
|
discard?: () => void
|
|
1144
|
+
writeField?: (key: string, text: string) => Promise<'saved' | 'invalid' | 'failed'>
|
|
1091
1145
|
}): ReactElement {
|
|
1092
1146
|
// The reasoning row reads the staged pair so options follow the fields before
|
|
1093
1147
|
// they are saved (typing a custom model refreshes them on its own).
|
|
1094
1148
|
const provider = viewOf(props.state, 'provider').text.trim()
|
|
1095
1149
|
const model = viewOf(props.state, 'model').text.trim()
|
|
1096
1150
|
const routing = useLlmRouting(provider, model)
|
|
1151
|
+
const [saveState, setSaveState] = useState<'idle' | 'saving' | 'saved' | 'failed'>('idle')
|
|
1152
|
+
const timers = useRef(new Map<string, ReturnType<typeof setTimeout>>())
|
|
1153
|
+
const savedTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
|
1154
|
+
const savedValues = useRef(new Map<string, string>())
|
|
1155
|
+
const sequence = useRef(0)
|
|
1156
|
+
|
|
1157
|
+
useEffect(() => {
|
|
1158
|
+
for (const row of ROWS) {
|
|
1159
|
+
if (!savedValues.current.has(row.key)) {
|
|
1160
|
+
savedValues.current.set(row.key, viewOf(props.state, row.key).text)
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}, [props.state])
|
|
1164
|
+
|
|
1165
|
+
const runSave = async (key: string, text: string): Promise<void> => {
|
|
1166
|
+
if (props.writeField === undefined) return
|
|
1167
|
+
// A focus/blur with no edit, or an edit reverted to the stored value, is
|
|
1168
|
+
// not a write: skip it without touching the footer status.
|
|
1169
|
+
if (savedValues.current.get(key) === text) return
|
|
1170
|
+
const ticket = ++sequence.current
|
|
1171
|
+
setSaveState('saving')
|
|
1172
|
+
const result = await props.writeField(key, text)
|
|
1173
|
+
if (ticket !== sequence.current) return
|
|
1174
|
+
if (result === 'invalid') {
|
|
1175
|
+
setSaveState('idle')
|
|
1176
|
+
return
|
|
1177
|
+
}
|
|
1178
|
+
if (result === 'failed') {
|
|
1179
|
+
setSaveState('failed')
|
|
1180
|
+
return
|
|
1181
|
+
}
|
|
1182
|
+
savedValues.current.set(key, text)
|
|
1183
|
+
markSaved()
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
const markSaved = (): void => {
|
|
1187
|
+
setSaveState('saved')
|
|
1188
|
+
if (savedTimer.current !== undefined) clearTimeout(savedTimer.current)
|
|
1189
|
+
savedTimer.current = setTimeout(() => {
|
|
1190
|
+
setSaveState('idle')
|
|
1191
|
+
savedTimer.current = undefined
|
|
1192
|
+
}, 2000)
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
const runReset = async (key: string, fallback: string): Promise<void> => {
|
|
1196
|
+
if (props.writeField === undefined) return
|
|
1197
|
+
const ticket = ++sequence.current
|
|
1198
|
+
setSaveState('saving')
|
|
1199
|
+
const result = await props.writeField(key, '')
|
|
1200
|
+
if (ticket !== sequence.current) return
|
|
1201
|
+
if (result === 'failed') {
|
|
1202
|
+
setSaveState('failed')
|
|
1203
|
+
return
|
|
1204
|
+
}
|
|
1205
|
+
if (result === 'invalid') {
|
|
1206
|
+
setSaveState('idle')
|
|
1207
|
+
return
|
|
1208
|
+
}
|
|
1209
|
+
savedValues.current.set(key, fallback)
|
|
1210
|
+
markSaved()
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
const resetAndSave = (key: string): void => {
|
|
1214
|
+
props.resetField(key)
|
|
1215
|
+
const fallback = defaultTextOf(key)
|
|
1216
|
+
savedValues.current.set(key, fallback)
|
|
1217
|
+
void runReset(key, fallback)
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
const manualSave = async (): Promise<void> => {
|
|
1221
|
+
if (props.writeField === undefined) {
|
|
1222
|
+
// Hosts without per-field writes keep the original whole-form action.
|
|
1223
|
+
props.save?.()
|
|
1224
|
+
markSaved()
|
|
1225
|
+
return
|
|
1226
|
+
}
|
|
1227
|
+
const changed = ROWS
|
|
1228
|
+
.map((row) => ({ key: row.key, text: viewOf(props.state, row.key).text }))
|
|
1229
|
+
.filter((item) => savedValues.current.get(item.key) !== item.text)
|
|
1230
|
+
if (changed.length === 0) {
|
|
1231
|
+
markSaved()
|
|
1232
|
+
return
|
|
1233
|
+
}
|
|
1234
|
+
const ticket = ++sequence.current
|
|
1235
|
+
setSaveState('saving')
|
|
1236
|
+
let failed = false
|
|
1237
|
+
let invalid = false
|
|
1238
|
+
for (const item of changed) {
|
|
1239
|
+
const result = await props.writeField(item.key, item.text)
|
|
1240
|
+
if (result === 'saved') savedValues.current.set(item.key, item.text)
|
|
1241
|
+
else if (result === 'failed') failed = true
|
|
1242
|
+
else invalid = true
|
|
1243
|
+
}
|
|
1244
|
+
if (ticket !== sequence.current) return
|
|
1245
|
+
if (failed) setSaveState('failed')
|
|
1246
|
+
else if (invalid && changed.every((item) => savedValues.current.get(item.key) !== item.text)) setSaveState('idle')
|
|
1247
|
+
else markSaved()
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
const scheduleSave = (key: string, text: string, immediate: boolean): void => {
|
|
1251
|
+
const pending = timers.current.get(key)
|
|
1252
|
+
if (pending !== undefined) clearTimeout(pending)
|
|
1253
|
+
if (immediate) {
|
|
1254
|
+
timers.current.delete(key)
|
|
1255
|
+
void runSave(key, text)
|
|
1256
|
+
return
|
|
1257
|
+
}
|
|
1258
|
+
const timer = setTimeout(() => {
|
|
1259
|
+
timers.current.delete(key)
|
|
1260
|
+
void runSave(key, text)
|
|
1261
|
+
}, 800)
|
|
1262
|
+
timers.current.set(key, timer)
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
const onEdit = (key: string, text: string): void => {
|
|
1266
|
+
props.edit(key, text)
|
|
1267
|
+
const row = ROWS.find((item) => item.key === key)
|
|
1268
|
+
const debounced = row !== undefined && (row.kind === 'text' || row.kind === 'number')
|
|
1269
|
+
scheduleSave(key, text, !debounced)
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
const flushField = (key: string, text?: string): void => {
|
|
1273
|
+
const pending = timers.current.get(key)
|
|
1274
|
+
if (pending !== undefined) {
|
|
1275
|
+
clearTimeout(pending)
|
|
1276
|
+
timers.current.delete(key)
|
|
1277
|
+
}
|
|
1278
|
+
void runSave(key, text ?? viewOf(props.state, key).text)
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
useEffect(() => () => {
|
|
1282
|
+
for (const timer of timers.current.values()) clearTimeout(timer)
|
|
1283
|
+
timers.current.clear()
|
|
1284
|
+
if (savedTimer.current !== undefined) clearTimeout(savedTimer.current)
|
|
1285
|
+
}, [])
|
|
1286
|
+
|
|
1287
|
+
const statusText = props.state.writable === false
|
|
1288
|
+
? props.copy.labels.readOnly
|
|
1289
|
+
: saveState === 'saving'
|
|
1290
|
+
? props.copy.labels.saving
|
|
1291
|
+
: saveState === 'saved'
|
|
1292
|
+
? props.copy.labels.saved
|
|
1293
|
+
: saveState === 'failed'
|
|
1294
|
+
? props.copy.labels.saveFailedShort
|
|
1295
|
+
: ''
|
|
1296
|
+
|
|
1297
|
+
const renderRow = (row: Row, index: number): ReactElement => {
|
|
1298
|
+
const raw = viewOf(props.state, row.key)
|
|
1299
|
+
// Hide the override badge once the value is back at its default; the
|
|
1300
|
+
// host's `overridden` flag alone would keep it visible.
|
|
1301
|
+
const view = raw.overridden && atDefaultValue(row.key, raw.text) ? { ...raw, overridden: false } : raw
|
|
1302
|
+
return (
|
|
1303
|
+
<div key={row.key} style={index === 0 ? undefined : SEPARATOR_STYLE}>
|
|
1304
|
+
<SettingsRow
|
|
1305
|
+
row={row}
|
|
1306
|
+
copy={props.copy}
|
|
1307
|
+
view={view}
|
|
1308
|
+
disabled={!props.state.writable}
|
|
1309
|
+
reasoningPair={{ provider, model }}
|
|
1310
|
+
routing={routing}
|
|
1311
|
+
providerValue={provider}
|
|
1312
|
+
onEdit={(text: string) => onEdit(row.key, text)}
|
|
1313
|
+
onReset={() => resetAndSave(row.key)}
|
|
1314
|
+
onCommit={() => flushField(row.key, viewOf(props.state, row.key).text)}
|
|
1315
|
+
/>
|
|
1316
|
+
</div>
|
|
1317
|
+
)
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1097
1320
|
return (
|
|
1098
|
-
<
|
|
1099
|
-
{
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
</SettingsForm>
|
|
1321
|
+
<div className="dsh-pe-form">
|
|
1322
|
+
{BASIC_ROWS.map(renderRow)}
|
|
1323
|
+
<details className="dsh-pe-advanced">
|
|
1324
|
+
<summary>{props.copy.labels.advanced}</summary>
|
|
1325
|
+
{ADVANCED_ROWS.map(renderRow)}
|
|
1326
|
+
</details>
|
|
1327
|
+
<div className="dsh-pe-form-footer">
|
|
1328
|
+
<div className="dsh-pe-save-group">
|
|
1329
|
+
{statusText === ''
|
|
1330
|
+
? null
|
|
1331
|
+
: <span className="dsh-pe-save-status" role="status" aria-live="polite">{statusText}</span>}
|
|
1332
|
+
<button
|
|
1333
|
+
type="button"
|
|
1334
|
+
className="dsh-pe-save-button"
|
|
1335
|
+
disabled={props.save === undefined || props.state.writable === false}
|
|
1336
|
+
onClick={() => { void manualSave() }}
|
|
1337
|
+
>
|
|
1338
|
+
{props.copy.labels.save}
|
|
1339
|
+
</button>
|
|
1340
|
+
</div>
|
|
1341
|
+
</div>
|
|
1342
|
+
</div>
|
|
1121
1343
|
)
|
|
1122
1344
|
}
|
|
1123
1345
|
|
|
@@ -1153,6 +1375,7 @@ export function SettingsSection(props: SectionProps): unknown {
|
|
|
1153
1375
|
resetField={resetField}
|
|
1154
1376
|
save={props.save}
|
|
1155
1377
|
discard={props.discard}
|
|
1378
|
+
writeField={props.writeField}
|
|
1156
1379
|
/>
|
|
1157
1380
|
</div>
|
|
1158
1381
|
)
|
|
@@ -1185,6 +1408,7 @@ export function SettingsPage(props: PageProps): unknown {
|
|
|
1185
1408
|
resetField={resetField}
|
|
1186
1409
|
save={props.save}
|
|
1187
1410
|
discard={props.discard}
|
|
1411
|
+
writeField={props.writeField}
|
|
1188
1412
|
/>
|
|
1189
1413
|
)
|
|
1190
1414
|
}
|
|
@@ -1193,6 +1417,8 @@ export function SettingsPage(props: PageProps): unknown {
|
|
|
1193
1417
|
interface ConfigScopeLike {
|
|
1194
1418
|
getSnapshot?: () => unknown
|
|
1195
1419
|
subscribe?: (listener: () => void) => () => void
|
|
1420
|
+
set?: (field: string, value: unknown) => Promise<boolean>
|
|
1421
|
+
unset?: (field: string) => Promise<boolean>
|
|
1196
1422
|
}
|
|
1197
1423
|
|
|
1198
1424
|
/** Structural face of the host's config-forms provider. */
|
|
@@ -1210,6 +1436,7 @@ interface SlotsFace {
|
|
|
1210
1436
|
/** Bridges one settings scope onto both pages' staged form. */
|
|
1211
1437
|
class SettingsPageController {
|
|
1212
1438
|
private readonly form: SettingsFormModel
|
|
1439
|
+
private readonly scope: ConfigScopeLike
|
|
1213
1440
|
private readonly store: unknown
|
|
1214
1441
|
private readonly getLocale: (() => LocaleFaceLike | undefined) | undefined
|
|
1215
1442
|
|
|
@@ -1219,6 +1446,7 @@ class SettingsPageController {
|
|
|
1219
1446
|
*/
|
|
1220
1447
|
constructor(scope: ConfigScopeLike, getLocale?: () => LocaleFaceLike | undefined) {
|
|
1221
1448
|
this.getLocale = getLocale
|
|
1449
|
+
this.scope = scope
|
|
1222
1450
|
this.form = createSettingsForm(scope)
|
|
1223
1451
|
this.store = this.form.bind(() => {
|
|
1224
1452
|
const out = this.form.shell()
|
|
@@ -1236,7 +1464,11 @@ class SettingsPageController {
|
|
|
1236
1464
|
/** The props the slots hand their pages. */
|
|
1237
1465
|
inject(): Record<string, unknown> {
|
|
1238
1466
|
const face = this.getLocale?.()
|
|
1239
|
-
const props: Record<string, unknown> = {
|
|
1467
|
+
const props: Record<string, unknown> = {
|
|
1468
|
+
hooks: { promptEnhance: this.store },
|
|
1469
|
+
...this.form.actions(),
|
|
1470
|
+
writeField: (key: string, text: string) => this.writeField(key, text),
|
|
1471
|
+
}
|
|
1240
1472
|
if (face !== undefined) {
|
|
1241
1473
|
// A small observable the slot turns into `usePromptEnhanceLocale`; the
|
|
1242
1474
|
// page reads the active language from it and re-renders on a switch.
|
|
@@ -1249,6 +1481,26 @@ class SettingsPageController {
|
|
|
1249
1481
|
return props
|
|
1250
1482
|
}
|
|
1251
1483
|
|
|
1484
|
+
/** Write one field through the host's per-field mutation API. */
|
|
1485
|
+
private async writeField(key: string, text: string): Promise<'saved' | 'invalid' | 'failed'> {
|
|
1486
|
+
const descriptor = FIELD_SPECS.find((item) => item.field === key)
|
|
1487
|
+
if (descriptor === undefined) return 'failed'
|
|
1488
|
+
const parsed = descriptor.parse(text)
|
|
1489
|
+
if (parsed === undefined) return 'invalid'
|
|
1490
|
+
if (this.scope.set === undefined) return 'failed'
|
|
1491
|
+
try {
|
|
1492
|
+
if (parsed.kind === 'clear') {
|
|
1493
|
+
if (this.scope.unset === undefined) return 'failed'
|
|
1494
|
+
if (!(await this.scope.unset(key))) return 'failed'
|
|
1495
|
+
} else {
|
|
1496
|
+
if (!(await this.scope.set(key, parsed.value))) return 'failed'
|
|
1497
|
+
}
|
|
1498
|
+
return 'saved'
|
|
1499
|
+
} catch {
|
|
1500
|
+
return 'failed'
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1252
1504
|
/** Release the form's subscriptions. */
|
|
1253
1505
|
dispose(): void {
|
|
1254
1506
|
try {
|