import type { DeviceAction } from '@midscene/core'; import type { ZodObjectSchema, ZodRuntimeAccess } from '../types'; import { isLocateField, isZodObjectSchema, unwrapZodType } from '../types'; import { apiMetadata } from './constants'; export interface InlineStructuredFieldConfig { name: string; placeholder?: string; } export const findActionForType = ( actionSpace: DeviceAction[] | undefined, selectedType: string, ): DeviceAction | undefined => actionSpace?.find( (action) => action.interfaceAlias === selectedType || action.name === selectedType, ); /** * Compute the list of action identifiers that should be offered in the prompt * input's action dropdown. * * Inclusion rules: * - If `actionSpace` is empty/undefined, fall back to the full metadata set * so dry-mode / offline renderers still show something to pick from. * - `aiAct` is always included. It is an Agent-level API and is executed via * Playground's fallback path, so it does not need to appear in the device * interface's low-level `actionSpace`. * - `extraction` and `validation` APIs are kept even when not in the device's * `actionSpace`: they are executed against the captured UI context rather * than being dispatched to the device, so they apply universally. * - All remaining `actionSpace` entries are included verbatim (device-specific * actions surface automatically). */ export const getAvailablePromptActionTypes = ( actionSpace: DeviceAction[] | undefined, ): string[] => { const metadataMethods = Object.keys(apiMetadata); if (!actionSpace?.length) { return metadataMethods; } const availableMethods = actionSpace.map( (action) => action.interfaceAlias || action.name, ); const finalMethods = new Set(); metadataMethods.forEach((method) => { const methodInfo = apiMetadata[method as keyof typeof apiMetadata]; if (method === 'aiAct') { finalMethods.add(method); return; } if ( methodInfo?.group === 'extraction' || methodInfo?.group === 'validation' ) { finalMethods.add(method); return; } if (availableMethods.includes(method)) { finalMethods.add(method); } }); availableMethods.forEach((method) => { finalMethods.add(method); }); return Array.from(finalMethods); }; export const getInlineStructuredFieldConfig = ( actionSpace: DeviceAction[] | undefined, selectedType: string, ): InlineStructuredFieldConfig | null => { if (!actionSpace?.length || !selectedType) { return null; } const action = findActionForType(actionSpace, selectedType); if (!action?.paramSchema || !isZodObjectSchema(action.paramSchema)) { return null; } const schema = action.paramSchema as ZodObjectSchema; const shape = schema.shape || {}; const keys = Object.keys(shape); if (keys.length !== 1) { return null; } const [name] = keys; const field = shape[name]; const { actualField } = unwrapZodType(field); const isLocate = isLocateField(actualField); const fieldType = (actualField as ZodRuntimeAccess)._def?.typeName; const isInlineField = fieldType === 'ZodString' || isLocate; if (!isInlineField) { return null; } const placeholder = (actualField as ZodRuntimeAccess)._def?.description || (actualField as ZodRuntimeAccess).description || (isLocate ? 'Describe the element you want to interact with' : `Enter ${name}`); return { name, placeholder, }; }; export const shouldOffsetEmptyStateForPromptInput = ( actionSpace: DeviceAction[] | undefined, selectedType: string, ): boolean => { if (!actionSpace?.length || !selectedType) { return false; } if (getInlineStructuredFieldConfig(actionSpace, selectedType)) { return false; } const action = findActionForType(actionSpace, selectedType); if (!action?.paramSchema || !isZodObjectSchema(action.paramSchema)) { return false; } const schema = action.paramSchema as ZodObjectSchema; return Object.keys(schema.shape || {}).length > 1; };