跳到主要内容

Mastra.getGatewayById()

按唯一 ID(gateway 实例的 id 属性)获取 gateway。需要按固有标识符而非注册键查找 gateway 时,此方法非常有用。

使用示例
使用示例的直接链接

import { Mastra } from '@mastra/core'

const mastra = new Mastra({
gateways: {
customKey: new MyCustomGateway(), // id = 'my-custom-gateway'
},
})

// Retrieve by ID (not registration key)
const gateway = mastra.getGatewayById('my-custom-gateway')
console.log(gateway.name) // 'My Custom Gateway'

使用场景
使用场景的直接链接

  • Gateway 版本控制:不同版本使用不同的唯一 ID ('gateway-v1', 'gateway-v2')
  • 在注册键未知时查找 gateway
  • 跨不同 Mastra 实例识别 gateway

参数
参数的直接链接

id:

string
gateway 的唯一 ID(gateway.id 属性)

返回值
返回值的直接链接

gateway:

MastraModelGateway | undefined
gateway 实例;如果未找到,则为 undefined