WebhookSignalProvider
新增于: @mastra/core@1.39.0
用于基于推送的事件投递的具体 Signal Provider。它通过将 payload 与可配置的资源 ID 提取器进行匹配,把传入的 webhook payload 路由至已订阅的 Agent thread。
它扩展了 SignalProvider,提供公共订阅管理和内置的 handleWebhook() 实现。
使用示例使用示例的直接链接
将 GitHub webhook 事件路由至已订阅的 thread:
import { WebhookSignalProvider } from '@mastra/core/signals'
const webhookProvider = new WebhookSignalProvider({
extractResourceId: payload => `${payload.repository?.full_name}`,
buildNotification: (payload, subscription) => ({
source: 'github-webhook',
kind: payload.action ?? 'event',
summary: `${payload.action} on ${subscription.externalResourceId}`,
payload,
}),
})
注册到 Agent 并订阅一个 thread:
import { Agent } from '@mastra/core/agent'
const agent = new Agent({
id: 'agent',
signals: [webhookProvider],
})
// subscribe a thread to a specific repo
webhookProvider.subscribeThread({ threadId: 'thread-1', resourceId: 'user-1' }, 'mastra-ai/mastra')
// handle an incoming webhook
const result = await webhookProvider.handleWebhook({
body: { repository: { full_name: 'mastra-ai/mastra' }, action: 'push' },
headers: {},
})
// result.matched === 1
构造函数参数构造函数参数的直接链接
id?:
name?:
extractResourceId?:
undefined 可跳过该事件。可返回数组以匹配多个资源。buildNotification?:
方法方法的直接链接
订阅管理订阅管理的直接链接
subscribeThread(target, externalResourceId, metadata?)subscribethreadtarget-externalresourceid-metadata的直接链接
订阅一个 thread,以接收特定外部资源的 webhook 事件。
webhookProvider.subscribeThread(
{ threadId: 'thread-1', resourceId: 'user-1' },
'mastra-ai/mastra',
{ watchType: 'push' },
)
返回:SignalSubscription
target:
threadId 和 resourceId。externalResourceId:
metadata?:
unsubscribeThread(target, externalResourceId)unsubscribethreadtarget-externalresourceid的直接链接
取消订阅一个 thread,使其不再接收特定外部资源的事件。
const removed = webhookProvider.unsubscribeThread(
{ threadId: 'thread-1', resourceId: 'user-1' },
'mastra-ai/mastra',
)
返回:boolean
Webhook 处理Webhook 处理的直接链接
handleWebhook(request)handlewebhookrequest的直接链接
处理传入的 webhook 请求。从 payload 提取资源 ID,查找匹配的订阅,为每个订阅构建通知,并调用 notify()。
const result = await webhookProvider.handleWebhook({
body: { repository: { full_name: 'mastra-ai/mastra' }, action: 'push' },
headers: { 'x-github-event': 'push' },
})
返回:Promise<{ status: number; body: { matched: number } }>
返回 { status: 200, body: { matched: N } },其中 N 是收到通知的订阅数量。当无法提取资源 ID 或没有订阅匹配时,返回 matched: 0。
request:
body:
headers:
静态 Signal 工厂静态 Signal 工厂的直接链接
WebhookSignalProvider.signals.subscribe(externalResourceId)webhooksignalprovidersignalssubscribeexternalresourceid的直接链接
创建一个 reactive Signal 输入,为当前 thread 订阅外部资源。
const signal = WebhookSignalProvider.signals.subscribe('mastra-ai/mastra')
返回:{ type: 'reactive'; tagName: string; contents: string; attributes: { resource: string } }
WebhookSignalProvider.signals.unsubscribe(externalResourceId)webhooksignalprovidersignalsunsubscribeexternalresourceid的直接链接
创建一个 reactive Signal 输入,取消当前 thread 对外部资源的订阅。
const signal = WebhookSignalProvider.signals.unsubscribe('mastra-ai/mastra')
返回:{ type: 'reactive'; tagName: string; contents: string; attributes: { resource: string } }