2025-05-25 22:40:12 +08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
const __SCOPE = "assistant / rendererCommon";
|
|
|
|
|
|
|
|
|
|
const IPC_METHOD_BASE = "$aura.pls";
|
|
|
|
|
|
|
|
|
|
const updatePlsStatusFromLocal = async () => {
|
|
|
|
|
const plsStatus = (
|
|
|
|
|
await global.ipcRenderer.invoke(`${IPC_METHOD_BASE}.getPlsStats`)
|
|
|
|
|
).data;
|
2025-06-06 02:05:04 +08:00
|
|
|
global.__HUGO_AURA__.plsStats = plsStatus;
|
2025-05-25 22:40:12 +08:00
|
|
|
return plsStatus;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updatePlsSettingsFromLocal = async () => {
|
|
|
|
|
const plsSettings = (
|
|
|
|
|
await global.ipcRenderer.invoke(`${IPC_METHOD_BASE}.getPlsSettings`)
|
|
|
|
|
).data;
|
2025-06-06 02:05:04 +08:00
|
|
|
global.__HUGO_AURA__.plsSettings = plsSettings;
|
2025-05-25 22:40:12 +08:00
|
|
|
return plsSettings;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updatePlsRulesFromLocal = async () => {
|
|
|
|
|
const plsRules = (
|
|
|
|
|
await global.ipcRenderer.invoke(`${IPC_METHOD_BASE}.getPlsRules`)
|
|
|
|
|
).data;
|
2025-06-06 02:05:04 +08:00
|
|
|
global.__HUGO_AURA__.plsRules = plsRules;
|
2025-05-25 22:40:12 +08:00
|
|
|
return plsRules;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
const genRandomHex = () => {
|
|
|
|
|
let result = "";
|
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
|
const randomNum = Math.floor(Math.random() * 0x10000);
|
|
|
|
|
result += randomNum.toString(16).padStart(4, "0");
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} configKey
|
|
|
|
|
* @param {any} configValue
|
|
|
|
|
*/
|
|
|
|
|
const updatePlsConfigToRemote = async (configKey, configValue) => {
|
|
|
|
|
const configLevels = configKey.split(".");
|
|
|
|
|
/** @type {Record<any, any>} */
|
2025-06-06 02:05:04 +08:00
|
|
|
// @ts-expect-error
|
2025-05-25 22:40:12 +08:00
|
|
|
let localUpdateTarget =
|
|
|
|
|
configLevels[0] === "ruleSettings"
|
2025-06-06 02:05:04 +08:00
|
|
|
? global.__HUGO_AURA__.plsRules
|
|
|
|
|
: global.__HUGO_AURA__.plsSettings;
|
2025-05-25 22:40:12 +08:00
|
|
|
for (const level of configLevels.slice(0, -1)) {
|
|
|
|
|
localUpdateTarget = localUpdateTarget[level];
|
|
|
|
|
}
|
|
|
|
|
localUpdateTarget[configLevels.slice(-1)[0]] = configValue;
|
|
|
|
|
|
|
|
|
|
const plsConfigUpdateEvent = new CustomEvent("onPLSConfigUpdate", {
|
|
|
|
|
detail: {
|
2025-06-07 23:51:54 +08:00
|
|
|
path: configLevels,
|
2025-05-25 22:40:12 +08:00
|
|
|
value: configValue,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
document.dispatchEvent(plsConfigUpdateEvent);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {ClientPLSRequest}
|
|
|
|
|
*/
|
|
|
|
|
const data = {
|
|
|
|
|
method: "config.action.updateConfig",
|
|
|
|
|
data: {
|
|
|
|
|
key: configKey,
|
|
|
|
|
value: configValue,
|
|
|
|
|
},
|
|
|
|
|
eventId: genRandomHex(), // 不用 crypto, 因为会带来不必要的性能开销
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
global.ipcRenderer.invoke(`${IPC_METHOD_BASE}.ws.sendWsMessage`, data);
|
|
|
|
|
global.ipcRenderer.invoke(`${IPC_METHOD_BASE}.syncPlsConfig`, {
|
2025-06-06 02:05:04 +08:00
|
|
|
basic: global.__HUGO_AURA__.plsSettings,
|
|
|
|
|
rules: global.__HUGO_AURA__.plsRules,
|
2025-05-25 22:40:12 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
updatePlsRulesFromLocal,
|
|
|
|
|
updatePlsStatusFromLocal,
|
|
|
|
|
updatePlsSettingsFromLocal,
|
|
|
|
|
updatePlsConfigToRemote,
|
|
|
|
|
};
|