Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): initialize UI worklet helper closures
guyong83e82bbc4ffb
diff --git a/apps/mobile/src/__tests__/workletProductionTransform.test.ts b/apps/mobile/src/__tests__/workletProductionTransform.test.tsnew file mode 100644index 0000000000..ce2eb44d39--- /dev/null+++ b/apps/mobile/src/__tests__/workletProductionTransform.test.ts@@ -0,0 +1,72 @@+import { createRequire } from 'node:module';+import { resolve } from 'node:path';+import { describe, expect, it } from 'vitest';++type BabelTransformResult = { code?: string | null };+type Babel = {+ transformFileSync: (filename: string, options: Record<string, unknown>) => BabelTransformResult;+};+type WorkletFunction = ((input: unknown) => unknown) & {+ __closure?: Record<string, unknown>;+};++const require = createRequire(import.meta.url);+const babel = require('@babel/core') as Babel;+const expoPreset = require('babel-preset-expo');++/** Evaluate a pure model module after the same transform used by Android release bundles. */+function loadProductionModule(relativePath: string): Record<string, unknown> {+ const filename = resolve(process.cwd(), relativePath);+ const transformed = babel.transformFileSync(filename, {+ babelrc: false,+ configFile: false,+ envName: 'production',+ filename,+ presets: [[expoPreset, { platform: 'android' }]],+ });+ if (!transformed.code) throw new Error(`Babel produced no code for ${relativePath}`);+ const moduleExports: Record<string, unknown> = {};+ const evaluate = Function('exports', transformed.code) as (exports: Record<string, unknown>) => void;+ evaluate(moduleExports);+ return moduleExports;+}++function worklet(moduleExports: Record<string, unknown>, name: string): WorkletFunction {+ const value = moduleExports[name];+ expect(typeof value).toBe('function');+ return value as WorkletFunction;+}++describe('Android production worklet transform', () => {+ it('initializes composer resize helper closures before the UI gesture calls them', () => {+ const moduleExports = loadProductionModule('src/session/composerResize.ts');+ const applyComposerResizeDrag = worklet(moduleExports, 'applyComposerResizeDrag');++ expect(typeof applyComposerResizeDrag.__closure?.normalizeBounds).toBe('function');+ expect(typeof applyComposerResizeDrag.__closure?.clamp).toBe('function');+ expect(applyComposerResizeDrag({+ bounds: { minContentHeight: 20, maxContentHeight: 200 },+ startContentHeight: 40,+ translationY: -10,+ })).toBe(50);+ });++ it('initializes context sheet helper closures before UI drag and settle worklets call them', () => {+ const moduleExports = loadProductionModule('src/session/contextSheetModel.ts');+ const applyContextSheetDrag = worklet(moduleExports, 'applyContextSheetDrag');+ const settleContextSheetDrag = worklet(moduleExports, 'settleContextSheetDrag');++ expect(typeof applyContextSheetDrag.__closure?.normalizeHeights).toBe('function');+ expect(typeof applyContextSheetDrag.__closure?.clamp).toBe('function');+ expect(typeof settleContextSheetDrag.__closure?.normalizeHeights).toBe('function');+ expect(applyContextSheetDrag({+ heights: { half: 320, full: 700 },+ startHeight: 320,+ translationY: -100,+ })).toBe(420);+ expect(settleContextSheetDrag({+ heights: { half: 320, full: 700 },+ draggedHeight: 600,+ })).toBe('full');+ });+});diff --git a/apps/mobile/src/session/composerResize.ts b/apps/mobile/src/session/composerResize.tsindex 92e78630e3..70d99c1eda 100644--- a/apps/mobile/src/session/composerResize.ts+++ b/apps/mobile/src/session/composerResize.ts@@ -47,6 +47,24 @@ export const COMPOSER_RESIZE_AUTO_SNAP_THRESHOLD = 24;/** manual 模式上边界之上保留的屏幕空间(顶部导航 + 至少一部分消息内容可见)。 */export const COMPOSER_RESIZE_TOP_RESERVED_HEIGHT = 220;+// Worklets' Babel transform lowers function declarations with a `worklet`+// directive into initialization expressions. Keep helpers before every+// worklet that captures them, otherwise the caller's closure is initialized+// with `undefined` during module evaluation.+function normalizeBounds(bounds: ComposerResizeBounds): ComposerResizeBounds {+ 'worklet';+ const minContentHeight = Math.max(1, Math.round(bounds.minContentHeight));+ return {+ minContentHeight,+ maxContentHeight: Math.max(minContentHeight, Math.round(bounds.maxContentHeight)),+ };+}++function clamp(value: number, min: number, max: number): number {+ 'worklet';+ return Math.min(Math.max(value, min), max);+}+export interface ResolveComposerInputHeightInput {/** TextInput 上报的内容高度(页面 state)。 */contentHeight: number;@@ -295,15 +313,6 @@ export function computeComposerResizeBounds(};}-function normalizeBounds(bounds: ComposerResizeBounds): ComposerResizeBounds {- 'worklet';- const minContentHeight = Math.max(1, Math.round(bounds.minContentHeight));- return {- minContentHeight,- maxContentHeight: Math.max(minContentHeight, Math.round(bounds.maxContentHeight)),- };-}-function normalizePositiveDimension(value: number, fallback: number): number {return Number.isFinite(value) && value > 0 ? value : fallback;}@@ -311,8 +320,3 @@ function normalizePositiveDimension(value: number, fallback: number): number {function normalizeNonNegativeDimension(value: number, fallback: number): number {return Number.isFinite(value) && value >= 0 ? value : fallback;}--function clamp(value: number, min: number, max: number): number {- 'worklet';- return Math.min(Math.max(value, min), max);-}diff --git a/apps/mobile/src/session/contextSheetModel.ts b/apps/mobile/src/session/contextSheetModel.tsindex 3e14abdcd5..1fccd0bd9b 100644--- a/apps/mobile/src/session/contextSheetModel.ts+++ b/apps/mobile/src/session/contextSheetModel.ts@@ -27,6 +27,20 @@ const FULL_TOP_RESERVED_HEIGHT = 64;/** 松手高度低于 half 档的该比例时判定为「想关掉」。 */export const CONTEXT_SHEET_DISMISS_RATIO = 0.62;+// Keep worklet helpers before their callers. The Worklets Babel transform+// materializes worklet function declarations in source order; a later helper+// would otherwise be captured as `undefined` during module initialization.+function normalizeHeights(heights: ContextSheetSnapHeights): ContextSheetSnapHeights {+ 'worklet';+ const half = Math.max(1, Math.round(heights.half));+ return { half, full: Math.max(half, Math.round(heights.full)) };+}++function clamp(value: number, min: number, max: number): number {+ 'worklet';+ return Math.min(Math.max(value, min), max);+}+export interface ComputeContextSheetSnapHeightsInput {/** 窗口高度(useWindowDimensions().height)。 */screenHeight: number;@@ -83,12 +97,6 @@ export function settleContextSheetDrag(input: SettleContextSheetDragInput): Contreturn 'half';}-function normalizeHeights(heights: ContextSheetSnapHeights): ContextSheetSnapHeights {- 'worklet';- const half = Math.max(1, Math.round(heights.half));- return { half, full: Math.max(half, Math.round(heights.full)) };-}-function normalizePositiveDimension(value: number | undefined, fallback: number): number {return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : fallback;}@@ -96,8 +104,3 @@ function normalizePositiveDimension(value: number | undefined, fallback: number)function normalizeNonNegativeDimension(value: number | undefined, fallback: number): number {return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : fallback;}--function clamp(value: number, min: number, max: number): number {- 'worklet';- return Math.min(Math.max(value, min), max);-}