Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dotenv": "^16.4.5",
"ipull": "^3.9.0",
"node-llama-cpp": "^3.0.0-beta.40",
"ollama": "^0.6.0",
"openai": "^4.55.1",
"tsx": "^4.16.2",
"typescript": "^5.5.4",
Expand Down
44 changes: 44 additions & 0 deletions src/commands/ollama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { cli } from "../cli.js";
import prettier from "../plugins/prettier.js";
import { unminify } from "../unminify.js";
import babel from "../plugins/babel/babel.js";
import { ollamaRename } from "../plugins/ollama/ollama-rename.js";
import { verbose } from "../verbose.js";
import { env } from "../env.js";
import { parseNumber } from "../number-utils.js";
import { DEFAULT_CONTEXT_WINDOW_SIZE } from "./default-args.js";

export const ollama = cli()
.name("ollama")
.description("Use Ollama to unminify code")
.option("-m, --model <model>", "The model to use", "gpt-oss:20b")
.option("-o, --outputDir <output>", "The output directory", "output")
.option(
"--baseURL <baseURL>",
"The Ollama base server URL.",
env("OLLAMA_BASE_URL") ?? "http://localhost:11434"
)
.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 baseURL = opts.baseURL;
const contextWindowSize = parseNumber(opts.contextSize);
await unminify(filename, opts.outputDir, [
babel,
ollamaRename({
baseURL,
model: opts.model,
contextWindowSize
}),
prettier
]);
});
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { version } from "../package.json";
import { download } from "./commands/download.js";
import { local } from "./commands/local.js";
import { openai } from "./commands/openai.js";
import { ollama } from "./commands/ollama.js";
import { cli } from "./cli.js";
import { azure } from "./commands/gemini.js";

cli()
.name("humanify")
.description("Unminify code using OpenAI's API or a local LLM")
.description("Unminify code using OpenAI's API, Ollama, or a local LLM")
.version(version)
.addCommand(local)
.addCommand(openai)
.addCommand(ollama)
.addCommand(azure)
.addCommand(download())
.parse(process.argv);
53 changes: 53 additions & 0 deletions src/plugins/ollama/ollama-rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Ollama } from "ollama";
import { visitAllIdentifiers } from "../local-llm-rename/visit-all-identifiers.js";
import { showPercentage } from "../../progress.js";
import { verbose } from "../../verbose.js";

export function ollamaRename({
baseURL,
model,
contextWindowSize
}: {
baseURL?: string;
model: string;
contextWindowSize: number;
}) {
const client = new Ollama({ host: 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.chat({
model,
messages: [
{
role: "system",
content: `Rename Javascript variables/function \`${name}\` to have descriptive name based on their usage in the code. Respond only with valid JSON in the format: {"newName": "your_new_name_here"}`
},
{
role: "user",
content: surroundingCode
}
],
format: "json"
});

const result = response.message?.content;
if (!result) {
throw new Error("Failed to rename", { cause: response });
}
const renamed = JSON.parse(result).newName;

verbose.log(`Renamed to ${renamed}`);

return renamed;
},
contextWindowSize,
showPercentage
);
};
}
Loading