npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

patchling

v0.2.0

Published

Natural-language code transformation as a browser-first JS primitive: generateDiff turns a plain-English goal into a git diff, and smartapply melds it into your source even when git apply would reject it. Wired to NanoGPT. Formerly gptdiff-js.

Downloads

157

Readme

patchling

Formerly gptdiff-js.

Natural-language code transformation, browser-first. Hand patchling an in-memory { path: content } file map plus a plain-English goal; generateDiff returns a unified git diff, and smartapply melds that diff into your source — even when git apply would reject it (drifted lines, fuzzy hunks, renames, new files, deletions). It's a bounded primitive you embed in your own app, not an open-ended coding agent. Wired to NanoGPT for completions (including "Sign in with NanoGPT" OAuth PKCE).

Everything runs in the browser (and in Node 18+): no filesystem, no build step, zero runtime dependencies. The diff engine is a faithful port of the Python gptdiff; it operates on an in-memory file map instead of a directory on disk.

See it in action: live browser demos →.

The familygptdiff (Python library + CLI) · patchling (this package — the browser/Node runtime) · nanoodle.com (visual AI workflow editor built on it)

import { generateDiff, smartapply, buildEnvironment } from 'patchling';

const files = { 'greet.py': 'def greet():\n    print("hello")\n' };

// 1. Ask the model for a unified diff
const diff = await generateDiff(buildEnvironment(files), 'Say goodbye instead of hello');

// 2. Apply it with AI-powered conflict resolution
const updated = await smartapply(diff, files);
console.log(updated['greet.py']);

Install / run

npm install patchling
import { generateDiff, smartapply } from 'patchling';

Browser, no build step — it's zero-dependency ESM, so a CDN import works directly (once the package is published to npm):

import { generateDiff, smartapply } from 'https://esm.sh/patchling';

Run from source — point an import at src/index.js, or open index.html from a static server:

npx serve .        # then visit the printed URL and try the demo
npm test           # run the unit suite (Node's built-in test runner)
npm run test:live  # hit NanoGPT for real (requires env vars, see below)

Configuration

Configuration comes from environment variables (Node) or setEnv(...) overrides (browser). OAuth sign-in sets the API key override for you.

| Variable | Purpose | Default | | --- | --- | --- | | GPTDIFF_LLM_API_KEY | NanoGPT API key (sk-nano-…) | — (required for live calls) | | GPTDIFF_LLM_BASE_URL | OpenAI-compatible base URL | https://nano-gpt.com/api/v1/ | | GPTDIFF_MODEL | Model id | xiaomi/mimo-v2.5-pro-ultraspeed |

import { setEnv } from 'patchling';
setEnv('GPTDIFF_LLM_API_KEY', 'sk-nano-…'); // browser, no process.env

Sign in with NanoGPT (OAuth 2.0 + PKCE)

src/oauth.js implements the NanoGPT OAuth PKCE flow using Web Crypto. In a browser:

import { registerClient, beginSignIn, completeSignIn } from 'patchling/oauth';

// On page load — finishes the redirect and stores the access token as
// the GPTDIFF_LLM_API_KEY override automatically:
await completeSignIn();

// To start sign-in (clientId from a one-time dynamic registration):
const { client_id } = await registerClient({
  clientName: 'My App',
  redirectUri: location.origin + location.pathname,
});
await beginSignIn({ clientId: client_id }); // redirects to NanoGPT

Low-level helpers are also exported: generatePkce, buildAuthorizeUrl, exchangeCodeForToken, generateCodeChallenge.

API

generateDiff(environment, goal, opts?) → Promise<string>

Builds the prompt, calls the LLM, and returns the unified diff extracted from the ```diff block(s) of the response.

  • opts.model, opts.temperature, opts.maxTokens, opts.apiKey, opts.baseUrl, opts.prepend, opts.images
  • opts.onToken(text) => void streaming callback. When provided, the completion is streamed (SSE) and called once per token as it arrives; the returned promise still resolves to the extracted diff. Tokens are the raw model output, which may include prose around the ```diff block.
  • opts.callLlm — inject a custom/mock completion client (used heavily in tests).
  • opts.onUsage — optional callback invoked with { promptTokens, completionTokens, totalTokens } after the LLM call completes, so callers can surface cost/usage without changing the Promise<string> return type.

smartapply(diffText, files, opts?) → Promise<{ path: content }>

Applies a diff to an in-memory file map with per-file, LLM-assisted conflict resolution (runs files concurrently). Handles creation, modification, and deletion; <think>…</think> and reasoning preambles are stripped automatically. Returns a new map; deleted files are omitted.

  • opts.model, opts.apiKey, opts.baseUrl, opts.maxTokens
  • opts.onToken(text, path) => void streaming callback for LLM-resolved files. Files stream concurrently, so calls for different files interleave; use the path argument to demultiplex. Files applied by the deterministic fast path emit no tokens.
  • opts.callLlmForApply — inject a custom/mock single-file applier.

applyDiff(files, diffText) → { changed, files }

Deterministic, no-LLM patch application (the strict "basic" applier). changed is true iff something was created, modified, or deleted; files is the updated map (equal to the input on failure — no partial writes).

Other exports

parseDiffPerFile, buildEnvironment, colorCodeDiff, swallowReasoning, stripBadOutput, extractDiffBlocks, callLlm, resolveApiKey, resolveBaseUrl, getEnv, setEnv, and the oauth namespace.

Differences from the Python package

  • No filesystem. applyDiff/smartapply take and return a { path: content } map rather than reading/writing a project directory.
  • Scope. Only generateDiff + smartapply (and their dependencies) are ported — not the gptdiff/gptpatch/plangptdiff CLIs.
  • Dependency injection replaces Python's monkeypatch: pass callLlm / callLlmForApply to test without a network.
  • fetch + Web Crypto replace openai/requests/tiktoken.

License

MIT