fix (templates): law OK.

This commit is contained in:
Rodrigo Rodriguez 2024-09-11 14:41:24 -03:00
parent 98ef0213ac
commit d43a0796b6
4 changed files with 67 additions and 60 deletions

View file

@ -414,7 +414,7 @@ export class SystemKeywords {
const buffer = pngPages[0].content; const buffer = pngPages[0].content;
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); 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 }; return { localName: localName, url: url, data: buffer };
} }
@ -707,7 +707,7 @@ export class SystemKeywords {
// Writes it to disk and calculate hash. // Writes it to disk and calculate hash.
const data = await response.arrayBuffer(); 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)); const hash = new Uint8Array(md5.array(data));
// Performs uploading passing local hash. // 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 localName = path.join('work', gbaiName, 'cache', `csv${GBAdminService.getRndReadableIdentifier()}.csv`);
const url = file['@microsoft.graph.downloadUrl']; const url = file['@microsoft.graph.downloadUrl'];
const response = await fetch(url); 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(); var workbook = new Excel.Workbook();
let worksheet = await workbook.csv.readFile(localName); let worksheet = await workbook.csv.readFile(localName);
@ -1490,7 +1490,7 @@ await fs.writeFile(localName, Buffer.from(data), { encoding: null });
user.userSystemId, user.userSystemId,
'systemPrompt.txt' '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); const res = await fetch(url);
let buf: any = Buffer.from(await res.arrayBuffer()); let buf: any = Buffer.from(await res.arrayBuffer());
localName = path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.docx`); 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. // 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 response = await fetch(url);
const buf = Buffer.from(await response.arrayBuffer()); 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 }) => { const getNormalSize = ({ width, height, orientation }) => {
return (orientation || 0) >= 5 ? [height, width] : [width, height]; return (orientation || 0) >= 5 ? [height, width] : [width, height];
@ -2068,7 +2068,7 @@ await fs.writeFile(localName, buf, { encoding: null });
doc.setData(data).render(); doc.setData(data).render();
buf = doc.getZip().generate({ type: 'nodebuffer', compression: 'DEFLATE' }); 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 }; 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 buf = Buffer.from(data.Payment.QrCodeBase64Image, 'base64');
const localName = path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`); 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)); const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
GBLogEx.info(min, `GBPay: ${data.MerchantOrderId} OK: ${url}.`); GBLogEx.info(min, `GBPay: ${data.MerchantOrderId} OK: ${url}.`);
@ -2688,25 +2688,29 @@ await fs.writeFile(localName, buf, { encoding: null });
public async getPdf({ pid, file }) { public async getPdf({ pid, file }) {
const { min } = await DialogKeywords.getProcessInfo(pid); const { min } = await DialogKeywords.getProcessInfo(pid);
GBLogEx.info(min, `BASIC GET (pdf): ${file}`); GBLogEx.info(min, `BASIC GET (pdf): ${file}`);
try {
let data;
let data; if (GBConfigService.get('STORAGE_NAME')) {
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
if (GBConfigService.get('STORAGE_NAME')) { const gbaiName = GBUtil.getGBAIPath(min.botId);
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); let packagePath = '/' + urlJoin(gbaiName, `${min.botId}.gbdrive`);
const gbaiName = GBUtil.getGBAIPath(min.botId); let template = await this.internalGetDocument(client, baseUrl, packagePath, file);
let packagePath = '/' + urlJoin(gbaiName, `${min.botId}.gbdrive`); let url = template['@microsoft.graph.downloadUrl'];
let template = await this.internalGetDocument(client, baseUrl, packagePath, file); const res = await fetch(url);
let url = template['@microsoft.graph.downloadUrl']; let buf: any = Buffer.from(await res.arrayBuffer());
const res = await fetch(url); data = new Uint8Array(buf);
let buf: any = Buffer.from(await res.arrayBuffer()); } else {
data = new Uint8Array(buf); let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`);
} else { let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file);
let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`); data = await fs.readFile(filePath);
let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file); data = new Uint8Array(data);
data = await fs.readFile(filePath, 'utf8'); }
data = new Uint8Array(Buffer.from(data, 'utf8')); return await GBUtil.getPdfText(data);
} catch (error) {
GBLogEx.error(min, error);
return null;
} }
return await GBUtil.getPdfText(data);
} }
public async setContext({ pid, text }) { 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 // Criação de um arquivo temporário para enviar
const tempFilePath = path.resolve('temp_image.jpg'); const tempFilePath = path.resolve('temp_image.jpg');
await fs.writeFile(tempFilePath, imageBuffer); await fs.writeFile(tempFilePath, imageBuffer);
// Publicação da imagem // Publicação da imagem
const page = new Page(pageId); const page = new Page(pageId);
@ -2779,8 +2783,7 @@ await fs.writeFile(tempFilePath, imageBuffer);
ig.state.generateDevice(username); ig.state.generateDevice(username);
await ig.account.login(username, password); await ig.account.login(username, password);
const imageBuffer = await fs.readFile(imagePath); const imageBuffer = await fs.readFile(imagePath);
const publishResult = await ig.publish.photo( const publishResult = await ig.publish.photo({ file: imageBuffer, caption });
{ file: imageBuffer, caption });
GBLogEx.info(min, `Image posted on IG: ${publishResult}`); GBLogEx.info(min, `Image posted on IG: ${publishResult}`);
} }
@ -2790,6 +2793,6 @@ await fs.writeFile(tempFilePath, imageBuffer);
ChatServices.usersMode[user.userSystemId] = mode; ChatServices.usersMode[user.userSystemId] = mode;
GBLogEx.info(min, `LLM Mode (user.userSystemId) : ${mode}`); GBLogEx.info(min, `LLM Mode (${user.userSystemId}): ${mode}`);
} }
} }

View file

@ -35,13 +35,13 @@
'use strict'; 'use strict';
import * as YAML from 'yaml'; import * as YAML from 'yaml';
import SwaggerClient from 'swagger-client'; 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 { GBConfigService } from '../packages/core.gbapp/services/GBConfigService.js';
import path from 'path'; import path from 'path';
import { VerbosityLevel, getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs'; import { VerbosityLevel, getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs';
VerbosityLevel.ERRORS=0; VerbosityLevel.ERRORS = 0;
VerbosityLevel.WARNINGS=0; VerbosityLevel.WARNINGS = 0;
VerbosityLevel.INFOS=0; VerbosityLevel.INFOS = 0;
import { Page } from 'puppeteer'; import { Page } from 'puppeteer';
import urljoin from 'url-join'; import urljoin from 'url-join';
@ -102,17 +102,17 @@ export class GBUtil {
return acc; return acc;
}, {}); }, {});
}; };
const extractedError = extractProps(data); const extractedError = extractProps(data);
// Inline formatting for logs // Inline formatting for logs
return YAML.stringify(extractedError, { return YAML.stringify(extractedError, {
indent: 2, // Defines the indentation indent: 2, // Defines the indentation
flowLevel: -1, // Forces inline formatting flowLevel: -1, // Forces inline formatting
styles: { '!!null': 'canonical' } // Optional: Customize null display styles: { '!!null': 'canonical' } // Optional: Customize null display
} as any); } as any);
} }
public static sleep(ms) { public static sleep(ms) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(resolve, ms); setTimeout(resolve, ms);
@ -146,44 +146,45 @@ export class GBUtil {
return false; // File does not exist return false; // File does not exist
} }
} }
public static async copyIfNewerRecursive(src, dest) { public static async copyIfNewerRecursive(src, dest) {
if (!await GBUtil.exists(src)) { if (!(await GBUtil.exists(src))) {
console.error(`Source path "${src}" does not exist.`); console.error(`Source path "${src}" does not exist.`);
return; return;
} }
// Check if the source is a directory // 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 // Create the destination directory if it doesn't exist
if (!await GBUtil.exists(dest)) { if (!(await GBUtil.exists(dest))) {
fs.mkdir(dest, { recursive: true }); await fs.mkdir(dest, { recursive: true });
} }
// Read all files and directories in the source directory // 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) { for (let entry of entries) {
const srcEntry = path.join(src, entry); const srcEntry = path.join(src, entry);
const destEntry = path.join(dest, entry); const destEntry = path.join(dest, entry);
// Recursively copy each entry // Recursively copy each entry
this.copyIfNewerRecursive(srcEntry, destEntry);
await this.copyIfNewerRecursive(srcEntry, destEntry);
} }
} else { } else {
// Source is a file, check if we need to copy it // Source is a file, check if we need to copy it
if (await GBUtil.exists(dest)) { if (await GBUtil.exists(dest)) {
const srcStat =await fs.stat(src); const srcStat = await fs.stat(src);
const destStat =await fs.stat(dest); const destStat = await fs.stat(dest);
// Copy only if the source file is newer than the destination file // Copy only if the source file is newer than the destination file
if (srcStat.mtime > destStat.mtime) { if (srcStat.mtime > destStat.mtime) {
fs.cp(src, dest, { force: true }); await fs.cp(src, dest, { force: true });
} }
} else { } else {
// Destination file doesn't exist, so copy it // 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<string> { public static async getPdfText(data): Promise<string> {
const pdf = await getDocument({ data }).promise;
const pdf = await getDocument({data}).promise;
let pages = []; let pages = [];
for (let i = 1; i <= pdf.numPages; i++) { for (let i = 1; i <= pdf.numPages; i++) {
@ -229,6 +229,4 @@ export class GBUtil {
return urljoin(gbai, packageName ? packageName : `${botId}.${packageType}`); return urljoin(gbai, packageName ? packageName : `${botId}.${packageType}`);
} }
} }
} }

View file

@ -1,8 +1,14 @@
TALK "What is the case number?" TALK "What is the case number?"
HEAR caseNumber HEAR cod
text = GET "case.pdf" text = GET "case-" + cod + ".pdf"
text = "Based on this document, answer the person's questions:\n\n" + text
SET CONTEXT text
SET ANSWER MODE "direct"
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