@infinity_cheng/dsh-prompt-editor 0.2.0 → 0.3.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 CHANGED
@@ -38,17 +38,23 @@ dsh plugin --profile web add @infinity_cheng/dsh-prompt-editor
38
38
  dsh web
39
39
  ```
40
40
 
41
- ## 输入框按钮(Phase 1)
41
+ ### 升级后必须重启 Host
42
+
43
+ Client 可以热更新,但 Host 的 JavaScript 模块换代必须**完全退出并重新打开** DeepSeek Harness。
44
+
45
+ 如果按钮能打开预览、但提交时提示 `HTTP 404` 或「Host 路由未加载」,说明 Client 已经是新版本、Host 还是旧模块。`Cmd+Q` 退出后重新打开即可;只关窗口不算。
46
+
47
+ ## 输入框按钮
42
48
 
43
49
  1. 在输入框里输入草稿,或选中其中一段。
44
- 2. 点输入框右侧的「增强」。
50
+ 2. 点输入框右侧的「增强」,或在输入框里按 `⌘⇧E` / `Ctrl+Shift+E`。
45
51
  3. 预览弹窗会流式显示改写结果;左侧是原文,右侧是改写,下面是确定性检查。
46
52
  4. 选 `替换选中` / `替换全文` 才写回草稿;`重试` 可换规则重跑,`取消` 不改任何内容。
47
53
  5. 有选区时只替换选区;没有选区时替换全文。替换后仍可用输入框自己的 `Ctrl+Z` 撤销。
48
54
 
49
55
  设置入口:**设置 → 提示词增强**。默认规则保存在本浏览器(`localStorage`),profile 的 `defaultMode` 仍是兜底默认值。
50
56
 
51
- 按钮在以下情况禁用:草稿为空、正在发送/执行、草稿含 `@文件`/链接等引用块(Phase 1 暂不支持替换含引用块的草稿)。
57
+ 含 `@文件`/链接引用块时,**先选中一段不含引用块的文字**再点增强:只替换选区,引用块原样保留。没有选区或选区包含引用块时,会提示重新选择;全文替换含引用块的草稿仍不支持。
52
58
 
53
59
  ## 工具调用
54
60
 
@@ -208,6 +214,16 @@ Safari 验证结果。
208
214
  - Host + Client 两半;Client 是 lazy-CJS bundle,无独立构建步骤
209
215
  - Client 槽位:`conversation.input.right`、`settings.section`
210
216
 
217
+ ### 0.3.0 Phase 2
218
+
219
+ - 输入框内 `⌘⇧E` / `Ctrl+Shift+E` 打开增强。
220
+ - 含引用块的草稿可选中不含引用块的一段增强,引用块原样保留;全文替换含引用块仍不支持。
221
+
222
+ ### 0.2.1 修复
223
+
224
+ - 404 不再只显示 `HTTP 404`,而是提示“Host 路由未加载,请完全退出并重新打开”。
225
+ - README 增加升级/404 排查说明。
226
+
211
227
  ### 0.2.0 Phase 1 验证
212
228
 
213
229
  - 在 DSH `0.2.2-rc.5` 的隔离 profile 中加载真实 Web UI:按钮出现在输入框右侧,空草稿禁用,输入后启用。
@@ -221,18 +237,17 @@ Safari 验证结果。
221
237
  ## 限制
222
238
 
223
239
  - **显式触发**:不会在每次发送时自动改写。
224
- - **含引用块的草稿暂不支持**:`@文件`、链接芯片等会禁用按钮;Phase 2 再处理。
240
+ - **含引用块全文替换不支持**:可选中不含引用块的一段增强;引用块本身不会被重写或删除。
225
241
  - **默认规则存在本浏览器**:设置页写 `localStorage`,不是 profile 配置;profile 的 `defaultMode` 仍是兜底。
226
- - **没有快捷键**:Phase 1 只有按钮入口。
227
242
  - **一次模型调用**:每次增强会消耗一次模型请求;硬失败时可能重试一次。
228
243
  - **检查是启发式的**:事实提取覆盖 URL、代码、版本、范围、路径、`@引用`、附件标记;不覆盖所有自然语言事实。
229
- - **不自动应用**:预览后必须点「替换输入框」。
244
+ - **不自动应用**:预览后必须点「替换选中」/「替换全文」。
230
245
  - **没有提示词库/A-B**:那是另一个产品方向。
231
246
 
232
247
  ## Roadmap
233
248
 
234
249
  - **Phase 1(已完成)**:输入框按钮 + 流式预览 + 选区感知 + 设置页
235
- - **Phase 2**:快捷键、引用块草稿、模型选择、设置持久化、inline diff
250
+ - **Phase 2(进行中)**:快捷键、引用块选区增强已完成;模型选择、设置持久化、inline diff 待做
236
251
  - **0.1.x**:收紧 React 升级类任务的长度;让 strict 模式在保持简短的同时补上验收信息
237
252
  - **后续**:规则集切换、A/B、团队词汇表、与提示词库插件联动
238
253
 
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+Shift+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': '改写结果',
@@ -49,6 +52,9 @@ window.__ModuleLoader__.load({
49
52
  'dialog.unchangedShort': '无需修改',
50
53
  'dialog.loading': '正在改写…',
51
54
  'dialog.failed': '未生成结果',
55
+ 'dialog.hostMissing': 'Host 路由未加载(HTTP 404)。请完全退出并重新打开 DeepSeek Harness,让 Host 加载 0.2.x。',
56
+ 'dialog.chipsSelect': '草稿含引用块。请先选中一段不含引用块的文字,再点增强。',
57
+ 'dialog.chipsOverlap': '选区包含引用块。请重新选择不含引用块的一段;引用块会原样保留。',
52
58
  'dialog.error': '改写失败:{message}',
53
59
  'dialog.stale': '草稿已变化,请重新打开。',
54
60
  'dialog.warnings': '校验提示',
@@ -99,6 +105,23 @@ window.__ModuleLoader__.load({
99
105
  }
100
106
 
101
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
+
102
125
  function visibleText(raw) {
103
126
  const text = String(raw ?? '')
104
127
  if (/^\s*UNCHANGED\b/i.test(text)) return ''
@@ -293,7 +316,7 @@ window.__ModuleLoader__.load({
293
316
  h('select', {
294
317
  className: 'dsh-prompt-editor-select',
295
318
  value: state.mode,
296
- disabled: state.loading,
319
+ disabled: state.loading || state.retryable === false,
297
320
  'aria-label': text('dialog.mode'),
298
321
  onChange: (event) => onMode(event.target.value),
299
322
  },
@@ -343,7 +366,7 @@ window.__ModuleLoader__.load({
343
366
  h('button', {
344
367
  type: 'button',
345
368
  className: 'dsh-prompt-editor-btn',
346
- disabled: state.loading,
369
+ disabled: state.loading || state.retryable === false,
347
370
  onClick: onRetry,
348
371
  }, text('dialog.retry')),
349
372
  h('button', {
@@ -396,11 +419,11 @@ window.__ModuleLoader__.load({
396
419
  return () => window.removeEventListener('keydown', onKey)
397
420
  }, [open, close])
398
421
 
399
- const run = useCallback(async (source, span, selected, mode) => {
422
+ const run = useCallback(async (source, span, selected, mode, context) => {
400
423
  abortRef.current?.abort()
401
424
  const controller = new AbortController()
402
425
  abortRef.current = controller
403
- 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 })
404
427
  let sawDone = false
405
428
 
406
429
  try {
@@ -408,14 +431,16 @@ window.__ModuleLoader__.load({
408
431
  method: 'POST',
409
432
  credentials: 'same-origin',
410
433
  headers: { 'content-type': 'application/json' },
411
- body: JSON.stringify({ draft: source, mode, language: 'auto', sessionId: props.sessionId }),
434
+ body: JSON.stringify({ draft: source, mode, language: 'auto', sessionId: props.sessionId, context }),
412
435
  signal: controller.signal,
413
436
  })
414
437
  if (!response.ok) {
415
- let message = `HTTP ${response.status}`
438
+ let message = response.status === 404
439
+ ? text('dialog.hostMissing')
440
+ : `HTTP ${response.status}`
416
441
  try {
417
442
  const body = await response.json()
418
- if (typeof body?.error === 'string') message = body.error
443
+ if (response.status !== 404 && typeof body?.error === 'string') message = body.error
419
444
  } catch {
420
445
  // Keep the status message.
421
446
  }
@@ -454,7 +479,7 @@ window.__ModuleLoader__.load({
454
479
  loading: false,
455
480
  })
456
481
  }
457
- }, [props.sessionId])
482
+ }, [props.sessionId, text])
458
483
 
459
484
  const openDialog = useCallback(() => {
460
485
  if (inputActions === undefined || typeof inputActions.captureInsertion !== 'function') return
@@ -469,9 +494,43 @@ window.__ModuleLoader__.load({
469
494
  const selected = span.end > span.start
470
495
  const source = selected ? draft.slice(span.start, span.end) : draft
471
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
+ }
472
515
  setOpen(true)
473
- run(source, span, selected, readMode())
474
- }, [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.shiftKey) return
523
+ if (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])
475
534
 
476
535
  const accept = useCallback(() => {
477
536
  if (state === null || state.result === null || inputActions === undefined) return
@@ -493,24 +552,25 @@ window.__ModuleLoader__.load({
493
552
  }, [close, inputActions, state, text])
494
553
 
495
554
  const retry = useCallback(() => {
496
- if (state === null) return
497
- 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)
498
557
  }, [run, state])
499
558
 
500
559
  const changeMode = useCallback((mode) => {
501
- 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
502
562
  if (mode === state.mode) return
503
- run(state.source, state.span, state.selected, mode)
563
+ run(state.source, state.span, state.selected, mode, state.context)
504
564
  }, [run, state])
505
565
 
506
- const blocked = inputActions === undefined || phase !== 'plain' || occurrences.length > 0 || draft.trim() === ''
566
+ const blocked = inputActions === undefined || phase !== 'plain' || draft.trim() === ''
507
567
  const reason = draft.trim() === ''
508
568
  ? text('button.empty')
509
569
  : phase !== 'plain'
510
570
  ? text('button.busy')
511
571
  : occurrences.length > 0
512
572
  ? text('button.references')
513
- : text('button.title')
573
+ : `${text('button.title')} · ${SHORTCUT_LABEL}`
514
574
 
515
575
  return h(React.Fragment, null,
516
576
  h('style', {}, CSS),
@@ -520,7 +580,7 @@ window.__ModuleLoader__.load({
520
580
  className: 'dsh-prompt-editor-trigger',
521
581
  disabled: blocked,
522
582
  title: reason,
523
- 'aria-label': `${text('button.label')}:${text('button.title')}`,
583
+ 'aria-label': `${text('button.label')}:${reason}`,
524
584
  onMouseDown: (event) => event.preventDefault(),
525
585
  onClick: openDialog,
526
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",
@@ -27,7 +29,10 @@
27
29
  "replaceWhole": "Replace draft",
28
30
  "fallback": "The rewrite failed validation; the original draft was kept.",
29
31
  "fallbackShort": "Rewrite failed",
30
- "done": "Rewrite complete ({count} checks)"
32
+ "done": "Rewrite complete ({count} checks)",
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."
31
36
  },
32
37
  "settings": {
33
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": "提示词增强",
@@ -27,7 +29,10 @@
27
29
  "replaceWhole": "替换全文",
28
30
  "fallback": "改写未通过校验,已保留原文。",
29
31
  "fallbackShort": "改写未通过",
30
- "done": "改写完成({count} 条提示)"
32
+ "done": "改写完成({count} 条提示)",
33
+ "hostMissing": "Host 路由未加载(HTTP 404)。请完全退出并重新打开 DeepSeek Harness,让 Host 加载 0.2.x。",
34
+ "chipsSelect": "草稿含引用块。请先选中一段不含引用块的文字,再点增强。",
35
+ "chipsOverlap": "选区包含引用块。请重新选择不含引用块的一段;引用块会原样保留。"
31
36
  },
32
37
  "settings": {
33
38
  "nav": "提示词增强",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infinity_cheng/dsh-prompt-editor",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
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",