> ## Documentation Index
> Fetch the complete documentation index at: https://docs.brainstormer.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Shared Packages

> Shared package exports: types, utilities, crypto, prompt renderer, constants, and schemas used across all services.

## Overview

`@brainstormer/shared` is the shared package used across all backend services. It provides common types, utilities, constants, error classes, and Zod schemas.

## Exports

| Module                     | Purpose                                              |
| -------------------------- | ---------------------------------------------------- |
| `types/`                   | Shared TypeScript interfaces                         |
| `utils/`                   | Utility functions                                    |
| `utils/crypto.ts`          | AES-256-GCM encrypt/decrypt (platform config system) |
| `utils/prompt-renderer.ts` | Shared `renderPrompt(template, variables)` utility   |
| `constants/`               | Shared constants                                     |
| `errors/`                  | Custom error classes                                 |
| `schemas/`                 | Shared Zod schemas                                   |

## Crypto Utilities

The crypto module provides AES-256-GCM encryption used by the platform config system:

```typescript theme={null}
import { encrypt, decrypt } from "@brainstormer/shared";
```

* Key is derived from `JWT_SECRET` via scrypt
* Used to encrypt sensitive values in the `platform_config` table
* Used to encrypt sensitive conversation variables in `conversation_variables.sensitive_variables`

## Prompt Renderer

The prompt renderer provides Mustache-style variable interpolation:

```typescript theme={null}
import { renderPrompt } from "@brainstormer/shared";

interface RenderResult {
  rendered: string;
  unresolvedVariables: string[];
}

const result: RenderResult = renderPrompt(
  "Hi {{user_name}}! Welcome to {{bot_name}}.",
  { user_name: "John", bot_name: "SalesBot" }
);
// result.rendered = "Hi John! Welcome to SalesBot."
// result.unresolvedVariables = []
```

* Pure function, no side effects
* Handles nested/escaped braces gracefully
* Unresolved variables replaced with empty string
* Returns list of unresolved variables for logging

## Usage in Services

All services reference the shared package via npm workspaces:

```json theme={null}
{
  "dependencies": {
    "@brainstormer/shared": "file:../../packages/shared"
  }
}
```

## Build

Built as part of the monorepo. Each service Dockerfile builds shared first:

```dockerfile theme={null}
RUN cd packages/shared && (npm run build || true)
```

## Config Package

`packages/config` provides the base `tsconfig.json` that all services extend.
