mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
4fe9a73ebc
* refactor: migrate adapter imports to package exports Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.) with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.) across all 484 adapter files. This decouples adapter import resolution from directory structure: - User CLIs in ~/.opencli/clis/ resolve via node_modules symlink - Internal adapters resolve via Node.js self-referencing - No more shim files needed for import resolution Changes: - package.json: add sub-path exports for all public modules - clis/**: replace relative imports with @jackwener/opencli/... - discovery.ts: simplify ensureUserCliCompatShims to symlink-only - registry-api.ts: export CommandArgs type - Remove root-level shim directories (browser/, download/, pipeline/) - Remove shim entries from tsconfig.json include and package.json files * test: add regression tests for package exports Prevents regressions like #788/#791 by: 1. Scanning all adapter files for forbidden relative imports (../../src/, ../../browser/, etc.) — fails if any remain 2. Verifying every package.json export maps to an existing source file 18 new test cases. * fix: use junction on Windows + broaden test patterns - discovery.ts: use 'junction' symlink type on Windows (no admin required) - package-exports.test.ts: generalize forbidden patterns to catch any depth of ../ traversal (not just ../../ and ../../../) * fix: update stale vi.mock/importActual paths in adapter tests Test files still used old relative paths for vi.mock() and vi.importActual() calls. Updated 5 test files to use package exports. Also broadened regression test patterns to catch mock/importActual paths. * fix: use rm instead of unlink for symlink cleanup, add warn on failure Addresses review feedback from Astro-Han: - rm() handles both symlinks and stale directories (unlink fails on dirs) - Log a warning when symlink creation fails instead of silent catch * docs: update import examples to use package exports Update all documentation, contributing guides, and skills to use @jackwener/opencli/registry instead of ../../src/registry.js. Without this, users following the docs would write adapters with broken imports since the old shim files are no longer created.
215 lines
8.4 KiB
TypeScript
215 lines
8.4 KiB
TypeScript
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
|
|
cli({
|
|
site: 'twitter',
|
|
name: 'accept',
|
|
description: 'Auto-accept DM requests containing specific keywords',
|
|
domain: 'x.com',
|
|
strategy: Strategy.UI,
|
|
browser: true,
|
|
timeoutSeconds: 600, // 10 min — batch operation iterating many conversations
|
|
args: [
|
|
{ name: 'query', type: 'string', required: true, positional: true, help: 'Keywords to match (comma-separated for OR, e.g. "群,微信")' },
|
|
{ name: 'max', type: 'int', required: false, default: 20, help: 'Maximum number of requests to accept (default: 20)' },
|
|
],
|
|
columns: ['index', 'status', 'user', 'message'],
|
|
func: async (page: IPage | null, kwargs: any) => {
|
|
if (!page) throw new CommandExecutionError('Browser session required for twitter accept');
|
|
|
|
const keywords: string[] = kwargs.query.split(',').map((k: string) => k.trim()).filter(Boolean);
|
|
const maxAccepts: number = kwargs.max ?? 20;
|
|
const results: Array<{ index: number; status: string; user: string; message: string }> = [];
|
|
let acceptCount = 0;
|
|
// Track already-visited conversations to avoid infinite loops
|
|
const visited = new Set<string>();
|
|
|
|
for (let round = 0; round < maxAccepts + 50; round++) {
|
|
if (acceptCount >= maxAccepts) break;
|
|
|
|
// Step 1: Navigate to DM requests page
|
|
await page.goto('https://x.com/messages/requests');
|
|
await page.wait(4);
|
|
|
|
// Step 2: Get conversations with scroll-to-load
|
|
const convInfo = await page.evaluate(`(async () => {
|
|
try {
|
|
// Wait for initial items
|
|
let attempts = 0;
|
|
while (attempts < 10) {
|
|
const convs = document.querySelectorAll('[data-testid="conversation"]');
|
|
if (convs.length > 0) break;
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
attempts++;
|
|
}
|
|
|
|
// Scroll to load more
|
|
const seenCount = new Set();
|
|
let noNewCount = 0;
|
|
for (let scroll = 0; scroll < 20; scroll++) {
|
|
const convs = Array.from(document.querySelectorAll('[data-testid="conversation"]'));
|
|
const prevSize = seenCount.size;
|
|
convs.forEach((_, i) => seenCount.add(i));
|
|
if (convs.length >= ${maxAccepts + 10}) break;
|
|
|
|
// Scroll last item into view
|
|
if (convs.length > 0) {
|
|
convs[convs.length - 1].scrollIntoView({ behavior: 'instant', block: 'end' });
|
|
}
|
|
await new Promise(r => setTimeout(r, 1500));
|
|
|
|
if (seenCount.size <= prevSize) {
|
|
noNewCount++;
|
|
if (noNewCount >= 3) break;
|
|
} else {
|
|
noNewCount = 0;
|
|
}
|
|
}
|
|
|
|
const convs = Array.from(document.querySelectorAll('[data-testid="conversation"]'));
|
|
if (convs.length === 0) return { ok: false, count: 0, items: [] };
|
|
|
|
const items = convs.map((conv, idx) => {
|
|
const text = conv.innerText || '';
|
|
const link = conv.querySelector('a[href]');
|
|
const href = link ? link.href : '';
|
|
const lines = text.split('\\n').filter(l => l.trim());
|
|
const user = lines[0] || 'Unknown';
|
|
return { idx, text, href, user };
|
|
});
|
|
return { ok: true, count: convs.length, items };
|
|
} catch(e) {
|
|
return { ok: false, error: String(e), count: 0, items: [] };
|
|
}
|
|
})()`);
|
|
|
|
if (!convInfo?.ok || convInfo.count === 0) {
|
|
if (results.length === 0) {
|
|
results.push({ index: 1, status: 'info', user: 'System', message: 'No message requests found' });
|
|
}
|
|
break;
|
|
}
|
|
|
|
let foundInThisRound = false;
|
|
|
|
// Step 3: Find first unvisited conversation with keyword match in preview
|
|
for (const item of convInfo.items) {
|
|
if (acceptCount >= maxAccepts) break;
|
|
const convKey = item.href || `conv-${item.idx}`;
|
|
if (visited.has(convKey)) continue;
|
|
visited.add(convKey);
|
|
|
|
// Check if preview text contains any keyword
|
|
const previewMatch = keywords.some((k: string) => item.text.includes(k));
|
|
if (!previewMatch) continue;
|
|
|
|
// Step 4: Click this conversation to open it
|
|
const clickResult = await page.evaluate(`(async () => {
|
|
try {
|
|
const convs = Array.from(document.querySelectorAll('[data-testid="conversation"]'));
|
|
const conv = convs[${item.idx}];
|
|
if (!conv) return { ok: false, error: 'Conversation element not found' };
|
|
conv.click();
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
return { ok: true };
|
|
} catch(e) {
|
|
return { ok: false, error: String(e) };
|
|
}
|
|
})()`);
|
|
|
|
if (!clickResult?.ok) continue;
|
|
|
|
// Wait for conversation to load
|
|
await page.wait(2);
|
|
|
|
// Step 5: Read full chat content and find Accept button
|
|
const res = await page.evaluate(`(async () => {
|
|
try {
|
|
const keywords = ${JSON.stringify(keywords)};
|
|
|
|
// Get username from conversation header
|
|
const heading = document.querySelector('[data-testid="conversation-header"]') ||
|
|
document.querySelector('[data-testid="DM-conversation-header"]');
|
|
let username = 'Unknown';
|
|
if (heading) {
|
|
username = heading.innerText.trim().split('\\n')[0];
|
|
}
|
|
|
|
// Read full chat area text
|
|
const chatArea = document.querySelector('[data-testid="DmScrollerContainer"]') ||
|
|
document.querySelector('[data-testid="DMConversationBody"]') ||
|
|
document.querySelector('main [data-testid="cellInnerDiv"]')?.closest('section') ||
|
|
document.querySelector('main');
|
|
const text = chatArea ? chatArea.innerText : '';
|
|
|
|
// Verify keyword match in full chat content
|
|
const matchedKw = keywords.filter(k => text.includes(k));
|
|
if (matchedKw.length === 0) {
|
|
return { status: 'skipped', user: username, message: 'No keyword match in full content' };
|
|
}
|
|
|
|
// Find the Accept button
|
|
const allBtns = Array.from(document.querySelectorAll('[role="button"]'));
|
|
const acceptBtn = allBtns.find(btn => {
|
|
const t = btn.innerText.trim().toLowerCase();
|
|
return t === 'accept' || t === '接受';
|
|
});
|
|
|
|
if (!acceptBtn) {
|
|
return { status: 'no_button', user: username, message: 'Keyword matched but no Accept button (already accepted?)' };
|
|
}
|
|
|
|
// Click Accept
|
|
acceptBtn.click();
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
|
|
// Check for confirmation dialog
|
|
const btnsAfter = Array.from(document.querySelectorAll('[role="button"]'));
|
|
const confirmBtn = btnsAfter.find(btn => {
|
|
const t = btn.innerText.trim().toLowerCase();
|
|
return (t === 'accept' || t === '接受') && btn !== acceptBtn;
|
|
});
|
|
if (confirmBtn) {
|
|
confirmBtn.click();
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
}
|
|
|
|
return { status: 'accepted', user: username, message: 'Accepted! Matched: ' + matchedKw.join(', ') };
|
|
} catch(e) {
|
|
return { status: 'error', user: 'system', message: String(e) };
|
|
}
|
|
})()`);
|
|
|
|
if (res?.status === 'accepted') {
|
|
acceptCount++;
|
|
foundInThisRound = true;
|
|
results.push({
|
|
index: acceptCount,
|
|
status: 'accepted',
|
|
user: res.user || 'Unknown',
|
|
message: res.message || 'Accepted',
|
|
});
|
|
// After accept, Twitter redirects to /messages — loop back to /messages/requests
|
|
await page.wait(2);
|
|
break; // break inner loop, outer loop will re-navigate to requests
|
|
} else if (res?.status === 'no_button') {
|
|
// Already accepted, skip
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// If no match found in this round, we've exhausted all visible requests
|
|
if (!foundInThisRound) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
results.push({ index: 0, status: 'info', user: 'System', message: `No requests matched keywords "${keywords.join(', ')}"` });
|
|
}
|
|
|
|
return results;
|
|
}
|
|
});
|