Skip to main content

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)下,会自动触发自定义网关的类型生成
  • 网关可立即用于模型解析

🌐 Related