monacopilot 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/build/index.d.mts +27 -15
- package/build/index.d.ts +27 -15
- package/build/index.js +8 -24
- package/build/index.mjs +8 -24
- package/package.json +3 -10
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Monacopilot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Monaco Editor](https://microsoft.github.io/monaco-editor/) with AI auto-completion, inspired by [GitHub Copilot](https://github.com/features/copilot/).
|
|
4
4
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
@@ -11,3 +11,7 @@ Extended [Monaco Editor](https://microsoft.github.io/monaco-editor/) with AI aut
|
|
|
11
11
|
For guidelines on contributing, Please read the [contributing guide](https://github.com/arshad-yaseen/monacopilot/blob/main/CONTRIBUTING.md).
|
|
12
12
|
|
|
13
13
|
We welcome contributions from the community to enhance Monacopilot's capabilities and make it even more powerful ❤️
|
|
14
|
+
|
|
15
|
+
## Security
|
|
16
|
+
|
|
17
|
+
MonacoPilot does not store or use your code; it only provides AI completions.
|
package/build/index.d.mts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, Theme, loader, useMonaco } from '@monaco-editor/react';
|
|
3
|
-
import React from 'react';
|
|
1
|
+
import * as monaco from 'monaco-editor';
|
|
4
2
|
|
|
3
|
+
type Monaco = typeof monaco;
|
|
4
|
+
|
|
5
|
+
type StandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
|
|
6
|
+
|
|
7
|
+
interface CopilotOptions {
|
|
8
|
+
model: CompletionModel | undefined;
|
|
9
|
+
}
|
|
5
10
|
type Endpoint = string;
|
|
6
11
|
type Filename = string;
|
|
7
12
|
type Technologies = string[];
|
|
@@ -20,17 +25,20 @@ type ExternalContext = {
|
|
|
20
25
|
*/
|
|
21
26
|
content: string;
|
|
22
27
|
}[];
|
|
23
|
-
interface
|
|
28
|
+
interface RegisterCopilotOptions {
|
|
24
29
|
/**
|
|
25
|
-
*
|
|
26
|
-
* For example, if you are editing a file named `utils.js`, the completions will be more relevant to utility functions.
|
|
30
|
+
* Language of the current model
|
|
27
31
|
*/
|
|
28
|
-
|
|
32
|
+
language: string;
|
|
29
33
|
/**
|
|
30
34
|
* The API endpoint where you started the completion service.
|
|
31
|
-
* [Learn more](https://monacopilot.vercel.app/copilot/setup#integrating-copilot-to-the-editor)
|
|
32
35
|
*/
|
|
33
|
-
endpoint
|
|
36
|
+
endpoint: Endpoint;
|
|
37
|
+
/**
|
|
38
|
+
* The name of the file you are editing. This is used to provide more relevant completions based on the file's purpose.
|
|
39
|
+
* For example, if you are editing a file named `utils.js`, the completions will be more relevant to utility functions.
|
|
40
|
+
*/
|
|
41
|
+
filename?: Filename;
|
|
34
42
|
/**
|
|
35
43
|
* The technologies (libraries, frameworks, etc.) you want to use for the completion.
|
|
36
44
|
* This can provide technology-specific completions.
|
|
@@ -70,10 +78,6 @@ interface CompletionMetadata {
|
|
|
70
78
|
};
|
|
71
79
|
}
|
|
72
80
|
|
|
73
|
-
interface CopilotOptions {
|
|
74
|
-
model: CompletionModel | undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
81
|
/**
|
|
78
82
|
* Copilot class for handling completions using the Groq API.
|
|
79
83
|
*/
|
|
@@ -96,6 +100,14 @@ declare class Copilot {
|
|
|
96
100
|
private createHeaders;
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Registers the Copilot with the Monaco editor.
|
|
105
|
+
* @param monaco The Monaco instance.
|
|
106
|
+
* @param editor The editor instance.
|
|
107
|
+
* @param options The options for the Copilot.
|
|
108
|
+
*
|
|
109
|
+
* @returns A function to unregister the Copilot. Use this to clean up the Copilot.
|
|
110
|
+
*/
|
|
111
|
+
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => (() => void);
|
|
100
112
|
|
|
101
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type
|
|
113
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
export { BeforeMount, DiffBeforeMount, DiffEditor, DiffEditorProps, DiffOnMount, Monaco, MonacoDiffEditor, OnChange, OnMount, OnValidate, Theme, loader, useMonaco } from '@monaco-editor/react';
|
|
3
|
-
import React from 'react';
|
|
1
|
+
import * as monaco from 'monaco-editor';
|
|
4
2
|
|
|
3
|
+
type Monaco = typeof monaco;
|
|
4
|
+
|
|
5
|
+
type StandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
|
|
6
|
+
|
|
7
|
+
interface CopilotOptions {
|
|
8
|
+
model: CompletionModel | undefined;
|
|
9
|
+
}
|
|
5
10
|
type Endpoint = string;
|
|
6
11
|
type Filename = string;
|
|
7
12
|
type Technologies = string[];
|
|
@@ -20,17 +25,20 @@ type ExternalContext = {
|
|
|
20
25
|
*/
|
|
21
26
|
content: string;
|
|
22
27
|
}[];
|
|
23
|
-
interface
|
|
28
|
+
interface RegisterCopilotOptions {
|
|
24
29
|
/**
|
|
25
|
-
*
|
|
26
|
-
* For example, if you are editing a file named `utils.js`, the completions will be more relevant to utility functions.
|
|
30
|
+
* Language of the current model
|
|
27
31
|
*/
|
|
28
|
-
|
|
32
|
+
language: string;
|
|
29
33
|
/**
|
|
30
34
|
* The API endpoint where you started the completion service.
|
|
31
|
-
* [Learn more](https://monacopilot.vercel.app/copilot/setup#integrating-copilot-to-the-editor)
|
|
32
35
|
*/
|
|
33
|
-
endpoint
|
|
36
|
+
endpoint: Endpoint;
|
|
37
|
+
/**
|
|
38
|
+
* The name of the file you are editing. This is used to provide more relevant completions based on the file's purpose.
|
|
39
|
+
* For example, if you are editing a file named `utils.js`, the completions will be more relevant to utility functions.
|
|
40
|
+
*/
|
|
41
|
+
filename?: Filename;
|
|
34
42
|
/**
|
|
35
43
|
* The technologies (libraries, frameworks, etc.) you want to use for the completion.
|
|
36
44
|
* This can provide technology-specific completions.
|
|
@@ -70,10 +78,6 @@ interface CompletionMetadata {
|
|
|
70
78
|
};
|
|
71
79
|
}
|
|
72
80
|
|
|
73
|
-
interface CopilotOptions {
|
|
74
|
-
model: CompletionModel | undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
81
|
/**
|
|
78
82
|
* Copilot class for handling completions using the Groq API.
|
|
79
83
|
*/
|
|
@@ -96,6 +100,14 @@ declare class Copilot {
|
|
|
96
100
|
private createHeaders;
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Registers the Copilot with the Monaco editor.
|
|
105
|
+
* @param monaco The Monaco instance.
|
|
106
|
+
* @param editor The editor instance.
|
|
107
|
+
* @param options The options for the Copilot.
|
|
108
|
+
*
|
|
109
|
+
* @returns A function to unregister the Copilot. Use this to clean up the Copilot.
|
|
110
|
+
*/
|
|
111
|
+
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => (() => void);
|
|
100
112
|
|
|
101
|
-
export { type CompletionRequest, type CompletionResponse, Copilot, type
|
|
113
|
+
export { type CompletionRequest, type CompletionResponse, Copilot, type Monaco, type RegisterCopilotOptions, type StandaloneCodeEditor, registerCopilot };
|
package/build/index.js
CHANGED
|
@@ -1,28 +1,12 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`);return e[e.length-1].length};var h=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),
|
|
3
|
-
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=h(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,n=Math.min(o.length,e.length);for(let i=1;i<=n;i++){let
|
|
1
|
+
"use strict";var I=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var te=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var re=(t,e)=>{for(var o in e)I(t,o,{get:e[o],enumerable:!0})},ne=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of te(e))!oe.call(t,n)&&n!==o&&I(t,n,{get:()=>e[n],enumerable:!(r=ee(e,n))||r.enumerable});return t};var ie=t=>ne(I({},"__esModule",{value:!0}),t);var ge={};re(ge,{Copilot:()=>O,registerCopilot:()=>Z});module.exports=ie(ge);var S=(t,e=1e3)=>{let o=null;return(...n)=>(o&&clearTimeout(o),new Promise((i,m)=>{o=setTimeout(()=>{t(...n).then(i).catch(m)},e)}))},B=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var x=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],$=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),d=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),_=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var h=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),b=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var D=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},n=e==="POST"&&o.body?JSON.stringify(o.body):void 0,i=await fetch(t,{method:e,headers:r,body:n,signal:o.signal});if(!i.ok)throw new Error(`${o.error||"Network error"}: ${i.statusText}`);return i.json()},se=(t,e)=>D(t,"GET",e),le=(t,e,o)=>D(t,"POST",{...o,body:e}),T={GET:se,POST:le};var P=class{constructor(e,o){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=o,this.lineCount=e.getLineCount()}ignoreBlankLines(){return this.formattedCompletion.trimStart()===""&&this.originalCompletion!==`
|
|
3
|
+
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=h(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,n=Math.min(o.length,e.length);for(let i=1;i<=n;i++){let m=e.slice(-i),s=o.slice(0,i);if(m===s)r=i;else break}return r>0&&(this.formattedCompletion=this.formattedCompletion.slice(r)),this}preventDuplicateLines(){for(let e=this.cursorPosition.lineNumber+1;e<this.cursorPosition.lineNumber+3&&e<this.lineCount;e++){let o=this.model.getLineContent(e);if(this.normalise(o)===this.normalise(this.originalCompletion))return this.formattedCompletion="",this}return this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}trimStart(){let e=this.formattedCompletion.search(/\S/);return e>this.cursorPosition.column-1&&(this.formattedCompletion=this.formattedCompletion.slice(e)),this}format(e){return this.originalCompletion=e,this.formattedCompletion=e,this.ignoreBlankLines().removeDuplicatesFromStartOfCompletion().preventDuplicateLines().removeInvalidLineBreaks().trimStart(),this.formattedCompletion}};var me=10,g=[],k=(t,e)=>g.filter(o=>{let r=e.getValueInRange(o.range);return d(t,e).startsWith(o.textBeforeCursorInLine)&&(o.range.startLineNumber===t.lineNumber&&t.column===o.range.startColumn||o.completion.startsWith(r)&&o.range.startLineNumber===t.lineNumber&&t.column>=o.range.startColumn-r.length&&t.column<=o.range.endColumn)}),q=t=>{g.length>=me&&g.shift(),g.push(t)},L=()=>{g.length=0};var F={llama:"llama3-70b-8192"},y="llama",U="https://api.groq.com/openai/v1/chat/completions";var G=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var j=(t,e)=>{let o=x(t,e);return!!o&&!G.has(o)},W=(t,e)=>{let o=$(t,e).trim(),r=d(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var H=(t,e,o,r)=>{let n=(t.match(/\n/g)||[]).length,i=_(t),m=x(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+n,endColumn:t.includes(m)?o.lineNumber===e.startLineNumber&&n===0?o.column+i:i:o.column}},K=(t,e,o)=>new P(t,e).format(o),c=t=>({items:t,enableForwardStability:!0});var R=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!j(this.cursorPosition,this.model)&&!W(this.cursorPosition,this.model)}};var M=class{static getModel(){return this.model}static setModel(e){this.model=e}};M.model=y;var N=M;var v=class{constructor(e){this.error=e}logError(e,o,r){console.error(`${e}: ${o}`,r)}monacopilotError(e){this.logError("MONACO_PILOT_ERROR",e,this.error)}apiError(e){this.logError("API_ERROR",e,this.error)}completionError(e){this.logError("COMPLETION_ERROR",e,this.error)}predictionError(e){this.logError("PREDICTION_ERROR",e,this.error)}editorError(e){this.logError("EDITOR_ERROR",e,this.error)}unexpectedError(){this.error instanceof Error?this.logError("UNEXPECTED_ERROR",this.error.message,this.error.stack):this.logError("UNKNOWN_ERROR",String(this.error))}},a=t=>new v(t);var f="<<CURSOR>>",V=t=>t==="javascript"?"latest JavaScript":t,Y=t=>{switch(t){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},J=t=>{let e=V(t.language),o=Y(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},ae=(t,e)=>{if(!t?.length&&!e)return"";let o=B(t),r=V(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},z=t=>{let{filename:e,language:o,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:m,externalContext:s}=t,u=Y(n.completionMode),E=e?`the file named ${e}`:"a larger project",l=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${f}'. Your task is to assist with ${u}. This code is part of ${E}. Please `;switch(n.completionMode){case"fill-in-the-middle":l+=`generate a completion to fill the middle of the code around '${f}'. Ensure the completion replaces '${f}' precisely, maintaining consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break;case"completion":l+=`provide the necessary completion for '${f}' while ensuring consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break}l+=` Output only the necessary completion code, without additional explanations or content.${ae(r,o)}`;let p=`${i}${f}${m}
|
|
4
4
|
|
|
5
|
-
`;return
|
|
6
|
-
${
|
|
5
|
+
`;return s&&s.length>0&&(p+=s.map(C=>`// Path: ${C.path}
|
|
6
|
+
${C.content}
|
|
7
7
|
`).join(`
|
|
8
|
-
`)),
|
|
8
|
+
`)),l+=`
|
|
9
9
|
|
|
10
10
|
<code>
|
|
11
|
-
${
|
|
12
|
-
</code>`,
|
|
13
|
-
$1
|
|
14
|
-
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
15
|
-
$1
|
|
16
|
-
}, $2)`,setInterval:`(() => {
|
|
17
|
-
$1
|
|
18
|
-
}, $2)`,"async f":`unction $1() {
|
|
19
|
-
$2
|
|
20
|
-
}`,"async (":`) => {
|
|
21
|
-
$1
|
|
22
|
-
}`,"async =>":` {
|
|
23
|
-
$1
|
|
24
|
-
}`,") =>":` {
|
|
25
|
-
$1
|
|
26
|
-
}`,"=>":` {
|
|
27
|
-
$1
|
|
28
|
-
}`,"new M":"ap","new W":"eakMap"};var ae=[{language:"javascript",snippets:le}];var v=class{constructor(){this.predictions=new Map,this.loadPredictions()}predict(e,o){try{let r=this.predictions.get(e);if(!r)return"";let n=A(o);return this.findMatchingPrediction(r,n)}catch(r){return a(r).predictionError("Error while predicting next text snippet"),""}}loadPredictions(){ae.forEach(e=>{this.predictions.set(e.language,e.snippets)})}findMatchingPrediction(e,o){for(let[r,n]of Object.entries(e))if(o.startsWith(A(r)))return n;return""}};var P=Te(require("react")),pe=require("@monaco-editor/react");var be=new v,ve=200,Ne=F(se,ve),we=async({monaco:t,model:e,position:o,context:r,token:n,hasCompletionBeenAccepted:i,onShowCompletion:s,options:l})=>{if(i)return u([]);let c=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new L(o,e).shouldProvideCompletions())return u([]);let m=H(o,e).map(d=>({insertText:d.completion,range:d.range}));if(m.length)return s(),u(m);if(n.isCancellationRequested)return u([]);let C=be.predict(l.language,e.getLineContent(o.lineNumber));if(C)return s(),u([{insertText:{snippet:C},range:p}]);try{let d=await Ne({...l,text:c,model:e,position:o,token:n});if(d){let N=te(e,o,d),k=ee(N,p,o,e);return K({completion:N,range:k,textBeforeCursorInLine:f(o,e)}),s(),u([{insertText:N,range:k}])}}catch(d){a(d).completionError("Failed to fetch completion item")}return u([])},me=we;var D=!1,_=!1,ce=({monaco:t,editor:e,...o})=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,i,s,l)=>me({monaco:t,model:n,position:i,context:s,token:l,hasCompletionBeenAccepted:D,onShowCompletion:()=>_=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(n=>{_&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?(D=!0,_=!1):D=!1}),()=>{r.dispose(),V()}}catch(r){a(r).editorError("Error while registering Copilot")}};var Ae=({filename:t,endpoint:e,technologies:o,externalContext:r,onMount:n,...i})=>{let s=P.default.useRef(),l=P.default.useCallback((c,p)=>{try{e&&i.language&&(s.current=ce({monaco:p,editor:c,filename:t,technologies:o,externalContext:r,endpoint:e,language:i.language}))}catch(m){a(m).monacopilotError("Error while registering copilot")}n?.(c,p)},[e,t,n,i.language,o,r]);return P.default.useEffect(()=>()=>{s.current?.()},[]),P.default.createElement(pe.Editor,{...i,onMount:l,options:w(i.options,Y)})},de=Ae;var g=require("@monaco-editor/react");0&&(module.exports={Copilot,DiffEditor,loader,useMonaco});
|
|
11
|
+
${p}
|
|
12
|
+
</code>`,l.endsWith(".")?l:`${l}.`};var ce="application/json",Q=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:n,model:i,position:m})=>{try{let{completion:s}=await T.POST(e,{completionMetadata:pe({filename:t,position:m,model:i,language:o,technologies:r,externalContext:n})},{headers:{"Content-Type":ce},error:"Error while fetching completion item"});return s||null}catch(s){return a(s).completionError("Error while fetching completion item"),null}},pe=({filename:t,position:e,model:o,language:r,technologies:n,externalContext:i})=>{let m=de(e,o),s=h(e,o),u=b(e,o);return{filename:t,language:r,technologies:n,externalContext:i,textBeforeCursor:s,textAfterCursor:u,editorState:{completionMode:m}}},de=(t,e)=>{let o=h(t,e),r=b(t,e);return o&&r?"fill-in-the-middle":"completion"};var O=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,N.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=N.getModel(),r=this.createRequestBody(e,o),n=this.createHeaders(),i=await T.POST(U,r,{headers:n});if(!i.choices||i.choices.length===0)throw new Error("No completion choices received from API");return{completion:i.choices[0].message.content}}catch(o){return o instanceof Error?a(o).apiError("Failed to fetch completion"):a(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{model:F[o],messages:[{role:"system",content:J(e)},{role:"user",content:z(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var ue=250,Ce=S(Q,ue),he=async({monaco:t,model:e,position:o,token:r,hasCompletionBeenAccepted:n,onShowCompletion:i,options:m})=>{let s=e.getValue(),u=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new R(o,e).shouldProvideCompletions())return c([]);let E=k(o,e).map(l=>({insertText:l.completion,range:l.range}));if(E.length)return i(),c(E);if(r.isCancellationRequested)return c([]);if(n)return c([]);try{let l=await Ce({...m,text:s,model:e,position:o});if(l){let p=K(e,o,l),C=H(p,u,o,e);return q({completion:p,range:C,textBeforeCursorInLine:d(o,e)}),i(),c([{insertText:p,range:C}])}}catch(l){a(l).completionError("Failed to fetch completion item")}return c([])},X=he;var w=!1,A=!1,Z=(t,e,o)=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,i,m,s)=>X({monaco:t,model:n,position:i,token:s,hasCompletionBeenAccepted:w,onShowCompletion:()=>A=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(n=>{A&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?(w=!0,A=!1):w=!1}),()=>{r.dispose(),L()}}catch(r){return a(r).editorError("Error while registering Copilot"),()=>{L()}}};0&&(module.exports={Copilot,registerCopilot});
|
package/build/index.mjs
CHANGED
|
@@ -1,28 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
`);return e[e.length-1].length};var
|
|
3
|
-
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=
|
|
1
|
+
var A=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Promise((n,m)=>{o=setTimeout(()=>{t(...i).then(n).catch(m)},e)}))},S=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var x=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],B=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),d=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),$=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var h=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),O=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var _=async(t,e,o={})=>{let r={"Content-Type":"application/json",...o.headers},i=e==="POST"&&o.body?JSON.stringify(o.body):void 0,n=await fetch(t,{method:e,headers:r,body:i,signal:o.signal});if(!n.ok)throw new Error(`${o.error||"Network error"}: ${n.statusText}`);return n.json()},X=(t,e)=>_(t,"GET",e),Z=(t,e,o)=>_(t,"POST",{...o,body:e}),T={GET:X,POST:Z};var P=class{constructor(e,o){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=o,this.lineCount=e.getLineCount()}ignoreBlankLines(){return this.formattedCompletion.trimStart()===""&&this.originalCompletion!==`
|
|
3
|
+
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=h(this.cursorPosition,this.model).trim(),o=this.normalise(this.formattedCompletion),r=0,i=Math.min(o.length,e.length);for(let n=1;n<=i;n++){let m=e.slice(-n),s=o.slice(0,n);if(m===s)r=n;else break}return r>0&&(this.formattedCompletion=this.formattedCompletion.slice(r)),this}preventDuplicateLines(){for(let e=this.cursorPosition.lineNumber+1;e<this.cursorPosition.lineNumber+3&&e<this.lineCount;e++){let o=this.model.getLineContent(e);if(this.normalise(o)===this.normalise(this.originalCompletion))return this.formattedCompletion="",this}return this}removeInvalidLineBreaks(){return this.formattedCompletion=this.formattedCompletion.trimEnd(),this}trimStart(){let e=this.formattedCompletion.search(/\S/);return e>this.cursorPosition.column-1&&(this.formattedCompletion=this.formattedCompletion.slice(e)),this}format(e){return this.originalCompletion=e,this.formattedCompletion=e,this.ignoreBlankLines().removeDuplicatesFromStartOfCompletion().preventDuplicateLines().removeInvalidLineBreaks().trimStart(),this.formattedCompletion}};var ee=10,g=[],D=(t,e)=>g.filter(o=>{let r=e.getValueInRange(o.range);return d(t,e).startsWith(o.textBeforeCursorInLine)&&(o.range.startLineNumber===t.lineNumber&&t.column===o.range.startColumn||o.completion.startsWith(r)&&o.range.startLineNumber===t.lineNumber&&t.column>=o.range.startColumn-r.length&&t.column<=o.range.endColumn)}),k=t=>{g.length>=ee&&g.shift(),g.push(t)},I=()=>{g.length=0};var q={llama:"llama3-70b-8192"},y="llama",F="https://api.groq.com/openai/v1/chat/completions";var U=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var G=(t,e)=>{let o=x(t,e);return!!o&&!U.has(o)},j=(t,e)=>{let o=B(t,e).trim(),r=d(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var W=(t,e,o,r)=>{let i=(t.match(/\n/g)||[]).length,n=$(t),m=x(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+i,endColumn:t.includes(m)?o.lineNumber===e.startLineNumber&&i===0?o.column+n:n:o.column}},H=(t,e,o)=>new P(t,e).format(o),c=t=>({items:t,enableForwardStability:!0});var R=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!G(this.cursorPosition,this.model)&&!j(this.cursorPosition,this.model)}};var M=class{static getModel(){return this.model}static setModel(e){this.model=e}};M.model=y;var b=M;var L=class{constructor(e){this.error=e}logError(e,o,r){console.error(`${e}: ${o}`,r)}monacopilotError(e){this.logError("MONACO_PILOT_ERROR",e,this.error)}apiError(e){this.logError("API_ERROR",e,this.error)}completionError(e){this.logError("COMPLETION_ERROR",e,this.error)}predictionError(e){this.logError("PREDICTION_ERROR",e,this.error)}editorError(e){this.logError("EDITOR_ERROR",e,this.error)}unexpectedError(){this.error instanceof Error?this.logError("UNEXPECTED_ERROR",this.error.message,this.error.stack):this.logError("UNKNOWN_ERROR",String(this.error))}},a=t=>new L(t);var f="<<CURSOR>>",K=t=>t==="javascript"?"latest JavaScript":t,V=t=>{switch(t){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},Y=t=>{let e=K(t.language),o=V(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},te=(t,e)=>{if(!t?.length&&!e)return"";let o=S(t),r=K(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},J=t=>{let{filename:e,language:o,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:m,externalContext:s}=t,u=V(i.completionMode),E=e?`the file named ${e}`:"a larger project",l=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${f}'. Your task is to assist with ${u}. This code is part of ${E}. Please `;switch(i.completionMode){case"fill-in-the-middle":l+=`generate a completion to fill the middle of the code around '${f}'. Ensure the completion replaces '${f}' precisely, maintaining consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break;case"completion":l+=`provide the necessary completion for '${f}' while ensuring consistency, semantic accuracy, and relevance to the context. The completion must start exactly from the cursor position without any preceding or following characters, and it should not introduce any syntactical or semantic errors to the existing code.`;break}l+=` Output only the necessary completion code, without additional explanations or content.${te(r,o)}`;let p=`${n}${f}${m}
|
|
4
4
|
|
|
5
|
-
`;return
|
|
6
|
-
${
|
|
5
|
+
`;return s&&s.length>0&&(p+=s.map(C=>`// Path: ${C.path}
|
|
6
|
+
${C.content}
|
|
7
7
|
`).join(`
|
|
8
|
-
`)),
|
|
8
|
+
`)),l+=`
|
|
9
9
|
|
|
10
10
|
<code>
|
|
11
|
-
${
|
|
12
|
-
</code>`,
|
|
13
|
-
$1
|
|
14
|
-
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
15
|
-
$1
|
|
16
|
-
}, $2)`,setInterval:`(() => {
|
|
17
|
-
$1
|
|
18
|
-
}, $2)`,"async f":`unction $1() {
|
|
19
|
-
$2
|
|
20
|
-
}`,"async (":`) => {
|
|
21
|
-
$1
|
|
22
|
-
}`,"async =>":` {
|
|
23
|
-
$1
|
|
24
|
-
}`,") =>":` {
|
|
25
|
-
$1
|
|
26
|
-
}`,"=>":` {
|
|
27
|
-
$1
|
|
28
|
-
}`,"new M":"ap","new W":"eakMap"};var ie=[{language:"javascript",snippets:ne}];var O=class{constructor(){this.predictions=new Map,this.loadPredictions()}predict(e,o){try{let r=this.predictions.get(e);if(!r)return"";let n=v(o);return this.findMatchingPrediction(r,n)}catch(r){return a(r).predictionError("Error while predicting next text snippet"),""}}loadPredictions(){ie.forEach(e=>{this.predictions.set(e.language,e.snippets)})}findMatchingPrediction(e,o){for(let[r,n]of Object.entries(e))if(o.startsWith(v(r)))return n;return""}};import L from"react";import{Editor as Te}from"@monaco-editor/react";var ge=new O,Ce=200,he=_(re,Ce),Ee=async({monaco:t,model:e,position:o,context:r,token:n,hasCompletionBeenAccepted:i,onShowCompletion:s,options:l})=>{if(i)return u([]);let c=e.getValue(),p=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new M(o,e).shouldProvideCompletions())return u([]);let m=j(o,e).map(d=>({insertText:d.completion,range:d.range}));if(m.length)return s(),u(m);if(n.isCancellationRequested)return u([]);let g=ge.predict(l.language,e.getLineContent(o.lineNumber));if(g)return s(),u([{insertText:{snippet:g},range:p}]);try{let d=await he({...l,text:c,model:e,position:o,token:n});if(d){let I=X(e,o,d),D=Q(I,p,o,e);return G({completion:I,range:D,textBeforeCursorInLine:f(o,e)}),s(),u([{insertText:I,range:D}])}}catch(d){a(d).completionError("Failed to fetch completion item")}return u([])},se=Ee;var $=!1,B=!1,le=({monaco:t,editor:e,...o})=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(n,i,s,l)=>se({monaco:t,model:n,position:i,context:s,token:l,hasCompletionBeenAccepted:$,onShowCompletion:()=>B=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(n=>{B&&(n.keyCode===t.KeyCode.Tab||n.keyCode===t.KeyCode.RightArrow&&n.metaKey)?($=!0,B=!1):$=!1}),()=>{r.dispose(),W()}}catch(r){a(r).editorError("Error while registering Copilot")}};var Pe=({filename:t,endpoint:e,technologies:o,externalContext:r,onMount:n,...i})=>{let s=L.useRef(),l=L.useCallback((c,p)=>{try{e&&i.language&&(s.current=le({monaco:p,editor:c,filename:t,technologies:o,externalContext:r,endpoint:e,language:i.language}))}catch(m){a(m).monacopilotError("Error while registering copilot")}n?.(c,p)},[e,t,n,i.language,o,r]);return L.useEffect(()=>()=>{s.current?.()},[]),L.createElement(Te,{...i,onMount:l,options:b(i.options,H)})},xe=Pe;import{DiffEditor as Qt,useMonaco as Xt,loader as Zt}from"@monaco-editor/react";export{S as Copilot,Qt as DiffEditor,xe as default,Zt as loader,Xt as useMonaco};
|
|
11
|
+
${p}
|
|
12
|
+
</code>`,l.endsWith(".")?l:`${l}.`};var oe="application/json",z=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:i,model:n,position:m})=>{try{let{completion:s}=await T.POST(e,{completionMetadata:re({filename:t,position:m,model:n,language:o,technologies:r,externalContext:i})},{headers:{"Content-Type":oe},error:"Error while fetching completion item"});return s||null}catch(s){return a(s).completionError("Error while fetching completion item"),null}},re=({filename:t,position:e,model:o,language:r,technologies:i,externalContext:n})=>{let m=ne(e,o),s=h(e,o),u=O(e,o);return{filename:t,language:r,technologies:i,externalContext:n,textBeforeCursor:s,textAfterCursor:u,editorState:{completionMode:m}}},ne=(t,e)=>{let o=h(t,e),r=O(t,e);return o&&r?"fill-in-the-middle":"completion"};var N=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,b.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=b.getModel(),r=this.createRequestBody(e,o),i=this.createHeaders(),n=await T.POST(F,r,{headers:i});if(!n.choices||n.choices.length===0)throw new Error("No completion choices received from API");return{completion:n.choices[0].message.content}}catch(o){return o instanceof Error?a(o).apiError("Failed to fetch completion"):a(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{model:q[o],messages:[{role:"system",content:Y(e)},{role:"user",content:J(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var ie=250,se=A(z,ie),le=async({monaco:t,model:e,position:o,token:r,hasCompletionBeenAccepted:i,onShowCompletion:n,options:m})=>{let s=e.getValue(),u=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new R(o,e).shouldProvideCompletions())return c([]);let E=D(o,e).map(l=>({insertText:l.completion,range:l.range}));if(E.length)return n(),c(E);if(r.isCancellationRequested)return c([]);if(i)return c([]);try{let l=await se({...m,text:s,model:e,position:o});if(l){let p=H(e,o,l),C=W(p,u,o,e);return k({completion:p,range:C,textBeforeCursorInLine:d(o,e)}),n(),c([{insertText:p,range:C}])}}catch(l){a(l).completionError("Failed to fetch completion item")}return c([])},Q=le;var v=!1,w=!1,me=(t,e,o)=>{try{let r=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(i,n,m,s)=>Q({monaco:t,model:i,position:n,token:s,hasCompletionBeenAccepted:v,onShowCompletion:()=>w=!0,options:o}),freeInlineCompletions:()=>{}});return e.onKeyDown(i=>{w&&(i.keyCode===t.KeyCode.Tab||i.keyCode===t.KeyCode.RightArrow&&i.metaKey)?(v=!0,w=!1):v=!1}),()=>{r.dispose(),I()}}catch(r){return a(r).editorError("Error while registering Copilot"),()=>{I()}}};export{N as Copilot,me as registerCopilot};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monacopilot",
|
|
3
|
-
"version": "0.9.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.9.3",
|
|
4
|
+
"description": "AI auto-completion for Monaco Editor",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"module": "./build/index.mjs",
|
|
7
7
|
"types": "./build/index.d.ts",
|
|
@@ -50,12 +50,5 @@
|
|
|
50
50
|
"url": "https://github.com/arshad-yaseen/monacopilot"
|
|
51
51
|
},
|
|
52
52
|
"license": "MIT",
|
|
53
|
-
"author": "Arshad Yaseen <m@arshadyaseen.com>"
|
|
54
|
-
"peerDependencies": {
|
|
55
|
-
"react": "^18.2.0",
|
|
56
|
-
"react-dom": "^18.2.0"
|
|
57
|
-
},
|
|
58
|
-
"dependencies": {
|
|
59
|
-
"@monaco-editor/react": "^4.6.0"
|
|
60
|
-
}
|
|
53
|
+
"author": "Arshad Yaseen <m@arshadyaseen.com>"
|
|
61
54
|
}
|