@gongfpp/dsh-novel-fish 0.1.4 → 1.0.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.
- package/README.md +67 -55
- package/assets/icon.png +0 -0
- package/docs/community-markets.md +48 -29
- package/docs/compatibility-017.md +70 -0
- package/docs/continuous-reading-020.md +46 -0
- package/docs/evidence/banner-100.png +0 -0
- package/docs/evidence/compatibility-017-result.json +65 -0
- package/docs/evidence/continuous-reading-020-result.json +72 -0
- package/docs/evidence/native-settings-sample.md +11 -0
- package/docs/evidence/native-settings-types-015.png +0 -0
- package/docs/evidence/npm-market-014-result.json +132 -0
- package/docs/evidence/plugin-settings-details.png +0 -0
- package/docs/evidence/public-release-015-result.json +36 -0
- package/docs/evidence/reading-demo-100.png +0 -0
- package/docs/evidence/settings-100.png +0 -0
- package/docs/host-adapter.md +3 -3
- package/docs/icon-metadata-021.md +40 -0
- package/docs/interaction-paths-020.md +39 -0
- package/docs/marketplace.md +3 -3
- package/docs/native-settings-update.md +48 -0
- package/docs/plugin-settings-update.md +61 -0
- package/docs/release.md +70 -1
- package/index.json +70 -6
- package/lib/client.js +306 -185
- package/locale/en.json +15 -6
- package/locale/zh.json +15 -6
- package/package.json +15 -13
- package/scripts/check-host-contracts.mjs +246 -0
- package/src/chat-adapter.js +22 -2
- package/src/client.js +5 -5
- package/src/settings.js +155 -95
- package/src/store.js +22 -3
- package/test/chat-adapter.test.js +61 -3
- package/test/client.test.js +82 -0
- package/test/helpers/host-primitives.js +28 -0
- package/test/host-contracts.test.js +32 -0
- package/test/release.test.js +19 -5
- package/test/settings.test.js +214 -65
- package/test/store.test.js +40 -0
- package/assets/icon.svg +0 -1
package/src/settings.js
CHANGED
|
@@ -1,57 +1,68 @@
|
|
|
1
1
|
import { TARGET_TYPES } from './store.js'
|
|
2
2
|
|
|
3
3
|
const TYPE_LABELS = { reasoning: 'typeReasoning', 'tool-call': 'typeToolCall', 'read-file': 'typeReadFile', command: 'typeCommand', 'file-search': 'typeFileSearch' }
|
|
4
|
-
|
|
5
|
-
const PANEL = {
|
|
6
|
-
display: 'grid', gap: 14, padding: 14,
|
|
7
|
-
border: '1px solid var(--dsw-alias-border-l2)',
|
|
8
|
-
borderRadius: 'var(--dsw-radius-md, 10px)',
|
|
9
|
-
color: 'var(--dsw-alias-label-primary)',
|
|
10
|
-
fontSize: 'var(--dsh-content-font-size-secondary, 13px)',
|
|
11
|
-
}
|
|
12
|
-
const LABEL = { display: 'grid', gap: 5, color: 'var(--dsw-alias-label-secondary)' }
|
|
13
|
-
const CONTROL = {
|
|
14
|
-
boxSizing: 'border-box', width: '100%', minWidth: 0,
|
|
15
|
-
border: '1px solid var(--dsw-alias-border-l2)',
|
|
16
|
-
borderRadius: 'var(--dsw-radius-sm, 6px)',
|
|
17
|
-
background: 'var(--dsw-alias-bg-base)', color: 'var(--dsw-alias-label-primary)',
|
|
18
|
-
padding: '7px 9px', font: 'inherit',
|
|
19
|
-
}
|
|
20
|
-
const BUTTON = {
|
|
21
|
-
...CONTROL, width: 'auto', cursor: 'pointer',
|
|
22
|
-
background: 'var(--dsw-alias-interactive-bg-hover)',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// 只自定义文字;图标、箭头和折叠交互完全交给 DSH。
|
|
26
4
|
const MAIN_TITLES = ['', '思考', '分析', '检查', 'Thinking']
|
|
27
5
|
const EXTRA_TITLES = ['', 'Checking project structure', 'Analyzing the request', 'Reviewing implementation', 'Verifying results']
|
|
28
6
|
|
|
29
|
-
|
|
30
|
-
|
|
7
|
+
// Only layout belongs to the plugin. Controls, menus, focus and dialogs belong to DSH.
|
|
8
|
+
const STYLES = `
|
|
9
|
+
.novel-fish-settings{display:flex;flex-direction:column;gap:32px;min-width:0;font-size:14px;line-height:1.6;color:var(--dsw-alias-label-primary)}
|
|
10
|
+
.novel-fish-settings .novel-fish-section{display:flex;flex-direction:column;gap:16px;min-width:0}
|
|
11
|
+
.novel-fish-settings .novel-fish-row{display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:12px}
|
|
12
|
+
.novel-fish-settings h3{margin:0;font:inherit;font-weight:600}
|
|
13
|
+
.novel-fish-settings .novel-fish-field{display:flex;flex-direction:column;gap:8px;min-width:0}
|
|
14
|
+
.novel-fish-settings .novel-fish-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,240px),1fr));gap:16px}
|
|
15
|
+
.novel-fish-settings .novel-fish-select,.novel-fish-settings .novel-fish-input{display:flex;width:100%;min-width:0}
|
|
16
|
+
.novel-fish-settings .novel-fish-select-button{width:100%;justify-content:space-between;gap:12px;text-align:start}
|
|
17
|
+
.novel-fish-settings .novel-fish-selection{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;min-width:0}
|
|
18
|
+
.novel-fish-settings .novel-fish-hint{margin:0;color:var(--dsw-alias-label-tertiary);font-size:13px;line-height:1.6}
|
|
19
|
+
.novel-fish-settings .novel-fish-error{color:var(--dsw-alias-state-error-primary)}
|
|
20
|
+
`
|
|
21
|
+
|
|
22
|
+
/** Embedded in this bundle's official configuration slot; never a reading page. */
|
|
23
|
+
export function createSettingsRow(React, { Switch, Button, Input, Menu, Modal, IconChevronDownOutlineRegular }) {
|
|
31
24
|
const h = React.createElement
|
|
32
25
|
|
|
33
|
-
function
|
|
26
|
+
function SelectMenu({ label, summary, value, items, disabled, onChange, marker, multiple = false, footer }) {
|
|
27
|
+
const [open, setOpen] = React.useState(false)
|
|
28
|
+
const selection = multiple ? summary : items.find(item => item.id === value)?.label ?? label
|
|
29
|
+
return h(Menu, {
|
|
30
|
+
open, onClose: () => setOpen(false), portal: true, autoFocus: true,
|
|
31
|
+
className: 'novel-fish-select',
|
|
32
|
+
items: items.map(item => ({ ...item, disabled: disabled || item.disabled })),
|
|
33
|
+
footer: footer?.map(item => ({ ...item, disabled })),
|
|
34
|
+
...(multiple ? { selectedIds: value } : { selectedId: value }),
|
|
35
|
+
onSelect: id => {
|
|
36
|
+
if (disabled) return
|
|
37
|
+
if (!multiple) setOpen(false)
|
|
38
|
+
onChange(id)
|
|
39
|
+
},
|
|
40
|
+
anchor: h(Button, {
|
|
41
|
+
variant: 'outline', disabled, className: 'novel-fish-select-button',
|
|
42
|
+
'aria-label': label, 'aria-haspopup': 'menu', 'aria-expanded': open,
|
|
43
|
+
...marker, onClick: () => { if (!disabled) setOpen(current => !current) },
|
|
44
|
+
}, h('span', { className: 'novel-fish-selection' }, selection), h(IconChevronDownOutlineRegular, { size: 16 })),
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function PresetField({ t, label, value, choices, disabled, onChange, testId }) {
|
|
34
49
|
const [custom, setCustom] = React.useState(!choices.includes(value))
|
|
35
50
|
const showCustom = custom || !choices.includes(value)
|
|
36
|
-
return h('
|
|
51
|
+
return h('div', { className: 'novel-fish-field' },
|
|
37
52
|
h('span', null, label),
|
|
38
|
-
h(
|
|
39
|
-
|
|
40
|
-
'data-novel-preset': testId,
|
|
41
|
-
|
|
42
|
-
|
|
53
|
+
h(SelectMenu, {
|
|
54
|
+
label, value: showCustom ? '__custom' : value, disabled,
|
|
55
|
+
marker: { 'data-novel-preset': testId },
|
|
56
|
+
items: [...choices.map(choice => ({ id: choice, label: choice || t(testId === 'title' ? 'settings.nativeTitle' : 'settings.noExtra') })), { id: '__custom', label: t('settings.custom') }],
|
|
57
|
+
onChange: next => {
|
|
43
58
|
if (next === '__custom') setCustom(true)
|
|
44
59
|
else void onChange(next).then(saved => { if (saved) setCustom(false) })
|
|
45
60
|
},
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
style: CONTROL, defaultValue: value, disabled, maxLength,
|
|
52
|
-
'aria-label': t('settings.customField', { label }),
|
|
53
|
-
[`data-novel-${testId}`]: true,
|
|
54
|
-
onChange: event => onChange(event.currentTarget.value),
|
|
61
|
+
}),
|
|
62
|
+
showCustom ? h(Input, {
|
|
63
|
+
className: 'novel-fish-input', defaultValue: value, disabled,
|
|
64
|
+
'aria-label': t('settings.customField', { label }), [`data-novel-${testId}`]: true,
|
|
65
|
+
onChange: event => { void onChange(event.currentTarget.value) },
|
|
55
66
|
}) : null,
|
|
56
67
|
)
|
|
57
68
|
}
|
|
@@ -61,21 +72,48 @@ export function createSettingsRow(React) {
|
|
|
61
72
|
const ui = useNovelUi(value => value)
|
|
62
73
|
const [busy, setBusy] = React.useState(false)
|
|
63
74
|
const [savingTypes, setSavingTypes] = React.useState(false)
|
|
75
|
+
const [savingEnabled, setSavingEnabled] = React.useState(false)
|
|
76
|
+
const [savingContinue, setSavingContinue] = React.useState(false)
|
|
77
|
+
const [removeBook, setRemoveBook] = React.useState(null)
|
|
78
|
+
const [removeError, setRemoveError] = React.useState(null)
|
|
79
|
+
const enabling = React.useRef(false)
|
|
80
|
+
const continuing = React.useRef(false)
|
|
81
|
+
const changingTypes = React.useRef(false)
|
|
82
|
+
const importing = React.useRef(false)
|
|
83
|
+
const removing = React.useRef(false)
|
|
84
|
+
const fileInput = React.useRef(null)
|
|
64
85
|
const selectedBook = state.books.find(book => book.id === state.settings.currentBookId)
|
|
65
86
|
const disabled = busy || !state.hydrated || Boolean(state.error)
|
|
66
87
|
const perform = async action => {
|
|
67
88
|
try { await action(); return true }
|
|
68
89
|
catch (error) { onSetNotice(t('settings.actionError', { message: errorMessage(error) })); return false }
|
|
69
90
|
}
|
|
91
|
+
const setEnabled = async enabled => {
|
|
92
|
+
if (disabled || enabling.current) return
|
|
93
|
+
enabling.current = true
|
|
94
|
+
setSavingEnabled(true)
|
|
95
|
+
try { await perform(() => onSetOption('enabled', enabled)) }
|
|
96
|
+
finally { enabling.current = false; setSavingEnabled(false) }
|
|
97
|
+
}
|
|
98
|
+
const setContinue = async enabled => {
|
|
99
|
+
if (disabled || continuing.current) return
|
|
100
|
+
continuing.current = true
|
|
101
|
+
setSavingContinue(true)
|
|
102
|
+
try { await perform(() => onSetOption('continueOnReopen', enabled)) }
|
|
103
|
+
finally { continuing.current = false; setSavingContinue(false) }
|
|
104
|
+
}
|
|
70
105
|
const setTypes = async types => {
|
|
106
|
+
if (disabled || changingTypes.current) return
|
|
107
|
+
changingTypes.current = true
|
|
71
108
|
setSavingTypes(true)
|
|
72
109
|
try { await perform(() => onSetOption('targetTypes', types)) }
|
|
73
|
-
finally { setSavingTypes(false) }
|
|
110
|
+
finally { changingTypes.current = false; setSavingTypes(false) }
|
|
74
111
|
}
|
|
75
112
|
const importFile = async event => {
|
|
76
113
|
const input = event.currentTarget
|
|
77
114
|
const file = input.files?.[0]
|
|
78
|
-
if (!file) return
|
|
115
|
+
if (!file || disabled || importing.current) return
|
|
116
|
+
importing.current = true
|
|
79
117
|
setBusy(true)
|
|
80
118
|
onSetNotice(t('settings.importing'))
|
|
81
119
|
try {
|
|
@@ -85,69 +123,91 @@ export function createSettingsRow(React) {
|
|
|
85
123
|
onSetNotice(t('settings.importError', { message: errorMessage(error) }))
|
|
86
124
|
} finally {
|
|
87
125
|
input.value = ''
|
|
126
|
+
importing.current = false
|
|
88
127
|
setBusy(false)
|
|
89
128
|
}
|
|
90
129
|
}
|
|
130
|
+
const closeRemove = () => { if (!removing.current) setRemoveBook(null) }
|
|
131
|
+
const confirmRemove = async () => {
|
|
132
|
+
if (!removeBook || disabled || removing.current) return
|
|
133
|
+
removing.current = true
|
|
134
|
+
setRemoveError(null)
|
|
135
|
+
setBusy(true)
|
|
136
|
+
try { await onDeleteBook(removeBook.id); setRemoveBook(null) }
|
|
137
|
+
catch (error) { setRemoveError(t('settings.actionError', { message: errorMessage(error) })) }
|
|
138
|
+
finally { removing.current = false; setBusy(false) }
|
|
139
|
+
}
|
|
91
140
|
|
|
92
|
-
return h('section', {
|
|
93
|
-
h('
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
h('input', { type: 'checkbox', disabled, checked: state.settings.enabled, onChange: event => { const enabled = event.currentTarget.checked; void perform(() => onSetOption('enabled', enabled)) } }),
|
|
101
|
-
t('settings.enabled'),
|
|
102
|
-
),
|
|
103
|
-
h('label', { style: LABEL },
|
|
104
|
-
h('span', null, t('settings.bookSource')),
|
|
105
|
-
h('input', { type: 'file', accept: '.epub,.txt,.md,application/epub+zip,text/plain,text/markdown', onChange: event => { void importFile(event) }, disabled, 'data-novel-file-input': true }),
|
|
106
|
-
),
|
|
107
|
-
h('label', { style: LABEL },
|
|
108
|
-
h('span', null, t('settings.selectBook')),
|
|
109
|
-
h('select', { style: CONTROL, disabled: disabled || !state.books.length, value: state.settings.currentBookId ?? '', 'data-novel-book-select': true,
|
|
110
|
-
onChange: event => { const id = event.currentTarget.value; void perform(() => onSelectBook(id)) } },
|
|
111
|
-
h('option', { value: '' }, t('settings.noBook')),
|
|
112
|
-
state.books.map(book => h('option', { key: book.id, value: book.id }, `${book.title} · ${book.format.toUpperCase()}`)),
|
|
141
|
+
return h('section', { className: 'novel-fish-settings', 'data-novel-settings': true },
|
|
142
|
+
h('style', null, STYLES),
|
|
143
|
+
!state.hydrated && !state.error ? h('div', { role: 'status' }, t('settings.storageLoading')) : null,
|
|
144
|
+
state.error ? h('div', { role: 'alert', className: 'novel-fish-error' }, state.error) : null,
|
|
145
|
+
h('section', { className: 'novel-fish-section' },
|
|
146
|
+
h('div', { className: 'novel-fish-row' },
|
|
147
|
+
h('h3', null, t('settings.books')),
|
|
148
|
+
h(Button, { variant: 'outline', disabled, 'data-novel-import': true, onClick: () => { if (!disabled) fileInput.current?.click() } }, t('settings.importBook')),
|
|
113
149
|
),
|
|
150
|
+
h('input', { ref: fileInput, hidden: true, type: 'file', accept: '.epub,.txt,.md,application/epub+zip,text/plain,text/markdown', onChange: event => { void importFile(event) }, disabled, 'data-novel-file-input': true }),
|
|
151
|
+
h('div', { className: 'novel-fish-field' },
|
|
152
|
+
h('span', null, t('settings.selectBook')),
|
|
153
|
+
h(SelectMenu, {
|
|
154
|
+
label: t('settings.selectBook'), value: state.settings.currentBookId ?? '', disabled: disabled || !state.books.length,
|
|
155
|
+
marker: { 'data-novel-book-select': true },
|
|
156
|
+
items: [{ id: '', label: t('settings.noBook') }, ...state.books.map(book => ({ id: book.id, label: `${book.title} · ${book.format.toUpperCase()}` }))],
|
|
157
|
+
onChange: id => { void perform(() => onSelectBook(id)) },
|
|
158
|
+
}),
|
|
159
|
+
),
|
|
160
|
+
selectedBook ? h('div', { className: 'novel-fish-row' },
|
|
161
|
+
h('span', { className: 'novel-fish-hint', 'data-novel-progress': true }, t('settings.progressValue', { progress: selectedBook.progress, total: selectedBook.segmentCount, percent: Math.floor(selectedBook.progress / selectedBook.segmentCount * 100) })),
|
|
162
|
+
h(Button, { variant: 'ghost', disabled, 'data-novel-delete': true, onClick: () => { setRemoveError(null); setRemoveBook(selectedBook) } }, t('settings.deleteBook')),
|
|
163
|
+
) : h('p', { className: 'novel-fish-hint' }, t('settings.emptyLibrary')),
|
|
114
164
|
),
|
|
115
|
-
|
|
116
|
-
h('
|
|
117
|
-
h('
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
),
|
|
165
|
+
h('section', { className: 'novel-fish-section' },
|
|
166
|
+
h('h3', null, t('settings.reading')),
|
|
167
|
+
h('div', { className: 'novel-fish-row' },
|
|
168
|
+
h('span', null, t('settings.enabled')),
|
|
169
|
+
h(Switch, { checked: state.settings.enabled, disabled: disabled || savingEnabled, label: t('settings.enabled'), onChange: enabled => { void setEnabled(enabled) } }),
|
|
170
|
+
),
|
|
171
|
+
h('div', { className: 'novel-fish-field' },
|
|
172
|
+
h('span', null, t('settings.targetType')),
|
|
173
|
+
h(SelectMenu, {
|
|
174
|
+
label: t('settings.targetType'), summary: t('settings.selectedTypes', { count: state.settings.targetTypes.length, total: TARGET_TYPES.length }),
|
|
175
|
+
value: state.settings.targetTypes, multiple: true, disabled: disabled || savingTypes,
|
|
176
|
+
marker: { 'data-novel-type-select': true },
|
|
177
|
+
items: TARGET_TYPES.map(type => ({ id: type, label: t(`settings.${TYPE_LABELS[type]}`) })),
|
|
178
|
+
footer: [{ id: '__all', label: t(state.settings.targetTypes.length === TARGET_TYPES.length ? 'settings.clearTypes' : 'settings.allTypes') }],
|
|
179
|
+
onChange: type => { void setTypes(type === '__all' ? (state.settings.targetTypes.length === TARGET_TYPES.length ? [] : [...TARGET_TYPES]) : TARGET_TYPES.filter(value => value === type ? !state.settings.targetTypes.includes(type) : state.settings.targetTypes.includes(value))) },
|
|
180
|
+
}),
|
|
181
|
+
h('p', { className: 'novel-fish-hint' }, t('settings.typeHint')),
|
|
182
|
+
),
|
|
183
|
+
h('div', { className: 'novel-fish-row' },
|
|
184
|
+
h('span', null, t('settings.continueOnReopen')),
|
|
185
|
+
h(Switch, { checked: state.settings.continueOnReopen === true, disabled: disabled || savingContinue, label: t('settings.continueOnReopen'), onChange: enabled => { void setContinue(enabled) } }),
|
|
136
186
|
),
|
|
137
|
-
h('
|
|
187
|
+
h('p', { className: 'novel-fish-hint' }, t('settings.continueOnReopenHint')),
|
|
138
188
|
),
|
|
139
|
-
h('
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
[
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
189
|
+
h('section', { className: 'novel-fish-section' },
|
|
190
|
+
h('h3', null, t('settings.appearance')),
|
|
191
|
+
h('div', { key: String(state.hydrated), className: 'novel-fish-grid' },
|
|
192
|
+
[
|
|
193
|
+
['mainTitle', 'settings.titleMain', 'title', MAIN_TITLES],
|
|
194
|
+
['extraTitle', 'settings.titleExtra', 'extra', EXTRA_TITLES],
|
|
195
|
+
].map(([key, label, testId, choices]) => h(PresetField, {
|
|
196
|
+
key, t, label: t(label), value: state.settings[key], choices, disabled, testId,
|
|
197
|
+
onChange: value => perform(() => onSetOption(key, value)),
|
|
198
|
+
})),
|
|
199
|
+
),
|
|
200
|
+
h('p', { className: 'novel-fish-hint' }, t('settings.nativeControls')),
|
|
147
201
|
),
|
|
148
|
-
h('span', { style: { color: 'var(--dsw-alias-label-tertiary)' } }, t('settings.nativeControls')),
|
|
149
|
-
h('span', { style: { color: 'var(--dsw-alias-label-tertiary)' } }, t('settings.presetHint')),
|
|
150
202
|
ui.notice ? h('div', { role: 'status', 'data-novel-notice': true }, ui.notice) : null,
|
|
203
|
+
h(Modal, {
|
|
204
|
+
open: Boolean(removeBook), onClose: closeRemove, title: t('settings.deleteBook'), closeLabel: t('settings.cancel'),
|
|
205
|
+
description: removeBook ? t('settings.removeConfirm', { title: removeBook.title }) : '',
|
|
206
|
+
footer: h(React.Fragment, null,
|
|
207
|
+
h(Button, { variant: 'outline', disabled: busy, onClick: closeRemove }, t('settings.cancel')),
|
|
208
|
+
h(Button, { variant: 'primary', disabled: disabled || !removeBook, 'data-novel-delete-confirm': true, onClick: () => { void confirmRemove() } }, t('settings.confirmDelete')),
|
|
209
|
+
),
|
|
210
|
+
}, removeError ? h('p', { role: 'alert' }, removeError) : null),
|
|
151
211
|
)
|
|
152
212
|
}
|
|
153
213
|
|
package/src/store.js
CHANGED
|
@@ -10,6 +10,7 @@ export const DEFAULT_SETTINGS = Object.freeze({
|
|
|
10
10
|
targetTypes: Object.freeze(['reasoning']),
|
|
11
11
|
mainTitle: '',
|
|
12
12
|
extraTitle: '',
|
|
13
|
+
continueOnReopen: false,
|
|
13
14
|
})
|
|
14
15
|
|
|
15
16
|
const EMPTY_STATUS = Object.freeze({ kind: 'empty' })
|
|
@@ -76,15 +77,17 @@ export class NovelStore {
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
async setOption(key, value) {
|
|
79
|
-
if (!['enabled', 'targetTypes', 'mainTitle', 'extraTitle'].includes(key)) throw new Error('未知设置项')
|
|
80
|
+
if (!['enabled', 'targetTypes', 'mainTitle', 'extraTitle', 'continueOnReopen'].includes(key)) throw new Error('未知设置项')
|
|
80
81
|
let normalized
|
|
81
82
|
if (key === 'targetTypes') {
|
|
82
83
|
if (!Array.isArray(value) || value.some(type => !TARGET_TYPES.includes(type)) || new Set(value).size !== value.length) {
|
|
83
84
|
throw new Error('阅读类型必须是有效且不重复的类型数组')
|
|
84
85
|
}
|
|
85
86
|
normalized = Object.freeze([...value])
|
|
87
|
+
} else if (key === 'enabled' || key === 'continueOnReopen') {
|
|
88
|
+
normalized = Boolean(value)
|
|
86
89
|
} else {
|
|
87
|
-
normalized =
|
|
90
|
+
normalized = String(value)
|
|
88
91
|
}
|
|
89
92
|
// Invalidate at intent time, not after a queued settings write finishes.
|
|
90
93
|
// Any type update also covers removals from settings writes still in flight.
|
|
@@ -142,7 +145,22 @@ export class NovelStore {
|
|
|
142
145
|
})
|
|
143
146
|
}
|
|
144
147
|
|
|
148
|
+
/** Reopening a region shows the passage it already holds; a first expansion reads the next one. */
|
|
145
149
|
async claimPassage(stepKey, targetType = 'reasoning') {
|
|
150
|
+
return this.#readPassage(stepKey, targetType, false)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Same contract as claimPassage, except a region that already shows a passage
|
|
155
|
+
* advances to the next one. Only an explicit reopen can reach this path, so a
|
|
156
|
+
* repeated expansion still cannot paginate by accident while long reads
|
|
157
|
+
* continue inside the same native row.
|
|
158
|
+
*/
|
|
159
|
+
async continuePassage(stepKey, targetType = 'reasoning') {
|
|
160
|
+
return this.#readPassage(stepKey, targetType, true)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async #readPassage(stepKey, targetType, advance) {
|
|
146
164
|
if (this.disposed) return EMPTY_STATUS
|
|
147
165
|
const generation = this.cacheGeneration
|
|
148
166
|
const requestKey = `${generation}\u0000${targetType}\u0000${stepKey}`
|
|
@@ -156,7 +174,8 @@ export class NovelStore {
|
|
|
156
174
|
bookId = this.state.settings.currentBookId
|
|
157
175
|
if (!bookId) return EMPTY_STATUS
|
|
158
176
|
const existing = this.getPassageStatus(bookId, stepKey)
|
|
159
|
-
if (existing.kind === '
|
|
177
|
+
if (existing.kind === 'end') return existing
|
|
178
|
+
if (!advance && existing.kind === 'ready') return existing
|
|
160
179
|
const book = this.state.books.find(item => item.id === bookId)
|
|
161
180
|
if (!book || book.progress >= book.segmentCount) {
|
|
162
181
|
return this.#writePassageStatus(bookId, stepKey, { kind: 'end', bookId })
|
|
@@ -34,8 +34,8 @@ function installDom(dom) {
|
|
|
34
34
|
function adapterFixture(content = thought('node-a'), session = 'session-a') {
|
|
35
35
|
const dom = new JSDOM(`<body><div data-conversation-session="${session}">${content}</div></body>`)
|
|
36
36
|
const restoreDom = installDom(dom)
|
|
37
|
-
let settings = { enabled: true, currentBookId: 'book', targetTypes: ['reasoning'], mainTitle: '思考', extraTitle: 'Checking project structure' }
|
|
38
|
-
const claims = [], types = [], effects = [], actions = {}
|
|
37
|
+
let settings = { enabled: true, currentBookId: 'book', targetTypes: ['reasoning'], mainTitle: '思考', extraTitle: 'Checking project structure', continueOnReopen: false }
|
|
38
|
+
const claims = [], continues = [], types = [], effects = [], actions = {}
|
|
39
39
|
const statuses = new Map()
|
|
40
40
|
let passagesChanged
|
|
41
41
|
const add = document.addEventListener.bind(document)
|
|
@@ -48,10 +48,11 @@ function adapterFixture(content = thought('node-a'), session = 'session-a') {
|
|
|
48
48
|
subscribePassages: callback => { passagesChanged = callback; return () => {} },
|
|
49
49
|
getPassageStatus: (_book, key) => statuses.get(key) ?? ({ kind: 'empty' }),
|
|
50
50
|
claimPassage: (key, type) => { claims.push(key); types.push(type); return Promise.resolve() },
|
|
51
|
+
continuePassage: (key, type) => { continues.push(key); types.push(type); return Promise.resolve() },
|
|
51
52
|
}
|
|
52
53
|
const ctx = { slots: { inject: (_key, fn) => fn(), register: () => () => {} }, effect: fn => { effects.push(fn()) } }
|
|
53
54
|
return {
|
|
54
|
-
dom, claims, types, actions, start: () => registerChatAdapter(ctx, store, React, 'novel'),
|
|
55
|
+
dom, claims, continues, types, actions, start: () => registerChatAdapter(ctx, store, React, 'novel'),
|
|
55
56
|
passage: (key, status) => { statuses.set(key, status); passagesChanged() },
|
|
56
57
|
invalidate: () => { store.cacheGeneration += 1 },
|
|
57
58
|
settings: patch => { settings = { ...settings, ...patch }; settingsChanged() },
|
|
@@ -373,3 +374,60 @@ test('native thought remains visible when its DOM identity is unavailable', asyn
|
|
|
373
374
|
assert.equal(view.root.findAllByProps({ 'data-novel-append': true }).length, 0)
|
|
374
375
|
} finally { await act(async () => view?.unmount()); dom.window.close() }
|
|
375
376
|
})
|
|
377
|
+
|
|
378
|
+
test('默认关闭续读时同一区域重复主动展开仍走缓存 claim', async () => {
|
|
379
|
+
const f = adapterFixture(thought('node-a'))
|
|
380
|
+
try {
|
|
381
|
+
f.start()
|
|
382
|
+
const header = document.querySelector('[data-disclosure-row]')
|
|
383
|
+
for (let index = 0; index < 2; index += 1) {
|
|
384
|
+
header.setAttribute('aria-expanded', 'false')
|
|
385
|
+
f.actions.click(trustedClick(header))
|
|
386
|
+
header.setAttribute('aria-expanded', 'true')
|
|
387
|
+
await settle()
|
|
388
|
+
}
|
|
389
|
+
assert.equal(f.continues.length, 0)
|
|
390
|
+
assert.deepEqual(f.claims, [readingKey('session-a', 'node-a'), readingKey('session-a', 'node-a')])
|
|
391
|
+
} finally { f.close() }
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
test('开启续读后同一区域主动展开改为继续下一段,非可信事件仍不读', async () => {
|
|
395
|
+
const f = adapterFixture(thought('node-a'))
|
|
396
|
+
try {
|
|
397
|
+
f.start()
|
|
398
|
+
f.settings({ continueOnReopen: true })
|
|
399
|
+
const header = document.querySelector('[data-disclosure-row]')
|
|
400
|
+
header.setAttribute('aria-expanded', 'false')
|
|
401
|
+
f.actions.click({ ...trustedClick(header), isTrusted: false })
|
|
402
|
+
header.setAttribute('aria-expanded', 'true')
|
|
403
|
+
await settle()
|
|
404
|
+
assert.equal(f.continues.length, 0, '未验证的用户事件不推进阅读')
|
|
405
|
+
for (let index = 0; index < 2; index += 1) {
|
|
406
|
+
header.setAttribute('aria-expanded', 'false')
|
|
407
|
+
f.actions.click(trustedClick(header))
|
|
408
|
+
header.setAttribute('aria-expanded', 'true')
|
|
409
|
+
await settle()
|
|
410
|
+
}
|
|
411
|
+
assert.deepEqual(f.continues, [readingKey('session-a', 'node-a'), readingKey('session-a', 'node-a')])
|
|
412
|
+
assert.equal(f.claims.length, 0)
|
|
413
|
+
} finally { f.close() }
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
test('流式文本和属性变更在同一帧内只合并成一次重扫', async () => {
|
|
417
|
+
const f = adapterFixture(thought('node-a'))
|
|
418
|
+
const frames = []
|
|
419
|
+
let cancelled = 0
|
|
420
|
+
f.dom.window.requestAnimationFrame = callback => { frames.push(callback); return frames.length }
|
|
421
|
+
f.dom.window.cancelAnimationFrame = () => { cancelled += 1 }
|
|
422
|
+
try {
|
|
423
|
+
f.start()
|
|
424
|
+
const title = document.querySelector('.native-title')
|
|
425
|
+
for (let index = 0; index < 4; index += 1) title.firstChild.nodeValue = `stream ${index}`
|
|
426
|
+
document.querySelector('[data-disclosure-row]').setAttribute('data-state', 'open')
|
|
427
|
+
await settle()
|
|
428
|
+
assert.equal(frames.length, 1, '一帧内的多次变更共享一次刷新,而不是每个 token 全量扫描')
|
|
429
|
+
frames[0]()
|
|
430
|
+
assert.equal(title.textContent, '思考')
|
|
431
|
+
assert.equal(cancelled, 0)
|
|
432
|
+
} finally { f.close() }
|
|
433
|
+
})
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { readFile } from 'node:fs/promises'
|
|
4
|
+
import vm from 'node:vm'
|
|
5
|
+
import React from 'react'
|
|
6
|
+
import TestRenderer, { act } from 'react-test-renderer'
|
|
7
|
+
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'
|
|
8
|
+
import { IndexedDbStorage } from '../src/persistence.js'
|
|
9
|
+
import { DEFAULT_SETTINGS } from '../src/store.js'
|
|
10
|
+
|
|
11
|
+
import { HostSwitch, hostPrimitives } from './helpers/host-primitives.js'
|
|
12
|
+
|
|
13
|
+
test('构建产物只在本包详情注册设置,复用宿主 Switch 与原有书库', async () => {
|
|
14
|
+
const indexedDB = new IDBFactory()
|
|
15
|
+
const storage = new IndexedDbStorage(indexedDB, IDBKeyRange)
|
|
16
|
+
const book = { id: 'existing-book', title: '已有书籍', format: 'txt', progress: 1, segmentCount: 2 }
|
|
17
|
+
const settings = { ...DEFAULT_SETTINGS, enabled: false, currentBookId: book.id, mainTitle: '已保存标题' }
|
|
18
|
+
await storage.addBook(book, [{ text: 'A' }, { text: 'B' }])
|
|
19
|
+
await storage.saveSettings(settings)
|
|
20
|
+
const imports = [], effects = [], injections = [], registrations = []
|
|
21
|
+
let module
|
|
22
|
+
vm.runInNewContext(await readFile(new URL('../lib/client.js', import.meta.url), 'utf8'), {
|
|
23
|
+
window: { __ModuleLoader__: { load: value => { module = value } } },
|
|
24
|
+
indexedDB, IDBKeyRange, TextDecoder, TextEncoder, setTimeout, clearTimeout,
|
|
25
|
+
console,
|
|
26
|
+
})
|
|
27
|
+
const plugin = module.factory(name => {
|
|
28
|
+
imports.push(name)
|
|
29
|
+
if (name === 'react') return React
|
|
30
|
+
if (name === '@deepseek-ai/dsh-client-ui-primitives') return hostPrimitives
|
|
31
|
+
throw new Error(`Unexpected host import: ${name}`)
|
|
32
|
+
})
|
|
33
|
+
plugin.apply({
|
|
34
|
+
locale: { register: () => () => {}, bind: () => key => key },
|
|
35
|
+
effect: callback => { effects.push(callback) },
|
|
36
|
+
slots: {
|
|
37
|
+
inject: (name, callback) => { injections.push(name); callback() },
|
|
38
|
+
register: (options, Component) => { registrations.push({ options, Component }); return () => {} },
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
const own = registrations.find(entry => entry.options.name === 'plugins.bundle.config')
|
|
42
|
+
let view
|
|
43
|
+
try {
|
|
44
|
+
assert.equal(module.id, '@gongfpp/dsh-novel-fish')
|
|
45
|
+
assert.ok(imports.includes('@deepseek-ai/dsh-client-ui-primitives'))
|
|
46
|
+
assert.equal(injections.includes('settings.general.item'), false)
|
|
47
|
+
assert.equal(own.options.key, module.id)
|
|
48
|
+
assert.equal(registrations.filter(entry => entry.options.name === 'plugins.bundle.config').length, 1)
|
|
49
|
+
assert.ok(injections.includes('conversation.chat.reasoning.body'))
|
|
50
|
+
const injected = own.options.inject()
|
|
51
|
+
const source = injected.hooks.novelState
|
|
52
|
+
await new Promise((resolve, reject) => {
|
|
53
|
+
const timer = setTimeout(() => { off(); reject(new Error('Book hydration timed out')) }, 2000)
|
|
54
|
+
const check = () => { if (source.getSnapshot().hydrated) { off(); clearTimeout(timer); resolve() } }
|
|
55
|
+
const off = source.subscribe(check)
|
|
56
|
+
check()
|
|
57
|
+
})
|
|
58
|
+
assert.equal(source.getSnapshot().error, null)
|
|
59
|
+
assert.equal(source.getSnapshot().books[0].progress, 1)
|
|
60
|
+
assert.equal(source.getSnapshot().settings.mainTitle, settings.mainTitle)
|
|
61
|
+
const props = { ...injected, t: key => key, view: 'page',
|
|
62
|
+
useNovelState: select => select(React.useSyncExternalStore(source.subscribe, source.getSnapshot)),
|
|
63
|
+
useNovelUi: select => select(React.useSyncExternalStore(injected.hooks.novelUi.subscribe, injected.hooks.novelUi.getSnapshot)),
|
|
64
|
+
}
|
|
65
|
+
await act(async () => { view = TestRenderer.create(React.createElement(own.Component, props)) })
|
|
66
|
+
const enabledSwitch = () => view.root.findAllByType(HostSwitch).find(node => node.props.label === 'settings.enabled')
|
|
67
|
+
assert.equal(enabledSwitch().props.checked, false)
|
|
68
|
+
assert.equal(source.getSnapshot().books[0].progress, 1)
|
|
69
|
+
await act(async () => { await injected.onSetOption('enabled', true) })
|
|
70
|
+
assert.equal(enabledSwitch().props.checked, true)
|
|
71
|
+
const saved = await storage.load()
|
|
72
|
+
assert.equal(saved.settings.enabled, true)
|
|
73
|
+
assert.equal(saved.settings.currentBookId, book.id)
|
|
74
|
+
assert.equal(saved.settings.mainTitle, settings.mainTitle)
|
|
75
|
+
assert.equal(saved.books[0].progress, 1)
|
|
76
|
+
assert.deepEqual(await storage.getSegment(book.id, 1), { text: 'B' })
|
|
77
|
+
} finally {
|
|
78
|
+
if (view) await act(async () => view.unmount())
|
|
79
|
+
effects[1]()() // Dispose only the store effect; do not install chat DOM observers.
|
|
80
|
+
storage.close()
|
|
81
|
+
}
|
|
82
|
+
})
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
// Contract doubles only. Appearance, focus, portals and keyboard behavior are
|
|
4
|
+
// verified against the actual host; these doubles must not claim to test them.
|
|
5
|
+
export function HostSwitch({ checked, disabled, label, onChange }) {
|
|
6
|
+
return React.createElement('button', { role: 'switch', 'aria-checked': checked, 'aria-label': label, disabled, onClick: () => onChange(!checked) })
|
|
7
|
+
}
|
|
8
|
+
export const HostButton = React.forwardRef(function HostButton({ variant, size, children, ...props }, ref) {
|
|
9
|
+
return React.createElement('button', { type: 'button', ...props, ref, 'data-host-variant': variant }, children)
|
|
10
|
+
})
|
|
11
|
+
export const HostInput = React.forwardRef(function HostInput(props, ref) {
|
|
12
|
+
return React.createElement('input', { ...props, ref })
|
|
13
|
+
})
|
|
14
|
+
export function HostMenu({ anchor, open, items, footer = [], selectedId, selectedIds = [], onSelect }) {
|
|
15
|
+
return React.createElement('div', null, anchor, open ? React.createElement('div', { role: 'menu' },
|
|
16
|
+
[...items, ...footer].map(item => React.createElement('button', {
|
|
17
|
+
key: item.id, role: 'menuitem', disabled: item.disabled,
|
|
18
|
+
'data-selected': selectedId === item.id || selectedIds.includes(item.id),
|
|
19
|
+
onClick: () => onSelect(item.id),
|
|
20
|
+
}, item.label)),
|
|
21
|
+
) : null)
|
|
22
|
+
}
|
|
23
|
+
export function HostModal({ open, title, description, children, footer }) {
|
|
24
|
+
return open ? React.createElement('div', { role: 'dialog', 'aria-label': title }, description, children, footer) : null
|
|
25
|
+
}
|
|
26
|
+
function Chevron() { return React.createElement('svg', { 'aria-hidden': true }) }
|
|
27
|
+
|
|
28
|
+
export const hostPrimitives = { Switch: HostSwitch, Button: HostButton, Input: HostInput, Menu: HostMenu, Modal: HostModal, IconChevronDownOutlineRegular: Chevron }
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
|
|
6
|
+
const script = fileURLToPath(new URL('../scripts/check-host-contracts.mjs', import.meta.url))
|
|
7
|
+
const run = args => spawnSync(process.execPath, [script, ...args], {
|
|
8
|
+
encoding: 'utf8', timeout: 30000, env: { ...process.env, HOST_ROOT: '' },
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('host-contract probe rejects relative installation paths with a JSON failure', () => {
|
|
12
|
+
const result = run(['relative/host'])
|
|
13
|
+
assert.equal(result.status, 1)
|
|
14
|
+
const report = JSON.parse(result.stdout)
|
|
15
|
+
assert.equal(report.ok, false)
|
|
16
|
+
assert.match(report.error, /absolute DSH installation path/)
|
|
17
|
+
assert.equal(report.claims, null, 'failed setup must not claim a measured result')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('host-contract probe exercises a real installation when HOST_ROOT is supplied', {
|
|
21
|
+
skip: process.env.HOST_ROOT ? false : 'Set HOST_ROOT to an absolute installed DSH directory to run real-host integration',
|
|
22
|
+
}, () => {
|
|
23
|
+
const result = run([process.env.HOST_ROOT])
|
|
24
|
+
assert.equal(result.status, 0, result.stdout + result.stderr)
|
|
25
|
+
const report = JSON.parse(result.stdout)
|
|
26
|
+
assert.equal(report.ok, true)
|
|
27
|
+
assert.equal(report.claims, 0)
|
|
28
|
+
assert.ok(report.checks.some(check => check.name === 'real SlotCore registrations'))
|
|
29
|
+
assert.ok(report.checks.some(check => check.name === 'real ReasoningRow / DisclosureRow / TextShimmer'))
|
|
30
|
+
assert.ok(report.checks.some(check => check.name === 'adapter anchors for title layers and fold state'))
|
|
31
|
+
assert.ok(report.limitations.some(limit => limit.includes('trusted-user-event')))
|
|
32
|
+
})
|