-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Gemini tests | ||
|
||
on: pull_request | ||
|
||
jobs: | ||
test-gemini: | ||
name: Run tests | ||
runs-on: macos-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Cache test model | ||
id: cache-model | ||
uses: actions/cache@v4 | ||
with: | ||
path: /Users/runner/.humanifyjs/models/ | ||
key: models-phi | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
cache: "npm" | ||
- run: npm install --ci | ||
- run: npm run build | ||
- run: npm run download-ci-model | ||
- run: npm run test:gemini | ||
env: | ||
GEMINI_API_KEY: ${{secrets.GEMINI_API_KEY}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { cli } from "../cli.js"; | ||
import prettier from "../plugins/prettier.js"; | ||
import { unminify } from "../unminify.js"; | ||
import babel from "../plugins/babel/babel.js"; | ||
import { verbose } from "../verbose.js"; | ||
import { geminiRename } from "../plugins/gemini-rename.js"; | ||
import { env } from "../env.js"; | ||
|
||
export const azure = cli() | ||
.name("gemini") | ||
.description("Use Google Gemini/AIStudio API to unminify code") | ||
.option("-m, --model <model>", "The model to use", "gemini-1.5-flash") | ||
.option("-o, --outputDir <output>", "The output directory", "output") | ||
.option( | ||
"-k, --apiKey <apiKey>", | ||
"The Google Gemini/AIStudio API key. Alternatively use GEMINI_API_KEY environment variable" | ||
) | ||
.option("--verbose", "Show verbose output") | ||
.argument("input", "The input minified Javascript file") | ||
.action(async (filename, opts) => { | ||
if (opts.verbose) { | ||
verbose.enabled = true; | ||
} | ||
|
||
const apiKey = opts.apiKey ?? env("GEMINI_API_KEY"); | ||
await unminify(filename, opts.outputDir, [ | ||
babel, | ||
geminiRename({ apiKey, model: opts.model }), | ||
prettier | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { visitAllIdentifiers } from "./local-llm-rename/visit-all-identifiers.js"; | ||
import { verbose } from "../verbose.js"; | ||
import { showPercentage } from "../progress.js"; | ||
import { | ||
GoogleGenerativeAI, | ||
ModelParams, | ||
SchemaType | ||
} from "@google/generative-ai"; | ||
|
||
export function geminiRename({ | ||
apiKey, | ||
model: modelName | ||
}: { | ||
apiKey: string; | ||
model: string; | ||
}) { | ||
const client = new GoogleGenerativeAI(apiKey); | ||
|
||
return async (code: string): Promise<string> => { | ||
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: string, model: string): ModelParams { | ||
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"] | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import test from "node:test"; | ||
import { readFile, rm } from "node:fs/promises"; | ||
import { testPrompt } from "./test-prompt.js"; | ||
import { gbnf } from "../plugins/local-llm-rename/gbnf.js"; | ||
import assert from "node:assert"; | ||
import { humanify } from "../test-utils.js"; | ||
|
||
const TEST_OUTPUT_DIR = "test-output"; | ||
|
||
test.afterEach(async () => { | ||
await rm(TEST_OUTPUT_DIR, { recursive: true, force: true }); | ||
}); | ||
|
||
test("Unminifies an example file successfully", async () => { | ||
const fileIsMinified = async (filename: string) => { | ||
const prompt = await testPrompt(); | ||
return await prompt( | ||
`Your job is to read the following code and rate it's readabillity and variable names. Answer "EXCELLENT", "GOOD" or "UNREADABLE".`, | ||
await readFile(filename, "utf-8"), | ||
gbnf`${/("EXCELLENT" | "GOOD" | "UNREADABLE") [^.]+/}.` | ||
); | ||
}; | ||
const expectStartsWith = (expected: string[], actual: string) => { | ||
assert( | ||
expected.some((e) => actual.startsWith(e)), | ||
`Expected "${expected}" but got ${actual}` | ||
); | ||
}; | ||
|
||
await expectStartsWith( | ||
["UNREADABLE"], | ||
await fileIsMinified(`fixtures/example.min.js`) | ||
); | ||
|
||
await humanify( | ||
"gemini", | ||
"fixtures/example.min.js", | ||
"--verbose", | ||
"--outputDir", | ||
TEST_OUTPUT_DIR | ||
); | ||
|
||
await expectStartsWith( | ||
["EXCELLENT", "GOOD"], | ||
await fileIsMinified(`${TEST_OUTPUT_DIR}/deobfuscated.js`) | ||
); | ||
}); |