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
3 changes: 3 additions & 0 deletions .github/workflows/deploy_pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ jobs:
yarn workspace a2ui-composer-shell run build --base-href=/composer/
yarn workspace lit-basic-catalog run build --base=/composer/samples/lit-basic-catalog/
yarn workspace react-basic-catalog run build --base=/composer/samples/react-basic-catalog/
yarn workspace react-slack-catalog run build --base=/composer/samples/react-slack-catalog/

# Explicitly pre-create all destination directories and use cp -r SOURCE/. DEST/ to prevent nesting traps
- name: Assemble Staging Directory (deploy_dist)
run: |
mkdir -p deploy_dist/samples/ng-basic-catalog
mkdir -p deploy_dist/samples/lit-basic-catalog
mkdir -p deploy_dist/samples/react-basic-catalog
mkdir -p deploy_dist/samples/react-slack-catalog

# Stage Shell
if [ -d "shell/dist/a2ui-composer/browser" ]; then
Expand All @@ -94,6 +96,7 @@ jobs:
# Stage Vite Samples
cp -r samples/lit-basic-catalog/dist/. deploy_dist/samples/lit-basic-catalog/
cp -r samples/react-basic-catalog/dist/. deploy_dist/samples/react-basic-catalog/
cp -r samples/react-slack-catalog/dist/. deploy_dist/samples/react-slack-catalog/

# Create Angular SPA fallback and bypass Jekyll
cp deploy_dist/index.html deploy_dist/404.html
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pr_preview_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ jobs:
yarn workspace a2ui-composer-shell run build --base-href=/composer/pr/${PR_NUM}/
yarn workspace lit-basic-catalog run build --base=/composer/pr/${PR_NUM}/samples/lit-basic-catalog/
yarn workspace react-basic-catalog run build --base=/composer/pr/${PR_NUM}/samples/react-basic-catalog/
yarn workspace react-slack-catalog run build --base=/composer/pr/${PR_NUM}/samples/react-slack-catalog/

- name: Assemble Staging Directory (deploy_dist)
run: |
mkdir -p deploy_dist/samples/ng-basic-catalog
mkdir -p deploy_dist/samples/lit-basic-catalog
mkdir -p deploy_dist/samples/react-basic-catalog
mkdir -p deploy_dist/samples/react-slack-catalog

if [ -d "shell/dist/a2ui-composer/browser" ]; then
cp -r shell/dist/a2ui-composer/browser/. deploy_dist/
Expand All @@ -97,6 +99,7 @@ jobs:

cp -r samples/lit-basic-catalog/dist/. deploy_dist/samples/lit-basic-catalog/
cp -r samples/react-basic-catalog/dist/. deploy_dist/samples/react-basic-catalog/
cp -r samples/react-slack-catalog/dist/. deploy_dist/samples/react-slack-catalog/

touch deploy_dist/.nojekyll

Expand Down
10 changes: 10 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
# limitations under the License.

nodeLinker: node-modules

# The Slack sample draws its preview with the Renderer component of
# @tightknitai/storybook-addon-slack-block-kit, which doesn't use Storybook at
# run time. The addon declares Storybook as a peer dependency for its Storybook
# panel, so mark that peer optional instead of installing Storybook.
packageExtensions:
'@tightknitai/storybook-addon-slack-block-kit@*':
peerDependenciesMeta:
storybook:
optional: true
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ flowchart TD
The ecosystem is architected as a modular, highly cohesive monorepo utilizing
**Yarn v4 Workspaces**:

| Workspace | Package Name | Description & Core Responsibilities |
| :------------- | :----------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`shell/`** | `a2ui-composer-shell` | Standalone web application hosting chat panel, live JSON editors, real-time iframe preview wrapper, debugging suite, interactive mock rules manager, and IndexedDB storage engines. |
| **`bridge/`** | `a2ui-bridge` | ESBuild-bundled lightweight cross-frame JavaScript library embedded inside child rendering iframes. |
| **`samples/`** | `ng-basic-catalog`<br>`lit-basic-catalog`<br>`react-basic-catalog` | Plug-and-play developer renderer sandbox applications demonstrating zero-boilerplate integration across Lit, Angular, and React rendering stacks. |
| Workspace | Package Name | Description & Core Responsibilities |
| :------------- | :------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`shell/`** | `a2ui-composer-shell` | Standalone web application hosting chat panel, live JSON editors, real-time iframe preview wrapper, debugging suite, interactive mock rules manager, and IndexedDB storage engines. |
| **`bridge/`** | `a2ui-bridge` | ESBuild-bundled lightweight cross-frame JavaScript library embedded inside child rendering iframes. |
| **`samples/`** | `ng-basic-catalog`<br>`lit-basic-catalog`<br>`react-basic-catalog`<br>`react-slack-catalog` | Plug-and-play developer renderer sandbox applications demonstrating zero-boilerplate integration across Lit, Angular, and React rendering stacks, plus a [Slack Block Kit preview](samples/react-slack-catalog/README.md) that needs no Slack workspace. |

## Getting Started

Expand All @@ -114,13 +114,18 @@ Ensure your local workspace is configured with the following dependencies:
# Install monorepo workspace dependencies via Yarn v4
yarn install

# Build the shared iframe bridge package used by sample renderer apps
yarn workspace a2ui-bridge build

# Launch one (or more) of the sample renderer apps. By default:
# ng-basic-catalog starts on localhost:3456
# lit-basic-catalog starts on localhost:3457
# react-basic-catalog starts on localhost:3458
# react-slack-catalog starts on localhost:3460
yarn --cwd samples/ng-basic-catalog start
yarn --cwd samples/lit-basic-catalog start
yarn --cwd samples/react-basic-catalog start
yarn --cwd samples/react-slack-catalog start

# Launch standalone interactive development shell on http://localhost:4200
yarn --cwd shell start
Expand All @@ -137,7 +142,19 @@ this case you'll get an error if you try to use the chat.
### Using A2UI Composer

After configuring the renderer app (see above) and your Gemini API key, you can
use the chat panel on the left to describe the interface you want created.
use the chat panel on the left to describe the interface you want created. The
renderer pill in the input toolbar opens the configured renderer choices. Choose
**Slack Block Kit Preview** to generate Slack-oriented A2UI, or an Angular, React,
or Lit renderer for standard A2UI.

You can also ask the assistant to change formats, for example: “Create a Slack
approval message with an Approve button.” The assistant can call a
`switchRenderer` function that Composer runs: it selects the renderer and waits
for its catalog before generating. Both controls
use the same selection. Switching renderer keeps a canvas you or the assistant
have changed, along with the conversation, and draws it with the new renderer; an
unchanged sample canvas is replaced by the new renderer's sample. Subsequent edits
use the selected catalog and current canvas.

Use **+ Add to prompt** in the input pill to attach files, include a screenshot of
the current canvas, or inspect the assistant's instructions. When screenshot
Expand All @@ -159,6 +176,8 @@ Once the A2UI JSON for your interface is rendered, you can:
- See console.log/warn/error and window.onerror messages in the Errors tab
- Follow all the details, including messages being sent to and received from the
LLM in the chat panel, by following the Raw Messages.
- Inspect or copy the generated Slack Block Kit from the Slack preview when the
Slack renderer is selected.

Once you are happy with the A2UI JSON, you can copy it and then integrate it as
a template in your Agent or Skill.
Expand Down
252 changes: 252 additions & 0 deletions bridge/src/preview-bridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,258 @@ describe('PreviewBridge Core API Runtime', () => {
);
});

describe('catalog requests across renderer lifecycles', () => {
it('does not start an HTML fallback after the renderer attaches its catalog', async () => {
vi.useFakeTimers();
vi.spyOn(console, 'warn').mockImplementation(() => {});
const postMessage = vi.spyOn(window.parent, 'postMessage');
let completeText!: (text: string) => void;
const pendingText = new Promise<string>(resolve => {
completeText = resolve;
});
const response = new Response();
vi.spyOn(response, 'text').mockReturnValue(pendingText);
window.fetch = vi.fn<typeof window.fetch>().mockResolvedValue(response);
window.dispatchEvent(
new MessageEvent('message', {
source: window.parent,
origin: window.location.origin,
data: {type: PreviewBridgeMessageType.GET_CATALOG},
}),
);
await vi.advanceTimersByTimeAsync(0);
expect(response.text).toHaveBeenCalledOnce();

bridge.attachRenderer(
{processMessages: vi.fn()},
{
surfaceGroup: {onSurfaceCreated: {subscribe: () => ({unsubscribe: vi.fn()})}},
onSurfaceReady: vi.fn(),
catalogJson: {catalogId: 'inline-catalog'},
},
);
completeText('<!doctype html><html>SPA fallback</html>');
await vi.runAllTimersAsync();

expect(window.fetch).toHaveBeenCalledExactlyOnceWith('catalog', expect.any(Object));
// The host's request is still unanswered, so it gets the attached renderer's catalog.
expect(
postMessage.mock.calls.filter(
([message]) => message.type === PreviewBridgeMessageType.A2UI_CATALOG,
),
).toEqual([
[
{type: PreviewBridgeMessageType.A2UI_CATALOG, payload: {catalogId: 'inline-catalog'}},
window.location.origin,
],
]);
});

it.each([
{startAttached: false, responseStatus: 200},
{startAttached: false, responseStatus: 404},
{startAttached: true, responseStatus: 200},
{startAttached: true, responseStatus: 404},
])(
'ignores a stale $responseStatus response after attachment (previous renderer: $startAttached)',
async ({startAttached, responseStatus}) => {
vi.useFakeTimers();
vi.spyOn(console, 'warn').mockImplementation(() => {});
const postMessage = vi.spyOn(window.parent, 'postMessage');
const surfaceGroup: SurfaceGroupLike = {
onSurfaceCreated: {subscribe: () => ({unsubscribe: vi.fn()})},
};
let completeFetch!: () => void;
const pendingFetch = new Promise<void>(resolve => {
completeFetch = resolve;
});
window.fetch = vi.fn<typeof window.fetch>().mockImplementation(async () => {
await pendingFetch;
return new Response(JSON.stringify({catalogId: 'stale-catalog'}), {
status: responseStatus,
});
});
const requestCatalog = () => {
window.dispatchEvent(
new MessageEvent('message', {
source: window.parent,
origin: window.location.origin,
data: {type: PreviewBridgeMessageType.GET_CATALOG},
}),
);
};
if (startAttached) {
bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn()},
);
}
requestCatalog();
expect(window.fetch).toHaveBeenCalledOnce();

const catalog = {catalogId: 'current-catalog', components: {}};
const onCatalogResolved = vi.fn();
bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn(), catalogJson: catalog, onCatalogResolved},
);
requestCatalog();
await vi.advanceTimersByTimeAsync(0);
completeFetch();
await vi.runAllTimersAsync();

const catalogMessages = postMessage.mock.calls.filter(
([message]) => message.type === PreviewBridgeMessageType.A2UI_CATALOG,
);
expect(catalogMessages).toEqual([
[{type: PreviewBridgeMessageType.A2UI_CATALOG, payload: catalog}, window.location.origin],
]);
expect(onCatalogResolved).toHaveBeenCalledExactlyOnceWith('current-catalog');
},
);

it("answers with the new renderer's catalog when the renderer changes mid-fetch", async () => {
vi.useFakeTimers();
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const postMessage = vi.spyOn(window.parent, 'postMessage');
const surfaceGroup: SurfaceGroupLike = {
onSurfaceCreated: {subscribe: () => ({unsubscribe: vi.fn()})},
};
let completeFetch!: () => void;
const pendingFetch = new Promise<void>(resolve => {
completeFetch = resolve;
});
window.fetch = vi.fn<typeof window.fetch>().mockImplementation(async () => {
await pendingFetch;
return new Response(JSON.stringify({catalogId: 'stale-catalog'}));
});
bridge.attachRenderer({processMessages: vi.fn()}, {surfaceGroup, onSurfaceReady: vi.fn()});
window.dispatchEvent(
new MessageEvent('message', {
source: window.parent,
origin: window.location.origin,
data: {type: PreviewBridgeMessageType.GET_CATALOG},
}),
);

// The host still waits for the first reply, so it doesn't request again when
// the replacement renderer announces itself.
const catalog = {catalogId: 'current-catalog', components: {}};
bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn(), catalogJson: catalog},
);
completeFetch();
await vi.runAllTimersAsync();

expect(
postMessage.mock.calls.filter(
([message]) => message.type === PreviewBridgeMessageType.A2UI_CATALOG,
),
).toEqual([
[{type: PreviewBridgeMessageType.A2UI_CATALOG, payload: catalog}, window.location.origin],
]);
expect(warn).toHaveBeenCalledWith(
"PreviewBridge: The renderer changed while its catalog was loading. Requesting the new renderer's catalog.",
);
});

it.each(['detach', 'destroy'] as const)(
'does not publish an in-flight catalog after %s',
async lifecycle => {
vi.useFakeTimers();
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const postMessage = vi.spyOn(window.parent, 'postMessage');
const connection = bridge.attachRenderer(
{processMessages: vi.fn()},
{
surfaceGroup: {onSurfaceCreated: {subscribe: () => ({unsubscribe: vi.fn()})}},
onSurfaceReady: vi.fn(),
catalogJson: {catalogId: 'detached-catalog'},
},
);
window.dispatchEvent(
new MessageEvent('message', {
source: window.parent,
origin: window.location.origin,
data: {type: PreviewBridgeMessageType.GET_CATALOG},
}),
);
if (lifecycle === 'detach') {
connection.unsubscribe();
} else {
bridge.destroy();
}
await vi.runAllTimersAsync();

expect(
postMessage.mock.calls.filter(
([message]) => message.type === PreviewBridgeMessageType.A2UI_CATALOG,
),
).toEqual([]);
// A detached renderer leaves the request for the next renderer to answer, so
// it's reported. A destroyed bridge has stopped listening, so there's no one
// to report to.
const owed =
'PreviewBridge: The renderer detached while its catalog was loading. The next renderer to attach will send its catalog.';
if (lifecycle === 'detach') {
expect(warn).toHaveBeenCalledWith(owed);
} else {
expect(warn).not.toHaveBeenCalledWith(owed);
}
},
);

it("answers with the next renderer's catalog when a renderer detaches mid-request", async () => {
vi.useFakeTimers();
vi.spyOn(console, 'warn').mockImplementation(() => {});
const postMessage = vi.spyOn(window.parent, 'postMessage');
const surfaceGroup: SurfaceGroupLike = {
onSurfaceCreated: {subscribe: () => ({unsubscribe: vi.fn()})},
};
const catalogMessages = () =>
postMessage.mock.calls.filter(
([message]) => message.type === PreviewBridgeMessageType.A2UI_CATALOG,
);
// A React StrictMode double mount or a hot swap detaches the first renderer
// before the second one attaches.
const first = bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn(), catalogJson: {catalogId: 'first-catalog'}},
);
window.dispatchEvent(
new MessageEvent('message', {
source: window.parent,
origin: window.location.origin,
data: {type: PreviewBridgeMessageType.GET_CATALOG},
}),
);
first.unsubscribe();
await vi.runAllTimersAsync();
expect(catalogMessages()).toEqual([]);

// The host ignores the second renderer's RENDERER_READY while it waits, so
// it never asks again.
const catalog = {catalogId: 'second-catalog', components: {}};
bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn(), catalogJson: catalog},
);
await vi.runAllTimersAsync();
expect(catalogMessages()).toEqual([
[{type: PreviewBridgeMessageType.A2UI_CATALOG, payload: catalog}, window.location.origin],
]);

// Once answered, the request is not answered again.
bridge.attachRenderer(
{processMessages: vi.fn()},
{surfaceGroup, onSurfaceReady: vi.fn(), catalogJson: {catalogId: 'third-catalog'}},
);
await vi.runAllTimersAsync();
expect(catalogMessages()).toHaveLength(1);
});
});

it('responds with COMPONENT_USAGES containing usages from getComponentUsages callback (async)', async () => {
const spy = vi.spyOn(window.parent, 'postMessage');
const mockUsages = {
Expand Down
Loading
Loading