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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mightymeld/codemirror-copilot

v0.0.9

Published

This CodeMirror extension lets you use GPT to autocomplete code in CodeMirror.

Downloads

34

Readme

codemirror-copilot

npm version

This CodeMirror extension lets you use GPT to autocomplete code in CodeMirror. It let's you call your API with current code (prefix and suffix from current cursor position) and let your API return the code to autocomplete.

Screenshot

Demo

https://copilot.asadmemon.com

Installation

npm install codemirror-copilot --save

Usage

import CodeMirror from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { inlineCopilot } from "codemirror-copilot";

function CodeEditor() {
  return (
    <CodeMirror
      value=""
      height="300px"
      extensions={[
        javascript({ jsx: true }),

        // Implement a function that returns a promise that resolves to the prediction
        inlineCopilot(async (prefix, suffix) => {
          const res = await fetch("/api/autocomplete", {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ prefix, suffix, language: "javascript" }),
          });

          const { prediction } = await res.json();
          return prediction;
        }),
      ]}
    />
  );
}

You also need to implement an API that returns the prediction. For above code, here is an example API in Next.js that uses OpenAI 's gpt-3.5-turbo-1106 model:

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

async function completion(
  prefix,
  suffix,
  model = "gpt-3.5-turbo-1106",
  language,
) {
  const chatCompletion = await openai.chat.completions.create({
    messages: [
      {
        role: "system",
        content: `You are a ${
          language ? language + " " : ""
        }programmer that replaces <FILL_ME> part with the right code. Only output the code that replaces <FILL_ME> part. Do not add any explanation or markdown.`,
      },
      { role: "user", content: `${prefix}<FILL_ME>${suffix}` },
    ],
    model,
  });

  return chatCompletion.choices[0].message.content;
}

export default async function handler(req, res) {
  const { prefix, suffix, model, language } = req.body;
  const prediction = await completion(prefix, suffix, model, language);
  console.log(prediction);
  res.status(200).json({ prediction });
}

API

inlineCopilot(apiCallingFn: (prefix: string, suffix: string) => Promise<string>, delay: number = 1000)

Provides extension for CodeMirror that renders the hints UI + Tab completion based on the prediction returned by the API.

The delay parameter is the time in milliseconds to wait before calling the apiCallingFn after the user stops typing. Default value is 1000.

The extension also implements local caching of predictions to avoid unnecessary API calls.

clearLocalCache()

Clears the local cache of predictions.

Local Development

In one terminal, build the library itself by running:

cd packages/codemirror-copilot
npm install
npm run dev

In another terminal, run the demo website:

cd website
npm install
npm run dev

Acknowledgements

This code is based on codemirror-extension-inline-suggestion by Shan Aminzadeh.

License

MIT © Asad Memon