@bevel-software/platform-core-frontend 0.32.0 → 0.33.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/package.json +2 -2
- package/src/core/CoreAppShell.tsx +1 -1
- package/src/modules/access/api.ts +1 -1
- package/src/modules/access/components/ManageAccessDialog.tsx +4 -5
- package/src/modules/access/hooks/useAccessRequests.ts +1 -1
- package/src/modules/access/hooks/useFileAccess.ts +1 -1
- package/src/modules/access/index.ts +11 -0
- package/src/modules/access/requests.api.ts +1 -1
- package/src/modules/admin/components/AddMemberInput.tsx +1 -1
- package/src/modules/admin/components/UserAccountsPage.tsx +3 -3
- package/src/modules/admin/index.ts +7 -0
- package/src/modules/admin/state/admin.context.tsx +1 -1
- package/src/modules/audit/components/AuditEventsPanel.tsx +1 -1
- package/src/modules/audit/components/AuditLogPage.tsx +2 -2
- package/src/modules/auth/index.ts +12 -0
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.access-rules.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.binary.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.brings-itself-up-to-date.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.edit.test.tsx +166 -0
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.fork-point.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.initial-file.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.moved.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.sync-banner.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.test.tsx +0 -2
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.unreadable.test.tsx +0 -1
- package/src/modules/change-requests/__tests__/apply-outcome-for-every-viewer.test.tsx +0 -1
- package/src/modules/change-requests/components/BranchFilePreview.tsx +4 -12
- package/src/modules/change-requests/components/ChangeRequestDialog.tsx +138 -30
- package/src/modules/change-requests/components/ChangeRequestLink.tsx +2 -3
- package/src/modules/change-requests/components/CrFileTree.tsx +1 -1
- package/src/modules/change-requests/components/FileChangeBoxes.tsx +2 -3
- package/src/modules/change-requests/hooks/useApplyChangeRequest.ts +3 -5
- package/src/modules/change-requests/hooks/useCrFileDiffs.ts +1 -1
- package/src/modules/change-requests/index.ts +14 -0
- package/src/modules/change-requests/services/change-requests.api.ts +2 -2
- package/src/modules/change-requests/services/denied-file.api.ts +2 -2
- package/src/modules/change-requests/services/propose.api.ts +3 -4
- package/src/modules/change-requests/utils/diff.ts +1 -1
- package/src/modules/change-requests/utils/readFailure.ts +1 -1
- package/src/modules/embed/__tests__/EmbedView.test.tsx +19 -2
- package/src/modules/embed/components/EmbedLinkPage.tsx +1 -2
- package/src/modules/git/components/BranchSwitcher.tsx +3 -4
- package/src/modules/git/components/FileHistoryPanel.tsx +2 -2
- package/src/modules/git/components/GitSyncFailedBanner.tsx +2 -4
- package/src/modules/git/components/HistoryVersionPreview.tsx +4 -15
- package/src/modules/git/components/PullNeededBanner.tsx +1 -1
- package/src/modules/git/hooks/useAutoPullUpdates.ts +1 -1
- package/src/modules/git/index.ts +11 -0
- package/src/modules/layout/components/MobileChatLayout.tsx +1 -1
- package/src/modules/layout/index.ts +9 -0
- package/src/modules/library/__tests__/SkillPage.test.tsx +39 -0
- package/src/modules/library/__tests__/propose-change.test.ts +75 -0
- package/src/modules/library/components/AddToPluginDialog.tsx +1 -1
- package/src/modules/library/components/ChangeRequestDock.tsx +1 -1
- package/src/modules/library/components/LibraryLayout.tsx +2 -2
- package/src/modules/library/components/LibraryPage.tsx +3 -3
- package/src/modules/library/components/ManagedPluginRequests.tsx +1 -1
- package/src/modules/library/components/NewSkillPanel.tsx +2 -2
- package/src/modules/library/components/PersonalAddDialog.tsx +1 -1
- package/src/modules/library/components/PersonalPluginPage.tsx +1 -1
- package/src/modules/library/components/PluginExtras.tsx +1 -4
- package/src/modules/library/components/PluginPage.tsx +1 -1
- package/src/modules/library/components/PluginRows.tsx +1 -1
- package/src/modules/library/components/SkillsTree.tsx +1 -4
- package/src/modules/library/components/plugin-page-parts.tsx +1 -1
- package/src/modules/library/components/skill-page/SkillFilePane.tsx +19 -7
- package/src/modules/library/components/skill-page/SkillPage.tsx +19 -26
- package/src/modules/library/components/tool-page/ToolConnectionSection.tsx +2 -4
- package/src/modules/library/components/tool-page/ToolVarRow.tsx +2 -2
- package/src/modules/library/hooks/useJoinRequests.ts +1 -1
- package/src/modules/library/hooks/useLibraryData.ts +3 -3
- package/src/modules/library/hooks/useToolPage.ts +1 -1
- package/src/modules/library/hooks/useToolSource.ts +1 -1
- package/src/modules/library/index.ts +12 -0
- package/src/modules/library/routes/LibraryRoutes.tsx +1 -1
- package/src/modules/library/routes/WorkspaceItemRoute.tsx +1 -3
- package/src/modules/library/routes/library-paths.ts +1 -1
- package/src/modules/library/services/library.api.ts +10 -8
- package/src/modules/library/services/plugins.api.ts +1 -1
- package/src/modules/library/services/teams.api.ts +1 -1
- package/src/modules/library/utils/status.ts +2 -2
- package/src/modules/onboarding/components/ConnectAgentPill.tsx +1 -1
- package/src/modules/onboarding/components/CreatorWelcomePage.tsx +2 -3
- package/src/modules/onboarding/components/GetStartedColumn.tsx +6 -13
- package/src/modules/onboarding/components/InviteButton.tsx +2 -2
- package/src/modules/onboarding/components/InviteDialog.tsx +4 -6
- package/src/modules/onboarding/components/RootLanding.tsx +2 -2
- package/src/modules/onboarding/components/StarterPackCard.tsx +1 -1
- package/src/modules/onboarding/components/WelcomePage.tsx +2 -5
- package/src/modules/onboarding/index.ts +10 -0
- package/src/modules/onboarding/paths.ts +1 -1
- package/src/modules/onboarding/state/agent-connection.ts +2 -2
- package/src/modules/onboarding/state/onboarding.ts +1 -1
- package/src/modules/onboarding/state/starter-packs.ts +1 -1
- package/src/modules/pr/components/OpenChangeRequestDialog.tsx +1 -1
- package/src/modules/pr/index.ts +14 -0
- package/src/modules/pr/services/pr-approvals.api.ts +1 -1
- package/src/modules/pr/services/pr-cancel.api.ts +1 -1
- package/src/modules/pr/services/pr-detail.api.ts +2 -2
- package/src/modules/pr/services/pr-edit.api.ts +22 -0
- package/src/modules/pr/services/pr-merge.api.ts +1 -1
- package/src/modules/pr/services/pr-open.api.ts +1 -1
- package/src/modules/review/components/DiffViewer.tsx +1 -1
- package/src/modules/review/components/MarkdownDiffViewer.tsx +1 -2
- package/src/modules/review/components/ReviewPanel.tsx +2 -3
- package/src/modules/review/state/ReviewProvider.tsx +2 -2
- package/src/modules/secrets-vault/index.ts +6 -0
- package/src/modules/settings/components/DeploymentPage.tsx +2 -2
- package/src/modules/settings/components/SettingsLayout.tsx +2 -2
- package/src/modules/settings/index.ts +8 -0
- package/src/modules/setup/components/FirstRunStorage.tsx +1 -1
- package/src/modules/setup/index.ts +6 -0
- package/src/modules/toolbar/commands/actions.tsx +3 -3
- package/src/modules/toolbar/commands/useCommandActions.ts +5 -9
- package/src/modules/toolbar/components/AgentInstructionsCard.tsx +2 -2
- package/src/modules/toolbar/components/AppSwitcher.tsx +1 -2
- package/src/modules/toolbar/components/CoworkSetupSteps.tsx +1 -1
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +1 -1
- package/src/modules/toolbar/components/ProfileMenu.tsx +3 -3
- package/src/modules/toolbar/components/SearchPalette.tsx +4 -8
- package/src/modules/toolbar/components/Toolbar.tsx +2 -5
- package/src/modules/toolbar/index.ts +6 -0
- package/src/modules/toolbar/search/sources.ts +4 -7
- package/src/modules/toolbar/services/agent-instructions.api.ts +1 -1
- package/src/modules/tools/ToolsExplorerPage.tsx +217 -18
- package/src/modules/tools/__tests__/ToolsExplorerPage.read-only.test.tsx +220 -0
- package/src/modules/tools/tool-hints.api.ts +44 -0
- package/src/modules/workflow/index.ts +7 -0
- package/src/modules/workflow/state/ChangeRequestStaleBinder.tsx +1 -0
- package/src/modules/workflow/state/EventBusFocusBinder.tsx +1 -1
- package/src/modules/workspace/components/FileExplorer.tsx +35 -8
- package/src/modules/workspace/components/FilePaneCard.tsx +37 -12
- package/src/modules/workspace/components/FileRoute.tsx +10 -3
- package/src/modules/workspace/components/FileViewer.tsx +169 -25
- package/src/modules/workspace/components/FullScreenView.tsx +93 -0
- package/src/modules/workspace/components/KbFileRail.tsx +1 -1
- package/src/modules/workspace/components/KbPageHeader.tsx +9 -0
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +137 -0
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +11 -0
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +487 -3
- package/src/modules/workspace/components/readingCarry.ts +59 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +36 -175
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +1 -1
- package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.tools.test.tsx +168 -42
- package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +20 -3
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +63 -4
- package/src/modules/workspace/components/renderers/__tests__/htmlToolBridge.test.ts +235 -1
- package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +14 -3
- package/src/modules/workspace/components/renderers/__tests__/pageToolsHarness.ts +79 -0
- package/src/modules/workspace/components/renderers/__tests__/useHtmlToolBridge.test.tsx +316 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +75 -13
- package/src/modules/workspace/components/renderers/htmlToolBridge.ts +196 -25
- package/src/modules/workspace/components/renderers/index.ts +10 -0
- package/src/modules/workspace/components/renderers/useHtmlToolBridge.tsx +253 -0
- package/src/modules/workspace/hooks/useFullScreenView.ts +52 -0
- package/src/modules/workspace/hooks/useImageRevision.ts +1 -1
- package/src/modules/workspace/hooks/useKnowledgeWriteTarget.ts +1 -1
- package/src/modules/workspace/hooks/useLastCommit.ts +1 -1
- package/src/modules/workspace/hooks/useWorkspaceState.ts +4 -4
- package/src/modules/workspace/index.ts +30 -1
- package/src/modules/workspace/routing/kb-routes.ts +1 -1
- package/src/modules/workspace/state/__tests__/applied-request-clears-pending.test.tsx +0 -1
- package/src/modules/workspace/state/open-change-requests.tsx +1 -1
- package/src/modules/workspace/utils/frameEscape.ts +15 -0
- package/src/modules/workspace/utils/treeConfirm.ts +1 -1
- package/src/shared/theme/measure.ts +7 -0
- package/src/modules/pr/services/pr-comments.api.ts +0 -45
|
@@ -4,16 +4,7 @@ import { useFileNav } from '../../routing/kb-routes';
|
|
|
4
4
|
import type { FileRendererProps, RendererSaveState } from './types';
|
|
5
5
|
import { buildSandboxedHtml, sanitizeAgentHtml } from './htmlSandbox';
|
|
6
6
|
import { useRendererSurface } from './rendererSurface';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
PageConsent,
|
|
10
|
-
PageToolsClient,
|
|
11
|
-
answerToolCall,
|
|
12
|
-
asToolCall,
|
|
13
|
-
pageName,
|
|
14
|
-
useOpenPageInPlatform,
|
|
15
|
-
usePageTools,
|
|
16
|
-
} from './htmlToolBridge';
|
|
7
|
+
import { useHtmlToolBridge } from './useHtmlToolBridge';
|
|
17
8
|
import {
|
|
18
9
|
HTML_FRAME_KEY_STEP,
|
|
19
10
|
HTML_FRAME_MAX_HEIGHT,
|
|
@@ -22,13 +13,6 @@ import {
|
|
|
22
13
|
attachFrameResizeHandle,
|
|
23
14
|
} from './frameResizeHandle';
|
|
24
15
|
|
|
25
|
-
/**
|
|
26
|
-
* The card the app shows over a page, outside its frame: the page cannot draw,
|
|
27
|
-
* see or answer it. `write`: the page's first writing call asks the viewer.
|
|
28
|
-
* `platform`: a page that needs tools asked for them where there are none.
|
|
29
|
-
*/
|
|
30
|
-
type PageCard = { kind: 'write' | 'platform'; answer: (ok: boolean | null) => void };
|
|
31
|
-
|
|
32
16
|
/**
|
|
33
17
|
* Where the reader's chosen frame height is kept, per browser: a
|
|
34
18
|
* convenience, like a remembered tab. A browser that cannot store it is
|
|
@@ -65,16 +49,11 @@ export function HtmlRenderer({
|
|
|
65
49
|
onSaveStateChange,
|
|
66
50
|
readOnly = false,
|
|
67
51
|
}: FileRendererProps) {
|
|
68
|
-
const { openLink
|
|
52
|
+
const { openLink } = useFileNav();
|
|
69
53
|
const surface = useRendererSurface();
|
|
70
54
|
// Whether the surface's host grows with the page (an issue panel) rather
|
|
71
55
|
// than giving it a pane to scroll in (the app, the MCP App's pane).
|
|
72
56
|
const sizesToContent = surface?.sizesToContent === true;
|
|
73
|
-
// The viewer's tools, for a page on the app's file page — the one place
|
|
74
|
-
// that provides them. Everywhere else the page gets `bevel.tools = null`.
|
|
75
|
-
const pageTools = usePageTools();
|
|
76
|
-
const openPageInPlatform = useOpenPageInPlatform();
|
|
77
|
-
const toolPagePath = surface === null && pageTools ? pageTools.pagePath : null;
|
|
78
57
|
const [mode, setMode] = useState<'preview' | 'edit'>('preview');
|
|
79
58
|
/**
|
|
80
59
|
* The frame's height where the host grows with the page: the standard
|
|
@@ -91,17 +70,20 @@ export function HtmlRenderer({
|
|
|
91
70
|
const [savedValue, setSavedValue] = useState(savedContent ?? content);
|
|
92
71
|
const [saveState, setSaveState] = useState<RendererSaveState>('idle');
|
|
93
72
|
const [saveError, setSaveError] = useState<string | null>(null);
|
|
94
|
-
const [bundledSrcdoc, setBundledSrcdoc] = useState<string | null>(null);
|
|
95
|
-
const [previewError, setPreviewError] = useState<string | null>(null);
|
|
96
73
|
const [reloadKey, setReloadKey] = useState(0);
|
|
97
74
|
const iframeRef = useRef<HTMLIFrameElement | null>(null);
|
|
98
75
|
const prevFilePathRef = useRef(filePath);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
|
|
76
|
+
|
|
77
|
+
// The page tool bridge: a page's `window.bevel.tools` and `requireTools()`,
|
|
78
|
+
// answered for one view of the page — a new frame document (a reload, a
|
|
79
|
+
// saved edit, another file) or the frame gone (Edit) is a new view. The
|
|
80
|
+
// view's key is a fresh identity whenever one of the INPUTS of the
|
|
81
|
+
// document changes, never the built document itself: the bridge's
|
|
82
|
+
// `toolBridge` goes into the build, so a key read off the build would
|
|
83
|
+
// chase it; and a distribution's document carries its inlined libraries,
|
|
84
|
+
// which a key built from it would compare on every render.
|
|
85
|
+
const viewKey = useMemo(() => ({ reloadKey, mode, savedValue, filePath }), [reloadKey, mode, savedValue, filePath]);
|
|
86
|
+
const { toolBridge, cards } = useHtmlToolBridge({ iframeRef, filePath, viewKey });
|
|
105
87
|
|
|
106
88
|
const dirty = !readOnly && value !== savedValue;
|
|
107
89
|
|
|
@@ -126,17 +108,23 @@ export function HtmlRenderer({
|
|
|
126
108
|
onSaveStateChange?.(saveState);
|
|
127
109
|
}, [saveState, onSaveStateChange]);
|
|
128
110
|
|
|
129
|
-
// Build the iframe document
|
|
130
|
-
// content
|
|
131
|
-
// (defense in depth — CSP also
|
|
132
|
-
// the KB's JS library inlined as
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
111
|
+
// Build the iframe document for the view on screen: in preview, from the
|
|
112
|
+
// saved content and the bridge's identity for this view. The agent's HTML
|
|
113
|
+
// is sanitized to strip any external URLs (defense in depth — CSP also
|
|
114
|
+
// blocks them at runtime), then bundled with the KB's JS library inlined as
|
|
115
|
+
// a `<script type="module">`. The result is a fully self-contained srcdoc —
|
|
116
|
+
// the iframe makes no network calls of any kind.
|
|
117
|
+
//
|
|
118
|
+
// Built DURING render, never in an effect after it: a new view (Refresh
|
|
119
|
+
// remounts the frame) must mount its frame with its own document. Built
|
|
120
|
+
// after the commit, the new frame first loaded the previous view's document
|
|
121
|
+
// and had the new one swapped in mid-load, and Chromium leaves a sandboxed
|
|
122
|
+
// frame whose srcdoc is replaced that early unpainted — blank on every
|
|
123
|
+
// Refresh.
|
|
124
|
+
const preview = useMemo((): { srcdoc: string; error: null } | { srcdoc: null; error: string } | null => {
|
|
125
|
+
if (mode !== 'preview') return null;
|
|
138
126
|
try {
|
|
139
|
-
const
|
|
127
|
+
const srcdoc = buildSandboxedHtml({
|
|
140
128
|
title: filePath,
|
|
141
129
|
// Core ships no inlined vendor libraries. (The enterprise build inlines
|
|
142
130
|
// d3 + mermaid and the KB graph client here — the knowledge system,
|
|
@@ -144,13 +132,15 @@ export function HtmlRenderer({
|
|
|
144
132
|
// extension.)
|
|
145
133
|
libModuleSources: [],
|
|
146
134
|
bodyHtml: sanitizeAgentHtml(savedValue),
|
|
147
|
-
toolBridge
|
|
135
|
+
toolBridge,
|
|
148
136
|
});
|
|
149
|
-
|
|
137
|
+
return { srcdoc, error: null };
|
|
150
138
|
} catch (err) {
|
|
151
|
-
|
|
139
|
+
return { srcdoc: null, error: err instanceof Error ? err.message : 'Failed to build preview' };
|
|
152
140
|
}
|
|
153
|
-
}, [mode, savedValue, filePath,
|
|
141
|
+
}, [mode, savedValue, filePath, toolBridge]);
|
|
142
|
+
const bundledSrcdoc = preview?.srcdoc ?? null;
|
|
143
|
+
const previewError = preview?.error ?? null;
|
|
154
144
|
|
|
155
145
|
// (The enterprise build additionally installs a postMessage bridge here that
|
|
156
146
|
// serves the iframe's KB-graph client from `/api/workspace/:id/graph` — the
|
|
@@ -178,100 +168,6 @@ export function HtmlRenderer({
|
|
|
178
168
|
return () => window.removeEventListener('message', onNavMessage);
|
|
179
169
|
}, [openLink, filePath]);
|
|
180
170
|
|
|
181
|
-
/** Show one of the app's cards over the page and resolve with the viewer's answer. A card already up answers this ask too. */
|
|
182
|
-
function showCard(kind: PageCard['kind']): Promise<boolean | null> {
|
|
183
|
-
const open = cardRef.current;
|
|
184
|
-
if (open && open.kind === kind) {
|
|
185
|
-
return new Promise((resolve) => {
|
|
186
|
-
const first = open.answer;
|
|
187
|
-
open.answer = (ok) => {
|
|
188
|
-
first(ok);
|
|
189
|
-
resolve(ok);
|
|
190
|
-
};
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
return new Promise((resolve) => {
|
|
194
|
-
const next: PageCard = { kind, answer: resolve };
|
|
195
|
-
cardRef.current = next;
|
|
196
|
-
setCard(next);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** Close the card with the viewer's answer, or null when the view went before they gave one. */
|
|
201
|
-
const closeCard = useCallback((ok: boolean | null) => {
|
|
202
|
-
const open = cardRef.current;
|
|
203
|
-
cardRef.current = null;
|
|
204
|
-
setCard(null);
|
|
205
|
-
open?.answer(ok);
|
|
206
|
-
}, []);
|
|
207
|
-
|
|
208
|
-
// When the page's document goes (a reload, a saved edit, another file, or
|
|
209
|
-
// Edit, which unmounts the frame even when the document comes back the same)
|
|
210
|
-
// its consent goes with it, and a card left open answers no: nothing the old
|
|
211
|
-
// view asked for runs in the new one.
|
|
212
|
-
useEffect(
|
|
213
|
-
() => () => {
|
|
214
|
-
consentRef.current = null;
|
|
215
|
-
closeCard(null);
|
|
216
|
-
},
|
|
217
|
-
[bundledSrcdoc, reloadKey, mode, closeCard],
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
// Tool bridge: a page's `window.bevel.tools` calls and its
|
|
221
|
-
// `requireTools()`, posted by the runtime in the frame (see
|
|
222
|
-
// `htmlSandbox.ts`), answered here with the viewer's session.
|
|
223
|
-
useEffect(() => {
|
|
224
|
-
function onToolMessage(event: MessageEvent) {
|
|
225
|
-
const iframeWin = iframeRef.current?.contentWindow;
|
|
226
|
-
if (!iframeWin || event.source !== iframeWin) return;
|
|
227
|
-
const reply = (message: unknown) => {
|
|
228
|
-
// Only to the document that asked: a reloaded frame is a new window.
|
|
229
|
-
if (iframeRef.current?.contentWindow === iframeWin) iframeWin.postMessage(message, '*');
|
|
230
|
-
};
|
|
231
|
-
const data = event.data as { type?: unknown; requestId?: unknown } | null;
|
|
232
|
-
if (data && data.type === 'bevel.tools.require' && typeof data.requestId === 'string') {
|
|
233
|
-
const requestId = data.requestId;
|
|
234
|
-
if (toolsClient) {
|
|
235
|
-
reply({ type: 'bevel.tools.require.response', requestId, ok: true, result: true });
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
void showCard('platform').then(() =>
|
|
239
|
-
// Either way the page that asked carries on without tools: Open in
|
|
240
|
-
// the platform opened it elsewhere, it did not give it tools here.
|
|
241
|
-
reply({ type: 'bevel.tools.require.response', requestId, ok: true, result: false }),
|
|
242
|
-
);
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
const call = asToolCall(data);
|
|
246
|
-
if (!call) return;
|
|
247
|
-
if (!toolsClient) {
|
|
248
|
-
reply({
|
|
249
|
-
type: 'bevel.tool.response',
|
|
250
|
-
requestId: call.requestId,
|
|
251
|
-
ok: false,
|
|
252
|
-
error: 'Tools are only available on the page in the platform.',
|
|
253
|
-
code: 'unavailable',
|
|
254
|
-
});
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
consentRef.current ??= new PageConsent(() => showCard('write'));
|
|
258
|
-
void answerToolCall(call, toolsClient, consentRef.current).then(reply);
|
|
259
|
-
}
|
|
260
|
-
window.addEventListener('message', onToolMessage);
|
|
261
|
-
return () => window.removeEventListener('message', onToolMessage);
|
|
262
|
-
}, [toolsClient]);
|
|
263
|
-
|
|
264
|
-
const openInPlatform = useCallback(() => {
|
|
265
|
-
closeCard(false);
|
|
266
|
-
// The host's own way when it has one (a skill page, whose filePath is
|
|
267
|
-
// relative to the skill); otherwise the file page, by an address that
|
|
268
|
-
// asks for it outright: on the embed the surface opens it through the
|
|
269
|
-
// host in a new tab, which carries no router state, and a page inside a
|
|
270
|
-
// skill's folder would otherwise open as that skill's page again.
|
|
271
|
-
if (openPageInPlatform) openPageInPlatform();
|
|
272
|
-
else openFilePage(filePath);
|
|
273
|
-
}, [closeCard, openPageInPlatform, openFilePage, filePath]);
|
|
274
|
-
|
|
275
171
|
useEffect(() => {
|
|
276
172
|
frameHeightRef.current = frameHeight;
|
|
277
173
|
}, [frameHeight]);
|
|
@@ -449,42 +345,7 @@ export function HtmlRenderer({
|
|
|
449
345
|
Loading preview…
|
|
450
346
|
</div>
|
|
451
347
|
)}
|
|
452
|
-
|
|
453
|
-
open={card?.kind === 'write'}
|
|
454
|
-
onClose={() => closeCard(false)}
|
|
455
|
-
title={`${pageName(filePath)} wants to write to the knowledge base`}
|
|
456
|
-
size="md"
|
|
457
|
-
footer={
|
|
458
|
-
<>
|
|
459
|
-
<Button variant="outline" onClick={() => closeCard(false)}>
|
|
460
|
-
Deny
|
|
461
|
-
</Button>
|
|
462
|
-
<Button variant="primary" onClick={() => closeCard(true)}>
|
|
463
|
-
Allow while open
|
|
464
|
-
</Button>
|
|
465
|
-
</>
|
|
466
|
-
}
|
|
467
|
-
>
|
|
468
|
-
<p className="font-mono text-detail text-ink-muted break-all">{toolPagePath ?? filePath}</p>
|
|
469
|
-
</Dialog>
|
|
470
|
-
<Dialog
|
|
471
|
-
open={card?.kind === 'platform'}
|
|
472
|
-
onClose={() => closeCard(false)}
|
|
473
|
-
title={`${pageName(filePath)} uses tools that are only available in the platform`}
|
|
474
|
-
size="md"
|
|
475
|
-
footer={
|
|
476
|
-
<>
|
|
477
|
-
<Button variant="outline" onClick={() => closeCard(false)}>
|
|
478
|
-
Continue without tools
|
|
479
|
-
</Button>
|
|
480
|
-
<Button variant="primary" onClick={openInPlatform}>
|
|
481
|
-
Open in the platform
|
|
482
|
-
</Button>
|
|
483
|
-
</>
|
|
484
|
-
}
|
|
485
|
-
>
|
|
486
|
-
<p className="font-mono text-detail text-ink-muted break-all">{filePath}</p>
|
|
487
|
-
</Dialog>
|
|
348
|
+
{cards}
|
|
488
349
|
</div>
|
|
489
350
|
);
|
|
490
351
|
}
|
|
@@ -7,7 +7,7 @@ import { Surface, useConfirm } from '../../../../shared/components';
|
|
|
7
7
|
import { Markdown } from '../../../../shared/markdown/Markdown';
|
|
8
8
|
import { ToolForm } from './ToolForm';
|
|
9
9
|
import { ToolSecretsPanel } from '../../../secrets-vault/components/ToolSecretsPanel';
|
|
10
|
-
import { listToolSecrets, type ToolSecrets } from '../../../secrets-vault
|
|
10
|
+
import { listToolSecrets, type ToolSecrets } from '../../../secrets-vault';
|
|
11
11
|
import { useRendererSurface } from './rendererSurface';
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
2
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
3
3
|
import { MemoryRouter } from 'react-router-dom';
|
|
4
|
-
import { RendererSurfaceContext, getFileRenderer, type RendererSurface } from '..';
|
|
5
|
-
import { OpenPageInPlatformContext, PageToolsContext } from '../htmlToolBridge';
|
|
4
|
+
import { OpenPageInPlatformContext, PageToolsContext, RendererSurfaceContext, getFileRenderer, type RendererSurface } from '..';
|
|
6
5
|
import { GitContext, type GitContextValue } from '../../../../git';
|
|
6
|
+
import { clearToken, setToken } from '../../../../../lib/api';
|
|
7
7
|
import { WorkspaceContext, type WorkspaceContextValue } from '../../../state/workspace.context';
|
|
8
|
+
import { frameChannel, stubServer } from './pageToolsHarness';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
* `window.bevel.tools` as the app gives it: present
|
|
11
|
-
* everywhere else, and the two cards — consent to write, and the way to the
|
|
11
|
+
* `window.bevel.tools` as the app gives it: present where a host provides the
|
|
12
|
+
* bridge (the file page, the skill page), `null` everywhere else, and the two cards — consent to write, and the way to the
|
|
12
13
|
* platform — drawn by the app around the frame, driven here by the very
|
|
13
14
|
* messages the frame's runtime posts.
|
|
14
15
|
*/
|
|
@@ -20,22 +21,28 @@ const PAGE = 'Dashboards/Roadmap.html';
|
|
|
20
21
|
const workspace = { workspaceId: 'main', kbDirName: 'knowledge-base' } as unknown as WorkspaceContextValue;
|
|
21
22
|
const git = { status: { branch: 'main' } } as unknown as GitContextValue;
|
|
22
23
|
|
|
23
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* The page on the app's file page: in the app, with the file page's provider.
|
|
26
|
+
* `receive` is the file's content changing under the open page (a teammate's
|
|
27
|
+
* save arriving): the same frame, a new document in it.
|
|
28
|
+
*/
|
|
24
29
|
function onFilePage({ readOnly = true }: { readOnly?: boolean } = {}) {
|
|
25
|
-
|
|
30
|
+
const tree = (content: string) => (
|
|
26
31
|
<MemoryRouter initialEntries={['/workspace/main/knowledge-base/Dashboards/Roadmap.html']}>
|
|
27
32
|
<WorkspaceContext.Provider value={workspace}>
|
|
28
33
|
<GitContext.Provider value={git}>
|
|
29
34
|
<PageToolsContext.Provider value={{ pagePath: PAGE }}>
|
|
30
|
-
<HtmlRenderer content=
|
|
35
|
+
<HtmlRenderer content={content} filePath={PATH} onSave={async () => undefined} readOnly={readOnly} />
|
|
31
36
|
</PageToolsContext.Provider>
|
|
32
37
|
</GitContext.Provider>
|
|
33
38
|
</WorkspaceContext.Provider>
|
|
34
|
-
</MemoryRouter
|
|
39
|
+
</MemoryRouter>
|
|
35
40
|
);
|
|
41
|
+
const result = render(tree('<h1>Roadmap</h1>'));
|
|
42
|
+
return { ...result, receive: (content: string) => result.rerender(tree(content)) };
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
/** The page in the app
|
|
45
|
+
/** The page in the app on a host without the bridge — a history or change-request preview. */
|
|
39
46
|
function inAppElsewhere() {
|
|
40
47
|
return render(
|
|
41
48
|
<MemoryRouter initialEntries={['/workspace/main']}>
|
|
@@ -80,37 +87,6 @@ async function frameOf(container: HTMLElement): Promise<HTMLIFrameElement> {
|
|
|
80
87
|
});
|
|
81
88
|
}
|
|
82
89
|
|
|
83
|
-
/** Post `data` as the frame would, and collect what the app posts back to it. */
|
|
84
|
-
function frameChannel(iframe: HTMLIFrameElement) {
|
|
85
|
-
const win = iframe.contentWindow!;
|
|
86
|
-
const replies: Array<Record<string, unknown>> = [];
|
|
87
|
-
vi.spyOn(win, 'postMessage').mockImplementation(((msg: Record<string, unknown>) => void replies.push(msg)) as never);
|
|
88
|
-
return {
|
|
89
|
-
replies,
|
|
90
|
-
post: (data: unknown) => window.dispatchEvent(new MessageEvent('message', { data, source: win })),
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** The hosted server and the manual, as the app's fetch sees them. */
|
|
95
|
-
function stubServer() {
|
|
96
|
-
const mcp: string[] = [];
|
|
97
|
-
vi.stubGlobal(
|
|
98
|
-
'fetch',
|
|
99
|
-
vi.fn(async (url: string, init?: RequestInit) => {
|
|
100
|
-
if (url === '/api/agent/utcp') return Response.json({ tools: [{ name: 'read_file', tags: [] }, { name: 'edit_file', tags: ['write'] }] });
|
|
101
|
-
if (url === '/api/mcp/page-refusals') return new Response(null, { status: 204 });
|
|
102
|
-
const body = JSON.parse(String(init!.body)) as { id: number; params: { name: string } };
|
|
103
|
-
mcp.push(body.params.name);
|
|
104
|
-
return Response.json({
|
|
105
|
-
jsonrpc: '2.0',
|
|
106
|
-
id: body.id,
|
|
107
|
-
result: { content: [{ type: 'text', text: JSON.stringify({ ran: body.params.name }) }] },
|
|
108
|
-
});
|
|
109
|
-
}),
|
|
110
|
-
);
|
|
111
|
-
return { mcp };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
90
|
afterEach(() => {
|
|
115
91
|
cleanup();
|
|
116
92
|
vi.restoreAllMocks();
|
|
@@ -118,11 +94,11 @@ afterEach(() => {
|
|
|
118
94
|
});
|
|
119
95
|
|
|
120
96
|
describe('window.bevel.tools on the file page and elsewhere', () => {
|
|
121
|
-
it('gives the page the
|
|
97
|
+
it('gives the page the five functions on the file page', async () => {
|
|
122
98
|
const { container } = onFilePage();
|
|
123
99
|
const srcdoc = (await frameOf(container)).getAttribute('srcdoc')!;
|
|
124
100
|
expect(srcdoc).toContain('globalThis.bevel.tools = Object.freeze({');
|
|
125
|
-
for (const fn of ['searchTools', 'toolsInfo', 'callTool', 'callToolChain']) expect(srcdoc).toContain(`${fn}: function`);
|
|
101
|
+
for (const fn of ['searchTools', 'toolsInfo', 'callTool', 'callToolChain', 'uploadFile']) expect(srcdoc).toContain(`${fn}: function`);
|
|
126
102
|
});
|
|
127
103
|
|
|
128
104
|
it('gives it null on the embed and on any other preview in the app', async () => {
|
|
@@ -212,6 +188,86 @@ describe('the consent card on the file page', () => {
|
|
|
212
188
|
});
|
|
213
189
|
});
|
|
214
190
|
|
|
191
|
+
describe('an upload from the file page', () => {
|
|
192
|
+
const TOKEN = 'bevel-up_s3cret';
|
|
193
|
+
|
|
194
|
+
/** The hosted server and the upload endpoint, as the app's fetch sees them: every request, with its headers. */
|
|
195
|
+
function stubUploadServer() {
|
|
196
|
+
const requests: Array<{ step: string; headers: Headers; body: unknown }> = [];
|
|
197
|
+
vi.stubGlobal(
|
|
198
|
+
'fetch',
|
|
199
|
+
vi.fn(async (url: string, init?: RequestInit) => {
|
|
200
|
+
const headers = new Headers(init?.headers);
|
|
201
|
+
if (url === '/api/agent/utcp') return Response.json({ tools: [] });
|
|
202
|
+
if (url === '/api/mcp/page-refusals') {
|
|
203
|
+
requests.push({ step: 'refusal', headers, body: JSON.parse(String(init!.body)) });
|
|
204
|
+
return new Response(null, { status: 204 });
|
|
205
|
+
}
|
|
206
|
+
if (url.startsWith('/api/agent/uploads')) {
|
|
207
|
+
requests.push({ step: `${init?.method} ${url}`, headers, body: init?.body });
|
|
208
|
+
return Response.json({ filename: 'Assets.xlsx', bytes: 4, kind: 'file' });
|
|
209
|
+
}
|
|
210
|
+
const rpc = JSON.parse(String(init!.body)) as { id: number; params: { name: string; arguments: { body: Record<string, unknown> } } };
|
|
211
|
+
requests.push({ step: rpc.params.name, headers, body: rpc.params.arguments });
|
|
212
|
+
const answer =
|
|
213
|
+
rpc.params.name === 'request_file_upload'
|
|
214
|
+
? { uploadUrl: `https://kb.example.com/api/agent/uploads/${TOKEN}`, token: TOKEN, maxBytes: 1024 }
|
|
215
|
+
: { destination: 'Data/Campaigns/Easter', count: 1, total: 1, files: [{ path: 'knowledge-base/Data/Campaigns/Easter/Assets.xlsx', outcome: 'created' }] };
|
|
216
|
+
return Response.json({ jsonrpc: '2.0', id: rpc.id, result: { content: [{ type: 'text', text: JSON.stringify(answer) }] } });
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
return { requests };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const file = () => new File([new Uint8Array([0x50, 0x4b, 0x03, 0x04])], 'Assets.xlsx');
|
|
223
|
+
|
|
224
|
+
it('asks with the write card, and on Allow runs the three steps in order with the viewer\'s session', async () => {
|
|
225
|
+
setToken('viewer-session');
|
|
226
|
+
try {
|
|
227
|
+
const server = stubUploadServer();
|
|
228
|
+
const { container } = onFilePage();
|
|
229
|
+
const frame = frameChannel(await frameOf(container));
|
|
230
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'u1', fn: 'uploadFile', args: [file(), { branch: 'main', destination: 'Data/Campaigns/Easter' }] });
|
|
231
|
+
expect(await screen.findByText('Roadmap wants to write to the knowledge base')).toBeTruthy();
|
|
232
|
+
expect(server.requests).toEqual([]);
|
|
233
|
+
|
|
234
|
+
fireEvent.click(screen.getByRole('button', { name: 'Allow while open' }));
|
|
235
|
+
await waitFor(() => expect(frame.replies.at(-1)).toMatchObject({ requestId: 'u1', ok: true, result: { count: 1, destination: 'Data/Campaigns/Easter' } }));
|
|
236
|
+
expect(server.requests.map((r) => r.step)).toEqual([
|
|
237
|
+
'request_file_upload',
|
|
238
|
+
'POST /api/agent/uploads?filename=Assets.xlsx',
|
|
239
|
+
'apply_file_upload',
|
|
240
|
+
]);
|
|
241
|
+
for (const r of server.requests) expect(r.headers.get('Authorization')).toBe('Bearer viewer-session');
|
|
242
|
+
for (const r of server.requests.filter((x) => x.step.endsWith('_upload'))) {
|
|
243
|
+
expect(decodeURIComponent(r.headers.get('X-Bevel-Page')!)).toBe(PAGE);
|
|
244
|
+
}
|
|
245
|
+
expect(server.requests[1]!.headers.get('x-upload-token')).toBe(TOKEN);
|
|
246
|
+
// The token went to the server and nowhere else: not into an address, not back to the frame.
|
|
247
|
+
expect(server.requests[1]!.step).not.toContain(TOKEN);
|
|
248
|
+
expect(JSON.stringify(frame.replies)).not.toContain(TOKEN);
|
|
249
|
+
|
|
250
|
+
// Allow holds for the view: the next upload runs with no card.
|
|
251
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'u2', fn: 'uploadFile', args: [file(), { branch: 'main', destination: 'Data' }] });
|
|
252
|
+
await waitFor(() => expect(frame.replies.at(-1)).toMatchObject({ requestId: 'u2', ok: true }));
|
|
253
|
+
expect(screen.queryByText(/wants to write to the knowledge base/)).toBeNull();
|
|
254
|
+
} finally {
|
|
255
|
+
clearToken();
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('Deny answers denied, uploads nothing, and reports a refused apply_file_upload under the page', async () => {
|
|
260
|
+
const server = stubUploadServer();
|
|
261
|
+
const { container } = onFilePage();
|
|
262
|
+
const frame = frameChannel(await frameOf(container));
|
|
263
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'u1', fn: 'uploadFile', args: [file(), { branch: 'main', destination: 'Data/Campaigns/Easter' }] });
|
|
264
|
+
fireEvent.click(await screen.findByRole('button', { name: 'Deny' }));
|
|
265
|
+
await waitFor(() => expect(frame.replies.at(-1)).toEqual({ type: 'bevel.tool.response', requestId: 'u1', ok: false, error: 'denied', code: 'denied' }));
|
|
266
|
+
await waitFor(() => expect(server.requests.map((r) => r.step)).toEqual(['refusal']));
|
|
267
|
+
expect(server.requests[0]!.body).toMatchObject({ page: PAGE, tool: 'apply_file_upload' });
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
215
271
|
describe('the platform card where there are no tools', () => {
|
|
216
272
|
it('on the embed offers Continue without tools (false) and Open in the platform (opens the page, false)', async () => {
|
|
217
273
|
const openFilePage = vi.fn();
|
|
@@ -270,3 +326,73 @@ describe('the platform card where there are no tools', () => {
|
|
|
270
326
|
expect(screen.queryByText(/only available in the platform/)).toBeNull();
|
|
271
327
|
});
|
|
272
328
|
});
|
|
329
|
+
|
|
330
|
+
describe('a call whose view ended before it was decided', () => {
|
|
331
|
+
// An ended view's calls are decided `closed` and their replies are never
|
|
332
|
+
// posted: the controller fences replies by view generation, which its own
|
|
333
|
+
// suite pins with a frame that keeps its window across a view change. A
|
|
334
|
+
// rebuilt document is a new frame window here, so this renderer shows the
|
|
335
|
+
// rest: no card in the new view, nothing run, and neither window hearing
|
|
336
|
+
// an answer meant for a view that is gone.
|
|
337
|
+
const settled = () => new Promise((r) => setTimeout(r, 50));
|
|
338
|
+
|
|
339
|
+
it('shows no card and runs nothing when the document changes under it while the lookup is in flight', async () => {
|
|
340
|
+
const server = stubServer({ holdManual: true });
|
|
341
|
+
const page = onFilePage();
|
|
342
|
+
const iframe = await frameOf(page.container);
|
|
343
|
+
const frame = frameChannel(iframe);
|
|
344
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'w1', fn: 'callTool', args: ['edit_file', {}] });
|
|
345
|
+
|
|
346
|
+
// The lookup of whether edit_file writes is still in flight when the page's document is replaced.
|
|
347
|
+
page.receive('<h1>Roadmap, v2</h1>');
|
|
348
|
+
await waitFor(() => expect(iframe.getAttribute('srcdoc')).toContain('Roadmap, v2'));
|
|
349
|
+
const changed = frameChannel(iframe);
|
|
350
|
+
server.releaseManual();
|
|
351
|
+
await settled();
|
|
352
|
+
|
|
353
|
+
expect(screen.queryByText(/wants to write to the knowledge base/)).toBeNull();
|
|
354
|
+
expect(server.mcp).toEqual([]);
|
|
355
|
+
expect(changed.replies).toEqual([]);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('does not run on an Allow the ended view gave', async () => {
|
|
359
|
+
const server = stubServer({ holdManual: true });
|
|
360
|
+
const page = onFilePage();
|
|
361
|
+
const iframe = await frameOf(page.container);
|
|
362
|
+
const frame = frameChannel(iframe);
|
|
363
|
+
// A chain asks without the manual: Allow it, so the view holds an Allow.
|
|
364
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'w1', fn: 'callToolChain', args: ['return 1'] });
|
|
365
|
+
fireEvent.click(await screen.findByRole('button', { name: 'Allow while open' }));
|
|
366
|
+
await waitFor(() => expect(frame.replies.at(-1)).toMatchObject({ requestId: 'w1', ok: true }));
|
|
367
|
+
|
|
368
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'w2', fn: 'callTool', args: ['edit_file', {}] });
|
|
369
|
+
page.receive('<h1>Roadmap, v2</h1>');
|
|
370
|
+
await waitFor(() => expect(iframe.getAttribute('srcdoc')).toContain('Roadmap, v2'));
|
|
371
|
+
server.releaseManual();
|
|
372
|
+
await settled();
|
|
373
|
+
|
|
374
|
+
expect(server.mcp).toEqual(['call_tool_chain']);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it('opens no card in the reloaded view and sends the server nothing when the viewer reloads first', async () => {
|
|
378
|
+
const server = stubServer({ holdManual: true });
|
|
379
|
+
const { container } = onFilePage();
|
|
380
|
+
const frame = frameChannel(await frameOf(container));
|
|
381
|
+
frame.post({ type: 'bevel.tool.call', requestId: 'w1', fn: 'callTool', args: ['edit_file', {}] });
|
|
382
|
+
|
|
383
|
+
fireEvent.click(screen.getByTitle('Reload preview'));
|
|
384
|
+
const reloaded = frameChannel(await waitFor(() => {
|
|
385
|
+
const el = container.querySelector('iframe')!;
|
|
386
|
+
if (el.contentWindow === null) throw new Error('not yet');
|
|
387
|
+
return el;
|
|
388
|
+
}));
|
|
389
|
+
server.releaseManual();
|
|
390
|
+
await settled();
|
|
391
|
+
|
|
392
|
+
expect(screen.queryByText(/wants to write to the knowledge base/)).toBeNull();
|
|
393
|
+
expect(server.mcp).toEqual([]);
|
|
394
|
+
// Neither document hears an answer meant for a view that is gone.
|
|
395
|
+
expect(frame.replies).toEqual([]);
|
|
396
|
+
expect(reloaded.replies).toEqual([]);
|
|
397
|
+
});
|
|
398
|
+
});
|
|
@@ -35,6 +35,14 @@ export interface BridgeRun {
|
|
|
35
35
|
* Answers once the frame's handlers and the promises they settle have run.
|
|
36
36
|
*/
|
|
37
37
|
deliver: (data: unknown, opts?: { fromParent?: boolean }) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Press `key` with focus inside the frame, as it reaches the window.
|
|
40
|
+
* `alreadyPrevented` is one a handler of the page's cancelled before the
|
|
41
|
+
* bridge's; `preventedAfter` one a handler the page added later cancelled
|
|
42
|
+
* after it, in the same dispatch. Answers once the dispatch is over and the
|
|
43
|
+
* task the bridge leaves behind has run.
|
|
44
|
+
*/
|
|
45
|
+
press: (key: string, opts?: { alreadyPrevented?: boolean; preventedAfter?: boolean }) => Promise<void>;
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
/**
|
|
@@ -44,7 +52,7 @@ export interface BridgeRun {
|
|
|
44
52
|
*/
|
|
45
53
|
export function runBridge(
|
|
46
54
|
page: { ids?: string[]; names?: string[]; namedControls?: string[] } = {},
|
|
47
|
-
opts: { toolBridge?: boolean } = {},
|
|
55
|
+
opts: { toolBridge?: boolean | { documentId: string; tools?: boolean } } = {},
|
|
48
56
|
): BridgeRun {
|
|
49
57
|
// No lib sources: a vm script cannot parse an `export`. The bridge is
|
|
50
58
|
// appended after the lib either way.
|
|
@@ -68,6 +76,7 @@ export function runBridge(
|
|
|
68
76
|
const names = new Set(page.names ?? []);
|
|
69
77
|
const controls = new Set(page.namedControls ?? []);
|
|
70
78
|
const messageHandlers: ((e: { source: unknown; data: unknown }) => void)[] = [];
|
|
79
|
+
const keyHandlers: ((e: { key: string; defaultPrevented: boolean }) => void)[] = [];
|
|
71
80
|
const parent = { postMessage: (msg: unknown) => posted.push(msg) };
|
|
72
81
|
// The frame's request timers, cleared when the test ends: a call a test
|
|
73
82
|
// leaves unanswered must not keep the worker alive for the runtime's
|
|
@@ -79,8 +88,10 @@ export function runBridge(
|
|
|
79
88
|
});
|
|
80
89
|
const ctx: Record<string, unknown> = {
|
|
81
90
|
// The frame's window: the tool bridge listens on it for the parent's answers.
|
|
82
|
-
addEventListener: (type: string, fn: (e:
|
|
83
|
-
|
|
91
|
+
addEventListener: (type: string, fn: (e: never) => void) => {
|
|
92
|
+
// One window, two event shapes: the type names which this listener takes.
|
|
93
|
+
if (type === 'message') messageHandlers.push(fn as (e: { source: unknown; data: unknown }) => void);
|
|
94
|
+
if (type === 'keydown') keyHandlers.push(fn as (e: { key: string; defaultPrevented: boolean }) => void);
|
|
84
95
|
},
|
|
85
96
|
setTimeout: (fn: () => void, ms?: number) => {
|
|
86
97
|
const t = setTimeout(() => {
|
|
@@ -136,5 +147,11 @@ export function runBridge(
|
|
|
136
147
|
for (const fn of messageHandlers) fn({ source, data });
|
|
137
148
|
await new Promise((r) => setTimeout(r, 0));
|
|
138
149
|
},
|
|
150
|
+
press: async (key, pressOpts = {}) => {
|
|
151
|
+
const event = { key, defaultPrevented: pressOpts.alreadyPrevented ?? false };
|
|
152
|
+
for (const fn of keyHandlers) fn(event);
|
|
153
|
+
if (pressOpts.preventedAfter) event.defaultPrevented = true;
|
|
154
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
155
|
+
},
|
|
139
156
|
};
|
|
140
157
|
}
|