A2UI와 함께 Todae 사용하기
A2UI v1.0은 메시지 형식입니다. 에이전트가 표면을 묘사한 JSON을 보내면 앱이 그립니다. Todae는 그 메시지를 해석하지 않으며 @a2ui/react나 @a2ui/web_core에 의존하지 않습니다. 카탈로그는 앱이 가집니다. 위젯은 Todae가 줍니다.
이 페이지의 내용
앱에 @a2ui/react와 @a2ui/web_core 0.16을 설치합니다. 아래 이름은 0.16.1의 export입니다. v0.8은 폐기되었습니다. 이 페이지는 v1.0만 다룹니다.
카탈로그는 앱이 가집니다
앱이 카탈로그 id를 알립니다. catalogId는 식별자이며, 에이전트가 가져오는 URL이 아닙니다. 공식 기본 카탈로그의 id는 https://a2ui.org/specification/v1_0/catalogs/basic/catalog.json입니다. 에이전트는 createSurface에서 알린 id 하나를 고릅니다.
위젯이 기본 컴포넌트와 그 props를 그대로 받으면 그 id를 유지합니다. 컴포넌트를 더하거나 prop을 바꾸는 것처럼 계약이 바뀔 때만 새 id를 씁니다. v1.0에는 와이어에 테마가 없습니다. 트리를 ThemeProvider와 DensityProvider로 감싸고, 스타일은 앱에서 둡니다.
같은 id를 다음 자리에 보냅니다.
- A2A 메시지는
message.metadata.a2uiRendererCapabilities["1.0"].supportedCatalogIds를 둡니다. - 상태를 가진 MCP 클라이언트는
initialize의params.capabilities.a2ui.rendererCapabilities를 둡니다. - 상태 없는 MCP
tools/call은params._meta.a2ui.rendererCapabilities를 둡니다.
MessageProcessor.getRendererCapabilities({ versions: ['1.0'] })가 그 세 칸에 들어가는, 버전을 키로 한 객체를 돌려줍니다.
기본 카탈로그를 대응합니다
weight는 숫자이며, Row나 Column의 직접 자식이 가지는 flex grow입니다. 글자 두께가 아닙니다. Stack에는 weight prop이 없으므로, weight가 숫자일 때 요소에 flexGrow를 둡니다. spaceAround와 spaceEvenly는 Stack의 justify에 없습니다. 그 둘과 주축의 stretch는 시작점에 모입니다.
| A2UI | Todae | Props와 빈칸 |
|---|---|---|
| Text | Text.Root | text가 children입니다. variant는 body(기본값) 또는 caption입니다. caption 크기는 없으므로 tone="muted"를 씁니다. Text는 markdown을 그리지 않습니다. markdown 문자열은 글자로 남습니다. |
| Image | 없음 | img를 씁니다. url은 src입니다. description은 alt입니다. fit은 object-fit이고, scaleDown은 scale-down입니다. variant(icon, avatar, smallFeature, mediumFeature, largeFeature, header)는 앱의 CSS입니다. |
| Icon | 없음 | Todae는 아이콘 세트를 두지 않습니다. name을 앱의 아이콘에 대응합니다. 예제는 lucide-react를 씁니다. name은 svgPath를 가진 객체일 수도 있습니다. GlyphsProvider는 Todae 자신의 글리프를 바꿉니다. 이 카탈로그가 아닙니다. |
| Video | 없음 | video를 씁니다. url은 src입니다. posterUrl은 poster입니다. |
| AudioPlayer | 없음 | controls가 있는 audio를 씁니다. url은 src입니다. description은 옆의 Text.Root에 둡니다. |
| Row | Stack.Root | direction="row"입니다. children은 자식 id 목록입니다. justify는 start, center, end, 그리고 spaceBetween을 between으로 대응합니다. align은 start, center, end, stretch를 대응합니다. |
| Column | Stack.Root | Row와 같고 direction="column"입니다. Stack의 기본 방향은 이미 column입니다. |
| List | Stack.Root | as="ul"이고, 자식마다 li로 감쌉니다. direction의 vertical은 column이고 horizontal은 row입니다. align은 Row와 같이 대응합니다. List export는 없습니다. |
| Card | Card.Root | child는 하나입니다. Card는 자식을 여러 개 받으므로, 그 자식 하나를 안에 그립니다. |
| Tabs | Tabs.Root | tabs는 title과 child입니다. Tabs.List, Tabs.Tab, Tabs.Panel을 value로 짝짓습니다. 카탈로그에는 선택된 탭이 없습니다. 앱이 가지며, 첫 탭에서 시작합니다. |
| Modal | Dialog.Root | trigger와 content는 자식 id입니다. Dialog에는 trigger 파트가 없습니다. trigger를 그리고, 그 주위를 감싼 요소의 클릭으로 open을 둡니다. content는 Dialog.Body에 두고, Dialog.Close를 넣습니다. |
| Divider | Separator.Root | axis는 horizontal(기본값) 또는 vertical입니다. Separator는 hr이고 방향이 없습니다. 세로 선은 앱의 테두리입니다. |
| Button | Button.Root | child는 버튼 안에 그리는 라벨 컴포넌트입니다. action은 함수로 풀립니다. onClick에서 호출합니다. variant의 primary는 primary, borderless는 ghost, default는 secondary입니다. |
| TextField | Field | label은 htmlFor가 필요한 Label.Root입니다. value는 문자열이고, 바인더가 setValue를 더합니다. shortText는 Field.TextInput입니다. obscured는 type="password"입니다. longText는 Field.Textarea입니다. number는 와이어에서 여전히 문자열입니다. Field.NumberInput을 위해 숫자로 읽고, 문자열로 다시 씁니다. checks가 isValid를 둡니다. false이면 invalid를 넘깁니다. |
| CheckBox | Checkbox.Root | label이 children입니다. value는 boolean입니다. checked와 onCheckedChange를 setValue에 묶습니다. |
| ChoicePicker | RadioGroup, Checkbox, ToggleGroup, 또는 combobox 레시피 | options는 label과 value입니다. value는 문자열 목록입니다. mutuallyExclusive(기본값)는 RadioGroup이고, 값은 문자열 하나 또는 null입니다. multipleSelection은 옵션마다 Checkbox.Root입니다. displayStyle의 chips는 ToggleGroup(single 또는 multiple)입니다. filterable은 combobox 레시피(useCombobox와 OptionList)를 씁니다. Combobox export는 없습니다. |
| Slider | Slider.Root | value는 숫자이고 setValue가 있습니다. min의 기본값은 0입니다. max는 필수입니다. steps는 칸의 개수이므로, 네이티브 step은 (max - min) / steps입니다. steps를 생략하면 Todae의 step은 1입니다. 엄지 하나는 onChange를 씁니다. |
| DateTimeInput | Field.DateInput, Field.TimeInput, 또는 Field.DateTimeInput | value는 ISO 문자열입니다. Todae는 Temporal.PlainDate, PlainTime, PlainDateTime을 원하며 polyfill을 두지 않습니다. enableDate만 켜면 날짜 입력입니다. enableTime만 켜면 시간 입력입니다. 둘 다 켜면 날짜와 시간 입력입니다. 둘 다 기본값은 false이고, 그때는 그릴 것이 없습니다. UTC 오프셋이 있는 날짜-시간 문자열은 PlainDateTime이 아닙니다. |
자식 prop은 문자열 id이거나, id와 basePath를 가진 객체입니다. buildChild(id, basePath)가 그립니다. createComponentImplementation은 API 객체에서 name과 schema를 복사합니다. 웹 컴포넌트 A2uiText가 아니라 TextApi를 넘깁니다. A2uiText를 펼치면 그 커스텀 요소가 남습니다.
구현을 등록합니다
basicCatalog.copyWith({ components })는 기본 카탈로그 id, 프로토콜 버전 1.0, formatString과 required 같은 카탈로그 함수를 유지합니다. components 배열은 목록 전체를 바꿉니다. 계속 받을 기본 컴포넌트를 모두 넣습니다. new Catalog(basicCatalog.id, '1.0', implementations, [...basicCatalog.functions.values()])는 같은 카탈로그를 풀어 쓴 것입니다.
'use client';
import { useEffect, useState, type ReactNode } from 'react';
import { A2uiSurface, createComponentImplementation } from '@a2ui/react';
import { MessageProcessor } from '@a2ui/web_core';
import {
AudioPlayerApi, ButtonApi, CardApi, CheckBoxApi, ChoicePickerApi, ColumnApi, DateTimeInputApi,
DividerApi, IconApi, ImageApi, ListApi, ModalApi, RowApi, SliderApi, TabsApi, TextApi, TextFieldApi,
VideoApi, basicCatalog,
} from '@a2ui/web_core/catalogs/basic/v1';
import {
Button, Card, Checkbox, Dialog, Field, Label, OptionList, Popover, RadioGroup, Separator, Slider,
Stack, Tabs, Text, ToggleGroup, useCombobox,
} from '@tounsoo/todae';
import { Calendar, Check, CircleUser, Plus, Search, X, type LucideIcon } from 'lucide-react';
type Build = (id: string, basePath?: string) => ReactNode;
type Child = string | { id: string; basePath?: string };
function renderChild(child: Child | undefined, build: Build) {
if (typeof child === 'string') return build(child);
if (child) return build(child.id, child.basePath);
return null;
}
const JUSTIFY = { start: 'start', center: 'center', end: 'end', spaceBetween: 'between' } as const;
function justifyOf(value?: string) {
return value && value in JUSTIFY ? JUSTIFY[value as keyof typeof JUSTIFY] : undefined;
}
function grow(weight?: number) {
return typeof weight === 'number' ? { flexGrow: weight } : undefined;
}
function FilteredChoices({ label, variant, options, value, setValue }: {
label?: string;
variant?: string;
options: { label: string; value: string }[];
value?: string[];
setValue: (next: string[]) => void;
}) {
const multiple = variant === 'multipleSelection';
const selected = value ?? [];
const cb = useCombobox({
options,
presentation: 'popover',
selectionMode: multiple ? 'multiple' : 'single',
value: multiple ? selected : (selected[0] ?? null),
onValueChange: (next) => setValue(Array.isArray(next) ? [...next] : next ? [next] : []),
});
return (
<>
<Field.Group>
<Label.Root {...cb.getLabelProps()}>{label}</Label.Root>
<Field.Root {...cb.getAnchorProps()}>
{multiple ? <Field.ChipInput {...cb.getChipInputProps()} /> : <Field.TextInput {...cb.getInputProps()} />}
</Field.Root>
</Field.Group>
<Popover.Root {...cb.getPopoverProps()}>
<OptionList.Root {...cb.getListboxProps()}>
{cb.items.map((item) => <OptionList.Option key={item.id} {...cb.getOptionProps(item)} />)}
</OptionList.Root>
</Popover.Root>
</>
);
}
const ICONS: Record<string, LucideIcon> = {
accountCircle: CircleUser, add: Plus, calendarToday: Calendar, check: Check, close: X, search: Search,
};
const implementations = [
createComponentImplementation(TextApi, ({ props }) => (
<Text.Root tone={props.variant === 'caption' ? 'muted' : 'default'} style={grow(props.weight)}>{props.text}</Text.Root>
)),
createComponentImplementation(ImageApi, ({ props }) => (
<img alt={props.description ?? ''} src={props.url} style={{ ...grow(props.weight), objectFit: props.fit === 'scaleDown' ? 'scale-down' : props.fit }} />
)),
createComponentImplementation(IconApi, ({ props }) => {
const style = grow(props.weight);
if (props.name && typeof props.name === 'object' && 'svgPath' in props.name && props.name.svgPath) {
return <svg aria-hidden viewBox="0 0 24 24" style={style}><path d={props.name.svgPath} /></svg>;
}
const Glyph = typeof props.name === 'string' ? ICONS[props.name] : undefined;
return Glyph ? <Glyph aria-hidden style={style} /> : null;
}),
createComponentImplementation(VideoApi, ({ props }) => (
<video controls poster={props.posterUrl} src={props.url} style={grow(props.weight)} />
)),
createComponentImplementation(AudioPlayerApi, ({ props }) => (
<span style={grow(props.weight)}>
{props.description ? <Text.Root>{props.description}</Text.Root> : null}
<audio controls src={props.url} />
</span>
)),
createComponentImplementation(RowApi, ({ props, buildChild }) => (
<Stack.Root align={props.align} direction="row" justify={justifyOf(props.justify)} style={grow(props.weight)}>
{(props.children ?? []).map((child) => renderChild(child, buildChild))}
</Stack.Root>
)),
createComponentImplementation(ColumnApi, ({ props, buildChild }) => (
<Stack.Root align={props.align} direction="column" justify={justifyOf(props.justify)} style={grow(props.weight)}>
{(props.children ?? []).map((child) => renderChild(child, buildChild))}
</Stack.Root>
)),
createComponentImplementation(ListApi, ({ props, buildChild }) => (
<Stack.Root align={props.align} as="ul" direction={props.direction === 'horizontal' ? 'row' : 'column'} style={grow(props.weight)}>
{(props.children ?? []).map((child, index) => <li key={index}>{renderChild(child, buildChild)}</li>)}
</Stack.Root>
)),
createComponentImplementation(CardApi, ({ props, buildChild }) => (
<Card.Root style={grow(props.weight)}>{renderChild(props.child, buildChild)}</Card.Root>
)),
createComponentImplementation(TabsApi, ({ props, buildChild }) => (
<Tabs.Root defaultValue="0">
<Tabs.List aria-label="Tabs">
{props.tabs.map((tab, index) => <Tabs.Tab key={index} value={String(index)}>{tab.title}</Tabs.Tab>)}
</Tabs.List>
{props.tabs.map((tab, index) => <Tabs.Panel key={index} value={String(index)}>{renderChild(tab.child, buildChild)}</Tabs.Panel>)}
</Tabs.Root>
)),
createComponentImplementation(ModalApi, ({ props, buildChild }) => {
const [open, setOpen] = useState(false);
return (
<>
<span onClick={() => setOpen(true)}>{renderChild(props.trigger, buildChild)}</span>
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Close />
<Dialog.Body>{renderChild(props.content, buildChild)}</Dialog.Body>
</Dialog.Root>
</>
);
}),
createComponentImplementation(DividerApi, ({ props }) => <Separator.Root style={grow(props.weight)} />),
createComponentImplementation(ButtonApi, ({ props, buildChild }) => (
<Button.Root variant={props.variant === 'primary' ? 'primary' : props.variant === 'borderless' ? 'ghost' : 'secondary'} onClick={() => props.action()}>
{renderChild(props.child, buildChild)}
</Button.Root>
)),
createComponentImplementation(TextFieldApi, ({ props, context }) => {
const id = context.componentModel.id;
const invalid = props.isValid === false;
if (props.variant === 'number') {
const parsed = props.value ? Number(props.value) : null;
return (
<Field.Root id={id} invalid={invalid}>
<Field.NumberInput aria-label={props.label} value={parsed !== null && Number.isNaN(parsed) ? null : parsed} onValueChange={(next) => props.setValue(next === null ? '' : String(next))} />
</Field.Root>
);
}
if (props.variant === 'longText') {
return (
<Field.Root id={id} invalid={invalid}>
<Field.Textarea aria-label={props.label} placeholder={props.placeholder} value={props.value ?? ''} onValueChange={props.setValue} />
</Field.Root>
);
}
return (
<Field.Root id={id} invalid={invalid}>
<Field.TextInput aria-label={props.label} placeholder={props.placeholder} type={props.variant === 'obscured' ? 'password' : 'text'} value={props.value ?? ''} onValueChange={props.setValue} />
</Field.Root>
);
}),
createComponentImplementation(CheckBoxApi, ({ props }) => (
<Checkbox.Root checked={props.value} invalid={props.isValid === false} onCheckedChange={props.setValue}>{props.label}</Checkbox.Root>
)),
createComponentImplementation(ChoicePickerApi, ({ props }) => {
const selected = props.value ?? [];
if (props.filterable) {
return <FilteredChoices label={props.label} options={props.options} setValue={props.setValue} value={selected} variant={props.variant} />;
}
if (props.displayStyle === 'chips') {
return (
<ToggleGroup.Root aria-label={props.label} selectionMode={props.variant === 'multipleSelection' ? 'multiple' : 'single'} value={props.variant === 'multipleSelection' ? selected : (selected[0] ?? null)} onValueChange={(next) => props.setValue(Array.isArray(next) ? [...next] : next ? [next] : [])}>
{props.options.map((option) => <ToggleGroup.Item key={option.value} value={option.value}>{option.label}</ToggleGroup.Item>)}
</ToggleGroup.Root>
);
}
if (props.variant === 'multipleSelection') {
return (
<>
{props.options.map((option) => (
<Checkbox.Root key={option.value} checked={selected.includes(option.value)} onCheckedChange={(checked) => props.setValue(checked ? [...selected, option.value] : selected.filter((item) => item !== option.value))}>
{option.label}
</Checkbox.Root>
))}
</>
);
}
return (
<RadioGroup.Root aria-label={props.label ?? 'Choices'} invalid={props.isValid === false} value={selected[0] ?? null} onValueChange={(next) => props.setValue(next ? [next] : [])}>
{props.options.map((option) => <RadioGroup.Item key={option.value} value={option.value}>{option.label}</RadioGroup.Item>)}
</RadioGroup.Root>
);
}),
createComponentImplementation(SliderApi, ({ props }) => (
<Slider.Root aria-label={props.label} max={props.max} min={props.min ?? 0} step={props.steps ? (props.max - (props.min ?? 0)) / props.steps : undefined} value={props.value} onChange={(event) => props.setValue(Number(event.currentTarget.value))} />
)),
createComponentImplementation(DateTimeInputApi, ({ props }) => {
const write = (next: { toString(): string } | null) => props.setValue(next ? next.toString() : '');
if (props.enableDate && props.enableTime) {
return <Field.DateTimeInput aria-label={props.label} value={props.value ? Temporal.PlainDateTime.from(props.value) : null} onValueChange={write} />;
}
if (props.enableTime) {
return <Field.TimeInput aria-label={props.label} value={props.value ? Temporal.PlainTime.from(props.value) : null} onValueChange={write} />;
}
if (props.enableDate) {
return <Field.DateInput aria-label={props.label} value={props.value ? Temporal.PlainDate.from(props.value) : null} onValueChange={write} />;
}
return null;
}),
];
const catalog = basicCatalog.copyWith({ components: implementations });
export function createTodaeProcessor() {
return new MessageProcessor([catalog], (action) => {
void action.name;
void action.surfaceId;
void action.context;
});
}
export function TodaeSurfaces({ processor }: { processor: MessageProcessor }) {
const [surfaces, setSurfaces] = useState(() => [...processor.model.surfacesMap.values()]);
useEffect(() => {
const sync = () => setSurfaces([...processor.model.surfacesMap.values()]);
const created = processor.onSurfaceCreated(sync);
const deleted = processor.onSurfaceDeleted(sync);
return () => {
created.unsubscribe();
deleted.unsubscribe();
};
}, [processor]);
return surfaces.map((surface) => <A2uiSurface key={surface.id} surface={surface} />);
}
const processor = createTodaeProcessor();
const capabilities = processor.getRendererCapabilities({ versions: ['1.0'] });
const a2aMetadata = { a2uiRendererCapabilities: capabilities };
const mcpInitializeCapabilities = { a2ui: { rendererCapabilities: capabilities } };
const mcpCallMeta = { a2ui: { rendererCapabilities: capabilities } };
processor.processMessages([
{ version: '1.0', createSurface: { surfaceId: 'main', catalogId: catalog.id } },
{
version: '1.0',
updateComponents: {
surfaceId: 'main',
components: [
{ id: 'root', component: 'Column', children: ['title', 'save'] },
{ id: 'title', component: 'Text', text: { '@path': '/title' } },
{ id: 'save-label', component: 'Text', text: 'Save' },
{ id: 'save', component: 'Button', child: 'save-label', variant: 'primary', action: { event: { name: 'save' } } },
],
},
},
{ version: '1.0', updateDataModel: { surfaceId: 'main', path: '/', value: { title: 'Welcome' } } },
]);
a2aMetadata는 A2A 메시지의 message.metadata입니다. mcpInitializeCapabilities는 MCP initialize의 params.capabilities입니다. mcpCallMeta는 상태 없는 tools/call의 params._meta입니다. catalog.id는 기본 카탈로그 id입니다.
TextField는 label로 aria-label을 둡니다. 보이는 라벨은 htmlFor가 필드 id인 Label.Root입니다. number는 와이어 문자열을 Field.NumberInput을 위해 읽고, 문자열로 다시 씁니다. 필터되는 ChoicePicker는 useCombobox와 Field, OptionList, Popover이며, combobox 레시피입니다. Combobox export는 없습니다. 대응하지 않은 아이콘 이름은 그리지 않습니다. 세로 Divider도 같은 Separator.Root입니다. spaceAround와 spaceEvenly는 시작점에 모입니다.
직접 만든 카탈로그
createComponentImplementation과 Zod 스키마로 컴포넌트를 더합니다. @a2ui/web_core/v1_0의 CommonSchemas가 DynamicString과 Action을 줍니다. derive로 카탈로그에 새 id를 줍니다. components의 문자열은 그 항목을 유지합니다. 객체는 하나를 더하거나 바꿉니다. toJson()이 카탈로그 문서입니다. 새 id에 게시하거나 인라인 카탈로그로 넘기고, 그 id를 알립니다. createSurface.catalogId는 새 id여야 합니다.
import { z } from 'zod';
import { CommonSchemas } from '@a2ui/web_core/v1_0';
const ProfileApi = {
name: 'Profile',
schema: z.object({ username: CommonSchemas.DynamicString, onEdit: CommonSchemas.Action }),
};
const profile = createComponentImplementation(ProfileApi, ({ props }) => (
<Button.Root variant="secondary" onClick={() => props.onEdit()}>{props.username}</Button.Root>
));
const profileCatalog = catalog.derive({
catalogId: 'https://example.com/a2ui/profile.json',
components: [...catalog.components.keys(), profile],
});
prop을 바꾸면 같은 수정에서 id도 바꿉니다. Zod 스키마, toJson(), supportedCatalogIds가 그 id를 함께 가리켜야 합니다.