rl-simulator-core 1.0.0 → 1.0.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/index.d.mts +1 -1
- package/dist/index.d.ts +45 -45
- package/dist/index.js +3 -4
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/src/index.js +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -451,7 +451,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
451
451
|
|
|
452
452
|
console.log(`Starting Task Loop for ${taskId}`);
|
|
453
453
|
|
|
454
|
-
for (let i = 1; i <=
|
|
454
|
+
for (let i = 1; i <= 5; i++) {
|
|
455
455
|
console.log(`\n=== Starting Iteration ${i}/5 ===`);
|
|
456
456
|
try {
|
|
457
457
|
const data = await executeSingleCycle(targetWeb, targetServer, taskId, i);
|
package/dist/index.d.ts
CHANGED
|
@@ -85,22 +85,22 @@ multiple lines
|
|
|
85
85
|
path.join(process.cwd(), 'ai_traffic.log');
|
|
86
86
|
|
|
87
87
|
const openai = createOpenAI({
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
baseURL: BASE_URL,
|
|
89
|
+
apiKey: API_KEY,
|
|
90
|
+
compatibility: 'compatible',
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
async function queryAI(messages) {
|
|
94
94
|
console.log("Querying AI with history length:", messages.length);
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
try {
|
|
97
97
|
console.log("⏳ 发送请求中...");
|
|
98
98
|
const startTime = Date.now();
|
|
99
99
|
|
|
100
100
|
const { text } = await generateText({
|
|
101
|
-
model: openai.chat(ENDPOINT),
|
|
101
|
+
model: openai.chat(ENDPOINT),
|
|
102
102
|
system: SYSTEM_PROMPT,
|
|
103
|
-
messages: messages,
|
|
103
|
+
messages: messages,
|
|
104
104
|
temperature: 0.1,
|
|
105
105
|
topP: 0.7,
|
|
106
106
|
});
|
|
@@ -108,19 +108,19 @@ async function queryAI(messages) {
|
|
|
108
108
|
const endTime = Date.now();
|
|
109
109
|
console.log("✅ 请求成功! 耗时:", (endTime - startTime) / 1000, "秒");
|
|
110
110
|
console.log("📥 响应内容:", text.substring(0, 500) + "...");
|
|
111
|
-
|
|
111
|
+
|
|
112
112
|
return text;
|
|
113
113
|
|
|
114
114
|
} catch (error) {
|
|
115
115
|
console.error("❌ AI 请求失败!", error);
|
|
116
|
-
return null;
|
|
116
|
+
return null;
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
function parseActions(aiContent) {
|
|
121
121
|
const actions = [];
|
|
122
122
|
const functionRegex = /<function[^>]*=([^>]+)>([\s\S]*?)<\/function[^>]*>/g;
|
|
123
|
-
|
|
123
|
+
|
|
124
124
|
let functionMatch;
|
|
125
125
|
while ((functionMatch = functionRegex.exec(aiContent)) !== null) {
|
|
126
126
|
const functionName = functionMatch[1];
|
|
@@ -147,7 +147,7 @@ function parseActions(aiContent) {
|
|
|
147
147
|
|
|
148
148
|
async function executeAction(page, action) {
|
|
149
149
|
console.log(`Executing action: ${action.name}`, action.params);
|
|
150
|
-
|
|
150
|
+
|
|
151
151
|
const { name, params } = action;
|
|
152
152
|
const viewport = page.viewportSize();
|
|
153
153
|
|
|
@@ -155,7 +155,7 @@ async function executeAction(page, action) {
|
|
|
155
155
|
const transformPoint = (pointStr) => {
|
|
156
156
|
if (!pointStr) return null;
|
|
157
157
|
const [x, y] = pointStr.split(' ').map(Number);
|
|
158
|
-
|
|
158
|
+
|
|
159
159
|
if (!viewport) return { x, y };
|
|
160
160
|
|
|
161
161
|
const actualX = (x / 1000) * viewport.width;
|
|
@@ -172,16 +172,16 @@ async function executeAction(page, action) {
|
|
|
172
172
|
await page.mouse.click(x, y);
|
|
173
173
|
}
|
|
174
174
|
break;
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
case 'left_double':
|
|
177
177
|
if (params.point) {
|
|
178
178
|
const { x, y } = transformPoint(params.point);
|
|
179
179
|
await page.mouse.dblclick(x, y);
|
|
180
180
|
}
|
|
181
181
|
break;
|
|
182
|
-
|
|
182
|
+
|
|
183
183
|
case 'left_triple':
|
|
184
|
-
|
|
184
|
+
if (params.point) {
|
|
185
185
|
const { x, y } = transformPoint(params.point);
|
|
186
186
|
await page.mouse.click(x, y, { clickCount: 3 });
|
|
187
187
|
}
|
|
@@ -207,30 +207,30 @@ async function executeAction(page, action) {
|
|
|
207
207
|
|
|
208
208
|
case 'move_to':
|
|
209
209
|
if (params.point) {
|
|
210
|
-
|
|
211
|
-
|
|
210
|
+
const { x, y } = transformPoint(params.point);
|
|
211
|
+
await page.mouse.move(x, y);
|
|
212
212
|
}
|
|
213
213
|
break;
|
|
214
214
|
|
|
215
215
|
case 'mouse_down':
|
|
216
|
-
|
|
216
|
+
if (params.point) {
|
|
217
217
|
const { x, y } = transformPoint(params.point);
|
|
218
218
|
await page.mouse.move(x, y);
|
|
219
219
|
await page.mouse.down({ button: params.button || 'left' });
|
|
220
|
-
|
|
220
|
+
} else {
|
|
221
221
|
await page.mouse.down({ button: params.button || 'left' });
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
|
|
225
225
|
case 'mouse_up':
|
|
226
|
-
|
|
226
|
+
if (params.point) {
|
|
227
227
|
const { x, y } = transformPoint(params.point);
|
|
228
228
|
await page.mouse.move(x, y);
|
|
229
229
|
await page.mouse.up({ button: params.button || 'left' });
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
230
|
+
} else {
|
|
231
|
+
await page.mouse.up({ button: params.button || 'left' });
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
234
|
|
|
235
235
|
case 'type':
|
|
236
236
|
if (params.content) {
|
|
@@ -238,20 +238,20 @@ async function executeAction(page, action) {
|
|
|
238
238
|
await page.keyboard.type(content);
|
|
239
239
|
}
|
|
240
240
|
break;
|
|
241
|
-
|
|
241
|
+
|
|
242
242
|
case 'hotkey':
|
|
243
243
|
if (params.key) {
|
|
244
244
|
const keys = params.key.split(' ').join('+');
|
|
245
245
|
await page.keyboard.press(keys);
|
|
246
246
|
}
|
|
247
247
|
break;
|
|
248
|
-
|
|
248
|
+
|
|
249
249
|
case 'press':
|
|
250
250
|
if (params.key) {
|
|
251
251
|
await page.keyboard.press(params.key);
|
|
252
252
|
}
|
|
253
253
|
break;
|
|
254
|
-
|
|
254
|
+
|
|
255
255
|
case 'scroll':
|
|
256
256
|
const direction = params.direction;
|
|
257
257
|
if (direction === 'down') {
|
|
@@ -260,7 +260,7 @@ async function executeAction(page, action) {
|
|
|
260
260
|
await page.evaluate(() => window.scrollBy(0, -500));
|
|
261
261
|
}
|
|
262
262
|
break;
|
|
263
|
-
|
|
263
|
+
|
|
264
264
|
case 'wait':
|
|
265
265
|
if (params.time) {
|
|
266
266
|
const ms = parseFloat(params.time) * 1000;
|
|
@@ -271,10 +271,10 @@ async function executeAction(page, action) {
|
|
|
271
271
|
case 'finished':
|
|
272
272
|
console.log("AI Task Finished:", params.content);
|
|
273
273
|
return 'FINISHED';
|
|
274
|
-
|
|
274
|
+
|
|
275
275
|
case 'call_user':
|
|
276
276
|
console.log("AI requests user input:", params.content);
|
|
277
|
-
|
|
277
|
+
return 'FINISHED';
|
|
278
278
|
|
|
279
279
|
default:
|
|
280
280
|
console.warn(`Unknown action: ${name}`);
|
|
@@ -282,7 +282,7 @@ async function executeAction(page, action) {
|
|
|
282
282
|
} catch (e) {
|
|
283
283
|
console.error(`Failed to execute action ${name}:`, e);
|
|
284
284
|
}
|
|
285
|
-
|
|
285
|
+
|
|
286
286
|
return 'CONTINUE';
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -296,7 +296,7 @@ async function runAgent({ targetUrl, taskInput, sessionId, simulatedUserKnownInf
|
|
|
296
296
|
}
|
|
297
297
|
|
|
298
298
|
// 2. Launch Browser
|
|
299
|
-
const browser = await chromium.launch({
|
|
299
|
+
const browser = await chromium.launch({
|
|
300
300
|
headless: true, // Visible for demo/debug
|
|
301
301
|
args: ['--start-maximized'] // Attempt to maximize
|
|
302
302
|
});
|
|
@@ -358,7 +358,7 @@ async function runAgent({ targetUrl, taskInput, sessionId, simulatedUserKnownInf
|
|
|
358
358
|
{ type: 'text', text: promptText },
|
|
359
359
|
{ type: 'image', image: screenshotBase64 }
|
|
360
360
|
];
|
|
361
|
-
|
|
361
|
+
|
|
362
362
|
conversationHistory.push({
|
|
363
363
|
role: 'user',
|
|
364
364
|
content: userContent
|
|
@@ -367,7 +367,7 @@ async function runAgent({ targetUrl, taskInput, sessionId, simulatedUserKnownInf
|
|
|
367
367
|
// 3. Query AI
|
|
368
368
|
console.log("Querying AI...");
|
|
369
369
|
const aiContent = await queryAI(conversationHistory);
|
|
370
|
-
|
|
370
|
+
|
|
371
371
|
if (!aiContent) {
|
|
372
372
|
console.error("Invalid AI response. Retrying...");
|
|
373
373
|
await page.waitForTimeout(2000);
|
|
@@ -398,7 +398,7 @@ async function runAgent({ targetUrl, taskInput, sessionId, simulatedUserKnownInf
|
|
|
398
398
|
break;
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
|
-
|
|
401
|
+
|
|
402
402
|
// 6. Short wait between cycles
|
|
403
403
|
if (isRunning) {
|
|
404
404
|
await page.waitForTimeout(3000);
|
|
@@ -438,7 +438,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
438
438
|
const datetime = getFormattedDate();
|
|
439
439
|
const folderName = `${taskId}_${datetime}`;
|
|
440
440
|
const outputDir = path.join(process.cwd(), folderName);
|
|
441
|
-
|
|
441
|
+
|
|
442
442
|
// Create root output directory
|
|
443
443
|
if (!fs.existsSync(outputDir)) {
|
|
444
444
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
@@ -451,12 +451,12 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
451
451
|
|
|
452
452
|
console.log(`Starting Task Loop for ${taskId}`);
|
|
453
453
|
|
|
454
|
-
for (let i = 1; i <=
|
|
454
|
+
for (let i = 1; i <= 5; i++) {
|
|
455
455
|
console.log(`\n=== Starting Iteration ${i}/5 ===`);
|
|
456
456
|
try {
|
|
457
457
|
const data = await executeSingleCycle(targetWeb, targetServer, taskId, i);
|
|
458
458
|
const { sessionId, agentMessages, ...rest } = data;
|
|
459
|
-
|
|
459
|
+
|
|
460
460
|
// Create session subdirectory
|
|
461
461
|
const sessionDir = path.join(outputDir, sessionId);
|
|
462
462
|
if (!fs.existsSync(sessionDir)) {
|
|
@@ -465,13 +465,13 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
465
465
|
|
|
466
466
|
// Save messages.json
|
|
467
467
|
fs.writeFileSync(
|
|
468
|
-
path.join(sessionDir, 'messages.json'),
|
|
468
|
+
path.join(sessionDir, 'messages.json'),
|
|
469
469
|
JSON.stringify(agentMessages, null, 2)
|
|
470
470
|
);
|
|
471
471
|
|
|
472
472
|
// Save result.json
|
|
473
473
|
fs.writeFileSync(
|
|
474
|
-
path.join(sessionDir, 'result.json'),
|
|
474
|
+
path.join(sessionDir, 'result.json'),
|
|
475
475
|
JSON.stringify(rest, null, 2)
|
|
476
476
|
);
|
|
477
477
|
|
|
@@ -493,7 +493,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
493
493
|
console.log("Packaging results into ZIP...");
|
|
494
494
|
const zipName = `${folderName}.zip`;
|
|
495
495
|
const zipPath = path.join(process.cwd(), zipName);
|
|
496
|
-
|
|
496
|
+
|
|
497
497
|
try {
|
|
498
498
|
const zip = new AdmZip();
|
|
499
499
|
zip.addLocalFolder(outputDir, folderName);
|
|
@@ -502,7 +502,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
502
502
|
} catch (zipError) {
|
|
503
503
|
console.error("Failed to create zip:", zipError);
|
|
504
504
|
}
|
|
505
|
-
|
|
505
|
+
|
|
506
506
|
return zipPath;
|
|
507
507
|
}
|
|
508
508
|
|
|
@@ -520,7 +520,7 @@ async function executeSingleCycle(targetWeb, targetServer, taskId, iteration) {
|
|
|
520
520
|
console.log("Calling Start API...");
|
|
521
521
|
const startUrl = `${targetServer}/api/tasks/${taskId}/start`;
|
|
522
522
|
const startData = await fetchJson(startUrl, { method: 'POST' });
|
|
523
|
-
|
|
523
|
+
|
|
524
524
|
if (startData.code !== 200) {
|
|
525
525
|
throw new Error(`Start API failed: ${JSON.stringify(startData)}`);
|
|
526
526
|
}
|
package/dist/index.js
CHANGED
|
@@ -417,7 +417,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
417
417
|
if (!targetWeb.startsWith("http")) targetWeb = "http://" + targetWeb;
|
|
418
418
|
if (!targetServer.startsWith("http")) targetServer = "http://" + targetServer;
|
|
419
419
|
console.log(`Starting Task Loop for ${taskId}`);
|
|
420
|
-
for (let i = 1; i <=
|
|
420
|
+
for (let i = 1; i <= 5; i++) {
|
|
421
421
|
console.log(`
|
|
422
422
|
=== Starting Iteration ${i}/5 ===`);
|
|
423
423
|
try {
|
|
@@ -476,7 +476,7 @@ async function executeSingleCycle(targetWeb, targetServer, taskId, iteration) {
|
|
|
476
476
|
}
|
|
477
477
|
const { session_id: sessionId, TaskJson } = startData.data;
|
|
478
478
|
console.log(`Setup for session ${sessionId}...`);
|
|
479
|
-
const setupUrl = `${targetServer}/api/setup
|
|
479
|
+
const setupUrl = `${targetServer}/api/tasks/setup`;
|
|
480
480
|
await fetchJson(setupUrl, {
|
|
481
481
|
method: "POST",
|
|
482
482
|
headers: { "Content-Type": "application/json" },
|
|
@@ -490,13 +490,12 @@ async function executeSingleCycle(targetWeb, targetServer, taskId, iteration) {
|
|
|
490
490
|
simulatedUserKnownInfo: TaskJson.task.simulated_user_known_info
|
|
491
491
|
});
|
|
492
492
|
console.log("Verifying result...");
|
|
493
|
-
const verifyUrl = `${targetServer}/api/verify
|
|
493
|
+
const verifyUrl = `${targetServer}/api/tasks/verify`;
|
|
494
494
|
const verifyData = await fetchJson(verifyUrl, {
|
|
495
495
|
method: "POST",
|
|
496
496
|
headers: { "Content-Type": "application/json" },
|
|
497
497
|
body: JSON.stringify({ task_id: taskId, session_id: sessionId })
|
|
498
498
|
});
|
|
499
|
-
console.log("Verifying result...", verifyData);
|
|
500
499
|
return {
|
|
501
500
|
iteration,
|
|
502
501
|
sessionId,
|
package/dist/index.mjs
CHANGED
|
@@ -383,7 +383,7 @@ async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
383
383
|
if (!targetWeb.startsWith("http")) targetWeb = "http://" + targetWeb;
|
|
384
384
|
if (!targetServer.startsWith("http")) targetServer = "http://" + targetServer;
|
|
385
385
|
console.log(`Starting Task Loop for ${taskId}`);
|
|
386
|
-
for (let i = 1; i <=
|
|
386
|
+
for (let i = 1; i <= 5; i++) {
|
|
387
387
|
console.log(`
|
|
388
388
|
=== Starting Iteration ${i}/5 ===`);
|
|
389
389
|
try {
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -43,7 +43,7 @@ export async function runTaskLoop(targetWeb, targetServer, taskId) {
|
|
|
43
43
|
|
|
44
44
|
console.log(`Starting Task Loop for ${taskId}`);
|
|
45
45
|
|
|
46
|
-
for (let i = 1; i <=
|
|
46
|
+
for (let i = 1; i <= 5; i++) {
|
|
47
47
|
console.log(`\n=== Starting Iteration ${i}/5 ===`);
|
|
48
48
|
try {
|
|
49
49
|
const data = await executeSingleCycle(targetWeb, targetServer, taskId, i);
|