Webhook Helpers
Simplify your webhook integration with AgentMark’s webhook helpers.
AgentMark provides webhook helpers to streamline the process of handling webhook events. This is particularly useful when working with the Vercel AI SDK, as it simplifies running prompts and datasets received from AgentMark webhooks.
Vercel AI SDK Helper
The @agentmark/vercel-ai-v4-webhook-helper
package is designed to work seamlessly with the Vercel AI SDK. It abstracts away the complexity of handling different prompt types, allowing you to use a single method to run any prompt.
How It Works
The WebhookHelper
class inspects the incoming prompt’s configuration (text_config
, object_config
, etc.) and automatically calls the appropriate handler (runTextPrompt
, runObjectPrompt
, etc.). This means you don’t have to write repetitive boilerplate code to handle each prompt type manually.
Installation
To get started, install the necessary packages:
Setup
Here’s how to set up your webhook endpoint using the helper in a Next.js app:
-
Initialize AgentMark SDK and Vercel AI Adapter
First, configure the AgentMark SDK and create a model registry for the Vercel AI SDK. Then, create an AgentMark client, which will be passed to the
WebhookHelper
. -
Use the WebhookHelper in Your Endpoint
Once you have the
agentmark
client, you can instantiate theWebhookHelper
and use it to process incoming webhook events. The example below shows a complete Next.js App Router endpoint.
The runPrompt
Method
The runPrompt
method is the core of the helper when handling single prompt runs. It takes two arguments:
event.data
: The prompt data from the webhook payload.options
(optional): An object to configure the prompt execution.
Options
The options
object allows you to specify how the prompt should be run.
shouldStream
(boolean): Defaults totrue
. Whentrue
, the method returns aReadableStream
to stream the response back to the AgentMark platform. This is the recommended approach for real-time feedback. Set tofalse
to receive the full response as a single JSON object after the model has finished generating.
Return Value & Handling
The runPrompt
method returns a promise that resolves to a WebhookResponse
. Your webhook must handle the response correctly to ensure proper communication with the AgentMark platform.
-
If
response.type
isstream
: You must return a newResponse
object. The body should be theresponse.stream
and the headers must includeresponse.streamHeader
. This is critical for streaming the result back to AgentMark correctly. -
If
response.type
is anything else (text
,object
,image
,speech
): You should return the entireresponse
object as a JSON response.
The runDataset
Method
The runDataset
method handles running a prompt against all items in a dataset. It is a “fire-and-forget” operation from the webhook’s perspective. The helper processes the entire dataset in the background and sends telemetry data to AgentMark for each item.
Parameters
The method takes a single argument, event.data
, which is an object containing the paths to the prompt and dataset, along with run metadata.
Return Value & Handling
The runDataset
method returns a Promise<RunDatasetResponse>
that contains a streaming response. The helper processes the entire dataset and streams the results back to AgentMark in real-time.
The runDataset
method currently supports prompts that generate text and objects. Image and speech generation prompts are not supported for dataset runs.
Manual Implementation
If you prefer not to use the WebhookHelper
or are not using the Vercel AI SDK, you can implement the event handling logic yourself. Please refer to our detailed event documentation for guidance:
Have Questions?
We’re here to help! Choose the best way to reach us:
Join our Discord community for quick answers and discussions
Email us at hello@agentmark.co for support
Schedule an Enterprise Demo to learn about our business solutions