dsh-mcviseron-plugin 0.0.0-stage → 0.6.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.
- package/CHANGELOG.md +222 -0
- package/CONTRIBUTING.md +60 -0
- package/LICENSE +202 -0
- package/README.en.md +146 -0
- package/README.md +287 -2
- package/SECURITY.md +38 -0
- package/cordis.patch.yml +12 -0
- package/docs/automation-task.md +63 -0
- package/docs/migration-from-standalone.md +115 -0
- package/docs/release.md +85 -0
- package/lib/appearance.js +320 -0
- package/lib/auto-review-fix.js +453 -0
- package/lib/client.js +1266 -0
- package/lib/index.js +1701 -0
- package/lib/restart.js +226 -0
- package/lib/task-guard.js +736 -0
- package/package.json +148 -4
- package/scripts/repair-profile.mjs +77 -0
- package/src/api.ts +5 -0
- package/src/appearance/index.ts +103 -0
- package/src/auto-review-fix/index.ts +45 -0
- package/src/client/AppearanceScheduleRow.tsx +112 -0
- package/src/client/ConfigForm.tsx +284 -0
- package/src/client/FallbackRestartRow.tsx +23 -0
- package/src/client/RestartCard.tsx +108 -0
- package/src/client/SettingsSection.tsx +110 -0
- package/src/client/bridge.ts +131 -0
- package/src/client/driver.ts +166 -0
- package/src/client/index.ts +114 -0
- package/src/client/locales.ts +193 -0
- package/src/client/primitives.d.ts +15 -0
- package/src/client/section-presence.ts +33 -0
- package/src/client/styles.ts +154 -0
- package/src/components/auto-review-fix/index.ts +421 -0
- package/src/components/auto-review-fix/translate.ts +235 -0
- package/src/components/task-guard/index.ts +1077 -0
- package/src/index.ts +71 -0
- package/src/loopback.ts +48 -0
- package/src/namespace.ts +7 -0
- package/src/restart/index.ts +268 -0
- package/src/routes.ts +228 -0
- package/src/schedule.ts +125 -0
- package/src/task-guard/index.ts +52 -0
- package/src/volatile.ts +32 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { readPluginConfig, writePluginConfig } from './bridge.ts'
|
|
3
|
+
import type { McviseronKey } from './locales.ts'
|
|
4
|
+
import { Switch } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
5
|
+
import { pageStyles } from './styles.ts'
|
|
6
|
+
|
|
7
|
+
type Status = 'loading' | 'ready' | 'saving' | 'saved' | 'failed' | 'unavailable'
|
|
8
|
+
type FieldKind = 'switch' | 'number' | 'text' | 'select' | 'list'
|
|
9
|
+
|
|
10
|
+
/** One editable config field backed by a nested entry path. */
|
|
11
|
+
export interface ConfigField {
|
|
12
|
+
path: readonly string[]
|
|
13
|
+
label: McviseronKey
|
|
14
|
+
hint?: McviseronKey
|
|
15
|
+
kind: FieldKind
|
|
16
|
+
min?: number
|
|
17
|
+
max?: number
|
|
18
|
+
options?: ReadonlyArray<{ value: string; label: McviseronKey }>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Props shared by the component sub-pages. */
|
|
22
|
+
export interface ConfigFormProps {
|
|
23
|
+
t: (key: McviseronKey) => string
|
|
24
|
+
title: McviseronKey
|
|
25
|
+
description: McviseronKey
|
|
26
|
+
fields: readonly ConfigField[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readPath(config: Record<string, unknown>, path: readonly string[]): unknown {
|
|
30
|
+
let current: unknown = config
|
|
31
|
+
for (const key of path) {
|
|
32
|
+
if (current === null || typeof current !== 'object' || Array.isArray(current)) return undefined
|
|
33
|
+
current = (current as Record<string, unknown>)[key]
|
|
34
|
+
}
|
|
35
|
+
return current
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function NumberControl({ value, min, max, disabled, onCommit }: {
|
|
39
|
+
value: number | undefined
|
|
40
|
+
min?: number
|
|
41
|
+
max?: number
|
|
42
|
+
disabled: boolean
|
|
43
|
+
onCommit: (value: number) => void
|
|
44
|
+
}) {
|
|
45
|
+
const [draft, setDraft] = useState(value === undefined ? '' : String(value))
|
|
46
|
+
useEffect(() => { setDraft(value === undefined ? '' : String(value)) }, [value])
|
|
47
|
+
const commit = (): void => {
|
|
48
|
+
const parsed = Number(draft)
|
|
49
|
+
const lower = min ?? Number.NEGATIVE_INFINITY
|
|
50
|
+
const upper = max ?? Number.POSITIVE_INFINITY
|
|
51
|
+
if (!Number.isInteger(parsed) || parsed < lower || parsed > upper) {
|
|
52
|
+
setDraft(value === undefined ? '' : String(value))
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
if (parsed !== value) onCommit(parsed)
|
|
56
|
+
}
|
|
57
|
+
return (
|
|
58
|
+
<input
|
|
59
|
+
type="number"
|
|
60
|
+
min={min}
|
|
61
|
+
max={max}
|
|
62
|
+
value={draft}
|
|
63
|
+
disabled={disabled}
|
|
64
|
+
onChange={(event) => { setDraft(event.target.value) }}
|
|
65
|
+
onBlur={commit}
|
|
66
|
+
onKeyDown={(event) => { if (event.key === 'Enter') event.currentTarget.blur() }}
|
|
67
|
+
style={pageStyles.formControl}
|
|
68
|
+
/>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function TextControl({ value, disabled, onCommit }: {
|
|
73
|
+
value: string
|
|
74
|
+
disabled: boolean
|
|
75
|
+
onCommit: (value: string) => void
|
|
76
|
+
}) {
|
|
77
|
+
const [draft, setDraft] = useState(value)
|
|
78
|
+
useEffect(() => { setDraft(value) }, [value])
|
|
79
|
+
return (
|
|
80
|
+
<input
|
|
81
|
+
type="text"
|
|
82
|
+
value={draft}
|
|
83
|
+
disabled={disabled}
|
|
84
|
+
onChange={(event) => { setDraft(event.target.value) }}
|
|
85
|
+
onBlur={() => { if (draft !== value) onCommit(draft) }}
|
|
86
|
+
onKeyDown={(event) => { if (event.key === 'Enter') event.currentTarget.blur() }}
|
|
87
|
+
style={pageStyles.formControl}
|
|
88
|
+
/>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ListControl({ value, disabled, onCommit }: {
|
|
93
|
+
value: string[]
|
|
94
|
+
disabled: boolean
|
|
95
|
+
onCommit: (value: string[]) => void
|
|
96
|
+
}) {
|
|
97
|
+
const joined = value.join(', ')
|
|
98
|
+
const [draft, setDraft] = useState(joined)
|
|
99
|
+
useEffect(() => { setDraft(value.join(', ')) }, [value])
|
|
100
|
+
const commit = (): void => {
|
|
101
|
+
const next = draft.split(',').map(item => item.trim()).filter(item => item !== '')
|
|
102
|
+
if (next.join('\u0000') !== value.join('\u0000')) onCommit(next)
|
|
103
|
+
}
|
|
104
|
+
return (
|
|
105
|
+
<input
|
|
106
|
+
type="text"
|
|
107
|
+
value={draft}
|
|
108
|
+
disabled={disabled}
|
|
109
|
+
placeholder="todo_write, ask_user_question"
|
|
110
|
+
onChange={(event) => { setDraft(event.target.value) }}
|
|
111
|
+
onBlur={commit}
|
|
112
|
+
onKeyDown={(event) => { if (event.key === 'Enter') event.currentTarget.blur() }}
|
|
113
|
+
style={pageStyles.formControl}
|
|
114
|
+
/>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function renderControl(
|
|
119
|
+
field: ConfigField,
|
|
120
|
+
value: unknown,
|
|
121
|
+
disabled: boolean,
|
|
122
|
+
commit: (value: unknown) => void,
|
|
123
|
+
t: (key: McviseronKey) => string,
|
|
124
|
+
) {
|
|
125
|
+
switch (field.kind) {
|
|
126
|
+
case 'switch':
|
|
127
|
+
return (
|
|
128
|
+
<Switch
|
|
129
|
+
checked={value === true}
|
|
130
|
+
label={t(field.label)}
|
|
131
|
+
disabled={disabled}
|
|
132
|
+
onChange={(next) => { commit(next) }}
|
|
133
|
+
/>
|
|
134
|
+
)
|
|
135
|
+
case 'number':
|
|
136
|
+
return (
|
|
137
|
+
<NumberControl
|
|
138
|
+
value={typeof value === 'number' ? value : undefined}
|
|
139
|
+
min={field.min}
|
|
140
|
+
max={field.max}
|
|
141
|
+
disabled={disabled}
|
|
142
|
+
onCommit={commit}
|
|
143
|
+
/>
|
|
144
|
+
)
|
|
145
|
+
case 'text':
|
|
146
|
+
return <TextControl value={typeof value === 'string' ? value : ''} disabled={disabled} onCommit={commit} />
|
|
147
|
+
case 'list':
|
|
148
|
+
return (
|
|
149
|
+
<ListControl
|
|
150
|
+
value={Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []}
|
|
151
|
+
disabled={disabled}
|
|
152
|
+
onCommit={(next) => { commit(next) }}
|
|
153
|
+
/>
|
|
154
|
+
)
|
|
155
|
+
case 'select':
|
|
156
|
+
return (
|
|
157
|
+
<select
|
|
158
|
+
value={typeof value === 'string' ? value : ''}
|
|
159
|
+
disabled={disabled}
|
|
160
|
+
onChange={(event) => { commit(event.target.value) }}
|
|
161
|
+
style={pageStyles.formControl}
|
|
162
|
+
>
|
|
163
|
+
{(field.options ?? []).map(option => (
|
|
164
|
+
<option key={option.value} value={option.value}>{t(option.label)}</option>
|
|
165
|
+
))}
|
|
166
|
+
</select>
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* One component's settings sub-page: reads the master-entry config through the
|
|
173
|
+
* loopback bridge and writes a single nested field per change.
|
|
174
|
+
*/
|
|
175
|
+
export function ConfigForm({ t, title, description, fields }: ConfigFormProps) {
|
|
176
|
+
const [config, setConfig] = useState<Record<string, unknown>>({})
|
|
177
|
+
const [status, setStatus] = useState<Status>('loading')
|
|
178
|
+
const [message, setMessage] = useState('')
|
|
179
|
+
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
let cancelled = false
|
|
182
|
+
void (async () => {
|
|
183
|
+
const view = await readPluginConfig()
|
|
184
|
+
if (cancelled) return
|
|
185
|
+
if (view === undefined) {
|
|
186
|
+
setStatus('unavailable')
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
setConfig(view.value)
|
|
190
|
+
setStatus('ready')
|
|
191
|
+
})()
|
|
192
|
+
return () => { cancelled = true }
|
|
193
|
+
}, [])
|
|
194
|
+
|
|
195
|
+
const save = async (path: readonly string[], value: unknown): Promise<void> => {
|
|
196
|
+
setStatus('saving')
|
|
197
|
+
setMessage('')
|
|
198
|
+
const result = await writePluginConfig(path, value)
|
|
199
|
+
if (result.ok) {
|
|
200
|
+
if (result.value !== undefined) setConfig(result.value)
|
|
201
|
+
setStatus('saved')
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
setStatus('failed')
|
|
205
|
+
setMessage(result.message ?? result.code ?? '')
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const disabled = status === 'loading' || status === 'saving' || status === 'unavailable'
|
|
209
|
+
const statusText = status === 'loading'
|
|
210
|
+
? t('form.loading')
|
|
211
|
+
: status === 'saving'
|
|
212
|
+
? t('form.saving')
|
|
213
|
+
: status === 'saved'
|
|
214
|
+
? t('form.saved')
|
|
215
|
+
: status === 'failed'
|
|
216
|
+
? t('form.failed') + (message === '' ? '' : ' (' + message + ')')
|
|
217
|
+
: status === 'unavailable'
|
|
218
|
+
? t('form.unavailable')
|
|
219
|
+
: t('form.autoSave')
|
|
220
|
+
|
|
221
|
+
return (
|
|
222
|
+
<div style={pageStyles.card}>
|
|
223
|
+
<div style={pageStyles.cardTitle}>{t(title)}</div>
|
|
224
|
+
<div style={pageStyles.cardBody}>{t(description)}</div>
|
|
225
|
+
<div style={pageStyles.form}>
|
|
226
|
+
{fields.map((field) => {
|
|
227
|
+
const value = readPath(config, field.path)
|
|
228
|
+
return (
|
|
229
|
+
<div key={field.path.join('.')} style={pageStyles.formRow}>
|
|
230
|
+
<div style={pageStyles.formLabel}>{t(field.label)}</div>
|
|
231
|
+
{field.hint === undefined ? null : <div style={pageStyles.formHint}>{t(field.hint)}</div>}
|
|
232
|
+
{renderControl(field, value, disabled, (next) => { void save(field.path, next) }, t)}
|
|
233
|
+
</div>
|
|
234
|
+
)
|
|
235
|
+
})}
|
|
236
|
+
</div>
|
|
237
|
+
<div style={pageStyles.formStatus}>{statusText}</div>
|
|
238
|
+
</div>
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Auto-review component fields. */
|
|
243
|
+
export const AUTO_REVIEW_FIELDS: readonly ConfigField[] = [
|
|
244
|
+
{ path: ['autoReviewFix', 'translateApprovalReason'], label: 'form.autoReview.translate', hint: 'form.autoReview.translateHint', kind: 'switch' },
|
|
245
|
+
{ path: ['autoReviewFix', 'keepPresetGuard'], label: 'form.autoReview.keepPreset', hint: 'form.autoReview.keepPresetHint', kind: 'switch' },
|
|
246
|
+
{ path: ['autoReviewFix', 'translateTimeoutMs'], label: 'form.autoReview.timeout', kind: 'number', min: 500, max: 60000 },
|
|
247
|
+
{ path: ['autoReviewFix', 'translateMaxChars'], label: 'form.autoReview.maxChars', kind: 'number', min: 100, max: 20000 },
|
|
248
|
+
{ path: ['autoReviewFix', 'translateProvider'], label: 'form.autoReview.provider', hint: 'form.autoReview.routeHint', kind: 'text' },
|
|
249
|
+
{ path: ['autoReviewFix', 'translateModel'], label: 'form.autoReview.model', hint: 'form.autoReview.routeHint', kind: 'text' },
|
|
250
|
+
]
|
|
251
|
+
|
|
252
|
+
/** Todo plan guard fields. */
|
|
253
|
+
export const TASK_GUARD_FIELDS: readonly ConfigField[] = [
|
|
254
|
+
{ path: ['taskGuard', 'enabled'], label: 'form.taskGuard.enabled', hint: 'form.taskGuard.enabledHint', kind: 'switch' },
|
|
255
|
+
{
|
|
256
|
+
path: ['taskGuard', 'locale'],
|
|
257
|
+
label: 'form.taskGuard.locale',
|
|
258
|
+
kind: 'select',
|
|
259
|
+
options: [
|
|
260
|
+
{ value: 'zh', label: 'form.taskGuard.localeZh' },
|
|
261
|
+
{ value: 'en', label: 'form.taskGuard.localeEn' },
|
|
262
|
+
],
|
|
263
|
+
},
|
|
264
|
+
{ path: ['taskGuard', 'staleAfterCalls'], label: 'form.taskGuard.stale', kind: 'number', min: 1, max: 1000 },
|
|
265
|
+
{ path: ['taskGuard', 'repeatEveryCalls'], label: 'form.taskGuard.repeat', kind: 'number', min: 0, max: 1000 },
|
|
266
|
+
{ path: ['taskGuard', 'blockAfterCalls'], label: 'form.taskGuard.block', hint: 'form.taskGuard.blockHint', kind: 'number', min: 0, max: 10000 },
|
|
267
|
+
{ path: ['taskGuard', 'rejectMissingActive'], label: 'form.taskGuard.rejectMissingActive', kind: 'switch' },
|
|
268
|
+
{
|
|
269
|
+
path: ['taskGuard', 'sourceKind'],
|
|
270
|
+
label: 'form.taskGuard.sourceKind',
|
|
271
|
+
kind: 'select',
|
|
272
|
+
options: [
|
|
273
|
+
{ value: 'auto', label: 'form.taskGuard.sourceAuto' },
|
|
274
|
+
{ value: 'v3', label: 'form.taskGuard.sourceV3' },
|
|
275
|
+
{ value: 'v4', label: 'form.taskGuard.sourceV4' },
|
|
276
|
+
],
|
|
277
|
+
},
|
|
278
|
+
{ path: ['taskGuard', 'maxListItems'], label: 'form.taskGuard.maxListItems', kind: 'number', min: 1, max: 100 },
|
|
279
|
+
{ path: ['taskGuard', 'detectStepIntent'], label: 'form.taskGuard.detectStepIntent', hint: 'form.taskGuard.detectStepIntentHint', kind: 'switch' },
|
|
280
|
+
{ path: ['taskGuard', 'translateTodoContent'], label: 'form.taskGuard.translateTodo', hint: 'form.taskGuard.translateTodoHint', kind: 'switch' },
|
|
281
|
+
{ path: ['taskGuard', 'todoTranslationTimeoutMs'], label: 'form.taskGuard.todoTimeout', kind: 'number', min: 500, max: 15000 },
|
|
282
|
+
{ path: ['taskGuard', 'include'], label: 'form.taskGuard.include', hint: 'form.taskGuard.listHint', kind: 'list' },
|
|
283
|
+
{ path: ['taskGuard', 'exclude'], label: 'form.taskGuard.exclude', hint: 'form.taskGuard.listHint', kind: 'list' },
|
|
284
|
+
]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react'
|
|
2
|
+
import type { McviseronKey } from './locales.ts'
|
|
3
|
+
import { RestartCard } from './RestartCard.tsx'
|
|
4
|
+
import { sectionPresence } from './section-presence.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* General-section restart row, rendered only while the MCviseron settings
|
|
8
|
+
* section is absent (slot not declared or registration failed).
|
|
9
|
+
*/
|
|
10
|
+
export function FallbackRestartRow({ t }: { t: (key: McviseronKey) => string }) {
|
|
11
|
+
const live = useSyncExternalStore(sectionPresence.subscribe, sectionPresence.isLive)
|
|
12
|
+
if (live) return null
|
|
13
|
+
const failure = sectionPresence.failure()
|
|
14
|
+
return (
|
|
15
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
|
16
|
+
<div style={{ fontSize: 12, color: 'var(--dsw-alias-label-tertiary)' }}>
|
|
17
|
+
{t('page.fallback.note')}
|
|
18
|
+
{failure !== '' ? ' (' + failure + ')' : ''}
|
|
19
|
+
</div>
|
|
20
|
+
<RestartCard t={t} />
|
|
21
|
+
</div>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import type { McviseronKey } from './locales.ts'
|
|
3
|
+
import { pageStyles, styles } from './styles.ts'
|
|
4
|
+
|
|
5
|
+
const STATUS = '/api/dsh-mcviseron/restart/status'
|
|
6
|
+
const EXECUTE = '/api/dsh-mcviseron/restart/execute'
|
|
7
|
+
|
|
8
|
+
type RestartState =
|
|
9
|
+
| { kind: 'loading' }
|
|
10
|
+
| { kind: 'ready'; appPath: string }
|
|
11
|
+
| { kind: 'unavailable' }
|
|
12
|
+
| { kind: 'route-missing' }
|
|
13
|
+
| { kind: 'restarting' }
|
|
14
|
+
| { kind: 'failed'; message: string }
|
|
15
|
+
|
|
16
|
+
async function readError(response: Response): Promise<string> {
|
|
17
|
+
try {
|
|
18
|
+
const body = await response.json() as { error?: unknown }
|
|
19
|
+
if (typeof body.error === 'string') return body.error
|
|
20
|
+
} catch {
|
|
21
|
+
// not JSON
|
|
22
|
+
}
|
|
23
|
+
return 'HTTP ' + String(response.status)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Restart card shared by the settings section and the General fallback row. */
|
|
27
|
+
export function RestartCard({ t }: { t: (key: McviseronKey) => string }) {
|
|
28
|
+
const [state, setState] = useState<RestartState>({ kind: 'loading' })
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
let cancelled = false
|
|
32
|
+
void (async () => {
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(STATUS, { method: 'GET' })
|
|
35
|
+
if (cancelled) return
|
|
36
|
+
if (response.status === 404 || response.status === 405) {
|
|
37
|
+
setState({ kind: 'route-missing' })
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
if (!response.ok) throw new Error(await readError(response))
|
|
41
|
+
const body = await response.json() as { ok?: unknown; available?: unknown; appPath?: unknown }
|
|
42
|
+
if (body.ok !== true) throw new Error('restart-status-rejected')
|
|
43
|
+
if (body.available === true) {
|
|
44
|
+
setState({ kind: 'ready', appPath: typeof body.appPath === 'string' ? body.appPath : '' })
|
|
45
|
+
} else {
|
|
46
|
+
setState({ kind: 'unavailable' })
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (!cancelled) setState({ kind: 'failed', message: error instanceof Error ? error.message : String(error) })
|
|
50
|
+
}
|
|
51
|
+
})()
|
|
52
|
+
return () => {
|
|
53
|
+
cancelled = true
|
|
54
|
+
}
|
|
55
|
+
}, [])
|
|
56
|
+
|
|
57
|
+
const startRestart = async (): Promise<void> => {
|
|
58
|
+
if (!window.confirm(t('page.restart.confirm'))) return
|
|
59
|
+
setState({ kind: 'restarting' })
|
|
60
|
+
try {
|
|
61
|
+
const response = await fetch(EXECUTE, {
|
|
62
|
+
method: 'POST',
|
|
63
|
+
headers: { 'content-type': 'application/json' },
|
|
64
|
+
body: JSON.stringify({ confirm: true }),
|
|
65
|
+
})
|
|
66
|
+
if (!response.ok) throw new Error(await readError(response))
|
|
67
|
+
const body = await response.json() as { ok?: unknown }
|
|
68
|
+
if (body.ok !== true) throw new Error('restart-rejected')
|
|
69
|
+
} catch (error) {
|
|
70
|
+
setState({ kind: 'failed', message: error instanceof Error ? error.message : String(error) })
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const status = state.kind === 'ready'
|
|
75
|
+
? t('page.restart.ready')
|
|
76
|
+
: state.kind === 'route-missing'
|
|
77
|
+
? t('page.restart.routeMissing')
|
|
78
|
+
: state.kind === 'restarting'
|
|
79
|
+
? t('page.restart.restarting')
|
|
80
|
+
: state.kind === 'failed'
|
|
81
|
+
? t('page.restart.failed') + (state.message !== '' ? ' (' + state.message + ')' : '')
|
|
82
|
+
: state.kind === 'loading'
|
|
83
|
+
? t('restart.checking')
|
|
84
|
+
: t('page.restart.unavailable')
|
|
85
|
+
|
|
86
|
+
const canRestart = state.kind === 'ready' || state.kind === 'failed'
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<div style={pageStyles.card}>
|
|
90
|
+
<div style={pageStyles.cardTitle}>{t('page.restart.title')}</div>
|
|
91
|
+
<div style={pageStyles.cardBody}>{t('page.restart.description')}</div>
|
|
92
|
+
<div style={pageStyles.cardStatus}>{status}</div>
|
|
93
|
+
{state.kind === 'ready' && state.appPath !== ''
|
|
94
|
+
? <div style={pageStyles.cardStatus}>{t('page.restart.appPath')}: {state.appPath}</div>
|
|
95
|
+
: null}
|
|
96
|
+
<div style={pageStyles.cardActions}>
|
|
97
|
+
<button
|
|
98
|
+
type="button"
|
|
99
|
+
disabled={!canRestart}
|
|
100
|
+
onClick={() => { void startRestart() }}
|
|
101
|
+
style={canRestart ? styles.button : styles.buttonDisabled}
|
|
102
|
+
>
|
|
103
|
+
{state.kind === 'restarting' ? t('page.restart.restarting') : t('page.restart.button')}
|
|
104
|
+
</button>
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import type { McviseronKey } from './locales.ts'
|
|
3
|
+
import { AUTO_REVIEW_FIELDS, ConfigForm, TASK_GUARD_FIELDS } from './ConfigForm.tsx'
|
|
4
|
+
import { RestartCard } from './RestartCard.tsx'
|
|
5
|
+
import { pageStyles } from './styles.ts'
|
|
6
|
+
|
|
7
|
+
type Tab = 'restart' | 'autoReview' | 'taskGuard' | 'components'
|
|
8
|
+
|
|
9
|
+
/** Props composed by the `settings.section` slot. */
|
|
10
|
+
export interface SettingsSectionProps {
|
|
11
|
+
t: (key: McviseronKey) => string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* First-level MCviseron settings section. Components with a settings surface
|
|
16
|
+
* get a tab here; the Appearance component intentionally stays in General.
|
|
17
|
+
* @param props - composed slot props.
|
|
18
|
+
* @returns the section element tree.
|
|
19
|
+
*/
|
|
20
|
+
export function SettingsSection({ t }: SettingsSectionProps) {
|
|
21
|
+
const [tab, setTab] = useState<Tab>('restart')
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<div style={pageStyles.page}>
|
|
25
|
+
<h2 style={pageStyles.heading}>{t('page.title')}</h2>
|
|
26
|
+
<p style={pageStyles.lede}>{t('page.description')}</p>
|
|
27
|
+
<div style={pageStyles.tabs} role="tablist">
|
|
28
|
+
<button
|
|
29
|
+
type="button"
|
|
30
|
+
role="tab"
|
|
31
|
+
aria-selected={tab === 'restart'}
|
|
32
|
+
onClick={() => { setTab('restart') }}
|
|
33
|
+
style={tab === 'restart' ? { ...pageStyles.tab, ...pageStyles.tabActive } : pageStyles.tab}
|
|
34
|
+
>
|
|
35
|
+
{t('page.tab.restart')}
|
|
36
|
+
</button>
|
|
37
|
+
<button
|
|
38
|
+
type="button"
|
|
39
|
+
role="tab"
|
|
40
|
+
aria-selected={tab === 'autoReview'}
|
|
41
|
+
onClick={() => { setTab('autoReview') }}
|
|
42
|
+
style={tab === 'autoReview' ? { ...pageStyles.tab, ...pageStyles.tabActive } : pageStyles.tab}
|
|
43
|
+
>
|
|
44
|
+
{t('page.tab.autoReview')}
|
|
45
|
+
</button>
|
|
46
|
+
<button
|
|
47
|
+
type="button"
|
|
48
|
+
role="tab"
|
|
49
|
+
aria-selected={tab === 'taskGuard'}
|
|
50
|
+
onClick={() => { setTab('taskGuard') }}
|
|
51
|
+
style={tab === 'taskGuard' ? { ...pageStyles.tab, ...pageStyles.tabActive } : pageStyles.tab}
|
|
52
|
+
>
|
|
53
|
+
{t('page.tab.taskGuard')}
|
|
54
|
+
</button>
|
|
55
|
+
<button
|
|
56
|
+
type="button"
|
|
57
|
+
role="tab"
|
|
58
|
+
aria-selected={tab === 'components'}
|
|
59
|
+
onClick={() => { setTab('components') }}
|
|
60
|
+
style={tab === 'components' ? { ...pageStyles.tab, ...pageStyles.tabActive } : pageStyles.tab}
|
|
61
|
+
>
|
|
62
|
+
{t('page.tab.components')}
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
|
|
66
|
+
{tab === 'restart'
|
|
67
|
+
? <RestartCard t={t} />
|
|
68
|
+
: tab === 'autoReview'
|
|
69
|
+
? (
|
|
70
|
+
<ConfigForm
|
|
71
|
+
t={t}
|
|
72
|
+
title="page.autoReview.title"
|
|
73
|
+
description="page.autoReview.description"
|
|
74
|
+
fields={AUTO_REVIEW_FIELDS}
|
|
75
|
+
/>
|
|
76
|
+
)
|
|
77
|
+
: tab === 'taskGuard'
|
|
78
|
+
? (
|
|
79
|
+
<ConfigForm
|
|
80
|
+
t={t}
|
|
81
|
+
title="page.taskGuard.title"
|
|
82
|
+
description="page.taskGuard.description"
|
|
83
|
+
fields={TASK_GUARD_FIELDS}
|
|
84
|
+
/>
|
|
85
|
+
)
|
|
86
|
+
: (
|
|
87
|
+
<div style={pageStyles.card}>
|
|
88
|
+
<div style={pageStyles.cardTitle}>{t('page.components.title')}</div>
|
|
89
|
+
<div style={pageStyles.componentRow}>
|
|
90
|
+
<span style={pageStyles.componentName}>{t('page.components.appearance')}</span>
|
|
91
|
+
<span style={pageStyles.componentHint}>{t('page.components.appearanceHint')}</span>
|
|
92
|
+
</div>
|
|
93
|
+
<div style={pageStyles.componentRow}>
|
|
94
|
+
<span style={pageStyles.componentName}>{t('page.components.autoReviewFix')}</span>
|
|
95
|
+
<span style={pageStyles.componentHint}>{t('page.components.autoReviewFixHint')}</span>
|
|
96
|
+
</div>
|
|
97
|
+
<div style={pageStyles.componentRow}>
|
|
98
|
+
<span style={pageStyles.componentName}>{t('page.components.taskGuard')}</span>
|
|
99
|
+
<span style={pageStyles.componentHint}>{t('page.components.taskGuardHint')}</span>
|
|
100
|
+
</div>
|
|
101
|
+
<div style={pageStyles.componentRow}>
|
|
102
|
+
<span style={pageStyles.componentName}>{t('page.components.restart')}</span>
|
|
103
|
+
<span style={pageStyles.componentHint}>{t('page.components.restartHint')}</span>
|
|
104
|
+
</div>
|
|
105
|
+
<div style={pageStyles.cardStatus}>{t('page.components.note')}</div>
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
</div>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { MCVISERON_API } from '../api.ts'
|
|
2
|
+
import { MCVISERON_SETTINGS_NAMESPACE } from '../namespace.ts'
|
|
3
|
+
import { normalizeScheduleConfig, type ScheduleConfig } from '../schedule.ts'
|
|
4
|
+
|
|
5
|
+
interface DescribeResponse {
|
|
6
|
+
ok?: boolean
|
|
7
|
+
value?: {
|
|
8
|
+
namespaces?: Array<{ ns?: unknown; value?: unknown; revision?: unknown }>
|
|
9
|
+
writable?: boolean
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface MutateResponse {
|
|
14
|
+
ok?: boolean
|
|
15
|
+
value?: { value?: unknown; revision?: unknown }
|
|
16
|
+
code?: string
|
|
17
|
+
message?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Full component config as the Host bridge serializes it. */
|
|
21
|
+
export interface PluginConfigView {
|
|
22
|
+
value: Record<string, unknown>
|
|
23
|
+
revision?: number
|
|
24
|
+
writable: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Result of one component-config write. */
|
|
28
|
+
export interface PluginWriteResult {
|
|
29
|
+
ok: boolean
|
|
30
|
+
value?: Record<string, unknown>
|
|
31
|
+
code?: string
|
|
32
|
+
message?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
36
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function post(path: string, body: unknown): Promise<unknown> {
|
|
40
|
+
const response = await fetch(path, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'content-type': 'application/json' },
|
|
43
|
+
body: JSON.stringify(body),
|
|
44
|
+
})
|
|
45
|
+
if (!response.ok) throw new Error('HTTP ' + response.status)
|
|
46
|
+
return await response.json()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Read the appearance entry through the Host bridge.
|
|
51
|
+
* @returns the stored schedule, or undefined when the bridge or namespace is
|
|
52
|
+
* unavailable (the caller keeps the built-in defaults).
|
|
53
|
+
*/
|
|
54
|
+
export async function readScheduleConfig(): Promise<ScheduleConfig | undefined> {
|
|
55
|
+
try {
|
|
56
|
+
const body = await post(MCVISERON_API.describe, {}) as DescribeResponse
|
|
57
|
+
if (body.ok !== true) return undefined
|
|
58
|
+
const view = body.value?.namespaces?.find(candidate => String(candidate.ns) === MCVISERON_SETTINGS_NAMESPACE)
|
|
59
|
+
if (view === undefined) return undefined
|
|
60
|
+
return normalizeScheduleConfig(view.value)
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Write the whole appearance config through the Host bridge (one atomic batch,
|
|
68
|
+
* so a partial window can never be persisted).
|
|
69
|
+
* @param config - the desired schedule.
|
|
70
|
+
* @returns the persisted schedule, or undefined when the write was refused.
|
|
71
|
+
*/
|
|
72
|
+
export async function writeScheduleConfig(config: ScheduleConfig): Promise<ScheduleConfig | undefined> {
|
|
73
|
+
try {
|
|
74
|
+
const body = await post(MCVISERON_API.mutate, {
|
|
75
|
+
ns: MCVISERON_SETTINGS_NAMESPACE,
|
|
76
|
+
ops: [
|
|
77
|
+
{ op: 'set', path: ['enabled'], value: config.enabled },
|
|
78
|
+
{ op: 'set', path: ['lightStart'], value: config.lightStart },
|
|
79
|
+
{ op: 'set', path: ['lightEnd'], value: config.lightEnd },
|
|
80
|
+
],
|
|
81
|
+
}) as MutateResponse
|
|
82
|
+
if (body.ok !== true) return undefined
|
|
83
|
+
return normalizeScheduleConfig(body.value?.value)
|
|
84
|
+
} catch {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read the whole master-entry config (appearance fields + component groups).
|
|
91
|
+
* @returns the config view, or undefined when the bridge/namespace is unavailable.
|
|
92
|
+
*/
|
|
93
|
+
export async function readPluginConfig(): Promise<PluginConfigView | undefined> {
|
|
94
|
+
try {
|
|
95
|
+
const body = await post(MCVISERON_API.describe, {}) as DescribeResponse
|
|
96
|
+
if (body.ok !== true) return undefined
|
|
97
|
+
const view = body.value?.namespaces?.find(candidate => String(candidate.ns) === MCVISERON_SETTINGS_NAMESPACE)
|
|
98
|
+
if (view === undefined) return undefined
|
|
99
|
+
return {
|
|
100
|
+
value: isRecord(view.value) ? view.value : {},
|
|
101
|
+
revision: typeof view.revision === 'number' ? view.revision : undefined,
|
|
102
|
+
writable: body.value?.writable !== false,
|
|
103
|
+
}
|
|
104
|
+
} catch {
|
|
105
|
+
return undefined
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Write one component-config field through the Host bridge.
|
|
111
|
+
* @param path - nested config path (e.g. ['autoReviewFix', 'translateApprovalReason']).
|
|
112
|
+
* @param value - JSON value validated by the Host bridge.
|
|
113
|
+
* @returns the write result and the freshly read config when accepted.
|
|
114
|
+
*/
|
|
115
|
+
export async function writePluginConfig(path: readonly string[], value: unknown): Promise<PluginWriteResult> {
|
|
116
|
+
try {
|
|
117
|
+
const body = await post(MCVISERON_API.mutate, {
|
|
118
|
+
ns: MCVISERON_SETTINGS_NAMESPACE,
|
|
119
|
+
ops: [{ op: 'set', path: [...path], value }],
|
|
120
|
+
}) as MutateResponse
|
|
121
|
+
if (body.ok !== true) {
|
|
122
|
+
return { ok: false, code: body.code, message: body.message }
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
ok: true,
|
|
126
|
+
value: isRecord(body.value?.value) ? body.value.value : undefined,
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return { ok: false, message: error instanceof Error ? error.message : String(error) }
|
|
130
|
+
}
|
|
131
|
+
}
|