monacopilot 0.9.11 → 0.9.12
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/LICENSE +21 -21
- package/build/index.js +13 -38
- package/build/index.mjs +13 -38
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Arshad Yaseen
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Arshad Yaseen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/build/index.js
CHANGED
|
@@ -1,54 +1,29 @@
|
|
|
1
|
-
"use strict";var L=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var
|
|
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
|
|
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),
|
|
1
|
+
"use strict";var L=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var le=(o,e)=>{for(var t in e)L(o,t,{get:e[t],enumerable:!0})},ae=(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=>ae(L({},"__esModule",{value:!0}),o);var Pe={};le(Pe,{Copilot:()=>R,registerCopilot:()=>re});module.exports=me(Pe);var 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)}))},w=o=>!o||o.length===0?"":o.length===1?o[0]:`${o.slice(0,-1).join(", ")} and ${o.slice(-1)}`;var T=(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),D=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 $=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)=>$(o,"GET",e),ce=(o,e,t)=>$(o,"POST",{...t,body:e}),P={GET:pe,POST:ce};var x=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),l=t.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 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)}),F=o=>{f.length>=de&&f.shift(),f.push(o)},k=()=>{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 K=(o,e)=>{let t=T(o,e);return!!t&&!W.has(t)},V=(o,e)=>{let t=B(o,e).trim(),r=d(o,e).trim();return o.column<=3&&(t!==""||r!=="")};var Y=(o,e,t,r)=>{let n=(o.match(/\n/g)||[]).length,i=D(o),s=T(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}},z=(o,e,t)=>new x(o,e).format(t),u=o=>({items:o,enableForwardStability:!0});var y=class{constructor(e,t){this.cursorPosition=e,this.model=t}shouldProvideCompletions(){return!K(this.cursorPosition,this.model)&&!V(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 N=h,J=()=>N.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 v=E;var p=(o,e)=>{v.getInstance().handleError(o,e)};var O="<<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. 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 ${w(o)}`:"",r=X(e);return`The code is written${r?` in ${r}`:""}${t}.`},ee=o=>{let{filename:e,language:t,technologies:r,editorState:{completionMode:n},textBeforeCursor:i,textAfterCursor:s,externalContext:l}=o,a=Q(n),c=e?`the file named "${e}"`:"a larger project",m=`You are tasked with ${a} for a code snippet. The code is part of ${c}.
|
|
4
4
|
|
|
5
|
-
|
|
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 (${T}).
|
|
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.
|
|
5
|
+
`;return m+=ue(r,t),m+=`
|
|
18
6
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
Your objective is to:
|
|
22
|
-
1. Analyze the provided code snippet and its context.
|
|
23
|
-
2. Generate a completion that seamlessly integrates with the existing code.
|
|
24
|
-
3. Ensure your completion is syntactically correct and semantically meaningful.
|
|
25
|
-
4. Adhere to the project's coding style and conventions.
|
|
26
|
-
5. Consider the broader context of the file and any provided external context.
|
|
27
|
-
|
|
28
|
-
${ue(r,t)}
|
|
29
|
-
|
|
30
|
-
Specific instructions:
|
|
31
|
-
- The cursor position is marked with '${T}'.
|
|
7
|
+
Here are the details about how the completion should be generated:
|
|
8
|
+
- The cursor position is marked with '${O}'.
|
|
32
9
|
- Your completion must start exactly at the cursor position.
|
|
33
10
|
- Do not repeat any code that appears before or after the cursor.
|
|
34
11
|
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
35
|
-
- If filling in the middle, replace '${
|
|
36
|
-
- If completing the code, start from '${
|
|
37
|
-
-
|
|
38
|
-
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
39
|
-
- Optimize for readability and performance where possible.
|
|
12
|
+
`,n==="fill-in-the-middle"?m+=` - If filling in the middle, replace '${O}' entirely with your completion.
|
|
13
|
+
`:n==="completion"&&(m+=` - If completing the code, start from '${O}' and provide a logical continuation.
|
|
14
|
+
`),m+=` - Optimize for readability and performance where possible.
|
|
40
15
|
|
|
41
16
|
Remember, output only the necessary completion code without any additional explanations or content.
|
|
42
17
|
|
|
43
18
|
Here's the code snippet for completion:
|
|
44
19
|
|
|
45
20
|
<code>
|
|
46
|
-
${i}${
|
|
47
|
-
</code
|
|
21
|
+
${i}${O}${s}
|
|
22
|
+
</code>`,l&&l.length>0&&(m+=`
|
|
48
23
|
|
|
49
24
|
Additional context from related files:
|
|
50
25
|
|
|
51
|
-
`,
|
|
26
|
+
`,m+=l.map(C=>`// Path: ${C.path}
|
|
52
27
|
${C.content}
|
|
53
28
|
`).join(`
|
|
54
|
-
`)),
|
|
29
|
+
`)),m.endsWith(".")?m:`${m}.`};var Ce="application/json",te=async({filename:o,endpoint:e,language:t,technologies:r,externalContext:n,model:i,position:s})=>{try{let{completion:l}=await P.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 l||null}catch(l){return p(l,"FETCH_COMPLETION_ITEM_ERROR"),null}},ge=({filename:o,position:e,model:t,language:r,technologies:n,externalContext:i})=>{let s=fe(e,t),l=g(e,t),a=b(e,t);return{filename:o,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:a,editorState:{completionMode:s}}},fe=(o,e)=>{let t=g(o,e),r=b(o,e);return t&&r?"fill-in-the-middle":"completion"};var R=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 P.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 p(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=350,Ee=_(te,he),Te=async({monaco:o,model:e,position:t,token:r,isCompletionAccepted:n,onShowCompletion:i,options:s})=>{if(!new y(t,e).shouldProvideCompletions())return u([]);let l=q(t,e).map(a=>({insertText:a.completion,range:a.range}));if(l.length)return i(),u(l);if(r.isCancellationRequested||n)return u([]);try{let a=await Ee({...s,text:e.getValue(),model:e,position:t});if(a){let c=z(e,t,a),m=new o.Range(t.lineNumber,t.column,t.lineNumber,t.column),C=Y(c,m,t,e);return F({completion:c,range:C,textBeforeCursorInLine:d(t,e)}),i(),u([{insertText:c,range:C}])}}catch(a){p(a,"FETCH_COMPLETION_ITEM_ERROR")}return u([])},oe=Te;var M=!1,I=!1,re=(o,e,t)=>{let r=[];try{let n=o.languages.registerInlineCompletionsProvider(t.language,{provideInlineCompletions:(s,l,a,c)=>oe({monaco:o,model:s,position:l,token:c,isCompletionAccepted:M,onShowCompletion:()=>I=!0,options:t}),freeInlineCompletions:S});r.push(n);let i=e.onKeyDown(s=>{let l=s.keyCode===o.KeyCode.Tab||s.keyCode===o.KeyCode.RightArrow&&s.metaKey;I&&l?(M=!0,I=!1):M=!1});return r.push(i),{deregister:()=>{r.forEach(s=>s.dispose()),k(),M=!1,I=!1}}}catch(n){return p(n,"REGISTER_COPILOT_ERROR"),{deregister:()=>r.forEach(i=>i.dispose())}}};0&&(module.exports={Copilot,registerCopilot});
|
package/build/index.mjs
CHANGED
|
@@ -1,54 +1,29 @@
|
|
|
1
|
-
var A=()=>{},
|
|
2
|
-
`);return e[e.length-1].length};var g=(o,e)=>e.getValueInRange({startLineNumber:1,startColumn:1,endLineNumber:o.lineNumber,endColumn:o.column}),
|
|
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,
|
|
1
|
+
var A=()=>{},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 T=(o,e)=>e.getLineContent(o.lineNumber)[o.column-1],w=(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}),I=(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()},oe=(o,e)=>D(o,"GET",e),re=(o,e,t)=>D(o,"POST",{...t,body:e}),P={GET:oe,POST:re};var x=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),l=t.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 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=[],$=(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)},F=()=>{f.length=0};var k={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=T(o,e);return!!t&&!j.has(t)},K=(o,e)=>{let t=w(o,e).trim(),r=d(o,e).trim();return o.column<=3&&(t!==""||r!=="")};var V=(o,e,t,r)=>{let n=(o.match(/\n/g)||[]).length,i=B(o),s=T(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}},Y=(o,e,t)=>new x(o,e).format(t),u=o=>({items:o,enableForwardStability:!0});var y=class{constructor(e,t){this.cursorPosition=e,this.model=t}shouldProvideCompletions(){return!W(this.cursorPosition,this.model)&&!K(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,z=()=>L.getInstance();var E=class E{constructor(){this.logger=z()}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 p=(o,e)=>{b.getInstance().handleError(o,e)};var O="<<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. 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 ${_(o)}`:"",r=J(e);return`The code is written${r?` in ${r}`:""}${t}.`},Z=o=>{let{filename:e,language:t,technologies:r,editorState:{completionMode:n},textBeforeCursor:i,textAfterCursor:s,externalContext:l}=o,a=X(n),c=e?`the file named "${e}"`:"a larger project",m=`You are tasked with ${a} for a code snippet. The code is part of ${c}.
|
|
4
4
|
|
|
5
|
-
|
|
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 (${T}).
|
|
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.
|
|
5
|
+
`;return m+=ie(r,t),m+=`
|
|
18
6
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
Your objective is to:
|
|
22
|
-
1. Analyze the provided code snippet and its context.
|
|
23
|
-
2. Generate a completion that seamlessly integrates with the existing code.
|
|
24
|
-
3. Ensure your completion is syntactically correct and semantically meaningful.
|
|
25
|
-
4. Adhere to the project's coding style and conventions.
|
|
26
|
-
5. Consider the broader context of the file and any provided external context.
|
|
27
|
-
|
|
28
|
-
${ie(r,t)}
|
|
29
|
-
|
|
30
|
-
Specific instructions:
|
|
31
|
-
- The cursor position is marked with '${T}'.
|
|
7
|
+
Here are the details about how the completion should be generated:
|
|
8
|
+
- The cursor position is marked with '${O}'.
|
|
32
9
|
- Your completion must start exactly at the cursor position.
|
|
33
10
|
- Do not repeat any code that appears before or after the cursor.
|
|
34
11
|
- Ensure your completion does not introduce any syntactical or logical errors.
|
|
35
|
-
- If filling in the middle, replace '${
|
|
36
|
-
- If completing the code, start from '${
|
|
37
|
-
-
|
|
38
|
-
- If applicable, use appropriate error handling and follow language-specific best practices.
|
|
39
|
-
- Optimize for readability and performance where possible.
|
|
12
|
+
`,n==="fill-in-the-middle"?m+=` - If filling in the middle, replace '${O}' entirely with your completion.
|
|
13
|
+
`:n==="completion"&&(m+=` - If completing the code, start from '${O}' and provide a logical continuation.
|
|
14
|
+
`),m+=` - Optimize for readability and performance where possible.
|
|
40
15
|
|
|
41
16
|
Remember, output only the necessary completion code without any additional explanations or content.
|
|
42
17
|
|
|
43
18
|
Here's the code snippet for completion:
|
|
44
19
|
|
|
45
20
|
<code>
|
|
46
|
-
${
|
|
47
|
-
</code
|
|
21
|
+
${i}${O}${s}
|
|
22
|
+
</code>`,l&&l.length>0&&(m+=`
|
|
48
23
|
|
|
49
24
|
Additional context from related files:
|
|
50
25
|
|
|
51
|
-
`,
|
|
26
|
+
`,m+=l.map(C=>`// Path: ${C.path}
|
|
52
27
|
${C.content}
|
|
53
28
|
`).join(`
|
|
54
|
-
`)),
|
|
29
|
+
`)),m.endsWith(".")?m:`${m}.`};var se="application/json",ee=async({filename:o,endpoint:e,language:t,technologies:r,externalContext:n,model:i,position:s})=>{try{let{completion:l}=await P.POST(e,{completionMetadata:le({filename:o,position:s,model:i,language:t,technologies:r,externalContext:n})},{headers:{"Content-Type":se},error:"Error while fetching completion item"});return l||null}catch(l){return p(l,"FETCH_COMPLETION_ITEM_ERROR"),null}},le=({filename:o,position:e,model:t,language:r,technologies:n,externalContext:i})=>{let s=ae(e,t),l=g(e,t),a=I(e,t);return{filename:o,language:r,technologies:n,externalContext:i,textBeforeCursor:l,textAfterCursor:a,editorState:{completionMode:s}}},ae=(o,e)=>{let t=g(o,e),r=I(o,e);return t&&r?"fill-in-the-middle":"completion"};var v=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(),n=await P.POST(H,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 p(t,"COPILOT_COMPLETION_FETCH_ERROR"),{error:"Failed to generate completion"}}}createRequestBody(e){return{...U,model:k[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=350,pe=S(ee,me),ce=async({monaco:o,model:e,position:t,token:r,isCompletionAccepted:n,onShowCompletion:i,options:s})=>{if(!new y(t,e).shouldProvideCompletions())return u([]);let l=$(t,e).map(a=>({insertText:a.completion,range:a.range}));if(l.length)return i(),u(l);if(r.isCancellationRequested||n)return u([]);try{let a=await pe({...s,text:e.getValue(),model:e,position:t});if(a){let c=Y(e,t,a),m=new o.Range(t.lineNumber,t.column,t.lineNumber,t.column),C=V(c,m,t,e);return q({completion:c,range:C,textBeforeCursorInLine:d(t,e)}),i(),u([{insertText:c,range:C}])}}catch(a){p(a,"FETCH_COMPLETION_ITEM_ERROR")}return u([])},te=ce;var R=!1,M=!1,de=(o,e,t)=>{let r=[];try{let n=o.languages.registerInlineCompletionsProvider(t.language,{provideInlineCompletions:(s,l,a,c)=>te({monaco:o,model:s,position:l,token:c,isCompletionAccepted:R,onShowCompletion:()=>M=!0,options:t}),freeInlineCompletions:A});r.push(n);let i=e.onKeyDown(s=>{let l=s.keyCode===o.KeyCode.Tab||s.keyCode===o.KeyCode.RightArrow&&s.metaKey;M&&l?(R=!0,M=!1):R=!1});return r.push(i),{deregister:()=>{r.forEach(s=>s.dispose()),F(),R=!1,M=!1}}}catch(n){return p(n,"REGISTER_COPILOT_ERROR"),{deregister:()=>r.forEach(i=>i.dispose())}}};export{v as Copilot,de as registerCopilot};
|