1
import logger from '../logger';
2
import { fetchWithCache } from '../cache';
4
import { REQUEST_TIMEOUT_MS } from './shared';
6
import type { ApiProvider, ProviderResponse } from '../types.js';
8
export class WebhookProvider implements ApiProvider {
12
constructor(webhookUrl: string, options: { id?: string; config?: object } = {}) {
13
const { id, config } = options;
14
this.webhookUrl = webhookUrl;
15
this.id = id ? () => id : this.id;
20
return `webhook:${this.webhookUrl}`;
24
return `[Webhook Provider ${this.webhookUrl}]`;
27
async callApi(prompt: string): Promise<ProviderResponse> {
28
const params: { prompt: string; config?: object } = {
32
params.config = this.config;
35
logger.debug(`Calling Webhook: ${this.webhookUrl} with params: ${JSON.stringify(params)}`);
38
response = await fetchWithCache(
43
'Content-Type': 'application/json',
45
body: JSON.stringify(params),
52
error: `Webhook call error: ${String(err)}`,
55
logger.debug(`\tWebhook response: ${JSON.stringify(response.data)}`);
57
if (response.data && typeof response.data.output === 'string') {
59
output: response.data.output,
63
error: `Webhook response error: Unexpected response format: ${JSON.stringify(