import { z } from 'zod';
import { handleAlert, getAlertText } from '../executor/wda.js';
import { getOrCreateSession } from '../utils/wda-session.js';
export const wdaAlertSchema = z.object({
action: z
.enum(['accept', 'dismiss'])
.describe('Action to take on the alert: accept or dismiss'),
bundle_id: z
.string()
.optional()
.describe('Bundle ID of app to activate for this session'),
port: z.number().optional().describe('WDA server port (default: 8100)'),
});
export type WdaAlertInput = z.infer<typeof wdaAlertSchema>;
export const wdaAlertTool = {
name: 'wda_alert',
description:
'Accept or dismiss a system alert dialog. Use this to handle permission prompts, confirmations, and other system alerts.',
inputSchema: wdaAlertSchema,
handler: async (input: WdaAlertInput) => {
const options = { port: input.port };
const sessionId = await getOrCreateSession(input.bundle_id, options);
// Try to get alert text first for better feedback
let alertText: string | null = null;
try {
alertText = await getAlertText(sessionId, options);
} catch {
// Alert might not exist or text unavailable
}
await handleAlert(sessionId, input.action, options);
const actionText = input.action === 'accept' ? 'Accepted' : 'Dismissed';
const message = alertText
? `${actionText} alert: "${alertText}"`
: `${actionText} alert`;
return {
content: [
{
type: 'text' as const,
text: message,
},
],
};
},
};