dsh-better-ask-user 0.1.7 → 0.3.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.
@@ -1,7 +1,8 @@
1
1
  /** Row configuration page: question wait and timeout behavior. */
2
- import { useCallback, useState } from 'react'
2
+ import { useCallback, useId, useState, type ReactNode } from 'react'
3
3
  import type { SettingsKey } from './settings-locales.ts'
4
4
  import { settingsEn } from './settings-locales.ts'
5
+ import css from './settings.module.css'
5
6
 
6
7
  const DEFAULTS = { questionTimeoutSeconds: 120, timeoutBehavior: 'native', goalTimeoutBehavior: 'native' } as const
7
8
  const MAX_QUESTION_TIMEOUT_SECONDS = 2147483
@@ -63,11 +64,46 @@ function parseDraft(draft: Draft): ConfigValue | undefined {
63
64
  return { questionTimeoutSeconds: seconds, timeoutBehavior: draft.timeoutBehavior, goalTimeoutBehavior: draft.goalTimeoutBehavior }
64
65
  }
65
66
 
66
- const fieldStyle = { display: 'grid', gap: 6, marginBottom: 18 } as const
67
- const labelStyle = { fontWeight: 600, fontSize: 13 } as const
68
- const hintStyle = { margin: 0, fontSize: 12, opacity: 0.65, lineHeight: 1.45 } as const
69
- const inputStyle = { maxWidth: 320, padding: '7px 9px', borderRadius: 8, border: '1px solid rgba(127,127,127,.35)', background: 'transparent', color: 'inherit', font: 'inherit' } as const
70
- const statusStyle = { fontSize: 12, lineHeight: 1.5 } as const
67
+ /** Compact settings row with an optional dependency description. */
68
+ function Row({ id, label, hint, children }: { id: string; label: string; hint?: string; children: ReactNode }) {
69
+ return (
70
+ <div className={css.row}>
71
+ <div className={css.rowText}>
72
+ <span className={css.rowTitle} id={`${id}-label`}>{label}</span>
73
+ {hint ? <span className={css.rowDesc} id={`${id}-hint`}>{hint}</span> : null}
74
+ </div>
75
+ <div className={css.control}>{children}</div>
76
+ </div>
77
+ )
78
+ }
79
+
80
+ /** Native radio semantics with one compact, fully styled choice surface. */
81
+ function Choices({ id, value, options, disabled, describedBy, onChange }: {
82
+ id: string
83
+ value: string
84
+ options: Array<{ value: string; label: string }>
85
+ disabled: boolean
86
+ describedBy?: string
87
+ onChange: (value: string) => void
88
+ }) {
89
+ const name = useId()
90
+ return (
91
+ <div className={css.choices} id={id} role="radiogroup" aria-labelledby={`${id}-label`} aria-disabled={disabled}>
92
+ {options.map((option) => (
93
+ <label className={css.choice} key={option.value}>
94
+ <input
95
+ id={`${id}-${option.value}`}
96
+ type="radio" name={name} value={option.value}
97
+ checked={value === option.value} disabled={disabled}
98
+ aria-describedby={describedBy}
99
+ onChange={() => { onChange(option.value) }}
100
+ />
101
+ <span>{option.label}</span>
102
+ </label>
103
+ ))}
104
+ </div>
105
+ )
106
+ }
71
107
 
72
108
  /** Page body, rendered inside the settings host's row config page. */
73
109
  export function SettingsPage({ form, t = fallbackT }: { form?: any; t?: (key: SettingsKey) => string }) {
@@ -77,12 +113,13 @@ export function SettingsPage({ form, t = fallbackT }: { form?: any; t?: (key: Se
77
113
  const base = host === null ? null : toDraft(host)
78
114
  const [draft, setDraft] = useState<DraftState | null>(null)
79
115
  const [busy, setBusy] = useState(false)
116
+ const [timedSeconds, setTimedSeconds] = useState(String(DEFAULTS.questionTimeoutSeconds))
80
117
  const [notice, setNotice] = useState<{ tone: 'ok' | 'error'; text: string } | null>(null)
81
118
 
82
119
  const live = draft !== null && draft.revision === revision ? draft.value : base
83
120
  const parsed = live === null ? undefined : parseDraft(live)
84
121
  const writable = state?.status === 'ready' && state?.writable === true
85
- const dirty = host !== null && parsed !== undefined && !same(host, parsed)
122
+ const dirty = host !== null && (parsed === undefined || !same(host, parsed))
86
123
 
87
124
  const edit = useCallback((mutate: (baseDraft: Draft) => Draft) => {
88
125
  setNotice(null)
@@ -112,7 +149,7 @@ export function SettingsPage({ form, t = fallbackT }: { form?: any; t?: (key: Se
112
149
  try {
113
150
  const accepted = await form.mutate(ops, revision)
114
151
  if (accepted) setNotice({ tone: 'ok', text: t('saved') })
115
- else { setDraft(null); setNotice({ tone: 'error', text: t('conflict') }) }
152
+ else setNotice({ tone: 'error', text: t('conflict') })
116
153
  } catch (error) {
117
154
  setNotice({ tone: 'error', text: t('saveFailed') + String((error as any)?.message ?? error) })
118
155
  } finally {
@@ -121,96 +158,95 @@ export function SettingsPage({ form, t = fallbackT }: { form?: any; t?: (key: Se
121
158
  }, [busy, dirty, form, host, parsed, revision, t, writable])
122
159
 
123
160
  if (state === undefined) {
124
- return <div style={{ padding: 4 }}><p style={statusStyle}>{t('waiting')}</p></div>
161
+ return <div className={css.cfg}><p className={css.note} role="status">{t('waiting')}</p></div>
125
162
  }
126
163
  if (state.status === 'loading') {
127
- return <div style={{ padding: 4 }}><p style={statusStyle}>{t('loading')}</p></div>
164
+ return <div className={css.cfg}><p className={css.note} role="status">{t('loading')}</p></div>
128
165
  }
129
166
  if (host === null || live === null) {
130
- return <div style={{ padding: 4 }}><p style={statusStyle}>{t('unavailable')}</p></div>
167
+ return <div className={css.cfg}><p className={css.note} role="alert">{t('unavailable')}</p></div>
131
168
  }
132
169
 
133
170
  const invalid = parsed === undefined
134
- const status = notice !== null
135
- ? <span style={{ ...statusStyle, color: notice.tone === 'ok' ? '#2f8f4e' : '#c0392b' }}>{notice.text}</span>
136
- : invalid
137
- ? <span style={{ ...statusStyle, color: '#c0392b' }}>{t('invalidWait')}</span>
138
- : dirty
139
- ? <span style={statusStyle}>{t('dirty')}</span>
140
- : null
171
+ const unlimited = live.questionTimeoutSeconds === '-1'
172
+ const controlsDisabled = !writable || busy
173
+ const behaviors = [{ value: 'native', label: t('native') }, { value: 'default', label: t('default') }]
141
174
 
142
175
  return (
143
- <div style={{ padding: '4px 2px', maxWidth: 720 }}>
144
- <h3 style={{ margin: '0 0 6px', fontSize: 16 }}>{t('title')}</h3>
145
- <p style={{ ...hintStyle, marginBottom: 18 }}>{t('intro')}</p>
146
- {writable ? null : <p style={{ ...statusStyle, marginBottom: 14 }}>{t('readonly')}</p>}
147
-
148
- <div style={fieldStyle}>
149
- <label style={labelStyle} htmlFor="better-ask-user-wait">{t('waitLabel')}</label>
150
- <input
151
- id="better-ask-user-wait"
152
- type="number"
153
- min={-1}
154
- max={MAX_QUESTION_TIMEOUT_SECONDS}
155
- step={1}
156
- style={inputStyle}
157
- value={live.questionTimeoutSeconds}
158
- disabled={!writable || busy}
159
- onChange={(event) => {
160
- const next = event.target.value
161
- edit((current) => ({ ...current, questionTimeoutSeconds: next }))
162
- }}
163
- />
164
- <p style={hintStyle}>{t('waitHint')}</p>
176
+ <div className={css.cfg} aria-busy={busy} data-settings-dirty={dirty} data-settings-busy={busy}>
177
+ <div className={css.pageHead}>
178
+ <h2 className={css.title} id="better-ask-user-title">{t('title')}</h2>
179
+ {writable ? null : <span className={css.badge} role="status">{t('readonly')}</span>}
165
180
  </div>
166
-
167
- <div style={fieldStyle}>
168
- <label style={labelStyle} htmlFor="better-ask-user-behavior">{t('behaviorLabel')}</label>
169
- <select
170
- id="better-ask-user-behavior"
171
- style={inputStyle}
172
- value={live.timeoutBehavior}
173
- disabled={!writable || busy}
174
- onChange={(event) => {
175
- const next = event.target.value === 'default' ? 'default' : 'native'
176
- edit((current) => ({ ...current, timeoutBehavior: next }))
177
- }}
178
- >
179
- <option value="native">{t('native')}</option>
180
- <option value="default">{t('default')}</option>
181
- </select>
182
- <p style={hintStyle}>{t(live.timeoutBehavior === 'default' ? 'defaultHint' : 'nativeHint')}</p>
183
- </div>
184
-
185
- <div style={fieldStyle}>
186
- <label style={labelStyle} htmlFor="better-ask-user-goal-behavior">{t('goalBehaviorLabel')}</label>
187
- <select
188
- id="better-ask-user-goal-behavior"
189
- style={inputStyle}
190
- value={live.goalTimeoutBehavior}
191
- disabled={!writable || busy}
192
- onChange={(event) => {
193
- const next = event.target.value === 'default' ? 'default' : 'native'
194
- edit((current) => ({ ...current, goalTimeoutBehavior: next }))
195
- }}
196
- >
197
- <option value="native">{t('native')}</option>
198
- <option value="default">{t('default')}</option>
199
- </select>
200
- <p style={hintStyle}>{t(live.goalTimeoutBehavior === 'default' ? 'defaultHint' : 'nativeHint')}</p>
201
- </div>
202
-
203
- <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
181
+ <section className={css.group} aria-label={t('title')}>
182
+ <Row id="better-ask-user-wait-mode" label={t('waitLabel')}>
183
+ <Choices
184
+ id="better-ask-user-wait-mode" value={unlimited ? 'unlimited' : 'timed'}
185
+ options={[{ value: 'timed', label: t('timed') }, { value: 'unlimited', label: t('unlimited') }]}
186
+ disabled={controlsDisabled} describedBy="better-ask-user-wait-hint"
187
+ onChange={(value) => {
188
+ const nextUnlimited = value === 'unlimited'
189
+ if (nextUnlimited && !invalid) setTimedSeconds(live.questionTimeoutSeconds)
190
+ edit(current => ({ ...current, questionTimeoutSeconds: nextUnlimited ? '-1' : timedSeconds }))
191
+ }}
192
+ />
193
+ {unlimited ? null : <span className={css.duration}>
194
+ <input
195
+ id="better-ask-user-wait" className={css.input}
196
+ aria-label={t('durationLabel')}
197
+ aria-describedby={invalid ? 'better-ask-user-error' : 'better-ask-user-wait-hint'}
198
+ type="text" inputMode="numeric" aria-invalid={invalid || undefined}
199
+ value={live.questionTimeoutSeconds} disabled={controlsDisabled}
200
+ onChange={(event) => { edit(current => ({ ...current, questionTimeoutSeconds: event.target.value })) }}
201
+ />
202
+ <span className={css.unit}>{t('seconds')}</span>
203
+ </span>}
204
+ </Row>
205
+ <p className={css.srOnly} id="better-ask-user-wait-hint">{t(unlimited ? 'unlimitedHint' : 'waitHint')}</p>
206
+ {invalid && <p className={css.fieldError} id="better-ask-user-error" role="alert">{t('invalidWait')}</p>}
207
+ {!unlimited && <>
208
+ <h3 className={css.sectionLabel}>{t('timeoutTitle')}</h3>
209
+ <Row id="better-ask-user-behavior" label={t('behaviorLabel')}>
210
+ <Choices
211
+ id="better-ask-user-behavior" value={live.timeoutBehavior} options={behaviors}
212
+ disabled={controlsDisabled} describedBy="better-ask-user-timeout-hint"
213
+ onChange={(value) => { edit(current => ({ ...current, timeoutBehavior: value as Behavior })) }}
214
+ />
215
+ </Row>
216
+ <p className={css.srOnly} id="better-ask-user-timeout-hint">{t(live.timeoutBehavior === 'default' ? 'defaultHint' : 'nativeHint')}</p>
217
+ <details className={css.advanced}>
218
+ <summary>
219
+ <span>{t('advanced')}</span>
220
+ {live.goalTimeoutBehavior !== DEFAULTS.goalTimeoutBehavior && <span className={css.advancedValue}>{t('goalBehaviorLabel')} · {t(live.goalTimeoutBehavior)}</span>}
221
+ <span className={css.chevron} aria-hidden="true" />
222
+ </summary>
223
+ <Row id="better-ask-user-goal-behavior" label={t('goalBehaviorLabel')}>
224
+ <Choices
225
+ id="better-ask-user-goal-behavior" value={live.goalTimeoutBehavior} options={behaviors}
226
+ disabled={controlsDisabled} describedBy="better-ask-user-goal-hint"
227
+ onChange={(value) => { edit(current => ({ ...current, goalTimeoutBehavior: value as Behavior })) }}
228
+ />
229
+ </Row>
230
+ <p className={css.srOnly} id="better-ask-user-goal-hint">{t(live.goalTimeoutBehavior === 'default' ? 'defaultHint' : 'nativeHint')}</p>
231
+ </details>
232
+ </>}
233
+ </section>
234
+ {notice !== null && <p className={notice.tone === 'ok' ? css.status : css.fieldError} role={notice.tone === 'error' ? 'alert' : 'status'}>{notice.text}</p>}
235
+ {dirty && <div className={css.footer} id="better-ask-user-settings-footer">
236
+ <span className={css.status}>{t('dirty')}</span>
237
+ <button type="button" className={css.button} id="better-ask-user-discard" disabled={controlsDisabled} onClick={() => {
238
+ setDraft(null)
239
+ setNotice(null)
240
+ setTimedSeconds(String(host.questionTimeoutSeconds === -1 ? DEFAULTS.questionTimeoutSeconds : host.questionTimeoutSeconds))
241
+ }}>{t('discard')}</button>
204
242
  <button
205
- type="button"
206
- disabled={!writable || busy || !dirty || invalid}
207
- onClick={() => { void save() }}
208
- style={{ padding: '7px 16px', borderRadius: 8, border: '1px solid rgba(127,127,127,.35)', background: 'rgba(127,127,127,.12)', color: 'inherit', font: 'inherit', cursor: 'pointer' }}
243
+ type="button" className={css.primary} data-settings-save disabled={controlsDisabled || invalid}
244
+ aria-busy={busy} onClick={() => { void save() }}
209
245
  >
246
+ {busy && <span className={css.spinner} aria-hidden="true" />}
210
247
  {busy ? t('saving') : t('save')}
211
248
  </button>
212
- {status}
213
- </div>
249
+ </div>}
214
250
  </div>
215
251
  )
216
252
  }
package/src/index.js CHANGED
@@ -16,9 +16,7 @@ export function apply(ctx, config = {}) {
16
16
 
17
17
  const states = new Map()
18
18
 
19
- function refresh(state) {
20
- state.askDispose?.()
21
- state.askDispose = undefined
19
+ function register(state) {
22
20
  const tools = state.actor.get('tools')
23
21
  const base = tools.get('ask_user_question', state.agent)
24
22
  if (base === undefined) return
@@ -29,7 +27,7 @@ export function apply(ctx, config = {}) {
29
27
  if (states.has(agent)) return states.get(agent)
30
28
  const state = { agent, actor: agent.ctx, askDispose: undefined }
31
29
  states.set(agent, state)
32
- refresh(state)
30
+ register(state)
33
31
  return state
34
32
  }
35
33
 
@@ -44,16 +42,6 @@ export function apply(ctx, config = {}) {
44
42
  if (state !== undefined) cleanup(state)
45
43
  states.delete(agent)
46
44
  })
47
- ctx.on('system-prompt/assemble', (assembly, context, next) => {
48
- const state = states.get(context.scope)
49
- if (state === undefined) return next()
50
- refresh(state)
51
- const schema = state.actor.get('tools').schemas(state.agent).find(tool => tool.name === 'ask_user_question')
52
- if (schema !== undefined) {
53
- assembly.tools = assembly.tools.map(tool => tool.name === 'ask_user_question' ? schema : tool)
54
- }
55
- return next()
56
- })
57
45
  for (const agent of ctx.agents.list()) setup(agent)
58
46
  ctx.effect(() => () => {
59
47
  for (const state of states.values()) cleanup(state)