Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
feat: webhooks for agent (#2628)
tom9dfceacac0fb
diff --git a/apps/api/src/controllers/v2/agent.ts b/apps/api/src/controllers/v2/agent.tsindex 7fd8e7d75..e68943a32 100644--- a/apps/api/src/controllers/v2/agent.ts+++ b/apps/api/src/controllers/v2/agent.ts@@ -101,6 +101,7 @@ export async function agentController(isFreeRequest,maxCredits: req.body.maxCredits ?? undefined,strictConstrainToURLs: req.body.strictConstrainToURLs ?? undefined,+ webhook: req.body.webhook ?? undefined,}),},);diff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.tsindex b0280bd81..61d231c15 100644--- a/apps/api/src/controllers/v2/types.ts+++ b/apps/api/src/controllers/v2/types.ts@@ -21,7 +21,10 @@ import { ErrorCodes } from "../../lib/error";import Ajv from "ajv";import addFormats from "ajv-formats";import { integrationSchema } from "../../utils/integration";-import { webhookSchema } from "../../services/webhook/schema";+import {+ webhookSchema,+ createWebhookSchema,+} from "../../services/webhook/schema";import { BrandingProfile } from "../../types/branding";// Base URL schema with common validation logic@@ -711,6 +714,14 @@ export const extractRequestSchema = extractOptions;export type ExtractRequest = z.infer<typeof extractRequestSchema>;export type ExtractRequestInput = z.input<typeof extractRequestSchema>;+const agentWebhookSchema = createWebhookSchema([+ "started",+ "action",+ "completed",+ "failed",+ "cancelled",+]);+export const agentRequestSchema = z.strictObject({urls: URL.array().optional(),prompt: z.string().max(10000),@@ -738,6 +749,7 @@ export const agentRequestSchema = z.strictObject({integration: integrationSchema.optional().transform(val => val || null),maxCredits: z.number().optional(),strictConstrainToURLs: z.boolean().optional(),+ webhook: agentWebhookSchema.optional(),overrideWhitelist: z.string().optional(),});diff --git a/apps/api/src/services/webhook/schema.ts b/apps/api/src/services/webhook/schema.tsindex c63751446..ed94d5321 100644--- a/apps/api/src/services/webhook/schema.ts+++ b/apps/api/src/services/webhook/schema.ts@@ -2,26 +2,34 @@ import { z } from "zod";const BLACKLISTED_WEBHOOK_HEADERS = ["x-firecrawl-signature"];-export const webhookSchema = z.preprocess(- x => (typeof x === "string" ? { url: x } : x),- z- .strictObject({- url: z.url(),- headers: z.record(z.string(), z.string()).prefault({}),- metadata: z.record(z.string(), z.string()).prefault({}),- events: z- .array(z.enum(["completed", "failed", "page", "started"]))- .prefault(["completed", "failed", "page", "started"]),- })- .refine(- obj => {- const blacklistedLower = BLACKLISTED_WEBHOOK_HEADERS.map(h =>- h.toLowerCase(),- );- return !Object.keys(obj.headers).some(key =>- blacklistedLower.includes(key.toLowerCase()),- );- },- `The following headers are not allowed: ${BLACKLISTED_WEBHOOK_HEADERS.join(", ")}`,- ),-);+export function createWebhookSchema<T extends [string, ...string[]]>(+ events: T,+) {+ const blacklistedLower = BLACKLISTED_WEBHOOK_HEADERS.map(h =>+ h.toLowerCase(),+ );+ return z.preprocess(+ x => (typeof x === "string" ? { url: x } : x),+ z+ .strictObject({+ url: z.url(),+ headers: z.record(z.string(), z.string()).prefault({}),+ metadata: z.record(z.string(), z.string()).prefault({}),+ events: z.array(z.enum(events)).prefault([...events]),+ })+ .refine(+ obj =>+ !Object.keys(obj.headers).some(key =>+ blacklistedLower.includes(key.toLowerCase()),+ ),+ `The following headers are not allowed: ${BLACKLISTED_WEBHOOK_HEADERS.join(", ")}`,+ ),+ );+}++export const webhookSchema = createWebhookSchema([+ "completed",+ "failed",+ "page",+ "started",+]);