-
Notifications
You must be signed in to change notification settings - Fork 944
Expand file tree
/
Copy pathplayground-utils.ts
More file actions
158 lines (141 loc) · 4.93 KB
/
playground-utils.ts
File metadata and controls
158 lines (141 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { UIContext } from '@midscene/core';
import { StaticPage, StaticPageAgent } from '@midscene/web/static';
import type { ZodObjectSchema } from '../types';
import { isZodObjectSchema, unwrapZodType } from '../types';
// Get action name based on type
export const actionNameForType = (type: string) => {
if (!type) return '';
// Remove 'ai' prefix and convert camelCase to space-separated words
const typeWithoutAi = type.startsWith('ai') ? type.slice(2) : type;
// Special handling for iOS-specific actions to preserve their full names
if (typeWithoutAi.startsWith('IOS')) {
// For IOS actions, keep IOS as a unit and add spaces before remaining capital letters
return typeWithoutAi
.substring(3)
.replace(/([A-Z])/g, ' $1')
.replace(/^/, 'IOS')
.trim();
}
const fullName = typeWithoutAi.replace(/([A-Z])/g, ' $1').trim();
// For long names, keep the last 3 words to make them shorter
const words = fullName.split(' ');
const result = words.length > 3 ? words.slice(-3).join(' ') : fullName;
// Capitalize the first letter of each word for consistent display
return result.replace(/\b\w/g, (c) => c.toUpperCase());
};
// Create static agent from context
export const staticAgentFromContext = (context: UIContext) => {
const page = new StaticPage(context);
return new StaticPageAgent(page);
};
// Get placeholder text based on run type
export const getPlaceholderForType = (type: string): string => {
if (type === 'aiQuery') {
return 'What do you want to query?';
}
if (type === 'aiAssert') {
return 'What do you want to assert?';
}
if (type === 'aiTap') {
return 'What element do you want to tap?';
}
if (type === 'aiDoubleClick') {
return 'What element do you want to double-click?';
}
if (type === 'aiHover') {
return 'What element do you want to hover over?';
}
if (type === 'aiInput') {
return 'Format: <value> | <element>\nExample: hello world | search box';
}
if (type === 'aiRightClick') {
return 'What element do you want to right-click?';
}
if (type === 'aiKeyboardPress') {
return 'Format: <key> | <element (optional)>\nExample: Enter | text field';
}
if (type === 'aiScroll') {
return 'Format: <direction> <amount> | <element (optional)>\nExample: down 500 | main content';
}
if (type === 'aiLocate') {
return 'What element do you want to locate?';
}
if (type === 'aiBoolean') {
return 'What do you want to check (returns true/false)?';
}
if (type === 'aiNumber') {
return 'What number do you want to extract?';
}
if (type === 'aiString') {
return 'What text do you want to extract?';
}
if (type === 'aiAsk') {
return 'What do you want to ask?';
}
if (type === 'aiWaitFor') {
return 'What condition do you want to wait for?';
}
return 'What do you want to do?';
};
export const isRunButtonEnabled = (
runButtonEnabled: boolean,
needsStructuredParams: boolean,
params: any,
actionMap: Map<string, any> | undefined,
selectedType: string,
promptValue: string,
) => {
if (!runButtonEnabled) {
return false;
}
const action = actionMap?.get(selectedType);
// Check if this method needs any input
const needsAnyInput = (() => {
if (actionMap) {
// If action exists in actionMap, check if it has paramSchema with actual fields
if (action) {
if (!action.paramSchema) return false;
// Check if paramSchema actually has fields
if (
typeof action.paramSchema === 'object' &&
'shape' in action.paramSchema
) {
const shape =
(action.paramSchema as { shape: Record<string, unknown> }).shape ||
{};
const shapeKeys = Object.keys(shape);
return shapeKeys.length > 0; // Only need input if there are actual fields
}
// If paramSchema exists but not in expected format, assume it needs input
return true;
}
// If not found in actionMap, assume most methods need input
return true;
}
// Fallback: most methods need some input
return true;
})();
// If method doesn't need any input, button is always enabled (when runButtonEnabled is true)
if (!needsAnyInput) {
return true;
}
if (needsStructuredParams) {
const currentParams = params || {};
if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) {
// Check if all required fields are filled
const schema = action.paramSchema as unknown as ZodObjectSchema;
const shape = schema.shape || {};
return Object.keys(shape).every((key) => {
const field = shape[key];
const { isOptional } = unwrapZodType(field);
const value = currentParams[key];
// A field is valid if it's optional or has a non-empty value
return (
isOptional || (value !== undefined && value !== '' && value !== null)
);
});
}
return true; // Fallback for safety
}
return promptValue.trim().length > 0;
};