Skip to content

Commit bce74c7

Browse files
committed
option handle seperately
1 parent 6111f8d commit bce74c7

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

src/McpContext.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {WaitForHelper} from './WaitForHelper.js';
3030
export interface TextSnapshotNode extends SerializedAXNode {
3131
id: string;
3232
children: TextSnapshotNode[];
33+
value?: string | number;
3334
}
3435

3536
export interface TextSnapshot {
@@ -314,19 +315,36 @@ export class McpContext implements Context {
314315
// will be used for the tree serialization and mapping ids back to nodes.
315316
let idCounter = 0;
316317
const idToNode = new Map<string, TextSnapshotNode>();
317-
const assignIds = (node: SerializedAXNode): TextSnapshotNode => {
318+
const assignIds = async (
319+
node: SerializedAXNode,
320+
): Promise<TextSnapshotNode> => {
318321
const nodeWithId: TextSnapshotNode = {
319322
...node,
320323
id: `${snapshotId}_${idCounter++}`,
321-
children: node.children
322-
? node.children.map(child => assignIds(child))
323-
: [],
324+
children: [],
324325
};
326+
327+
// The AXNode for an option doesn't contain its value, so add it from the element.
328+
if (node.role === 'option') {
329+
const handle = await node.elementHandle();
330+
if (handle) {
331+
const valueHandle = await handle.getProperty('value');
332+
const optionValue = await valueHandle.jsonValue();
333+
if (optionValue) {
334+
nodeWithId.value = optionValue.toString();
335+
}
336+
}
337+
}
338+
339+
nodeWithId.children = node.children
340+
? await Promise.all(node.children.map(child => assignIds(child)))
341+
: [];
342+
325343
idToNode.set(nodeWithId.id, nodeWithId);
326344
return nodeWithId;
327345
};
328346

329-
const rootNodeWithId = assignIds(rootNode);
347+
const rootNodeWithId = await assignIds(rootNode);
330348
this.#textSnapshot = {
331349
root: rootNodeWithId,
332350
snapshotId: String(snapshotId),

0 commit comments

Comments
 (0)