convex-components
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<overview>
Use Convex Components to add isolated backend features and compose component APIs.
</overview>
<reference>
- **Components overview**: https://docs.convex.dev/components
- **Understanding**: https://docs.convex.dev/components/understanding
- **Using**: https://docs.convex.dev/components/using
- **Authoring**: https://docs.convex.dev/components/authoring
- **Directory**: https://convex.dev/components
</reference>
<context name="Component References">
You SHOULD consult these reference files for specific component knowledge:
- `references/agent.md` — Agent component: threads/messages, streaming, tools, context, debugging, usage tracking, and install/setup.
- `references/rag.md` — RAG component: namespaces, add/search/generateText, filters, chunking, prompt vs tool RAG.
- `references/workpool.md` — Workpool component: enqueue, retries, onComplete, parallelism, batching, monitoring.
- `references/workflow.md` — Workflow component: durable steps, events, retries, status, cancel/cleanup, limits.
</context>
<rules>
<overview>
使用Convex Components添加独立的后端功能并组合组件API。
</overview>
<reference>
- **组件概览**:https://docs.convex.dev/components
- **组件详解**:https://docs.convex.dev/components/understanding
- **组件使用**:https://docs.convex.dev/components/using
- **组件编写**:https://docs.convex.dev/components/authoring
- **组件目录**:https://convex.dev/components
</reference>
<context name="组件参考文件">
你应查阅这些参考文件以获取特定组件的相关知识:
- `references/agent.md` — Agent组件:线程/消息、流式传输、工具、上下文、调试、使用跟踪以及安装/设置。
- `references/rag.md` — RAG组件:命名空间、添加/搜索/生成文本、过滤器、分块、提示词与工具RAG。
- `references/workpool.md` — Workpool组件:入队、重试、onComplete、并行处理、批处理、监控。
- `references/workflow.md` — Workflow组件:持久化步骤、事件、重试、状态、取消/清理、限制。
</context>
<rules>
Mental Model
心智模型
- Components are isolated mini backends with their own schema, tables, file storage, and functions.
- Components MUST NOT access app tables/functions/env unless passed explicitly.
- Calls into components are transactional with the caller, but component mutations are sub-transactions.
- 组件是独立的迷你后端,拥有自己的schema、表、文件存储和函数。
- 组件不得访问应用的表/函数/环境变量,除非被显式传入。
- 对组件的调用与调用者保持事务性,但组件的变更属于子事务。
Installing Components
安装组件
- Install package: .
npm i @convex-dev/<component> - Add :
convex/convex.config.tsimport { defineApp } from "convex/server";- ; use
app.use(component)for multiple instances.app.use(component, { name: "custom" })
- You MUST run to generate component code.
npx convex dev - Access via in
components.<name>.convex/_generated/api
- 安装包:。
npm i @convex-dev/<component> - 添加:
convex/convex.config.tsimport { defineApp } from "convex/server";- ;若要创建多个实例,使用
app.use(component)。app.use(component, { name: "custom" })
- 你必须运行以生成组件代码。
npx convex dev - 通过中的
convex/_generated/api访问组件。components.<name>
Calling Component APIs
调用组件API
- Use with
ctx.runQuery/Mutation/Action.components.<name>.<fn> - Public component functions MUST NOT be called directly from clients (they are internal references).
- Queries remain reactive; mutations are transactional.
- 使用配合
ctx.runQuery/Mutation/Action。components.<name>.<fn> - 不得直接从客户端调用公共组件函数(它们是内部引用)。
- 查询保持响应式;变更具备事务性。
Transaction Semantics
事务语义
- Top-level mutation commits all writes across components together.
- If a component mutation throws, only its writes are rolled back; the caller MAY catch and continue.
- 顶层变更会提交所有跨组件的写入操作。
- 如果组件变更抛出异常,仅回滚该组件的写入;调用者可捕获异常并继续执行。
Component API Differences
组件API差异
- exposes ONLY public component functions.
components.<name> - types cross boundaries as
Id; You MUST NOT usestringfor external tables.v.id("table") - Each component has its own directory; You MUST use the app's
_generatedreferences.components
- 仅暴露公共组件函数。
components.<name> - 类型跨边界时以
Id形式存在;不得对外部表使用string。v.id("table") - 每个组件都有自己的目录;必须使用应用的
_generated引用。components
Environment Variables
环境变量
- Components MUST NOT access directly.
process.env - You MUST pass env values as arguments from the app, or store config in a component table.
- 组件不得直接访问。
process.env - 你必须从应用中传入环境变量值,或在组件表中存储配置。
HTTP Actions
HTTP动作
- Components MUST NOT expose routes directly; app MUST mount handlers in .
convex/http.ts
- 组件不得直接暴露路由;应用必须在中挂载处理程序。
convex/http.ts
Auth in Components
组件中的认证
- is NOT available inside component functions.
ctx.auth - You MUST authenticate in the app and pass identifiers (userId) to component functions.
- 组件函数内部无法使用。
ctx.auth - 你必须在应用中完成认证,然后将标识符(userId)传入组件函数。
Pagination
分页
- Built-in is NOT supported inside components.
.paginate() - You SHOULD use paginator and
convex-helpersfrom convex-helpers if needed.usePaginatedQuery
- 组件内部不支持内置的。
.paginate() - 如有需要,应使用分页器和来自convex-helpers的
convex-helpers。usePaginatedQuery
Authoring Components
编写组件
- Component folder MUST include ,
convex.config.ts, functions, andschema.ts._generated - defines component; use
defineComponent("name")for child components.component.use(...) - Local components MAY live in or any folder.
convex/components/ - NPM components SHOULD export:
@pkg/convex.config.js@pkg/_generated/component.js- helpers
@pkg/test
- 组件文件夹必须包含、
convex.config.ts、函数以及schema.ts。_generated - 用于定义组件;使用
defineComponent("name")引入子组件。component.use(...) - 本地组件可存放于或任意文件夹。
convex/components/ - NPM组件应导出:
@pkg/convex.config.js@pkg/_generated/component.js- 辅助工具
@pkg/test
Function Handles
函数句柄
- You SHOULD use to pass callbacks across boundaries.
createFunctionHandle(api.foo.bar) - Handles are strings; use validators and cast back to
v.string().FunctionHandle
- 你应使用在边界之间传递回调。
createFunctionHandle(api.foo.bar) - 句柄为字符串类型;使用验证器并转换回
v.string()。FunctionHandle
Testing Components
测试组件
- You SHOULD register component with using component schema/modules or provided test helpers.
convex-test - For component packages, You SHOULD use register helper.
@pkg/test
- 你应使用组件的schema/模块或提供的测试辅助工具,通过注册组件。
convex-test - 对于组件包,你应使用注册辅助工具。
@pkg/test
Best Practices
最佳实践
- You MUST always validate args/returns on public component functions.
- You SHOULD prefer app-level wrappers to add auth/rate limiting when re-exporting component APIs.
- You SHOULD use a single-row globals/config table for static configuration.
- 必须始终验证公共组件函数的参数/返回值。
- 重新导出组件API时,应优先通过应用级包装器添加认证/速率限制。
- 应使用单行全局/配置表存储静态配置。