@mastra/core
Version:
114 lines • 3.78 kB
TypeScript
import type { SendNotificationSignalInput } from '../notifications/types.js';
import { SignalProvider } from './signal-provider.js';
import type { SignalProviderTarget, SignalProviderWebhookRequest, SignalSubscription } from './signal-provider.js';
/**
* Configuration for the webhook signal provider.
*
* @experimental Agent signals are experimental and may change in a future release.
*/
export type WebhookSignalProviderOptions = {
/**
* Unique identifier for the provider instance.
* @default 'webhook-signals'
*/
id?: string;
/**
* Human-readable name.
* @default 'Webhook Signals'
*/
name?: string;
/**
* Optional function to extract a matching key from an incoming webhook payload.
* The returned string is matched against `externalResourceId` in subscriptions.
*
* @default Returns `payload.resource` or `payload.externalResourceId` if present.
*/
extractResourceId?: (payload: unknown) => string | string[] | undefined;
/**
* Optional function to build the notification from a webhook payload.
* When not provided, a default notification is built from the payload.
*/
buildNotification?: (payload: unknown, subscription: SignalSubscription) => SendNotificationSignalInput;
};
/**
* A generic webhook-based signal provider.
*
* Receives external events via HTTP webhooks and routes them to
* subscribed agent threads as notification signals.
*
* ## Usage
*
* ```ts
* const webhooks = new WebhookSignalProvider({
* extractResourceId: (payload) => (payload as any).repository,
* buildNotification: (payload, sub) => ({
* source: 'ci',
* kind: 'build-status',
* priority: 'medium',
* summary: `Build ${(payload as any).status} for ${sub.externalResourceId}`,
* }),
* });
*
* const agent = new Agent({
* signals: [webhooks],
* });
*
* // Subscribe a thread to a resource
* webhooks.subscribeThread(
* { threadId: 'thread-1', resourceId: 'user-1' },
* 'my-org/my-repo',
* );
*
* // Later, when a webhook fires:
* await webhooks.handleWebhook({
* body: { repository: 'my-org/my-repo', status: 'failed' },
* headers: {},
* });
* ```
*
* @experimental Agent signals are experimental and may change in a future release.
*/
export declare class WebhookSignalProvider extends SignalProvider<string> {
#private;
readonly id: string;
readonly name: string;
constructor(options?: WebhookSignalProviderOptions);
/**
* Create signal inputs for subscribing/unsubscribing threads via signals.
*/
static signals: {
subscribe(resource: string): {
type: "reactive";
tagName: string;
contents: string;
attributes: {
resource: string;
};
};
unsubscribe(resource: string): {
type: "reactive";
tagName: string;
contents: string;
attributes: {
resource: string;
};
};
};
/**
* Programmatically subscribe a thread to an external resource.
*/
subscribeThread(target: SignalProviderTarget, externalResourceId: string, metadata?: Record<string, unknown>): SignalSubscription;
/**
* Programmatically unsubscribe a thread from an external resource.
*/
unsubscribeThread(target: SignalProviderTarget, externalResourceId: string): boolean;
/**
* Handle an incoming webhook. Matches the payload against subscriptions
* and emits notification signals to matching threads.
*/
handleWebhook(request: SignalProviderWebhookRequest): Promise<{
status?: number;
body?: unknown;
}>;
}
//# sourceMappingURL=webhook-signal-provider.d.ts.map