Skip to content

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

fix(pet): 修复 Codex 宠物拖动始终朝右 (#1140)

Hairyf06fc9619f27
diff --git a/package.json b/package.json
index 779f5196..1ef49db3 100644
--- a/package.json
+++ b/package.json
@@ -159,6 +159,7 @@
"dsh-h3": "catalog:",
"eslint": "catalog:lint",
"eslint-plugin-react-refresh": "catalog:lint",
+ "fake-indexeddb": "catalog:",
"h3": "catalog:",
"immer": "catalog:testing",
"jsdom": "catalog:testing",
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 00000000..3c000480
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,23 @@
+diff --git a/dist/index.mjs b/dist/index.mjs
+index dc6e0ff2768245e817b06d6493060573a0277a02..7ac738d28242256bcc2e178ecd6f95659d01aa25 100644
+--- a/dist/index.mjs
++++ b/dist/index.mjs
+@@ -2722,7 +2722,8 @@ function CodexPet(props) {
+ fallbackSize: 231
+ });
+ const height = width * (cellHeight / cellWidth);
+- const renderMotion = dragging ? state.type === "moving-left" || state.type === "moving-right" ? state.type : "moving-right" : state.type;
++ const dragMotion = typeof motion === "string" ? motion : motion?.type;
++ const renderMotion = dragging ? dragMotion === "moving-left" || dragMotion === "moving-right" ? dragMotion : "moving-right" : state.type;
+ const frame = useMemo(() => resolveCodexFrame(renderMotion, config), [config, renderMotion]);
+ const src = useCachedMediaUrl(resolveSpritesheetUrl(uri, configSource, config), cache, onError);
+ useEffect(() => {
+@@ -3494,7 +3495,7 @@ function Pet(props) {
+ }
+ for (const id of [...fired.keys()]) if (!bubbles.some((bubble) => bubble.id === id)) fired.delete(id);
+ }, [bubbles, motionRequest]);
+- const effectiveMotion = bubbleMotion ?? motion;
++ const effectiveMotion = resolvedKind === "codex" && common.dragging ? motion : bubbleMotion ?? motion;
+ const mutteringSuspended = bubbles.some((bubble) => bubble.loading);
+ const { width } = useElementSize(shellRef);
+ const plan = useMemo(() => resolveMutteringPlan({
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 303689ee..b894c465 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -68,6 +68,9 @@ catalogs:
dsh-h3:
specifier: 0.2.1
version: 0.2.1
+ fake-indexeddb:
+ specifier: ^6.2.6
+ version: 6.2.6
h3:
specifier: 2.0.1
version: 2.0.1
@@ -487,6 +490,7 @@ catalogs:
patchedDependencies:
'@tauri-apps/[email protected]': 6bbec7f8a8e969a867d222e8b2b542d2dbd639ffbaf2184a4df47f330b6ad431
bumpp: ea42dd1273f65a02e22aa9c25e39361c8dac2320ed2fbff9c58d2bd3f46a40a3
+ [email protected]: bc78110c62c900ae95f521e415f4c491503e89d69d81406def275b0566cfdfee
importers:
@@ -533,7 +537,7 @@ importers:
version: 2.4.4
dsh-pet-component:
specifier: catalog:runtime
+ version: 0.2.5(patch_hash=bc78110c62c900ae95f521e415f4c491503e89d69d81406def275b0566cfdfee)([email protected])([email protected])([email protected])
expo:
specifier: catalog:native
@@ -850,6 +854,9 @@ importers:
eslint-plugin-react-refresh:
specifier: catalog:lint
+ fake-indexeddb:
+ specifier: 'catalog:'
+ version: 6.2.6
h3:
specifier: 'catalog:'
version: 2.0.1
@@ -6580,6 +6587,10 @@ packages:
engines: {node: '>= 10.17.0'}
hasBin: true
+ resolution: {integrity: sha512-ihhmr0KoET51jT8R4YhbUYuLLvr1qqO6INISygtN16i8loR8kAxtAUr4+EKHTSMjL+lggaAn4OC7DRqzrNgnuQ==}
+ engines: {node: '>=18'}
+
resolution: {integrity: sha512-bCK/2Z4zLidyB4ReuIsvALH6w31YfAQDmXMqMx6FyfHqvBxtjC0eRumeSu4Bs3XtXwpyIywtSTrVT99BxY1f9w==}
@@ -14576,7 +14587,7 @@ snapshots:
- crossws
- ocache
+ [email protected](patch_hash=bc78110c62c900ae95f521e415f4c491503e89d69d81406def275b0566cfdfee)([email protected])([email protected])([email protected]):
dependencies:
'@gravity-ui/icons': 2.21.0([email protected])
'@reause/core': 0.1.7([email protected])
@@ -15454,6 +15465,8 @@ snapshots:
transitivePeerDependencies:
- supports-color
+
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 2b4f7af0..fd01f7f9 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -112,6 +112,7 @@ packages:
patchedDependencies:
'@tauri-apps/[email protected]': patches/@[email protected]
bumpp: patches/bumpp.patch
catalog:
'@choochmeque/tauri-plugin-notifications-api': 0.5.0-rc.14
@@ -123,6 +124,7 @@ catalog:
'@tauri-apps/plugin-os': ^2.3.2
'@tauri-apps/plugin-store': ^2.4.4
dsh-h3: 0.2.1
+ fake-indexeddb: ^6.2.6
h3: 2.0.1
react-native-lucide: npm:[email protected]
valtio: ^2.3.2
diff --git a/src/hooks/use-window-draggable.test.ts b/src/hooks/use-window-draggable.test.ts
index 7cb5db9c..0a2be596 100644
--- a/src/hooks/use-window-draggable.test.ts
+++ b/src/hooks/use-window-draggable.test.ts
@@ -226,12 +226,11 @@ describe('useWindowDraggable', () => {
expect(result.current.dragging).toBe(true)
})
- it('方向按相邻样本的水平位移判定,拖拽停顿时归零', async () => {
+ it('方向按累计水平位移判定,拖拽停顿时归零', async () => {
const { result } = renderHook(() => useDraggable())
await startDrag(result.current, 12)
- // 越过门槛的那一帧没有上一个样本,方向先不判定(与上游轨迹语义一致)。
- expect(result.current.direction).toBeUndefined()
+ expect(result.current.direction).toBe('right')
emitCursor(CURSOR.x + 16, CURSOR.y)
expect(result.current.direction).toBe('right')
@@ -249,6 +248,34 @@ describe('useWindowDraggable', () => {
expect(result.current.direction).toBe('right')
})
+ it.each([1, 2])('缩放=%s 时慢速拖动累计水平位移后更新方向,反向也能识别', async (scale) => {
+ vi.stubGlobal('devicePixelRatio', scale)
+ const { result } = renderHook(() => useDraggable())
+
+ await startDrag(result.current, 0, 12 * scale)
+ expect(result.current.direction).toBeUndefined()
+
+ for (let step = 1; step < 3 * scale; step += 1) {
+ emitCursor(CURSOR.x - step, CURSOR.y + 12 * scale)
+ expect(result.current.direction).toBeUndefined()
+ }
+ emitCursor(CURSOR.x - 3 * scale, CURSOR.y + 12 * scale)
+ expect(result.current.direction).toBe('left')
+
+ for (let step = 1; step < 3 * scale; step += 1) {
+ emitCursor(CURSOR.x - 3 * scale + step, CURSOR.y + 12 * scale)
+ expect(result.current.direction).toBe('left')
+ }
+ emitCursor(CURSOR.x, CURSOR.y + 12 * scale)
+ expect(result.current.direction).toBe('right')
+ })
+
+ it('首个有效拖动样本向左时立即朝左', async () => {
+ const { result } = renderHook(() => useDraggable())
+ await startDrag(result.current, -12)
+ expect(result.current.direction).toBe('left')
+ })
+
it('pointerup 收尾,且只认同一个 pointerId', async () => {
const { result } = renderHook(() => useDraggable())
diff --git a/src/hooks/use-window-draggable.ts b/src/hooks/use-window-draggable.ts
index 13f0590b..c84ca5cc 100644
--- a/src/hooks/use-window-draggable.ts
+++ b/src/hooks/use-window-draggable.ts
@@ -105,8 +105,7 @@ interface Gesture {
vy: number
/** 是否越过手势门槛(门槛前窗口不动,位移由门槛后的弹簧按原点补齐)。 */
engaged: boolean
- /** 方向判定用的上一个样本(上游拿轨迹末段比较)。 */
- lastSample: { x: number, y: number } | undefined
+ directionAnchor: { x: number, y: number } | undefined
}
/**
@@ -298,7 +297,7 @@ export function useWindowDraggable(options: UseWindowDraggableOptions): UseWindo
vx: 0,
vy: 0,
engaged: false,
- lastSample: undefined,
+ directionAnchor: latest === undefined ? undefined : { x: latest.x, y: latest.y },
}
gestureRef.current = gesture
// 按下即开始兜底计时:设备流与指针事件同时中断时也能收尾。
@@ -332,7 +331,7 @@ export function useWindowDraggable(options: UseWindowDraggableOptions): UseWindo
if (gesture.anchor === undefined) {
// 设备流首个样本:手势原点顺延到这里(按下时还没有样本,等价于从此刻起跟手)。
gesture.anchor = { x: payload.x, y: payload.y }
- gesture.lastSample = { x: payload.x, y: payload.y }
+ gesture.directionAnchor = { x: payload.x, y: payload.y }
return
}
const scale = globalThis.devicePixelRatio || 1
@@ -351,10 +350,11 @@ export function useWindowDraggable(options: UseWindowDraggableOptions): UseWindo
setDragging(true)
rafRef.current?.resume()
}
- const last = gesture.lastSample
- if (last !== undefined && Math.abs(payload.x - last.x) >= DRAG_DIRECTION_THRESHOLD * scale)
+ const last = gesture.directionAnchor
+ if (last !== undefined && Math.abs(payload.x - last.x) >= DRAG_DIRECTION_THRESHOLD * scale) {
setDirection(payload.x > last.x ? 'right' : 'left')
- gesture.lastSample = { x: payload.x, y: payload.y }
+ gesture.directionAnchor = { x: payload.x, y: payload.y }
+ }
armDirectionTimer()
})
diff --git a/src/pet/app.dragging.test.tsx b/src/pet/app.dragging.test.tsx
new file mode 100644
index 00000000..333cddd8
--- /dev/null
+++ b/src/pet/app.dragging.test.tsx
@@ -0,0 +1,107 @@
+// @vitest-environment jsdom
+import type { PetRef } from 'dsh-pet-component'
+import { act, cleanup, render } from '@testing-library/react'
+import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { App } from './app'
+
+const state = vi.hoisted(() => ({
+ drag: { dragging: false, direction: undefined as 'left' | 'right' | undefined },
+ pet: null as PetRef | null,
+}))
+
+vi.mock('@gravity-ui/icons', () => ({
+ CircleCheck: () => null,
+ CircleExclamation: () => null,
+ CircleInfo: () => null,
+ TriangleExclamation: () => null,
+}))
+vi.mock('@/ui/pet/hint', () => ({ Hint: () => null }))
+vi.mock('@/hooks/use-window-draggable', () => ({
+ useWindowDraggable: () => ({ ...state.drag, onPointerDown: vi.fn(), onPointerUp: vi.fn() }),
+}))
+vi.mock('@/hooks/use-omit-ignore-cursor-events', () => ({ useOmitIgnoreCursorEvents: vi.fn() }))
+vi.mock('../hooks/use-wakelock-release', () => ({ useWakelockRelease: vi.fn() }))
+vi.mock('./hooks/use-pet-source', () => ({
+ usePetSource: () => ({
+ source: { kind: 'codex', config: { spriteVersionNumber: 2 }, uri: 'data:image/png;base64,AA==', cache: false },
+ error: null,
+ }),
+}))
+vi.mock('./hooks/use-pet-status', () => ({
+ usePetStatus: () => ({ active_pet: 'codex:test', enabled: true, visible: true, pet_size: 100 }),
+ normalizeSizePercent: (value: number) => value,
+}))
+vi.mock('./hooks/use-pet-physics', () => ({ usePetPhysics: () => ({ onFling: vi.fn(), onGrab: vi.fn(), onRelease: vi.fn() }) }))
+vi.mock('./hooks/use-pet-window-clamp', () => ({
+ usePetWindowClamp: () => ({ clampPosition: (position: unknown) => position, refreshWorkAreas: vi.fn() }),
+}))
+vi.mock('./hooks/use-pet-window', () => ({ usePetWindowSize: vi.fn() }))
+vi.mock('./hooks/use-bubble-tracker', () => ({
+ useBubbleTracker: (pet: PetRef) => {
+ state.pet = pet
+ },
+}))
+
+beforeEach(() => {
+ vi.useFakeTimers()
+ state.drag = { dragging: false, direction: undefined }
+ state.pet = null
+ vi.stubGlobal('indexedDB', new IDBFactory())
+ vi.stubGlobal('IDBKeyRange', IDBKeyRange)
+ vi.stubGlobal('matchMedia', vi.fn(() => ({
+ matches: false,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ addListener: vi.fn(),
+ removeListener: vi.fn(),
+ })))
+ vi.stubGlobal('ResizeObserver', class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ })
+})
+
+afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+})
+
+describe('codex pet drag motion', () => {
+ it('会话气泡存续时拖动方向优先,松手后恢复会话动画', () => {
+ const view = render(<App />)
+ const sprite = view.container.querySelector('.dsh-pet[data-motion]')!
+ act(() => {
+ state.pet!.bubble({ id: 'session', title: '会话', motion: 'working', timeout: 0 })
+ vi.advanceTimersByTime(150)
+ })
+ expect(sprite.getAttribute('data-motion')).toBe('working')
+
+ state.drag = { dragging: true, direction: 'left' }
+ view.rerender(<App />)
+ expect(sprite.getAttribute('data-motion')).toBe('moving-left')
+
+ state.drag = { dragging: true, direction: 'right' }
+ view.rerender(<App />)
+ expect(sprite.getAttribute('data-motion')).toBe('moving-right')
+
+ state.drag = { dragging: false, direction: undefined }
+ view.rerender(<App />)
+ expect(sprite.getAttribute('data-motion')).toBe('working')
+ })
+
+ it('拖动期间新到达的限时气泡动画不能覆盖方向', () => {
+ state.drag = { dragging: true, direction: 'left' }
+ const view = render(<App />)
+ const sprite = view.container.querySelector('.dsh-pet[data-motion]')!
+ expect(sprite.getAttribute('data-motion')).toBe('moving-left')
+
+ act(() => {
+ state.pet!.bubble({ id: 'done', title: '完成', motion: 'success', timeout: 2000 })
+ })
+ expect(sprite.getAttribute('data-motion')).toBe('moving-left')
+ })
+})
diff --git a/test/tooling-config.test.ts b/test/tooling-config.test.ts
index f1c916d1..ba9bc456 100644
--- a/test/tooling-config.test.ts
+++ b/test/tooling-config.test.ts
@@ -51,7 +51,7 @@ describe('tooling configuration contracts', () => {
it('each project retains its distinct setup, scheduling and timeout envelope', () => {
expect(unitConfig.test).toEqual({
name: 'unit',
- server: { deps: { inline: ['@deepseek-ai/dsh-client-ui-primitives'] } },
+ server: { deps: { inline: ['@deepseek-ai/dsh-client-ui-primitives', 'dsh-pet-component'] } },
include: ['packages/**/*.{test,spec}.{ts,tsx,js,mjs,cjs}', 'test/**/*.test.ts', 'src/**/*.test.{ts,tsx}'],
exclude: [
'**/node_modules/**',
diff --git a/vitest.unit.config.ts b/vitest.unit.config.ts
index 8cbd8f31..c6fdc916 100644
--- a/vitest.unit.config.ts
+++ b/vitest.unit.config.ts
@@ -29,7 +29,7 @@ export default defineProject({
test: {
name: 'unit',
server: {
- deps: { inline: ['@deepseek-ai/dsh-client-ui-primitives'] },
+ deps: { inline: ['@deepseek-ai/dsh-client-ui-primitives', 'dsh-pet-component'] },
},
include: [
'packages/**/*.{test,spec}.{ts,tsx,js,mjs,cjs}',