ShopAI-Extension/scripts/data/product.js
2024-05-05 23:00:58 +08:00

94 lines
3.1 KiB
JavaScript

/* ask.js
Ask product information to Google Gemini. */
// Import the storage management module.
import {global, session, compare} from "/scripts/secretariat.js";
import hash from "/scripts/utils/hash.js";
import texts from "/scripts/mapping/read.js";
import logging from "/scripts/logging.js";
import {URLs} from "/scripts/utils/URLs.js";
// Don't forget to set the class as export default.
export default class product {
// Create private variables for explicit use for the storage.
#snip;
#options;
/* Initialize a new product with its details.
@param {object} details the product details
@param {object} URL the URL
@param {object} options the options
*/
constructor (details, URL = window.location.href, options) {
if (!((typeof options).includes(`obj`) && !Array.isArray(options) && options != null)) {
options = {};
}
// Set this product's details as part of the object's properties.
this.URL = URLs.clean(URL);
this.details = details;
// Set private variables.
this.#options = options;
// Set the status.
this.status = {};
};
/* Attach the product data to the storage. */
async attach() {
// Add the data digest.
this.#snip = (await hash.digest(this.details, {"output": "Array"}));
// Add the status about this data.
this.status[`update`] = !(await (compare([`sites`, this.URL, `snip`], this.#snip)));
}
async save() {
// Stop when not attached (basically, not entirely initialized).
if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)};
// There is only a need to save the data if an update is needed.
if (this.status[`update`]) {
// Save the data to the storage.
await global.write([`sites`, this.URL, `status`], this.status, -1);
await global.write([`sites`, this.URL, `snip`], this.#snip, 1);
// Write the analysis data to the storage.
(this[`analysis`]) ? global.write([`sites`, this.URL, `analysis`], this.analysis, 1): false;
}
};
async analyze() {
// Stop when the data is already analyzed.
if (this[`analysis`]) {return(this.analysis)}
else if (this.status ? (!this.status.update) : false) {this.analysis = await global.read([`sites`, this.URL, `analysis`]);}
if ((this.analysis && this.analysis != null && this.analysis != undefined) ? !((typeof this.analysis).includes(`obj`) && !Array.isArray(this.analysis)) : true) {
const gemini = (await import(chrome.runtime.getURL("scripts/AI/gemini.js"))).default;
let analyzer = new gemini (await global.read([`settings`,`analysis`,`api`,`key`]), `gemini-pro`);
// Add the prompt.
let PROMPT = [];
PROMPT.push({"text": ((new texts(`AI_message_prompt`)).localized).concat(JSON.stringify(this.details))});
// Run the analysis.
await analyzer.generate(PROMPT);
// Raise an error if the product analysis is blocked.
this.status[`blocked`] = analyzer.blocked;
if (this.status[`blocked`]) {
throw new Error((new texts(`error_msg_blocked`)).localized)
};
if (analyzer.candidate) {
// Remove all markdown formatting.
this.analysis = JSON.parse(analyzer.candidate.replace(/(```json|```|`)/g, ''));
};
};
return(this.analysis);
};
};