@infinity_cheng/dsh-prompt-editor 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -15
- package/client.js +608 -0
- package/index.js +126 -10
- package/locale/en.json +39 -0
- package/locale/zh.json +39 -0
- package/package.json +30 -4
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
**把一句“帮我搞一下”,改写成执行者不用追问的任务说明。**
|
|
8
8
|
|
|
9
|
-
`dsh-prompt-editor` 是 [DeepSeek Harness](https://www.deepseek.com/harness/)
|
|
9
|
+
`dsh-prompt-editor` 是 [DeepSeek Harness](https://www.deepseek.com/harness/) 的宿主插件。它在输入框旁放一个「增强」按钮:把草稿改写成可执行的 prompt,先流式预览,确认后才替换;也注册同名 `enhance_prompt` 工具供 Agent 调用。
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
它只改草稿——**不发送、不执行**。草稿已经清楚时返回 `UNCHANGED`;检查不通过时保留原文。
|
|
12
12
|
|
|
13
|
-
>
|
|
13
|
+
> **Phase 1 已完成**:输入框按钮、选区感知、SSE 流式预览、替换/重试/取消、设置页。没有自动发送,也没有自动应用。
|
|
14
14
|
|
|
15
15
|
## 为什么不是“提示词扩写器”
|
|
16
16
|
|
|
@@ -38,7 +38,19 @@ dsh plugin --profile web add @infinity_cheng/dsh-prompt-editor
|
|
|
38
38
|
dsh web
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
##
|
|
41
|
+
## 输入框按钮(Phase 1)
|
|
42
|
+
|
|
43
|
+
1. 在输入框里输入草稿,或选中其中一段。
|
|
44
|
+
2. 点输入框右侧的「增强」。
|
|
45
|
+
3. 预览弹窗会流式显示改写结果;左侧是原文,右侧是改写,下面是确定性检查。
|
|
46
|
+
4. 选 `替换选中` / `替换全文` 才写回草稿;`重试` 可换规则重跑,`取消` 不改任何内容。
|
|
47
|
+
5. 有选区时只替换选区;没有选区时替换全文。替换后仍可用输入框自己的 `Ctrl+Z` 撤销。
|
|
48
|
+
|
|
49
|
+
设置入口:**设置 → 提示词增强**。默认规则保存在本浏览器(`localStorage`),profile 的 `defaultMode` 仍是兜底默认值。
|
|
50
|
+
|
|
51
|
+
按钮在以下情况禁用:草稿为空、正在发送/执行、草稿含 `@文件`/链接等引用块(Phase 1 暂不支持替换含引用块的草稿)。
|
|
52
|
+
|
|
53
|
+
## 工具调用
|
|
42
54
|
|
|
43
55
|
在会话里说:
|
|
44
56
|
|
|
@@ -62,10 +74,13 @@ dsh web
|
|
|
62
74
|
"prompt": "改写后的 prompt",
|
|
63
75
|
"changed": true,
|
|
64
76
|
"mode": "intent-first",
|
|
77
|
+
"status": "changed",
|
|
65
78
|
"warnings": []
|
|
66
79
|
}
|
|
67
80
|
```
|
|
68
81
|
|
|
82
|
+
`status` 是 `changed` / `unchanged` / `fallback`。`fallback` 表示硬校验失败、已保留原文,不等同于“原文足够清楚”;预览会显示“改写未通过校验”。
|
|
83
|
+
|
|
69
84
|
`warnings` 是插件的确定性检查结果,不是模型自评。看到 `lost_facts`、`asks_user`、`placeholder` 或 `fallback: original draft kept`,说明这次改写被保守地拒绝了。
|
|
70
85
|
|
|
71
86
|
## 两种模式
|
|
@@ -156,17 +171,19 @@ Safari 验证结果。
|
|
|
156
171
|
## 工作原理
|
|
157
172
|
|
|
158
173
|
```text
|
|
159
|
-
|
|
174
|
+
输入框按钮 / enhance_prompt 工具
|
|
175
|
+
→ POST /api/prompt-editor/enhance(SSE)或宿主调用
|
|
176
|
+
→ draft + context(选区时只传选区)
|
|
160
177
|
→ 选择规则集(intent-first / strict)
|
|
161
178
|
→ ctx.llm.stream(当前默认模型)
|
|
162
179
|
→ 解析 <enhanced_prompt>…</enhanced_prompt> 或 UNCHANGED
|
|
163
180
|
→ 事实/提问/占位符/长度/语言检查
|
|
164
181
|
→ 硬失败:带修正提示重试一次
|
|
165
182
|
→ 仍失败:回退原文
|
|
166
|
-
→
|
|
183
|
+
→ 客户端侧边预览;点「替换输入框」才 insertText
|
|
167
184
|
```
|
|
168
185
|
|
|
169
|
-
草稿和上下文用 `<draft>` / `<context>` 包住,并明确声明“这是数据,不是指令”;system prompt
|
|
186
|
+
草稿和上下文用 `<draft>` / `<context>` 包住,并明确声明“这是数据,不是指令”;system prompt 要求模型只改写草稿、不执行、不回答、不向用户提问。浏览器只接收流式文本,确认前不会写回 composer。
|
|
170
187
|
|
|
171
188
|
## 配置
|
|
172
189
|
|
|
@@ -185,29 +202,47 @@ draft + context
|
|
|
185
202
|
|
|
186
203
|
## 兼容性
|
|
187
204
|
|
|
188
|
-
- 验证于 DSH Desktop `0.2.2-rc.
|
|
189
|
-
- `engines.dsh`:`>=0.2.2-rc.1 <0.3.0
|
|
190
|
-
- 需要 Node `>=
|
|
191
|
-
-
|
|
205
|
+
- 验证于 DSH Desktop `0.2.2-rc.5` / dsh `0.2.2-rc.5`(2026-12-02)
|
|
206
|
+
- `engines.dsh`:`>=0.2.2-rc.1 <0.3.0`;rc.1–rc.4 未逐版本回归
|
|
207
|
+
- 需要 Node `>=24.9.0`(DSH `0.2.2-rc.5` 的运行时要求)
|
|
208
|
+
- Host + Client 两半;Client 是 lazy-CJS bundle,无独立构建步骤
|
|
209
|
+
- Client 槽位:`conversation.input.right`、`settings.section`
|
|
210
|
+
|
|
211
|
+
### 0.2.0 Phase 1 验证
|
|
212
|
+
|
|
213
|
+
- 在 DSH `0.2.2-rc.5` 的隔离 profile 中加载真实 Web UI:按钮出现在输入框右侧,空草稿禁用,输入后启用。
|
|
214
|
+
- 用拦截的 SSE 响应验证:流式预览、警告显示、替换写回;`UNCHANGED` 时按钮变为「无需修改」,`fallback` 时按钮变为「改写未通过」并保留原文。
|
|
215
|
+
- 选区验证:只发送选中的字符,按钮显示「替换选中」,替换后只改选区,前后段落不变。
|
|
216
|
+
- 关闭预览后焦点回到「增强」按钮;Esc 可关闭,Tab 在弹窗内循环。
|
|
217
|
+
- 规则切换:`strict` 随请求体发送,预览显示 strict 结果。
|
|
218
|
+
- 设置页:出现在「设置 → 提示词增强」,切换默认规则写入 `localStorage`。
|
|
219
|
+
- 真实 Host 路由:无 API Key 时返回结构化 SSE error;有凭据的 Agent 工具链路已在 Desktop 验证。
|
|
192
220
|
|
|
193
221
|
## 限制
|
|
194
222
|
|
|
195
|
-
- **Phase 0 没有输入框按钮**:需要模型调用 `enhance_prompt`,或用户明确要求增强。
|
|
196
223
|
- **显式触发**:不会在每次发送时自动改写。
|
|
224
|
+
- **含引用块的草稿暂不支持**:`@文件`、链接芯片等会禁用按钮;Phase 2 再处理。
|
|
225
|
+
- **默认规则存在本浏览器**:设置页写 `localStorage`,不是 profile 配置;profile 的 `defaultMode` 仍是兜底。
|
|
226
|
+
- **没有快捷键**:Phase 1 只有按钮入口。
|
|
197
227
|
- **一次模型调用**:每次增强会消耗一次模型请求;硬失败时可能重试一次。
|
|
198
228
|
- **检查是启发式的**:事实提取覆盖 URL、代码、版本、范围、路径、`@引用`、附件标记;不覆盖所有自然语言事实。
|
|
199
|
-
-
|
|
229
|
+
- **不自动应用**:预览后必须点「替换输入框」。
|
|
200
230
|
- **没有提示词库/A-B**:那是另一个产品方向。
|
|
201
231
|
|
|
202
232
|
## Roadmap
|
|
203
233
|
|
|
204
|
-
- **Phase 1
|
|
234
|
+
- **Phase 1(已完成)**:输入框按钮 + 流式预览 + 选区感知 + 设置页
|
|
235
|
+
- **Phase 2**:快捷键、引用块草稿、模型选择、设置持久化、inline diff
|
|
205
236
|
- **0.1.x**:收紧 React 升级类任务的长度;让 strict 模式在保持简短的同时补上验收信息
|
|
206
237
|
- **后续**:规则集切换、A/B、团队词汇表、与提示词库插件联动
|
|
207
238
|
|
|
208
239
|
## 开发
|
|
209
240
|
|
|
210
241
|
```sh
|
|
242
|
+
# 语法检查
|
|
243
|
+
node --check index.js
|
|
244
|
+
node --check client.js
|
|
245
|
+
|
|
211
246
|
# 结构测试
|
|
212
247
|
node tests/run.mjs
|
|
213
248
|
|
|
@@ -218,7 +253,7 @@ node ~/.dsh/skills/dsh-plugin-development/scripts/check-plugin.mjs .
|
|
|
218
253
|
npm pack --dry-run --json
|
|
219
254
|
```
|
|
220
255
|
|
|
221
|
-
当前结构测试
|
|
256
|
+
当前结构测试 14 项通过:fixtures、规则契约、解析器、校验器、`UNCHANGED`、事实丢失回退、提问回退、占位符/附件回退、完整性提示、工具注册等。
|
|
222
257
|
|
|
223
258
|
## 许可证
|
|
224
259
|
|
package/client.js
ADDED
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@infinity_cheng/dsh-prompt-editor',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react')
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
const { useCallback, useEffect, useMemo, useRef, useState } = React
|
|
7
|
+
|
|
8
|
+
// react-dom is optional in the module table. Without it the dialog still
|
|
9
|
+
// renders in place; with it, the overlay escapes the composer's transform.
|
|
10
|
+
const createPortal = (() => {
|
|
11
|
+
try {
|
|
12
|
+
const dom = require('react-dom')
|
|
13
|
+
if (typeof dom?.createPortal === 'function') return dom.createPortal.bind(dom)
|
|
14
|
+
if (typeof dom?.default?.createPortal === 'function') return dom.default.createPortal.bind(dom.default)
|
|
15
|
+
} catch {
|
|
16
|
+
// No react-dom in this host.
|
|
17
|
+
}
|
|
18
|
+
return null
|
|
19
|
+
})()
|
|
20
|
+
|
|
21
|
+
const NS = 'prompt-editor'
|
|
22
|
+
const MODE_KEY = 'dsh-prompt-editor:mode'
|
|
23
|
+
const OPEN_TAG = '<enhanced_prompt>'
|
|
24
|
+
const CLOSE_TAG = '</enhanced_prompt>'
|
|
25
|
+
|
|
26
|
+
const FALLBACK = {
|
|
27
|
+
'meta.title': '提示词增强',
|
|
28
|
+
'meta.description': '在输入框旁改写草稿,预览后替换;不发送、不执行。',
|
|
29
|
+
'button.label': '增强',
|
|
30
|
+
'button.title': '改写草稿(不发送)',
|
|
31
|
+
'button.empty': '先输入草稿',
|
|
32
|
+
'button.busy': '正在发送或执行,暂时不能改写',
|
|
33
|
+
'button.references': '含引用块的草稿暂不支持',
|
|
34
|
+
'dialog.title': '提示词增强',
|
|
35
|
+
'dialog.original': '原文',
|
|
36
|
+
'dialog.enhanced': '改写结果',
|
|
37
|
+
'dialog.selection': '已选中 {count} 字符',
|
|
38
|
+
'dialog.whole': '全文',
|
|
39
|
+
'dialog.mode': '规则',
|
|
40
|
+
'dialog.cancel': '取消',
|
|
41
|
+
'dialog.retry': '重试',
|
|
42
|
+
'dialog.accept': '替换输入框',
|
|
43
|
+
'dialog.replaceSelection': '替换选中',
|
|
44
|
+
'dialog.replaceWhole': '替换全文',
|
|
45
|
+
'dialog.fallback': '改写未通过校验,已保留原文。',
|
|
46
|
+
'dialog.fallbackShort': '改写未通过',
|
|
47
|
+
'dialog.done': '改写完成({count} 条提示)',
|
|
48
|
+
'dialog.unchanged': '原文已足够清楚,不需要修改。',
|
|
49
|
+
'dialog.unchangedShort': '无需修改',
|
|
50
|
+
'dialog.loading': '正在改写…',
|
|
51
|
+
'dialog.failed': '未生成结果',
|
|
52
|
+
'dialog.error': '改写失败:{message}',
|
|
53
|
+
'dialog.stale': '草稿已变化,请重新打开。',
|
|
54
|
+
'dialog.warnings': '校验提示',
|
|
55
|
+
'dialog.close': '关闭',
|
|
56
|
+
'mode.intent-first': '意图优先',
|
|
57
|
+
'mode.strict': '最小改动',
|
|
58
|
+
'settings.nav': '提示词增强',
|
|
59
|
+
'settings.title': '提示词增强',
|
|
60
|
+
'settings.intro': '设置输入框按钮使用的默认规则。规则只影响改写,不会发送消息。',
|
|
61
|
+
'settings.mode': '默认规则',
|
|
62
|
+
'settings.modeHint': '意图优先会补全成任务说明;最小改动只消除歧义。',
|
|
63
|
+
'settings.profileDefault': 'Profile 默认:{mode}',
|
|
64
|
+
'settings.local': '保存在本浏览器;Profile 的 defaultMode 仍是兜底默认值。',
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function makeText(t) {
|
|
68
|
+
return (key, vars) => {
|
|
69
|
+
let value
|
|
70
|
+
try {
|
|
71
|
+
value = typeof t === 'function' ? t(key, vars) : undefined
|
|
72
|
+
} catch {
|
|
73
|
+
value = undefined
|
|
74
|
+
}
|
|
75
|
+
if (typeof value !== 'string' || value === '' || value === key) value = FALLBACK[key] ?? key
|
|
76
|
+
if (vars !== undefined) {
|
|
77
|
+
value = value.replace(/\{(\w+)\}/g, (match, name) => (
|
|
78
|
+
vars[name] === undefined ? match : String(vars[name])
|
|
79
|
+
))
|
|
80
|
+
}
|
|
81
|
+
return value
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function readMode() {
|
|
86
|
+
try {
|
|
87
|
+
return localStorage.getItem(MODE_KEY) === 'strict' ? 'strict' : 'intent-first'
|
|
88
|
+
} catch {
|
|
89
|
+
return 'intent-first'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function writeMode(mode) {
|
|
94
|
+
try {
|
|
95
|
+
localStorage.setItem(MODE_KEY, mode === 'strict' ? 'strict' : 'intent-first')
|
|
96
|
+
} catch {
|
|
97
|
+
// Storage may be disabled; the component still keeps its local state.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Show only the prompt body while the model is still emitting its tagged answer. */
|
|
102
|
+
function visibleText(raw) {
|
|
103
|
+
const text = String(raw ?? '')
|
|
104
|
+
if (/^\s*UNCHANGED\b/i.test(text)) return ''
|
|
105
|
+
const start = text.indexOf(OPEN_TAG)
|
|
106
|
+
if (start < 0) return ''
|
|
107
|
+
let body = text.slice(start + OPEN_TAG.length)
|
|
108
|
+
const end = body.indexOf(CLOSE_TAG)
|
|
109
|
+
if (end >= 0) body = body.slice(0, end)
|
|
110
|
+
return body.replace(/^\s+/, '')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function readSse(response, onEvent) {
|
|
114
|
+
const reader = response.body?.getReader()
|
|
115
|
+
if (!reader) throw new Error('response has no body')
|
|
116
|
+
const decoder = new TextDecoder()
|
|
117
|
+
let buffer = ''
|
|
118
|
+
try {
|
|
119
|
+
for (;;) {
|
|
120
|
+
const { done, value } = await reader.read()
|
|
121
|
+
if (done) break
|
|
122
|
+
buffer += decoder.decode(value, { stream: true })
|
|
123
|
+
let index
|
|
124
|
+
while ((index = buffer.indexOf('\n\n')) >= 0) {
|
|
125
|
+
const frame = buffer.slice(0, index)
|
|
126
|
+
buffer = buffer.slice(index + 2)
|
|
127
|
+
for (const line of frame.split('\n')) {
|
|
128
|
+
if (!line.startsWith('data:')) continue
|
|
129
|
+
const data = line.slice(5).trim()
|
|
130
|
+
if (data === '') continue
|
|
131
|
+
try {
|
|
132
|
+
onEvent(JSON.parse(data))
|
|
133
|
+
} catch {
|
|
134
|
+
// Ignore a malformed frame; the final done event is authoritative.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
} finally {
|
|
140
|
+
try {
|
|
141
|
+
reader.releaseLock()
|
|
142
|
+
} catch {
|
|
143
|
+
// Already released by an abort.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const CSS = `
|
|
149
|
+
.dsh-prompt-editor-trigger{display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 9px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-secondary);font:inherit;font-size:12px;line-height:1;white-space:nowrap;cursor:pointer;transition:background .15s,border-color .15s,color .15s}
|
|
150
|
+
.dsh-prompt-editor-trigger:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover);border-color:var(--dsw-alias-border-l2);color:var(--dsw-alias-label-primary)}
|
|
151
|
+
.dsh-prompt-editor-trigger:disabled{opacity:.45;cursor:default}
|
|
152
|
+
.dsh-prompt-editor-trigger svg{display:block;flex:0 0 auto}
|
|
153
|
+
.dsh-prompt-editor-overlay{position:fixed;inset:0;z-index:10000;display:flex;align-items:center;justify-content:center;padding:24px;background:rgba(0,0,0,.38)}
|
|
154
|
+
.dsh-prompt-editor-dialog{display:flex;flex-direction:column;width:min(920px,100%);max-height:min(760px,88vh);overflow:hidden;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);border:1px solid var(--dsw-alias-border-l1);border-radius:14px;box-shadow:0 18px 60px rgba(0,0,0,.28)}
|
|
155
|
+
.dsh-prompt-editor-head{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:14px 16px;border-bottom:1px solid var(--dsw-alias-border-l1)}
|
|
156
|
+
.dsh-prompt-editor-titleWrap{display:flex;align-items:center;gap:10px;min-width:0}
|
|
157
|
+
.dsh-prompt-editor-title{font-size:14px;line-height:20px;font-weight:600;color:var(--dsw-alias-label-primary)}
|
|
158
|
+
.dsh-prompt-editor-badge{padding:2px 7px;border:1px solid var(--dsw-alias-border-l1);border-radius:999px;color:var(--dsw-alias-label-secondary);font-size:11px;line-height:16px;white-space:nowrap}
|
|
159
|
+
.dsh-prompt-editor-headActions{display:flex;align-items:center;gap:8px;flex:0 0 auto}
|
|
160
|
+
.dsh-prompt-editor-modeLabel{display:flex;align-items:center;gap:5px;color:var(--dsw-alias-label-secondary);font-size:12px}
|
|
161
|
+
.dsh-prompt-editor-select{height:28px;padding:0 6px;border:1px solid var(--dsw-alias-border-l1);border-radius:7px;background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font:inherit;font-size:12px}
|
|
162
|
+
.dsh-prompt-editor-iconBtn{display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;padding:0;border:1px solid transparent;border-radius:7px;background:transparent;color:var(--dsw-alias-label-secondary);font:inherit;font-size:18px;line-height:1;cursor:pointer}
|
|
163
|
+
.dsh-prompt-editor-iconBtn:hover{background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary)}
|
|
164
|
+
.dsh-prompt-editor-body{display:grid;grid-template-columns:1fr 1fr;gap:12px;padding:14px 16px;overflow:auto;min-height:0}
|
|
165
|
+
.dsh-prompt-editor-pane{display:flex;flex-direction:column;gap:6px;min-width:0}
|
|
166
|
+
.dsh-prompt-editor-paneTitle{margin:0;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px;font-weight:500}
|
|
167
|
+
.dsh-prompt-editor-pre{margin:0;padding:10px 12px;min-height:96px;max-height:360px;overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere;background:var(--dsw-alias-bg-layer-1);border:1px solid var(--dsw-alias-border-l1);border-radius:10px;color:var(--dsw-alias-label-primary);font:12px/1.65 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
|
|
168
|
+
.dsh-prompt-editor-muted{color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px;margin:0}
|
|
169
|
+
.dsh-prompt-editor-loading{display:flex;align-items:center;gap:6px;min-height:96px;padding:10px 12px;border:1px dashed var(--dsw-alias-border-l1);border-radius:10px;color:var(--dsw-alias-label-secondary);font-size:12px}
|
|
170
|
+
.dsh-prompt-editor-dots::after{content:'…';display:inline-block;animation:dshPromptEditorPulse 1.2s steps(4,end) infinite}
|
|
171
|
+
@keyframes dshPromptEditorPulse{0%{opacity:.2}50%{opacity:1}100%{opacity:.2}}
|
|
172
|
+
.dsh-prompt-editor-warnings{margin:0 16px 12px;padding:10px 12px;border:1px solid var(--dsw-alias-border-l1);border-radius:10px;background:var(--dsw-alias-bg-layer-1)}
|
|
173
|
+
.dsh-prompt-editor-warningsTitle{margin:0 0 4px;color:var(--dsw-alias-label-secondary);font-size:12px;font-weight:500}
|
|
174
|
+
.dsh-prompt-editor-warnings ul{margin:0;padding-left:18px;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px}
|
|
175
|
+
.dsh-prompt-editor-error{margin:0 16px 12px;padding:8px 10px;border:1px solid var(--dsw-alias-state-error-primary);border-radius:8px;color:var(--dsw-alias-state-error-primary);font-size:12px;line-height:18px}
|
|
176
|
+
.dsh-prompt-editor-foot{display:flex;align-items:center;justify-content:space-between;gap:12px;padding:12px 16px;border-top:1px solid var(--dsw-alias-border-l1)}
|
|
177
|
+
.dsh-prompt-editor-footHint{color:var(--dsw-alias-label-secondary);font-size:11px;line-height:16px}
|
|
178
|
+
.dsh-prompt-editor-actions{display:flex;align-items:center;gap:8px;flex:0 0 auto}
|
|
179
|
+
.dsh-prompt-editor-btn{height:32px;padding:0 12px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;cursor:pointer}
|
|
180
|
+
.dsh-prompt-editor-btn:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover);border-color:var(--dsw-alias-border-l2)}
|
|
181
|
+
.dsh-prompt-editor-btn:disabled{opacity:.45;cursor:default}
|
|
182
|
+
.dsh-prompt-editor-btnPrimary{border-color:transparent;background:var(--dsw-alias-button-primary-fill,var(--dsw-alias-brand-primary-new-colorprimary-new-color,#2f6fed));color:var(--dsw-alias-label-on-color,#fff)}
|
|
183
|
+
.dsh-prompt-editor-btnPrimary:hover:not(:disabled){background:var(--dsw-alias-button-primary-fill,var(--dsw-alias-brand-primary-new-colorprimary-new-color,#2f6fed));filter:brightness(1.05)}
|
|
184
|
+
.dsh-prompt-editor-settings{display:flex;flex-direction:column;gap:10px;max-width:760px}
|
|
185
|
+
.dsh-prompt-editor-settings h2{margin:0;font-size:14px;line-height:22px;font-weight:600;color:var(--dsw-alias-label-primary)}
|
|
186
|
+
.dsh-prompt-editor-settings p{margin:0;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px}
|
|
187
|
+
.dsh-prompt-editor-settingsRow{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:12px 14px;border:1px solid var(--dsw-alias-border-l1);border-radius:10px;background:var(--dsw-alias-bg-layer-1)}
|
|
188
|
+
.dsh-prompt-editor-settingsCopy{display:flex;flex-direction:column;gap:2px;min-width:0}
|
|
189
|
+
.dsh-prompt-editor-settingsTitle{color:var(--dsw-alias-label-primary);font-size:13px;line-height:20px;font-weight:500}
|
|
190
|
+
.dsh-prompt-editor-settingsHint{color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px;margin:0}
|
|
191
|
+
@media (max-width:700px){.dsh-prompt-editor-overlay{padding:10px}.dsh-prompt-editor-body{grid-template-columns:1fr}.dsh-prompt-editor-head{align-items:flex-start;flex-direction:column}.dsh-prompt-editor-headActions{width:100%;justify-content:flex-end}.dsh-prompt-editor-foot{align-items:flex-start;flex-direction:column}.dsh-prompt-editor-actions{width:100%;justify-content:flex-end}}
|
|
192
|
+
`
|
|
193
|
+
|
|
194
|
+
const ICON = h('svg', {
|
|
195
|
+
width: 14,
|
|
196
|
+
height: 14,
|
|
197
|
+
viewBox: '0 0 24 24',
|
|
198
|
+
fill: 'none',
|
|
199
|
+
stroke: 'currentColor',
|
|
200
|
+
strokeWidth: 2,
|
|
201
|
+
strokeLinecap: 'round',
|
|
202
|
+
strokeLinejoin: 'round',
|
|
203
|
+
'aria-hidden': true,
|
|
204
|
+
},
|
|
205
|
+
h('path', { d: 'M4 20l4.2-1.1L20 7.1 16.9 4 5.1 15.8 4 20z' }),
|
|
206
|
+
h('path', { d: 'M14.5 6.5l3 3' }))
|
|
207
|
+
|
|
208
|
+
function PreviewDialog({ state, text, onClose, onAccept, onRetry, onMode }) {
|
|
209
|
+
const acceptRef = useRef(null)
|
|
210
|
+
const cancelRef = useRef(null)
|
|
211
|
+
const dialogRef = useRef(null)
|
|
212
|
+
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
const timer = setTimeout(() => {
|
|
215
|
+
if (state.loading) {
|
|
216
|
+
cancelRef.current?.focus?.()
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
const target = acceptRef.current !== null && acceptRef.current.disabled === false
|
|
220
|
+
? acceptRef.current
|
|
221
|
+
: cancelRef.current
|
|
222
|
+
target?.focus?.()
|
|
223
|
+
}, 30)
|
|
224
|
+
return () => clearTimeout(timer)
|
|
225
|
+
}, [state.loading, state.result])
|
|
226
|
+
|
|
227
|
+
const result = state.result
|
|
228
|
+
const enhanced = result?.prompt ?? visibleText(state.raw)
|
|
229
|
+
const fallback = result?.status === 'fallback'
|
|
230
|
+
const unchanged = result !== null && result.changed === false && !fallback
|
|
231
|
+
const canAccept = result !== null && result.changed === true && !fallback && state.loading !== true
|
|
232
|
+
const warningCount = result?.warnings?.length ?? 0
|
|
233
|
+
const statusText = state.loading
|
|
234
|
+
? text('dialog.loading')
|
|
235
|
+
: state.error !== null
|
|
236
|
+
? text('dialog.failed')
|
|
237
|
+
: fallback
|
|
238
|
+
? text('dialog.fallback')
|
|
239
|
+
: unchanged
|
|
240
|
+
? text('dialog.unchanged')
|
|
241
|
+
: result !== null
|
|
242
|
+
? text('dialog.done', { count: warningCount })
|
|
243
|
+
: ''
|
|
244
|
+
const acceptLabel = fallback
|
|
245
|
+
? text('dialog.fallbackShort')
|
|
246
|
+
: unchanged
|
|
247
|
+
? text('dialog.unchangedShort')
|
|
248
|
+
: state.selected
|
|
249
|
+
? text('dialog.replaceSelection')
|
|
250
|
+
: text('dialog.replaceWhole')
|
|
251
|
+
|
|
252
|
+
const dialog = h('div', {
|
|
253
|
+
className: 'dsh-prompt-editor-overlay',
|
|
254
|
+
role: 'presentation',
|
|
255
|
+
onMouseDown: (event) => {
|
|
256
|
+
if (event.target === event.currentTarget) onClose()
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
h('div', {
|
|
260
|
+
ref: dialogRef,
|
|
261
|
+
className: 'dsh-prompt-editor-dialog',
|
|
262
|
+
role: 'dialog',
|
|
263
|
+
'aria-modal': 'true',
|
|
264
|
+
'aria-label': text('dialog.title'),
|
|
265
|
+
'aria-busy': state.loading ? 'true' : 'false',
|
|
266
|
+
onMouseDown: (event) => event.stopPropagation(),
|
|
267
|
+
onKeyDown: (event) => {
|
|
268
|
+
if (event.key !== 'Tab' || dialogRef.current === null) return
|
|
269
|
+
const focusables = [...dialogRef.current.querySelectorAll('button:not([disabled]), select:not([disabled])')]
|
|
270
|
+
if (focusables.length === 0) return
|
|
271
|
+
const first = focusables[0]
|
|
272
|
+
const last = focusables[focusables.length - 1]
|
|
273
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
274
|
+
event.preventDefault()
|
|
275
|
+
last.focus()
|
|
276
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
277
|
+
event.preventDefault()
|
|
278
|
+
first.focus()
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
h('style', {}, CSS),
|
|
283
|
+
h('div', { className: 'dsh-prompt-editor-head' },
|
|
284
|
+
h('div', { className: 'dsh-prompt-editor-titleWrap' },
|
|
285
|
+
h('span', { className: 'dsh-prompt-editor-title' }, text('dialog.title')),
|
|
286
|
+
h('span', { className: 'dsh-prompt-editor-badge' },
|
|
287
|
+
state.selected
|
|
288
|
+
? text('dialog.selection', { count: state.source.length })
|
|
289
|
+
: text('dialog.whole'))),
|
|
290
|
+
h('div', { className: 'dsh-prompt-editor-headActions' },
|
|
291
|
+
h('label', { className: 'dsh-prompt-editor-modeLabel' },
|
|
292
|
+
text('dialog.mode'),
|
|
293
|
+
h('select', {
|
|
294
|
+
className: 'dsh-prompt-editor-select',
|
|
295
|
+
value: state.mode,
|
|
296
|
+
disabled: state.loading,
|
|
297
|
+
'aria-label': text('dialog.mode'),
|
|
298
|
+
onChange: (event) => onMode(event.target.value),
|
|
299
|
+
},
|
|
300
|
+
h('option', { value: 'intent-first' }, text('mode.intent-first')),
|
|
301
|
+
h('option', { value: 'strict' }, text('mode.strict')))),
|
|
302
|
+
h('button', {
|
|
303
|
+
type: 'button',
|
|
304
|
+
className: 'dsh-prompt-editor-iconBtn',
|
|
305
|
+
'aria-label': text('dialog.close'),
|
|
306
|
+
title: text('dialog.close'),
|
|
307
|
+
onClick: onClose,
|
|
308
|
+
}, '×'))),
|
|
309
|
+
h('div', { className: 'dsh-prompt-editor-body' },
|
|
310
|
+
h('section', { className: 'dsh-prompt-editor-pane' },
|
|
311
|
+
h('h3', { className: 'dsh-prompt-editor-paneTitle' }, text('dialog.original')),
|
|
312
|
+
h('pre', { className: 'dsh-prompt-editor-pre', tabIndex: 0 }, state.source)),
|
|
313
|
+
h('section', { className: 'dsh-prompt-editor-pane' },
|
|
314
|
+
h('h3', { className: 'dsh-prompt-editor-paneTitle' }, text('dialog.enhanced')),
|
|
315
|
+
state.loading && enhanced === ''
|
|
316
|
+
? h('div', { className: 'dsh-prompt-editor-loading' },
|
|
317
|
+
h('span', { className: 'dsh-prompt-editor-dots' }, text('dialog.loading')))
|
|
318
|
+
: h('pre', { className: 'dsh-prompt-editor-pre', tabIndex: 0 },
|
|
319
|
+
enhanced !== ''
|
|
320
|
+
? enhanced
|
|
321
|
+
: state.error !== null
|
|
322
|
+
? text('dialog.failed')
|
|
323
|
+
: (unchanged ? text('dialog.unchanged') : text('dialog.loading'))))),
|
|
324
|
+
result?.warnings?.length > 0
|
|
325
|
+
? h('div', { className: 'dsh-prompt-editor-warnings' },
|
|
326
|
+
h('p', { className: 'dsh-prompt-editor-warningsTitle' }, text('dialog.warnings')),
|
|
327
|
+
h('ul', {}, result.warnings.map((warning) => h('li', { key: warning }, warning))))
|
|
328
|
+
: null,
|
|
329
|
+
state.error !== null
|
|
330
|
+
? h('p', { className: 'dsh-prompt-editor-error', role: 'alert' },
|
|
331
|
+
text('dialog.error', { message: state.error }))
|
|
332
|
+
: null,
|
|
333
|
+
h('div', { className: 'dsh-prompt-editor-foot' },
|
|
334
|
+
h('span', { className: 'dsh-prompt-editor-footHint', role: 'status', 'aria-live': 'polite' },
|
|
335
|
+
statusText),
|
|
336
|
+
h('div', { className: 'dsh-prompt-editor-actions' },
|
|
337
|
+
h('button', {
|
|
338
|
+
ref: cancelRef,
|
|
339
|
+
type: 'button',
|
|
340
|
+
className: 'dsh-prompt-editor-btn',
|
|
341
|
+
onClick: onClose,
|
|
342
|
+
}, text('dialog.cancel')),
|
|
343
|
+
h('button', {
|
|
344
|
+
type: 'button',
|
|
345
|
+
className: 'dsh-prompt-editor-btn',
|
|
346
|
+
disabled: state.loading,
|
|
347
|
+
onClick: onRetry,
|
|
348
|
+
}, text('dialog.retry')),
|
|
349
|
+
h('button', {
|
|
350
|
+
ref: acceptRef,
|
|
351
|
+
type: 'button',
|
|
352
|
+
className: 'dsh-prompt-editor-btn dsh-prompt-editor-btnPrimary',
|
|
353
|
+
disabled: !canAccept,
|
|
354
|
+
onClick: onAccept,
|
|
355
|
+
}, acceptLabel)))))
|
|
356
|
+
|
|
357
|
+
return createPortal !== null ? createPortal(dialog, document.body) : dialog
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function ComposerButton(props) {
|
|
361
|
+
const text = useMemo(() => makeText(props.t), [props.t])
|
|
362
|
+
const draft = props.useInput((state) => state.draft) ?? ''
|
|
363
|
+
const draftRev = props.useInput((state) => state.draftRev)
|
|
364
|
+
const phase = props.useInput((state) => state.phase) ?? 'plain'
|
|
365
|
+
const occurrences = props.useInput((state) => state.occurrences) ?? []
|
|
366
|
+
const inputActions = props.inputActions
|
|
367
|
+
|
|
368
|
+
const [open, setOpen] = useState(false)
|
|
369
|
+
const [state, setState] = useState(null)
|
|
370
|
+
const abortRef = useRef(null)
|
|
371
|
+
const mountedRef = useRef(true)
|
|
372
|
+
const triggerRef = useRef(null)
|
|
373
|
+
|
|
374
|
+
useEffect(() => () => {
|
|
375
|
+
mountedRef.current = false
|
|
376
|
+
abortRef.current?.abort()
|
|
377
|
+
}, [])
|
|
378
|
+
|
|
379
|
+
const close = useCallback(() => {
|
|
380
|
+
abortRef.current?.abort()
|
|
381
|
+
abortRef.current = null
|
|
382
|
+
setOpen(false)
|
|
383
|
+
setState(null)
|
|
384
|
+
requestAnimationFrame(() => triggerRef.current?.focus?.())
|
|
385
|
+
}, [])
|
|
386
|
+
|
|
387
|
+
useEffect(() => {
|
|
388
|
+
if (!open) return undefined
|
|
389
|
+
const onKey = (event) => {
|
|
390
|
+
if (event.key === 'Escape') {
|
|
391
|
+
event.preventDefault()
|
|
392
|
+
close()
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
window.addEventListener('keydown', onKey)
|
|
396
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
397
|
+
}, [open, close])
|
|
398
|
+
|
|
399
|
+
const run = useCallback(async (source, span, selected, mode) => {
|
|
400
|
+
abortRef.current?.abort()
|
|
401
|
+
const controller = new AbortController()
|
|
402
|
+
abortRef.current = controller
|
|
403
|
+
setState({ source, span, selected, mode, raw: '', result: null, error: null, loading: true })
|
|
404
|
+
let sawDone = false
|
|
405
|
+
|
|
406
|
+
try {
|
|
407
|
+
const response = await fetch('/api/prompt-editor/enhance', {
|
|
408
|
+
method: 'POST',
|
|
409
|
+
credentials: 'same-origin',
|
|
410
|
+
headers: { 'content-type': 'application/json' },
|
|
411
|
+
body: JSON.stringify({ draft: source, mode, language: 'auto', sessionId: props.sessionId }),
|
|
412
|
+
signal: controller.signal,
|
|
413
|
+
})
|
|
414
|
+
if (!response.ok) {
|
|
415
|
+
let message = `HTTP ${response.status}`
|
|
416
|
+
try {
|
|
417
|
+
const body = await response.json()
|
|
418
|
+
if (typeof body?.error === 'string') message = body.error
|
|
419
|
+
} catch {
|
|
420
|
+
// Keep the status message.
|
|
421
|
+
}
|
|
422
|
+
throw new Error(message)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
await readSse(response, (event) => {
|
|
426
|
+
if (!mountedRef.current) return
|
|
427
|
+
if (event.type === 'done') sawDone = true
|
|
428
|
+
setState((previous) => {
|
|
429
|
+
if (previous === null) return previous
|
|
430
|
+
if (event.type === 'delta') {
|
|
431
|
+
return { ...previous, raw: previous.raw + (typeof event.text === 'string' ? event.text : '') }
|
|
432
|
+
}
|
|
433
|
+
if (event.type === 'reset') return { ...previous, raw: '' }
|
|
434
|
+
if (event.type === 'done') {
|
|
435
|
+
return { ...previous, result: event, raw: event.prompt ?? previous.raw, error: null, loading: false }
|
|
436
|
+
}
|
|
437
|
+
if (event.type === 'error') {
|
|
438
|
+
return { ...previous, error: typeof event.message === 'string' ? event.message : 'unknown error', loading: false }
|
|
439
|
+
}
|
|
440
|
+
return previous
|
|
441
|
+
})
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
if (!sawDone && !controller.signal.aborted) {
|
|
445
|
+
setState((previous) => previous !== null && previous.loading
|
|
446
|
+
? { ...previous, error: 'stream ended before the result', loading: false }
|
|
447
|
+
: previous)
|
|
448
|
+
}
|
|
449
|
+
} catch (error) {
|
|
450
|
+
if (error?.name === 'AbortError' || !mountedRef.current) return
|
|
451
|
+
setState((previous) => previous === null ? previous : {
|
|
452
|
+
...previous,
|
|
453
|
+
error: error instanceof Error ? error.message : String(error),
|
|
454
|
+
loading: false,
|
|
455
|
+
})
|
|
456
|
+
}
|
|
457
|
+
}, [props.sessionId])
|
|
458
|
+
|
|
459
|
+
const openDialog = useCallback(() => {
|
|
460
|
+
if (inputActions === undefined || typeof inputActions.captureInsertion !== 'function') return
|
|
461
|
+
let span
|
|
462
|
+
try {
|
|
463
|
+
span = inputActions.captureInsertion()
|
|
464
|
+
} catch {
|
|
465
|
+
return
|
|
466
|
+
}
|
|
467
|
+
if (span === null || span === undefined || !Number.isFinite(span.start) || !Number.isFinite(span.end)) return
|
|
468
|
+
if (span.draftRev !== undefined && draftRev !== undefined && span.draftRev !== draftRev) return
|
|
469
|
+
const selected = span.end > span.start
|
|
470
|
+
const source = selected ? draft.slice(span.start, span.end) : draft
|
|
471
|
+
if (source.trim() === '') return
|
|
472
|
+
setOpen(true)
|
|
473
|
+
run(source, span, selected, readMode())
|
|
474
|
+
}, [draft, inputActions, run])
|
|
475
|
+
|
|
476
|
+
const accept = useCallback(() => {
|
|
477
|
+
if (state === null || state.result === null || inputActions === undefined) return
|
|
478
|
+
if (state.result.changed !== true) {
|
|
479
|
+
close()
|
|
480
|
+
return
|
|
481
|
+
}
|
|
482
|
+
const span = state.selected
|
|
483
|
+
? state.span
|
|
484
|
+
: { start: 0, end: state.source.length, draftRev: state.span.draftRev }
|
|
485
|
+
let accepted = false
|
|
486
|
+
try {
|
|
487
|
+
accepted = inputActions.insertText(state.result.prompt, span) === true
|
|
488
|
+
} catch {
|
|
489
|
+
accepted = false
|
|
490
|
+
}
|
|
491
|
+
if (accepted) close()
|
|
492
|
+
else setState((previous) => previous === null ? previous : { ...previous, error: text('dialog.stale') })
|
|
493
|
+
}, [close, inputActions, state, text])
|
|
494
|
+
|
|
495
|
+
const retry = useCallback(() => {
|
|
496
|
+
if (state === null) return
|
|
497
|
+
run(state.source, state.span, state.selected, state.mode)
|
|
498
|
+
}, [run, state])
|
|
499
|
+
|
|
500
|
+
const changeMode = useCallback((mode) => {
|
|
501
|
+
if (state === null || (mode !== 'intent-first' && mode !== 'strict')) return
|
|
502
|
+
if (mode === state.mode) return
|
|
503
|
+
run(state.source, state.span, state.selected, mode)
|
|
504
|
+
}, [run, state])
|
|
505
|
+
|
|
506
|
+
const blocked = inputActions === undefined || phase !== 'plain' || occurrences.length > 0 || draft.trim() === ''
|
|
507
|
+
const reason = draft.trim() === ''
|
|
508
|
+
? text('button.empty')
|
|
509
|
+
: phase !== 'plain'
|
|
510
|
+
? text('button.busy')
|
|
511
|
+
: occurrences.length > 0
|
|
512
|
+
? text('button.references')
|
|
513
|
+
: text('button.title')
|
|
514
|
+
|
|
515
|
+
return h(React.Fragment, null,
|
|
516
|
+
h('style', {}, CSS),
|
|
517
|
+
h('button', {
|
|
518
|
+
ref: triggerRef,
|
|
519
|
+
type: 'button',
|
|
520
|
+
className: 'dsh-prompt-editor-trigger',
|
|
521
|
+
disabled: blocked,
|
|
522
|
+
title: reason,
|
|
523
|
+
'aria-label': `${text('button.label')}:${text('button.title')}`,
|
|
524
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
525
|
+
onClick: openDialog,
|
|
526
|
+
}, ICON, h('span', null, text('button.label'))),
|
|
527
|
+
open && state !== null
|
|
528
|
+
? h(PreviewDialog, {
|
|
529
|
+
state,
|
|
530
|
+
text,
|
|
531
|
+
onClose: close,
|
|
532
|
+
onAccept: accept,
|
|
533
|
+
onRetry: retry,
|
|
534
|
+
onMode: changeMode,
|
|
535
|
+
})
|
|
536
|
+
: null)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function SettingsPage(props) {
|
|
540
|
+
const text = useMemo(() => makeText(props.t), [props.t])
|
|
541
|
+
const [mode, setMode] = useState(() => readMode())
|
|
542
|
+
const [hostMode, setHostMode] = useState(null)
|
|
543
|
+
|
|
544
|
+
useEffect(() => {
|
|
545
|
+
let cancelled = false
|
|
546
|
+
fetch('/api/prompt-editor/settings', { credentials: 'same-origin' })
|
|
547
|
+
.then((response) => response.ok ? response.json() : null)
|
|
548
|
+
.then((body) => {
|
|
549
|
+
if (cancelled || body?.ok !== true) return
|
|
550
|
+
setHostMode(body.values?.defaultMode ?? null)
|
|
551
|
+
})
|
|
552
|
+
.catch(() => {})
|
|
553
|
+
return () => {
|
|
554
|
+
cancelled = true
|
|
555
|
+
}
|
|
556
|
+
}, [])
|
|
557
|
+
|
|
558
|
+
const change = (next) => {
|
|
559
|
+
if (next !== 'intent-first' && next !== 'strict') return
|
|
560
|
+
setMode(next)
|
|
561
|
+
writeMode(next)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
return h('div', { className: 'dsh-prompt-editor-settings' },
|
|
565
|
+
h('style', {}, CSS),
|
|
566
|
+
h('h2', null, text('settings.title')),
|
|
567
|
+
h('p', null, text('settings.intro')),
|
|
568
|
+
h('div', { className: 'dsh-prompt-editor-settingsRow' },
|
|
569
|
+
h('div', { className: 'dsh-prompt-editor-settingsCopy' },
|
|
570
|
+
h('span', { className: 'dsh-prompt-editor-settingsTitle' }, text('settings.mode')),
|
|
571
|
+
h('p', { className: 'dsh-prompt-editor-settingsHint' }, text('settings.modeHint'))),
|
|
572
|
+
h('select', {
|
|
573
|
+
className: 'dsh-prompt-editor-select',
|
|
574
|
+
value: mode,
|
|
575
|
+
'aria-label': text('settings.mode'),
|
|
576
|
+
onChange: (event) => change(event.target.value),
|
|
577
|
+
},
|
|
578
|
+
h('option', { value: 'intent-first' }, text('mode.intent-first')),
|
|
579
|
+
h('option', { value: 'strict' }, text('mode.strict')))),
|
|
580
|
+
hostMode !== null
|
|
581
|
+
? h('p', null, text('settings.profileDefault', {
|
|
582
|
+
mode: hostMode === 'strict' ? text('mode.strict') : text('mode.intent-first'),
|
|
583
|
+
}))
|
|
584
|
+
: null,
|
|
585
|
+
h('p', null, text('settings.local')))
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return {
|
|
589
|
+
inject: ['slots'],
|
|
590
|
+
apply(ctx) {
|
|
591
|
+
ctx.slots.inject('conversation.input.right', () => ctx.slots.register({
|
|
592
|
+
name: 'conversation.input.right',
|
|
593
|
+
id: 'prompt-editor',
|
|
594
|
+
order: 40,
|
|
595
|
+
locale: NS,
|
|
596
|
+
}, ComposerButton))
|
|
597
|
+
|
|
598
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
599
|
+
name: 'settings.section',
|
|
600
|
+
id: 'prompt-editor',
|
|
601
|
+
order: 60,
|
|
602
|
+
locale: NS,
|
|
603
|
+
label: () => FALLBACK['settings.nav'],
|
|
604
|
+
}, SettingsPage))
|
|
605
|
+
},
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
})
|
package/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { buildUserMessage, MODE_IDS, resolveMode } from './rules.mjs'
|
|
|
7
7
|
export const name = 'prompt-editor'
|
|
8
8
|
|
|
9
9
|
/** Services used by the tool. */
|
|
10
|
-
export const inject = ['tools', 'llm', 'agentDefaultModel', 'fs']
|
|
10
|
+
export const inject = ['tools', 'llm', 'agentDefaultModel', 'fs', 'connection']
|
|
11
11
|
|
|
12
12
|
/** User-editable defaults. */
|
|
13
13
|
export const Config = Schema.object({
|
|
@@ -78,6 +78,7 @@ export function apply(ctx, config) {
|
|
|
78
78
|
prompt: { type: 'string', required: true },
|
|
79
79
|
changed: { type: 'boolean', required: true },
|
|
80
80
|
mode: { type: 'string', required: true },
|
|
81
|
+
status: { type: 'string', required: true },
|
|
81
82
|
warnings: {
|
|
82
83
|
type: 'array',
|
|
83
84
|
required: true,
|
|
@@ -114,14 +115,117 @@ export function apply(ctx, config) {
|
|
|
114
115
|
})
|
|
115
116
|
},
|
|
116
117
|
}))
|
|
118
|
+
|
|
119
|
+
ctx.effect(
|
|
120
|
+
() => ctx.connection.fetch.register({
|
|
121
|
+
path: SETTINGS_PATH,
|
|
122
|
+
methods: ['GET'],
|
|
123
|
+
requestBody: 'buffered',
|
|
124
|
+
fetch: () => Response.json({
|
|
125
|
+
ok: true,
|
|
126
|
+
values: { defaultMode: config.defaultMode },
|
|
127
|
+
}),
|
|
128
|
+
}),
|
|
129
|
+
'prompt-editor: settings route',
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
ctx.effect(
|
|
133
|
+
() => ctx.connection.fetch.register({
|
|
134
|
+
path: ENHANCE_PATH,
|
|
135
|
+
methods: ['POST'],
|
|
136
|
+
requestBody: 'buffered',
|
|
137
|
+
fetch: (request) => handleEnhanceRoute(ctx, config, request),
|
|
138
|
+
}),
|
|
139
|
+
'prompt-editor: enhance route',
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Browser route for the composer button. */
|
|
144
|
+
const ENHANCE_PATH = '/api/prompt-editor/enhance'
|
|
145
|
+
|
|
146
|
+
/** Read-only settings route for the settings page. */
|
|
147
|
+
const SETTINGS_PATH = '/api/prompt-editor/settings'
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Stream one enhancement to the browser as Server-Sent Events.
|
|
151
|
+
* `delta` carries raw model text; `done` carries the validated result.
|
|
152
|
+
* @param {any} ctx
|
|
153
|
+
* @param {any} config
|
|
154
|
+
* @param {Request} request
|
|
155
|
+
*/
|
|
156
|
+
async function handleEnhanceRoute(ctx, config, request) {
|
|
157
|
+
let payload
|
|
158
|
+
try {
|
|
159
|
+
payload = await request.json()
|
|
160
|
+
} catch {
|
|
161
|
+
return Response.json({ ok: false, error: 'invalid JSON body' }, { status: 400 })
|
|
162
|
+
}
|
|
163
|
+
if (payload === null || typeof payload !== 'object' || typeof payload.draft !== 'string' || payload.draft.trim() === '') {
|
|
164
|
+
return Response.json({ ok: false, error: 'draft is required' }, { status: 400 })
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const encoder = new TextEncoder()
|
|
168
|
+
let closed = false
|
|
169
|
+
const stream = new ReadableStream({
|
|
170
|
+
async start(controller) {
|
|
171
|
+
const send = (event) => {
|
|
172
|
+
if (closed) return
|
|
173
|
+
try {
|
|
174
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`))
|
|
175
|
+
} catch {
|
|
176
|
+
closed = true
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const end = () => {
|
|
180
|
+
if (closed) return
|
|
181
|
+
closed = true
|
|
182
|
+
try {
|
|
183
|
+
controller.close()
|
|
184
|
+
} catch {
|
|
185
|
+
// The browser may have cancelled the stream.
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const result = await enhance({
|
|
191
|
+
ctx,
|
|
192
|
+
config,
|
|
193
|
+
draft: payload.draft,
|
|
194
|
+
mode: payload.mode,
|
|
195
|
+
language: payload.language,
|
|
196
|
+
context: payload.context,
|
|
197
|
+
signal: request.signal,
|
|
198
|
+
onDelta: (text) => send({ type: 'delta', text }),
|
|
199
|
+
onReset: () => send({ type: 'reset' }),
|
|
200
|
+
})
|
|
201
|
+
send({ type: 'done', ...result })
|
|
202
|
+
} catch (error) {
|
|
203
|
+
send({ type: 'error', message: error instanceof Error ? error.message : String(error) })
|
|
204
|
+
} finally {
|
|
205
|
+
end()
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
cancel() {
|
|
209
|
+
closed = true
|
|
210
|
+
},
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
return new Response(stream, {
|
|
214
|
+
status: 200,
|
|
215
|
+
headers: {
|
|
216
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
217
|
+
'cache-control': 'no-cache, no-store',
|
|
218
|
+
'x-accel-buffering': 'no',
|
|
219
|
+
},
|
|
220
|
+
})
|
|
117
221
|
}
|
|
118
222
|
|
|
119
223
|
/**
|
|
120
224
|
* Enhance a draft end to end.
|
|
121
|
-
* @param {{ ctx: any, config: any, draft: string, mode?: string, language?: string, context?: string, signal?: AbortSignal }} request
|
|
225
|
+
* @param {{ ctx: any, config: any, draft: string, mode?: string, language?: string, context?: string, signal?: AbortSignal, onDelta?: (text: string) => void, onReset?: () => void }} request
|
|
122
226
|
*/
|
|
123
227
|
export async function enhance(request) {
|
|
124
|
-
const { ctx, config, signal } = request
|
|
228
|
+
const { ctx, config, signal, onDelta, onReset } = request
|
|
125
229
|
const draft = normalizeDraft(request.draft, config.maxDraftChars)
|
|
126
230
|
const mode = resolveMode(request.mode ?? config.defaultMode)
|
|
127
231
|
const language = normalizeLanguage(request.language)
|
|
@@ -132,11 +236,12 @@ export async function enhance(request) {
|
|
|
132
236
|
let lastWarnings = []
|
|
133
237
|
|
|
134
238
|
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
135
|
-
|
|
239
|
+
if (attempt > 0) onReset?.()
|
|
240
|
+
const raw = await callModel({ ctx, config, mode, userMessage, signal, onDelta })
|
|
136
241
|
const parsed = parseModelOutput(raw)
|
|
137
242
|
|
|
138
243
|
if (parsed.kind === 'unchanged') {
|
|
139
|
-
return { prompt: draft, changed: false, mode: mode.id, warnings: [] }
|
|
244
|
+
return { prompt: draft, changed: false, mode: mode.id, warnings: [], status: 'unchanged' }
|
|
140
245
|
}
|
|
141
246
|
|
|
142
247
|
if (parsed.kind === 'prompt') {
|
|
@@ -157,6 +262,7 @@ export async function enhance(request) {
|
|
|
157
262
|
changed: parsed.prompt.trim() !== draft.trim(),
|
|
158
263
|
mode: mode.id,
|
|
159
264
|
warnings,
|
|
265
|
+
status: parsed.prompt.trim() !== draft.trim() ? 'changed' : 'unchanged',
|
|
160
266
|
}
|
|
161
267
|
}
|
|
162
268
|
|
|
@@ -166,6 +272,7 @@ export async function enhance(request) {
|
|
|
166
272
|
changed: false,
|
|
167
273
|
mode: mode.id,
|
|
168
274
|
warnings: [...warnings, 'fallback: original draft kept'],
|
|
275
|
+
status: 'fallback',
|
|
169
276
|
}
|
|
170
277
|
}
|
|
171
278
|
|
|
@@ -175,19 +282,19 @@ export async function enhance(request) {
|
|
|
175
282
|
}
|
|
176
283
|
|
|
177
284
|
lastWarnings = [parsed.kind === 'refusal' ? 'refusal' : 'empty_output']
|
|
178
|
-
if (attempt === 1) return { prompt: draft, changed: false, mode: mode.id, warnings: lastWarnings }
|
|
285
|
+
if (attempt === 1) return { prompt: draft, changed: false, mode: mode.id, warnings: lastWarnings, status: 'fallback' }
|
|
179
286
|
userMessage = withCorrection(baseUser, lastWarnings, raw)
|
|
180
287
|
}
|
|
181
288
|
|
|
182
|
-
return { prompt: draft, changed: false, mode: mode.id, warnings: lastWarnings }
|
|
289
|
+
return { prompt: draft, changed: false, mode: mode.id, warnings: lastWarnings, status: 'fallback' }
|
|
183
290
|
}
|
|
184
291
|
|
|
185
292
|
/**
|
|
186
293
|
* One model call.
|
|
187
|
-
* @param {{ ctx: any, config: any, mode: any, userMessage: string, signal?: AbortSignal }} request
|
|
294
|
+
* @param {{ ctx: any, config: any, mode: any, userMessage: string, signal?: AbortSignal, onDelta?: (text: string) => void }} request
|
|
188
295
|
*/
|
|
189
296
|
async function callModel(request) {
|
|
190
|
-
const { ctx, config, mode, userMessage, signal } = request
|
|
297
|
+
const { ctx, config, mode, userMessage, signal, onDelta } = request
|
|
191
298
|
const selection = ctx.agentDefaultModel.currentSelection()
|
|
192
299
|
if (selection?.provider === undefined || selection?.model === undefined) {
|
|
193
300
|
throw new Error('no default model selection is available')
|
|
@@ -210,6 +317,9 @@ async function callModel(request) {
|
|
|
210
317
|
signal: llmSignal,
|
|
211
318
|
})) {
|
|
212
319
|
assembler.push(chunk)
|
|
320
|
+
if (chunk.type === 'text-delta' && typeof chunk.text === 'string' && onDelta !== undefined) {
|
|
321
|
+
onDelta(chunk.text)
|
|
322
|
+
}
|
|
213
323
|
}
|
|
214
324
|
|
|
215
325
|
const text = assembler.blocks()
|
|
@@ -447,7 +557,13 @@ function cjkRatio(text) {
|
|
|
447
557
|
* @param {{ prompt: string, changed: boolean, mode: string, warnings: string[] }} value
|
|
448
558
|
*/
|
|
449
559
|
export function formatResult(value) {
|
|
450
|
-
const header = value.
|
|
560
|
+
const header = value.status === 'fallback'
|
|
561
|
+
? '未改写(校验未通过,已保留原文)'
|
|
562
|
+
: value.status === 'unchanged'
|
|
563
|
+
? '未改写(原文已足够清楚)'
|
|
564
|
+
: value.changed
|
|
565
|
+
? `已改写(${value.mode})`
|
|
566
|
+
: '未改写(原文已足够清楚)'
|
|
451
567
|
const lines = [header, '', value.prompt]
|
|
452
568
|
if (value.warnings.length > 0) {
|
|
453
569
|
lines.push('', '校验提示:', ...value.warnings.map((warning) => `- ${warning}`))
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Prompt editor",
|
|
4
|
+
"description": "Rewrite a composer draft beside the input: selection-aware, streaming preview, replace after confirmation. Never sends or executes."
|
|
5
|
+
},
|
|
6
|
+
"button": {
|
|
7
|
+
"label": "Enhance",
|
|
8
|
+
"title": "Rewrite the draft without sending it"
|
|
9
|
+
},
|
|
10
|
+
"dialog": {
|
|
11
|
+
"title": "Prompt editor",
|
|
12
|
+
"original": "Original",
|
|
13
|
+
"enhanced": "Rewritten",
|
|
14
|
+
"selection": "{count} characters selected",
|
|
15
|
+
"whole": "Whole draft",
|
|
16
|
+
"mode": "Rule",
|
|
17
|
+
"cancel": "Cancel",
|
|
18
|
+
"retry": "Retry",
|
|
19
|
+
"accept": "Replace draft",
|
|
20
|
+
"unchanged": "The draft is already clear; no change is needed.",
|
|
21
|
+
"unchangedShort": "No change",
|
|
22
|
+
"loading": "Rewriting…",
|
|
23
|
+
"failed": "No result",
|
|
24
|
+
"close": "Close",
|
|
25
|
+
"warnings": "Checks",
|
|
26
|
+
"replaceSelection": "Replace selection",
|
|
27
|
+
"replaceWhole": "Replace draft",
|
|
28
|
+
"fallback": "The rewrite failed validation; the original draft was kept.",
|
|
29
|
+
"fallbackShort": "Rewrite failed",
|
|
30
|
+
"done": "Rewrite complete ({count} checks)"
|
|
31
|
+
},
|
|
32
|
+
"settings": {
|
|
33
|
+
"nav": "Prompt editor",
|
|
34
|
+
"title": "Prompt editor",
|
|
35
|
+
"intro": "Choose the default rule for the composer button. It only rewrites; it never sends.",
|
|
36
|
+
"mode": "Default rule",
|
|
37
|
+
"modeHint": "Intent-first expands a thin draft; strict only removes ambiguity."
|
|
38
|
+
}
|
|
39
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "提示词增强",
|
|
4
|
+
"description": "在输入框旁改写草稿:选区感知、流式预览、确认后替换;不发送、不执行。"
|
|
5
|
+
},
|
|
6
|
+
"button": {
|
|
7
|
+
"label": "增强",
|
|
8
|
+
"title": "改写草稿(不发送)"
|
|
9
|
+
},
|
|
10
|
+
"dialog": {
|
|
11
|
+
"title": "提示词增强",
|
|
12
|
+
"original": "原文",
|
|
13
|
+
"enhanced": "改写结果",
|
|
14
|
+
"selection": "已选中 {count} 字符",
|
|
15
|
+
"whole": "全文",
|
|
16
|
+
"mode": "规则",
|
|
17
|
+
"cancel": "取消",
|
|
18
|
+
"retry": "重试",
|
|
19
|
+
"accept": "替换输入框",
|
|
20
|
+
"unchanged": "原文已足够清楚,不需要修改。",
|
|
21
|
+
"unchangedShort": "无需修改",
|
|
22
|
+
"loading": "正在改写…",
|
|
23
|
+
"failed": "未生成结果",
|
|
24
|
+
"close": "关闭",
|
|
25
|
+
"warnings": "校验提示",
|
|
26
|
+
"replaceSelection": "替换选中",
|
|
27
|
+
"replaceWhole": "替换全文",
|
|
28
|
+
"fallback": "改写未通过校验,已保留原文。",
|
|
29
|
+
"fallbackShort": "改写未通过",
|
|
30
|
+
"done": "改写完成({count} 条提示)"
|
|
31
|
+
},
|
|
32
|
+
"settings": {
|
|
33
|
+
"nav": "提示词增强",
|
|
34
|
+
"title": "提示词增强",
|
|
35
|
+
"intro": "设置输入框按钮使用的默认规则。规则只影响改写,不会发送消息。",
|
|
36
|
+
"mode": "默认规则",
|
|
37
|
+
"modeHint": "意图优先会补全成任务说明;最小改动只消除歧义。"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infinity_cheng/dsh-prompt-editor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Rewrite a rough prompt draft into a clear, executable prompt
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Rewrite a rough prompt draft into a clear, executable prompt in DeepSeek Harness: streaming preview, selection-aware replace, and fact-preservation checks.",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"files": [
|
|
9
9
|
"index.js",
|
|
10
10
|
"rules.mjs",
|
|
11
|
+
"client.js",
|
|
11
12
|
"cordis.patch.yml",
|
|
13
|
+
"locale/*.json",
|
|
12
14
|
"icon.svg"
|
|
13
15
|
],
|
|
14
16
|
"keywords": [
|
|
@@ -25,7 +27,7 @@
|
|
|
25
27
|
"license": "MIT",
|
|
26
28
|
"engines": {
|
|
27
29
|
"dsh": ">=0.2.2-rc.1 <0.3.0",
|
|
28
|
-
"node": ">=
|
|
30
|
+
"node": ">=24.9.0"
|
|
29
31
|
},
|
|
30
32
|
"publishConfig": {
|
|
31
33
|
"access": "public"
|
|
@@ -34,11 +36,35 @@
|
|
|
34
36
|
"@deepseek-ai/cordis": "^4.0.0",
|
|
35
37
|
"@deepseek-ai/dsh-llm": ">=0.2.2-rc.1 <0.3.0",
|
|
36
38
|
"@deepseek-ai/dsh-tools": ">=0.2.2-rc.1 <0.3.0",
|
|
37
|
-
"@deepseek-ai/schemastery": "^3.18.0"
|
|
39
|
+
"@deepseek-ai/schemastery": "^3.18.0",
|
|
40
|
+
"@deepseek-ai/dsh-client-ui-conversation": "*",
|
|
41
|
+
"@deepseek-ai/dsh-client-locale": "*"
|
|
38
42
|
},
|
|
39
43
|
"dsh": {
|
|
40
44
|
"bundle": {
|
|
41
45
|
"patch": "./cordis.patch.yml"
|
|
46
|
+
},
|
|
47
|
+
"client": {
|
|
48
|
+
"platform": "web",
|
|
49
|
+
"immediately": true,
|
|
50
|
+
"inject": [
|
|
51
|
+
"@deepseek-ai/dsh-client-ui-conversation",
|
|
52
|
+
"@deepseek-ai/dsh-client-locale"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"exports": {
|
|
57
|
+
".": "./index.js",
|
|
58
|
+
"./client": "./client.js",
|
|
59
|
+
"./locale/*.json": "./locale/*.json",
|
|
60
|
+
"./icon": "./icon.svg"
|
|
61
|
+
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@deepseek-ai/dsh-client-ui-conversation": {
|
|
64
|
+
"optional": true
|
|
65
|
+
},
|
|
66
|
+
"@deepseek-ai/dsh-client-locale": {
|
|
67
|
+
"optional": true
|
|
42
68
|
}
|
|
43
69
|
}
|
|
44
70
|
}
|