Skip to content

Commit 186a03a

Browse files
committed
refactor: Mark tool errors with isError flag for consistent error detection and handling throughout the codebase
1 parent f19eb9c commit 186a03a

19 files changed

Lines changed: 48 additions & 8 deletions

src/recording/step-recorder.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export function withRecording(toolName: string, callback: ToolCallback): ToolCal
4949
return async (params, extra) => {
5050
const start = Date.now();
5151
const result = await callback(params, extra);
52-
const isError = result.content.some(
53-
(c: any) => c.type === 'text' && typeof (c as any).text === 'string' && (c as any).text.startsWith('Error'),
54-
);
52+
const isError = (result as any).isError === true;
5553
appendStep(
5654
toolName,
5755
params as Record<string, unknown>,

src/tools/app-actions.tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const getAppStateTool: ToolCallback = async (args: {
4444
};
4545
} catch (e) {
4646
return {
47+
isError: true,
4748
content: [{ type: 'text', text: `Error getting app state: ${e}` }],
4849
};
4950
}

src/tools/app-session.tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export const startAppTool: ToolCallback = async (args: {
228228
};
229229
} catch (e) {
230230
return {
231+
isError: true,
231232
content: [{ type: 'text', text: `Error starting app session: ${e}` }],
232233
};
233234
}

src/tools/attach-browser.tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export const attachBrowserTool: ToolCallback = async ({
9898
};
9999
} catch (e) {
100100
return {
101+
isError: true,
101102
content: [{ type: 'text', text: `Error attaching to browser: ${e}` }],
102103
};
103104
}

src/tools/browser.tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export const closeSessionTool: ToolCallback = async (args: { detach?: boolean }
258258
};
259259
} catch (e) {
260260
return {
261+
isError: true,
261262
content: [{ type: 'text', text: `Error closing session: ${e}` }],
262263
};
263264
}

src/tools/click.tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const clickAction = async (selector: string, timeout: number, scrollToView = tru
2929
};
3030
} catch (e) {
3131
return {
32+
isError: true,
3233
content: [{ type: 'text', text: `Error clicking element: ${e}` }],
3334
};
3435
}

src/tools/context.tool.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const getContextsTool: ToolCallback = async (): Promise<CallToolResult> =
4848
};
4949
} catch (e) {
5050
return {
51+
isError: true,
5152
content: [{ type: 'text', text: `Error getting contexts: ${e}` }],
5253
};
5354
}
@@ -65,6 +66,7 @@ export const getCurrentContextTool: ToolCallback = async (): Promise<CallToolRes
6566
};
6667
} catch (e) {
6768
return {
69+
isError: true,
6870
content: [{ type: 'text', text: `Error getting current context: ${e}` }],
6971
};
7072
}
@@ -103,6 +105,7 @@ export const switchContextTool: ToolCallback = async (args: {
103105
};
104106
} catch (e) {
105107
return {
108+
isError: true,
106109
content: [{ type: 'text', text: `Error switching context: ${e}` }],
107110
};
108111
}

src/tools/cookies.tool.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const getCookiesTool: ToolCallback = async ({ name}: { name?: string }):
4343

4444
} catch (e) {
4545
return {
46+
isError: true,
4647
content: [{ type: 'text', text: `Error getting cookies: ${e}` }],
4748
};
4849
}
@@ -86,6 +87,7 @@ export const setCookieTool: ToolCallback = async ({
8687
};
8788
} catch (e) {
8889
return {
90+
isError: true,
8991
content: [{ type: 'text', text: `Error setting cookie: ${e}` }],
9092
};
9193
}
@@ -119,6 +121,7 @@ export const deleteCookiesTool: ToolCallback = async ({ name}: { name?: string }
119121

120122
} catch (e) {
121123
return {
124+
isError: true,
122125
content: [{ type: 'text', text: `Error deleting cookies: ${e}` }],
123126
};
124127
}

src/tools/device.tool.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const rotateDeviceTool: ToolCallback = async (args: {
5151
};
5252
} catch (e) {
5353
return {
54+
isError: true,
5455
content: [{ type: 'text', text: `Error rotating device: ${e}` }],
5556
};
5657
}
@@ -68,6 +69,7 @@ export const hideKeyboardTool: ToolCallback = async (): Promise<CallToolResult>
6869
};
6970
} catch (e) {
7071
return {
72+
isError: true,
7173
content: [{ type: 'text', text: `Error hiding keyboard: ${e}` }],
7274
};
7375
}
@@ -90,6 +92,7 @@ export const getGeolocationTool: ToolCallback = async (): Promise<CallToolResult
9092
};
9193
} catch (e) {
9294
return {
95+
isError: true,
9396
content: [{ type: 'text', text: `Error getting geolocation: ${e}` }],
9497
};
9598
}
@@ -117,6 +120,7 @@ export const setGeolocationTool: ToolCallback = async (args: {
117120
};
118121
} catch (e) {
119122
return {
123+
isError: true,
120124
content: [{ type: 'text', text: `Error setting geolocation: ${e}` }],
121125
};
122126
}

src/tools/emulate-device.tool.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ export const emulateDeviceTool: ToolCallback = async ({
4141
// Guard: mobile sessions
4242
if (metadata?.type === 'ios' || metadata?.type === 'android') {
4343
return {
44+
isError: true,
4445
content: [{ type: 'text', text: 'Error: emulate_device is only supported for web browser sessions, not iOS/Android.' }],
4546
};
4647
}
4748

4849
// Guard: BiDi required
4950
if (!browser.isBidi) {
5051
return {
52+
isError: true,
5153
content: [{
5254
type: 'text',
5355
text: 'Error: emulate_device requires a BiDi-enabled session.\nRestart the browser with: start_browser({ capabilities: { webSocketUrl: true } })',
@@ -68,7 +70,7 @@ export const emulateDeviceTool: ToolCallback = async ({
6870
content: [{ type: 'text', text: `Available devices (${names.length}):\n${names.join('\n')}` }],
6971
};
7072
}
71-
return { content: [{ type: 'text', text: `Error listing devices: ${e}` }] };
73+
return { isError: true, content: [{ type: 'text', text: `Error listing devices: ${e}` }] };
7274
}
7375
return { content: [{ type: 'text', text: 'Could not retrieve device list.' }] };
7476
}
@@ -101,10 +103,11 @@ export const emulateDeviceTool: ToolCallback = async ({
101103
}],
102104
};
103105
}
104-
return { content: [{ type: 'text', text: `Error: ${e}` }] };
106+
return { isError: true, content: [{ type: 'text', text: `Error: ${e}` }] };
105107
}
106108
} catch (e) {
107109
return {
110+
isError: true,
108111
content: [{ type: 'text', text: `Error: ${e}` }],
109112
};
110113
}

0 commit comments

Comments
 (0)