与 AI 协作
为智能体提供与项目版本一致的契约,检查它修改的标记,并在接受修改前验证渲染后的界面。
本页内容
向编程智能体提供项目当前使用版本的契约,再让它检查自己修改的标记。契约列出类名、必需元素、状态属性和公开的自定义属性。检查器无需渲染页面即可发现常见的结构错误。
要让智能体安装库,请把安装提示词粘贴给它。该提示词也会让智能体安装技能和下文的指导说明。
修改前,智能体应查看项目如何导入 Master UI、使用哪个主题,以及 master-ui.json 是否记录了由项目自行维护的组件。本地契约优先于包内的默认契约。修改后,静态检查和浏览器验证各有侧重。标记即使有效,仍可能存在布局错误、交互不具备无障碍支持或缺少行为脚本的问题。
契约检查和浏览器验证
检查源码和渲染结果。 按已安装包中的契约或项目自行维护的契约编写标记。CLI 检查它能读取的元素、类名和属性,浏览器则能验证布局、键盘操作和状态行为。接受修改前,先解决两种检查发现的问题。
#技能
master-ui 技能供在使用本库的项目中工作的智能体使用。运行以下命令安装:
npx skills add master-co/ui --skill master-ui| 文件 | 内容 |
|---|---|
SKILL.md | 如何确认项目通过什么方式引入库、哪些组件由项目自行维护,如何按契约编写标记,库缺少所需功能时如何处理,以及如何验证结果 |
references/choosing.md | 有多个组件可用时,如何为覆盖在页面上的浮层、消息、从多个选项中选择、数值、导航或一组记录选用合适的组件 |
references/customizing.md | 定制层级及由项目自行维护组件的条件 |
references/frameworks.md | 在 React、Svelte 和 Vue 中哪些模式仍用标记实现,哪些模式提供可复制的组件,以及如何构建这类组件 |
技能指导智能体在已导入 Master UI 的项目中使用它。项目已使用其他库时,技能不会引入 Master UI。
#网站为程序提供的资源
网站发布结构化数据和可移植的 Markdown。组件的 Markdown 由契约生成,指南、布局、区块和页面则从各自编写的页面导出。这些资源仅提供文本和代码,不包含网站的交互式副本。
| URL | 内容 |
|---|---|
https://ui.master.co/llms.txt | 索引,包含编写 Master UI 标记的规则及各页面的 Markdown 版链接 |
https://ui.master.co/<section>/<slug>.md | 任意页面的 Markdown 版,所属分区为 guide、components、charts、blocks 或 pages。组件页面根据契约生成,涵盖标记、类名、状态、属性、脚本、键盘操作和平台支持。组件分区中的布局保留原有页面内容,并在末尾附上契约 |
https://ui.master.co/contracts.json | 所有契约的结构化数据 |
https://ui.master.co/contract.schema.json | 所有契约遵循的 JSON Schema |
https://ui.master.co/theme.json | 主题中的所有令牌及其值 |
https://ui.master.co/r/registry.json | 注册表,提供可复制到项目中的源码 |
无法使用命令行的智能体可从 llms.txt 开始,阅读相关组件页面,例如 https://ui.master.co/components/dialog.md。公开网站说明的是网站当前部署的库版本,可能与项目安装的版本不同。如果可以访问包,优先使用包内的 @master/ui/contracts.json 和 @master/ui/theme.json。如果项目自行维护契约,请优先用下文介绍的 CLI。
#查阅契约
安装软件包后,从项目根目录运行 CLI。它会从已安装的包和项目中读取契约。查询时应使用已安装的命令,避免临时获取无关版本。
npx master-ui list # every contract, one line eachnpx master-ui contract dialog # one contract as JSON可以用类名查询,因此 npx master-ui contract btn-primary 会输出按钮的契约。npx master-ui list --json 会以数据形式输出列表。
两个命令也会读取 master-ui.json 中配置的目录(默认为 ui/)和额外契约路径。由项目自行维护的组件以项目中的契约副本为准,项目自行编写的组件会和库中的组件一同列出。
#检查标记
npx master-ui check 读取标记并报告违反契约的位置。例如,这个文件有三处错误和一条警告:
<button type="button" class="btn-primary">Save changes</button><button type="button" class="btn btn-outline">Cancel</button><button type="button" class="btn btn-square"> <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 4L12 12"/><path d="M4 12L12 4"/></svg></button><div class="dialog"> <h2 class="dialog-title">Delete this project?</h2></div>npx master-ui check page.htmlpage.html:1:1 error missing-base "btn-primary" modifies "btn": write class="btn btn-primary".page.html:2:1 warning unknown-class "btn-outline" is not a class of btn. It has: btn, btn-primary, btn-quiet, btn-brand, btn-danger, btn-sm, btn-lg, btn-square, btn-pill, btn-group.page.html:3:1 error label "btn-square" needs an accessible name: text of its own, aria-label, aria-labelledby, or a <label> that wraps or points at it.page.html:6:1 error element "dialog" goes on <dialog>, not <div>.1 files checked: 3 errors, 1 warnings.每行列出问题的位置、严重程度、诊断代码和建议写法,方便智能体修正标记并重新运行命令,直到没有诊断信息。契约列出了所有诊断代码。
- 检查路径。 不传参数时,命令检查工作目录下所有
.html、.htm、.astro、.svelte、.vue、.jsx、.tsx、.mdx、.erb、.php、.njk、.liquid和.hbs文件,跳过node_modules和构建产物。命令还会读取其中的样式表以及 JavaScript 和 TypeScript 模块,检查它们导入库的方式。可传入文件、目录或 glob 模式来缩小范围。 - 检查范围。 检查器直接读取标签、类名和属性,无需启动浏览器。对于无法判定的情况,例如通过表达式生成的类名,或根元素位于另一个文件中的组件部件,检查器不会报告。
--json以数据形式输出结果,包括文件数量,以及每个发现问题的文件对应的诊断信息。每条诊断包含code、severity、className、contract、message、line和column。--strict将unknown-class报告为错误。以组件根类名加连字符开头、且未列入任何契约的类名默认只触发警告,因为它可能是工具类。项目安装了 Master CSS 时,检查器可以识别预设工具类。也要查看涉及项目自定义类名的诊断信息。即使没有诊断信息,自定义工具类和动态计算的类名也可能存在未检出的问题。
命令发现错误时会以状态码 1 退出,因此可直接用于 CI,出错时相应步骤会失败:
{ "scripts": { "check:ui": "master-ui check --strict" }}在程序中执行检查时,使用 @master/ui/check。契约说明了检查器读取的数据。将浏览器验证纳入智能体的验收标准:测试受影响的键盘操作、状态、窄屏布局和配色方案,并验证所需的行为脚本是否已绑定。
#给智能体的指令
智能体会优先遵循项目自己的指令。将以下内容粘贴到 AGENTS.md 或智能体读取的文件中:
## Master UIThis project builds its interface with Master UI: classes on native elements, each described by a contract.- Read the contract before writing a component's markup: `npx master-ui contract <name>`, and `npx master-ui list` to find one. Do not write markup from memory.- Never invent a Master UI component class. Use the installed or locally owned contract, and keep project-defined classes outside the library's class families.- Mark a state with the attribute the contract lists (`disabled`, `aria-pressed="true"`, `open`). Never add a class for a state.- A component is a class on a native element in any framework. Do not wrap one in a component to rename it. The patterns that hold state have a component to copy: `npx master-ui add` lists them, and `ui/` may already hold one.- To change how something looks, take the lowest step that works: a utility on the element, a theme token, a public custom property, a theme file, and only then `npx master-ui own <name>`. A project rule may select a library class to set its documented custom properties; change its implementation through owned source.- Icons are Master Icons unless this project names another set. Look a name up before importing it (`node node_modules/@master/icons/skills/master-icons/scripts/search.mjs "<words>"`), pick the 16, 20 or 24 drawing by the size the component gives the icon, put `icon` on it and never set `stroke-width` in CSS.- Run `npx master-ui check` after changing markup. Fix every error and read each warning. Check the affected interactions in a browser, including the keyboard, a narrow viewport and both color schemes.#MCP
Master UI 软件包提供前文介绍的 CLI 和契约文件。如果智能体还连接了 Master CSS MCP 服务器,请先查看该连接提供的工具,再使用它查询组件契约。可用工具取决于连接的服务器。也可以直接使用已安装的 CLI 读取项目实际使用的契约。
#在聊天沙箱或交互产物中使用
如果聊天中生成的交互产物或静态 HTML 原型没有 CSS 编译流程,请链接网站编译好的样式表。如果环境没有提供基础样式重置,请一并引入网站的重置样式:
<link rel="stylesheet" href="https://ui.master.co/base.css"><link rel="stylesheet" href="https://ui.master.co/standalone.css"><button type="button" class="btn btn-primary">Save changes</button>这些 URL 指向网站当前部署的版本,适合临时原型。对于需要持续维护的项目,请使用固定版本的软件包来提供编译好的文件,并读取同一版本的契约。加载 CSS 不会绑定行为脚本。需要行为脚本的模式仍须使用浏览器模块或框架组件。
在这种环境中无法使用工具类。flex 或 gap-md 等工具类需要由 Master CSS 生成。请用自己的 CSS 排布页面,将样式写在 style 属性或一小段样式块中。主题令牌是自定义属性,可以直接使用。color: var(--color-text-muted) 会读取主题中的值。