| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- import { config } from "../src/config/index.js";
- import { chunkText } from "../src/services/textChunker.js";
- import { embedTexts, visionExtractFromImage } from "../src/services/ollamaClient.js";
- import { ensureCollection } from "../src/services/collectionService.js";
- import { qdrant } from "../src/services/qdrantClient.js";
- import { createHash } from "node:crypto";
- import { createRequire } from "node:module";
- import fs from "node:fs";
- import mammoth from "mammoth";
- const require = createRequire(import.meta.url);
- function isVisionRefusal(text) {
- const t = String(text ?? "").toLowerCase();
- return (
- t.includes("desculpe") &&
- (t.includes("não posso") || t.includes("nao posso") || t.includes("não posso fornecer") || t.includes("nao posso fornecer"))
- );
- }
- function visionPrompt() {
- return [
- "Analise a imagem (print de tela / manual interno).",
- "Extraia apenas informações úteis para busca: nomes de campos, rótulos, valores exibidos, opções selecionadas (checkbox/radio/dropdown), botões, mensagens de erro e códigos.",
- "Não transcreva parágrafos longos; prefira listas curtas e objetivas.",
- "Responda em português, em texto puro."
- ].join("\n");
- }
- function stableUuid(seed) {
- const hex = createHash("sha1")
- .update(String(seed))
- .digest("hex")
- .slice(0, 32);
- const timeLow = hex.slice(0, 8);
- const timeMid = hex.slice(8, 12);
- let timeHiAndVersion = parseInt(hex.slice(12, 16), 16);
- timeHiAndVersion = (timeHiAndVersion & 0x0fff) | 0x5000;
- let clockSeqHi = parseInt(hex.slice(16, 18), 16);
- clockSeqHi = (clockSeqHi & 0x3f) | 0x80;
- const clockSeqLow = hex.slice(18, 20);
- const node = hex.slice(20, 32);
- return [
- timeLow,
- timeMid,
- timeHiAndVersion.toString(16).padStart(4, "0"),
- `${clockSeqHi.toString(16).padStart(2, "0")}${clockSeqLow}`,
- node
- ].join("-");
- }
- export async function ingestDocuments(documents) {
- const collectionName = config.qdrant.collection;
- const allChunks = [];
- for (const doc of documents) {
- const baseSeed = doc.id ?? `${doc.source ?? "doc"}:${doc.text.slice(0, 64)}`;
- const chunks = chunkText(doc.text, {
- chunkSize: config.rag.chunkSize,
- chunkOverlap: config.rag.chunkOverlap
- });
- chunks.forEach((chunk, idx) => {
- allChunks.push({
- id: stableUuid(`${baseSeed}:${idx}`),
- source: doc.source ?? null,
- metadata: doc.metadata ?? null,
- chunkIndex: idx,
- text: chunk
- });
- });
- }
- if (allChunks.length === 0) return { upserted: 0 };
- // #region debug-point B:ingest-chunks
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "B",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] ingestDocuments chunks built",
- data: {
- collection: collectionName,
- documents: Array.isArray(documents) ? documents.length : null,
- chunks: allChunks.length,
- avgChunkLen: allChunks.length ? Math.round(allChunks.reduce((a, c) => a + (c?.text?.length ?? 0), 0) / allChunks.length) : 0,
- sample0: allChunks[0]?.text ? String(allChunks[0].text).slice(0, 160) : ""
- },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- const vectors = await embedTexts(allChunks.map((c) => c.text));
- const vectorSize = vectors[0]?.length ?? 0;
- if (!vectorSize) {
- const err = new Error("embeddings_empty");
- err.statusCode = 502;
- throw err;
- }
- await ensureCollection({ vectorSize });
- const points = allChunks.map((c, idx) => ({
- id: c.id,
- vector: vectors[idx],
- payload: {
- source: c.source,
- chunkIndex: c.chunkIndex,
- text: c.text,
- metadata: c.metadata
- }
- }));
- // #region debug-point B:ingest-upsert
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "B",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] ingestDocuments upsert start",
- data: { collection: collectionName, points: points.length, vectorSize },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- await qdrant.upsert(collectionName, {
- wait: true,
- points
- });
- // #region debug-point B:ingest-upsert-done
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "B",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] ingestDocuments upsert done",
- data: { collection: collectionName, points: points.length },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- return { upserted: points.length };
- }
- function guessFileKind({ mimeType, filename }) {
- const name = String(filename ?? "").toLowerCase();
- const mt = String(mimeType ?? "").toLowerCase();
- if (mt === "text/plain" || name.endsWith(".txt")) return "txt";
- if (mt === "application/pdf" || name.endsWith(".pdf")) return "pdf";
- if (
- mt === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
- name.endsWith(".docx")
- )
- return "docx";
- if (mt.startsWith("image/") || /\.(png|jpe?g|webp)$/i.test(name)) return "image";
- return "unknown";
- }
- export async function extractDocumentsFromUpload({ buffer, filename, mimeType, source }) {
- const kind = guessFileKind({ mimeType, filename });
- const src = source ?? filename ?? "upload";
- const metadata = { filename: filename ?? null, mimeType: mimeType ?? null, kind };
- // #region debug-point A:extract-start
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "A",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] extractDocumentsFromUpload start",
- data: { kind, filename: filename ?? null, mimeType: mimeType ?? null, bytes: buffer?.length ?? null, source: src },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- if (kind === "txt") {
- const text = buffer.toString("utf8").trim();
- // #region debug-point A:extract-txt
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "A",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] extract txt done",
- data: { chars: text.length, head: text.slice(0, 160) },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- return [{ text, source: src, metadata }];
- }
- if (kind === "pdf") {
- let pdfParse;
- try {
- const mod = await import("pdf-parse");
- pdfParse = mod?.default ?? mod;
- } catch {
- try {
- pdfParse = require("pdf-parse");
- } catch {
- pdfParse = require("pdf-parse/lib/pdf-parse.js");
- }
- pdfParse = pdfParse?.default ?? pdfParse;
- }
- if (typeof pdfParse !== "function") {
- const err = new Error("pdf_parse_unavailable");
- err.statusCode = 500;
- throw err;
- }
- const parsed = await pdfParse(buffer);
- const text = String(parsed?.text ?? "").trim();
- // #region debug-point A:extract-pdf
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "A",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] extract pdf done",
- data: { chars: text.length, head: text.slice(0, 160) },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- return [{ text, source: src, metadata }];
- }
- if (kind === "docx") {
- const extracted = await mammoth.extractRawText({ buffer });
- const baseText = String(extracted?.value ?? "").trim();
- const images = [];
- await mammoth.convertToHtml(
- { buffer },
- {
- convertImage: mammoth.images.inline(async (image) => {
- const arr = await image.read();
- images.push(Buffer.from(arr));
- return { src: "" };
- })
- }
- );
- const ocrTexts = [];
- let visionSkipped = 0;
- let visionError = "";
- let visionUnavailable = false;
- for (let i = 0; i < images.length; i += 1) {
- if (visionUnavailable) {
- visionSkipped += 1;
- continue;
- }
- const imageBase64 = images[i].toString("base64");
- try {
- let r = await visionExtractFromImage({ imageBase64, prompt: visionPrompt() });
- let t = String(r?.content ?? "").trim();
- if (isVisionRefusal(t)) {
- r = await visionExtractFromImage({
- imageBase64,
- prompt: [
- "Analise a imagem e descreva somente os elementos de UI e estados selecionados.",
- "Liste itens curtos: campos/labels, opções marcadas, botões e mensagens de erro.",
- "Não faça transcrição literal de textos longos.",
- "Responda em português."
- ].join("\n")
- });
- t = String(r?.content ?? "").trim();
- }
- if (isVisionRefusal(t)) {
- visionError = "vision_refused";
- visionSkipped += 1;
- continue;
- }
- if (t) ocrTexts.push(`Imagem ${i + 1}:\n${t}`);
- } catch (e) {
- const msg = typeof e?.message === "string" ? e.message : "";
- visionError = msg || "vision_failed";
- if (msg.startsWith("ollama_model_not_found:")) {
- visionUnavailable = true;
- visionSkipped += images.length - i;
- } else {
- visionSkipped += 1;
- }
- }
- }
- const textParts = [];
- if (baseText) textParts.push(baseText);
- if (ocrTexts.length) textParts.push(ocrTexts.join("\n\n"));
- const text = textParts.join("\n\n").trim();
- // #region debug-point A:extract-docx
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "post",
- hypothesisId: "A",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] extract docx done",
- data: {
- baseChars: baseText.length,
- totalChars: text.length,
- imagesTotal: images.length,
- imagesProcessed: images.length - visionSkipped,
- imagesSkipped: visionSkipped,
- visionError: visionError || null,
- head: text.slice(0, 160)
- },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- return [
- {
- text,
- source: src,
- metadata: {
- ...metadata,
- imagesTotal: images.length,
- imagesProcessed: images.length - visionSkipped,
- imagesSkipped: visionSkipped,
- visionError: visionError || null
- }
- }
- ];
- }
- if (kind === "image") {
- const imageBase64 = buffer.toString("base64");
- try {
- let r = await visionExtractFromImage({ imageBase64, prompt: visionPrompt() });
- let text = String(r?.content ?? "").trim();
- if (isVisionRefusal(text)) {
- r = await visionExtractFromImage({
- imageBase64,
- prompt: [
- "Analise a imagem e descreva somente os elementos de UI e estados selecionados.",
- "Liste itens curtos: campos/labels, opções marcadas, botões e mensagens de erro.",
- "Não faça transcrição literal de textos longos.",
- "Responda em português."
- ].join("\n")
- });
- text = String(r?.content ?? "").trim();
- }
- // #region debug-point A:extract-image
- (() => {
- let u = "http://127.0.0.1:7777/event";
- let s = "image-rag-miss";
- try {
- const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
- u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
- s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
- } catch {}
- fetch(u, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- sessionId: s,
- runId: "pre",
- hypothesisId: "A",
- location: "api/chat/ingest.js",
- msg: "[DEBUG] extract image done",
- data: { chars: text.length, head: text.slice(0, 160) },
- ts: Date.now()
- })
- }).catch(() => {});
- })();
- // #endregion
- if (isVisionRefusal(text)) {
- const err = new Error("vision_refused");
- err.statusCode = 400;
- throw err;
- }
- return [{ text, source: src, metadata }];
- } catch (e) {
- const msg = typeof e?.message === "string" ? e.message : "";
- if (msg.startsWith("ollama_model_not_found:")) {
- const err = new Error(msg);
- err.statusCode = 400;
- throw err;
- }
- throw e;
- }
- }
- const err = new Error("unsupported_file_type");
- err.statusCode = 400;
- throw err;
- }
|