WebhookSignalProvider
追加バージョン: @mastra/core@1.39.0
Push 型イベント配信のための具象 Signal Provider です。設定可能な Resource ID 抽出関数を使用して Payload を照合し、受信した Webhook Payload を購読中の Agent Thread にルーティングします。
SignalProvider を拡張し、公開 Subscription 管理機能と組み込みの 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 を返します。複数の Resource と照合するために配列を返すこともできます。buildNotification?:
メソッドメソッドへの直接リンク
Subscription の管理Subscription の管理への直接リンク
subscribeThread(target, externalResourceId, metadata?)subscribethreadtarget-externalresourceid-metadataへの直接リンク
特定の外部 Resource の Webhook イベントを受信するよう Thread を購読します。
webhookProvider.subscribeThread(
{ threadId: 'thread-1', resourceId: 'user-1' },
'mastra-ai/mastra',
{ watchType: 'push' },
)
戻り値: SignalSubscription
target:
threadId と resourceId を含める必要があります。externalResourceId:
metadata?:
unsubscribeThread(target, externalResourceId)unsubscribethreadtarget-externalresourceidへの直接リンク
特定の外部 Resource に対する Thread の購読を解除します。
const removed = webhookProvider.unsubscribeThread(
{ threadId: 'thread-1', resourceId: 'user-1' },
'mastra-ai/mastra',
)
戻り値: boolean
Webhook の処理Webhook の処理への直接リンク
handleWebhook(request)handlewebhookrequestへの直接リンク
受信した Webhook リクエストを処理します。Payload から Resource ID を抽出し、一致する Subscription を検索して、それぞれの通知を構築し、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 } }>
通知を受信した Subscription 数を N として、{ status: 200, body: { matched: N } } を返します。Resource ID を抽出できない場合や、一致する Subscription がない場合は matched: 0 を返します。
request:
body:
headers:
静的 Signal Factory静的 Signal Factoryへの直接リンク
WebhookSignalProvider.signals.subscribe(externalResourceId)webhooksignalprovidersignalssubscribeexternalresourceidへの直接リンク
現在の Thread が外部 Resource を購読する Reactive Signal Input を作成します。
const signal = WebhookSignalProvider.signals.subscribe('mastra-ai/mastra')
戻り値: { type: 'reactive'; tagName: string; contents: string; attributes: { resource: string } }
WebhookSignalProvider.signals.unsubscribe(externalResourceId)webhooksignalprovidersignalsunsubscribeexternalresourceidへの直接リンク
現在の Thread が外部 Resource の購読を解除する Reactive Signal Input を作成します。
const signal = WebhookSignalProvider.signals.unsubscribe('mastra-ai/mastra')
戻り値: { type: 'reactive'; tagName: string; contents: string; attributes: { resource: string } }