From d43a0796b6a8c8d121f54e71ef841f34ce8f5cae Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 11 Sep 2024 14:41:24 -0300 Subject: [PATCH] fix (templates): law OK. --- .../basic.gblib/services/SystemKeywords.ts | 61 +++++++++--------- src/util.ts | 48 +++++++------- templates/law.gbai/law.gbdialog/case.bas | 18 ++++-- .../law.gbdrive/{case.pdf => case-4500.pdf} | Bin 4 files changed, 67 insertions(+), 60 deletions(-) rename templates/law.gbai/law.gbdrive/{case.pdf => case-4500.pdf} (100%) diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index 2bb11820..c4f9bd7d 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -414,7 +414,7 @@ export class SystemKeywords { const buffer = pngPages[0].content; const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); - await fs.writeFile(localName, buffer, { encoding: null }); + await fs.writeFile(localName, buffer, { encoding: null }); return { localName: localName, url: url, data: buffer }; } @@ -707,7 +707,7 @@ export class SystemKeywords { // Writes it to disk and calculate hash. const data = await response.arrayBuffer(); -await fs.writeFile(localName, Buffer.from(data), { encoding: null }); + await fs.writeFile(localName, Buffer.from(data), { encoding: null }); const hash = new Uint8Array(md5.array(data)); // Performs uploading passing local hash. @@ -1114,7 +1114,7 @@ await fs.writeFile(localName, Buffer.from(data), { encoding: null }); const localName = path.join('work', gbaiName, 'cache', `csv${GBAdminService.getRndReadableIdentifier()}.csv`); const url = file['@microsoft.graph.downloadUrl']; const response = await fetch(url); - await fs.writeFile(localName, Buffer.from(await response.arrayBuffer()), { encoding: null }); + await fs.writeFile(localName, Buffer.from(await response.arrayBuffer()), { encoding: null }); var workbook = new Excel.Workbook(); let worksheet = await workbook.csv.readFile(localName); @@ -1490,7 +1490,7 @@ await fs.writeFile(localName, Buffer.from(data), { encoding: null }); user.userSystemId, 'systemPrompt.txt' ); - await fs.writeFile(systemPromptFile, text); + await fs.writeFile(systemPromptFile, text); } } @@ -1981,7 +1981,7 @@ await fs.writeFile(localName, Buffer.from(data), { encoding: null }); const res = await fetch(url); let buf: any = Buffer.from(await res.arrayBuffer()); localName = path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.docx`); -await fs.writeFile(localName, buf, { encoding: null }); + await fs.writeFile(localName, buf, { encoding: null }); // Replace image path on all elements of data. @@ -2019,7 +2019,7 @@ await fs.writeFile(localName, buf, { encoding: null }); ); const response = await fetch(url); const buf = Buffer.from(await response.arrayBuffer()); - await fs.writeFile(imageName, buf, { encoding: null }); + await fs.writeFile(imageName, buf, { encoding: null }); const getNormalSize = ({ width, height, orientation }) => { return (orientation || 0) >= 5 ? [height, width] : [width, height]; @@ -2068,7 +2068,7 @@ await fs.writeFile(localName, buf, { encoding: null }); doc.setData(data).render(); buf = doc.getZip().generate({ type: 'nodebuffer', compression: 'DEFLATE' }); -await fs.writeFile(localName, buf, { encoding: null }); + await fs.writeFile(localName, buf, { encoding: null }); return { localName: localName, url: url, data: buf }; } @@ -2526,7 +2526,7 @@ await fs.writeFile(localName, buf, { encoding: null }); const buf = Buffer.from(data.Payment.QrCodeBase64Image, 'base64'); const localName = path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`); -await fs.writeFile(localName, buf, { encoding: null }); + await fs.writeFile(localName, buf, { encoding: null }); const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); GBLogEx.info(min, `GBPay: ${data.MerchantOrderId} OK: ${url}.`); @@ -2688,25 +2688,29 @@ await fs.writeFile(localName, buf, { encoding: null }); public async getPdf({ pid, file }) { const { min } = await DialogKeywords.getProcessInfo(pid); GBLogEx.info(min, `BASIC GET (pdf): ${file}`); + try { + let data; - let data; - - if (GBConfigService.get('STORAGE_NAME')) { - let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); - const gbaiName = GBUtil.getGBAIPath(min.botId); - let packagePath = '/' + urlJoin(gbaiName, `${min.botId}.gbdrive`); - let template = await this.internalGetDocument(client, baseUrl, packagePath, file); - let url = template['@microsoft.graph.downloadUrl']; - const res = await fetch(url); - let buf: any = Buffer.from(await res.arrayBuffer()); - data = new Uint8Array(buf); - } else { - let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`); - let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file); - data = await fs.readFile(filePath, 'utf8'); - data = new Uint8Array(Buffer.from(data, 'utf8')); + if (GBConfigService.get('STORAGE_NAME')) { + let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); + const gbaiName = GBUtil.getGBAIPath(min.botId); + let packagePath = '/' + urlJoin(gbaiName, `${min.botId}.gbdrive`); + let template = await this.internalGetDocument(client, baseUrl, packagePath, file); + let url = template['@microsoft.graph.downloadUrl']; + const res = await fetch(url); + let buf: any = Buffer.from(await res.arrayBuffer()); + data = new Uint8Array(buf); + } else { + let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`); + let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file); + data = await fs.readFile(filePath); + data = new Uint8Array(data); + } + return await GBUtil.getPdfText(data); + } catch (error) { + GBLogEx.error(min, error); + return null; } - return await GBUtil.getPdfText(data); } public async setContext({ pid, text }) { @@ -2752,7 +2756,7 @@ await fs.writeFile(localName, buf, { encoding: null }); // Criação de um arquivo temporário para enviar const tempFilePath = path.resolve('temp_image.jpg'); -await fs.writeFile(tempFilePath, imageBuffer); + await fs.writeFile(tempFilePath, imageBuffer); // Publicação da imagem const page = new Page(pageId); @@ -2779,8 +2783,7 @@ await fs.writeFile(tempFilePath, imageBuffer); ig.state.generateDevice(username); await ig.account.login(username, password); const imageBuffer = await fs.readFile(imagePath); - const publishResult = await ig.publish.photo( - { file: imageBuffer, caption }); + const publishResult = await ig.publish.photo({ file: imageBuffer, caption }); GBLogEx.info(min, `Image posted on IG: ${publishResult}`); } @@ -2790,6 +2793,6 @@ await fs.writeFile(tempFilePath, imageBuffer); ChatServices.usersMode[user.userSystemId] = mode; - GBLogEx.info(min, `LLM Mode (user.userSystemId) : ${mode}`); + GBLogEx.info(min, `LLM Mode (${user.userSystemId}): ${mode}`); } } diff --git a/src/util.ts b/src/util.ts index ad014fac..17c5325a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -35,13 +35,13 @@ 'use strict'; import * as YAML from 'yaml'; import SwaggerClient from 'swagger-client'; -import fs from 'fs/promises'; +import fs from 'fs/promises'; import { GBConfigService } from '../packages/core.gbapp/services/GBConfigService.js'; import path from 'path'; import { VerbosityLevel, getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs'; -VerbosityLevel.ERRORS=0; -VerbosityLevel.WARNINGS=0; -VerbosityLevel.INFOS=0; +VerbosityLevel.ERRORS = 0; +VerbosityLevel.WARNINGS = 0; +VerbosityLevel.INFOS = 0; import { Page } from 'puppeteer'; import urljoin from 'url-join'; @@ -102,17 +102,17 @@ export class GBUtil { return acc; }, {}); }; - + const extractedError = extractProps(data); - + // Inline formatting for logs return YAML.stringify(extractedError, { - indent: 2, // Defines the indentation - flowLevel: -1, // Forces inline formatting - styles: { '!!null': 'canonical' } // Optional: Customize null display + indent: 2, // Defines the indentation + flowLevel: -1, // Forces inline formatting + styles: { '!!null': 'canonical' } // Optional: Customize null display } as any); } - + public static sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms); @@ -146,44 +146,45 @@ export class GBUtil { return false; // File does not exist } } - public static async copyIfNewerRecursive(src, dest) { - if (!await GBUtil.exists(src)) { + if (!(await GBUtil.exists(src))) { console.error(`Source path "${src}" does not exist.`); return; } // Check if the source is a directory - if ((await fs.stat(src)).isDirectory()) { + if ((await fs.stat(src)).isDirectory()) { // Create the destination directory if it doesn't exist - if (!await GBUtil.exists(dest)) { - fs.mkdir(dest, { recursive: true }); + if (!(await GBUtil.exists(dest))) { + await fs.mkdir(dest, { recursive: true }); } // Read all files and directories in the source directory - const entries =await fs.readdir(src); + const entries = await fs.readdir(src); for (let entry of entries) { const srcEntry = path.join(src, entry); const destEntry = path.join(dest, entry); // Recursively copy each entry - this.copyIfNewerRecursive(srcEntry, destEntry); + + await this.copyIfNewerRecursive(srcEntry, destEntry); } } else { // Source is a file, check if we need to copy it + if (await GBUtil.exists(dest)) { - const srcStat =await fs.stat(src); - const destStat =await fs.stat(dest); + const srcStat = await fs.stat(src); + const destStat = await fs.stat(dest); // Copy only if the source file is newer than the destination file if (srcStat.mtime > destStat.mtime) { - fs.cp(src, dest, { force: true }); + await fs.cp(src, dest, { force: true }); } } else { // Destination file doesn't exist, so copy it - fs.cp(src, dest, { force: true }); + await fs.cp(src, dest, { force: true }); } } } @@ -198,8 +199,7 @@ export class GBUtil { } public static async getPdfText(data): Promise { - - const pdf = await getDocument({data}).promise; + const pdf = await getDocument({ data }).promise; let pages = []; for (let i = 1; i <= pdf.numPages; i++) { @@ -229,6 +229,4 @@ export class GBUtil { return urljoin(gbai, packageName ? packageName : `${botId}.${packageType}`); } } - - } diff --git a/templates/law.gbai/law.gbdialog/case.bas b/templates/law.gbai/law.gbdialog/case.bas index 6224e582..8bdfd041 100644 --- a/templates/law.gbai/law.gbdialog/case.bas +++ b/templates/law.gbai/law.gbdialog/case.bas @@ -1,8 +1,14 @@ TALK "What is the case number?" -HEAR caseNumber -text = GET "case.pdf" -text = "Based on this document, answer the person's questions:\n\n" + text -SET CONTEXT text -SET ANSWER MODE "direct" +HEAR cod +text = GET "case-" + cod + ".pdf" -TALK "Case ${caseNumber} loaded. You can ask me anything about the case or request a summary in any way you need." +IF text THEN + + text = "Based on this document, answer the person's questions:\n\n" + text + SET CONTEXT text + SET ANSWER MODE "document" + TALK "Case ${cod} loaded. You can ask me anything about the case or request a summary in any way you need." + +ELSE + TALK "The case was not found, please try again." +END IF \ No newline at end of file diff --git a/templates/law.gbai/law.gbdrive/case.pdf b/templates/law.gbai/law.gbdrive/case-4500.pdf similarity index 100% rename from templates/law.gbai/law.gbdrive/case.pdf rename to templates/law.gbai/law.gbdrive/case-4500.pdf