synabun 2.2.2 → 2.3.0
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/CHANGELOG.md +26 -0
- package/README.md +1 -1
- package/docs/whatsapp.md +52 -20
- package/lib/mcp-build.js +519 -0
- package/lib/mcp-dist.js +10 -4
- package/lib/server-supervisor.js +7 -1
- package/lib/virustotal.mjs +4 -2
- package/mcp-server/dist/http.d.ts +38 -0
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +75 -7
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.d.ts +3 -2
- package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.js +4 -3
- package/mcp-server/dist/services/browser-route-flow.js.map +1 -1
- package/mcp-server/dist/services/embedding-service.d.ts.map +1 -1
- package/mcp-server/dist/services/embedding-service.js +2 -1
- package/mcp-server/dist/services/embedding-service.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +21 -0
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +76 -4
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/services/tool-catalog.js +2 -2
- package/mcp-server/dist/services/tool-catalog.js.map +1 -1
- package/mcp-server/dist/tools/agents.d.ts +3 -2
- package/mcp-server/dist/tools/agents.d.ts.map +1 -1
- package/mcp-server/dist/tools/agents.js +6 -4
- package/mcp-server/dist/tools/agents.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +3 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +30 -8
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-navigate.d.ts +3 -3
- package/mcp-server/dist/tools/browser-navigate.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-navigate.js +19 -16
- package/mcp-server/dist/tools/browser-navigate.js.map +1 -1
- package/mcp-server/dist/tools/browser-observe.d.ts +2 -1
- package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-observe.js +61 -20
- package/mcp-server/dist/tools/browser-observe.js.map +1 -1
- package/mcp-server/dist/tools/morelogin.d.ts +13 -3
- package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
- package/mcp-server/dist/tools/morelogin.js +81 -20
- package/mcp-server/dist/tools/morelogin.js.map +1 -1
- package/mcp-server/dist/tools/youtube/upload.d.ts.map +1 -1
- package/mcp-server/dist/tools/youtube/upload.js +225 -88
- package/mcp-server/dist/tools/youtube/upload.js.map +1 -1
- package/mcp-server/package-lock.json +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/http.ts +79 -7
- package/mcp-server/src/services/browser-route-flow.ts +4 -3
- package/mcp-server/src/services/embedding-service.ts +2 -1
- package/mcp-server/src/services/neural-interface.ts +77 -4
- package/mcp-server/src/services/tool-catalog.ts +2 -2
- package/mcp-server/src/tools/agents.ts +8 -4
- package/mcp-server/src/tools/browser-advanced.ts +29 -8
- package/mcp-server/src/tools/browser-navigate.ts +19 -16
- package/mcp-server/src/tools/browser-observe.ts +62 -20
- package/mcp-server/src/tools/morelogin.ts +81 -20
- package/mcp-server/src/tools/youtube/upload.ts +218 -99
- package/neural-interface/i18n/en.json +631 -123
- package/neural-interface/i18n/pt-BR.json +518 -10
- package/neural-interface/lib/assistant-api.js +26 -0
- package/neural-interface/lib/assistant-brains/claude.js +5 -0
- package/neural-interface/lib/assistant-catalog.js +114 -1
- package/neural-interface/lib/assistant-dispatch.js +71 -1
- package/neural-interface/lib/assistant-persona.js +14 -7
- package/neural-interface/lib/assistant-router.js +6 -4
- package/neural-interface/lib/assistant-runtime.js +3 -0
- package/neural-interface/lib/assistant-task-prompt.js +5 -0
- package/neural-interface/lib/automation-approval.js +249 -0
- package/neural-interface/lib/automation-browser-owner-access.js +82 -0
- package/neural-interface/lib/backup-service.js +16 -1
- package/neural-interface/lib/browser-background-tab.js +72 -3
- package/neural-interface/lib/browser-extraction.js +106 -45
- package/neural-interface/lib/browser-profile-choices.js +143 -0
- package/neural-interface/lib/browser-profiles.js +142 -0
- package/neural-interface/lib/browser-tab-recovery.js +153 -0
- package/neural-interface/lib/browser-tool-policy.js +2 -0
- package/neural-interface/lib/chromium-on-device-model.js +67 -0
- package/neural-interface/lib/claude-agent-bridge.js +7 -4
- package/neural-interface/lib/claude-model-catalog.js +12 -0
- package/neural-interface/lib/effort-levels.js +28 -2
- package/neural-interface/lib/local-profile-proxy.js +362 -0
- package/neural-interface/lib/local-profiles.js +929 -0
- package/neural-interface/lib/loop-logger.js +25 -4
- package/neural-interface/lib/maritaca/api.js +179 -0
- package/neural-interface/lib/maritaca/attachments.js +44 -0
- package/neural-interface/lib/maritaca/client.js +362 -0
- package/neural-interface/lib/maritaca/key-store.js +122 -0
- package/neural-interface/lib/maritaca/models.js +181 -0
- package/neural-interface/lib/maritaca/modes.js +39 -0
- package/neural-interface/lib/maritaca/opencode-proxy.js +236 -0
- package/neural-interface/lib/maritaca/opencode-stream.js +345 -0
- package/neural-interface/lib/maritaca/opencode.js +218 -0
- package/neural-interface/lib/maritaca/panel-bridge.js +273 -0
- package/neural-interface/lib/maritaca/sessions.js +198 -0
- package/neural-interface/lib/morelogin-agent-profiles.js +149 -0
- package/neural-interface/lib/morelogin.js +278 -14
- package/neural-interface/lib/native-loop-providers.js +3 -1
- package/neural-interface/lib/owner-authority.js +30 -12
- package/neural-interface/lib/page-session.js +43 -17
- package/neural-interface/lib/request-gate.js +2 -0
- package/neural-interface/lib/system-activity.js +1 -1
- package/neural-interface/lib/terminal-tokens.js +82 -5
- package/neural-interface/package-lock.json +3 -2
- package/neural-interface/package.json +2 -1
- package/neural-interface/public/assets/mascot/README.md +11 -0
- package/neural-interface/public/assets/mascot/ask.gif +0 -0
- package/neural-interface/public/assets/mascot/ask.png +0 -0
- package/neural-interface/public/assets/mascot/blocked.gif +0 -0
- package/neural-interface/public/assets/mascot/blocked.png +0 -0
- package/neural-interface/public/assets/mascot/budget.gif +0 -0
- package/neural-interface/public/assets/mascot/budget.png +0 -0
- package/neural-interface/public/assets/mascot/computer.gif +0 -0
- package/neural-interface/public/assets/mascot/computer.png +0 -0
- package/neural-interface/public/assets/mascot/edit.gif +0 -0
- package/neural-interface/public/assets/mascot/edit.png +0 -0
- package/neural-interface/public/assets/mascot/error.gif +0 -0
- package/neural-interface/public/assets/mascot/error.png +0 -0
- package/neural-interface/public/assets/mascot/hover-attention.gif +0 -0
- package/neural-interface/public/assets/mascot/hover-attention.png +0 -0
- package/neural-interface/public/assets/mascot/idle-hero.gif +0 -0
- package/neural-interface/public/assets/mascot/idle-hero.png +0 -0
- package/neural-interface/public/assets/mascot/idle.gif +0 -0
- package/neural-interface/public/assets/mascot/idle.png +0 -0
- package/neural-interface/public/assets/mascot/manifest.json +254 -0
- package/neural-interface/public/assets/mascot/mcp.gif +0 -0
- package/neural-interface/public/assets/mascot/mcp.png +0 -0
- package/neural-interface/public/assets/mascot/memory.gif +0 -0
- package/neural-interface/public/assets/mascot/memory.png +0 -0
- package/neural-interface/public/assets/mascot/offline.gif +0 -0
- package/neural-interface/public/assets/mascot/offline.png +0 -0
- package/neural-interface/public/assets/mascot/pick.gif +0 -0
- package/neural-interface/public/assets/mascot/pick.png +0 -0
- package/neural-interface/public/assets/mascot/plan.gif +0 -0
- package/neural-interface/public/assets/mascot/plan.png +0 -0
- package/neural-interface/public/assets/mascot/present.gif +0 -0
- package/neural-interface/public/assets/mascot/present.png +0 -0
- package/neural-interface/public/assets/mascot/project.gif +0 -0
- package/neural-interface/public/assets/mascot/project.png +0 -0
- package/neural-interface/public/assets/mascot/read.gif +0 -0
- package/neural-interface/public/assets/mascot/read.png +0 -0
- package/neural-interface/public/assets/mascot/runs.gif +0 -0
- package/neural-interface/public/assets/mascot/runs.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/automation.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/automation.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/browser.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/browser.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/code.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/code.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/complex.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/complex.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/computer.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/computer.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/design.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/design.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/edit.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/edit.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/generic.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/generic.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/image_gen.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/image_gen.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/manifest.json +173 -0
- package/neural-interface/public/assets/mascot/scenes/memory.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/memory.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/quick.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/quick.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/read.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/read.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/research.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/research.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/review.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/review.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/search.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/search.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/shell.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/shell.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/thinking.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/thinking.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/video_gen.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/video_gen.png +0 -0
- package/neural-interface/public/assets/mascot/scenes/web.gif +0 -0
- package/neural-interface/public/assets/mascot/scenes/web.png +0 -0
- package/neural-interface/public/assets/mascot/search.gif +0 -0
- package/neural-interface/public/assets/mascot/search.png +0 -0
- package/neural-interface/public/assets/mascot/shell.gif +0 -0
- package/neural-interface/public/assets/mascot/shell.png +0 -0
- package/neural-interface/public/assets/mascot/skill.gif +0 -0
- package/neural-interface/public/assets/mascot/skill.png +0 -0
- package/neural-interface/public/assets/mascot/sleep.gif +0 -0
- package/neural-interface/public/assets/mascot/sleep.png +0 -0
- package/neural-interface/public/assets/mascot/subagent.gif +0 -0
- package/neural-interface/public/assets/mascot/subagent.png +0 -0
- package/neural-interface/public/assets/mascot/success.gif +0 -0
- package/neural-interface/public/assets/mascot/success.png +0 -0
- package/neural-interface/public/assets/mascot/think.gif +0 -0
- package/neural-interface/public/assets/mascot/think.png +0 -0
- package/neural-interface/public/assets/mascot/wait.gif +0 -0
- package/neural-interface/public/assets/mascot/wait.png +0 -0
- package/neural-interface/public/assets/mascot/watch.gif +0 -0
- package/neural-interface/public/assets/mascot/watch.png +0 -0
- package/neural-interface/public/assets/mascot/web.gif +0 -0
- package/neural-interface/public/assets/mascot/web.png +0 -0
- package/neural-interface/public/onboarding.html +14 -1
- package/neural-interface/public/shared/agent-runtime-options.js +49 -8
- package/neural-interface/public/shared/agy/agy-panel.js +2 -2
- package/neural-interface/public/shared/api.js +4 -4
- package/neural-interface/public/shared/assistant/asst-ask-styles.js +8 -8
- package/neural-interface/public/shared/assistant/asst-ask.js +1 -1
- package/neural-interface/public/shared/assistant/asst-brain-picker.js +99 -9
- package/neural-interface/public/shared/assistant/asst-control.js +1 -1
- package/neural-interface/public/shared/assistant/asst-limit-wait.js +4 -4
- package/neural-interface/public/shared/assistant/asst-panel.js +6 -2
- package/neural-interface/public/shared/assistant/asst-rack-styles.js +4 -4
- package/neural-interface/public/shared/assistant/asst-render.js +6 -6
- package/neural-interface/public/shared/assistant/asst-route-models.js +64 -4
- package/neural-interface/public/shared/assistant/asst-route.js +24 -7
- package/neural-interface/public/shared/assistant/asst-routes-editor.js +154 -39
- package/neural-interface/public/shared/assistant/asst-run-stage.js +3 -3
- package/neural-interface/public/shared/assistant/asst-scene-styles.js +8 -111
- package/neural-interface/public/shared/assistant/asst-scenes.js +46 -318
- package/neural-interface/public/shared/assistant/asst-sidepanel.js +2 -2
- package/neural-interface/public/shared/assistant/asst-state.js +72 -2
- package/neural-interface/public/shared/assistant/asst-styles.js +58 -14
- package/neural-interface/public/shared/automation-approval-view.js +75 -0
- package/neural-interface/public/shared/cdx/cdx-panel.js +2 -2
- package/neural-interface/public/shared/context-menu-model.js +2 -0
- package/neural-interface/public/shared/editor/editor-panel-logic.js +10 -0
- package/neural-interface/public/shared/editor/editor-panel.js +10 -2
- package/neural-interface/public/shared/html-shell.js +30 -22
- package/neural-interface/public/shared/morelogin-profile-choices.js +107 -0
- package/neural-interface/public/shared/morelogin-profile-field.js +186 -0
- package/neural-interface/public/shared/mtc/mtc-client.js +181 -0
- package/neural-interface/public/shared/mtc/mtc-compose.js +40 -0
- package/neural-interface/public/shared/mtc/mtc-copy.js +253 -0
- package/neural-interface/public/shared/mtc/mtc-logic.js +478 -0
- package/neural-interface/public/shared/mtc/mtc-panel.js +1354 -0
- package/neural-interface/public/shared/mtc/mtc-render.js +172 -0
- package/neural-interface/public/shared/mtc/mtc-styles.js +437 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +6 -2
- package/neural-interface/public/shared/provider-icons.js +3 -0
- package/neural-interface/public/shared/schedules-view.js +7 -0
- package/neural-interface/public/shared/server-back.js +50 -0
- package/neural-interface/public/shared/settings/settings-browser-owner-access.js +72 -0
- package/neural-interface/public/shared/settings/settings-data.js +1 -0
- package/neural-interface/public/shared/settings/settings-ia.js +3 -1
- package/neural-interface/public/shared/settings/settings-local-profiles.js +153 -0
- package/neural-interface/public/shared/settings/settings-maritaca-opencode.js +105 -0
- package/neural-interface/public/shared/sidepanel-session-status.js +16 -2
- package/neural-interface/public/shared/state.js +1 -1
- package/neural-interface/public/shared/styles.css +356 -157
- package/neural-interface/public/shared/synabun-mascot-assets.js +67 -0
- package/neural-interface/public/shared/synabun-mascot.js +136 -12
- package/neural-interface/public/shared/synabun-ticker.js +3 -0
- package/neural-interface/public/shared/topright-overflow-logic.js +1 -0
- package/neural-interface/public/shared/ui-automation-approval.js +188 -0
- package/neural-interface/public/shared/ui-automation-studio.js +130 -90
- package/neural-interface/public/shared/ui-claude-panel.js +50 -2
- package/neural-interface/public/shared/ui-maritaca-panel.js +15 -0
- package/neural-interface/public/shared/ui-navbar.js +17 -0
- package/neural-interface/public/shared/ui-panels.js +3 -1
- package/neural-interface/public/shared/ui-schedules-studio.js +118 -17
- package/neural-interface/public/shared/ui-scroll-follow.js +3 -0
- package/neural-interface/public/shared/ui-settings.js +590 -27
- package/neural-interface/public/shared/ui-sidepanel-session-entry.js +8 -2
- package/neural-interface/public/shared/ui-sidepanel-sessions.js +3 -3
- package/neural-interface/public/shared/ui-sidepanel-tray.js +24 -0
- package/neural-interface/public/shared/ui-sidepanel-windows.js +1 -1
- package/neural-interface/scripts/build-mascot-assets.mjs +30 -0
- package/neural-interface/server.js +1784 -195
- package/npm-shrinkwrap.json +5 -17
- package/package.json +5 -6
- package/postinstall.js +38 -39
- package/setup.js +33 -31
- package/skills/synabun/modules/changelog.md +14 -0
|
@@ -11,9 +11,15 @@ function extractionError(message, code) {
|
|
|
11
11
|
return Object.assign(new Error(message), { code });
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Apply the output budget to complete items, after deduplication, preserving JSON.
|
|
16
|
+
* `types` is read before the projection: the distinct `type` of the returned items (null for
|
|
17
|
+
* an item without a string one), so a caller whose `fields` drop `type` still knows what it got
|
|
18
|
+
* (browser_extract_tweets says "trend(s)" for an Explore tab's trends).
|
|
19
|
+
*/
|
|
15
20
|
export function budgetExtractionItems(items, { fields, maxItems = 50, maxChars } = {}) {
|
|
16
21
|
const selected = [];
|
|
22
|
+
const types = new Set();
|
|
17
23
|
let chars = 2; // JSON array brackets
|
|
18
24
|
let reason;
|
|
19
25
|
for (const item of items) {
|
|
@@ -22,9 +28,101 @@ export function budgetExtractionItems(items, { fields, maxItems = 50, maxChars }
|
|
|
22
28
|
const size = JSON.stringify(value).length + (selected.length ? 1 : 0);
|
|
23
29
|
if (maxChars !== undefined && chars + size > maxChars) { reason = 'max_chars'; break; }
|
|
24
30
|
selected.push(value);
|
|
31
|
+
types.add(typeof item.type === 'string' ? item.type : null);
|
|
25
32
|
chars += size;
|
|
26
33
|
}
|
|
27
|
-
return { items: selected, truncated: selected.length < items.length, budgetReason: reason };
|
|
34
|
+
return { items: selected, types: [...types], truncated: selected.length < items.length, budgetReason: reason };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ── Page functions ──
|
|
38
|
+
// Everything that runs in the page is a function with the extractor and the scroll
|
|
39
|
+
// target written into its source. Playwright evaluates a function's source once,
|
|
40
|
+
// inside the DevTools call, which Chromium exempts from the page's Content Security
|
|
41
|
+
// Policy; the readiness polls that follow run the compiled function on the page's
|
|
42
|
+
// own timer, where the page's CSP applies and nothing may call eval. Until
|
|
43
|
+
// 2026-11-06 the polls did `(0, eval)(script)` in the page, and x.com, whose
|
|
44
|
+
// `script-src` has no 'unsafe-eval', refused the second poll of every scrolled
|
|
45
|
+
// extraction with an EvalError ("the tweet extractor doesn't work on X").
|
|
46
|
+
|
|
47
|
+
const BUSY_SELECTOR = '[aria-busy="true"], [role="progressbar"]';
|
|
48
|
+
|
|
49
|
+
/** An extractor or a scroll target is one JavaScript expression (statements go in an IIFE). */
|
|
50
|
+
function expressionSource(source, what) {
|
|
51
|
+
const text = typeof source === 'string' ? source.trim().replace(/;+\s*$/, '') : '';
|
|
52
|
+
if (!text) throw new Error(`${what} required`);
|
|
53
|
+
return text;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Compile a page function from its source. The function is only ever serialized for
|
|
58
|
+
* the page (Playwright sends its source and runs it there); nothing calls it here.
|
|
59
|
+
* A script that is not an expression fails here, before anything touches the page.
|
|
60
|
+
*/
|
|
61
|
+
function pageFunction(parameter, body, what) {
|
|
62
|
+
try {
|
|
63
|
+
return new Function(parameter, body);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new Error(`${what} must be a JavaScript expression that returns an array of objects: ${error.message}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The four page functions of one extraction: the first read, the scroll (returns whether
|
|
71
|
+
* the scroll root was already loading), the readiness predicate `page.waitForFunction`
|
|
72
|
+
* polls, and the one read after a readiness timeout. None of them evaluates a string.
|
|
73
|
+
* Their own locals are `__sb`-prefixed: the extractor and the scroll target were evaluated
|
|
74
|
+
* in the page's global scope before, and an expression that names `items`, `root` or
|
|
75
|
+
* `target` must still mean the page's, never a local of the function around it.
|
|
76
|
+
*/
|
|
77
|
+
export function extractionPageFunctions(script, scrollTarget = 'window') {
|
|
78
|
+
const items = expressionSource(script, 'Extractor script');
|
|
79
|
+
const target = expressionSource(scrollTarget, 'Scroll target');
|
|
80
|
+
const observe = `
|
|
81
|
+
const __sbTarget = (
|
|
82
|
+
${target}
|
|
83
|
+
);
|
|
84
|
+
const __sbRoot = __sbTarget === window ? document : __sbTarget;
|
|
85
|
+
if (!__sbRoot) throw new Error('Scroll container disappeared while waiting.');
|
|
86
|
+
const __sbBusy = ${JSON.stringify(BUSY_SELECTOR)};
|
|
87
|
+
const __sbLoading = !!(__sbRoot.matches?.(__sbBusy) || __sbRoot.querySelector(__sbBusy));`;
|
|
88
|
+
return {
|
|
89
|
+
read: pageFunction('', `return (
|
|
90
|
+
${items}
|
|
91
|
+
);`, 'Extractor script'),
|
|
92
|
+
scroll: pageFunction('__sbState', `
|
|
93
|
+
const __sbTarget = (
|
|
94
|
+
${target}
|
|
95
|
+
);
|
|
96
|
+
if (!__sbTarget || typeof __sbTarget.scrollBy !== 'function') throw new Error('Scroll container not found.');
|
|
97
|
+
const __sbRoot = __sbTarget === window ? document : __sbTarget;
|
|
98
|
+
const __sbBusy = ${JSON.stringify(BUSY_SELECTOR)};
|
|
99
|
+
const __sbLoading = !!(__sbRoot.matches?.(__sbBusy) || __sbRoot.querySelector(__sbBusy));
|
|
100
|
+
__sbTarget.scrollBy(0, __sbState.distance);
|
|
101
|
+
return __sbLoading;`, 'Scroll target'),
|
|
102
|
+
readiness: pageFunction('__sbState', `
|
|
103
|
+
const __sbItems = (
|
|
104
|
+
${items}
|
|
105
|
+
);
|
|
106
|
+
if (!Array.isArray(__sbItems)) throw new Error('Extractor must return an array of objects.');
|
|
107
|
+
const __sbKnown = new Set(__sbState.seen);
|
|
108
|
+
const __sbHasNew = __sbItems.some(__sbItem => {
|
|
109
|
+
if (!__sbItem || typeof __sbItem !== 'object' || Array.isArray(__sbItem)) throw new Error('Extractor must return an array of objects.');
|
|
110
|
+
let __sbKey;
|
|
111
|
+
for (const __sbField of __sbState.dedupeKeys) {
|
|
112
|
+
if (__sbItem[__sbField] !== null && __sbItem[__sbField] !== undefined && __sbItem[__sbField] !== '') { __sbKey = __sbField + ':' + __sbItem[__sbField]; break; }
|
|
113
|
+
}
|
|
114
|
+
return !__sbKnown.has(__sbKey ?? JSON.stringify(__sbItem));
|
|
115
|
+
});${observe}
|
|
116
|
+
// Playwright reuses this argument between predicate polls. Remember loading
|
|
117
|
+
// that begins after scroll dispatch as well as before it.
|
|
118
|
+
__sbState.loadingBefore ||= __sbLoading;
|
|
119
|
+
const __sbLoadingEnded = __sbState.loadingBefore && !__sbLoading;
|
|
120
|
+
return !__sbLoading && (__sbHasNew || __sbLoadingEnded) ? { items: __sbItems } : false;`, 'Extractor script'),
|
|
121
|
+
finalRead: pageFunction('', `${observe}
|
|
122
|
+
return { loading: __sbLoading, items: __sbLoading ? [] : (
|
|
123
|
+
${items}
|
|
124
|
+
) };`, 'Extractor script'),
|
|
125
|
+
};
|
|
28
126
|
}
|
|
29
127
|
|
|
30
128
|
/**
|
|
@@ -40,6 +138,8 @@ export async function runBrowserExtraction(page, script, opts = {}, runtime = {}
|
|
|
40
138
|
const deadline = Math.min(Date.now() + timeoutMs, runtime.deadline ?? Infinity);
|
|
41
139
|
const signal = runtime.signal;
|
|
42
140
|
const measure = runtime.measure || ((_name, fn) => fn());
|
|
141
|
+
// A script that is not an expression is refused here, before any page call.
|
|
142
|
+
const fns = extractionPageFunctions(script, opts.scrollTarget || 'window');
|
|
43
143
|
const collected = new Map();
|
|
44
144
|
let raw = 0;
|
|
45
145
|
let scrollsUsed = 0;
|
|
@@ -86,7 +186,7 @@ export async function runBrowserExtraction(page, script, opts = {}, runtime = {}
|
|
|
86
186
|
}
|
|
87
187
|
|
|
88
188
|
try {
|
|
89
|
-
let items = await bounded('extraction', () => page.evaluate(
|
|
189
|
+
let items = await bounded('extraction', () => page.evaluate(fns.read));
|
|
90
190
|
for (let round = 0; ; round++) {
|
|
91
191
|
active();
|
|
92
192
|
const added = merge(items);
|
|
@@ -100,60 +200,21 @@ export async function runBrowserExtraction(page, script, opts = {}, runtime = {}
|
|
|
100
200
|
if (round >= scrolls + bonusRounds) break;
|
|
101
201
|
|
|
102
202
|
const distance = (opts.scrollDistance ?? 1200) * (opts.scrollDirection ?? 1);
|
|
103
|
-
const
|
|
104
|
-
const loadingBefore = await bounded('action', () => page.evaluate(({ scrollTarget, distance }) => {
|
|
105
|
-
const target = (0, eval)(scrollTarget);
|
|
106
|
-
if (!target || typeof target.scrollBy !== 'function') throw new Error('Scroll container not found.');
|
|
107
|
-
const root = target === window ? document : target;
|
|
108
|
-
const busy = '[aria-busy="true"], [role="progressbar"]';
|
|
109
|
-
const loading = !!(root.matches?.(busy) || root.querySelector(busy));
|
|
110
|
-
target.scrollBy(0, distance);
|
|
111
|
-
return loading;
|
|
112
|
-
}, { scrollTarget, distance }));
|
|
203
|
+
const loadingBefore = await bounded('action', () => page.evaluate(fns.scroll, { distance }));
|
|
113
204
|
scrollsUsed++;
|
|
114
205
|
// Poll inside the renderer, not over HTTP. Reuse the first newly visible
|
|
115
206
|
// extraction so virtualized items cannot disappear between wait and read.
|
|
116
207
|
const waitMs = Math.min(opts.settleMs ?? 3000, Math.max(1, deadline - Date.now()));
|
|
117
208
|
try {
|
|
118
209
|
items = await bounded('readiness', async () => {
|
|
119
|
-
const handle = await page.waitForFunction(
|
|
120
|
-
const { script, dedupeKeys, seen, scrollTarget } = state;
|
|
121
|
-
const items = (0, eval)(script);
|
|
122
|
-
if (!Array.isArray(items)) throw new Error('Extractor must return an array of objects.');
|
|
123
|
-
const known = new Set(seen);
|
|
124
|
-
const hasNew = items.some(item => {
|
|
125
|
-
if (!item || typeof item !== 'object' || Array.isArray(item)) throw new Error('Extractor must return an array of objects.');
|
|
126
|
-
let key;
|
|
127
|
-
for (const field of dedupeKeys) {
|
|
128
|
-
if (item[field] !== null && item[field] !== undefined && item[field] !== '') { key = `${field}:${item[field]}`; break; }
|
|
129
|
-
}
|
|
130
|
-
return !known.has(key ?? JSON.stringify(item));
|
|
131
|
-
});
|
|
132
|
-
const target = (0, eval)(scrollTarget);
|
|
133
|
-
const root = target === window ? document : target;
|
|
134
|
-
if (!root) throw new Error('Scroll container disappeared while waiting.');
|
|
135
|
-
const busy = '[aria-busy="true"], [role="progressbar"]';
|
|
136
|
-
const loading = !!(root.matches?.(busy) || root.querySelector(busy));
|
|
137
|
-
// Playwright reuses this argument between predicate polls. Remember
|
|
138
|
-
// loading that begins after scroll dispatch as well as before it.
|
|
139
|
-
state.loadingBefore ||= loading;
|
|
140
|
-
const loadingEnded = state.loadingBefore && !loading;
|
|
141
|
-
return !loading && (hasNew || loadingEnded) ? { items } : false;
|
|
142
|
-
}, { script, dedupeKeys, seen: [...collected.keys()], scrollTarget, loadingBefore }, { timeout: waitMs, polling: 50 });
|
|
210
|
+
const handle = await page.waitForFunction(fns.readiness, { dedupeKeys, seen: [...collected.keys()], loadingBefore }, { timeout: waitMs, polling: 50 });
|
|
143
211
|
try { return (await handle.jsonValue()).items; } finally { await handle.dispose(); }
|
|
144
212
|
});
|
|
145
213
|
} catch (error) {
|
|
146
214
|
if (error.name !== 'TimeoutError') throw error;
|
|
147
215
|
// Read once at the readiness boundary, but never accept known loading
|
|
148
216
|
// skeletons merely because the readiness timer elapsed.
|
|
149
|
-
const finalRead = await bounded('extraction', () => page.evaluate(
|
|
150
|
-
const target = (0, eval)(scrollTarget);
|
|
151
|
-
const root = target === window ? document : target;
|
|
152
|
-
if (!root) throw new Error('Scroll container disappeared while waiting.');
|
|
153
|
-
const busy = '[aria-busy="true"], [role="progressbar"]';
|
|
154
|
-
const loading = !!(root.matches?.(busy) || root.querySelector(busy));
|
|
155
|
-
return { loading, items: loading ? [] : (0, eval)(script) };
|
|
156
|
-
}, { script, scrollTarget }));
|
|
217
|
+
const finalRead = await bounded('extraction', () => page.evaluate(fns.finalRead));
|
|
157
218
|
if (finalRead.loading) throw extractionError('Feed is still loading after the readiness deadline; collected items are preserved.', 'readiness_timeout');
|
|
158
219
|
items = finalRead.items;
|
|
159
220
|
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// ── A session's own MoreLogin profile: what it resolves to, and where the choice is kept (DOM-free) ──
|
|
2
|
+
//
|
|
3
|
+
// A run can be launched with a MoreLogin profile (a schedule's, a group's, a timer's, a launch's, a dispatch's:
|
|
4
|
+
// server.js pinRunMoreLoginProfile). A session can also choose one for itself while it runs (the `morelogin` tool's
|
|
5
|
+
// "use" action, POST /api/morelogin/use): the same pin, set for the caller's own owner key and for nobody else. The
|
|
6
|
+
// default of Settings → Browser and every other session stay as they are.
|
|
7
|
+
//
|
|
8
|
+
// The pin map lives in memory, and a run's launch pin is read back from its loop file after a restart. A choice a
|
|
9
|
+
// session made itself has no such file (a side panel, the Assistant's brain, a CLI window), and losing it would send
|
|
10
|
+
// that session's next browser call to the default profile without a word: another account. So choices are kept in one
|
|
11
|
+
// small file (DATA_HOME/data/browser-profile-choices.json) and put back into the pin map when the server starts.
|
|
12
|
+
// `envId: null` is a choice too: "the Browser settings default", made by a run that was launched with a profile.
|
|
13
|
+
|
|
14
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
15
|
+
import { dirname } from 'node:path';
|
|
16
|
+
|
|
17
|
+
/** Choices kept at most (the oldest go first). */
|
|
18
|
+
export const PROFILE_CHOICES_MAX = 500;
|
|
19
|
+
/** A choice older than this is not put back after a restart: no session lives that long. */
|
|
20
|
+
export const PROFILE_CHOICE_MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000;
|
|
21
|
+
|
|
22
|
+
const OWNER_RE = /^[A-Za-z0-9_:-]{1,128}$/;
|
|
23
|
+
const cleanId = (value) => {
|
|
24
|
+
const id = value === null || value === undefined ? '' : String(value).trim();
|
|
25
|
+
return id && id.length <= 128 && !/[\u0000-\u001f]/.test(id) ? id : '';
|
|
26
|
+
};
|
|
27
|
+
const cleanName = (value) => (typeof value === 'string' ? value.trim().slice(0, 200) : '');
|
|
28
|
+
|
|
29
|
+
function refusal(status, code, message, extra = {}) {
|
|
30
|
+
return Object.assign(new Error(message), { status, code, ...extra });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The profile a reference names among MoreLogin's listed profiles.
|
|
35
|
+
* @param {{ envId?: unknown, name?: unknown, profile?: unknown }} ref `envId`: an id; `name`: a name; `profile`:
|
|
36
|
+
* either (an id first, then a name). Names match whole, without regard to case.
|
|
37
|
+
* @param {Array<{ id: unknown, name?: unknown }>} envs what MoreLogin lists
|
|
38
|
+
* @returns {{ envId: string, name: string|null }}
|
|
39
|
+
* @throws 400 PROFILE_REQUIRED (nothing named), 404 ENV_NOT_FOUND, 409 PROFILE_AMBIGUOUS (a name several profiles have)
|
|
40
|
+
*/
|
|
41
|
+
export function resolveProfileRef(ref, envs) {
|
|
42
|
+
const rows = (Array.isArray(envs) ? envs : []).filter((env) => env && cleanId(env.id));
|
|
43
|
+
const byId = (id) => rows.find((env) => String(env.id) === id) || null;
|
|
44
|
+
const byName = (name) => rows.filter((env) => cleanName(env.name).toLowerCase() === name.toLowerCase());
|
|
45
|
+
const found = (env) => ({ envId: String(env.id), name: cleanName(env.name) || null });
|
|
46
|
+
const listed = () => (rows.length
|
|
47
|
+
? ` MoreLogin lists: ${rows.slice(0, 20).map((env) => `${cleanName(env.name) || '(no name)'} (id ${env.id})`).join(', ')}${rows.length > 20 ? ', …' : ''}.`
|
|
48
|
+
: ' MoreLogin lists no profile.');
|
|
49
|
+
const envId = cleanId(ref?.envId);
|
|
50
|
+
const name = cleanName(ref?.name);
|
|
51
|
+
const either = cleanName(ref?.profile);
|
|
52
|
+
if (!envId && !name && !either) throw refusal(400, 'PROFILE_REQUIRED', 'Name the MoreLogin profile: its id (envId) or its name. The "list" action of the morelogin tool gives both.');
|
|
53
|
+
if (envId) {
|
|
54
|
+
const env = byId(envId);
|
|
55
|
+
if (env) return found(env);
|
|
56
|
+
if (!name && !either) throw refusal(404, 'ENV_NOT_FOUND', `MoreLogin has no profile with id ${envId.slice(0, 64)}.${listed()}`);
|
|
57
|
+
}
|
|
58
|
+
const wanted = name || either;
|
|
59
|
+
if (!envId && either) {
|
|
60
|
+
const env = byId(either);
|
|
61
|
+
if (env) return found(env);
|
|
62
|
+
}
|
|
63
|
+
const matches = byName(wanted);
|
|
64
|
+
if (matches.length === 1) return found(matches[0]);
|
|
65
|
+
if (matches.length > 1) {
|
|
66
|
+
throw refusal(409, 'PROFILE_AMBIGUOUS', `${matches.length} MoreLogin profiles are named "${wanted}" (ids ${matches.map((env) => env.id).join(', ')}): name the one you mean by its id.`,
|
|
67
|
+
{ ids: matches.map((env) => String(env.id)) });
|
|
68
|
+
}
|
|
69
|
+
throw refusal(404, 'ENV_NOT_FOUND', `MoreLogin has no profile named "${wanted.slice(0, 64)}"${envId ? ` and none with id ${envId.slice(0, 64)}` : ''}.${listed()}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The choices file. Every method is synchronous and never throws: a file that cannot be read is no choices, and a
|
|
74
|
+
* write that fails is logged and leaves the in-memory pin (the session's choice still holds until a restart).
|
|
75
|
+
* @param {{ file: string, now?: () => number, log?: (line: string) => void, max?: number, maxAgeMs?: number }} opts
|
|
76
|
+
*/
|
|
77
|
+
export function createProfileChoiceStore({ file, now = Date.now, log = () => {}, max = PROFILE_CHOICES_MAX, maxAgeMs = PROFILE_CHOICE_MAX_AGE_MS } = {}) {
|
|
78
|
+
/** ownerKey → { envId: string|null, name: string|null, at: number }, oldest first. */
|
|
79
|
+
let choices = null;
|
|
80
|
+
|
|
81
|
+
function read() {
|
|
82
|
+
const out = new Map();
|
|
83
|
+
try {
|
|
84
|
+
if (!file || !existsSync(file)) return out;
|
|
85
|
+
const data = JSON.parse(readFileSync(file, 'utf-8'));
|
|
86
|
+
const rows = data && typeof data === 'object' && data.choices && typeof data.choices === 'object' ? Object.entries(data.choices) : [];
|
|
87
|
+
const fresh = rows
|
|
88
|
+
.filter(([owner, row]) => OWNER_RE.test(owner) && row && typeof row === 'object' && Number.isFinite(Number(row.at)) && now() - Number(row.at) <= maxAgeMs)
|
|
89
|
+
.sort((a, b) => Number(a[1].at) - Number(b[1].at));
|
|
90
|
+
for (const [owner, row] of fresh) {
|
|
91
|
+
const envId = cleanId(row.envId);
|
|
92
|
+
out.set(owner, { envId: envId || null, name: envId ? (cleanName(row.name) || null) : null, at: Number(row.at) });
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
log(`the sessions' MoreLogin profile choices could not be read (${error?.message || error}): none is put back`);
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function write() {
|
|
101
|
+
if (!file) return;
|
|
102
|
+
try {
|
|
103
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
104
|
+
const temp = `${file}.${process.pid}.tmp`;
|
|
105
|
+
writeFileSync(temp, JSON.stringify({ version: 1, choices: Object.fromEntries(choices) }, null, 2));
|
|
106
|
+
renameSync(temp, file);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
log(`a session's MoreLogin profile choice was not saved (${error?.message || error}): it holds until the server restarts`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const loaded = () => (choices ||= read());
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
/** Every kept choice: [ownerKey, { envId, name }], oldest first. */
|
|
116
|
+
entries() {
|
|
117
|
+
return [...loaded()].map(([owner, row]) => [owner, { envId: row.envId, name: row.name }]);
|
|
118
|
+
},
|
|
119
|
+
/** The choice of one owner ({ envId, name }; envId null = the default), or undefined when it made none. */
|
|
120
|
+
get(ownerKey) {
|
|
121
|
+
const row = loaded().get(ownerKey);
|
|
122
|
+
return row ? { envId: row.envId, name: row.name } : undefined;
|
|
123
|
+
},
|
|
124
|
+
/** Keep `ownerKey`'s choice: a profile, or null for "the Browser settings default". */
|
|
125
|
+
set(ownerKey, choice) {
|
|
126
|
+
if (!OWNER_RE.test(String(ownerKey || ''))) return false;
|
|
127
|
+
const envId = cleanId(choice?.envId);
|
|
128
|
+
const map = loaded();
|
|
129
|
+
map.delete(ownerKey);
|
|
130
|
+
map.set(ownerKey, { envId: envId || null, name: envId ? (cleanName(choice?.name) || null) : null, at: now() });
|
|
131
|
+
while (map.size > max) map.delete(map.keys().next().value);
|
|
132
|
+
write();
|
|
133
|
+
return true;
|
|
134
|
+
},
|
|
135
|
+
/** Forget `ownerKey`'s choice (it never made one). */
|
|
136
|
+
delete(ownerKey) {
|
|
137
|
+
const map = loaded();
|
|
138
|
+
if (!map.delete(ownerKey)) return false;
|
|
139
|
+
write();
|
|
140
|
+
return true;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One face for the two kinds of browser profile SynaBun drives over the Chrome DevTools protocol:
|
|
3
|
+
* • MoreLogin profiles (lib/morelogin.js): the MoreLogin app's own, reached through its local API;
|
|
4
|
+
* • local profiles (lib/local-profiles.js): a Chrome SynaBun starts itself with a data folder of its own.
|
|
5
|
+
*
|
|
6
|
+
* Everything server.js does with a profile is keyed by the profile's id (the default AI browser, the owner's list of
|
|
7
|
+
* profiles agents may choose, a run's or a session's pin, the pickers, start / stop). So this module re-exports
|
|
8
|
+
* lib/morelogin.js as it is and routes the calls that take an id: a local id (`lp-` + 12 hex characters) goes to the
|
|
9
|
+
* local launcher, every other id to MoreLogin. The lists hold both kinds; a local row says `kind: 'local'`.
|
|
10
|
+
*
|
|
11
|
+
* A local profile is never created in, listed by or started through MoreLogin, and `createProfile` stays MoreLogin's.
|
|
12
|
+
*/
|
|
13
|
+
import * as ml from './morelogin.js';
|
|
14
|
+
import { LOCAL_PROFILE_PORT_RANGE, createLocalProfiles, isLocalProfileId } from './local-profiles.js';
|
|
15
|
+
|
|
16
|
+
export * from './morelogin.js';
|
|
17
|
+
export { isLocalProfileId, LOCAL_PROFILE_PORT_RANGE };
|
|
18
|
+
|
|
19
|
+
let _local = null;
|
|
20
|
+
|
|
21
|
+
/** Sets up the local profiles of this process (server.js, once; a test, with its own root). → the instance. */
|
|
22
|
+
export function configureLocalProfiles(options) {
|
|
23
|
+
_local = options ? createLocalProfiles(options) : null;
|
|
24
|
+
return _local;
|
|
25
|
+
}
|
|
26
|
+
/** The local profiles, or null where none were set up. */
|
|
27
|
+
export function localProfiles() { return _local; }
|
|
28
|
+
|
|
29
|
+
function local() {
|
|
30
|
+
if (!_local) throw Object.assign(new Error('Local profiles are not available in this process.'), { status: 500, code: 'LOCAL_PROFILES_UNAVAILABLE' });
|
|
31
|
+
return _local;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Rows of the local profiles in the list's shape ([] where none were set up). Throws only for an unreadable store. */
|
|
35
|
+
export async function listLocalProfiles() { return _local ? _local.listLocalProfiles() : []; }
|
|
36
|
+
export async function localProfileDetails() { return local().localProfileDetails(); }
|
|
37
|
+
export async function createLocalProfile(input) { return local().createLocalProfile(input); }
|
|
38
|
+
export async function updateLocalProfile(id, input) { return local().updateLocalProfile(id, input); }
|
|
39
|
+
export async function deleteLocalProfile(id) { return local().deleteLocalProfile(id); }
|
|
40
|
+
export async function checkLocalProfileProxy(id, override) { return local().checkLocalProfileProxy(id, override); }
|
|
41
|
+
export async function adoptLocalProfiles() { return _local ? _local.adoptRunning() : []; }
|
|
42
|
+
/** How many local profiles there are (0 where none were set up; nothing is started or probed). */
|
|
43
|
+
export function localProfileCount() { return _local ? _local.count() : 0; }
|
|
44
|
+
/** A local profile's name, or null. */
|
|
45
|
+
export function localProfileName(id) { return _local && isLocalProfileId(id) ? _local.nameOf(id) : null; }
|
|
46
|
+
/** { name, timezone, language } of a local profile, or null. */
|
|
47
|
+
export function localProfileSettings(id) { return _local && isLocalProfileId(id) ? _local.settingsOf(id) : null; }
|
|
48
|
+
/**
|
|
49
|
+
* The loopback ports SynaBun keeps for local profiles ({ from, to }): every proxy relay and every DevTools port of a
|
|
50
|
+
* local profile is inside it. A guarded worker's sandbox denies the whole range (server.js workerBrowserControlPorts).
|
|
51
|
+
*/
|
|
52
|
+
export function localProfilePortRange() { return _local ? _local.portRange : LOCAL_PROFILE_PORT_RANGE; }
|
|
53
|
+
/** The loopback ports of the local profiles' proxy relays. */
|
|
54
|
+
export function localRelayPorts() { try { return _local ? _local.relayPorts() : []; } catch { return []; } }
|
|
55
|
+
|
|
56
|
+
/** A short reason of a failed MoreLogin call, as the lists carry it. */
|
|
57
|
+
function describeError(err) {
|
|
58
|
+
return { message: String(err?.message || err), code: err?.code || null };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Every profile: MoreLogin's, then the local ones. → { envs, moreloginError, localCount }.
|
|
63
|
+
* `moreloginError` ({ message, code } or null): MoreLogin's list could not be read (the app is closed, its API
|
|
64
|
+
* refused); `envs` then holds the local profiles alone. Never throws for MoreLogin; an unreadable local store does.
|
|
65
|
+
*/
|
|
66
|
+
export async function listAllProfiles(opts = {}) {
|
|
67
|
+
const firstPage = (opts.pageNo || 1) === 1;
|
|
68
|
+
const localRows = firstPage ? await listLocalProfiles() : [];
|
|
69
|
+
let remote = [];
|
|
70
|
+
let moreloginError = null;
|
|
71
|
+
try { remote = await ml.listProfiles(opts); } catch (err) { moreloginError = describeError(err); moreloginError.cause = err; }
|
|
72
|
+
return { envs: [...remote, ...localRows], moreloginError, localCount: localRows.length };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The list server.js reads everywhere: MoreLogin's profiles and the local ones. When MoreLogin cannot be reached the
|
|
77
|
+
* local ones are returned, and the array carries the reason as `moreloginError` (not enumerable). With no local
|
|
78
|
+
* profile at all the answer is MoreLogin's alone, its failure included, as it was before local profiles existed.
|
|
79
|
+
*/
|
|
80
|
+
export async function listProfiles(opts = {}) {
|
|
81
|
+
const all = await listAllProfiles(opts);
|
|
82
|
+
if (all.moreloginError && !all.localCount) throw all.moreloginError.cause;
|
|
83
|
+
const rows = all.envs;
|
|
84
|
+
Object.defineProperty(rows, 'moreloginError', { value: all.moreloginError ? { message: all.moreloginError.message, code: all.moreloginError.code } : null, enumerable: false });
|
|
85
|
+
return rows;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Start a profile → { debugPort, webdriver, kernel }. A local id starts SynaBun's own Chrome. `opts.owner === true`
|
|
90
|
+
* is the owner's own Start: only that may end a running local profile to start it again. Every other caller of a
|
|
91
|
+
* local profile that has to be restarted first (its proxy changed) or that does not answer is refused with
|
|
92
|
+
* LOCAL_PROFILE_RESTART_NEEDED / LOCAL_PROFILE_BUSY, and nothing here ever answers with another profile in its place.
|
|
93
|
+
*/
|
|
94
|
+
export async function startProfile(envId, opts = {}) {
|
|
95
|
+
if (isLocalProfileId(String(envId))) return local().startLocalProfile(String(envId), { owner: opts?.owner === true });
|
|
96
|
+
return ml.startProfile(envId, opts);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Stop a profile. */
|
|
100
|
+
export async function stopProfile(envId, opts = {}) {
|
|
101
|
+
if (isLocalProfileId(String(envId))) return local().stopLocalProfile(String(envId));
|
|
102
|
+
return ml.stopProfile(envId, opts);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** One profile's status. A local one: { status: 'running' | 'stopped', debugPort }. */
|
|
106
|
+
export async function profileStatus(envId, opts = {}) {
|
|
107
|
+
if (isLocalProfileId(String(envId))) return local().localProfileStatus(String(envId));
|
|
108
|
+
return ml.profileStatus(envId, opts);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// A local profile is Chrome: its browser is known at once, without a lookup.
|
|
112
|
+
export async function profileKernelOf(envId, opts = {}) {
|
|
113
|
+
if (isLocalProfileId(String(envId))) return 'chromium';
|
|
114
|
+
return ml.profileKernelOf(envId, opts);
|
|
115
|
+
}
|
|
116
|
+
export function heldProfileKernel(envId, now) {
|
|
117
|
+
if (isLocalProfileId(String(envId))) return { known: true, kernel: 'chromium' };
|
|
118
|
+
return ml.heldProfileKernel(envId, now);
|
|
119
|
+
}
|
|
120
|
+
export function lookupProfileKernel(envId, opts = {}) {
|
|
121
|
+
if (isLocalProfileId(String(envId))) return Promise.resolve('chromium');
|
|
122
|
+
return ml.lookupProfileKernel(envId, opts);
|
|
123
|
+
}
|
|
124
|
+
export async function profileKernels(envIds, opts = {}) {
|
|
125
|
+
const ids = [...new Set((envIds || []).map(String))];
|
|
126
|
+
const remoteIds = ids.filter((id) => !isLocalProfileId(id));
|
|
127
|
+
const answer = remoteIds.length ? await ml.profileKernels(remoteIds, opts) : { kernels: {}, pending: 0 };
|
|
128
|
+
const kernels = { ...(answer.kernels || {}) };
|
|
129
|
+
for (const id of ids) if (isLocalProfileId(id)) kernels[id] = 'chromium';
|
|
130
|
+
return { kernels, pending: Number(answer.pending) || 0 };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// MoreLogin refuses a new profile when the account's plan holds no more. Its exact words are not documented and vary
|
|
134
|
+
// by language, so the match is loose: an API or HTTP refusal of the create call whose text speaks of a limit.
|
|
135
|
+
const PLAN_LIMIT_RE = /\b(?:limit\w*|quota|maximum|upgrade|plan|package|exceed\w*)\b|上限|套餐|超出|限制|额度/i;
|
|
136
|
+
/** Whether a failed `createProfile` reads as MoreLogin's plan limit. */
|
|
137
|
+
export function isMoreLoginPlanLimit(err) {
|
|
138
|
+
if (!err || (err.code !== 'MORELOGIN_API' && err.code !== 'MORELOGIN_HTTP')) return false;
|
|
139
|
+
// The hint SynaBun itself adds to an auth refusal is not MoreLogin's text.
|
|
140
|
+
const text = String(err.message || '').replace(/\(set the API ID and API Key[^)]*\)/g, '');
|
|
141
|
+
return PLAN_LIMIT_RE.test(text);
|
|
142
|
+
}
|
|
@@ -66,6 +66,159 @@ export function isRecoverableBrowserPageError(error) {
|
|
|
66
66
|
return classifyBrowserPageError(error) !== BrowserPageFailureDisposition.NONE;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
export const SLOW_NAVIGATION_NOTICE = 'The page is still loading: its document arrived, but did not finish loading within the navigation budget. Wait a moment, then browser_snapshot or extract; do not navigate to it again.';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* browser_navigate answers once the document commits, then waits for DOMContentLoaded at most
|
|
73
|
+
* this long (benchmarked 2026-11-08: waiting for DCL plus a default snapshot added 0.6–1.0 s per
|
|
74
|
+
* navigate and far more on heavy pages). A page that has not parsed by then is answered with
|
|
75
|
+
* `loadState: 'commit'` and PAGE_STILL_PARSING_NOTICE; never a failure.
|
|
76
|
+
*/
|
|
77
|
+
export const NAVIGATE_DCL_CAP_MS = 1500;
|
|
78
|
+
/** The bounded wait the snapshot routes give a page that is still parsing before they capture it. */
|
|
79
|
+
export const SNAPSHOT_DCL_WAIT_MS = 5000;
|
|
80
|
+
|
|
81
|
+
export const PAGE_STILL_PARSING_NOTICE = 'The page committed but is still parsing; browser_snapshot (or any action) waits briefly for it. Do not navigate to it again.';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Wait for the page's DOMContentLoaded, at most `capMs`. Resolves 'domcontentloaded' when it
|
|
85
|
+
* arrived (at once when it already had), 'commit' when the cap ran out or the wait failed
|
|
86
|
+
* (closed page, a new navigation): it never rejects.
|
|
87
|
+
*/
|
|
88
|
+
export async function waitForDomContentLoadedCapped(page, capMs = NAVIGATE_DCL_CAP_MS) {
|
|
89
|
+
const timeout = Math.max(1, Math.floor(Number(capMs) || 0));
|
|
90
|
+
try {
|
|
91
|
+
await page.waitForLoadState('domcontentloaded', { timeout });
|
|
92
|
+
return 'domcontentloaded';
|
|
93
|
+
} catch {
|
|
94
|
+
return 'commit';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A navigation that timed out after its document committed is a slow page, not a wedged
|
|
100
|
+
* tab. x.com's first load in a cold profile starts its service worker and parses its bundles
|
|
101
|
+
* for longer than the route's 15 s, and its busy renderer misses the 1 s liveness probe: until
|
|
102
|
+
* 2026-11-06 the route then reset the loading tab to about:blank and blocked the URL for two
|
|
103
|
+
* minutes, so "x.com times out on first load" while a warm retry loaded fine.
|
|
104
|
+
* `committed` is whether the main frame navigated while the goto ran; `url` the page's now.
|
|
105
|
+
*/
|
|
106
|
+
export function navigationLandedDespiteTimeout(error, { committed, url }) {
|
|
107
|
+
const message = errorMessage(error);
|
|
108
|
+
const timedOut = error?.code === 'SYNABUN_BROWSER_OPERATION_TIMEOUT'
|
|
109
|
+
|| error?.name === 'TimeoutError'
|
|
110
|
+
|| /Timeout \d+ms exceeded/i.test(message);
|
|
111
|
+
if (!timedOut || committed !== true) return false;
|
|
112
|
+
return typeof url === 'string' && /^https?:/i.test(url);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A URL as navigation compares it: parsed, without its fragment; the text itself when it does not parse. */
|
|
116
|
+
function documentUrl(url) {
|
|
117
|
+
try { const parsed = new URL(String(url)); parsed.hash = ''; return parsed.href; } catch { return String(url ?? ''); }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether the main frame commits the document a goto asked for: `framenavigated` of the main frame
|
|
122
|
+
* (never a subframe) to the requested URL or to a URL its main-frame redirect chain led to.
|
|
123
|
+
* A navigation of the main frame elsewhere (the previous page's own script, an ad frame) never
|
|
124
|
+
* counts. `stop()` removes both listeners and may be called any number of times; `committed`
|
|
125
|
+
* keeps its value after it.
|
|
126
|
+
*/
|
|
127
|
+
export function trackMainDocumentCommit(page, requestedUrl) {
|
|
128
|
+
const accepted = new Set([documentUrl(requestedUrl)]);
|
|
129
|
+
let committed = false;
|
|
130
|
+
let stopped = false;
|
|
131
|
+
const isMain = frame => { try { return frame === page.mainFrame(); } catch { return false; } };
|
|
132
|
+
const onRequest = request => {
|
|
133
|
+
try {
|
|
134
|
+
if (!request.isNavigationRequest() || !isMain(request.frame())) return;
|
|
135
|
+
const from = request.redirectedFrom?.();
|
|
136
|
+
if (from && accepted.has(documentUrl(from.url()))) accepted.add(documentUrl(request.url()));
|
|
137
|
+
} catch { /* a request of a closing page: nothing to learn */ }
|
|
138
|
+
};
|
|
139
|
+
const onNavigated = frame => {
|
|
140
|
+
try { if (isMain(frame) && accepted.has(documentUrl(frame.url()))) committed = true; } catch { /* detached */ }
|
|
141
|
+
};
|
|
142
|
+
page.on('request', onRequest);
|
|
143
|
+
page.on('framenavigated', onNavigated);
|
|
144
|
+
return {
|
|
145
|
+
get committed() { return committed; },
|
|
146
|
+
stop() {
|
|
147
|
+
if (stopped) return;
|
|
148
|
+
stopped = true;
|
|
149
|
+
page.off('request', onRequest);
|
|
150
|
+
page.off('framenavigated', onNavigated);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const SLOW_LOAD_PROBE_DELAY_MS = 5_000;
|
|
156
|
+
export const SLOW_LOAD_PROBE_DEADLINE_MS = 1_500;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* After a navigate answered `slowLoad`, ask the tab once, later, whether it is alive: a page
|
|
160
|
+
* still loading answers a trivial evaluate within `deadlineMs` once its first parse is done, a
|
|
161
|
+
* dead renderer never does. Nothing here blocks the answer that was sent. Cancelled (and
|
|
162
|
+
* `onDead` never called) when the tab closes, when its main frame starts another document,
|
|
163
|
+
* when `isCurrent()` says the tab was replaced, or by `cancel()`. `done` resolves with
|
|
164
|
+
* 'alive' | 'dead' | 'cancelled' once the listeners are gone.
|
|
165
|
+
*/
|
|
166
|
+
export function scheduleSlowLoadProbe(page, {
|
|
167
|
+
delayMs = SLOW_LOAD_PROBE_DELAY_MS,
|
|
168
|
+
deadlineMs = SLOW_LOAD_PROBE_DEADLINE_MS,
|
|
169
|
+
isCurrent = () => true,
|
|
170
|
+
onDead,
|
|
171
|
+
setTimer = setTimeout,
|
|
172
|
+
clearTimer = clearTimeout,
|
|
173
|
+
} = {}) {
|
|
174
|
+
let state = 'waiting';
|
|
175
|
+
let timer = null;
|
|
176
|
+
let finish;
|
|
177
|
+
const done = new Promise(resolve => { finish = resolve; });
|
|
178
|
+
const detach = () => {
|
|
179
|
+
page.off('request', onRequest);
|
|
180
|
+
page.off('close', onClose);
|
|
181
|
+
};
|
|
182
|
+
const cancel = () => {
|
|
183
|
+
if (state === 'done' || state === 'cancelled') return;
|
|
184
|
+
const wasWaiting = state === 'waiting';
|
|
185
|
+
state = 'cancelled';
|
|
186
|
+
if (timer !== null) clearTimer(timer);
|
|
187
|
+
detach();
|
|
188
|
+
// A probe already running settles `done` itself, after its evaluate returns.
|
|
189
|
+
if (wasWaiting) finish('cancelled');
|
|
190
|
+
};
|
|
191
|
+
function onRequest(request) {
|
|
192
|
+
try { if (request.isNavigationRequest() && request.frame() === page.mainFrame()) cancel(); } catch { /* closing */ }
|
|
193
|
+
}
|
|
194
|
+
function onClose() { cancel(); }
|
|
195
|
+
page.on('request', onRequest);
|
|
196
|
+
page.on('close', onClose);
|
|
197
|
+
timer = setTimer(async () => {
|
|
198
|
+
timer = null;
|
|
199
|
+
if (state !== 'waiting') return;
|
|
200
|
+
if (!isCurrent()) { cancel(); return; }
|
|
201
|
+
state = 'probing';
|
|
202
|
+
let failure = null;
|
|
203
|
+
try {
|
|
204
|
+
await withBrowserOperationDeadline(Promise.resolve().then(() => page.evaluate(() => 1)), deadlineMs, 'Slow page liveness probe');
|
|
205
|
+
} catch (error) {
|
|
206
|
+
failure = error;
|
|
207
|
+
}
|
|
208
|
+
if (state === 'cancelled') { finish('cancelled'); return; }
|
|
209
|
+
state = 'done';
|
|
210
|
+
detach();
|
|
211
|
+
if (failure && isCurrent()) {
|
|
212
|
+
try { await onDead?.(failure); } catch { /* the recovery reports its own failures */ }
|
|
213
|
+
finish('dead');
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
finish(failure ? 'cancelled' : 'alive');
|
|
217
|
+
}, delayMs);
|
|
218
|
+
timer?.unref?.();
|
|
219
|
+
return { cancel, done };
|
|
220
|
+
}
|
|
221
|
+
|
|
69
222
|
function runLateCleanup(callback, value, onCleanupError) {
|
|
70
223
|
if (!callback) return;
|
|
71
224
|
Promise.resolve()
|
|
@@ -73,6 +73,7 @@ function browserToolNames(tools) {
|
|
|
73
73
|
screenshot: map.browserScreenshot || `${prefix}browser_screenshot`,
|
|
74
74
|
console: map.browserConsole || `${prefix}browser_console`,
|
|
75
75
|
session: map.browserSession || `${prefix}browser_session`,
|
|
76
|
+
morelogin: map.morelogin || `${prefix}morelogin`,
|
|
76
77
|
};
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -102,6 +103,7 @@ export function browserPolicyText({ tools = '', host = null } = {}) {
|
|
|
102
103
|
const web = WEB_TOOL_WORDS[hostKey(host)] || 'WebSearch / WebFetch (OpenCode webfetch / websearch, Codex web_search)';
|
|
103
104
|
return [
|
|
104
105
|
`- Every page, public or localhost, goes through SynaBun's browser tools (${t.navigate}, ${t.snapshot}, ${t.screenshot}, ${t.console} and the other browser_* tools); they open the browser configured in SynaBun (MoreLogin). Nothing else opens a page.`,
|
|
106
|
+
`- MoreLogin profiles: the browser tools work in your own profile (the default of Settings → Browser, or the one your run was started with). To work in another profile (a second account), call ${t.morelogin} with action "use" (envId or name): only your own browser tools move there, in a tab of your own, while other automations keep working in theirs. You can choose only a profile the owner allowed agents to choose (the tool's "list" and "status" say which; none until the owner switches one on in SynaBun's Settings → Connected tools → MoreLogin). When a profile is refused as not on the owner's list, nothing was opened: tell the user which profile to switch on there and stop; never work in another profile or browser instead. Never try to change the default for that (the default profile is the owner's choice in Settings → Browser: use_default is refused for every agent; "start" takes only a profile on the owner's list too), and never say a second profile cannot be driven: a refused one is not on the owner's list, which is what you say.`,
|
|
105
107
|
'- Never run Playwright, Puppeteer or Selenium code of your own to look at a page (a script, node -e, python -c, a heredoc), and never `npx playwright` except to run automated tests (npx playwright test).',
|
|
106
108
|
`- ${TEST_RUN_RULE}`,
|
|
107
109
|
'- Never launch Chrome, Chromium, Edge, chrome-headless-shell or any other browser binary, and never take headless screenshots (--headless, --screenshot).',
|