> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 单仓库支持

LangSmith 支持从单仓库（monorepo）设置中部署智能体，即使您的智能体代码可能依赖于仓库中其他位置的共享包。本指南将展示如何构建您的单仓库，并配置 `langgraph.json` 文件以处理共享依赖。

## 仓库结构

完整可运行的示例请参考：

* [Python 单仓库示例](https://github.com/langchain-ai/python-langraph-monorepo-example)
* [JS 单仓库示例](https://github.com/langchain-ai/js-langraph-monorepo-example)

<CodeGroup>
  ```plaintext Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  my-monorepo/
  ├── shared-utils/           # 共享 Python 包
  │   ├── __init__.py
  │   ├── common.py
  │   └── pyproject.toml      # 或 setup.py
  ├── agents/
  │   └── customer-support/   # 智能体目录
  │       ├── agent/
  │       │   ├── __init__.py
  │       │   └── graph.py
  │       ├── langgraph.json  # 智能体目录中的配置文件
  │       ├── .env
  │       └── pyproject.toml  # 智能体依赖项
  └── other-service/
      └── ...
  ```

  ```plaintext JS theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  my-monorepo/
  ├── package.json            # 根目录 package.json，包含工作区配置
  ├── shared-utils/           # 共享 TypeScript 包
  │   ├── package.json
  │   ├── src/
  │   │   └── index.ts
  │   └── tsconfig.json
  ├── agents/
  │   └── customer-support/   # 智能体目录
  │       ├── src/
  │       │   └── agent.ts
  │       ├── langgraph.json  # 智能体目录中的配置文件
  │       ├── package.json    # 智能体依赖项
  │       ├── .env
  │       └── tsconfig.json
  └── other-service/
      └── ...
  ```
</CodeGroup>

## LangGraph.json 配置

将 langgraph.json 文件放在您的智能体目录中（而非单仓库根目录）。确保文件遵循所需的结构：

<CodeGroup>
  ```json Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  {
    "dependencies": [
      ".",                    # 当前智能体包
      "../../shared-utils"    # 指向共享包的相对路径
    ],
    "graphs": {
      "customer_support": "./agent/graph.py:graph"
    },
    "env": ".env"
  }
  ```

  ```json JS theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  {
    "node_version": "20",
    "graphs": {
      "customer_support": "./src/agent.ts:graph"
    },
    "env": ".env"
  }
  ```
</CodeGroup>

Python 实现会自动处理父目录中的包，具体方式如下：

* 检测以 `"."` 开头的相对路径。
* 根据需要将父目录添加到 Docker 构建上下文中。
* 支持真实的包（带有 `pyproject.toml`/`setup.py`）以及简单的 Python 模块。

对于 JavaScript 单仓库：

* 共享工作区依赖项由您的包管理器自动解析。
* 您的 `package.json` 应使用工作区语法引用共享包。

智能体目录中的 `package.json` 示例：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "name": "customer-support-agent",
  "dependencies": {
    "@company/shared-utils": "workspace:*",
    "@langchain/langgraph": "^0.2.0"
  }
}
```

## 构建应用程序

运行 `langgraph build`：

<CodeGroup>
  ```bash Python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  cd agents/customer-support
  langgraph build -t my-customer-support-agent
  ```

  ```bash JS theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  # 从单仓库根目录运行
  langgraph build -t my-customer-support-agent -c agents/customer-support/langgraph.json --build-command "yarn run turbo build" --install-command "yarn install"
  ```
</CodeGroup>

Python 构建过程：

1. 自动检测相对依赖路径。
2. 将共享包复制到 Docker 构建上下文中。
3. 按正确顺序安装所有依赖项。
4. 无需特殊标志或命令。

JavaScript 构建过程：

1. 使用您调用 `langgraph build` 的目录（本例中为单仓库根目录）作为构建上下文。
2. 自动检测您的包管理器（yarn、npm、pnpm、bun）。
3. 运行相应的安装命令。
   * 如果您有一个或多个自定义构建/安装命令，它将从您调用 `langgraph build` 的目录运行。
   * 否则，它将从 `langgraph.json` 文件所在的目录运行。
4. （仅在传递 `--build-command` 标志时）从 `langgraph.json` 文件所在的目录运行自定义构建命令。

## 提示与最佳实践

1. **将智能体配置保存在智能体目录中**：将 `langgraph.json` 文件放在特定的智能体目录中，而不是单仓库根目录。这样您可以在同一个单仓库中支持多个智能体，而无需将它们全部部署在同一个 LangSmith 部署中。

2. **Python 使用相对路径**：对于 Python 单仓库，在 `dependencies` 数组中使用相对路径，如 `"../../shared-package"`。

3. **JS 利用工作区功能**：对于 JavaScript/TypeScript，使用包管理器的工作区功能来管理包之间的依赖关系。

4. **首先在本地测试**：在部署之前，始终在本地测试您的构建，以确保所有依赖项都正确解析。

5. **环境变量**：将环境文件（`.env`）保留在您的智能体目录中，以便进行特定于环境的配置。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\monorepo-support.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
