humanifyjs
Version:
> Deobfuscate Javascript code using LLMs ("AI")
41 lines (40 loc) • 1.73 kB
JavaScript
import { visitAllIdentifiers } from "./local-llm-rename/visit-all-identifiers.js";
import { verbose } from "../verbose.js";
import { showPercentage } from "../progress.js";
import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";
export function geminiRename({ apiKey, model: modelName }) {
const client = new GoogleGenerativeAI(apiKey);
return async (code) => {
return await visitAllIdentifiers(code, async (name, surroundingCode) => {
verbose.log(`Renaming ${name}`);
verbose.log("Context: ", surroundingCode);
const model = client.getGenerativeModel(toRenameParams(name, modelName));
const result = await model.generateContent(surroundingCode);
const renamed = JSON.parse(result.response.text()).newName;
verbose.log(`Renamed to ${renamed}`);
return renamed;
}, showPercentage);
};
}
function toRenameParams(name, model) {
return {
model,
systemInstruction: `Rename Javascript variables/function \`${name}\` to have descriptive name based on their usage in the code."`,
generationConfig: {
responseMimeType: "application/json",
responseSchema: {
nullable: false,
description: "The new name for the variable/function",
type: SchemaType.OBJECT,
properties: {
newName: {
type: SchemaType.STRING,
nullable: false,
description: `The new name for the variable/function called \`${name}\``
}
},
required: ["newName"]
}
}
};
}