@xkwdstore/agent 2.0.2 → 2.0.4

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.
@@ -1,91 +1,91 @@
1
- 'use strict';
2
-
3
- /**
4
- * Tweet action — compose and post a tweet.
5
- * Ported from backend/routes/browser.js doTweet().
6
- */
7
-
8
- const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage, waitForLoggedIn, injectCookies } = require('./helpers');
9
-
10
- async function tweet(page, payload) {
11
- const { text } = payload;
12
- if (!text) throw new Error('Tweet text required');
13
-
14
- // Navigate to compose page
15
- await page.goto('https://x.com/compose/post', { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
16
- await randDelay(2000, 4000);
17
-
18
- // Check if logged in
19
- const stage = await detectStage(page);
20
- if (stage === 'login_required' || stage === 'login_username_step' || stage === 'login_password_step') {
21
- throw new Error('Not logged in — open browser and login first');
22
- }
23
-
24
- // Wait for the compose textarea
25
- const editorSelector = '[data-testid="tweetTextarea_0"]';
26
- await page.waitForSelector(editorSelector, { timeout: 15000 });
27
- const editor = page.locator(editorSelector).first();
28
- await editor.click();
29
- await humanDelay(300, 600);
30
-
31
- // Detect RTL (Arabic) text
32
- const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(text);
33
- if (isRTL) {
34
- await page.evaluate((t) => {
35
- const el = document.querySelector('[data-testid="tweetTextarea_0"]');
36
- if (el) { el.setAttribute('dir', 'rtl'); }
37
- }, text);
38
- // Type word by word for RTL
39
- const words = text.split(' ');
40
- for (const word of words) {
41
- await page.evaluate((w) => {
42
- document.execCommand('insertText', false, w + ' ');
43
- }, word);
44
- await humanDelay(50, 120);
45
- }
46
- } else {
47
- // Humanized typing with randomized delays, pauses, and rare typos
48
- await humanType(page, text, { delayMin: 50, delayMax: 150 });
49
- }
50
-
51
- await humanDelay(500, 1000);
52
-
53
- // Click the post button
54
- const postSelectors = [
55
- '[data-testid="tweetButton"]',
56
- '[data-testid="tweetButtonInline"]',
57
- ];
58
- let posted = false;
59
- for (const sel of postSelectors) {
60
- const btn = page.locator(sel).first();
61
- if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
62
- await humanClick(page, btn);
63
- posted = true;
64
- break;
65
- }
66
- }
67
-
68
- if (!posted) {
69
- // Fallback: Ctrl+Enter
70
- await page.keyboard.press('Control+Enter');
71
- }
72
-
73
- // Wait for compose box to close (success indicator)
74
- try {
75
- await page.waitForSelector(editorSelector, { state: 'detached', timeout: 15000 });
76
- } catch {
77
- // Retry with Ctrl+Enter
78
- await page.keyboard.press('Control+Enter');
79
- await sleep(3000);
80
- }
81
-
82
- // Check for failure toast
83
- const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
84
- if (/something went wrong|try again|failed/i.test(bodyText)) {
85
- throw new Error('Tweet submit reported a failure toast');
86
- }
87
-
88
- return { success: true, message: 'Tweet posted' };
89
- }
90
-
1
+ 'use strict';
2
+
3
+ /**
4
+ * Tweet action — compose and post a tweet.
5
+ * Ported from backend/routes/browser.js doTweet().
6
+ */
7
+
8
+ const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage, waitForLoggedIn, injectCookies } = require('./helpers');
9
+
10
+ async function tweet(page, payload) {
11
+ const { text } = payload;
12
+ if (!text) throw new Error('Tweet text required');
13
+
14
+ // Navigate to compose page
15
+ await page.goto('https://x.com/compose/post', { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
16
+ await randDelay(2000, 4000);
17
+
18
+ // Check if logged in
19
+ const stage = await detectStage(page);
20
+ if (stage === 'login_required' || stage === 'login_username_step' || stage === 'login_password_step') {
21
+ throw new Error('Not logged in — open browser and login first');
22
+ }
23
+
24
+ // Wait for the compose textarea
25
+ const editorSelector = '[data-testid="tweetTextarea_0"]';
26
+ await page.waitForSelector(editorSelector, { timeout: 15000 });
27
+ const editor = page.locator(editorSelector).first();
28
+ await editor.click();
29
+ await humanDelay(300, 600);
30
+
31
+ // Detect RTL (Arabic) text
32
+ const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(text);
33
+ if (isRTL) {
34
+ await page.evaluate((t) => {
35
+ const el = document.querySelector('[data-testid="tweetTextarea_0"]');
36
+ if (el) { el.setAttribute('dir', 'rtl'); }
37
+ }, text);
38
+ // Type word by word for RTL
39
+ const words = text.split(' ');
40
+ for (const word of words) {
41
+ await page.evaluate((w) => {
42
+ document.execCommand('insertText', false, w + ' ');
43
+ }, word);
44
+ await humanDelay(50, 120);
45
+ }
46
+ } else {
47
+ // Humanized typing with randomized delays, pauses, and rare typos
48
+ await humanType(page, text, { delayMin: 50, delayMax: 150 });
49
+ }
50
+
51
+ await humanDelay(500, 1000);
52
+
53
+ // Click the post button
54
+ const postSelectors = [
55
+ '[data-testid="tweetButton"]',
56
+ '[data-testid="tweetButtonInline"]',
57
+ ];
58
+ let posted = false;
59
+ for (const sel of postSelectors) {
60
+ const btn = page.locator(sel).first();
61
+ if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
62
+ await humanClick(page, btn);
63
+ posted = true;
64
+ break;
65
+ }
66
+ }
67
+
68
+ if (!posted) {
69
+ // Fallback: Ctrl+Enter
70
+ await page.keyboard.press('Control+Enter');
71
+ }
72
+
73
+ // Wait for compose box to close (success indicator)
74
+ try {
75
+ await page.waitForSelector(editorSelector, { state: 'detached', timeout: 15000 });
76
+ } catch {
77
+ // Retry with Ctrl+Enter
78
+ await page.keyboard.press('Control+Enter');
79
+ await sleep(3000);
80
+ }
81
+
82
+ // Check for failure toast
83
+ const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
84
+ if (/something went wrong|try again|failed/i.test(bodyText)) {
85
+ throw new Error('Tweet submit reported a failure toast');
86
+ }
87
+
88
+ return { success: true, message: 'Tweet posted' };
89
+ }
90
+
91
91
  module.exports = { tweet };
@@ -1,32 +1,32 @@
1
- 'use strict';
2
-
3
- /**
4
- * Phase 8 — Canonical challenge encoding (shared between client and server).
5
- *
6
- * The canonical form is a stable, unambiguous JSON encoding of the binding
7
- * fields, sorted by key, with no extra whitespace. This ensures the client
8
- * and server produce identical signature inputs.
9
- */
10
-
11
- /**
12
- * Build the canonical encoded payload that the client must sign.
13
- *
14
- * @param {object} fields — { protocolVersion, connectionId, nonce, issuedAt, expiresAt, serverOrigin, deviceId, publicKeyFingerprint }
15
- * @returns {Buffer} canonical bytes to sign
16
- */
17
- function canonicalChallengeInput(fields) {
18
- const payload = {
19
- protocolVersion: fields.protocolVersion,
20
- connectionId: fields.connectionId,
21
- nonce: fields.nonce,
22
- issuedAt: fields.issuedAt,
23
- expiresAt: fields.expiresAt,
24
- serverOrigin: fields.serverOrigin,
25
- deviceId: fields.deviceId,
26
- publicKeyFingerprint: fields.publicKeyFingerprint,
27
- };
28
- // Stable key order (alphabetical), no extra whitespace.
29
- return Buffer.from(JSON.stringify(payload, Object.keys(payload).sort()), 'utf8');
30
- }
31
-
1
+ 'use strict';
2
+
3
+ /**
4
+ * Phase 8 — Canonical challenge encoding (shared between client and server).
5
+ *
6
+ * The canonical form is a stable, unambiguous JSON encoding of the binding
7
+ * fields, sorted by key, with no extra whitespace. This ensures the client
8
+ * and server produce identical signature inputs.
9
+ */
10
+
11
+ /**
12
+ * Build the canonical encoded payload that the client must sign.
13
+ *
14
+ * @param {object} fields — { protocolVersion, connectionId, nonce, issuedAt, expiresAt, serverOrigin, deviceId, publicKeyFingerprint }
15
+ * @returns {Buffer} canonical bytes to sign
16
+ */
17
+ function canonicalChallengeInput(fields) {
18
+ const payload = {
19
+ protocolVersion: fields.protocolVersion,
20
+ connectionId: fields.connectionId,
21
+ nonce: fields.nonce,
22
+ issuedAt: fields.issuedAt,
23
+ expiresAt: fields.expiresAt,
24
+ serverOrigin: fields.serverOrigin,
25
+ deviceId: fields.deviceId,
26
+ publicKeyFingerprint: fields.publicKeyFingerprint,
27
+ };
28
+ // Stable key order (alphabetical), no extra whitespace.
29
+ return Buffer.from(JSON.stringify(payload, Object.keys(payload).sort()), 'utf8');
30
+ }
31
+
32
32
  module.exports = { canonicalChallengeInput };
@@ -1,155 +1,155 @@
1
- 'use strict';
2
-
3
- /**
4
- * Phase 4 — System Chrome detection.
5
- *
6
- * Detects the system-installed Google Chrome. Does NOT download Chromium or
7
- * Firefox. Does NOT run `npx playwright install`. Does NOT extract browser
8
- * ZIP files.
9
- *
10
- * When Chrome is missing, fails clearly with an actionable error.
11
- */
12
-
13
- const fs = require('fs');
14
- const os = require('os');
15
- const path = require('path');
16
- const { execSync } = require('child_process');
17
-
18
- /**
19
- * Known Chrome installation paths by platform.
20
- */
21
- const CHROME_PATHS = {
22
- win32: [
23
- 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
24
- 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
25
- path.join(process.env.LOCALAPPDATA || '', 'Google\\Chrome\\Application\\chrome.exe'),
26
- ],
27
- darwin: [
28
- '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
29
- ],
30
- linux: [
31
- '/usr/bin/google-chrome',
32
- '/usr/bin/google-chrome-stable',
33
- '/usr/bin/chromium',
34
- '/usr/bin/chromium-browser',
35
- '/usr/bin/google-chrome-dev',
36
- '/usr/bin/google-chrome-beta',
37
- '/snap/bin/chromium',
38
- ],
39
- };
40
-
41
- /**
42
- * Resolve the system Chrome executable path.
43
- *
44
- * @returns {string|null} path to chrome executable, or null if not found
45
- */
46
- function resolveChrome() {
47
- const platform = os.platform();
48
- const paths = CHROME_PATHS[platform] || [];
49
-
50
- // Check known paths first
51
- for (const p of paths) {
52
- if (p && fs.existsSync(p)) {
53
- return p;
54
- }
55
- }
56
-
57
- // On Linux, also try `which google-chrome` as a fallback
58
- if (platform === 'linux') {
59
- try {
60
- const result = execSync('which google-chrome 2>/dev/null || which google-chrome-stable 2>/dev/null || which chromium 2>/dev/null || which chromium-browser 2>/dev/null', {
61
- encoding: 'utf8',
62
- timeout: 5000,
63
- stdio: ['pipe', 'pipe', 'ignore'],
64
- }).trim();
65
- if (result && fs.existsSync(result)) {
66
- return result;
67
- }
68
- } catch {}
69
- }
70
-
71
- // On macOS, also try `mdfind` for non-standard install locations
72
- if (platform === 'darwin') {
73
- try {
74
- const result = execSync('mdfind "kMDItemCFBundleIdentifier == com.google.Chrome" 2>/dev/null | head -1', {
75
- encoding: 'utf8',
76
- timeout: 5000,
77
- stdio: ['pipe', 'pipe', 'ignore'],
78
- }).trim();
79
- if (result) {
80
- const chromePath = path.join(result, 'Contents', 'MacOS', 'Google Chrome');
81
- if (fs.existsSync(chromePath)) {
82
- return chromePath;
83
- }
84
- }
85
- } catch {}
86
- }
87
-
88
- return null;
89
- }
90
-
91
- /**
92
- * Detect the Chrome version string.
93
- *
94
- * @param {string} chromePath
95
- * @returns {string|null} version string (e.g. "142.0.7339.4") or null
96
- */
97
- function detectChromeVersion(chromePath) {
98
- if (!chromePath) return null;
99
- const platform = os.platform();
100
-
101
- try {
102
- if (platform === 'win32') {
103
- // Use wmic or powershell to get version
104
- const result = execSync(`powershell -NoProfile -Command "(Get-Item '${chromePath}').VersionInfo.FileVersion"`, {
105
- encoding: 'utf8',
106
- timeout: 10000,
107
- windowsHide: true,
108
- stdio: ['pipe', 'pipe', 'ignore'],
109
- }).trim();
110
- return result || null;
111
- }
112
- if (platform === 'darwin' || platform === 'linux') {
113
- const result = execSync(`"${chromePath}" --version`, {
114
- encoding: 'utf8',
115
- timeout: 10000,
116
- windowsHide: true,
117
- stdio: ['pipe', 'pipe', 'ignore'],
118
- }).trim();
119
- // Parse "Google Chrome 142.0.7339.4" → "142"
120
- const match = result.match(/(\d+)\./);
121
- return match ? match[1] : result;
122
- }
123
- } catch {
124
- return null;
125
- }
126
- return null;
127
- }
128
-
129
- /**
130
- * Resolve Chrome or fail with a clear, actionable error.
131
- *
132
- * @returns {object} { chromePath, chromeVersion } or throws
133
- */
134
- function resolveChromeOrFail() {
135
- const chromePath = resolveChrome();
136
- if (!chromePath) {
137
- const err = new Error(
138
- 'Google Chrome was not found.\n' +
139
- 'Install Chrome and run the command again.\n' +
140
- 'No browser was downloaded or installed automatically.'
141
- );
142
- err.code = 'CHROME_NOT_FOUND';
143
- throw err;
144
- }
145
-
146
- const chromeVersion = detectChromeVersion(chromePath);
147
- return { chromePath, chromeVersion };
148
- }
149
-
150
- module.exports = {
151
- resolveChrome,
152
- detectChromeVersion,
153
- resolveChromeOrFail,
154
- CHROME_PATHS,
1
+ 'use strict';
2
+
3
+ /**
4
+ * Phase 4 — System Chrome detection.
5
+ *
6
+ * Detects the system-installed Google Chrome. Does NOT download Chromium or
7
+ * Firefox. Does NOT run `npx playwright install`. Does NOT extract browser
8
+ * ZIP files.
9
+ *
10
+ * When Chrome is missing, fails clearly with an actionable error.
11
+ */
12
+
13
+ const fs = require('fs');
14
+ const os = require('os');
15
+ const path = require('path');
16
+ const { execSync } = require('child_process');
17
+
18
+ /**
19
+ * Known Chrome installation paths by platform.
20
+ */
21
+ const CHROME_PATHS = {
22
+ win32: [
23
+ 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
24
+ 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
25
+ path.join(process.env.LOCALAPPDATA || '', 'Google\\Chrome\\Application\\chrome.exe'),
26
+ ],
27
+ darwin: [
28
+ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
29
+ ],
30
+ linux: [
31
+ '/usr/bin/google-chrome',
32
+ '/usr/bin/google-chrome-stable',
33
+ '/usr/bin/chromium',
34
+ '/usr/bin/chromium-browser',
35
+ '/usr/bin/google-chrome-dev',
36
+ '/usr/bin/google-chrome-beta',
37
+ '/snap/bin/chromium',
38
+ ],
39
+ };
40
+
41
+ /**
42
+ * Resolve the system Chrome executable path.
43
+ *
44
+ * @returns {string|null} path to chrome executable, or null if not found
45
+ */
46
+ function resolveChrome() {
47
+ const platform = os.platform();
48
+ const paths = CHROME_PATHS[platform] || [];
49
+
50
+ // Check known paths first
51
+ for (const p of paths) {
52
+ if (p && fs.existsSync(p)) {
53
+ return p;
54
+ }
55
+ }
56
+
57
+ // On Linux, also try `which google-chrome` as a fallback
58
+ if (platform === 'linux') {
59
+ try {
60
+ const result = execSync('which google-chrome 2>/dev/null || which google-chrome-stable 2>/dev/null || which chromium 2>/dev/null || which chromium-browser 2>/dev/null', {
61
+ encoding: 'utf8',
62
+ timeout: 5000,
63
+ stdio: ['pipe', 'pipe', 'ignore'],
64
+ }).trim();
65
+ if (result && fs.existsSync(result)) {
66
+ return result;
67
+ }
68
+ } catch {}
69
+ }
70
+
71
+ // On macOS, also try `mdfind` for non-standard install locations
72
+ if (platform === 'darwin') {
73
+ try {
74
+ const result = execSync('mdfind "kMDItemCFBundleIdentifier == com.google.Chrome" 2>/dev/null | head -1', {
75
+ encoding: 'utf8',
76
+ timeout: 5000,
77
+ stdio: ['pipe', 'pipe', 'ignore'],
78
+ }).trim();
79
+ if (result) {
80
+ const chromePath = path.join(result, 'Contents', 'MacOS', 'Google Chrome');
81
+ if (fs.existsSync(chromePath)) {
82
+ return chromePath;
83
+ }
84
+ }
85
+ } catch {}
86
+ }
87
+
88
+ return null;
89
+ }
90
+
91
+ /**
92
+ * Detect the Chrome version string.
93
+ *
94
+ * @param {string} chromePath
95
+ * @returns {string|null} version string (e.g. "142.0.7339.4") or null
96
+ */
97
+ function detectChromeVersion(chromePath) {
98
+ if (!chromePath) return null;
99
+ const platform = os.platform();
100
+
101
+ try {
102
+ if (platform === 'win32') {
103
+ // Use wmic or powershell to get version
104
+ const result = execSync(`powershell -NoProfile -Command "(Get-Item '${chromePath}').VersionInfo.FileVersion"`, {
105
+ encoding: 'utf8',
106
+ timeout: 10000,
107
+ windowsHide: true,
108
+ stdio: ['pipe', 'pipe', 'ignore'],
109
+ }).trim();
110
+ return result || null;
111
+ }
112
+ if (platform === 'darwin' || platform === 'linux') {
113
+ const result = execSync(`"${chromePath}" --version`, {
114
+ encoding: 'utf8',
115
+ timeout: 10000,
116
+ windowsHide: true,
117
+ stdio: ['pipe', 'pipe', 'ignore'],
118
+ }).trim();
119
+ // Parse "Google Chrome 142.0.7339.4" → "142"
120
+ const match = result.match(/(\d+)\./);
121
+ return match ? match[1] : result;
122
+ }
123
+ } catch {
124
+ return null;
125
+ }
126
+ return null;
127
+ }
128
+
129
+ /**
130
+ * Resolve Chrome or fail with a clear, actionable error.
131
+ *
132
+ * @returns {object} { chromePath, chromeVersion } or throws
133
+ */
134
+ function resolveChromeOrFail() {
135
+ const chromePath = resolveChrome();
136
+ if (!chromePath) {
137
+ const err = new Error(
138
+ 'Google Chrome was not found.\n' +
139
+ 'Install Chrome and run the command again.\n' +
140
+ 'No browser was downloaded or installed automatically.'
141
+ );
142
+ err.code = 'CHROME_NOT_FOUND';
143
+ throw err;
144
+ }
145
+
146
+ const chromeVersion = detectChromeVersion(chromePath);
147
+ return { chromePath, chromeVersion };
148
+ }
149
+
150
+ module.exports = {
151
+ resolveChrome,
152
+ detectChromeVersion,
153
+ resolveChromeOrFail,
154
+ CHROME_PATHS,
155
155
  };