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

# 单体仓库支持

LangSmith 支持从单体仓库设置中部署代理，其中您的代理代码可能依赖于位于仓库其他位置的共享包。本指南展示如何构建您的单体仓库并配置 `langgraph.json` 文件以与共享依赖项协同工作。

## 仓库结构

完整的可运行示例，请参阅：

* [Python 单体仓库示例](https://github.com/langchain-ai/python-langraph-monorepo-example)
* [JS 单体仓库示例](https://github.com/langchain-ai/js-langgraph-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. 可选地从 `langgraph.json` 文件所在的目录运行自定义构建命令（仅当您传递 `--build-command` 标志时）。

## 提示和最佳实践

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

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

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

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

5. **环境变量**：将环境文件（`.env`）保留在您的代理目录中，用于特定于环境的配置。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/monorepo-support.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
