跳到主要内容

向 zai 提供提示

.zaihints 是一个文本文件,用于提供有关项目的额外上下文,改善与 zai 的沟通。使用 .zaihints 可以确保 zai 更好地理解你的需求,并更有效地执行任务。

zai Hints 视频演练

当你发现自己反复输入提示词,或多次提供同类指令时,就是考虑添加 .zaihints 文件的好时机。它也是提供大量上下文的绝佳方式,这些内容可能更适合放在文件中。

本指南将带你一步步创建和使用 .zaihints 文件,通过自定义指令和上下文简化你的工作流。

需要 Developer 扩展

要使用提示文件,你需要启用Developer 扩展。

创建提示文件​

zai 支持两种类型的提示文件:

  • 全局提示文件 - 这些提示适用于你与 zai 的所有会话,无论目录为何。全局提示存储在 ~/.config/zai/.zaihints。
  • 本地提示文件 - 这些提示仅在你处理特定目录或目录层级时适用。

你可以同时使用全局和本地提示。当二者同时存在时,zai 会同时考虑你的全局偏好和项目特定需求。如果本地提示文件中的指令与全局偏好冲突,zai 会优先采用本地提示。

自定义上下文文件

你可以通过 CONTEXT_FILE_NAMES 环境变量在 zai 中使用其他智能体规则文件。

全局提示文件​

  1. 在 ~/.config/zai 中创建 .zaihints 文件

本地提示文件​

  1. 点击应用底部的目录路径,打开要创建文件的目录
  2. 点击左上角的汉堡按钮 打开侧边栏
  3. 点击侧边栏中的 Settings
  4. 点击 Chat
  5. 向下滚动到 Project Hints (.zaihints) 部分,点击 Configure
  6. 在文本区域中输入你的本地提示
  7. 点击 Save
  8. 重启会话,让 zai 读取更新后的 .zaihints

如果给定目录中已存在 .zaihints 文件,你可以编辑现有提示。

.zaihints 文件可以包含与你的项目相关的任何指令或上下文细节。

设置提示​

.zaihints 文件支持自然语言。使用 zai 易于理解和遵循的直接语言,编写清晰、具体的指令。包含与你的项目和工作流偏好相关的上下文,并将最重要的准则放在最前面。

zai 在会话启动时加载提示。当它访问嵌套目录中的文件时,也会加载这些目录的提示文件。zai 会为每个请求将提示添加到系统提示词中。由于 .zaihints 内容会消耗 token,保持简洁可以降低成本并提升性能。

全局 .zaihints 文件示例​

Always use TypeScript for new Next.js projects.

@coding-standards.md # Contains our coding standards
docs/contributing.md # Contains our pull request process

Follow the [Google Style Guide](https://google.github.io/styleguide/pyguide.html) for Python code.

Run unit tests before committing any changes.

Prefer functional programming patterns where applicable.

本地 .zaihints 文件示例​

This is a simple example JavaScript web application that uses the Express.js framework. View [Express documentation](https://expressjs.com/) for extended guidance.

Go through the @README.md for information on how to build and test it as needed.

Make sure to confirm all changes with me before applying.

Run tests with `npm run test` ideally after each change.

这些示例展示了引用其他文件的两种方式:

  • @ 语法:自动将文件内容包含到 zai 的即时上下文中
  • 普通引用:指向 zai 需要时查看的文件(适用于可选或非常大的文件)

嵌套 .zaihints 文件​

zai 支持 git 仓库中的层级化本地提示。会话启动时,zai 从你的工作目录向上直至仓库根目录加载已配置的上下文文件。当 zai 读取或修改嵌套子目录中的文件时,它还可以自动发现并加载这些目录中的额外提示文件。

这在 monorepo 或代码库不同部分有不同约定的大型项目中尤其有用。

默认情况下,zai 在每一级查找 AGENTS.md 和 .zaihints。如果你使用自定义上下文文件,zai 会对这些文件名应用相同的嵌套加载行为。

作为最佳实践,每一级的 .zaihints 应只包含与该范围相关的提示:

  • 根级别:包含项目级标准、构建流程和通用准则
  • 模块/功能级别:添加代码库该区域的特定要求
  • 目录级别:包含非常具体的上下文,例如本地测试流程或组件模式

项目结构示例:

my-project/
├── .git/
├── .zaihints # Project-wide hints
├── frontend/
│ ├── .zaihints # Frontend-specific hints
│ └── components/
│ ├── .zaihints # Component-specific hints
│ └── Button.tsx
└── backend/
├── .zaihints # Backend-specific hints
└── api/
└── routes.py

如果你在 my-project/ 中启动 zai,根级提示会立即加载。之后,当 zai 访问 frontend/components/ 下的文件时,它会加载该路径的嵌套提示,并按以下顺序组合:

  1. my-project/.zaihints(项目根目录)
    This is a React + TypeScript project using Vite.

    @README.md # Project overview and setup instructions
    @docs/development-setup.md # Development environment configuration

    Always run tests before committing: `npm test`
    Use conventional commits for all changes.
  2. frontend/.zaihints
    This frontend uses React 18 with TypeScript and Tailwind CSS.

    @package.json # Dependencies and scripts
    @docs/frontend-architecture.md # Frontend structure and patterns

    ## Development Standards
    - Use functional components with hooks (no class components)
    - Implement proper TypeScript interfaces for all props
    - Follow the component structure: /components/ComponentName/index.tsx
    - Use Tailwind classes instead of custom CSS when possible

    ## Testing Requirements
    - Write unit tests for all components using React Testing Library
    - Test files should be co-located: ComponentName.test.tsx
    - Run `npm run test:frontend` before committing changes

    ## State Management
    - Use React Query for server state
    - Use Zustand for client state management
    - Avoid prop drilling - lift state appropriately

    Always confirm UI changes with design team before implementation.
  3. frontend/components/.zaihints(当前目录)
    Components in this directory use our design system.

    @docs/component-api.md # Component interface standards and examples

    All components must:
    - Export a default component
    - Include TypeScript props interface
    - Have corresponding .test.tsx file
    - Follow naming convention: PascalCase
备注

某个目录的嵌套提示加载后,它们会在会话的剩余时间内保持生效。如果你更新了提示文件并希望 zai 可靠地获取新内容,请重启会话。

常见用例​

以下是一些人们使用提示为 zai 提供额外上下文的方式:

  • 决策方式:指明 zai 应自主进行更改,还是先与你确认操作。

  • 验证例程:提供 zai 应执行的测试用例或验证方法,以确保更改符合项目规范。

  • 反馈循环:包含让 zai 接收反馈并迭代改进其建议的步骤。

  • 指向更详细的文档:标明 README.md、docs/setup-guide.md 等重要文件,供 zai 查阅详细说明。

  • 用 @ 提及组织内容:对于经常需要的文档,使用 @filename.md 或 @relative/path/testing.md 自动将文件内容包含到当前上下文中,而不仅仅是引用。这确保 zai 可以立即访问重要信息。 对于核心文档(如 API schema 或编码标准),使用 @ 提及以获得即时上下文;对于可选或非常大的文件,则使用普通引用(不带 @)。

与提示词一样,这并非塑造 .zaihints 文件的详尽清单。你可以根据需要包含任意多的上下文。

最佳实践​

  • 保持文件更新:定期更新 .zaihints 文件,以反映项目协议或优先级的变化。
  • 保持简洁:确保内容直接了当、切中要点,让 zai 能够快速解析并根据信息采取行动。
  • 从小处着手:创建一小组清晰、具体的提示,并根据需要逐步扩展。这有助于更容易地了解 zai 如何解释和应用你的指令。
  • 引用其他文件:将 zai 指向 /docs/style.md 或 /scripts/validation.js 等相关文件,以减少重复并保持指令轻量。

自定义上下文文件​

zai 默认查找 AGENTS.md 然后是 .zaihints 文件,但你可以通过 CONTEXT_FILE_NAMES 环境变量配置不同的文件名或多个上下文文件。这在以下情况很有用:

  • 工具兼容性:使用其他 AI 工具的约定(例如 CLAUDE.md)
  • 组织管理:将常用的规则拆分到多个自动加载的文件中
  • 项目约定:使用项目既有工具链的上下文文件(.cursorrules)

它的工作方式如下:

  1. zai 在全局(~/.config/zai/)和本地项目位置查找每个已配置的文件名
  2. 会话启动时,zai 从你的工作目录层级加载匹配的文件
  3. 会话期间,当 zai 访问嵌套子目录时,可以加载额外的匹配文件
  4. 所有找到的文件都会被加载并合并到上下文中

配置​

将 CONTEXT_FILE_NAMES 环境变量设置为 JSON 数组形式的文件名。默认值为 ["AGENTS.md", ".zaihints"]。

# Single custom file
export CONTEXT_FILE_NAMES='["AGENTS.md"]'

# Project toolchain files
export CONTEXT_FILE_NAMES='[".cursorrules", "AGENTS.md"]'

# Multiple files
export CONTEXT_FILE_NAMES='["CLAUDE.md", ".zaihints", "project_rules.txt"]'