跳至主要內容

Mastra.getGatewayById()

按閘道的唯一 ID(閘道實例的 id 屬性)擷取閘道。當你需要使用閘道的內在識別碼,而非登錄鍵來尋找閘道時,這十分實用。

使用範例
使用範例 的直接連結

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'

使用情境
使用情境 的直接連結

  • 閘道版本管理:不同版本使用不同的唯一 ID('gateway-v1''gateway-v2'
  • 在登錄鍵未知時尋找閘道
  • 跨不同 Mastra 實例識別閘道

參數
參數 的直接連結

id:

string
閘道的唯一 ID(gateway.id 屬性)

傳回值
傳回值 的直接連結

gateway:

MastraModelGateway | undefined
閘道實例;如找不到則為 undefined