Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.3 KB
Raw Blame
import {
  AuthenticationType,
  HttpMethod,
  httpClient,
  QueryParams,
} from '@activepieces/pieces-common';
import type { GroqAuthValue } from './types';

function baseUrl(): string {
  return 'https://api.groq.com/openai/v1';
}

async function request<T>({
  auth,
  method,
  path,
  query,
  body,
  headers,
}: RequestParams & { auth: GroqAuthValue }): Promise<T> {
  const response = await httpClient.sendRequest<T>({
    method,
    url: `${baseUrl()}${path}`,
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: auth.secret_text,
    },
    headers,
    queryParams: query,
    body,
  });
  return response.body;
}

async function requestBuffer({
  auth,
  method,
  path,
  body,
}: RequestParams & { auth: GroqAuthValue }): Promise<Buffer> {
  const response = await httpClient.sendRequest<ArrayBuffer>({
    method,
    url: `${baseUrl()}${path}`,
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: auth.secret_text,
    },
    body,
    responseType: 'arraybuffer',
  });
  return Buffer.from(response.body);
}

export const groqClient = { baseUrl, request, requestBuffer };

type RequestParams = {
  method: HttpMethod;
  path: string;
  query?: QueryParams;
  body?: unknown;
  headers?: Record<string, string>;
};