UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

54 lines (53 loc) 2.18 kB
import fs from 'fs'; import path from 'path'; import { Readable } from 'stream'; export async function downloadRobot(file, downloadPath) { const result = await fetch(`${process.env.ROBOT_HOST}/api/llm/kb/file/${file}`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${btoa(`${process.env.ROBOT_USER}:${process.env.ROBOT_PASSWORD}`)}` } }); if (!result.ok) { throw new Error(`Error while downloading file ${file} from ROBOT. Status: ${result.status} - ${result.statusText}`); } // parse filename from Content-Disposition header const contentDisposition = result.headers.get('content-disposition'); const match = contentDisposition?.match(/filename="?(.+?)"?$/); if (!match) { throw new Error(`Error while parsing filename from Content-Disposition header: ${contentDisposition}`); } const filename = match[1]; if (!filename) { throw new Error(`Filename is empty in Content-Disposition header: ${contentDisposition}`); } const filePath = path.join(downloadPath, filename); const fileStream = fs.createWriteStream(filePath); const resultBody = result.body; if (resultBody) { await new Promise((resolve, reject)=>{ Readable.fromWeb(resultBody).pipe(fileStream); fileStream.on('finish', resolve); fileStream.on('error', reject); }); } return filename; } export async function fetchRobot(endpoint, method, body, headers = {}) { const result = await fetch(`${process.env.ROBOT_HOST}/${endpoint}`, { method: method, headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${btoa(`${process.env.ROBOT_USER}:${process.env.ROBOT_PASSWORD}`)}`, ...headers }, body: JSON.stringify(body) }); if (!result.ok) { const error = new Error(`Error while fetching from ROBOT endpoint ${endpoint}. Status: ${result.status} - ${result.statusText}`); error.details = await result.text(); throw error; } return await result.json(); } //# sourceMappingURL=robot.js.map