1---2name: convex-components3description: Use Convex Components to add isolated backend features and compose component APIs. Use for installing components, calling component APIs, authoring components, and handling component-specific constraints (Id types, env vars, pagination, auth). Use proactively when users mention components, workpool, workflow, agent component, or reusable backend modules. Examples: - user: "Install the Agent component" → add convex.config.ts + use() + components API - user: "Call component functions" → ctx.runQuery(components.foo.bar, args) - user: "Build a component" → defineComponent, schema, _generated, packaging - user: "Expose component API to clients" → re-export functions with auth4---5
6<overview>
7Use Convex Components to add isolated backend features and compose component APIs.
8</overview>
9
10<reference>
11- **Components overview**: https://docs.convex.dev/components
12- **Understanding**: https://docs.convex.dev/components/understanding
13- **Using**: https://docs.convex.dev/components/using
14- **Authoring**: https://docs.convex.dev/components/authoring
15- **Directory**: https://convex.dev/components
16</reference>
17
18<context name="Component References">
19You SHOULD consult these reference files for specific component knowledge:
20- `references/agent.md` — Agent component: threads/messages, streaming, tools, context, debugging, usage tracking, and install/setup.
21- `references/rag.md` — RAG component: namespaces, add/search/generateText, filters, chunking, prompt vs tool RAG.
22- `references/workpool.md` — Workpool component: enqueue, retries, onComplete, parallelism, batching, monitoring.
23- `references/workflow.md` — Workflow component: durable steps, events, retries, status, cancel/cleanup, limits.
24</context>
25
26<rules>
27
28### Mental Model
29- Components are isolated mini backends with their own schema, tables, file storage, and functions.
30- Components MUST NOT access app tables/functions/env unless passed explicitly.
31- Calls into components are transactional with the caller, but component mutations are sub-transactions.
32
33### Installing Components
34- Install package: `npm i @convex-dev/<component>`.
35- Add `convex/convex.config.ts`:
36 - `import { defineApp } from "convex/server";`
37 - `app.use(component)`; use `app.use(component, { name: "custom" })` for multiple instances.
38- You MUST run `npx convex dev` to generate component code.
39- Access via `components.<name>` in `convex/_generated/api`.
40
41### Calling Component APIs
42- Use `ctx.runQuery/Mutation/Action` with `components.<name>.<fn>`.
43- Public component functions MUST NOT be called directly from clients (they are internal references).
44- Queries remain reactive; mutations are transactional.
45
46### Transaction Semantics
47- Top-level mutation commits all writes across components together.
48- If a component mutation throws, only its writes are rolled back; the caller MAY catch and continue.
49
50### Component API Differences
51- `components.<name>` exposes ONLY public component functions.
52- `Id` types cross boundaries as `string`; You MUST NOT use `v.id("table")` for external tables.
53- Each component has its own `_generated` directory; You MUST use the app's `components` references.
54
55### Environment Variables
56- Components MUST NOT access `process.env` directly.
57- You MUST pass env values as arguments from the app, or store config in a component table.
58
59### HTTP Actions
60- Components MUST NOT expose routes directly; app MUST mount handlers in `convex/http.ts`.
61
62### Auth in Components
63- `ctx.auth` is NOT available inside component functions.
64- You MUST authenticate in the app and pass identifiers (userId) to component functions.
65
66### Pagination
67- Built-in `.paginate()` is NOT supported inside components.
68- You SHOULD use `convex-helpers` paginator and `usePaginatedQuery` from convex-helpers if needed.
69
70### Authoring Components
71- Component folder MUST include `convex.config.ts`, `schema.ts`, functions, and `_generated`.
72- `defineComponent("name")` defines component; use `component.use(...)` for child components.
73- Local components MAY live in `convex/components/` or any folder.
74- NPM components SHOULD export:
75 - `@pkg/convex.config.js`
76 - `@pkg/_generated/component.js`
77 - `@pkg/test` helpers
78
79### Function Handles
80- You SHOULD use `createFunctionHandle(api.foo.bar)` to pass callbacks across boundaries.
81- Handles are strings; use `v.string()` validators and cast back to `FunctionHandle`.
82
83### Testing Components
84- You SHOULD register component with `convex-test` using component schema/modules or provided test helpers.
85- For component packages, You SHOULD use `@pkg/test` register helper.
86
87### Best Practices
88- You MUST always validate args/returns on public component functions.
89- You SHOULD prefer app-level wrappers to add auth/rate limiting when re-exporting component APIs.
90- You SHOULD use a single-row globals/config table for static configuration.
91
92</rules>