Mastra.addGateway()
动态向 Mastra 实例添加网关。可在初始化期间或之后使用。
🌐 Dynamically adds a gateway to the Mastra instance. Can be used during or after initialization.
使用示例Direct link to 使用示例
🌐 Usage example
import { Mastra } from '@mastra/core';
const mastra = new Mastra();
// Add with explicit key
mastra.addGateway(new MyCustomGateway(), 'myGateway');
// Add using gateway's ID as the key
mastra.addGateway(new MyCustomGateway());
// Stored with key 'my-custom-gateway' (the gateway's id)
参数Direct link to 参数
🌐 Parameters
gateway:
MastraModelGateway
The gateway instance to add
key?:
string
Optional registration key. If not provided, uses gateway.getId()
返回Direct link to 返回
🌐 Returns
空。网关已被添加到内部注册表中。
🌐 Void. The gateway is added to the internal registry.
行为Direct link to 行为
🌐 Behavior
- 如果具有相同密钥的网关已存在,则会跳过添加
- 在开发模式(
MASTRA_DEV=true)下,会自动触发自定义网关的类型生成 - 网关可立即用于模型解析
相关Direct link to 相关
🌐 Related
- Mastra.getGateway() - 通过注册密钥获取网关
- Mastra.getGatewayById() - 通过ID获取网关
- Mastra.listGateways() - 列出所有网关
- MastraModelGateway - 网关基类
- 自定义网关指南 - 创建自定义网关