monacopilot 0.9.0 → 0.9.1
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/build/index.d.mts +4 -4
- package/build/index.d.ts +4 -4
- package/build/index.js +9 -10
- package/build/index.mjs +9 -10
- package/package.json +5 -1
package/build/index.d.mts
CHANGED
|
@@ -63,8 +63,8 @@ interface CompletionMetadata {
|
|
|
63
63
|
filename: Filename | undefined;
|
|
64
64
|
technologies: Technologies | undefined;
|
|
65
65
|
externalContext: ExternalContext | undefined;
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
textAfterCursor: string;
|
|
67
|
+
textBeforeCursor: string;
|
|
68
68
|
editorState: {
|
|
69
69
|
completionMode: CompletionMode;
|
|
70
70
|
};
|
|
@@ -75,7 +75,7 @@ interface CopilotOptions {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
* Copilot class for handling
|
|
78
|
+
* Copilot class for handling completions using the Groq API.
|
|
79
79
|
*/
|
|
80
80
|
declare class Copilot {
|
|
81
81
|
private readonly apiKey;
|
|
@@ -89,7 +89,7 @@ declare class Copilot {
|
|
|
89
89
|
/**
|
|
90
90
|
* Sends a completion request to Groq API and returns the completion.
|
|
91
91
|
* @param {CompletionRequest} params - The metadata required to generate the completion.
|
|
92
|
-
* @returns {Promise<CompletionResponse>} The completed
|
|
92
|
+
* @returns {Promise<CompletionResponse>} The completed text snippet or an error.
|
|
93
93
|
*/
|
|
94
94
|
complete({ completionMetadata, }: CompletionRequest): Promise<CompletionResponse>;
|
|
95
95
|
private createRequestBody;
|
package/build/index.d.ts
CHANGED
|
@@ -63,8 +63,8 @@ interface CompletionMetadata {
|
|
|
63
63
|
filename: Filename | undefined;
|
|
64
64
|
technologies: Technologies | undefined;
|
|
65
65
|
externalContext: ExternalContext | undefined;
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
textAfterCursor: string;
|
|
67
|
+
textBeforeCursor: string;
|
|
68
68
|
editorState: {
|
|
69
69
|
completionMode: CompletionMode;
|
|
70
70
|
};
|
|
@@ -75,7 +75,7 @@ interface CopilotOptions {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
* Copilot class for handling
|
|
78
|
+
* Copilot class for handling completions using the Groq API.
|
|
79
79
|
*/
|
|
80
80
|
declare class Copilot {
|
|
81
81
|
private readonly apiKey;
|
|
@@ -89,7 +89,7 @@ declare class Copilot {
|
|
|
89
89
|
/**
|
|
90
90
|
* Sends a completion request to Groq API and returns the completion.
|
|
91
91
|
* @param {CompletionRequest} params - The metadata required to generate the completion.
|
|
92
|
-
* @returns {Promise<CompletionResponse>} The completed
|
|
92
|
+
* @returns {Promise<CompletionResponse>} The completed text snippet or an error.
|
|
93
93
|
*/
|
|
94
94
|
complete({ completionMetadata, }: CompletionRequest): Promise<CompletionResponse>;
|
|
95
95
|
private createRequestBody;
|
package/build/index.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)[e.
|
|
3
|
-
|
|
4
|
-
`)[t-1];var Q=async(e,t,o={})=>{let r={"Content-Type":"application/json",...o.headers},n=t==="POST"&&o.body?JSON.stringify(o.body):void 0,i=await fetch(e,{method:t,headers:r,body:n,signal:o.signal});if(!i.ok)throw new Error(`${o.error||"Network error"}: ${i.statusText}`);return i.json()},Ae=(e,t)=>Q(e,"GET",t),we=(e,t,o)=>Q(e,"POST",{...o,body:t}),b={GET:Ae,POST:we};var E="<<CURSOR>>",Z=e=>e==="javascript"?"latest JavaScript":e,ee=e=>{switch(e){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},te=e=>{let t=Z(e.language),o=ee(e.editorState.completionMode);return`You are an expert ${t||""} code completion assistant known for exceptional skill in ${o}.`},_e=(e,t)=>{if(!e?.length&&!t)return"";let o=z(e),r=Z(t);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},oe=e=>{let{filename:t,language:o,technologies:r,editorState:n,codeBeforeCursor:i,codeAfterCursor:s,externalContext:c}=e,l=ee(n.completionMode),p=t?`the file named ${t}`:"a larger project",a=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${E}'. Your task is to assist with ${l}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":a+=`generate a completion to fill the middle of the code around '${E}'. Ensure the completion replaces '${E}' 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":a+=`provide the necessary completion for '${E}' 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}a+=` Output only the necessary completion code, without additional explanations or content.${_e(r,o)}`;let h=`${i}${E}${s}
|
|
1
|
+
"use strict";var ue=Object.create;var x=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var Ce=Object.getPrototypeOf,he=Object.prototype.hasOwnProperty;var Ee=(t,e)=>{for(var o in e)x(t,o,{get:e[o],enumerable:!0})},q=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of ge(e))!he.call(t,n)&&n!==o&&x(t,n,{get:()=>e[n],enumerable:!(r=fe(e,n))||r.enumerable});return t};var Te=(t,e,o)=>(o=t!=null?ue(Ce(t)):{},q(e||!t||!t.__esModule?x(o,"default",{value:t,enumerable:!0}):o,t)),Pe=t=>q(x({},"__esModule",{value:!0}),t);var Se={};Ee(Se,{Copilot:()=>b,DiffEditor:()=>g.DiffEditor,default:()=>de,loader:()=>g.loader,useMonaco:()=>g.useMonaco});module.exports=Pe(Se);var F=(t,e=1e3)=>{let o=null;return(...n)=>(o&&clearTimeout(o),new Promise((i,s)=>{o=setTimeout(()=>{t(...n).then(i).catch(s)},e)}))},w=(t,e)=>{let o={...e};for(let r in t)typeof t[r]=="object"&&!Array.isArray(t[r])?e[r]&&typeof e[r]=="object"&&!Array.isArray(e[r])?o[r]=w(t[r],e[r]):o[r]={...t[r]}:o[r]=t[r];return o},U=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`,A=t=>t.split("").reverse().join("");var y=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],j=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),f=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),G=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}),S=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var W=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()},xe=(t,e)=>W(t,"GET",e),ye=(t,e,o)=>W(t,"POST",{...o,body:e}),M={GET:xe,POST:ye};var R=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 s=e.slice(-i),l=o.slice(0,i);if(s===l)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=25,E=[],H=(t,e)=>E.filter(o=>{let r=e.getValueInRange(o.range);return f(t,e)===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=>{E.length>=Me&&E.shift(),E.push(t)},V=()=>{E.length=0};var Y={scrollBeyondLastColumn:0,codeLens:!1,minimap:{enabled:!1},quickSuggestions:!1,folding:!1,foldingHighlight:!1,foldingImportsByDefault:!1,links:!1,fontSize:14,wordWrap:"on",automaticLayout:!0,formatOnPaste:!0,inlineSuggest:{enabled:!0,mode:void 0}};var z={llama:"llama3-70b-8192"},O="llama",J="https://api.groq.com/openai/v1/chat/completions";var Q=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var X=(t,e)=>{let o=y(t,e);return!!o&&!Q.has(o)},Z=(t,e)=>{let o=j(t,e).trim(),r=f(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var ee=(t,e,o,r)=>{let n=(t.match(/\n/g)||[]).length,i=G(t),s=y(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+n,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&n===0?o.column+i:i:o.column}},te=(t,e,o)=>new R(t,e).format(o),u=t=>({items:t,enableForwardStability:!0});var L=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!X(this.cursorPosition,this.model)&&!Z(this.cursorPosition,this.model)}};var I=class{static getModel(){return this.model}static setModel(e){this.model=e}};I.model=O;var $=I;var B=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 B(t);var T="<<CURSOR>>",oe=t=>t==="javascript"?"latest JavaScript":t,re=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"}},ne=t=>{let e=oe(t.language),o=re(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},Re=(t,e)=>{if(!t?.length&&!e)return"";let o=U(t),r=oe(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},ie=t=>{let{filename:e,language:o,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:s,externalContext:l}=t,c=re(n.completionMode),p=e?`the file named ${e}`:"a larger project",m=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${T}'. Your task is to assist with ${c}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":m+=`generate a completion to fill the middle of the code around '${T}'. Ensure the completion replaces '${T}' 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":m+=`provide the necessary completion for '${T}' 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}m+=` Output only the necessary completion code, without additional explanations or content.${Re(r,o)}`;let C=`${i}${T}${s}
|
|
5
4
|
|
|
6
|
-
`;return
|
|
7
|
-
${
|
|
5
|
+
`;return l&&l.length>0&&(C+=l.map(d=>`// Path: ${d.path}
|
|
6
|
+
${d.content}
|
|
8
7
|
`).join(`
|
|
9
|
-
`)),
|
|
8
|
+
`)),m+=`
|
|
10
9
|
|
|
11
10
|
<code>
|
|
12
|
-
${
|
|
13
|
-
</code>`,
|
|
11
|
+
${C}
|
|
12
|
+
</code>`,m.endsWith(".")?m:`${m}.`};var Oe="application/json",se=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:n,model:i,position:s,token:l})=>{let c=new AbortController;if(l.isCancellationRequested)return c.abort(),null;try{let{completion:p}=await M.POST(e,{completionMetadata:Le({filename:t,position:s,model:i,language:o,technologies:r,externalContext:n})},{headers:{"Content-Type":Oe},error:"Error while fetching completion item",signal:c.signal});return p||null}catch(p){return a(p).completionError("Error while fetching completion item"),null}},Le=({filename:t,position:e,model:o,language:r,technologies:n,externalContext:i})=>{let s=Ie(e,o),l=h(e,o),c=S(e,o);return{filename:t,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},Ie=(t,e)=>{let o=h(t,e),r=S(t,e);return o&&r?"fill-in-the-middle":"completion"};var b=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,$.setModel(o?.model||O)}async complete({completionMetadata:e}){try{let o=$.getModel(),r=this.createRequestBody(e,o),n=this.createHeaders(),i=await M.POST(J,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:z[o],messages:[{role:"system",content:ne(e)},{role:"user",content:ie(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var le={conso:"le.log($1)","new P":`romise((resolve, reject) => {
|
|
14
13
|
$1
|
|
15
14
|
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
16
15
|
$1
|
|
@@ -26,4 +25,4 @@ ${h}
|
|
|
26
25
|
$1
|
|
27
26
|
}`,"=>":` {
|
|
28
27
|
$1
|
|
29
|
-
}`,"new M":"ap","new W":"eakMap"};var
|
|
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});
|
package/build/index.mjs
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)[e.
|
|
3
|
-
|
|
4
|
-
`)[t-1];var z=async(e,t,o={})=>{let r={"Content-Type":"application/json",...o.headers},n=t==="POST"&&o.body?JSON.stringify(o.body):void 0,i=await fetch(e,{method:t,headers:r,body:n,signal:o.signal});if(!i.ok)throw new Error(`${o.error||"Network error"}: ${i.statusText}`);return i.json()},Ce=(e,t)=>z(e,"GET",t),fe=(e,t,o)=>z(e,"POST",{...o,body:t}),R={GET:Ce,POST:fe};var f="<<CURSOR>>",J=e=>e==="javascript"?"latest JavaScript":e,X=e=>{switch(e){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code";default:return"completing the code"}},Q=e=>{let t=J(e.language),o=X(e.editorState.completionMode);return`You are an expert ${t||""} code completion assistant known for exceptional skill in ${o}.`},Ee=(e,t)=>{if(!e?.length&&!t)return"";let o=V(e),r=J(t);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},Z=e=>{let{filename:t,language:o,technologies:r,editorState:n,codeBeforeCursor:i,codeAfterCursor:s,externalContext:c}=e,l=X(n.completionMode),p=t?`the file named ${t}`:"a larger project",a=`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 ${l}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":a+=`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":a+=`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}a+=` Output only the necessary completion code, without additional explanations or content.${Ee(r,o)}`;let h=`${i}${f}${s}
|
|
1
|
+
var _=(t,e=1e3)=>{let o=null;return(...n)=>(o&&clearTimeout(o),new Promise((i,s)=>{o=setTimeout(()=>{t(...n).then(i).catch(s)},e)}))},b=(t,e)=>{let o={...e};for(let r in t)typeof t[r]=="object"&&!Array.isArray(t[r])?e[r]&&typeof e[r]=="object"&&!Array.isArray(e[r])?o[r]=b(t[r],e[r]):o[r]={...t[r]}:o[r]=t[r];return o},k=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`,v=t=>t.split("").reverse().join("");var T=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],q=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),f=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),F=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var C=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),N=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var U=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()},ae=(t,e)=>U(t,"GET",e),me=(t,e,o)=>U(t,"POST",{...o,body:e}),P={GET:ae,POST:me};var x=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=C(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 s=e.slice(-i),l=o.slice(0,i);if(s===l)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 ce=25,h=[],j=(t,e)=>h.filter(o=>{let r=e.getValueInRange(o.range);return f(t,e)===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}),G=t=>{h.length>=ce&&h.shift(),h.push(t)},W=()=>{h.length=0};var H={scrollBeyondLastColumn:0,codeLens:!1,minimap:{enabled:!1},quickSuggestions:!1,folding:!1,foldingHighlight:!1,foldingImportsByDefault:!1,links:!1,fontSize:14,wordWrap:"on",automaticLayout:!0,formatOnPaste:!0,inlineSuggest:{enabled:!0,mode:void 0}};var K={llama:"llama3-70b-8192"},y="llama",V="https://api.groq.com/openai/v1/chat/completions";var Y=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var z=(t,e)=>{let o=T(t,e);return!!o&&!Y.has(o)},J=(t,e)=>{let o=q(t,e).trim(),r=f(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var Q=(t,e,o,r)=>{let n=(t.match(/\n/g)||[]).length,i=F(t),s=T(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+n,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&n===0?o.column+i:i:o.column}},X=(t,e,o)=>new x(t,e).format(o),u=t=>({items:t,enableForwardStability:!0});var M=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!z(this.cursorPosition,this.model)&&!J(this.cursorPosition,this.model)}};var R=class{static getModel(){return this.model}static setModel(e){this.model=e}};R.model=y;var w=R;var A=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 A(t);var E="<<CURSOR>>",Z=t=>t==="javascript"?"latest JavaScript":t,ee=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"}},te=t=>{let e=Z(t.language),o=ee(t.editorState.completionMode);return`You are an expert ${e||""} code completion assistant known for exceptional skill in ${o}.`},pe=(t,e)=>{if(!t?.length&&!e)return"";let o=k(t),r=Z(e);return`The code is written${r?` in ${r}`:""}${o?` using ${o}`:""}.`},oe=t=>{let{filename:e,language:o,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:s,externalContext:l}=t,c=ee(n.completionMode),p=e?`the file named ${e}`:"a larger project",m=`You will be presented with a code snippet in '<code>' tag where the cursor location is marked with '${E}'. Your task is to assist with ${c}. This code is part of ${p}. Please `;switch(n.completionMode){case"fill-in-the-middle":m+=`generate a completion to fill the middle of the code around '${E}'. Ensure the completion replaces '${E}' 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":m+=`provide the necessary completion for '${E}' 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}m+=` Output only the necessary completion code, without additional explanations or content.${pe(r,o)}`;let g=`${i}${E}${s}
|
|
5
4
|
|
|
6
|
-
`;return
|
|
7
|
-
${
|
|
5
|
+
`;return l&&l.length>0&&(g+=l.map(d=>`// Path: ${d.path}
|
|
6
|
+
${d.content}
|
|
8
7
|
`).join(`
|
|
9
|
-
`)),
|
|
8
|
+
`)),m+=`
|
|
10
9
|
|
|
11
10
|
<code>
|
|
12
|
-
${
|
|
13
|
-
</code>`,
|
|
11
|
+
${g}
|
|
12
|
+
</code>`,m.endsWith(".")?m:`${m}.`};var de="application/json",re=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:n,model:i,position:s,token:l})=>{let c=new AbortController;if(l.isCancellationRequested)return c.abort(),null;try{let{completion:p}=await P.POST(e,{completionMetadata:ue({filename:t,position:s,model:i,language:o,technologies:r,externalContext:n})},{headers:{"Content-Type":de},error:"Error while fetching completion item",signal:c.signal});return p||null}catch(p){return a(p).completionError("Error while fetching completion item"),null}},ue=({filename:t,position:e,model:o,language:r,technologies:n,externalContext:i})=>{let s=fe(e,o),l=C(e,o),c=N(e,o);return{filename:t,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},fe=(t,e)=>{let o=C(t,e),r=N(t,e);return o&&r?"fill-in-the-middle":"completion"};var S=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,w.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=w.getModel(),r=this.createRequestBody(e,o),n=this.createHeaders(),i=await P.POST(V,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:K[o],messages:[{role:"system",content:te(e)},{role:"user",content:oe(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var ne={conso:"le.log($1)","new P":`romise((resolve, reject) => {
|
|
14
13
|
$1
|
|
15
14
|
})`,"new A":"rray","new S":"et","throw n":"ew Error($1)",setTimeout:`(() => {
|
|
16
15
|
$1
|
|
@@ -26,4 +25,4 @@ ${h}
|
|
|
26
25
|
$1
|
|
27
26
|
}`,"=>":` {
|
|
28
27
|
$1
|
|
29
|
-
}`,"new M":"ap","new W":"eakMap"};var
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monacopilot",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Extended Monaco Editor with AI auto-completion and new themes for React.",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"module": "./build/index.mjs",
|
|
@@ -45,6 +45,10 @@
|
|
|
45
45
|
"ai-editor"
|
|
46
46
|
],
|
|
47
47
|
"homepage": "https://monacopilot.vercel.app",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "https://github.com/arshad-yaseen/monacopilot"
|
|
51
|
+
},
|
|
48
52
|
"license": "MIT",
|
|
49
53
|
"author": "Arshad Yaseen <m@arshadyaseen.com>",
|
|
50
54
|
"peerDependencies": {
|