|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { toolsForLoop } from './backend'; |
| 18 | +import { debug } from '../../utilsBundle'; |
| 19 | +import { Loop } from '../../mcpBundle'; |
| 20 | + |
| 21 | +import type { Progress } from '../progress'; |
| 22 | +import type * as channels from '@protocol/channels'; |
| 23 | +import type { Page } from '../page'; |
| 24 | +import type * as loopTypes from '@lowire/loop'; |
| 25 | + |
| 26 | +export async function pagePerform(progress: Progress, page: Page, options: channels.PagePerformParams): Promise<void> { |
| 27 | + const resultSchema = { |
| 28 | + type: 'object' as const, |
| 29 | + properties: { |
| 30 | + actions: { type: 'array' as const, items: { type: 'object' as const } }, |
| 31 | + }, |
| 32 | + required: ['actions'] |
| 33 | + }; |
| 34 | + const task = ` |
| 35 | +### Instructions |
| 36 | +- Perform the following actions on the page. |
| 37 | +- Return performed actions. |
| 38 | +
|
| 39 | +### Task |
| 40 | +${options.task} |
| 41 | + `; |
| 42 | + const actions = await perform(progress, page, task, resultSchema, options); |
| 43 | + // eslint-disable-next-line no-console |
| 44 | + console.log(actions); |
| 45 | +} |
| 46 | + |
| 47 | +export async function pageExtract(progress: Progress, page: Page, options: channels.PageExtractParams) { |
| 48 | + const task = ` |
| 49 | +### Instructions |
| 50 | +Extract the following information from the page. Do not perform any actions, just extract the information. |
| 51 | +
|
| 52 | +### Query |
| 53 | +${options.query}`; |
| 54 | + return await perform(progress, page, task, options.schema, options); |
| 55 | +} |
| 56 | + |
| 57 | +async function perform(progress: Progress, page: Page, userTask: string, resultSchema: loopTypes.Schema, options: { maxTurns?: number } = {}): Promise<any> { |
| 58 | + const context = page.browserContext; |
| 59 | + if (!context._options.agent) |
| 60 | + throw new Error(`page.perform() and page.extract() require the agent to be set on the browser context`); |
| 61 | + |
| 62 | + const { full } = await page.snapshotForAI(progress); |
| 63 | + const { tools, callTool } = toolsForLoop(page); |
| 64 | + |
| 65 | + const loop = new Loop(context._options.agent.provider as any, { |
| 66 | + model: context._options.agent.model, |
| 67 | + summarize: true, |
| 68 | + debug, |
| 69 | + callTool, |
| 70 | + tools, |
| 71 | + ...options |
| 72 | + }); |
| 73 | + |
| 74 | + const task = `${userTask} |
| 75 | +
|
| 76 | +### Page snapshot |
| 77 | +${full} |
| 78 | +`; |
| 79 | + |
| 80 | + return await loop.run(task, { |
| 81 | + resultSchema |
| 82 | + }); |
| 83 | +} |
0 commit comments