-
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
7 changed files
with
229 additions
and
2 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
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,50 @@ | ||
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 { anthropicRename } from "../plugins/anthropic-rename.js"; | ||
import { env } from "../env.js"; | ||
import { parseNumber } from "../number-utils.js"; | ||
import { DEFAULT_CONTEXT_WINDOW_SIZE } from "./default-args.js"; | ||
|
||
export const anthropic = cli() | ||
.name("anthropic") | ||
.description("Use Anthropic's Claude API to unminify code") | ||
.option("-m, --model <model>", "The model to use", "claude-3-sonnet-20240229") | ||
.option("-o, --outputDir <output>", "The output directory", "output") | ||
.option( | ||
"-k, --apiKey <apiKey>", | ||
"The Anthropic API key. Alternatively use ANTHROPIC_API_KEY environment variable" | ||
) | ||
.option( | ||
"--baseURL <baseURL>", | ||
"The Anthropic base server URL.", | ||
env("ANTHROPIC_BASE_URL") ?? "https://api.anthropic.com" | ||
) | ||
.option("--verbose", "Show verbose output") | ||
.option( | ||
"--contextSize <contextSize>", | ||
"The context size to use for the LLM", | ||
`${DEFAULT_CONTEXT_WINDOW_SIZE}` | ||
) | ||
.argument("input", "The input minified Javascript file") | ||
.action(async (filename, opts) => { | ||
if (opts.verbose) { | ||
verbose.enabled = true; | ||
} | ||
const apiKey = opts.apiKey ?? env("ANTHROPIC_API_KEY"); | ||
const baseURL = opts.baseURL; | ||
const contextWindowSize = parseNumber(opts.contextSize); | ||
|
||
await unminify(filename, opts.outputDir, [ | ||
babel, | ||
anthropicRename({ | ||
apiKey, | ||
baseURL, | ||
model: opts.model, | ||
contextWindowSize | ||
}), | ||
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,85 @@ | ||
import Anthropic from "@anthropic-ai/sdk"; | ||
import { visitAllIdentifiers } from "./local-llm-rename/visit-all-identifiers.js"; | ||
import { showPercentage } from "../progress.js"; | ||
import { verbose } from "../verbose.js"; | ||
|
||
export function anthropicRename({ | ||
apiKey, | ||
baseURL, | ||
model, | ||
contextWindowSize | ||
}: { | ||
apiKey: string; | ||
baseURL?: string; | ||
model: string; | ||
contextWindowSize: number; | ||
}) { | ||
const client = new Anthropic({ | ||
apiKey, | ||
baseURL | ||
}); | ||
|
||
return async (code: string): Promise<string> => { | ||
return await visitAllIdentifiers( | ||
code, | ||
async (name, surroundingCode) => { | ||
verbose.log(`Renaming ${name}`); | ||
verbose.log("Context: ", surroundingCode); | ||
|
||
const response = await client.messages.create( | ||
toRenamePrompt(name, surroundingCode, model, contextWindowSize) | ||
); | ||
|
||
const result = response.content[0]; | ||
if (!result) { | ||
throw new Error('Failed to rename', { cause: response }); | ||
} | ||
const renamed = result.input.newName | ||
verbose.log(`${name} renamed to ${renamed}`); | ||
return renamed; | ||
}, | ||
contextWindowSize, | ||
showPercentage | ||
); | ||
}; | ||
} | ||
|
||
function toRenamePrompt( | ||
name: string, | ||
surroundingCode: string, | ||
model: string, | ||
contextWindowSize: number, | ||
): Anthropic.Messages.MessageCreateParams { | ||
return { | ||
model, | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: `Analyze this code and suggest a descriptive name for the variable/function \`${name}\`: | ||
${surroundingCode}` | ||
} | ||
], | ||
max_tokens: contextWindowSize, | ||
tools: [ | ||
{ | ||
name: "suggest_name", | ||
description: "Suggest a descriptive name for the code element", | ||
input_schema: { | ||
type: "object", | ||
properties: { | ||
newName: { | ||
type: "string", | ||
description: `The new descriptive name for the variable/function called \`${name}\`` | ||
} | ||
}, | ||
required: ["newName"], | ||
additionalProperties: false | ||
} | ||
} | ||
], | ||
tool_choice: { | ||
type: "tool", | ||
name: "suggest_name" | ||
} | ||
}; | ||
} |
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( | ||
"anthropic", | ||
"fixtures/example.min.js", | ||
"--verbose", | ||
"--outputDir", | ||
TEST_OUTPUT_DIR | ||
); | ||
|
||
await expectStartsWith( | ||
["EXCELLENT", "GOOD"], | ||
await fileIsMinified(`${TEST_OUTPUT_DIR}/deobfuscated.js`) | ||
); | ||
}); |