@a2ui/lit

中文文档
0.15.0发布于 2026/11/02Apache-2.0TypeScriptES Module近 30 天下载 93.4万

A2UI Lit Library

原始使用文档
文档目录11 节
  1. Installation
  2. Protocol Versioning
  3. Quick Start
  4. Defining Custom Components
  5. 1. Define the Component API
  6. 2. Create the Lit Implementation
  7. 3. Define the Catalog
  8. 4. Use the Custom Catalog
  9. Basic Catalog Components
  10. Migration from v0.8
  11. Security

@a2ui/lit

Lit renderer for A2UI (Agent-to-User Interface) - enables AI agents to generate rich, interactive user interfaces through declarative JSON.

A basic catalog implementation is included to help you get started right away.

Additional custom components can be defined with type-safe properties inferred from Zod schemas, and can be added to your own custom catalogs for total control over the generated code.

Installation

yarn add @a2ui/lit @a2ui/web_core

Protocol Versioning

A2UI supports multiple protocol versions. For new projects, it is recommended to use the v0.9 protocol.

To use the v0.9 implementation, import from the versioned path:

import {A2uiSurface, basicCatalog} from '@a2ui/lit/v0_9';

Quick Start

The Lit renderer works alongside the MessageProcessor from @a2ui/web_core.

import {LitElement, html} from 'lit';
import {customElement, state} from 'lit/decorators.js';
import {MessageProcessor} from '@a2ui/web_core/v0_9';
import {A2uiSurface, basicCatalog} from '@a2ui/lit/v0_9';

@customElement('my-app')
export class MyApp extends LitElement {
  private processor = new MessageProcessor([basicCatalog]);

  @state()
  private surface: any;

  connectedCallback() {
    super.connectedCallback();

    // Listen for surface creation
    this.processor.onSurfaceCreated(s => {
      if (s.id === 'main-chat') {
        this.surface = s;
      }
    });

    // The message objects should come from your agent
    this.processor.processMessages([
      {
        version: 'v0.9',
        createSurface: {
          surfaceId: 'main-chat',
          catalogId: basicCatalog.id,
        },
      },
    ]);
  }

  render() {
    return html`
      <div class="a2ui-container">
        ${this.surface
          ? html`<a2ui-surface .surface=${this.surface}></a2ui-surface>`
          : html`<div>Initializing Agent UI...</div>`}
      </div>
    `;
  }
}

Defining Custom Components

A2UI v0.9 separates a component's API (Schema) from its implementation.

1. Define the Component API

Use Zod to define the properties.

import {z} from 'zod';
import {CommonSchemas} from '@a2ui/web_core/v0_9';

export const MyProfileApi = {
  name: 'Profile',
  schema: z.object({
    username: CommonSchemas.DynamicString,
    bio: CommonSchemas.DynamicString,
    onEdit: CommonSchemas.Action,
  }),
};

2. Create the Lit Implementation

Extend A2uiLitElement and implement createController and render.

import {A2uiLitElement} from '@a2ui/lit/v0_9';
import {customElement} from 'lit/decorators.js';
import {html, nothing} from 'lit';
import {MyProfileApi} from './my-profile-api';

@customElement('my-profile')
export class MyProfileElement extends A2uiLitElement<typeof MyProfileApi> {
  protected readonly api = MyProfileApi;

  render() {
    const props = this.controller?.props;
    if (!props) return nothing;

    return html`
      <div class="profile-widget">
        <h2>${props.username}</h2>
        <p>${props.bio}</p>
        <button @click=${props.onEdit}>Edit</button>
      </div>
    `;
  }
}

// Export the catalog entry
export const MyProfile = {
  ...MyProfileApi,
  tagName: 'my-profile',
};

3. Define the Catalog

Group your custom components into a Catalog from @a2ui/web_core/v0_9.

import {Catalog} from '@a2ui/web_core/v0_9';
import {MyProfile} from './my-profile';

export const myCatalog = new Catalog('https://example.com/catalogs/my-catalog.json', 'v0.9', [
  MyProfile,
]);

4. Use the Custom Catalog

Pass your custom catalog to the MessageProcessor alongside the basic catalog in your app setup (as seen in the Quick Start).

import {MessageProcessor} from '@a2ui/web_core/v0_9';
import {basicCatalog} from '@a2ui/lit/v0_9';
import {myCatalog} from './my-catalog';

// Initialize the MessageProcessor with both catalogs
const processor = new MessageProcessor([basicCatalog, myCatalog]);

Basic Catalog Components

The @a2ui/lit/v0_9 package includes a basicCatalog with standard components. You can find the full specification of the basic catalog in the GitHub repository.

  • Layout: Row, Column, List, Card, Tabs, Modal, Divider
  • Content: Text, Image, Icon, Video
  • Input: Button, TextField, CheckBox, ChoicePicker, Slider, DateTimeInput

The standard basic catalog components are implemented as framework-agnostic universal Custom Elements in @a2ui/web_core (source code on GitHub) and re-exported from @a2ui/lit/v0_9 for backwards compatibility.

Migration from v0.8

If you are migrating an existing application from v0.8 to v0.9, you can reference the migration of the shell sample in Pull Request #1105 (commit a5ad0c328628df6fd8f53aef86f5b3db452ba3a8).

Key changes in that migration included:

  • Header Update in client.ts: You must update the X-A2A-Extensions header to request the v0.9 protocol: https://a2ui.org/a2a-extension/a2ui/v0.9.
  • Removal of Manual Action Handling in app.ts: In v0.8, applications had to listen for custom action events (like @a2uiaction) and manually resolve context paths to build action messages. In v0.9, the MessageProcessor and the Generic Binder handle this automatically. You can remove complex event listeners from your templates.
  • CSS-based Basic Catalog Theming: The Basic Catalog widgets can now be styled by overriding CSS variables, like --a2ui-primary-color.

We strongly discourage starting new projects with v0.8.

Security

[!IMPORTANT] The sample code provided is for demonstration purposes and illustrates the mechanics of A2UI and the Agent-to-Agent (A2A) protocol. When building production applications, it is critical to treat any agent operating outside of your direct control as a potentially untrusted entity.

All operational data received from an external agent—including its AgentCard, messages, artifacts, and task statuses—should be handled as untrusted input. For example, a malicious agent could provide crafted data in its fields (e.g., name, skills.description) that, if used without sanitization to construct prompts for a Large Language Model (LLM), could expose your application to prompt injection attacks.

Similarly, any UI definition or data stream received must be treated as untrusted. Malicious agents could attempt to spoof legitimate interfaces to deceive users (phishing), inject malicious scripts via property values (XSS), or generate excessive layout complexity to degrade client performance (DoS). If your application supports optional embedded content (such as iframes or web views), additional care must be taken to prevent exposure to malicious external sites.

Developer Responsibility: Failure to properly validate data and strictly sandbox rendered content can introduce severe vulnerabilities. Developers are responsible for implementing appropriate security measures—such as input sanitization, Content Security Policies (CSP), strict isolation for optional embedded content, and secure credential handling—to protect their systems and users.

版本与发布记录

@a2ui/lit 共发布 21 个版本,首次发布于 2025/12/16;过去 12 个月发布了 21 个版本。

版本发布日期距今
0.15.0latest2026/11/024 天前
0.14.02026/10/1720 天前
0.13.02026/10/1027 天前
0.12.02026/09/291 个月前
0.11.02026/09/121 个月前
0.10.42026/09/012 个月前

关于 @a2ui/lit 的常见问题

@a2ui/lit 是什么?

A2UI Lit Library

如何安装 @a2ui/lit?

在项目目录运行 npm install @a2ui/lit,或使用 pnpm add / yarn add / bun add。

@a2ui/lit 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@a2ui/lit 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@a2ui/lit 可以商用吗?

@a2ui/lit 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@a2ui/lit 的下载量有多少?

根据 npm 官方下载统计,@a2ui/lit 在 2026/10/05 – 2026/11/03 这 30 天内被下载 933,571 次,日均约 31,119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@a2ui/lit 还在维护吗?

最新版本 0.15.0 发布于 2026/11/02(4 天前);至今共发布 21 个版本,其中过去 12 个月发布了 21 个;首次发布于 2025/12/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。