monacopilot 0.9.7 → 0.9.9
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 +3 -3
- package/build/index.d.ts +3 -3
- package/build/index.js +60 -18
- package/build/index.mjs +60 -18
- package/package.json +8 -1
package/build/index.d.mts
CHANGED
|
@@ -58,10 +58,10 @@ interface RegisterCopilotOptions {
|
|
|
58
58
|
}
|
|
59
59
|
interface CopilotRegistration {
|
|
60
60
|
/**
|
|
61
|
-
*
|
|
61
|
+
* Deregisters the Copilot from the Monaco editor.
|
|
62
62
|
* This should be called when the Copilot is no longer needed.
|
|
63
63
|
*/
|
|
64
|
-
|
|
64
|
+
deregister: () => void;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
type CompletionModel = 'llama';
|
|
@@ -112,7 +112,7 @@ declare class Copilot {
|
|
|
112
112
|
* @param monaco The Monaco instance.
|
|
113
113
|
* @param editor The editor instance.
|
|
114
114
|
* @param options The options for the Copilot.
|
|
115
|
-
* @returns CopilotRegistration object with
|
|
115
|
+
* @returns CopilotRegistration object with a deregister method.
|
|
116
116
|
*/
|
|
117
117
|
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
118
118
|
|
package/build/index.d.ts
CHANGED
|
@@ -58,10 +58,10 @@ interface RegisterCopilotOptions {
|
|
|
58
58
|
}
|
|
59
59
|
interface CopilotRegistration {
|
|
60
60
|
/**
|
|
61
|
-
*
|
|
61
|
+
* Deregisters the Copilot from the Monaco editor.
|
|
62
62
|
* This should be called when the Copilot is no longer needed.
|
|
63
63
|
*/
|
|
64
|
-
|
|
64
|
+
deregister: () => void;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
type CompletionModel = 'llama';
|
|
@@ -112,7 +112,7 @@ declare class Copilot {
|
|
|
112
112
|
* @param monaco The Monaco instance.
|
|
113
113
|
* @param editor The editor instance.
|
|
114
114
|
* @param options The options for the Copilot.
|
|
115
|
-
* @returns CopilotRegistration object with
|
|
115
|
+
* @returns CopilotRegistration object with a deregister method.
|
|
116
116
|
*/
|
|
117
117
|
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
118
118
|
|
package/build/index.js
CHANGED
|
@@ -1,19 +1,61 @@
|
|
|
1
|
-
"use strict";var I=Object.defineProperty;var
|
|
2
|
-
`);return e[e.length-1].length};var g=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),L=(t,e)=>e.getValueInRange({startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var
|
|
3
|
-
`&&(this.formattedCompletion=this.formattedCompletion.trim()),this}normalise(e){return e?.trim()??""}removeDuplicatesFromStartOfCompletion(){let e=g(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 s=e.slice(-n),
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
"use strict";var I=Object.defineProperty;var oe=Object.getOwnPropertyDescriptor;var re=Object.getOwnPropertyNames;var ne=Object.prototype.hasOwnProperty;var ie=(t,e)=>{for(var o in e)I(t,o,{get:e[o],enumerable:!0})},se=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of re(e))!ne.call(t,i)&&i!==o&&I(t,i,{get:()=>e[i],enumerable:!(r=oe(e,i))||r.enumerable});return t};var le=t=>se(I({},"__esModule",{value:!0}),t);var Ee={};ie(Ee,{Copilot:()=>M,registerCopilot:()=>te});module.exports=le(Ee);var w=()=>{},S=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Promise((n,s)=>{o=setTimeout(()=>{t(...i).then(n).catch(s)},e)}))},_=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var E=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],B=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),C=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),D=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var g=(t,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:t.lineNumber,endColumn:t.column}),L=(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()},ae=(t,e)=>$(t,"GET",e),me=(t,e,o)=>$(t,"POST",{...o,body:e}),x={GET:ae,POST:me};var T=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=g(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 s=e.slice(-n),l=o.slice(0,n);if(s===l)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 pe=10,h=[],k=(t,e)=>h.filter(o=>{let r=e.getValueInRange(o.range);return C(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=>{h.length>=pe&&h.shift(),h.push(t)},F=()=>{h.length=0};var U={llama:"llama3-70b-8192"},y="llama",G="https://api.groq.com/openai/v1/chat/completions",j={temperature:.3};var H=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var W=(t,e)=>{let o=E(t,e);return!!o&&!H.has(o)},K=(t,e)=>{let o=B(t,e).trim(),r=C(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var Y=(t,e,o,r)=>{let i=(t.match(/\n/g)||[]).length,n=D(t),s=E(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+i,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&i===0?o.column+n:n:o.column}},z=(t,e,o)=>new T(t,e).format(o),p=t=>({items:t,enableForwardStability:!0});var P=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!W(this.cursorPosition,this.model)&&!K(this.cursorPosition,this.model)}};var R=class{static getModel(){return this.model}static setModel(e){this.model=e}};R.model=y;var v=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))}},m=t=>new A(t);var f="<<CURSOR>>",V=t=>t==="javascript"?"latest JavaScript":t,J=t=>{switch(t){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code"}},Q=t=>{let e=V(t.language),o=J(t.editorState.completionMode),r=e?` ${e}`:"";return`You are an advanced AI coding assistant with expertise in ${o} for${r} programming. Your goal is to provide accurate, efficient, and context-aware code completions.
|
|
4
|
+
|
|
5
|
+
### Instructions:
|
|
6
|
+
- Provide only the requested code completion without any additional explanations or markdown.
|
|
7
|
+
- Ensure your suggestions seamlessly integrate with the existing code structure and style.
|
|
8
|
+
- Maintain consistent indentation and formatting with the surrounding code.
|
|
9
|
+
- Start your completion exactly at the cursor position (${f}).
|
|
10
|
+
- Do not repeat any existing code before or after the cursor.
|
|
11
|
+
- Respect the project's coding conventions, naming styles, and design patterns.
|
|
12
|
+
- Consider the broader context of the file and project when making suggestions.
|
|
13
|
+
- Optimize for readability, maintainability, and performance where applicable.
|
|
14
|
+
- Handle potential errors and edge cases in your completions when appropriate.
|
|
15
|
+
- Utilize modern language features and best practices specific to${r}.
|
|
16
|
+
- If multiple valid completions exist, choose the most likely and idiomatic one.
|
|
17
|
+
- If you cannot provide a meaningful completion, return an empty string.
|
|
18
|
+
|
|
19
|
+
### Formatting Rules:
|
|
20
|
+
- Do not include any markdown or code block syntax.
|
|
21
|
+
- Insert newlines after opening brackets ({, [, () and before closing brackets (}, ], )).
|
|
22
|
+
- Add newlines after commas in multi-line structures.
|
|
23
|
+
- Do not suggest newlines after spaces or existing newlines.
|
|
24
|
+
- Match the indentation level of the current line for new lines.
|
|
25
|
+
|
|
26
|
+
Remember, your role is to act as an extension of the developer's thought process, providing intelligent and contextually appropriate code completions.`},ce=(t,e)=>{if(!t?.length&&!e)return"";let o=t?` using ${_(t)}`:"",r=V(e);return`The code is written${r?` in ${r}`:""}${o}.`},X=t=>{let{filename:e,language:o,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:s,externalContext:l}=t,c=J(i.completionMode),d=e?`the file named "${e}"`:"a larger project",a=`You are tasked with ${c} for a code snippet. The code is part of ${d}.
|
|
27
|
+
|
|
28
|
+
Your objective is to:
|
|
29
|
+
1. Analyze the provided code snippet and its context.
|
|
30
|
+
2. Generate a completion that seamlessly integrates with the existing code.
|
|
31
|
+
3. Ensure your completion is syntactically correct and semantically meaningful.
|
|
32
|
+
4. Adhere to the project's coding style and conventions.
|
|
33
|
+
5. Consider the broader context of the file and any provided external context.
|
|
34
|
+
|
|
35
|
+
${ce(r,o)}
|
|
16
36
|
|
|
17
|
-
|
|
18
|
-
${
|
|
19
|
-
|
|
37
|
+
Specific instructions:
|
|
38
|
+
- The cursor position is marked with '${f}'.
|
|
39
|
+
- Your completion must start exactly at the cursor position.
|
|
40
|
+
- Do not repeat any code that appears before or after the cursor.
|
|
41
|
+
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
42
|
+
- If filling in the middle, replace '${f}' entirely with your completion.
|
|
43
|
+
- If completing the code, start from '${f}' and provide a logical continuation.
|
|
44
|
+
- Consider any imports, function definitions, or class structures in the surrounding code.
|
|
45
|
+
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
46
|
+
- Optimize for readability and performance where possible.
|
|
47
|
+
|
|
48
|
+
Remember, output only the necessary completion code without any additional explanations or content.
|
|
49
|
+
|
|
50
|
+
Here's the code snippet for completion:
|
|
51
|
+
|
|
52
|
+
<code>
|
|
53
|
+
${n}${f}${s}
|
|
54
|
+
</code>`;return l&&l.length>0&&(a+=`
|
|
55
|
+
|
|
56
|
+
Additional context from related files:
|
|
57
|
+
|
|
58
|
+
`,a+=l.map(u=>`// Path: ${u.path}
|
|
59
|
+
${u.content}
|
|
60
|
+
`).join(`
|
|
61
|
+
`)),a.endsWith(".")?a:`${a}.`};var de="application/json",Z=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:i,model:n,position:s})=>{try{let{completion:l}=await x.POST(e,{completionMetadata:ue({filename:t,position:s,model:n,language:o,technologies:r,externalContext:i})},{headers:{"Content-Type":de},error:"Error while fetching completion item"});return l||null}catch(l){return m(l).completionError("Error while fetching completion item"),null}},ue=({filename:t,position:e,model:o,language:r,technologies:i,externalContext:n})=>{let s=Ce(e,o),l=g(e,o),c=L(e,o);return{filename:t,language:r,technologies:i,externalContext:n,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},Ce=(t,e)=>{let o=g(t,e),r=L(t,e);return o&&r?"fill-in-the-middle":"completion"};var M=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,v.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=v.getModel(),r=this.createRequestBody(e,o),i=this.createHeaders(),n=await x.POST(G,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?m(o).apiError("Failed to fetch completion"):m(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{...j,model:U[o],messages:[{role:"system",content:Q(e)},{role:"user",content:X(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var ge=300,he=S(Z,ge),fe=async({monaco:t,model:e,position:o,token:r,isCompletionAccepted:i,onShowCompletion:n,options:s})=>{let l=e.getValue(),c=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new P(o,e).shouldProvideCompletions())return p([]);let d=k(o,e).map(a=>({insertText:a.completion,range:a.range}));if(d.length)return n(),p(d);if(r.isCancellationRequested)return p([]);if(i)return p([]);try{let a=await he({...s,text:l,model:e,position:o});if(a){let u=z(e,o,a),N=Y(u,c,o,e);return q({completion:u,range:N,textBeforeCursorInLine:C(o,e)}),n(),p([{insertText:u,range:N}])}}catch(a){m(a).completionError("Failed to fetch completion item")}return p([])},ee=fe;var b=!1,O=!1,te=(t,e,o)=>{let r=[];try{let i=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(s,l,c,d)=>ee({monaco:t,model:s,position:l,token:d,isCompletionAccepted:b,onShowCompletion:()=>O=!0,options:o}),freeInlineCompletions:w});r.push(i);let n=e.onKeyDown(s=>{let l=s.keyCode===t.KeyCode.Tab||s.keyCode===t.KeyCode.RightArrow&&s.metaKey;O&&l?(b=!0,O=!1):b=!1});return r.push(n),{deregister:()=>{r.forEach(s=>s.dispose()),F(),b=!1,O=!1}}}catch(i){return m(i).editorError("Error while registering Copilot"),{deregister:()=>r.forEach(n=>n.dispose())}}};0&&(module.exports={Copilot,registerCopilot});
|
package/build/index.mjs
CHANGED
|
@@ -1,19 +1,61 @@
|
|
|
1
|
-
var
|
|
2
|
-
`);return e[e.length-1].length};var g=(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=g(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 s=e.slice(-n),
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
var N=()=>{},w=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Promise((n,s)=>{o=setTimeout(()=>{t(...i).then(n).catch(s)},e)}))},S=t=>!t||t.length===0?"":t.length===1?t[0]:`${t.slice(0,-1).join(", ")} and ${t.slice(-1)}`;var E=(t,e)=>e.getLineContent(t.lineNumber)[t.column-1],_=(t,e)=>e.getLineContent(t.lineNumber).slice(t.column-1),C=(t,e)=>e.getLineContent(t.lineNumber).slice(0,t.column-1),B=t=>{let e=t.split(`
|
|
2
|
+
`);return e[e.length-1].length};var g=(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 D=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()},ee=(t,e)=>D(t,"GET",e),te=(t,e,o)=>D(t,"POST",{...o,body:e}),x={GET:ee,POST:te};var T=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=g(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 s=e.slice(-n),l=o.slice(0,n);if(s===l)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 oe=10,h=[],$=(t,e)=>h.filter(o=>{let r=e.getValueInRange(o.range);return C(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=>{h.length>=oe&&h.shift(),h.push(t)},q=()=>{h.length=0};var F={llama:"llama3-70b-8192"},y="llama",U="https://api.groq.com/openai/v1/chat/completions",G={temperature:.3};var j=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var H=(t,e)=>{let o=E(t,e);return!!o&&!j.has(o)},W=(t,e)=>{let o=_(t,e).trim(),r=C(t,e).trim();return t.column<=3&&(o!==""||r!=="")};var K=(t,e,o,r)=>{let i=(t.match(/\n/g)||[]).length,n=B(t),s=E(o,r);return{startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:o.lineNumber+i,endColumn:t.includes(s)?o.lineNumber===e.startLineNumber&&i===0?o.column+n:n:o.column}},Y=(t,e,o)=>new T(t,e).format(o),p=t=>({items:t,enableForwardStability:!0});var P=class{constructor(e,o){this.cursorPosition=e,this.model=o}shouldProvideCompletions(){return!H(this.cursorPosition,this.model)&&!W(this.cursorPosition,this.model)}};var R=class{static getModel(){return this.model}static setModel(e){this.model=e}};R.model=y;var I=R;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))}},m=t=>new L(t);var f="<<CURSOR>>",z=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"}},J=t=>{let e=z(t.language),o=V(t.editorState.completionMode),r=e?` ${e}`:"";return`You are an advanced AI coding assistant with expertise in ${o} for${r} programming. Your goal is to provide accurate, efficient, and context-aware code completions.
|
|
4
|
+
|
|
5
|
+
### Instructions:
|
|
6
|
+
- Provide only the requested code completion without any additional explanations or markdown.
|
|
7
|
+
- Ensure your suggestions seamlessly integrate with the existing code structure and style.
|
|
8
|
+
- Maintain consistent indentation and formatting with the surrounding code.
|
|
9
|
+
- Start your completion exactly at the cursor position (${f}).
|
|
10
|
+
- Do not repeat any existing code before or after the cursor.
|
|
11
|
+
- Respect the project's coding conventions, naming styles, and design patterns.
|
|
12
|
+
- Consider the broader context of the file and project when making suggestions.
|
|
13
|
+
- Optimize for readability, maintainability, and performance where applicable.
|
|
14
|
+
- Handle potential errors and edge cases in your completions when appropriate.
|
|
15
|
+
- Utilize modern language features and best practices specific to${r}.
|
|
16
|
+
- If multiple valid completions exist, choose the most likely and idiomatic one.
|
|
17
|
+
- If you cannot provide a meaningful completion, return an empty string.
|
|
18
|
+
|
|
19
|
+
### Formatting Rules:
|
|
20
|
+
- Do not include any markdown or code block syntax.
|
|
21
|
+
- Insert newlines after opening brackets ({, [, () and before closing brackets (}, ], )).
|
|
22
|
+
- Add newlines after commas in multi-line structures.
|
|
23
|
+
- Do not suggest newlines after spaces or existing newlines.
|
|
24
|
+
- Match the indentation level of the current line for new lines.
|
|
25
|
+
|
|
26
|
+
Remember, your role is to act as an extension of the developer's thought process, providing intelligent and contextually appropriate code completions.`},re=(t,e)=>{if(!t?.length&&!e)return"";let o=t?` using ${S(t)}`:"",r=z(e);return`The code is written${r?` in ${r}`:""}${o}.`},Q=t=>{let{filename:e,language:o,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:s,externalContext:l}=t,c=V(i.completionMode),d=e?`the file named "${e}"`:"a larger project",a=`You are tasked with ${c} for a code snippet. The code is part of ${d}.
|
|
27
|
+
|
|
28
|
+
Your objective is to:
|
|
29
|
+
1. Analyze the provided code snippet and its context.
|
|
30
|
+
2. Generate a completion that seamlessly integrates with the existing code.
|
|
31
|
+
3. Ensure your completion is syntactically correct and semantically meaningful.
|
|
32
|
+
4. Adhere to the project's coding style and conventions.
|
|
33
|
+
5. Consider the broader context of the file and any provided external context.
|
|
34
|
+
|
|
35
|
+
${re(r,o)}
|
|
16
36
|
|
|
17
|
-
|
|
18
|
-
${
|
|
19
|
-
|
|
37
|
+
Specific instructions:
|
|
38
|
+
- The cursor position is marked with '${f}'.
|
|
39
|
+
- Your completion must start exactly at the cursor position.
|
|
40
|
+
- Do not repeat any code that appears before or after the cursor.
|
|
41
|
+
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
42
|
+
- If filling in the middle, replace '${f}' entirely with your completion.
|
|
43
|
+
- If completing the code, start from '${f}' and provide a logical continuation.
|
|
44
|
+
- Consider any imports, function definitions, or class structures in the surrounding code.
|
|
45
|
+
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
46
|
+
- Optimize for readability and performance where possible.
|
|
47
|
+
|
|
48
|
+
Remember, output only the necessary completion code without any additional explanations or content.
|
|
49
|
+
|
|
50
|
+
Here's the code snippet for completion:
|
|
51
|
+
|
|
52
|
+
<code>
|
|
53
|
+
${n}${f}${s}
|
|
54
|
+
</code>`;return l&&l.length>0&&(a+=`
|
|
55
|
+
|
|
56
|
+
Additional context from related files:
|
|
57
|
+
|
|
58
|
+
`,a+=l.map(u=>`// Path: ${u.path}
|
|
59
|
+
${u.content}
|
|
60
|
+
`).join(`
|
|
61
|
+
`)),a.endsWith(".")?a:`${a}.`};var ne="application/json",X=async({filename:t,endpoint:e,language:o,technologies:r,externalContext:i,model:n,position:s})=>{try{let{completion:l}=await x.POST(e,{completionMetadata:ie({filename:t,position:s,model:n,language:o,technologies:r,externalContext:i})},{headers:{"Content-Type":ne},error:"Error while fetching completion item"});return l||null}catch(l){return m(l).completionError("Error while fetching completion item"),null}},ie=({filename:t,position:e,model:o,language:r,technologies:i,externalContext:n})=>{let s=se(e,o),l=g(e,o),c=O(e,o);return{filename:t,language:r,technologies:i,externalContext:n,textBeforeCursor:l,textAfterCursor:c,editorState:{completionMode:s}}},se=(t,e)=>{let o=g(t,e),r=O(t,e);return o&&r?"fill-in-the-middle":"completion"};var v=class{constructor(e,o){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,I.setModel(o?.model||y)}async complete({completionMetadata:e}){try{let o=I.getModel(),r=this.createRequestBody(e,o),i=this.createHeaders(),n=await x.POST(U,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?m(o).apiError("Failed to fetch completion"):m(o).apiError("Unknown error while fetching completion"),{error:"Failed to generate completion"}}}createRequestBody(e,o){return{...G,model:F[o],messages:[{role:"system",content:J(e)},{role:"user",content:Q(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var le=300,ae=w(X,le),me=async({monaco:t,model:e,position:o,token:r,isCompletionAccepted:i,onShowCompletion:n,options:s})=>{let l=e.getValue(),c=new t.Range(o.lineNumber,o.column,o.lineNumber,o.column);if(!new P(o,e).shouldProvideCompletions())return p([]);let d=$(o,e).map(a=>({insertText:a.completion,range:a.range}));if(d.length)return n(),p(d);if(r.isCancellationRequested)return p([]);if(i)return p([]);try{let a=await ae({...s,text:l,model:e,position:o});if(a){let u=Y(e,o,a),A=K(u,c,o,e);return k({completion:u,range:A,textBeforeCursorInLine:C(o,e)}),n(),p([{insertText:u,range:A}])}}catch(a){m(a).completionError("Failed to fetch completion item")}return p([])},Z=me;var M=!1,b=!1,pe=(t,e,o)=>{let r=[];try{let i=t.languages.registerInlineCompletionsProvider(o.language,{provideInlineCompletions:async(s,l,c,d)=>Z({monaco:t,model:s,position:l,token:d,isCompletionAccepted:M,onShowCompletion:()=>b=!0,options:o}),freeInlineCompletions:N});r.push(i);let n=e.onKeyDown(s=>{let l=s.keyCode===t.KeyCode.Tab||s.keyCode===t.KeyCode.RightArrow&&s.metaKey;b&&l?(M=!0,b=!1):M=!1});return r.push(n),{deregister:()=>{r.forEach(s=>s.dispose()),q(),M=!1,b=!1}}}catch(i){return m(i).editorError("Error while registering Copilot"),{deregister:()=>r.forEach(n=>n.dispose())}}};export{v as Copilot,pe as registerCopilot};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monacopilot",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.9",
|
|
4
4
|
"description": "AI auto-completion for Monaco Editor",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"module": "./build/index.mjs",
|
|
@@ -49,6 +49,13 @@
|
|
|
49
49
|
"type": "git",
|
|
50
50
|
"url": "https://github.com/arshad-yaseen/monacopilot"
|
|
51
51
|
},
|
|
52
|
+
"maintainers": [
|
|
53
|
+
{
|
|
54
|
+
"name": "Arshad Yaseen",
|
|
55
|
+
"email": "m@arshadyaseen.com",
|
|
56
|
+
"url": "https://arshadyaseen.com"
|
|
57
|
+
}
|
|
58
|
+
],
|
|
52
59
|
"license": "MIT",
|
|
53
60
|
"author": "Arshad Yaseen <m@arshadyaseen.com>"
|
|
54
61
|
}
|