Skip to content

The open alternative to Salesforce, designed for AI.

3.1 KB
Raw Blame
import { ToolCategory } from 'twenty-shared/ai';
import { isDefined } from 'twenty-shared/utils';
import { z } from 'zod';

import { DefineTool } from 'src/engine/core-modules/tool-provider/decorators/define-tool.decorator';
import { BaseTool } from 'src/engine/core-modules/tool-provider/interfaces/base-tool.interface';
import { type ToolExecuteArgs } from 'src/engine/core-modules/tool-provider/types/tool-execute-args.type';
import { type ToolOutput } from 'src/engine/core-modules/tool/types/tool-output.type';
import { WEBHOOK_WRITE_ACCESS_POLICY } from 'src/engine/metadata-modules/webhook/constants/webhook-write-access-policy.constant';
import { webhookOperationSchema } from 'src/engine/metadata-modules/webhook/tools/schemas/webhook-operation.schema';
import { compileWebhookOperations } from 'src/engine/metadata-modules/webhook/tools/utils/compile-webhook-operations.util';
import { WebhookService } from 'src/engine/metadata-modules/webhook/webhook.service';

const updateWebhookInputSchema = z.object({
  id: z.uuid().describe('The id of the webhook to update'),
  targetUrl: z
    .string()
    .url()
    .optional()
    .describe('New target URL. Leave unset to keep the current value.'),
  operations: webhookOperationSchema
    .optional()
    .describe('Replaces the operations list. Leave unset to keep current.'),
  description: z.string().optional(),
  secret: z.string().optional(),
  roleId: z
    .uuid()
    .optional()
    .describe('Id of the new role. Leave unset to keep the current role.'),
});

@DefineTool({
  name: 'update_webhook',
  category: ToolCategory.WEBHOOK,
  accessPolicy: WEBHOOK_WRITE_ACCESS_POLICY,
  description: `Update an existing webhook. Only the fields you pass are modified; everything else is preserved.`,
})
export class UpdateWebhookTool extends BaseTool<
  typeof updateWebhookInputSchema
> {
  readonly inputSchema = updateWebhookInputSchema;

  constructor(private readonly webhookService: WebhookService) {
    super();
  }

  async execute({
    input,
    context,
  }: ToolExecuteArgs<typeof updateWebhookInputSchema>): Promise<ToolOutput> {
    const update: {
      targetUrl?: string;
      operations?: string[];
      description?: string;
      secret?: string;
      roleId?: string;
    } = {};

    if (isDefined(input.targetUrl)) {
      update.targetUrl = input.targetUrl;
    }
    if (isDefined(input.operations)) {
      update.operations = compileWebhookOperations(input.operations);
    }
    if (isDefined(input.description)) {
      update.description = input.description;
    }
    if (isDefined(input.secret)) {
      update.secret = input.secret;
    }
    if (isDefined(input.roleId)) {
      update.roleId = input.roleId;
    }

    const webhook = await this.webhookService.update({
      input: { id: input.id, update },
      workspaceId: context.workspaceId,
    });

    return {
      success: true,
      message: `Webhook ${webhook.id} updated`,
      result: {
        id: webhook.id,
        targetUrl: webhook.targetUrl,
        operations: webhook.operations,
        description: webhook.description,
      },
    };
  }
}