Ingest.Controller.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { BadRequestError } from "../shared/errors/index.js";
  2. import { extractDocumentsFromUpload, ingestDocuments, fetchUrlText } from "../services/ingestService.js";
  3. export const IngestController = {
  4. Ingerir: async function (req, res, next) {
  5. try {
  6. const result = await ingestDocuments(req.body.documents);
  7. res.json(result);
  8. } catch (err) {
  9. next(err);
  10. }
  11. },
  12. IngerirArquivo: async function (req, res, next) {
  13. try {
  14. const f = req.file;
  15. if (!f?.buffer) throw new BadRequestError("file_required");
  16. const source = typeof req.body?.source === "string" && req.body.source.trim() ? req.body.source.trim() : undefined;
  17. const docs = await extractDocumentsFromUpload({
  18. buffer: f.buffer,
  19. filename: f.originalname,
  20. mimeType: f.mimetype,
  21. source
  22. });
  23. const extractedChars = docs.reduce((acc, d) => acc + (d?.text?.length ?? 0), 0);
  24. if (!extractedChars) throw new BadRequestError("empty_extracted_text");
  25. const result = await ingestDocuments(docs);
  26. res.json({ ...result, documents: docs.length, extractedChars });
  27. } catch (err) {
  28. next(err);
  29. }
  30. },
  31. IngerirUrl: async function (req, res, next) {
  32. try {
  33. const body = req.body;
  34. const text = await fetchUrlText(body.url);
  35. if (!text) throw new BadRequestError("empty_extracted_text");
  36. const source = body.source ?? body.url;
  37. const result = await ingestDocuments([{ text, source, title: source }]);
  38. res.json({ ...result, source, extractedChars: text.length });
  39. } catch (err) {
  40. next(err);
  41. }
  42. }
  43. };
  44. export default IngestController;