@surfishs/dsh-prompt-enhance 0.1.4 → 0.1.6

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 CHANGED
@@ -25,9 +25,9 @@ The plugin adds a Prompt enhancement button to the composer toolbar: one click s
25
25
 
26
26
  The result appears in a card docked above the composer without covering the conversation; your draft is never replaced — apply it, copy it, regenerate it, or undo, and the original text can always be restored.
27
27
 
28
- With context awareness on, the plugin passes the recent messages you and the assistant exchanged to the model, helping it understand references and the task background (such as what "it" or "that" means, or premises the draft leaves out), so the rewrite matches what you meant. Only messages you sent and the assistant's replies are counted; environment, skill, time and other context injected by DSH are not conversation messages. Turn it off in the settings page or the right-click quick settings card at any time.
28
+ With context awareness on, the plugin passes the most recent messages between you and the assistant to the model, helping it resolve references such as "it" or "that" and fill in task context the draft leaves out, so the rewrite stays closer to what you meant.
29
29
 
30
- > **Note**: it only reads the most recent messages. If the draft depends on a topic that has dropped out of that window, add the needed background to the draft; otherwise the rewrite may drift from your intent.
30
+ > **Note**: the context window is limited. If the draft depends on earlier messages, add the necessary background to the draft; otherwise the rewrite may drift from your intent.
31
31
 
32
32
  ## Feature overview
33
33
 
@@ -40,7 +40,7 @@ With context awareness on, the plugin passes the recent messages you and the ass
40
40
  | 🔄 **Regenerate** | Request another version from the current text; an applied enhanced draft can be regenerated too |
41
41
  | 💬 **`/enhance` command** | Rewrite any text from the slash menu; the result never enters model history |
42
42
  | 🧭 **Model routing** | A pinned `provider` + `model` pair wins; otherwise it follows the session model, then the global default |
43
- | 🧩 **Context aware** | With `contextAware` on, recent messages resolve references such as "it" or "that" (DSH-injected environment/skill/time and other messages are excluded); turn it off at any time |
43
+ | 🧩 **Context aware** | With `contextAware` on, recent messages between you and the assistant resolve references such as "it" or "that" and fill in task context the draft leaves out; you can turn it off at any time |
44
44
  | 🔑 **Zero credentials** | Calls use DSH's model service and credential store; the plugin never reads or stores keys |
45
45
  | 🛡️ **Draft safety** | Empty, attachment-only, over-length and command-or-reference drafts are blocked locally; upstream failures get a readable message and never touch the draft |
46
46
  | ⚙️ **Live settings** | Fields save automatically, common rows first and the rest under Advanced; changes apply on the next call |
@@ -111,12 +111,20 @@ dsh plugin --profile <profile> remove @surfishs/dsh-prompt-enhance
111
111
  3. **Undo**: click Undo in the card footer to restore the original draft; the card expands again.
112
112
  4. **Close**: sending the draft closes the card automatically; Escape also closes it.
113
113
 
114
+ ### Button appearance
115
+
116
+ ![Enhance button states: empty, default and hover](./docs/images/enhance-button.png)
117
+
118
+ The button sits at the right end of the composer toolbar. From left to right: empty (dimmed), default and hover.
119
+
114
120
  An empty or attachment-only draft dims the button and makes it unclickable; Regenerate is also dimmed on an empty draft and says to type text first; over-length drafts, or drafts containing commands or file references, are rejected locally. A result that hits the token cap shows a red truncation warning; once the draft changes, that warning yields to the stale-draft notice.
115
121
 
116
122
  ### Quick settings card
117
123
 
118
124
  Right-click or long-press the enhance button in the toolbar to open the quick settings card: choose a reasoning tier inside its list, toggle context awareness directly, and read the pinned provider/model (or "Follow session"). The card only changes settings and never starts an enhancement; full settings stay on the settings page.
119
125
 
126
+ ![Quick settings card opened by right-click or long press](./docs/images/quick-settings-en.png)
127
+
120
128
  `/enhance <text>`: rewrite any text from the slash menu; the result is copyable and never enters model history.
121
129
 
122
130
  The ⓘ in the result-card footer reports the conversation messages and characters this enhancement actually referenced, e.g. `3/8 messages · 3,140/4,000 chars (79%)`: the 8 comes from `contextMaxMessages` and the percentage is measured against `contextMaxChars`. DSH-injected environment, skill, time and other messages are not counted.
package/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  [![license](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/surfishs/dsh-prompt-enhance/blob/main/LICENSE)
8
8
  [![平台:DSH 桌面版 + Web 版](https://img.shields.io/badge/Platform-DSH%20Desktop%20%2B%20Web-2F81F7?style=flat)](https://github.com/surfishs/dsh-prompt-enhance/blob/main/README.md#安装)
9
9
  [![支持的 DSH 版本:0.1.1–0.2.x](https://img.shields.io/badge/DSH-0.1.1%E2%80%930.2.x-8A2BE2?style=flat)](https://github.com/surfishs/dsh-prompt-enhance/blob/main/README.md#环境要求)
10
+ [![awesome · DSH plugin](https://img.shields.io/badge/awesome%20%C2%B7%20DSH%20plugin-%E2%9C%93-c0392b?labelColor=f6f2ea&style=flat)](https://awesome-dsh-plugin.com/)
10
11
  [![GitHub stars](https://img.shields.io/github/stars/surfishs/dsh-prompt-enhance.svg?style=flat)](https://github.com/surfishs/dsh-prompt-enhance/stargazers)
11
12
 
12
13
  简体中文 | [English](./README.en.md)
@@ -25,9 +26,9 @@
25
26
 
26
27
  结果出现在输入框上方的停靠卡里,不遮挡对话;草稿不会被直接替换——你可以回填、复制、重新生成或撤销,原文随时可恢复。
27
28
 
28
- 开启上下文感知后,插件会把最近几条你和助手的消息提供给模型参考,帮助它理解指代与任务背景(比如「它」「这个」指什么、草稿省略了哪些前提),让改写更贴近你原本的意思。统计只计入你本人发送的消息与助手回复,DSH 注入的环境、技能、时间等说明不算对话消息。可在设置页或右键快捷卡里随时关闭。
29
+ 开启后,插件会参考最近几条你和助手的消息,帮模型理解「它」「这个」指什么,补全草稿省略的任务背景,让改写更贴近原意。
29
30
 
30
- > **注意**:它只参考最近几条消息。如果草稿依赖的话题已经不在最近几条消息里,请把必要背景补进草稿,否则改写可能偏离你的原意。
31
+ > **注意**:上下文窗口有限;如果草稿依赖更早的消息,请把必要背景补进草稿,否则改写可能偏离你的原意。
31
32
 
32
33
  ## 功能一览
33
34
 
@@ -40,7 +41,7 @@
40
41
  | 🔄 **重新生成** | 按当前输入框内容再请求一版;回填后的增强稿也可继续生成 |
41
42
  | 💬 **`/enhance` 命令** | 斜杠面板直接改写任意文本;结果不进入模型历史 |
42
43
  | 🧭 **模型路由** | 设置中同时指定 `provider` 与 `model` 时优先;否则依次跟随当前会话模型、全局默认模型 |
43
- | 🧩 **上下文感知** | `contextAware` 开启时参考最近几条消息补全「它 / 这个」这类指代(DSH 注入的环境/技能/时间等消息不计入),可随时关闭 |
44
+ | 🧩 **上下文感知** | `contextAware` 开启时参考最近几条你与助手的消息,补全「它 / 这个」这类指代和草稿省略的任务背景;可随时关闭 |
44
45
  | 🔑 **零凭据配置** | 调用走 DSH 的模型服务,密钥来自 DSH 凭据存储;插件不读取也不保存 |
45
46
  | 🛡️ **草稿安全** | 空输入、仅附件、超长、含命令或文件引用都在本地拦截;上游失败给出可读提示,且不改动草稿 |
46
47
  | ⚙️ **配置热生效** | 字段改完自动保存,常用项在前、其余收进「高级设置」;下一次调用立即生效 |
@@ -111,12 +112,20 @@ dsh plugin --profile <profile> remove @surfishs/dsh-prompt-enhance
111
112
  3. **撤销**:点卡片底部的「撤销」恢复原文,同时展开结果卡。
112
113
  4. **关闭**:发送消息时卡片自动收起,按 `Esc` 也可以关闭。
113
114
 
115
+ ### 按钮外观
116
+
117
+ ![增强按钮的三种状态:空输入、默认、悬停](./docs/images/enhance-button.png)
118
+
119
+ 按钮位于输入框工具栏右侧。从左到右:空输入(灰)、默认颜色、悬停颜色。
120
+
114
121
  输入为空或只有附件时,按钮会变灰、不可点;「重新生成」在空草稿时同样置灰并提示先输入文字;超长、含命令或文件引用的草稿会被本地拒绝。输出达到 token 上限时,结果卡显示红色截断警告;草稿改动后该警告隐藏,只保留草稿变更提示。
115
122
 
116
123
  ### 快捷设置卡
117
124
 
118
125
  右键或长按工具栏的增强按钮,会打开一个快捷设置卡:推理等级点进去选档位,上下文感知可直接开关;模型行只读,显示固定的 provider/model 或「跟随会话」。这个卡片只修改设置,不会触发增强;完整设置仍在设置页。
119
126
 
127
+ ![右键或长按按钮打开的快捷设置卡](./docs/images/quick-settings.png)
128
+
120
129
  `/enhance <文本>`:从斜杠面板改写任意文本,结果可复制,不进入模型历史。
121
130
 
122
131
  结果卡页脚的 ⓘ 显示本次增强实际引用的对话消息条数与字符数,例如 `3/8 条 · 3,140/4,000 字(79%)`:8 来自 `contextMaxMessages`,百分比按 `contextMaxChars` 计算;DSH 注入的环境/技能/时间等消息不计入。
@@ -136,7 +145,7 @@ dsh plugin --profile <profile> remove @surfishs/dsh-prompt-enhance
136
145
  | `contextAware` | `true` | 读取最近几条消息以消解指代;含敏感内容的会话建议关闭 |
137
146
  | `streaming` | `true` | 边写边显示;仅影响显示节奏,最终结果不变 |
138
147
  | `systemPrompt` | 空 | 自定义系统提示词;留空使用内置增强策略 |
139
- | `shortcut` | 空(禁用) | 触发增强的快捷键;例如 `ctrl+alt+shift+e`。须包含 ctrl/alt/meta 中至少一个修饰键,shift 可选;设置页可点「录制」直接按键录入 |
148
+ | `shortcut` | 空(禁用) | 触发增强的快捷键;例如 `ctrl+alt+shift+e`。须包含 ctrl/alt/meta 中至少一个修饰键,shift 可选;F13–F24 这类物理键盘上通常没有、由宏键盘或改键工具发出的键可单独使用(无修饰键);设置页可点「录制」直接按键录入 |
140
149
 
141
150
  ### 高级设置
142
151
 
@@ -157,8 +166,10 @@ dsh plugin --profile <profile> remove @surfishs/dsh-prompt-enhance
157
166
 
158
167
  ## 安全
159
168
 
160
- - **仅本机可访问**:增强路由由本机 DSH 宿主提供,只接受来自本机的请求(连接来自回环地址,且 `Host` / `Origin` 指向本机),外部来源一律拒绝。
161
- - **不要经反向代理暴露**:代理后的外部请求会以回环地址到达,`Host` / `Origin` 校验随之失效;请只在本机直连使用,不要把本插件的路由放在反向代理后面。
169
+ - **仅本机可访问**:增强路由由本机 DSH 宿主提供,信任判断分两层。宿主提供连接校验(`ctx.connection.requestRejection`)时以宿主为准:`Host` 必须是回环或可信主机、`Sec-Fetch-Site` 非 cross-site、`Origin` 与 `Host` 同源,并校验浏览器会话;返回 401/403 即拒绝。宿主放行后,插件只再做结构性检查(socket 回环、`Host` 允许列表、拒绝 `X-Forwarded-For` / `Forwarded`),不再自行判断 `Origin` / `Sec-Fetch-*`——桌面版(Electron 桥接)的请求不带这些头,判断由宿主完成。宿主不提供该服务时退回插件栅栏并记录一次警告,此时按完整规则默认拒绝:缺 `Host`、`Sec-Fetch-Site: cross-site`、非 GET 请求缺 `Origin` 均被拒。宿主提供连接校验时,本机脚本直接调用需要 DSH 会话,否则会收到 401;调试请用 DSH 界面里发起的请求。
170
+ - **只接受 JSON 请求体**:缺 `Content-Type` 或不是 `application/json`(含 `+json` 后缀)的请求返回 415,挡掉表单式的简单跨站请求。
171
+ - **不要经反向代理暴露**:代理会改写 `Host`、可能丢弃 `Origin`,插件看到的就不再是访客的真实来源;带 `X-Forwarded-For` / `Forwarded` 的请求会被直接拒绝,但这不足以让反代场景变安全。请只在本机直连使用,不要把本插件的路由放在反向代理后面。
172
+ - **远程访问下不可用**:当 GUI 不是从本机地址(`127.0.0.1` / `localhost`)打开,而是经由远程访问或中继地址打开时,点增强会失败并返回 `403 forbidden: loopback-only`。这类请求会带上 `X-Forwarded-For`,`Host` 也是非回环域名,而插件的结构性检查不区分转发来源,一律拒绝。这是有意的安全边界,不是故障;请在本机浏览器中使用增强。
162
173
  - **提示注入边界**:草稿被包在 `<raw_prompt>` 标签里,草稿内出现的闭合标签会被中和;这能降低标签逃逸风险,但提示词层面的边界是**最佳努力,不是硬保证**。
163
174
  - **不保存模型密钥**:插件不读取或存储模型 API key,调用复用 DSH 已有的模型配置与凭据。
164
175
  - **上下文仅在开启时发送**:`contextAware` 开启时,最近几条 user/assistant 消息和当前草稿会发给当前配置的模型服务商(默认最多 8 条消息、4000 字符);系统消息、模型推理内容、工具结果和附件不会进入上下文;增强器只会知道草稿附带的附件数量(用于避免误称“没有附件”),不会收到附件内容;它可以在任务描述里写「见附件」这类指代,但不会据此描述或推断其内容;只统计你本人发送的消息与助手回复,DSH 注入的环境/技能/时间等上下文不会计入,也不会消耗这段预算。关闭后只发送草稿。
@@ -184,7 +195,7 @@ flowchart LR
184
195
  - **宿主端(Host half)**:声明设置项,在宿主 Web 服务挂 `POST /dsh-prompt-enhance/enhance`(流式 `/dsh-prompt-enhance/enhance-stream`),注册 `/enhance` 命令,并通过 `ctx.llm.stream` 调模型;路由按「设置里的 `provider`/`model` → 当前会话记录的模型 → harness 默认模型」解析。
185
196
  - **浏览器端(Browser half)**:输入框按钮、右键快捷设置卡、停靠结果卡、撤销条与快捷键都在这一半,它同时把设置镜像到界面。
186
197
  - 点按钮后先在**本地拦截**:空输入、仅附件、超长、含命令或文件引用的草稿直接拒绝,不发请求。
187
- - 通过后请求宿主路由;宿主再校验一次并调用模型,浏览器优先走 SSE,不支持时回退一次性 JSON。
198
+ - 通过后请求宿主路由;路由入口先过信任闸门(宿主连接校验,宿主缺少该服务时由插件 fence 兜底,见「安全」),再对入参做一次服务端校验并调用模型;浏览器优先走 SSE,不支持时回退一次性 JSON。
188
199
  - 模型输出经**归一化**(去代码围栏、裁剪空白、空结果拒绝)后进入**停靠结果卡**。
189
200
  - **回填**把结果写回输入框,并把原文压入撤销栈;点「撤销」恢复原文。
190
201
  - `/enhance` 走命令通道,结果只出现在命令面板、不进入会话历史。
@@ -257,6 +268,7 @@ flowchart LR
257
268
  | 提示「每分钟最多 N 次增强」 | 达到 `rateLimitPerMinute` | 按提示等待,或在设置中调大该值 |
258
269
  | 提示增强超时,或增强被取消 | 超过 `timeoutMs`,或你点了「取消」 | 超时可重试或调大 `timeoutMs`;取消不会改动原输入,重新点增强即可 |
259
270
  | 提示鉴权失败 | 对应 provider 的凭据或 API key 无效 | 检查该 provider 在 DSH 中的凭据/API key 是否有效 |
271
+ | 手机或另一台设备上,增强不可用 | 页面不是从本机打开,而是经远程访问 / 中继转发进来;插件只接受本机请求 | 改在本机浏览器里使用增强 |
260
272
  | 结果被截断 | 达到 `maxOutputTokens` | 查看截断提示,并在设置中调高 `maxOutputTokens` |
261
273
  | 上下文用量显示 `8/8 条`(默认值) | 达到 `contextMaxMessages` 的消息条数上限(默认 8);这是正常截断,不显示警告 | 一般无需处理;需要更早的对话时调大 `contextMaxMessages`,但可能先撞上 `contextMaxChars`(默认 4,000 字) |
262
274
  | 上下文用量显示 `4,000/4,000 字`(默认值,琥珀色) | 达到 `contextMaxChars` 的字数上限(默认 4,000),较早内容未发送;最新一条被截头时原因行会写「最新一条仅发送了开头」 | 调大 `contextMaxChars`,或精简对话/减少引用;上下文越多,输入 token 和费用越高 |
Binary file
Binary file
package/lib/client.js CHANGED
@@ -2101,7 +2101,7 @@ var zh = {
2101
2101
  "settings.row.rateLimitPerMinute.label": "\u6BCF\u5206\u949F\u8C03\u7528\u4E0A\u9650",
2102
2102
  "settings.row.rateLimitPerMinute.hint": "\u6BCF\u5206\u949F\u589E\u5F3A\u6B21\u6570\u4E0A\u9650\uFF08\u6ED1\u52A8\u7A97\u53E3\uFF09\uFF1B\u8D85\u9650\u65F6\u63D0\u793A\u7A0D\u540E\u518D\u8BD5\u3002\u9ED8\u8BA4 10\u3002",
2103
2103
  "settings.row.shortcut.label": "\u5FEB\u6377\u952E",
2104
- "settings.row.shortcut.hint": "\u9ED8\u8BA4\u7559\u7A7A\uFF08\u7981\u7528\uFF09\uFF1B\u586B\u5199\u540E\u751F\u6548\uFF0C\u4F8B\uFF1Actrl+alt+shift+e\uFF08\u987B\u5305\u542B ctrl/alt/meta \u4E2D\u81F3\u5C11\u4E00\u4E2A\u4FEE\u9970\u952E\uFF09\u3002",
2104
+ "settings.row.shortcut.hint": "\u9ED8\u8BA4\u7559\u7A7A\uFF08\u7981\u7528\uFF09\uFF1B\u586B\u5199\u540E\u751F\u6548\uFF0C\u4F8B\uFF1Actrl+alt+shift+e\uFF08\u987B\u5305\u542B ctrl/alt/meta \u4E2D\u81F3\u5C11\u4E00\u4E2A\u4FEE\u9970\u952E\uFF1BF13\u2013F24 \u53EF\u5355\u72EC\u4F7F\u7528\uFF09\u3002",
2105
2105
  "settings.row.shortcut.placeholder": "\u4F8B\uFF1Actrl+alt+shift+e",
2106
2106
  "settings.strategy.replace": "\u66FF\u6362\u5185\u7F6E\u7B56\u7565",
2107
2107
  "settings.strategy.extend": "\u8FFD\u52A0\u5728\u5185\u7F6E\u7B56\u7565\u540E",
@@ -2283,7 +2283,7 @@ var en = {
2283
2283
  "settings.row.rateLimitPerMinute.label": "Calls per minute",
2284
2284
  "settings.row.rateLimitPerMinute.hint": "Successful enhancements per minute (sliding window); past the limit the request is told to retry later. Default 10.",
2285
2285
  "settings.row.shortcut.label": "Shortcut",
2286
- "settings.row.shortcut.hint": "Empty by default (disabled); set it to enable, e.g. ctrl+alt+shift+e (needs at least one of ctrl/alt/meta).",
2286
+ "settings.row.shortcut.hint": "Empty by default (disabled); set it to enable, e.g. ctrl+alt+shift+e (needs at least one of ctrl/alt/meta; F13\u2013F24 may be used alone).",
2287
2287
  "settings.row.shortcut.placeholder": "e.g. ctrl+alt+shift+e",
2288
2288
  "settings.strategy.replace": "Replace the built-in strategy",
2289
2289
  "settings.strategy.extend": "Append after the built-in strategy",
@@ -3133,7 +3133,9 @@ var MODIFIER_ALIASES = /* @__PURE__ */ new Map([
3133
3133
  ["win", "meta"],
3134
3134
  ["super", "meta"]
3135
3135
  ]);
3136
- var isKeyToken = (token) => /^[a-z0-9]$/.test(token) || /^f([1-9]|1[0-2])$/.test(token);
3136
+ var FUNCTION_KEY_TOKEN = /^f([1-9]|1[0-9]|2[0-4])$/;
3137
+ var isKeyToken = (token) => /^[a-z0-9]$/.test(token) || FUNCTION_KEY_TOKEN.test(token);
3138
+ var isBareFunctionKey = (token) => /^f(1[3-9]|2[0-4])$/.test(token);
3137
3139
  function codeFor(token) {
3138
3140
  if (/^[a-z]$/.test(token)) return `Key${token.toUpperCase()}`;
3139
3141
  if (/^[0-9]$/.test(token)) return `Digit${token}`;
@@ -3151,7 +3153,7 @@ function parseShortcut(spec) {
3151
3153
  if (modifier === void 0) return null;
3152
3154
  modifiers[modifier] = true;
3153
3155
  }
3154
- if (!modifiers.ctrl && !modifiers.alt && !modifiers.meta) return null;
3156
+ if (!modifiers.ctrl && !modifiers.alt && !modifiers.meta && !isBareFunctionKey(last)) return null;
3155
3157
  return { ...modifiers, key: last, code: codeFor(last) };
3156
3158
  }
3157
3159
  function matchesShortcut(event, combo) {
@@ -3163,15 +3165,15 @@ function keyTokenOf(event) {
3163
3165
  if (letter !== void 0) return letter.toLowerCase();
3164
3166
  const digit = /^Digit([0-9])$/.exec(event.code)?.[1];
3165
3167
  if (digit !== void 0) return digit;
3166
- const fn = /^F([1-9]|1[0-2])$/.exec(event.code)?.[1];
3168
+ const fn = /^F([1-9]|1[0-9]|2[0-4])$/.exec(event.code)?.[1];
3167
3169
  if (fn !== void 0) return `f${fn}`;
3168
3170
  const byKey = event.key.toLowerCase();
3169
3171
  return isKeyToken(byKey) ? byKey : null;
3170
3172
  }
3171
3173
  function shortcutSpecOf(event) {
3172
- if (!event.ctrlKey && !event.altKey && !event.metaKey) return null;
3173
3174
  const token = keyTokenOf(event);
3174
3175
  if (token === null) return null;
3176
+ if (!event.ctrlKey && !event.altKey && !event.metaKey && !isBareFunctionKey(token)) return null;
3175
3177
  const parts = [];
3176
3178
  if (event.ctrlKey) parts.push("ctrl");
3177
3179
  if (event.altKey) parts.push("alt");
@@ -3683,7 +3685,7 @@ var REPOSITORY_URL = "https://github.com/surfishs/dsh-prompt-enhance";
3683
3685
  var NPM_URL = "https://www.npmjs.com/package/@surfishs/dsh-prompt-enhance";
3684
3686
  var GITHUB_MARK = "M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.6 7.6 0 0 1 2-.27c.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0 0 16 8c0-4.42-3.58-8-8-8z";
3685
3687
  var NPM_MARK = "M1.763 0C.786 0 0 .786 0 1.763v20.474C0 23.214.786 24 1.763 24h20.474c.977 0 1.763-.786 1.763-1.763V1.763C24 .786 23.214 0 22.237 0zM5.13 5.323l13.837.019-.009 13.836h-3.464l.01-10.382h-3.456L12.04 19.17H5.113z";
3686
- var PLUGIN_VERSION = true ? "0.1.4" : "0.0.0";
3688
+ var PLUGIN_VERSION = true ? "0.1.6" : "0.0.0";
3687
3689
  var HEADER_STYLE = { display: "flex", flexDirection: "column", gap: 12, padding: "2px 0" };
3688
3690
  var HEADER_ROW_STYLE = { display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap" };
3689
3691
  var HEADER_TITLE_STYLE = {
package/lib/index.js CHANGED
@@ -140,7 +140,7 @@ var Config = z.object({
140
140
  timeoutMs: z.number().step(1).min(FIELD_LIMITS.timeoutMs.min).max(FIELD_LIMITS.timeoutMs.max).default(DEFAULT_CONFIG.timeoutMs).description("\u5355\u6B21\u589E\u5F3A\u7684\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09").volatile(),
141
141
  systemPrompt: z.string().role("textarea").default(DEFAULT_CONFIG.systemPrompt).description("\u7CFB\u7EDF\u63D0\u793A\u8BCD\uFF1B\u7559\u7A7A\u4F7F\u7528\u5185\u7F6E\u589E\u5F3A\u7B56\u7565").volatile(),
142
142
  strategyMode: z.union(["replace-default", "extend-default"]).default(DEFAULT_CONFIG.strategyMode).description("\u81EA\u5B9A\u4E49\u7CFB\u7EDF\u63D0\u793A\u8BCD\u7684\u7EC4\u5408\u65B9\u5F0F\uFF1A\u6574\u4F53\u66FF\u6362\u5185\u7F6E\u7B56\u7565\uFF08\u9ED8\u8BA4\uFF1B\u4EC5\u4FDD\u7559\u63D2\u4EF6\u59CB\u7EC8\u8FFD\u52A0\u7684\u6700\u5C0F\u8F93\u51FA\u5951\u7EA6\uFF1A\u8F93\u5165\u662F\u6570\u636E\u3001\u53EA\u8F93\u51FA\u6539\u5199\u7ED3\u679C\uFF09\uFF0C\u6216\u628A\u81EA\u5B9A\u4E49\u6587\u672C\u8FFD\u52A0\u5728\u5185\u7F6E\u7B56\u7565\u4E4B\u540E\uFF08\u5185\u7F6E\u89C4\u5219\u7EE7\u7EED\u751F\u6548\uFF09").volatile(),
143
- shortcut: z.string().default(DEFAULT_CONFIG.shortcut).description("\u89E6\u53D1\u5FEB\u6377\u952E\uFF0C\u9ED8\u8BA4\u7559\u7A7A\u5373\u7981\u7528\uFF1B\u5982\u9700\u542F\u7528\uFF0C\u586B\u5982 ctrl+alt+e\uFF08\u987B\u5305\u542B ctrl/alt/meta \u4E2D\u81F3\u5C11\u4E00\u4E2A\u4FEE\u9970\u952E\uFF0Cshift \u4EC5\u53EF\u4F5C\u9644\u52A0\uFF0C\u7EAF\u5B57\u6BCD/\u6570\u5B57\u6216 shift+\u5B57\u6BCD\u4F1A\u88AB\u5FFD\u7565\uFF09").volatile(),
143
+ shortcut: z.string().default(DEFAULT_CONFIG.shortcut).description("\u89E6\u53D1\u5FEB\u6377\u952E\uFF0C\u9ED8\u8BA4\u7559\u7A7A\u5373\u7981\u7528\uFF1B\u5982\u9700\u542F\u7528\uFF0C\u586B\u5982 ctrl+alt+e\uFF08\u987B\u5305\u542B ctrl/alt/meta \u4E2D\u81F3\u5C11\u4E00\u4E2A\u4FEE\u9970\u952E\uFF0Cshift \u4EC5\u53EF\u4F5C\u9644\u52A0\uFF0C\u7EAF\u5B57\u6BCD/\u6570\u5B57\u6216 shift+\u5B57\u6BCD\u4F1A\u88AB\u5FFD\u7565\uFF1BF13\u2013F24 \u53EF\u5355\u72EC\u4F7F\u7528\uFF09").volatile(),
144
144
  maxConcurrent: z.number().step(1).min(FIELD_LIMITS.maxConcurrent.min).max(FIELD_LIMITS.maxConcurrent.max).default(DEFAULT_CONFIG.maxConcurrent).description("\u5BBF\u4E3B\u4FA7\u5E76\u53D1\u4E0A\u9650\uFF1A\u540C\u65F6\u8FDB\u884C\u7684\u589E\u5F3A\u8C03\u7528\u6570\uFF0C\u8D85\u51FA\u7684\u8BF7\u6C42\u8FD4\u56DE 429").volatile(),
145
145
  rateLimitPerMinute: z.number().step(1).min(FIELD_LIMITS.rateLimitPerMinute.min).max(FIELD_LIMITS.rateLimitPerMinute.max).default(DEFAULT_CONFIG.rateLimitPerMinute).description("\u6BCF\u5206\u949F\u589E\u5F3A\u6B21\u6570\u4E0A\u9650\uFF08\u6ED1\u52A8\u7A97\u53E3\uFF09\uFF0C\u8D85\u51FA\u8FD4\u56DE 429").volatile(),
146
146
  streaming: z.boolean().default(DEFAULT_CONFIG.streaming).description("\u589E\u91CF\u5C55\u793A\uFF1A\u6A21\u578B\u8FB9\u5199\u8FB9\u5728\u9762\u677F\u663E\u793A\uFF08\u4EC5\u5F71\u54CD\u663E\u793A\u8282\u594F\uFF0C\u6700\u7EC8\u7ED3\u679C\u4E0D\u53D8\uFF1B\u5BBF\u4E3B\u6216\u7F51\u7EDC\u4E0D\u652F\u6301\u65F6\u81EA\u52A8\u56DE\u9000\u4E3A\u4E00\u6B21\u6027\u8FD4\u56DE\uFF09").volatile(),
@@ -1247,15 +1247,39 @@ function isTrustedOrigin(origin, expectedPort) {
1247
1247
  return false;
1248
1248
  }
1249
1249
  }
1250
- function isTrustedRequest(req) {
1250
+ var MUTATING_METHODS = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
1251
+ function isTrustedTransport(req) {
1251
1252
  if (req.headers["x-forwarded-for"] !== void 0 || req.headers.forwarded !== void 0) return false;
1252
1253
  if (!isLoopbackRequest(req)) return false;
1253
1254
  const host = req.headers.host;
1254
- if (typeof host !== "string" || host.trim() === "") return true;
1255
- if (!TRUSTED_HOSTNAMES.has(hostnameOf(host))) return false;
1255
+ if (typeof host !== "string" || host.trim() === "") return false;
1256
+ return TRUSTED_HOSTNAMES.has(hostnameOf(host));
1257
+ }
1258
+ function isTrustedRequest(req) {
1259
+ if (!isTrustedTransport(req)) return false;
1260
+ const site = req.headers["sec-fetch-site"];
1261
+ if (typeof site === "string" && site.trim().toLowerCase() === "cross-site") return false;
1262
+ const host = req.headers.host;
1256
1263
  const origin = req.headers.origin;
1257
- if (typeof origin === "string" && origin.trim() !== "" && !isTrustedOrigin(origin, portOf(host))) return false;
1258
- return true;
1264
+ const method = (req.method ?? "GET").toUpperCase();
1265
+ if (typeof origin !== "string" || origin.trim() === "") return !MUTATING_METHODS.has(method);
1266
+ return isTrustedOrigin(origin, portOf(host));
1267
+ }
1268
+
1269
+ // src/trust.ts
1270
+ var warnedFallback = false;
1271
+ function trustRejection(ctx, req) {
1272
+ const connection = ctx.get("connection");
1273
+ if (connection !== void 0 && connection !== null && typeof connection.requestRejection === "function") {
1274
+ const rejection = connection.requestRejection({ headers: req.headers });
1275
+ if (rejection !== void 0) return rejection;
1276
+ return isTrustedTransport(req) ? void 0 : 403;
1277
+ }
1278
+ if (!warnedFallback) {
1279
+ warnedFallback = true;
1280
+ console.warn("[dsh-prompt-enhance] ctx.connection is unavailable; route trust falls back to the loopback fence");
1281
+ }
1282
+ return isTrustedRequest(req) ? void 0 : 403;
1259
1283
  }
1260
1284
 
1261
1285
  // src/http.ts
@@ -1267,7 +1291,19 @@ var BodyTooLargeError = class extends Error {
1267
1291
  this.name = "BodyTooLargeError";
1268
1292
  }
1269
1293
  };
1294
+ var UnsupportedMediaTypeError = class extends Error {
1295
+ /** Stable discriminant for `instanceof`-free checks across realms. */
1296
+ code = "unsupported-media-type";
1297
+ constructor() {
1298
+ super("unsupported media type");
1299
+ this.name = "UnsupportedMediaTypeError";
1300
+ }
1301
+ };
1270
1302
  async function readBoundedJson(req, maxBytes) {
1303
+ const contentType = req.headers["content-type"];
1304
+ if (typeof contentType !== "string" || !/^application\/(?:[a-z0-9!#$&^_.+-]+\+)?json\b/i.test(contentType.trim())) {
1305
+ throw new UnsupportedMediaTypeError();
1306
+ }
1271
1307
  const chunks = [];
1272
1308
  let size = 0;
1273
1309
  for await (const chunk of req) {
@@ -1380,8 +1416,12 @@ function queryParam(url, name2) {
1380
1416
  return value === void 0 || value === "" ? void 0 : value;
1381
1417
  }
1382
1418
  async function serveReasoningEfforts(ctx, req, res) {
1383
- if (!isTrustedRequest(req)) {
1384
- writeJson(res, 403, { ok: false, error: { code: "internal", message: "forbidden: loopback-only" } });
1419
+ const rejection = trustRejection(ctx, req);
1420
+ if (rejection !== void 0) {
1421
+ writeJson(res, rejection, {
1422
+ ok: false,
1423
+ error: { code: "internal", message: rejection === 401 ? "unauthorized" : "forbidden: loopback-only" }
1424
+ });
1385
1425
  return;
1386
1426
  }
1387
1427
  const url = new URL(req.url ?? "/", "http://x");
@@ -1489,8 +1529,12 @@ function requiredParam(url, name2) {
1489
1529
  return value === void 0 || value === "" ? void 0 : value;
1490
1530
  }
1491
1531
  async function serveCatalog(ctx, req, res, pathname) {
1492
- if (!isTrustedRequest(req)) {
1493
- writeJson(res, 403, { ok: false, error: { code: "internal", message: "forbidden: loopback-only" } });
1532
+ const rejection = trustRejection(ctx, req);
1533
+ if (rejection !== void 0) {
1534
+ writeJson(res, rejection, {
1535
+ ok: false,
1536
+ error: { code: "internal", message: rejection === 401 ? "unauthorized" : "forbidden: loopback-only" }
1537
+ });
1494
1538
  return;
1495
1539
  }
1496
1540
  if (req.method !== "GET") {
@@ -1534,8 +1578,12 @@ function attachmentCountOf(value) {
1534
1578
  return typeof count === "number" && Number.isSafeInteger(count) && count >= 0 && count <= MAX_ATTACHMENT_COUNT ? count : void 0;
1535
1579
  }
1536
1580
  async function serveEnhance(ctx, readConfig, gate, pathname, req, res, stream) {
1537
- if (!isTrustedRequest(req)) {
1538
- writeJson(res, 403, { ok: false, error: { code: "internal", message: "forbidden: loopback-only" } });
1581
+ const rejection = trustRejection(ctx, req);
1582
+ if (rejection !== void 0) {
1583
+ writeJson(res, rejection, {
1584
+ ok: false,
1585
+ error: { code: "internal", message: rejection === 401 ? "unauthorized" : "forbidden: loopback-only" }
1586
+ });
1539
1587
  return;
1540
1588
  }
1541
1589
  if (pathname !== ENHANCE_ENDPOINT && pathname !== ENHANCE_STREAM_ENDPOINT) {
@@ -1546,11 +1594,6 @@ async function serveEnhance(ctx, readConfig, gate, pathname, req, res, stream) {
1546
1594
  writeJson(res, 405, { ok: false, error: { code: "internal", message: "only POST is allowed" } });
1547
1595
  return;
1548
1596
  }
1549
- const contentType = req.headers["content-type"];
1550
- if (contentType !== void 0 && !String(contentType).toLowerCase().startsWith("application/json")) {
1551
- writeJson(res, 415, { ok: false, error: { code: "rejected", message: "\u4EC5\u652F\u6301 application/json \u8BF7\u6C42\u4F53\u3002" } });
1552
- return;
1553
- }
1554
1597
  let config;
1555
1598
  try {
1556
1599
  config = readConfig();
@@ -1574,10 +1617,11 @@ async function serveEnhance(ctx, readConfig, gate, pathname, req, res, stream) {
1574
1617
  body = await readBoundedJson(req, cap);
1575
1618
  } catch (error) {
1576
1619
  const tooLarge = error instanceof BodyTooLargeError;
1620
+ const badType = error instanceof UnsupportedMediaTypeError;
1577
1621
  if (tooLarge) res.setHeader("connection", "close");
1578
- writeJson(res, tooLarge ? 413 : 422, {
1622
+ writeJson(res, badType ? 415 : tooLarge ? 413 : 422, {
1579
1623
  ok: false,
1580
- error: { code: "rejected", message: tooLarge ? "\u8BF7\u6C42\u4F53\u8D85\u8FC7\u5927\u5C0F\u4E0A\u9650\u3002" : "\u8BF7\u6C42\u4F53\u4E0D\u662F\u6709\u6548\u7684 JSON\u3002" }
1624
+ error: { code: "rejected", message: badType ? "\u4EC5\u652F\u6301 application/json \u8BF7\u6C42\u4F53\u3002" : tooLarge ? "\u8BF7\u6C42\u4F53\u8D85\u8FC7\u5927\u5C0F\u4E0A\u9650\u3002" : "\u8BF7\u6C42\u4F53\u4E0D\u662F\u6709\u6548\u7684 JSON\u3002" }
1581
1625
  });
1582
1626
  return;
1583
1627
  }