forked from NirDiamant/agents-towards-production
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-entry-example.js
More file actions
85 lines (75 loc) · 2.67 KB
/
Copy pathdata-entry-example.js
File metadata and controls
85 lines (75 loc) · 2.67 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
const { chromium } = require('playwright-core');
const axios = require('axios');
const ANCHOR_API_KEY = process.env.ANCHOR_API_KEY;
if (!ANCHOR_API_KEY) {
console.error("❌ ANCHOR_API_KEY is not set. Run:");
console.error("export ANCHOR_API_KEY=<your-api-key>");
process.exit(1);
}
async function createSession(ANCHOR_API_KEY) {
const url = "https://api.anchorbrowser.io/v1/sessions";
const payload = {
session: {
timeout: {
max_duration: 4,
idle_timeout: 2
}
}
};
const headers = {
"anchor-api-key": ANCHOR_API_KEY,
"Content-Type": "application/json"
};
try {
// Initialize session
const response = await axios.post(url, payload, { headers });
return response.data;
} catch (error) {
if (error.response) {
const { status, statusText, data } = error.response;
if (status === 401) {
console.error("❌ Authentication failed: Invalid or missing API key");
} else {
console.error(`❌ HTTP Error ${status}: ${statusText}`);
console.error("Response data:", data);
}
} else if (error.request) {
console.error("❌ Network error: No response from server");
} else {
console.error("❌ Error:", error.message);
}
process.exit(1);
}
}
async function executeFormSubmission(connectionString) {
const browser = await chromium.connectOverCDP(connectionString);
const context = browser.contexts()[0];
const ai = context.serviceWorkers()[0];
const page = context.pages()[0];
// Navigate to resume
await page.goto('https://www.wix.com/demone2/nicol-rider');
// Execute AI task
const taskPayload = {
"prompt": "Read the resume, understand the details, and complete the form at https://formspree.io/library/donation/charity-donation-form/preview.html as if you were her. Limit the donation to $10.",
"provider": "openai",
"model": "gpt-4",
"highlight_elements": true
};
const result = await ai.evaluate(JSON.stringify(taskPayload));
console.log("Task result:", result);
await browser.close();
return result;
}
(async () => {
try {
const sessionData = await createSession(ANCHOR_API_KEY);
console.log(sessionData);
const connectionString = sessionData.data.cdp_url;
const result = await executeFormSubmission(connectionString);
console.log(result);
process.exit(0);
} catch (error) {
console.error("Error:", error.message);
process.exit(1);
}
})();