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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ jobs:
# The app's real-browser tests: a running step's words
# under reduced motion, where onboarding's Back lands beside the Mac window's buttons, the name
# step's orb drawn whole under the step dashes, a conversation's rail of turns clear of the
# Bot's text, one frame around an interface drawn in a Card, the page under the update banner
# Bot's text, one frame around an interface drawn in a Card, a Business Intelligence Bot's
# headline figure and bar chart drawn from the standard components and redrawn in place by a
# follow-up, the page under the update banner
# holding still as an update gets ready, Home's message box holding still while the
# microphone's notice shows, a reply staying where it was written once its turn is over,
# every skin's top-left corner clear of the Mac window's buttons, OpenDots' Bots badge on its
Expand Down Expand Up @@ -459,6 +461,7 @@ jobs:
- run: |
# One file per line, sorted: pull requests that each add a file then merge cleanly.
files=(
app/tests/a2ui-bi-answer.test.tsx
app/tests/a2ui-frame.test.tsx
app/tests/account-row-layout.test.tsx
app/tests/admin-people-remove-layout.test.tsx
Expand Down
321 changes: 321 additions & 0 deletions app/tests/a2ui-bi-answer.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
import type { Message } from "@ag-ui/core";

/**
* A Business Intelligence Bot's answer, as a Bot sends it today: one interface drawn from the
* standard components alone (a headline figure in a Card, and a Chart), then two or three sentences
* on what changed. Read by a2ui-bi-answer.fixture.tsx, which draws it, and a2ui-bi-answer.test.tsx,
* which checks what it draws.
*
* The figures are the sample dataset's (examples/business-intelligence/sample-data, "Expected
* answers"), all in BOOKINGS: every figure the card shows, and every one the test expects, follows from
* them. The sentences are beside them, since they name figures too.
*/
export const BOOKINGS = {
metric: "Bookings",
quarter: "Q3 2026",
previous: "Q2 2026",
period: "Jul 1 to Sep 30, 2026",
definition: "Closed-won ACV, USD",
source: "Sample RevOps data (opportunities)",
/** Largest first, by this quarter: the chart ranks them in this order. */
byRegion: [
{ region: "North America", q3: 4_040_200, q2: 4_250_100 },
{ region: "EMEA", q3: 3_675_000, q2: 2_620_000 },
{ region: "APAC", q3: 1_660_000, q2: 1_679_800 },
{ region: "LATAM", q3: 839_900, q2: 750_000 },
],
/** This quarter's months, in order. They add up to the quarter's total. */
byMonth: [
{ month: "Jul", bookings: 2_125_700 },
{ month: "Aug", bookings: 3_676_600 },
{ month: "Sep", bookings: 4_412_800 },
],
question: "What were bookings by region last quarter, and what changed?",
answer:
"Q3 2026 bookings came to $10.2M, up 9.8% on Q2. EMEA drove the gain, up 40.3% to $3.7M on two large Privileged Access deals, while North America slipped 4.9% after two enterprise deals moved into Q4. APAC held flat and LATAM grew 12%.",
followUp: "Show me that by month instead.",
followUpAnswer:
"Bookings rose every month of the quarter, from $2.1M in July to $4.4M in September, when 44 deals closed.",
} as const;

const money = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
notation: "compact",
maximumFractionDigits: 1,
});

const sum = (values: readonly number[]) =>
values.reduce((total, value) => total + value, 0);

const total = sum(BOOKINGS.byRegion.map((row) => row.q3));
const before = sum(BOOKINGS.byRegion.map((row) => row.q2));
const change = (total / before - 1) * 100;

/** What the headline card says, worked out from BOOKINGS. */
export const KPI = {
label: `${BOOKINGS.metric}, ${BOOKINGS.quarter}`,
figure: money.format(total),
change: `${change >= 0 ? "+" : "−"}${Math.abs(change).toFixed(1)}%`,
comparison: `vs ${BOOKINGS.previous} (${money.format(before)})`,
caption: `${BOOKINGS.period} · ${BOOKINGS.definition} · Source: ${BOOKINGS.source}`,
rising: change >= 0,
};

export const SURFACE = "bookings";
/** The id CopilotKit's runtime gives a surface's activity, `generativeUISurfaceMessageId`. */
export const SURFACE_MESSAGE = `genui:${SURFACE}`;

export const REGION_CHART_TITLE = `${BOOKINGS.metric} by region`;
export const MONTH_CHART_TITLE = `${BOOKINGS.metric} by month, ${BOOKINGS.quarter}`;

type Component = Record<string, unknown> & { id: string; component: string };
type Operation = Record<string, unknown> & { version: "1.0" };

/**
* The top of a chart's value axis: four equal steps of 1, 2 or 5 in a power of ten, the first that
* reaches the largest figure.
*
* The axis is given both ends because the standard Chart leaves them to Recharts' "auto": a bar
* chart whose figures are close together (three months of $4M to $5M) then starts its bars at $4M,
* which makes a 23% rise look like a fivefold one. And its axis labels round to whole millions, so
* a step of $1.5M reads "$2M"; whole steps keep every label true.
*/
function axisTop(values: readonly number[]): number {
const largest = Math.max(...values);
const magnitude = 10 ** Math.floor(Math.log10(largest / 4));
const step =
[1, 2, 5, 10]
.map((unit) => unit * magnitude)
.find((s) => s * 4 >= largest) ?? largest / 4;
return step * 4;
}

/** The chart of the first answer: regions lying down, largest first, this quarter beside last. */
const REGION_CHART: Component = {
id: "chart",
component: "Chart",
title: REGION_CHART_TITLE,
chartType: "bar",
layout: "horizontal",
xKey: "region",
series: [
{ key: "q3", label: BOOKINGS.quarter },
{ key: "q2", label: BOOKINGS.previous },
],
data: { "@path": "/rows" },
format: "currency",
currency: "USD",
// Room for "North America" on one line: at the default 96px it wraps to two.
yAxis: {
min: 0,
max: axisTop(BOOKINGS.byRegion.flatMap((row) => [row.q3, row.q2])),
width: 112,
},
height: 240,
};

/** The chart the follow-up asks for: this quarter's months, standing up, in order. */
const MONTH_CHART: Component = {
id: "chart",
component: "Chart",
title: MONTH_CHART_TITLE,
chartType: "bar",
layout: "vertical",
xKey: "month",
series: [{ key: "bookings", label: BOOKINGS.quarter }],
data: { "@path": "/rows" },
format: "currency",
currency: "USD",
yAxis: {
min: 0,
max: axisTop(BOOKINGS.byMonth.map((row) => row.bookings)),
},
height: 240,
showLegend: false,
};

/** The headline figure and the chart, in one Card: the Card is the answer's one frame. */
const ANSWER: Component[] = [
{ id: "root", component: "Card", child: "body" },
{ id: "body", component: "Column", children: ["kpi", "rule", "chart"] },
{
id: "kpi",
component: "Column",
gap: 1,
children: ["label", "figure", "change", "caption"],
},
{
id: "label",
component: "Text",
text: KPI.label,
size: "sm",
color: "secondary",
},
{
id: "figure",
component: "Title",
text: KPI.figure,
size: "3xl",
tabularNums: true,
},
{
id: "change",
component: "Row",
align: "center",
gap: 2,
children: ["delta", "against"],
},
{
id: "delta",
component: "Badge",
label: KPI.change,
color: KPI.rising ? "success" : "danger",
size: "sm",
},
{
id: "against",
component: "Text",
text: KPI.comparison,
size: "sm",
color: "secondary",
},
{
id: "caption",
component: "Text",
text: KPI.caption,
size: "xs",
color: "secondary",
},
{ id: "rule", component: "Divider" },
REGION_CHART,
];

/** The surface's operations, as its activity holds them once the first answer has drawn. */
function answerOperations(catalogId: string): Operation[] {
return [
{
version: "1.0",
createSurface: {
surfaceId: SURFACE,
catalogId,
dataModel: { rows: BOOKINGS.byRegion },
},
},
{
version: "1.0",
updateComponents: { surfaceId: SURFACE, components: ANSWER },
},
];
}

/**
* What the follow-up adds: the same surface, its chart swapped and its rows replaced. The runtime
* appends these to the surface's activity (`ACTIVITY_DELTA`, `add /operations/-`), so the answer is
* redrawn where it is rather than drawn a second time under the question.
*/
const FOLLOW_UP_OPERATIONS: Operation[] = [
{
version: "1.0",
updateComponents: { surfaceId: SURFACE, components: [MONTH_CHART] },
},
{
version: "1.0",
updateDataModel: {
surfaceId: SURFACE,
path: "/rows",
value: BOOKINGS.byMonth,
},
},
];

function surface(operations: Operation[], catalogId: string): Message {
return {
id: SURFACE_MESSAGE,
role: "activity",
activityType: "generative-ui-surface",
content: {
surfaceId: SURFACE,
target: "chat",
catalogId,
status: "ready",
rejected: [],
operations,
},
};
}

/** A `render_ui` call and the runtime's answer to it, as the thread stores them. */
function drawing(
id: string,
operations: Operation[],
): { call: Message; result: Message } {
return {
call: {
id: `${id}-call`,
role: "assistant",
content: "",
toolCalls: [
{
id,
type: "function",
function: {
name: "render_ui",
arguments: JSON.stringify({ target: "chat", operations }),
},
},
],
},
result: {
id: `${id}-result`,
role: "tool",
toolCallId: id,
content: JSON.stringify({
status: "rendered",
applied: operations.length,
calls: 0,
rejected: [],
}),
},
};
}

/**
* The conversation, as its thread stores it.
*
* - `"answer"`: the question, the Bot drawing its answer, and its sentences.
* - `"follow-up"`: then the follow-up and the Bot redrawing the same surface. The surface's activity
* stays where it was first drawn and holds every operation, the follow-up's appended to the
* answer's, which is how the runtime's deltas leave it in the store.
*/
export function conversation(
stage: "answer" | "follow-up",
catalogId: string,
): Message[] {
const first = answerOperations(catalogId);
const drawn = drawing("draw-answer", first);
const opening: Message[] = [
{ id: "ask", role: "user", content: BOOKINGS.question },
drawn.call,
];
const reply: Message[] = [
drawn.result,
{ id: "answer", role: "assistant", content: BOOKINGS.answer },
];
if (stage === "answer")
return [...opening, surface(first, catalogId), ...reply];
const redrawn = drawing("draw-by-month", FOLLOW_UP_OPERATIONS);
return [
...opening,
surface([...first, ...FOLLOW_UP_OPERATIONS], catalogId),
...reply,
{ id: "follow-up", role: "user", content: BOOKINGS.followUp },
redrawn.call,
redrawn.result,
{
id: "follow-up-answer",
role: "assistant",
content: BOOKINGS.followUpAnswer,
},
];
}
Loading