iii-functions-and-triggers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFunctions & Triggers
函数与触发器
Comparable to: Serverless function runtimes, Lambda, Cloud Functions
可类比于:Serverless函数运行时、Lambda、Cloud Functions
Key Concepts
核心概念
Use the concepts below when they fit the task. Not every worker needs all of them.
- A Function is an async handler registered with a unique ID
- A Trigger binds an event source to a function — types include http, queue, cron, state, stream, and subscribe
- Functions invoke other functions via regardless of language or worker location
trigger() - The engine handles serialization, routing, and delivery automatically
- HTTP-invoked functions wrap external endpoints as callable function IDs
- Functions can declare request/response formats for documentation and discovery — auto-generated from types in Rust (via ) and Python (via type hints / Pydantic), or manually provided in Node.js
schemars::JsonSchema
当任务需要时使用以下概念,并非每个工作器都需要全部概念。
- Function 是一个注册了唯一ID的异步处理器
- Trigger 将事件源绑定到函数——类型包括http、queue、cron、state、stream和subscribe
- 函数可通过 调用其他函数,不受语言或工作器位置限制
trigger() - 引擎自动处理序列化、路由和交付
- HTTP调用的函数会将外部端点包装为可调用的函数ID
- 函数可声明请求/响应格式用于文档和发现——在Rust中通过自动生成,在Python中通过类型提示/Pydantic自动生成,在Node.js中手动提供
schemars::JsonSchema
Architecture
架构
registerWorker()registerFunctionregisterTriggertrigger()registerWorker()registerFunctionregisterTriggertrigger()iii Primitives Used
使用的iii原语
| Primitive | Purpose |
|---|---|
| Connect worker to engine |
| Define a function handler |
| Bind an event source to a function |
| Invoke a function synchronously |
| Fire-and-forget invocation |
| Durable async invocation via queue |
| 原语 | 用途 |
|---|---|
| 将工作器连接到引擎 |
| 定义函数处理器 |
| 将事件源绑定到函数 |
| 同步调用函数 |
| 即发即弃式调用 |
| 通过队列实现持久化异步调用 |
Reference Implementation
参考实现
- TypeScript: ../references/functions-and-triggers.js
- Python: ../references/functions-and-triggers.py
- Rust: ../references/functions-and-triggers.rs
Each reference shows the same patterns (function registration, trigger binding, cross-function invocation) in its respective language.
- TypeScript: ../references/functions-and-triggers.js
- Python: ../references/functions-and-triggers.py
- Rust: ../references/functions-and-triggers.rs
每个参考示例都展示了相同的模式(函数注册、触发器绑定、跨函数调用),对应各自的语言。
Common Patterns
常见模式
Code using this pattern commonly includes, when relevant:
- — connect to the engine
registerWorker('ws://localhost:49134', { workerName: 'my-worker' }) - — register a handler
registerFunction({ id: 'namespace::name' }, async (input) => { ... }) - — HTTP trigger
registerTrigger({ type: 'http', function_id, config: { api_path, http_method } }) - — queue trigger
registerTrigger({ type: 'queue', function_id, config: { topic } }) - — cron trigger
registerTrigger({ type: 'cron', function_id, config: { expression } }) - — state change trigger
registerTrigger({ type: 'state', function_id, config: { scope, key } }) - — stream trigger
registerTrigger({ type: 'stream', function_id, config: { stream } }) - — pubsub subscriber
registerTrigger({ type: 'subscribe', function_id, config: { topic } }) - Cross-language invocation: a TypeScript function can trigger a Python or Rust function by ID
使用此模式的代码通常包含以下相关内容:
- — 连接到引擎
registerWorker('ws://localhost:49134', { workerName: 'my-worker' }) - — 注册处理器
registerFunction({ id: 'namespace::name' }, async (input) => { ... }) - — HTTP触发器
registerTrigger({ type: 'http', function_id, config: { api_path, http_method } }) - — 队列触发器
registerTrigger({ type: 'queue', function_id, config: { topic } }) - — cron触发器
registerTrigger({ type: 'cron', function_id, config: { expression } }) - — 状态变更触发器
registerTrigger({ type: 'state', function_id, config: { scope, key } }) - — 流触发器
registerTrigger({ type: 'stream', function_id, config: { stream } }) - — 发布订阅订阅器
registerTrigger({ type: 'subscribe', function_id, config: { topic } }) - 跨语言调用:TypeScript函数可通过ID触发Python或Rust函数
Request/Response Format (Auto-Registration)
请求/响应格式(自动注册)
Functions can declare their input/output schemas for documentation and discovery:
- Rust: Derive on handler input/output types —
schemars::JsonSchemaauto-generates JSON Schema (Draft 7) from the typeRegisterFunction::new() - Python: Use type hints (Pydantic models or primitives) on handler parameters and return types — auto-extracts JSON Schema (Draft 2020-12)
register_function() - Node.js: Pass /
request_formatmanually in the registration message (e.g., via Zod'sresponse_format)toJSONSchema()
函数可声明其输入/输出模式用于文档和发现:
- Rust: 在处理器输入/输出类型上派生 —
schemars::JsonSchema会从类型自动生成JSON Schema(Draft 7)RegisterFunction::new() - Python: 在处理器参数和返回类型上使用类型提示(Pydantic模型或基本类型) — 会自动提取JSON Schema(Draft 2020-12)
register_function() - Node.js: 在注册消息中手动传入 /
request_format(例如,通过Zod的response_format)toJSONSchema()
Adapting This Pattern
适配此模式
Use the adaptations below when they apply to the task.
- Replace placeholder handler logic with real business logic (API calls, DB queries, LLM calls)
- Use convention for function IDs to group related functions
namespace::name - For HTTP endpoints, configure and
api_pathin the trigger confighttp_method - For durable async work, use instead of synchronous trigger
TriggerAction.Enqueue({ queue }) - For fire-and-forget side effects, use
TriggerAction.Void() - Multiple workers in different languages can register functions that invoke each other by ID
当任务适用时,可进行以下适配:
- 用真实业务逻辑(API调用、数据库查询、LLM调用)替换占位处理器逻辑
- 使用 约定作为函数ID,对相关函数进行分组
namespace::name - 对于HTTP端点,在触发器配置中设置 和
api_pathhttp_method - 对于持久化异步任务,使用 替代同步触发
TriggerAction.Enqueue({ queue }) - 对于即发即弃的副作用,使用
TriggerAction.Void() - 不同语言的多个工作器可注册函数,并通过ID相互调用
Pattern Boundaries
模式边界
- For HTTP endpoint specifics (request/response format, path params), prefer .
iii-http-endpoints - For queue processing details (retries, concurrency, FIFO), prefer .
iii-queue-processing - For cron scheduling details (expressions, timezones), prefer .
iii-cron-scheduling - For invocation modes (sync vs void vs enqueue), prefer .
iii-trigger-actions - Stay with when the primary problem is registering functions, binding triggers, or cross-language invocation.
iii-functions-and-triggers
- 若涉及HTTP端点细节(请求/响应格式、路径参数),优先使用。
iii-http-endpoints - 若涉及队列处理细节(重试、并发、FIFO),优先使用。
iii-queue-processing - 若涉及cron调度细节(表达式、时区),优先使用。
iii-cron-scheduling - 若涉及调用模式(同步、即发即弃、入队),优先使用。
iii-trigger-actions - 当主要需求是注册函数、绑定触发器或跨语言调用时,使用。
iii-functions-and-triggers
When to Use
使用场景
- Use this skill when the task is primarily about in the iii engine.
iii-functions-and-triggers - Triggers when the request directly asks for this pattern or an equivalent implementation.
- 当任务主要涉及iii引擎中的时,使用此技能。
iii-functions-and-triggers - 当请求直接要求此模式或等效实现时触发。
Boundaries
边界限制
- Never use this skill as a generic fallback for unrelated tasks.
- You must not apply this skill when a more specific iii skill is a better fit.
- Always verify environment and safety constraints before applying examples from this skill.
- 切勿将此技能作为无关任务的通用 fallback。
- 当更具体的iii技能更合适时,不得应用此技能。
- 在应用此技能的示例前,务必验证环境和安全约束。