@infinity_cheng/dsh-prompt-editor 0.2.1 → 0.3.1

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 CHANGED
@@ -44,17 +44,17 @@ Client 可以热更新,但 Host 的 JavaScript 模块换代必须**完全退
44
44
 
45
45
  如果按钮能打开预览、但提交时提示 `HTTP 404` 或「Host 路由未加载」,说明 Client 已经是新版本、Host 还是旧模块。`Cmd+Q` 退出后重新打开即可;只关窗口不算。
46
46
 
47
- ## 输入框按钮(Phase 1)
47
+ ## 输入框按钮
48
48
 
49
49
  1. 在输入框里输入草稿,或选中其中一段。
50
- 2. 点输入框右侧的「增强」。
50
+ 2. 点输入框右侧的「增强」,或在输入框里按 `⌘E` / `Ctrl+E`(`⌘⇧E` / `Ctrl+Shift+E` 也兼容)。
51
51
  3. 预览弹窗会流式显示改写结果;左侧是原文,右侧是改写,下面是确定性检查。
52
52
  4. 选 `替换选中` / `替换全文` 才写回草稿;`重试` 可换规则重跑,`取消` 不改任何内容。
53
53
  5. 有选区时只替换选区;没有选区时替换全文。替换后仍可用输入框自己的 `Ctrl+Z` 撤销。
54
54
 
55
55
  设置入口:**设置 → 提示词增强**。默认规则保存在本浏览器(`localStorage`),profile 的 `defaultMode` 仍是兜底默认值。
56
56
 
57
- 按钮在以下情况禁用:草稿为空、正在发送/执行、草稿含 `@文件`/链接等引用块(Phase 1 暂不支持替换含引用块的草稿)。
57
+ 含 `@文件`/链接引用块时,**先选中一段不含引用块的文字**再点增强:只替换选区,引用块原样保留。没有选区或选区包含引用块时,会提示重新选择;全文替换含引用块的草稿仍不支持。
58
58
 
59
59
  ## 工具调用
60
60
 
@@ -214,6 +214,15 @@ Safari 验证结果。
214
214
  - Host + Client 两半;Client 是 lazy-CJS bundle,无独立构建步骤
215
215
  - Client 槽位:`conversation.input.right`、`settings.section`
216
216
 
217
+ ### 0.3.1 快捷键修复
218
+
219
+ - 主快捷键改为 `⌘E` / `Ctrl+E`,避开 Mac 输入法常吞掉的 Shift+字母;`⌘⇧E` / `Ctrl+Shift+E` 仍兼容。
220
+
221
+ ### 0.3.0 Phase 2
222
+
223
+ - 输入框内 `⌘E` / `Ctrl+E` 打开增强;兼容 `⌘⇧E` / `Ctrl+Shift+E`。
224
+ - 含引用块的草稿可选中不含引用块的一段增强,引用块原样保留;全文替换含引用块仍不支持。
225
+
217
226
  ### 0.2.1 修复
218
227
 
219
228
  - 404 不再只显示 `HTTP 404`,而是提示“Host 路由未加载,请完全退出并重新打开”。
@@ -232,18 +241,17 @@ Safari 验证结果。
232
241
  ## 限制
233
242
 
234
243
  - **显式触发**:不会在每次发送时自动改写。
235
- - **含引用块的草稿暂不支持**:`@文件`、链接芯片等会禁用按钮;Phase 2 再处理。
244
+ - **含引用块全文替换不支持**:可选中不含引用块的一段增强;引用块本身不会被重写或删除。
236
245
  - **默认规则存在本浏览器**:设置页写 `localStorage`,不是 profile 配置;profile 的 `defaultMode` 仍是兜底。
237
- - **没有快捷键**:Phase 1 只有按钮入口。
238
246
  - **一次模型调用**:每次增强会消耗一次模型请求;硬失败时可能重试一次。
239
247
  - **检查是启发式的**:事实提取覆盖 URL、代码、版本、范围、路径、`@引用`、附件标记;不覆盖所有自然语言事实。
240
- - **不自动应用**:预览后必须点「替换输入框」。
248
+ - **不自动应用**:预览后必须点「替换选中」/「替换全文」。
241
249
  - **没有提示词库/A-B**:那是另一个产品方向。
242
250
 
243
251
  ## Roadmap
244
252
 
245
253
  - **Phase 1(已完成)**:输入框按钮 + 流式预览 + 选区感知 + 设置页
246
- - **Phase 2**:快捷键、引用块草稿、模型选择、设置持久化、inline diff
254
+ - **Phase 2(进行中)**:快捷键、引用块选区增强已完成;模型选择、设置持久化、inline diff 待做
247
255
  - **0.1.x**:收紧 React 升级类任务的长度;让 strict 模式在保持简短的同时补上验收信息
248
256
  - **后续**:规则集切换、A/B、团队词汇表、与提示词库插件联动
249
257
 
package/client.js CHANGED
@@ -22,6 +22,8 @@ window.__ModuleLoader__.load({
22
22
  const MODE_KEY = 'dsh-prompt-editor:mode'
23
23
  const OPEN_TAG = '<enhanced_prompt>'
24
24
  const CLOSE_TAG = '</enhanced_prompt>'
25
+ const IS_MAC = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform || '')
26
+ const SHORTCUT_LABEL = IS_MAC ? '⌘E' : 'Ctrl+E'
25
27
 
26
28
  const FALLBACK = {
27
29
  'meta.title': '提示词增强',
@@ -30,7 +32,8 @@ window.__ModuleLoader__.load({
30
32
  'button.title': '改写草稿(不发送)',
31
33
  'button.empty': '先输入草稿',
32
34
  'button.busy': '正在发送或执行,暂时不能改写',
33
- 'button.references': '含引用块的草稿暂不支持',
35
+ 'button.references': '含引用块:选中不含引用块的一段再增强',
36
+ 'button.shortcut': '快捷键:{combo}',
34
37
  'dialog.title': '提示词增强',
35
38
  'dialog.original': '原文',
36
39
  'dialog.enhanced': '改写结果',
@@ -50,6 +53,8 @@ window.__ModuleLoader__.load({
50
53
  'dialog.loading': '正在改写…',
51
54
  'dialog.failed': '未生成结果',
52
55
  'dialog.hostMissing': 'Host 路由未加载(HTTP 404)。请完全退出并重新打开 DeepSeek Harness,让 Host 加载 0.2.x。',
56
+ 'dialog.chipsSelect': '草稿含引用块。请先选中一段不含引用块的文字,再点增强。',
57
+ 'dialog.chipsOverlap': '选区包含引用块。请重新选择不含引用块的一段;引用块会原样保留。',
53
58
  'dialog.error': '改写失败:{message}',
54
59
  'dialog.stale': '草稿已变化,请重新打开。',
55
60
  'dialog.warnings': '校验提示',
@@ -100,6 +105,23 @@ window.__ModuleLoader__.load({
100
105
  }
101
106
 
102
107
  /** Show only the prompt body while the model is still emitting its tagged answer. */
108
+ /** Chip ranges in draft coordinates; tolerate the old offset/length shape. */
109
+ function chipRanges(occurrences) {
110
+ return (Array.isArray(occurrences) ? occurrences : [])
111
+ .map((chip) => {
112
+ const start = Number.isFinite(chip?.start) ? chip.start : chip?.offset
113
+ const end = Number.isFinite(chip?.end)
114
+ ? chip.end
115
+ : (Number.isFinite(start) && Number.isFinite(chip?.length) ? start + chip.length : undefined)
116
+ return { start, end }
117
+ })
118
+ .filter((chip) => Number.isFinite(chip.start) && Number.isFinite(chip.end) && chip.end > chip.start)
119
+ }
120
+
121
+ function overlapsChip(chips, span) {
122
+ return chips.some((chip) => span.start < chip.end && span.end > chip.start)
123
+ }
124
+
103
125
  function visibleText(raw) {
104
126
  const text = String(raw ?? '')
105
127
  if (/^\s*UNCHANGED\b/i.test(text)) return ''
@@ -294,7 +316,7 @@ window.__ModuleLoader__.load({
294
316
  h('select', {
295
317
  className: 'dsh-prompt-editor-select',
296
318
  value: state.mode,
297
- disabled: state.loading,
319
+ disabled: state.loading || state.retryable === false,
298
320
  'aria-label': text('dialog.mode'),
299
321
  onChange: (event) => onMode(event.target.value),
300
322
  },
@@ -344,7 +366,7 @@ window.__ModuleLoader__.load({
344
366
  h('button', {
345
367
  type: 'button',
346
368
  className: 'dsh-prompt-editor-btn',
347
- disabled: state.loading,
369
+ disabled: state.loading || state.retryable === false,
348
370
  onClick: onRetry,
349
371
  }, text('dialog.retry')),
350
372
  h('button', {
@@ -397,11 +419,11 @@ window.__ModuleLoader__.load({
397
419
  return () => window.removeEventListener('keydown', onKey)
398
420
  }, [open, close])
399
421
 
400
- const run = useCallback(async (source, span, selected, mode) => {
422
+ const run = useCallback(async (source, span, selected, mode, context) => {
401
423
  abortRef.current?.abort()
402
424
  const controller = new AbortController()
403
425
  abortRef.current = controller
404
- setState({ source, span, selected, mode, raw: '', result: null, error: null, loading: true })
426
+ setState({ source, span, selected, mode, context, raw: '', result: null, error: null, loading: true, retryable: true })
405
427
  let sawDone = false
406
428
 
407
429
  try {
@@ -409,7 +431,7 @@ window.__ModuleLoader__.load({
409
431
  method: 'POST',
410
432
  credentials: 'same-origin',
411
433
  headers: { 'content-type': 'application/json' },
412
- body: JSON.stringify({ draft: source, mode, language: 'auto', sessionId: props.sessionId }),
434
+ body: JSON.stringify({ draft: source, mode, language: 'auto', sessionId: props.sessionId, context }),
413
435
  signal: controller.signal,
414
436
  })
415
437
  if (!response.ok) {
@@ -472,9 +494,43 @@ window.__ModuleLoader__.load({
472
494
  const selected = span.end > span.start
473
495
  const source = selected ? draft.slice(span.start, span.end) : draft
474
496
  if (source.trim() === '') return
497
+ const chips = chipRanges(occurrences)
498
+ const mode = readMode()
499
+ if (chips.length > 0 && !selected) {
500
+ setOpen(true)
501
+ setState({
502
+ source: draft, span, selected: false, mode, context: undefined, raw: '',
503
+ result: null, error: text('dialog.chipsSelect'), loading: false, retryable: false,
504
+ })
505
+ return
506
+ }
507
+ if (chips.length > 0 && overlapsChip(chips, span)) {
508
+ setOpen(true)
509
+ setState({
510
+ source, span, selected: true, mode, context: undefined, raw: '',
511
+ result: null, error: text('dialog.chipsOverlap'), loading: false, retryable: false,
512
+ })
513
+ return
514
+ }
475
515
  setOpen(true)
476
- run(source, span, selected, readMode())
477
- }, [draft, inputActions, run])
516
+ run(source, span, selected, mode, chips.length > 0 ? draft : undefined)
517
+ }, [draft, draftRev, inputActions, occurrences, run, text])
518
+
519
+ useEffect(() => {
520
+ const onKey = (event) => {
521
+ if (open) return
522
+ if (!(event.metaKey || event.ctrlKey) || event.altKey) return
523
+ if (event.code !== 'KeyE' && event.key.toLowerCase() !== 'e') return
524
+ const target = event.target
525
+ if (!(target instanceof HTMLElement)) return
526
+ if (target.closest('[contenteditable="true"]') === null
527
+ && target.closest('.dsh-prompt-editor-trigger') === null) return
528
+ event.preventDefault()
529
+ openDialog()
530
+ }
531
+ window.addEventListener('keydown', onKey, true)
532
+ return () => window.removeEventListener('keydown', onKey, true)
533
+ }, [open, openDialog])
478
534
 
479
535
  const accept = useCallback(() => {
480
536
  if (state === null || state.result === null || inputActions === undefined) return
@@ -496,24 +552,25 @@ window.__ModuleLoader__.load({
496
552
  }, [close, inputActions, state, text])
497
553
 
498
554
  const retry = useCallback(() => {
499
- if (state === null) return
500
- run(state.source, state.span, state.selected, state.mode)
555
+ if (state === null || state.retryable === false) return
556
+ run(state.source, state.span, state.selected, state.mode, state.context)
501
557
  }, [run, state])
502
558
 
503
559
  const changeMode = useCallback((mode) => {
504
- if (state === null || (mode !== 'intent-first' && mode !== 'strict')) return
560
+ if (state === null || state.retryable === false) return
561
+ if (mode !== 'intent-first' && mode !== 'strict') return
505
562
  if (mode === state.mode) return
506
- run(state.source, state.span, state.selected, mode)
563
+ run(state.source, state.span, state.selected, mode, state.context)
507
564
  }, [run, state])
508
565
 
509
- const blocked = inputActions === undefined || phase !== 'plain' || occurrences.length > 0 || draft.trim() === ''
566
+ const blocked = inputActions === undefined || phase !== 'plain' || draft.trim() === ''
510
567
  const reason = draft.trim() === ''
511
568
  ? text('button.empty')
512
569
  : phase !== 'plain'
513
570
  ? text('button.busy')
514
571
  : occurrences.length > 0
515
572
  ? text('button.references')
516
- : text('button.title')
573
+ : `${text('button.title')} · ${SHORTCUT_LABEL}`
517
574
 
518
575
  return h(React.Fragment, null,
519
576
  h('style', {}, CSS),
@@ -523,7 +580,7 @@ window.__ModuleLoader__.load({
523
580
  className: 'dsh-prompt-editor-trigger',
524
581
  disabled: blocked,
525
582
  title: reason,
526
- 'aria-label': `${text('button.label')}:${text('button.title')}`,
583
+ 'aria-label': `${text('button.label')}:${reason}`,
527
584
  onMouseDown: (event) => event.preventDefault(),
528
585
  onClick: openDialog,
529
586
  }, ICON, h('span', null, text('button.label'))),
package/locale/en.json CHANGED
@@ -5,7 +5,9 @@
5
5
  },
6
6
  "button": {
7
7
  "label": "Enhance",
8
- "title": "Rewrite the draft without sending it"
8
+ "title": "Rewrite the draft without sending it",
9
+ "references": "References present: select a passage without chips, then enhance",
10
+ "shortcut": "Shortcut: {combo}"
9
11
  },
10
12
  "dialog": {
11
13
  "title": "Prompt editor",
@@ -28,7 +30,9 @@
28
30
  "fallback": "The rewrite failed validation; the original draft was kept.",
29
31
  "fallbackShort": "Rewrite failed",
30
32
  "done": "Rewrite complete ({count} checks)",
31
- "hostMissing": "Host route is not loaded (HTTP 404). Fully quit and reopen DeepSeek Harness so the Host loads 0.2.x."
33
+ "hostMissing": "Host route is not loaded (HTTP 404). Fully quit and reopen DeepSeek Harness so the Host loads 0.2.x.",
34
+ "chipsSelect": "This draft has reference chips. Select a passage that contains no chip, then enhance.",
35
+ "chipsOverlap": "The selection contains a reference chip. Select a passage without chips; chips are preserved."
32
36
  },
33
37
  "settings": {
34
38
  "nav": "Prompt editor",
package/locale/zh.json CHANGED
@@ -5,7 +5,9 @@
5
5
  },
6
6
  "button": {
7
7
  "label": "增强",
8
- "title": "改写草稿(不发送)"
8
+ "title": "改写草稿(不发送)",
9
+ "references": "含引用块:选中不含引用块的一段再增强",
10
+ "shortcut": "快捷键:{combo}"
9
11
  },
10
12
  "dialog": {
11
13
  "title": "提示词增强",
@@ -28,7 +30,9 @@
28
30
  "fallback": "改写未通过校验,已保留原文。",
29
31
  "fallbackShort": "改写未通过",
30
32
  "done": "改写完成({count} 条提示)",
31
- "hostMissing": "Host 路由未加载(HTTP 404)。请完全退出并重新打开 DeepSeek Harness,让 Host 加载 0.2.x。"
33
+ "hostMissing": "Host 路由未加载(HTTP 404)。请完全退出并重新打开 DeepSeek Harness,让 Host 加载 0.2.x。",
34
+ "chipsSelect": "草稿含引用块。请先选中一段不含引用块的文字,再点增强。",
35
+ "chipsOverlap": "选区包含引用块。请重新选择不含引用块的一段;引用块会原样保留。"
32
36
  },
33
37
  "settings": {
34
38
  "nav": "提示词增强",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infinity_cheng/dsh-prompt-editor",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
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",