Skip to content

Commit

Permalink
feat: implement the 'sleep' action (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyutaotao authored Aug 7, 2024
1 parent 292c183 commit 41058a0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
Empty file.
5 changes: 4 additions & 1 deletion packages/midscene/src/automation/planning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ export function systemPromptToTaskPlanning(query: string) {
* param: { scrollType: 'ScrollUntilBottom', 'ScrollUntilTop', 'ScrollDown', 'ScrollUp' }
* type: 'Error'
* param: { message: string }, the error message
* type: 'Sleep'
* param: { timeMs: number }, wait for timeMs milliseconds
Here is an example of how to decompose a task.
When a user says 'Input "Weather in Shanghai" into the search bar, hit enter', by viewing the page screenshot and description, you my decompose this task into something like this:
When a user says 'Input "Weather in Shanghai" into the search bar, wait 1 second, hit enter', by viewing the page screenshot and description, you my decompose this task into something like this:
* Find: 'The search bar'
* Input: 'Weather in Shanghai'
* Sleep: 1000
* KeyboardPress: 'Enter'
Remember:
Expand Down
7 changes: 6 additions & 1 deletion packages/midscene/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export interface PlanningAction<ParamType = any> {
| 'KeyboardPress'
| 'Scroll'
| 'Error'
| 'Assert';
| 'Assert'
| 'Sleep';
param: ParamType;
}

Expand All @@ -203,6 +204,10 @@ export interface PlanningActionParamAssert {
assertion: string;
}

export interface PlanningActionParamSleep {
timeMs: number;
}

/**
* misc
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`automation - planning > basic run 1`] = `
{
"timeMs": 3500,
}
`;

exports[`automation - planning > basic run 2`] = `
{
"value": "Enter",
}
`;
9 changes: 6 additions & 3 deletions packages/midscene/tests/automation/planning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ describe('automation - planning', () => {
const { context } = await getPageDataOfTestName('todo');

const { plans } = await plan(
'type "Why is the earth a sphere?", hit Enter',
'type "Why is the earth a sphere?", wait 3.5s, hit Enter',
{
context,
},
);
expect(plans.length).toBe(3);
expect(plans.length).toBe(4);
expect(plans[0].thought).toBeTruthy();
expect(plans[0].type).toBe('Locate');
expect(plans[1].type).toBe('Input');
expect(plans[2].type).toBe('KeyboardPress');
expect(plans[2].type).toBe('Sleep');
expect(plans[2].param).toMatchSnapshot();
expect(plans[3].type).toBe('KeyboardPress');
expect(plans[3].param).toMatchSnapshot();
});

it('should raise an error when prompt is irrelevant with page', async () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/web-integration/src/common/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Insight, {
type PlanningActionParamHover,
type PlanningActionParamInputOrKeyPress,
type PlanningActionParamScroll,
type PlanningActionParamSleep,
type PlanningActionParamTap,
} from '@midscene/core';
import { base64Encoded } from '@midscene/core/image';
Expand Down Expand Up @@ -191,7 +192,6 @@ export class PageTaskExecutor {
};
return taskActionInput;
}

if (plan.type === 'KeyboardPress') {
const taskActionKeyboardPress: ExecutionTaskActionApply<PlanningActionParamInputOrKeyPress> =
{
Expand Down Expand Up @@ -271,6 +271,19 @@ export class PageTaskExecutor {
};
return taskActionScroll;
}
if (plan.type === 'Sleep') {
const taskActionSleep: ExecutionTaskActionApply<PlanningActionParamSleep> =
{
type: 'Action',
subType: 'Sleep',
param: plan.param,
executor: async (taskParam) => {
assert(taskParam.timeMs, 'No time to sleep');
await sleep(taskParam.timeMs);
},
};
return taskActionSleep;
}
if (plan.type === 'Error') {
throw new Error(`Got a task plan with type Error: ${plan.thought}`);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/web-integration/tests/puppeteer/showcase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ describe('puppeteer integration', () => {
const mid = new PuppeteerAgent(page);

// perform a search
await mid.aiAction('type "Headphones" in search box, hit Enter');
await sleep(5000);
await mid.aiAction('type "Headphones" in search box, hit Enter, wait 5s');

// find the items
const items = await mid.aiQuery(
Expand Down

0 comments on commit 41058a0

Please sign in to comment.