@y1x1n/dsh-prompt-optimizer 0.3.13
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 +178 -0
- package/README.md +180 -0
- package/cordis.patch.yml +5 -0
- package/lib/client.js +1494 -0
- package/lib/controller.js +348 -0
- package/lib/index.js +648 -0
- package/lib/prompt.js +254 -0
- package/package.json +54 -0
package/README.en.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# dsh-prompt-optimizer
|
|
2
|
+
|
|
3
|
+
[中文](README.md) | **English**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Y1X1n/dsh-prompt-optimizer/actions/workflows/ci.yml)
|
|
6
|
+
|
|
7
|
+
A DeepSeek Harness plugin that adds an **Optimize** button (✨) next to the composer, one click analyzes and rewrites the prompt draft in your input box, with results **streamed section by section over SSE**. The optimization call reuses the current session's model route by default (read live on every click — switching models in the session takes effect immediately).
|
|
8
|
+
|
|
9
|
+
- **Host side**: registers two routes, `POST /dsh-prompt-optimizer/optimize` (SSE streaming) and `POST /dsh-prompt-optimizer/test-model` (connectivity probe), and drives `ctx.llm` for the "analyze + rewrite" call.
|
|
10
|
+
- **Client side**: injects the button into the `conversation.input.right` slot, the result panel into `conversation.input.dock` (a full-width row above the input card, same family as TodoDock — renders on the new-session screen too and never covers the input box), and a collapsible settings card into `settings.plugin.item`. UI copy follows the DSH interface language (中文 / English).
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
| | |
|
|
15
|
+
|---|---|
|
|
16
|
+
|  |  |
|
|
17
|
+
|  |  |
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- An **Optimize** button on the right of the composer tool row (disabled while empty, breathing animation while working). During the wait the panel shows the live stage (waiting for model / analyzing / writing the rewrite) with an elapsed-seconds readout; on completion the model badge shows the total duration.
|
|
22
|
+
- Reads the current session's provider/model on every click; **analysis and rewrite stream onto the panel** above the input card — no staring at a spinner for the whole generation.
|
|
23
|
+
- **Dual optimization strategies**: with no conversation context (fresh session, or context disabled) it rewrites with a **structured template** (role / task / constraints / output format); with context it switches to **distill-intent + polish** — reads the recent conversation, keeps the draft's original framing instead of forcing a template, never re-asks what the context already answers, and never resurfaces **directions you already rejected** (the do-not-enter set).
|
|
24
|
+
- **Fidelity discipline** (inspired by Fishsb/dsh-prompt-enhancer): semantic-equivalence floor, traceability (inferences are marked "unless otherwise specified / by default"), a pre-output element-by-element **fidelity self-check** against drift, with the self-check explicitly outranking the length discipline (target under 800 characters for simple tasks — a missing element is worse than verbosity), and few-shot examples to stabilize output style.
|
|
25
|
+
- **Lightweight memory chain**: edit our optimized text and click Optimize again, and the previous round's result rides along as a continuity reference (accepted decisions carry over, only your changes are worked on). Same-text retries, cross-session calls, and degraded (non-wellFormed) results never ride (degradation is only judged in full mode — fast mode always counts as compliant); the chain resets the moment you send or close the panel, and it follows the "include context" switch.
|
|
26
|
+
- **Slash-command safe**: input like `/goal help me …` optimizes only the body; the command prefix is re-attached on replace — the command word is never rewritten. A bare command with no body (just `/goal`) is rejected up front with a notice instead of being "optimized" into garbage.
|
|
27
|
+
- **Context aware**: by default carries the session's recent conversation as reference (**user messages are guaranteed a floor** — agentic sessions produce far more assistant step fragments than user input, and naive recency sampling would crowd out what you actually asked), capped at 8 turns / 1600 chars; the meta-prompt explicitly forbids answering or continuing the context. Empty sessions fall back to the template strategy. Can be disabled in the settings card.
|
|
28
|
+
- Result panel: **five-dimension diagnosis** (goal clarity / context / constraints / structure / output spec) + the **full optimized prompt**; actions: **replace the input box** (with **undo**, which auto-invalidates once you edit the replaced text) / copy / re-optimize / close. **The panel closes itself after you send the message or clear the input.**
|
|
29
|
+
- **Cancel keeps the content**: press Esc while optimizing and the panel freezes in a cancelled state showing everything generated so far — review it, hit "re-optimize" to continue, or close. In any other state Esc just closes the panel. A client-side **timeout watchdog** (settings timeout + 5s margin) turns a hung Host or black-holed network into an explicit timeout error instead of an endless spinner.
|
|
30
|
+
- Panels are session-scoped: switching sessions never leaks an old result into another session's view or input box.
|
|
31
|
+
- **Auto-follow scrolling while streaming**: the live view sticks to the bottom as content grows, pauses when you scroll up, and resumes when you return near the bottom.
|
|
32
|
+
- All panel styling uses official Harness design tokens (`--dsw-alias-*`); follows light/dark theme automatically.
|
|
33
|
+
- Long-draft friendly: the output cap rises with estimated input length by default (full mode ×2 / fast mode ×1.5, capped at 32768); on real truncation the partial result is still shown with a clear notice instead of disappearing.
|
|
34
|
+
- Format tolerance: marker variants (e.g. `<<< ANALYSIS >>>`) parse correctly; in full mode, outright non-compliant output degrades gracefully with a warning; in fast mode the result is a single section, so it always counts as compliant whether or not the model emits markers (no warning, memory chain unaffected) — friendly to third-party/free models that skip the marker format — their raw stream is previewed live, character by character, until a marker appears.
|
|
35
|
+
- Settings → Plugins → "Prompt Optimizer" card (collapsed by default, click the header to expand; the collapsed header shows a "model · mode" summary; every change can be reverted in one step via "Undo last change" at the bottom — an in-memory stack of the last 20):
|
|
36
|
+
- **Model**: optimization model (**follow the current session** (default), or pin one from the model catalog, provider/model dropdown with manual refresh) / fallback model (automatic failover when the primary route fails before producing anything — the panel badge shows "· fallback", hover to see why) / connectivity ("Test connection", 32-token / 20s capped probe showing the actual route and latency)
|
|
37
|
+
- **Generation parameters**: output language (中文 / English); mode: full (analysis + rewrite) / **fast** (rewrite only, roughly half the output tokens); reasoning effort: clamp to lowest tier (default — dramatically shortens the pre-first-token stall on reasoning models) / follow session; max output tokens (default 8192), timeout (default 120s), temperature (default 0.2, hover for the rationale); auto output-cap toggle (default on)
|
|
38
|
+
- **Context**: include-context toggle (default on): when off, only the draft itself is read — no session history
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
Prerequisite: the `dsh` CLI (an environment where `npx @deepseek-ai/dsh web` works).
|
|
43
|
+
|
|
44
|
+
### From a Release (recommended, no build)
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
# Download y1x1n-dsh-prompt-optimizer.tgz (always points to the latest release), then install the local file
|
|
48
|
+
dsh plugin --profile web add ./y1x1n-dsh-prompt-optimizer.tgz
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Download: https://github.com/Y1X1n/dsh-prompt-optimizer/releases/latest/download/y1x1n-dsh-prompt-optimizer.tgz
|
|
52
|
+
|
|
53
|
+
### From source (tarball)
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
cd dsh-prompt-optimizer
|
|
57
|
+
npm install --legacy-peer-deps # the prepare hook builds lib/ automatically
|
|
58
|
+
npm pack # produces y1x1n-dsh-prompt-optimizer-<version>.tgz
|
|
59
|
+
dsh plugin --profile web add ./y1x1n-dsh-prompt-optimizer-<version>.tgz
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Then (re)start `dsh web` and open the Web UI — the button appears next to the composer.
|
|
63
|
+
|
|
64
|
+
> **Windows note**: `dsh plugin add ./directory` goes through pnpm `link:`, which currently misparses the drive-letter colon as a protocol separator and produces a broken symlink (`node_modules/<pkg>` points nowhere), so the plugin never loads. Use the tarball form for local installs; the directory-link form works on macOS/Linux.
|
|
65
|
+
|
|
66
|
+
### From GitHub
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
dsh plugin --profile web add github:Y1X1n/dsh-prompt-optimizer
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Git installs pull the source; this package self-builds at install time via its `prepare` script (only Node needed, no monorepo). pnpm ≥10 refuses to run build scripts on first install — add the package name to `allowBuilds` in that profile's `pnpm-workspace.yaml` as the terminal prompt suggests, then retry. Pinning a commit is recommended: `github:Y1X1n/dsh-prompt-optimizer#<sha>`.
|
|
73
|
+
|
|
74
|
+
### Uninstall
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
dsh plugin --profile web remove @y1x1n/dsh-prompt-optimizer
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Compatibility
|
|
81
|
+
|
|
82
|
+
- Development baseline: `@deepseek-ai/*` **0.1.0-rc.7** (matching the packages bundled with `npx @deepseek-ai/dsh@0.1.0-rc.7`); verified on the **0.1.0-rc.8** runtime (2026-08-20, Windows, real-profile install + Web routes / client bundle / session-history RPC / end-to-end LLM call), and re-verified on **0.1.1-rc.2** (2026-08-27, Windows: `--dump-config` composition layer, route registration, client bundle all healthy).
|
|
83
|
+
- Protocol contract: `/dsh-prompt-optimizer/optimize` returns 400/405/409/413 as plain JSON on pre-check failure and switches to an SSE stream on success (model errors arrive as `error` events); `/dsh-prompt-optimizer/test-model` **always returns HTTP 200** with an `ok` field in the body (a probe is application-level semantics, deliberately not mapped to transport status codes) — integrate against `ok`, not the status code.
|
|
84
|
+
- The HTTP carrier service name has drifted between releases (`httpServer` in the npm 0.0.1-rc.x type packages, `webServer` in the 0.1.0-rc.x runtime): the plugin waits on both names via `ctx.inject` with no static hard dependency — if the name changes again, only this plugin's routes fail to register (with a log warning after 10s); the Harness startup is never dragged down.
|
|
85
|
+
- Client and Host must be the same version (the SSE protocol is a private contract): after upgrading, restart `dsh web` and refresh the browser.
|
|
86
|
+
|
|
87
|
+
## FAQ
|
|
88
|
+
|
|
89
|
+
- **Clicked "Optimize" and nothing happened?** Open the browser console and look for logs starting with `[dsh-prompt-optimizer]`; common causes are no model configured (set up a provider under Settings → Models first), or the upstream slot the panel needs is not ready yet (refresh the page).
|
|
90
|
+
- **"No usable model found"?** The session has no routable model selected and no model is pinned in the settings card; fix either one. You can also expand the settings card and hit "Test connection" to confirm the route works.
|
|
91
|
+
- **Result truncated?** The panel shows a truncation notice; the auto output cap (on by default) already rises with draft length — if it is still not enough, raise "Max output tokens" in the settings card.
|
|
92
|
+
- **Session model change not taking effect?** Every click re-queries the session's current model; if it still looks wrong, check the console for a `session model query failed` warning (the first available route is used as a fallback then). Note: a model pinned in the settings card overrides the session selection.
|
|
93
|
+
- **Occasional timeout / RATE_LIMIT failures on optimization?** Transient-error retries are handled by the host at the **provider layer** (dsh 0.1.1+ provider config ships a retry policy covering `RATE_LIMIT / SERVER / TIMEOUT / TRANSPORT / EMPTY_RESPONSE` by default). Tune retry count and backoff under Settings → Models → the provider in question — not the plugin's "timeout" setting, which only bounds a single call's total duration.
|
|
94
|
+
- **Can't find the card on the settings page?** 0.3.0's strict enum schema could clash with older settings documents and stop the card from rendering; fixed in 0.3.1 (unknown enum values fall back to defaults). Make sure you are on ≥0.3.1 and refresh.
|
|
95
|
+
- **Button still there after uninstall?** Plugin-set changes only take effect after restarting `dsh web`; a page refresh is not enough.
|
|
96
|
+
|
|
97
|
+
## Verification status
|
|
98
|
+
|
|
99
|
+
Verified in a real environment (dsh 0.1.0-rc.8 tested + 0.1.1-rc.2 re-verified, Windows — see Compatibility); v0.3.9–0.3.11 were additionally verified end-to-end on a **third-party free model** (openrouter `minimax/minimax-m3:free`, which emits no marker format): both strategies, element-by-element fidelity, `[TODO]` markers, memory chain, and cancel-keeps-content all behave as designed — and this live testing is what surfaced the fast-mode false-positive fixed in v0.3.10/11.
|
|
100
|
+
|
|
101
|
+
- Composition-layer load: `--dump-config` shows the `# == dsh-prompt-optimizer` layer;
|
|
102
|
+
- Host: startup log `[dsh-prompt-optimizer] loaded`; both routes behave correctly across their 400/405/409/413 paths; SSE streaming verified live;
|
|
103
|
+
- Client: the bundle is picked up by client-modules, appears in `window.__DSH_BOOT__`, and `/plugins/dsh-prompt-optimizer/client.js` is reachable;
|
|
104
|
+
- End to end: a real `ctx.llm` call (DeepSeek route) completes "analysis + rewrite" with correct marker parsing (`wellFormed: true`).
|
|
105
|
+
- Automated tests (`npm test`, 62 cases):
|
|
106
|
+
- `test/smoke.mjs`: 28 Host smoke cases (real cordis Context + mocked services: route-resolution priority, empty/malformed config, 400/405/409/413, SSE event flow, max-tokens truncation, timeout, fast mode, reasoning clamp, legacy-settings normalization, connectivity test, fallback chain with fallback-reason passthrough, tool-calls guard, auto output cap, context injection and hard switch, strategy selection, memory-chain injection and truncation, origin fence);
|
|
107
|
+
- `test/prompt.test.mjs`: 13 meta-prompt parsing cases (marker whitespace variants, degradation paths, partial-stream parsing, stream-buffer compaction, token estimation, context payload and budgeting, strategy fork, fidelity rules and examples, memory-chain payload);
|
|
108
|
+
- `test/controller.test.mjs`: 21 client pure-logic cases (SSE frame parsing, frame coalescing, interrupted connections, session-query skipping, undo flow, retry, close-abort, cancel keeping partial output plus its state gate, client timeout watchdog, history extraction filtering and degradation, slash-prefix splitting and bare-command rejection, memory-chain passing and gates, send-to-close decisions, duration recording, user-first context sampling).
|
|
109
|
+
|
|
110
|
+
## How it works
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
Click "Optimize"
|
|
114
|
+
→ Client reads the input draft + the session's model selection (session.models RPC, queried
|
|
115
|
+
live on every click; skipped when a model is pinned in settings — the Host's pinned value wins)
|
|
116
|
+
→ POST /dsh-prompt-optimizer/optimize { text, provider, model, reasoningEffort, context?, previous? }
|
|
117
|
+
→ Host calls ctx.llm.stream() with the system meta-prompt
|
|
118
|
+
(route resolution: pinned setting → session selection → first available route; an optional
|
|
119
|
+
fallback route fails over on zero-output failure; the output cap rises with estimated input
|
|
120
|
+
tokens by default; optional reasoning clamp / low temperature)
|
|
121
|
+
→ text-delta chunks are pushed over SSE and the panel shows "analysis / rewrite" sections live
|
|
122
|
+
(incrementally parsed on <<<ANALYSIS>>> / <<<OPTIMIZED>>> markers, whitespace variants tolerated)
|
|
123
|
+
→ the done event carries the final parsed result; a max-tokens finish carries a truncated flag; a fallback adds its reason
|
|
124
|
+
→ one click replaces the input box (undoable, slash prefix re-attached) / copy / re-optimize (edits continue from the previous result; same text regenerates fresh)
|
|
125
|
+
→ Esc while optimizing = cancel (a cancelled state keeps what was generated); a client-side watchdog guards against an unresponsive Host
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Development
|
|
129
|
+
|
|
130
|
+
```sh
|
|
131
|
+
npm install --legacy-peer-deps # install deps and trigger the build
|
|
132
|
+
npm run sync:types # sync the client type packages (see below)
|
|
133
|
+
npm run typecheck # tsc --noEmit
|
|
134
|
+
npm run build # produces lib/{index,client,prompt,controller}.js
|
|
135
|
+
npm test # smoke + prompt + controller suites
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### About `sync:types`
|
|
139
|
+
|
|
140
|
+
The upstream monorepo publishes only part of the `@deepseek-ai/*` packages (the rest are `publishConfig: restricted`), so the client type packages' transitive dependencies cannot be installed from npm. `scripts/sync-types.mjs` handles it:
|
|
141
|
+
|
|
142
|
+
1. Published packages are `npm pack`ed and extracted straight into `node_modules` (bypassing npm's dependency-tree resolution);
|
|
143
|
+
2. For unpublished packages (e.g. `dsh-type-meta`), it scans every `.d.ts` reference and generates minimal stub packages (only module resolvability matters under `skipLibCheck`).
|
|
144
|
+
|
|
145
|
+
These packages only participate in type checking; at runtime everything is provided by the Harness page/process (`react` and `@deepseek-ai/*` are external).
|
|
146
|
+
|
|
147
|
+
### Directory layout
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
dsh-prompt-optimizer/
|
|
151
|
+
├── package.json # dsh.bundle + dsh.client dual manifest
|
|
152
|
+
├── cordis.patch.yml # composition layer: inserts the Host plugin line
|
|
153
|
+
├── src/
|
|
154
|
+
│ ├── index.ts # Host plugin: settings namespace + two HTTP routes + llm call (with fallback chain)
|
|
155
|
+
│ ├── prompt.ts # meta-prompts, marker parsing, token estimation (pure functions)
|
|
156
|
+
│ └── client/
|
|
157
|
+
│ ├── index.tsx # Client entry: slot registrations
|
|
158
|
+
│ ├── controller.ts # shared state machine for button/panel + SSE consumption (standalone artifact, unit-testable)
|
|
159
|
+
│ ├── i18n.ts # zh/en UI copy, follows the DSH interface language
|
|
160
|
+
│ ├── OptimizeButton.tsx # composer button
|
|
161
|
+
│ ├── ResultDock.tsx # result panel above the input card (live streaming + undo)
|
|
162
|
+
│ ├── SettingsCard.tsx # collapsible settings card
|
|
163
|
+
│ └── SparkleIcon.tsx # hand-drawn ✨ icon
|
|
164
|
+
├── scripts/build.mjs # esbuild: Host ESM + Client lazy-CJS factory + two test artifacts
|
|
165
|
+
├── scripts/sync-types.mjs
|
|
166
|
+
├── scripts/prompt-probe.mjs # prompt evidence probe: fires real optimize requests at a running instance (PROBE_ROUTE=provider/model to pin a route)
|
|
167
|
+
└── test/ # smoke.mjs (Host) / prompt.test.mjs / controller.test.mjs
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Security notes
|
|
171
|
+
|
|
172
|
+
- The HTTP routes are registered on dsh's own web server, which listens on `127.0.0.1` by default. If you expose dsh to your LAN (`0.0.0.0`), this plugin's optimize endpoint becomes callable from the LAN too — it consumes your configured model quota; be aware.
|
|
173
|
+
- Both routes enforce an **origin fence**: requests carrying an `Origin` header (browser POSTs always do) must match `Host`, and `Host` must be a loopback address or the machine's actual local address — cross-site forged requests (CSRF) and DNS rebinding get a 403; command-line calls without `Origin` are unaffected.
|
|
174
|
+
- The plugin holds no API keys: every model call goes through the Harness-configured `ctx.llm` routes.
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# dsh-prompt-optimizer
|
|
2
|
+
|
|
3
|
+
**中文** | [English](README.en.md)
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Y1X1n/dsh-prompt-optimizer/actions/workflows/ci.yml)
|
|
6
|
+
|
|
7
|
+
DeepSeek Harness 插件:在会话输入框(发送栏)旁提供一个「优化」按钮(✨ 图标),一键分析并优化当前输入的提示词草稿,**结果经 SSE 流式逐段上屏**。优化调用默认复用当前会话的模型路由(每次点击实时读取,会话里切换模型立即生效)。
|
|
8
|
+
|
|
9
|
+
- **Host 半侧**:注册 `POST /dsh-prompt-optimizer/optimize`(SSE 流式)与 `POST /dsh-prompt-optimizer/test-model`(连通性探活)两条路由,调用 `ctx.llm` 完成「分析 + 改写」。
|
|
10
|
+
- **Client 半侧**:向 `conversation.input.right` 槽位注入按钮,向 `conversation.input.dock` 注入结果面板(输入卡上方整行、与 TodoDock 同族,新会话界面也渲染,且不遮挡输入框),向 `settings.plugin.item` 注入可折叠的设置卡片(设置页自动获得配置界面,无需单独开发页面)。界面文案跟随 DSH 界面语言(中文 / English)。
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
| | |
|
|
15
|
+
|---|---|
|
|
16
|
+
|  |  |
|
|
17
|
+
|  |  |
|
|
18
|
+
|
|
19
|
+
## 功能
|
|
20
|
+
|
|
21
|
+
- 发送栏工具行右侧新增「优化」按钮(输入为空时禁用,优化中带呼吸动画);等待期间面板实时显示当前阶段(等待模型响应 / 分析诊断 / 输出优化稿)与已用秒数,完成后模型徽章处展示总用时。
|
|
22
|
+
- 点击后实时读取当前会话的 provider/model 发起辅助调用;**分析与优化结果逐段流式显示**在输入卡上方的面板里,不用干等整段生成。
|
|
23
|
+
- **双策略优化**:无上下文(空会话/关闭携带)时按**结构模板**规范化改写(角色/任务/约束/输出格式);有上下文时自动切换为**提炼目的 + 顺势润色**——先通读会话近期对话提炼真实意图,保留草稿原始表达框架打磨,不套模板,不重复追问上下文已给出的信息,且**已否决方向不再重提**(禁入集合)。
|
|
24
|
+
- **保真纪律**(借鉴 Fishsb/dsh-prompt-enhancer):语义等价底线、来源可回溯(推断处以「如无特别说明/默认」标注)、输出前逐要素**保真自检**防漂移且**保真自检优先于长度纪律**(简单任务目标 800 字符内,要素缺失比冗长更严重),并带 few-shot 示例稳定输出风格。
|
|
25
|
+
- **轻量记忆链**:在我们产出的优化稿上继续修改后再点优化,自动携带上一轮结果作为延续参考(沿用已确认决策,只围绕变化点调整);同文重试、跨会话、上轮格式退化的结果都不带(格式退化仅在完整模式判定,快速模式恒视为合规),发送或关闭面板后自然归零;跟随「携带上下文」开关,关闭后不携带。
|
|
26
|
+
- **斜杠命令安全**:「/goal 帮我……」这类输入只优化正文,替换时自动拼回命令前缀,不会把命令词改坏;只敲命令没有正文时直接提示、不发无意义请求。
|
|
27
|
+
- **上下文感知**:默认携带当前会话的近期对话作为参考(**用户消息优先保底**——agentic 会话里 assistant 步骤碎片远多于用户输入,纯按时间取样会把你的真实诉求挤出去),总量 ≤8 条 / 1600 字符;上下文仅供模型理解意图,提示词中明确禁止其回答或延续上下文;空会话自动退化为模板策略。可在设置卡关闭。
|
|
28
|
+
- 结果面板:**分析诊断**(五维度:目标清晰度/上下文/约束/结构/输出规格)+ **优化后的完整提示词**;操作:**替换输入框**(可**撤回**,在替换内容上继续编辑时撤回入口自动失效)/ 复制 / 重新优化 / 关闭。**发送消息后或清空输入框时面板自动关闭。**
|
|
29
|
+
- **取消不丢内容**:优化中按 Esc 取消,面板定格展示已生成的部分,可直接查看、「重新优化」续跑或关闭;非加载状态按 Esc 直接关闭面板。客户端另带**超时看门狗**(设置超时秒数 + 5s 余量):Host 挂死或网络黑洞时不再无限转圈,直接落明确的超时错误。
|
|
30
|
+
- 面板按会话隔离:切换会话后旧结果不会飘到别的会话,也不会误替换别的会话的输入框。
|
|
31
|
+
- **流式自动跟随滚动**:实况内容增长时贴住底部,向上滚动即暂停跟随,滚回底部附近自动恢复。
|
|
32
|
+
- 面板样式全部走 Harness 官方设计令牌(`--dsw-alias-*`),明暗主题自动跟随。
|
|
33
|
+
- 长草稿友好:输出上限默认跟随输入长度自动抬升(按本地 token 估算,完整模式 ×2 / 快速模式 ×1.5,32768 封顶);真截断时也会展示已生成部分并明确提示,不会整段消失。
|
|
34
|
+
- 格式容错:模型输出的标记变体(如 `<<< ANALYSIS >>>`)能正确解析;完整模式不遵守格式时降级展示并给出警示;快速模式只有单段落,无论模型是否输出标记都视为合规(不弹警示、记忆链照常携带)——对不输出标记的第三方/免费模型友好,且其原始流可直接实时逐字预览(标记出现前)。
|
|
35
|
+
- 设置 → 插件配置 → 「提示词优化」卡片(默认收起,点击标题栏展开;收起时标题栏显示「模型 · 模式」关键摘要;所有改动可一步撤销——底部「撤销上一次修改」,内存栈最近 20 次):
|
|
36
|
+
- **模型**:优化用模型(**跟随当前会话**(默认),或从模型目录固定一个,provider/model 下拉、目录可手动刷新)/ 回退模型(主路由零产出失败时自动 failover,面板徽章显示「· 已回退」,悬停可见失败原因)/ 模型连通性(「测试连接」,32 token / 20s 封顶探活,显示实际路由与耗时)
|
|
37
|
+
- **调用参数**:输出语言(中文 / English);优化模式:完整(分析 + 优化)/ **快速**(仅优化,输出 token 约减半);推理强度:降到最低档(默认,推理模型首 token 前的空转显著缩短)/ 跟随会话;最大输出 Token(默认 8192)、超时时间(默认 120s)、采样温度(默认 0.2,悬停见设计依据);输出上限自适应开关(默认开)
|
|
38
|
+
- **上下文**:携带上下文开关(默认开):关闭后仅看草稿本身,不读取会话历史
|
|
39
|
+
|
|
40
|
+
## 安装
|
|
41
|
+
|
|
42
|
+
前提:已安装 `dsh` CLI(`npx @deepseek-ai/dsh web` 可用的环境)。
|
|
43
|
+
|
|
44
|
+
### 从 Release 安装(推荐,免构建)
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
# 下载 y1x1n-dsh-prompt-optimizer.tgz(始终指向最新版),再安装本地文件
|
|
48
|
+
dsh plugin --profile web add ./y1x1n-dsh-prompt-optimizer.tgz
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
下载地址:https://github.com/Y1X1n/dsh-prompt-optimizer/releases/latest/download/y1x1n-dsh-prompt-optimizer.tgz
|
|
52
|
+
|
|
53
|
+
### 本地源码安装(tarball)
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
cd dsh-prompt-optimizer
|
|
57
|
+
npm install --legacy-peer-deps # prepare 钩子会自动构建 lib/
|
|
58
|
+
npm pack # 产出 y1x1n-dsh-prompt-optimizer-<version>.tgz
|
|
59
|
+
dsh plugin --profile web add ./y1x1n-dsh-prompt-optimizer-<version>.tgz
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
然后(重新)启动 `dsh web`,打开 Web UI 即可在发送栏旁看到按钮。
|
|
63
|
+
|
|
64
|
+
> **Windows 注意**:`dsh plugin add ./目录` 走 pnpm 的 `link:`,目前会把盘符冒号错解析成协议分隔符,生成失效符号链接(`node_modules/<pkg>` 指向不存在的路径),表现为插件不加载。本地安装请用上面的 tarball 形式;目录链接形式在 macOS/Linux 正常。
|
|
65
|
+
|
|
66
|
+
### 从 GitHub 安装
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
dsh plugin --profile web add github:Y1X1n/dsh-prompt-optimizer
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Git 安装拉取的是源码,本包通过 `prepare` 脚本在安装时自包含构建(只需 Node,不依赖 monorepo 环境)。pnpm ≥10 首次会拒绝运行构建脚本,按终端提示把包名加入该 profile 的 `pnpm-workspace.yaml` 的 `allowBuilds` 后重试即可。建议锁定 commit:`github:Y1X1n/dsh-prompt-optimizer#<sha>`。
|
|
73
|
+
|
|
74
|
+
### 卸载
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
dsh plugin --profile web remove @y1x1n/dsh-prompt-optimizer
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 兼容性
|
|
81
|
+
|
|
82
|
+
- 开发基线:`@deepseek-ai/*` **0.1.0-rc.7**(与 `npx @deepseek-ai/dsh@0.1.0-rc.7` 内置包一致);已在 **0.1.0-rc.8** 运行时实测通过(2026-08-20,Windows,真实 profile 安装 + Web 路由/客户端 bundle/会话历史 RPC/端到端 LLM 调用),并在 **0.1.1-rc.2** 上复测通过(2026-08-27,Windows:`--dump-config` 组合层、路由注册、client bundle 均正常)。
|
|
83
|
+
- HTTP 载体服务名在发布版间漂移过(npm 0.0.1-rc.x 类型包叫 `httpServer`,0.1.0-rc.x 运行时叫 `webServer`):本插件用 `ctx.inject` 同时等待两个名字,且不做静态硬依赖——即使服务名再次变化,也只会使本插件的路由不注册(10 秒后日志告警),不会拖垮整个 Harness 启动。
|
|
84
|
+
- **客户端协议口径**:`/dsh-prompt-optimizer/optimize` 预校验失败返回 400/405/409/413(普通 JSON),成功后进入 SSE 流,模型错误经 `error` 事件传达;`/dsh-prompt-optimizer/test-model` **无论成败一律 HTTP 200**,由 body 的 `ok` 字段区分(探活是应用层语义,刻意不走传输层状态码)——对接方请以 `ok` 为准。
|
|
85
|
+
- 客户端与 Host 需同版本(SSE 协议是私有约定):升级插件后请重启 `dsh web` 并刷新浏览器页面。
|
|
86
|
+
|
|
87
|
+
## 常见问题
|
|
88
|
+
|
|
89
|
+
- **点了「优化」没有反应?** 打开浏览器 Console 查看 `[dsh-prompt-optimizer]` 开头的日志;常见原因是未配置任何模型(先在 设置 → 模型 里配好提供方),或面板所需的上游槽位尚未就绪(刷新页面)。
|
|
90
|
+
- **提示「未找到可用模型」?** 会话没有选择可路由的模型,且设置卡里也没有固定模型;两者补其一即可。也可以展开设置卡点「测试连接」确认路由可用。
|
|
91
|
+
- **结果被截断?** 面板会出现截断提示;默认开启的「输出上限自适应」会按草稿长度自动抬升上限,仍不够再到设置卡调高「最大输出 Token」。
|
|
92
|
+
- **换了会话模型没生效?** 每次点击都会实时查询会话当前模型;若仍不对,看 Console 是否有 `会话模型查询失败` 的警告(此时会用第一个可用路由兜底)。注意:设置卡里固定了模型时会话选择不生效。
|
|
93
|
+
- **优化调用偶发超时 / RATE_LIMIT 失败?** 这类瞬态错误的重试由宿主在**提供方层**统一处理(dsh 0.1.1+ 的提供方配置内置重试策略,默认覆盖 `RATE_LIMIT / SERVER / TIMEOUT / TRANSPORT / EMPTY_RESPONSE`)。到 设置 → 模型 → 对应提供方 里调整重试次数与退避,而不是调本插件的「超时时间」——后者只管单次调用的总时长。
|
|
94
|
+
- **设置页找不到卡片?** 0.3.0 的严格枚举 schema 与旧版设置文档冲突会导致卡片不渲染,0.3.1 起已修复(未知枚举值自动回落默认);确认版本 ≥0.3.1 并刷新页面。
|
|
95
|
+
- **卸载后按钮还在?** 插件集合变更需重启 `dsh web` 才生效;刷新页面不够。
|
|
96
|
+
|
|
97
|
+
## 验证状态
|
|
98
|
+
|
|
99
|
+
已在真实环境验证(dsh 0.1.0-rc.8 实测 + 0.1.1-rc.2 复测,Windows,详见「兼容性」);v0.3.9–0.3.11 另在**第三方免费模型**(openrouter 的 `minimax/minimax-m3:free`,不输出标记格式)上完成端到端实测:模板/意图双策略、保真逐要素保留、待补充标记、记忆链与取消保留等行为均按设计工作,并据此修复了快速模式的格式误报。
|
|
100
|
+
|
|
101
|
+
- 组合层加载:`--dump-config` 出现 `# == dsh-prompt-optimizer` 层;
|
|
102
|
+
- Host:启动日志 `[dsh-prompt-optimizer] loaded`,优化路由与测试路由的 400/405/409/413 各路径行为正确,SSE 流式输出实测正常;
|
|
103
|
+
- Client:bundle 被 client-modules 扫描收录并出现在 `window.__DSH_BOOT__`,`/plugins/dsh-prompt-optimizer/client.js` 可访问;
|
|
104
|
+
- 端到端:真实调用 `ctx.llm`(DeepSeek 路由)完成「分析 + 优化」,标记解析正确(`wellFormed: true`)。
|
|
105
|
+
- 自动化测试(`npm test`,共 62 例):
|
|
106
|
+
- `test/smoke.mjs`:28 项 Host 冒烟用例(真实 cordis Context + mock 服务,覆盖路由解析优先级、空字符串/畸形配置、400/405/409/413、SSE 事件流、max-tokens 截断、超时、快速模式、推理钳档、旧版设置文档归一化、连接测试、回退链与回退原因透传、tool-calls 防御、输出上限自适应、上下文注入与硬开关、策略选择、记忆链注入与截断、来源围栏);
|
|
107
|
+
- `test/prompt.test.mjs`:13 项元提示词解析用例(标记空白变体、降级路径、流式实况解析、流式缓冲压缩、token 估算、上下文载荷与预算收敛、策略分叉、保真纪律与示例、记忆链载荷);
|
|
108
|
+
- `test/controller.test.mjs`:21 项客户端纯逻辑用例(SSE 帧解析、合帧节流、连接中断、跳过会话查询、撤回流转、retry、close 中止、cancel 取消保留已生成部分及其状态门槛、客户端超时看门狗、历史提取过滤与失败降级、斜杠前缀拆分与空命令拦截、记忆链传递与门槛、发送即关闭判定、耗时记录、上下文取样用户消息保底)。
|
|
109
|
+
|
|
110
|
+
## 工作原理
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
点击「优化」
|
|
114
|
+
→ Client 拆分斜杠命令前缀(/goal 等只优化正文),并行拉取当前会话模型选择
|
|
115
|
+
(session.models RPC,每次点击实时查询;设置里固定了模型时跳过,Host 固定值优先)
|
|
116
|
+
与会话近期对话(session.history RPC,最近 8 条/1600 字符封顶,可在设置卡关闭)
|
|
117
|
+
→ POST /dsh-prompt-optimizer/optimize { text, provider, model, reasoningEffort, context?, previous? }
|
|
118
|
+
(previous = 轻量记忆链:在优化稿上修改后再优化时携带的上轮结果)
|
|
119
|
+
→ Host 以系统元提示词调用 ctx.llm.stream()
|
|
120
|
+
(路由解析:设置固定值 → 会话选择 → 第一个可用路由;可配回退路由,零产出失败自动 failover;
|
|
121
|
+
策略分叉:有上下文走「提炼目的+润色」,无上下文走「结构模板」;
|
|
122
|
+
输出上限默认按输入 token 估算抬升;默认推理钳最低档/低温度)
|
|
123
|
+
→ text-delta 经 SSE 逐段推送,面板实时显示「分析诊断 / 优化结果」两段
|
|
124
|
+
(按 <<<ANALYSIS>>> / <<<OPTIMIZED>>> 标记增量解析,容忍标记空白变体)
|
|
125
|
+
→ done 事件携带最终解析结果;max-tokens 结束带 truncated 标记;回退发生时附 fallbackReason
|
|
126
|
+
→ 一键替换输入框(可撤回,自动拼回斜杠前缀)/ 复制 / 重新优化(改稿续接带上轮结果,同文全新生成);发送消息后面板自动关闭
|
|
127
|
+
→ 优化中 Esc = 取消(cancelled 态,保留已生成的部分);客户端看门狗在 Host 无响应时兜底超时
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## 开发
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
npm install --legacy-peer-deps # 安装依赖并触发构建
|
|
134
|
+
npm run sync:types # 同步客户端类型包(见下)
|
|
135
|
+
npm run typecheck # tsc --noEmit
|
|
136
|
+
npm run build # 产出 lib/{index,client,prompt,controller}.js
|
|
137
|
+
npm test # smoke + prompt + controller 三套测试
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 关于 `sync:types`
|
|
141
|
+
|
|
142
|
+
上游 monorepo 只发布了部分 `@deepseek-ai/*` 包(其余 `publishConfig: restricted`),客户端类型包的传递依赖无法直接从 npm 安装。`scripts/sync-types.mjs` 的处理方式:
|
|
143
|
+
|
|
144
|
+
1. 对已发布的包,`npm pack` 后直接解压进 `node_modules`(绕开 npm 依赖树解析);
|
|
145
|
+
2. 对未发布的包(如 `dsh-type-meta`),扫描全部 `.d.ts` 引用并生成最小占位包(`skipLibCheck` 下仅要求模块可解析)。
|
|
146
|
+
|
|
147
|
+
这些包只参与类型检查;运行时一律由 Harness 页面/进程提供(`react`、`@deepseek-ai/*` 均为外部依赖)。
|
|
148
|
+
|
|
149
|
+
### 目录结构
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
dsh-prompt-optimizer/
|
|
153
|
+
├── package.json # dsh.bundle + dsh.client 双 manifest
|
|
154
|
+
├── cordis.patch.yml # 组合层:插入 Host 插件行
|
|
155
|
+
├── src/
|
|
156
|
+
│ ├── index.ts # Host 插件:设置命名空间 + 两条 HTTP 路由 + llm 调用(含回退链)
|
|
157
|
+
│ ├── prompt.ts # 元提示词、标记解析、token 估算(纯函数)
|
|
158
|
+
│ └── client/
|
|
159
|
+
│ ├── index.tsx # Client 入口:槽位注册
|
|
160
|
+
│ ├── controller.ts # 按钮/面板共享的状态机与 SSE 消费(独立产物,可单测)
|
|
161
|
+
│ ├── i18n.ts # 界面文案 zh/en 字典,跟随 DSH 界面语言
|
|
162
|
+
│ ├── OptimizeButton.tsx # 发送栏按钮
|
|
163
|
+
│ ├── ResultDock.tsx # 输入卡上方的结果面板(流式实况 + 撤回)
|
|
164
|
+
│ ├── SettingsCard.tsx # 设置页折叠卡片
|
|
165
|
+
│ └── SparkleIcon.tsx # 手绘 ✨ 图标
|
|
166
|
+
├── scripts/build.mjs # esbuild:Host ESM + Client lazy-CJS factory + 两个测试用产物
|
|
167
|
+
├── scripts/sync-types.mjs
|
|
168
|
+
├── scripts/prompt-probe.mjs # 提示词实证探针:对运行实例发真实优化请求(PROBE_ROUTE=provider/model 可指定路由)
|
|
169
|
+
└── test/ # smoke.mjs(Host)/ prompt.test.mjs / controller.test.mjs
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## 安全说明
|
|
173
|
+
|
|
174
|
+
- HTTP 路由注册在 dsh 自带的 Web 服务器上。默认监听 `127.0.0.1`;若把 dsh 暴露到局域网(`0.0.0.0`),本插件的优化接口同样可被局域网调用——它会消耗你配置的模型额度,请知悉。
|
|
175
|
+
- 两条路由均带**来源围栏**:带 `Origin` 头的请求(浏览器 POST 必带)必须与 `Host` 同源,且 `Host` 必须是回环地址或本机实际地址——网页侧的跨站伪造请求(CSRF)与 DNS rebinding 会被 403 拒绝;不带 `Origin` 的命令行调用不受影响。
|
|
176
|
+
- 插件不持有任何 API Key:模型调用全部经由 Harness 已配置的 `ctx.llm` 路由。
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT
|