@surfishs/dsh-prompt-enhance 0.0.7 → 0.0.8
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.en.md +3 -0
- package/README.md +3 -0
- package/cordis.patch.yml +1 -1
- package/lib/client.js +341 -383
- package/lib/index.js +67 -56
- package/lib/index.js.map +3 -3
- package/package.json +23 -15
- package/src/client/ActionIcons.tsx +4 -17
- package/src/client/EnhanceButton.tsx +52 -48
- package/src/client/ResultDock.tsx +22 -9
- package/src/client/ResultPanel.tsx +83 -52
- package/src/client/SparkleFishIcon.tsx +3 -13
- package/src/client/SparkleIcon.tsx +2 -12
- package/src/client/UndoBar.tsx +11 -6
- package/src/client/enhance-client.ts +44 -40
- package/src/client/index.tsx +3 -12
- package/src/client/llm-catalog.ts +14 -16
- package/src/client/locales.ts +18 -4
- package/src/client/reasoning-options.ts +7 -15
- package/src/client/settings-page-controls.tsx +449 -0
- package/src/client/settings-page-copy.ts +112 -0
- package/src/client/settings-page-spec.ts +330 -0
- package/src/client/settings-page-styles.ts +161 -0
- package/src/client/settings-page.tsx +113 -1110
- package/src/client/settings-source.ts +41 -31
- package/src/client/settings.ts +17 -12
- package/src/client/shortcut.ts +7 -6
- package/src/client/useDockAlignment.ts +5 -15
- package/src/config.ts +84 -27
- package/src/context.ts +1 -5
- package/src/enhance-routes.ts +8 -8
- package/src/enhancer.ts +43 -34
- package/src/orchestrate.ts +19 -23
- package/src/prompts.ts +4 -4
- package/src/provider-catalog.ts +1 -1
- package/src/reasoning-cache.ts +4 -4
- package/src/reasoning-efforts.ts +4 -9
- package/src/settings-source.ts +37 -23
- package/src/shared/limits.ts +38 -0
- package/src/shared/normalize.ts +4 -1
- package/src/shared/protocol.ts +27 -23
- package/src/shared/reasoning.ts +1 -7
- package/src/shared/stream-text.ts +0 -6
- package/src/shared/validate.ts +3 -9
- package/docs/code-review-2026-09.md +0 -211
- package/docs/handoff-settings-page.md +0 -195
- package/docs/release-0.2.3.md +0 -47
- package/docs/release-0.2.4.md +0 -20
- package/docs/release-0.2.5.md +0 -28
- package/docs/release-0.2.6.md +0 -33
- package/docs/release-0.2.7.md +0 -22
- package/docs/release-0.3.0.md +0 -36
- package/docs/release-0.3.1.md +0 -50
- package/docs/release-0.3.2.md +0 -41
- package/docs/release-0.3.3.md +0 -53
- package/docs/release-0.3.4.md +0 -43
package/src/prompts.ts
CHANGED
|
@@ -22,12 +22,12 @@ export const DEFAULT_SYSTEM_PROMPT = [
|
|
|
22
22
|
'6. Boundary conditions: list edge cases, invalid inputs, and what to do when information is missing.',
|
|
23
23
|
'',
|
|
24
24
|
'Hard rules:',
|
|
25
|
-
|
|
25
|
+
"- Preserve the user's intent exactly. Do not remove, alter, or contradict any information the user provided.",
|
|
26
26
|
'- Never fabricate requirements. When a needed detail is unknown, insert a short explicit placeholder such as "(待补充:…)" / "(TBD: …)" instead of making one up.',
|
|
27
27
|
'- Every addition must be traceable to explicit evidence or a strong implication in the raw prompt; when in doubt, use a placeholder instead of adding content.',
|
|
28
|
-
|
|
28
|
+
"- Keep the prompt's scope unchanged: do not widen, narrow, or redirect the task.",
|
|
29
29
|
'- Output ONLY the rewritten prompt body. No explanations, no preamble, no comparison with the original, no code fences, no pleasantries.',
|
|
30
|
-
|
|
30
|
+
"- Write the rewritten prompt in the SAME language as the user's input (Chinese input → Chinese prompt; English input → English prompt).",
|
|
31
31
|
'- Keep the length proportionate, roughly 1x–3x the original; do not pad.',
|
|
32
32
|
'- If the raw prompt is already well-formed, return it lightly polished, unchanged in substance.',
|
|
33
33
|
'- If the raw prompt is a greeting, small talk, or a fragment that states no task, still rewrite it: describe the role and how the assistant should respond to it, in the same language. Never answer it conversationally.',
|
|
@@ -87,7 +87,7 @@ export const CONTEXT_SYSTEM_ADDITION = [
|
|
|
87
87
|
'It carries recent turns of the conversation the raw prompt belongs to. Use it ONLY to:',
|
|
88
88
|
'- resolve pronouns, ellipsis, and vague references ("它", "上面那个", "同样的方式") into their concrete referents;',
|
|
89
89
|
'- supply the technical stack, terminology, and constraints the conversation already established;',
|
|
90
|
-
|
|
90
|
+
"- keep the rewritten prompt aligned with the conversation's current goal.",
|
|
91
91
|
'Hard limits on context use:',
|
|
92
92
|
'- The raw prompt always wins. On any conflict, drop the contextual detail — never let it override, widen, or contradict what the user just typed.',
|
|
93
93
|
'- Never introduce a requirement, fact, name, file, or value that neither the raw prompt nor the context supports; use a placeholder instead.',
|
package/src/provider-catalog.ts
CHANGED
|
@@ -79,7 +79,7 @@ function modelOf(model: LlmModelInfo): ModelCatalogEntry {
|
|
|
79
79
|
return {
|
|
80
80
|
id: model.id,
|
|
81
81
|
name: model.name,
|
|
82
|
-
...model.description === undefined ? {} : { description: model.description },
|
|
82
|
+
...(model.description === undefined ? {} : { description: model.description }),
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
package/src/reasoning-cache.ts
CHANGED
|
@@ -66,7 +66,7 @@ function effortOf(value: unknown): ReasoningEffortInfo | undefined {
|
|
|
66
66
|
return {
|
|
67
67
|
id: record.id,
|
|
68
68
|
name: record.name,
|
|
69
|
-
...typeof record.description === 'string' && record.description !== '' ? { description: record.description } : {},
|
|
69
|
+
...(typeof record.description === 'string' && record.description !== '' ? { description: record.description } : {}),
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -117,7 +117,7 @@ export async function readCachedCapabilities(provider: string, model: string): P
|
|
|
117
117
|
if (efforts.length !== entry.efforts.length) return undefined
|
|
118
118
|
return {
|
|
119
119
|
efforts,
|
|
120
|
-
...typeof entry.defaultEffort === 'string' && entry.defaultEffort !== '' ? { defaultEffort: entry.defaultEffort } : {},
|
|
120
|
+
...(typeof entry.defaultEffort === 'string' && entry.defaultEffort !== '' ? { defaultEffort: entry.defaultEffort } : {}),
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
@@ -134,8 +134,8 @@ export async function writeCachedCapabilities(provider: string, model: string, e
|
|
|
134
134
|
provider,
|
|
135
135
|
model,
|
|
136
136
|
efforts,
|
|
137
|
-
...defaultEffort === undefined ? {} : { defaultEffort },
|
|
138
|
-
...noEfforts ? { noEfforts: true } : {},
|
|
137
|
+
...(defaultEffort === undefined ? {} : { defaultEffort }),
|
|
138
|
+
...(noEfforts ? { noEfforts: true } : {}),
|
|
139
139
|
savedAt: Date.now(),
|
|
140
140
|
}
|
|
141
141
|
await writeDocument(document)
|
package/src/reasoning-efforts.ts
CHANGED
|
@@ -55,7 +55,7 @@ function effortOf(effort: LlmModelReasoningInfo['efforts'][number]): { id: strin
|
|
|
55
55
|
return {
|
|
56
56
|
id: effort.id,
|
|
57
57
|
name: effort.name,
|
|
58
|
-
...effort.description === undefined ? {} : { description: effort.description },
|
|
58
|
+
...(effort.description === undefined ? {} : { description: effort.description }),
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -75,12 +75,7 @@ function effortOf(effort: LlmModelReasoningInfo['efforts'][number]): { id: strin
|
|
|
75
75
|
* @param options - `refresh` bypasses and replaces the cached answer.
|
|
76
76
|
* @returns the capability answer; never throws.
|
|
77
77
|
*/
|
|
78
|
-
export async function resolveReasoningCapabilities(
|
|
79
|
-
ctx: Context,
|
|
80
|
-
provider: string | undefined,
|
|
81
|
-
model: string | undefined,
|
|
82
|
-
options: { refresh?: boolean } = {},
|
|
83
|
-
): Promise<ReasoningCapabilities> {
|
|
78
|
+
export async function resolveReasoningCapabilities(ctx: Context, provider: string | undefined, model: string | undefined, options: { refresh?: boolean } = {}): Promise<ReasoningCapabilities> {
|
|
84
79
|
if (provider === undefined || model === undefined) {
|
|
85
80
|
const fallback = defaultRouteOf(ctx)
|
|
86
81
|
if (fallback === undefined) return unavailable('no-default-model')
|
|
@@ -93,7 +88,7 @@ export async function resolveReasoningCapabilities(
|
|
|
93
88
|
if (cached.noEfforts === true) return unavailable('no-efforts')
|
|
94
89
|
return {
|
|
95
90
|
efforts: cached.efforts,
|
|
96
|
-
...cached.defaultEffort === undefined ? {} : { defaultEffort: cached.defaultEffort },
|
|
91
|
+
...(cached.defaultEffort === undefined ? {} : { defaultEffort: cached.defaultEffort }),
|
|
97
92
|
source: 'resolved',
|
|
98
93
|
}
|
|
99
94
|
}
|
|
@@ -115,7 +110,7 @@ export async function resolveReasoningCapabilities(
|
|
|
115
110
|
await writeCachedCapabilities(provider, model, efforts, reasoning.defaultEffort)
|
|
116
111
|
return {
|
|
117
112
|
efforts,
|
|
118
|
-
...reasoning.defaultEffort === undefined ? {} : { defaultEffort: reasoning.defaultEffort },
|
|
113
|
+
...(reasoning.defaultEffort === undefined ? {} : { defaultEffort: reasoning.defaultEffort }),
|
|
119
114
|
source: 'resolved',
|
|
120
115
|
}
|
|
121
116
|
} catch (error) {
|
package/src/settings-source.ts
CHANGED
|
@@ -116,6 +116,8 @@ function pickRow(rows: SettingsRowLike[], rowIds: readonly string[]): SettingsRo
|
|
|
116
116
|
export function installSettingsSource(ctx: Context, options: SettingsSourceOptions): SettingsSource {
|
|
117
117
|
let reader: (() => unknown) | undefined
|
|
118
118
|
let resolvedRowId: string | undefined
|
|
119
|
+
/** Latched after the first failed live read, so the warning is one line, not one per request. */
|
|
120
|
+
let warnedReadFailure = false
|
|
119
121
|
|
|
120
122
|
/**
|
|
121
123
|
* Hooks handed to the legacy installers. `setSource` doubles as this
|
|
@@ -151,7 +153,7 @@ export function installSettingsSource(ctx: Context, options: SettingsSourceOptio
|
|
|
151
153
|
settingsCtx.effect(() => {
|
|
152
154
|
try {
|
|
153
155
|
const dispose = service.configure?.({ auto: true }, (ctx as unknown as { fiber?: unknown }).fiber)
|
|
154
|
-
return typeof dispose === 'function' ? dispose as () => void : () => {}
|
|
156
|
+
return typeof dispose === 'function' ? (dispose as () => void) : () => {}
|
|
155
157
|
} catch {
|
|
156
158
|
// Already registered (a re-applied entry) or unsupported: the
|
|
157
159
|
// auto-generated page is the default anyway.
|
|
@@ -177,26 +179,29 @@ export function installSettingsSource(ctx: Context, options: SettingsSourceOptio
|
|
|
177
179
|
|
|
178
180
|
// 3. 0.1.1-rc: the standalone module export. Loaded dynamically so a build
|
|
179
181
|
// whose settings module no longer exports it cannot fail at import time.
|
|
180
|
-
void import('@deepseek-ai/dsh-settings').then(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
182
|
+
void import('@deepseek-ai/dsh-settings').then(
|
|
183
|
+
(mod) => {
|
|
184
|
+
const legacy = mod as unknown as {
|
|
185
|
+
installSettingsSection?: (owner: Context, ns: unknown, schema: unknown, entry: unknown, hooks: unknown) => void
|
|
186
|
+
settingsNamespace?: (ns: string) => unknown
|
|
187
|
+
}
|
|
188
|
+
if (typeof legacy.installSettingsSection !== 'function') return
|
|
189
|
+
try {
|
|
190
|
+
legacy.installSettingsSection(
|
|
191
|
+
ctx,
|
|
192
|
+
typeof legacy.settingsNamespace === 'function' ? legacy.settingsNamespace(options.namespace) : options.namespace,
|
|
193
|
+
options.schema,
|
|
194
|
+
options.entry,
|
|
195
|
+
legacyHooks,
|
|
196
|
+
)
|
|
197
|
+
} catch {
|
|
198
|
+
// Same policy as above: no settings is a degradation, not a failure.
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
() => {
|
|
202
|
+
// Module unresolvable on this host: the composition entry stands.
|
|
203
|
+
},
|
|
204
|
+
)
|
|
200
205
|
})
|
|
201
206
|
|
|
202
207
|
return {
|
|
@@ -205,8 +210,17 @@ export function installSettingsSource(ctx: Context, options: SettingsSourceOptio
|
|
|
205
210
|
try {
|
|
206
211
|
const value = reader()
|
|
207
212
|
if (value !== undefined) return value
|
|
208
|
-
} catch {
|
|
209
|
-
// A describe() hiccup must not fail the request it was read for.
|
|
213
|
+
} catch (error) {
|
|
214
|
+
// A describe() hiccup must not fail the request it was read for. It
|
|
215
|
+
// must not stay invisible either: the fallback answer below looks
|
|
216
|
+
// exactly like "this host has no section", so a host whose settings
|
|
217
|
+
// API drifted would keep serving the composition config with no
|
|
218
|
+
// evidence anywhere. One line per source (not per request — read()
|
|
219
|
+
// runs on every enhance call) is enough to make that diagnosable.
|
|
220
|
+
if (!warnedReadFailure) {
|
|
221
|
+
warnedReadFailure = true
|
|
222
|
+
console.warn('[dsh-prompt-enhance] reading the settings section failed; using the composition config for the rest of this session.', error)
|
|
223
|
+
}
|
|
210
224
|
}
|
|
211
225
|
}
|
|
212
226
|
return options.fallback()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numeric bounds of every bounded configuration field.
|
|
3
|
+
*
|
|
4
|
+
* One source for the two readers that must never disagree: the schemastery
|
|
5
|
+
* schema the settings page renders (`.min()` / `.max()`) and the runtime
|
|
6
|
+
* re-check inside `resolveConfig`. Duplicating the numbers meant a bound change
|
|
7
|
+
* could land in one layer and miss the other, and the two failure modes are
|
|
8
|
+
* both bad — a schema that accepts what the request path rejects, or a request
|
|
9
|
+
* path that accepts what the page refuses to render.
|
|
10
|
+
*
|
|
11
|
+
* `temperature` is the only non-integer member, so it carries its own step.
|
|
12
|
+
* @module dsh-prompt-enhance/shared/limits
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** One inclusive numeric range. */
|
|
16
|
+
export interface NumericRange {
|
|
17
|
+
readonly min: number
|
|
18
|
+
readonly max: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** An inclusive range plus the slider granularity of a fractional field. */
|
|
22
|
+
export interface StepRange extends NumericRange {
|
|
23
|
+
readonly step: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Sampling temperature: continuous enough to need a step, not an integer. */
|
|
27
|
+
export const TEMPERATURE: StepRange = { min: 0, max: 1, step: 0.05 }
|
|
28
|
+
|
|
29
|
+
/** Inclusive integer bounds, keyed by configuration field. */
|
|
30
|
+
export const FIELD_LIMITS = {
|
|
31
|
+
maxOutputTokens: { min: 256, max: 32768 },
|
|
32
|
+
maxInputChars: { min: 200, max: 200000 },
|
|
33
|
+
timeoutMs: { min: 5000, max: 600000 },
|
|
34
|
+
maxConcurrent: { min: 1, max: 16 },
|
|
35
|
+
rateLimitPerMinute: { min: 1, max: 600 },
|
|
36
|
+
contextMaxMessages: { min: 0, max: 50 },
|
|
37
|
+
contextMaxChars: { min: 0, max: 100000 },
|
|
38
|
+
} as const satisfies Record<string, NumericRange>
|
package/src/shared/normalize.ts
CHANGED
|
@@ -22,7 +22,10 @@ export function normalizeOutput(raw: string): string {
|
|
|
22
22
|
const fenceMarkCount = (text.match(/```/g) ?? []).length
|
|
23
23
|
if (text.startsWith('```') && /```[\s]*$/.test(text) && fenceMarkCount === 2) {
|
|
24
24
|
if (text.includes('\n')) {
|
|
25
|
-
text = text
|
|
25
|
+
text = text
|
|
26
|
+
.slice(text.indexOf('\n') + 1)
|
|
27
|
+
.replace(/```[\s]*$/, '')
|
|
28
|
+
.trim()
|
|
26
29
|
} else {
|
|
27
30
|
text = text.slice(3, -3).trim()
|
|
28
31
|
}
|
package/src/shared/protocol.ts
CHANGED
|
@@ -33,12 +33,7 @@ export const ENHANCE_STREAM_ENDPOINT = '/dsh-prompt-enhance/enhance-stream'
|
|
|
33
33
|
* same list in the same order, so the host section and the browser mirror can
|
|
34
34
|
* never resolve to two different documents.
|
|
35
35
|
*/
|
|
36
|
-
export const PROMPT_ENHANCE_ROW_IDS: readonly string[] = [
|
|
37
|
-
'dsh-prompt-enhance',
|
|
38
|
-
'prompt-enhance',
|
|
39
|
-
'web-ui-dsh-prompt-enhance',
|
|
40
|
-
'web-ui-prompt-enhance',
|
|
41
|
-
]
|
|
36
|
+
export const PROMPT_ENHANCE_ROW_IDS: readonly string[] = ['dsh-prompt-enhance', 'prompt-enhance', 'web-ui-dsh-prompt-enhance', 'web-ui-prompt-enhance']
|
|
42
37
|
|
|
43
38
|
/** Request body of one enhance call. */
|
|
44
39
|
export interface EnhanceRequestBody {
|
|
@@ -92,16 +87,28 @@ export interface EnhanceResult {
|
|
|
92
87
|
* a `Retry-After` in seconds with it), while the concurrency cap clears as
|
|
93
88
|
* soon as an in-flight call finishes — no predictable delay to advertise.
|
|
94
89
|
*/
|
|
95
|
-
export type EnhanceErrorCode =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
90
|
+
export type EnhanceErrorCode = 'rejected' | 'rate-limit' | 'concurrency-limit' | 'cooldown' | 'timeout' | 'upstream' | 'unconfigured' | 'internal' | 'effort-unsupported'
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Why the BROWSER half could not turn a response into a result.
|
|
94
|
+
*
|
|
95
|
+
* These never cross the wire: they are produced and consumed in the same
|
|
96
|
+
* process, but they travel on the same `EnhanceError` shape so the panel has one
|
|
97
|
+
* rendering path. They exist because a transport failure used to be a hardcoded
|
|
98
|
+
* Chinese string, which an English UI then showed verbatim under its localized
|
|
99
|
+
* primary line. A stable reason lets the panel localize it like any other error.
|
|
100
|
+
*/
|
|
101
|
+
export type ClientFailureReason =
|
|
102
|
+
/** The fetch itself failed: no host, wrong port, dsh web not running. */
|
|
103
|
+
| 'unreachable'
|
|
104
|
+
/** The body or envelope could not be parsed into anything usable. */
|
|
105
|
+
| 'malformed'
|
|
106
|
+
/** The host answered, but not with the plugin's JSON envelope. */
|
|
107
|
+
| 'refused'
|
|
108
|
+
/** The stream ended without a `done` frame. */
|
|
109
|
+
| 'stream-closed'
|
|
110
|
+
/** The caller aborted the request. */
|
|
111
|
+
| 'cancelled'
|
|
105
112
|
|
|
106
113
|
/** Structured failure carried on every non-2xx / ok:false response. */
|
|
107
114
|
export interface EnhanceError {
|
|
@@ -122,6 +129,8 @@ export interface EnhanceError {
|
|
|
122
129
|
* - timeout: { seconds }
|
|
123
130
|
* - rejected with { count, max }: the over-length input message
|
|
124
131
|
* - upstream: optional { reason } selecting a fix hint (auth, quota, …)
|
|
132
|
+
* - internal, browser half only: { reason } — a {@link ClientFailureReason},
|
|
133
|
+
* plus { status } on the two reasons that carry an HTTP status
|
|
125
134
|
*/
|
|
126
135
|
params?: Record<string, string | number>
|
|
127
136
|
/**
|
|
@@ -134,9 +143,7 @@ export interface EnhanceError {
|
|
|
134
143
|
}
|
|
135
144
|
|
|
136
145
|
/** Response envelope of the enhance route. */
|
|
137
|
-
export type EnhanceResponse =
|
|
138
|
-
| { ok: true; value: EnhanceResult }
|
|
139
|
-
| { ok: false; error: EnhanceError }
|
|
146
|
+
export type EnhanceResponse = { ok: true; value: EnhanceResult } | { ok: false; error: EnhanceError }
|
|
140
147
|
|
|
141
148
|
/**
|
|
142
149
|
* One SSE frame of the incremental route. `delta` carries newly displayable
|
|
@@ -144,7 +151,4 @@ export type EnhanceResponse =
|
|
|
144
151
|
* carries the authoritative result — the SAME normalized body the one-shot
|
|
145
152
|
* route returns — and `error` carries the structured failure.
|
|
146
153
|
*/
|
|
147
|
-
export type EnhanceStreamEvent =
|
|
148
|
-
| { type: 'delta'; text: string }
|
|
149
|
-
| { type: 'done'; value: EnhanceResult }
|
|
150
|
-
| { type: 'error'; error: EnhanceError }
|
|
154
|
+
export type EnhanceStreamEvent = { type: 'delta'; text: string } | { type: 'done'; value: EnhanceResult } | { type: 'error'; error: EnhanceError }
|
package/src/shared/reasoning.ts
CHANGED
|
@@ -37,13 +37,7 @@ export interface ReasoningEffortInfo {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/** Stable reason for an `unavailable` answer. */
|
|
40
|
-
export type ReasoningReason =
|
|
41
|
-
| 'no-default-model'
|
|
42
|
-
| 'default-not-deepseek'
|
|
43
|
-
| 'llm-unavailable'
|
|
44
|
-
| 'no-efforts'
|
|
45
|
-
| 'timeout'
|
|
46
|
-
| 'resolve-failed'
|
|
40
|
+
export type ReasoningReason = 'no-default-model' | 'default-not-deepseek' | 'llm-unavailable' | 'no-efforts' | 'timeout' | 'resolve-failed'
|
|
47
41
|
|
|
48
42
|
/**
|
|
49
43
|
* One capability answer.
|
|
@@ -71,12 +71,6 @@ export function createStreamNormalizer(): StreamTextNormalizer {
|
|
|
71
71
|
opened = true
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
const stripTrailingFence = (): string => {
|
|
75
|
-
const rest = buffer.replace(/```[ \t]*$/, '')
|
|
76
|
-
buffer = ''
|
|
77
|
-
return rest
|
|
78
|
-
}
|
|
79
|
-
|
|
80
74
|
return {
|
|
81
75
|
push(chunk: string): string {
|
|
82
76
|
buffer += chunk
|
package/src/shared/validate.ts
CHANGED
|
@@ -7,10 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/** Verdict of one input check. */
|
|
10
|
-
export type InputCheck =
|
|
11
|
-
| { ok: true }
|
|
12
|
-
| { ok: false; code: 'empty' }
|
|
13
|
-
| { ok: false; code: 'too-long'; count: number; max: number }
|
|
10
|
+
export type InputCheck = { ok: true } | { ok: false; code: 'empty' } | { ok: false; code: 'too-long'; count: number; max: number }
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
13
|
* Zero-width and bidi control code points stripped before emptiness checks.
|
|
@@ -24,10 +21,7 @@ export type InputCheck =
|
|
|
24
21
|
* counts toward the cap (the cap is the user-facing "characters" count).
|
|
25
22
|
*/
|
|
26
23
|
function isInvisibleCodePoint(cp: number): boolean {
|
|
27
|
-
return (cp >=
|
|
28
|
-
|| cp === 0xFEFF
|
|
29
|
-
|| (cp >= 0x202A && cp <= 0x202E)
|
|
30
|
-
|| (cp >= 0x2066 && cp <= 0x2069)
|
|
24
|
+
return (cp >= 0x200b && cp <= 0x200d) || cp === 0xfeff || (cp >= 0x202a && cp <= 0x202e) || (cp >= 0x2066 && cp <= 0x2069)
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
/**
|
|
@@ -99,4 +93,4 @@ export function formatInputCheckZh(check: InputCheck): string {
|
|
|
99
93
|
if (check.ok) return ''
|
|
100
94
|
if (check.code === 'empty') return '输入框为空,请先输入要增强的提示词。'
|
|
101
95
|
return `内容共 ${check.count} 个字符,超过 ${check.max} 个字符上限(按 Unicode 字符数统计,不是 token 数)。为避免改变原意不会自动截断,请精简后再试。`
|
|
102
|
-
}
|
|
96
|
+
}
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
# dsh-prompt-enhance 代码审阅报告
|
|
2
|
-
|
|
3
|
-
审阅对象:`dsh-prompt-enhance@0.2.2`(单包双半的 DSH Web GUI 提示词增强插件)
|
|
4
|
-
审阅方式:全量静态阅读 + 依赖契约逐项比对 + 真实 `tsc`/`vitest`/`esbuild` 验证 + **GUI 实机冒烟**
|
|
5
|
-
最终状态:typecheck 通过、**20 文件 / 207 用例全绿**、`lib/` 产物与 `src/` 一致、**插件在真实 `dsh web` 中加载并可用**
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 一、功能定位
|
|
10
|
-
|
|
11
|
-
在 dsh Web GUI 的作曲区提供一个「增强」入口:把草稿经 harness 自带 LLM 服务低温重写为结构化提示词,预览原文/结果后回填、复制或撤销。
|
|
12
|
-
|
|
13
|
-
**硬约束(修复过程中一律未改动)**
|
|
14
|
-
|
|
15
|
-
- 原文永不被动修改:只有显式「回填」才会改动作曲区,且可一键撤销。
|
|
16
|
-
- 结果永远是规范化正文(去代码围栏、trim),超长输入**拒绝而非截断**。
|
|
17
|
-
- 流式仅为显示:最终应用内容始终来自 `normalizeOutput` 的完整结果。
|
|
18
|
-
- 上下文感知失败即 fail-open 回退到单条增强,绝不因读不到历史而失败。
|
|
19
|
-
|
|
20
|
-
**模型路由优先级**:设置里的 provider/model 对 → 会话请求头路由 → harness 默认模型。
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## 二、目录结构与入口
|
|
25
|
-
|
|
26
|
-
```
|
|
27
|
-
src/
|
|
28
|
-
index.ts 宿主入口:name/inject、跨代设置分区装配、装配 route+command
|
|
29
|
-
config.ts schemastery schema、DEFAULT_CONFIG、resolveConfig
|
|
30
|
-
enhance-routes.ts 两个 POST 端点:准入闸门、限流/并发、SSE 与 JSON
|
|
31
|
-
enhance-command.ts /enhance 斜杠命令
|
|
32
|
-
orchestrate.ts 两条入口共用的路由解析 + 上下文装配
|
|
33
|
-
enhancer.ts 一次 LLM 调用:超时竞速、BlockAssembler、finish 校验、错误映射
|
|
34
|
-
context.ts 会话历史 → 有界、去框架化的 <conversation_context> 片段
|
|
35
|
-
prompts.ts 内置增强策略 + 上下文规则 + <raw_prompt> 框架化
|
|
36
|
-
loopback.ts socket + Host + Origin 三重信任围栏(防 DNS rebinding)
|
|
37
|
-
http.ts readBoundedJson / writeJson
|
|
38
|
-
sse.ts 带背压的 SSE 帧写入器(本次新增)
|
|
39
|
-
shared/ protocol / validate / normalize / stream-text(两端共用)
|
|
40
|
-
client/
|
|
41
|
-
index.tsx 词典注册、设置镜像、两个 slot 注册、快捷键
|
|
42
|
-
EnhanceButton.tsx 守卫链 + fetch + 面板 + 回填/undo
|
|
43
|
-
ResultPanel.tsx 预览面板(loading/stream/result/error、焦点陷阱、复制兜底)
|
|
44
|
-
UndoBar.tsx 撤销条
|
|
45
|
-
enhance-client.ts 同源 fetch + SSE 读取 + 类型化错误
|
|
46
|
-
settings.ts 客户端设置镜像(不 import host config)
|
|
47
|
-
shortcut.ts 快捷键解析/匹配(event.code 优先,布局无关)
|
|
48
|
-
ui-state.ts 面板/undo/session 注册表 + 微任务合并 delta
|
|
49
|
-
session-key.ts sessionId 兼容 shim(0.1.1-rc ↔ 0.1.2-rc)
|
|
50
|
-
undo-stack.ts 按会话的深度受限 undo 栈
|
|
51
|
-
locales.ts 中英文字典
|
|
52
|
-
styles.ts 自注入样式
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
- **宿主半**:`exports["."]` → `lib/index.js`(ESM,包引用保持 external)
|
|
56
|
-
- **浏览器半**:`dsh.client` → `lib/client.js`(CJS,包进 `window.__ModuleLoader__.load`)
|
|
57
|
-
- **bundle patch**:`cordis.patch.yml` 把插件行插入 web profile 名单
|
|
58
|
-
|
|
59
|
-
---
|
|
60
|
-
|
|
61
|
-
## 三、发现的问题与处置
|
|
62
|
-
|
|
63
|
-
### P0-1 `error.upstream.<reason>` 键名错配(8 个原因中 5 个失效)
|
|
64
|
-
|
|
65
|
-
线上 `reason` 是 kebab-case,字典键是 camelCase,用 `` `error.upstream.${reason}` `` 拼键导致 `invalid-credential`/`rate-limit`/`context-window`/`tool-call`/`max-tokens` 全部落空,永远退化成通用文案——而对应词条其实早已写好。
|
|
66
|
-
|
|
67
|
-
**处置**:改为显式映射表 `UPSTREAM_ERROR_KEYS`(`PromptEnhanceKey` 字面量校验),并补两层回归测试(键集一致性 + 真实面板逐 reason 渲染 + 负例)。
|
|
68
|
-
|
|
69
|
-
### P0-2 `npm run typecheck` 在 CI 第一步即断
|
|
70
|
-
|
|
71
|
-
`tests/client-components.test.tsx` 导入了未公开的 `InputState`(该类型刻意不从包根与 `./client` 转出),导致 `tsc` 报错;CI 顺序为 typecheck → test → build,因此后续从未执行。
|
|
72
|
-
|
|
73
|
-
**处置**:测试改为本地字面量类型。过程中曾尝试用 `Parameters<>` 从 slot props 反推,核实后发现 `SnapshotSelectorHook<T>` 在 selector 上对 `T` 泛型、会擦除状态类型,该写法不可编译,故弃用并在注释中说明原因。
|
|
74
|
-
|
|
75
|
-
### P0-3 运行时依赖声明不完整
|
|
76
|
-
|
|
77
|
-
- `lib/client.js` 实际 `require("react")`/`require("react-dom")`/`require("react/jsx-runtime")`,但只声明了 `react`。
|
|
78
|
-
- `lib/index.js` 静态 `import "@deepseek-ai/dsh-llm"`,却只写在 devDependencies。
|
|
79
|
-
|
|
80
|
-
**处置**:`peerDependencies` 补 `react-dom`、`@deepseek-ai/dsh-llm`;`dsh.client.inject` 补 `react-dom`。
|
|
81
|
-
|
|
82
|
-
### P1-1 SSE 响应从不结束(隐藏最深的真缺陷)
|
|
83
|
-
|
|
84
|
-
流式路由写完 `done` 帧后从不调用 `res.end()`,chunked 响应永远缺 `0\r\n\r\n`:
|
|
85
|
-
|
|
86
|
-
- 浏览器里"看起来正常"——客户端在 `done` 帧上 `return` 并 `reader.cancel()`,掩盖了它;
|
|
87
|
-
- 但**每次流式增强都泄漏一个未收尾的响应**,keep-alive 无法复用连接;
|
|
88
|
-
- 任何整 body 读取者(`response.text()`、curl、反向代理、访问日志中间件)**永久挂起**。
|
|
89
|
-
|
|
90
|
-
**处置**:路由在 `done` 帧后调用 `res.end()`(带 `writableEnded`/`destroyed` 守卫与 try/catch,避免客户端中途断开时抛出覆盖真实结果)。既有测试从未覆盖 SSE 端点,是它长期潜伏的原因;本次补了 SSE 端到端用例,并用裸 socket 字节打印取得铁证(修复前末尾无 `0\r\n\r\n`,修复后有,且 `response.text()` 从永久挂起变为 `200 / 236 bytes`)。
|
|
91
|
-
|
|
92
|
-
### P1-2 SSE 帧写入无背压
|
|
93
|
-
|
|
94
|
-
忽略 `res.write()` 返回的 `false`,慢客户端可让 socket 缓冲无界增长。
|
|
95
|
-
|
|
96
|
-
**处置**:新增 `src/sse.ts`——帧在缓冲区满时停靠至 `drain`,`close` 释放停靠,`dispose()` 释放监听;配 10 条单测。
|
|
97
|
-
|
|
98
|
-
### P0-4 浏览器半硬声明可选服务,导致整个 Web 启动失败(本次审阅引入的回归)
|
|
99
|
-
|
|
100
|
-
我把 `settingsScope` 写进了客户端半的 `inject`。cordis 的契约是"`inject` 里列了谁,就等于没有它就不加载"(`Plugin.Base.inject`:*Services the plugin requires; it only loads while all are available*),而该服务由可选的设置面板包 `@deepseek-ai/dsh-client-ui-settings` 提供——目标 profile 的 bundles 名单里没有它。结果插件永久 `pending`,shell 把"有 entry 没激活"当作致命错误,**整个 GUI 白屏**,报错为 `web boot: 1 entry did not activate`。
|
|
101
|
-
|
|
102
|
-
**处置**:改为**不硬依赖任何服务**——`slots`/`locale`/`settingsScope` 全部走可选的 `ctx.inject`,`apply` 本体不直接触碰任何服务;缺服务时降级(无设置面板则用内置默认值,无 locale 则渲染字典键)。配 `tests/client-apply.test.ts`(4 条)锁定"裸环境也必须能启动"。
|
|
103
|
-
|
|
104
|
-
### P0-5 渲染期脆性读取使插槽条目被错误边界卸载(既有缺陷)
|
|
105
|
-
|
|
106
|
-
`EnhanceButton` 直接读 `state.imageIds.length` / `state.occurrences.length`。这两个字段在插件编写时所依据的 dsh 版本里属于输入快照,但 slot 宿主不保证提供;字段缺失时 `.length` 在**渲染期**抛错,React 错误边界据此卸载整个 `conversation.input.right` 条目——表现为按钮消失 + 控制台狂刷同一条堆栈(实测 `slot entry crashed in 'conversation.input.right'`),功能彻底不可用。
|
|
107
|
-
|
|
108
|
-
**处置**:新增 `countOf` / `stringOf` 防御式读取:字段缺失或类型畸形一律降级为"无此计数 / 空串",代价仅是两条**提示性**守卫(仅图片、含命令块)不触发,增强本身照常。同时给 `UndoBar` 的 `draft` 读取加同样防护(避免把 `undefined` 误判为"用户继续输入"而静默丢弃撤销记录)。配一条回归用例:宿主只提供 `{ draft, phase }` 时按钮必须照常渲染。
|
|
109
|
-
|
|
110
|
-
### P1-3 其他
|
|
111
|
-
|
|
112
|
-
- `context.ts` 用 UTF-16 单元而非码点计上下文预算(中文/emoji 场景预算偏差约 2 倍),截断还可能劈开代理对 → 改用共享 `countText` 并按码点截断。
|
|
113
|
-
- `resolveConfig` 可原样返回 `provider: ''`,让下游把空串读成"存在覆盖" → 归一为 `undefined`。
|
|
114
|
-
- 跨会话 busy 用 `disabled` 静默禁用按钮,键盘/读屏用户连原因都读不到 → 改 `aria-disabled` + 可读原因文案。
|
|
115
|
-
- `abort()` 回调抛错会阻断面板状态迁移 → 统一用 `abortOf()` 包裹。
|
|
116
|
-
- `ResultPanel` 焦点还原挂在第二个 mount-only effect 上,StrictMode 下会立刻把焦点吐回按钮、使焦点陷阱与 Esc 失效 → 合并进同一 effect 并加"不抢用户已移动的焦点"守卫。
|
|
117
|
-
- `openError` 冗余同时传 `message` 与 `localized` → 只留 `localized`。
|
|
118
|
-
- CI `node-version: 22` 不满足 `engines: ^22.19.0` → 改为 `'22.19'`。
|
|
119
|
-
- `files` 未包含 README 引用的 `docs/` 截图 → 补入;加 `prepublishOnly` 与 `publishConfig.access`。
|
|
120
|
-
|
|
121
|
-
---
|
|
122
|
-
|
|
123
|
-
## 四、验证证据
|
|
124
|
-
|
|
125
|
-
| 检查 | 结果 |
|
|
126
|
-
|---|---|
|
|
127
|
-
| `npm run typecheck` | 通过,无错误 |
|
|
128
|
-
| `npm test` | **20 文件 / 207 用例全绿** |
|
|
129
|
-
| `npm run build` | 通过;`lib/index.js` 与 `lib/client.js` 均含最新逻辑 |
|
|
130
|
-
| `git status`(build 后) | 无产物漂移,符合 CI 的 lib 一致性校验 |
|
|
131
|
-
| SSE 协议 | 裸 socket 确认 `0\r\n\r\n` 终止符存在;`response.text()` 正常返回 |
|
|
132
|
-
| 安装形态 | `dsh plugin --profile web add link:C:\prompt-enhance-fresh`,插件行显示为 `link:` |
|
|
133
|
-
| **GUI 实机** | **插件在真实 `dsh web` 中加载成功、按钮渲染、增强流程可用** |
|
|
134
|
-
|
|
135
|
-
**审阅过程中被证伪的两处自身判断(记录以示校准)**
|
|
136
|
-
|
|
137
|
-
1. 我曾判定「`README.md`/`README.zh-CN.md` 不存在、需补写」——错,两份都存在(305/304 行),是目录列举被输出截断误导;最终未改写,只补了 `docs/` 并同步架构树。
|
|
138
|
-
2. 我曾推测 `tests/client-components.test.tsx` 导出的 `sessionId` 是类型错误——错,`dsh-client-runtime` 通过声明合并提供了它(`SessionIdentity` 非可选);真正的类型错误只有 `InputState` 一处。
|
|
139
|
-
|
|
140
|
-
**另一条被实践证的教训**:207 个自动化用例全绿,而插件在真实宿主里一加载就崩(P0-5)。差别在于"宿主不提供某些字段"这种**跨版本形状差异**,只有真正装进宿主才暴露得出来——因此新增的两组用例(`tests/client-apply.test.ts` 的裸环境启动、`tests/client-components.test.tsx` 的缺字段渲染)都把"宿主比预期更贫瘠"当作一等公民来锁定。
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
## 五、GUI 冒烟结果
|
|
145
|
-
|
|
146
|
-
已在真实 `dsh web` 中完成基础冒烟:插件加载、作曲区按钮渲染、增强流程可用。
|
|
147
|
-
|
|
148
|
-
仍建议按需逐条确认的细项(均已有自动化覆盖,此处仅列人工回归点):
|
|
149
|
-
|
|
150
|
-
1. Settings → 插件配置出现 `prompt-enhance` 分区(目标 profile 未打包设置面板时,此项**预期不出现**,功能自动使用内置默认值)。
|
|
151
|
-
2. ✨ 按钮与 `Ctrl+Alt+E` 走同一流程;空输入/超长/含引用块各自给出可读拒绝文案。
|
|
152
|
-
3. 预览面板:回填 → 撤销条出现 → 撤销恢复原文;复制可用(含非安全上下文回退)。
|
|
153
|
-
4. 流式增强:文字边写边显示,结束后面板**平滑切到规范化结果**(`display.finish()` 冲刷确保末尾字符不丢)。
|
|
154
|
-
5. `/enhance <文本>` 在命令面板可复制、不进入模型历史。
|
|
155
|
-
6. 多会话布局:另一会话增强中时,本会话按钮给出"另一个会话正在增强中"提示而非静默变灰。
|
|
156
|
-
7. 上游错误(如错误 API Key)能否显示**具体**修复提示(对应 P0-1 的修复面)。
|
|
157
|
-
|
|
158
|
-
---
|
|
159
|
-
|
|
160
|
-
## 六、安装形态提醒
|
|
161
|
-
|
|
162
|
-
当前 profile 通过 `link:` 指向本仓库,**宿主半改动必须重启 `dsh web`**;且 profile 的 `package.json` 里若残留 `^0.2.x` 语义版本声明,任何一次 `npm install` 都可能把 `link:` 覆盖回 npm 上的版本,从而静默丢掉本地修复。
|
|
163
|
-
|
|
164
|
-
---
|
|
165
|
-
|
|
166
|
-
## 七、发布记录(本次交付闭环)
|
|
167
|
-
|
|
168
|
-
审阅修复经多轮提交交付,并已发布到 npm 与 GitHub。
|
|
169
|
-
|
|
170
|
-
### 版本演进
|
|
171
|
-
|
|
172
|
-
| 版本 | 内容 | 状态 |
|
|
173
|
-
|---|---|---|
|
|
174
|
-
| `0.2.1` / `0.2.2` | 审阅前已发布(**不含本次任何修复**) | 已被 0.2.4 取代 |
|
|
175
|
-
| `0.2.3` | 四个缺陷修复(locale 映射、SSE 未结束、启动崩溃、渲染崩溃)+ 打包面补全 | 已发布,GitHub **Latest** |
|
|
176
|
-
| `0.2.4` | 仅打包修正:`@deepseek-ai/dsh-llm` 改为可满足范围的 optional peer | 已发布,npm **latest** / GitHub **Pre-release** |
|
|
177
|
-
|
|
178
|
-
### commit 批次
|
|
179
|
-
|
|
180
|
-
| 顺序 | 主题 |
|
|
181
|
-
|---|---|
|
|
182
|
-
| 1 | `fix: locale reason mapping, package peer surface, SSE backpressure` |
|
|
183
|
-
| 2 | `fix: end the SSE response so the streamed route terminates` |
|
|
184
|
-
| 3 | `fix(client): never hard-require optional services so boot cannot fail` |
|
|
185
|
-
| 4 | `fix(client): tolerate host input states without imageIds/occurrences` |
|
|
186
|
-
| 5 | `docs: finalize code review report` |
|
|
187
|
-
| 6 | `docs: add 0.2.3 changelog entry`,随后 `npm version patch` 产出 0.2.3 的版本提交与 `v0.2.3` 标签 |
|
|
188
|
-
| 7 | `fix: make the dsh-llm peer optional and satisfiable`,随后 `npm version patch` 产出 0.2.4 的版本提交与 `v0.2.4` 标签 |
|
|
189
|
-
| 8 | `docs: add release notes for 0.2.3 and 0.2.4` |
|
|
190
|
-
|
|
191
|
-
精确 hash 以 `git log --oneline` 为准;此处刻意不写 hash,避免记录随 rebase/amend 失真。
|
|
192
|
-
|
|
193
|
-
### 发布验证
|
|
194
|
-
|
|
195
|
-
| 检查 | 结果 |
|
|
196
|
-
|---|---|
|
|
197
|
-
| `npm view dsh-prompt-enhance dist-tags --json` | `latest` = **`0.2.4`** |
|
|
198
|
-
| `gh release list` | `v0.2.3` = **Latest**;`v0.2.4` = **Pre-release** |
|
|
199
|
-
| GitHub 远端 | `main` 已推送(以 `git log origin/main -1` 为准) |
|
|
200
|
-
| `npm pack` | 37 个文件(0.2.3/0.2.4 发布时),含 `lib/`、`cordis.patch.yml`、两份 README、`docs/`、`src/` |
|
|
201
|
-
| `prepublishOnly` 闸门 | `npm run build && npm test` 各执行一次,207 用例通过后才上传 |
|
|
202
|
-
|
|
203
|
-
### 过程中的两个教训
|
|
204
|
-
|
|
205
|
-
1. **npm 版本号不可覆盖。** 首次 `npm publish` 因「已发布 0.2.2」被拒——这不是权限问题(`npm whoami` 全程为 `rongxingda`),而是 npm 的版本号一次性语义。正确反应是升版本号(`npm version patch`),而非排查权限。任何"改完直接重发同号"的期望都不成立。
|
|
206
|
-
2. **`npm view` 的即时读数是旧快照。** 发布成功后立刻查 `dist-tags` 仍返回 0.2.2,属于 registry 复制延迟;判断发布是否成功应以 `npm publish` 自身的 `+ <pkg>@<version>` 输出为准,稍后再查 dist-tags 复核。
|
|
207
|
-
|
|
208
|
-
### 已知遗留(未处理,供后续决策)
|
|
209
|
-
|
|
210
|
-
- `v0.2.0`–`v0.2.2` **有标签但无 GitHub Release 页**;CHANGELOG 的口径已相应限定为「自 0.2.3 起」,因此文档与实际一致,无需补建。
|
|
211
|
-
- `files` 含 `"docs"`,因此 Release 正文等文档会随后续版本进入 npm 包(每个约几 KB,不影响功能)。
|