Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(pet): restore hitbox interaction and pulse failures

hairyf1b20cd9b737b
diff --git a/src/pet/app.tsx b/src/pet/app.tsx
index 1b668843..aca32ab0 100644
--- a/src/pet/app.tsx
+++ b/src/pet/app.tsx
@@ -6,6 +6,7 @@ import { ToastProvider } from '@/components/toast-provider'
import { Pet } from './components/pet'
import { useBubble } from './hooks/use-bubble'
import { useDrag } from './hooks/use-drag'
+import { useOmitIgnoreCursorEvents } from './hooks/use-omit-ignore-cursor-events'
import { usePet } from './hooks/use-pet'
/** 拖拽方向 → 动画状态:桌面宠物在原生拖拽期间播放对应的移动动画。 */
@@ -21,6 +22,7 @@ export function App() {
const bubble = useBubble()
const dragRef = useRef<HTMLDivElement>(null)
const { clickCount, direction, dragging } = useDrag(dragRef)
+ useOmitIgnoreCursorEvents(dragRef)
const drawStatus = direction === undefined ? undefined : DRAW_STATUS[direction]
useWatch(
@@ -37,16 +39,11 @@ export function App() {
return (
<ToastProvider hideCloseButton>
- {/* pointer-events-none:原生拖拽面只在宠物本体(pet 内层 pointer-events-auto)上生效,
- 透明空白区点击保持惰性(事件经宠物节点冒泡到 dragRef 触发拖拽)。
- 点击回应不依赖 DOM click/dblclick:Windows 原生拖拽会吞掉 pointerup,
- 事件不合成——由 useDrag 按「两次按下间隔 < 500ms 且未拖拽」判定双击并用
- clickCount 上报;单击/拖拽结束均不触发。 */}
- <div
- ref={dragRef}
- className="pointer-events-none h-full w-full touch-none select-none"
- >
- <Pet status={drawStatus} dragging={dragging} clickCount={clickCount} ref={petRef} />
+ {/* 外层只负责铺满透明窗口,保持 pointer-events-none;dragRef 绑定到 Pet 内部
+ 与 dsh-pet .dsh-pet-hit 一致的唯一可交互命中区。useDrag 负责拖拽/双击,
+ useOmitIgnoreCursorEvents 负责穿透恢复,调用方无需管理鼠标流和窗口几何。 */}
+ <div className="pointer-events-none h-full w-full touch-none select-none">
+ <Pet status={drawStatus} dragging={dragging} clickCount={clickCount} hitboxRef={dragRef} ref={petRef} />
</div>
</ToastProvider>
)
diff --git a/src/pet/components/pet.tsx b/src/pet/components/pet.tsx
index 02ccfdd0..186340ab 100644
--- a/src/pet/components/pet.tsx
+++ b/src/pet/components/pet.tsx
@@ -1,4 +1,4 @@
-import type { CSSProperties, Ref, SyntheticEvent } from 'react'
+import type { CSSProperties, Ref, RefObject, SyntheticEvent } from 'react'
import type { PetHandle, PetStatus } from '../hooks/use-pet'
import { invoke } from '@tauri-apps/api/core'
import { listen } from '@tauri-apps/api/event'
@@ -26,6 +26,12 @@ const IDLE_DURATIONS = [280, 110, 110, 140, 140, 320] as const
*/
const IDLE_TURN_DELAY_MIN = 25000
const IDLE_TURN_DELAY_MAX = 50000
+/**
+ * 拖拽/点击命中区(视频筐内百分比),与 dsh-pet 的 HIT_BOX 一致
+ * (source/dsh-pet/dsh-pet/src/shared/constants.ts:8,640×360 画布坐标
+ * x0:200 y0:50 x1:440 y1:335):命中区 = 宠物身体,视频/空白区不响应事件。
+ */
+const PET_HIT_BOX = { left: '31.25%', top: '13.8888888889%', width: '37.5%', height: '79.1666666667%' } as const
const ACTIONS = {
'moving-right': { row: 1, frames: 8, duration: 120, lastDuration: 220 },
'moving-left': { row: 2, frames: 8, duration: 120, lastDuration: 220 },
@@ -56,6 +62,7 @@ interface RustPetStatus {
export interface PetProps {
ref?: Ref<PetHandle | null>
+ hitboxRef?: RefObject<HTMLDivElement | null>
status?: PetStatus
/** 原生拖拽会话进行中;内置宠物据此播放拖拽浮动动画(忽略方向)。 */
dragging?: boolean
@@ -357,13 +364,16 @@ export function Pet(props: PetProps) {
return (
<If cond={visible}>
<main className="pointer-events-none fixed inset-0 flex items-end justify-center overflow-visible" style={style}>
- <div className="pointer-events-auto relative h-[calc(var(--pet-width)*var(--pet-aspect))] w-[var(--pet-width)] select-none">
+ {/* 视频筐本身不响应事件:可交互面收缩到下方 PET_HIT_BOX 命中区(与 dsh-pet
+ .dsh-pet-hit 一致),事件从命中区冒泡到 app.tsx 的 dragRef 壳触发拖拽。 */}
+ <div className="pointer-events-none relative h-[calc(var(--pet-width)*var(--pet-aspect))] w-[var(--pet-width)] select-none">
<If cond={isBuiltin}>
{/* 双 video 缓冲:前台 opacity-100 淡入、后台 opacity-0 淡出,
- 切换经 loadeddata 就绪后交换(见开关 effect),无空窗/黑帧闪跳。 */}
+ 切换经 loadeddata 就绪后交换(见开关 effect),无空窗/黑帧闪跳。
+ 视频均 pointer-events-none,避免截获命中区外的点击。 */}
<video
ref={videoARef}
- className={`absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 0 ? 'opacity-100' : 'opacity-0'}`}
+ className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 0 ? 'opacity-100' : 'opacity-0'}`}
muted
playsInline
preload="auto"
@@ -371,7 +381,7 @@ export function Pet(props: PetProps) {
/>
<video
ref={videoBRef}
- className={`absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 1 ? 'opacity-100' : 'opacity-0'}`}
+ className={`pointer-events-none absolute inset-0 h-full w-full object-contain transition-opacity duration-200 ${frontIdx === 1 ? 'opacity-100' : 'opacity-0'}`}
muted
playsInline
preload="auto"
@@ -381,7 +391,7 @@ export function Pet(props: PetProps) {
<If cond={!isBuiltin && hasCustomAsset}>
<div
ref={spriteRef}
- className="absolute inset-0 bg-contain bg-no-repeat"
+ className="pointer-events-none absolute inset-0 bg-contain bg-no-repeat"
style={{
backgroundImage: customAsset ? `url(${customAsset.spritesheet})` : undefined,
backgroundSize: customAsset ? `${customAsset.columns * 100}% ${customAsset.rows * 100}%` : undefined,
@@ -398,8 +408,14 @@ export function Pet(props: PetProps) {
/>
</If>
<If cond={failed && assets.fallback !== undefined}>
- <img className="absolute inset-0 h-full w-full object-contain" src={assets.fallback} alt="" draggable={false} />
+ <img className="pointer-events-none absolute inset-0 h-full w-full object-contain" src={assets.fallback} alt="" draggable={false} />
</If>
+ {/* 命中区:唯一可交互面(拖拽/双击),尺寸与 dsh-pet .dsh-pet-hit 一致。 */}
+ <div
+ ref={props.hitboxRef}
+ className="pointer-events-auto absolute cursor-grab touch-none select-none"
+ style={PET_HIT_BOX}
+ />
</div>
</main>
</If>
diff --git a/src/pet/hooks/use-bubble.ts b/src/pet/hooks/use-bubble.ts
index a0b56b22..18ef7a8e 100644
--- a/src/pet/hooks/use-bubble.ts
+++ b/src/pet/hooks/use-bubble.ts
@@ -20,6 +20,10 @@ const DESCRIPTION_MAX_LENGTH = 120
/** 终态气泡自动隐藏时长(毫秒):失败展示 4s、待审阅 2.5s(对齐参考实现的完成脉冲)。实时状态(running / waiting)常驻,由后续会话事件更新内容。 */
const FAILED_BUBBLE_TIMEOUT = 4000
const REVIEW_BUBBLE_TIMEOUT = 2500
+/** 失败动画脉冲时长(毫秒):对齐参考实现 companion-reducer 的工具失败脉冲 ttlMs=1800。
+ * 失败只作为瞬态脉冲展示一次,到期自动恢复到底层会话状态,避免 DSH 快照上粘性的
+ * lastAgentError 经插件 250ms 心跳重发后永久占用 'failed'、阻塞其他会话的动画切换。 */
+const FAILED_PULSE_TTL = 1800
/** 桌宠窗口的会话气泡:DSH 发送原始会话快照,本 hook 私有管理会话→toast key 映射,仅暴露聚合宠物状态。 */
export function useBubble(): BubbleHandle {
@@ -30,6 +34,11 @@ export function useBubble(): BubbleHandle {
const toastKeys = new Map<string, string>()
const hideTimers = new Map<string, number>()
const previousStatus = new Map<string, PetStatus | undefined>()
+ /** 失败脉冲截止时间戳(参考实现 ttlMs=1800):截止前该会话贡献 'failed',到期后忽略其失败信号。 */
+ const failedUntil = new Map<string, number>()
+ const pulseTimers = new Map<string, number>()
+ /** 已消费的失败会话:脉冲播完一次后记录,粘性 lastAgentError 心跳重发 / 刷新重连不再重播。 */
+ const consumedFailed = new Set<string>()
/** 已自动隐藏的终态会话:保持隐藏直到状态离开终态,避免插件心跳重发 update 反复重建气泡。 */
const dismissed = new Set<string>()
let disposed = false
@@ -43,16 +52,65 @@ export function useBubble(): BubbleHandle {
sessions.delete(session.id)
previousStatus.delete(session.id)
dismissed.delete(session.id)
+ failedUntil.delete(session.id)
+ consumedFailed.delete(session.id)
+ clearPulseTimer(session.id)
clearHideTimer(session.id)
closeToast(session.id)
}
else {
sessions.set(session.id, session)
+ trackFailedPulse(session)
syncToast(session)
}
if (!disposed)
- setStatus(statusOf(sessions))
+ setStatus(statusOf(sessions, failedUntil, Date.now()))
+ }
+
+ /**
+ * 参考实现 companion-reducer 的失败脉冲语义:工具失败以 ttlMs=1800 的 PULSE
+ * 发射,动画播完失败片段后恢复到底层工作状态,而不是把记录钉死在 ERROR。
+ * 这里在会话状态跃迁到 failed 的瞬间启动一次脉冲计时器;插件心跳(250ms)重发
+ * 相同的失败快照不会刷新截止时间,刷新/重连后首次收到失败快照也只是重新播一次
+ * 脉冲,随后立即让位给 waiting / review / running / idle。
+ */
+ function trackFailedPulse(session: BubbleSession): void {
+ const current = sessionStatus(session)
+ const previous = previousStatus.get(session.id)
+ if (current === 'failed') {
+ // 粘性失败快照(lastAgentError 不随心跳清除):脉冲只在首次跃迁时播一次,
+ // 心跳重发 / 刷新重连后该会话的失败信号保持已消费,直到状态真正离开 failed。
+ if (previous === 'failed' || consumedFailed.has(session.id))
+ return
+ const deadline = Date.now() + FAILED_PULSE_TTL
+ failedUntil.set(session.id, deadline)
+ clearPulseTimer(session.id)
+ const timer = window.setTimeout(() => {
+ if (disposed)
+ return
+ if (failedUntil.get(session.id) !== deadline)
+ return
+ failedUntil.delete(session.id)
+ clearPulseTimer(session.id)
+ consumedFailed.add(session.id)
+ setStatus(statusOf(sessions, failedUntil, Date.now()))
+ }, FAILED_PULSE_TTL)
+ pulseTimers.set(session.id, timer)
+ }
+ else {
+ failedUntil.delete(session.id)
+ clearPulseTimer(session.id)
+ consumedFailed.delete(session.id)
+ }
+ }
+
+ function clearPulseTimer(id: string): void {
+ const timer = pulseTimers.get(id)
+ if (timer !== undefined) {
+ window.clearTimeout(timer)
+ pulseTimers.delete(id)
+ }
}
function syncToast(session: BubbleSession): void {
@@ -156,6 +214,9 @@ export function useBubble(): BubbleHandle {
for (const timer of hideTimers.values())
window.clearTimeout(timer)
hideTimers.clear()
+ for (const timer of pulseTimers.values())
+ window.clearTimeout(timer)
+ pulseTimers.clear()
for (const key of toastKeys.values())
toast.close(key)
toastKeys.clear()
@@ -222,15 +283,44 @@ function statusLabel(status: PetStatus | undefined): string {
}
}
-function statusOf(sessions: ReadonlyMap<string, BubbleSession>): PetStatus | undefined {
- const statuses = [...sessions.values()].map(sessionStatus)
- return statuses.find(value => value === 'failed')
- ?? statuses.find(value => value === 'waiting')
+/**
+ * 聚合优先级对齐参考实现 companion-reducer 的 statePriority:WAITING(60) > ERROR(50)
+ * > WORKING(30)/THINKING(20)。因此 waiting / review(等待用户)优先于 failed,
+ * 任一会话处于等待时动画必须让位给等待状态,失败不再拥有绝对优先级。
+ * failed 仅在其脉冲窗口(FAILED_PULSE_TTL)内生效,窗口过后恢复到底层状态
+ * (underlyingStatus),避免粘性 lastAgentError 永久占用聚合状态。
+ */
+function statusOf(
+ sessions: ReadonlyMap<string, BubbleSession>,
+ failedUntil: ReadonlyMap<string, number>,
+ now: number,
+): PetStatus | undefined {
+ const statuses = [...sessions.values()].map((session) => {
+ const status = sessionStatus(session)
+ if (status !== 'failed')
+ return status
+ const deadline = failedUntil.get(session.id)
+ return deadline !== undefined && now < deadline ? 'failed' : underlyingStatus(session)
+ })
+ return statuses.find(value => value === 'waiting')
?? statuses.find(value => value === 'review')
+ ?? statuses.find(value => value === 'failed')
?? statuses.find(value => value === 'running')
?? undefined
}
+/** 忽略粘性 lastAgentError / 终态 value 后该会话的底层状态:失败脉冲结束后的恢复目标(参考实现 resumeState)。 */
+function underlyingStatus(session: BubbleSession): PetStatus | undefined {
+ const value = session.status ?? session.activity ?? session.phase
+ if (value === 'review' || value === 'reviewing' || value === 'plan-review')
+ return 'review'
+ if (value === 'waiting' || value === 'pending' || value === 'blocked' || hasPendingInteraction(session.pendingInteraction) || hasPendingItems(session.pending))
+ return 'waiting'
+ if (value === 'running' || value === 'working' || value === 'thinking' || session.running === true)
+ return 'running'
+ return undefined
+}
+
function isTerminalStatus(status: PetStatus): boolean {
return status === 'failed' || status === 'review'
}
diff --git a/src/pet/hooks/use-omit-ignore-cursor-events.ts b/src/pet/hooks/use-omit-ignore-cursor-events.ts
new file mode 100644
index 00000000..bd3e4b40
--- /dev/null
+++ b/src/pet/hooks/use-omit-ignore-cursor-events.ts
@@ -0,0 +1,96 @@
+import type { RefObject } from 'react'
+import { invoke } from '@tauri-apps/api/core'
+import { listen } from '@tauri-apps/api/event'
+import { getCurrentWindow } from '@tauri-apps/api/window'
+import { useEffect } from 'react'
+
+interface DeviceMousePosition {
+ x: number
+ y: number
+}
+
+/**
+ * 根据元素的真实屏幕位置自动切换桌宠窗口的点击穿透。
+ *
+ * 穿透后 WebView 不再收到 mouseenter/mouseleave,因此通过 Rust 端 rdev
+ * 全局鼠标流获取设备像素坐标,再与元素的 DOMRect 命中区比较。调用方只需
+ * 传入可交互元素的 ref,不需要管理启动监听、窗口移动、缩放或穿透状态。
+ */
+export function useOmitIgnoreCursorEvents(elementRef: RefObject<HTMLElement | null>): void {
+ useEffect(() => {
+ const appWindow = getCurrentWindow()
+ let disposed = false
+ let isIgnored = true
+ let windowPosition: { x: number, y: number } | undefined
+ let geometryRevision = 0
+ let unlistenMouseMove: (() => void) | undefined
+ let unlistenMoved: (() => void) | undefined
+ let unlistenResized: (() => void) | undefined
+
+ function setIgnoreCursorEvents(ignore: boolean): void {
+ if (ignore === isIgnored)
+ return
+ isIgnored = ignore
+ void appWindow.setIgnoreCursorEvents(ignore).catch(() => {})
+ }
+
+ async function refreshWindowPosition(): Promise<void> {
+ const revision = ++geometryRevision
+ const position = await appWindow.innerPosition()
+ if (!disposed && revision === geometryRevision)
+ windowPosition = position
+ }
+
+ function isCursorInElement(x: number, y: number): boolean | undefined {
+ const element = elementRef.current
+ if (element === null || windowPosition === undefined)
+ return undefined
+
+ const rect = element.getBoundingClientRect()
+ const scale = globalThis.devicePixelRatio || 1
+ const left = windowPosition.x + rect.left * scale
+ const top = windowPosition.y + rect.top * scale
+ const width = rect.width * scale
+ const height = rect.height * scale
+ return x >= left && x <= left + width && y >= top && y <= top + height
+ }
+
+ // 初始整窗穿透;穿透态下交互恢复完全由全局鼠标流驱动。
+ void appWindow.setIgnoreCursorEvents(true).catch(() => {})
+ void invoke('start_pet_mouse_stream').catch(() => {})
+ void refreshWindowPosition()
+
+ const movedPromise = appWindow.onMoved(() => {
+ void refreshWindowPosition()
+ })
+ const resizedPromise = appWindow.onResized(() => {
+ void refreshWindowPosition()
+ })
+ const mouseMovePromise = listen<DeviceMousePosition>('device-mouse-move', ({ payload }) => {
+ const inElement = isCursorInElement(payload.x, payload.y)
+ if (inElement !== undefined)
+ setIgnoreCursorEvents(!inElement)
+ })
+
+ void Promise.all([movedPromise, resizedPromise, mouseMovePromise]).then(([moved, resized, mouseMove]) => {
+ if (disposed) {
+ moved()
+ resized()
+ mouseMove()
+ }
+ else {
+ unlistenMoved = moved
+ unlistenResized = resized
+ unlistenMouseMove = mouseMove
+ }
+ }).catch(() => {})
+
+ return () => {
+ disposed = true
+ geometryRevision++
+ unlistenMoved?.()
+ unlistenResized?.()
+ unlistenMouseMove?.()
+ }
+ }, [elementRef])
+}