import { z } from 'zod';
import { simctl } from '../executor/simctl.js';
import { resolveDevice } from '../utils/device-resolver.js';
export const shutdownSchema = z.object({
device: z
.string()
.describe('Device UDID, name, or "all" to shutdown all simulators'),
});
export type ShutdownInput = z.infer<typeof shutdownSchema>;
export const shutdownTool = {
name: 'simctl_shutdown',
description: 'Shutdown a simulator device or all simulators',
inputSchema: shutdownSchema,
handler: async (input: ShutdownInput) => {
const device = input.device.toLowerCase() === 'all'
? 'all'
: resolveDevice(input.device);
const result = simctl('shutdown', [device]);
if (result.exitCode !== 0) {
// "Unable to shutdown device in current state: Shutdown" is not an error
if (result.stderr.includes('current state: Shutdown')) {
return {
content: [
{
type: 'text' as const,
text: `Device ${input.device} is already shutdown`,
},
],
};
}
throw new Error(`Failed to shutdown: ${result.stderr || result.stdout}`);
}
return {
content: [
{
type: 'text' as const,
text: device === 'all'
? 'Successfully shutdown all simulators'
: `Successfully shutdown device ${input.device}`,
},
],
};
},
};