@aiscene/shared 1.6.0 → 1.8.0
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/es/cli/cli-runner.mjs +13 -1
- package/dist/es/constants/example-code.mjs +25 -21
- package/dist/es/env/parse-model-config.mjs +11 -1
- package/dist/es/env/types.mjs +1 -0
- package/dist/es/extractor/dom-util.mjs +9 -5
- package/dist/es/index.mjs +1 -3
- package/dist/es/logger.mjs +1 -7
- package/dist/es/mcp/tool-generator.mjs +130 -56
- package/dist/es/node/fs.mjs +1 -1
- package/dist/es/utils.mjs +1 -6
- package/dist/lib/cli/cli-runner.js +13 -1
- package/dist/lib/constants/example-code.js +25 -21
- package/dist/lib/env/parse-model-config.js +11 -1
- package/dist/lib/env/types.js +1 -0
- package/dist/lib/extractor/dom-util.js +9 -5
- package/dist/lib/index.js +2 -13
- package/dist/lib/logger.js +0 -9
- package/dist/lib/mcp/tool-generator.js +129 -55
- package/dist/lib/node/fs.js +1 -1
- package/dist/lib/utils.js +0 -8
- package/dist/types/cli/cli-runner.d.ts +8 -0
- package/dist/types/cli/index.d.ts +1 -1
- package/dist/types/constants/example-code.d.ts +1 -1
- package/dist/types/env/types.d.ts +1 -1
- package/dist/types/extractor/dom-util.d.ts +5 -4
- package/dist/types/index.d.ts +0 -2
- package/dist/types/logger.d.ts +0 -1
- package/dist/types/mcp/types.d.ts +1 -0
- package/dist/types/utils.d.ts +0 -3
- package/package.json +2 -2
- package/src/cli/cli-runner.ts +26 -1
- package/src/cli/index.ts +1 -1
- package/src/constants/example-code.ts +25 -21
- package/src/env/parse-model-config.ts +21 -1
- package/src/env/types.ts +2 -0
- package/src/extractor/dom-util.ts +10 -5
- package/src/index.ts +0 -7
- package/src/logger.ts +0 -11
- package/src/mcp/tool-generator.ts +217 -66
- package/src/mcp/types.ts +4 -0
- package/src/utils.ts +0 -17
- package/dist/es/build/copy-static.mjs +0 -31
- package/dist/es/build/rspack-config.mjs +0 -4
- package/dist/lib/build/copy-static.js +0 -79
- package/dist/lib/build/rspack-config.js +0 -38
- package/dist/types/build/copy-static.d.ts +0 -31
- package/dist/types/build/rspack-config.d.ts +0 -8
- package/src/build/copy-static.ts +0 -68
- package/src/build/rspack-config.ts +0 -12
package/src/cli/cli-runner.ts
CHANGED
|
@@ -15,12 +15,21 @@ const debug = getDebug('cli-runner');
|
|
|
15
15
|
interface CLICommand {
|
|
16
16
|
name: string;
|
|
17
17
|
def: ToolDefinition;
|
|
18
|
+
hidden?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CLIExtraCommand {
|
|
22
|
+
name: string;
|
|
23
|
+
def: ToolDefinition;
|
|
24
|
+
aliases?: string[];
|
|
25
|
+
hidden?: boolean;
|
|
18
26
|
}
|
|
19
27
|
|
|
20
28
|
export interface CLIRunnerOptions {
|
|
21
29
|
stripPrefix?: string;
|
|
22
30
|
argv?: string[];
|
|
23
31
|
version?: string;
|
|
32
|
+
extraCommands?: CLIExtraCommand[];
|
|
24
33
|
}
|
|
25
34
|
|
|
26
35
|
export class CLIError extends Error {
|
|
@@ -142,7 +151,7 @@ function printHelp(
|
|
|
142
151
|
}
|
|
143
152
|
console.log(`\nUsage: ${scriptName} <command> [options]\n`);
|
|
144
153
|
console.log('Commands:');
|
|
145
|
-
for (const { name, def } of commands) {
|
|
154
|
+
for (const { name, def } of commands.filter((command) => !command.hidden)) {
|
|
146
155
|
console.log(` ${name.padEnd(30)} ${def.description}`);
|
|
147
156
|
}
|
|
148
157
|
console.log(` ${'version'.padEnd(30)} Show CLI version`);
|
|
@@ -169,6 +178,22 @@ export async function runToolsCLI(
|
|
|
169
178
|
name: removePrefix(def.name, options?.stripPrefix).toLowerCase(),
|
|
170
179
|
def,
|
|
171
180
|
}));
|
|
181
|
+
if (options?.extraCommands?.length) {
|
|
182
|
+
commands.push(
|
|
183
|
+
...options.extraCommands.flatMap((cmd) => [
|
|
184
|
+
{
|
|
185
|
+
name: cmd.name.toLowerCase(),
|
|
186
|
+
def: cmd.def,
|
|
187
|
+
hidden: cmd.hidden,
|
|
188
|
+
},
|
|
189
|
+
...(cmd.aliases ?? []).map((alias) => ({
|
|
190
|
+
name: alias.toLowerCase(),
|
|
191
|
+
def: cmd.def,
|
|
192
|
+
hidden: true,
|
|
193
|
+
})),
|
|
194
|
+
]),
|
|
195
|
+
);
|
|
196
|
+
}
|
|
172
197
|
const cliVersion = options?.version;
|
|
173
198
|
|
|
174
199
|
const [commandName, ...restArgs] = rawArgs;
|
package/src/cli/index.ts
CHANGED
|
@@ -81,6 +81,23 @@ test('ai shop', async ({
|
|
|
81
81
|
`;
|
|
82
82
|
|
|
83
83
|
export const YAML_EXAMPLE_CODE = `
|
|
84
|
+
CRITICAL - YAML Indentation Rules:
|
|
85
|
+
For actions with additional parameters (aiScroll, aiInput, aiKeyboardPress), the parameters must be SIBLING keys at the SAME indentation level as the action key, NOT nested children indented further.
|
|
86
|
+
CORRECT (parameters align with the action key):
|
|
87
|
+
- aiScroll:
|
|
88
|
+
direction: 'down'
|
|
89
|
+
scrollType: 'singleAction'
|
|
90
|
+
distance: 500
|
|
91
|
+
locate: "main content area"
|
|
92
|
+
- aiInput: 'text value'
|
|
93
|
+
locate: 'input field description'
|
|
94
|
+
WRONG (parameters are indented further than the action key, DO NOT do this):
|
|
95
|
+
- aiScroll:
|
|
96
|
+
direction: 'down'
|
|
97
|
+
scrollType: 'singleAction'
|
|
98
|
+
- aiInput: 'text value'
|
|
99
|
+
locate: 'input field description'
|
|
100
|
+
|
|
84
101
|
1. Format:
|
|
85
102
|
|
|
86
103
|
web:
|
|
@@ -92,13 +109,18 @@ tasks:
|
|
|
92
109
|
- name: "descriptive task name"
|
|
93
110
|
flow:
|
|
94
111
|
- aiTap: "element description"
|
|
112
|
+
xpath: '/html/body/div[1]/button[1]'
|
|
95
113
|
- aiInput: 'text value'
|
|
96
114
|
locate: 'input field description'
|
|
115
|
+
xpath: '/html/body/div[1]/input[1]'
|
|
97
116
|
- aiScroll:
|
|
98
|
-
direction: down
|
|
99
|
-
scrollType:
|
|
117
|
+
direction: 'down'
|
|
118
|
+
scrollType: 'singleAction'
|
|
119
|
+
distance: 500
|
|
120
|
+
locate: "scrollable area description"
|
|
121
|
+
xpath: '/html/body/div[1]/main[1]'
|
|
100
122
|
- aiAssert: "expected state"
|
|
101
|
-
- sleep:
|
|
123
|
+
- sleep: 1000
|
|
102
124
|
|
|
103
125
|
2. Action Types:
|
|
104
126
|
- aiTap: for clicks (natural language targeting)
|
|
@@ -107,24 +129,6 @@ tasks:
|
|
|
107
129
|
- aiAssert: for validations
|
|
108
130
|
- sleep: for delays (milliseconds)
|
|
109
131
|
|
|
110
|
-
3. Best Practices:
|
|
111
|
-
- Group related actions into logical tasks
|
|
112
|
-
- Use natural language descriptions
|
|
113
|
-
- Add deepLocate: true for complex interactions
|
|
114
|
-
- Keep task names concise but descriptive
|
|
115
|
-
|
|
116
|
-
4. CRITICAL - YAML Indentation Rules:
|
|
117
|
-
- For actions with additional parameters (aiScroll, aiInput, aiKeyboardPress), the parameters must be SIBLING keys, NOT nested children
|
|
118
|
-
- Parameters like direction, scrollType, locate must align with the action key, not indented further
|
|
119
|
-
- CORRECT indentation example:
|
|
120
|
-
- aiScroll:
|
|
121
|
-
direction: down
|
|
122
|
-
scrollType: singleAction
|
|
123
|
-
- WRONG indentation (DO NOT do this):
|
|
124
|
-
- aiScroll:
|
|
125
|
-
direction: down
|
|
126
|
-
scrollType: singleAction
|
|
127
|
-
|
|
128
132
|
|
|
129
133
|
|
|
130
134
|
YAML type
|
|
@@ -136,6 +136,26 @@ const getModelDescription = (
|
|
|
136
136
|
return '';
|
|
137
137
|
};
|
|
138
138
|
|
|
139
|
+
const normalizeOpenaiExtraConfig = (
|
|
140
|
+
config: unknown,
|
|
141
|
+
): Record<string, unknown> | undefined => {
|
|
142
|
+
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const { defaultHeaders, extra_headers, extraHeaders, ...rest } =
|
|
147
|
+
config as Record<string, unknown>;
|
|
148
|
+
|
|
149
|
+
// Priority: defaultHeaders > extra_headers > extraHeaders
|
|
150
|
+
const headers = defaultHeaders ?? extra_headers ?? extraHeaders;
|
|
151
|
+
|
|
152
|
+
if (headers !== undefined) {
|
|
153
|
+
return { ...rest, defaultHeaders: headers };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return rest;
|
|
157
|
+
};
|
|
158
|
+
|
|
139
159
|
/**
|
|
140
160
|
* Parse OpenAI SDK config
|
|
141
161
|
*/
|
|
@@ -201,7 +221,7 @@ export const parseOpenaiSdkConfig = ({
|
|
|
201
221
|
httpProxy,
|
|
202
222
|
openaiBaseURL,
|
|
203
223
|
openaiApiKey,
|
|
204
|
-
openaiExtraConfig,
|
|
224
|
+
openaiExtraConfig: normalizeOpenaiExtraConfig(openaiExtraConfig),
|
|
205
225
|
extraBody,
|
|
206
226
|
modelFamily,
|
|
207
227
|
uiTarsModelVersion,
|
package/src/env/types.ts
CHANGED
|
@@ -293,6 +293,7 @@ export type TModelFamily =
|
|
|
293
293
|
| 'qwen2.5-vl'
|
|
294
294
|
| 'qwen3-vl'
|
|
295
295
|
| 'qwen3.5'
|
|
296
|
+
| 'qwen3.6'
|
|
296
297
|
| 'doubao-vision'
|
|
297
298
|
| 'doubao-seed'
|
|
298
299
|
| 'gemini'
|
|
@@ -311,6 +312,7 @@ export const MODEL_FAMILY_VALUES: TModelFamily[] = [
|
|
|
311
312
|
'qwen2.5-vl',
|
|
312
313
|
'qwen3-vl',
|
|
313
314
|
'qwen3.5',
|
|
315
|
+
'qwen3.6',
|
|
314
316
|
'vlm-ui-tars',
|
|
315
317
|
'vlm-ui-tars-doubao',
|
|
316
318
|
'vlm-ui-tars-doubao-1.5',
|
|
@@ -181,7 +181,8 @@ export function generateElementByPoint(
|
|
|
181
181
|
|
|
182
182
|
/**
|
|
183
183
|
* Generate a LocateResultElement from a rect.
|
|
184
|
-
* This function calculates the center point from the rect and
|
|
184
|
+
* This function calculates the center point from the rect and preserves the
|
|
185
|
+
* original rect as the returned element boundary.
|
|
185
186
|
*
|
|
186
187
|
* Note: The rect uses inclusive coordinates where:
|
|
187
188
|
* - A rect from [left=10, top=10] with [width=1, height=1] covers exactly 1 pixel
|
|
@@ -189,13 +190,13 @@ export function generateElementByPoint(
|
|
|
189
190
|
*
|
|
190
191
|
* @param sourceRect - The source rect to generate element from (typically contains integer values)
|
|
191
192
|
* @param description - Description of the element
|
|
192
|
-
* @param edgeSize -
|
|
193
|
-
* @returns A LocateResultElement with rect, center (always integers), and description
|
|
193
|
+
* @param edgeSize - Deprecated, retained for backward compatibility
|
|
194
|
+
* @returns A LocateResultElement with the original rect, center (always integers), and description
|
|
194
195
|
*/
|
|
195
196
|
export function generateElementByRect(
|
|
196
197
|
sourceRect: { left: number; top: number; width: number; height: number },
|
|
197
198
|
description: string,
|
|
198
|
-
|
|
199
|
+
_edgeSize = 8,
|
|
199
200
|
): LocateResultElement {
|
|
200
201
|
/**
|
|
201
202
|
* Calculate center point from rect
|
|
@@ -222,5 +223,9 @@ export function generateElementByRect(
|
|
|
222
223
|
const centerX = sourceRect.left + Math.floor((sourceRect.width - 1) / 2);
|
|
223
224
|
const centerY = sourceRect.top + Math.floor((sourceRect.height - 1) / 2);
|
|
224
225
|
|
|
225
|
-
return
|
|
226
|
+
return {
|
|
227
|
+
rect: sourceRect,
|
|
228
|
+
center: [centerX, centerY],
|
|
229
|
+
description: description || '',
|
|
230
|
+
};
|
|
226
231
|
}
|
package/src/index.ts
CHANGED
package/src/logger.ts
CHANGED
|
@@ -94,14 +94,3 @@ export function enableDebug(topic: string): void {
|
|
|
94
94
|
}
|
|
95
95
|
debug.enable(`${topicPrefix}:${topic}`);
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
// Cleanup function to close all log streams
|
|
99
|
-
export function cleanupLogStreams(): void {
|
|
100
|
-
if (!ifInNode) return;
|
|
101
|
-
|
|
102
|
-
for (const stream of logStreams.values()) {
|
|
103
|
-
stream.end();
|
|
104
|
-
}
|
|
105
|
-
logStreams.clear();
|
|
106
|
-
debugInstances.clear();
|
|
107
|
-
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { parseBase64 } from '../img';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getZodDescription,
|
|
5
|
+
getZodTypeName,
|
|
6
|
+
isMidsceneLocatorField,
|
|
7
|
+
unwrapZodField,
|
|
8
|
+
} from '../zod-schema-utils';
|
|
4
9
|
import type {
|
|
5
10
|
ActionSpaceItem,
|
|
6
11
|
BaseAgent,
|
|
@@ -26,23 +31,18 @@ function describeActionForMCP(action: ActionSpaceItem): string {
|
|
|
26
31
|
return `${action.name} action, ${actionDesc}`;
|
|
27
32
|
}
|
|
28
33
|
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
shape?: Record<string, unknown>;
|
|
32
|
-
};
|
|
33
|
-
const isZodObjectType = schema._def?.typeName === 'ZodObject';
|
|
34
|
-
|
|
35
|
-
if (!isZodObjectType || !schema.shape) {
|
|
34
|
+
const shape = getZodObjectShape(action.paramSchema);
|
|
35
|
+
if (!shape) {
|
|
36
36
|
// Simple type schema
|
|
37
|
-
const typeName = getZodTypeName(
|
|
38
|
-
const description = getZodDescription(
|
|
37
|
+
const typeName = getZodTypeName(action.paramSchema);
|
|
38
|
+
const description = getZodDescription(action.paramSchema as z.ZodTypeAny);
|
|
39
39
|
const paramDesc = description ? `${typeName} - ${description}` : typeName;
|
|
40
40
|
return `${action.name} action, ${actionDesc}. Parameter: ${paramDesc}`;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
// Object schema with multiple fields
|
|
44
44
|
const paramDescriptions: string[] = [];
|
|
45
|
-
for (const [key, field] of Object.entries(
|
|
45
|
+
for (const [key, field] of Object.entries(shape)) {
|
|
46
46
|
if (field && typeof field === 'object') {
|
|
47
47
|
const isFieldOptional =
|
|
48
48
|
typeof (field as { isOptional?: () => boolean }).isOptional ===
|
|
@@ -96,25 +96,42 @@ function unwrapOptional(value: z.ZodTypeAny): {
|
|
|
96
96
|
return { innerValue: value, isOptional: false };
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return false;
|
|
99
|
+
function getZodObjectShape(
|
|
100
|
+
value: z.ZodTypeAny | undefined,
|
|
101
|
+
): Record<string, z.ZodTypeAny> | undefined {
|
|
102
|
+
if (!value) {
|
|
103
|
+
return undefined;
|
|
105
104
|
}
|
|
106
|
-
|
|
105
|
+
|
|
106
|
+
const actualValue = unwrapZodField(value) as {
|
|
107
|
+
_def?: { typeName?: string; shape?: () => Record<string, z.ZodTypeAny> };
|
|
108
|
+
shape?: Record<string, z.ZodTypeAny>;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
if (actualValue._def?.typeName !== 'ZodObject') {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof actualValue._def.shape === 'function') {
|
|
116
|
+
return actualValue._def.shape();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return actualValue.shape;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
123
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
107
124
|
}
|
|
108
125
|
|
|
109
126
|
/**
|
|
110
127
|
* Transform a locate field schema to make its 'prompt' field optional
|
|
111
128
|
*/
|
|
112
129
|
function makePromptOptional(
|
|
113
|
-
|
|
130
|
+
shape: Record<string, z.ZodTypeAny>,
|
|
114
131
|
wrapInOptional: boolean,
|
|
115
132
|
): z.ZodTypeAny {
|
|
116
|
-
const newShape = { ...
|
|
117
|
-
newShape.prompt =
|
|
133
|
+
const newShape = { ...shape };
|
|
134
|
+
newShape.prompt = shape.prompt.optional();
|
|
118
135
|
|
|
119
136
|
let newSchema: z.ZodTypeAny = z.object(newShape).passthrough();
|
|
120
137
|
if (wrapInOptional) {
|
|
@@ -131,9 +148,10 @@ function transformSchemaField(
|
|
|
131
148
|
value: z.ZodTypeAny,
|
|
132
149
|
): [string, z.ZodTypeAny] {
|
|
133
150
|
const { innerValue, isOptional } = unwrapOptional(value);
|
|
151
|
+
const shape = getZodObjectShape(innerValue);
|
|
134
152
|
|
|
135
|
-
if (
|
|
136
|
-
return [key, makePromptOptional(
|
|
153
|
+
if (shape && isMidsceneLocatorField(innerValue)) {
|
|
154
|
+
return [key, makePromptOptional(shape, isOptional)];
|
|
137
155
|
}
|
|
138
156
|
return [key, value];
|
|
139
157
|
}
|
|
@@ -148,18 +166,117 @@ function extractActionSchema(
|
|
|
148
166
|
return {};
|
|
149
167
|
}
|
|
150
168
|
|
|
151
|
-
const
|
|
152
|
-
if (!
|
|
153
|
-
return
|
|
169
|
+
const shape = getZodObjectShape(paramSchema);
|
|
170
|
+
if (!shape) {
|
|
171
|
+
return paramSchema as unknown as Record<string, z.ZodTypeAny>;
|
|
154
172
|
}
|
|
155
173
|
|
|
156
174
|
return Object.fromEntries(
|
|
157
|
-
Object.entries(
|
|
175
|
+
Object.entries(shape).map(([key, value]) =>
|
|
158
176
|
transformSchemaField(key, value as z.ZodTypeAny),
|
|
159
177
|
),
|
|
160
178
|
);
|
|
161
179
|
}
|
|
162
180
|
|
|
181
|
+
function getPromptText(prompt: unknown): string | undefined {
|
|
182
|
+
if (typeof prompt === 'string') {
|
|
183
|
+
return prompt;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (isRecord(prompt) && typeof prompt.prompt === 'string') {
|
|
187
|
+
return prompt.prompt;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function moveLocateExtrasIntoPrompt(
|
|
194
|
+
value: Record<string, unknown>,
|
|
195
|
+
locateFieldKeys: Set<string>,
|
|
196
|
+
): Record<string, unknown> {
|
|
197
|
+
const promptText = getPromptText(value.prompt);
|
|
198
|
+
if (!promptText) {
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const normalizedPrompt: Record<string, unknown> = isRecord(value.prompt)
|
|
203
|
+
? { ...value.prompt }
|
|
204
|
+
: { prompt: promptText };
|
|
205
|
+
const normalizedLocate: Record<string, unknown> = {};
|
|
206
|
+
let movedExtraField = false;
|
|
207
|
+
|
|
208
|
+
for (const [key, fieldValue] of Object.entries(value)) {
|
|
209
|
+
if (key === 'prompt') {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (locateFieldKeys.has(key)) {
|
|
214
|
+
normalizedLocate[key] = fieldValue;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
movedExtraField = true;
|
|
219
|
+
if (!(key in normalizedPrompt)) {
|
|
220
|
+
normalizedPrompt[key] = fieldValue;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (!movedExtraField) {
|
|
225
|
+
return value;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { ...normalizedLocate, prompt: normalizedPrompt };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function normalizeLocateLikeArg(
|
|
232
|
+
value: unknown,
|
|
233
|
+
fieldSchema: z.ZodTypeAny,
|
|
234
|
+
): unknown {
|
|
235
|
+
if (typeof value === 'string') {
|
|
236
|
+
return { prompt: value };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!isRecord(value)) {
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const shape = getZodObjectShape(fieldSchema);
|
|
244
|
+
if (!shape) {
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return moveLocateExtrasIntoPrompt(value, new Set(Object.keys(shape)));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function normalizeActionArgs(
|
|
252
|
+
args: Record<string, unknown>,
|
|
253
|
+
paramSchema?: z.ZodTypeAny,
|
|
254
|
+
): Record<string, unknown> {
|
|
255
|
+
if (!paramSchema) {
|
|
256
|
+
return args;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const shape = getZodObjectShape(paramSchema);
|
|
260
|
+
if (!shape) {
|
|
261
|
+
return args;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return Object.fromEntries(
|
|
265
|
+
Object.entries(args).map(([key, value]) => {
|
|
266
|
+
const fieldSchema = shape[key] as z.ZodTypeAny | undefined;
|
|
267
|
+
if (!fieldSchema) {
|
|
268
|
+
return [key, value];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (isMidsceneLocatorField(fieldSchema)) {
|
|
272
|
+
return [key, normalizeLocateLikeArg(value, fieldSchema)];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return [key, value];
|
|
276
|
+
}),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
163
280
|
/**
|
|
164
281
|
* Serialize args to human-readable description for AI action
|
|
165
282
|
*/
|
|
@@ -194,10 +311,9 @@ function buildActionInstruction(
|
|
|
194
311
|
actionName: string,
|
|
195
312
|
args: Record<string, unknown>,
|
|
196
313
|
): string {
|
|
197
|
-
const locatePrompt =
|
|
198
|
-
args.locate
|
|
199
|
-
|
|
200
|
-
: undefined;
|
|
314
|
+
const locatePrompt = isRecord(args.locate)
|
|
315
|
+
? getPromptText(args.locate.prompt)
|
|
316
|
+
: undefined;
|
|
201
317
|
|
|
202
318
|
switch (actionName) {
|
|
203
319
|
case 'Tap':
|
|
@@ -227,39 +343,71 @@ function buildActionInstruction(
|
|
|
227
343
|
}
|
|
228
344
|
}
|
|
229
345
|
|
|
346
|
+
async function executeAction(
|
|
347
|
+
agent: BaseAgent,
|
|
348
|
+
actionName: string,
|
|
349
|
+
args: Record<string, unknown>,
|
|
350
|
+
): Promise<unknown> {
|
|
351
|
+
if (agent.callActionInActionSpace) {
|
|
352
|
+
return agent.callActionInActionSpace(actionName, args);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (agent.aiAction) {
|
|
356
|
+
const instruction = buildActionInstruction(actionName, args);
|
|
357
|
+
return agent.aiAction(instruction);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
throw new Error(`Action "${actionName}" is not supported by this agent`);
|
|
361
|
+
}
|
|
362
|
+
|
|
230
363
|
/**
|
|
231
364
|
* Capture screenshot and return as tool result
|
|
232
365
|
*/
|
|
233
366
|
async function captureScreenshotResult(
|
|
234
367
|
agent: BaseAgent,
|
|
235
368
|
actionName: string,
|
|
369
|
+
actionResult?: unknown,
|
|
236
370
|
): Promise<ToolResult> {
|
|
371
|
+
const content: ToolResult['content'] = [
|
|
372
|
+
{ type: 'text', text: `Action "${actionName}" completed.` },
|
|
373
|
+
];
|
|
374
|
+
|
|
375
|
+
if (actionResult !== undefined) {
|
|
376
|
+
content.push({
|
|
377
|
+
type: 'text',
|
|
378
|
+
text: `Result: ${serializeActionResult(actionResult)}`,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
237
382
|
try {
|
|
238
383
|
const screenshot = await agent.page?.screenshotBase64();
|
|
239
384
|
if (!screenshot) {
|
|
240
|
-
return {
|
|
241
|
-
content: [{ type: 'text', text: `Action "${actionName}" completed.` }],
|
|
242
|
-
};
|
|
385
|
+
return { content };
|
|
243
386
|
}
|
|
244
387
|
|
|
245
388
|
const { mimeType, body } = parseBase64(screenshot);
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
{ type: 'text', text: `Action "${actionName}" completed.` },
|
|
249
|
-
{ type: 'image', data: body, mimeType },
|
|
250
|
-
],
|
|
251
|
-
};
|
|
389
|
+
content.push({ type: 'image', data: body, mimeType });
|
|
390
|
+
return { content };
|
|
252
391
|
} catch (error: unknown) {
|
|
253
392
|
const errorMessage = getErrorMessage(error);
|
|
254
393
|
console.error('Error capturing screenshot:', errorMessage);
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
type: 'text',
|
|
259
|
-
text: `Action "${actionName}" completed (screenshot unavailable: ${errorMessage})`,
|
|
260
|
-
},
|
|
261
|
-
],
|
|
394
|
+
content[0] = {
|
|
395
|
+
type: 'text',
|
|
396
|
+
text: `Action "${actionName}" completed (screenshot unavailable: ${errorMessage})`,
|
|
262
397
|
};
|
|
398
|
+
return { content };
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function serializeActionResult(actionResult: unknown): string {
|
|
403
|
+
if (typeof actionResult === 'string') {
|
|
404
|
+
return actionResult;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
try {
|
|
408
|
+
return JSON.stringify(actionResult);
|
|
409
|
+
} catch {
|
|
410
|
+
return String(actionResult);
|
|
263
411
|
}
|
|
264
412
|
}
|
|
265
413
|
|
|
@@ -323,28 +471,31 @@ export function generateToolsFromActionSpace(
|
|
|
323
471
|
handler: async (args: Record<string, unknown>) => {
|
|
324
472
|
try {
|
|
325
473
|
const agent = await getAgent();
|
|
474
|
+
const normalizedArgs = normalizeActionArgs(args, action.paramSchema);
|
|
475
|
+
let actionResult: unknown;
|
|
326
476
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
errorMessage,
|
|
343
|
-
);
|
|
344
|
-
}
|
|
477
|
+
try {
|
|
478
|
+
actionResult = await executeAction(
|
|
479
|
+
agent,
|
|
480
|
+
action.name,
|
|
481
|
+
normalizedArgs,
|
|
482
|
+
);
|
|
483
|
+
} catch (error: unknown) {
|
|
484
|
+
const errorMessage = getErrorMessage(error);
|
|
485
|
+
console.error(
|
|
486
|
+
`Error executing action "${action.name}":`,
|
|
487
|
+
errorMessage,
|
|
488
|
+
);
|
|
489
|
+
// Return screenshot + warning instead of hard error,
|
|
490
|
+
// so the AI agent can see current state and decide to retry or adjust strategy
|
|
491
|
+
return await captureFailureResult(agent, action.name, errorMessage);
|
|
345
492
|
}
|
|
346
493
|
|
|
347
|
-
return await captureScreenshotResult(
|
|
494
|
+
return await captureScreenshotResult(
|
|
495
|
+
agent,
|
|
496
|
+
action.name,
|
|
497
|
+
actionResult,
|
|
498
|
+
);
|
|
348
499
|
} catch (error: unknown) {
|
|
349
500
|
// Connection/agent errors are still hard errors
|
|
350
501
|
const errorMessage = getErrorMessage(error);
|
package/src/mcp/types.ts
CHANGED
|
@@ -84,6 +84,10 @@ export interface BaseAgent {
|
|
|
84
84
|
page?: {
|
|
85
85
|
screenshotBase64(): Promise<string>;
|
|
86
86
|
};
|
|
87
|
+
callActionInActionSpace?: (
|
|
88
|
+
actionName: string,
|
|
89
|
+
params?: unknown,
|
|
90
|
+
) => Promise<unknown>;
|
|
87
91
|
aiAction?: (
|
|
88
92
|
description: string,
|
|
89
93
|
params?: Record<string, unknown>,
|
package/src/utils.ts
CHANGED
|
@@ -64,23 +64,6 @@ export function assert(condition: any, message?: string): asserts condition {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
type GlobalScope = typeof window | typeof globalThis | typeof self | undefined;
|
|
68
|
-
|
|
69
|
-
export function getGlobalScope(): GlobalScope {
|
|
70
|
-
if (typeof window !== 'undefined') {
|
|
71
|
-
return window;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (typeof globalThis !== 'undefined') {
|
|
75
|
-
return globalThis;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (typeof self !== 'undefined') {
|
|
79
|
-
return self;
|
|
80
|
-
}
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
67
|
let isMcp = false;
|
|
85
68
|
|
|
86
69
|
export function setIsMcp(value: boolean) {
|