@ui-tars-test/shared 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base/agent.d.ts +9 -0
- package/dist/base/agent.d.ts.map +1 -0
- package/dist/base/agent.js +54 -0
- package/dist/base/agent.js.map +1 -0
- package/dist/base/agent.mjs +10 -0
- package/dist/base/agent.mjs.map +1 -0
- package/dist/base/index.d.ts +4 -0
- package/dist/base/index.d.ts.map +1 -0
- package/dist/base/index.js +84 -0
- package/dist/base/index.js.map +1 -0
- package/dist/base/index.mjs +7 -0
- package/dist/base/operator.d.ts +140 -0
- package/dist/base/operator.d.ts.map +1 -0
- package/dist/base/operator.js +112 -0
- package/dist/base/operator.js.map +1 -0
- package/dist/base/operator.mjs +75 -0
- package/dist/base/operator.mjs.map +1 -0
- package/dist/base/parser.d.ts +11 -0
- package/dist/base/parser.d.ts.map +1 -0
- package/dist/base/parser.js +43 -0
- package/dist/base/parser.js.map +1 -0
- package/dist/base/parser.mjs +9 -0
- package/dist/base/parser.mjs.map +1 -0
- package/dist/types/actions.d.ts +224 -0
- package/dist/types/actions.d.ts.map +1 -0
- package/dist/types/actions.js +155 -0
- package/dist/types/actions.js.map +1 -0
- package/dist/types/actions.mjs +115 -0
- package/dist/types/actions.mjs.map +1 -0
- package/dist/types/agents.d.ts +108 -0
- package/dist/types/agents.d.ts.map +1 -0
- package/dist/types/agents.js +42 -0
- package/dist/types/agents.js.map +1 -0
- package/dist/types/agents.mjs +8 -0
- package/dist/types/agents.mjs.map +1 -0
- package/dist/types/archived.d.ts +44 -0
- package/dist/types/archived.d.ts.map +1 -0
- package/dist/types/archived.js +86 -0
- package/dist/types/archived.js.map +1 -0
- package/dist/types/archived.mjs +46 -0
- package/dist/types/archived.mjs.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +84 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +7 -0
- package/dist/utils/actions.d.ts +15 -0
- package/dist/utils/actions.d.ts.map +1 -0
- package/dist/utils/actions.js +196 -0
- package/dist/utils/actions.js.map +1 -0
- package/dist/utils/actions.mjs +156 -0
- package/dist/utils/actions.mjs.map +1 -0
- package/dist/utils/coordinateNormalizer.d.ts +10 -0
- package/dist/utils/coordinateNormalizer.d.ts.map +1 -0
- package/dist/utils/coordinateNormalizer.js +59 -0
- package/dist/utils/coordinateNormalizer.js.map +1 -0
- package/dist/utils/coordinateNormalizer.mjs +25 -0
- package/dist/utils/coordinateNormalizer.mjs.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +93 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +8 -0
- package/dist/utils/sleep.d.ts +14 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +45 -0
- package/dist/utils/sleep.js.map +1 -0
- package/dist/utils/sleep.mjs +11 -0
- package/dist/utils/sleep.mjs.map +1 -0
- package/dist/utils/systemPromptProcessor.d.ts +16 -0
- package/dist/utils/systemPromptProcessor.d.ts.map +1 -0
- package/dist/utils/systemPromptProcessor.js +61 -0
- package/dist/utils/systemPromptProcessor.js.map +1 -0
- package/dist/utils/systemPromptProcessor.mjs +24 -0
- package/dist/utils/systemPromptProcessor.mjs.map +1 -0
- package/package.json +66 -0
- package/src/base/agent.ts +13 -0
- package/src/base/index.ts +7 -0
- package/src/base/operator.ts +221 -0
- package/src/base/parser.ts +16 -0
- package/src/types/actions.ts +382 -0
- package/src/types/agents.ts +128 -0
- package/src/types/archived.ts +55 -0
- package/src/types/index.ts +8 -0
- package/src/utils/actions.ts +244 -0
- package/src/utils/coordinateNormalizer.ts +49 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/sleep.ts +21 -0
- package/src/utils/systemPromptProcessor.ts +48 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
+
|
|
8
|
+
import { BaseAction } from '../types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Serializes a BaseAction into a string representation
|
|
12
|
+
* Format: "actionType(param1='value1', param2='value2', ...)"
|
|
13
|
+
*/
|
|
14
|
+
export function serializeAction<T extends string, I extends Record<string, any>>(
|
|
15
|
+
action: BaseAction<T, I>,
|
|
16
|
+
): string {
|
|
17
|
+
const { type, inputs } = action;
|
|
18
|
+
const params = Object.entries(inputs)
|
|
19
|
+
.map(([key, value]) => {
|
|
20
|
+
if (typeof value === 'string') {
|
|
21
|
+
return `${key}='${value}'`;
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === 'object' && value !== null) {
|
|
24
|
+
if (value.raw) {
|
|
25
|
+
return `${key}='(${value.raw.x}, ${value.raw.y})'`;
|
|
26
|
+
}
|
|
27
|
+
if (value.referenceBox) {
|
|
28
|
+
return `${key}='<bbox>${value.referenceBox.x1}, ${value.referenceBox.y1}, ${value.referenceBox.x2}, ${value.referenceBox.y2}</bbox>'`;
|
|
29
|
+
}
|
|
30
|
+
if (value.normalized) {
|
|
31
|
+
return `${key}='(${value.normalized.x}, ${value.normalized.y})'`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (type === 'wait' && typeof value === 'number') {
|
|
35
|
+
return `${key}='${value}s'`;
|
|
36
|
+
}
|
|
37
|
+
if (type === 'navigate' && typeof value === 'string') {
|
|
38
|
+
return `url='${value}'`;
|
|
39
|
+
}
|
|
40
|
+
return `unsupported`;
|
|
41
|
+
})
|
|
42
|
+
.join(', ');
|
|
43
|
+
return `${type}(${params})`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const actionTypeMap: Record<string, string> = {
|
|
47
|
+
// ---- ScreenShotAction ----
|
|
48
|
+
|
|
49
|
+
snapshot: 'screenshot',
|
|
50
|
+
screenshot: 'screenshot',
|
|
51
|
+
take_screenshot: 'screenshot',
|
|
52
|
+
takescreenshot: 'screenshot',
|
|
53
|
+
|
|
54
|
+
// ---- Mouse Actions ----
|
|
55
|
+
|
|
56
|
+
// click
|
|
57
|
+
click: 'click',
|
|
58
|
+
left_click: 'click',
|
|
59
|
+
left_single: 'click',
|
|
60
|
+
leftclick: 'click',
|
|
61
|
+
leftsingle: 'click',
|
|
62
|
+
// double click
|
|
63
|
+
double_click: 'double_click',
|
|
64
|
+
left_double: 'double_click',
|
|
65
|
+
doubleclick: 'double_click',
|
|
66
|
+
leftdouble: 'double_click',
|
|
67
|
+
// right click
|
|
68
|
+
right_click: 'right_click',
|
|
69
|
+
right_single: 'right_click',
|
|
70
|
+
rightclick: 'right_click',
|
|
71
|
+
rightsingle: 'right_click',
|
|
72
|
+
// middle click
|
|
73
|
+
middle_click: 'middle_click',
|
|
74
|
+
middle_single: 'middle_click',
|
|
75
|
+
middleclick: 'middle_click',
|
|
76
|
+
middlesingle: 'middle_click',
|
|
77
|
+
// mouse move
|
|
78
|
+
move: 'mouse_move',
|
|
79
|
+
move_to: 'mouse_move',
|
|
80
|
+
mouse_move: 'mouse_move',
|
|
81
|
+
moveto: 'mouse_move',
|
|
82
|
+
mousemove: 'mouse_move',
|
|
83
|
+
hover: 'mouse_move',
|
|
84
|
+
// mouse down
|
|
85
|
+
mouse_down: 'mouse_down',
|
|
86
|
+
mousedown: 'mouse_down',
|
|
87
|
+
// mouse up
|
|
88
|
+
mouse_up: 'mouse_up',
|
|
89
|
+
mouseup: 'mouse_up',
|
|
90
|
+
// drag
|
|
91
|
+
drag: 'drag',
|
|
92
|
+
select: 'drag',
|
|
93
|
+
left_click_drag: 'drag',
|
|
94
|
+
leftclickdrag: 'drag',
|
|
95
|
+
// swipe
|
|
96
|
+
swipe: 'swipe',
|
|
97
|
+
// scroll
|
|
98
|
+
scroll: 'scroll',
|
|
99
|
+
|
|
100
|
+
// ---- Keyboard Actions ----
|
|
101
|
+
|
|
102
|
+
type: 'type',
|
|
103
|
+
hotkey: 'hotkey',
|
|
104
|
+
press: 'press',
|
|
105
|
+
release: 'release',
|
|
106
|
+
|
|
107
|
+
// ---- Browser Actions ----
|
|
108
|
+
|
|
109
|
+
navigate: 'navigate',
|
|
110
|
+
navigate_back: 'navigate_back',
|
|
111
|
+
navigateback: 'navigate_back',
|
|
112
|
+
|
|
113
|
+
// ---- App Actions ----
|
|
114
|
+
|
|
115
|
+
// long press
|
|
116
|
+
long_press: 'long_press',
|
|
117
|
+
longpress: 'long_press',
|
|
118
|
+
// home
|
|
119
|
+
home: 'press_home',
|
|
120
|
+
press_home: 'press_home',
|
|
121
|
+
presshome: 'press_home',
|
|
122
|
+
// back
|
|
123
|
+
back: 'press_back',
|
|
124
|
+
press_back: 'press_back',
|
|
125
|
+
pressback: 'press_back',
|
|
126
|
+
// open app
|
|
127
|
+
open: 'open_app',
|
|
128
|
+
open_app: 'open_app',
|
|
129
|
+
openapp: 'open_app',
|
|
130
|
+
|
|
131
|
+
// ---- Agent Actions ----
|
|
132
|
+
|
|
133
|
+
wait: 'wait',
|
|
134
|
+
finished: 'finished',
|
|
135
|
+
call_user: 'call_user',
|
|
136
|
+
calluser: 'call_user',
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export function unifyActionType(name: string) {
|
|
140
|
+
name = name.toLowerCase();
|
|
141
|
+
return actionTypeMap[name] || name;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const actionInputNameMap: Record<string, string> = {
|
|
145
|
+
// ---- Start related fields ----
|
|
146
|
+
start: 'start',
|
|
147
|
+
start_box: 'start',
|
|
148
|
+
startbox: 'start',
|
|
149
|
+
start_point: 'start',
|
|
150
|
+
start_position: 'start',
|
|
151
|
+
start_coordinate: 'start',
|
|
152
|
+
start_coordinates: 'start',
|
|
153
|
+
|
|
154
|
+
// ---- End related fields ----
|
|
155
|
+
end: 'end',
|
|
156
|
+
end_box: 'end',
|
|
157
|
+
endbox: 'end',
|
|
158
|
+
end_point: 'end',
|
|
159
|
+
end_position: 'end',
|
|
160
|
+
end_coordinate: 'end',
|
|
161
|
+
end_coordinates: 'end',
|
|
162
|
+
|
|
163
|
+
// ---- Point related fields ----
|
|
164
|
+
point: 'point',
|
|
165
|
+
position: 'point',
|
|
166
|
+
coordinate: 'point',
|
|
167
|
+
coordinates: 'point',
|
|
168
|
+
|
|
169
|
+
// ---- Button related fields ----
|
|
170
|
+
button: 'button',
|
|
171
|
+
mouse_button: 'button',
|
|
172
|
+
mousebutton: 'button',
|
|
173
|
+
|
|
174
|
+
// ---- Direction related fields ----
|
|
175
|
+
direction: 'direction',
|
|
176
|
+
dir: 'direction',
|
|
177
|
+
scroll_direction: 'direction',
|
|
178
|
+
scrolldirection: 'direction',
|
|
179
|
+
|
|
180
|
+
// ---- Content related fields ----
|
|
181
|
+
content: 'content',
|
|
182
|
+
text: 'content',
|
|
183
|
+
input_text: 'content',
|
|
184
|
+
inputtext: 'content',
|
|
185
|
+
type: 'content',
|
|
186
|
+
|
|
187
|
+
// ---- Key related fields ----
|
|
188
|
+
key: 'key',
|
|
189
|
+
keyname: 'key',
|
|
190
|
+
hotkey: 'key',
|
|
191
|
+
keyboard_key: 'key',
|
|
192
|
+
keyboardkey: 'key',
|
|
193
|
+
|
|
194
|
+
// ---- URL related fields ----
|
|
195
|
+
url: 'url',
|
|
196
|
+
link: 'url',
|
|
197
|
+
website: 'url',
|
|
198
|
+
|
|
199
|
+
// ---- Name related fields ----
|
|
200
|
+
name: 'name',
|
|
201
|
+
appname: 'name',
|
|
202
|
+
app_name: 'name',
|
|
203
|
+
application: 'name',
|
|
204
|
+
|
|
205
|
+
// ---- Time related fields ----
|
|
206
|
+
time: 'time',
|
|
207
|
+
duration: 'time',
|
|
208
|
+
wait_time: 'time',
|
|
209
|
+
waittime: 'time',
|
|
210
|
+
delay: 'time',
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// Special mappings based on action type
|
|
214
|
+
const actionTypeSpecificMappings: Record<string, Record<string, string>> = {
|
|
215
|
+
navigate: {
|
|
216
|
+
content: 'url',
|
|
217
|
+
url: 'url',
|
|
218
|
+
},
|
|
219
|
+
open_app: {
|
|
220
|
+
content: 'name',
|
|
221
|
+
appname: 'name',
|
|
222
|
+
app_name: 'name',
|
|
223
|
+
name: 'name',
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Standardizes action input field names using mapping tables
|
|
229
|
+
* @param actionType The type of action being performed
|
|
230
|
+
* @param inputName The original input field name
|
|
231
|
+
* @returns The standardized input field name
|
|
232
|
+
*/
|
|
233
|
+
export function unifyActionInputName(actionType: string, inputName: string): string {
|
|
234
|
+
actionType = actionType.toLowerCase();
|
|
235
|
+
inputName = inputName.toLowerCase();
|
|
236
|
+
actionType = unifyActionType(actionType);
|
|
237
|
+
// First check for action type specific mappings
|
|
238
|
+
const typeSpecificMap = actionTypeSpecificMappings[actionType];
|
|
239
|
+
if (typeSpecificMap && typeSpecificMap[inputName]) {
|
|
240
|
+
return typeSpecificMap[inputName];
|
|
241
|
+
}
|
|
242
|
+
// Then check general mappings
|
|
243
|
+
return actionInputNameMap[inputName] || inputName;
|
|
244
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { BaseAction, Coordinates, NormalizeCoordinates } from '../types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes coordinates in a BaseAction object
|
|
10
|
+
* Processes point, start, and end coordinate fields if they exist
|
|
11
|
+
* @param action - The BaseAction object to normalize coordinates for
|
|
12
|
+
* @param normalizeCoordinates - The coordinate normalization function to use
|
|
13
|
+
* @returns The BaseAction object with normalized coordinates added
|
|
14
|
+
*/
|
|
15
|
+
export function normalizeActionCoords<T extends BaseAction>(
|
|
16
|
+
action: T,
|
|
17
|
+
normalizeCoordinates: NormalizeCoordinates,
|
|
18
|
+
): T {
|
|
19
|
+
const normalizedAction = { ...action };
|
|
20
|
+
|
|
21
|
+
// Normalize point coordinates
|
|
22
|
+
if (
|
|
23
|
+
normalizedAction.inputs &&
|
|
24
|
+
'point' in normalizedAction.inputs &&
|
|
25
|
+
normalizedAction.inputs.point
|
|
26
|
+
) {
|
|
27
|
+
const normalizedResult = normalizeCoordinates(normalizedAction.inputs.point as Coordinates);
|
|
28
|
+
normalizedAction.inputs.point = normalizedResult.normalized;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Normalize start coordinates
|
|
32
|
+
if (
|
|
33
|
+
normalizedAction.inputs &&
|
|
34
|
+
'start' in normalizedAction.inputs &&
|
|
35
|
+
normalizedAction.inputs.start
|
|
36
|
+
) {
|
|
37
|
+
const normalizedResult = normalizeCoordinates(normalizedAction.inputs.start as Coordinates);
|
|
38
|
+
normalizedAction.inputs.start = normalizedResult.normalized;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Normalize end coordinates
|
|
42
|
+
// eslint-disable-next-line prettier/prettier
|
|
43
|
+
if (normalizedAction.inputs && 'end' in normalizedAction.inputs && normalizedAction.inputs.end) {
|
|
44
|
+
const normalizedResult = normalizeCoordinates(normalizedAction.inputs.end as Coordinates);
|
|
45
|
+
normalizedAction.inputs.end = normalizedResult.normalized;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return normalizedAction;
|
|
49
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sleeps for a specified duration
|
|
8
|
+
* @param time The duration to sleep
|
|
9
|
+
* @param unit The time unit ('ms' for milliseconds or 's' for seconds), defaults to 'ms'
|
|
10
|
+
*
|
|
11
|
+
* Examples:
|
|
12
|
+
* ```
|
|
13
|
+
* await sleep(1000); // Sleeps for 1000 milliseconds (1 second)
|
|
14
|
+
* await sleep(1, 's'); // Sleeps for 1 second
|
|
15
|
+
* await sleep(500, 'ms'); // Sleeps for 500 milliseconds
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export async function sleep(time: number, unit: 'ms' | 's' = 'ms'): Promise<void> {
|
|
19
|
+
const ms = unit === 's' ? time * 1000 : time;
|
|
20
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { SystemPromptTemplate, ACTION_SPACE_PLACEHOLDER } from '../types/agents';
|
|
7
|
+
import { SupportedActionType } from '../types/actions';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Assemble system prompt template by replacing placeholders with actual values
|
|
11
|
+
* @param template - The system prompt template configuration
|
|
12
|
+
* @param supportedActions - Array of supported action types
|
|
13
|
+
* @returns Assembled system prompt string with placeholders replaced
|
|
14
|
+
*/
|
|
15
|
+
export function assembleSystemPrompt(
|
|
16
|
+
template: SystemPromptTemplate,
|
|
17
|
+
supportedActions: SupportedActionType[],
|
|
18
|
+
): string {
|
|
19
|
+
let assembledPrompt = template.template;
|
|
20
|
+
|
|
21
|
+
// Replace action space placeholder if actionsToString function is provided
|
|
22
|
+
if (template.actionsToString) {
|
|
23
|
+
const actionSpacePlaceholder = `{{${ACTION_SPACE_PLACEHOLDER}}}`;
|
|
24
|
+
const realActionSpaces = template.actionsToString(supportedActions);
|
|
25
|
+
assembledPrompt = assembledPrompt.replace(actionSpacePlaceholder, realActionSpaces || '');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Handle other custom placeholders replacement
|
|
29
|
+
if (template.placeholders) {
|
|
30
|
+
Object.entries(template.placeholders).forEach(([key, value]) => {
|
|
31
|
+
const placeholder = `{{${key}}}`;
|
|
32
|
+
assembledPrompt = assembledPrompt.replace(new RegExp(placeholder, 'g'), String(value));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return assembledPrompt;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Type guard to check if a system prompt is a template object
|
|
41
|
+
* @param systemPrompt - The system prompt to check
|
|
42
|
+
* @returns Whether the system prompt is a SystemPromptTemplate
|
|
43
|
+
*/
|
|
44
|
+
export function isSystemPromptTemplate(
|
|
45
|
+
systemPrompt: string | SystemPromptTemplate,
|
|
46
|
+
): systemPrompt is SystemPromptTemplate {
|
|
47
|
+
return typeof systemPrompt === 'object' && 'template' in systemPrompt;
|
|
48
|
+
}
|