import { TaskBuilder, locatePlanForLocate } from '@/agent/task-builder'; import type { TaskCache } from '@/agent/task-cache'; import { getMidsceneLocationSchema } from '@/ai-model'; import { getModelRuntime } from '@/ai-model/models'; import { AbstractInterface, defineActionSleep } from '@/device'; import { getMidsceneDistanceSchema } from '@/distance-schema'; import type Service from '@/service'; import type { DeviceAction, PlanningAction } from '@/types'; import { afterEach, describe, expect, it, rs } from '@rstest/core'; import { z } from 'zod'; class MockInterface extends AbstractInterface { interfaceType = 'mock'; override cacheFeatureForPoint: AbstractInterface['cacheFeatureForPoint'] = undefined; override rectMatchesCacheFeature: AbstractInterface['rectMatchesCacheFeature'] = undefined; override destroy: AbstractInterface['destroy'] = undefined; override describe: AbstractInterface['describe'] = undefined; override beforeInvokeAction: AbstractInterface['beforeInvokeAction'] = undefined; override afterInvokeAction: AbstractInterface['afterInvokeAction'] = undefined; override getUITree: AbstractInterface['getUITree'] = undefined; override getElementsNodeTree: AbstractInterface['getElementsNodeTree'] = undefined; override url: AbstractInterface['url'] = undefined; override evaluateJavaScript: AbstractInterface['evaluateJavaScript'] = undefined; constructor(private readonly actions: DeviceAction[]) { super(); } async screenshotBase64(): Promise { return 'mock'; } async size(): Promise<{ width: number; height: number }> { return { width: 0, height: 0 }; } actionSpace(): DeviceAction[] { return this.actions; } } describe('TaskBuilder', () => { const mockModelRuntime = getModelRuntime({ modelName: 'mock-model', modelDescription: 'mock-model-description', intent: 'default', slot: 'default', }); afterEach(() => { rs.useRealTimers(); }); it.each([ { hasCoordinates: true, cacheable: false }, { hasCoordinates: false, cacheable: false }, { hasCoordinates: true, cacheable: undefined }, { hasCoordinates: false, cacheable: undefined }, ])( 'honors locate caching independently of the planner: %j', async ({ hasCoordinates, cacheable }) => { const feature = { xpaths: ['/button'] }; const taskCache = { isCacheResultUsed: true, matchLocateCache: rs.fn(), updateOrAppendCacheRecord: rs.fn(), }; const device = new MockInterface([]); const extractFeature = rs.fn().mockResolvedValue(feature); const matchFeature = rs.fn(); device.cacheFeatureForPoint = extractFeature; device.rectMatchesCacheFeature = matchFeature; const locate = rs.fn().mockResolvedValue({ element: { center: [20, 20], description: 'target' }, }); const builder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: device, service: { locate } as unknown as Service, taskCache: taskCache as unknown as TaskCache, actionSpace: [], }); // A standalone locate may cache even if the configured planner cannot. const planningModel = getModelRuntime({ ...mockModelRuntime.config, modelFamily: 'auto-glm', }); const prompt = hasCoordinates ? '' : 'target'; const { tasks } = await builder.build( [ locatePlanForLocate({ prompt, cacheable: true, ...(hasCoordinates ? { locatedPixelResult: { center: [20, 20] } } : {}), }), ], planningModel, mockModelRuntime, { cacheable }, ); await tasks[0].executor({ task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1, deprecatedDpr: 1 }, } as any); expect(locate).toHaveBeenCalledTimes(hasCoordinates ? 0 : 1); expect(matchFeature).not.toHaveBeenCalled(); if (cacheable === false) { expect(taskCache.matchLocateCache).not.toHaveBeenCalled(); expect(extractFeature).not.toHaveBeenCalled(); expect(taskCache.updateOrAppendCacheRecord).not.toHaveBeenCalled(); } else { expect(taskCache.matchLocateCache).toHaveBeenCalledWith(prompt); expect(extractFeature).toHaveBeenCalledOnce(); expect(taskCache.updateOrAppendCacheRecord).toHaveBeenCalledWith( { type: 'locate', prompt, cache: feature }, undefined, ); } }, ); it('normalizes the deprecated locate deepThink alias before task reporting', () => { const plan = locatePlanForLocate({ prompt: 'cart icon', deepThink: true, } as any); expect(plan.param).toEqual({ prompt: 'cart icon', deepLocate: true, }); expect(plan.param).not.toHaveProperty('deepThink'); }); it.each([ { prompt: 'target icon', inheritImages: true }, { prompt: { prompt: 'target icon' }, inheritImages: true }, { prompt: { prompt: 'target icon', images: [{ name: 'local', url: 'https://example.com/local.png' }], convertHttpImage2Base64: false, }, inheritImages: false, }, ])( 'preserves locator prompts while inheriting reference images: %j', async ({ prompt, inheritImages }) => { const mockInterface = new MockInterface([]); const locate = rs.fn().mockResolvedValue({ element: { center: [50, 50], rect: { left: 40, top: 40, width: 20, height: 20 }, description: 'target icon', }, }); const taskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: mockInterface, service: { locate } as unknown as Service, actionSpace: [], }); const multimodalPrompt = { images: [ { name: 'reference', url: 'https://example.com/reference.png' }, ], convertHttpImage2Base64: true, }; const { tasks } = await taskBuilder.build( [locatePlanForLocate({ prompt })], mockModelRuntime, mockModelRuntime, { multimodalPrompt }, ); await tasks[0].executor({ task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1, deprecatedDpr: 1 }, } as any); expect(locate).toHaveBeenCalledWith( { prompt: inheritImages ? { prompt: 'target icon', ...multimodalPrompt } : prompt, }, expect.any(Object), mockModelRuntime, undefined, ); }, ); it.each([false, true])( 'reuses planner coordinates with or without a dedicated Locate model: %s', async (separateLocate) => { const locateRuntime = getModelRuntime({ ...mockModelRuntime.config, intent: 'locate', slot: separateLocate ? 'locate' : 'default', modelName: 'locator', }); const locate = rs.fn().mockResolvedValue({ element: { center: [80, 80], description: 'button' }, }); const builder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: new MockInterface([]), service: { locate } as unknown as Service, actionSpace: [], }); const { tasks } = await builder.build( [ locatePlanForLocate({ prompt: 'button', locatedPixelResult: { center: [20, 20] }, } as any), ], mockModelRuntime, locateRuntime, ); const result = await tasks[0].executor({ task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1 }, } as any); expect(locate).not.toHaveBeenCalled(); expect((result as any).output.element.center).toEqual([20, 20]); }, ); it('accepts coordinate-only planning with an explicit Locate model', async () => { const locate = rs.fn(); const runtime = getModelRuntime({ ...mockModelRuntime.config, intent: 'locate', slot: 'locate', }); const builder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: new MockInterface([]), service: { locate } as unknown as Service, actionSpace: [], }); const { tasks } = await builder.build( [ locatePlanForLocate({ locatedPixelResult: { center: [20, 20] }, } as any), ], mockModelRuntime, runtime, ); await expect( tasks[0].executor({ task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1 }, } as any), ).resolves.toMatchObject({ output: { element: { center: [20, 20] } } }); expect(locate).not.toHaveBeenCalled(); }); it('dispatches plans using handler registry', async () => { const actionSchema = z.object({ locate: getMidsceneLocationSchema().describe('element to locate'), }); const mockAction: DeviceAction = { name: 'Tap', description: 'mock tap action', paramSchema: actionSchema, planningFeedbackMaxLength: 123, call: rs.fn(), }; const mockInterface = new MockInterface([mockAction, defineActionSleep()]); const insightService = { contextRetrieverFn: rs.fn(), locate: rs.fn(), } as unknown as Service; const taskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: mockInterface, service: insightService, actionSpace: mockInterface.actionSpace(), }); const plans: PlanningAction[] = [ { type: 'Locate', thought: 'find element', param: { prompt: 'first' }, }, { type: 'Finished', thought: 'all done', param: null, }, { type: 'Sleep', thought: 'take a break', param: { timeMs: 100 }, }, { type: 'Tap', thought: 'tap element', param: { locate: { prompt: 'button' } }, }, ]; const { tasks } = await taskBuilder.build( plans, mockModelRuntime, mockModelRuntime, ); expect(tasks.map((task) => [task.type, task.subType])).toEqual([ ['Planning', 'Locate'], ['Action Space', 'Finished'], ['Action Space', 'Sleep'], ['Planning', 'Locate'], ['Action Space', 'Tap'], ]); expect(tasks.at(-1)?.planningFeedbackMaxLength).toBe(123); }); it('uses promptDisplay for the located element passed to an action', async () => { const actionSchema = z.object({ locate: getMidsceneLocationSchema().describe('element to locate'), }); const mockAction: DeviceAction = { name: 'Tap', description: 'mock tap action', paramSchema: actionSchema, call: rs.fn(), }; const mockInterface = new MockInterface([mockAction]); const locateDump = { taskInfo: { durationMs: 1 }, matchedElement: [ { center: [50, 50], rect: { left: 40, top: 40, width: 20, height: 20 }, description: 'favorite fruit ruleorange', }, ], }; const insightService = { contextRetrieverFn: rs.fn(), locate: rs.fn(async () => ({ element: { center: [50, 50], rect: { left: 40, top: 40, width: 20, height: 20 }, description: 'favorite fruit ruleorange', }, dump: locateDump, })), } as unknown as Service; const taskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: mockInterface, service: insightService, actionSpace: mockInterface.actionSpace(), }); const plans: PlanningAction[] = [ { type: 'Tap', param: { locate: { prompt: 'favorite fruit ruleorange', promptDisplay: 'orange', context: 'favorite fruit rule', }, }, }, ]; const { tasks } = await taskBuilder.build( plans, mockModelRuntime, mockModelRuntime, ); const locateTask = tasks[0]; await locateTask.executor({ task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1, deprecatedDpr: 1, }, } as any); expect((plans[0].param as any).locate.description).toBe('orange'); expect(locateDump.matchedElement[0].description).toBe('orange'); }); it('throws when building an executable task for an action outside actionSpace', async () => { const mockInterface = new MockInterface([defineActionSleep()]); const insightService = { contextRetrieverFn: rs.fn(), locate: rs.fn(), } as unknown as Service; const taskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: mockInterface, service: insightService, actionSpace: mockInterface.actionSpace(), }); await expect( taskBuilder.build( [{ type: 'Tap', thought: 'tap missing action', param: {} }], mockModelRuntime, mockModelRuntime, ), ).rejects.toThrow( /Action type 'Tap' is not in the current action space. Available actions: Sleep/, ); }); it('supports fast-path action delays for system actions', async () => { rs.useFakeTimers(); const defaultBeforeHook = rs.fn(async () => undefined); const defaultAfterHook = rs.fn(async () => undefined); const defaultActionCall = rs.fn(async () => undefined); const defaultAction: DeviceAction = { name: 'DefaultExit', description: 'default exit action', call: defaultActionCall, }; const defaultInterface = new MockInterface([defaultAction]); defaultInterface.beforeInvokeAction = defaultBeforeHook; defaultInterface.afterInvokeAction = defaultAfterHook; const fastBeforeHook = rs.fn(async () => undefined); const fastAfterHook = rs.fn(async () => undefined); const fastActionCall = rs.fn(async () => undefined); const fastAction: DeviceAction = { name: 'FastExit', description: 'fast exit action', delayBeforeRunner: 0, delayAfterRunner: 0, call: fastActionCall, }; const fastInterface = new MockInterface([fastAction]); fastInterface.beforeInvokeAction = fastBeforeHook; fastInterface.afterInvokeAction = fastAfterHook; const insightService = { contextRetrieverFn: rs.fn(), locate: rs.fn(), } as unknown as Service; const defaultTaskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: defaultInterface, service: insightService, actionSpace: defaultInterface.actionSpace(), }); const fastTaskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: fastInterface, service: insightService, actionSpace: fastInterface.actionSpace(), }); const { tasks: defaultTasks } = await defaultTaskBuilder.build( [{ type: 'DefaultExit', thought: '', param: {} }], mockModelRuntime, mockModelRuntime, ); const { tasks: fastTasks } = await fastTaskBuilder.build( [{ type: 'FastExit', thought: '', param: {} }], mockModelRuntime, mockModelRuntime, ); const defaultTask = defaultTasks[0]; const fastTask = fastTasks[0]; const taskContext = { task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1 }, } as any; const defaultPromise = defaultTask.executor(taskContext); await rs.advanceTimersByTimeAsync(199); expect(defaultBeforeHook).toHaveBeenCalledTimes(1); expect(defaultActionCall).not.toHaveBeenCalled(); expect(defaultAfterHook).not.toHaveBeenCalled(); await rs.advanceTimersByTimeAsync(1); expect(defaultActionCall).toHaveBeenCalledTimes(1); expect(defaultAfterHook).not.toHaveBeenCalled(); await rs.advanceTimersByTimeAsync(299); expect(defaultAfterHook).not.toHaveBeenCalled(); await rs.advanceTimersByTimeAsync(1); await expect(defaultPromise).resolves.toEqual({ output: undefined }); expect(defaultAfterHook).toHaveBeenCalledTimes(1); const fastPromise = fastTask.executor(taskContext); await expect(fastPromise).resolves.toEqual({ output: undefined }); expect(fastBeforeHook).toHaveBeenCalledTimes(1); expect(fastActionCall).toHaveBeenCalledTimes(1); expect(fastAfterHook).toHaveBeenCalledTimes(1); }); it('allows actions to attach planning feedback to the running task', async () => { const actionCall = rs.fn(async () => '0\n'); const readStateAction: DeviceAction<{ key: string }, string> = { name: 'ReadState', description: 'read state', delayBeforeRunner: 0, delayAfterRunner: 0, call: async (param, context) => { const output = await actionCall(); if (!context?.task) { throw new Error('executor context task is required'); } context.task.planningFeedback = `ReadState returned ${param.key}: ${output}`; return output; }, }; const mockInterface = new MockInterface([readStateAction]); const insightService = { contextRetrieverFn: rs.fn(), locate: rs.fn(), } as unknown as Service; const taskBuilder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: mockInterface, service: insightService, actionSpace: mockInterface.actionSpace(), }); const { tasks } = await taskBuilder.build( [ { type: 'ReadState', thought: 'read brightness state', param: { key: 'brightness' }, }, ], mockModelRuntime, mockModelRuntime, ); const taskContext = { task: { timing: {} }, uiContext: { shrunkShotToLogicalRatio: 1 }, } as any; const result = await tasks[0].executor(taskContext); expect(result).toEqual({ output: '0\n', }); expect(taskContext.task.planningFeedback).toBe( 'ReadState returned brightness: 0\n', ); }); it.each([ { distanceUnit: 'model-input-image-pixel' as const, distance: 120, expected: 80, }, { distanceUnit: 'device-logical' as const, distance: 120, expected: 120 }, { distanceUnit: 'model-input-image-pixel' as const, distance: undefined, expected: 100, }, ])( 'preserves the distance unit and device-unit defaults: %j', async ({ distanceUnit, distance, expected }) => { const call = rs.fn(); const action: DeviceAction = { name: 'CustomMove', paramSchema: z.object({ distance: getMidsceneDistanceSchema().default(100), duration: z.number().default(500), }), delayBeforeRunner: 0, delayAfterRunner: 0, call, }; const builder = new TaskBuilder({ getDefaultModelRuntime: () => mockModelRuntime, interfaceInstance: new MockInterface([action]), service: {} as Service, actionSpace: [action], }); const { tasks } = await builder.build( [{ type: 'CustomMove', param: { distance } }], mockModelRuntime, mockModelRuntime, { distanceUnit }, ); await tasks[0].executor({ task: { timing: {} }, uiContext: { shotSize: { width: 600, height: 1200 }, shrunkShotToLogicalRatio: 1.5, }, } as any); expect(call.mock.calls[0][0]).toEqual({ distance: expected, duration: 500, }); }, ); });