Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 输入框拖拽 worklet 的辅助函数提前声明,修复 Android release 拖拽即闪退 (#4048)
fico-hubf56f33a015f1
diff --git a/apps/mobile/src/__tests__/composerResizeWorklet.test.ts b/apps/mobile/src/__tests__/composerResizeWorklet.test.tsnew file mode 100644index 0000000000..0abb2a5cec--- /dev/null+++ b/apps/mobile/src/__tests__/composerResizeWorklet.test.ts@@ -0,0 +1,77 @@+/**+ * #4039 回归:输入框拖拽的 UI 线程 worklet 在 Android release 包里崩溃+ * (`undefined is not a function at applyComposerResizeDrag`)。+ *+ * 根因不在逻辑而在**声明顺序**:react-native-worklets 的 babel 插件把每个 `'worklet'`+ * 函数改写成「模块求值时立即执行的工厂」,并在那一刻把它引用的模块内标识符抓进+ * `__closure`。`applyComposerResizeDrag` 若写在它依赖的 `normalizeBounds` / `clamp`+ * 之前,抓到的就是两个尚未赋值的 `var`(undefined);UI 线程按 `__closure` 取值调用即崩。+ * vitest 走 esbuild、不跑该插件,普通单测永远发现不了 —— 本测试用仓库实际的 babel+ * preset + worklets 插件把源码真转译一遍并求值,断言闭包里拿到的是函数。+ */+import { describe, expect, it } from 'vitest';+import { transformFileSync, type TransformOptions } from '@babel/core';+import Module, { createRequire } from 'node:module';+import path from 'node:path';++const require = createRequire(import.meta.url);+const sourcePath = path.resolve(__dirname, '..', 'session', 'composerResize.ts');++interface WorkletLike {+ (...args: unknown[]): unknown;+ __closure?: Record<string, unknown>;+ __workletHash?: number;+}++function loadWorkletTransformedModule(): Record<string, unknown> {+ const out = transformFileSync(sourcePath, {+ filename: sourcePath,+ babelrc: false,+ configFile: false,+ presets: [[require.resolve('babel-preset-expo'), {}]],+ plugins: [require.resolve('react-native-worklets/plugin')],+ // Metro 的 caller 带 platform 字段(babel-preset-expo 据此选平台分支),类型定义里没有。+ caller: {+ name: 'metro',+ supportsStaticESM: false,+ platform: 'android',+ } as unknown as TransformOptions['caller'],+ envName: 'production',+ });+ if (!out?.code) throw new Error('babel produced no output');+ const compiledPath = sourcePath.replace(/\.ts$/, '.js');+ const mod = new Module(compiledPath);+ const nodeModulePaths = (+ Module as unknown as { _nodeModulePaths: (from: string) => string[] }+ )._nodeModulePaths;+ mod.paths = nodeModulePaths(path.dirname(sourcePath));+ (mod as unknown as { _compile: (code: string, filename: string) => void })._compile(+ out.code,+ compiledPath,+ );+ return mod.exports as Record<string, unknown>;+}++describe('composerResize worklets(经 react-native-worklets 插件转译后)', () => {+ it('applyComposerResizeDrag 的 __closure 在模块求值时已拿到辅助函数,UI 线程可调用', () => {+ const exported = loadWorkletTransformedModule();+ const worklet = exported.applyComposerResizeDrag as WorkletLike;+ expect(typeof worklet).toBe('function');+ expect(typeof worklet.__workletHash).toBe('number');+ const closure = worklet.__closure ?? {};+ // 本次回归只关乎两个辅助函数的声明顺序:它们必须以函数身份进入闭包。+ expect(typeof closure.normalizeBounds, '__closure.normalizeBounds').toBe('function');+ expect(typeof closure.clamp, '__closure.clamp').toBe('function');+ // 更一般的约束是「抓进闭包的标识符不能是尚未赋值的 var」;worklet 合法捕获常量 /+ // 对象在仓库里有先例,所以只拒绝 undefined,不限定类型。+ for (const [name, value] of Object.entries(closure)) {+ expect(value, `__closure.${name}`).not.toBeUndefined();+ }+ // 转译后的 JS 线程副本与 UI 线程副本共用同一份闭包捕获:直接调用等价于 UI 线程调用。+ expect(worklet({+ startContentHeight: 100,+ translationY: -20,+ bounds: { minContentHeight: 20, maxContentHeight: 300 },+ })).toBe(120);+ });+});diff --git a/apps/mobile/src/session/composerResize.ts b/apps/mobile/src/session/composerResize.tsindex 92e78630e3..c8a4ea0b05 100644--- a/apps/mobile/src/session/composerResize.ts+++ b/apps/mobile/src/session/composerResize.ts@@ -47,6 +47,25 @@ export const COMPOSER_RESIZE_AUTO_SNAP_THRESHOLD = 24;/** manual 模式上边界之上保留的屏幕空间(顶部导航 + 至少一部分消息内容可见)。 */export const COMPOSER_RESIZE_TOP_RESERVED_HEIGHT = 220;+// ⚠️ 声明顺序即正确性(#4039):react-native-worklets 插件把每个 `'worklet'` 函数改写成+// 模块求值时立即执行的工厂,并在那一刻把它引用的模块内标识符抓进 `__closure`。+// 被其它 worklet 引用的辅助函数必须写在引用者**之前**,否则抓到的是尚未赋值的 `var`+// (undefined),UI 线程一调用就是 `undefined is not a function`(Android release 崩溃)。+// vitest 不跑该插件,靠 composerResizeWorklet.test.ts 用真实转译守住这条约束。+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,14 +314,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 +322,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);-}