dsh-rwkv-plugin 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +50 -2
- package/cordis.patch.yml +59 -0
- package/lib/client.js +3148 -0
- package/lib/client.js.map +7 -0
- package/lib/index.js +6294 -0
- package/lib/index.js.map +7 -0
- package/lib/line-edit.js +1423 -0
- package/lib/line-edit.js.map +7 -0
- package/lib/todo.js +610 -0
- package/lib/todo.js.map +7 -0
- package/package.json +72 -4
- package/scripts/build.mjs +77 -0
- package/scripts/headless.ts +279 -0
- package/scripts/link-dsh.sh +31 -0
- package/scripts/relay-e2e.mjs +157 -0
- package/scripts/smoke.ts +116 -0
- package/scripts/state.ts +39 -0
- package/scripts/tokenizer-parity.ts +107 -0
- package/scripts/trace-stats.ts +33 -0
- package/src/adapter.ts +388 -0
- package/src/backend/generator.ts +221 -0
- package/src/backend/irwkv.ts +117 -0
- package/src/backend/lightning.ts +228 -0
- package/src/backend/stream.ts +97 -0
- package/src/client/AdapterPicker.tsx +264 -0
- package/src/client/DeveloperPanel.tsx +403 -0
- package/src/client/FoldNode.tsx +82 -0
- package/src/client/ModelCard.tsx +230 -0
- package/src/client/RwkvSettings.tsx +265 -0
- package/src/client/Segment.tsx +15 -0
- package/src/client/StatePicker.tsx +248 -0
- package/src/client/TraceFrames.tsx +462 -0
- package/src/client/TraceView.tsx +77 -0
- package/src/client/WirePanel.tsx +566 -0
- package/src/client/fields.tsx +362 -0
- package/src/client/index.tsx +109 -0
- package/src/client/styles.ts +277 -0
- package/src/client/traces-api.ts +222 -0
- package/src/client/url.ts +20 -0
- package/src/core/agent.ts +586 -0
- package/src/core/defaults.ts +51 -0
- package/src/core/dsh-prompt.ts +32 -0
- package/src/core/edit-modes/index.ts +108 -0
- package/src/core/edit-modes/line-edit.ts +793 -0
- package/src/core/edit-modes/literal.ts +26 -0
- package/src/core/edit-modes/shared.ts +213 -0
- package/src/core/edit-modes/tolerant.ts +558 -0
- package/src/core/fold-pairs.ts +407 -0
- package/src/core/fold-window.ts +563 -0
- package/src/core/line-edit.ts +33 -0
- package/src/core/parse.ts +290 -0
- package/src/core/policy.ts +500 -0
- package/src/core/prompt.ts +220 -0
- package/src/core/relay.ts +400 -0
- package/src/core/step-gates.ts +371 -0
- package/src/core/time-zone.ts +32 -0
- package/src/core/todo-state.ts +227 -0
- package/src/core/tolerant-edit.ts +14 -0
- package/src/core/tool-mappings.ts +209 -0
- package/src/core/tool-repairs.ts +255 -0
- package/src/core/tools.ts +162 -0
- package/src/core/worklist.ts +606 -0
- package/src/dsh-host.ts +48 -0
- package/src/edit-modes/folded-read-guard.ts +32 -0
- package/src/edit-modes/index.ts +26 -0
- package/src/edit-modes/line-edit.ts +154 -0
- package/src/edit-modes/literal.ts +9 -0
- package/src/edit-modes/observations.ts +234 -0
- package/src/edit-modes/syntax-check.ts +226 -0
- package/src/edit-modes/tolerant.ts +232 -0
- package/src/folded-read-guard.ts +6 -0
- package/src/history.ts +163 -0
- package/src/index.ts +287 -0
- package/src/line-edit.ts +15 -0
- package/src/live-wire.ts +133 -0
- package/src/paths.ts +91 -0
- package/src/relay-store.ts +59 -0
- package/src/result-budget.ts +225 -0
- package/src/routes.ts +324 -0
- package/src/session-setup.ts +150 -0
- package/src/syntax-check.ts +2 -0
- package/src/todo.ts +328 -0
- package/src/tokenizer.ts +48 -0
- package/src/tolerant-edit.ts +2 -0
- package/src/trace-stats.ts +225 -0
- package/src/traces.ts +289 -0
- package/vendor/rwkv-tokenizer/NOTICE.md +26 -0
- package/vendor/rwkv-tokenizer/package.json +5 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer.d.ts +11 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer.js +280 -0
- package/vendor/rwkv-tokenizer/rwkv_tokenizer_bg.wasm +0 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// Adapter choice for an rwkv_lightning model: the adapters the server
|
|
2
|
+
// already has (auto-fetched), "use" one or the base model, upload a
|
|
3
|
+
// local file with its id suggested from the file name, delete with a
|
|
4
|
+
// confirmation. The Host relays every call (see src/routes.ts).
|
|
5
|
+
|
|
6
|
+
import { useEffect, useRef, useState } from 'react'
|
|
7
|
+
import { isHttpUrl } from './url.ts'
|
|
8
|
+
|
|
9
|
+
const ROUTE = '/api/rwkv/adapters'
|
|
10
|
+
/** Same as MAX_ADAPTER_UPLOAD_BYTES in the Host: the server's request-body cap. */
|
|
11
|
+
const MAX_UPLOAD_BYTES = 512 << 20
|
|
12
|
+
|
|
13
|
+
export interface AdapterConnection {
|
|
14
|
+
baseURL: string
|
|
15
|
+
apiKey?: string | undefined
|
|
16
|
+
headers?: Record<string, string> | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface ServerAdapter {
|
|
20
|
+
id: string
|
|
21
|
+
method: string
|
|
22
|
+
rank?: number
|
|
23
|
+
version: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function connectionHeader(connection: AdapterConnection): string {
|
|
27
|
+
const bytes = new TextEncoder().encode(JSON.stringify({
|
|
28
|
+
baseURL: connection.baseURL.trim(),
|
|
29
|
+
...connection.apiKey ? { apiKey: connection.apiKey } : {},
|
|
30
|
+
...connection.headers ? { headers: connection.headers } : {},
|
|
31
|
+
}))
|
|
32
|
+
let binary = ''
|
|
33
|
+
for (const byte of bytes) binary += String.fromCharCode(byte)
|
|
34
|
+
return btoa(binary)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function failureOf(response: Response): Promise<string> {
|
|
38
|
+
try {
|
|
39
|
+
const body = await response.json() as { error?: string }
|
|
40
|
+
return body.error || `HTTP ${response.status}`
|
|
41
|
+
} catch {
|
|
42
|
+
return `HTTP ${response.status}`
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** MiSS / NoRA from the server manifest; anything else upper-cased. */
|
|
47
|
+
function methodLabel(method: string): string {
|
|
48
|
+
const value = method.trim().toLowerCase()
|
|
49
|
+
if (value === 'miss') return 'MiSS'
|
|
50
|
+
if (value === 'nora') return 'NoRA'
|
|
51
|
+
return value.toUpperCase()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Training outputs share fixed file names (adapter-final.pth,
|
|
56
|
+
* checkpoint-N/training.pth); the directory tells them apart. A browser file
|
|
57
|
+
* carries only its name, so that is all this sees in practice.
|
|
58
|
+
*/
|
|
59
|
+
export function suggestID(path: string): string {
|
|
60
|
+
const parts = path.trim().split(/[\\/]+/).filter(Boolean)
|
|
61
|
+
const stem = (parts.at(-1) || '').replace(/\.pth$/i, '')
|
|
62
|
+
if (stem === 'adapter-final' || stem === 'training') {
|
|
63
|
+
const parent = parts.at(-2) || ''
|
|
64
|
+
if (/^checkpoint-\d+$/.test(parent) && parts.length >= 3) return `${parts.at(-3)}-${parent}`
|
|
65
|
+
return parent || stem
|
|
66
|
+
}
|
|
67
|
+
return stem
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function AdapterPicker(props: {
|
|
71
|
+
connection: AdapterConnection
|
|
72
|
+
value: string | undefined
|
|
73
|
+
disabled: boolean
|
|
74
|
+
onChange: (id: string | undefined) => void
|
|
75
|
+
}) {
|
|
76
|
+
const { connection } = props
|
|
77
|
+
const ready = isHttpUrl(connection.baseURL)
|
|
78
|
+
const key = JSON.stringify(connection)
|
|
79
|
+
const [adapters, setAdapters] = useState<ServerAdapter[] | undefined>()
|
|
80
|
+
const [loading, setLoading] = useState(false)
|
|
81
|
+
const [error, setError] = useState('')
|
|
82
|
+
const [pending, setPending] = useState<{ file: File; id: string } | undefined>()
|
|
83
|
+
const [customInput, setCustomInput] = useState(false)
|
|
84
|
+
const [customId, setCustomId] = useState('')
|
|
85
|
+
const [progress, setProgress] = useState<number | undefined>()
|
|
86
|
+
const [deleting, setDeleting] = useState<string | undefined>()
|
|
87
|
+
const fileInput = useRef<HTMLInputElement>(null)
|
|
88
|
+
const request = useRef(0)
|
|
89
|
+
|
|
90
|
+
const refresh = async () => {
|
|
91
|
+
if (!ready) return
|
|
92
|
+
const current = ++request.current
|
|
93
|
+
setLoading(true)
|
|
94
|
+
setError('')
|
|
95
|
+
try {
|
|
96
|
+
const response = await fetch(ROUTE, { headers: { 'x-rwkv-connection': connectionHeader(connection) }, credentials: 'same-origin' })
|
|
97
|
+
if (!response.ok) throw new Error(await failureOf(response))
|
|
98
|
+
const body = await response.json() as { adapters: ServerAdapter[] }
|
|
99
|
+
if (current === request.current) setAdapters(body.adapters)
|
|
100
|
+
} catch (cause) {
|
|
101
|
+
if (current === request.current) {
|
|
102
|
+
setAdapters(undefined)
|
|
103
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
104
|
+
}
|
|
105
|
+
} finally {
|
|
106
|
+
if (current === request.current) setLoading(false)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
setAdapters(undefined)
|
|
112
|
+
if (!ready) return
|
|
113
|
+
const timer = setTimeout(() => { void refresh() }, 400)
|
|
114
|
+
return () => clearTimeout(timer)
|
|
115
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
|
+
}, [key, ready])
|
|
117
|
+
|
|
118
|
+
const upload = (file: File, id: string) => {
|
|
119
|
+
if (file.size > MAX_UPLOAD_BYTES) {
|
|
120
|
+
setError(`文件 ${(file.size / 1048576).toFixed(0)} MB,超过 ${MAX_UPLOAD_BYTES >> 20} MB 上限;请上传 adapter-final.pth,不要传完整训练 checkpoint`)
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
setError('')
|
|
124
|
+
setProgress(0)
|
|
125
|
+
const xhr = new XMLHttpRequest()
|
|
126
|
+
xhr.open('PUT', `${ROUTE}/${encodeURIComponent(id)}?name=${encodeURIComponent(file.name)}`)
|
|
127
|
+
xhr.setRequestHeader('x-rwkv-connection', connectionHeader(connection))
|
|
128
|
+
xhr.setRequestHeader('content-type', 'application/octet-stream')
|
|
129
|
+
xhr.upload.onprogress = event => {
|
|
130
|
+
if (event.lengthComputable) setProgress(Math.round((event.loaded * 100) / event.total))
|
|
131
|
+
}
|
|
132
|
+
xhr.onload = () => {
|
|
133
|
+
setProgress(undefined)
|
|
134
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
135
|
+
setPending(undefined)
|
|
136
|
+
props.onChange(id)
|
|
137
|
+
void refresh()
|
|
138
|
+
} else {
|
|
139
|
+
let message = `HTTP ${xhr.status}`
|
|
140
|
+
try { message = (JSON.parse(xhr.responseText) as { error?: string }).error || message } catch { /* keep the status */ }
|
|
141
|
+
setError(message)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
xhr.onerror = () => {
|
|
145
|
+
setProgress(undefined)
|
|
146
|
+
setError('上传中断:连接被断开')
|
|
147
|
+
}
|
|
148
|
+
xhr.send(file)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const remove = async (id: string) => {
|
|
152
|
+
setDeleting(undefined)
|
|
153
|
+
setError('')
|
|
154
|
+
try {
|
|
155
|
+
const response = await fetch(`${ROUTE}/${encodeURIComponent(id)}`, {
|
|
156
|
+
method: 'DELETE', headers: { 'x-rwkv-connection': connectionHeader(connection) }, credentials: 'same-origin',
|
|
157
|
+
})
|
|
158
|
+
if (!response.ok) throw new Error(await failureOf(response))
|
|
159
|
+
if (props.value === id) props.onChange(undefined)
|
|
160
|
+
await refresh()
|
|
161
|
+
} catch (cause) {
|
|
162
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const current = props.value?.trim() || ''
|
|
167
|
+
const busy = props.disabled || loading || progress !== undefined
|
|
168
|
+
const missing = adapters !== undefined && current !== '' && !adapters.some(adapter => adapter.id === current)
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<div className="rwkv-field">
|
|
172
|
+
<div className="rwkv-block-head">
|
|
173
|
+
<span className="rwkv-label">Adapter</span>
|
|
174
|
+
<span className="rwkv-actions">
|
|
175
|
+
<button type="button" className="rwkv-link" disabled={busy || !ready} onClick={() => { void refresh() }}>{loading ? '查询中…' : '刷新'}</button>
|
|
176
|
+
<button type="button" className="rwkv-link" disabled={props.disabled} onClick={() => setCustomInput(!customInput)}>
|
|
177
|
+
{customInput ? '取消输入' : '手填 ID'}
|
|
178
|
+
</button>
|
|
179
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small" disabled={busy || !ready} onClick={() => fileInput.current?.click()}>上传</button>
|
|
180
|
+
</span>
|
|
181
|
+
<input
|
|
182
|
+
ref={fileInput} type="file" accept=".pth" hidden
|
|
183
|
+
onChange={event => {
|
|
184
|
+
const file = event.target.files?.[0]
|
|
185
|
+
event.target.value = ''
|
|
186
|
+
if (file) setPending({ file, id: suggestID(file.name) })
|
|
187
|
+
}}
|
|
188
|
+
/>
|
|
189
|
+
</div>
|
|
190
|
+
{customInput ? (
|
|
191
|
+
<div className="rwkv-upload-row" style={{ marginBottom: 8 }}>
|
|
192
|
+
<input
|
|
193
|
+
className="rwkv-input"
|
|
194
|
+
value={customId}
|
|
195
|
+
spellCheck={false}
|
|
196
|
+
placeholder="输入已有的 adapter ID"
|
|
197
|
+
disabled={props.disabled}
|
|
198
|
+
onChange={event => setCustomId(event.target.value)}
|
|
199
|
+
/>
|
|
200
|
+
<button
|
|
201
|
+
type="button"
|
|
202
|
+
className="rwkv-btn rwkv-primary rwkv-small"
|
|
203
|
+
disabled={!customId.trim()}
|
|
204
|
+
onClick={() => {
|
|
205
|
+
props.onChange(customId.trim() || undefined)
|
|
206
|
+
setCustomInput(false)
|
|
207
|
+
setCustomId('')
|
|
208
|
+
}}
|
|
209
|
+
>
|
|
210
|
+
使用
|
|
211
|
+
</button>
|
|
212
|
+
</div>
|
|
213
|
+
) : null}
|
|
214
|
+
<div className="rwkv-list">
|
|
215
|
+
<button type="button" className="rwkv-option" aria-pressed={current === ''} disabled={props.disabled} onClick={() => props.onChange(undefined)}>
|
|
216
|
+
<span className="rwkv-option-name">不用 adapter</span>
|
|
217
|
+
{current === '' ? <span className="rwkv-option-state">使用中</span> : null}
|
|
218
|
+
</button>
|
|
219
|
+
{(adapters ?? []).map(adapter => (
|
|
220
|
+
<div key={adapter.id} className="rwkv-option" aria-pressed={current === adapter.id}>
|
|
221
|
+
<button type="button" className="rwkv-option-main" disabled={props.disabled} onClick={() => props.onChange(adapter.id)}>
|
|
222
|
+
<span className="rwkv-option-name">{adapter.id}</span>
|
|
223
|
+
<span className="rwkv-option-meta">
|
|
224
|
+
{[methodLabel(adapter.method), adapter.rank ? `rank ${adapter.rank}` : '', adapter.version.slice(0, 8)].filter(Boolean).join(' · ')}
|
|
225
|
+
</span>
|
|
226
|
+
{current === adapter.id ? <span className="rwkv-option-state">使用中</span> : null}
|
|
227
|
+
</button>
|
|
228
|
+
{deleting === adapter.id ? (
|
|
229
|
+
<span className="rwkv-actions">
|
|
230
|
+
<button type="button" className="rwkv-link" onClick={() => setDeleting(undefined)}>取消</button>
|
|
231
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small rwkv-danger" onClick={() => { void remove(adapter.id) }}>从服务器删除</button>
|
|
232
|
+
</span>
|
|
233
|
+
) : (
|
|
234
|
+
<button type="button" className="rwkv-link" disabled={busy} onClick={() => setDeleting(adapter.id)}>删除</button>
|
|
235
|
+
)}
|
|
236
|
+
</div>
|
|
237
|
+
))}
|
|
238
|
+
{missing ? (
|
|
239
|
+
<div className="rwkv-option" aria-pressed>
|
|
240
|
+
<span className="rwkv-option-main">
|
|
241
|
+
<span className="rwkv-option-name">{current}</span>
|
|
242
|
+
<span className="rwkv-error">未在服务器列表中(将直接按此 ID 请求)</span>
|
|
243
|
+
<span className="rwkv-option-state">使用中</span>
|
|
244
|
+
</span>
|
|
245
|
+
<button type="button" className="rwkv-link" disabled={props.disabled} onClick={() => props.onChange(undefined)}>清除</button>
|
|
246
|
+
</div>
|
|
247
|
+
) : null}
|
|
248
|
+
</div>
|
|
249
|
+
{pending ? (
|
|
250
|
+
<div className="rwkv-upload">
|
|
251
|
+
<span className="rwkv-option-meta" title={pending.file.name}>{pending.file.name} · {(pending.file.size / 1048576).toFixed(1)} MB</span>
|
|
252
|
+
<div className="rwkv-upload-row">
|
|
253
|
+
<input className="rwkv-input" value={pending.id} spellCheck={false} placeholder="adapter id" disabled={progress !== undefined} onChange={event => setPending({ ...pending, id: event.target.value })} />
|
|
254
|
+
<button type="button" className="rwkv-btn rwkv-primary rwkv-small" disabled={!pending.id.trim() || progress !== undefined} onClick={() => upload(pending.file, pending.id.trim())}>
|
|
255
|
+
{progress === undefined ? '上传' : `${progress}%`}
|
|
256
|
+
</button>
|
|
257
|
+
<button type="button" className="rwkv-link" disabled={progress !== undefined} onClick={() => setPending(undefined)}>取消</button>
|
|
258
|
+
</div>
|
|
259
|
+
</div>
|
|
260
|
+
) : null}
|
|
261
|
+
{error ? <p className="rwkv-error">{error}</p> : null}
|
|
262
|
+
</div>
|
|
263
|
+
)
|
|
264
|
+
}
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// Developer section of the RWKV settings page: raw trace recording, the two
|
|
2
|
+
// crutch switches, statistics over the recorded sessions, the session list
|
|
3
|
+
// with its frames, and the export of whole tasks.
|
|
4
|
+
|
|
5
|
+
import { useEffect, useState, type ReactNode } from 'react'
|
|
6
|
+
import { Segment } from './Segment.tsx'
|
|
7
|
+
import { TraceFrames } from './TraceFrames.tsx'
|
|
8
|
+
import {
|
|
9
|
+
deleteSession, downloadExport, fetchSession, fetchSessions, OUTCOME_LABEL, percent, REPAIR_LABEL,
|
|
10
|
+
type ModelStats, type SessionSummary, type TraceRecord,
|
|
11
|
+
} from './traces-api.ts'
|
|
12
|
+
|
|
13
|
+
export interface DeveloperSettings {
|
|
14
|
+
recordTraces: boolean
|
|
15
|
+
traceDir: string
|
|
16
|
+
outputRepairs: boolean
|
|
17
|
+
guards: boolean
|
|
18
|
+
/** File editing mode; edits `editMode` in the Host schema. */
|
|
19
|
+
editMode: 'tolerant' | 'line-edit' | 'literal'
|
|
20
|
+
/** Context management mode; edits `historyMode` in the Host schema. */
|
|
21
|
+
historyMode: 'auto-fold' | 'full' | 'worklist'
|
|
22
|
+
worklistMinFoldTokens: number
|
|
23
|
+
worklistBudgetTokens: number
|
|
24
|
+
worklistEpochTargetTokens: number
|
|
25
|
+
worklistKeepRecentPairs: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const DEVELOPER_DEFAULTS: DeveloperSettings = {
|
|
29
|
+
recordTraces: true,
|
|
30
|
+
traceDir: '',
|
|
31
|
+
outputRepairs: true,
|
|
32
|
+
guards: true,
|
|
33
|
+
editMode: 'tolerant',
|
|
34
|
+
historyMode: 'auto-fold',
|
|
35
|
+
worklistMinFoldTokens: 8000,
|
|
36
|
+
worklistBudgetTokens: 13500,
|
|
37
|
+
worklistEpochTargetTokens: 9000,
|
|
38
|
+
worklistKeepRecentPairs: 3,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function Toggle(props: { label: string; hint: string; checked: boolean; disabled: boolean; onChange: (value: boolean) => void }) {
|
|
42
|
+
return (
|
|
43
|
+
<label className="rwkv-check rwkv-toggle">
|
|
44
|
+
<input type="checkbox" checked={props.checked} disabled={props.disabled} onChange={event => props.onChange(event.target.checked)} />
|
|
45
|
+
<span>
|
|
46
|
+
{props.label}
|
|
47
|
+
<span className="rwkv-hint rwkv-toggle-hint">{props.hint}</span>
|
|
48
|
+
</span>
|
|
49
|
+
</label>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Counts(props: { title: string; counts: Record<string, number>; total: number; labels: Record<string, string> }) {
|
|
54
|
+
const entries = Object.entries(props.counts).sort((a, b) => b[1] - a[1])
|
|
55
|
+
if (entries.length === 0) return null
|
|
56
|
+
return (
|
|
57
|
+
<div className="rwkv-counts">
|
|
58
|
+
<span className="rwkv-block-title">{props.title}</span>
|
|
59
|
+
{entries.map(([key, count]) => (
|
|
60
|
+
<span key={key} className="rwkv-count" title={key}>
|
|
61
|
+
{props.labels[key] ?? key}<b>{count}</b><span className="rwkv-hint">{percent(count, props.total)}</span>
|
|
62
|
+
</span>
|
|
63
|
+
))}
|
|
64
|
+
</div>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function StatsCard(props: { stats: readonly ModelStats[] }) {
|
|
69
|
+
// Traces written before outcome / step records existed carry nothing to count.
|
|
70
|
+
const counted = props.stats.filter(stats => stats.generations > 0 || stats.steps > 0)
|
|
71
|
+
if (counted.length === 0) return <p className="rwkv-hint">还没有记录。</p>
|
|
72
|
+
return (
|
|
73
|
+
<>
|
|
74
|
+
{counted.map(stats => (
|
|
75
|
+
<div key={stats.model} className="rwkv-stats">
|
|
76
|
+
<div className="rwkv-stats-head">
|
|
77
|
+
<span className="rwkv-name">{stats.model}</span>
|
|
78
|
+
<span className="rwkv-stat"><b>{percent(stats.clean, stats.generations)}</b> 生成原样可用({stats.clean}/{stats.generations})</span>
|
|
79
|
+
<span className="rwkv-stat"><b>{percent(stats.cleanSteps, stats.steps)}</b> 请求一次定下且无修复({stats.cleanSteps}/{stats.steps})</span>
|
|
80
|
+
</div>
|
|
81
|
+
<Counts title="插件怎么处理的" counts={stats.outcomes} total={stats.generations} labels={OUTCOME_LABEL} />
|
|
82
|
+
<Counts title="用到的修复(按生成计)" counts={stats.repairs} total={stats.generations} labels={REPAIR_LABEL} />
|
|
83
|
+
</div>
|
|
84
|
+
))}
|
|
85
|
+
</>
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function SessionRow(props: { item: SessionSummary; selected: boolean; onSelect: (value: boolean) => void; onDeleted: () => void }) {
|
|
90
|
+
const { item } = props
|
|
91
|
+
const [open, setOpen] = useState(false)
|
|
92
|
+
const [confirming, setConfirming] = useState(false)
|
|
93
|
+
const [deleting, setDeleting] = useState(false)
|
|
94
|
+
const remove = async () => {
|
|
95
|
+
setDeleting(true)
|
|
96
|
+
setError('')
|
|
97
|
+
try {
|
|
98
|
+
await deleteSession(item.session)
|
|
99
|
+
props.onDeleted()
|
|
100
|
+
} catch (cause) {
|
|
101
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
102
|
+
setDeleting(false)
|
|
103
|
+
setConfirming(false)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const [records, setRecords] = useState<TraceRecord[] | undefined>()
|
|
107
|
+
const [onlyTouched, setOnlyTouched] = useState(true)
|
|
108
|
+
const [error, setError] = useState('')
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (!open || records !== undefined) return
|
|
111
|
+
const abort = new AbortController()
|
|
112
|
+
fetchSession(item.session, abort.signal)
|
|
113
|
+
.then(body => setRecords(body.records))
|
|
114
|
+
.catch((cause: unknown) => { if (!abort.signal.aborted) setError(cause instanceof Error ? cause.message : String(cause)) })
|
|
115
|
+
return () => abort.abort()
|
|
116
|
+
}, [open, records, item.session])
|
|
117
|
+
return (
|
|
118
|
+
<li className="rwkv-session">
|
|
119
|
+
<div className="rwkv-session-head">
|
|
120
|
+
<input type="checkbox" checked={props.selected} onChange={event => props.onSelect(event.target.checked)} aria-label="选中这个会话" />
|
|
121
|
+
<button type="button" className="rwkv-session-title" onClick={() => setOpen(value => !value)} aria-expanded={open}>
|
|
122
|
+
<span className="rwkv-session-task">{item.task || item.session}</span>
|
|
123
|
+
<span className="rwkv-session-meta">
|
|
124
|
+
{new Date(item.modified).toLocaleString()} · {item.models.join(', ')} · {item.steps} 次请求 · {item.generations} 次生成
|
|
125
|
+
{item.touchedGenerations > 0 ? ` · ${item.touchedGenerations} 次用到兜底` : ''}
|
|
126
|
+
{item.persisted ? '' : ' · 仅在内存'}
|
|
127
|
+
</span>
|
|
128
|
+
</button>
|
|
129
|
+
<span className="rwkv-actions">
|
|
130
|
+
{confirming ? (
|
|
131
|
+
<>
|
|
132
|
+
<button type="button" className="rwkv-link" disabled={deleting} onClick={() => setConfirming(false)}>取消</button>
|
|
133
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small rwkv-danger" disabled={deleting} onClick={() => { void remove() }}>确认删除</button>
|
|
134
|
+
</>
|
|
135
|
+
) : (
|
|
136
|
+
<button type="button" className="rwkv-link" title="只删 RWKV 轨迹,不动 DSH 的会话记录" onClick={() => setConfirming(true)}>删除</button>
|
|
137
|
+
)}
|
|
138
|
+
</span>
|
|
139
|
+
</div>
|
|
140
|
+
{!open && error ? <p className="rwkv-error">{error}</p> : null}
|
|
141
|
+
{open ? (
|
|
142
|
+
<div className="rwkv-session-body">
|
|
143
|
+
<label className="rwkv-check"><input type="checkbox" checked={onlyTouched} onChange={event => setOnlyTouched(event.target.checked)} />只看用到兜底的生成</label>
|
|
144
|
+
{error ? <p className="rwkv-error">{error}</p> : records === undefined ? <p className="rwkv-hint">加载中…</p> : (
|
|
145
|
+
<TraceFrames session={item.session} records={records} onlyTouched={onlyTouched} showSide={false} />
|
|
146
|
+
)}
|
|
147
|
+
</div>
|
|
148
|
+
) : null}
|
|
149
|
+
</li>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function Traces(props: { recording: boolean }) {
|
|
154
|
+
const [data, setData] = useState<{ dir: string; sessions: SessionSummary[]; stats: ModelStats[] } | undefined>()
|
|
155
|
+
const [error, setError] = useState('')
|
|
156
|
+
const [selected, setSelected] = useState<ReadonlySet<string>>(new Set())
|
|
157
|
+
const [busy, setBusy] = useState(false)
|
|
158
|
+
const [reload, setReload] = useState(0)
|
|
159
|
+
const [listOpen, setListOpen] = useState(false)
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
const abort = new AbortController()
|
|
162
|
+
setError('')
|
|
163
|
+
fetchSessions(abort.signal)
|
|
164
|
+
.then(body => setData(body))
|
|
165
|
+
.catch((cause: unknown) => { if (!abort.signal.aborted) setError(cause instanceof Error ? cause.message : String(cause)) })
|
|
166
|
+
return () => abort.abort()
|
|
167
|
+
}, [reload, props.recording])
|
|
168
|
+
const exportSessions = async (sessions: readonly string[]) => {
|
|
169
|
+
setBusy(true)
|
|
170
|
+
setError('')
|
|
171
|
+
try {
|
|
172
|
+
await downloadExport(sessions)
|
|
173
|
+
} catch (cause) {
|
|
174
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
175
|
+
}
|
|
176
|
+
setBusy(false)
|
|
177
|
+
}
|
|
178
|
+
const select = (session: string, value: boolean) => setSelected(current => {
|
|
179
|
+
const next = new Set(current)
|
|
180
|
+
if (value) next.add(session)
|
|
181
|
+
else next.delete(session)
|
|
182
|
+
return next
|
|
183
|
+
})
|
|
184
|
+
if (error && data === undefined) return <p className="rwkv-error">{error}</p>
|
|
185
|
+
if (data === undefined) return <p className="rwkv-hint">加载中…</p>
|
|
186
|
+
return (
|
|
187
|
+
<>
|
|
188
|
+
<div className="rwkv-block">
|
|
189
|
+
<div className="rwkv-block-head">
|
|
190
|
+
<span className="rwkv-block-title">兜底统计</span>
|
|
191
|
+
<button type="button" className="rwkv-link" onClick={() => setReload(value => value + 1)}>刷新</button>
|
|
192
|
+
</div>
|
|
193
|
+
<StatsCard stats={data.stats} />
|
|
194
|
+
</div>
|
|
195
|
+
<div className="rwkv-block">
|
|
196
|
+
<div className="rwkv-block-head">
|
|
197
|
+
<span
|
|
198
|
+
className="rwkv-block-title rwkv-session-toggle" title={data.dir}
|
|
199
|
+
onClick={() => { if (data.sessions.length > 0) setListOpen(value => !value) }}
|
|
200
|
+
>已记录的会话 · {data.sessions.length} 个</span>
|
|
201
|
+
<span className="rwkv-actions">
|
|
202
|
+
{listOpen || selected.size > 0 ? (
|
|
203
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small" disabled={busy || selected.size === 0} onClick={() => { void exportSessions([...selected]) }}>导出所选({selected.size})</button>
|
|
204
|
+
) : null}
|
|
205
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small" disabled={busy || data.sessions.length === 0} onClick={() => { void exportSessions([]) }}>导出全部</button>
|
|
206
|
+
{data.sessions.length > 0 ? (
|
|
207
|
+
<button
|
|
208
|
+
type="button" className="rwkv-btn rwkv-secondary rwkv-small" aria-expanded={listOpen}
|
|
209
|
+
onClick={() => setListOpen(value => !value)}
|
|
210
|
+
>{listOpen ? '收起' : '展开'}</button>
|
|
211
|
+
) : null}
|
|
212
|
+
</span>
|
|
213
|
+
</div>
|
|
214
|
+
{error ? <p className="rwkv-error">{error}</p> : null}
|
|
215
|
+
{data.sessions.length === 0 ? <p className="rwkv-hint">还没有轨迹。</p> : listOpen ? (
|
|
216
|
+
<>
|
|
217
|
+
<p className="rwkv-hint">保存在 {data.dir}(删除仅删轨迹,不影响 DSH 会话)。</p>
|
|
218
|
+
<ul className="rwkv-sessions">
|
|
219
|
+
{data.sessions.map(item => (
|
|
220
|
+
<SessionRow
|
|
221
|
+
key={item.session} item={item} selected={selected.has(item.session)} onSelect={value => select(item.session, value)}
|
|
222
|
+
onDeleted={() => { select(item.session, false); setReload(value => value + 1) }}
|
|
223
|
+
/>
|
|
224
|
+
))}
|
|
225
|
+
</ul>
|
|
226
|
+
</>
|
|
227
|
+
) : null}
|
|
228
|
+
</div>
|
|
229
|
+
</>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function DirInput(props: { value: string; disabled: boolean; onSave: (value: string) => void }) {
|
|
234
|
+
const [text, setText] = useState(props.value)
|
|
235
|
+
useEffect(() => setText(props.value), [props.value])
|
|
236
|
+
return (
|
|
237
|
+
<span className="rwkv-dir">
|
|
238
|
+
<input
|
|
239
|
+
className="rwkv-input" value={text} disabled={props.disabled} spellCheck={false} autoComplete="off"
|
|
240
|
+
placeholder="留空:存在会话目录里"
|
|
241
|
+
onChange={event => setText(event.target.value)}
|
|
242
|
+
onKeyDown={event => { if (event.key === 'Enter') props.onSave(text.trim()) }}
|
|
243
|
+
/>
|
|
244
|
+
<button type="button" className="rwkv-btn rwkv-secondary rwkv-small" disabled={props.disabled || text.trim() === props.value} onClick={() => props.onSave(text.trim())}>保存</button>
|
|
245
|
+
</span>
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* A number the Host validates, so it is edited as text and written on blur or
|
|
251
|
+
* Enter: a half-typed value would otherwise be saved and refused.
|
|
252
|
+
*/
|
|
253
|
+
function NumberField(props: { value: number; onSave: (value: number) => void; disabled: boolean; placeholder?: string; min?: number }) {
|
|
254
|
+
const [text, setText] = useState(String(props.value))
|
|
255
|
+
useEffect(() => setText(String(props.value)), [props.value])
|
|
256
|
+
const minVal = props.min ?? 1
|
|
257
|
+
const commit = () => {
|
|
258
|
+
const parsed = Number(text)
|
|
259
|
+
if (text.trim() === '' || !Number.isInteger(parsed) || parsed < minVal) {
|
|
260
|
+
setText(String(props.value))
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
if (parsed !== props.value) props.onSave(parsed)
|
|
264
|
+
}
|
|
265
|
+
return (
|
|
266
|
+
<input
|
|
267
|
+
className="rwkv-input" inputMode="numeric" value={text} disabled={props.disabled} placeholder={props.placeholder}
|
|
268
|
+
onChange={event => setText(event.target.value)} onBlur={commit}
|
|
269
|
+
onKeyDown={event => { if (event.key === 'Enter') commit() }}
|
|
270
|
+
/>
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const EDIT_MODE_HINT: Record<DeveloperSettings['editMode'], string> = {
|
|
275
|
+
tolerant: '自动对齐缩进空行并做语法检查。',
|
|
276
|
+
'line-edit': '语义首行锚点(start_text)三级自愈 + 原子批量 edits[] + ≤200 行 read 上限,隐藏标准 edit。',
|
|
277
|
+
literal: '原样精确匹配,不做容错与检查。',
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const HISTORY_MODE_HINT: Record<DeveloperSettings['historyMode'], string> = {
|
|
281
|
+
'auto-fold': '超水位自动压缩早期步骤输出,不挂 todo 工具。',
|
|
282
|
+
full: '保留全部历史原文,不做压缩。',
|
|
283
|
+
worklist: '启用 todo 工具由模型记工单,超限后折叠旧步骤。',
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function DeveloperCard(props: {
|
|
287
|
+
settings: DeveloperSettings
|
|
288
|
+
disabled: boolean
|
|
289
|
+
write: (field: string, value: unknown) => Promise<boolean>
|
|
290
|
+
}): ReactNode {
|
|
291
|
+
const { settings, disabled, write } = props
|
|
292
|
+
const [open, setOpen] = useState(false)
|
|
293
|
+
const [busy, setBusy] = useState(false)
|
|
294
|
+
const set = async (field: keyof DeveloperSettings, value: unknown) => {
|
|
295
|
+
setBusy(true)
|
|
296
|
+
await write(field, value)
|
|
297
|
+
setBusy(false)
|
|
298
|
+
}
|
|
299
|
+
const off = disabled || busy
|
|
300
|
+
const historyMode = settings.historyMode === 'worklist' ? 'worklist' : settings.historyMode === 'full' ? 'full' : 'auto-fold'
|
|
301
|
+
return (
|
|
302
|
+
<div className="rwkv-card">
|
|
303
|
+
<button
|
|
304
|
+
type="button" className="rwkv-head rwkv-developer-head" aria-expanded={open}
|
|
305
|
+
onClick={() => setOpen(value => !value)}
|
|
306
|
+
>
|
|
307
|
+
<span className="rwkv-identity">
|
|
308
|
+
<span className="rwkv-chevron" aria-hidden="true" />
|
|
309
|
+
<span className="rwkv-name">开发者</span>
|
|
310
|
+
</span>
|
|
311
|
+
<span className="rwkv-meta">
|
|
312
|
+
{settings.recordTraces ? '轨迹随会话保存' : '轨迹只在内存'}
|
|
313
|
+
{settings.outputRepairs ? '' : ' · 修正已关'}
|
|
314
|
+
{settings.guards ? '' : ' · 修复已关'}
|
|
315
|
+
{settings.editMode === 'tolerant' ? ' · 容错 edit' : settings.editMode === 'literal' ? ' · 精确 edit' : ' · 锚点行号 edit_lines'}
|
|
316
|
+
{historyMode === 'auto-fold'
|
|
317
|
+
? ` · 自动折叠(${settings.worklistEpochTargetTokens} ← ${settings.worklistBudgetTokens})`
|
|
318
|
+
: historyMode === 'worklist'
|
|
319
|
+
? ` · 工单模式(${settings.worklistMinFoldTokens} / ${settings.worklistBudgetTokens})`
|
|
320
|
+
: ' · 完整对话'}
|
|
321
|
+
</span>
|
|
322
|
+
</button>
|
|
323
|
+
{open ? (
|
|
324
|
+
<div className="rwkv-developer-body">
|
|
325
|
+
<div className="rwkv-field">
|
|
326
|
+
<span className="rwkv-label">文件编辑模式</span>
|
|
327
|
+
<Segment
|
|
328
|
+
value={settings.editMode} disabled={off}
|
|
329
|
+
options={[['tolerant', '容错 edit(默认)'], ['line-edit', '锚点行号 edit_lines'], ['literal', '精确 edit']] as const}
|
|
330
|
+
onChange={value => { void set('editMode', value) }}
|
|
331
|
+
/>
|
|
332
|
+
<p className="rwkv-hint">{EDIT_MODE_HINT[settings.editMode] ?? EDIT_MODE_HINT.tolerant}</p>
|
|
333
|
+
</div>
|
|
334
|
+
<div className="rwkv-field">
|
|
335
|
+
<span className="rwkv-label">上下文管理模式</span>
|
|
336
|
+
<Segment
|
|
337
|
+
value={historyMode} disabled={off}
|
|
338
|
+
options={[['auto-fold', '自动折叠(默认)'], ['full', '完整对话'], ['worklist', '工单模式(实验)']] as const}
|
|
339
|
+
onChange={value => { void set('historyMode', value) }}
|
|
340
|
+
/>
|
|
341
|
+
<p className="rwkv-hint">{HISTORY_MODE_HINT[historyMode]}</p>
|
|
342
|
+
</div>
|
|
343
|
+
{historyMode === 'auto-fold' || historyMode === 'worklist' ? (
|
|
344
|
+
<div className="rwkv-grid3">
|
|
345
|
+
{historyMode === 'auto-fold' ? (
|
|
346
|
+
<label className="rwkv-field">
|
|
347
|
+
<span className="rwkv-label">回落水位 (token)</span>
|
|
348
|
+
<NumberField
|
|
349
|
+
value={settings.worklistEpochTargetTokens} min={0} disabled={off} placeholder="9000"
|
|
350
|
+
onSave={value => { void set('worklistEpochTargetTokens', value) }}
|
|
351
|
+
/>
|
|
352
|
+
<p className="rwkv-hint">批量原地遮蔽后的目标水位</p>
|
|
353
|
+
</label>
|
|
354
|
+
) : (
|
|
355
|
+
<label className="rwkv-field">
|
|
356
|
+
<span className="rwkv-label">开始折叠 (token)</span>
|
|
357
|
+
<NumberField
|
|
358
|
+
value={settings.worklistMinFoldTokens} min={0} disabled={off} placeholder="8000"
|
|
359
|
+
onSave={value => { void set('worklistMinFoldTokens', value) }}
|
|
360
|
+
/>
|
|
361
|
+
<p className="rwkv-hint">超此值才允许折叠</p>
|
|
362
|
+
</label>
|
|
363
|
+
)}
|
|
364
|
+
<label className="rwkv-field">
|
|
365
|
+
<span className="rwkv-label">单轮上限 (token)</span>
|
|
366
|
+
<NumberField
|
|
367
|
+
value={settings.worklistBudgetTokens} disabled={off} placeholder="13500"
|
|
368
|
+
onSave={value => { void set('worklistBudgetTokens', value) }}
|
|
369
|
+
/>
|
|
370
|
+
<p className="rwkv-hint">{historyMode === 'auto-fold' ? '超此值触发批量原地遮蔽' : '超此值追加预算提醒'}</p>
|
|
371
|
+
</label>
|
|
372
|
+
<label className="rwkv-field">
|
|
373
|
+
<span className="rwkv-label">保留最近步数</span>
|
|
374
|
+
<NumberField
|
|
375
|
+
value={settings.worklistKeepRecentPairs} min={0} disabled={off} placeholder="3"
|
|
376
|
+
onSave={value => { void set('worklistKeepRecentPairs', value) }}
|
|
377
|
+
/>
|
|
378
|
+
<p className="rwkv-hint">折叠时保留最近原文步数</p>
|
|
379
|
+
</label>
|
|
380
|
+
</div>
|
|
381
|
+
) : null}
|
|
382
|
+
<Toggle
|
|
383
|
+
label="原始运行轨迹落盘" checked={settings.recordTraces} disabled={off} onChange={value => { void set('recordTraces', value) }}
|
|
384
|
+
hint="随会话保存在本地,关闭后仅留内存。"
|
|
385
|
+
/>
|
|
386
|
+
<label className="rwkv-field">
|
|
387
|
+
<span className="rwkv-label">自定义目录</span>
|
|
388
|
+
<DirInput value={settings.traceDir} disabled={off} onSave={value => { void set('traceDir', value) }} />
|
|
389
|
+
</label>
|
|
390
|
+
<Toggle
|
|
391
|
+
label="修正模型输出" checked={settings.outputRepairs} disabled={off} onChange={value => { void set('outputRepairs', value) }}
|
|
392
|
+
hint="自动修格式、工具名与参数。"
|
|
393
|
+
/>
|
|
394
|
+
<Toggle
|
|
395
|
+
label="插件修复" checked={settings.guards} disabled={off} onChange={value => { void set('guards', value) }}
|
|
396
|
+
hint="格式重问、防重复调用与强制收尾。"
|
|
397
|
+
/>
|
|
398
|
+
<Traces recording={settings.recordTraces} />
|
|
399
|
+
</div>
|
|
400
|
+
) : null}
|
|
401
|
+
</div>
|
|
402
|
+
)
|
|
403
|
+
}
|