# 同步文档以适应代码更改

副驾驶聊天 可帮助使代码文档保持最新状态。

随着对代码进行更改，使文档保持最新可能很困难。 然而，良好的文档对于维护代码库和确保开发人员能够有效地使用代码至关重要。 副驾驶聊天 可帮助更新现有代码文档。

## 示例方案

假设有一个 TypeScript 函数，该函数按类别名称检索产品，但文档已过期。

```typescript id=fetch-products-by-category
/**
 * Retrieves all products belonging to a specific category.
 * * @param categoryId - The unique identifier of the product category
 * @returns Promise that resolves to an array of Product objects
 * * @example
 * const products = await getByCategoryName(5);
 * // Returns: [{id: 1, name: "Product 1", categoryId: 5}, ...]
 * * @throws {PrismaClientKnownRequestError} If database query fails
 * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}
 */
async getByCategoryName(categoryId: number, page = 1, pageSize = 10): Promise<Products[]> {
  return await prisma.products.findMany({
    where: { categoryId },
    skip: (page - 1) * pageSize,
    take: pageSize,
    include: {
      reviews: {
        orderBy: { date: 'desc' },
        take: 5,
      },
    },
  });
}
```

## 示例提示

可以让 副驾驶聊天 根据当前代码更新文档。

```copilot copy prompt ref=fetch-products-by-category
Update the existing documentation for the getByCategoryName function to reflect the current implementation.
```

## 示例响应

> \[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。

Copilot 说明它将为 `getByCategoryName` 函数创建更新的文档。 然后，它提供更新的文档，其中现在包括 `page` 和 `pageSize` 参数，以及可能引发的 `PrismaClientKnownRequestError` 错误。

例如：

```typescript
/**
 * Retrieves all products belonging to a specific category with pagination and recent reviews.
 * * @param categoryId - The unique identifier of the product category
 * @param page - The page number for pagination (default: 1)
 * @param pageSize - Number of products per page (default: 10)
 * @returns Promise that resolves to an array of Product objects with their 5 most recent reviews
 * * @example
 * const products = await getByCategoryName(5, 2, 20);
 * // Returns: [{id: 1, name: "Product 1", categoryId: 5, reviews: [...5 recent reviews]}, ...]
 * * @throws {PrismaClientKnownRequestError} If database query fails
 * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}
 */
```

## 其他阅读材料

* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)
* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)