Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
codemirror-copilot
Advanced tools
This CodeMirror extension lets you use GPT to autocomplete code in CodeMirror.
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.
npm install codemirror-copilot --save
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 });
}
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.
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
This code is based on codemirror-extension-inline-suggestion by Shan Aminzadeh.
MIT © Asad Memon
FAQs
This CodeMirror extension lets you use GPT to autocomplete code in CodeMirror.
The npm package codemirror-copilot receives a total of 930 weekly downloads. As such, codemirror-copilot popularity was classified as not popular.
We found that codemirror-copilot demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.