synabun 2.2.1 → 2.2.2
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 +27 -0
- package/README.md +1 -1
- package/hooks/claude-code/pre-websearch.mjs +4 -1
- package/hooks/claude-code/prompt-submit.mjs +7 -0
- package/lib/release-scan.mjs +23 -10
- package/lib/virustotal.mjs +143 -39
- package/mcp-server/dist/http.d.ts +19 -0
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +55 -13
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/index.d.ts.map +1 -1
- package/mcp-server/dist/index.js +14 -9
- package/mcp-server/dist/index.js.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.d.ts +118 -0
- package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -0
- package/mcp-server/dist/services/browser-route-flow.js +339 -0
- package/mcp-server/dist/services/browser-route-flow.js.map +1 -0
- package/mcp-server/dist/services/desktop-client.js +2 -2
- package/mcp-server/dist/services/desktop-client.js.map +1 -1
- package/mcp-server/dist/services/identity.d.ts +66 -3
- package/mcp-server/dist/services/identity.d.ts.map +1 -1
- package/mcp-server/dist/services/identity.js +61 -4
- package/mcp-server/dist/services/identity.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +112 -1
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +555 -61
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/tools/bluesky.d.ts +20 -7
- package/mcp-server/dist/tools/bluesky.d.ts.map +1 -1
- package/mcp-server/dist/tools/bluesky.js +147 -45
- package/mcp-server/dist/tools/bluesky.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +11 -2
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +131 -18
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts +2 -0
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.js +3 -0
- package/mcp-server/dist/tools/browser-cheatsheet.js.map +1 -1
- package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
- package/mcp-server/dist/tools/morelogin.js +4 -2
- package/mcp-server/dist/tools/morelogin.js.map +1 -1
- package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
- package/mcp-server/dist/tools/youtube/helpers.d.ts +1 -0
- package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
- package/mcp-server/dist/tools/youtube/helpers.js +1 -1
- package/mcp-server/dist/tools/youtube/helpers.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 +58 -12
- package/mcp-server/src/index.ts +15 -9
- package/mcp-server/src/services/browser-route-flow.ts +402 -0
- package/mcp-server/src/services/desktop-client.ts +2 -2
- package/mcp-server/src/services/identity.ts +80 -4
- package/mcp-server/src/services/neural-interface.ts +572 -70
- package/mcp-server/src/tools/bluesky.ts +164 -51
- package/mcp-server/src/tools/browser-advanced.ts +121 -17
- package/mcp-server/src/tools/browser-cheatsheet.ts +3 -0
- package/mcp-server/src/tools/morelogin.ts +4 -2
- package/mcp-server/src/tools/youtube/helpers.ts +2 -2
- package/neural-interface/i18n/en.json +24 -0
- package/neural-interface/i18n/pt-BR.json +24 -0
- package/neural-interface/lib/assistant-api.js +64 -23
- package/neural-interface/lib/assistant-catalog.js +20 -2
- package/neural-interface/lib/assistant-dispatch.js +27 -1
- package/neural-interface/lib/assistant-persona.js +1 -0
- package/neural-interface/lib/assistant-runtime.js +15 -1
- package/neural-interface/lib/assistant-tools.js +10 -1
- package/neural-interface/lib/browser-automation-routes.js +84 -0
- package/neural-interface/lib/browser-execution.js +16 -3
- package/neural-interface/lib/browser-owner.js +48 -0
- package/neural-interface/lib/browser-session-close.js +84 -0
- package/neural-interface/lib/browser-start-handoff.js +202 -0
- package/neural-interface/lib/browser-tool-policy.js +21 -4
- package/neural-interface/lib/browser-wedge-retire.js +64 -0
- package/neural-interface/lib/claude-agent-bridge.js +36 -1
- package/neural-interface/lib/claude-background-hold.js +239 -0
- package/neural-interface/lib/exec-loop-command.js +8 -0
- package/neural-interface/lib/http-guards.js +11 -6
- package/neural-interface/lib/loop-logger.js +6 -1
- package/neural-interface/lib/mcp-external-servers.js +37 -10
- package/neural-interface/lib/native-loop-providers.js +121 -10
- package/neural-interface/lib/native-loop-runtime.js +112 -11
- package/neural-interface/lib/owner-authority.js +113 -37
- package/neural-interface/lib/page-session.js +81 -23
- package/neural-interface/lib/peer-identity.js +263 -43
- package/neural-interface/lib/provider-panel-api-key-auth.js +7 -2
- package/neural-interface/lib/pty-host/core.js +36 -0
- package/neural-interface/lib/pty-host/protocol.js +2 -0
- package/neural-interface/lib/skills/api-v2.js +13 -2
- package/neural-interface/lib/skills/creation-api.js +3 -1
- package/neural-interface/lib/skills/evals-api.js +5 -1
- package/neural-interface/lib/social-write-pacing.js +151 -0
- package/neural-interface/lib/terminal-token-redaction.js +293 -0
- package/neural-interface/lib/terminal-tokens.js +359 -0
- package/neural-interface/lib/worker-mcp-channel.js +45 -10
- package/neural-interface/package-lock.json +2 -2
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/shared/agy/agy-render.js +41 -1
- package/neural-interface/public/shared/agy/agy-styles.js +5 -0
- package/neural-interface/public/shared/api.js +1 -0
- package/neural-interface/public/shared/assistant/asst-computer.js +47 -2
- package/neural-interface/public/shared/assistant/asst-limit-wait.js +3 -1
- package/neural-interface/public/shared/assistant/asst-panel.js +7 -3
- package/neural-interface/public/shared/assistant/asst-render.js +10 -4
- package/neural-interface/public/shared/assistant/asst-styles.js +2 -1
- package/neural-interface/public/shared/cdx/cdx-render.js +73 -7
- package/neural-interface/public/shared/cdx/cdx-tabs.js +3 -3
- package/neural-interface/public/shared/cp/cp-agents.js +8 -0
- package/neural-interface/public/shared/cp/cp-permissions.js +6 -2
- package/neural-interface/public/shared/cp/cp-rehydrate.js +25 -1
- package/neural-interface/public/shared/cp/cp-tasks-model.js +33 -0
- package/neural-interface/public/shared/judgments-log-origins.js +29 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-automation-ownership.js +23 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +13 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-icons.js +3 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +4 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +6 -86
- package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +5 -2
- package/neural-interface/public/shared/owner-access.js +134 -0
- package/neural-interface/public/shared/owner-authority-view.js +92 -0
- package/neural-interface/public/shared/page-session.js +143 -14
- package/neural-interface/public/shared/poll-backoff.js +59 -0
- package/neural-interface/public/shared/settings/settings-data.js +6 -1
- package/neural-interface/public/shared/settings/settings-kit.js +11 -3
- package/neural-interface/public/shared/settings/settings-shell.js +7 -0
- package/neural-interface/public/shared/shown-children.js +72 -0
- package/neural-interface/public/shared/static-bg-state.js +22 -0
- package/neural-interface/public/shared/styles.css +101 -7
- package/neural-interface/public/shared/synabun-tools.js +173 -0
- package/neural-interface/public/shared/tool-heartbeat.js +43 -0
- package/neural-interface/public/shared/tool-list-picker.js +3 -1
- package/neural-interface/public/shared/tool-list-view.js +23 -4
- package/neural-interface/public/shared/ui-automation-studio.js +8 -1
- package/neural-interface/public/shared/ui-claude-panel.js +159 -29
- package/neural-interface/public/shared/ui-judgments.js +37 -10
- package/neural-interface/public/shared/ui-navbar.js +6 -0
- package/neural-interface/public/shared/ui-owner-access.js +191 -0
- package/neural-interface/public/shared/ui-owner-authority.js +162 -0
- package/neural-interface/public/shared/ui-sessions.js +45 -16
- package/neural-interface/public/shared/ui-settings.js +86 -15
- package/neural-interface/public/shared/ui-terminal.js +15 -7
- package/neural-interface/public/shared/ui-tutorial-preview.js +4 -0
- package/neural-interface/public/shared/ui-update.js +3 -0
- package/neural-interface/public/shared/ui-whiteboard.js +4 -0
- package/neural-interface/public/shared/window-keys.js +38 -0
- package/neural-interface/server.js +1160 -205
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
import {
|
|
36
36
|
revertToMessage, restoreReverted, retryFromMessage, forkSession,
|
|
37
37
|
} from './ocp-v2-session-actions.js';
|
|
38
|
+
import { synabunToolKey, synabunToolLabel, synabunToolSummary } from '../synabun-tools.js';
|
|
38
39
|
|
|
39
40
|
const _expandedReasoningIds = new Set();
|
|
40
41
|
const _activeRenderers = new Set();
|
|
@@ -748,62 +749,14 @@ function extractQuestionsFromToolInput(input) {
|
|
|
748
749
|
// ── SynaBun MCP tool branding ────────────────────────────────────────────────
|
|
749
750
|
const SYNABUN_LOGO_HTML = '<img src="logoHD.png" alt="" style="width:14px;height:14px;object-fit:contain;">';
|
|
750
751
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
forget: 'Forget', restore: 'Restore', memories: 'Memories', sync: 'Sync',
|
|
754
|
-
category: 'Category', loop: 'Loop', git: 'Git', tictactoe: 'TicTacToe',
|
|
755
|
-
image_staged: 'Images', profile: 'Profile',
|
|
756
|
-
browser_navigate: 'Browser · Navigate', browser_screenshot: 'Browser · Screenshot', browser_console: 'Browser · Console',
|
|
757
|
-
browser_snapshot: 'Browser · Snapshot', browser_content: 'Browser · Content',
|
|
758
|
-
browser_click: 'Browser · Click', browser_type: 'Browser · Type',
|
|
759
|
-
browser_fill: 'Browser · Fill', browser_hover: 'Browser · Hover',
|
|
760
|
-
browser_select: 'Browser · Select', browser_press: 'Browser · Press',
|
|
761
|
-
browser_scroll: 'Browser · Scroll', browser_wait: 'Browser · Wait',
|
|
762
|
-
browser_go_back: 'Browser · Back', browser_go_forward: 'Browser · Forward',
|
|
763
|
-
browser_reload: 'Browser · Reload', browser_evaluate: 'Browser · Evaluate',
|
|
764
|
-
browser_upload: 'Browser · Upload', browser_session: 'Browser · Session',
|
|
765
|
-
browser_cheatsheet: 'Browser · Cheatsheet',
|
|
766
|
-
browser_extract_tweets: 'Extract · Tweets', browser_extract_fb_posts: 'Extract · Facebook',
|
|
767
|
-
browser_fb_composer_state: 'Facebook · Composer', browser_extract_fb_groups: 'Extract · FB Groups',
|
|
768
|
-
browser_extract_ig_feed: 'Extract · Instagram', browser_extract_ig_post: 'Extract · IG Post',
|
|
769
|
-
browser_extract_ig_reels: 'Extract · IG Reels', browser_extract_ig_profile: 'Extract · IG Profile',
|
|
770
|
-
browser_extract_ig_search: 'Extract · IG Search',
|
|
771
|
-
browser_extract_tiktok_videos: 'Extract · TikTok', browser_extract_tiktok_search: 'Extract · TikTok Search',
|
|
772
|
-
browser_extract_tiktok_studio: 'Extract · TikTok Studio', browser_extract_tiktok_profile: 'Extract · TikTok Profile',
|
|
773
|
-
browser_extract_li_feed: 'Extract · LinkedIn', browser_extract_li_profile: 'Extract · LI Profile',
|
|
774
|
-
browser_extract_li_post: 'Extract · LI Post', browser_extract_li_notifications: 'Extract · LI Notifications',
|
|
775
|
-
browser_extract_li_messages: 'Extract · LI Messages', browser_extract_li_search_people: 'Extract · LI People',
|
|
776
|
-
browser_extract_li_network: 'Extract · LI Network', browser_extract_li_jobs: 'Extract · LI Jobs',
|
|
777
|
-
browser_extract_wa_chats: 'Extract · WhatsApp', browser_extract_wa_messages: 'Extract · WA Messages',
|
|
778
|
-
bluesky_session: 'BlueSky · Session', bluesky_timeline: 'BlueSky · Timeline',
|
|
779
|
-
bluesky_author_feed: 'BlueSky · Author Feed', bluesky_thread: 'BlueSky · Thread',
|
|
780
|
-
bluesky_profile: 'BlueSky · Profile', bluesky_search_posts: 'BlueSky · Search Posts',
|
|
781
|
-
bluesky_search_actors: 'BlueSky · Search Users', bluesky_notifications: 'BlueSky · Notifications',
|
|
782
|
-
bluesky_graph: 'BlueSky · Graph', bluesky_likes: 'BlueSky · Likes', bluesky_feed: 'BlueSky · Feed',
|
|
783
|
-
bluesky_post: 'BlueSky · Post', bluesky_action: 'BlueSky · Action',
|
|
784
|
-
bluesky_resolve: 'BlueSky · Resolve', bluesky_dm: 'BlueSky · DM',
|
|
785
|
-
whiteboard_read: 'Whiteboard · Read', whiteboard_add: 'Whiteboard · Add',
|
|
786
|
-
whiteboard_update: 'Whiteboard · Update', whiteboard_remove: 'Whiteboard · Remove',
|
|
787
|
-
whiteboard_screenshot: 'Whiteboard · Screenshot',
|
|
788
|
-
card_list: 'Cards · List', card_open: 'Cards · Open', card_close: 'Cards · Close',
|
|
789
|
-
card_update: 'Cards · Update', card_screenshot: 'Cards · Screenshot',
|
|
790
|
-
discord_guild: 'Discord · Guild', discord_channel: 'Discord · Channel',
|
|
791
|
-
discord_role: 'Discord · Role', discord_message: 'Discord · Message',
|
|
792
|
-
discord_member: 'Discord · Member', discord_onboarding: 'Discord · Onboarding',
|
|
793
|
-
discord_webhook: 'Discord · Webhook', discord_thread: 'Discord · Thread',
|
|
794
|
-
};
|
|
795
|
-
|
|
752
|
+
// Which call is SynaBun's, its name and its one-line summary: synabun-tools.js, the same
|
|
753
|
+
// for every panel (`SynaBun_x` here, `mcp__SynaBun__x` from the others, any letter case).
|
|
796
754
|
function isSynaBunTool(name) {
|
|
797
|
-
return typeof name === 'string' && (name
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
function synabunToolKey(name) {
|
|
801
|
-
return (name || '').replace(/^mcp__SynaBun__/, '').replace(/^SynaBun_/, '');
|
|
755
|
+
return typeof name === 'string' && synabunToolKey(name) != null;
|
|
802
756
|
}
|
|
803
757
|
|
|
804
758
|
function synabunDisplayName(name) {
|
|
805
|
-
|
|
806
|
-
return SYNABUN_LABELS[key] || (key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, ' '));
|
|
759
|
+
return synabunToolLabel(synabunToolKey(name) || '');
|
|
807
760
|
}
|
|
808
761
|
|
|
809
762
|
function _truncate(s, n = 90) {
|
|
@@ -813,40 +766,7 @@ function _truncate(s, n = 90) {
|
|
|
813
766
|
|
|
814
767
|
function synabunSummaryText(name, input) {
|
|
815
768
|
if (!input || typeof input !== 'object') return '';
|
|
816
|
-
|
|
817
|
-
const t = (s) => _truncate(s, 90);
|
|
818
|
-
switch (key) {
|
|
819
|
-
case 'recall': return t(input.query);
|
|
820
|
-
case 'remember': return t(input.content);
|
|
821
|
-
case 'reflect': return t(input.content || input.memory_id);
|
|
822
|
-
case 'forget':
|
|
823
|
-
case 'restore': return t(input.memory_id);
|
|
824
|
-
case 'memories': return t(input.action);
|
|
825
|
-
case 'category': return t(`${input.action || ''}${input.name ? ' · ' + input.name : ''}`);
|
|
826
|
-
case 'sync': return '';
|
|
827
|
-
case 'git': return t(input.action);
|
|
828
|
-
case 'loop': return t(`${input.action || ''}${input.prompt ? ' · ' + input.prompt : ''}`);
|
|
829
|
-
case 'browser_navigate': return t(input.url);
|
|
830
|
-
case 'browser_click':
|
|
831
|
-
case 'browser_hover': return t(input.selector || input.label);
|
|
832
|
-
case 'browser_type':
|
|
833
|
-
case 'browser_fill': return t(input.text || input.value || input.selector);
|
|
834
|
-
case 'browser_select': return t(input.value || input.selector);
|
|
835
|
-
case 'browser_press': return t(input.key);
|
|
836
|
-
case 'browser_scroll': return t(input.direction || input.selector);
|
|
837
|
-
case 'browser_evaluate': return t(input.script);
|
|
838
|
-
case 'browser_session': return t(input.action);
|
|
839
|
-
case 'image_staged': return t(input.action);
|
|
840
|
-
case 'discord_message': return t(input.content || input.action);
|
|
841
|
-
case 'discord_channel':
|
|
842
|
-
case 'discord_member':
|
|
843
|
-
case 'discord_role': return t(input.action || input.name);
|
|
844
|
-
case 'whiteboard_add':
|
|
845
|
-
case 'whiteboard_update': return t(input.content || input.title);
|
|
846
|
-
case 'card_open':
|
|
847
|
-
case 'card_update': return t(input.title || input.id);
|
|
848
|
-
default: return '';
|
|
849
|
-
}
|
|
769
|
+
return synabunToolSummary(synabunToolKey(name) || '', input);
|
|
850
770
|
}
|
|
851
771
|
|
|
852
772
|
// Persist expand/collapse state across re-renders (render() rebuilds DOM).
|
|
@@ -12,8 +12,8 @@ import { createOutcomeNotifications } from './ocp-v2-outcome-notifications.js';
|
|
|
12
12
|
import { getDefaultStore } from './ocp-v2-state.js';
|
|
13
13
|
import { notify as uiNotify, NOTIF_TYPE } from '../ui-notifications.js';
|
|
14
14
|
import { nativeLoopWindowId } from '../ui-native-window-id.js';
|
|
15
|
-
import { registerAutomationSession } from './ocp-v2-automation-ownership.js';
|
|
16
|
-
import { applyEvent, resolveEventTargets } from './ocp-v2-events.js';
|
|
15
|
+
import { registerAutomationSession, automationRunAnswersAsks } from './ocp-v2-automation-ownership.js';
|
|
16
|
+
import { applyEvent, resolveEventTargets, askNotifies } from './ocp-v2-events.js';
|
|
17
17
|
import { createCapabilities, gatedRequest } from './ocp-v2-caps.js';
|
|
18
18
|
import { shouldAutoAccept, autoAcceptRequest, autoAcceptInherited } from './ocp-v2-approvals.js';
|
|
19
19
|
import { verifyDescendant, descendantState, knownDescendant, createReconnectNotifier, requestOwner } from './ocp-v2-rehydrate.js';
|
|
@@ -535,6 +535,9 @@ const EVENT_HOOKS = {
|
|
|
535
535
|
onAsk(store, id, kind, ev) {
|
|
536
536
|
if (!id || _notifiedPermissionIds.has(id)) return;
|
|
537
537
|
_notifiedPermissionIds.add(id);
|
|
538
|
+
// A live automation answers its own session's permission asks by itself (a protected worker's
|
|
539
|
+
// OpenCode asks for every shell call): the card shows while the reply is out, nothing is announced.
|
|
540
|
+
if (!askNotifies(kind, ev, { runAnswers: automationRunAnswersAsks })) return;
|
|
538
541
|
// Auto-accept is the user's standing answer for this session: reply and
|
|
539
542
|
// stay quiet. A refused reply leaves the card up for them.
|
|
540
543
|
if (kind === 'permission' && shouldAutoAccept(autoAcceptEnabledFor(store), ev)) {
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════
|
|
2
|
+
// SynaBun Neural Interface — a page without the owner's access: what it is and what it says
|
|
3
|
+
// ═══════════════════════════════════════════
|
|
4
|
+
//
|
|
5
|
+
// SynaBun's automation browser (the one its browser tools drive) may open SynaBun's own page, by design: it reads it,
|
|
6
|
+
// clicks it and takes screenshots. It never gets the owner's page secret (GET /api/page-session answers 403
|
|
7
|
+
// WORKER_BROWSER), so every owner route refuses it (403 WORKER_BROWSER, PAGE_SECRET_REQUIRED, OWNER_ROUTE). That
|
|
8
|
+
// refusal is right and stays. What this module is for is the page's side of it: a refused read is not an empty list,
|
|
9
|
+
// a default value or an "Off" state, and the page says so in one way everywhere (ui-owner-access.js draws it: one
|
|
10
|
+
// banner, and a "No access" state in the panels whose data was refused, their Save and actions disabled).
|
|
11
|
+
//
|
|
12
|
+
// page-session.js (the page's fetch, wrapped once) tells every final refusal (`synabun:owner-access-refused`); the
|
|
13
|
+
// panels that read through their own paths ask `refusalOfError` / `refusalOfResponse` about what they got.
|
|
14
|
+
//
|
|
15
|
+
// No DOM: `node --test` loads this file (tests/owner-access.test.mjs). OWNER_ACCESS_COPY is the English of every
|
|
16
|
+
// sentence; `ownerAccess.*` in i18n/en.json must equal it and i18n/pt-BR.json must have the same keys.
|
|
17
|
+
|
|
18
|
+
import { OWNER_ACCESS_CODES } from './page-session.js';
|
|
19
|
+
|
|
20
|
+
export { OWNER_ACCESS_CODES };
|
|
21
|
+
|
|
22
|
+
export const OWNER_ACCESS_COPY = Object.freeze({
|
|
23
|
+
label: 'No owner access',
|
|
24
|
+
title: 'This page doesn\'t have owner access.',
|
|
25
|
+
bodyAutomation: 'It was opened from SynaBun\'s automation browser, which can read SynaBun\'s pages but not your settings, files or actions. Open SynaBun in your own browser or the desktop app.',
|
|
26
|
+
bodyOwner: 'SynaBun refused this page your settings and actions. Reload the page; if it persists, open SynaBun in your own browser or the desktop app.',
|
|
27
|
+
reload: 'Reload',
|
|
28
|
+
dismiss: 'Dismiss',
|
|
29
|
+
panelTitle: 'No access',
|
|
30
|
+
panelBody: 'This page doesn\'t have owner access, so this can\'t be shown or changed here. Open SynaBun in your own browser or the desktop app.',
|
|
31
|
+
navStatus: 'No access',
|
|
32
|
+
actionDisabled: 'Needs owner access',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/** Why a page has no owner access: SynaBun's automation browser, or a refusal the page could not cure. */
|
|
36
|
+
export const OWNER_ACCESS_REASONS = Object.freeze(['automation', 'owner']);
|
|
37
|
+
|
|
38
|
+
/** The English of a key with its {values} filled; an unknown key is itself. */
|
|
39
|
+
export function ownerAccessText(key, params = {}) {
|
|
40
|
+
const template = OWNER_ACCESS_COPY[key];
|
|
41
|
+
if (typeof template !== 'string') return key;
|
|
42
|
+
return template.replace(/\{(\w+)\}/g, (whole, name) => (params[name] != null ? String(params[name]) : whole));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const reasonOf = (code) => (code === 'WORKER_BROWSER' ? 'automation' : 'owner');
|
|
46
|
+
|
|
47
|
+
/** A refusal from an answer's status and body: { code, reason } when it is a final owner refusal, else null. */
|
|
48
|
+
export function readOwnerAccessRefusal(status, body) {
|
|
49
|
+
if (Number(status) !== 403 || !body || typeof body !== 'object') return null;
|
|
50
|
+
const code = typeof body.code === 'string' ? body.code : null;
|
|
51
|
+
if (!code || !OWNER_ACCESS_CODES.includes(code)) return null;
|
|
52
|
+
return { code, reason: reasonOf(code) };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The refusal an error (or a read result) stands for: api.js / asst-api.js errors carry `status` and `code` (and
|
|
57
|
+
* `body`), a pane's own reading `{ ok: false, status, code }` too. null for anything else (a network error, a 500…).
|
|
58
|
+
*/
|
|
59
|
+
export function refusalOfError(err) {
|
|
60
|
+
if (!err || typeof err !== 'object') return null;
|
|
61
|
+
const status = Number(err.status ?? err.statusCode);
|
|
62
|
+
const code = typeof err.code === 'string' ? err.code : (typeof err.body?.code === 'string' ? err.body.code : null);
|
|
63
|
+
return readOwnerAccessRefusal(status, { code });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The refusal a fetch Response stands for (its body read from a clone: the response is untouched). */
|
|
67
|
+
export async function refusalOfResponse(res) {
|
|
68
|
+
if (!res || Number(res.status) !== 403 || typeof res.clone !== 'function') return null;
|
|
69
|
+
try { return readOwnerAccessRefusal(res.status, await res.clone().json()); } catch { return null; }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* What the page knows about its own access: null (nothing refused, or restored since) or { code, reason, url, at }.
|
|
74
|
+
* Once SynaBun's automation browser is named (WORKER_BROWSER) the reason stays `automation` whatever comes after: that
|
|
75
|
+
* is the one that explains the rest. `subscribe(fn)` is told every change (fn(current)); returns the unsubscribe.
|
|
76
|
+
*/
|
|
77
|
+
export function createOwnerAccessState({ now = () => Date.now() } = {}) {
|
|
78
|
+
let current = null;
|
|
79
|
+
const listeners = new Set();
|
|
80
|
+
const tell = () => { for (const fn of [...listeners]) { try { fn(current); } catch { /* a listener's own error */ } } };
|
|
81
|
+
return {
|
|
82
|
+
current: () => current,
|
|
83
|
+
/** A refusal was seen (an event's detail or a refusal): true when what the page knows changed. */
|
|
84
|
+
note(detail) {
|
|
85
|
+
const code = typeof detail?.code === 'string' ? detail.code : null;
|
|
86
|
+
if (!code || !OWNER_ACCESS_CODES.includes(code)) return false;
|
|
87
|
+
const reason = current?.reason === 'automation' ? 'automation' : reasonOf(code);
|
|
88
|
+
if (current && current.reason === reason) return false;
|
|
89
|
+
current = { code, reason, url: typeof detail.url === 'string' ? detail.url : '', at: now() };
|
|
90
|
+
tell();
|
|
91
|
+
return true;
|
|
92
|
+
},
|
|
93
|
+
/** The page got this boot's page secret: it is the owner's again. */
|
|
94
|
+
clear() {
|
|
95
|
+
if (!current) return false;
|
|
96
|
+
current = null;
|
|
97
|
+
tell();
|
|
98
|
+
return true;
|
|
99
|
+
},
|
|
100
|
+
subscribe(fn) {
|
|
101
|
+
if (typeof fn !== 'function') return () => {};
|
|
102
|
+
listeners.add(fn);
|
|
103
|
+
return () => listeners.delete(fn);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A translator over OWNER_ACCESS_COPY: a key the page's translator does not know is English. */
|
|
109
|
+
export function ownerAccessSayer(t) {
|
|
110
|
+
return (key, params) => {
|
|
111
|
+
if (typeof t !== 'function') return ownerAccessText(key, params);
|
|
112
|
+
try {
|
|
113
|
+
const text = t(key, params);
|
|
114
|
+
return typeof text === 'string' && text && text !== key && !text.endsWith(`.${key}`) ? text : ownerAccessText(key, params);
|
|
115
|
+
} catch { return ownerAccessText(key, params); }
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* What the banner shows for what the page knows, or null (nothing refused).
|
|
121
|
+
* → { key, label, title, body, actions: [{ id: 'reload'|'dismiss', label }] }. `key` is what a dismissal is
|
|
122
|
+
* remembered by: the same reason stays dismissed.
|
|
123
|
+
*/
|
|
124
|
+
export function ownerAccessNoticeView(refusal, { t = null } = {}) {
|
|
125
|
+
if (!refusal || !OWNER_ACCESS_REASONS.includes(refusal.reason)) return null;
|
|
126
|
+
const say = ownerAccessSayer(t);
|
|
127
|
+
return {
|
|
128
|
+
key: `owner-access:${refusal.reason}`,
|
|
129
|
+
label: say('label'),
|
|
130
|
+
title: say('title'),
|
|
131
|
+
body: say(refusal.reason === 'automation' ? 'bodyAutomation' : 'bodyOwner'),
|
|
132
|
+
actions: [{ id: 'reload', label: say('reload') }, { id: 'dismiss', label: say('dismiss') }],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════
|
|
2
|
+
// SynaBun Neural Interface — the banner of a degraded owner authority: what it says
|
|
3
|
+
// ═══════════════════════════════════════════
|
|
4
|
+
//
|
|
5
|
+
// On macOS the server tells the owner's own page from every other local program by the process at the other end of
|
|
6
|
+
// the connection (lib/peer-identity.js). When it cannot look that process up (netstat or ps missing, denied because
|
|
7
|
+
// the server was started inside a sandbox, slow past its cap), it proves nothing about who is asking and pauses the
|
|
8
|
+
// owner's actions: no page secret, and the owner's writes, reads and sockets answer 503 PEER_UNAVAILABLE. GET
|
|
9
|
+
// /api/health says so (`ownerAuthority: { state: 'degraded', reason, retryable, since }`), and so does every 503
|
|
10
|
+
// PEER_UNAVAILABLE a request of the page gets (public/shared/page-session.js turns it into the
|
|
11
|
+
// `synabun:peer-unavailable` event). This module decides the words of the banner; ui-owner-authority.js asks the
|
|
12
|
+
// server, listens and draws them. `unsupported` (Windows, Linux) and `off` (SYNABUN_PEER_IDENTITY=off) show nothing:
|
|
13
|
+
// there the rules from before apply and nothing is paused.
|
|
14
|
+
//
|
|
15
|
+
// No imports and no DOM: `node --test` loads this file as it is (tests/owner-authority-notice.test.mjs).
|
|
16
|
+
// OWNER_AUTHORITY_NOTICE_COPY is the English of every sentence; `ownerAuthorityNotice.*` in i18n/en.json must equal
|
|
17
|
+
// it and i18n/pt-BR.json must have the same keys.
|
|
18
|
+
|
|
19
|
+
export const OWNER_AUTHORITY_NOTICE_COPY = Object.freeze({
|
|
20
|
+
label: 'Owner actions paused',
|
|
21
|
+
title: 'SynaBun can\'t verify which program is calling it, so the owner\'s actions are paused.',
|
|
22
|
+
body: 'Settings, files, terminals, the agent panels and dispatching are refused until the check works again. Pages and memories still load.',
|
|
23
|
+
reason: 'Reason: {reason}',
|
|
24
|
+
hint: 'If this persists, start SynaBun outside any sandbox, or add SYNABUN_PEER_IDENTITY=off to ~/.synabun/.env and restart SynaBun (this turns the check off).',
|
|
25
|
+
reload: 'Reload to retry',
|
|
26
|
+
dismiss: 'Dismiss',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** The states GET /api/health can name (lib/peer-identity.js authorityState). */
|
|
30
|
+
export const OWNER_AUTHORITY_STATES = Object.freeze(['ok', 'degraded', 'unsupported', 'off']);
|
|
31
|
+
/** The longest reason the banner shows (the server's reasons are short; a longer one is cut). */
|
|
32
|
+
export const REASON_MAX = 200;
|
|
33
|
+
|
|
34
|
+
/** A sentence of OWNER_AUTHORITY_NOTICE_COPY in English with its {values} filled; an unknown key is itself. */
|
|
35
|
+
export function ownerAuthorityText(key, params = {}) {
|
|
36
|
+
const template = OWNER_AUTHORITY_NOTICE_COPY[key];
|
|
37
|
+
if (typeof template !== 'string') return key;
|
|
38
|
+
return template.replace(/\{(\w+)\}/g, (whole, name) => (params[name] != null ? String(params[name]) : whole));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const cleanReason = (value) => {
|
|
42
|
+
const text = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
|
|
43
|
+
return text.length > REASON_MAX ? `${text.slice(0, REASON_MAX - 1)}…` : text;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The owner authority a GET /api/health answer reports, or null (an older server says nothing, a body that is not
|
|
48
|
+
* one): { state, reason, retryable, since }.
|
|
49
|
+
*/
|
|
50
|
+
export function readOwnerAuthority(health) {
|
|
51
|
+
const raw = health && typeof health === 'object' ? health.ownerAuthority : null;
|
|
52
|
+
if (!raw || typeof raw !== 'object' || !OWNER_AUTHORITY_STATES.includes(raw.state)) return null;
|
|
53
|
+
return {
|
|
54
|
+
state: raw.state,
|
|
55
|
+
reason: cleanReason(raw.reason) || null,
|
|
56
|
+
retryable: raw.retryable === true,
|
|
57
|
+
since: typeof raw.since === 'string' ? raw.since : null,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The state a 503 PEER_UNAVAILABLE answer stands for (the event's detail, or the answer's body). */
|
|
62
|
+
export function unavailableAuthority(detail) {
|
|
63
|
+
return { state: 'degraded', reason: cleanReason(detail?.reason) || null, retryable: detail?.retryable === true, since: null };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What the banner shows for a state, or null when it shows nothing (ok, unsupported, off, nothing known).
|
|
68
|
+
* `t(key, params)` is the page's translator for `ownerAuthorityNotice.*` (English by default).
|
|
69
|
+
* → { key, label, title, body, reason, hint, actions: [{ id: 'reload'|'dismiss', label }] }. `key` is what a
|
|
70
|
+
* dismissal is remembered by: the same outage stays dismissed, a new reason shows again.
|
|
71
|
+
*/
|
|
72
|
+
export function ownerAuthorityNoticeView(authority, { t = ownerAuthorityText } = {}) {
|
|
73
|
+
if (!authority || authority.state !== 'degraded') return null;
|
|
74
|
+
// (a translator that does not know a key answers the key itself, short or as its whole path: English then)
|
|
75
|
+
const say = (key, params) => {
|
|
76
|
+
try {
|
|
77
|
+
const text = t(key, params);
|
|
78
|
+
return typeof text === 'string' && text && text !== key && !text.endsWith(`.${key}`) ? text : ownerAuthorityText(key, params);
|
|
79
|
+
} catch { return ownerAuthorityText(key, params); }
|
|
80
|
+
};
|
|
81
|
+
const reason = cleanReason(authority.reason);
|
|
82
|
+
return {
|
|
83
|
+
key: `degraded:${reason || 'unknown'}`,
|
|
84
|
+
label: say('label'),
|
|
85
|
+
title: say('title'),
|
|
86
|
+
body: say('body'),
|
|
87
|
+
reason: reason ? say('reason', { reason }) : '',
|
|
88
|
+
// A failure that may pass by itself (a timeout) is what a reload cures; one that does not needs the way out.
|
|
89
|
+
hint: authority.retryable ? '' : say('hint'),
|
|
90
|
+
actions: [{ id: 'reload', label: say('reload') }, { id: 'dismiss', label: say('dismiss') }],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -10,17 +10,51 @@
|
|
|
10
10
|
// page's fetch once: when a same-origin write is refused with one of those two codes, it asks GET /api/page-session
|
|
11
11
|
// (which sets this boot's cookie), then sends the very same request once more and hands back that answer. No
|
|
12
12
|
// reload, no toast (the toast fires on the error jsonFetch throws, and it throws only on the final answer), no lost
|
|
13
|
-
// action.
|
|
14
|
-
//
|
|
13
|
+
// action. A read is recovered the same way when it is refused 403 PAGE_SECRET_REQUIRED: since 2026-11-04 the browser
|
|
14
|
+
// routes (/api/browser/*) ask every request of the page for this boot's secret, reads included, so the browser panel
|
|
15
|
+
// and the browser settings keep working across a restart. Anything else (a read refused UI_ONLY, another 403, a refusal
|
|
16
|
+
// the refresh did not cure, a body that cannot be sent twice) is handed back as it came, so nothing changes for a
|
|
17
|
+
// request this is not about. refreshPageSession() asks for this boot's cookie by itself (one request at a time): the
|
|
18
|
+
// browser panel calls it before it opens its socket again, because a WebSocket refused for a stale cookie never says why.
|
|
15
19
|
//
|
|
16
20
|
// No imports and nothing done at load under Node: tests load it with node --test and a fake fetch. In a browser it
|
|
17
21
|
// installs itself on window.fetch when the page loads it (index.html, index2d.html and sidepanel-session.html load
|
|
18
22
|
// it as their first module script, before any module that writes), and once only.
|
|
23
|
+
//
|
|
24
|
+
// 503 PEER_UNAVAILABLE is something else: the server could not tell which program sent the request (macOS, its
|
|
25
|
+
// lookup of the calling process failed: lib/peer-identity.js), so it hands out no page secret and refuses the owner's
|
|
26
|
+
// actions until a lookup works again. Nothing here cures that, so nothing is retried: the answer goes back as it came,
|
|
27
|
+
// and the page is told once per answer (the `synabun:peer-unavailable` event on the window, which
|
|
28
|
+
// ui-owner-authority.js turns into its banner). A refresh that met it (GET /api/page-session answered 503) is not asked
|
|
29
|
+
// again for a few seconds: a burst of refused writes costs the server one lookup, not one each.
|
|
30
|
+
//
|
|
31
|
+
// A refusal nothing here cures (403 WORKER_BROWSER, OWNER_ROUTE, or PAGE_SECRET_REQUIRED the refresh did not cure) is
|
|
32
|
+
// the page not having the owner's access at all: SynaBun's automation browser opened it, by design. The answer still
|
|
33
|
+
// goes back as it came; the page is told (`synabun:owner-access-refused` on the window, which ui-owner-access.js turns
|
|
34
|
+
// into one banner and each panel into "No access" instead of an empty list or default values). A refresh that gets
|
|
35
|
+
// this boot's secret is told too (`synabun:owner-access-restored`).
|
|
19
36
|
|
|
20
37
|
/** The route that hands an open tab this boot's cookie (lib/page-session.js PAGE_SECRET_REFRESH_PATH). */
|
|
21
38
|
export const PAGE_SECRET_REFRESH_PATH = '/api/page-session';
|
|
22
39
|
/** The refusals that mean "the page secret is missing or stale", and nothing else. */
|
|
23
40
|
export const PAGE_SECRET_CODES = Object.freeze(['UI_ONLY', 'PAGE_SECRET_REQUIRED']);
|
|
41
|
+
/** The answer that means "the server could not identify the caller" (lib/owner-authority.js PEER_UNAVAILABLE). */
|
|
42
|
+
export const PEER_UNAVAILABLE_CODE = 'PEER_UNAVAILABLE';
|
|
43
|
+
/** The window event a 503 PEER_UNAVAILABLE becomes (detail: { reason, retryable, error, url, method }). */
|
|
44
|
+
export const PEER_UNAVAILABLE_EVENT = 'synabun:peer-unavailable';
|
|
45
|
+
/** How long a refresh that met 503 PEER_UNAVAILABLE is not asked again. */
|
|
46
|
+
export const REFRESH_COOLDOWN_MS = 5000;
|
|
47
|
+
/**
|
|
48
|
+
* The final refusals that mean "this page does not have the owner's access" (after any recovery here was tried):
|
|
49
|
+
* WORKER_BROWSER (SynaBun's automation browser: never the owner), PAGE_SECRET_REQUIRED (no secret, and the refresh
|
|
50
|
+
* did not cure it), OWNER_ROUTE (a route the owner's page only reaches). Told once per answer to `onRefused`, which
|
|
51
|
+
* the window turns into OWNER_ACCESS_REFUSED_EVENT (ui-owner-access.js: the banner and the panels' "No access").
|
|
52
|
+
*/
|
|
53
|
+
export const OWNER_ACCESS_CODES = Object.freeze(['WORKER_BROWSER', 'PAGE_SECRET_REQUIRED', 'OWNER_ROUTE']);
|
|
54
|
+
/** The window event a final owner refusal becomes (detail: { code, url, method, status }). */
|
|
55
|
+
export const OWNER_ACCESS_REFUSED_EVENT = 'synabun:owner-access-refused';
|
|
56
|
+
/** The window event a refresh that got this boot's page secret becomes (detail: {}): the page is the owner's again. */
|
|
57
|
+
export const OWNER_ACCESS_RESTORED_EVENT = 'synabun:owner-access-restored';
|
|
24
58
|
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
25
59
|
|
|
26
60
|
/** Whether an answer is the server refusing a write for want of this boot's page secret. */
|
|
@@ -28,6 +62,16 @@ export function isPageSecretRefusal(status, body) {
|
|
|
28
62
|
return Number(status) === 403 && !!body && typeof body === 'object' && PAGE_SECRET_CODES.includes(body.code);
|
|
29
63
|
}
|
|
30
64
|
|
|
65
|
+
/** Whether an answer is a final refusal of the owner's access (403 with one of OWNER_ACCESS_CODES). */
|
|
66
|
+
export function isOwnerAccessRefusal(status, body) {
|
|
67
|
+
return Number(status) === 403 && !!body && typeof body === 'object' && OWNER_ACCESS_CODES.includes(body.code);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether an answer is the server saying it could not identify the caller (never retried here). */
|
|
71
|
+
export function isPeerUnavailable(status, body) {
|
|
72
|
+
return Number(status) === 503 && !!body && typeof body === 'object' && body.code === PEER_UNAVAILABLE_CODE;
|
|
73
|
+
}
|
|
74
|
+
|
|
31
75
|
/**
|
|
32
76
|
* What a request is, for the retry: its method, whether it is same-origin, and the arguments that send it again,
|
|
33
77
|
* or null when it is not a write, goes elsewhere, or cannot be sent twice (a streamed body).
|
|
@@ -45,52 +89,137 @@ export function replayPlan(input, init, origin = null) {
|
|
|
45
89
|
return { method, url, args: init === undefined ? [again] : [again, init] };
|
|
46
90
|
}
|
|
47
91
|
|
|
92
|
+
/** The same for a read (GET / HEAD of this origin): sent again only when it was refused for the page secret. */
|
|
93
|
+
export function readReplayPlan(input, init, origin = null) {
|
|
94
|
+
const isRequest = typeof Request !== 'undefined' && input instanceof Request;
|
|
95
|
+
const method = String(init?.method || (isRequest ? input.method : '') || 'GET').toUpperCase();
|
|
96
|
+
if (method !== 'GET' && method !== 'HEAD') return null;
|
|
97
|
+
const url = isRequest ? input.url : (input instanceof URL ? input.href : String(input ?? ''));
|
|
98
|
+
if (!sameOrigin(url, origin)) return null;
|
|
99
|
+
const again = isRequest ? input.clone() : input;
|
|
100
|
+
return { method, url, read: true, args: init === undefined ? [again] : [again, init] };
|
|
101
|
+
}
|
|
102
|
+
|
|
48
103
|
function sameOrigin(url, origin) {
|
|
49
104
|
if (!/^[a-z][a-z0-9+.-]*:/i.test(url) && !url.startsWith('//')) return true; // a path of this page
|
|
50
105
|
if (!origin) return false;
|
|
51
106
|
try { return new URL(url).origin === new URL(origin).origin; } catch { return false; }
|
|
52
107
|
}
|
|
53
108
|
|
|
109
|
+
/** A request's URL and method as given to fetch (for the event's detail). */
|
|
110
|
+
function describeRequest(input, init) {
|
|
111
|
+
const isRequest = typeof Request !== 'undefined' && input instanceof Request;
|
|
112
|
+
const method = String(init?.method || (isRequest ? input.method : '') || 'GET').toUpperCase();
|
|
113
|
+
const url = isRequest ? input.url : (input instanceof URL ? input.href : String(input ?? ''));
|
|
114
|
+
return { method, url };
|
|
115
|
+
}
|
|
116
|
+
|
|
54
117
|
/**
|
|
55
118
|
* A fetch over `fetchImpl` that recovers the page secret once: the refresh is one request at a time (writes refused
|
|
56
119
|
* together share it), the refused request is sent again only when the refresh answered, and the second answer is
|
|
57
|
-
* final whatever it says.
|
|
120
|
+
* final whatever it says. A 503 PEER_UNAVAILABLE (a write's, a read's, or the refresh's) is never retried: it is handed
|
|
121
|
+
* back as it came and told to `onUnavailable` ({ reason, retryable, error, url, method }); after the refresh met one,
|
|
122
|
+
* refused writes are handed back without a refresh for `cooldownMs`.
|
|
58
123
|
*/
|
|
59
|
-
export function createPageSessionFetch(fetchImpl, { refreshPath = PAGE_SECRET_REFRESH_PATH, origin = null, onRecover = null } = {}) {
|
|
124
|
+
export function createPageSessionFetch(fetchImpl, { refreshPath = PAGE_SECRET_REFRESH_PATH, origin = null, onRecover = null, onUnavailable = null, onRefused = null, onRestored = null, cooldownMs = REFRESH_COOLDOWN_MS, now = () => Date.now() } = {}) {
|
|
60
125
|
if (typeof fetchImpl !== 'function') throw new TypeError('createPageSessionFetch needs a fetch');
|
|
61
126
|
let refreshing = null;
|
|
127
|
+
let quietUntil = 0;
|
|
128
|
+
const tell = (body, request) => {
|
|
129
|
+
try { onUnavailable?.({ reason: typeof body?.reason === 'string' ? body.reason : '', retryable: body?.retryable === true, error: typeof body?.error === 'string' ? body.error : '', url: request.url, method: request.method }); } catch { /* a listener's own error */ }
|
|
130
|
+
};
|
|
131
|
+
const tellRefused = (body, request, status = 403) => {
|
|
132
|
+
try { onRefused?.({ code: body.code, url: request.url, method: request.method, status }); } catch { /* a listener's own error */ }
|
|
133
|
+
};
|
|
134
|
+
/** Read a 403 answer's body and tell it when it is a final owner refusal (the answer itself is untouched). */
|
|
135
|
+
async function noteRefused(res, request) {
|
|
136
|
+
if (!onRefused || !res || res.status !== 403 || typeof res.clone !== 'function') return res;
|
|
137
|
+
let body = null;
|
|
138
|
+
try { body = await res.clone().json(); } catch { return res; }
|
|
139
|
+
if (isOwnerAccessRefusal(res.status, body)) tellRefused(body, request);
|
|
140
|
+
return res;
|
|
141
|
+
}
|
|
142
|
+
/** Read a 503 answer's body and tell it when it is PEER_UNAVAILABLE (the answer itself is untouched). */
|
|
143
|
+
async function noteUnavailable(res, request) {
|
|
144
|
+
if (!res || res.status !== 503 || typeof res.clone !== 'function') return false;
|
|
145
|
+
let body = null;
|
|
146
|
+
try { body = await res.clone().json(); } catch { return false; }
|
|
147
|
+
if (!isPeerUnavailable(res.status, body)) return false;
|
|
148
|
+
tell(body, request);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
62
151
|
function refreshCookie() {
|
|
152
|
+
if (now() < quietUntil) return Promise.resolve(false);
|
|
63
153
|
if (!refreshing) {
|
|
64
154
|
refreshing = (async () => {
|
|
65
155
|
try {
|
|
66
156
|
const res = await fetchImpl(refreshPath, { method: 'GET', credentials: 'same-origin', cache: 'no-store', headers: { Accept: 'application/json' } });
|
|
67
|
-
|
|
157
|
+
if (await noteUnavailable(res, { url: refreshPath, method: 'GET' })) { quietUntil = now() + Math.max(0, Number(cooldownMs) || 0); return false; }
|
|
158
|
+
if (res && res.ok === true) { try { onRestored?.(); } catch { /* a listener's own error */ } return true; }
|
|
159
|
+
// The server refusing this page the secret itself (403 WORKER_BROWSER: the automation browser) is told too.
|
|
160
|
+
await noteRefused(res, { url: refreshPath, method: 'GET' });
|
|
161
|
+
return false;
|
|
68
162
|
} catch { return false; } finally { refreshing = null; }
|
|
69
163
|
})();
|
|
70
164
|
}
|
|
71
165
|
return refreshing;
|
|
72
166
|
}
|
|
73
|
-
|
|
74
|
-
const plan = replayPlan(input, init, origin);
|
|
167
|
+
async function pageSessionFetch(input, init) {
|
|
168
|
+
const plan = replayPlan(input, init, origin) || readReplayPlan(input, init, origin);
|
|
169
|
+
const request = describeRequest(input, init);
|
|
75
170
|
const res = await fetchImpl(input, init);
|
|
76
|
-
if (
|
|
171
|
+
if (res && res.status === 503) { await noteUnavailable(res, request); return res; }
|
|
172
|
+
if (!plan || !res || res.status !== 403 || typeof res.clone !== 'function') return noteRefused(res, request);
|
|
77
173
|
let body = null;
|
|
78
174
|
try { body = await res.clone().json(); } catch { return res; }
|
|
79
|
-
if (!isPageSecretRefusal(res.status, body)) return res;
|
|
80
|
-
|
|
175
|
+
if (!isPageSecretRefusal(res.status, body)) { if (isOwnerAccessRefusal(res.status, body)) tellRefused(body, request); return res; }
|
|
176
|
+
// A read is the page secret's business only when it says so (the browser routes): UI_ONLY on a read is handed back.
|
|
177
|
+
if (plan.read && body.code !== 'PAGE_SECRET_REQUIRED') return res;
|
|
178
|
+
if (!(await refreshCookie())) { if (isOwnerAccessRefusal(res.status, body)) tellRefused(body, request); return res; }
|
|
81
179
|
try { onRecover?.({ method: plan.method, url: plan.url, code: body.code }); } catch { /* a listener's own error */ }
|
|
82
|
-
|
|
83
|
-
|
|
180
|
+
const again = await fetchImpl(...plan.args);
|
|
181
|
+
if (again && again.status === 503) await noteUnavailable(again, { url: plan.url, method: plan.method });
|
|
182
|
+
return noteRefused(again, { url: plan.url, method: plan.method });
|
|
183
|
+
}
|
|
184
|
+
// The refresh by itself (one at a time, the same cooldown): true when the server handed this boot's cookie.
|
|
185
|
+
pageSessionFetch.refreshPageSession = () => refreshCookie();
|
|
186
|
+
return pageSessionFetch;
|
|
84
187
|
}
|
|
85
188
|
|
|
86
|
-
/** Wrap `target.fetch` (the window's) once; returns true when it did, false when it already had or there is none.
|
|
189
|
+
/** Wrap `target.fetch` (the window's) once; returns true when it did, false when it already had or there is none.
|
|
190
|
+
* A 503 PEER_UNAVAILABLE is dispatched on `target` as PEER_UNAVAILABLE_EVENT, a final owner refusal as
|
|
191
|
+
* OWNER_ACCESS_REFUSED_EVENT and a refresh that got the secret as OWNER_ACCESS_RESTORED_EVENT (when it can dispatch). */
|
|
87
192
|
export function installPageSessionRecovery(target = globalThis) {
|
|
88
193
|
if (!target || typeof target.fetch !== 'function' || target.__synabunPageSession) return false;
|
|
89
194
|
const original = target.fetch.bind(target);
|
|
90
195
|
const origin = target.location && typeof target.location.origin === 'string' ? target.location.origin : null;
|
|
91
|
-
|
|
196
|
+
const dispatch = (name, detail) => {
|
|
197
|
+
if (typeof target.dispatchEvent !== 'function' || typeof CustomEvent === 'undefined') return;
|
|
198
|
+
target.dispatchEvent(new CustomEvent(name, { detail }));
|
|
199
|
+
};
|
|
200
|
+
const onUnavailable = (detail) => dispatch(PEER_UNAVAILABLE_EVENT, detail);
|
|
201
|
+
// The last refusal is kept on the window too, for a module that listens only after it was told.
|
|
202
|
+
const onRefused = (detail) => { try { target.__synabunOwnerAccessRefused = detail; } catch { /* a frozen target */ } dispatch(OWNER_ACCESS_REFUSED_EVENT, detail); };
|
|
203
|
+
const onRestored = () => { try { delete target.__synabunOwnerAccessRefused; } catch { /* a frozen target */ } dispatch(OWNER_ACCESS_RESTORED_EVENT, {}); };
|
|
204
|
+
target.fetch = createPageSessionFetch(original, { origin, onUnavailable, onRefused, onRestored });
|
|
92
205
|
try { Object.defineProperty(target, '__synabunPageSession', { value: true, configurable: true }); } catch { target.__synabunPageSession = true; }
|
|
93
206
|
return true;
|
|
94
207
|
}
|
|
95
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Ask for this boot's page secret now (GET /api/page-session through the installed recovery: one request at a time,
|
|
211
|
+
* none during a cooldown after a 503). Resolves true when the server handed it, false otherwise; never rejects. For a
|
|
212
|
+
* socket the browser opens by itself (the browser panel's /ws/browser/:id), whose refusal for a stale cookie the page
|
|
213
|
+
* cannot read.
|
|
214
|
+
*/
|
|
215
|
+
export async function refreshPageSession(target = globalThis) {
|
|
216
|
+
try {
|
|
217
|
+
const refresh = target?.fetch?.refreshPageSession;
|
|
218
|
+
if (typeof refresh === 'function') return (await refresh()) === true;
|
|
219
|
+
if (typeof target?.fetch !== 'function') return false;
|
|
220
|
+
const res = await target.fetch(PAGE_SECRET_REFRESH_PATH, { method: 'GET', credentials: 'same-origin', cache: 'no-store', headers: { Accept: 'application/json' } });
|
|
221
|
+
return !!res && res.ok === true;
|
|
222
|
+
} catch { return false; }
|
|
223
|
+
}
|
|
224
|
+
|
|
96
225
|
if (typeof window !== 'undefined' && typeof document !== 'undefined') installPageSessionRecovery(window);
|