@surfishs/dsh-prompt-enhance 0.1.0 → 0.1.2

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.
@@ -21,6 +21,7 @@
21
21
  */
22
22
 
23
23
  import type { Context as ClientContext } from '@deepseek-ai/cordis'
24
+ import { getSettingsWriter, setSettingsWriter } from './settings-writer'
24
25
 
25
26
  /** Snapshot face shared by every generation's form object. */
26
27
  interface FormSnapshotLike {
@@ -33,6 +34,9 @@ interface BoundFormLike {
33
34
  getSnapshot?: () => FormSnapshotLike | unknown
34
35
  subscribe?: (listener: () => void) => (() => void) | undefined
35
36
  dispose?: () => unknown
37
+ /** Present on hosts that allow writing this form (the settings page uses it). */
38
+ set?: (field: string, value: unknown) => Promise<boolean>
39
+ unset?: (field: string) => Promise<boolean>
36
40
  }
37
41
 
38
42
  /** The shared describe mirror's read face (`ctx.configForms.describe()`). */
@@ -95,6 +99,22 @@ function servedNamespaces(forms: ConfigFormsLike): readonly string[] {
95
99
  * @param ctx - the browser half's context.
96
100
  * @param options - candidate row ids and the section sink.
97
101
  */
102
+ /**
103
+ * Adopt the form's write methods when it has them. The settings page writes
104
+ * through the same object, so a form without set is a host where the
105
+ * composer cannot save — the quick-settings entry then stays hidden.
106
+ * @param form - the bound form of one source.
107
+ * @param highest - whether this source outranks any writer already installed.
108
+ */
109
+ function adoptWriter(form: BoundFormLike, highest: boolean): void {
110
+ if (typeof form.set !== 'function' || typeof form.unset !== 'function') return
111
+ if (!highest && getSettingsWriter() !== undefined) return
112
+ setSettingsWriter({
113
+ set: (field, value) => form.set?.(field, value) ?? Promise.resolve(false),
114
+ unset: (field) => form.unset?.(field) ?? Promise.resolve(false),
115
+ })
116
+ }
117
+
98
118
  export function installClientSettingsSource(ctx: ClientContext, options: ClientSettingsSourceOptions): void {
99
119
  let rank: number = RANK.none
100
120
  const adopt = (next: number, section: unknown): void => {
@@ -169,6 +189,7 @@ export function installClientSettingsSource(ctx: ClientContext, options: ClientS
169
189
  unsubscribeForm = undefined
170
190
  boundId = entryId
171
191
  adoptSnapshot(RANK.configForms, form)
192
+ adoptWriter(form, true)
172
193
  try {
173
194
  unsubscribeForm = form.subscribe?.(() => adoptSnapshot(RANK.configForms, form as BoundFormLike))
174
195
  } catch {
@@ -225,6 +246,7 @@ export function installClientSettingsSource(ctx: ClientContext, options: ClientS
225
246
  }
226
247
  if (bound === undefined) return
227
248
  adoptSnapshot(RANK.binder, bound)
249
+ adoptWriter(bound, false)
228
250
  let unsubscribe: (() => void) | undefined
229
251
  try {
230
252
  unsubscribe = bound.subscribe?.(() => adoptSnapshot(RANK.binder, bound as BoundFormLike))
@@ -259,6 +281,7 @@ export function installClientSettingsSource(ctx: ClientContext, options: ClientS
259
281
  return
260
282
  }
261
283
  adoptSnapshot(RANK.legacyScope, bound)
284
+ adoptWriter(bound, false)
262
285
  let unsubscribe: (() => void) | undefined
263
286
  try {
264
287
  unsubscribe = bound.subscribe?.(() => adoptSnapshot(RANK.legacyScope, bound))
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The plugin's own settings, written from the composer half.
3
+ *
4
+ * The settings page writes through props the host hands to that page; the
5
+ * composer slot gets no such props. This module is the one shared seam: the
6
+ * settings source installs a writer when the host exposes one, and any
7
+ * composer-side control asks for it here. When no writer is installed the
8
+ * quick-settings entry stays hidden — a menu that cannot save is worse than no
9
+ * menu at all.
10
+ * @module dsh-prompt-enhance/client/settings-writer
11
+ */
12
+
13
+ /** One write surface of the plugin's config section. */
14
+ export interface SettingsWriter {
15
+ /** Stage a value; resolves true once the host accepted it. */
16
+ set(field: string, value: unknown): Promise<boolean>
17
+ /** Clear a value back to its default. */
18
+ unset(field: string): Promise<boolean>
19
+ }
20
+
21
+ let current: SettingsWriter | undefined
22
+ const listeners = new Set<() => void>()
23
+
24
+ /**
25
+ * Install (or clear) the writer. Called by the settings source as it binds a
26
+ * form; tests call it directly.
27
+ * @param next - the writer, or undefined when the host offers none.
28
+ */
29
+ export function setSettingsWriter(next: SettingsWriter | undefined): void {
30
+ if (current === next) return
31
+ current = next
32
+ for (const listener of listeners) listener()
33
+ }
34
+
35
+ /** The installed writer, when the host can write settings from here. */
36
+ export function getSettingsWriter(): SettingsWriter | undefined {
37
+ return current
38
+ }
39
+
40
+ /**
41
+ * Subscribe to writer replacement, so a control can appear as soon as the host
42
+ * binding settles (it resolves asynchronously after apply).
43
+ * @param listener - notified after each replacement.
44
+ * @returns the unsubscribe function.
45
+ */
46
+ export function subscribeSettingsWriter(listener: () => void): () => void {
47
+ listeners.add(listener)
48
+ return () => {
49
+ listeners.delete(listener)
50
+ }
51
+ }
@@ -20,6 +20,18 @@ export interface ClientSettings {
20
20
  shortcut: string
21
21
  /** Show the model's output as it streams (host may still fall back). */
22
22
  streaming: boolean
23
+ /** Play the result card's expand/collapse transition. */
24
+ foldAnimation: boolean
25
+ /** Fold the card after Apply and re-open it after Undo. */
26
+ autoFold: boolean
27
+ /** Configured reasoning effort; inherit omits the field on the call. */
28
+ reasoningEffort: string
29
+ /** Read the conversation's recent turns to ground the rewrite. */
30
+ contextAware: boolean
31
+ /** Pinned provider override (empty follows the session). */
32
+ provider: string
33
+ /** Pinned model override (empty follows the session). */
34
+ model: string
23
35
  }
24
36
 
25
37
  // Compile-time guarantee: the mirror only tracks keys that exist in the host
@@ -34,6 +46,12 @@ export const DEFAULT_CLIENT_SETTINGS: ClientSettings = {
34
46
  maxInputChars: DEFAULT_VALUES.maxInputChars,
35
47
  shortcut: DEFAULT_VALUES.shortcut,
36
48
  streaming: DEFAULT_VALUES.streaming,
49
+ foldAnimation: DEFAULT_VALUES.foldAnimation,
50
+ autoFold: DEFAULT_VALUES.autoFold,
51
+ reasoningEffort: DEFAULT_VALUES.reasoningEffort,
52
+ contextAware: DEFAULT_VALUES.contextAware,
53
+ provider: '',
54
+ model: '',
37
55
  }
38
56
 
39
57
  const listeners = new Set<() => void>()
@@ -80,5 +98,11 @@ export function decodeClientSettings(section: unknown): ClientSettings {
80
98
  typeof record.maxInputChars === 'number' && Number.isFinite(record.maxInputChars) && record.maxInputChars > 0 ? Math.floor(record.maxInputChars) : DEFAULT_CLIENT_SETTINGS.maxInputChars,
81
99
  shortcut: typeof record.shortcut === 'string' ? record.shortcut : DEFAULT_CLIENT_SETTINGS.shortcut,
82
100
  streaming: typeof record.streaming === 'boolean' ? record.streaming : DEFAULT_CLIENT_SETTINGS.streaming,
101
+ foldAnimation: typeof record.foldAnimation === 'boolean' ? record.foldAnimation : DEFAULT_CLIENT_SETTINGS.foldAnimation,
102
+ autoFold: typeof record.autoFold === 'boolean' ? record.autoFold : DEFAULT_CLIENT_SETTINGS.autoFold,
103
+ reasoningEffort: typeof record.reasoningEffort === 'string' ? record.reasoningEffort : DEFAULT_CLIENT_SETTINGS.reasoningEffort,
104
+ contextAware: typeof record.contextAware === 'boolean' ? record.contextAware : DEFAULT_CLIENT_SETTINGS.contextAware,
105
+ provider: typeof record.provider === 'string' ? record.provider : DEFAULT_CLIENT_SETTINGS.provider,
106
+ model: typeof record.model === 'string' ? record.model : DEFAULT_CLIENT_SETTINGS.model,
83
107
  }
84
108
  }
@@ -94,3 +94,48 @@ export function matchesShortcut(event: KeyboardEvent, combo: ShortcutCombo | nul
94
94
  event.ctrlKey === combo.ctrl && event.altKey === combo.alt && event.shiftKey === combo.shift && event.metaKey === combo.meta && (event.code === combo.code || event.key.toLowerCase() === combo.key)
95
95
  )
96
96
  }
97
+
98
+ /** The slice of a keyboard event the recorder reads; structural so tests can pass plain objects. */
99
+ export interface ShortcutKeyEvent {
100
+ readonly code: string
101
+ readonly key: string
102
+ readonly ctrlKey: boolean
103
+ readonly altKey: boolean
104
+ readonly shiftKey: boolean
105
+ readonly metaKey: boolean
106
+ }
107
+
108
+ /** The settings spec for one recorded press, or null when it cannot be a shortcut. */
109
+ function keyTokenOf(event: ShortcutKeyEvent): string | null {
110
+ const letter = /^Key([A-Z])$/.exec(event.code)?.[1]
111
+ if (letter !== undefined) return letter.toLowerCase()
112
+ const digit = /^Digit([0-9])$/.exec(event.code)?.[1]
113
+ if (digit !== undefined) return digit
114
+ const fn = /^F([1-9]|1[0-2])$/.exec(event.code)?.[1]
115
+ if (fn !== undefined) return `f${fn}`
116
+ const byKey = event.key.toLowerCase()
117
+ return isKeyToken(byKey) ? byKey : null
118
+ }
119
+
120
+ /**
121
+ * Render one recorded key press as a settings spec (`ctrl+alt+e`).
122
+ *
123
+ * Returns null — meaning "this press cannot be a shortcut" — when neither
124
+ * ctrl / alt / meta is held (a bare key or shift+letter would swallow ordinary
125
+ * typing), or when the key falls outside the a-z / 0-9 / f1-f12 whitelist the
126
+ * parser accepts (Space, Tab, arrows …).
127
+ * @param event - the recorded key press.
128
+ * @returns the spec string, or null.
129
+ */
130
+ export function shortcutSpecOf(event: ShortcutKeyEvent): string | null {
131
+ if (!event.ctrlKey && !event.altKey && !event.metaKey) return null
132
+ const token = keyTokenOf(event)
133
+ if (token === null) return null
134
+ const parts: string[] = []
135
+ if (event.ctrlKey) parts.push('ctrl')
136
+ if (event.altKey) parts.push('alt')
137
+ if (event.shiftKey) parts.push('shift')
138
+ if (event.metaKey) parts.push('meta')
139
+ parts.push(token)
140
+ return parts.join('+')
141
+ }