Files
jackwener__opencli/clis/twitter/accept.ts
jakevin 80eef46b4e refactor: monorepo adapter separation (clis/ at root) (#782)
* refactor: move adapters from src/clis/ to root clis/ for monorepo separation

Separates CLI adapters from the core runtime to prepare for independent
adapter distribution via postinstall fetch.

Key changes:
- Move src/clis/ → clis/ (adapters at repo root)
- Change tsconfig rootDir from "src" to "." so tsc compiles both
- Create root-level shim files (registry.ts, errors.ts, etc.) so adapter
  relative imports (../../registry.js) resolve correctly
- Update build-manifest.ts, main.ts paths for new dist/src/ structure
- Expand ensureUserCliCompatShims() to cover all adapter import targets
  (types, utils, logger, launcher, browser/*, download/*, pipeline/*)
- Add scripts/fetch-adapters.js postinstall for ~/.opencli/clis/ sync
- Update vitest.config.ts adapter test paths
- Add package.json files field to exclude adapters from npm package

Official adapter files are unconditionally overwritten on update;
user-created files not in the manifest are preserved.

* fix: add dist/clis/ and cli-manifest.json to npm files, harden fetch-adapters

- Add dist/clis/ and dist/cli-manifest.json to package.json files field
  so built-in adapters and manifest ship with the npm package
- Replace execSync with execFileSync to prevent command injection
- Add version check to skip redundant adapter fetches
- Track tmpRoot explicitly for reliable cleanup

* fix: address review blockers — manifest-based updates, global-only fetch, first-run fallback

1. Manifest-based update strategy:
   - Read old manifest to identify previously-official files
   - Clean up files removed upstream (in old manifest but not new)
   - User-created files (never in any manifest) remain untouched

2. Only run fetch-adapters on global install (npm_config_global=true)
   or explicit OPENCLI_FETCH=1, preventing heavy side effects for
   local/dev installs

3. First-run fallback in discovery.ts:
   - ensureUserAdapters() checks for adapter-manifest.json
   - If missing and ~/.opencli/clis/ is empty, spawns fetch-adapters.js
   - Guarantees adapters are available even with --ignore-scripts

* fix: remove OPENCLI_FETCH env var, use internal _OPENCLI_FIRST_RUN instead

* feat: also support OPENCLI_FETCH=1 for explicit adapter fetch trigger

* simplify: replace git clone with local copy from dist/clis/

Adapters already ship in the npm package (dist/clis/), so there's no
need to clone from GitHub. Copy directly from the installed package:

- Eliminates git, curl, tar dependencies
- No network calls in postinstall
- No timeout/offline issues
- Version always matches the installed CLI
- ~65 lines of clone/download code replaced by one cpSync loop
2026-04-05 01:46:36 +08:00

215 lines
8.4 KiB
TypeScript

import { CommandExecutionError } from '../../errors.js';
import { cli, Strategy } from '../../registry.js';
import type { IPage } from '../../types.js';
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;
}
});