|
| 1 | +# Local Development |
| 2 | + |
| 3 | +This guide explains how to set up and develop this application locally using the monorepo structure with npm workspaces. |
| 4 | + |
| 5 | +## Monorepo Structure |
| 6 | + |
| 7 | +This repository is organized as a **monorepo** using [npm workspaces](https://docs.npmjs.com/cli/v7/using-npm/workspaces). A monorepo allows multiple related packages to be managed in a single repository, making it easier to share code and maintain consistency. |
| 8 | + |
| 9 | +### Package Structure |
| 10 | + |
| 11 | +The repository contains two main workspaces in the `packages-v1` directory: |
| 12 | + |
| 13 | +``` |
| 14 | +azure-typescript-langchainjs/ |
| 15 | +├── packages-v1/ |
| 16 | +│ ├── langgraph-agent/ # Core agent implementation |
| 17 | +│ └── server-api/ # Fastify API server |
| 18 | +├── package.json # Root package with workspace config |
| 19 | +└── azure.yaml # Azure deployment configuration |
| 20 | +``` |
| 21 | + |
| 22 | +#### langgraph-agent |
| 23 | + |
| 24 | +The core agent implementation package containing: |
| 25 | +- **LangGraph agent architecture** for orchestrating AI components |
| 26 | +- **Azure OpenAI integration** for embeddings and completions |
| 27 | +- **Azure AI Search vector store** integration |
| 28 | +- **PDF document processing** and loading scripts |
| 29 | +- **Reusable utilities** for Azure credential management |
| 30 | + |
| 31 | +#### server-api |
| 32 | + |
| 33 | +The API server package that: |
| 34 | +- Exposes the agent functionality via REST API |
| 35 | +- Uses [Fastify](https://www.fastify.io/) web framework |
| 36 | +- Depends on the `langgraph-agent` package |
| 37 | +- Handles HTTP requests to the `/answer` endpoint |
| 38 | + |
| 39 | +### Separation of Concerns |
| 40 | + |
| 41 | +Each workspace has a clear responsibility: |
| 42 | +- **langgraph-agent**: Business logic, AI orchestration, data processing |
| 43 | +- **server-api**: HTTP layer, request/response handling, API contracts |
| 44 | + |
| 45 | +This separation makes it easy to: |
| 46 | +- Test agent logic independently |
| 47 | +- Reuse the agent in different contexts (CLI, API, batch processing) |
| 48 | +- Scale different components independently |
| 49 | + |
| 50 | +## Setting Up Local Development |
| 51 | + |
| 52 | +### 1. Install Dependencies |
| 53 | + |
| 54 | +From the repository root: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Install all dependencies for all workspaces |
| 58 | +npm install |
| 59 | +``` |
| 60 | + |
| 61 | +This will install dependencies for both the root project and all workspace packages. |
| 62 | + |
| 63 | +### 2. Configure Environment Variables |
| 64 | + |
| 65 | +**Prerequisites**: Complete the `azd up` deployment first (see [Getting Started](./01-getting-started.md)), which automatically creates the `.env` file with all necessary Azure resource information. |
| 66 | + |
| 67 | +If you need to manually update environment variables, edit the `.env` file in the repository root: |
| 68 | + |
| 69 | +```bash |
| 70 | +# Azure OpenAI |
| 71 | +AZURE_OPENAI_EMBEDDING_INSTANCE="your-openai-instance" |
| 72 | +AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-small" |
| 73 | +AZURE_OPENAI_EMBEDDING_API_VERSION="2023-05-15" |
| 74 | + |
| 75 | +AZURE_OPENAI_COMPLETE_INSTANCE="your-openai-instance" |
| 76 | +AZURE_OPENAI_COMPLETE_MODEL="gpt-4.1-mini" |
| 77 | +AZURE_OPENAI_COMPLETE_API_VERSION="2025-01-01-preview" |
| 78 | +AZURE_OPENAI_COMPLETE_MAX_TOKENS=1000 |
| 79 | + |
| 80 | +# Azure AI Search |
| 81 | +AZURE_AISEARCH_ENDPOINT="https://your-search-service.search.windows.net" |
| 82 | +AZURE_AISEARCH_INDEX_NAME="northwind" |
| 83 | + |
| 84 | +# Authentication (use passwordless for local dev) |
| 85 | +SET_PASSWORDLESS=true |
| 86 | +``` |
| 87 | + |
| 88 | +**Note**: For local development, use passwordless authentication (`SET_PASSWORDLESS=true`) with Azure CLI login. See [Azure Identity Authentication](./05-azure-identity-authentication.md) for details. |
| 89 | + |
| 90 | +### 3. Build All Packages |
| 91 | + |
| 92 | +```bash |
| 93 | +# Build all workspaces |
| 94 | +npm run build |
| 95 | +``` |
| 96 | + |
| 97 | +This command: |
| 98 | +1. Builds all workspace packages using their individual `build` scripts |
| 99 | +2. Links packages together (via `postbuild` script) |
| 100 | +3. Creates compiled JavaScript in each package's `dist/` directory |
| 101 | + |
| 102 | +## Key npm Scripts |
| 103 | + |
| 104 | +The root `package.json` defines several useful scripts that work across all workspaces: |
| 105 | + |
| 106 | +### Building |
| 107 | + |
| 108 | +```bash |
| 109 | +# Build all packages |
| 110 | +npm run build |
| 111 | + |
| 112 | +# Build specific workspace |
| 113 | +npm run build --workspace=packages-v1/langgraph-agent |
| 114 | +``` |
| 115 | + |
| 116 | +### Running the Server |
| 117 | + |
| 118 | +```bash |
| 119 | +# Run the server in development mode (builds and runs with hot reload) |
| 120 | +npm run dev |
| 121 | + |
| 122 | +# Or run production mode after building |
| 123 | +npm run build |
| 124 | +npm start |
| 125 | +``` |
| 126 | + |
| 127 | +The API server will start on `http://localhost:3000`. |
| 128 | + |
| 129 | +### Working with Vector Data |
| 130 | + |
| 131 | +**Note**: If you deployed with `azd up`, vector data is already loaded. These commands are only needed if you're setting up from scratch without using `azd up`. |
| 132 | + |
| 133 | +```bash |
| 134 | +# Load PDF documents into Azure AI Search index (only needed if not using azd up) |
| 135 | +npm run load_data |
| 136 | + |
| 137 | +# Test embedding generation (useful for verifying Azure OpenAI authentication) |
| 138 | +npm run embed |
| 139 | + |
| 140 | +# Test LLM completion (useful for verifying Azure OpenAI authentication) |
| 141 | +npm run llm |
| 142 | +``` |
| 143 | + |
| 144 | +### Docker |
| 145 | + |
| 146 | +```bash |
| 147 | +# Build Docker image |
| 148 | +npm run build:docker |
| 149 | + |
| 150 | +# Build and run in Docker |
| 151 | +npm run start:docker |
| 152 | +``` |
| 153 | + |
| 154 | +### Cleanup |
| 155 | + |
| 156 | +```bash |
| 157 | +# Clean build artifacts from all packages |
| 158 | +npm run clean |
| 159 | +``` |
| 160 | + |
| 161 | +## Development Workflow |
| 162 | + |
| 163 | +### 1. Make Changes to Agent Logic |
| 164 | + |
| 165 | +Edit files in `packages-v1/langgraph-agent/src/`: |
| 166 | + |
| 167 | +```bash |
| 168 | +# Make changes to agent code |
| 169 | +vim packages-v1/langgraph-agent/src/graph.ts |
| 170 | + |
| 171 | +# Rebuild the package |
| 172 | +npm run build --workspace=packages-v1/langgraph-agent |
| 173 | +``` |
| 174 | + |
| 175 | +### 2. Make Changes to API Server |
| 176 | + |
| 177 | +Edit files in `packages-v1/server-api/src/`: |
| 178 | + |
| 179 | +```bash |
| 180 | +# Make changes to server code |
| 181 | +vim packages-v1/server-api/src/server.ts |
| 182 | + |
| 183 | +# Rebuild and run |
| 184 | +npm run dev --workspace=packages-v1/server-api |
| 185 | +``` |
| 186 | + |
| 187 | +### 3. Test Your Changes |
| 188 | + |
| 189 | +See [Testing with HTTP Files](./04-testing-with-http-files.md) for details on testing the API. |
| 190 | + |
| 191 | +## Workspace-Specific Commands |
| 192 | + |
| 193 | +You can run commands in specific workspaces: |
| 194 | + |
| 195 | +```bash |
| 196 | +# Run a command in langgraph-agent |
| 197 | +npm run <script> --workspace=packages-v1/langgraph-agent |
| 198 | + |
| 199 | +# Run a command in server-api |
| 200 | +npm run <script> --workspace=packages-v1/server-api |
| 201 | +``` |
| 202 | + |
| 203 | +## TypeScript Configuration |
| 204 | + |
| 205 | +Each package has its own `tsconfig.json` for TypeScript compilation: |
| 206 | + |
| 207 | +- **langgraph-agent**: Configured for ES modules with Node18 target |
| 208 | +- **server-api**: Similar configuration, references langgraph-agent types |
| 209 | + |
| 210 | +## Troubleshooting |
| 211 | + |
| 212 | +### Build Errors |
| 213 | + |
| 214 | +If you encounter build errors: |
| 215 | + |
| 216 | +```bash |
| 217 | +# Clean and rebuild everything |
| 218 | +npm run clean |
| 219 | +npm install |
| 220 | +npm run build |
| 221 | +``` |
| 222 | + |
| 223 | +### Module Not Found Errors |
| 224 | + |
| 225 | +If the server can't find the agent package: |
| 226 | + |
| 227 | +```bash |
| 228 | +# Re-link packages |
| 229 | +npm run build |
| 230 | +``` |
| 231 | + |
| 232 | +The `postbuild` script handles linking, but you may need to run it manually. |
| 233 | + |
| 234 | +### Environment Variable Issues |
| 235 | + |
| 236 | +Ensure you're running commands from the repository root where the `.env` file is located. The npm scripts use `--env-file` to load environment variables. |
| 237 | + |
| 238 | +## Additional Resources |
| 239 | + |
| 240 | +- [npm Workspaces Documentation](https://docs.npmjs.com/cli/v7/using-npm/workspaces) |
| 241 | +- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/intro.html) |
| 242 | +- [Node.js ES Modules](https://nodejs.org/api/esm.html) |
| 243 | +- [Fastify Documentation](https://www.fastify.io/) |
0 commit comments