fix (templates): law OK.
This commit is contained in:
		
							parent
							
								
									98ef0213ac
								
							
						
					
					
						commit
						d43a0796b6
					
				
					 4 changed files with 67 additions and 60 deletions
				
			
		| 
						 | 
					@ -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.
 | 
				
			||||||
| 
						 | 
					@ -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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,7 +2688,7 @@ 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')) {
 | 
					      if (GBConfigService.get('STORAGE_NAME')) {
 | 
				
			||||||
| 
						 | 
					@ -2703,10 +2703,14 @@ await fs.writeFile(localName, buf, { encoding: null });
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`);
 | 
					        let packagePath = GBUtil.getGBAIPath(min.botId, `gbdrive`);
 | 
				
			||||||
        let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file);
 | 
					        let filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath, file);
 | 
				
			||||||
      data = await fs.readFile(filePath, 'utf8');
 | 
					        data = await fs.readFile(filePath);
 | 
				
			||||||
      data = new Uint8Array(Buffer.from(data, 'utf8'));
 | 
					        data = new Uint8Array(data);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      return await GBUtil.getPdfText(data);
 | 
					      return await GBUtil.getPdfText(data);
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      GBLogEx.error(min, error);
 | 
				
			||||||
 | 
					      return null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  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}`);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										32
									
								
								src/util.ts
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								src/util.ts
									
										
									
									
									
								
							| 
						 | 
					@ -39,9 +39,9 @@ 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';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -147,9 +147,8 @@ export class GBUtil {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
  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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -157,33 +156,35 @@ export class GBUtil {
 | 
				
			||||||
    // 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}`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue