|
| 1 | +import { z } from 'zod'; |
| 2 | +import { ToolResponse } from '../../../types/common.ts'; |
| 3 | +import { log } from '../../../utils/logging/index.ts'; |
| 4 | +import { CommandExecutor, getDefaultCommandExecutor } from '../../../utils/execution/index.ts'; |
| 5 | +import { createTypedTool } from '../../../utils/typed-tool-factory.ts'; |
| 6 | + |
| 7 | +const eraseSimsBaseSchema = z.object({ |
| 8 | + simulatorUuid: z.string().optional().describe('UUID of the simulator to erase.'), |
| 9 | + all: z.boolean().optional().describe('When true, erases all simulators.'), |
| 10 | +}); |
| 11 | + |
| 12 | +const eraseSimsSchema = eraseSimsBaseSchema.refine( |
| 13 | + (v) => { |
| 14 | + const selectors = (v.simulatorUuid ? 1 : 0) + (v.all === true ? 1 : 0); |
| 15 | + return selectors === 1; |
| 16 | + }, |
| 17 | + { message: 'Provide exactly one of: simulatorUuid OR all=true.' }, |
| 18 | +); |
| 19 | + |
| 20 | +type EraseSimsParams = z.infer<typeof eraseSimsSchema>; |
| 21 | + |
| 22 | +async function eraseSingle(udid: string, executor: CommandExecutor): Promise<ToolResponse> { |
| 23 | + const result = await executor( |
| 24 | + ['xcrun', 'simctl', 'erase', udid], |
| 25 | + 'Erase Simulator', |
| 26 | + true, |
| 27 | + undefined, |
| 28 | + ); |
| 29 | + if (result.success) { |
| 30 | + return { content: [{ type: 'text', text: `Successfully erased simulator ${udid}` }] }; |
| 31 | + } |
| 32 | + return { |
| 33 | + content: [ |
| 34 | + { type: 'text', text: `Failed to erase simulator: ${result.error ?? 'Unknown error'}` }, |
| 35 | + ], |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +export async function erase_simsLogic( |
| 40 | + params: EraseSimsParams, |
| 41 | + executor: CommandExecutor, |
| 42 | +): Promise<ToolResponse> { |
| 43 | + try { |
| 44 | + if (params.simulatorUuid) { |
| 45 | + log('info', `Erasing simulator ${params.simulatorUuid}`); |
| 46 | + return await eraseSingle(params.simulatorUuid, executor); |
| 47 | + } |
| 48 | + |
| 49 | + if (params.all === true) { |
| 50 | + log('info', 'Erasing ALL simulators'); |
| 51 | + const result = await executor( |
| 52 | + ['xcrun', 'simctl', 'erase', 'all'], |
| 53 | + 'Erase All Simulators', |
| 54 | + true, |
| 55 | + undefined, |
| 56 | + ); |
| 57 | + if (!result.success) { |
| 58 | + return { |
| 59 | + content: [ |
| 60 | + { |
| 61 | + type: 'text', |
| 62 | + text: `Failed to erase all simulators: ${result.error ?? 'Unknown error'}`, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }; |
| 66 | + } |
| 67 | + return { content: [{ type: 'text', text: 'Successfully erased all simulators' }] }; |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + content: [{ type: 'text', text: 'Invalid parameters: provide simulatorUuid or all=true.' }], |
| 72 | + }; |
| 73 | + } catch (error: unknown) { |
| 74 | + const message = error instanceof Error ? error.message : String(error); |
| 75 | + log('error', `Error erasing simulators: ${message}`); |
| 76 | + return { content: [{ type: 'text', text: `Failed to erase simulators: ${message}` }] }; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export default { |
| 81 | + name: 'erase_sims', |
| 82 | + description: |
| 83 | + 'Erases simulator content and settings. Provide exactly one of: simulatorUuid or all=true.', |
| 84 | + schema: eraseSimsBaseSchema.shape, |
| 85 | + handler: createTypedTool(eraseSimsSchema, erase_simsLogic, getDefaultCommandExecutor), |
| 86 | +}; |
0 commit comments