new(whatsapp.gblib): LLM SQL.

This commit is contained in:
Rodrigo Rodriguez 2024-08-11 16:44:10 -03:00
parent 8dd7ac5597
commit 067163afa6
2 changed files with 20 additions and 18 deletions

View file

@ -630,33 +630,35 @@ export class GBVMService extends GBService {
} }
const ensureTokens = async (firstTime) => { const ensureTokens = async (firstTime) => {
const REFRESH_THRESHOLD_MS = 10 * 60 * 1000; // 10 minutes in milliseconds
const tokens = this.tokens ? this.tokens.split(',') : []; const tokens = this.tokens ? this.tokens.split(',') : [];
for(__indexer in tokens) { for (let i = 0; i < tokens.length; i++) {
const tokenName = tokens[__indexer]; const tokenName = tokens[i];
// Auto update Bearar authentication for the first token. // Auto update Bearer authentication for the first token.
const expiresOn = new Date(global[tokenName + '_expiresOn']);
const expiresOn = new Date(global[tokenName + "_expiresOn"]); const expiration = expiresOn.getTime() - REFRESH_THRESHOLD_MS;
const expiration = expiresOn.getTime() - (10 * 60 * 1000);
// Expires token 10min. before or if it the first time, load it. // Expires token 10min. before or if it the first time, load it.
if (expiration < Date.now() || firstTime) {
if (expiration < new Date().getTime() || firstTime) { console.log('Expired. Refreshing token...' + expiration);
console.log ('Expired. Refreshing token...' + expiration); try {
const {token, expiresOn} = await sys.getCustomToken({pid, tokenName}); const result = await sys.getCustomToken({pid: this.pid, tokenName: tokenName});
global[tokenName] = result.token;
global[tokenName] = token; global[tokenName + '_expiresOn'] = result.expiresOn;
global[tokenName + "_expiresOn"]= expiresOn; console.log('DONE:' + new Date(global[tokenName + '_expiresOn']));
console.log ('DONE:' + new Date(global[tokenName + "_expiresOn"]) ); } catch (error) {
console.error('Failed to refresh token for ' + tokenName + ':', error);
continue;
}
} }
if (__indexer == 0) { if (i == 0) {
headers['Authorization'] = 'Bearer ' + global[tokenName]; headers['Authorization'] = 'Bearer ' + global[tokenName];
} }
} }
}; };
const sleep = async (ms) => { const sleep = async (ms) => {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(resolve, ms); setTimeout(resolve, ms);

View file

@ -244,7 +244,7 @@ export class KeywordsExpressions {
keywords[i++] = [/^\s*end function/gim, '}']; keywords[i++] = [/^\s*end function/gim, '}'];
keywords[i++] = [/^\s*function +(.*)\((.*)\)/gim, '$1 = async ($2) => {\n']; keywords[i++] = [/^\s*function +(.*)\((.*)\)/gim, 'const $1 = async ($2) => {\n'];
keywords[i++] = [/^\s*for +(.*to.*)/gim, 'for ($1) {']; keywords[i++] = [/^\s*for +(.*to.*)/gim, 'for ($1) {'];