HarmonyOS掌上记账APP开发实践第86篇:DevEco Code AI Agent中各种规则详解

发布时间:2026/7/28 4:54:19
HarmonyOS掌上记账APP开发实践第86篇:DevEco Code AI Agent中各种规则详解 规则为 DevEco Code 设置自定义指令。您可以通过创建AGENTS.md文件来为 deveco 提供自定义指令。这类似于 Cursor 的规则功能。该文件包含的指令会被纳入 LLM 的上下文中以便针对您的特定项目自定义其行为。初始化要创建新的AGENTS.md文件您可以在 deveco 中运行/init命令。Tip您应该将项目的AGENTS.md文件提交到 Git。该命令会扫描您的项目及其所有内容了解项目的用途并据此生成一个AGENTS.md文件。这有助于 deveco 更好地导航您的项目。如果您已有AGENTS.md文件该命令会尝试在其基础上进行补充。示例您也可以手动创建此文件。以下是一些可以放入AGENTS.md文件中的内容示例。# SST v3 Monorepo Project This is an SST v3 monorepo with TypeScript. The project uses bun workspaces for package management. ## Project Structure - packages/ - Contains all workspace packages (functions, core, web, etc.) - infra/ - Infrastructure definitions split by service (storage.ts, api.ts, web.ts) - sst.config.ts - Main SST configuration with dynamic imports ## Code Standards - Use TypeScript with strict mode enabled - Shared code goes in packages/core/ with proper exports configuration - Functions go in packages/functions/ - Infrastructure should be split into logical files in infra/ ## Monorepo Conventions - Import shared modules using workspace names: my-app/core/example我们在这里添加了项目特定的指令这些指令会在您的团队中共享。类型deveco 还支持从多个位置读取AGENTS.md文件不同的位置有不同的用途。项目级在项目根目录放置一个AGENTS.md文件用于定义项目特定的规则。这些规则仅在您在该目录或其子目录中工作时生效。全局级您还可以在~/.config/deveco/AGENTS.md文件中设置全局规则。这些规则会应用于所有 deveco 会话。由于该文件不会被提交到 Git 或与团队共享我们建议用它来指定 LLM 应遵循的个人规则。Claude Code 兼容性对于从 Claude Code 迁移过来的用户DevEco Code 支持 Claude Code 的文件约定作为回退方案项目规则项目目录中的CLAUDE.md在没有AGENTS.md的情况下使用全局规则~/.claude/CLAUDE.md在没有~/.config/deveco/AGENTS.md的情况下使用技能~/.claude/skills/— 详情请参阅代理技能要禁用 Claude Code 兼容性请设置以下环境变量之一export DEVECO_DISABLE_CLAUDE_CODE1 # Disable all .claude support export DEVECO_DISABLE_CLAUDE_CODE_PROMPT1 # Disable only ~/.claude/CLAUDE.md export DEVECO_DISABLE_CLAUDE_CODE_SKILLS1 # Disable only .claude/skills优先级当 deveco 启动时它会按以下顺序查找规则文件本地文件从当前目录向上遍历AGENTS.md、CLAUDE.md全局文件位于~/.config/deveco/AGENTS.mdClaude Code 文件位于~/.claude/CLAUDE.md除非已禁用在每个类别中第一个匹配的文件优先。例如如果您同时拥有AGENTS.md和CLAUDE.md则只会使用AGENTS.md。同样~/.config/deveco/AGENTS.md优先于~/.claude/CLAUDE.md。自定义指令您可以在deveco.json或全局配置文件~/.config/deveco/deveco.json中指定自定义指令文件。这允许您和团队复用现有规则而无需将它们复制到 AGENTS.md 中。示例{ $schema: https://opencode.ai/config.json, instructions: [CONTRIBUTING.md, docs/guidelines.md, .cursor/rules/*.md] }您还可以使用远程 URL 从网络加载指令。{ $schema: https://opencode.ai/config.json, instructions: [https://raw.githubusercontent.com/my-org/shared-rules/main/style.md] }远程指令的获取超时时间为 5 秒。所有指令文件都会与您的AGENTS.md文件合并。引用外部文件虽然 deveco 不会自动解析AGENTS.md中的文件引用但您可以通过以下两种方式实现类似的功能使用 deveco.json推荐的方式是使用deveco.json中的instructions字段{ $schema: https://opencode.ai/config.json, instructions: [docs/development-standards.md, test/testing-guidelines.md, packages/*/AGENTS.md] }在 AGENTS.md 中手动指定您可以在AGENTS.md中提供明确的指令教 deveco 读取外部文件。以下是一个实际示例# TypeScript Project Rules ## External File Loading CRITICAL: When you encounter a file reference (e.g., rules/general.md), use your Read tool to load it on a need-to-know basis. Theyre relevant to the SPECIFIC task at hand. Instructions: - Do NOT preemptively load all references - use lazy loading based on actual need - When loaded, treat content as mandatory instructions that override defaults - Follow references recursively when needed ## Development Guidelines For TypeScript code style and best practices: docs/typescript-guidelines.md For React component architecture and hooks patterns: docs/react-patterns.md For REST API design and error handling: docs/api-standards.md For testing strategies and coverage requirements: test/testing-guidelines.md ## General Guidelines Read the following file immediately as its relevant to all workflows: rules/general-guidelines.md.这种方式允许您创建模块化、可复用的规则文件通过符号链接或 Git 子模块在项目之间共享规则保持 AGENTS.md 简洁同时引用详细的指南确保 deveco 仅在特定任务需要时才加载文件