monacopilot 0.9.8 → 0.9.10
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 -3
- package/build/index.d.ts +4 -3
- package/build/index.js +14 -21
- package/build/index.mjs +14 -21
- 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';
|
|
@@ -90,6 +90,7 @@ interface CompletionMetadata {
|
|
|
90
90
|
*/
|
|
91
91
|
declare class Copilot {
|
|
92
92
|
private readonly apiKey;
|
|
93
|
+
private readonly model;
|
|
93
94
|
/**
|
|
94
95
|
* Initializes the Copilot with an API key and optional configuration.
|
|
95
96
|
* @param {string} apiKey - The Groq API key.
|
|
@@ -112,7 +113,7 @@ declare class Copilot {
|
|
|
112
113
|
* @param monaco The Monaco instance.
|
|
113
114
|
* @param editor The editor instance.
|
|
114
115
|
* @param options The options for the Copilot.
|
|
115
|
-
* @returns CopilotRegistration object with
|
|
116
|
+
* @returns CopilotRegistration object with a deregister method.
|
|
116
117
|
*/
|
|
117
118
|
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
118
119
|
|
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';
|
|
@@ -90,6 +90,7 @@ interface CompletionMetadata {
|
|
|
90
90
|
*/
|
|
91
91
|
declare class Copilot {
|
|
92
92
|
private readonly apiKey;
|
|
93
|
+
private readonly model;
|
|
93
94
|
/**
|
|
94
95
|
* Initializes the Copilot with an API key and optional configuration.
|
|
95
96
|
* @param {string} apiKey - The Groq API key.
|
|
@@ -112,7 +113,7 @@ declare class Copilot {
|
|
|
112
113
|
* @param monaco The Monaco instance.
|
|
113
114
|
* @param editor The editor instance.
|
|
114
115
|
* @param options The options for the Copilot.
|
|
115
|
-
* @returns CopilotRegistration object with
|
|
116
|
+
* @returns CopilotRegistration object with a deregister method.
|
|
116
117
|
*/
|
|
117
118
|
declare const registerCopilot: (monaco: Monaco, editor: StandaloneCodeEditor, options: RegisterCopilotOptions) => CopilotRegistration;
|
|
118
119
|
|
package/build/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"use strict";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
|
+
"use strict";var L=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var ae=(o,e)=>{for(var t in e)L(o,t,{get:e[t],enumerable:!0})},le=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of ie(e))!se.call(o,n)&&n!==t&&L(o,n,{get:()=>e[n],enumerable:!(r=ne(e,n))||r.enumerable});return o};var me=o=>le(L({},"__esModule",{value:!0}),o);var xe={};ae(xe,{Copilot:()=>O,registerCopilot:()=>re});module.exports=me(xe);var w=()=>{},S=(o,e=1e3)=>{let t=null;return(...n)=>(t&&clearTimeout(t),new Promise((i,s)=>{t=setTimeout(()=>{o(...n).then(i).catch(s)},e)}))},_=o=>!o||o.length===0?"":o.length===1?o[0]:`${o.slice(0,-1).join(", ")} and ${o.slice(-1)}`;var x=(o,e)=>e.getLineContent(o.lineNumber)[o.column-1],B=(o,e)=>e.getLineContent(o.lineNumber).slice(o.column-1),d=(o,e)=>e.getLineContent(o.lineNumber).slice(0,o.column-1),$=o=>{let e=o.split(`
|
|
2
|
+
`);return e[e.length-1].length};var g=(o,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:o.lineNumber,endColumn:o.column}),b=(o,e)=>e.getValueInRange({startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var D=async(o,e,t={})=>{let r={"Content-Type":"application/json",...t.headers},n=e==="POST"&&t.body?JSON.stringify(t.body):void 0,i=await fetch(o,{method:e,headers:r,body:n,signal:t.signal});if(!i.ok)throw new Error(`${t.error||"Network error"}: ${i.statusText}`);return i.json()},pe=(o,e)=>D(o,"GET",e),ce=(o,e,t)=>D(o,"POST",{...t,body:e}),y={GET:pe,POST:ce};var P=class{constructor(e,t){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=t,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(),t=this.normalise(this.formattedCompletion),r=0,n=Math.min(t.length,e.length);for(let i=1;i<=n;i++){let s=e.slice(-i),a=t.slice(0,i);if(s===a)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 t=this.model.getLineContent(e);if(this.normalise(t)===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 de=10,f=[],q=(o,e)=>f.filter(t=>{let r=e.getValueInRange(t.range);return d(o,e).startsWith(t.textBeforeCursorInLine)&&(t.range.startLineNumber===o.lineNumber&&o.column===t.range.startColumn||t.completion.startsWith(r)&&t.range.startLineNumber===o.lineNumber&&o.column>=t.range.startColumn-r.length&&o.column<=t.range.endColumn)}),k=o=>{f.length>=de&&f.shift(),f.push(o)},F=()=>{f.length=0};var G={llama:"llama3-70b-8192"},H="llama",U="https://api.groq.com/openai/v1/chat/completions",j={temperature:.3};var W=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var Y=(o,e)=>{let t=x(o,e);return!!t&&!W.has(t)},z=(o,e)=>{let t=B(o,e).trim(),r=d(o,e).trim();return o.column<=3&&(t!==""||r!=="")};var K=(o,e,t,r)=>{let n=(o.match(/\n/g)||[]).length,i=$(o),s=x(t,r);return{startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:t.lineNumber+n,endColumn:o.includes(s)?t.lineNumber===e.startLineNumber&&n===0?t.column+i:i:t.column}},V=(o,e,t)=>new P(o,e).format(t),u=o=>({items:o,enableForwardStability:!0});var I=class{constructor(e,t){this.cursorPosition=e,this.model=t}shouldProvideCompletions(){return!Y(this.cursorPosition,this.model)&&!z(this.cursorPosition,this.model)}};var h=class h{constructor(){}static getInstance(){return h.instance}log(e,...t){console.log(`[LOG] ${e}`,...t)}info(e,...t){console.info(`[INFO] ${e}`,...t)}warn(e,...t){console.warn(`[WARN] ${e}`,...t)}error(e,...t){console.error(`[ERROR] ${e}`,...t)}debug(e,...t){console.debug(`[DEBUG] ${e}`,...t)}};h.instance=new h;var v=h,J=()=>v.getInstance();var E=class E{constructor(){this.logger=J()}static getInstance(){return E.instance}handleError(e,t){let r=this.getErrorDetails(e);this.logger.error(`${t}: ${r.message}`,r)}getErrorDetails(e){return e instanceof Error?{message:e.message,name:e.name,stack:e.stack}:{message:String(e),name:"UnknownError"}}};E.instance=new E;var N=E;var m=(o,e)=>{N.getInstance().handleError(o,e)};var T="<<CURSOR>>",X=o=>o==="javascript"?"latest JavaScript":o,Q=o=>{switch(o){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code"}},Z=o=>{let e=X(o.language),t=Q(o.editorState.completionMode),r=e?` ${e}`:"";return`You are an advanced AI coding assistant with expertise in ${t} for${r} programming. Your goal is to provide accurate, efficient, and context-aware code completions.
|
|
4
4
|
|
|
5
5
|
### Instructions:
|
|
6
6
|
- Provide only the requested code completion without any additional explanations or markdown.
|
|
7
7
|
- Ensure your suggestions seamlessly integrate with the existing code structure and style.
|
|
8
8
|
- Maintain consistent indentation and formatting with the surrounding code.
|
|
9
|
-
- Start your completion exactly at the cursor position (${
|
|
9
|
+
- Start your completion exactly at the cursor position (${T}).
|
|
10
10
|
- Do not repeat any existing code before or after the cursor.
|
|
11
11
|
- Respect the project's coding conventions, naming styles, and design patterns.
|
|
12
12
|
- Consider the broader context of the file and project when making suggestions.
|
|
@@ -16,14 +16,7 @@
|
|
|
16
16
|
- If multiple valid completions exist, choose the most likely and idiomatic one.
|
|
17
17
|
- If you cannot provide a meaningful completion, return an empty string.
|
|
18
18
|
|
|
19
|
-
|
|
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,p=J(i.completionMode),f=e?`the file named "${e}"`:"a larger project",a=`You are tasked with ${p} for a code snippet. The code is part of ${f}.
|
|
19
|
+
Remember, your role is to act as an extension of the developer's thought process, providing intelligent and contextually appropriate code completions.`},ue=(o,e)=>{if(!o?.length&&!e)return"";let t=o?` using ${_(o)}`:"",r=X(e);return`The code is written${r?` in ${r}`:""}${t}.`},ee=o=>{let{filename:e,language:t,technologies:r,editorState:n,textBeforeCursor:i,textAfterCursor:s,externalContext:a}=o,l=Q(n.completionMode),p=e?`the file named "${e}"`:"a larger project",c=`You are tasked with ${l} for a code snippet. The code is part of ${p}.
|
|
27
20
|
|
|
28
21
|
Your objective is to:
|
|
29
22
|
1. Analyze the provided code snippet and its context.
|
|
@@ -32,15 +25,15 @@
|
|
|
32
25
|
4. Adhere to the project's coding style and conventions.
|
|
33
26
|
5. Consider the broader context of the file and any provided external context.
|
|
34
27
|
|
|
35
|
-
${
|
|
28
|
+
${ue(r,t)}
|
|
36
29
|
|
|
37
30
|
Specific instructions:
|
|
38
|
-
- The cursor position is marked with '${
|
|
31
|
+
- The cursor position is marked with '${T}'.
|
|
39
32
|
- Your completion must start exactly at the cursor position.
|
|
40
33
|
- Do not repeat any code that appears before or after the cursor.
|
|
41
34
|
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
42
|
-
- If filling in the middle, replace '${
|
|
43
|
-
- If completing the code, start from '${
|
|
35
|
+
- If filling in the middle, replace '${T}' entirely with your completion.
|
|
36
|
+
- If completing the code, start from '${T}' and provide a logical continuation.
|
|
44
37
|
- Consider any imports, function definitions, or class structures in the surrounding code.
|
|
45
38
|
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
46
39
|
- Optimize for readability and performance where possible.
|
|
@@ -50,12 +43,12 @@
|
|
|
50
43
|
Here's the code snippet for completion:
|
|
51
44
|
|
|
52
45
|
<code>
|
|
53
|
-
${
|
|
54
|
-
</code>`;return
|
|
46
|
+
${i}${T}${s}
|
|
47
|
+
</code>`;return a&&a.length>0&&(c+=`
|
|
55
48
|
|
|
56
49
|
Additional context from related files:
|
|
57
50
|
|
|
58
|
-
`,a
|
|
59
|
-
${
|
|
51
|
+
`,c+=a.map(C=>`// Path: ${C.path}
|
|
52
|
+
${C.content}
|
|
60
53
|
`).join(`
|
|
61
|
-
`)),
|
|
54
|
+
`)),c.endsWith(".")?c:`${c}.`};var Ce="application/json",te=async({filename:o,endpoint:e,language:t,technologies:r,externalContext:n,model:i,position:s})=>{try{let{completion:a}=await y.POST(e,{completionMetadata:ge({filename:o,position:s,model:i,language:t,technologies:r,externalContext:n})},{headers:{"Content-Type":Ce},error:"Error while fetching completion item"});return a||null}catch(a){return m(a,"FETCH_COMPLETION_ITEM_ERROR"),null}},ge=({filename:o,position:e,model:t,language:r,technologies:n,externalContext:i})=>{let s=fe(e,t),a=g(e,t),l=b(e,t);return{filename:o,language:r,technologies:n,externalContext:i,textBeforeCursor:a,textAfterCursor:l,editorState:{completionMode:s}}},fe=(o,e)=>{let t=g(o,e),r=b(o,e);return t&&r?"fill-in-the-middle":"completion"};var O=class{constructor(e,t){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,this.model=t?.model||H}async complete({completionMetadata:e}){try{let t=this.createRequestBody(e),r=this.createHeaders(),n=await y.POST(U,t,{headers:r});if(!n.choices?.length)throw new Error("No completion choices received from API");return{completion:n.choices[0].message.content}}catch(t){return m(t,"COPILOT_COMPLETION_FETCH_ERROR"),{error:"Failed to generate completion"}}}createRequestBody(e){return{...j,model:G[this.model],messages:[{role:"system",content:Z(e)},{role:"user",content:ee(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var he=300,Ee=S(te,he),Te=async({monaco:o,model:e,position:t,token:r,isCompletionAccepted:n,onShowCompletion:i,options:s})=>{if(!new I(t,e).shouldProvideCompletions())return u([]);let a=q(t,e).map(l=>({insertText:l.completion,range:l.range}));if(a.length)return i(),u(a);if(r.isCancellationRequested||n)return u([]);try{let l=await Ee({...s,text:e.getValue(),model:e,position:t});if(l){let p=V(e,t,l),c=new o.Range(t.lineNumber,t.column,t.lineNumber,t.column),C=K(p,c,t,e);return k({completion:p,range:C,textBeforeCursorInLine:d(t,e)}),i(),u([{insertText:p,range:C}])}}catch(l){m(l,"FETCH_COMPLETION_ITEM_ERROR")}return u([])},oe=Te;var R=!1,M=!1,re=(o,e,t)=>{let r=[];try{let n=o.languages.registerInlineCompletionsProvider(t.language,{provideInlineCompletions:async(s,a,l,p)=>oe({monaco:o,model:s,position:a,token:p,isCompletionAccepted:R,onShowCompletion:()=>M=!0,options:t}),freeInlineCompletions:w});r.push(n);let i=e.onKeyDown(s=>{let a=s.keyCode===o.KeyCode.Tab||s.keyCode===o.KeyCode.RightArrow&&s.metaKey;M&&a?(R=!0,M=!1):R=!1});return r.push(i),{deregister:()=>{r.forEach(s=>s.dispose()),F(),R=!1,M=!1}}}catch(n){return m(n,"REGISTER_COPILOT_ERROR"),{deregister:()=>r.forEach(i=>i.dispose())}}};0&&(module.exports={Copilot,registerCopilot});
|
package/build/index.mjs
CHANGED
|
@@ -1,12 +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=()=>{},w=(o,e=1e3)=>{let t=null;return(...i)=>(t&&clearTimeout(t),new Promise((n,s)=>{t=setTimeout(()=>{o(...i).then(n).catch(s)},e)}))},S=o=>!o||o.length===0?"":o.length===1?o[0]:`${o.slice(0,-1).join(", ")} and ${o.slice(-1)}`;var x=(o,e)=>e.getLineContent(o.lineNumber)[o.column-1],_=(o,e)=>e.getLineContent(o.lineNumber).slice(o.column-1),d=(o,e)=>e.getLineContent(o.lineNumber).slice(0,o.column-1),B=o=>{let e=o.split(`
|
|
2
|
+
`);return e[e.length-1].length};var g=(o,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:o.lineNumber,endColumn:o.column}),M=(o,e)=>e.getValueInRange({startLineNumber:o.lineNumber,startColumn:o.column,endLineNumber:e.getLineCount(),endColumn:e.getLineMaxColumn(e.getLineCount())});var $=async(o,e,t={})=>{let r={"Content-Type":"application/json",...t.headers},i=e==="POST"&&t.body?JSON.stringify(t.body):void 0,n=await fetch(o,{method:e,headers:r,body:i,signal:t.signal});if(!n.ok)throw new Error(`${t.error||"Network error"}: ${n.statusText}`);return n.json()},oe=(o,e)=>$(o,"GET",e),re=(o,e,t)=>$(o,"POST",{...t,body:e}),y={GET:oe,POST:re};var P=class{constructor(e,t){this.formattedCompletion="";this.originalCompletion="";this.model=e,this.cursorPosition=t,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(),t=this.normalise(this.formattedCompletion),r=0,i=Math.min(t.length,e.length);for(let n=1;n<=i;n++){let s=e.slice(-n),a=t.slice(0,n);if(s===a)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 t=this.model.getLineContent(e);if(this.normalise(t)===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 ne=10,f=[],D=(o,e)=>f.filter(t=>{let r=e.getValueInRange(t.range);return d(o,e).startsWith(t.textBeforeCursorInLine)&&(t.range.startLineNumber===o.lineNumber&&o.column===t.range.startColumn||t.completion.startsWith(r)&&t.range.startLineNumber===o.lineNumber&&o.column>=t.range.startColumn-r.length&&o.column<=t.range.endColumn)}),q=o=>{f.length>=ne&&f.shift(),f.push(o)},k=()=>{f.length=0};var F={llama:"llama3-70b-8192"},G="llama",H="https://api.groq.com/openai/v1/chat/completions",U={temperature:.3};var j=new Set(['"',"'","`","{","}","[","]","(",")",","," ",":","."]);var W=(o,e)=>{let t=x(o,e);return!!t&&!j.has(t)},Y=(o,e)=>{let t=_(o,e).trim(),r=d(o,e).trim();return o.column<=3&&(t!==""||r!=="")};var z=(o,e,t,r)=>{let i=(o.match(/\n/g)||[]).length,n=B(o),s=x(t,r);return{startLineNumber:t.lineNumber,startColumn:t.column,endLineNumber:t.lineNumber+i,endColumn:o.includes(s)?t.lineNumber===e.startLineNumber&&i===0?t.column+n:n:t.column}},K=(o,e,t)=>new P(o,e).format(t),u=o=>({items:o,enableForwardStability:!0});var I=class{constructor(e,t){this.cursorPosition=e,this.model=t}shouldProvideCompletions(){return!W(this.cursorPosition,this.model)&&!Y(this.cursorPosition,this.model)}};var h=class h{constructor(){}static getInstance(){return h.instance}log(e,...t){console.log(`[LOG] ${e}`,...t)}info(e,...t){console.info(`[INFO] ${e}`,...t)}warn(e,...t){console.warn(`[WARN] ${e}`,...t)}error(e,...t){console.error(`[ERROR] ${e}`,...t)}debug(e,...t){console.debug(`[DEBUG] ${e}`,...t)}};h.instance=new h;var L=h,V=()=>L.getInstance();var E=class E{constructor(){this.logger=V()}static getInstance(){return E.instance}handleError(e,t){let r=this.getErrorDetails(e);this.logger.error(`${t}: ${r.message}`,r)}getErrorDetails(e){return e instanceof Error?{message:e.message,name:e.name,stack:e.stack}:{message:String(e),name:"UnknownError"}}};E.instance=new E;var b=E;var m=(o,e)=>{b.getInstance().handleError(o,e)};var T="<<CURSOR>>",J=o=>o==="javascript"?"latest JavaScript":o,X=o=>{switch(o){case"fill-in-the-middle":return"filling in the middle of the code";case"completion":return"completing the code"}},Q=o=>{let e=J(o.language),t=X(o.editorState.completionMode),r=e?` ${e}`:"";return`You are an advanced AI coding assistant with expertise in ${t} for${r} programming. Your goal is to provide accurate, efficient, and context-aware code completions.
|
|
4
4
|
|
|
5
5
|
### Instructions:
|
|
6
6
|
- Provide only the requested code completion without any additional explanations or markdown.
|
|
7
7
|
- Ensure your suggestions seamlessly integrate with the existing code structure and style.
|
|
8
8
|
- Maintain consistent indentation and formatting with the surrounding code.
|
|
9
|
-
- Start your completion exactly at the cursor position (${
|
|
9
|
+
- Start your completion exactly at the cursor position (${T}).
|
|
10
10
|
- Do not repeat any existing code before or after the cursor.
|
|
11
11
|
- Respect the project's coding conventions, naming styles, and design patterns.
|
|
12
12
|
- Consider the broader context of the file and project when making suggestions.
|
|
@@ -16,14 +16,7 @@ var M=()=>{},w=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Prom
|
|
|
16
16
|
- If multiple valid completions exist, choose the most likely and idiomatic one.
|
|
17
17
|
- If you cannot provide a meaningful completion, return an empty string.
|
|
18
18
|
|
|
19
|
-
|
|
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,p=V(i.completionMode),f=e?`the file named "${e}"`:"a larger project",a=`You are tasked with ${p} for a code snippet. The code is part of ${f}.
|
|
19
|
+
Remember, your role is to act as an extension of the developer's thought process, providing intelligent and contextually appropriate code completions.`},ie=(o,e)=>{if(!o?.length&&!e)return"";let t=o?` using ${S(o)}`:"",r=J(e);return`The code is written${r?` in ${r}`:""}${t}.`},Z=o=>{let{filename:e,language:t,technologies:r,editorState:i,textBeforeCursor:n,textAfterCursor:s,externalContext:a}=o,l=X(i.completionMode),p=e?`the file named "${e}"`:"a larger project",c=`You are tasked with ${l} for a code snippet. The code is part of ${p}.
|
|
27
20
|
|
|
28
21
|
Your objective is to:
|
|
29
22
|
1. Analyze the provided code snippet and its context.
|
|
@@ -32,15 +25,15 @@ var M=()=>{},w=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Prom
|
|
|
32
25
|
4. Adhere to the project's coding style and conventions.
|
|
33
26
|
5. Consider the broader context of the file and any provided external context.
|
|
34
27
|
|
|
35
|
-
${
|
|
28
|
+
${ie(r,t)}
|
|
36
29
|
|
|
37
30
|
Specific instructions:
|
|
38
|
-
- The cursor position is marked with '${
|
|
31
|
+
- The cursor position is marked with '${T}'.
|
|
39
32
|
- Your completion must start exactly at the cursor position.
|
|
40
33
|
- Do not repeat any code that appears before or after the cursor.
|
|
41
34
|
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
42
|
-
- If filling in the middle, replace '${
|
|
43
|
-
- If completing the code, start from '${
|
|
35
|
+
- If filling in the middle, replace '${T}' entirely with your completion.
|
|
36
|
+
- If completing the code, start from '${T}' and provide a logical continuation.
|
|
44
37
|
- Consider any imports, function definitions, or class structures in the surrounding code.
|
|
45
38
|
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
46
39
|
- Optimize for readability and performance where possible.
|
|
@@ -50,12 +43,12 @@ var M=()=>{},w=(t,e=1e3)=>{let o=null;return(...i)=>(o&&clearTimeout(o),new Prom
|
|
|
50
43
|
Here's the code snippet for completion:
|
|
51
44
|
|
|
52
45
|
<code>
|
|
53
|
-
${n}${
|
|
54
|
-
</code>`;return
|
|
46
|
+
${n}${T}${s}
|
|
47
|
+
</code>`;return a&&a.length>0&&(c+=`
|
|
55
48
|
|
|
56
49
|
Additional context from related files:
|
|
57
50
|
|
|
58
|
-
`,a
|
|
59
|
-
${
|
|
51
|
+
`,c+=a.map(C=>`// Path: ${C.path}
|
|
52
|
+
${C.content}
|
|
60
53
|
`).join(`
|
|
61
|
-
`)),
|
|
54
|
+
`)),c.endsWith(".")?c:`${c}.`};var se="application/json",ee=async({filename:o,endpoint:e,language:t,technologies:r,externalContext:i,model:n,position:s})=>{try{let{completion:a}=await y.POST(e,{completionMetadata:ae({filename:o,position:s,model:n,language:t,technologies:r,externalContext:i})},{headers:{"Content-Type":se},error:"Error while fetching completion item"});return a||null}catch(a){return m(a,"FETCH_COMPLETION_ITEM_ERROR"),null}},ae=({filename:o,position:e,model:t,language:r,technologies:i,externalContext:n})=>{let s=le(e,t),a=g(e,t),l=M(e,t);return{filename:o,language:r,technologies:i,externalContext:n,textBeforeCursor:a,textAfterCursor:l,editorState:{completionMode:s}}},le=(o,e)=>{let t=g(o,e),r=M(o,e);return t&&r?"fill-in-the-middle":"completion"};var N=class{constructor(e,t){if(!e)throw new Error("Groq API key is required to initialize Copilot.");this.apiKey=e,this.model=t?.model||G}async complete({completionMetadata:e}){try{let t=this.createRequestBody(e),r=this.createHeaders(),i=await y.POST(H,t,{headers:r});if(!i.choices?.length)throw new Error("No completion choices received from API");return{completion:i.choices[0].message.content}}catch(t){return m(t,"COPILOT_COMPLETION_FETCH_ERROR"),{error:"Failed to generate completion"}}}createRequestBody(e){return{...U,model:F[this.model],messages:[{role:"system",content:Q(e)},{role:"user",content:Z(e)}]}}createHeaders(){return{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"}}};var me=300,pe=w(ee,me),ce=async({monaco:o,model:e,position:t,token:r,isCompletionAccepted:i,onShowCompletion:n,options:s})=>{if(!new I(t,e).shouldProvideCompletions())return u([]);let a=D(t,e).map(l=>({insertText:l.completion,range:l.range}));if(a.length)return n(),u(a);if(r.isCancellationRequested||i)return u([]);try{let l=await pe({...s,text:e.getValue(),model:e,position:t});if(l){let p=K(e,t,l),c=new o.Range(t.lineNumber,t.column,t.lineNumber,t.column),C=z(p,c,t,e);return q({completion:p,range:C,textBeforeCursorInLine:d(t,e)}),n(),u([{insertText:p,range:C}])}}catch(l){m(l,"FETCH_COMPLETION_ITEM_ERROR")}return u([])},te=ce;var O=!1,R=!1,de=(o,e,t)=>{let r=[];try{let i=o.languages.registerInlineCompletionsProvider(t.language,{provideInlineCompletions:async(s,a,l,p)=>te({monaco:o,model:s,position:a,token:p,isCompletionAccepted:O,onShowCompletion:()=>R=!0,options:t}),freeInlineCompletions:A});r.push(i);let n=e.onKeyDown(s=>{let a=s.keyCode===o.KeyCode.Tab||s.keyCode===o.KeyCode.RightArrow&&s.metaKey;R&&a?(O=!0,R=!1):O=!1});return r.push(n),{deregister:()=>{r.forEach(s=>s.dispose()),k(),O=!1,R=!1}}}catch(i){return m(i,"REGISTER_COPILOT_ERROR"),{deregister:()=>r.forEach(n=>n.dispose())}}};export{N as Copilot,de as registerCopilot};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "monacopilot",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.10",
|
|
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
|
}
|