dp-worker 0.1.19 → 0.1.20
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/builtin-skills/x.md +27 -0
- package/dist/bird/bird-frontend.d.ts +33 -0
- package/dist/bird/bird-frontend.js +68 -0
- package/dist/bird/bird-frontend.js.map +1 -0
- package/dist/bird/credentials.d.ts +36 -0
- package/dist/bird/credentials.js +82 -0
- package/dist/bird/credentials.js.map +1 -0
- package/dist/bird/ops.d.ts +103 -0
- package/dist/bird/ops.js +523 -0
- package/dist/bird/ops.js.map +1 -0
- package/dist/bird/sidecar-entry.d.ts +25 -0
- package/dist/bird/sidecar-entry.js +192 -0
- package/dist/bird/sidecar-entry.js.map +1 -0
- package/dist/bird/sidecar-host.d.ts +54 -0
- package/dist/bird/sidecar-host.js +318 -0
- package/dist/bird/sidecar-host.js.map +1 -0
- package/dist/bird/socks-connector.d.ts +26 -0
- package/dist/bird/socks-connector.js +49 -0
- package/dist/bird/socks-connector.js.map +1 -0
- package/dist/browser/cloakbrowser-backend.js +10 -27
- package/dist/browser/cloakbrowser-backend.js.map +1 -1
- package/dist/chrome-extension/omni-flow/manifest.json +1 -1
- package/dist/config/center-config.d.ts +8 -0
- package/dist/config/center-config.js +1 -0
- package/dist/config/center-config.js.map +1 -1
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -1
- package/node_modules/dp-shared/dist/admin-broadcast.d.ts +1 -1
- package/node_modules/dp-shared/dist/admin-broadcast.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/bird-contracts.d.ts +483 -0
- package/node_modules/dp-shared/dist/bird-contracts.d.ts.map +1 -0
- package/node_modules/dp-shared/dist/bird-contracts.js +372 -0
- package/node_modules/dp-shared/dist/bird-contracts.js.map +1 -0
- package/node_modules/dp-shared/dist/configs-contracts.d.ts +11 -1
- package/node_modules/dp-shared/dist/configs-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/configs-contracts.js +43 -0
- package/node_modules/dp-shared/dist/configs-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/index.d.ts +2 -0
- package/node_modules/dp-shared/dist/index.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/index.js +2 -0
- package/node_modules/dp-shared/dist/index.js.map +1 -1
- package/node_modules/dp-shared/dist/post-drafts-contracts.d.ts +9 -3
- package/node_modules/dp-shared/dist/post-drafts-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/post-drafts-contracts.js +34 -0
- package/node_modules/dp-shared/dist/post-drafts-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/promo-contracts.d.ts +651 -0
- package/node_modules/dp-shared/dist/promo-contracts.d.ts.map +1 -0
- package/node_modules/dp-shared/dist/promo-contracts.js +566 -0
- package/node_modules/dp-shared/dist/promo-contracts.js.map +1 -0
- package/node_modules/dp-shared/dist/rest-contracts.d.ts +73 -0
- package/node_modules/dp-shared/dist/rest-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/rest-contracts.js +70 -0
- package/node_modules/dp-shared/dist/rest-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/smart-money-contracts.d.ts +86 -0
- package/node_modules/dp-shared/dist/smart-money-contracts.d.ts.map +1 -0
- package/node_modules/dp-shared/dist/smart-money-contracts.js +71 -0
- package/node_modules/dp-shared/dist/smart-money-contracts.js.map +1 -0
- package/node_modules/dp-shared/dist/task-history-contracts.d.ts +2 -0
- package/node_modules/dp-shared/dist/task-history-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/task-history-contracts.js +2 -0
- package/node_modules/dp-shared/dist/task-history-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts +7 -0
- package/node_modules/dp-shared/dist/worker-detail-contracts.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/worker-detail-contracts.js +9 -0
- package/node_modules/dp-shared/dist/worker-detail-contracts.js.map +1 -1
- package/node_modules/dp-shared/dist/ws-protocol.d.ts +52 -0
- package/node_modules/dp-shared/dist/ws-protocol.d.ts.map +1 -1
- package/node_modules/dp-shared/dist/ws-protocol.js +37 -0
- package/node_modules/dp-shared/dist/ws-protocol.js.map +1 -1
- package/package.json +4 -2
- package/dist/browser/proxy-relay.d.ts +0 -50
- package/dist/browser/proxy-relay.js +0 -50
- package/dist/browser/proxy-relay.js.map +0 -1
- package/node_modules/dp-shared/src/admin-broadcast.ts +0 -41
- package/node_modules/dp-shared/src/audit-contracts.ts +0 -33
- package/node_modules/dp-shared/src/auto-post-contracts.ts +0 -206
- package/node_modules/dp-shared/src/cloak-contracts.ts +0 -110
- package/node_modules/dp-shared/src/configs-contracts.ts +0 -249
- package/node_modules/dp-shared/src/db-rows.ts +0 -303
- package/node_modules/dp-shared/src/deploy-contracts.ts +0 -38
- package/node_modules/dp-shared/src/domain.ts +0 -124
- package/node_modules/dp-shared/src/fingerprint.ts +0 -21
- package/node_modules/dp-shared/src/identities.ts +0 -38
- package/node_modules/dp-shared/src/index.ts +0 -30
- package/node_modules/dp-shared/src/omni-flow-cookie.ts +0 -272
- package/node_modules/dp-shared/src/omni-flow-session.ts +0 -79
- package/node_modules/dp-shared/src/post-drafts-contracts.ts +0 -807
- package/node_modules/dp-shared/src/rest-contracts.ts +0 -592
- package/node_modules/dp-shared/src/task-history-contracts.ts +0 -124
- package/node_modules/dp-shared/src/worker-detail-contracts.ts +0 -236
- package/node_modules/dp-shared/src/ws-protocol.ts +0 -495
package/builtin-skills/x.md
CHANGED
|
@@ -241,6 +241,33 @@ fallbacks for compatibility.
|
|
|
241
241
|
This prevents a published post from being wrongly recorded as failed (which
|
|
242
242
|
would otherwise trigger a re-send and a duplicate post).
|
|
243
243
|
|
|
244
|
+
## Selectors: two different engines, do NOT mix them
|
|
245
|
+
|
|
246
|
+
Playwright locators and the browser's own `document.querySelector` accept **different**
|
|
247
|
+
selector languages. Mixing them throws `SyntaxError: ... is not a valid selector` and the
|
|
248
|
+
whole task fails — this has already burned real posts.
|
|
249
|
+
|
|
250
|
+
- **Playwright-only** (locator / click / type / expect): `:has-text("…")`, `:text("…")`,
|
|
251
|
+
`:visible`, `>> nth=`, `text=…`. These are Playwright inventions, **not CSS**.
|
|
252
|
+
- **Inside `page.evaluate` / any raw DOM call** you are in the browser, which only knows
|
|
253
|
+
**standard CSS**. `:has-text()` does not exist there. `:has()` does, but it takes a
|
|
254
|
+
*selector*, never text.
|
|
255
|
+
|
|
256
|
+
To find an element by its text from inside `evaluate`, select a set with plain CSS and
|
|
257
|
+
filter in JS:
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
// WRONG — throws SyntaxError, kills the task
|
|
261
|
+
document.querySelector('article[data-testid="tweet"]:has-text("start small")')
|
|
262
|
+
|
|
263
|
+
// RIGHT
|
|
264
|
+
[...document.querySelectorAll('article[data-testid="tweet"]')]
|
|
265
|
+
.find(el => el.innerText.includes('start small'))
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Same rule for `[data-testid="cellInnerDiv"]:has([data-testid="tweetText"]:has-text("…"))`
|
|
269
|
+
— the outer `:has()` is fine, the inner `:has-text()` is what breaks it.
|
|
270
|
+
|
|
244
271
|
## Post length (280 for standard accounts)
|
|
245
272
|
|
|
246
273
|
- If YOU are composing the post (a "write a post about …" task): keep it within
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { CenterClient } from "../center/center-client.js";
|
|
2
|
+
/**
|
|
3
|
+
* center ↔ bird 的适配器。
|
|
4
|
+
*
|
|
5
|
+
* 跟 CenterFrontend 平行地挂在同一条 WebSocket 上,但**没有任何交集**:不碰 Agent、
|
|
6
|
+
* 不碰 BrowserLock、不发 status_change、不产生 task_run。这是刻意的 ——
|
|
7
|
+
* bird 是一次只读 HTTP,不该影响这台机器在调度系统眼里的忙闲状态。
|
|
8
|
+
*
|
|
9
|
+
* 唯一的对外副作用是回一条 `bird_job_result`。
|
|
10
|
+
*/
|
|
11
|
+
export interface BirdFrontendOpts {
|
|
12
|
+
client: CenterClient;
|
|
13
|
+
workspace: string;
|
|
14
|
+
workerId: string;
|
|
15
|
+
logger?: {
|
|
16
|
+
info: (m: string) => void;
|
|
17
|
+
warn: (m: string) => void;
|
|
18
|
+
error: (m: string) => void;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare class BirdFrontend {
|
|
22
|
+
private readonly client;
|
|
23
|
+
private readonly workerId;
|
|
24
|
+
private readonly sidecar;
|
|
25
|
+
private readonly logger;
|
|
26
|
+
private detach;
|
|
27
|
+
constructor(opts: BirdFrontendOpts);
|
|
28
|
+
start(): void;
|
|
29
|
+
stop(): Promise<void>;
|
|
30
|
+
/** session 换了 —— 让下次调用重新读 cookie。由 CenterFrontend 的 session swap 调用。 */
|
|
31
|
+
onSessionChanged(): void;
|
|
32
|
+
private handle;
|
|
33
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { BirdSidecar } from "./sidecar-host.js";
|
|
2
|
+
export class BirdFrontend {
|
|
3
|
+
client;
|
|
4
|
+
workerId;
|
|
5
|
+
sidecar;
|
|
6
|
+
logger;
|
|
7
|
+
detach = null;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
this.client = opts.client;
|
|
10
|
+
this.workerId = opts.workerId;
|
|
11
|
+
this.logger = opts.logger ?? {
|
|
12
|
+
info: (m) => console.log(m),
|
|
13
|
+
warn: (m) => console.warn(m),
|
|
14
|
+
error: (m) => console.error(m),
|
|
15
|
+
};
|
|
16
|
+
this.sidecar = new BirdSidecar({ workspace: opts.workspace, logger: this.logger });
|
|
17
|
+
}
|
|
18
|
+
start() {
|
|
19
|
+
const onMessage = (msg) => {
|
|
20
|
+
if (msg.type !== "bird_job_dispatch")
|
|
21
|
+
return;
|
|
22
|
+
// zod 的推断联合不会把 payload 跟 type 一起收窄(见 center-frontend 里的同款
|
|
23
|
+
// 注释),运行期已由 schema 校验过。
|
|
24
|
+
const p = msg.payload;
|
|
25
|
+
void this.handle(p);
|
|
26
|
+
};
|
|
27
|
+
this.client.on("message", onMessage);
|
|
28
|
+
this.detach = () => this.client.off("message", onMessage);
|
|
29
|
+
this.logger.info(`[bird] executor enabled for worker ${this.workerId}`);
|
|
30
|
+
}
|
|
31
|
+
async stop() {
|
|
32
|
+
this.detach?.();
|
|
33
|
+
this.detach = null;
|
|
34
|
+
await this.sidecar.stop();
|
|
35
|
+
}
|
|
36
|
+
/** session 换了 —— 让下次调用重新读 cookie。由 CenterFrontend 的 session swap 调用。 */
|
|
37
|
+
onSessionChanged() {
|
|
38
|
+
this.sidecar.invalidateSession();
|
|
39
|
+
}
|
|
40
|
+
async handle(p) {
|
|
41
|
+
this.sidecar.setMinInterval(p.min_interval_ms);
|
|
42
|
+
const res = await this.sidecar.run(p.op, p.params ?? {}, p.timeout_ms);
|
|
43
|
+
if (!res.ok) {
|
|
44
|
+
this.logger.warn(`[bird] job ${p.job_id} (${p.op}) failed: ${res.code} — ${res.error ?? ""}`);
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
await this.client.send({
|
|
48
|
+
type: "bird_job_result",
|
|
49
|
+
payload: {
|
|
50
|
+
job_id: p.job_id,
|
|
51
|
+
ok: res.ok,
|
|
52
|
+
...(res.result ? { result: res.result } : {}),
|
|
53
|
+
...(res.code ? { error_code: res.code } : {}),
|
|
54
|
+
...(res.error ? { error: res.error } : {}),
|
|
55
|
+
elapsed_ms: res.elapsedMs,
|
|
56
|
+
rate_limited: res.code === "rate_limited",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
// 结果送不回去就丢了 —— center 侧的 job_timeout_s 会把这条作业标成 timeout,
|
|
62
|
+
// 不会永远挂着。这里不重试:重试意味着要在 worker 侧存一份未送达队列,
|
|
63
|
+
// 而一次只读查询不值得那套机制。
|
|
64
|
+
this.logger.warn(`[bird] could not report job ${p.job_id}: ${err instanceof Error ? err.message : err}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=bird-fr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AAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC7C,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC7C,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC1C,UAAU,EAAE,GAAG,CAAC,SAAS;oBACzB,YAAY,EAAE,GAAG,CAAC,IAAI,KAAK,cAAc;iBAC1C;aACF,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,yDAAyD;YACzD,yCAAyC;YACzC,kBAAkB;YAClB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,+BAA+B,CAAC,CAAC,MAAM,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,CACvF,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bird 的凭据来源 —— **就是这台 worker 自己的 OmniFlow session**。
|
|
3
|
+
*
|
|
4
|
+
* center 把 identity 的 session 推下来,session-sync 把它落到
|
|
5
|
+
* `<workspace>/.dipclaw/omni-flow-session.json`,Chrome 启动时由扩展导入。这份文件
|
|
6
|
+
* 里已经装着我们要的全部东西:X 的 cookie(auth_token/ct0)和该号绑定的专用代理。
|
|
7
|
+
* 跟 sourcepool 的 `secrets/x_session.json` 是同一个格式。
|
|
8
|
+
*
|
|
9
|
+
* 所以这里**不新增任何凭据存储**。好处不只是少写一张表:
|
|
10
|
+
* · session 轮换(重新导出、center 推新 hash)自动跟随,不会出现"表里的 cookie 和
|
|
11
|
+
* 浏览器里的 cookie 不是同一个"的分裂状态;
|
|
12
|
+
* · 代理天然跟 cookie 配对 —— 这正是下面那条硬规则的前提。
|
|
13
|
+
*
|
|
14
|
+
* ── 为什么代理拿不到就必须失败 ──────────────────────────────────────────────
|
|
15
|
+
* X 的 cookie 是在这个代理后面签发的。用同一个 cookie 从另一个出口 IP 发请求,是
|
|
16
|
+
* 会话劫持的经典特征,也是我们能给风控送的最响的信号。所以 `loadBirdCredentials`
|
|
17
|
+
* 在缺少代理时**抛错**,而不是返回一个"没有代理"的凭据让调用方自己决定 ——
|
|
18
|
+
* 让调用方决定,就迟早有人为了让功能跑起来而选择直连。
|
|
19
|
+
*/
|
|
20
|
+
export interface BirdCredentials {
|
|
21
|
+
authToken: string;
|
|
22
|
+
ct0: string;
|
|
23
|
+
/** 完整 cookie 头(含 auth_token/ct0 之外的项,更像真实浏览器)。 */
|
|
24
|
+
cookieHeader: string;
|
|
25
|
+
/** 代理 URL,形如 `http://user:pass@host:port` / `socks5://host:port`。 */
|
|
26
|
+
proxyUrl: string;
|
|
27
|
+
proxyProtocol: string;
|
|
28
|
+
/** 时区伪装值(如 America/New_York),只作日志展示用。 */
|
|
29
|
+
timezone: string | null;
|
|
30
|
+
}
|
|
31
|
+
export declare class BirdCredentialError extends Error {
|
|
32
|
+
readonly code: "session_missing" | "session_incomplete" | "proxy_unavailable" | "proxy_unsupported";
|
|
33
|
+
constructor(code: BirdCredentialError["code"], message: string);
|
|
34
|
+
}
|
|
35
|
+
export declare function loadBirdCredentials(workspace: string): BirdCredentials;
|
|
36
|
+
export declare function isSocksProxy(protocol: string): boolean;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { OmniFlowSessionExportSchema } from "dp-shared";
|
|
3
|
+
import { omniFlowSessionPath } from "../center/session-sync.js";
|
|
4
|
+
export class BirdCredentialError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "BirdCredentialError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** undici 的 ProxyAgent 只认 http/https。socks 走另一条路(见 sidecar-entry)。 */
|
|
13
|
+
const HTTP_PROXY_TYPES = new Set(["http", "https"]);
|
|
14
|
+
const SOCKS_PROXY_TYPES = new Set(["socks", "socks4", "socks5", "socks5h"]);
|
|
15
|
+
function proxyToUrl(pc) {
|
|
16
|
+
const type = String(pc.type || "http").toLowerCase();
|
|
17
|
+
const cred = pc.username
|
|
18
|
+
? `${encodeURIComponent(pc.username)}:${encodeURIComponent(pc.password ?? "")}@`
|
|
19
|
+
: "";
|
|
20
|
+
return `${type}://${cred}${pc.host}:${pc.port}`;
|
|
21
|
+
}
|
|
22
|
+
export function loadBirdCredentials(workspace) {
|
|
23
|
+
const p = omniFlowSessionPath(workspace);
|
|
24
|
+
let raw;
|
|
25
|
+
try {
|
|
26
|
+
raw = fs.readFileSync(p, "utf-8");
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new BirdCredentialError("session_missing", `no OmniFlow session at ${p} — this worker has no X session to borrow`);
|
|
30
|
+
}
|
|
31
|
+
const parsed = OmniFlowSessionExportSchema.safeParse(JSON.parse(raw));
|
|
32
|
+
if (!parsed.success) {
|
|
33
|
+
throw new BirdCredentialError("session_missing", `OmniFlow session at ${p} failed schema validation: ${parsed.error.message.slice(0, 200)}`);
|
|
34
|
+
}
|
|
35
|
+
const blob = parsed.data;
|
|
36
|
+
// 只取 x.com / twitter.com 域的 cookie。session 里可能还有别的站点的,带上去
|
|
37
|
+
// 既没用又扩大了泄露面。
|
|
38
|
+
const xCookies = blob.cookies.filter((c) => {
|
|
39
|
+
const d = (c.domain ?? "").replace(/^\./, "").toLowerCase();
|
|
40
|
+
return d === "x.com" || d === "twitter.com" || d.endsWith(".x.com") || d.endsWith(".twitter.com");
|
|
41
|
+
});
|
|
42
|
+
const byName = new Map(xCookies.map((c) => [c.name, c.value]));
|
|
43
|
+
const authToken = byName.get("auth_token");
|
|
44
|
+
const ct0 = byName.get("ct0");
|
|
45
|
+
if (!authToken || !ct0) {
|
|
46
|
+
throw new BirdCredentialError("session_incomplete", "OmniFlow session has no auth_token/ct0 for x.com — the account is not logged in");
|
|
47
|
+
}
|
|
48
|
+
// 去重后拼完整 cookie 头。bird 接受 cookieHeader 覆盖它自己那份两项的拼接;
|
|
49
|
+
// 带上 guest_id / personalization_id 这些附属项更接近真实浏览器发出的请求。
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
const parts = [];
|
|
52
|
+
for (const c of xCookies) {
|
|
53
|
+
if (seen.has(c.name))
|
|
54
|
+
continue;
|
|
55
|
+
seen.add(c.name);
|
|
56
|
+
parts.push(`${c.name}=${c.value}`);
|
|
57
|
+
}
|
|
58
|
+
const pc = blob.extensionStorage.proxyConfig;
|
|
59
|
+
if (!pc || !pc.host) {
|
|
60
|
+
// I3。这里是唯一一处能悄悄"退化成直连"的地方,所以它必须是抛错而不是返回可选值。
|
|
61
|
+
throw new BirdCredentialError("proxy_unavailable", "session has no proxy — refusing to use these cookies from the host IP (the cookies were minted behind a dedicated exit)");
|
|
62
|
+
}
|
|
63
|
+
const type = String(pc.type || "http").toLowerCase();
|
|
64
|
+
if (!HTTP_PROXY_TYPES.has(type) && !SOCKS_PROXY_TYPES.has(type)) {
|
|
65
|
+
throw new BirdCredentialError("proxy_unsupported", `unsupported proxy type "${pc.type}" — refusing to fall back to a direct connection`);
|
|
66
|
+
}
|
|
67
|
+
const timezone = blob.extensionStorage.timezoneEnabled === false
|
|
68
|
+
? null
|
|
69
|
+
: blob.extensionStorage.spoofTimezone || null;
|
|
70
|
+
return {
|
|
71
|
+
authToken,
|
|
72
|
+
ct0,
|
|
73
|
+
cookieHeader: parts.join("; "),
|
|
74
|
+
proxyUrl: proxyToUrl(pc),
|
|
75
|
+
proxyProtocol: type,
|
|
76
|
+
timezone,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export function isSocksProxy(protocol) {
|
|
80
|
+
return SOCKS_PROXY_TYPES.has(protocol.toLowerCase());
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../src/bird/credentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,2BAA2B,EAAE,MAAM,WAAW,CAAC;AAExD,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAkChE,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,IAAI,CAIW;IAExB,YACE,IAAiC,EACjC,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,sEAAsE;AACtE,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AACpD,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;AAE5E,SAAS,UAAU,CAAC,EAAuB;IACzC,MAAM,IAAI,GAAG,MAAM,CAAC,EAAE,CAAC,IAAI,IAAI,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;IACrD,MAAM,IAAI,GAAG,EAAE,CAAC,QAAQ;QACtB,CAAC,CAAC,GAAG,kBAAkB,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,kBAAkB,CAAC,EAAE,CAAC,QAAQ,IAAI,EAAE,CAAC,GAAG;QAChF,CAAC,CAAC,EAAE,CAAC;IACP,OAAO,GAAG,IAAI,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,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AAkD,CACrF,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,GACZ,IAAI,CAAC,gBAAgB,CAAC,eAAe,KAAK,KAAK;QAC7C,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,aAAa,IAAI,IAAI,CAAC;IAElD,OAAO;QACL,SAAS;QACT,GAAG;QACH,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;QAC9B,QAAQ,EAAE,UAAU,CAAC,EAAE,CAAC;QACxB,aAAa,EAAE,IAAI;QACnB,QAAQ;KACT,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,QAAgB;IAC3C,OAAO,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { BirdOpResult, BirdTweet } from "dp-shared";
|
|
2
|
+
/**
|
|
3
|
+
* op → bird 调用 + 结果归一化。
|
|
4
|
+
*
|
|
5
|
+
* 这个文件只在 sidecar 子进程里跑(它 import 了 `@steipete/bird`,而 bird 会走全局
|
|
6
|
+
* dispatcher —— 主进程绝不能加载它,否则代理会污染 LLM 调用和 center WS)。
|
|
7
|
+
*
|
|
8
|
+
* ── 为什么要自己写提取器,而不是直接用 bird 的类型 ──────────────────────────
|
|
9
|
+
* bird 的 `TweetData` 只声明了 reply/retweet/like 三个计数,`TwitterUser.followersCount`
|
|
10
|
+
* 只出现在 followers/following 列表里。但**浏览量、书签数、引用数、粉丝数**在原始
|
|
11
|
+
* GraphQL 响应里是有的 —— bird 的类型只是没声明它用不到的字段(`_raw` 的文档说明
|
|
12
|
+
* 是 "full raw API response")。汇报要的恰恰是浏览量和粉丝数,所以这里从 `_raw`
|
|
13
|
+
* 里自己挖,并且对多条候选路径都试一遍:X 这两年把 `legacy.screen_name` 挪到了
|
|
14
|
+
* `core.screen_name`、把 views 从 `ext_views` 挪到 `views.count`,写死一条路径的
|
|
15
|
+
* 提取器活不过下一次改版。挖不到就是 null —— 一条没有浏览量的推仍然有价值,不能
|
|
16
|
+
* 因为字段缺失把整条记录丢掉。
|
|
17
|
+
*/
|
|
18
|
+
/** bird 的 TwitterClient(结构化引用,避免在类型层面依赖它的具体 mixin 形状)。 */
|
|
19
|
+
interface PageOpts {
|
|
20
|
+
includeRaw?: boolean;
|
|
21
|
+
cursor?: string;
|
|
22
|
+
/** 页间延迟。bird 的 user-tweets / replies / thread 支持;search 与各 timeline 不支持
|
|
23
|
+
* —— 那几个由我们自己的 pagedFetch 控速。 */
|
|
24
|
+
pageDelayMs?: number;
|
|
25
|
+
maxPages?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface BirdClientLike {
|
|
28
|
+
search(query: string, count?: number, options?: PageOpts): Promise<unknown>;
|
|
29
|
+
getTweet(tweetId: string, options?: PageOpts): Promise<unknown>;
|
|
30
|
+
getRepliesPaged(tweetId: string, options?: PageOpts): Promise<unknown>;
|
|
31
|
+
getThreadPaged(tweetId: string, options?: PageOpts): Promise<unknown>;
|
|
32
|
+
getUserTweetsPaged(userId: string, limit: number, options?: PageOpts): Promise<unknown>;
|
|
33
|
+
getUserIdByUsername(username: string): Promise<unknown>;
|
|
34
|
+
getUserAboutAccount(username: string): Promise<unknown>;
|
|
35
|
+
getListTimeline(listId: string, count?: number, options?: PageOpts): Promise<unknown>;
|
|
36
|
+
getHomeTimeline(count?: number, options?: PageOpts): Promise<unknown>;
|
|
37
|
+
getHomeLatestTimeline(count?: number, options?: PageOpts): Promise<unknown>;
|
|
38
|
+
getLikes(count?: number, options?: PageOpts): Promise<unknown>;
|
|
39
|
+
getBookmarks(count?: number, options?: PageOpts): Promise<unknown>;
|
|
40
|
+
getFollowers(userId: string, count?: number, cursor?: string): Promise<unknown>;
|
|
41
|
+
getFollowing(userId: string, count?: number, cursor?: string): Promise<unknown>;
|
|
42
|
+
getOwnedLists(count?: number): Promise<unknown>;
|
|
43
|
+
getListMemberships(count?: number): Promise<unknown>;
|
|
44
|
+
getNews(count?: number, options?: Record<string, unknown>): Promise<unknown>;
|
|
45
|
+
getCurrentUser(): Promise<unknown>;
|
|
46
|
+
}
|
|
47
|
+
export interface BirdOpContext {
|
|
48
|
+
client: BirdClientLike;
|
|
49
|
+
/**
|
|
50
|
+
* 直接打 X 的 REST(users/show.json)。粉丝数最可靠的来源 —— bird 内部有这条
|
|
51
|
+
* 回退路径但公开方法把结果丢了,所以由宿主注入一个同样握着 cookie 的实现。
|
|
52
|
+
*/
|
|
53
|
+
fetchUserShow(handle: string): Promise<Record<string, unknown> | null>;
|
|
54
|
+
}
|
|
55
|
+
export declare class BirdOpError extends Error {
|
|
56
|
+
readonly code: string;
|
|
57
|
+
constructor(code: string, message: string);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* handle 归一化 —— 认不出来就**抛错,不猜**。
|
|
61
|
+
*
|
|
62
|
+
* 早先这里是把非法字符剔掉再返回,后果是一个打错的 handle 会被"修"成另一个合法
|
|
63
|
+
* handle,然后我们对着**别人的账号**发查询,报告上却挂着我们自己的号名。bird 自己
|
|
64
|
+
* 的 normalizeHandle 也是严格校验后返回 null,这里对齐它的判据。
|
|
65
|
+
*/
|
|
66
|
+
export declare function normalizeHandle(h: string): string;
|
|
67
|
+
/** 从 id 或任意 x.com/twitter.com 链接里取出推文 id。 */
|
|
68
|
+
export declare function extractTweetId(input: string): string | null;
|
|
69
|
+
/** bird 的 TweetData(可能带 _raw)→ 我们的 BirdTweet。 */
|
|
70
|
+
export declare function toBirdTweet(raw: unknown): BirdTweet | null;
|
|
71
|
+
/** 把 bird / X 的错误串映射成我们的错误码。UI 和告警按码分流,不做字符串匹配。 */
|
|
72
|
+
export declare function classifyError(msg: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* 把结构化参数拼成 X 的搜索语法。
|
|
75
|
+
*
|
|
76
|
+
* 用 X 自己的算子(`min_faves:` / `since:` / `-word`)而不是拉回来再过滤,是因为
|
|
77
|
+
* **过滤发生在 X 那边就少一次翻页**,而翻页是按次计风控的。
|
|
78
|
+
*/
|
|
79
|
+
export declare function buildSearchQuery(p: {
|
|
80
|
+
query?: string;
|
|
81
|
+
min_faves?: number;
|
|
82
|
+
within_hours?: number;
|
|
83
|
+
excludes?: string[];
|
|
84
|
+
}): string;
|
|
85
|
+
export interface RunOpInput {
|
|
86
|
+
op: string;
|
|
87
|
+
params: Record<string, unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* 我们自己的请求间隔(毫秒)。见下面 pagedFetch 的说明 —— 这是**这次修复的核心**。
|
|
90
|
+
*/
|
|
91
|
+
pageDelayMs?: number;
|
|
92
|
+
}
|
|
93
|
+
/** X 的 GraphQL 一页固定 20 条。翻页次数 = ceil(count/20)。 */
|
|
94
|
+
export declare const PAGE_SIZE = 20;
|
|
95
|
+
/**
|
|
96
|
+
* 一次 op 大约会打出多少个 HTTP 请求。
|
|
97
|
+
*
|
|
98
|
+
* center 的日配额按这个数记账,而不是"一个作业算一次" —— `search(q, 100)` 是
|
|
99
|
+
* **5 个请求**,按 1 次记会让实际频率比配额显示的高 5 倍,而频率正是 X 的风控口径。
|
|
100
|
+
*/
|
|
101
|
+
export declare function estimateRequests(op: string, params: Record<string, unknown>): number;
|
|
102
|
+
export declare function runBirdOp(ctx: BirdOpContext, input: RunOpInput): Promise<BirdOpResult>;
|
|
103
|
+
export {};
|