|
| 1 | +import { createHmac } from 'crypto' |
| 2 | +import { v4 as uuidv4 } from 'uuid' |
| 3 | +import type { ToolConfig } from '@/tools/types' |
| 4 | +import type { RequestResponse, WebhookRequestParams } from './types' |
| 5 | +import { transformTable } from './utils' |
| 6 | + |
| 7 | +/** |
| 8 | + * Generates HMAC-SHA256 signature for webhook payload |
| 9 | + */ |
| 10 | +function generateSignature(secret: string, timestamp: number, body: string): string { |
| 11 | + const signatureBase = `${timestamp}.${body}` |
| 12 | + return createHmac('sha256', secret).update(signatureBase).digest('hex') |
| 13 | +} |
| 14 | + |
| 15 | +export const webhookRequestTool: ToolConfig<WebhookRequestParams, RequestResponse> = { |
| 16 | + id: 'webhook_request', |
| 17 | + name: 'Webhook Request', |
| 18 | + description: 'Send a webhook request with automatic headers and optional HMAC signing', |
| 19 | + version: '1.0.0', |
| 20 | + |
| 21 | + params: { |
| 22 | + url: { |
| 23 | + type: 'string', |
| 24 | + required: true, |
| 25 | + description: 'The webhook URL to send the request to', |
| 26 | + }, |
| 27 | + body: { |
| 28 | + type: 'object', |
| 29 | + description: 'JSON payload to send', |
| 30 | + }, |
| 31 | + secret: { |
| 32 | + type: 'string', |
| 33 | + description: 'Optional secret for HMAC-SHA256 signature', |
| 34 | + }, |
| 35 | + headers: { |
| 36 | + type: 'object', |
| 37 | + description: 'Additional headers to include', |
| 38 | + }, |
| 39 | + }, |
| 40 | + |
| 41 | + request: { |
| 42 | + url: (params: WebhookRequestParams) => params.url, |
| 43 | + |
| 44 | + method: () => 'POST', |
| 45 | + |
| 46 | + headers: (params: WebhookRequestParams) => { |
| 47 | + const timestamp = Date.now() |
| 48 | + const deliveryId = uuidv4() |
| 49 | + |
| 50 | + // Start with webhook-specific headers |
| 51 | + const webhookHeaders: Record<string, string> = { |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + 'X-Webhook-Timestamp': timestamp.toString(), |
| 54 | + 'X-Delivery-ID': deliveryId, |
| 55 | + 'Idempotency-Key': deliveryId, |
| 56 | + } |
| 57 | + |
| 58 | + // Add signature if secret is provided |
| 59 | + if (params.secret) { |
| 60 | + const bodyString = |
| 61 | + typeof params.body === 'string' ? params.body : JSON.stringify(params.body || {}) |
| 62 | + const signature = generateSignature(params.secret, timestamp, bodyString) |
| 63 | + webhookHeaders['X-Webhook-Signature'] = `t=${timestamp},v1=${signature}` |
| 64 | + } |
| 65 | + |
| 66 | + // Merge with user-provided headers (user headers take precedence) |
| 67 | + // Handle different header formats: |
| 68 | + // - Array of TableRow objects (from block usage): [{ cells: { Key, Value } }] |
| 69 | + // - Plain object (from direct tool usage): { key: value } |
| 70 | + // - undefined/null |
| 71 | + let userHeaders: Record<string, string> = {} |
| 72 | + if (params.headers) { |
| 73 | + if (Array.isArray(params.headers)) { |
| 74 | + userHeaders = transformTable(params.headers) |
| 75 | + } else if (typeof params.headers === 'object') { |
| 76 | + userHeaders = params.headers as Record<string, string> |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { ...webhookHeaders, ...userHeaders } |
| 81 | + }, |
| 82 | + |
| 83 | + body: (params: WebhookRequestParams) => params.body, |
| 84 | + }, |
| 85 | + |
| 86 | + transformResponse: async (response: Response) => { |
| 87 | + const contentType = response.headers.get('content-type') || '' |
| 88 | + |
| 89 | + const headers: Record<string, string> = {} |
| 90 | + response.headers.forEach((value, key) => { |
| 91 | + headers[key] = value |
| 92 | + }) |
| 93 | + |
| 94 | + const data = await (contentType.includes('application/json') |
| 95 | + ? response.json() |
| 96 | + : response.text()) |
| 97 | + |
| 98 | + // Check if this is a proxy response |
| 99 | + if ( |
| 100 | + contentType.includes('application/json') && |
| 101 | + typeof data === 'object' && |
| 102 | + data !== null && |
| 103 | + data.data !== undefined && |
| 104 | + data.status !== undefined |
| 105 | + ) { |
| 106 | + return { |
| 107 | + success: data.success, |
| 108 | + output: { |
| 109 | + data: data.data, |
| 110 | + status: data.status, |
| 111 | + headers: data.headers || {}, |
| 112 | + }, |
| 113 | + error: data.success ? undefined : data.error, |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + success: response.ok, |
| 119 | + output: { |
| 120 | + data, |
| 121 | + status: response.status, |
| 122 | + headers, |
| 123 | + }, |
| 124 | + error: undefined, |
| 125 | + } |
| 126 | + }, |
| 127 | + |
| 128 | + outputs: { |
| 129 | + data: { |
| 130 | + type: 'json', |
| 131 | + description: 'Response data from the webhook endpoint', |
| 132 | + }, |
| 133 | + status: { |
| 134 | + type: 'number', |
| 135 | + description: 'HTTP status code', |
| 136 | + }, |
| 137 | + headers: { |
| 138 | + type: 'object', |
| 139 | + description: 'Response headers', |
| 140 | + }, |
| 141 | + }, |
| 142 | +} |
0 commit comments