|
| 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 fs from 'fs'; |
| 18 | + |
| 19 | +import { toolsForLoop } from './backend'; |
| 20 | +import { debug } from '../../utilsBundle'; |
| 21 | +import { Loop, z, zodToJsonSchema } from '../../mcpBundle'; |
| 22 | +import { runAction } from './actionRunner'; |
| 23 | +import { Context } from './context'; |
| 24 | + |
| 25 | +import type { Progress } from '../progress'; |
| 26 | +import type * as channels from '@protocol/channels'; |
| 27 | +import type { Page } from '../page'; |
| 28 | +import type * as loopTypes from '@lowire/loop'; |
| 29 | +import type * as actions from './actions'; |
| 30 | + |
| 31 | +export async function pagePerform(progress: Progress, page: Page, options: channels.PagePerformParams): Promise<void> { |
| 32 | + const context = new Context(progress, page); |
| 33 | + |
| 34 | + if (await cachedPerform(context, options)) |
| 35 | + return; |
| 36 | + |
| 37 | + await perform(context, options.task, zodToJsonSchema(z.object({ |
| 38 | + error: z.string().optional().describe('An error message if the task could not be completed successfully'), |
| 39 | + })) as loopTypes.Schema, options); |
| 40 | + await updateCache(context, options); |
| 41 | +} |
| 42 | + |
| 43 | +export async function pageExtract(progress: Progress, page: Page, options: channels.PageExtractParams) { |
| 44 | + const context = new Context(progress, page); |
| 45 | + const task = ` |
| 46 | +### Instructions |
| 47 | +Extract the following information from the page. Do not perform any actions, just extract the information. |
| 48 | +
|
| 49 | +### Query |
| 50 | +${options.query}`; |
| 51 | + return await perform(context, task, options.schema, options); |
| 52 | +} |
| 53 | + |
| 54 | +async function perform(context: Context, userTask: string, resultSchema: loopTypes.Schema, options: { maxTurns?: number } = {}): Promise<any> { |
| 55 | + const { progress, page } = context; |
| 56 | + const browserContext = page.browserContext; |
| 57 | + if (!browserContext._options.agent) |
| 58 | + throw new Error(`page.perform() and page.extract() require the agent to be set on the browser context`); |
| 59 | + |
| 60 | + const { full } = await page.snapshotForAI(progress); |
| 61 | + const { tools, callTool } = toolsForLoop(context); |
| 62 | + |
| 63 | + const loop = new Loop(browserContext._options.agent.provider as any, { |
| 64 | + model: browserContext._options.agent.model, |
| 65 | + summarize: true, |
| 66 | + debug, |
| 67 | + callTool, |
| 68 | + tools, |
| 69 | + ...options |
| 70 | + }); |
| 71 | + |
| 72 | + const task = `${userTask} |
| 73 | +
|
| 74 | +### Page snapshot |
| 75 | +${full} |
| 76 | +`; |
| 77 | + |
| 78 | + return await loop.run(task, { |
| 79 | + resultSchema |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +type CachedActions = Record<string, actions.Action[]>; |
| 84 | + |
| 85 | +const allCaches = new Map<string, CachedActions>(); |
| 86 | + |
| 87 | +async function cachedPerform(context: Context, options: channels.PagePerformParams): Promise<boolean> { |
| 88 | + const agentSettings = context.page.browserContext._options.agent; |
| 89 | + if (!agentSettings?.cacheFile || agentSettings.cacheMode === 'ignore') |
| 90 | + return false; |
| 91 | + |
| 92 | + const cache = await cachedActions(agentSettings.cacheFile); |
| 93 | + const cacheKey = options.key ?? options.task; |
| 94 | + const actions = cache[cacheKey]; |
| 95 | + if (!actions) { |
| 96 | + if (agentSettings.cacheMode === 'force') |
| 97 | + throw new Error(`No cached actions for key "${cacheKey}", but cache mode is set to "force"`); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + for (const action of actions) |
| 102 | + await runAction(context.progress, context.page, action); |
| 103 | + return true; |
| 104 | +} |
| 105 | + |
| 106 | +async function updateCache(context: Context, options: channels.PagePerformParams) { |
| 107 | + const cacheFile = context.page.browserContext._options.agent?.cacheFile; |
| 108 | + if (!cacheFile) |
| 109 | + return; |
| 110 | + const cache = await cachedActions(cacheFile); |
| 111 | + const cacheKey = options.key ?? options.task; |
| 112 | + cache[cacheKey] = context.actions; |
| 113 | + await fs.promises.writeFile(cacheFile, JSON.stringify(cache, undefined, 2)); |
| 114 | +} |
| 115 | + |
| 116 | +async function cachedActions(cacheFile: string): Promise<CachedActions> { |
| 117 | + let cache = allCaches.get(cacheFile); |
| 118 | + if (!cache) { |
| 119 | + const text = await fs.promises.readFile(cacheFile, 'utf-8').catch(() => '{}'); |
| 120 | + cache = JSON.parse(text) as CachedActions; |
| 121 | + allCaches.set(cacheFile, cache); |
| 122 | + } |
| 123 | + return cache; |
| 124 | +} |
0 commit comments