1. 前端 AI 辅助开发模式决策矩阵

工作模式核心定位典型应用场景交互界面与控制粒度风险与架构防御策略
Ask 模式技术顾问与概念答疑框架特性版本验证、CLI 参数查询、A11y 准则咨询侧边栏对话;无代码侵入,纯文本/代码块输出校验生成代码的时效性,防止混入旧版 NgModule 模式
Edit 模式行内定向重构与微调模板 A11y 增强、表单校验规则微调、局部 CSS 优化Cmd + I 行内浮层;精确到行级 Diff 审查严控选中代码上下文边界,禁止跨方法大面积重写
Agent 模式跨文件脚手架与用例扩增单元测试边界用例合成、复杂 Mock 数据生成、DTO 骨架自动化任务面板;支持多文件遍历与编辑禁止用于核心领域架构推演,必须配合强类型约束
持久化规范 (Instructions)全局思维护栏与契约注入团队代码风格对齐、框架弃用 API 屏蔽、测试模板锁定.github/instructions/*.md 文件自动检索避免单文件过长,按领域解耦为独立指令模块

2. 持久化指令规范配置范式

通过分层 Markdown 规范文件将工程约束持久化注入到 IDE 的上下文中:

全局规范入口 (.github/copilot-instructions.md)

markdown
# Bookstore 前端工程全局 AI 研发规范
 
## 1. 核心框架与语法基线
- 严格遵循 Angular 22 与 TypeScript 5.8+ 规范。
- 严禁生成任何 `@NgModule`、`*ngIf`、`*ngFor`、`*ngSwitch` 代码。
- 所有组件必须为 Standalone 独立组件。
- 模板必须使用原生块级控制流:`@if`、`@for (track item.id)`、`@switch`。
- 组件输入输出必须使用 `input()` 与 `output()` 信号函数,严禁使用 `@Input()` 与 `@Output()` 装饰器。
- 依赖注入统一使用函数式 `inject()`,禁止在构造函数中堆砌注入参数。
 
## 2. 测试规范
- 单元测试运行在 Vitest 体系下,严禁引入 Jasmine 或 Karma 依赖。
- 异步断言优先使用 `await fixture.whenStable()`。
- 依赖 Mock 统一采用 Vitest 原生 API(`vi.fn()` / `vi.spyOn()`)。

响应式表单指令 (.github/instructions/forms.instructions.md)

markdown
# 响应式表单指令集 (Reactive Forms Instructions)
 
- 统一使用 `NonNullableFormBuilder`(`fb.nonNullable.group()`)。
- 控件默认必须设置 `nonNullable: true`,杜绝不必要的 `null` 类型污染。
- 提取完整提交载荷必须使用 `form.getRawValue()`,防止处于禁用状态的字段被遗漏。
- 异步校验器必须配置 `updateOn: 'blur'`,严禁在击键事件中直接发起网络请求。

3. Agent 模式测试生成生产范式

借助明确的指令规范,利用 Agent 模式自动合成覆盖数据渲染、用户交互、事件发射与空状态的 Vitest 测试用例:

typescript
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BookList } from './book-list';
import { By } from '@angular/platform-browser';
import { describe, it, expect, beforeEach, vi } from 'vitest';
 
describe('BookList Component - Integration Tests', () => {
  let component: BookList;
  let fixture: ComponentFixture<BookList>;
 
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [BookList], // Standalone 独立组件直接导入
    }).compileComponents();
 
    fixture = TestBed.createComponent(BookList);
    component = fixture.componentInstance;
  });
 
  it('表格行数应与注入的图书数据集长度严格一致', async () => {
    fixture.componentRef.setInput('books', [
      { id: 1, title: 'Clean Architecture', authorName: 'Robert C. Martin', price: 34.99, published: '2017-09-20', isbn: '978-0134494166', genre: 'Architecture' },
      { id: 2, title: 'Designing Data-Intensive Applications', authorName: 'Martin Kleppmann', price: 45.00, published: '2017-03-16', isbn: '978-1449373320', genre: 'Distributed Systems' }
    ]);
    await fixture.whenStable();
 
    const rows = fixture.debugElement.queryAll(By.css('tr.clickable-row'));
    expect(rows.length).toBe(2);
  });
 
  it('点击操作按钮应触发 Output 信号发射对应图书主键', async () => {
    const selectSpy = vi.fn();
    component.bookSelected.subscribe(selectSpy);
 
    const testBook = { id: 1, title: 'Refactoring', authorName: 'Martin Fowler', price: 39.99, published: '2018-11-20', isbn: '978-0134757599', genre: 'Software Engineering' };
    fixture.componentRef.setInput('books', [testBook]);
    await fixture.whenStable();
 
    const selectBtn = fixture.debugElement.query(By.css('button[aria-label="选择图书"]'));
    selectBtn.triggerEventHandler('click', new MouseEvent('click'));
    await fixture.whenStable();
 
    expect(selectSpy).toHaveBeenCalledWith(testBook);
  });
 
  it('当图书数据为空时能够稳定展示空占位区域', async () => {
    fixture.componentRef.setInput('books', []);
    await fixture.whenStable();
 
    const rows = fixture.debugElement.queryAll(By.css('tr.clickable-row'));
    expect(rows.length).toBe(0);
  });
});

4. 前端 AI 辅助工程避坑清单 (Do's & Don'ts)

错误实践 (Don't)正确实践 (Do)
依赖无上下文的即兴提示词(ad-hoc prompts)让 AI 直接生成完整业务组件。建立版本化的 .github/instructions/*.md 指令库,将架构决策固化为模型提示词上下文。
放任 AI 按照过时训练数据生成带有 NgModule、*ngFor 或混用 Zone.js 的代码。在全局提示词中显式设置弃用黑名单,强制要求 Standalone、原生控制流与 Signals 原语。
盲目接受 Agent 模式一键生成的代码而不经过类型核验与断言审查。严格限制 Agent 仅用于边界测试用例扩增与 Mock 数据构造,核心领域逻辑实施严格 Code Review。
使用模糊、情绪化或宽泛的提示词(如“请让这个表单看起来更现代更酷”)。给出明确的技术维度(如“基于 MatFormField 补齐 mat-error,并在失焦时展示校验提示”)。