dsh-prompt-scheduler 0.5.1 → 0.5.5

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.
Files changed (4) hide show
  1. package/README.md +2 -0
  2. package/client.js +128 -26
  3. package/index.js +7 -3
  4. package/package.json +2 -3
package/README.md CHANGED
@@ -28,6 +28,8 @@
28
28
  - **谷时 / 自定义**:`谷时` = 按 DeepSeek 官方峰谷规则算出的下一个可发送的非峰时时刻;`自定义` = 任意日期时间,
29
29
  并标出该时刻是谷价 / 峰价 / 平价。
30
30
  - **输入框为空时跳过**:到点如果输入框是空的,不会发送,定时自动取消(不会把空消息发出去)。
31
+ - **提前手动发送会取消定时**:在到点之前你自己把消息发了,定时立刻作废
32
+ (否则到点它会去按发送,把那时输入框里的内容——可能是刚打了一半的字——发出去)。
31
33
  - **多标签页不重复**:同一时间点由先到达的标签页按一次发送,其它标签页看到后放弃。
32
34
  - **视觉跟随 DSH**:只用 `--dsw-*` 主题变量,颜色/圆角/字号跟随宿主主题与深浅色。
33
35
  - **独立运行**:不依赖任何其他插件;未装 `cost-meter` 时用内置的 DeepSeek 官方规则。
package/client.js CHANGED
@@ -369,7 +369,24 @@ window.__ModuleLoader__.load({
369
369
  }
370
370
 
371
371
  // ---------------------------------------------------------------- timer engine
372
- var engine = { timeout: null, interval: null, firing: false, sessionId: null }
372
+ var engine = { timeout: null, interval: null, firing: false, sessionId: null, firingWatch: null }
373
+
374
+ /**
375
+ * 火控锁的唯一写入口:开锁时挂一个看门狗。
376
+ * 到点按发送这条路上有几次网络往返,任何一次异常没收敛都会让 firing 永远为 true,
377
+ * 之后到这里就被 `if (engine.firing) return` 挡掉——表现是"定时还在、但永远不再发"。
378
+ */
379
+ function holdFiring(on) {
380
+ if (engine.firingWatch !== null) { clearTimeout(engine.firingWatch); engine.firingWatch = null }
381
+ engine.firing = !!on
382
+ if (on) {
383
+ engine.firingWatch = setTimeout(function () {
384
+ engine.firingWatch = null
385
+ engine.firing = false
386
+ try { syncTimer() } catch (e) { /* 下一轮轮询会再试 */ }
387
+ }, 15000)
388
+ }
389
+ }
373
390
 
374
391
  function clearEngineTimer() {
375
392
  if (engine.timeout !== null) { clearTimeout(engine.timeout); engine.timeout = null }
@@ -406,10 +423,17 @@ window.__ModuleLoader__.load({
406
423
 
407
424
  function finishClear() {
408
425
  var sessionId = store.state.sessionId
409
- callHost('/timer/clear', { sessionId: sessionId }).catch(function () {}).then(function () {
410
- engine.firing = false
411
- patch({ timer: null, rev: store.state.rev + 1 })
412
- })
426
+ // 成功/失败都要解锁并收起卡片:定时已经没了,卡片继续停在"打开"状态只会让下一次
427
+ // 点击变成"先关掉一个看不见的卡片",看起来就像按钮失灵。
428
+ var done = function () {
429
+ holdFiring(false)
430
+ patch({ timer: null, open: false, rev: store.state.rev + 1 })
431
+ }
432
+ try {
433
+ callHost('/timer/clear', { sessionId: sessionId }).then(done, done)
434
+ } catch (e) {
435
+ done()
436
+ }
413
437
  }
414
438
  /**
415
439
  * 到点结局回报:Host 只记一行事件日志,不改 timer.json、不做副作用。
@@ -429,6 +453,28 @@ window.__ModuleLoader__.load({
429
453
  } catch (e) { /* reporting must never break the fire path */ }
430
454
  }
431
455
 
456
+ /**
457
+ * 用户抢在定时之前自己把消息发了:立刻作废这条定时。
458
+ * 不这么做的话,到点它会去按发送,把**那时候输入框里的内容**(可能是刚打了一半的字)发出去,
459
+ * 而用户显然已经用行动表示"我不等了"。
460
+ */
461
+ function cancelForManualSend() {
462
+ var sessionId = store.state.sessionId
463
+ var timer = store.state.timer
464
+ if (!sessionId || !timer) return
465
+ try {
466
+ console.info('[prompt-scheduler] manual send detected, timer cancelled', {
467
+ fireAtMs: timer.fireAtMs,
468
+ earlyByMs: timer.fireAtMs - Date.now(),
469
+ })
470
+ } catch (e) { /* no console */ }
471
+ try {
472
+ callHost('/timer/clear', { sessionId: sessionId, reason: 'manual-send' }).catch(function () { /* host half may be reloading */ })
473
+ } catch (e) { /* clearing is best effort */ }
474
+ clearEngineTimer()
475
+ patch({ timer: null, open: false, rev: store.state.rev + 1 })
476
+ }
477
+
432
478
  function fireNow(reason) {
433
479
  if (engine.firing) return
434
480
  var timer = store.state.timer
@@ -438,19 +484,19 @@ window.__ModuleLoader__.load({
438
484
  if (reason !== 'timer' && overdue <= -1000) return
439
485
  if (overdue > GRACE_MS) {
440
486
  // Too late to mean anything: drop the timer instead of sending stale input.
441
- engine.firing = true
487
+ holdFiring(true)
442
488
  reportFire(sessionId, timer, 'skipped-overdue', '', 'overdue ' + Math.round(overdue / 1000) + 's')
443
489
  finishClear()
444
490
  return
445
491
  }
446
492
  if (!String(store.state.draft || '').trim()) {
447
- engine.firing = true
493
+ holdFiring(true)
448
494
  reportFire(sessionId, timer, 'skipped-empty', '', 'composer was empty')
449
495
  finishClear()
450
496
  return
451
497
  }
452
498
  // Hold the lock across press → clear so a concurrent poll cannot press twice.
453
- engine.firing = true
499
+ holdFiring(true)
454
500
  if (!claimFire(sessionId, timer.fireAtMs)) {
455
501
  reportFire(sessionId, timer, 'claimed-elsewhere', '', 'another tab owned the deadline')
456
502
  finishClear()
@@ -459,14 +505,13 @@ window.__ModuleLoader__.load({
459
505
  var method = pressSend()
460
506
  if (!method) {
461
507
  reportFire(sessionId, timer, 'press-failed', '', 'send button and Enter fallback both missed')
462
- engine.firing = false
508
+ holdFiring(false)
463
509
  return
464
510
  }
465
511
  reportFire(sessionId, timer, 'sent', method, '')
466
512
  finishClear()
467
513
  }
468
- function syncTimer() {
469
- var sessionId = store.state.sessionId
514
+ function syncTimer() { var sessionId = store.state.sessionId
470
515
  if (!sessionId) return
471
516
  engine.sessionId = sessionId
472
517
  callHost('/timer', { sessionId: sessionId }).then(function (data) {
@@ -483,7 +528,16 @@ window.__ModuleLoader__.load({
483
528
  if (document.hidden) return
484
529
  syncTimer()
485
530
  }, POLL_MS)
486
- function onVisible() { if (!document.hidden) syncTimer() }
531
+ function onVisible() {
532
+ if (document.hidden) return
533
+ // Self-heal: state says open but no card is mounted (a render threw, or the
534
+ // host remounted the overlay). Clear it so the next click opens instead of
535
+ // closing something invisible.
536
+ try {
537
+ if (store.state.open && !document.querySelector('.psx-pop')) patch({ open: false })
538
+ } catch (e) { /* no document */ }
539
+ syncTimer()
540
+ }
487
541
  document.addEventListener('visibilitychange', onVisible)
488
542
  window.addEventListener('focus', onVisible)
489
543
  return function () {
@@ -502,6 +556,11 @@ window.__ModuleLoader__.load({
502
556
  sendButton: !!labelledSendButton(),
503
557
  editor: !!composerEditor(),
504
558
  commands: { enabled: commands.enabled, registered: commands.registered.slice(), error: commands.error },
559
+ // 排查"点了没反应"用:open 说有卡片、overlayInDom 说实际有没有渲染出来。
560
+ firing: engine.firing,
561
+ open: !!store.state.open,
562
+ overlayInDom: !!document.querySelector('.psx-pop'),
563
+ anchor: store.state.anchor,
505
564
  }
506
565
  }
507
566
  } catch (e) { /* non-browser context */ }
@@ -545,11 +604,15 @@ window.__ModuleLoader__.load({
545
604
  var sessionId = props && props.sessionId ? props.sessionId : null
546
605
  var btnRef = React.useRef(null)
547
606
  var draft = ''
607
+ var phase = ''
548
608
  try {
549
609
  if (props && typeof props.useInput === 'function') {
550
610
  draft = props.useInput(function (input) { return (input && input.draft) || '' }) || ''
611
+ // dsh 的 composer 状态机:'plain' | 'adjudicating' | 'claimed' | 'submitting' | 'sending'。
612
+ // 'sending' 出现 = 用户(或我们的 pressSend)真的发出了一条消息。
613
+ phase = props.useInput(function (input) { return (input && input.phase) || '' }) || ''
551
614
  }
552
- } catch (e) { draft = '' }
615
+ } catch (e) { draft = ''; phase = '' }
553
616
 
554
617
  React.useEffect(function () {
555
618
  if (sessionId && store.state.sessionId !== sessionId) {
@@ -564,22 +627,55 @@ window.__ModuleLoader__.load({
564
627
  if (store.state.timer && String(draft || '').trim()) fireNow('draft')
565
628
  }, [draft])
566
629
 
630
+ // 用户自己提前把消息发了 → 定时立刻作废。
631
+ // 否则到点会去按发送,把**那时候输入框里的内容**(可能是刚打了一半的字)发出去。
632
+ // 只在"进入 sending 的那一刻"触发:我们的 pressSend 走这条路时 engine.firing 已经是 true,会跳过。
633
+ var prevPhase = React.useRef(phase)
634
+ React.useEffect(function () {
635
+ var previous = prevPhase.current
636
+ prevPhase.current = phase
637
+ if (phase !== 'sending' || previous === 'sending') return
638
+ if (engine.firing) return
639
+ if (!store.state.timer || !store.state.sessionId) return
640
+ cancelForManualSend()
641
+ }, [phase])
642
+
567
643
  var [rect, setRect] = React.useState(null)
568
644
  var openPopover = function (event) {
569
- var btnRect = event.currentTarget.getBoundingClientRect()
570
- // Anchor above the composer CARD, not above the button: the button sits
571
- // inside the card, so a button-anchored card would cover the draft text.
572
- var card = event.currentTarget.closest ? event.currentTarget.closest('[data-composer-card]') : null
573
- var cardRect = card ? card.getBoundingClientRect() : null
574
- var anchor = cardRect
575
- ? { left: btnRect.left, top: cardRect.top, bottom: cardRect.bottom, width: btnRect.width, height: cardRect.height }
576
- : { left: btnRect.left, top: btnRect.top, bottom: btnRect.bottom, width: btnRect.width, height: btnRect.height }
645
+ // 每点必打一行:排查"点不动"时,有这行 = 我们收到了点击(问题在渲染/定位),
646
+ // 没有这行 = 点击根本没到按钮(宿主把工具行禁用了)。别删,成本只有一行 console.
647
+ var rendered = false
648
+ try { rendered = !!document.querySelector('.psx-pop') } catch (e) { rendered = false }
649
+ try {
650
+ console.info('[prompt-scheduler] button click', {
651
+ open: !!store.state.open,
652
+ rendered: rendered,
653
+ timer: store.state.timer ? store.state.timer.fireAtMs : null,
654
+ firing: engine.firing,
655
+ })
656
+ } catch (e) { /* no console */ }
657
+ var anchor = null
658
+ try {
659
+ var btnRect = event.currentTarget.getBoundingClientRect()
660
+ // Anchor above the composer CARD, not above the button: the button sits
661
+ // inside the card, so a button-anchored card would cover the draft text.
662
+ var card = event.currentTarget.closest ? event.currentTarget.closest('[data-composer-card]') : null
663
+ var cardRect = card ? card.getBoundingClientRect() : null
664
+ anchor = cardRect
665
+ ? { left: btnRect.left, top: cardRect.top, bottom: cardRect.bottom, width: btnRect.width, height: cardRect.height }
666
+ : { left: btnRect.left, top: btnRect.top, bottom: btnRect.bottom, width: btnRect.width, height: btnRect.height }
667
+ } catch (e) {
668
+ anchor = null
669
+ }
577
670
  syncTimer()
671
+ // 只开不关:关闭靠 Esc / 点外部 / 卡片右上角 ×。
672
+ // 开关式遇到"状态说开着但没渲染"就会吃掉一次点击,看起来像按钮失灵。
578
673
  patch({
579
- open: !state.open,
674
+ open: true,
580
675
  sessionId: sessionId,
581
676
  draft: draft,
582
- anchor: anchor,
677
+ // 万一取不到矩形也绝不能给 null:null 会让卡片停在视口外(left/top = -9999)。
678
+ anchor: anchor || composerAnchorRect(),
583
679
  })
584
680
  }
585
681
 
@@ -603,7 +699,9 @@ window.__ModuleLoader__.load({
603
699
  var state = useStore()
604
700
  var t = useT()
605
701
  if (!state.open) return null
606
- return h(PopoverPosition, { anchor: state.anchor },
702
+ // 锚点缺失时给一个兜底位置,卡片宁可位置不完美,也不能渲染到视口外。
703
+ var anchor = state.anchor || composerAnchorRect()
704
+ return h(PopoverPosition, { anchor: anchor },
607
705
  h('div', { className: 'psx-card', role: 'dialog', 'aria-modal': 'false', 'aria-label': t('title') }, h(ScheduleCard))
608
706
  )
609
707
  }
@@ -730,7 +828,11 @@ window.__ModuleLoader__.load({
730
828
  var armedLine = timer && !editing
731
829
  ? h('div', null,
732
830
  h('div', { className: 'psx-armed' },
733
- h(ClockIcon(14)),
831
+ // ClockIcon is a plain function that RETURNS an element: pass the result
832
+ // itself. Wrapping it again (h(ClockIcon(14))) hands React an object as
833
+ // the element type -> minified React error #130, which dsh reports as
834
+ // "slot entry crashed in 'shell.overlay'" and drops for good.
835
+ ClockIcon(14),
734
836
  h('span', null, t('armedAt').split('{time}')[0]),
735
837
  h('b', null, clockOf(timer.fireAtMs)),
736
838
  h('span', null, t('armedAt').split('{time}')[1] || '')
@@ -797,7 +899,7 @@ window.__ModuleLoader__.load({
797
899
  return h('div', { ref: cardRef },
798
900
  h('div', { className: 'psx-head' },
799
901
  h('span', { className: 'psx-title' }, t('title')),
800
- h('button', { type: 'button', className: 'psx-iconbtn', 'aria-label': t('close'), title: t('close'), onClick: closePopover }, h(CloseIcon))
902
+ h('button', { type: 'button', className: 'psx-iconbtn', 'aria-label': t('close'), title: t('close'), onClick: closePopover }, CloseIcon())
801
903
  ),
802
904
  armedLine,
803
905
  armedLine ? null : h('p', { className: 'psx-muted', style: { margin: 0 } }, t('hint')),
package/index.js CHANGED
@@ -446,14 +446,18 @@ function timerSet(body, cfg, store, events, source) {
446
446
  return { ok: true, timer: view }
447
447
  }
448
448
 
449
- /** 清掉该会话的 deadline;没有则 cleared:false。 */
449
+ /**
450
+ * 清掉该会话的 deadline;没有则 cleared:false。
451
+ * 请求可带 reason(如 'manual-send':用户提前自己发了,定时作废),只进事件日志,不影响行为。
452
+ */
450
453
  function timerClear(body, store, events, source) {
451
454
  const input = body && typeof body === 'object' && !Array.isArray(body) ? body : {}
452
455
  const sessionId = textValue(input.sessionId)
453
456
  if (!sessionId) return fail('invalid_session', 'sessionId 必须是非空字符串')
457
+ const reason = textValue(input.reason)
454
458
  const cleared = store.clearTimer(sessionId)
455
- events?.record('clear', { sessionId, source, cleared })
456
- return { ok: true, cleared }
459
+ events?.record('clear', { sessionId, source, cleared, reason })
460
+ return { ok: true, cleared, ...(reason ? { reason } : {}) }
457
461
  }
458
462
 
459
463
  /** 记下 set 用的是哪种时间选择方式,方便回看"当初排的是什么"。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-prompt-scheduler",
3
- "version": "0.5.1",
3
+ "version": "0.5.5",
4
4
  "description": "Arm a timer on the dsh send button: at the chosen time the plugin presses send for you, so the message that leaves the composer is whatever is in it then.",
5
5
  "keywords": [
6
6
  "dsh",
@@ -35,7 +35,7 @@
35
35
  "node": ">=22.19.0"
36
36
  },
37
37
  "scripts": {
38
- "check": "node --check index.js && node --check lib/deadline.js && node --check lib/events.js && node --check lib/peak.js && node --check lib/time.js && node --check client.js",
38
+ "check": "node --check index.js && node --check lib/deadline.js && node --check lib/events.js && node --check lib/peak.js && node --check lib/time.js && node --check client.js && node scripts/check-client.mjs",
39
39
  "check:manifest": "node scripts/check-manifest.mjs",
40
40
  "test": "node test/time.test.mjs && node test/peak.test.mjs && node test/host-smoke.mjs",
41
41
  "pack:dry": "npm pack --dry-run"
@@ -44,7 +44,6 @@
44
44
  "access": "public",
45
45
  "registry": "https://registry.npmjs.org/"
46
46
  },
47
-
48
47
  "dsh": {
49
48
  "bundle": {
50
49
  "patch": "./cordis.patch.yml"