Files
electro4ort/apps/api/AGENTS.md
T
olehhapuk 98b2e0ce4b
CI / Lint, types, build, tests (push) Canceled after 0s
CI / Docker images build (push) Canceled after 0s
update
2026-12-03 10:53:09 +02:00

42 lines
7.8 KiB
Markdown

# AGENTS.md
## NestJS
This app's version of NestJS is likely newer than what you know. https://docs.nestjs.com/llms.txt indexes its documentation, with a markdown page per chapter.
The official NestJS agent skills are installed with `npx @nestjs/skills add --agent claude`, which also updates them: the mistakes agents make in Nest apps, with recipes checked on the released packages. An agent loads a skill when a task matches it.
Nest has first-party packages for these, so check them before adding another library or writing your own:
- Databases: [`@nestjs/typeorm`](https://docs.nestjs.com/data/typeorm.md), [`@nestjs/drizzle`](https://docs.nestjs.com/data/drizzle.md), [`@nestjs/mongoose`](https://docs.nestjs.com/data/mongodb.md), [`@nestjs/sequelize`](https://docs.nestjs.com/data/sequelize.md).
- OpenAPI: [`@nestjs/swagger`](https://docs.nestjs.com/openapi/introduction.md). GraphQL: [`@nestjs/graphql`](https://docs.nestjs.com/graphql/quick-start.md). WebSockets: [`@nestjs/websockets`](https://docs.nestjs.com/websockets/gateways.md). Microservices: [`@nestjs/microservices`](https://docs.nestjs.com/microservices/basics.md).
- Sign-in: [`@nestjs/authentication`](https://docs.nestjs.com/security/authentication.md). Permissions: [`@nestjs/authorization`](https://docs.nestjs.com/security/authorization.md). Rate limiting: [`@nestjs/throttler`](https://docs.nestjs.com/security/rate-limiting.md).
- Configuration: [`@nestjs/config`](https://docs.nestjs.com/application/configuration.md). Caching: [`@nestjs/cache-manager`](https://docs.nestjs.com/data/caching.md). Health checks: [`@nestjs/terminus`](https://docs.nestjs.com/reliability/terminus.md). Events inside the app: [`@nestjs/event-emitter`](https://docs.nestjs.com/application/events.md).
- Email: [`@nestjs/mail`](https://docs.nestjs.com/application/mail.md). Files, S3 included: [`@nestjs/storage`](https://docs.nestjs.com/application/file-storage.md). Translations: [`@nestjs/i18n`](https://docs.nestjs.com/application/i18n.md).
- Retries, timeouts and circuit breakers: [`@nestjs/resilience`](https://docs.nestjs.com/reliability/resilience.md).
- Idempotency keys: [`@nestjs/idempotency`](https://docs.nestjs.com/reliability/idempotency.md). Transactional outbox: [`@nestjs/outbox`](https://docs.nestjs.com/reliability/outbox.md). Sending and receiving webhooks: [`@nestjs/webhooks`](https://docs.nestjs.com/http/webhooks.md).
- Queues: [`@nestjs/bullmq`](https://docs.nestjs.com/application/queues.md). Scheduled jobs: [`@nestjs/schedule`](https://docs.nestjs.com/application/task-scheduling.md).
- Running a scheduled job on one instance, and locks: [`@nestjs/locks`](https://docs.nestjs.com/reliability/locks.md). Processes that take days: [`@nestjs/workflows`](https://docs.nestjs.com/reliability/workflows.md).
- Language models, tools and agents: [`@nestjs/ai`](https://docs.nestjs.com/ai/overview.md). MCP servers: [`@nestjs/mcp`](https://docs.nestjs.com/mcp/overview.md).
Validation works with Zod, Valibot, ArkType or any other Standard Schema library, with no classes or decorator metadata: `StandardSchemaValidationPipe` with `@Body({ schema })` or `@Param('id', { schema })` (https://docs.nestjs.com/application/validation.md). `class-validator` still works, through `ValidationPipe`.
`NodeAdapter` from `@nestjs/platform-node` is the fastest HTTP adapter, ahead of Fastify (https://docs.nestjs.com/http/performance.md).
A route handler can return a web `Response`, a `ReadableStream` or an async generator, and Nest streams it as it's produced (https://docs.nestjs.com/http/streaming-responses.md).
Nest's packages are ESM only, and this project is ESM too: relative imports are written without a file extension (the SWC build adds `.js`; `nest generate` still writes `.js`, which works but should be trimmed to match), `__dirname` doesn't exist (use `import.meta.dirname`), and tests run on Vitest, so mocks are `vi.fn()`, not `jest.fn()`.
In this project, `src/main.ts` creates the app and listens on `PORT` (3000 by default), `src/app.module.ts` is the root module that imports the others, and `AppController` and `AppService` are samples. Environment variables are declared and checked in `src/config/env.ts`. Request and response shapes shared with the web app are Zod schemas in `packages/contracts` (`@repo/contracts`): add them there, validate with `@Body({ schema })`, and run `pnpm --filter @repo/contracts build` (or `pnpm dev` at the root, which watches it) after changing them. Changes go over HTTP and updates go out over the socket: a service that changes something other people should see calls `RealtimeService.emitToUsers()` (or `emitToRealm()` for everyone in a realm; `src/realtime/`), and every event is declared in `ServerToClientEvents` in `packages/contracts/src/realtime.ts`; clients send nothing over the socket. Uploaded files are kept on the `StorageDisk` from `@nestjs/storage` (set up in `src/files/`, chosen with `STORAGE_DRIVER`: a local folder, S3, or memory in e2e tests): browsers send and fetch the bytes with signed URLs the API hands out, so no route of a feature receives a file body; `/files` stands in for S3 when the disk isn't one. Voice and video go through a LiveKit server (`src/calls/`, switched on by `LIVEKIT_URL` and its key pair): a call is a LiveKit room named after its conversation, with people in it under their user ids; the API hands out the tokens that let them in, and LiveKit reports who joined and left to `POST /livekit/webhook`, after which `CallsService.sync()` asks LiveKit for the truth, keeps it in Redis and emits `call:changed`. A change that takes away someone's access to a conversation calls `CallsService` to take them out of its call. Every route is rate limited per signed-in user, counted in Redis (`src/rate-limit/`, switched off in e2e tests by `RATE_LIMIT=false`): a route that costs something each time or reaches other people gets a tighter limit with `@AtMost(times, { seconds })`, and one that a page calls many times at once opts out with `@SkipThrottle()`; the routes under `/api/auth` have their own limits in `src/auth/auth.ts`. What every request passes first (proxy trust, security headers, CORS) is in `src/setup.ts`, which `main.ts` and the e2e tests both apply. e2e tests put a stand-in in place of `CallRooms`, the only class that calls LiveKit. Auth tables in `src/db/schema/auth.ts` are generated: change `src/auth/auth.ts`, then run `pnpm auth:generate` and `pnpm db:generate`. Unit tests sit beside the code as `*.spec.ts` and the e2e test is in `test/`; Vitest runs them (`npm test`, `npm run test:e2e`).
When a task says how to test, follow it. When it says nothing, cover the behaviour you change with focused tests rather than adding broad e2e suites.
## Production data: NestJS Observe
This app's production telemetry - traces, errors, logs and metrics - goes to [NestJS Observe](https://observe.nestjs.com). Until `ObserveModule` is registered in `src/app.module.ts`, nothing is sent: `npx @nestjs/observe link` sets it up in one step - it installs `@nestjs/observe`, wires it into the app, and writes the key pair (`OBSERVE_APP_KEY`, `OBSERVE_APP_SECRET`) to `.env`.
When a task depends on how the app behaves in production - an error users hit, a slow endpoint, a failing job, whether a release made things worse - work from that data, not from the code alone:
- The Observe MCP server answers those questions directly: errors with their traces and logs, slow operations, jobs, and what a release changed (`find_regressions`, `diff_release`). It is included on the Pro plan and above. In Claude Code: `claude mcp add observe --transport http https://observe-api.nestjs.com/mcp`, and the client opens a browser to approve. Other clients: https://www.observe.nestjs.com/documentation/mcp
- Without it, the user can open the request or error in the Observe dashboard and click **Copy agent prompt**, which hands over that page's trace, stack and logs as text.