Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
font-family: var(--openbitfun-type-heading-section-font-family);
font-size: var(--openbitfun-type-heading-section-font-size);
font-weight: var(--openbitfun-type-heading-section-font-weight);
line-height: var(--openbitfun-line-height-snug);
line-height: var(--openbitfun-type-modifier-leading-snug-line-height);
letter-spacing: var(--openbitfun-type-heading-section-letter-spacing);
white-space: normal;
overflow-wrap: anywhere;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';

import { buildDesktopCardSrcDoc, DESKTOP_CARD_WIDGET_CSS } from './DesktopCardCustomFrame';
import { buildDesktopCardSrcDoc, DESKTOP_CARD_WIDGET_CSS } from './desktopCardSrcDoc';

const TOOL_SOURCE = readFileSync(
new URL('../../../../../../src/crates/assembly/core/src/agentic/tools/implementations/desktop_card_tool.rs', import.meta.url),
Expand Down
117 changes: 2 additions & 115 deletions src/web-ui/src/app/components/DesktopCard/DesktopCardCustomFrame.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { widgetAppearanceAdapter } from '@/infrastructure/appearance/adapters/WidgetAppearanceAdapter';
import {
createWidgetAppearanceFallbackCss,
readWidgetAppearancePayload,
} from '@/tools/generative-widget/appearancePayload';

const FRAME_MESSAGE_SOURCE = 'openbitfun-desktop-card';
const HOST_MESSAGE_SOURCE = 'openbitfun-desktop-card-host';

export interface DesktopCardCustomPayload {
data: unknown;
card: { id: string; title: string; subtitle?: string };
error?: string;
}
import { readWidgetAppearancePayload } from '@/tools/generative-widget/appearancePayload';
import { buildDesktopCardSrcDoc, FRAME_MESSAGE_SOURCE, HOST_MESSAGE_SOURCE, type DesktopCardCustomPayload, type DesktopCardWidgetAppearance } from './desktopCardSrcDoc';

interface DesktopCardCustomFrameProps {
html: string;
Expand All @@ -21,113 +10,11 @@ interface DesktopCardCustomFrameProps {
onOpenLink: (url: string) => void;
}

// The widget runs in an opaque-origin sandbox and talks to the card only through this bridge.
const BRIDGE_SCRIPT = `(function () {
var handlers = [];
var latest = null;
function deliver(handler) {
try { handler(latest); } catch (error) { console.error(error); }
}
window.openbitfunCard = {
onData: function (handler) {
if (typeof handler !== 'function') return;
handlers.push(handler);
if (latest) deliver(handler);
},
openLink: function (url) {
parent.postMessage({ source: '${FRAME_MESSAGE_SOURCE}', type: 'open-link', url: String(url) }, '*');
}
};
window.addEventListener('message', function (event) {
if (event.source !== parent) return;
var message = event.data;
if (!message || message.source !== '${HOST_MESSAGE_SOURCE}' || message.type !== 'data') return;
latest = message.payload;
handlers.forEach(deliver);
});
parent.postMessage({ source: '${FRAME_MESSAGE_SOURCE}', type: 'ready' }, '*');
})();`;

export const DESKTOP_CARD_WIDGET_CSS = `
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; width: 100%; height: 100%; background: transparent; overflow: auto; }
html { scrollbar-width: thin; scrollbar-color: var(--openbitfun-color-scrollbar-thumb) transparent; }
body {
color: var(--openbitfun-color-content-primary);
font-family: var(--openbitfun-type-body-sm-font-family);
font-size: var(--openbitfun-type-label-md-font-size);
line-height: var(--openbitfun-type-body-sm-line-height);
}
a { color: var(--openbitfun-color-accent-default); text-decoration: none; }
img, svg, canvas { max-width: 100%; height: auto; }
.openbitfun-card-stack { display: flex; flex-direction: column; gap: var(--openbitfun-space-2); }
.openbitfun-card-row { display: flex; align-items: center; justify-content: space-between; gap: var(--openbitfun-space-3); min-width: 0; }
.openbitfun-card-value {
font-family: var(--openbitfun-type-heading-page-font-family);
font-size: var(--openbitfun-type-heading-page-font-size);
font-weight: var(--openbitfun-type-heading-page-font-weight);
line-height: var(--openbitfun-type-heading-page-line-height);
font-variant-numeric: tabular-nums;
}
.openbitfun-card-muted { color: var(--openbitfun-color-content-secondary); font-size: var(--openbitfun-type-body-sm-font-size); }
.openbitfun-card-truncate { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.openbitfun-card-list { display: flex; flex-direction: column; margin: 0; padding: 0; list-style: none; }
.openbitfun-card-item {
display: flex;
align-items: center;
gap: var(--openbitfun-space-2);
min-width: 0;
padding: var(--openbitfun-space-1) var(--openbitfun-space-1);
border-radius: var(--openbitfun-radius-sm);
}
.openbitfun-card-item > .openbitfun-card-truncate { flex: 1 1 auto; }
.openbitfun-card-list > .openbitfun-card-item + .openbitfun-card-item { border-top: var(--openbitfun-border-width-default) solid var(--openbitfun-color-border-subtle); }
.openbitfun-card-item[role='link'], .openbitfun-card-item[data-url] { cursor: pointer; }
.openbitfun-card-item[role='link']:hover, .openbitfun-card-item[data-url]:hover { background: var(--openbitfun-color-action-quiet-hover); }
.openbitfun-card-good { color: var(--openbitfun-color-status-success-content); }
.openbitfun-card-warn { color: var(--openbitfun-color-status-warning-content); }
.openbitfun-card-bad { color: var(--openbitfun-color-status-danger-content); }
`;

export interface DesktopCardWidgetAppearance {
mode?: string;
vars: Record<string, string>;
}

function escapeStyleText(value: string): string {
return value.replace(/<\/style/gi, '<\\/style');
}

function readAppearance(): DesktopCardWidgetAppearance {
const payload = readWidgetAppearancePayload();
return { mode: payload?.mode, vars: payload?.vars ?? {} };
}

export function buildDesktopCardSrcDoc(html: string, appearance: DesktopCardWidgetAppearance): string {
const themeCss = Object.entries(appearance.vars)
.filter(([name, value]) => /^--[a-z0-9-]+$/i.test(name) && !/[;{}<>]/.test(value))
.map(([name, value]) => `${name}: ${value};`)
.join(' ');
const mode = appearance.mode === 'light' || appearance.mode === 'dark' ? appearance.mode : undefined;
// A color-scheme that differs from the card window's makes Chromium (WebView2)
// paint an opaque light canvas and light scrollbars behind the widget.
return `<!DOCTYPE html>
<html${mode ? ` data-openbitfun-appearance-mode="${mode}"` : ''}>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: https:; font-src data:; media-src data: https:" />
<style>:root {
color-scheme: ${mode ?? 'light dark'};
${createWidgetAppearanceFallbackCss()}
${escapeStyleText(themeCss)}
}
${DESKTOP_CARD_WIDGET_CSS}</style>
<script>${BRIDGE_SCRIPT}</script>
</head>
<body>${html}</body>
</html>`;
}

export function DesktopCardCustomFrame({ html, title, payload, onOpenLink }: DesktopCardCustomFrameProps) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const payloadRef = useRef(payload);
Expand Down
112 changes: 112 additions & 0 deletions src/web-ui/src/app/components/DesktopCard/desktopCardSrcDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { createWidgetAppearanceFallbackCss } from '@/tools/generative-widget/appearancePayload';

export const FRAME_MESSAGE_SOURCE = 'openbitfun-desktop-card';
export const HOST_MESSAGE_SOURCE = 'openbitfun-desktop-card-host';

export interface DesktopCardCustomPayload {
data: unknown;
card: { id: string; title: string; subtitle?: string };
error?: string;
}

// The widget runs in an opaque-origin sandbox and talks to the card only through this bridge.
const BRIDGE_SCRIPT = `(function () {
var handlers = [];
var latest = null;
function deliver(handler) {
try { handler(latest); } catch (error) { console.error(error); }
}
window.openbitfunCard = {
onData: function (handler) {
if (typeof handler !== 'function') return;
handlers.push(handler);
if (latest) deliver(handler);
},
openLink: function (url) {
parent.postMessage({ source: '${FRAME_MESSAGE_SOURCE}', type: 'open-link', url: String(url) }, '*');
}
};
window.addEventListener('message', function (event) {
if (event.source !== parent) return;
var message = event.data;
if (!message || message.source !== '${HOST_MESSAGE_SOURCE}' || message.type !== 'data') return;
latest = message.payload;
handlers.forEach(deliver);
});
parent.postMessage({ source: '${FRAME_MESSAGE_SOURCE}', type: 'ready' }, '*');
})();`;

export const DESKTOP_CARD_WIDGET_CSS = `
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; width: 100%; height: 100%; background: transparent; overflow: auto; }
html { scrollbar-width: thin; scrollbar-color: var(--openbitfun-color-scrollbar-thumb) transparent; }
body {
color: var(--openbitfun-color-content-primary);
font-family: var(--openbitfun-type-body-sm-font-family);
font-size: var(--openbitfun-type-label-md-font-size);
line-height: var(--openbitfun-type-body-sm-line-height);
}
a { color: var(--openbitfun-color-accent-default); text-decoration: none; }
img, svg, canvas { max-width: 100%; height: auto; }
.openbitfun-card-stack { display: flex; flex-direction: column; gap: var(--openbitfun-space-2); }
.openbitfun-card-row { display: flex; align-items: center; justify-content: space-between; gap: var(--openbitfun-space-3); min-width: 0; }
.openbitfun-card-value {
font-family: var(--openbitfun-type-heading-page-font-family);
font-size: var(--openbitfun-type-heading-page-font-size);
font-weight: var(--openbitfun-type-heading-page-font-weight);
line-height: var(--openbitfun-type-heading-page-line-height);
font-variant-numeric: tabular-nums;
}
.openbitfun-card-muted { color: var(--openbitfun-color-content-secondary); font-size: var(--openbitfun-type-body-sm-font-size); }
.openbitfun-card-truncate { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.openbitfun-card-list { display: flex; flex-direction: column; margin: 0; padding: 0; list-style: none; }
.openbitfun-card-item {
display: flex;
align-items: center;
gap: var(--openbitfun-space-2);
min-width: 0;
padding: var(--openbitfun-space-1) var(--openbitfun-space-1);
border-radius: var(--openbitfun-radius-sm);
}
.openbitfun-card-item > .openbitfun-card-truncate { flex: 1 1 auto; }
.openbitfun-card-list > .openbitfun-card-item + .openbitfun-card-item { border-top: var(--openbitfun-border-width-default) solid var(--openbitfun-color-border-subtle); }
.openbitfun-card-item[role='link'], .openbitfun-card-item[data-url] { cursor: pointer; }
.openbitfun-card-item[role='link']:hover, .openbitfun-card-item[data-url]:hover { background: var(--openbitfun-color-action-quiet-hover); }
.openbitfun-card-good { color: var(--openbitfun-color-status-success-content); }
.openbitfun-card-warn { color: var(--openbitfun-color-status-warning-content); }
.openbitfun-card-bad { color: var(--openbitfun-color-status-danger-content); }
`;

export interface DesktopCardWidgetAppearance {
mode?: string;
vars: Record<string, string>;
}

function escapeStyleText(value: string): string {
return value.replace(/<\/style/gi, '<\\/style');
}

export function buildDesktopCardSrcDoc(html: string, appearance: DesktopCardWidgetAppearance): string {
const themeCss = Object.entries(appearance.vars)
.filter(([name, value]) => /^--[a-z0-9-]+$/i.test(name) && !/[;{}<>]/.test(value))
.map(([name, value]) => `${name}: ${value};`)
.join(' ');
const mode = appearance.mode === 'light' || appearance.mode === 'dark' ? appearance.mode : undefined;
// A color-scheme that differs from the card window's makes Chromium (WebView2)
// paint an opaque light canvas and light scrollbars behind the widget.
return `<!DOCTYPE html>
<html${mode ? ` data-openbitfun-appearance-mode="${mode}"` : ''}>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: https:; font-src data:; media-src data: https:" />
<style>:root {
color-scheme: ${mode ?? 'light dark'};
${createWidgetAppearanceFallbackCss()}
${escapeStyleText(themeCss)}
}
${DESKTOP_CARD_WIDGET_CSS}</style>
<script>${BRIDGE_SCRIPT}</script>
</head>
<body>${html}</body>
</html>`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import { getChildSessionBadge } from '../sessions/sessionRowBadges';
import { resolveSessionRelationship } from '@/flow_chat/utils/sessionMetadata';
import { sessionBelongsToWorkspaceNavRow } from '@/flow_chat/utils/sessionOrdering';
import type { SidebarDeviceSlot } from './useSidebarDevices';
import { CatalogSessionMenu, catalogSessionActionKey, runCatalogSessionAction } from './CatalogSessionMenu';
import { CatalogSessionMenu } from './CatalogSessionMenu';
import { catalogSessionActionKey, runCatalogSessionAction } from './catalogSessionActions';

export interface BackgroundActions {
pendingKey: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,10 @@ import { sessionWorktreeRootPath } from '@/flow_chat/utils/sessionWorktree';
import { useSideAnchoredPopoverPosition } from '@/shared/utils/useSideAnchoredPopoverPosition';
import ArchivedSessionMenuItems from '../sessions/ArchivedSessionMenuItems';
import SessionDeleteMenuItem from '../sessions/SessionDeleteMenuItem';
import { activateDeviceSlot, runDeviceSessionAction, type DeviceSessionAction } from './openOnDevice';
import { activateDeviceSlot, type DeviceSessionAction } from './openOnDevice';
import type { BackgroundActions } from './BackgroundSessionList';
import type { SidebarDeviceSlot } from './useSidebarDevices';

/** Intent key of a catalog session action; the row shows progress while it runs. */
export const catalogSessionActionKey = (sessionId: string, kind: string) => `session-action:${sessionId}:${kind}`;

const FAILURE_KEYS: Partial<Record<DeviceSessionAction['kind'], string>> = {
duplicate: 'nav.sessions.duplicateFailed',
'open-worktree': 'nav.sessions.openWorktreeWorkspaceFailed',
'reveal-storage': 'nav.sessions.openStorageDirectoryFailed',
};

export function runCatalogSessionAction(
actions: BackgroundActions,
peer: ReturnType<typeof usePeerDeviceModeOptional>,
slot: SidebarDeviceSlot,
sessionId: string,
action: DeviceSessionAction,
t: (key: string, options?: Record<string, unknown>) => string,
): void {
actions.run(
catalogSessionActionKey(sessionId, action.kind),
() => runDeviceSessionAction(peer, slot, sessionId, action, t),
FAILURE_KEYS[action.kind] ?? 'nav.devices.sessionActionFailed',
);
}
import { catalogSessionActionKey, runCatalogSessionAction } from './catalogSessionActions';

export const CatalogSessionMenu: React.FC<{
slot: SidebarDeviceSlot;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { usePeerDeviceModeOptional } from '@/infrastructure/peer-device/peerDeviceContextState';
import type { BackgroundActions } from './BackgroundSessionList';
import type { SidebarDeviceSlot } from './useSidebarDevices';
import { runDeviceSessionAction, type DeviceSessionAction } from './openOnDevice';

/** Intent key of a catalog session action; the row shows progress while it runs. */
export const catalogSessionActionKey = (sessionId: string, kind: string) => `session-action:${sessionId}:${kind}`;

const FAILURE_KEYS: Partial<Record<DeviceSessionAction['kind'], string>> = {
duplicate: 'nav.sessions.duplicateFailed',
'open-worktree': 'nav.sessions.openWorktreeWorkspaceFailed',
'reveal-storage': 'nav.sessions.openStorageDirectoryFailed',
};

export function runCatalogSessionAction(
actions: BackgroundActions,
peer: ReturnType<typeof usePeerDeviceModeOptional>,
slot: SidebarDeviceSlot,
sessionId: string,
action: DeviceSessionAction,
t: (key: string, options?: Record<string, unknown>) => string,
): void {
actions.run(
catalogSessionActionKey(sessionId, action.kind),
() => runDeviceSessionAction(peer, slot, sessionId, action, t),
FAILURE_KEYS[action.kind] ?? 'nav.devices.sessionActionFailed',
);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import React, { createContext, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState } from 'react';
import React, { useCallback, useContext, useLayoutEffect, useMemo, useRef, useState } from 'react';
import type { EditorGroupId } from './types';
import { CanvasStoreModeContext } from './stores';

type ToolbarEntry = { owner: symbol; content: React.ReactNode };
type ToolbarRegistry = Partial<Record<EditorGroupId, ToolbarEntry>>;
const RegistrationContext = createContext<{
register: (id: EditorGroupId, owner: symbol, content: React.ReactNode) => void;
remove: (id: EditorGroupId, owner: symbol) => void;
} | null>(null);
const ContentContext = createContext<ToolbarRegistry>({});
import { RegistrationContext, ContentContext, type ToolbarRegistry } from './canvasToolbarContext';

/** A host chooses toolbar placement; canvas groups continue to own tab behavior. */
export function CanvasToolbarProvider({ children }: { children: React.ReactNode }) {
Expand All @@ -31,11 +24,6 @@ export function CanvasToolbarProvider({ children }: { children: React.ReactNode
</RegistrationContext.Provider>;
}

export function useExternalCanvasToolbar(enabled: boolean) {
const registration = useContext(RegistrationContext);
return enabled && registration !== null;
}

export function CanvasToolbarContribution({ groupId, children }: {
groupId: EditorGroupId; children: React.ReactNode;
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import React, { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from 'react';
import { Toolbar } from '@openbitfun/ui';
import { CanvasToolbarContribution, useExternalCanvasToolbar } from './CanvasToolbar';
import { CanvasToolbarContribution } from './CanvasToolbar';
import { useExternalCanvasToolbar } from './canvasToolbarContext';
import { EditorArea } from './editor-area';
import { AnchorZone } from './anchor-zone';
import { MissionControl } from './mission-control';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createContext, useContext, type ReactNode } from 'react';
import type { EditorGroupId } from './types';

type ToolbarEntry = { owner: symbol; content: ReactNode };
export type ToolbarRegistry = Partial<Record<EditorGroupId, ToolbarEntry>>;
export const RegistrationContext = createContext<{
register: (id: EditorGroupId, owner: symbol, content: ReactNode) => void;
remove: (id: EditorGroupId, owner: symbol) => void;
} | null>(null);
export const ContentContext = createContext<ToolbarRegistry>({});

export function useExternalCanvasToolbar(enabled: boolean) {
const registration = useContext(RegistrationContext);
return enabled && registration !== null;
}
Loading
Loading