豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,20 @@ export class McpContext implements Context {
}
const node = this.#textSnapshot?.idToNode.get(uid);
if (!node) {
throw new Error('No such element found in the snapshot');
throw new Error('No such element found in the snapshot.');
}
const handle = await node.elementHandle();
if (!handle) {
throw new Error('No such element found in the snapshot');
const message = `Element with uid ${uid} no longer exists on the page.`;
try {
const handle = await node.elementHandle();
if (!handle) {
throw new Error(message);
}
return handle;
} catch (error) {
throw new Error(message, {
cause: error,
});
}
return handle;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/tools/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {logger} from '../logger.js';
import type {McpContext, TextSnapshotNode} from '../McpContext.js';
import {zod} from '../third_party/index.js';
import type {ElementHandle} from '../third_party/index.js';
Expand All @@ -22,6 +23,16 @@ const includeSnapshotSchema = zod
.optional()
.describe('Whether to include a snapshot in the response. Default is false.');

function handleActionError(error: unknown, uid: string) {
logger('failed to act using a locator', error);
throw new Error(
`Failed to interact with the element with uid ${uid}. The element did not become interactive within the configured timeout.`,
{
cause: error,
},
);
}

export const click = defineTool({
name: 'click',
description: `Clicks on the provided element`,
Expand Down Expand Up @@ -55,6 +66,8 @@ export const click = defineTool({
if (request.params.includeSnapshot) {
response.includeSnapshot();
}
} catch (error) {
handleActionError(error, uid);
} finally {
void handle.dispose();
}
Expand Down Expand Up @@ -119,6 +132,8 @@ export const hover = defineTool({
if (request.params.includeSnapshot) {
response.includeSnapshot();
}
} catch (error) {
handleActionError(error, uid);
} finally {
void handle.dispose();
}
Expand Down Expand Up @@ -180,6 +195,8 @@ async function fillFormElement(
value.length * timeoutPerChar;
await handle.asLocator().setTimeout(fillTimeout).fill(value);
}
} catch (error) {
handleActionError(error, uid);
} finally {
void handle.dispose();
}
Expand Down